+
+
+<!--
+With streams in hand, we need only rewrite our continuation tree
+monadizer so that instead of mapping trees to lists, it maps them to
+streams. Instead of
+
+ # tree_monadize (fun a k -> a :: k a) t1 (fun t -> []);;
+ - : int list = [2; 3; 5; 7; 11]
+
+as above, we have
+
+ # tree_monadize (fun i k -> Next (i, fun () -> k ())) t1 (fun _ -> End);;
+ - : int stream = Next (2, <fun>)
+
+We can see the first element in the stream, the first leaf (namely,
+2), but in order to see the next, we'll have to force a thunk.
+
+Then to complete the same-fringe function, we simply convert both
+trees into leaf-streams, then compare the streams element by element.
+The code is entirely routine, but for the sake of completeness, here it is:
+
+ let rec compare_streams stream1 stream2 =
+ match stream1, stream2 with
+ | End, End -> true (* Done! Fringes match. *)
+ | Next (next1, rest1), Next (next2, rest2) when next1 = next2 -> compare_streams (rest1 ()) (rest2 ())
+ | _ -> false;;
+
+ let same_fringe t1 t2 =
+ let stream1 = tree_monadize (fun i k -> Next (i, fun () -> k ())) t1 (fun _ -> End) in
+ let stream2 = tree_monadize (fun i k -> Next (i, fun () -> k ())) t2 (fun _ -> End) in
+ compare_streams stream1 stream2;;
+
+Notice the forcing of the thunks in the recursive call to
+`compare_streams`. So indeed:
+
+ # same_fringe ta tb;;
+ - : bool = true
+ # same_fringe ta tc;;
+ - : bool = false
+
+Now, you might think that this implementation is a bit silly, since in
+order to convert the trees to leaf streams, our `tree_monadizer`
+function has to visit every node in the tree, so we'd have to traverse
+the entire tree at some point. But you'd be wrong: part of what gets
+suspended in the thunking of the stream is the computation of the rest
+of the monadized tree.
+
+-->