+<!--
+More complex: here we use the monadic `list_reader` or `tree_reader` we got back from `distribute` and `bind` it to other operations:
+
+ # let u = LR.distribute (fun i -> R.(asks (fun e -> e i))) l1 in
+ LR.(run(u >>= fun i -> plus (unit i) (unit (10*i)))) (fun i -> i + i);;
+ - : int list = [4; 40; 6; 60; 10; 100; 14; 140; 22; 220]
+ # let v = TR.distribute (fun i -> R.(asks (fun e -> e i))) t1 in
+ TR.(run(v >>= fun i -> plus (unit i) (unit (10*i)))) (fun i -> i + i);;
+ - : int T.tree option =
+ Some
+ (T.Node
+ (T.Node (T.Node (T.Leaf 4, T.Leaf 40), T.Node (T.Leaf 6, T.Leaf 60)),
+ T.Node
+ (T.Node (T.Leaf 10, T.Leaf 100),
+ T.Node (T.Node (T.Leaf 14, T.Leaf 140), T.Node (T.Leaf 22, T.Leaf 220)))))
+-->
+
+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:
+
+ # let tree_annotater = TS.distribute (fun i -> S.(puts succ >> get >>= fun s -> unit (i,s))) t1 in
+ TS.run tree_annotater 0;;
+ - : (int * S.store) T.tree option * S.store =
+ (Some
+ (T.Node
+ (T.Node (T.Leaf (2, 1), T.Leaf (3, 2)),
+ T.Node (T.Leaf (5, 3), T.Node (T.Leaf (7, 4), T.Leaf (11, 5))))),
+ 5)
+
+Here's a comparison of how distribute works for trees and how it works for lists:
+
+ # module LS = L.T(S);;
+
+ # let list_counter = LS.distribute (fun i -> S.(puts succ >> unit i)) l1 in
+ LS.run list_counter 0;;
+ - : int list * S.store = ([2; 3; 5; 7; 11], 5)
+
+ # let list_annotater = LS.distribute (fun i -> S.(puts succ >> get >>= fun s -> unit (i,s) )) l1 in
+ LS.run list_annotater 0;;
+ - : (int * S.store) list * S.store =
+ ([(2, 1); (3, 2); (5, 3); (7, 4); (11, 5)], 5)
+
+
+<!--
+# let u = LS.distribute (fun i -> if i = -1 then S.get else if i < 0 then S.(puts succ >> unit 0) else S.unit i) [10;-1;-2;-1;20] in
+ LS.run u 0;;
+- : S.store list * S.store = ([10; 0; 0; 1; 20], 1)
+-->
+
+
+We can use TreeT(List) to copy the tree with different choices for some of the leaves:
+
+ # let tree_chooser = TL.distribute (fun i -> L.(if i = 2 then plus (unit 20) (unit 21) else unit i)) t1;;
+ # TL.run tree_chooser;;
+ - : ('_a, int) TL.result =
+ [Some
+ (T.Node
+ (T.Node (T.Leaf 20, T.Leaf 3),
+ T.Node (T.Leaf 5, T.Node (T.Leaf 7, T.Leaf 11))));
+ Some
+ (T.Node
+ (T.Node (T.Leaf 21, T.Leaf 3),
+ T.Node (T.Leaf 5, T.Node (T.Leaf 7, T.Leaf 11))))]
+
+
+Finally, we use TreeT(Continuation) to do various things. For reasons I won't explain here, the library currently requires you to run the Tree-plus-Continuation bundle using a different sequence of `run` commands:
+
+We can do nothing:
+<!--
+let initial_continuation = fun t -> t in
+TreeCont.monadize Continuation_monad.unit t1 initial_continuation;;
+-->
+
+ # C.run_exn TC.(run (distribute C.unit t1)) (fun t -> t);;
+ - : int T.tree option =
+ Some
+ (T.Node
+ (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]].
+<!--
+let initial_continuation = fun t -> t in
+TreeCont.monadize (fun a k -> k (a*a)) t1 initial_continuation;;
+-->
+
+ # C.run_exn TC.(run (distribute C.(fun a -> shift (fun k -> k (a*a))) t1)) (fun t -> t);;
+ - : int T.tree option =
+ Some
+ (T.Node
+ (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:
+<!--
+let initial_continuation = fun t -> 0 in
+TreeCont.monadize (fun a k -> 1 + k a) t1 initial_continuation;;
+-->
+
+ # C.run_exn TC.(run (distribute C.(fun a -> shift (fun k -> k a >>= fun v -> unit (1+v))) t1)) (fun t -> 0);;
+ - : int = 5
+
+
+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;;
+-->
+
+ # C.run_exn TC.(run (distribute C.(fun a -> shift (fun k -> k a >>= fun v -> unit (a::v))) t1)) (fun t -> []);;
+ - : int list = [2; 3; 5; 7; 11]