[Haskell-cafe] Stacking monads

David Menendez dave at zednenem.com
Thu Oct 2 14:11:44 EDT 2008

On Thu, Oct 2, 2008 at 1:18 PM, Andrew Coppin
<andrewcoppin at btinternet.com> wrote:
> 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... I'd really
> prefer to just layer error handling on the top. But I just can't get it to
> work right. It's soooo fiddly untangling the multiple monads to try to *do*
> any useful work.
> Does anybody have any idea how this whole monad stacking craziness is
> *supposed* to work?

In general, monads don't compose. That is, there's no foolproof way to
take two monads m1 and m2 and create a third monad m3 which does
everything m1 and m2 does. People mostly get around that by using
monad transformers.

You could try using an exception monad transformer here, but that
won't give you the same semantics. "ErrorT ErrorType ResultSet a" is
isomorphic to "ResultSet (Either ErrorType a)".

If you must have something equivalent to Either ErrorType (ResultSet
a), you either need to (1) redesign ResultSet to include error
handling, (2) redesign ResultSet to be a monad transformer, or (3)
restrict yourself to the operations in Applicative.

Option (3) works because applicative functors *do* compose. (Also,
every instance of Monad is trivially an instance of Applicative.)

Dave Menendez <dave at zednenem.com>

More information about the Haskell-Cafe mailing list