changes
authorChris <chris.barker@nyu.edu>
Sun, 26 Apr 2015 21:13:52 +0000 (17:13 -0400)
committerChris <chris.barker@nyu.edu>
Sun, 26 Apr 2015 21:13:52 +0000 (17:13 -0400)
content.mdwn
exercises/_assignment12.mdwn [deleted file]
exercises/assignment12.mdwn [new file with mode: 0644]
exercises/assignment4.mdwn
index.mdwn
topics/_coroutines_and_aborts.mdwn
topics/week12_abortable_traversals.mdwn
topics/week12_list_and_tree_zippers.mdwn

index e6a02af..9279e29 100644 (file)
@@ -9,6 +9,8 @@ week in which they were introduced.
 
 *   [[Kaplan on Plexy|topics/week6_plexy]]
 *   [[Groenendijk, Stokhof, and Veltman|/topics/week10_gsv]]
+*   Mutation and hyper-synonymy (no notes)
+
 
 *   Functional Programming
 
@@ -26,6 +28,7 @@ week in which they were introduced.
     *   [[Ramble on Monads and Modules|topics/week8_monads_and_modules]]
     *   [[Installing and Using the Juli8 Libraries|/juli8]]
     *   [[Programming with mutable state|/topics/week9_mutable_state]]
+    *   Mutation and hyper-synonymy (no notes)
 
 
 *   Order, "static versus dynamic"
@@ -35,6 +38,9 @@ week in which they were introduced.
     *   [[Unit and its usefulness|topics/week3 unit]]
     *   Combinatory evaluator ([[for home|topics/week7_combinatory_evaluator]])
     *   [[Programming with mutable state|/topics/week9_mutable_state]]
+    *   [[Abortable list traversals|/topics/week12_abortable_traversals]]
+    *   [[List and tree zippers|/topics/week12_list_and_tree_zippers]]
+
 
 *   The Untyped Lambda Calculus
 
@@ -48,6 +54,7 @@ week in which they were introduced.
         *   [[Arithmetic with Church numbers|topics/week3_church_arithmetic]]
         *   [[How to get the `tail` of v1 lists?|topics/week3 lists#tails]]
         *   [[Some other list encodings|topics/week3 lists#other-lists]]
+        *   [[Abortable list traversals|/topics/week12_abortable_traversals]]
     *   [[Reduction Strategies and Normal Forms|topics/week3_evaluation_order]]
     *   [[Fixed point combinators|topics/week4_fixed_point_combinators]]
     *   [[More about fixed point combinators|topics/week4_more_about_fixed_point_combinators]]
@@ -79,6 +86,11 @@ week in which they were introduced.
     *   [[Groenendijk, Stokhof, and Veltman|/topics/week10_gsv]]
 
 
+*   Continuations
+    *   [[Abortable list traversals|/topics/week12_abortable_traversals]]
+    *   [[List and tree zippers|/topics/week12_list_and_tree_zippers]]
+
+
 ## Topics by week ##
 
 Week 1:
@@ -158,7 +170,13 @@ Week 9:
 
 Week 10:
 
-*    Groenendijk, Stokhof, and Veltman, "[[Coreference and Modality|/readings/coreference-and-modality.pdf]]" (1996)
-*    [[Notes on GSV|/topics/week10_gsv]], with links to code
+*   Groenendijk, Stokhof, and Veltman, "[[Coreference and Modality|/readings/coreference-and-modality.pdf]]" (1996)
+*   [[Notes on GSV|/topics/week10_gsv]], with links to code
+
 
+Week 12:
 
+*   Mutation and hyper-synonymy (no notes)
+*   [[Abortable list traversals|/topics/week12_abortable_traversals]]
+*   [[List and tree zippers|/topics/week12_list_and_tree_zippers]]
+*   [[Homework for week 12|exercises/assignment12]]
diff --git a/exercises/_assignment12.mdwn b/exercises/_assignment12.mdwn
deleted file mode 100644 (file)
index 2af6513..0000000
+++ /dev/null
@@ -1,235 +0,0 @@
-1.  Complete the definitions of `move_botleft` and `move_right_or_up` from the same-fringe solution in [[this week's notes|/topics/week12_list_and_tree_zippers]]. **Test your attempts** 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.
-
-        type 'a tree = Leaf of 'a | Node of ('a tree * 'a tree)
-
-        type 'a starred_level = Root | Starring_left of 'a starred_nonroot | Starring_right of 'a starred_nonroot
-        and 'a starred_nonroot = { parent : 'a starred_level; sibling: 'a tree };;
-
-        type 'a zipper = { level : 'a starred_level; filler: 'a tree };;
-
-        let new_zipper (t : 'a tree) : 'a zipper =
-          {level = Root; filler = t}
-
-        let rec move_botleft (z : 'a zipper) : 'a zipper =
-          (* returns z if the targetted node in z has no children *)
-          (* else returns move_botleft (zipper which results from moving down from z to the leftmost child) *)
-          _____ (* YOU SUPPLY THE DEFINITION *)
-
-    <!--
-    match z.filler with Leaf _ -> z | Node (l, r) -> move_botleft { level = Starring_left { parent = z.level; sibling = r }; filler = l }
-    -->
-
-        let rec move_right_or_up (z : 'a zipper) : 'a zipper option =
-          (* if it's possible to move right in z, returns Some (the result of doing so) *)
-          (* else if it's not possible to move any further up in z, returns None *)
-          (* else returns move_right_or_up (result of moving up in z) *)
-          _____ (* YOU SUPPLY THE DEFINITION *)
-
-    <!--
-    match z.level with
-    | Starring_left { parent = p; sibling = right } -> Some { level = Starring_right { parent = p; sibling = z.filler }; filler = right }
-    | Starring_right { parent = p; sibling = left } -> let new_tree = Node (left, z.filler) in move_right_or_up { level = p; filler = new_tree}
-    | Root -> None
-    -->
-
-    &nbsp;
-
-        let make_fringe_enumerator (t: 'a tree) : 'b * 'a zipper option =
-          (* create a zipper targetting the botleft of t *)
-          let zbotleft = move_botleft (new_zipper t) in
-          (* create initial state, pointing to zbotleft *)
-          let initial_state = Some zbotleft in
-          (* construct the next_leaf function *)
-          let next_leaf : 'a zipper option -> ('a * 'a zipper option) option = function
-            | Some z -> (
-              (* extract label of currently-targetted leaf *)
-              let Leaf current = z.filler in
-              (* create next_state pointing to next leaf, if there is one *)
-              let next_state : 'a zipper option = match move_right_or_up z with
-                | None -> None
-                | Some z' -> Some (move_botleft z') in
-              (* return saved label and next_state *)
-              Some (current, next_state)
-              )
-            | None -> (* we've finished enumerating the fringe *)
-              None in
-          (* return the next_leaf function and initial state *)
-          next_leaf, initial_state
-
-        let same_fringe (t1 : 'a tree) (t2 : 'a tree) : bool =
-          let next1, initial_state1 = make_fringe_enumerator t1 in
-          let next2, initial_state2 = make_fringe_enumerator t2 in
-          let rec loop state1 state2 : bool =
-            match next1 state1, next2 state2 with
-            | Some (a, state1'), Some (b, state2') when a = b -> loop state1' state2'
-            | None, None -> true
-            | _ -> false in
-          loop initial_state1 initial_state2
-
-
-2.  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 `process` function and an initial state, and each time you want to advance the `process` 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 `process` 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 `process` 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.
-
-    Given that usage pattern, it would be appropriate and convenient to make the `process` 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:
-
-        let make_fringe_enumerator (t: 'a tree) =
-          (* create a zipper targetting the botleft of t *)
-          let zbotleft = move_botleft (new_zipper t) in
-          (* create refcell, initially pointing to zbotleft *)
-          let zcell = ref (Some zbotleft) in
-          (* construct the next_leaf function *)
-          let next_leaf () : 'a option =
-            match !zcell with
-            | Some z -> (
-              (* extract label of currently-targetted leaf *)
-              let Leaf current = z.filler in
-              (* update zcell to point to next leaf, if there is one *)
-              let () = zcell := match move_right_or_up z with
-                | None -> None
-                | Some z' -> _____ in
-              (* return saved label *)
-              _____
-              )
-            | None -> (* we've finished enumerating the fringe *)
-              None in
-          (* return the next_leaf function *)
-          next_leaf
-
-        let same_fringe (t1 : 'a tree) (t2 : 'a tree) : bool =
-          let next1 = make_fringe_enumerator t1 in
-          let next2 = make_fringe_enumerator t2 in
-          let rec loop () : bool =
-            match _____, _____ with
-            | Some a, Some b when a = b -> loop ()
-            | None, None -> true
-            | _ -> false in
-          loop ()
-
-    You should fill in the blanks.
-
-    <!--
-        let make_fringe_enumerator (t: 'a tree) =
-          (* create a zipper targetting the botleft of t *)
-          let zbotleft = move_botleft (new_zipper t) in
-          (* create refcell, initially pointing to zbotleft *)
-          let zcell = ref (Some zbotleft) in
-          (* construct the next_leaf function *)
-          let next_leaf () : 'a option =
-            match !zcell with
-            | Some z -> (
-              (* extract label of currently-targetted leaf *)
-              let Leaf current = z.filler in
-              (* update zcell to point to next leaf, if there is one *)
-              let () = zcell := match move_right_or_up z with
-                | None -> None
-                | Some z' -> Some (move_botleft z') in
-              (* return saved label *)
-              Some current
-              )
-            | None -> (* we've finished enumerating the fringe *)
-              None in
-          (* return the next_leaf function *)
-          next_leaf
-
-        let same_fringe (t1 : 'a tree) (t2 : 'a tree) : bool =
-          let next1 = make_fringe_enumerator t1 in
-          let next2 = make_fringe_enumerator t2 in
-          let rec loop () : bool =
-            match next1 (), next2 () with
-            | Some a, Some b when a = b -> loop ()
-            | None, None -> true
-            | _ -> false in
-          loop ()
-    -->
-
-
-3.  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.
-
-    This code uses Scheme's `cond` construct. That works like this;
-
-        (cond
-            ((test1 argument argument) result1)
-            ((test2 argument argument) result2)
-            ((test3 argument argument) result3)
-            (else result4))
-
-    is equivalent to:
-
-        (if (test1 argument argument)
-           ; then
-             result1
-           ; else
-             (if (test2 argument argument)
-                ; then
-                  result2
-                ; else
-                  (if (test3 argument argument)
-                     ; then
-                       result3
-                     ; else
-                       result4)))
-
-    Some other Scheme details or reminders:
-
-    *   `#t` is true and `#f` is false
-    *   `(lambda () ...)` constructs a thunk
-    *   there is no difference in meaning between `[...]` and `(...)`; we just sometimes use the square brackets for clarity
-    *   `'(1 . 2)` and `(cons 1 2)` are pairs (the same pair)
-    *   `(list)` and `'()` both evaluate to the empty list
-    *   `(null? lst)` tests whether `lst` is the empty list
-    *   non-empty lists are implemented as pairs whose second member is a list
-    *   `'()` `'(1)` `'(1 2)` `'(1 2 3)` are all lists
-    *   `(list)` `(list 1)` `(list 1 2)` `(list 1 2 3)` are the same lists as the preceding
-    *   `'(1 2 3)` and `(cons 1 '(2 3))` are both pairs and lists (the same list)
-    *   `(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
-    *   `(car lst)` extracts the first member of a pair / head of a list
-    *   `(cdr lst)` extracts the second member of a pair / tail of a list
-
-    Here is the implementation:
-
-        (define (lazy-flatten tree)
-          (letrec ([helper (lambda (tree tail-thunk)
-                             (cond
-                               [(pair? tree)
-                                 (helper (car tree) (lambda () (helper _____ tail-thunk)))]
-                               [else (cons tree tail-thunk)]))])
-                  (helper tree (lambda () _____))))
-
-        (define (stream-equal? stream1 stream2)
-          (cond
-            [(and (null? stream1) (null? stream2)) _____]
-            [(and (pair? stream1) (pair? stream2))
-              (and (equal? (car stream1) (car stream2))
-                _____)]
-            [else #f]))
-
-        (define (same-fringe? tree1 tree2)
-          (stream-equal? (lazy-flatten tree1) (lazy-flatten tree2)))
-
-        (define tree1 '(((1 . 2) . (3 . 4)) . (5 . 6)))
-        (define tree2 '(1 . (((2 . 3) . (4 . 5)) . 6)))
-
-        (same-fringe? tree1 tree2)
-
-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:
-
-    type 'a stream = End | Next of 'a * (unit -> 'a stream);;
-
-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, ...
-
-    # let rec make_int_stream i = Next (i, fun () -> make_int_stream (i + 1));;
-    val make_int_stream : int -> int stream = [fun]
-
-    # let int_stream = make_int_stream 1;;
-    val int_stream : int stream = Next (1, [fun])         (* First element: 1 *)
-
-    # let tail = match int_stream with Next (i, rest) -> rest;;      
-    val tail : unit -> int stream = [fun]                 (* Tail: a thunk *)
-
-    (* Force the thunk to compute the second element *)
-    # tail ();;
-    - : int stream = Next (2, [fun])                      (* Second element: 2 *)
-
-    # match tail () with Next (_, rest) -> rest ();;
-    - : int stream = Next (3, [fun])                      (* Third element: 3 *)
-
-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".
diff --git a/exercises/assignment12.mdwn b/exercises/assignment12.mdwn
new file mode 100644 (file)
index 0000000..58068c4
--- /dev/null
@@ -0,0 +1,392 @@
+## Same-fringe using zippers ##
+
+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):
+
+        .                .
+       / \              / \
+      .   3            1   .
+     / \                  / \
+    1   2                2   3
+
+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.
+
+
+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:
+
+        .                .
+       / \              / \
+      .   3            1   .
+     / \                  / \
+    1   2                2   3
+
+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.
+
+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.
+
+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.
+
+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:
+
+    # type blah = Blah of int * int * (char -> bool);;
+
+and then having to remember which element in the triple was which:
+
+    # let b1 = Blah (1, (fun c -> c = 'M'), 2);;
+    Error: This expression has type int * (char -> bool) * int
+    but an expression was expected of type int * int * (char -> bool)
+    # (* damnit *)
+    # let b1 = Blah (1, 2, (fun c -> c = 'M'));;
+    val b1 : blah = Blah (1, 2, <fun>)
+
+records let you attach descriptive labels to the components of the tuple:
+
+    # type blah_record = { height : int; weight : int; char_tester : char -> bool };;
+    # let b2 = { height = 1; weight = 2; char_tester = (fun c -> c = 'M') };;
+    val b2 : blah_record = {height = 1; weight = 2; char_tester = <fun>}
+    # let b3 = { height = 1; char_tester = (fun c -> c = 'K'); weight = 3 };; (* also works *)
+    val b3 : blah_record = {height = 1; weight = 3; char_tester = <fun>}
+
+These were the strategies to extract the components of an unlabeled tuple:
+
+    let h = fst some_pair (* accessor functions fst and snd are only predefined for pairs *)
+
+    let (h, w, test) = b1 (* works for arbitrary tuples *)
+
+    match b1 with
+    | (h, w, test) -> ... (* same as preceding *)
+
+Here is how you can extract the components of a labeled record:
+
+    let h = b2.height (* handy! *)
+
+    let {height = h; weight = w; char_tester = test} = b2 in
+    (* go on to use h, w, and test ... *)
+
+    match test with
+    | {height = h; weight = w; char_tester = test} ->
+    (* same as preceding *)
+
+Anyway, using record types, we might define the tree zipper interface like so. First, we define a type for leaf-labeled, binary trees:
+
+    type 'a tree = Leaf of 'a | Node of 'a tree * 'a tree
+
+Next, the types for our tree zippers:
+
+    type 'a zipper = { in_focus: 'a tree; context : 'a context }
+    and 'a context = Root | Nonroot of 'a nonroot_context
+    and 'a nonroot_context = { up : 'a context; left: 'a tree option; right: 'a tree option }
+
+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.
+
+The following function takes an `'a tree` and returns an `'a zipper` focused on its root:
+
+    let new_zipper (t : 'a tree) : 'a zipper =
+      {in_focus = t; context = Root}
+
+Here are the beginnings of functions to move from one focused tree to another:
+
+    let rec move_botleft (z : 'a zipper) : 'a zipper =
+      (* returns z if the focused node in z has no children *)
+      (* else returns move_botleft (zipper which results from moving down from z's focused node to its leftmost child) *)
+      _____ (* YOU SUPPLY THE DEFINITION *)
+
+<!--
+    match z.in_focus with
+    | Leaf _ -> z
+    | Node(left, right) ->
+        move_botleft {in_focus = left; context = Nonroot {up = z.context; left = None; right = Some right}}
+-->
+
+
+    let rec move_right_or_up (z : 'a zipper) : 'a zipper option =
+      (* if it's possible to move right in z, returns Some (the result of doing so) *)
+      (* else if it's not possible to move any further up in z, returns None *)
+      (* else returns move_right_or_up (result of moving up in z) *)
+      _____ (* YOU SUPPLY THE DEFINITION *)
+
+<!--
+    match z.context with
+    | Nonroot {up; left= None; right = Some right} ->
+        Some {in_focus = right; context = Nonroot {up; left = Some z.in_focus; right = None}}
+    | Root -> None
+    | Nonroot {up; left = Some left; right = None} ->
+        move_right_or_up {in_focus = Node(left, z.in_focus); context = up}
+    | _ -> assert false
+-->
+
+
+1.  Your first assignment is to complete the definitions of `move_botleft` and `move_right_or_up`. (Really it should be `move_right_or_up_..._and_right`.)
+
+    Having completed that, we can use define a function that enumerates a tree's fringe, step by step, until it's exhausted:
+
+        let make_fringe_enumerator (t: 'a tree) =
+          (* create a zipper focusing the botleft of t *)
+          let zbotleft = move_botleft (new_zipper t) in
+          (* create initial state, pointing to zbotleft *)
+          let initial_state : 'a zipper option = Some zbotleft in
+          (* construct the next_leaf function *)
+          let next_leaf : 'a zipper option -> ('a * 'a zipper option) option =
+            fun state -> match state with
+            | Some z -> (
+              (* extract label of currently-focused leaf *)
+              let Leaf current = z.in_focus in
+              (* create next_state pointing to next leaf, if there is one *)
+              let next_state : 'a zipper option = match move_right_or_up z with
+                | None -> None
+                | Some z' -> Some (move_botleft z') in
+              (* return saved label and next_state *)
+              Some (current, next_state)
+              )
+            | None -> (* we've finished enumerating the fringe *)
+              None in
+          (* return the next_leaf function and initial state *)
+          next_leaf, initial_state
+
+    Here's an example of `make_fringe_enumerator` in action:
+
+        # let tree1 = Leaf 1;;
+        val tree1 : int tree = Leaf 1
+        # let next1, state1 = make_fringe_enumerator tree1;;
+        val next1 : int zipper option -> (int * int zipper option) option = <fun>
+        val state1 : int zipper option = Some ...
+        # let Some (res1, state1') = next1 state1;;
+        val res1 : int = 1
+        val state1' : int zipper option = None
+        # next1 state1';;
+        - : (int * int zipper option) option = None
+        # let tree2 = Node (Node (Leaf 1, Leaf 2), Leaf 3);;
+        val tree2 : int tree = Node (Node (Leaf 1, Leaf 2), Leaf 3)
+        # let next2, state2 = make_fringe_enumerator tree2;;
+        val next2 : int zipper option -> (int * int zipper option) option = <fun>
+        val state2 : int zipper option = Some ...
+        # let Some (res2, state2') = next2 state2;;
+        val res2 : int = 1
+        val state2' : int zipper option = Some ...
+        # let Some (res2, state2'') = next2 state2';;
+        val res2 : int = 2
+        val state2'' : int zipper option = Some ...
+        # let Some (res2, state2''') = next2 state2'';;
+        val res2 : int = 3
+        val state2''' : int zipper option = None
+        # next2 state2''';;
+        - : (int * int zipper option) option = None
+
+    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 `next_state` variables that are part of its input and output.
+
+    Using these fringe enumerators, we can write our `same_fringe` function like this:
+
+        let same_fringe (t1 : 'a tree) (t2 : 'a tree) : bool =
+          let next1, initial_state1 = make_fringe_enumerator t1 in
+          let next2, initial_state2 = make_fringe_enumerator t2 in
+          let rec loop state1 state2 : bool =
+            match next1 state1, next2 state2 with
+            | Some (a, state1'), Some (b, state2') when a = b -> loop state1' state2'
+            | None, None -> true
+            | _ -> false in
+          loop initial_state1 initial_state2
+
+    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 state1, next2 state2` matches the pattern `None, None`) then we've established that the trees do have the same fringe.
+
+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.
+
+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 leaf label 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 `next_leaf` function 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.
+
+    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:
+
+        let make_fringe_enumerator (t: 'a tree) =
+          (* create a zipper focusing the botleft of t *)
+          let zbotleft = move_botleft (new_zipper t) in
+          (* create a refcell, initially pointing to zbotleft *)
+          let zcell = ref (Some zbotleft) in
+          (* construct the next_leaf function *)
+          let next_leaf : unit -> 'a option = fun () ->
+            match !zcell with
+            | Some z -> (
+              (* extract label of currently-focused leaf *)
+              let Leaf current = z.in_focus in
+              (* update zcell to point to next leaf, if there is one *)
+              let () = zcell := match move_right_or_up z with
+                | None -> None
+                | Some z' -> _____ in
+              (* return saved label *)
+              _____
+              )
+            | None -> (* we've finished enumerating the fringe *)
+              None in
+          (* return the next_leaf function *)
+          next_leaf
+
+        let same_fringe (t1 : 'a tree) (t2 : 'a tree) : bool =
+          let next1 = make_fringe_enumerator t1 in
+          let next2 = make_fringe_enumerator t2 in
+          let rec loop () : bool =
+            match _____, _____ with
+            | Some a, Some b when a = b -> loop ()
+            | None, None -> true
+            | _ -> false in
+          loop ()
+
+    You should fill in the blanks.
+
+    <!--
+        let make_fringe_enumerator (t: 'a tree) =
+          (* create a zipper focusing the botleft of t *)
+          let zbotleft = move_botleft (new_zipper t) in
+          (* create refcell, initially pointing to zbotleft *)
+          let zcell = ref (Some zbotleft) in
+          (* construct the next_leaf function *)
+          let next_leaf : unit -> 'a option = fun () ->
+            match !zcell with
+            | Some z -> (
+              (* extract label of currently-focused leaf *)
+              let Leaf current = z.in_focus in
+              (* update zcell to point to next leaf, if there is one *)
+              let () = zcell := match move_right_or_up z with
+                | None -> None
+                | Some z' -> Some (move_botleft z') in
+              (* return saved label *)
+              Some current
+              )
+            | None -> (* we've finished enumerating the fringe *)
+              None in
+          (* return the next_leaf function *)
+          next_leaf
+
+        let same_fringe (t1 : 'a tree) (t2 : 'a tree) : bool =
+          let next1 = make_fringe_enumerator t1 in
+          let next2 = make_fringe_enumerator t2 in
+          let rec loop () : bool =
+            match next1 (), next2 () with
+            | Some a, Some b when a = b -> loop ()
+            | None, None -> true
+            | _ -> false in
+          loop ()
+    -->
+
+    Here's an example of our new `make_fringe_enumerator` in action:
+
+        # let tree1 = Leaf 1;;
+        val tree1 : int tree = Leaf 1
+        # let next1 = make_fringe_enumerator tree1;;
+        val next1 : unit -> int option = <fun>
+        # next1 ();;
+        - : int option = Some 1
+        # next1 ();;
+        - : int option = None
+        # next1 ();;
+        - : int option = None
+        # let tree2 = Node (Node (Leaf 1, Leaf 2), Leaf 3);;
+        val tree2 : int tree = Node (Node (Leaf 1, Leaf 2), Leaf 3)
+        # let next2 = make_fringe_enumerator tree2;;
+        val next2 : unit -> int option = <fun>
+        # next2 ();;
+        - : int option = Some 1
+        # next2 ();;
+        - : int option = Some 2
+        # next2 ();;
+        - : int option = Some 3
+        # next2 ();;
+        - : int option = None
+        # next2 ();;
+        - : int option = None
+
+## Same-fringe using streams ##
+
+Now we'll describe a different way to create "the little subprograms" that we built above with `make_fringe_enumerator`. This code will make use of a data structure called a "stream". 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 turn the stream off and on by thunking it, nad by forcing the thunk.
+
+We'll first show how to implement streams in OCaml, so that the types are manifest. But then we'll switch to a Scheme version to do the same-fringe problem. In part because that's how we have the code already written; but also because some later discussion will use the Scheme code as a starting point. In principle, though, we could have used OCaml throughout.
+
+So here is a natural OCaml type for a stream. (You could also do things differently.)
+
+    type 'a stream = End | Next of 'a * (unit -> 'a stream);;
+
+We have a special variant called `End` that encodes a stream that contains no (more) elements, analogous to the empty list `[]`. Streams that are not empty contain a first element, paired with a thunked stream representing the rest of the stream. In order to get access to the next element in the stream, we must force the thunk by applying it to `()`. Watch the behavior of this stream in detail. This stream delivers the natural numbers, in order: `1, 2, 3, ...`
+
+    # let rec make_int_stream i = Next (i, fun () -> make_int_stream (i + 1));;
+    val make_int_stream : int -> int stream = <fun>
+
+    # let int_stream = make_int_stream 1;;
+    val int_stream : int stream = Next (1, <fun>)         (* First element: 1 *)
+
+    # let tail = match int_stream with Next (i, rest) -> rest;;
+    val tail : unit -> int stream = <fun>                 (* Tail: a thunk *)
+
+    (* Force the thunk to compute the second element *)
+    # tail ();;
+    - : int stream = Next (2, <fun>)                      (* Second element: 2 *)
+
+    # match tail () with Next (_, rest) -> rest ();;
+    - : int stream = Next (3, <fun>)                      (* Third element: 3 *)
+
+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".
+
+Okay, now armed with the idea of a stream, let's use a Scheme version of them to handle the same-fringe problem. This code is 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.
+
+This code uses Scheme's `cond` construct. That works like this;
+
+    (cond
+        ((test1 argument argument) result1)
+        ((test2 argument argument) result2)
+        ((test3 argument argument) result3)
+        (else result4))
+
+is equivalent to:
+
+    (if (test1 argument argument)
+       ; then
+         result1
+       ; else
+         (if (test2 argument argument)
+            ; then
+              result2
+            ; else
+              (if (test3 argument argument)
+                 ; then
+                   result3
+                 ; else
+                   result4)))
+
+Some other Scheme details or reminders:
+
+*   `#t` is true and `#f` is false
+*   `(lambda () ...)` constructs a thunk
+*   there is no difference in meaning between `[...]` and `(...)`; we just sometimes use the square brackets for clarity
+*   `'(1 . 2)` and `(cons 1 2)` are pairs (the same pair)
+*   `(list)` and `'()` both evaluate to the empty list
+*   `(null? lst)` tests whether `lst` is the empty list
+*   non-empty lists are implemented as pairs whose second member is a list
+*   `'()` `'(1)` `'(1 2)` `'(1 2 3)` are all lists
+*   `(list)` `(list 1)` `(list 1 2)` `(list 1 2 3)` are the same lists as the preceding
+*   `'(1 2 3)` and `(cons 1 '(2 3))` are both pairs and lists (the same list)
+*   `(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
+*   `(car lst)` extracts the first member of a pair / head of a list
+*   `(cdr lst)` extracts the second member of a pair / tail of a list
+
+<!-- -->
+
+4.  Here is the Scheme code handling the same-fringe problem. You should fill in the blanks:
+
+        (define (lazy-flatten tree)
+          (letrec ([helper (lambda (tree tail-thunk)
+                             (cond
+                               [(pair? tree)
+                                 (helper (car tree) (lambda () (helper _____ tail-thunk)))]
+                               [else (cons tree tail-thunk)]))])
+                  (helper tree (lambda () _____))))
+
+        (define (stream-equal? stream1 stream2)
+          (cond
+            [(and (null? stream1) (null? stream2)) _____]
+            [(and (pair? stream1) (pair? stream2))
+              (and (equal? (car stream1) (car stream2))
+                _____)]
+            [else #f]))
+
+        (define (same-fringe? tree1 tree2)
+          (stream-equal? (lazy-flatten tree1) (lazy-flatten tree2)))
+
+        (define tree1 '(((1 . 2) . (3 . 4)) . (5 . 6)))
+        (define tree2 '(1 . (((2 . 3) . (4 . 5)) . 6)))
+
+        (same-fringe? tree1 tree2)
+
index 3786a61..9517111 100644 (file)
@@ -114,7 +114,7 @@ For instance, `fact 0 ~~> 1`, `fact 1 ~~> 1`, `fact 2 ~~> 2`, `fact 3 ~~>
     Your assignment is to write a Lambda Calculus function that expects a tree, encoded in the way just described, as an argument, and returns the sum of its leaves as a result. So for all of the trees listed above, it should return `1 + 2 + 3`, namely `6`. You can use any Lambda Calculus implementation of lists you like.
 
 
-
+<a id=fringe></a>
 8.    The **fringe** of a leaf-labeled tree is the list of values at its leaves, ordered from left-to-right. For example, the fringe of all three trees displayed above is the same list, `[1, 2, 3]`. We are going to return to the question of how to tell whether trees have the same fringe several times this course. We'll discover more interesting and more efficient ways to do it as our conceptual toolboxes get fuller. For now, we're going to explore the straightforward strategy. Write a function that expects a tree as an argument, and returns the list which is its fringe. Next write a function that expects two trees as arguments, converts each of them into their fringes, and then determines whether the two lists so produced are equal. (Convert your `list_equal?` function from last week's homework into the Lambda Calculus for this last step.)
 
 
index 82b3f6a..38eb21b 100644 (file)
@@ -191,7 +191,9 @@ We've posted a [[State Monad Tutorial]].
 
 (**Week 12**) Thursday April 23
 
-> Topics: Mutation and hyper-synonymy; [[Abortable list traversals|/topics/week12_abortable_traversals]]; List and tree zippers; Homework
+> Topics: Mutation and hyper-synonymy (no notes); [[Abortable list traversals|/topics/week12_abortable_traversals]]; [[List and tree zippers|/topics/week12_list_and_tree_zippers]]; [[Homework|exercises/assignment12]]
+
+> For amusement/tangential edification: [xkcd on code quality](https://xkcd.com/1513/); [turning a sphere inside out](https://www.youtube.com/watch?v=-6g3ZcmjJ7k)
 
 
 ## Course Overview ##
index 4b2b5da..ce525b3 100644 (file)
@@ -1,189 +1,5 @@
 [[!toc]]
 
-##Same-fringe using a zipper-based coroutine##
-
-Recall back in [[Assignment4]], 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):
-
-           .                .
-          / \              / \
-         .   3            1   .
-        / \                  / \
-       1   2                2   3
-
-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.
-
-
-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 targetted 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 position in a sibling list), 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 targetted 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:
-
-           .                .
-          / \              / \
-         .   3            1   .
-        / \                  / \
-       1   2                2   3
-
-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.
-
-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.
-
-Let's sketch the implementation of this. We won't provide all the details for an implementation of the tree zipper, but we will sketch an interface for it.
-
-First, we define a type for leaf-labeled, binary trees:
-
-       type 'a tree = Leaf of 'a | Node of ('a tree * 'a tree)
-
-Next, the interface for our tree zippers. We'll help ourselves to OCaml's **record types**. These are nothing more than tuples with a pretty interface. Instead of saying:
-
-       # type blah = Blah of (int * int * (char -> bool));;
-
-and then having to remember which element in the triple was which:
-
-       # let b1 = Blah (1, (fun c -> c = 'M'), 2);;
-       Error: This expression has type int * (char -> bool) * int
-       but an expression was expected of type int * int * (char -> bool)
-       # (* damnit *)
-       # let b1 = Blah (1, 2, (fun c -> c = 'M'));;
-       val b1 : blah = Blah (1, 2, <fun>)
-
-records let you attach descriptive labels to the components of the tuple:
-
-       # type blah_record = { height : int; weight : int; char_tester : char -> bool };;
-       # let b2 = { height = 1; weight = 2; char_tester = (fun c -> c = 'M') };;
-       val b2 : blah_record = {height = 1; weight = 2; char_tester = <fun>}
-       # let b3 = { height = 1; char_tester = (fun c -> c = 'K'); weight = 3 };; (* also works *)
-       val b3 : blah_record = {height = 1; weight = 3; char_tester = <fun>}
-
-These were the strategies to extract the components of an unlabeled tuple:
-
-       let h = fst some_pair;; (* accessor functions fst and snd are only predefined for pairs *)
-
-       let (h, w, test) = b1;; (* works for arbitrary tuples *)
-
-       match b1 with
-       | (h, w, test) -> ...;; (* same as preceding *)
-
-Here is how you can extract the components of a labeled record:
-
-       let h = b2.height;; (* handy! *)
-
-       let {height = h; weight = w; char_tester = test} = b2
-       in (* go on to use h, w, and test ... *)
-
-       match test with
-       | {height = h; weight = w; char_tester = test} ->
-         (* same as preceding *)
-
-Anyway, using record types, we might define the tree zipper interface like so:
-
-       type 'a starred_level = Root | Starring_Left of 'a starred_nonroot | Starring_Right of 'a starred_nonroot
-       and 'a starred_nonroot = { parent : 'a starred_level; sibling: 'a tree };;
-
-       type 'a zipper = { level : 'a starred_level; filler: 'a tree };;
-
-       let rec move_botleft (z : 'a zipper) : 'a zipper =
-           (* returns z if the targetted node in z has no children *)
-           (* else returns move_botleft (zipper which results from moving down and left in z) *)
-
-<!--
-           let {level; filler} = z
-           in match filler with
-           | Leaf _ -> z
-           | Node(left, right) ->
-               let zdown = {level = Starring_Left {parent = level; sibling = right}; filler = left}
-               in move_botleft zdown
-           ;;
--->
-
-       let rec move_right_or_up (z : 'a zipper) : 'a zipper option =
-           (* if it's possible to move right in z, returns Some (the result of doing so) *)
-           (* else if it's not possible to move any further up in z, returns None *)
-           (* else returns move_right_or_up (result of moving up in z) *)
-
-<!--
-           let {level; filler} = z
-           in match level with
-           | Starring_Left {parent; sibling = right} -> Some {level = Starring_Right {parent; sibling = filler}; filler = right}
-           | Root -> None
-           | Starring_Right {parent; sibling = left} ->
-               let z' = {level = parent; filler = Node(left, filler)}
-               in move_right_or_up z'
-           ;;
--->
-
-The following function takes an `'a tree` and returns an `'a zipper` focused on its root:
-
-       let new_zipper (t : 'a tree) : 'a zipper =
-           {level = Root; filler = t}
-           ;;
-
-Finally, we can use a mutable reference cell to define a function that enumerates a tree's fringe until it's exhausted:
-
-       let make_fringe_enumerator (t: 'a tree) =
-           (* create a zipper targetting the botleft of t *)
-           let zbotleft = move_botleft (new_zipper t)
-           (* create a refcell initially pointing to zbotleft *)
-           in let zcell = ref (Some zbotleft)
-           (* construct the next_leaf function *)
-           in let next_leaf () : 'a option =
-               match !zcell with
-               | Some z -> (
-                   (* extract label of currently-targetted leaf *)
-                   let Leaf current = z.filler
-                   (* update zcell to point to next leaf, if there is one *)
-                   in let () = zcell := match move_right_or_up z with
-                       | None -> None
-                       | Some z' -> Some (move_botleft z')
-                   (* return saved label *)
-                   in Some current
-               | None -> (* we've finished enumerating the fringe *)
-                   None
-               )
-           (* return the next_leaf function *)
-           in next_leaf
-           ;;
-
-Here's an example of `make_fringe_enumerator` in action:
-
-       # let tree1 = Leaf 1;;
-       val tree1 : int tree = Leaf 1
-       # let next1 = make_fringe_enumerator tree1;;
-       val next1 : unit -> int option = <fun>
-       # next1 ();;
-       - : int option = Some 1
-       # next1 ();;
-       - : int option = None
-       # next1 ();;
-       - : int option = None
-       # let tree2 = Node (Node (Leaf 1, Leaf 2), Leaf 3);;
-       val tree2 : int tree = Node (Node (Leaf 1, Leaf 2), Leaf 3)
-       # let next2 = make_fringe_enumerator tree2;;
-       val next2 : unit -> int option = <fun>
-       # next2 ();;
-       - : int option = Some 1
-       # next2 ();;
-       - : int option = Some 2
-       # next2 ();;
-       - : int option = Some 3
-       # next2 ();;
-       - : int option = None
-       # next2 ();;
-       - : int option = None
-
-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 keep returning `None`.
-
-Using these fringe enumerators, we can write our `same_fringe` function like this:
-
-       let same_fringe (t1 : 'a tree) (t2 : 'a tree) : bool =
-           let next1 = make_fringe_enumerator t1
-           in let next2 = make_fringe_enumerator t2
-           in let rec loop () : bool =
-               match next1 (), next2 () with
-               | Some a, Some b when a = b -> loop ()
-               | None, None -> true
-               | _ -> false
-           in loop ()
-           ;;
-
-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.
 
 The technique illustrated here with our fringe enumerators is a powerful and important one. It's an example of what's sometimes called **cooperative threading**. A "thread" is a subprogram that the main computation spawns off. Threads are called "cooperative" when the code of the main computation and the thread fixes when control passes back and forth between them. (When the code doesn't control this---for example, it's determined by the operating system or the hardware in ways that the programmer can't predict---that's called "preemptive threading.") Cooperative threads are also sometimes called *coroutines* or *generators*.
 
index 59d5517..7cccacb 100644 (file)
@@ -63,11 +63,10 @@ Here's how to get the last element of such a list:
 
 This is similar to getting the first element, except that each step delivers its output to the `keep_going` handler rather than to the `done` handler. That ensures that we will only get the output of the last step, when the traversal function is applied to the last member of the list. If the list is empty, then we'll get the `err` value, just as with the function that tries to extract the list's head.
 
-One thing to note is that there are limits to how much you can immunize yourself against doing unnecessary work. A demon evaluator who got to custom-pick the evaluation order (including doing reductions underneath lambdas when he wanted to) could ensure that lots of unnecessary computations got performed, despite your best efforts. We don't yet have any way to prevent that. (Later we will see some ways to *computationally force* the evaluation order we prefer. Of course, in any real computing environment you'll mostly know what evaluation order you're dealing with, and you can mostly program efficiently around that.) The current scheme at least makes our result not *computationally depend on* what happens further on in the traversal, once we've passed a result to the `done_handler`. We don't even depend on the later steps in the traversal cooperating to pass our result through.
+One thing to note is that there are limits to how much you can immunize yourself against doing unnecessary work. A demon evaluator who got to custom-pick the evaluation order (including doing reductions underneath lambdas when he wanted to) could ensure that lots of unnecessary computations got performed, despite your best efforts. We don't yet have any way to prevent that. (Later we will see some ways to *computationally force* the evaluation order we prefer. Of course, in any real computing environment you'll mostly know what evaluation order you're dealing with, and you can mostly program efficiently around that.) The current scheme at least makes our result not *computationally depend on* what happens further on in the traversal, once we've passed a result to the `done_handler`. We don't even rely on the later steps in the traversal cooperating to pass our result through.
 
-All of that gave us a left-fold implementation of lists. (Perhaps if you were _aiming_ for a left-fold implementation of lists, you would make the traversal function `f` take its `current_list_element` and `seed_value` arguments in the flipped order, but let's not worry about that.)
-
-Now, let's think about how to get a right-fold implementation. It's not profoundly different, but it does require us to change our interface a little. Our left-fold implementation of `[10,20,30,40]`, above, looked like this (now we abbreviate some of the variables):
+All of that gave us a *left*-fold implementation of lists. (Perhaps if you were _aiming_ for a left-fold implementation of lists, you would make the traversal function `f` take its `current_list_element` and `seed_value` arguments in the flipped order, but let's not worry about that.)
+Now, let's think about how to get a *right*-fold implementation. It's not profoundly different, but it does require us to change our interface a little. Our left-fold implementation of `[10,20,30,40]`, above, looked like this (now we abbreviate some of the variables):
 
     \f z d. f 10 z d (\z. [20,30,40] f z d)
 
@@ -85,7 +84,7 @@ Now suppose we had just the implementation of the tail of the list, `[20,30,40]`
 
 How should we take that value and transform it into the preceding value, which represents `10` consed onto that tail? I can't see how to do it in a general way, and I expect it's just not possible. Essentially what we want is to take that second `d` in the innermost function `\z. f 20 z d d`, we want to replace that second `d` with something like `(\z. f 10 z d d)`. But how can we replace just the second `d` without also replacing the first `d`, and indeed all the other bound occurrences of `d` in the expansion of `[20,30,40]`.
 
-The difficulty here is that our traversal function `f` expects two handlers, but we are only giving the fold function we implement the list as a single handler. That single handler gets fed twice to the traversal function. One time it may be transformed, but at the end of the traversal, as with `\z. f 20 z d d`, there's nothing left to do to "keep going", so here it's just the single handler `d` fed to `f` twice. But we can see that in order to implement `cons` for a right-folding traversal, we don't want it to be the single handler `d` fed to `f` twice. It'd work better if we implemented `[20,30,40]` like this:
+The difficulty here is that our traversal function `f` expects two handlers, but we are only giving a single handler to the fold function we implement the list as. That single handler gets fed twice to the traversal function. One time it may be transformed, but at the end of the traversal, as with `\z. f 20 z d d`, there's nothing left to do to "keep going", so here it's just the single handler `d` fed to `f` twice. But we can see that in order to implement `cons` for a right-folding traversal, we don't want it to be the single handler `d` fed to `f` twice. It'd work better if we implemented `[20,30,40]` like this:
 
     \f z d g. f 40 z d (\z. f 30 z d (\z. f 20 z d g))
 
index 1eb5d07..093b9b0 100644 (file)
@@ -15,7 +15,7 @@ Say you've got some moderately-complex function for searching through a list, fo
                | x :: xs -> helper (position + 1) n xs
            in helper 0 n lst;;
 
-This searches for the `n`th element of a list that satisfies the predicate `test`, and returns a pair containing the position of that element, and the element itself. Good. But now what if you wanted to retrieve a different kind of information, such as the `n`th element matching `test`, together with its preceding and succeeding elements? In a real situation, you'd want to develop some good strategy for reporting when the target element doesn't have a predecessor and successor; but we'll just simplify here and report them as having some default value:
+This searches for the `n`th element of a list that satisfies the predicate `test`, and returns a pair containing the position of that element, and the element itself. (We follow the dominant convention of counting list positions from the left starting at 0.) Good. But now what if you wanted to retrieve a different kind of information, such as the `n`th element matching `test`, together with its preceding and succeeding elements? In a real situation, you'd want to develop some good strategy for reporting when the target element doesn't have a predecessor and successor; but we'll just simplify here and report them as having some default value:
 
        let find_nth' (test : 'a -> bool) (n : int) (lst : 'a list) (default : 'a) : ('a * 'a * 'a) ->
            let rec helper (predecessor : 'a) n lst =
@@ -36,7 +36,7 @@ Here's an idea. What if we had some way of representing a list as "broken" at a
 
        [10; 20; 30; 40; 50; 60; 70; 80; 90]
 
-we might imagine the list "broken" at position 3 like this (positions are numbered starting from 0):
+we might imagine the list "broken" at position 3 like this:
 
                    40;
                30;     50;
@@ -55,7 +55,7 @@ Then if we move one step forward in the list, it would be "broken" at position 4
 
 If we had some convenient representation of these "broken" lists, then our search function could hand *that* off to the retrieval function, and the retrieval function could start right at the position where the list was broken, without having to start at the beginning and traverse many elements to get there. The retrieval function would also be able to inspect elements both forwards and backwards from the position where the list was "broken".
 
-The kind of data structure we're looking for here is called a **list zipper**. To represent our first broken list, we'd use two lists: (1) containing the elements in the left branch, preceding the target element, *in the order reverse to their appearance in the base list*. (2) containing the target element and the rest of the list, in normal order. So:
+The kind of data structure we're looking for here is called a **list zipper**. To represent our first broken list, we'd use two lists: (1) containing the elements in the left branch, preceding the target or focused element, *in the order reverse to their appearance in the base list*. (2) containing the target or focus element and the rest of the list, in normal order. So:
 
                    40;
                30;     50;
@@ -64,9 +64,9 @@ The kind of data structure we're looking for here is called a **list zipper**. T
                                    80;
                                        90]
 
-would be represented as `([30; 20; 10], [40; 50; 60; 70; 80; 90])`. To move forward in the base list, we pop the head element `40` off of the head element of the second list in the zipper, and push it onto the first list, getting `([40; 30; 20; 10], [50; 60; 70; 80; 90])`. To move backwards again, we pop off of the first list, and push it onto the second. To reconstruct the base list, we just "move backwards" until the first list is empty. (This is supposed to evoke the image of zipping up a zipper; hence the data structure's name.)
+would be represented as `([30; 20; 10], [40; 50; 60; 70; 80; 90])`. To move forward in the base list, we pop the head element `40` off of the head element of the second list in the zipper, and push it onto the first list, getting `([40; 30; 20; 10], [50; 60; 70; 80; 90])`. To move backwards again, we pop off of the first list, and push it onto the second. To reconstruct the base list, we just "moved backwards" until the first list is empty. (This is supposed to evoke the image of zipping up a zipper; hence the data structure's name.)
 
-We had some discussion in seminar of the right way to understand the "zipper" metaphor. I think it's best to think of the tab of the zipper being here:
+Last time we gave the class, we had some discussion of what's the right way to apply the "zipper" metaphor. I suggest it's best to think of the tab of the zipper being here:
 
                 t
                  a
@@ -78,31 +78,35 @@ We had some discussion in seminar of the right way to understand the "zipper" me
                                    80;
                                        90]
 
-And imagine that you're just seeing the left half of a real-zipper, rotated 60 degrees counter-clockwise. When the list is all "zipped up", we've "move backwards" to the state where the first element is targetted:
+And imagine that you're just seeing the left half of a real-zipper, rotated 60 degrees counter-clockwise. When the list is all "zipped up", we've "move backwards" to the state where the first element is targeted or in focus:
 
-       ([], [10; 20; 30; 40; 50; 60; 70; 80; 90])
+    ([], [10; 20; 30; 40; 50; 60; 70; 80; 90])
 
-However you understand the "zipper" metaphor, this is a very handy data structure, and it will become even more handy when we translate it over to more complicated base structures, like trees. To help get a good conceptual grip on how to do that, it's useful to introduce a kind of symbolism for talking about zippers. This is just a metalanguage notation, for us theorists; we don't need our programs to interpret the notation. We'll use a specification like this:
+However you understand the "zipper" metaphor, this is a very handy data structure, and it will become even more handy when we translate it over to more complicated base structures, like trees. To help get a good conceptual grip on how to do that, it's useful to introduce a kind of symbolism for talking about zippers. This is just a metalanguage notation, for us theorists.
 
-       [10; 20; 30; *; 50; 60; 70; 80; 90], * filled by 40
+    [10; 20; 30; *; 50; 60; 70; 80; 90], * filled by 40
 
-to represent a list zipper where the break is at position 3, and the element occupying that position is 40. For a list zipper, this is implemented using the pairs-of-lists structure described above.
+would represent a list zipper where the break is at position 3, and the element occupying that position is `40`. For a list zipper, this could be implemented using the pairs-of-lists structure described above.
+
+Alternatively, we could present it in a form more like we used in the seminar for tree zippers:
+
+    in_focus = 40, context = (before = [30; 20; 10], after = [50; 60; 70; 80; 90])
 
 
 ##Tree Zippers##
 
 Now how could we translate a zipper-like structure over to trees? What we're aiming for is a way to keep track of where we are in a tree, in the same way that the "broken" lists let us keep track of where we are in the base list.
 
-It's important to set some ground rules for what will follow. If you don't understand these ground rules you will get confused. First off, for many uses of trees one wants some of the nodes or leaves in the tree to be *labeled* with additional information. It's important not to conflate the label with the node itself. Numerically one and the same piece of information---for example, the same `int`---could label two nodes of the tree without those nodes thereby being identical, as here:
+It's important to set some ground rules for what will follow. If you don't understand these ground rules you will get confused. First off, for many uses of trees one wants some of the nodes or leaves in the tree to be *labeled* with additional information. It's important not to conflate the label with the node itself. Numerically one and the same piece of information --- for example, the same `int` --- could label two nodes of the tree without those nodes thereby being identical, as here:
 
                root
                / \
              /     \
-           /  \    label 1
+           /  \    label 10
          /      \
-       label 1  label 2
+       label 10 label 20
 
-The leftmost leaf and the rightmost leaf have the same label; but they are different leaves. The leftmost leaf has a sibling leaf with the label 2; the rightmost leaf has no siblings that are leaves. Sometimes when one is diagramming trees, one will annotate the nodes with the labels, as above. Other times, when one is diagramming trees, one will instead want to annotate the nodes with tags to make it easier to refer to particular parts of the tree. So for instance, I could diagram the same tree as above as:
+The leftmost leaf and the rightmost leaf have the same label; but they are different leaves. The leftmost leaf has a sibling leaf with the label `20`; the rightmost leaf has no siblings that are leaves. Sometimes when one is diagramming trees, one will annotate the nodes with the labels, as above. Other times, when one is diagramming trees, one will instead want to annotate the nodes with tags to make it easier to refer to particular parts of the tree. So for instance, I could diagram the same tree as above as:
 
                 1
                / \
@@ -111,9 +115,9 @@ The leftmost leaf and the rightmost leaf have the same label; but they are diffe
          /      \
         3        4
 
-Here I haven't drawn what the labels are. The leftmost leaf, the node tagged "3" in this diagram, doesn't have the label `3`. It has the label 1, as we said before. I just haven't put that into the diagram. The node tagged "2" doesn't have the label `2`. It doesn't have any label. The tree in this example only has information labeling its leaves, not any of its inner nodes. The identity of its inner nodes is exhausted by their position in the tree.
+Here I haven't drawn what the labels are. The leftmost leaf, the node tagged "3" in this diagram, doesn't have the label `3`. It has the label `10`, as we said before. I just haven't put that into the diagram. The node tagged "2" doesn't have the label `2`. It doesn't have any label. The tree in this example only has information labeling its leaves, not any of its inner nodes. The identity of its inner nodes is exhausted by their position in the tree.
 
-That is a second thing to note. In what follows, we'll only be working with *leaf-labeled* trees. In some uses of trees, one also wants labels on inner nodes. But we won't be discussing any such trees now. Our trees only have labels on their leaves. The diagrams below will tag all of the nodes, as in the second diagram above, and won't display what the leaves' labels are.
+That is a second thing to note. In what follows, we'll only be working with *leaf-labeled* trees. In some uses of trees, one also (or sometimes, only) wants labels on inner nodes. But we won't be discussing any such trees now. Our trees only have labels on their leaves. The diagrams below will tag all of the nodes, as in the second diagram above, and won't display what the leaves' labels are.
 
 Final introductory comment: in particular applications, you may only need to work with binary trees---trees where internal nodes always have exactly two subtrees. That is what we'll work with in the homework, for example. But to get the guiding idea of how tree zippers work, it's helpful first to think about trees that permit nodes to have many subtrees. So that's how we'll start.
 
@@ -134,117 +138,90 @@ This is a leaf-labeled tree whose labels aren't displayed. The `9200` and so on
 
 Suppose we want to represent that we're *at* the node marked `50`. We might use the following metalanguage notation to specify this:
 
-       {parent = ...; siblings = [subtree 20; *; subtree 80]}, * filled by subtree 50
+    in_focus = subtree rooted at 50,
+    context = (up = ..., left_siblings = [subtree rooted at 20], right_siblings = [subtree rooted at 80])
 
-This is modeled on the notation suggested above for list zippers. Here `subtree 20` refers to the whole subtree rooted at node `20`:
+This is modeled on the notation suggested above for list zippers. Here "subtree rooted at 20" means the whole subtree underneath node `20`:
 
-         20
-        / | \
-       1  2  3
+      20
+     / | \
+    1  2  3
 
-Similarly for `subtree 50` and `subtree 80`. We haven't said yet what goes in the `parent = ...` slot. Well, the parent of a subtree targetted on `node 50` should intuitively be a tree targetted on `node 500`:
+For brevity, we'll just call this `subtree 20`; and similarly for `subtree 50` and `subtree 80`. We'll also abbreviate `left_siblings = [subtree 20], right_siblings = [subtree 80]` to just:
 
-       {parent = ...; siblings = [*; subtree 920; subtree 950]}, * filled by subtree 500
+    siblings = [subtree 20; *; subtree 80]
 
-And the parent of that targetted subtree should intuitively be a tree targetted on `node 9200`:
+The `*` marks where the left siblings stop and the right siblings start.
 
-       {parent = None; siblings = [*]}, * filled by tree 9200
+We haven't said yet what goes in the `up = ...` slot. But if you think about it, the parent of the context centered on `node 50` should intuitively be the context centered on `node 500`:
 
-This tree has no parents because it's the root of the base tree. Fully spelled out, then, our tree targetted on `node 50` would be:
+       (up = ..., siblings = [*; subtree 920; subtree 950])
 
-       {
-          parent = {
-             parent = {
-                parent = None;
-                siblings = [*]
-             }, * filled by tree 9200;
-             siblings = [*; subtree 920; subtree 950]
-          }, * filled by subtree 500;
-          siblings = [subtree 20; *; subtree 80]
-       }, * filled by subtree 50
+And the parent of that context should intuitively be a context centered on `node 9200`. This context has no left or right siblings, and there is no going further up from it. So let's mark it as a special context, that we'll call:
 
-In fact, there's some redundancy in this structure, at the points where we have `* filled by tree 9200` and `* filled by subtree 500`. Since node 9200 doesn't have any label attached to it, the subtree rooted in it is determined by the rest of this structure; and so too with `subtree 500`. So we could really work with:
+       Root
 
-       {
-          parent = {
-             parent = {
-                parent = None;
-                siblings = [*]
-             },
-             siblings = [*; subtree 920; subtree 950]
-          },
-          siblings = [subtree 20; *; subtree 80]
-       }, * filled by subtree 50
+Fully spelled out, then, our tree focused on `node 50` would look like this:
 
+    in_focus = subtree 50,
+    context = (up = (up = Root,
+                     siblings = [*; subtree 920; subtree 950]),
+               siblings = [subtree 20; *; subtree 80])
 
-We still do need to keep track of what fills the outermost targetted position---`* filled by subtree 50`---because that contain a subtree of arbitrary complexity, that is not determined by the rest of this data structure.
+For brevity, we may sometimes write like this, using ellipsis and such:
 
-For simplicity, I'll continue to use the abbreviated form:
+       up = ..., siblings = [subtree 20; *; subtree 80], * filled by subtree 50
 
-       {parent = ...; siblings = [subtree 20; *; subtree 80]}, * filled by subtree 50
+But that should be understood as standing for the more fully-spelled-out structure.
 
-But that should be understood as standing for the more fully-spelled-out structure. Structures of this sort are called **tree zippers**. They should already seem intuitively similar to list zippers, at least in what we're using them to represent. I think it may also be helpful to call them **targetted trees**, though, and so will be switching back and forth between these different terms.
+Structures of this sort are called **tree zippers**. They should already seem intuitively similar to list zippers, at least in what we're using them to represent. It may also be helpful to call them **focused trees**, though, and so will be switching back and forth between these different terms.
 
-Moving left in our targetted tree that's targetted on `node 50` would be a matter of shifting the `*` leftwards:
+Moving left in our tree focused on `node 50` would be a matter of shifting the `*` leftwards:
 
-       {parent = ...; siblings = [*; subtree 50; subtree 80]}, * filled by subtree 20
+    up = ..., siblings = [*; subtree 50; subtree 80], * filled by subtree 20
 
 and similarly for moving right. If the sibling list is implemented as a list zipper, you should already know how to do that. If one were designing a tree zipper for a more restricted kind of tree, however, such as a binary tree, one would probably not represent siblings with a list zipper, but with something more special-purpose and economical.
 
-Moving downward in the tree would be a matter of constructing a tree targetted on some child of `node 20`, with the first part of the targetted tree above as its parent:
+Moving downward in the tree would be a matter of constructing a tree focused on some child of `node 20`, with the context part of the focused tree above (everything but the specification of the element in focus) as its `up`:
 
-       {
-          parent = {parent = ...; siblings = [*; subtree 50; subtree 80]};
-          siblings = [*; leaf 2; leaf 3]
-       }, * filled by leaf 1
+       up = (up = ..., siblings = [*; subtree 50; subtree 80]),
+       siblings = [*; leaf 2; leaf 3],
+       * filled by leaf 1
 
-How would we move upward in a tree? Well, we'd build a regular, untargetted tree with a root node---let's call it `20'`---and whose children are given by the outermost sibling list in the targetted tree above, after inserting the targetted subtree into the `*` position:
+How would we move upward in a tree? Well, to move up from the focused tree just displayed (focused on leaf `1`), we'd build a regular, unfocused tree with a root node --- let's call it `20'` --- whose children are given by the outermost sibling list in the focused tree above (`[*; leaf 2; leaf 3]`), after inserting the currently focused subtree (`leaf 1`) into the `*` position:
 
               node 20'
            /     |    \
         /        |      \
        leaf 1  leaf 2  leaf 3
 
-We'll call this new untargetted tree `subtree 20'`. The result of moving upward from our previous targetted tree, targetted on `leaf 1`, would be the outermost `parent` element of that targetted tree, with `subtree 20'` being the subtree that fills that parent's target position `*`:
+Call the unfocused tree just specified `subtree 20'`. (It's the same as `subtree 20` was before. We just give it a different name because `subtree 20` wasn't a component we could extract from the previous zipper. We had to rebuild it from the information the previous zipper encoded.) The result of moving upward from our previous focused tree, focused on `leaf 1`, would be a tree focused on the subtree just described, with the context being the outermost `up` element of the previous focused tree (what's written above as `(up = ..., siblings = [*; subtree 50; subtree 80])`. That is:
 
-       {
-          parent = ...;
-          siblings = [*; subtree 50; subtree 80]
-       }, * filled by subtree 20'
+    up = ...,
+    siblings = [*; subtree 50; subtree 80],
+    * filled by subtree 20'
 
 Or, spelling that structure out fully:
 
-       {
-          parent = {
-             parent = {
-                parent = None;
-                siblings = [*]
-             },
-             siblings = [*; subtree 920; subtree 950]
-          },
-          siblings = [*; subtree 50; subtree 80]
-       }, * filled by subtree 20'
+    in_focus = subtree 20',
+    context = (up = (up = Root,
+                     siblings = [*; subtree 920; subtree 950]),
+               siblings = [*; subtree 50; subtree 80])
 
 Moving upwards yet again would get us:
 
-       {
-          parent = {
-             parent = None;
-             siblings = [*]
-          },
-          siblings = [*; subtree 920; subtree 950]
-       }, * filled by subtree 500'
+    in_focus = subtree 500',
+    context = (up = Root,
+               siblings = [*; subtree 920; subtree 950])
 
-where `subtree 500'` refers to a tree built from a root node whose children are given by the list `[*; subtree 50; subtree 80]`, with `subtree 20'` inserted into the `*` position. Moving upwards yet again would get us:
+where `subtree 500'` refers to a subtree built from a root node whose children are given by the list `[*; subtree 50; subtree 80]`, with `subtree 20'` inserted into the `*` position. Moving upwards yet again would get us:
 
-       {
-          parent = None;
-          siblings = [*]
-       }, * filled by tree 9200'
+    in_focus = subtree 9200',
+    context = Root
 
-where the targetted element is the root of our base tree. Like the "moving backward" operation for the list zipper, this "moving upward" operation is supposed to be reminiscent of closing a zipper, and that's why these data structures are called zippers.
+where the focused node is exactly the root of our complete tree. Like the "moving backward" operation for the list zipper, this "moving upward" operation is supposed to be reminiscent of closing a zipper, and that's why these data structures are called zippers.
 
-We haven't given you a real implementation of the tree zipper, but only a suggestive notation. We have however told you enough that you should be able to implement it yourself. Or if you're lazy, you can read:
+We haven't given you an executable implementation of the tree zipper, but only a suggestive notation. We have however told you enough that you should be able to implement it yourself. Or if you're lazy, you can read:
 
 *      [[!wikipedia Zipper (data structure)]]
 *      [Haskell wikibook on zippers](http://en.wikibooks.org/wiki/Haskell/Zippers)