4dfd5dbf1c1995c50dccb8fd7653cf98fe656c9d
[lambda.git] / exercises / _assignment4.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 2.  Consider `Ω ξ` for an arbitrary term `ξ`.
6 `Ω` is so busy reducing itself (the eternal narcissist) that it
7 never gets around to noticing whether it has an argument, let alone
8 doing anything with that argument.  If so, how could `Ω` have a
9 fixed point?  That is, how could there be an `ξ` such that
10 `Ω ξ <~~> ξ`?  To answer this
11 question, begin by constructing `Y Ω`.  Prove that 
12 `Y Ω` is a fixed point for `Ω`.
13
14 3.  Find two different terms that have the same fixed point.  That is,
15 find terms `F`, `G`, and `ξ` such that `F ξ <~~> ξ` and `G ξ
16 <~~> ξ`.  (If you need a hint, reread the notes on fixed
17 points.)
18
19
20 ## Writing recursive functions ##
21
22 4.  Helping yourself to the functions given below,
23 write a recursive function called `fact` that computes the factorial.
24 The factorial `n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1`.
25 For instance, `fact 0 ~~> 1`, `fact 1 ~~> 1`, `fact 2 ~~> 2`, `fact 3 ~~>
26 6`, and `fact 4 ~~> 24`.
27
28         let true = \y n. y in
29         let false = \y n. n in
30         let zero? = \n. n (\p. false) true in
31         let pred = \n f z. n (\u v. v (u f)) (K z) I in
32         let succ = \n s z. s (n s z) in
33         let add = \l r. r succ l in
34         let mult = \l r. r (add l) 0 in
35         let Y = \h. (\u. h (u u)) (\u. h (u u)) in
36
37         let fact = ... in
38
39         fac 4
40
41 5.  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_more_lists_]].
42     <!--
43     let empty? = \xs. xs (\y ys. false) true in
44     let head = \xs. xs (\y ys. y) err in
45     let tail = \xs. xs (\y ys. ys) empty in
46     -->
47
48         ; all functions from the previous question, plus
49         let num_equal? = ??? in
50         let neg = \b y n. b n y in
51         let empty = \f n. n in
52         let cons = \x xs. \f n. f x xs in
53         let take_while = Y (\take_while. \p xs. xs (\y ys. (p y) (cons y (take_while p ys)) empty) empty) in
54         let drop_while = Y (\drop_while. \p xs. xs (\y ys. (p y) (drop_while p ys) xs) empty) in
55         ...
56         
57     The functions `take_while` and `drop_while` work as described in [[Week 1's homework|assignment1]].
58
59     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.)
60
61     Here are some tips for getting started. Use `drop_while` and `num_equal?` 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` and `drop_while` 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.
62
63
64 6.  Questions about trees.
65
66
67 ## Arithmetic infinity? ##
68
69 The next few questions involve reasoning about Church arithmetic and
70 infinity.  Let's choose some arithmetic functions:
71
72     succ = \n s z. s (n s z)
73     add = \l r. r succ l in
74     mult = \l r. r (add l) 0 in
75     exp = \base r. r (mult base) 1 in
76
77 There is a pleasing pattern here: addition is defined in terms of
78 the successor function, multiplication is defined in terms of
79 addition, and exponentiation is defined in terms of multiplication.
80
81
82 1.  Find a fixed point `ξ` for the successor function.  Prove it's a fixed
83 point, i.e., demonstrate that `succ ξ <~~> ξ`.  
84
85     We've had surprising success embedding normal arithmetic in the lambda
86 calculus, modeling the natural numbers, addition, multiplication, and
87 so on.  But one thing that some versions of arithmetic supply is a
88 notion of infinity, which we'll write as `inf`.  This object usually
89 satisfies the following constraints, for any finite natural number `n`:
90
91         n + inf == inf
92         n * inf == inf
93         n ^ inf == inf
94         leq n inf == true
95
96     (Note, though, that with some notions of infinite numbers, operations like `+` and `*` are defined in such a way that `inf + n` is different from `n + inf`, and does exceed `inf`.)
97
98 2. Prove that `add 1 ξ <~~> ξ`, where `ξ` is the fixed
99 point you found in (1).  What about `add 2 ξ <~~> ξ`?  
100
101 Comment: a fixed point for the successor function is an object such that it
102 is unchanged after adding 1 to it.  It makes a certain amount of sense
103 to use this object to model arithmetic infinity.  For instance,
104 depending on implementation details, it might happen that `leq n ξ` is
105 true for all (finite) natural numbers `n`.  However, the fixed point
106 you found for `succ` may not be a fixed point for `mult n` or for
107 `exp n`.
108
109
110 ## Mutually-recursive functions ##
111
112 10. (Challenging.) One way to define the function `even?` is to have it hand off
113 part of the work to another function `odd?`:
114
115         let even? = \x. (zero? x)
116                         ; if x == 0 then result is
117                         true
118                         ; else result turns on whether x-1 is odd
119                         (odd? (pred x))
120
121     At the same tme, though, it's natural to define `odd?` in such a way that it
122 hands off part of the work to `even?`:
123
124         let odd? = \x. (zero? x)
125                        ; if x == 0 then result is
126                        false
127                        ; else result turns on whether x-1 is even
128                        (even? (pred x))
129
130     Such a definition of `even?` and `odd?` is called **mutually recursive**. If you
131 trace through the evaluation of some sample numerical arguments, you can see
132 that eventually we'll always reach a base step. So the recursion should be
133 perfectly well-grounded:
134
135         even? 3
136         ~~> (zero? 3) true (odd? (pred 3))
137         ~~> odd? 2
138         ~~> (zero? 2) false (even? (pred 2))
139         ~~> even? 1
140         ~~> (zero? 1) true (odd? (pred 1))
141         ~~> odd? 0
142         ~~> (zero? 0) false (even? (pred 0))
143         ~~> false
144
145     But we don't yet know how to implement this kind of recursion in the Lambda
146 Calculus.
147
148     The fixed point operators we've been working with so far worked like this:
149
150         let ξ = Y h in
151         ξ <~~> h ξ
152
153     Suppose we had a pair of fixed point operators, `Y1` and `Y2`, that operated on
154 a *pair* of functions `h` and `g`, as follows:
155
156         let ξ1 = Y1 h g in
157         let ξ2 = Y2 h g in
158         ξ1 <~~> h ξ1 ξ2 and
159         ξ2 <~~> g ξ1 ξ2
160
161     If we gave you such a `Y1` and `Y2`, how would you implement the above
162 definitions of `even?` and `odd?`?
163
164                                         
165 11. (More challenging.) Using our derivation of `Y` from [[this week's notes|topics/week4_fixed_point_combinators_]] as a model, construct a pair `Y1` and `Y2` that behave in the way described above.
166
167     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`.