(no commit message)
[lambda.git] / monad_transformers.mdwn
index 086a2ff..26fb13f 100644 (file)
@@ -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;;