X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=monad_transformers.mdwn;h=b72c035cf5ec1a93e3e97a3b9a07dbdba0a6ff52;hp=efe35b2871d55dec0685839ad1d17bc86b71521b;hb=9fe62083953213cce34fc4458e36666902c5ee4b;hpb=0638e3b38097e20ebf645fe7cb1ecec4f17aef7a diff --git a/monad_transformers.mdwn b/monad_transformers.mdwn index efe35b28..b72c035c 100644 --- a/monad_transformers.mdwn +++ b/monad_transformers.mdwn @@ -10,7 +10,12 @@ So far, we've defined monads as single-layered things. Though in the Groenendijk let bind (u: 'a reader) (f : 'a -> 'b reader) : 'b reader = fun e -> (fun v -> f v e) (u e);; -We've just beta-expanded the familiar `f (u e) e` into `(fun v -> f v e) (u e)`, in order to factor out the parts where any Reader monad is being supplied as an argument to another function. Then if we want instead to add a Reader layer to some arbitrary other monad M, with its own M.unit and M.bind, here's how we do it: +We've just beta-expanded the familiar `f (u e) e` into `(fun v -> f v +e) (u e)`, in order to factor out the parts where any Reader monad is +being supplied as an argument to another function, as illustrated in +the `bind` function in the following example. Then if we want instead +to add a Reader layer to some arbitrary other monad M, with its own +M.unit and M.bind, here's how we do it: (* monadic operations for the ReaderT monadic transformer *) @@ -29,7 +34,11 @@ We've just beta-expanded the familiar `f (u e) e` into `(fun v -> f v e) (u e)`, let bind (u : ('a, M) readerT) (f : 'a -> ('b, M) readerT) : ('b, M) readerT = fun e -> M.bind (u e) (fun v -> f v e);; -Notice the key differences: where before we just returned `a`, now we instead return `M.unit a`. Where before we just supplied value `u e` of type `'a reader` as an argument to a function, now we instead `M.bind` the `'a reader` to that function. Notice also the differences in the types. +Notice the key differences: where before we just returned `a`, now we +instead return `M.unit a`. Where before we just supplied value `u e` +of type `'a reader` as an argument to a function, now we instead +`M.bind` the `'a reader` to that function. Notice also the differences +in the types. What is the relation between Reader and ReaderT? Well, suppose you started with the Identity monad: