cps tweak, another Seasoned cite
[lambda.git] / monad_transformers.mdwn
index b40661a..26fb13f 100644 (file)
@@ -306,8 +306,8 @@ Our Tree monad has a corresponding TreeT transformer. Simplified, its implementa
                                  reader_unit (Node(ls, rs))))
                in loop us);;
 
-       let tree_reader_elevate (w : 'a reader) : 'a tree_reader =
-               reader_bind w (fun a -> reader_unit (Leaf a))
+       let tree_reader_elevate (inner : 'a reader) : 'a tree_reader =
+               reader_bind inner (fun a -> reader_unit (Leaf a))
 
 Recall our earlier definition of `tree_monadize`, specialized for the Reader monad:
 
@@ -317,9 +317,9 @@ Recall our earlier definition of `tree_monadize`, specialized for the Reader mon
                        (* the next line is equivalent to: tree_reader_elevate (f a) *)
                reader_bind (f a) (fun b -> reader_unit (Leaf b))
            | Node (l, r) ->
-               reader_bind (tree_monadize f l) (fun l' ->
-                 reader_bind (tree_monadize f r) (fun r' ->
-                   reader_unit (Node (l', r'))));;
+               reader_bind (tree_monadize f l) (fun ls ->
+                 reader_bind (tree_monadize f r) (fun rs ->
+                   reader_unit (Node (ls, rs))));;
 
 We rendered the result type here as `'b tree reader`, as we did in our earlier discussion, but as we can see from the above implementation of TreeT(Reader), that's the type of an `'b tree_reader`, that is, of a layered box consisting of TreeT packaging wrapped around an inner Reader box.
 
@@ -347,13 +347,11 @@ We can use TreeT(Reader) to modify leaves:
 
        # let tree_reader = TR.distribute (fun i -> R.asks (fun e -> e i)) t1;;
        # TR.run tree_reader (fun i -> i+i);;
-       (*
        - : int T.tree option =
        Some
         (T.Node
           (T.Node (T.Leaf 4, T.Leaf 6),
                T.Node (T.Leaf 10, T.Node (T.Leaf 14, T.Leaf 22))))
-       *)
 
 Here's a comparison of how distribute works for trees and how it works for lists:
 
@@ -383,14 +381,12 @@ We can use TreeT(State) to count leaves:
 
        # let tree_counter = TS.distribute (fun i -> S.(puts succ >> unit i)) t1 in
          TS.run tree_counter 0;;
-       (*
        - : int T.tree option * S.store =
        (Some
          (T.Node
                (T.Node (T.Leaf 2, T.Leaf 3),
                 T.Node (T.Leaf 5, T.Node (T.Leaf 7, T.Leaf 11)))),
         5)
-       *)
 
 or to annotate leaves:
 
@@ -454,7 +450,7 @@ TreeCont.monadize Continuation_monad.unit t1 initial_continuation;;
           (T.Node (T.Leaf 2, T.Leaf 3),
                T.Node (T.Leaf 5, T.Node (T.Leaf 7, T.Leaf 11))))
 
-We can square each leaf. The meaning of `shift` will be explained in [[CPS and Continuation Operators]].
+We can square each leaf:
 <!--
 let initial_continuation = fun t -> t in
 TreeCont.monadize (fun a k -> k (a*a)) t1 initial_continuation;;
@@ -467,7 +463,13 @@ TreeCont.monadize (fun a k -> k (a*a)) t1 initial_continuation;;
           (T.Node (T.Leaf 4, T.Leaf 9),
                T.Node (T.Leaf 25, T.Node (T.Leaf 49, T.Leaf 121))))
 
-We can count the leaves:
+The meaning of `shift` will be explained in [[CPS and Continuation Operators]]. Here you should just regard it as a primitive operation in our Continuation monad. In [this code](/code/tree_monadize.ml) you could simply write:
+
+       TreeCont.monadize (fun a -> fun k -> k (a*a)) t1 (fun t -> t);;
+
+But because of the way our monad library hides the underlying machinery, here you can no longer just say `fun k -> k (a*a)`; you have to say `shift (fun k -> k (a*a))`.
+
+Moving on, we can count the leaves:
 <!--
 let initial_continuation = fun t -> 0 in
 TreeCont.monadize (fun a k -> 1 + k a) t1 initial_continuation;;
@@ -477,7 +479,7 @@ TreeCont.monadize (fun a k -> 1 + k a) t1 initial_continuation;;
        - : int = 5
 
 
-We can convert the tree to a list of leaves:
+And we can convert the tree to a list of leaves:
 <!--
 let initial_continuation = fun t -> [] in
 TreeCont.monadize (fun a k -> a :: k a) t1 initial_continuation;;