X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=monad_transformers.mdwn;h=26fb13ff9fc47e5f0969e0e41d4ce52bbbf03c3d;hp=b40661a85f3664771b02fafaae3acda905fbaeec;hb=945835525c1a4f9d8210693af0b60ecf957ba8be;hpb=10906eaaea38d130ae301688749a94e6450447c2 diff --git a/monad_transformers.mdwn b/monad_transformers.mdwn index b40661a8..26fb13ff 100644 --- a/monad_transformers.mdwn +++ b/monad_transformers.mdwn @@ -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: