ae6653b633f9726607b218f9824945beb5848fb8
[lambda.git] / topics / week4_more_fixed_points.mdwn
1 #Q: How do you know that every term in the untyped lambda calculus has a fixed point?#
2
3 A: That's easy: let `T` be an arbitrary term in the lambda calculus.  If
4 `T` has a fixed point, then there exists some `X` such that `X <~~>
5 TX` (that's what it means to *have* a fixed point).
6
7 <pre><code>let L = \x. T (x x) in
8 let X = L L in
9 X &equiv; L L &equiv; (\x. T (x x)) L ~~> T (L L) &equiv; T X
10 </code></pre>
11
12 Please slow down and make sure that you understand what justified each
13 of the equalities in the last line.
14
15 #Q: How do you know that for any term `T`, `Y T` is a fixed point of `T`?#
16
17 A: Note that in the proof given in the previous answer, we chose `T`
18 and then set <code>X &equiv; L L &equiv; (\x. T (x x)) (\x. T (x x))</code>.  If we abstract over
19 `T`, we get the Y combinator, `\T. (\x. T (x x)) (\x. T (x x))`.  No matter
20 what argument `T` we feed `Y`, it returns some `X` that is a fixed point
21 of `T`, by the reasoning in the previous answer.
22
23 #Q: So if every term has a fixed point, even `Y` has fixed point.#
24
25 A: Right:
26
27 <pre><code>let Y = \T. (\x. T (x x)) (\x. T (x x)) in
28 Y Y
29 &equiv;   \T. (\x. T (x x)) (\x. T (x x)) Y
30 ~~> (\x. Y (x x)) (\x. Y (x x))
31 ~~> Y ((\x. Y (x x)) (\x. Y (x x)))
32 ~~> Y (Y ((\x. Y (x x)) (\x. Y (x x))))
33 ~~> Y (Y (Y (...(Y (Y Y))...)))
34 </code></pre>
35
36
37 #Q: Ouch!  Stop hurting my brain.#
38
39 A: Is that a question?
40
41 Let's come at it from the direction of arithmetic.  Recall that we
42 claimed that even `succ`---the function that added one to any
43 number---had a fixed point.  How could there be an X such that X = X+1?
44 That would imply that
45
46     X <~~> succ X <~~> succ (succ X) <~~> succ (succ (succ X)) <~~> succ (... (succ X)...)
47
48 In other words, the fixed point of `succ` is a term that is its own
49 successor.  Let's just check that `X = succ X`:
50
51 <pre><code>let succ = \n s z. s (n s z) in
52 let X = (\x. succ (x x)) (\x. succ (x x)) in
53 succ X 
54 &equiv;   succ ( (\x. succ (x x)) (\x. succ (x x)) ) 
55 ~~> succ (succ ( (\x. succ (x x)) (\x. succ (x x)) ))
56 &equiv;   succ (succ X)
57 </code></pre>
58
59 You should see the close similarity with `Y Y` here.
60
61
62 #Q. So `Y` applied to `succ` returns a number that is not finite!#
63
64 A. Yes!  Let's see why it makes sense to think of `Y succ` as a Church
65 numeral:
66
67 <pre><code>[same definitions]
68 succ X
69 &equiv;    (\n s z. s (n s z)) X 
70 ~~>  \s z. s (X s z)
71 <~~> succ (\s z. s (X s z)) ; using fixed-point reasoning
72 &equiv;    (\n s z. s (n s z)) (\s z. s (X s z))
73 ~~>  \s z. s ((\s z. s (X s z)) s z)
74 ~~>  \s z. s (s (X s z))
75 </code></pre>
76
77 So `succ X` looks like a numeral: it takes two arguments, `s` and `z`,
78 and returns a sequence of nested applications of `s`...
79
80 You should be able to prove that `add 2 (Y succ) <~~> Y succ`,
81 likewise for `mul`, `sub`, `pow`.  What happens if we try `sub (Y
82 succ) (Y succ)`?  What would you expect infinity minus infinity to be?
83 (Hint: choose your evaluation strategy so that you add two `s`s to the
84 first number for every `s` that you add to the second number.)
85
86 This is amazing, by the way: we're proving things about a term that
87 represents arithmetic infinity.  
88
89 It's important to bear in mind the simplest term in question is not
90 infinite:
91
92         Y succ = (\f. (\x. f (x x)) (\x. f (x x))) (\n s z. s (n s z))
93
94 The way that infinity enters into the picture is that this term has
95 no normal form: no matter how many times we perform beta reduction,
96 there will always be an opportunity for more beta reduction.  (Lather,
97 rinse, repeat!)
98
99
100 #Q. That reminds me, what about [[evaluation order]]?#
101
102 A. For a recursive function that has a well-behaved base case, such as
103 the factorial function, evaluation order is crucial.  In the following
104 computation, we will arrive at a normal form.  Watch for the moment at
105 which we have to make a choice about which beta reduction to perform
106 next: one choice leads to a normal form, the other choice leads to
107 endless reduction:
108
109 <pre><code>let prefact = \f n. iszero n 1 (mul n (f (pred n))) in
110 let fact = Y prefact in
111 fact 2
112 &equiv;   [(\f. (\x. f (x x)) (\x. f (x x))) prefact] 2
113 ~~> [(\x. prefact (x x)) (\x. prefact (x x))] 2
114 ~~> [prefact ((\x. prefact (x x)) (\x. prefact (x x)))] 2
115 ~~> [prefact (prefact ((\x. prefact (x x)) (\x. prefact (x x))))] 2
116 &equiv;   [ (\f n. iszero n 1 (mul n (f (pred n)))) (prefact ((\x. prefact (x x)) (\x. prefact (x x))))] 2
117 ~~> [\n. iszero n 1 (mul n ([prefact ((\x. prefact (x x)) (\x. prefact (x x)))] (pred n)))] 2
118 ~~> iszero 2 1 (mul 2 ([prefact ((\x. prefact (x x)) (\x. prefact (x x)))] (pred 2)))
119 ~~> mul 2 ([prefact ((\x. prefact (x x)) (\x. prefact (x x)))] 1)
120 ...
121 ~~> mul 2 (mul 1 ([prefact ((\x. prefact (x x)) (\x. prefact (x x)))] 0))
122 &equiv;   mul 2 (mul 1 (iszero 0 1 (mul 1 ([prefact ((\x. prefact (x x)) (\x. prefact (x x)))] (pred 0)))))
123 ~~> mul 2 (mul 1 1)
124 ~~> mul 2 1
125 ~~> 2
126 </code></pre>
127
128 The crucial step is the third from the last.  We have our choice of
129 either evaluating the test `iszero 0 1 ...`, which evaluates to `1`,
130 no matter what the ... contains;
131 or we can evaluate the `Y` pump, `(\x. prefact (x x)) (\x. prefact (x x))`, to
132 produce another copy of `prefact`.  If we postpone evaluting the
133 `iszero` test, we'll pump out copy after copy of `prefact`, and never
134 realize that we've bottomed out in the recursion.  But if we adopt a
135 leftmost/call-by-name/normal-order evaluation strategy, we'll always
136 start with the `iszero` predicate, and only produce a fresh copy of
137 `prefact` if we are forced to. 
138
139
140 #Q.  You claimed that the Ackermann function couldn't be implemented using our primitive recursion techniques (such as the techniques that allow us to define addition and multiplication).  But you haven't shown that it is possible to define the Ackermann function using full recursion.#
141
142
143 A. OK:
144   
145         A(m,n) =
146                 | when m == 0 -> n + 1
147                 | else when n == 0 -> A(m-1,1)
148                 | else -> A(m-1, A(m,n-1))
149
150         let A = Y (\A m n. iszero m (succ n) (iszero n (A (pred m) 1) (A (pred m) (A m (pred n)))))
151
152 So for instance:
153
154         A 1 2
155         ~~> A 0 (A 1 1)
156         ~~> A 0 (A 0 (A 1 0))
157         ~~> A 0 (A 0 (A 0 1))
158         ~~> A 0 (A 0 2)
159         ~~> A 0 3
160         ~~> 4
161
162 `A 1 x` is to `A 0 x` as addition is to the successor function;
163 `A 2 x` is to `A 1 x` as multiplication is to addition;
164 `A 3 x` is to `A 2 x` as exponentiation is to multiplication---
165 so `A 4 x` is to `A 3 x` as hyper-exponentiation is to exponentiation...
166
167 #Q. What other questions should I be asking?#
168
169 *    What is it about the variant fixed-point combinators that makes
170      them compatible with a call-by-value evaluation strategy?
171
172 *    How do you know that the Ackermann function can't be computed
173      using primitive recursion techniques?
174
175 *    What *exactly* is primitive recursion?
176
177 *    I hear that `Y` delivers the *least* fixed point.  Least
178      according to what ordering?  How do you know it's least?
179      Is leastness important?
180
181
182