Merge branch 'working'
authorJim <jim.pryor@nyu.edu>
Wed, 29 Apr 2015 14:51:17 +0000 (10:51 -0400)
committerJim <jim.pryor@nyu.edu>
Wed, 29 Apr 2015 14:51:17 +0000 (10:51 -0400)
* working:
  refunct zippers code

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
topics/week13_from_list_zippers_to_continuations.mdwn [moved from topics/_from_list_zippers_to_continuations.mdwn with 100% similarity]

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 f09556f..0000000
+++ /dev/null
@@ -1,135 +0,0 @@
-1.     Complete the definitions of `move_botleft` and `move_right_or_up` from the same-fringe solution in the [[week11]] notes. **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 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 *)
-
-
-               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 *)
-
-
-               let new_zipper (t : 'a tree) : 'a zipper =
-                       {level = Root; filler = t}
-                       ;;
-
-       &nbsp;
-
-               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
-                       ;;
-
-               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 ()
-                       ;;
-
-
-2.     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:
-
-       *       `#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)
-
-
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 6976a51..7cccacb 100644 (file)
@@ -1,4 +1,118 @@
-#Aborting a search through a list#
+We've talked about different implementations of lists in the Lambda Calculus; and we've also talked about using lists to implement other data structures, like sets. One thing we observed is that if you're going to implement a set using a list (this isn't an especially efficient implementation of a set, but let's do the best we can with it), it's helpful to make sure that the list is always sorted. That way, as you're searching through the list, you might come to a point before the end where you knew the element you're looking for wasn't going to be found anymore. So you wouldn't have to continue the search.
+
+If you were implementing lists with `letrec` or a fixed point combinator, then at that point you could just have your traversal function return some appropriate result, instead of requesting that the recursion continue.
+
+But what if you were using our right-fold or left-fold implementation of lists, rather than using `letrec` or a fixed point combinator? It's true that `letrec` and fixed point combinators are cool. But if you use them, the terms you construct won't be strongly normalizing. Whether their reduction stops at a normal form will depend on what evaluation order is operative. For efficiency, it can also be helpful to know how to get by without those powerful devices when they're not strictly necessary. Plus that knowledge could carry over to settings where `letrec` is no longer available, even in principle.
+
+When dealing with a right-fold or left-fold implementation of lists, if your traversal function returns a result, that result automatically gets passed to the next stage of the traversal, as the updated seed value from the previous steps. If we make the seed value type complex, we could signal to the rest of the traversal that the job is done, they don't need to do any more work. For example, the seed value might be a pair of `false` and some default value until we reach a certain stage, and all the traversal steps until that stage have to do their normal work, but once we've gotten to the stage where we're ready to return a result, we make the seed value be a pair of `true` and the result we've computed. Then all the later traversal steps see that `true` and just pass the existing seed pair on to the rest of the traversal. At the end, we throw away the `true` and take the second member of the final seed value as our desired result.
+
+That will work OK, but we still have to go through every step of the traversal. Let's think about whether we can modify the right-fold and/or left-fold implementation of lists to allow for a genuine early abort. We'd like to avoid any unnecessary steps of the traversal. If we've worked out our result mid-way through the traversal, we want to be able to deliver it _immediately_ to the larger computation in which our traversal was embedded. The problem with the fold-based implementations of lists we've got so far is that they are pre-programmed to traverse the whole list. We'd have to implement the folds differently to be able to achieve what we're now envisaging.
+
+We worked out such an implementation in the homework session on Wed April 22. The scheme we used was that, whereas before our traversal functions would have an interface like this:
+
+    \current_list_element seed_value_so_far. do_something
+
+Our traversal functions will instead now have an interface like this:
+
+    \current_list_element seed_value_so_far done_handler keep_going_handler.
+      if ... then done_handler (some_result) else keep_going_handler (another_result)
+
+and we worked out that a left-fold implementation of the list `[10,20,30,40]` could look like this:
+
+    \f z done_handler. f 10 z done_handler (\z. [20,30,40] f z done_handler)
+
+This is all reminiscent of the way in which our encodings of triples and pairs and such too their handler functions as arguments. Remember with triples, we say:
+
+    triple (\x y z. add x y)
+
+or:
+
+    triple (\x y z. z)
+
+to get the last element of the triple. Of course you can wrap this up in _another_ function, that takes the triple as _its_ argument, but at bottom, you are going to be supplying the handler to the triple. And then the handler gets the triple's _elements_, not the triple itself, as arguments. (Another of our implementations of lists followed a similar strategy.)
+
+Something similar is happening here, where both the traversal function `f` and the fold function that implements the list both take such handler arguments. The only not-completely-straightforward thing going on in the fold function is that the `keep_going_handler` we pass to the traversal function `f` *encodes* how the fold should continue, using the updated seed value `z` from the current step, *without actually computing* the rest of the traversal. It's up to the body of `f` to decide whether to invoke the rest of the traversal, by supplying a value to that `keep_going_handler`, or to finish the traversal right now by supply a value to the `done_handler` instead.
+
+A question came up in the session of why we need the `done_handler` in this scheme. We could just eliminate it and have the traversal function `f` choose between simply returning a value --- that'd abort the traversal, the way we did above by passing the value to `done_handler` --- or instead supplying a value to the `keep_going_handler`. And the answer is that yes, in this particular scheme, that is correct. (In a few weeks when we look at delimited continuations in terms of `reset`/`shift`, you'll see that what we just described is how the `abort` operation gets implemented in terms of `shift`.) However, sometimes it helps to express a basic case a bit more verbosely than seems immediately necessary, because then later generalizations will look more natural. That's true here. So let's just use the implementation as we've written it. If you prefer, you can just make the `done_handler` be the identity function.
+
+With that general scheme, here is the empty list:
+
+    \f z done_handler. done_handler z
+
+and here is the `cons` operation:
+
+    \x xs. \f z done_handler. f x z done_handler (\z. xs f z done_handler)
+
+(The latter can just be read off of our construction of `[10,20,30,40]`; I just substituted `x` for `10` and `xs` for `[20, 30, 40]`.)
+
+Here's an example of how to get the head of such a list:
+
+    xs (\x z done keep_going. done x) err done_handler
+
+`err` is what's returned if you ask for the `head` of the empty list.
+
+Here's how to get the length of such a list:
+
+    xs (\x z done keep_going. keep_going (succ z)) 0 done_handler
+
+Here there is no opportunity to abort early with a correct value, so our traversal function always delivers its output to the `keep_going` handler.
+
+Here's how to get the last element of such a list:
+
+    xs (\x z done keep_going. keep_going x) err done_handler
+
+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 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):
+
+    \f z d. f 10 z d (\z. [20,30,40] f z d)
+
+Expanding the definition of `[20,30,40]`, and all the successive tails, this comes to:
+
+    \f z d. f 10 z d (\z. f 20 z d (\z. f 30 z d (\z. f 40 z d d)))
+
+For a right-fold implementation, that should instead look like roughly like this:
+
+    \f z d. f 40 z d (\z. f 30 z d (\z. f 20 z d (\z. f 10 z d d)))
+
+Now suppose we had just the implementation of the tail of the list, `[20,30,40]`, that is:
+
+    \f z d. f 40 z d (\z. f 30 z d (\z. f 20 z d d))
+
+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 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))
+
+Notice that now the fold function we implement the list as takes *two* handlers, `d` (for "done") and `g` (for "keep going"). Generally we'll *invoke* the fold function *by supplying the same handler function to both of these*. However, it's still useful to have the list be defined so that they're separate arguments. For now we can `cons` `10` onto the list by just substituting `(\z. f 10 z d g)` in for the bound `g`. That is:
+
+    [10,20,30,40] ≡ \f z d g. [20,30,40] f z d (\z. f 10 z d g)
+
+Spelling this out, here are the implementations of the functions we defined before, only now for the right-fold lists:
+
+    null = \f z d g. g z
+    cons x xs = \f z d g. xs f z d (\z. f x z d g)
+    head xs = xs (\x z d g. g x) err done_handler done_handler
+    length xs = xs (\x z d g. g (succ z)) 0 done_handler done_handler
+    last xs = xs (\x z d g. d x) err done_handler done_handler
+
+*Exercise*: when considering just the implementation of `null`, both `\f z d g. g z` and `\f z d g. d z` may seem like reasonable candidates. What would go wrong with the rest of our scheme is we had instead used the latter?
+
+To extract tails efficiently, too, it'd be nice to merge the apparatus developed above with the ideas from [[another of our implementations of lists|week3_lists#v3-lists]], where we passed the traversal function `f` not merely the current element and *result of traversing the list's tail* (or in the present case, a modified `keep_going` handler which encodes how to do that), but also *the list's tail itself*. That would make some computations more efficient. But we leave this as an exercise.
+
+Of course, like everything elegant and exciting in this seminar, [Oleg
+discusses it in much more
+detail](http://okmij.org/ftp/Streams.html#enumerator-stream).
+
+
+
+
+
+<!--
 
 We said that the sorted-list implementation of a set was more efficient than
 the unsorted-list implementation, because as you were searching through the
@@ -298,11 +412,8 @@ What we've done here does take some work to follow. But it should be within
 your reach. And once you have followed it, you'll be well on your way to
 appreciating the full terrible power of continuations.
 
-<!-- (Silly [cultural reference](http://www.newgrounds.com/portal/view/33440).) -->
+(Silly [cultural reference](http://www.newgrounds.com/portal/view/33440).)
 
-Of course, like everything elegant and exciting in this seminar, [Oleg
-discusses it in much more
-detail](http://okmij.org/ftp/Streams.html#enumerator-stream).
 
 >      *Comments*:
 
@@ -357,3 +468,4 @@ detail](http://okmij.org/ftp/Streams.html#enumerator-stream).
 >      developed in these v5 lists with the ideas from 
 >      [v4](/advanced_lambda/#index1h1) lists. But that is left as an exercise.
 
+-->
index 1eb5d07..89d83b6 100644 (file)
@@ -1,3 +1,5 @@
+<!-- λ ◊ ≠ ∃ Λ ∀ ≡ α β γ ρ ω φ ψ Ω ○ μ η δ ζ ξ ⋆ ★ • ∙ ● ⚫ 𝟎 𝟏 𝟐 𝟘 𝟙 𝟚 𝟬 𝟭 𝟮 ⇧ (U+2e17) ¢ -->
+
 [[!toc]]
 
 ##List Zippers##
@@ -15,7 +17,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 +38,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 +57,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 +66,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,177 +80,226 @@ 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:
-
-       ([], [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:
-
-       [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.
-
-
-##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:
-
-               root
-               / \
-             /     \
-           /  \    label 1
-         /      \
-       label 1  label 2
-
-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:
-
-                1
-               / \
-             2     \
-           /  \     5
-         /      \
-        3        4
+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:
 
-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.
+    ([], [10; 20; 30; 40; 50; 60; 70; 80; 90])
 
-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.
+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.
 
-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.
+    [10; 20; 30; *; 50; 60; 70; 80; 90], * filled by 40
 
-Suppose we have the following tree:
+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.
 
-                                9200
-                           /      |  \
-                        /         |    \
-                     /            |      \
-                  /               |        \
-               /                  |          \
-              500                920          950
-           /   |    \          /  |  \      /  |  \
-        20     50     80      91  92  93   94  95  96
-       1 2 3  4 5 6  7 8 9
+Alternatively, we could present it in a form more like we used in the seminar for tree zippers:
 
-This is a leaf-labeled tree whose labels aren't displayed. The `9200` and so on are tags to make it easier for us to refer to particular parts of the tree.
+    in_focus = 40, context = (before = [30; 20; 10], after = [50; 60; 70; 80; 90])
 
-Suppose we want to represent that we're *at* the node marked `50`. We might use the following metalanguage notation to specify this:
+In order to facilitate the discussion of tree zippers, 
+let's consolidate a bit with a concrete implementation.
 
-       {parent = ...; siblings = [subtree 20; *; subtree 80]}, * filled by subtree 50
+<pre>
+type int_list_zipper = int * (int list) * (int list)
+let zip_open (z:int_list_zipper):int_list_zipper = match z with
+    focus, ls, r::rs -> r, focus::ls, rs
+  | _ -> z
+let zip_closed (z:int_list_zipper):int_list_zipper = match z with
+    focus, l::ls, rs -> l, ls, focus::rs
+</pre>
 
-This is modeled on the notation suggested above for list zippers. Here `subtree 20` refers to the whole subtree rooted at node `20`:
+Here, an int list zipper is an int list with one element in focus.
+The context of that element is divided into two subparts: the left
+context, which gives the elements adjacent to the focussed element
+first (so looks reversed relative to the original list); and the right
+context, which is just the remainder of the list to the right of the
+focussed element.
 
-         20
-        / | \
-       1  2  3
+Then we have the following behavior:
 
-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`:
+<pre>
+# let z1:int_list_zipper = 1, [], [2;3;4];;
+val z1 : int_list_zipper = (1, [], [2; 3; 4])
+# let z2 = zip_open z1;;
+val z2 : int_list_zipper = (2, [1], [3; 4])
+# let z3 = zip_open z2;;
+val z3 : int_list_zipper = (3, [2; 1], [4])
+# let z4 = zip_closed (zip_closed z3);;
+val z4 : int_list_zipper = (1, [], [2; 3; 4])
+# z4 = z1;;
+# - : bool = true
+</pre>
 
-       {parent = ...; siblings = [*; subtree 920; subtree 950]}, * filled by subtree 500
 
-And the parent of that targetted subtree should intuitively be a tree targetted on `node 9200`:
 
-       {parent = None; siblings = [*]}, * filled by tree 9200
-
-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:
-
-       {
-          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
-
-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:
-
-       {
-          parent = {
-             parent = {
-                parent = None;
-                siblings = [*]
-             },
-             siblings = [*; subtree 920; subtree 950]
-          },
-          siblings = [subtree 20; *; subtree 80]
-       }, * filled by subtree 50
-
-
-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 simplicity, I'll continue to use the abbreviated form:
-
-       {parent = ...; siblings = [subtree 20; *; subtree 80]}, * filled by subtree 50
-
-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.
+##Tree Zippers##
 
-Moving left in our targetted tree that's targetted on `node 50` would be a matter of shifting the `*` leftwards:
+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.
 
-       {parent = ...; siblings = [*; subtree 50; subtree 80]}, * filled by subtree 20
+Thus tree zippers are analogous to list zippers, but with one
+additional dimension to deal with: in addition to needing to shift
+focus to the left or to the right, we want to be able to shift the
+focus up or down.
 
-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.
+In order to emphasize the similarity with list zippers, we'll use
+trees that are conceived of as lists of lists:
 
-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:
+    type tree = Leaf of int | Branch of tree list
 
-       {
-          parent = {parent = ...; siblings = [*; subtree 50; subtree 80]};
-          siblings = [*; leaf 2; leaf 3]
-       }, * filled by leaf 1
+On this conception, a tree is nothing more than a list of subtrees.
+For instance, we might have
 
-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:
+    let t1 = Branch [Leaf 1; Branch [Branch [Leaf 2; Leaf 3]; Leaf 4]];;
 
-              node 20'
-           /     |    \
-        /        |      \
-       leaf 1  leaf 2  leaf 3
+    _|__
+    |  |
+    1  |
+      _|__
+      |  |
+      |  4
+     _|__
+     |  |
+     2  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 `*`:
+For simplicity, we'll work with trees that don't have labels on their
+internal nodes.  Note that there can be any number of siblings, though
+we'll work with binary trees here to prevent clutter.
 
-       {
-          parent = ...;
-          siblings = [*; subtree 50; subtree 80]
-       }, * filled by subtree 20'
+    _*__
+    |  |
+    1  |
+      _|__
+      |  |
+      |  4
+     _|__
+     |  |
+     2  3
 
-Or, spelling that structure out fully:
+How should we represent a tree with the starred subtree in focus?
+Well, that's easy: the focussed element is the entire tree, and the
+context is null.  We'll represent this as the ordered pair (t1, Root).
 
-       {
-          parent = {
-             parent = {
-                parent = None;
-                siblings = [*]
-             },
-             siblings = [*; subtree 920; subtree 950]
-          },
-          siblings = [*; subtree 50; subtree 80]
-       }, * filled by subtree 20'
+    _|__
+    |  |
+    1  |
+      _*__
+      |  |
+      |  4
+     _|__
+     |  |
+     2  3
+
+How should we represent a tree with this other starred subtree in
+focus?  Well, viewing this tree as a list of subtrees, we've merely
+put the second element of the list in focus.  We can almost just use
+the list zipper technique from the previous section:
+
+    Branch (Branch (Leaf 2, Leaf 3), Leaf 4), ([Leaf 1], [])
+
+This is just a list zipper where the list elements are trees instead
+of ints.
+
+But this won't be quite enough if we go one more level down.
+
+    _|__
+    |  |
+    1  |
+      _|__
+      |  |
+      |  4
+     _*__
+     |  |
+     2  3
+
+The focussed element is the subtree Branch (Leaf 2, Leaf 3).
+And we know how to represent the part of the context that involves the
+siblings of the focussed tree:
+
+    Branch (Leaf 2, Leaf 3), ([], [Leaf 4])
+
+We still need to add the rest of the context.  But we just computed that
+context a minute ago.  It was ([Leaf 1], []).  If we add it here, we get:
+
+    Branch (Leaf 2, Leaf 3), ([], [Leaf 4], ([Leaf 1], [])
+
+Here's the type suggested by this idea:
+
+    type context = Root | Context of (tree list) * (tree list) * context
+    type zipper = tree * context
+
+We can gloss the triple `(tree list) * (tree list) * context` as 
+`(left siblings) * (right siblings) * (context of parent)`.
+
+Here, then, is the full tree zipper we've been looking for:
+
+    (Branch [Leaf 2; Leaf 3],
+     Context ([], [Leaf 4], Context ([Leaf 1], [], Root)))
+
+Just as with the simple list zipper, note that elements that are near
+the focussed element in the tree are near the focussed element in the
+zipper representation.  This is what makes it easy to shift the
+focus to nearby elements in the tree.
+
+It should be clear that moving left and right in the tree zipper is
+just like moving left and right in a list zipper. 
+
+Moving down requires looking inside the tree in focus, and grabbing
+hold of one of its subtrees.  Since that subtree is the new focus, its
+context will be the list zipper consisting of its siblings (also
+recovered from the original focus).
+
+    let downleft (z:zipper):zipper = match z with
+        (Branch (l::rest)), context -> l, Context ([], rest, context)
+
+Moving up involves gathering up the left and right siblings and
+re-building the original subtree.  It's easiest to do this when the
+sibling zipper is fully closed, i.e., when the list of left siblings
+is empty:
+
+    let rec up (z:zipper):zipper = match z with
+        focus, Context ([], rs, rest) -> Branch (focus::rs), rest
+      | focus, Context (l::ls, _, _) -> up (left z)
+
+The second match says that if the list of left siblings isn't empty,
+we just shift focus left and try again.
+
+This tree zipper works for trees with arbitrary numbers of siblings
+per subtree.  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.
+
+With these functions, we can refocus on any part of the tree.
+Let's abbreviate a tree zipper like this:
 
-Moving upwards yet again would get us:
+    [2;3], ([] [4] ([1], [], Root)) 
 
-       {
-          parent = {
-             parent = None;
-             siblings = [*]
-          },
-          siblings = [*; subtree 920; subtree 950]
-       }, * filled by subtree 500'
+    ≡ (Branch [Leaf 2; Leaf 3],
+       Context ([], [Leaf 4], Context ([Leaf 1], [], Root)))
 
-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:
+Then we can take a tour of the original tree like this:
 
-       {
-          parent = None;
-          siblings = [*]
-       }, * filled by tree 9200'
+<pre>
+_|__
+|  |
+1  |
+  _|__
+  |  |
+  |  4
+ _|__
+ |  |
+ 2  3
 
-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.
+    [1;[[2;3];4]],Root           =                                                              [1;[[2;3];4]],Root
+  downleft                                                                                               up
+1, ([],[[2;3];4],Root) right [[2;3];4],([1],[],Root)                                            [[2;3];4],([1],[],Root)
+                           downleft                                                                      up                                
+                        [2;3],([],[4],([1],[],Root))         [2;3],([],[4],([1],[],Root)) right 4,([2;3],[],([1],[],Root))
+                      downleft                                           up
+                    2, ([],[3],([],[4],([1],[],Root))) right 3, ([2],[],([],[4],([1],[],Root)))
+</pre>
 
-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:
+Here's more on zippers:
 
 *      [[!wikipedia Zipper (data structure)]]
 *      [Haskell wikibook on zippers](http://en.wikibooks.org/wiki/Haskell/Zippers)
 *      Huet, Gerard. ["Functional Pearl: The Zipper"](http://www.st.cs.uni-sb.de/edu/seminare/2005/advanced-fp/docs/huet-zipper.pdf) Journal of Functional Programming 7 (5): 549-554, September 1997.
 *      As always, [Oleg](http://okmij.org/ftp/continuations/Continuations.html#zipper) takes this a few steps deeper.
-
-