[Haskell-cafe] Anyone mind proofing a short monad transformers explanation?

Jack Kelly endgame.dos at gmail.com
Mon Dec 17 18:38:39 EST 2007

I sent this to a friend who asked me about monad transformers and he 
reckons it should be saved somewhere. If anyone feels up to giving it a 
quick fact-check, that'd be great. I'm not sure what to do with it, 
though. It doesn't seem to be made of the right stuff for a tutorial but 
I'm not sure what to do with it.

--- SNIP ---

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 Identity or IO 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 thread
must be of type IO () (that's because forkIO :: IO () -> IO

Suppose also that this server has some configuration that (in an
imperative program) would be global that the clients all need to

 > data Config = Config Foo Bar Baz

One way of doing this is to use currying and making all the client
threads of type Config -> IO (). Not too nice because any functions
they call have to be passed the Config parameter manually. The Reader
monad solves this problem but we've already got one monad. We need to
wrap IO in a ReaderT. The type constructor for ReaderT is ReaderT r m
a, with r the shared environment to read from, m the inner monad and a
the return type. Our client_func becomes:

 > client_func :: ReaderT Config IO ()

We can then use the ask, asks and local functions as if Reader was the
only Monad:

(these examples are inside do blocks)

 > p <- asks port

(Assuming some function port :: Config -> Int or similar.)

To do stuff in IO (or in the general case any inner monad) the liftIO
function is used to make an IO function work in this wrapped space:

(given h :: Handle, the client's handle)

 > liftIO $ hPutStrLn h "You lose"
 > liftIO $ hFlush h

IO is once again special. For other inner monads, the lift function
does the same thing. Note also that IO has no transformer and must
therefore always be the innermost monad.

This is all well and good, but the client_func now has type ReaderT
Config IO () and forkIO needs a function of type IO (). The escape
function for Reader is runReader :: Reader r a -> r -> a and similarly
for ReaderT the escape function is runReaderT :: ReaderT r m a -> r ->
m a:

(Given some c :: Config that's been assembled from config files or the

 > forkIO (runReaderT client_func c)

Will do the trick.

Monad transformers are like onions. They make you cry but 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 IO (), which lets us make haskell do something.

More information about the Haskell-Cafe mailing list