next up previous contents
Next: Fixed points Up: The (untyped) lambda calculus Previous: Recursive functions   Contents

Encoding recursive functions in lambda calculus

Recall that we have already fixed the following definitions:

The function succ is the encoding $\langle{\mathit{S}}\rangle $ for the initial function Successor. We can define encodings for the other initial functions Zero and Projection as follows:

Similarly, the composition of functions is easily defined in the lambda calculus. What remains is to encode primitive recursion and minimalization. We begin by defining encodings for constructing pairs and projecting out the components of a pair.

Observe that $\mathit{fst} (\mathit{pair~} a~ b) \equiv (\lambda p. (p
(\lambda xy. x) ))((\lambda xyz. (z x y))~ a~ b) \rightarrow $ $(\lambda p. (p
(\lambda xy. x) )) \rightarrow $
$(\lambda z. (z a b)) (\lambda xy. x) \rightarrow $ $(\lambda xy. x) a b \rightarrow $ $(\lambda y.a) b \rightarrow a$. Similarly, we can show that $\mathit{snd} (\mathit{pair~} a~ b) \rightarrow ^* b$.

We now show how to encode primitive recursion. For notational simplicity, we restrict ourselves to the case where the function $f$ being defined by primitive recursion has just one argument. Thus, the definition of primitive recursion becomes


\begin{displaymath}
\begin{array}{lcl}
f(0) & = & g\\
f(n{+1}) & = & h(n, f(n))
\end{array}\end{displaymath}

Observe that $g$ is just a constant--that is, some fixed number $k$. Inductively assume that $h$ is a function defined by the lambda expression $\langle{h}\rangle $--in other words, for all numbers $m$ and $n$, $\langle{h}\rangle \langle{m}\rangle \langle{n}\rangle \rightarrow ^* \langle{g(m,n)}\rangle $. The aim is to come up with a lambda expression $\langle{f}\rangle $ defining $f$, such that for all $n$, $\langle{f}\rangle \langle{n}\rangle \rightarrow ^* \langle{f(n)}\rangle $.

Consider the function $t(n) = (n, f(n))$. Then, $t$ can be defined by primitive recursion as follows:


\begin{displaymath}
\begin{array}{lclcl}
t(0) & = & (0, f(0)) & = & (0, g)\\
t(...
...(t(n))),
h(\mathit{fst}(t(n)), \mathit{sec}(t(n))))
\end{array}\end{displaymath}

This is a simpler form of primitive recursion called iteration--think of it as repeatedly iterating a step function starting with $(0,g)$. (In programming language parlance, this is equivalent to translating a top-down recursive definition into an equivalent bottom-up iterative one).

The step function is lambda-definable by the following expression:


\begin{displaymath}
\mathit{step} \equiv \lambda x. \mathit{pair} (\mathit{succ}...
...} x)) (\langle{h}\rangle (\mathit{fst~} x) (\mathit{sec~} x)).
\end{displaymath}

We can check that $\mathit{step~} (\mathit{pair~} \langle{n}\rangle
\langle{f(n)}\rangle ) \rightarrow ^* \mathit{pair~} \langle{n{+1}}\rangle \langle{f(n{+1})}\rangle $.


\begin{displaymath}
\begin{array}{l}
\mathit{step~} (\mathit{pair~} \langle{n}\...
...1}}\rangle ~\langle{f(n{+1})}\rangle .
\end{array}\end{array}\end{displaymath}

The encoding for $t$ is as follows:


\begin{displaymath}
\langle{t}\rangle \equiv \lambda y. y~ \mathit{step~} (\mathit{pair~} \langle{0}\rangle ~
\langle{g}\rangle )
\end{displaymath}

We can check that for all $n$, $\langle{t}\rangle \langle{n}\rangle \rightarrow ^*
\mathit{pair~} \langle{n}\rangle ~\langle{f(n)}\rangle $.


\begin{displaymath}
\begin{array}{l}
\langle{t}\rangle \langle{0}\rangle \right...
...\mbox{(by what has
been proved above)}
\end{array}\end{array}\end{displaymath}

Since $f(n)$ is $\mathit{sec}(t(n))$, $\langle{f}\rangle $ is the following expression:


\begin{displaymath}
\langle{f}\rangle \equiv \lambda y. \mathit{sec} (\langle{t}\rangle y)
\end{displaymath}

It is immediate that for all $n$, $\langle{f}\rangle \langle{n}\rangle \rightarrow ^* \langle{f(n)}\rangle $.

Collapsing all the steps we have seen above, we can write down an expression $\mathit{PR}$ that constructs a primitive recursive definition from the functions $g$ and $h$:


\begin{displaymath}
\mathit{PR} \equiv \lambda hgy. \mathit{sec} (y (\lambda x.
...
...)~
(\mathit{sec~} x))) (\mathit{pair~} \langle{0}\rangle ~ g))
\end{displaymath}

We could also have used recursive definitions of lambda experssions to encode primitive recursion. Recursive definitions can also be used to define minimalization. It turns out that every recursive definition in the lambda calculus can be ``solved'' by finding its fixed point.


next up previous contents
Next: Fixed points Up: The (untyped) lambda calculus Previous: Recursive functions   Contents
Madhavan Mukund 2004-04-29