added proto-monad
[lambda.git] / assignment2.mdwn
1 For these assignments, you'll probably want to use our [[lambda evaluator]] to check your work. This accepts any grammatical lambda expression and reduces it to normal form, when possible.
2
3
4 More Lambda Practice
5 --------------------
6
7 Insert all the implicit `( )`s and <code>&lambda;</code>s into the following abbreviated expressions:
8
9 1.      `x x (x x x) x`
10 2.      `v w (\x y. v x)`
11 3.      `(\x y. x) u v`
12 4.      `w (\x y z. x z (y z)) u v`
13
14 Mark all occurrences of `x y` in the following terms:
15
16 <OL start=5>
17 <LI>`(\x y. x y) x y`
18 <LI>`(\x y. x y) (x y)`
19 <LI> `\x y. x y (x y)`
20 </OL>
21
22 Reduce to beta-normal forms:
23
24 <OL start=8>
25 <LI>`(\x. x (\y. y x)) (v w)`
26 <LI>`(\x. x (\x. y x)) (v w)`
27 <LI>`(\x. x (\y. y x)) (v x)`
28 <LI>`(\x. x (\y. y x)) (v y)`
29
30 <LI>`(\x y. x y y) u v`
31 <LI>`(\x y. y x) (u v) z w`
32 <LI>`(\x y. x) (\u u)`
33 <LI>`(\x y z. x z (y z)) (\u v. u)`
34 </OL>
35
36 Combinatory Logic
37 -----------------
38
39 Reduce the following forms, if possible:
40
41 <OL start=16>
42 <LI> `Kxy`
43 <LI> `KKxy`
44 <LI> `KKKxy`
45 <LI> `SKKxy`
46 <LI> `SIII`
47 <LI> `SII(SII)`
48
49 <LI> Give Combinatory Logic combinators that behave like our boolean functions.
50   You'll need combinators for `true`, `false`, `neg`, `and`, `or`, and `xor`.
51 </OL>
52
53 Using the mapping specified in the lecture notes,
54 translate the following lambda terms into combinatory logic:
55
56 <OL start=23>
57 <LI> `\x.x`
58 <LI> `\xy.x`
59 <LI> `\xy.y`
60 <LI> `\xy.yx`
61 <LI> `\x.xx`
62 <LI> `\xyz.x(yz)`
63 <LI> For each translation, how many I's are there?  Give a rule for 
64    describing what each I corresponds to in the original lambda term.
65 </OL>
66
67 Lists and Numbers
68 -----------------
69
70 We'll assume the "Version 3" implementation of lists and numbers throughout. So:
71
72 <pre><code>zero &equiv; \s z. z
73 succ &equiv; \n. \s z. s (n s z)
74 iszero &equiv; \n. n (\x. false) true
75 add &equiv; \m \n. m succ n
76 mul &equiv; \m \n. \s. m (n s)</code></pre>
77
78 And:
79
80 <pre><code>empty &equiv; \f z. z
81 make-list &equiv; \hd tl. \f z. f hd (tl f z)
82 isempty &equiv; \lst. lst (\hd sofar. false) true
83 extract-head &equiv; \lst. lst (\hd sofar. hd) junk</code></pre>
84
85 The `junk` in `extract-head` is what you get back if you evaluate:
86
87         extract-head empty
88
89 As we said, the predecessor and the extract-tail functions are harder to define. We'll just give you one implementation of these, so that you'll be able to test and evaluate lambda-expressions using them in Scheme or OCaml.
90
91 <pre><code>predecesor &equiv; (\shift n. n shift (make-pair zero junk) get-second) (\pair. pair (\fst snd. make-pair (successor fst) fst))
92
93 extract-tail &equiv; (\shift lst. lst shift (make-pair empty junk) get-second) (\hd pair. pair (\fst snd. make-pair (make-list hd fst) fst))</code></pre>
94
95 The `junk` is what you get back if you evaluate:
96
97         predecessor zero
98
99         extract-tail empty
100
101 Alternatively, we might reasonably declare the predecessor of zero to be zero (this is a common construal of the predecessor function in discrete math), and the tail of the empty list to be the empty list.
102  
103
104 For these exercises, assume that `LIST` is the result of evaluating:
105
106         (make-list a (make-list b (make-list c (make-list d (make-list e empty)))))
107
108
109 <OL start=16>
110 <LI>What would be the result of evaluating (see [[hints/Assignment 2 hint]] for a hint):
111
112         LIST make-list empty
113
114 <LI>Based on your answer to question 16, how might you implement the **map** function? Expected behavior:
115
116         map f LIST <~~> (make-list (f a) (make-list (f b) (make-list (f c) (make-list (f d) (make-list (f e) empty)))))
117
118 <LI>Based on your answer to question 16, how might you implement the **filter** function? The expected behavior is that:
119
120         filter f LIST
121
122 should evaluate to a list containing just those of `a`, `b`, `c`, `d`, and `e` such that `f` applied to them evaluates to `true`.
123
124 <LI>What goes wrong when we try to apply these techniques using the version 1 or version 2 implementation of lists?
125
126 <LI>Our version 3 implementation of the numbers are usually called "Church numerals". If `m` is a Church numeral, then `m s z` applies the function `s` to the result of applying `s` to ... to `z`, for a total of *m* applications of `s`, where *m* is the number that `m` represents or encodes.
127
128 Given the primitive arithmetic functions above, how would you implement the less-than-or-equal function? Here is the expected behavior, where `one` abbreviates `succ zero`, and `two` abbreviates `succ (succ zero)`.
129
130         less-than-or-equal zero zero ~~> true
131         less-than-or-equal zero one ~~> true
132         less-than-or-equal zero two ~~> true
133         less-than-or-equal one zero ~~> false
134         less-than-or-equal one one ~~> true
135         less-than-or-equal one two ~~> true
136         less-than-or-equal two zero ~~> false
137         less-than-or-equal two one ~~> false
138         less-than-or-equal two two ~~> true
139
140 You'll need to make use of the predecessor function, but it's not essential to understand how the implementation we gave above works. You can treat it as a black box.
141 </OL>
142