From b1d420acee7b904af41aabe6db71e872baf251f5 Mon Sep 17 00:00:00 2001 From: Jim Pryor Date: Thu, 2 Dec 2010 10:55:11 -0500 Subject: [PATCH] manip trees tweaks Signed-off-by: Jim Pryor --- manipulating_trees_with_monads.mdwn | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/manipulating_trees_with_monads.mdwn b/manipulating_trees_with_monads.mdwn index 10c95564..445722be 100644 --- a/manipulating_trees_with_monads.mdwn +++ b/manipulating_trees_with_monads.mdwn @@ -397,7 +397,7 @@ that is intended to represent non-deterministic computations as a tree. What's this have to do with tree\_mondadize? -------------------------------------------- -So we've defined a Tree monad +So we've defined a Tree monad: type 'a tree = Leaf of 'a | Node of ('a tree) * ('a tree);; let tree_unit (a: 'a) : 'a tree = Leaf a;; @@ -417,7 +417,7 @@ What's this have to do with the `tree_monadize` functions we defined earlier? ... and so on for different monads? -The answer is that each of those `tree_monadize` functions is adding a Tree monad *layer* to a pre-existing Reader (and so on) monad. So far, we've defined monads as single-layered things. (Though in the Groenendijk, Stokhoff, and Veltmann application, we had to figure out how to combine Reader, State, and Set monads in an ad-hoc way.) But in practice, one often wants to combine the abilities of several monads. Corresponding to each monad like Reader, there's a corresponding ReaderT **monad transformer**. That takes an existing monad M and adds a Reader monad layer to it. The way these are defined parallels the way the single-layer versions are defined. For example, here's the Reader monad: +The answer is that each of those `tree_monadize` functions is adding a Tree monad *layer* to a pre-existing Reader (and so on) monad. So far, we've defined monads as single-layered things. Though in the Groenendijk, Stokhoff, and Veltmann homework, we had to figure out how to combine Reader, State, and Set monads in an ad-hoc way. In practice, one often wants to combine the abilities of several monads. Corresponding to each monad like Reader, there's a corresponding ReaderT **monad transformer**. That takes an existing monad M and adds a Reader monad layer to it. The way these are defined parallels the way the single-layer versions are defined. For example, here's the Reader monad: (* monadic operations for the Reader monad *) @@ -428,18 +428,18 @@ The answer is that each of those `tree_monadize` functions is adding a Tree mona 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)` 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. 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 *) (* We're not giving valid OCaml code, but rather something * that's conceptually easier to digest. - * How you really need to write this in OCaml is more circuitious... + * How you really need to write this in OCaml is more circuitous... * see http://lambda.jimpryor.net/code/tree_monadize.ml for some details. *) type ('a, M) readerT = env -> 'a M;; - (* this is just an 'a M reader; but that doesn't generalize *) + (* this is just an 'a M reader; but don't rely on that pattern to generalize *) let unit (a : 'a) : ('a, M) readerT = fun e -> M.unit a;; @@ -447,7 +447,7 @@ 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 return `M.unit a`. Where before we just supplied a 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: -- 2.11.0