manip trees tweaks
[lambda.git] / manipulating_trees_with_monads.mdwn
index 1609763..445722b 100644 (file)
@@ -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:
 
@@ -498,7 +498,7 @@ Okay, now let's do the same thing for our Tree monad.
 
        let rec bind (u : 'a tree) (f : 'a -> 'b tree) : 'b tree =
            match u with
-           | Leaf a -> f a
+           | Leaf a -> (fun b -> Leaf b) (f a)
            | Node (l, r) -> (fun l' r' -> Node (l', r')) (bind l f) (bind r f);;
 
        (* monadic operations for the TreeT monadic transformer *)
@@ -511,7 +511,7 @@ Okay, now let's do the same thing for our Tree monad.
 
        let rec bind (u : ('a, M) tree) (f : 'a -> ('b, M) tree) : ('b, M) tree =
            match u with
-           | Leaf a -> M.unit (f a)
+           | Leaf a -> M.bind (f a) (fun b -> M.unit (Leaf b))
            | Node (l, r) -> M.bind (bind l f) (fun l' ->
                                                        M.bind (bind r f) (fun r' ->
                                                                M.unit (Node (l', r'));;