Computer Science 591i
Recursion in the Lambda Calculus
The fixed-point combinator lets us recurse
A programming language has to allow us, in some sense, to perform an action
repeatedly. In imperative languages you will have met constructs like
while
and {\bf for} loops. It is not immediately apparent that the
-calculus has
equivalent power, but it does!
Let us suppose we have an operator Y
which acts upon expressions of the
-calculus according to the rule
Doing the factorial function with Y
Consider the expression
F =
u.
x. (if (= x 0) 1 (* x (u(- x 1))))
For some variable n
using
-reduction we obtain:
= (
x. if (= x 0) 1 (* x (YF (- x 1)))) n
again using
-reduction we obtain:
= if (= n 0) 1 (* n (YF (- n 1)))
that is to say, YF
satisfies the equation usually given for the factorial
function
fact(n) = if n=0 then 1 else n*fact(n-1)
YE is a fixed point of E
We say that YE is a fixed point of E, and we
call Y a fixpoint combinator.[There is an interesting
analogy in linear algebra - let \cal E
be a function which returns
the eigenvectors of a matrix. Then
A(\cal E A) \equiv \cal E A,
where vectors are equivalent if they have the same direction. Thus
eigenvectors are a fixed point of the matrix. Indeed, if we take
A
to be a linear function over projective space, then \cal E is a fixpoint
combinator in the same sense as Y]
Working out the factorial function
For an exercise, let us evaluate YF for a few natural numbers:
YF(0) = if (= 0 0) 1 (* 0 (YF (- 0 1)))
= if true 1 (* 0 (YF (- 0 1)))
YF(1) = if (= 1 0) 1 (* 1 (YF (- 1 1)))
= if false 1 (* 1 (YF (- 1 1)))
YF(2) = if (= 2 0) 2 (* 2 (YF (- 2 1)))
= if false 1 (* 2 (YF (- 2 1)))
But we have freedom in where to reduce
Note that where chose to apply our reduction rules is significant
in working out YF.
For example, we could have chosen to expand using
YF = F(YF),
and gone on forever. In the next section we will consider
reduction strategies.
We can define Y in
-calculus!
Up to now we have supposed that Y is an operator that we have made
available as an addition to the
-calculus. But it is not! We can define it as an
expression in the
-calculus:
Y = (
h. (
x.h (x x)) (
x.h (x x)))
It is left as an exercise to the reader to verify that, for any expression
E of the
-calculus, YE = E(YE), where Y is defined as above.