5 **Chris:** I'll be working on this page heavily until 11--11:30 or so. Sorry not to do it last night, I crashed.
9 #Recursion: fixed points in the Lambda Calculus#
11 Sometimes when you type in a web search, Google will suggest
12 alternatives. For instance, if you type in "Lingusitics", it will ask
13 you "Did you mean Linguistics?". But the engineers at Google have
14 added some playfulness to the system. For instance, if you search for
15 "anagram", Google asks you "Did you mean: nag a ram?" And if you
16 [search for "recursion"](http://www.google.com/search?q=recursion), Google asks: "Did you mean: recursion?"
18 ##What is the "rec" part of "letrec" doing?##
20 How could we compute the length of a list? Without worrying yet about what Lambda Calculus encoding we're using for the list, the basic idea is to define this recursively:
22 > the empty list has length 0
24 > any non-empty list has length 1 + (the length of its tail)
26 In OCaml, you'd define that like this:
28 let rec length = fun xs ->
29 if xs == [] then 0 else 1 + length (tail xs)
30 in ... (* here you go on to use the function "length" *)
32 In Scheme you'd define it like this:
34 (letrec [(length (lambda (xs)
36 (+ 1 (length (cdr xs))) )))]
37 ... ; here you go on to use the function "length"
40 Some comments on this:
42 1. `null?` is Scheme's way of saying `empty?`. That is, `(null? xs)` returns true (which Scheme writes as `#t`) iff `xs` is the empty list (which Scheme writes as `'()` or `(list)`).
44 2. `cdr` is function that gets the tail of a Scheme list. (By definition, it's the function for getting the second member of a [[dotted pair|week3_unit#imp]]. As we discussed in notes for last week, it just turns out to return the tail of a list because of the particular way Scheme implements lists.)
46 3. We alternate between `[ ]`s and `( )`s in the Scheme code just to make it more readable. These have no syntactic difference.
49 The main question for us to dwell on here is: What are the `let rec` in the OCaml code and the `letrec` in the Scheme code?
51 Answer: These work a lot like `let` expressions, except that they let you use the variable `length` *inside* the body of the function being bound to it---with the understanding that it will there be bound to *the same function* that you're *then* in the process of binding `length` to. So our recursively-defined function works the way we'd expect it to. Here is OCaml:
53 let rec length = fun xs ->
54 if xs == [] then 0 else 1 + length (tail xs)
56 (* this evaluates to 2 *)
60 (letrec [(length (lambda (xs)
62 (+ 1 (length (cdr xs))) )))]
63 (length (list 20 30)))
66 If you instead use an ordinary `let` (or `let*`), here's what would happen, in OCaml:
68 let length = fun xs ->
69 if xs == [] then 0 else 1 + length (tail xs)
71 (* fails with error "Unbound value length" *)
75 (let* [(length (lambda (xs)
77 (+ 1 (length (cdr xs))) )))]
78 (length (list 20 30)))
79 ; fails with error "reference to undefined identifier: length"
81 Why? Because we said that constructions of this form:
87 really were just another way of saying:
91 and so the occurrences of `length` in A *aren't bound by the `\length` that wraps B*. Those occurrences are free.
93 We can verify this by wrapping the whole expression in a more outer binding of `length` to some other function, say the constant function from any list to the integer `99`:
95 let length = fun xs -> 99
96 in let length = fun xs ->
97 if xs == [] then 0 else 1 + length (tail xs)
99 (* evaluates to 1 + 99 *)
101 Here the use of `length` in `1 + length (tail xs)` can clearly be seen to be bound by the outermost `let`.
103 And indeed, if you tried to define `length` in the Lambda Calculus, how would you do it?
105 \xs. (empty? xs) 0 (succ (length (tail xs)))
107 We've defined all of `empty?`, `0`, `succ`, and `tail` in earlier discussion. But what about `length`? That's not yet defined! In fact, that's the very formula we're trying here to specify.
109 What we really want to do is something like this:
111 \xs. (empty? xs) 0 (succ (... (tail xs)))
113 where this very same formula occupies the `...` position:
115 \xs. (empty? xs) 0 (succ (\xs. (empty? xs) 0 (succ (... (tail xs)))
118 but as you can see, we'd still have to plug the formula back into itself again, and again, and again... No dice.
120 So how could we do it? And how do OCaml and Scheme manage to do it, with their `let rec` and `letrec`?
122 1. OCaml and Scheme do it using a trick. Well, not a trick. Actually an impressive, conceptually deep technique, which we haven't yet developed. Since we want to build up all the techniques we're using by hand, then, we shouldn't permit ourselves to rely on `let rec` or `letrec` until we thoroughly understand what's going on under the hood.
124 2. If you tried this in Scheme:
126 (define length (lambda (xs)
128 (+ 1 (length (cdr xs))) )))
130 (length (list 20 30))
132 You'd find that it works! This is because `define` in Scheme is really shorthand for `letrec`, not for plain `let` or `let*`. So we should regard this as cheating, too.
134 3. In fact, it *is* possible to define the `length` function in the Lambda Calculus despite these obstacles. This depends on using the "version 3" encoding of lists, and exploiting its internal structure: that it takes a function and a base value and returns the result of folding that function over the list, with that base value. So we could use this as a definition of `length`:
136 \xs. xs (\x sofar. successor sofar) 0
138 What's happening here? We start with the value `0`, then we apply the function `\x sofar. successor sofar` to the two arguments <code>x<sub>n</sub></code> and `0`, where <code>x<sub>n</sub></code> is the last element of the list. This gives us `successor 0`, or `1`. That's the value we've accumuluted "so far." Then we go apply the function `\x sofar. successor sofar` to the two arguments <code>x<sub>n-1</sub></code> and the value `1` that we've accumulated "so far." This gives us `two`. We continue until we get to the start of the list. The value we've then built up "so far" will be the length of the list.
140 We can use similar techniques to define many recursive operations on
141 lists and numbers. The reason we can do this is that our "version 3,"
142 fold-based encoding of lists, and Church's encodings of
143 numbers, have a internal structure that *mirrors* the common recursive
144 operations we'd use lists and numbers for. In a sense, the recursive
145 structure of the `length` operation is built into the data
146 structure we are using to represent the list. The non-recursive
147 version of length exploits this embedding of the recursion into
150 This is one of the themes of the course: using data structures to
151 encode the state of some recursive operation. See discussions of the
152 [[zipper]] technique, and [[defunctionalization]].
154 As we said before, it does take some ingenuity to define functions like `tail` or `predecessor` for these encodings. However it can be done. (And it's not *that* difficult.) Given those functions, we can go on to define other functions like numeric equality, subtraction, and so on, just by exploiting the structure already present in our encodings of lists and numbers.
156 With sufficient ingenuity, a great many functions can be defined in the same way. For example, the factorial function is straightforward. The function which returns the nth term in the Fibonacci series is a bit more difficult, but also achievable.
158 ##Some functions require full-fledged recursive definitions##
160 However, some computable functions are just not definable in this
161 way. We can't, for example, define a function that tells us, for
162 whatever function `f` we supply it, what is the smallest integer `x`
163 where `f x` is `true`. (You may be thinking: but that
164 smallest-integer function is not a proper algorithm, since it is not
165 guaranteed to halt in any finite amount of time for every argument.
166 This is the famous [[!wikipedia Halting problem]]. But the fact that
167 an implementation may not terminate doesn't mean that such a function
168 isn't well-defined. The point of interest here is that its definition
169 requires recursion in the function definition.)
171 Neither do the resources we've so far developed suffice to define the
172 [[!wikipedia Ackermann function]]:
175 | when m == 0 -> n + 1
176 | else when n == 0 -> A(m-1,1)
177 | else -> A(m-1, A(m,n-1))
183 A(4,y) = 2^(2^(2^...2)) [where there are y+3 2s] - 3
186 Many simpler functions always *could* be defined using the resources we've so far developed, although those definitions won't always be very efficient or easily intelligible.
188 But functions like the Ackermann function require us to develop a more general technique for doing recursion---and having developed it, it will often be easier to use it even in the cases where, in principle, we didn't have to.
190 ##Using fixed-point combinators to define recursive functions##
194 In general, a **fixed point** of a function `f` is any value `x`
195 such that `f x` is equivalent to `x`. For example,
196 consider the squaring function `square` that maps natural numbers to their squares.
197 `square 2 = 4`, so `2` is not a fixed point. But `square 1 = 1`, so `1` is a
198 fixed point of the squaring function.
200 There are many beautiful theorems guaranteeing the existence of a
201 fixed point for various classes of interesting functions. For
202 instance, imainge that you are looking at a map of Manhattan, and you
203 are standing somewhere in Manhattan. The the [[!wikipedia Brouwer
204 fixed-point theorem]] guarantees that there is a spot on the map that is
205 directly above the corresponding spot in Manhattan. It's the spot
206 where the blue you-are-here dot should be.
208 Whether a function has a fixed point depends on the set of arguments
209 it is defined for. For instance, consider the successor function `succ`
210 that maps each natural number to its successor. If we limit our
211 attention to the natural numbers, then this function has no fixed
212 point. (See the discussion below concerning a way of understanding
213 the successor function on which it does have a fixed point.)
215 In the Lambda Calculus, we say a fixed point of a term `f` is any term `X` such that:
219 You should be able to immediately provide a fixed point of the
220 identity combinator I. In fact, you should be able to provide a
221 whole bunch of distinct fixed points.
223 With a little thought, you should be able to provide a fixed point of
224 the false combinator, KI. Here's how to find it: recall that KI
225 throws away its first argument, and always returns I. Therefore, if
226 we give it I as an argument, it will throw away the argument, and
227 return I. So KII ~~> I, which is all it takes for I to qualify as a
230 What about K? Does it have a fixed point? You might not think so,
231 after trying on paper for a while.
233 However, it's a theorem of the Lambda Calculus that every formula has
234 a fixed point. In fact, it will have infinitely many, non-equivalent
235 fixed points. And we don't just know that they exist: for any given
236 formula, we can explicit define many of them.
238 Yes, as we've mentioned, even the formula that you're using the define
239 the successor function will have a fixed point. Isn't that weird?
240 Think about how it might be true. We'll return to this point below.
242 ###How fixed points help define recursive functions###
244 Recall our initial, abortive attempt above to define the `length` function in the Lambda Calculus. We said "What we really want to do is something like this:
246 \xs. if empty? xs then 0 else succ (... (tail xs))
248 where this very same formula occupies the `...` position."
250 Imagine replacing the `...` with some function that computes the
251 length function. Call that function `length`. Then we have
253 \xs. if empty? xs then 0 else succ (length (tail xs))
255 At this point, we have a definition of the length function, though
256 it's not complete, since we don't know what value to use for the
257 symbol `length`. Technically, it has the status of an unbound
260 Imagine now binding the mysterious variable, and calling the resulting
263 h := \length \xs. if empty? xs then 0 else succ (length (tail xs))
265 Now we have no unbound variables, and we have complete non-recursive
266 definitions of each of the other symbols.
268 So `h` takes an argument, and returns a function that accurately
269 computes the length of a list---as long as the argument we supply is
270 already the length function we are trying to define. (Dehydrated
271 water: to reconstitute, just add water!)
273 Here is where the discussion of fixed points becomes relevant. Saying
274 that `h` is looking for an argument (call it `LEN`) that has the same
275 behavior as the result of applying `h` to `LEN` is just another way of
276 saying that we are looking for a fixed point for `h`.
280 Replacing `h` with its definition, we have
282 (\xs. if empty? xs then 0 else succ (LEN (tail xs))) <~~> LEN
284 If we can find a value for `LEN` that satisfies this constraint, we'll
285 have a function we can use to compute the length of an arbitrary list.
286 All we have to do is find a fixed point for `h`.
288 The strategy we will present will turn out to be a general way of
289 finding a fixed point for any lambda term.
291 ##Deriving Y, a fixed point combinator##
293 How shall we begin? Well, we need to find an argument to supply to
294 `h`. The argument has to be a function that computes the length of a
295 list. The function `h` is *almost* a function that computes the
296 length of a list. Let's try applying `h` to itself. It won't quite
297 work, but examining the way in which it fails will lead to a solution.
299 h h <~~> \xs. if empty? xs then 0 else 1 + h (tail xs)
301 The problem is that in the subexpression `h (tail list)`, we've
302 applied `h` to a list, but `h` expects as its first argument the
305 So let's adjust h, calling the adjusted function H:
307 H = \h \xs. if empty? xs then 0 else 1 + ((h h) (tail xs))
309 This is the key creative step. Instead of applying `h` to a list, we
310 apply it first to itself. After applying `h` to an argument, it's
311 ready to apply to a list, so we've solved the problem just noted.
312 We're not done yet, of course; we don't yet know what argument to give
313 to `H` that will behave in the desired way.
315 So let's reason about `H`. What exactly is H expecting as its first
316 argument? Based on the excerpt `(h h) (tail l)`, it appears that
317 `H`'s argument, `h`, should be a function that is ready to take itself
318 as an argument, and that returns a function that takes a list as an
319 argument. `H` itself fits the bill:
321 H H <~~> (\h \xs. if empty? xs then 0 else 1 + ((h h) (tail xs))) H
322 <~~> \xs. if empty? xs then 0 else 1 + ((H H) (tail xs))
323 == \xs. if empty? xs then 0 else 1 + ((\xs. if empty? xs then 0 else 1 + ((H H) (tail xs))) (tail xs))
324 <~~> \xs. if empty? xs then 0
325 else 1 + (if empty? (tail xs) then 0 else 1 + ((H H) (tail (tail xs))))
329 How does the recursion work?
330 We've defined `H` in such a way that `H H` turns out to be the length function.
331 In order to evaluate `H H`, we substitute `H` into the body of the
332 lambda term. Inside the lambda term, once the substitution has
333 occurred, we are once again faced with evaluating `H H`. And so on.
335 We've got the infinite regress we desired, defined in terms of a
336 finite lambda term with no undefined symbols.
338 Since `H H` turns out to be the length function, we can think of `H`
339 by itself as half of the length function (which is why we called it
340 `H`, of course). Can you think up a recursion strategy that involves
341 "dividing" the recursive function into equal thirds `T`, such that the
342 length function <~~> T T T?
344 We've starting with a particular recursive definition, and arrived at
345 a fixed point for that definition.
346 What's the general recipe?
348 1. Start with any recursive definition `h` that takes itself as an arg: `h := \fn ... fn ...`
349 2. Next, define `H := \f . h (f f)`
350 3. Then compute `H H = ((\f . h (f f)) (\f . h (f f)))`
351 4. That's the fixed point, the recursive function we're trying to define
353 So here is a general method for taking an arbitrary h-style recursive function
354 and returning a fixed point for that function:
356 Y := \h. ((\f.h(ff))(\f.h(ff)))
360 Yh == ((\f.h(ff))(\f.h(ff)))
361 <~~> h((\f.h(ff))(\f.h(ff)))
364 That is, Yh is a fixed point for h.
368 Let's do one more example to illustrate. We'll do `K`, since we
369 wondered above whether it had a fixed point.
371 Before we begin, we can reason a bit about what the fixed point must
372 be like. We're looking for a fixed point for `K`, i.e., `\xy.x`. `K`
373 ignores its second argument. That means that no matter what we give
374 `K` as its first argument, the result will ignore the next argument
375 (that is, `KX` ignores its first argument, no matter what `X` is). So
376 if `KX <~~> X`, `X` had also better ignore its first argument. But we
377 also have `KX == (\xy.x)X ~~> \y.X`. This means that if `X` ignores
378 its first argument, then `\y.X` will ignore its first two arguments.
379 So once again, if `KX <~~> X`, `X` also had better ignore at least its
380 first two arguments. Repeating this reasoning, we realize that `X`
381 must be a function that ignores an infinite series of arguments.
382 Our expectation, then, is that our recipe for finding fixed points
383 will build us a function that somehow manages to ignore an infinite
387 H := \f.h(ff) == \f.(\xy.x)(ff) ~~> \fy.ff
388 H H := (\fy.ff)(\fy.ff) ~~> \y.(\fy.ff)(\fy.ff)
390 Let's check that it is in fact a fixed point:
392 K(H H) == (\xy.x)((\fy.ff)(\fy.ff)
393 ~~> \y.(\fy.ff)(\fy.ff)
395 Yep, `H H` and `K(H H)` both reduce to the same term.
397 To see what this fixed point does, let's reduce it a bit more:
399 H H == (\fy.ff)(\fy.ff)
400 ~~> \y.(\fy.ff)(\fy.ff)
401 ~~> \yy.(\fy.ff)(\fy.ff)
402 ~~> \yyy.(\fy.ff)(\fy.ff)
404 Sure enough, this fixed point ignores an endless, infinite series of
405 arguments. It's a write-only memory, a black hole.
407 Now that we have one fixed point, we can find others, for instance,
410 ~~> \y.(\fy.fff)(\fy.fff)(\fy.fff)
411 ~~> \yy.(\fy.fff)(\fy.fff)(\fy.fff)(\fy.fff)
412 ~~> \yyy.(\fy.fff)(\fy.fff)(\fy.fff)(\fy.fff)(\fy.fff)
414 Continuing in this way, you can now find an infinite number of fixed
415 points, all of which have the crucial property of ignoring an infinite
418 ##What is a fixed point for the successor function?##
420 As we've seen, the recipe just given for finding a fixed point worked
421 great for our `h`, which we wrote as a definition for the length
422 function. But the recipe doesn't make any assumptions about the
423 internal structure of the function it works with. That means it can
424 find a fixed point for literally any function whatsoever.
426 In particular, what could the fixed point for the
427 successor function possibly be like?
429 Well, you might think, only some of the formulas that we might give to the `successor` as arguments would really represent numbers. If we said something like:
433 who knows what we'd get back? Perhaps there's some non-number-representing formula such that when we feed it to `successor` as an argument, we get the same formula back.
435 Yes! That's exactly right. And which formula this is will depend on the particular way you've implemented the successor function.
437 One (by now obvious) upshot is that the recipes that enable us to name
438 fixed points for any given formula aren't *guaranteed* to give us
439 *terminating* fixed points. They might give us formulas X such that
440 neither `X` nor `f X` have normal forms. (Indeed, what they give us
441 for the square function isn't any of the Church numerals, but is
442 rather an expression with no normal form.) However, if we take care we
443 can ensure that we *do* get terminating fixed points. And this gives
444 us a principled, fully general strategy for doing recursion. It lets
445 us define even functions like the Ackermann function, which were until
446 now out of our reach. It would also let us define arithmetic and list
447 functions on the "version 1" and "version 2" encodings, where it
448 wasn't always clear how to force the computation to "keep going."
450 ###Varieties of fixed-point combinators###
452 OK, so how do we make use of this?
454 Many fixed-point combinators have been discovered. (And some
455 fixed-point combinators give us models for building infinitely many
456 more, non-equivalent fixed-point combinators.)
460 Θ′ ≡ (\u f. f (\n. u u f n)) (\u f. f (\n. u u f n))
461 Y′ ≡ \f. (\u. f (\n. u u n)) (\u. f (\n. u u n))
463 `Θ′` has the advantage that `f (Θ′ f)` really *reduces to* `Θ′ f`. Whereas `f (Y′ f)` is only *convertible with* `Y′ f`; that is, there's a common formula they both reduce to. For most purposes, though, either will do.
465 You may notice that both of these formulas have eta-redexes inside them: why can't we simplify the two `\n. u u f n` inside `Θ′` to just `u u f`? And similarly for `Y′`?
467 Indeed you can, getting the simpler:
469 Θ ≡ (\u f. f (u u f)) (\u f. f (u u f))
470 Y ≡ \f. (\u. f (u u)) (\u. f (u u))
472 I stated the more complex formulas for the following reason: in a language whose evaluation order is *call-by-value*, the evaluation of `Θ (\self. BODY)` and `Y (\self. BODY)` will in general not terminate. But evaluation of the eta-unreduced primed versions will.
474 Of course, if you define your `\self. BODY` stupidly, your formula will never terminate. For example, it doesn't matter what fixed point combinator you use for `Ψ` in:
476 Ψ (\self. \n. self n)
478 When you try to evaluate the application of that to some argument `M`, it's going to try to give you back:
482 where `self` is equivalent to the very formula `\n. self n` that contains it. So the evaluation will proceed:
490 You've written an infinite loop!
492 However, when we evaluate the application of our:
494 Ψ (\self (\xs. (empty? xs) 0 (succ (self (tail xs))) ))
496 to some list `L`, we're not going to go into an infinite evaluation loop of that sort. At each cycle, we're going to be evaluating the application of:
498 \xs. (empty? xs) 0 (succ (self (tail xs)))
500 to *the tail* of the list we were evaluating its application to at the previous stage. Assuming our lists are finite (and the encodings we're using don't permit otherwise), at some point one will get a list whose tail is empty, and then the evaluation of that formula to that tail will return `0`. So the recursion eventually bottoms out in a base value.
502 ##Fixed-point Combinators Are a Bit Intoxicating##
504 ![tatoo](/y-combinator-fixed.jpg)
506 There's a tendency for people to say "Y-combinator" to refer to fixed-point combinators generally. We'll probably fall into that usage ourselves. Speaking correctly, though, the Y-combinator is only one of many fixed-point combinators.
508 I used `Ψ` above to stand in for an arbitrary fixed-point combinator. I don't know of any broad conventions for this. But this seems a useful one.
510 As we said, there are many other fixed-point combinators as well. For example, Jan Willem Klop pointed out that if we define `L` to be:
512 \a b c d e f g h i j k l m n o p q s t u v w x y z r. (r (t h i s i s a f i x e d p o i n t c o m b i n a t o r))
514 then this is a fixed-point combinator:
516 L L L L L L L L L L L L L L L L L L L L L L L L L L
519 ##Watching Y in action##
521 For those of you who like to watch ultra slow-mo movies of bullets
522 piercing apples, here's a stepwise computation of the application of a
523 recursive function. We'll use a function `sink`, which takes one
524 argument. If the argument is boolean true (i.e., `\x y.x`), it
525 returns itself (a copy of `sink`); if the argument is boolean false
526 (`\x y. y`), it returns `I`. That is, we want the following behavior:
529 sink true false ~~> I
530 sink true true false ~~> I
531 sink true true true false ~~> I
533 So we make `sink = Y (\f b. b f I)`:
537 3. (\f. (\h. f (h h)) (\h. f (h h))) (\fb.bfI) false
538 4. (\h. [\fb.bfI] (h h)) (\h. [\fb.bfI] (h h)) false
539 5. [\fb.bfI] ((\h. [\fb.bsI] (h h))(\h. [\fb.bsI] (h h))) false
540 6. (\b.b[(\h. [\fb.bsI] (h h))(\h. [\fb.bsI] (h h))]I) false
541 7. false [(\h. [\fb.bsI] (h h))(\h. [\fb.bsI] (h h))] I
542 --------------------------------------------
545 So far so good. The crucial thing to note is that as long as we
546 always reduce the outermost redex first, we never have to get around
547 to computing the underlined redex: because `false` ignores its first
548 argument, we can throw it away unreduced.
550 Now we try the next most complex example:
553 2. Y (\fb.bfI) true false
554 3. (\f. (\h. f (h h)) (\h. f (h h))) (\fb.bfI) true false
555 4. (\h. [\fb.bfI] (h h)) (\h. [\fb.bfI] (h h)) true false
556 5. [\fb.bfI] ((\h. [\fb.bsI] (h h))(\h. [\fb.bsI] (h h))) true false
557 6. (\b.b[(\h. [\fb.bsI] (h h))(\h. [\fb.bsI] (h h))]I) true false
558 7. true [(\h. [\fb.bsI] (h h))(\h. [\fb.bsI] (h h))] I false
559 8. [(\h. [\fb.bsI] (h h))(\h. [\fb.bsI] (h h))] false
561 We've now arrived at line (4) of the first computation, so the result
564 You should be able to see that `sink` will consume as many `true`s as
565 we throw at it, then turn into the identity function after it
566 encounters the first `false`.
568 The key to the recursion is that, thanks to Y, the definition of
569 `sink` contains within it the ability to fully regenerate itself as
570 many times as is necessary. The key to *ending* the recursion is that
571 the behavior of `sink` is sensitive to the nature of the input: if the
572 input is the magic function `false`, the self-regeneration machinery
573 will be discarded, and the recursion will stop.
575 That's about as simple as recursion gets.
577 ##Application to the truth teller/liar paradoxes##
579 ###Base cases, and their lack###
581 As any functional programmer quickly learns, writing a recursive
582 function divides into two tasks: figuring out how to handle the
583 recursive case, and remembering to insert a base case. The
584 interesting and enjoyable part is figuring out the recursive pattern,
585 but the base case cannot be ignored, since leaving out the base case
586 creates a program that runs forever. For instance, consider computing
587 a factorial: `n!` is `n * (n-1) * (n-2) * ... * 1`. The recursive
588 case says that the factorial of a number `n` is `n` times the
589 factorial of `n-1`. But if we leave out the base case, we get
591 3! = 3 * 2! = 3 * 2 * 1! = 3 * 2 * 1 * 0! = 3 * 2 * 1 * 0 * -1! ...
593 That's why it's crucial to declare that 0! = 1, in which case the
594 recursive rule does not apply. In our terms,
596 fac = Y (\fac n. zero? n 1 (fac (predecessor n)))
598 If `n` is 0, `fac` reduces to 1, without computing the recursive case.
600 Curry originally called `Y` the paradoxical combinator, and discussed
601 it in connection with certain well-known paradoxes from the philosophy
602 literature. The truth teller paradox has the flavor of a recursive
603 function without a base case: the truth-teller paradox (and related
606 (1) This sentence is true.
608 If we assume that the complex demonstrative "this sentence" can refer
609 to (1), then the proposition expressed by (1) will be true just in
610 case the thing referred to by *this sentence* is true. Thus (1) will
611 be true just in case (1) is true, and (1) is true just in case (1) is
612 true, and so on. If (1) is true, then (1) is true; but if (1) is not
613 true, then (1) is not true.
615 Without pretending to give a serious analysis of the paradox, let's
616 assume that sentences can have for their meaning boolean functions
617 like the ones we have been working with here. Then the sentence *John
618 is John* might denote the function `\x y. x`, our `true`.
620 Then (1) denotes a function from whatever the referent of *this
621 sentence* is to a boolean. So (1) denotes `\f. f true false`, where
622 the argument `f` is the referent of *this sentence*. Of course, if
623 `f` is a boolean, `f true false <~~> f`, so for our purposes, we can
624 assume that (1) denotes the identity function `I`.
626 If we use (1) in a context in which *this sentence* refers to the
627 sentence in which the demonstrative occurs, then we must find a
628 meaning `m` such that `I m = I`. But since in this context `m` is the
629 same as the meaning `I`, so we have `m = I m`. In other words, `m` is
630 a fixed point for the denotation of the sentence (when used in the
631 appropriate context).
633 That means that in a context in which *this sentence* refers to the
634 sentence in which it occurs, the sentence denotes a fixed point for
635 the identity function. Here's a fixed point for the identity
639 (\f. (\h. f (h h)) (\h. f (h h))) I
640 (\h. I (h h)) (\h. I (h h)))
641 (\h. (h h)) (\h. (h h)))
645 Oh. Well! That feels right. The meaning of *This sentence is true*
646 in a context in which *this sentence* refers to the sentence in which
647 it occurs is `Ω`, our prototypical infinite loop...
649 What about the liar paradox?
651 (2) This sentence is false.
653 Used in a context in which *this sentence* refers to the utterance of
654 (2) in which it occurs, (2) will denote a fixed point for `\f.neg f`,
655 or `\f l r. f r l`, which is the `C` combinator. So in such a
656 context, (2) might denote
659 (\f. (\h. f (h h)) (\h. f (h h))) I
660 (\h. C (h h)) (\h. C (h h)))
661 C ((\h. C (h h)) (\h. C (h h)))
662 C (C ((\h. C (h h))(\h. C (h h))))
663 C (C (C ((\h. C (h h))(\h. C (h h)))))
666 And infinite sequence of `C`s, each one negating the remainder of the
667 sequence. Yep, that feels like a reasonable representation of the
670 See Barwise and Etchemendy's 1987 OUP book, [The Liar: an essay on
671 truth and circularity](http://tinyurl.com/2db62bk) for an approach
672 that is similar, but expressed in terms of non-well-founded sets
673 rather than recursive functions.
677 You should be cautious about feeling too comfortable with
678 these results. Thinking again of the truth-teller paradox, yes,
679 `Ω` is *a* fixed point for `I`, and perhaps it has
680 some a privileged status among all the fixed points for `I`, being the
681 one delivered by Y and all (though it is not obvious why Y should have
684 But one could ask: look, literally every formula is a fixed point for
689 for any choice of X whatsoever.
691 So the Y combinator is only guaranteed to give us one fixed point out
692 of infinitely many---and not always the intuitively most useful
693 one. (For instance, the squaring function has `0` as a fixed point,
694 since `0 * 0 = 0`, and `1` as a fixed point, since `1 * 1 = 1`, but `Y
695 (\x. mul x x)` doesn't give us `0` or `1`.) So with respect to the
696 truth-teller paradox, why in the reasoning we've
697 just gone through should we be reaching for just this fixed point at
700 One obstacle to thinking this through is the fact that a sentence
701 normally has only two truth values. We might consider instead a noun
704 (3) the entity that this noun phrase refers to
706 The reference of (3) depends on the reference of the embedded noun
707 phrase *this noun phrase*. It's easy to see that any object is a
708 fixed point for this referential function: if this pen cap is the
709 referent of *this noun phrase*, then it is the referent of (3), and so
712 The chameleon nature of (3), by the way (a description that is equally
713 good at describing any object), makes it particularly well suited as a
714 gloss on pronouns such as *it*. In the system of
715 [Jacobson 1999](http://www.springerlink.com/content/j706674r4w217jj5/),
716 pronouns denote (you guessed it!) identity functions...
718 Ultimately, in the context of this course, these paradoxes are more
719 useful as a way of gaining leverage on the concepts of fixed points
720 and recursion, rather than the other way around.