Mon Oct 24 22:30:34 EDT 2011

Monad transformers

Two questions:

1. How can it be?

Following [1].  Dropping wrappers for clarity, we have:

  StateT s m a  =  s -> m (s, a)

which is a state monad parameterized by a monad.  If m = Id we
get the ordinary state monad:

                   s -> (s, a)

if two of these are chained we get

  StateT t (StateT s Id) a =

                   t -> (StateT s Id) (t, a)
                   t -> s -> (s, (t, a))

So the deal is: a monad transformer is parameterized by a monad in
such a way that simply substituting a different type will give a
meaningful result.

2. How can it be used?

Each transformer needs to define a method that provides access to the
wrapped monad:

  lift :: (MonadTrans t, Monad m) => m a -> t m a

It looks like the MTL has some type classes defined that prevent the
use of multiple lift applications to dig into the onion[2].

[1] http://web.cecs.pdx.edu/~mpj/pubs/modinterp.html
[2] http://www.haskell.org/haskellwiki/Monad_Transformers_Explained