+ .
+ ___|___
+ | |
+ . .
+ ( _|__ _|__ , 5 )
+ | | | |
+ 2 3 5 .
+ _|__
+ | |
+ 7 11
+
+Note that the value returned is a pair consisting of a tree and an
+integer, 5, which represents the count of the leaves in the tree.
+
+Why does this work? Because the operation `incrementer`
+takes an argument `a` and wraps it in an State monadic box that
+increments the store and leaves behind a wrapped `a`. When we give that same operations to our
+`tree_monadize` function, it then wraps an `int tree` in a box, one
+that does the same store-incrementing for each of its leaves.
+
+We can use the state monad to annotate leaves with a number
+corresponding to that leave's ordinal position. When we do so, we
+reveal the order in which the monadic tree forces evaluation:
+
+ # tree_monadize (fun a -> fun s -> ((a,s+1), s+1)) t1 0;;
+ - : int tree * int =
+ (Node
+ (Node (Leaf (2, 1), Leaf (3, 2)),
+ Node
+ (Leaf (5, 3),
+ Node (Leaf (7, 4), Leaf (11, 5)))),
+ 5)
+
+The key thing to notice is that instead of just wrapping `a` in the
+monadic box, we wrap a pair of `a` and the current store.
+
+Reversing the annotation order requires reversing the order of the `state_bind`
+operations. It's not obvious that this will type correctly, so think
+it through:
+
+ let rec tree_monadize_rev (f : 'a -> 'b state) (t : 'a tree) : 'b tree state =
+ match t with
+ | Leaf a -> state_bind (f a) (fun b -> state_unit (Leaf b))
+ | Node (l, r) -> state_bind (tree_monadize f r) (fun r' -> (* R first *)
+ state_bind (tree_monadize f l) (fun l'-> (* Then L *)
+ state_unit (Node (l', r'))));;
+
+ # tree_monadize_rev (fun a -> fun s -> ((a,s+1), s+1)) t1 0;;
+ - : int tree * int =
+ (Node
+ (Node (Leaf (2, 5), Leaf (3, 4)),
+ Node
+ (Leaf (5, 3),
+ Node (Leaf (7, 2), Leaf (11, 1)))),
+ 5)