In a purely functional language — like lambda calculus — programs are expressed as nested function calls. Repetition in such an environment requires that nesting of function calls continues until some condition is met. During the repetition, each function passes its result to the next function in the nested chain and this repetition is completed when a test for some condition passes. The repetitive behaviour I’ve just described is recursion:
Repetition in functional programming is based on recursion: the definition of something in terms of itself. The nested function call sequence which is to be repeated is given a name. If some condition is met within the sequence, then that name is invoked to repeat the nested function call sequence within itself. The condition often checks whether or not the end of a linear or nested object sequence has been reached.
Greg Michaelson — An Introduction to Functional Programming Through Lambda Calculus
A simple first pass at implementing recursion in lambda calculus is to
introduce additional syntax for defining a function with a name, and then
using that name within the function’s body. The following example defines
add that performs a mathematical addition of two numbers
using Church numerals
(which are not defined here).
def add x y = if iszero y then x else add (succ x) (pred y)
This function adds two numbers by repeatedly taking one away from argument
y and adding it to argument
x until there are no more
y to take. An
interesting artifact of using recursion for this implementation is the
choice of when to evaluate a function’s arguments. For example, if we
choose to evaluate the
add function by delaying evaluation of the
arguments, we encounter the following scenario.
add x y if iszero y then x else add (succ x) (pred y)
add x y if iszero y then x else (( add x y if iszero y then x else add (succ x) (pred y)))
add x y if iszero y then x else (( add x y if iszero y then x else (( add x y if iszero y then x else add (succ x) (pred y)))
ad infinitum. In this example, I expanded the definition of
evaluating the arguments to
add, and an infinite loop that will never
Recursion and Fixed-Point Operators
The above example shows how lambda calculus does not directly support recursion using self-referential functions: we end up in an infinite loop. This is problematic because for lambda calculus to be useful requires that any program can be expressed using nested function calls — including functions that express looping using recursion. We can resolve this dilemma by using what is known as a fixed-point function.
In mathematics, a fixed point (sometimes shortened to fixpoint, also known as an invariant point) of a function is an element of the function's domain that is mapped to itself by the function. That is to say, c is a fixed point of the function $f(x)$ if $f(c) = c$. This means $f(f(...f(c)...)) = f^n(c) = c$, an important terminating consideration when recursively computing $f$.
In other words, a fixed point of a function f is a value that does not change under the application of the function f. For example, the following function has two fixed-points — 0 and 1 — because $f(0) = 0$ and $f(1) = 1$:
Let’s use the idea of a fixed-point function to help solve our addition problem using recursion. We already know how to use a function in lambda calculus: function application. Application involves substituting a function’s bound variables (arguments) with argument expressions and evaluating the function’s body. You can delay this application by wrapping your function in another function. For example,
is equivalent to the following function
where the original function $f$ becomes the argument of a new function application in the body of $g$ — when $g$ is applied, the return value is $f\ a$. By using $g$ in place of $f$ in a recursive function, we can substitute the recursive call with a new function that does not recurse. Rather it applies the recursion at the correct point in time using function application.
In general, the $Y$ combinator function is used to create a recursive version of a function. It is a function that will return a fixed-point for any input function $f$. When used as a function constructor, it creates a fixed-point over the argument that, when applied recurses. The $Y$ combinator is defined as:
By repeatedly applying this equality we get,
Recursion is thus achieved by ensuring that a copy of the function’s argument and the self-application of the function, will continue with each application.
Applying the Y Combinator to
Let’s return to our original problem
def add x y = if iszero y then x else add (succ x) (pred y)
and define a recursive version of this function that takes the recursion
point as a parameter. This transformation is a quick slight-of-hand to
make the function usable as a input to the $Y$ combinator. In general, for
a recursive function
def <name> = ... (<name ...>) ...
you can an alternate version with the recursion point as a function parameter by introducing a helper function as an argument to the $Y$ combinator:
def <name1> f = ... (f ...) ... def <name> = Y <name1>
For the case of
add, or function becomes:
def add f x y = if iszero y then x else f (succ x) (pred y)
We can then feed this revised
add function to the $Y$ combinator and
evaluate it using the regular rules of lambda calculus.
This form of the function takes two parameters, $x$ and $y$. We can make this concrete by evaluating the function with concrete values $1$ and $1$.
This brings us back to our original equation with new parameters. That is, we started with $Y\ add\ 1\ 1$, used the definition of the $Y$ combinator to turn that into an equivalent problem $add\ (Y\ add)\ 1\ 1$, and derived $Y\ add\ 2\ 0$ using the rules of lambda calculus:
We can continue our evaluation from this point to get our final answer for adding one and one:
We’ve successfully applied the $Y$ combinator to recursively add two numbers! I’ll conclude by putting the following steps together into one continuous equation: