add some answers
[lambda.git] / exercises / assignment5_answers.mdwn
1 <!-- λ Λ ∀ ≡ α β ω Ω -->
2
3
4 ## Option / Maybe Types ##
5
6 You've already (in [[assignment1]] and [[/topics/week2 encodings]]) defined and worked with `map` as a function on lists. Now we're going to work instead with the type OCaml defines like this:
7
8     type ('a) option = None | Some of 'a
9
10 and Haskell defines like this:
11
12     data Maybe a = Nothing | Just a
13
14 That is, instances of this type are either an instance of `'a` (this can be any type), wrapped up in a `Some` or `Just` box, or they are a separate value representing a failure. This is sort of like working with a set or a list guaranteed to be either singleton or empty.
15
16 In one of the homework meetings, Chris posed the challenge: you know those dividers they use in checkout lines to separate your purchases from the next person's? What if you wanted to buy one of those dividers? How could they tell whether it belonged to your purchases or was separating them from others?
17
18 The OCaml and Haskell solution is to use not supermarket dividers but instead those gray bins from airport security. If you want to buy something, it goes into a bin. (OCaml's `Some`, Haskell's `Just`). If you want to separate your stuff from the next person, you send an empty bin (OCaml's `None`, Haskell's `Nothing`). If you happen to be buying a bin, OK, you put that into a bin. In OCaml it'd be `Some None` (or `Some (Some stuff)` if the bin you're buying itself contains some stuff); in Haskell `Just Nothing`. This way, we can't confuse a bin that contains a bin with an empty bin. (Not even if the contained bin is itself empty.)
19
20 1.  Your first problem is to write a `maybe_map` function for these types. Here is the type of the function you should write:
21
22         (* OCaml *)
23         maybe_map : ('a -> 'b) -> ('a) option -> ('b) option
24
25         -- Haskell
26         maybe_map :: (a -> b) -> Maybe a -> Maybe b
27
28     If your `maybe_map` function is given a `None` or `Nothing` as its second argument, that should be what it returns. Otherwise, it should apply the function it got as its first argument to the contents of the `Some` or `Just` bin that it got as its second, and return the result, wrapped back up in a `Some` or `Just`. (Yes, we know that the `fmap` function in Haskell already implements this functionality. Your job is to write it yourself.)
29
30     One way to extract the contents of an `option`/`Maybe` value is to pattern match on that value, as you did with lists. In OCaml:
31
32         match m with
33         | None -> ...
34         | Some y -> ...
35
36     In Haskell:
37
38         case m of {
39           Nothing -> ...;
40           Just y -> ...
41         }
42
43     Some other tips: In OCaml you write recursive functions using `let rec`, in Haskell you just use `let` (it's already assumed to be recursive). In OCaml when you finish typing something and want the interpreter to parse it, check and display its type, and evaluate it, type `;;` and then return. In Haskell, if you want to display the type of an expression `expr`, type `:t expr` or `:i expr`.
44
45     You may want to review [[Rosetta pages|/rosetta1]] and also read some of the tutorials we linked to for [[/learning OCaml]] or [[/learning Haskell]].
46
47     HERE IS AN OCAML ANSWER:
48
49         let maybe_map (f: 'a -> 'b) -> (u : 'a option) : 'b option =
50           match u with
51           | Some a -> Some (f a)
52           | None -> None
53
54
55 2.  Next write a `maybe_map2` function. Its type should be:
56
57         (* OCaml *)
58         maybe_map2 : ('a -> 'b -> 'c) -> ('a) option -> ('b) option -> ('c) option
59
60         -- Haskell
61         maybe_map2 :: (a -> b -> c) -> Maybe a -> Maybe b -> Maybe c
62
63
64     HERE IS AN OCAML ANSWER:
65
66         let maybe_map2 (f : 'a -> 'b -> 'c) (u : 'a option) (v : 'b option) : 'c option =
67           match (u, v) with
68           | Some a, Some b -> Some (f a b)
69           | None, _ -> None
70           | _, None -> None
71
72
73
74 ## Color Trees ##
75
76 (The questions on Color and Search Trees are adapted from homeworks in Chapters 1 and 2 of Friedman and Wand, *Essentials of Programming Languages*.)
77
78 Here are type definitions for one kind of binary tree:
79
80     (* OCaml *)
81     type color = Red | Green | Blue | ... (* you can add as many as you like *)
82     type ('a) color_tree = Leaf of 'a | Branch of 'a color_tree * color * 'a color_tree
83
84     -- Haskell
85     data Color = Red | Green | Blue | ...  deriving (Eq, Show)
86     data Color_tree a = Leaf a | Branch (Color_tree a) Color (Color_tree a)  deriving (Show)
87
88 These trees always have colors labeling their inner branching nodes, and will have elements of some type `'a` labeling their leaves. `(int) color_tree`s will have `int`s there, `(bool) color_tree`s will have `bool`s there, and so on. The `deriving (Eq, Show)` part at the end of the Haskell declarations is boilerplate to tell Haskell you want to be able to compare the colors for equality, and also that you want the Haskell interpreter to display colors and trees to you when they are the result of evaluating an expression.
89
90 Here's how you create an instance of such a tree:
91
92     (* OCaml *)
93     let t1 = Branch (Leaf 1, Red, Branch (Leaf 2, Green, Leaf 0))
94
95     -- Haskell
96     let t1 = Branch (Leaf 1) Red (Branch (Leaf 2) Green (Leaf 0))
97
98 Here's how you pattern match such a tree, binding variables to its components:
99
100     (* OCaml *)
101     match t with
102     | Leaf n -> false
103     | Branch (_, col, _) -> col = Red
104
105     -- Haskell
106     case t of {
107       Leaf n -> False;
108       Branch _ col _ -> col == Red
109     }
110
111 These expressions query whether `t` is a branching `color_tree` (that is, not a leaf) whose root is labeled `Red`.
112
113 Notice that for equality, you should use single `=` in OCaml and double `==` in Haskell. (Double `==` in OCaml will often give the same results, but it is subtly different in ways we're not yet in a position to explain.) *In*equality is expressed as `<>` in OCaml and `/=` in Haskell.
114
115 Choose one of these languages and write the following functions.
116
117
118 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.
119
120     HERE IS A DIRECT OCAML ANSWER:
121
122         let rec tree_map (f: 'a -> 'b) (t: 'a color_tree) : 'b color_tree =
123           match t with
124           | Leaf a -> Leaf (f a)
125           | Branch (l,col,r) ->
126               let l' = tree_map f l in
127               let r' = tree_map f r in
128               Branch(l', col, r')
129
130     IT MIGHT BE USEFUL TO GENERALIZE THIS PATTERN, LIKE SO:
131
132         let rec tree_walker (leaf_handler: 'a -> 'h) (joiner : 'h -> color -> 'h -> 'h) (t: 'a color_tree) : 'h =
133           match t with
134           | Leaf a -> leaf_handler a
135           | Branch (l,col,r) ->
136               let lh = tree_walker leaf_handler joiner l in
137               let rh = tree_walker leaf_handler joiner r in
138               joiner lh col rh
139
140     <!-- traverse (k: a-> Mb) t = Leaf a -> map Leaf (k a) | Branch(l,col,r) -> map3 Branch l' c' r' -->
141
142     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`
143
144
145 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.
146
147     HERE IS A DIRECT OCAML ANSWER:
148
149         let rec tree_foldleft (g: 'z -> 'a -> 'z) (z : 'z) (t: 'a color_tree) : 'z =
150           match t with
151           | Leaf a -> g z a
152           | Branch (l,_,r) ->
153               let z' = tree_foldleft g z l in
154               let z'' = tree_foldleft g z' r in
155               r''
156
157     HERE IS AN ANSWER THAT RE-USES OUR `tree_walker` FUNCTION FROM THE PREVIOUS ANSWER:
158
159         let tree_foldleft g z t =
160           let leaf_handler a = fun z -> g z a in
161           let joiner lh _ rh = fun z -> rh (lh z) in
162           let expects_z = tree_walker leaf_handler joiner t in
163           expects_z z
164
165     <!-- traverse (k: a-> Mb) t = Leaf a -> map Leaf (k a) | Branch(l,col,r) -> map3 Branch l' c' r' -->
166     <!-- linearize (f: a->Mon) t = Leaf a -> f a | Branch(l,col,r) -> l' && [col' &&] r' -->
167
168     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.
169
170
171 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`?
172
173     ANSWER: `tree_foldleft (fun z a -> z + a) 0 your_tree`
174
175 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.)
176
177     ANSWER: `tree_foldleft (fun z a -> a :: z) [] your_tree`
178
179 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.)
180
181     HERE IS A DIRECT OCAML ANSWER, FOLLOWING [[the hint|assignment5_hint3]]:
182
183         let rec enumerate_from (t:'a color_tree) counter =
184           match t with
185           | Leaf x -> (Leaf counter, counter+1)
186           | Branch (left,col,right) -> let (left',counter') = enumerate_from left counter in
187                                        let (right',counter'') = enumerate_from right counter' in
188                                        (Branch (left',col,right'), counter'')
189
190         (* then this will be the function we were asked for *)
191         let enumerate t =
192           let (t', _) = enumerate_from t 0 in
193           t'
194
195     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`:
196
197         let enumerate t =
198           let leaf_handler a = fun counter -> (Leaf counter, counter+1) in
199           let joiner lh col rh =
200             fun counter ->
201               let (left',counter') = lh counter in
202               let (right',counter'') = rh counter' in
203               (Branch (left',col,right'), counter'') in
204           fst (tree_walker leaf_handler joiner t 0)
205
206
207 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:
208
209     <pre>
210         Red
211         / \
212       Blue \
213       / \  Green
214      a   b  / \
215            c   Red
216                / \
217               d   e
218     </pre>
219
220     (for any leaf values `a` through `e`), it should return:
221
222     <pre>
223         Red
224         / \
225       Blue \
226       / \  Green
227      1   1  / \
228            1   Red
229                / \
230               2   2
231     </pre>
232
233     HERE IS A DIRECT OCAML SOLUTION:
234
235         let rec tree_red_ancestors_from (cur : int) (t : 'a tree) : int tree =
236           match t with
237           | Leaf a -> Leaf cur
238           | Branch(l, col, r) ->
239               let cur' = if col = Red then cur + 1 else cur in
240               let l' = tree_red_ancestors_from cur' l in
241               let r' = tree_red_ancestors_from cur' r in
242               Branch(l',col,r')
243
244         (* here is the function we were asked for *)
245         let tree_red_ancestors t = tree_red_ancestors_from 0 t
246
247
248     HERE IS HOW TO DO IT USING `tree_walker`:
249
250         let tree_red_ancestors t =
251           let leaf_handler a = fun cur -> Leaf cur in
252           let joiner lh col rh = fun cur ->
253             let cur' = if col = Red then cur + 1 else cur in
254             Branch(lh cur', col, rh cur') in
255           tree_walker leaf_handler joiner t 0
256
257
258 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.
259
260     HERE IS A DIRECT OCAML SOLUTION:
261
262         type maybe_leader = (color * int) option
263
264         let rec tree_best_sofar (t : 'a color_tree) (lead : maybe_leader) : maybe_leader * int =
265           match t with
266           | Leaf a -> (None, a)
267           | Branch(l, col, r) ->
268               let (lead',left_score) = tree_best_sofar l lead in
269               let (lead'',right_score) = tree_best_sofar r lead' in
270               let my_score = left_score + right_score in
271               (match lead'' with
272               | None -> Some(col,my_score), my_score
273               | Some(_, lead_score) -> (if my_score > lead_score then Some(col,my_score) else lead''), my_score)
274
275         (* Note that the invocations of that function have to return who-is-the-current-leader?
276            plus their OWN score, even if they are not the current leader. Their parents need the
277            second value to calculate whether they should become the current leader. *)
278
279         (* here is the function we were asked for *)
280         let tree_best t =
281           match tree_best_sofar t None with
282           | Some(leader,leader_score), _ -> leader
283           | None, _ -> failwith "no colors"
284
285
286     HERE IS HOW TO DO IT USING `tree_walker`:
287
288         let tree_best_sofar t =
289           let leaf_handler a = fun leader -> leader, a in
290           let joiner lh col rh = fun leader ->
291             let (leader',left_score) = lh leader in
292             let (leader'',right_score) = rh leader' in
293             let my_score = left_score + right_score in
294             (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
295           tree_walker leaf_handler joiner t
296
297     Then `tree_best` could be defined as in the direct answer.
298
299
300 ## Search Trees ##
301
302 (More challenging.) For the next problem, assume the following type definition:
303
304     (* OCaml *)
305     type search_tree = Nil | Inner of search_tree * int * search_tree
306
307     -- Haskell
308     data Search_tree = Nil | Inner Search_tree Int Search_tree  deriving (Show)
309
310 That is, its leaves have no labels and its inner nodes are labeled with `int`s. Additionally, assume that all the `int`s in branches descending to the left from a given node will be less than the `int` of that parent node, and all the `int`s in branches descending to the right will be greater. We can't straightforwardly specify this constraint in OCaml's or Haskell's type definitions. We just have to be sure to maintain it by hand.
311
312 10. Write a function `search_for` with the following type, as displayed by OCaml:
313
314         type direction = Left | Right
315         search_for : int -> search_tree -> direction list option
316
317     Haskell would say instead:
318
319         data Direction = Left | Right  deriving (Eq, Show)
320         search_for :: Int -> Search_tree -> Maybe [Direction]
321
322     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]`.
323
324     HERE IS AN OCAML ANSWER:
325
326         let search_for (sought : int) (t : search_tree) : direction list option =
327           let rec aux (trace : direction list) t =
328             match t with
329             | Nil -> None
330             | Inner(_,x,_) when x = sought -> Some(List.rev trace)
331             | Inner(l,_,r) ->
332               (match aux (Left :: trace) l with
333               | None -> aux (Right :: trace) r
334               | _ as result -> result) in
335         aux [] t
336
337
338 ## More Map2s ##
339
340 In question 2 above, you defined `maybe_map2`. [[Before|assignment1]] we encountered `map2` for lists. There are in fact several different approaches to mapping two lists together.
341
342 11. One approach is to apply the supplied function to the first element of each list, and then to the second element of each list, and so on, until the lists are exhausted. If the lists are of different lengths, you might stop with the shortest, or you might raise an error. Different implementations make different choices about that. Let's call this function:
343
344         (* OCaml *)
345         map2_zip : ('a -> 'b -> 'c) -> ('a) list -> ('b) list -> ('c) list
346
347     Write a recursive function that implements this, in Haskell or OCaml. Let's say you can stop when the shorter list runs out, if they're of different lengths. (OCaml and Haskell each already have functions in their standard libraries --- `map2` or `zipWith` -- that do this. And it also corresponds to a list comprehension you can write in Haskell like this:
348
349         :set -XParallelListComp
350         [ f x y | x <- xs | y <- ys ]
351
352     <!-- or `f <$/fmap> ZipList xs <*/ap> ZipList ys`; or `pure f <*> ...`; or `liftA2 f (ZipList xs) (ZipList ys)` -->
353     But we want you to write this function from scratch.)
354
355 12. What is the relation between the function you just wrote, and the `maybe_map2` function you wrote for problem 2, above?
356
357 13. Another strategy is to take the *cross product* of the two lists. If the function:
358
359         (* OCaml *)
360         map2_cross : ('a -> 'b -> 'c) -> ('a) list -> ('b) list -> ('c) list
361
362     is applied to the arguments `f`, `[x0, x1, x2]`, and `[y0, y1]`, then the result should be: `[f x0 y0, f x0 y1, f x1 y0, f x1 y1, f x2 y0, f x2 y1]`. Write this function.
363     <!-- in Haskell, `liftA2 f xs ys` -->
364
365 A similar choice between "zipping" and "crossing" could be made when `map2`-ing two trees. For example, the trees:
366
367 <pre>
368     0       5
369    / \     / \
370   1   2   6   7
371  / \         / \
372  3  4        8  9
373 </pre>
374
375 could be "zipped" like this (ignoring any parts of branches on the one tree that extend farther than the corresponding branch on the other):
376
377 <pre>
378    f 0 5
379    /    \
380 f 1 6  f 2 7
381 </pre>
382
383 14. You can try defining that if you like, for extra credit.
384
385 "Crossing" the trees would instead add copies of the second tree as subtrees replacing each leaf of the original tree, with the leaves of that larger tree labeled with `f` applied to `3` and `6`, then `f` applied to `3` and `8`, and so on across the fringe of the second tree; then beginning again (in the subtree that replaces the `4` leaf) with `f` applied to `4` and `6`, and so on.
386
387 *   In all the plain `map` functions, whether for lists, or for `option`/`Maybe`s, or for trees, the structure of the result exactly matched the structure of the argument.
388
389 *   In the `map2` functions, whether for lists or for `option`/`Maybe`s or for trees, and whether done in the "zipping" style or in the "crossing" style, the structure of the result may be a bit different from the structure of the arguments. But the *structure* of the arguments is enough to determine the structure of the result; you don't have to look at the specific list elements or labels on a tree's leaves or nodes to know what the *structure* of the result will be.
390
391 *   We can imagine more radical transformations, where the structure of the result *does* depend on what specific elements the original structure(s) had. For example, what if we had to transform a tree by turning every leaf into a subtree that contained all of those leaf's prime factors? Or consider our problem from [[assignment3]] where you converted `[3, 1, 0, 2]` not into `[[3,3,3], [1], [], [2,2]]` --- which still has the same structure, that is length, as the original --- but rather into `[3, 3, 3, 1, 2, 2]` --- which doesn't.
392     (Some of you had the idea last week to define this last transformation in Haskell as `[x | x <- [3,1,0,2], y <- [0..(x-1)]]`, which just looks like a cross product, that we counted under the *previous* bullet point. However, in that expression, the second list's structure depends upon the specific values of the elements in the first list. So it's still true, as I said, that you can't specify the structure of the output list without looking at those elements.)
393
394 These three levels of how radical a transformation you are making to a structure, and the parallels between the transformations to lists, to `option`/`Maybe`s, and to trees, will be ideas we build on in coming weeks.
395
396
397
398
399
400 ## Untyped Lambda Terms ##
401
402 In OCaml, you can define some datatypes that represent terms in the untyped Lambda Calculus like this:
403
404     type identifier = string
405     type lambda_term = Var of identifier | Abstract of identifier * _____ | App of _____
406
407 We've left some gaps.
408
409 In Haskell, you'd define it instead like this:
410
411     type Identifier = String
412     data Lambda_term = Var Identifier | Abstract Identifier _____ | App ________  deriving (Show)
413
414 Again, we've left some gaps. (The use of `type` for the first line in Haskell and `data` for the second line is not a typo. The first specifies that `Identifier` will be just a shorthand for an already existing type. The second introduces a new datatype, with its own variant/constructor tags.)
415
416 15. Choose one of these languages and fill in the gaps to complete the definition.
417
418     HERE IS AN OCAML DEFINITION:
419
420     type lambda_term = Var of identifier | Abstract of identifier * lambda_term | App of lambda_term * lambda_term
421
422 16. Write a function `occurs_free` that has the following type:
423
424         occurs_free : identifier -> lambda_term -> bool
425
426     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`.
427
428     HERE IS AN OCAML DEFINITION:
429
430         let rec occurs_free (ident : identifier) (term : lambda_term) : bool =
431           match term with
432           | Var var_ident -> ident = var_indent (* `x` is free in Var "x" but not in Var "y" *)
433           | 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 *)
434           | App (head, arg) -> occurs_free ident head || occurs_free ident arg
435
436
437 ## Encoding Booleans, Church numerals, and Right-Fold Lists in System F ##
438
439 <!-- These questions are adapted from web materials by Umut Acar. Were at <http://www.mpi-sws.org/~umut/>. Now he's moved to <http://www.umut-acar.org/> and I can't find the page anymore. -->
440
441 (For the System F questions, you can either work on paper, or [download and compile](http://www.cis.upenn.edu/~bcpierce/tapl/resources.html#checkers) Pierce's evaluator for system F to test your work. Under the "implementations" link on that page, you want to use Pierce's `fullpoly` or the `fullomega` code. The Chapters of Pierce's book *Types and Programming Languages* most relevant to this week's lectures are 22 and 23; though for context we also recommend at least Chapters 8, 9, 11, 20, and 29. We don't expect most of you to follow these recommendations now, or even to be comfortable enough yet with the material to be *able* to. We're providing the pointers as references that some might conceivably pursue now, and others later.)
442
443
444 Let's think about the encodings of booleans, numerals and lists in System F,
445 and get datatypes with the same form working in OCaml or Haskell. (Of course, OCaml and Haskell
446 have *native* versions of these types: OCaml's `true`, `1`, and `[1;2;3]`.
447 But the point of our exercise requires that we ignore those.)
448
449 Recall from class System F, or the polymorphic λ-calculus, with this grammar:
450
451     types ::= constants | α ... | type1 -> type2 | ∀α. type
452     expressions ::= x ... | λx:type. expr | expr1 expr2 | Λα. expr | expr [type]
453
454 The boolean type, and its two values, may be encoded as follows:
455
456     Bool ≡ ∀α. α -> α -> α
457     true ≡ Λα. λy:α. λn:α. y
458     false ≡ Λα. λy:α. λn:α. n
459
460 It's used like this:
461
462     b [T] res1 res2
463
464 where `b` is a `Bool` value, and `T` is the shared type of `res1` and `res2`.
465
466
467 17. How should we implement the following terms? Note that the result
468 of applying them to the appropriate arguments should also give us a term of
469 type `Bool`.
470
471     (a) the term `not` that takes an argument of type `Bool` and computes its negation  
472     (b) the term `and` that takes two arguments of type `Bool` and computes their conjunction  
473     (c) the term `or` that takes two arguments of type `Bool` and computes their disjunction
474
475 The type `Nat` (for "natural number") may be encoded as follows:
476
477     Nat ≡ ∀α. (α -> α) -> α -> α
478     zero ≡ Λα. λs:α -> α. λz:α. z
479     succ ≡ λn:Nat. Λα. λs:α -> α. λz:α. s (n [α] s z)
480
481 A number `n` is defined by what it can do, which is to compute a function iterated `n`
482 times. In the polymorphic encoding above, the result of that iteration can be
483 any type `α`, as long as your function is of type `α -> α` and you have a base element of type `α`.
484
485
486 18. Translate these encodings of booleans and Church numbers into OCaml or Haskell, implementing versions of `sysf_bool`, `sysf_true`, `sysf_false`, `sysf_nat`, `sysf_zero`, `sysf_iszero` (this is what we'd earlier write as `zero?`, but you can't use `?`s in function names in OCaml or Haskell), `sysf_succ`, and `sysf_pred`. We include the `sysf_` prefixes so as not to collide with any similarly-named native functions or values in these languages. The point of the exercise is to do these things on your own, so avoid using the built-in OCaml or Haskell booleans and integers.
487
488     Keep in mind the capitalization rules. In OCaml, types are written `sysf_bool`, and in Haskell, they are capitalized `Sysf_bool`. In both languages, variant/constructor tags (like `None` or `Some`) are capitalized, and function names start lowercase. But for this problem, you shouldn't need to use any variant/constructor tags.
489
490     To get you started, here is how to define `sysf_bool` and `sysf_true` in OCaml:
491
492         type ('a) sysf_bool = 'a -> 'a -> 'a
493         let sysf_true : ('a) sysf_bool = fun y n -> y
494
495     And here in Haskell:
496
497         type Sysf_bool a = a -> a -> a  -- this is another case where Haskell uses `type` instead of `data`
498         -- To my mind the natural thing to write next would be:
499         let sysf_true :: Sysf_bool a = \y n -> y
500         -- But for complicated reasons, that won't work, and you need to do this instead:
501         let { sysf_true :: Sysf_bool a; sysf_true = \y n -> y }
502         -- Or this:
503         let sysf_true = (\y n -> y) :: Sysf_bool a
504
505             :set -XExplicitForAll
506         let { sysf_true :: forall a. Sysf_bool a; ... }
507         -- or
508         let { sysf_true :: forall a. a -> a -> a; ... }
509
510     You can't however, put a `forall a.` in the `type Sysf_bool ...` declaration. The reasons for this are too complicated to explain here.
511
512     Note also that `sysf_true` can be applied to further arguments directly:
513
514         sysf_true 10 20
515
516     You don't do anything like System F's `true [int] 10 20`. The OCaml and Haskell interpreters figure out what type `sysf_true` needs to be specialized to (in this case, to `int`), and do that automatically.
517
518     It's especially useful for you to implement a version of a System F encoding `pred`, starting with one of the (untyped) versions available in [[assignment3 answers]].
519
520
521
522 Consider the following list type, specified using OCaml or Haskell datatypes:
523
524     (* OCaml *)
525     type ('t) my_list = Nil | Cons of 't * 't my_list
526
527      -- Haskell
528      data My_list t = Nil | Cons t (My_list t)  deriving (Show)
529
530 We can encode that type into System F in terms of its right-fold, just as we did in the untyped Lambda Calculus, like this:
531
532     list_T ≡ ∀α. (T -> α -> α) -> α -> α
533     nil_T ≡ Λα. λc:T -> α -> α. λn:α. n
534     cons_T ≡ λx:T. λxs:list_T. Λα. λc:T -> α -> α. λn:α. c x (xs [α] c n)
535
536 As with `Nat`s, the natural recursion on the type is built into our encoding of it.
537
538 There is some awkwardness here, because System F doesn't have any parameterized types like OCaml's `('t) my_list` or Haskell's `My_list t`. For those, we need to use a more complex system called System F<sub>ω</sub>. System F *can* already define a more polymorphic list type:
539
540     list ≡ ∀τ. ∀α. (τ -> α -> α) -> α -> α
541
542 But this is more awkward to work with, because for functions like `map` we want to give them not just the type:
543
544     (T -> S) -> list -> list
545
546 but more specifically, the type:
547
548     (T -> S) -> list [T] -> list [S]
549
550 Yet we haven't given ourselves the capacity to talk about `list [S]` and so on as a type in System F. Hence, I'll just use the more clumsy, ad hoc specification of `map`'s type as:
551
552     (T -> S) -> list_T -> list_S
553
554 <!--
555     = λf:T -> S. λxs:list. xs [T] [list [S]] (λx:T. λys:list [S]. cons [S] (f x) ys) (nil [S])
556 -->
557
558 *Update: Never mind, don't bother with the next three questions. They proved to be more difficult to implement in OCaml than we expected. Here is [[some explanation|assignment5 hint4]].*
559
560 19. Convert this list encoding and the `map` function to OCaml or Haskell. Again, call the type `sysf_list`, and the functions `sysf_nil`, `sysf_cons`, and `sysf_map`, to avoid collision with the names for native lists and functions in these languages. (In OCaml and Haskell you *can* say `('t) sysf_list` or `Sysf_list t`.)
561
562 20. Also give us the type and definition for a `sysf_head` function. Think about what value to give back if its argument is the empty list. It might be cleanest to use the `option`/`Maybe` technique explored in questions 1--2, but for this assignment, just pick a strategy, no matter how clunky. 
563
564 21. Modify your implementation of the predecessor function for Church numerals, above, to implement a `sysf_tail` function for your lists.
565
566 Be sure to test your proposals with simple lists. (You'll have to `sysf_cons` up a few sample lists yourself; don't expect OCaml or Haskell to magically translate between their native lists and the ones you've just defined.)
567
568
569
570
571
572
573 ## More on Types ##
574
575 22.  Recall that the **S** combinator is given by `\f g x. f x (g x)`. Give two different typings for this term in OCaml or Haskell. To get you started, here's one typing for **K**:
576
577         # let k (y:'a) (n:'b) = y ;;
578         val k : 'a -> 'b -> 'a = [fun]
579         # k 1 true ;;
580         - : int = 1
581
582     If you can't understand how one term can have several types, recall our discussion in this week's notes of "principal types". 
583
584
585
586
587 ## Evaluation Order ##
588
589 Do these last three problems specifically with OCaml in mind, not Haskell. Analogues of the questions exist in Haskell, but because the default evaluation rules for these languages are different, it's too complicated to look at how these questions should be translated into the Haskell setting.
590
591
592 23.  Which of the following expressions is well-typed in OCaml? For those that are, give the type of the expression as a whole. For those that are not, why not?
593
594         let rec f x = f x
595         let rec f x = f f
596         let rec f x = f x in f f
597         let rec f x = f x in f ()
598         let rec f () = f f
599         let rec f () = f ()
600         let rec f () = f () in f f
601         let rec f () = f () in f ()
602
603 24.  Throughout this problem, assume that we have:
604
605         let rec blackhole x = blackhole x
606
607     <!-- Haskell could say: `let blackhole = \x -> fix (\f -> f)` -->
608     All of the following are well-typed. Which ones terminate?  What generalizations can you make?
609
610         blackhole
611         blackhole ()
612         fun () -> blackhole ()
613         (fun () -> blackhole ()) ()
614         if true then blackhole else blackhole
615         if false then blackhole else blackhole
616         if true then blackhole else blackhole ()
617         if false then blackhole else blackhole ()
618         if true then blackhole () else blackhole
619         if false then blackhole () else blackhole
620         if true then blackhole () else blackhole ()
621         if false then blackhole () else blackhole ()
622         let _ = blackhole in 2
623         let _ = blackhole () in 2
624
625 25.  This problem aims to get you thinking about how to control order of evaluation.
626 Here is an attempt to make explicit the behavior of `if ... then ... else ...` explored in the previous question.
627 The idea is to define an `if ... then ... else ...` expression using
628 other expression types.  So assume that `yes` is any (possibly complex) OCaml expression,
629 and `no` is any other OCaml expression (of the same type as `yes`!),
630 and that `bool` is any boolean expression.  Then we can try the following:
631 `if bool then yes else no` should be equivalent to
632
633         let b = bool in
634         let y = yes in
635         let n = no in
636         match b with true -> y | false -> n
637
638     This almost works.  For instance,
639
640         if true then 1 else 2
641
642     evaluates to `1`, and
643
644         let b = true in let y = 1 in let n = 2 in
645         match b with true -> y | false -> n
646
647     also evaluates to `1`.  Likewise,
648
649         if false then 1 else 2
650
651     and
652
653         let b = false in let y = 1 in let n = 2 in
654         match b with true -> y | false -> n
655
656     both evaluate to `2`.
657
658     However,
659
660         let rec blackhole x = blackhole x in
661         if true then blackhole else blackhole ()
662
663     terminates, but
664
665         let rec blackhole x = blackhole x in
666         let b = true in
667         let y = blackhole in
668         let n = blackhole () in
669         match b with true -> y | false -> n
670
671     does not terminate.  Incidentally, using the shorter `match bool with true -> yes | false -> no` rather than the longer `let b = bool ... in match b with ...` *would* work as we desire. But your assignment is to control the evaluation order *without* using the special evaluation order properties of OCaml's native `if` or of its `match`. That is, you must keep the `let b = ... in match b with ...` structure in your answer, though you are allowed to adjust what `b`, `y`, and `n` get assigned to.
672
673     Here's a [[hint|assignment5 hint1]].
674
675 Hint: Use thunks!
676
677 Further hint: What does
678
679     let x = (fun () -> 2) in
680     let y = (fun () -> 3) in
681     match true with true -> x | false -> y
682
683 evaluate to?