Latest revision as of 13:16, 5 November 2012
Below is a short explanation of and motivating example for monad transformers. Comments and aggressive editing welcome.
Thanks to Ryan Ingram and Tommy M McGuire of Haskell-Cafe for their comments on an earlier draft.
Basically it's like making a double, triple, quadruple, ... monad by wrapping around existing monads that provide wanted functionality.
You have an innermost monad (usually
but you can use any monad). You then wrap monad transformers around this monad to make bigger, better monads.
Concrete example: Suppose I was writing a server. Each client handling thread must be of type
forkIO :: IO () -> IO ThreadID
Suppose also that this server has some configuration that (in an imperative program) would be global because client handling threads all need to query it.
data Config = Config Foo Bar Baz
One way of doing this is to use currying and making all the client threads of type
. Not too nice because any functions they call have to be passed the
parameter manually. The
monad solves this problem but we've already got one monad. We need to wrap
. The type constructor for
the shared environment to read from,
the inner monad and
the return type. Our
client_func :: ReaderT Config IO ()
We can then use the
functions as if Reader was the only Monad:
(these examples are inside
(Assuming some function
To do stuff in an inner monad, there is the function
. Because having big chains of
can get annoying, the libraries in mtl (the Monad Transformer Library) play a trick using the type system. Each monad in the mtl is defined in terms of a type class. For example,
is actually an instance of
, as is
. Further, anything in the mtl that wraps a
is also set up to be a
, so the
functions will work without any (manual) lifting. Other mtl monads behave in a similar way.
To do stuff in
function is used:
, the client's handle)
liftIO $ hPutStrLn h "You win"
liftIO $ hFlush h
A type class called
is used to implement a similar trick as above.
is an instance of
, and any mtl transformer that wraps a
instance also is an instance of
. This means that
functions need only use
and not a big chain of
s. Note also that
has no transformer and must therefore always be the innermost monad.
This is all well and good, but the
now has type
needs a function of type
. The escape function for
runReader :: Reader r a -> r -> a
and similarly for
the escape function is
runReaderT :: ReaderT r m a -> r -> m a
that's been assembled from config files or the like)
forkIO (runReaderT client_func c)
Will do the trick.
Monad transformers are like onions. At first, they make you cry but then you learn to appreciate them. Like onions, they're also made of layers. Each layer is the functionality of a new monad, you lift monadic functions to get into the inner monads and you have transformerised functions to unwrap each layer. They're also like a present in that regard: in this example we unwrapped the outer wrapping paper to get to the present: an object of type
, which lets us make Haskell do something.