[Haskell-cafe] Stacking monads

Andrew Coppin andrewcoppin at btinternet.com
Thu Oct 2 15:53:24 EDT 2008

Jonathan Cast wrote:
> On Thu, 2008-10-02 at 18:18 +0100, Andrew Coppin wrote:
>> After an insane amount of time making my head hurt, I disocvered that 
>> the type "Either ErrorType (ResultSet State)" is actually a monad.
> It's a monad if you can write a function
> join :: Either ErrorType (ResultSet (Either ErrorType (ResultSet alpha)))
>      -> Either ErrorType (ResultSet alpha)
> (which follows from being able to write a function
> interleave :: Either ErrorType (ResultSet alpha)
>            -> ResultSet (Either ErrorType alpha)
> satisfying certain laws).  Otherwise not, as you noticed.

Er... OK. Yes, I guess that kind of makes sense...

>> Since ResultSet *just happens* to also be in Functor,
> It doesn't just happen to be one.  liftM is *always* a law-abiding
> definition for fmap, when used at a law-abiding monad.

I'm lost...

(What does liftM have to do with fmap?)

> (This is why
> posters here are always bringing up head-hurting category theory, btw.
> Absorbing it sufficiently actually teaches you useful things about
> Haskell programming.)

That would be a surprising and unexpected result. After all, knowing 
about set theory doesn't help you write SQL...

>> At this point I am sorely tempted to just change ResultSet to include 
>> the error functionallity I need. However, ResultSet is *already* an 
>> extremely complicated monad that took me weeks to get working 
>> correctly...
> What does it look like?

A list, basically. (But obviously slightly more complicated than that.)

> Quite possibly it can be factored out into
> smaller pieces using monad transformers.  (In which case adding error
> handling is just sticking in another transformer at the right layer in
> the stack --- that is, the layer where adding error handling works :).

Well I'm *already* trying to layer an error transformer on the top and 
it's failing horribly. I don't see how splitting things up even more 
could do anything but make the program even *more* complex.

>> Does anybody have any idea how this whole monad stacking craziness is 
>> *supposed* to work?
> No. [1]

Ah, good. :-)

> But we know how it *can* work; this is what monad transformers exist to
> do.  You want to either change ResultSet to be a monad transformer,
> or
> you want the monad ErrorT ErrorType ResultSet.  Very little can be said
> in general without knowing what ResultSet looks like.

I thought ErrorT was a class name...?

More information about the Haskell-Cafe mailing list