017a8d36797d501434775449c54cbfb28d09593b
[lambda.git] / exercises / _assignment12.mdwn
1 ## Same-fringe using zippers ##
2
3 Recall back in [[Assignment 4|assignment4#fringe]], we asked you to enumerate the "fringe" of a leaf-labeled tree. Both of these trees (here I *am* drawing the labels in the diagram):
4
5             .                .
6            / \              / \
7           .   3            1   .
8          / \                  / \
9         1   2                2   3
10
11 have the same fringe: `[1; 2; 3]`. We also asked you to write a function that determined when two trees have the same fringe. The way you approached that back then was to enumerate each tree's fringe, and then compare the two lists for equality. Today, and then again in a later class, we'll encounter new ways to approach the problem of determining when two trees have the same fringe.
12
13
14 Supposing you did work out an implementation of the tree zipper, then one way to determine whether two trees have the same fringe would be: go downwards (and leftwards) in each tree as far as possible. Compare the focused leaves. If they're different, stop because the trees have different fringes. If they're the same, then for each tree, move rightward if possible; if it's not (because you're at the rightmost leaf in a subtree), move upwards then try again to move rightwards. Repeat until you are able to move rightwards. Once you do move rightwards, go downwards (and leftwards) as far as possible. Then you'll be focused on the next leaf in the tree's fringe. The operations it takes to get to "the next leaf" may be different for the two trees. For example, in these trees:
15
16             .                .
17            / \              / \
18           .   3            1   .
19          / \                  / \
20         1   2                2   3
21
22 you won't move upwards at the same steps. Keep comparing "the next leaves" until they are different, or you exhaust the leaves of only one of the trees (then again the trees have different fringes), or you exhaust the leaves of both trees at the same time, without having found leaves with different labels. In this last case, the trees have the same fringe.
23
24 If your trees are very big---say, millions of leaves---you can imagine how this would be quicker and more memory-efficient than traversing each tree to construct a list of its fringe, and then comparing the two lists so built to see if they're equal. For one thing, the zipper method can abort early if the fringes diverge early, without needing to traverse or build a list containing the rest of each tree's fringe.
25
26 Let's sketch the implementation of this. We won't provide all the details for an implementation of the tree zipper (you'll need to fill those in), but we will sketch an interface for it.
27
28 In these exercises, we'll help ourselves to OCaml's **record types**. These are nothing more than tuples with a pretty interface. Instead of saying:
29
30     # type blah = Blah of (int * int * (char -> bool));;
31
32 and then having to remember which element in the triple was which:
33
34     # let b1 = Blah (1, (fun c -> c = 'M'), 2);;
35     Error: This expression has type int * (char -> bool) * int
36     but an expression was expected of type int * int * (char -> bool)
37     # (* damnit *)
38     # let b1 = Blah (1, 2, (fun c -> c = 'M'));;
39     val b1 : blah = Blah (1, 2, <fun>)
40
41 records let you attach descriptive labels to the components of the tuple:
42
43     # type blah_record = { height : int; weight : int; char_tester : char -> bool };;
44     # let b2 = { height = 1; weight = 2; char_tester = (fun c -> c = 'M') };;
45     val b2 : blah_record = {height = 1; weight = 2; char_tester = <fun>}
46     # let b3 = { height = 1; char_tester = (fun c -> c = 'K'); weight = 3 };; (* also works *)
47     val b3 : blah_record = {height = 1; weight = 3; char_tester = <fun>}
48
49 These were the strategies to extract the components of an unlabeled tuple:
50
51     let h = fst some_pair;; (* accessor functions fst and snd are only predefined for pairs *)
52
53     let (h, w, test) = b1;; (* works for arbitrary tuples *)
54
55     match b1 with
56     | (h, w, test) -> ...;; (* same as preceding *)
57
58 Here is how you can extract the components of a labeled record:
59
60     let h = b2.height;; (* handy! *)
61
62     let {height = h; weight = w; char_tester = test} = b2 in
63     (* go on to use h, w, and test ... *)
64
65     match test with
66     | {height = h; weight = w; char_tester = test} ->
67     (* same as preceding *)
68
69 Anyway, using record types, we might define the tree zipper interface like so. First, we define a type for leaf-labeled, binary trees:
70
71     type 'a tree = Leaf of 'a | Node of ('a tree * 'a tree)
72
73 Next, the types for our tree zippers:
74
75     type 'a zipper = { in_focus: 'a tree; context : 'a context }
76     and 'a context = Root | Nonroot of 'a nonroot_context
77     and 'a nonroot_context = { up : 'a context; left: 'a tree option; right: 'a tree option }
78
79 Unlike in seminar, here we represent the siblings as `'a tree option`s rather than `'a tree list`s. Since we're dealing with binary trees, each context will have exactly one sibling, either to the right or to the left.
80
81 The following function takes an `'a tree` and returns an `'a zipper` focused on its root:
82
83     let new_zipper (t : 'a tree) : 'a zipper =
84       {in_focus = t; context = Root}
85
86 Here are the beginnings of functions to move from one focused tree to another:
87
88     let rec move_botleft (z : 'a zipper) : 'a zipper =
89       (* returns z if the focused node in z has no children *)
90       (* else returns move_botleft (zipper which results from moving down from z's focused node to its leftmost child) *)
91       _____ (* YOU SUPPLY THE DEFINITION *)
92
93 <!--
94     match z.in_focus with
95     | Leaf _ -> z
96     | Node(left, right) ->
97         move_botleft {in_focus = left; context = Nonroot {up = z.context; left = None; right = Some right}}
98 -->
99
100
101     let rec move_right_or_up (z : 'a zipper) : 'a zipper option =
102       (* if it's possible to move right in z, returns Some (the result of doing so) *)
103       (* else if it's not possible to move any further up in z, returns None *)
104       (* else returns move_right_or_up (result of moving up in z) *)
105       _____ (* YOU SUPPLY THE DEFINITION *)
106
107 <!--
108     match z.context with
109     | Nonroot {up; left= None; right = Some right} ->
110         Some {in_focus = right; context = Nonroot {up; left = Some z.in_focus; right = None}}
111     | Root -> None
112     | Nonroot {up; left = Some left; right = None} ->
113         move_right_or_up {in_focus = Node(left, z.in_focus); context = up}
114 -->
115
116
117 1.  Your first assignment is to complete the definitions of `move_botleft` and `move_right_or_up`.
118
119     Having completed that, we can use define a function that enumerates a tree's fringe, step by step, until it's exhausted:
120
121         let make_fringe_enumerator (t: 'a tree) : 'b * 'a zipper option =
122           (* create a zipper focusing the botleft of t *)
123           let zbotleft = move_botleft (new_zipper t) in
124           (* create initial state, pointing to zbotleft *)
125           let initial_state = Some zbotleft in
126           (* construct the next_leaf function *)
127           let next_leaf : 'a zipper option -> ('a * 'a zipper option) option = function
128             | Some z -> (
129               (* extract label of currently-focused leaf *)
130               let Leaf current = z.in_focus in
131               (* create next_state pointing to next leaf, if there is one *)
132               let next_state : 'a zipper option = match move_right_or_up z with
133                 | None -> None
134                 | Some z' -> Some (move_botleft z') in
135               (* return saved label and next_state *)
136               Some (current, next_state)
137               )
138             | None -> (* we've finished enumerating the fringe *)
139               None in
140           (* return the next_leaf function and initial state *)
141           next_leaf, initial_state
142
143     Here's an example of `make_fringe_enumerator` in action:
144
145         # let tree1 = Leaf 1;;
146         val tree1 : int tree = Leaf 1
147         # let next1, state1 = make_fringe_enumerator tree1;;
148         val next1 : unit -> int option = <fun>
149         # let res1, state1' = next1 state1;;
150         - : int option = Some 1
151         # next1 state1';;
152         - : int option = None
153         # let tree2 = Node (Node (Leaf 1, Leaf 2), Leaf 3);;
154         val tree2 : int tree = Node (Node (Leaf 1, Leaf 2), Leaf 3)
155         # let next2, state2 = make_fringe_enumerator tree2;;
156         val next2 : unit -> int option = <fun>
157         # let res2, state2' = next2 state2;;
158         - : int option = Some 1
159         # let res2, state2'' = next2 state2';;
160         - : int option = Some 2
161         # let res2, state2''' = next2 state2'';;
162         - : int option = Some 3
163         # let res2, state2'''' = next2 state2''';;
164         - : int option = None
165
166     You might think of it like this: `make_fringe_enumerator` returns a little subprogram that will keep returning the next leaf in a tree's fringe, in the form `Some ...`, until it gets to the end of the fringe. After that, it will return `None`. The subprogram's memory of where it is and what steps to perform next are stored in the `state` variables that are part of its input and output.
167
168     Using these fringe enumerators, we can write our `same_fringe` function like this:
169
170         let same_fringe (t1 : 'a tree) (t2 : 'a tree) : bool =
171           let next1, initial_state1 = make_fringe_enumerator t1 in
172           let next2, initial_state2 = make_fringe_enumerator t2 in
173           let rec loop state1 state2 : bool =
174             match next1 state1, next2 state2 with
175             | Some (a, state1'), Some (b, state2') when a = b -> loop state1' state2'
176             | None, None -> true
177             | _ -> false in
178           loop initial_state1 initial_state2
179
180     The auxiliary `loop` function will keep calling itself recursively until a difference in the fringes has manifested itself---either because one fringe is exhausted before the other, or because the next leaves in the two fringes have different labels. If we get to the end of both fringes at the same time (`next1 (), next2 ()` matches the pattern `None, None`) then we've established that the trees do have the same fringe.
181
182 2.  Test your implementations of `move_botleft` and `move_right_or_up` against some example trees to see if the resulting `make_fringe_enumerator` and `same_fringe` functions work as expected. Show us some of your tests.
183
184 3.  Now we'll talk about another way to implement the `make_fringe_enumerator` function above (and so too the `same_fringe` function which uses it). Notice that the pattern given above is that the `make_fringe_enumerator` creates a `next_leaf` function and an initial state, and each time you want to advance the `next_leaf` by one step, you do so by calling it with the current state. It will return a result plus a modified state, which you can use when you want to call it again and take another step. All of the `next_leaf` function's memory about where it is in the enumeration is contained in the state. If you saved an old state, took three steps, and then called the `next_leaf` function again with the saved old state, it would be back where it was three steps ago. But in fact, the way we use the process and state above, there is no back-tracking. Neither do we "fork" any of the states and pursue different forward paths. Their progress is deterministic, and fixed independently of anything that `same_fringe` might do. All that's up to `same_fringe` is to take the decision of when (and whether) to take another step forward.
185
186     Given that usage pattern, it would be appropriate and convenient to make the `next_leaf` function remember its state itself, in a mutable variable. The client function `same_fringe` doesn't need to do anything with, or even be given access to, this variable. Here's how we might write `make_fringe_enumerator` according to this plan:
187
188         let make_fringe_enumerator (t: 'a tree) =
189           (* create a zipper focusing the botleft of t *)
190           let zbotleft = move_botleft (new_zipper t) in
191           (* create refcell, initially pointing to zbotleft *)
192           let zcell = ref (Some zbotleft) in
193           (* construct the next_leaf function *)
194           let next_leaf () : 'a option =
195             match !zcell with
196             | Some z -> (
197               (* extract label of currently-focused leaf *)
198               let Leaf current = z.in_focus in
199               (* update zcell to point to next leaf, if there is one *)
200               let () = zcell := match move_right_or_up z with
201                 | None -> None
202                 | Some z' -> _____ in
203               (* return saved label *)
204               _____
205               )
206             | None -> (* we've finished enumerating the fringe *)
207               None in
208           (* return the next_leaf function *)
209           next_leaf
210
211         let same_fringe (t1 : 'a tree) (t2 : 'a tree) : bool =
212           let next1 = make_fringe_enumerator t1 in
213           let next2 = make_fringe_enumerator t2 in
214           let rec loop () : bool =
215             match _____, _____ with
216             | Some a, Some b when a = b -> loop ()
217             | None, None -> true
218             | _ -> false in
219           loop ()
220
221     You should fill in the blanks.
222
223     <!--
224         let make_fringe_enumerator (t: 'a tree) =
225           (* create a zipper focusing the botleft of t *)
226           let zbotleft = move_botleft (new_zipper t) in
227           (* create refcell, initially pointing to zbotleft *)
228           let zcell = ref (Some zbotleft) in
229           (* construct the next_leaf function *)
230           let next_leaf () : 'a option =
231             match !zcell with
232             | Some z -> (
233               (* extract label of currently-focused leaf *)
234               let Leaf current = z.in_focus in
235               (* update zcell to point to next leaf, if there is one *)
236               let () = zcell := match move_right_or_up z with
237                 | None -> None
238                 | Some z' -> Some (move_botleft z') in
239               (* return saved label *)
240               Some current
241               )
242             | None -> (* we've finished enumerating the fringe *)
243               None in
244           (* return the next_leaf function *)
245           next_leaf
246
247         let same_fringe (t1 : 'a tree) (t2 : 'a tree) : bool =
248           let next1 = make_fringe_enumerator t1 in
249           let next2 = make_fringe_enumerator t2 in
250           let rec loop () : bool =
251             match next1 (), next2 () with
252             | Some a, Some b when a = b -> loop ()
253             | None, None -> true
254             | _ -> false in
255           loop ()
256     -->
257
258
259 ---
260
261
262 Finally, we can use a mutable reference cell to define a function that enumerates a tree's fringe until it's exhausted:
263
264         let make_fringe_enumerator (t: 'a tree) =
265             (* create a zipper focusing the botleft of t *)
266             let zbotleft = move_botleft (new_zipper t)
267             (* create a refcell initially pointing to zbotleft *)
268             in let zcell = ref (Some zbotleft)
269             (* construct the next_leaf function *)
270             in let next_leaf () : 'a option =
271                 match !zcell with
272                 | Some z -> (
273                     (* extract label of currently-focused leaf *)
274                     let Leaf current = z.in_focus
275                     (* update zcell to point to next leaf, if there is one *)
276                     in let () = zcell := match move_right_or_up z with
277                         | None -> None
278                         | Some z' -> Some (move_botleft z')
279                     (* return saved label *)
280                     in Some current
281                 | None -> (* we've finished enumerating the fringe *)
282                     None
283                 )
284             (* return the next_leaf function *)
285             in next_leaf
286             ;;
287
288
289 Using these fringe enumerators, we can write our `same_fringe` function like this:
290
291         let same_fringe (t1 : 'a tree) (t2 : 'a tree) : bool =
292             let next1 = make_fringe_enumerator t1
293             in let next2 = make_fringe_enumerator t2
294             in let rec loop () : bool =
295                 match next1 (), next2 () with
296                 | Some a, Some b when a = b -> loop ()
297                 | None, None -> true
298                 | _ -> false
299             in loop ()
300             ;;
301
302 The auxiliary `loop` function will keep calling itself recursively until a difference in the fringes has manifested itself---either because one fringe is exhausted before the other, or because the next leaves in the two fringes have different labels. If we get to the end of both fringes at the same time (`next1 (), next2 ()` matches the pattern `None, None`) then we've established that the trees do have the same fringe.
303
304
305
306 ---
307
308 4.  Here's another implementation of the same-fringe function, in Scheme. It's taken from <http://c2.com/cgi/wiki?SameFringeProblem>. It uses thunks to delay the evaluation of code that computes the tail of a list of a tree's fringe. It also involves passing "the rest of the enumeration of the fringe" as a thunk argument (`tail-thunk` below). Your assignment is to fill in the blanks in the code, **and also to supply comments to the code,** to explain what every significant piece is doing. Don't forget to supply the comments, this is an important part of the assignment.
309
310     This code uses Scheme's `cond` construct. That works like this;
311
312         (cond
313             ((test1 argument argument) result1)
314             ((test2 argument argument) result2)
315             ((test3 argument argument) result3)
316             (else result4))
317
318     is equivalent to:
319
320         (if (test1 argument argument)
321            ; then
322              result1
323            ; else
324              (if (test2 argument argument)
325                 ; then
326                   result2
327                 ; else
328                   (if (test3 argument argument)
329                      ; then
330                        result3
331                      ; else
332                        result4)))
333
334     Some other Scheme details or reminders:
335
336     *   `#t` is true and `#f` is false
337     *   `(lambda () ...)` constructs a thunk
338     *   there is no difference in meaning between `[...]` and `(...)`; we just sometimes use the square brackets for clarity
339     *   `'(1 . 2)` and `(cons 1 2)` are pairs (the same pair)
340     *   `(list)` and `'()` both evaluate to the empty list
341     *   `(null? lst)` tests whether `lst` is the empty list
342     *   non-empty lists are implemented as pairs whose second member is a list
343     *   `'()` `'(1)` `'(1 2)` `'(1 2 3)` are all lists
344     *   `(list)` `(list 1)` `(list 1 2)` `(list 1 2 3)` are the same lists as the preceding
345     *   `'(1 2 3)` and `(cons 1 '(2 3))` are both pairs and lists (the same list)
346     *   `(pair? lst)` tests whether `lst` is a pair; if `lst` is a non-empty list, it will also pass this test; if `lst` fails this test, it may be because `lst` is the empty list, or because it's not a list or pair at all
347     *   `(car lst)` extracts the first member of a pair / head of a list
348     *   `(cdr lst)` extracts the second member of a pair / tail of a list
349
350     Here is the implementation:
351
352         (define (lazy-flatten tree)
353           (letrec ([helper (lambda (tree tail-thunk)
354                              (cond
355                                [(pair? tree)
356                                  (helper (car tree) (lambda () (helper _____ tail-thunk)))]
357                                [else (cons tree tail-thunk)]))])
358                   (helper tree (lambda () _____))))
359
360         (define (stream-equal? stream1 stream2)
361           (cond
362             [(and (null? stream1) (null? stream2)) _____]
363             [(and (pair? stream1) (pair? stream2))
364               (and (equal? (car stream1) (car stream2))
365                 _____)]
366             [else #f]))
367
368         (define (same-fringe? tree1 tree2)
369           (stream-equal? (lazy-flatten tree1) (lazy-flatten tree2)))
370
371         (define tree1 '(((1 . 2) . (3 . 4)) . (5 . 6)))
372         (define tree2 '(1 . (((2 . 3) . (4 . 5)) . 6)))
373
374         (same-fringe? tree1 tree2)
375
376 The previous problem implemented a "stream" in Scheme. A stream is like a list in that it wraps a series of elements of a single type. It differs from a list in that the tail of the series is left uncomputed until needed. We will turn the stream on and off by thunking it. Here is one way to implement streams in OCaml:
377
378     type 'a stream = End | Next of 'a * (unit -> 'a stream);;
379
380 There is a special stream called End that represents a stream that contains no (more) elements, analogous to the empty list []. Streams that are not empty contain a first object, paired with a thunked stream representing the rest of the series. In order to get access to the next element in the stream, we must force the thunk by applying it to the unit. Watch the behavior of this stream in detail. This stream delivers the natural numbers, in order: 1, 2, 3, ...
381
382     # let rec make_int_stream i = Next (i, fun () -> make_int_stream (i + 1));;
383     val make_int_stream : int -> int stream = [fun]
384
385     # let int_stream = make_int_stream 1;;
386     val int_stream : int stream = Next (1, [fun])         (* First element: 1 *)
387
388     # let tail = match int_stream with Next (i, rest) -> rest;;      
389     val tail : unit -> int stream = [fun]                 (* Tail: a thunk *)
390
391     (* Force the thunk to compute the second element *)
392     # tail ();;
393     - : int stream = Next (2, [fun])                      (* Second element: 2 *)
394
395     # match tail () with Next (_, rest) -> rest ();;
396     - : int stream = Next (3, [fun])                      (* Third element: 3 *)
397
398 You can think of `int_stream` as a functional object that provides access to an infinite sequence of integers, one at a time. It's as if we had written `[1;2;...]` where `...` meant "continue for as long as some other process needs new integers".