[Haskell-cafe] Question of a manual computation on Reader Monad.

Malte Milatz malte at gmx-topmail.de
Sat Aug 25 10:45:37 EDT 2007


Peter Cai:
> Hi all,
> 
> In order to improve my understanding of monad, I am trying to do some manual
> computation on "Reader Monad" but I got some problem.
> 
> The computation is like this:
> 
> --instance Monad (Reader e) where
> --    return a         = Reader $ \e -> a
> --    (Reader r) >>= f = Reader $ \e -> f (r e) e

This cannot work out because this definition is ill-typed, which you
will notice if you try to compile it (or if you contemplate about what
the type of (f (r e) e) is). Here is a way to define a reader monad:

newtype Reader e a = Reader { runReader :: (e -> a) }

instance Monad (Reader e) where
    return a        = Reader $ \_ -> a
    Reader r >>= f  = Reader $ \e -> runReader (f (r e)) e

You can then do your calculation like this:

    Reader show >>= return
=   Reader $ \e -> runReader (return (show e)) e
=   Reader $ \e -> runReader (Reader (\_ -> show e)) e
=   Reader $ \e -> (\_ -> show e) e
=   Reader $ \e -> show e
=   Reader show;

    -- Note that, except for the use of “show” as a specific
    -- example, the above is equivalent to proving that the
    -- second monad law holds for (Reader e).

    runReader (Reader show >>= return) 
=   runReader (Reader show)
=   show.

Malte


More information about the Haskell-Cafe mailing list