(no commit message)
[lambda.git] / exercises / assignment4_answers.mdwn
1 ## Fixed points ##
2
3 1.  Recall that `ω ≡ \x. x x`, and `Ω ≡ ω ω`.  Is `Ω` a fixed point for `ω`?  Find a fixed point for `ω`, and prove that it is a fixed point.
4
5     ANSWER: Most of you just stated that `Ω` a fixed point for `ω`, but didn't explain why. The mere fact that `ω Ω` doesn't immediately reduce to `Ω` doesn't show that the two terms are not convertible. However, one can argue that `Ω` syntactically has two lambdas, and that upon reduction it always yields itself. So it suffices to show that neither `ω Ω` nor anything it can reduces to can have two lambdas. And indeed it has three lambdas, and anything it reduces to will either have three or four lambdas. For the last part of the question, `Y X` is a fixed point for any `X`, as we've already demonstrated in the notes.
6
7 2.  Consider `Ω ξ` for an arbitrary term `ξ`.
8 `Ω` is so busy reducing itself (the eternal narcissist) that it
9 never gets around to noticing whether it has an argument, let alone
10 doing anything with that argument.  If so, how could `Ω` have a
11 fixed point?  That is, how could there be an `ξ` such that
12 `Ω ξ <~~> ξ`?  To answer this
13 question, begin by constructing `Y Ω`.  Prove that 
14 `Y Ω` is a fixed point for `Ω`.
15
16     ANSWER: Already demonstrated in the notes. We don't need `Ω ξ` to reduce to `ξ`, because `Y Ω` is a `ξ` that can do its own reducing.
17
18 3.  Find two different terms that have the same fixed point.  That is,
19 find terms `F`, `G`, and `ξ` such that `F ξ <~~> ξ` and `G ξ
20 <~~> ξ`.  (If you need a hint, reread the notes on fixed
21 points.)
22
23     ANSWER: Everything is a fixed point of `I`, so in particular `I` is. `I` is also a fixed point for `K I`. There are many other examples.
24
25 4.  Assume that `Ψ` is some fixed point combinator; we're not telling you which one. (You can just write `Psi` in your homework if you don't know how to generate the symbol `Ψ`.) Prove that `Ψ Ψ` is a fixed point of itself, that is, that `Ψ Ψ <~~> Ψ Ψ (Ψ Ψ)`.
26
27     ANSWER: By the definition of a fixed point operator, `Ψ Ψ` is a fixed point for `Ψ`; and `Ψ (Ψ Ψ)` is a fixed point for `Ψ Ψ`. That is: (a) `Ψ (Ψ Ψ) <~~> Ψ Ψ`; and (b) `Ψ Ψ (Ψ (Ψ Ψ)) <~~> Ψ (Ψ Ψ)`. Now a fact we did not discuss in class, but which Hankin and other readings do discuss, is that substitution of convertible subterms preserves convertibility (and as part of that, `<~~>` is transitive). Hence in the lhs of (b), we can substitute `Ψ Ψ` for the subterm `Ψ (Ψ Ψ)`, because of (a), getting (c) `Ψ Ψ (Ψ Ψ) <~~> Ψ (Ψ Ψ)`. But then by (a) and transitivity of `<~~>`, we get (d) `Ψ Ψ (Ψ Ψ) <~~> Ψ Ψ`. Which states that `Ψ Ψ` is a fixed point for itself.
28
29
30 ## Writing recursive functions ##
31
32 5.  Helping yourself to the functions given below,
33 write a recursive function called `fact` that computes the factorial.
34 The factorial `n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1`.
35 For instance, `fact 0 ~~> 1`, `fact 1 ~~> 1`, `fact 2 ~~> 2`, `fact 3 ~~>
36 6`, and `fact 4 ~~> 24`.
37
38         let true = \y n. y in
39         let false = \y n. n in
40         let pair = \a b. \v. v a b in
41         let fst = \a b. a in   ; aka true
42         let snd = \a b. b in   ; aka false
43         let zero = \s z. z in
44         let succ = \n s z. s (n s z) in
45         let zero? = \n. n (\p. false) true in
46         let pred = \n. n (\p. p (\a b. pair (succ a) a)) (pair zero zero) snd in
47         let add = \l r. r succ l in
48         let mult = \l r. r (add l) 0 in
49         let Y = \h. (\u. h (u u)) (\u. h (u u)) in
50
51         let fact = ... in
52
53         fact 4
54
55     ANSWER: `let fact = Y (\fact n. (zero? n) 1 (mult n (fact (pred n))))`.
56
57 6.  For this question, we want to implement **sets** of numbers in terms of lists of numbers, where we make sure as we construct those lists that they never contain a single number more than once. (It would be even more efficient if we made sure that the lists were always sorted, but we won't try to implement that refinement here.) To enforce the idea of modularity, let's suppose you don't know the details of how the lists are implemented. You just are given the functions defined below for them (but pretend you don't see the actual definitions). These define lists in terms of [[one of the new encodings discussed last week|/topics/week3_lists#v5-lists]].
58
59         ; all functions from the previous question, plus
60         ; `num_cmp x y lt eq gt` returns lt when x<y, eq when x==y, gt when x>y
61         let num_cmp = (\base build consume. \l r. r consume (l build base) fst)
62                 ; where base is
63                 (pair (\a b c. b) (K (\a b c. a)))
64                 ; and build is
65                 (\p. pair (\a b c. c) p)
66                 ; and consume is
67                 (\p. p fst p (p snd) (p snd)) in
68         let num_equal? = \x y. num_cmp x y false true false in
69         let neg = \b y n. b n y in
70         let empty = \f n. n in
71         let cons = \x xs. \f n. f x xs in
72         let empty? = \xs. xs (\y ys. false) true in
73         let head = \xs. xs (\y ys. y) err in
74         let tail = \xs. xs (\y ys. ys) empty in
75         let append = Y (\append. \xs zs. xs (\y ys. (cons y (append ys zs))) zs) in
76         let take_while = Y (\take_while. \p xs. xs (\y ys. (p y) (cons y (take_while p ys)) empty) empty) in
77         let drop_while = Y (\drop_while. \p xs. xs (\y ys. (p y) (drop_while p ys) xs) empty) in
78         ...
79         
80     The functions `take_while` and `drop_while` work as described in [[Week 1's homework|assignment1]].
81
82     Using those resources, define a `set_cons` and a `set_equal?` function. The first should take a number argument `x` and a set argument `xs` (implemented as a list of numbers assumed to have no repeating elements), and return a (possibly new) set argument which contains `x`. (But make sure `x` doesn't appear in the result twice!) The `set_equal?` function should take two set arguments `xs` and `ys` and say whether they represent the same set. (Be careful, the lists `[1, 2]` and `[2, 1]` are different lists but do represent the same set. Hence, you can't just use the `list_equal?` function you defined in last week's homework.)
83
84     Here are some tips for getting started. Use `drop_while`, `num_equal?`, and `empty?` to define a `mem?` function that returns `true` if number `x` is a member of a list of numbers `xs`, else returns `false`. Also use `take_while`, `drop_while`, `num_equal?`, `tail` and `append` to define a `without` function that returns a copy of a list of numbers `xs` that omits the first occurrence of a number `x`, if there be such. You may find these functions `mem?` and `without` useful in defining `set_cons` and `set_equal?`. Also, for `set_equal?`, you are probably going to want to define the function recursively... as now you know how to do.
85
86     Some comments comparing this exercise to *The Little Schemer*, and Scheme more generally:
87
88     *   The `set_equal?` you're trying to define here is like `eqset?` in Chapter 7 of *The Little Schemer*, and `set_cons x xs` would be like `(makeset (cons x xs))`, from that same chapter.
89     *   `mem?` and `without` are like the `member?` and `rember` functions defined in Chapter 2 and 3 of *The Little Schemer*, though those functions are defined for lists of symbolic atoms, and here you are instead defining them for lists of numbers.  *The Little Schemer* also defines `multirember`, which removes all occurrences of a match rather than just the first; and `member*` and `rember*` in Chapter 5, that operate on lists that may contain other, embedded lists.
90     *   The native Scheme function that most resembles the `mem?` you're defining is `memv`, though that is defined for more than just numbers, and when that `memv` finds a match it returns a *list* starting with the match, rather than `#t`.
91
92
93     ANSWER:
94
95         let not_equal? = \n m. neg (num_equal? n m) in
96         let mem? = \n xs. neg (empty? (drop_while (not_equal? n) xs)) in
97         let without = \n xs. append (take_while (not_equal? n) xs) (tail (drop_while (not_equal? n) xs)) in
98         let set_cons = \x xs. (mem? x xs) xs (cons x xs) in
99         let set_equal? = Y (\set_equal?. \xs ys. (empty? xs)
100                                            (empty? ys)
101                                            ; else when xs aren't empty
102                                            ((mem? (head xs) ys)
103                                              (set_equal? (tail xs) (without (head xs) ys))
104                                              ; else when head xs not in ys
105                                              false))
106
107 7.  Linguists often analyze natural language expressions into **trees**. We'll need trees in future weeks, and tree structures provide good opportunities for learning how to write recursive functions. Making use of our current resources, we might approximate trees as follows. Instead of words or syntactic categories, we'll have the nodes of the tree labeled with Church numbers. We'll think of a tree as a list in which each element is itself a tree. For simplicity, we'll adopt the convention that a tree of length 1 must contain a number as its only element.
108
109     Then we have the following representations:
110
111               .   
112              /|\  
113             / | \ 
114             1 2  3
115
116         [[1], [2], [3]]
117
118                .  
119               / \ 
120              /\  3
121             1  2  
122
123         [[[1], [2]], [3]]
124
125               .
126              / \
127             1  /\
128               2  3
129
130         [[1], [[2], [3]]]
131
132     Some limitations of this scheme: there is no easy way to label an inner, branching node (for example with a syntactic category like VP), and there is no way to represent a tree in which a mother node has a single daughter.
133
134     When processing a tree, you can test for whether the tree is a leaf node (that is, contains only a single number), by testing whether the length of the list is 1. This will be your base case for your recursive definitions that work on these trees. (You'll probably want to write a function `leaf?` that encapsulates this check.)
135
136     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.
137
138     ANSWER:
139
140     The tricky thing about defining these functions is that it's easy to unwittingly violate the conventions about how we're encoding trees. Thus `Leaf1 ≡ [1]` is a tree, and `[Leaf1, Leaf1, Leaf1]` is also a tree, but `[Leaf1]` is not a tree. (It's a singleton whose content is not a number, but rather a leaf, a list containing a number.) Thus when we are recursively processing `[Leaf1, Leaf1, Leaf1]`, we have to be careful not to just blindly call our function with the tail of our input, since when we get to the end the tail `[Leaf1]` is not itself a tree, on the conventions we've adopted. And our function is likely to rely on those conventions in such a way that it chokes when they are violated.
141
142     That understood, here is a recursive implementation of `sum_leaves`:
143
144         let singleton? = \xs. num_equal? one (length xs) in
145         let singleton = \x. cons x empty in
146         let doubleton = \x y. cons x (singleton y) in
147         let second = \xs. head (tail xs) in
148
149         let sum_leaves = Y (\sum_leaves. \t.
150                               (singleton? t)
151                                 (head t)
152                                 ; else if t is a tree, it contains two or more subtrees
153                                 (add
154                                   (sum_leaves (head t))
155                                   ; don't recurse if (tail t) is a singleton
156                                   ((singleton? (tail t))
157                                     (sum_leaves (second t))
158                                     ; else it's ok to recurse
159                                     (sum_leaves (tail t))))) in
160         ...
161
162
163 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.)
164
165     ANSWER using right-fold lists:
166
167         ; are xs strictly longer than ys?
168         let longer? = \xs ys. neg (leq? (length xs) (length ys)) in
169
170         ; uncons xs f ~~> f (head xs) (tail xs)
171         let uncons = \xs f. f (head xs) (tail xs) in
172
173         let check = \x p. p (\bool ys. uncons ys (\y ys. pair (and (num_equal? x y) bool) ys)) in
174         let finish = \bool ys. (empty? ys) bool false in
175         let list_equal? = \xs ys. (longer? xs ys) false (xs check (pair true (rev ys)) finish) in
176
177         let get_fringe = Y (\get_fringe. \t.
178                               ; this uses a similar pattern to previous problem
179                               (singleton? t)
180                                 t
181                                 ; else if t is a tree, it contains two or more subtrees
182                                 (append
183                                   (get_fringe (head t))
184                                   ; don't recurse if (tail t) is a singleton
185                                   ((singleton? (tail t))
186                                     (get_fringe (second t))
187                                     ; else it's ok to recurse
188                                     (get_fringe (tail t))))) in
189
190     Here is some test data:
191
192         let leaf1 = singleton 1 in
193         let leaf2 = singleton 2 in
194         let leaf3 = singleton 3 in
195         let t12 = doubleton leaf1 leaf2 in
196         let t23 = doubleton leaf2 leaf3 in
197         let alpha = cons leaf1 t23 in
198         let beta = doubleton t12 leaf3 in
199         let gamma = doubleton leaf1 t23 in
200         list_equal? (get_fringe gamma) (get_fringe alpha)
201
202     And here are some cleverer implementations of some of the functions used above:
203
204         let box = \a. \v. v a in
205         let singleton? = \xs. xs (\x b. box (b (K true))) (K false) not in
206
207         ; this function works by first converting [x1,x2,x3] into (true,(true,(true,(K false))))
208         ; then each element of ys unpacks that stack by applying its fst to its snd and itself
209         ; so long as we've not gotten to the end, this will have the result of selecting the snd each time
210         ; when we get to the end of the stack, ((K false) fst) ((K false) snd) (K false) ~~> K false
211         ; after ys are done iterating, we apply the result to fst, which will give us either true or ((K false) fst) ~~> false
212         let longer? = \xs ys. ys (\y p. (p fst) (p snd) p) (xs (\x. pair true) (K false)) fst in
213
214         let shift = \x t. t (\a b c. triple (cons x a) a (pair x))) in
215         let uncons = \xs. xs shift (triple empty empty (K err_head)) (\a b c. c b) in
216         ...
217
218
219 ## Arithmetic infinity? ##
220
221 The next few questions involve reasoning about Church arithmetic and
222 infinity.  Let's choose some arithmetic functions:
223
224     succ = \n s z. s (n s z)
225     add = \l r. r succ l in
226     mult = \l r. r (add l) 0 in
227     exp = \base r. r (mult base) 1 in
228
229 There is a pleasing pattern here: addition is defined in terms of
230 the successor function, multiplication is defined in terms of
231 addition, and exponentiation is defined in terms of multiplication.
232
233
234 9.  Find a fixed point `ξ` for the successor function.  Prove it's a fixed
235 point, i.e., demonstrate that `succ ξ <~~> ξ`.  
236
237     We've had surprising success embedding normal arithmetic in the Lambda
238 Calculus, modeling the natural numbers, addition, multiplication, and
239 so on.  But one thing that some versions of arithmetic supply is a
240 notion of infinity, which we'll write as `inf`.  This object sometimes
241 satisfies the following constraints, for any finite natural number `n`:
242
243         n + inf == inf
244         n * inf == inf
245         n ^ inf == inf
246         leq n inf == true
247
248     (Note, though, that with *some* notions of infinite numbers, like [[!wikipedia ordinal numbers]], operations like `+` are defined in such a way that `inf + n` is different from `n + inf`, and does exceed `inf`; similarly for `*` and `^`. With other notions of infinite numbers, like the [[!wikipedia cardinal numbers]], even less familiar arithmetic operations are employed.)
249
250     ANSWER: Let `H ≡ \u. succ (u u)`, and `X ≡ H H ≡ (\u. succ (u u)) H`. Note that `X ≡ H H ~~> succ (H H)`. Hence `X` is a fixed point for `succ`.
251
252 10. Prove that `add ξ 1 <~~> ξ`, where `ξ` is the fixed
253 point you found in (1).  What about `add ξ 2 <~~> ξ`?  
254
255     Comment: a fixed point for the successor function is an object such that it
256 is unchanged after adding 1 to it.  It makes a certain amount of sense
257 to use this object to model arithmetic infinity.  For instance,
258 depending on implementation details, it might happen that `leq n ξ` is
259 true for all (finite) natural numbers `n`.  However, the fixed point
260 you found for `succ` and `(+n)` (recall this is shorthand for `\x. add x n`) may not be a fixed point for `(*n)`, for example.
261
262     ANSWER: Prove that `add X 1 <~~> X`:
263
264         add X 1 == (\m n. n succ m) X 1
265                 ~~> 1 succ X
266                 == (\s z. s z) succ X
267                 ~~> succ X
268
269     Which by the previous problem is convertible with `X`. (In particular, `X ~~> succ X`.) What about `add X 2`?
270
271         add X 2 == (\m n. n succ m) X 2
272                 ~~> 2 succ X
273                 == (\s z. s (s z)) succ X
274                 ~~> succ (succ X)
275
276     And we know the inner term will be convertible with `X`, and hence we get that the whole result is convertible with `succ X`. Which we already said is convertible with `X`. We can readily see that `add X n <~~> X` for all (finite) natural numbers `n`.
277
278
279
280 ## Mutually-recursive functions ##
281
282 11. (Challenging.) One way to define the function `even?` is to have it hand off
283 part of the work to another function `odd?`:
284
285         let even? = \x. (zero? x)
286                         ; if x == 0 then result is
287                         true
288                         ; else result turns on whether x-1 is odd
289                         (odd? (pred x))
290
291     At the same tme, though, it's natural to define `odd?` in such a way that it
292 hands off part of the work to `even?`:
293
294         let odd? = \x. (zero? x)
295                        ; if x == 0 then result is
296                        false
297                        ; else result turns on whether x-1 is even
298                        (even? (pred x))
299
300     Such a definition of `even?` and `odd?` is called **mutually recursive**. If you
301 trace through the evaluation of some sample numerical arguments, you can see
302 that eventually we'll always reach a base step. So the recursion should be
303 perfectly well-grounded:
304
305         even? 3
306         ~~> (zero? 3) true (odd? (pred 3))
307         ~~> odd? 2
308         ~~> (zero? 2) false (even? (pred 2))
309         ~~> even? 1
310         ~~> (zero? 1) true (odd? (pred 1))
311         ~~> odd? 0
312         ~~> (zero? 0) false (even? (pred 0))
313         ~~> false
314
315     But we don't yet know how to implement this kind of recursion in the Lambda
316 Calculus.
317
318     The fixed point operators we've been working with so far worked like this:
319
320         let ξ = Y h in
321         ξ <~~> h ξ
322
323     Suppose we had a pair of fixed point operators, `Y1` and `Y2`, that operated on
324 a *pair* of functions `h` and `g`, as follows:
325
326         let ξ1 = Y1 h g in
327         let ξ2 = Y2 h g in
328         ξ1 <~~> h ξ1 ξ2 and
329         ξ2 <~~> g ξ1 ξ2
330
331     If we gave you such a `Y1` and `Y2`, how would you implement the above
332 definitions of `even?` and `odd?`?
333
334     ANSWER:
335
336     <pre>
337     let proto_even = \even odd. <span class="ul">\n. (zero? n) true (odd (pred n))</span> in
338     let proto_odd = \even odd. <span class="ul">\n. (zero? n) false (even (pred n))</span> in
339     let even = Y1 proto_even proto_odd in
340     let odd = Y2 proto_even proto_odd in
341     ...
342     </pre>
343
344     By the definitions of `Y1` and `Y2`, we know that `even <~~> proto_even even odd`, and `odd <~~> proto_odd even odd`. Hence the bound variables `even` and `odd` inside the `proto_...` functions have the values we want. `even` will be bound to (something convertible with) the underlined portion of `proto_even`, and `odd` will be bound to (something convertible with) the underlined portion of `proto_odd`.
345                                         
346 12. (More challenging.) Using our derivation of `Y` from [[this week's notes|topics/week4_fixed_point_combinators#deriving-y]] as a model, construct a pair `Y1` and `Y2` that behave in the way described above.
347
348     Here is one hint to get you started: remember that in the notes, we constructed a fixed point for `h` by evolving it into `H` and using `H H` as `h`'s fixed point. We suggested the thought exercise, how might you instead evolve `h` into some `T` and then use `T T T` as `h`'s fixed point. Try solving this problem first. It may help give you the insights you need to define a `Y1` and `Y2`. [[Here are some hints|assignment4_hint]].
349
350     ANSWER: One solution is given in the hint. Here is another:
351
352         let Y1 = \f g . (\u v . f (u u v)(v v u))
353                         (\u v . f (u u v)(v v u))
354                         (\v u . g (v v u)(u u v)) in
355         let Y2 = \f g . Y1 g f in
356         ...
357