next up previous contents
Next: One step reduction Up: The (untyped) lambda calculus Previous: Variable capture   Contents

Encoding arithmetic

Having set up this rather abstract notation for computable functions, let us see an example of how to use it.

Before proceeding, we set up some conventions. Implicitly, lambda expressions associate to the left. Thus, the expresssion $M_1 M_2
M_3$ denotes $((M_1 M_2) M_3)$. Also, if we have an abstraction with multiple variables at the outermost level, such as $\lambda x.(\lambda
y. x y)$, we can pull out all the variables at the outermost level into a single sequence such as $\lambda xy. x y$.

In set theory, the number $n$ is identified, in a sense, with a set that has nesting level of depth $n$. For instance, we start with the emptyset for representing the number 0 and, inductively, use the set $\{0,1,..,n-1\}$ to represent the number $n$. Thus we have


\begin{displaymath}
\begin{array}{rcl}
0 & = & \emptyset\\
1 & = & \{\emptys...
...t,\{\emptyset\}\}\}\\
\multicolumn{3}{l}{\ldots}
\end{array}\end{displaymath}

In the lambda calculus, we encode the number $n$ by the number of times we apply a function to an argument. Thus, the number $n$ is a lambda expression that takes two arguments and applies the first argument to the second argument $n$ times. Let $\langle{n}\rangle $ be an abbreviation for the lambda term denoting $n$. Then, we have:


\begin{displaymath}
\begin{array}{rcl}
\langle{0}\rangle & = & \lambda fx.x\\ ...
...ngle & = & \lambda fx.f (\langle{n}\rangle f x)\\
\end{array}\end{displaymath}

This encoding was proposed by Church and terms of the form $\langle{n}\rangle $ are hence called the Church numerals. Concretely, we have


\begin{displaymath}
\langle{1}\rangle = \lambda fx.f (\langle{0}\rangle f x) =
\lambda fx.(f ((\lambda fx.x) f x))
\end{displaymath}

Note that $\langle{0}\rangle g y \rightarrow _\beta (\lambda x.x) y \rightarrow _\beta y$. Thus $\langle{0}\rangle f x \rightarrow _\beta x$. So, inside the definition of $\langle{1}\rangle $, we get


\begin{displaymath}
\langle{1}\rangle = \ldots = \lambda fx.(f \underbrace{\lam...
...}_{\mbox{apply }
\beta}) \rightarrow _\beta \lambda fx.(f x)
\end{displaymath}

Since $\langle{1}\rangle g y \rightarrow _\beta (\lambda x.(g x)) y \rightarrow _\beta g y$, we have


\begin{displaymath}
\langle{2}\rangle = \lambda fx.f (\langle{1}\rangle f x) =
...
...\mbox{apply } \beta})
\rightarrow _\beta
\lambda fx.(f (f x))
\end{displaymath}

and


\begin{displaymath}
\langle{2}\rangle g y \rightarrow _\beta \lambda x.(g (g x)) y = g (g y)
\end{displaymath}

Let us write $g^k y$ to denote the term $g (g (\ldots (g y)))$ with $k$ applications of $g$ to $y$. Then, it is not difficult to show inductively that


\begin{displaymath}
\langle{n}\rangle = \lambda fx.f (\langle{n{-1}}\rangle f x) \rightarrow _\beta \ldots \rightarrow _\beta \lambda fx.(f^n x)
\end{displaymath}

We can now define arithmetic functions such as successor, plus, .... For instance, successor is just $\lambda pfx. f(pfx)$. To see how this works, we observe that


\begin{displaymath}
(\lambda pfx. f(pfx)) \langle{n}\rangle \rightarrow _\beta ...
... fx. f(f^n x) = \lambda fx. f^{n+1} x = \langle{n{+1}}\rangle
\end{displaymath}

Thus, if the function representing successor is applied a term of the right ``type'', it yields the expected answer. We could also apply it to a ``meaningless'' term, in which case we get a ``meaningless'' answer!

Here is a definition for plus: $ \lambda pqfx. pf(qfx)$. We can verify that


\begin{displaymath}
\begin{array}{lcl}
(\lambda pqfx. pf(qfx)) \langle{m}\rang...
...(\lambda fx. f^{m{+n}} x) = \langle{m{+n}}\rangle
\end{array}\end{displaymath}

Similarly, we define multiplication and exponentiation as follows:


\begin{displaymath}
\begin{array}{lcl}
\mbox{\emph{multiplication}} & : & \lam...
...mbox{\emph{exponentiation}} & : & \lambda pq. (pq)
\end{array}\end{displaymath}


next up previous contents
Next: One step reduction Up: The (untyped) lambda calculus Previous: Variable capture   Contents
Madhavan Mukund 2004-04-29