On Jun 17, 2009, at 11:54 , .shawn wrote:
mapTreeM action (Leaf a) = do
    lift (putStrLn ("Leaf" ++ show a))
    b <- action a
    return (Leaf b)

mapTreeM :: (MonadTrans t, Monad (t IO), Show a) => (a -> t IO a1) - > Tree a -> t IO (Tree a1)

Why does the type signature of mapTreeM look like this?

A monad transformer "wraps" one monad around another. To get at the wrapped monad, you use (lift). The context (MonadTrans t) specifies that the type (t) is a monad transformer, meaning it provides (lift). The type (t) actually looks like (StateT s), but this isn't needed to write (mapTreeM) (it will, however, be needed when you write functions to be passed as (action)).

The context (Monad (t IO)), given the MonadTrans context above, then declares that the transformer (t) is wrapped around the IO monad, so Haskell knows what you are (lift)ing *to*.

You use this when you want to build a monad based on IO but with extra functionality, such as (StateT IO) which wraps an IO monad in a StateT transformer so you can carry around extra information in the state.

--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allb...@kf8nh.com
system administrator [openafs,heimdal,too many hats] allb...@ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH


Attachment: PGP.sig
Description: This is a digitally signed message part

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to