add some answers
[lambda.git] / exercises / assignment5_answers.mdwn
index 34a7dda..e7b8274 100644 (file)
@@ -1,14 +1,5 @@
 <!-- λ Λ ∀ ≡ α β ω Ω -->
 
-This is a long and substantial assignment. On the one hand, it doesn't have any stumpers like we gave you in past weeks, such as defining `pred` or mutual recursion. (Well, we *do* ask you to define `pred` again, but now you understand a basic strategy for doing so, so it's no longer a stumper.) On the other hand, there are a bunch of problems; many of them demand a modest amount of time; and this week you're coming to terms with both System F *and* with OCaml or Haskell. So it's a lot to do.
-
-The upside is that there won't be any new homework assigned this week, and you can take longer to complete this assignment if you need to. As always, don't get stuck on the "More challenging" questions if you find them too hard. Be sure to send us your work even if it's not entirely completed, so we can see where you are. And consult with us (over email or in person on Wednesday) about things you don't understand, especially issues you're having working with OCaml or Haskell, or with translating between System F and them. We will update this homework page with clarifications or explanations that your questions prompt.
-
-We will also be assigning some philosophy of language readings for you to look at before this Thursday's seminar meeting.
-
-Those, and lecture notes from this past week, will be posted shortly.
-
-
 
 ## Option / Maybe Types ##
 
@@ -53,6 +44,13 @@ The OCaml and Haskell solution is to use not supermarket dividers but instead th
 
     You may want to review [[Rosetta pages|/rosetta1]] and also read some of the tutorials we linked to for [[/learning OCaml]] or [[/learning Haskell]].
 
+    HERE IS AN OCAML ANSWER:
+
+        let maybe_map (f: 'a -> 'b) -> (u : 'a option) : 'b option =
+          match u with
+          | Some a -> Some (f a)
+          | None -> None
+
 
 2.  Next write a `maybe_map2` function. Its type should be:
 
@@ -63,6 +61,15 @@ The OCaml and Haskell solution is to use not supermarket dividers but instead th
         maybe_map2 :: (a -> b -> c) -> Maybe a -> Maybe b -> Maybe c
 
 
+    HERE IS AN OCAML ANSWER:
+
+        let maybe_map2 (f : 'a -> 'b -> 'c) (u : 'a option) (v : 'b option) : 'c option =
+          match (u, v) with
+          | Some a, Some b -> Some (f a b)
+          | None, _ -> None
+          | _, None -> None
+
+
 
 ## Color Trees ##
 
@@ -93,12 +100,12 @@ Here's how you pattern match such a tree, binding variables to its components:
     (* OCaml *)
     match t with
     | Leaf n -> false
-    | Branch (_, c, _) -> c = Red
+    | Branch (_, col, _) -> col = Red
 
     -- Haskell
     case t of {
       Leaf n -> False;
-      Branch _ c _ -> c == Red
+      Branch _ col _ -> col == Red
     }
 
 These expressions query whether `t` is a branching `color_tree` (that is, not a leaf) whose root is labeled `Red`.
@@ -110,22 +117,91 @@ Choose one of these languages and write the following functions.
 
 3.  Define a function `tree_map` whose type is (as shown by OCaml): `('a -> 'b) -> ('a) color_tree -> ('b) color_tree`. It expects a function `f` and an `('a) color_tree`, and returns a new tree with the same structure and inner branch colors as the original, but with all of its leaves now having had `f` applied to their original value. So for example, `map (fun x->2*x) t1` would return `t1` with all of its leaf values doubled.
 
+    HERE IS A DIRECT OCAML ANSWER:
+
+        let rec tree_map (f: 'a -> 'b) (t: 'a color_tree) : 'b color_tree =
+          match t with
+          | Leaf a -> Leaf (f a)
+          | Branch (l,col,r) ->
+              let l' = tree_map f l in
+              let r' = tree_map f r in
+              Branch(l', col, r')
+
+    IT MIGHT BE USEFUL TO GENERALIZE THIS PATTERN, LIKE SO:
+
+        let rec tree_walker (leaf_handler: 'a -> 'h) (joiner : 'h -> color -> 'h -> 'h) (t: 'a color_tree) : 'h =
+          match t with
+          | Leaf a -> leaf_handler a
+          | Branch (l,col,r) ->
+              let lh = tree_walker leaf_handler joiner l in
+              let rh = tree_walker leaf_handler joiner r in
+              joiner lh col rh
+
+    <!-- traverse (k: a-> Mb) t = Leaf a -> map Leaf (k a) | Branch(l,col,r) -> map3 Branch l' c' r' -->
+
+    THEN `tree_map f t` can be defined as `tree_walker (fun a -> Leaf (f a)) (fun l' col r' -> Branch(l',col,r')) t`
+
+
 4.  Define a function `tree_foldleft` that accepts an argument `g : 'z -> 'a -> 'z` and a seed value `z : 'z` and a tree  `t : ('a) color_tree`, and returns the result of applying `g` first to `z` and `t`'s leftmost leaf, and then applying `g` to *that result* and `t`'s second-leftmost leaf, and so on, all the way across `t`'s fringe. In our examples, only the leaf values affect the result; the inner branch colors are ignored.
 
+    HERE IS A DIRECT OCAML ANSWER:
+
+        let rec tree_foldleft (g: 'z -> 'a -> 'z) (z : 'z) (t: 'a color_tree) : 'z =
+          match t with
+          | Leaf a -> g z a
+          | Branch (l,_,r) ->
+              let z' = tree_foldleft g z l in
+              let z'' = tree_foldleft g z' r in
+              r''
+
+    HERE IS AN ANSWER THAT RE-USES OUR `tree_walker` FUNCTION FROM THE PREVIOUS ANSWER:
+
+        let tree_foldleft g z t =
+          let leaf_handler a = fun z -> g z a in
+          let joiner lh _ rh = fun z -> rh (lh z) in
+          let expects_z = tree_walker leaf_handler joiner t in
+          expects_z z
+
+    <!-- traverse (k: a-> Mb) t = Leaf a -> map Leaf (k a) | Branch(l,col,r) -> map3 Branch l' c' r' -->
+    <!-- linearize (f: a->Mon) t = Leaf a -> f a | Branch(l,col,r) -> l' && [col' &&] r' -->
+
+    If you look at the definition of `tree_walker` above, you'll see that its interface doesn't supply the `leaf_handler` function with any input like z; the `leaf_handler` only gets the content of each leaf to work on. Thus we're forced to make our `leaf_handler` return a function, that will get its `z` input later. (The strategy used here is like [[the strategy for reversing a list using fold_right in assignment2|assignment2_answers/#cps-reverse]].) Then the `joiner` function chains the results of handling the two branches together, so that when the seed `z` is supplied, we feed it first to `lh` and then the result of that to `rh`. The result of processing any tree then will be a function that expects a `z` argument. Finally, we supply the `z` argument that `tree_foldleft` was invoked with.
+
+
 5.  How would you use the function defined in problem 4 (the previous problem) to sum up the values labeling the leaves of an `(int) color_tree`?
 
+    ANSWER: `tree_foldleft (fun z a -> z + a) 0 your_tree`
+
 6.  How would you use the function defined in problem 4 to enumerate a tree's fringe? (Don't worry about whether it comes out left-to-right or right-to-left.)
 
+    ANSWER: `tree_foldleft (fun z a -> a :: z) [] your_tree`
+
 7.  Write a recursive function to make a copy of a `color_tree` with the same structure and inner branch colors, but where the leftmost leaf is now labeled `0`, the second-leftmost leaf is now labeled `1`, and so on. (Here's a [[hint|assignment5 hint3]], if you need one.)
 
-Hint: Consider this pattern:
+    HERE IS A DIRECT OCAML ANSWER, FOLLOWING [[the hint|assignment5_hint3]]:
+
+        let rec enumerate_from (t:'a color_tree) counter =
+          match t with
+          | Leaf x -> (Leaf counter, counter+1)
+          | Branch (left,col,right) -> let (left',counter') = enumerate_from left counter in
+                                       let (right',counter'') = enumerate_from right counter' in
+                                       (Branch (left',col,right'), counter'')
+
+        (* then this will be the function we were asked for *)
+        let enumerate t =
+          let (t', _) = enumerate_from t 0 in
+          t'
 
-    # let rec enumerate_from (t:'a color_tree) counter = match t with
-      | Leaf x -> (Leaf counter, counter+1)
-      | Branch (left,col,right) -> let (left',counter') = ... in
-                                   let (right',counter'') = ... in
-                                   ...
-      ;;
+    IT WOULD ALSO BE POSSIBLE TO WRITE THIS USING OUR `tree_walker` FUNCTION, USING A TECHNIQUE THAT COMBINES THE STRATEGY USED ABOVE WITH THE ONE USED IN `tree_foldleft`:
+
+        let enumerate t =
+          let leaf_handler a = fun counter -> (Leaf counter, counter+1) in
+          let joiner lh col rh =
+            fun counter ->
+              let (left',counter') = lh counter in
+              let (right',counter'') = rh counter' in
+              (Branch (left',col,right'), counter'') in
+          fst (tree_walker leaf_handler joiner t 0)
 
 
 8.  (More challenging.) Write a recursive function that makes a copy of a `color_tree` with the same structure and inner branch colors, but replaces each leaf label with the `int` that reports how many of that leaf's ancestors are labeled `Red`. For example, if we give your function a tree:
@@ -154,8 +230,72 @@ Hint: Consider this pattern:
               2   2
     </pre>
 
+    HERE IS A DIRECT OCAML SOLUTION:
+
+        let rec tree_red_ancestors_from (cur : int) (t : 'a tree) : int tree =
+          match t with
+          | Leaf a -> Leaf cur
+          | Branch(l, col, r) ->
+              let cur' = if col = Red then cur + 1 else cur in
+              let l' = tree_red_ancestors_from cur' l in
+              let r' = tree_red_ancestors_from cur' r in
+              Branch(l',col,r')
+
+        (* here is the function we were asked for *)
+        let tree_red_ancestors t = tree_red_ancestors_from 0 t
+
+
+    HERE IS HOW TO DO IT USING `tree_walker`:
+
+        let tree_red_ancestors t =
+          let leaf_handler a = fun cur -> Leaf cur in
+          let joiner lh col rh = fun cur ->
+            let cur' = if col = Red then cur + 1 else cur in
+            Branch(lh cur', col, rh cur') in
+          tree_walker leaf_handler joiner t 0
+
+
 9.  (More challenging.) Assume you have a `color_tree` whose leaves are labeled with `int`s (which may be negative). For this problem, assume also that no color labels multiple `Branch`s (non-leaf nodes). Write a recursive function that reports which color has the greatest "score" when you sum up all the values of its descendent leaves. Since some leaves may have negative values, the answer won't always be the color at the tree root. In the case of ties, you can return whichever of the highest scoring colors you like.
 
+    HERE IS A DIRECT OCAML SOLUTION:
+
+        type maybe_leader = (color * int) option
+
+        let rec tree_best_sofar (t : 'a color_tree) (lead : maybe_leader) : maybe_leader * int =
+          match t with
+          | Leaf a -> (None, a)
+          | Branch(l, col, r) ->
+              let (lead',left_score) = tree_best_sofar l lead in
+              let (lead'',right_score) = tree_best_sofar r lead' in
+              let my_score = left_score + right_score in
+              (match lead'' with
+              | None -> Some(col,my_score), my_score
+              | Some(_, lead_score) -> (if my_score > lead_score then Some(col,my_score) else lead''), my_score)
+
+        (* Note that the invocations of that function have to return who-is-the-current-leader?
+           plus their OWN score, even if they are not the current leader. Their parents need the
+           second value to calculate whether they should become the current leader. *)
+
+        (* here is the function we were asked for *)
+        let tree_best t =
+          match tree_best_sofar t None with
+          | Some(leader,leader_score), _ -> leader
+          | None, _ -> failwith "no colors"
+
+
+    HERE IS HOW TO DO IT USING `tree_walker`:
+
+        let tree_best_sofar t =
+          let leaf_handler a = fun leader -> leader, a in
+          let joiner lh col rh = fun leader ->
+            let (leader',left_score) = lh leader in
+            let (leader'',right_score) = rh leader' in
+            let my_score = left_score + right_score in
+            (match leader'' with | None -> Some(col,my_score), my_score | Some(_,leader_score) -> (if my_score > leader_score then Some(col,my_score) else leader''), my_score) in
+          tree_walker leaf_handler joiner t
+
+    Then `tree_best` could be defined as in the direct answer.
+
 
 ## Search Trees ##
 
@@ -181,6 +321,19 @@ That is, its leaves have no labels and its inner nodes are labeled with `int`s.
 
     Your function should search through the tree for the specified `int`. If it's never found, it should return the value OCaml calls `None` and Haskell calls `Nothing`. If it finds the `int` right at the root of the `search_tree`, it should return the value OCaml calls `Some []` and Haskell calls `Just []`. If it finds the `int` by first going down the left branch from the tree root, and then going right twice, it should return `Some [Left; Right; Right]` or `Just [Left, Right, Right]`.
 
+    HERE IS AN OCAML ANSWER:
+
+        let search_for (sought : int) (t : search_tree) : direction list option =
+          let rec aux (trace : direction list) t =
+            match t with
+            | Nil -> None
+            | Inner(_,x,_) when x = sought -> Some(List.rev trace)
+            | Inner(l,_,r) ->
+              (match aux (Left :: trace) l with
+              | None -> aux (Right :: trace) r
+              | _ as result -> result) in
+        aux [] t
+
 
 ## More Map2s ##
 
@@ -262,13 +415,23 @@ Again, we've left some gaps. (The use of `type` for the first line in Haskell an
 
 15. Choose one of these languages and fill in the gaps to complete the definition.
 
+    HERE IS AN OCAML DEFINITION:
+
+    type lambda_term = Var of identifier | Abstract of identifier * lambda_term | App of lambda_term * lambda_term
+
 16. Write a function `occurs_free` that has the following type:
 
         occurs_free : identifier -> lambda_term -> bool
 
     That's how OCaml would show it. Haskell would use double colons `::` instead, and would also capitalize all the type names. Your function should tell us whether the supplied identifier ever occurs free in the supplied `lambda_term`.
 
+    HERE IS AN OCAML DEFINITION:
 
+        let rec occurs_free (ident : identifier) (term : lambda_term) : bool =
+          match term with
+          | Var var_ident -> ident = var_indent (* `x` is free in Var "x" but not in Var "y" *)
+          | Abstract(bound_ident, term') -> ident <> bound_ident && occurs_free ident term' (* `x` is free in \y. x but not in \x. blah or \y. y *)
+          | App (head, arg) -> occurs_free ident head || occurs_free ident arg
 
 
 ## Encoding Booleans, Church numerals, and Right-Fold Lists in System F ##