[<<][compsci][>>][..]
Sun Feb 19 09:58:44 EST 2012

Monad transformers

Forget the creative forking of last post, I'm going to use Reader +
State.  I had to write a small example program to understand the
wrapping / unwrapping mechanism.


> f :: s -> M s
> f = undefined

which is wrapped in this monad onion:

> type M = ReaderT String (StateT Int Identity)

Given a value :: s and the function :: s -> M s, we can unwrap one
layer at a time.  First peel off the ReaderT, then the StateT and last
the Identity.

> run s = s' where
>   mStateT  = runReaderT (f s) "Context"
>   mIdentity = runStateT mStateT 123
>   Identity (s', _) = mIdentity


ACCESS:

> getInt :: M Int
> getInt = lift get

> getString :: M String
> getString = ask

(Check these later; timing out..)

[1] http://hackage.haskell.org/packages/archive/mtl/2.0.1.0/doc/html/Control-Monad-Reader.html
[2] http://hackage.haskell.org/packages/archive/mtl/2.0.1.0/doc/html/Control-Monad-State.html
[3] http://cvs.haskell.org/Hugs/pages/libraries/mtl/Control-Monad-State.html
[4] http://cvs.haskell.org/Hugs/pages/libraries/mtl/Control-Monad-Reader.html



[Reply][About]
[<<][compsci][>>][..]