Lambda calculus

Lambda Definability

Reading preferences

Optional display controls need JavaScript. All reading content and navigation work without it.

Source file content/lambda-calculus/lambda-definability/lambda-definability.tex

Editorial

This chapter is experimental. It needs more explanation, and the material should be structured better into definitions and propositions with proofs, and more examples.

Source file content/lambda-calculus/lambda-definability/introduction.tex

Introduction

At first glance, the lambda calculus is just a very abstract calculus of expressions that represent functions and applications of them to others. Nothing in the syntax of the lambda calculus suggests that these are functions of particular kinds of objects, in particular, the syntax includes no mention of natural numbers. Its basic operations---application and lambda abstractions---are operations that apply to any function, not just functions on natural numbers.

Nevertheless, with some ingenuity, it is possible to define arithmetical functions, i.e., functions on the natural numbers, in the lambda calculus. To do this, we define, for each natural number nn \in \Natsource, a special λ\lambdsource-term n¯\num nsource, the Church numeral for nnsource. (Church numerals are named for Alonzo Church.)

Definition of Church numerals

If nn \in \Natsource, the corresponding Church numeral n¯\num{n}source represents nnsource:

n¯λfx.fn(x)\num{n} \ident \lambd[fx][f^n(x)]source

Here, fn(x)f^n(x)source stands for the result of applying ffsource to xxsource nnsource times. For example, 0¯\num{0}source is λfx.x\lambd[fx][x]source, and 3¯\num{3}source is λfx.f(f(fx))\lambd[fx][f(f(f\,x))]source.

The Church numeral n¯\num nsource is encoded as a lambda term which represents a function accepting two arguments ffsource and xxsource, and returns fn(x)f^n(x)source. Church numerals are evidently in normal form.

A representation of natural numbers in the lambda calculus is only useful, of course, if we can compute with them. Computing with Church numerals in the lambda calculus means applying a λ\lambdsource-term FFsource to such a Church numeral, and reducing the combined term Fn¯F\, \num nsource to a normal form. If it always reduces to a normal form, and the normal form is always a Church numeral m¯\num msource, we can think of the output of the computation as being the number mmsource. We can then think of FFsource as defining a function f:f\colon \Nat \to \Natsource, namely the function such that f(n)=mf(n) = msource iff Fn¯m¯F\, \num n \red \num msource. Because of the Church--Rosser property, normal forms are unique if they exist. So if Fn¯m¯F\, \num n \red \num msource, there can be no other term in normal form, in particular no other Church numeral, that Fn¯F \, \num nsource reduces to.

Conversely, given a function f:f\colon \Nat \to \Natsource, we can ask if there is a term FFsource that defines ffsource in this way. In that case we say that FFsource lambda-defines ffsource, and that ffsource is lambda-definable. We can generalize this to many-place and partial functions.

Definition of lambda definability for a partial function

Suppose f:kf\colon \Nat^k \to \Natsource. We say that a lambda term FFsource lambda-defines ffsource if for all n0n_0source, dots, nk1n_{k-1}source,

Fn0¯n1¯nk1¯f(n0,n1,,nk1)¯F \, \num{n_0} \, \num{n_1} \dots \num{n_{k-1}} \red \num{f(n_0, n_1, \dots, n_{k-1})}source

if f(n0,,nk1)f(n_0, \dots, n_{k-1})source is defined, and Fn0¯n1¯nk1¯F \, \num{n_0} \, \num{n_1} \dots \num{n_{k-1}}source has no normal form otherwise.

A very simple example are the constant functions. The term Ckλx.k¯C_k \ident \lambd[x][\num{k}]source lambda-defines the function ck:c_k\colon \Nat \to \Natsource such that c(n)=kc(n) = ksource. For Ckn¯(λx.k¯)n¯k¯C_k \, \num n \ident (\lambd[x][\num{k}])\num n \redone \num{k}source for any nnsource. The identity function is lambda-defined by λx.x\lambd[x][x]source. More complex functions are of course harder to define, and often require a lot of ingenuity. So it is perhaps surprising that every computable function is lambda-definable. The converse is also true: if a function is lambda-definable, it is computable.

Source file content/lambda-calculus/lambda-definability/arithmetical-functions.tex

lambda definable Arithmetical Functions

Successor is lambda definable

The successor function succ\Succsource is lambda-definable.

Proof

A term that lambda-defines the successor function is

Succλa.λfx.f(afx).Given our conventions, this is short forSuccλa.λf.λx.(f((af)x)).Succ is a function that accepts as argument a number a, and evaluates to another function, λfx.f(afx). That function is not itself a Church numeral. However, if the argument a is a Church numeral, it reduces to one. Consider:(λa.λfx.f(afx))n¯λfx.f(n¯fx).The embedded term n¯fx is a redex, since n¯ is λfx.fnx. So n¯fxfnx and so, for the entire term we haveSuccn¯λfx.f(fn(x)),\fn{Succ} & \ident \lambd[a][\lambd[fx][f ({a} f x)]]. \intertext{Given our conventions, this is short for} \fn{Succ} & \ident \lambd[a][\lambd[f][\lambd[x][(f (({a} f) x))]]]. \intertext{$\fn{Succ}$ is a function that accepts as argument a number~$a$, and evaluates to another function, $\lambd[fx][f ({a} f x)]$. That function is not itself a Church numeral. However, if the argument $a$ is a Church numeral, it reduces to one. Consider:} (\lambd[a][\lambd[fx][f ({a} f x)]])\,\num{n} & \redone \lambd[fx][f ({\num{n}} f x)]. \intertext{The embedded term $\num{n}fx$ is a redex, since $\num{n}$ is $\lambd[fx][f^nx]$. So $\num{n}fx \redone f^nx$ and so, for the entire term we have} \fn{Succ}\, \num n & \red \lambd[fx][f(f^n(x))],source

i.e., n+1¯\num{n+1}source.

Example computing the successor of zero

Let's look at what happens when we apply Succ\fn{Succ}source to 0¯\num{0}source, i.e., λfx.x\lambd[fx][x]source. We'll spell the terms out in full:

Succ0¯(λa.λf.λx.(f((af)x)))(λf.λx.x)λf.λx.(f(((λf.λx.x)f)x))λf.λx.(f((λx.x)x))λf.λx.(fx)1¯\fn{Succ}\, \num{0} & \ident (\lambd[a][\lambd[f][\lambd[x][(f (({a} f) x))]]])(\lambd[f][\lambd[x][x]])\\ & \redone \lambd[f][\lambd[x][(f (({(\lambd[f][\lambd[x][x]])} f) x))]] \\ & \redone \lambd[f][\lambd[x][(f ({(\lambd[x][x])} x))]] \\ & \redone \lambd[f][\lambd[x][(f x)]] \ident \num{1}\\source

Exercise on an alternative successor term

The term

Succλn.λfx.nf(fx)\fn{Succ}' & \ident \lambd[{n}][\lambd[fx][{n} f (f x)]]source

lambda-defines the successor function. Explain why.

Addition is lambda definable

The addition function add\Addsource is lambda-definable.

Proof

Addition is lambda-defined by the terms

Addλab.λfx.af(bfx)or, alternatively,Addλab.aSuccb.The first addition works as follows: Add first accept two numbers a and b. The result is a function that accepts f and x and returns af(bfx). If a and b are Church numerals n¯ and m¯, this reduces to fn+m(x), which is identical to fn(fm(x)). Or, slowly:(λab.λfx.af(bfx))n¯m¯λfx.n¯f(m¯fx)λfx.n¯f(fmx)λfx.fn(fmx)n+m¯.The second representation of addition Add works differently: Applied to two Church numerals n¯ and m¯,Addn¯m¯n¯Succm¯.But n¯fx always reduces to fn(x). So,n¯Succm¯Succn(m¯).\fn{Add} & \ident \lambd[{a}{b}][\lambd[fx][{a} f ({b} f x)]] \intertext{or, alternatively,} \fn{Add}' & \ident \lambd[{a}{b}][{a}\, \fn{Succ} \, {b}]. \intertext{The first addition works as follows: $\fn{Add}$ first accept two numbers ${a}$ and ${b}$. The result is a function that accepts $f$ and $x$ and returns $af(bfx)$. If $a$ and $b$ are Church numerals $\num{n}$ and $\num{m}$, this reduces to $f^{n+m}(x)$, which is identical to $f^{n}(f^{m}(x))$. Or, slowly:} (\lambd[{a}{b}][\lambd[fx][{a} f ({b} f x)]])\num n\,\num m & \redone \lambd[fx][\num{n}\, f (\num {m}\, f x)] \\ & \redone \lambd[fx][\num{n}\, f (f^m x)] \\ & \redone \lambd[fx][f^n (f^m x)] \ident \num {n+m}. \intertext{The second representation of addition $\fn{Add'}$ works differently: Applied to two Church numerals $\num{n}$ and $\num{m}$,} \fn{Add}' \num n \,\num m & \redone \num{n}\, \fn{Succ}\, \num{m}. \intertext{But $\num{n} f x$ always reduces to $f^n(x)$. So,} \num{n}\, \fn{Succ}\, \num{m} & \red \fn{Succ}^n(\num{m}).source

And since Succ\fn{Succ}source lambda-defines the successor function, and the successor function applied nnsource times to mmsource gives n+mn+msource, this in turn reduces to n+m¯\num{n+m}source.

Multiplication is lambda definable

Multiplication is lambda-definable by the term

Multλab.λfx.a(bf)x\fn{Mult} \ident \lambd[ab][\lambd[fx][a (b f) x]]source

Proof

To see how this works, suppose we apply Mult\fn{Mult}source to Church numerals n¯\num{n}source and m¯\num{m}source: Multn¯m¯\fn{Mult} \, \num{n} \, \num{m}source reduces to λfx.n¯(m¯f)x\lambd[fx][\num{n}(\num{m}\, f)x]source. The term m¯f\num{m} fsource defines a function which applies ffsource to its argument mmsource times. Consequently, n¯(m¯f)x\num{n} (\num{m} f) xsource applies the function “apply ffsource mmsource times” itself nnsource times to xxsource. In other words, we apply ffsource to xxsource, n·mn\cdot msource times. But the resulting normal term is just the Church numeral nm¯\num{nm}source.

Editorial

We can actually simplify this term further by η\etasource-reduction:

Multλab.λf.a(bf).\fn{Mult} \ident \lambd[ab][\lambd[f][a (b f)]].source

But then we first have to explain η\etasource-reduction.

Exercise on an alternative multiplication term

Multiplication can be lambda-defined by the term

Multλab.a(Adda)0¯.\fn{Mult}' \ident \lambd[ab][a (\fn{Add}\, a) \num{0}].source

Explain why this works.

The definition of exponentiation as a λ\lambdsource-term is surprisingly simple:

Expλbe.eb.\fn{Exp} \ident \lambd[be][e b].source

The first argument bbsource is the base and the second eesource is the exponent. Intuitively, efe fsource is fef^esource by our encoding of numbers. If you find it hard to understand, we can still define exponentiation also by iterated multiplication:

Expλbe.e(Multb)1¯.\fn{Exp}' \ident \lambd[be][e (\fn{Mult}\, b) \num{1}].source

Predecessor and subtraction on Church numeral is not as simple as we might think: it requires encoding of pairs.

Source file content/lambda-calculus/lambda-definability/pairs.tex

Pairs and Predecessor

Definition of an encoded pair

The pair of MMsource and NNsource (written M,N\tuple{M,N}source) is defined as follows:

M,Nλf.fMN.\tuple{M,N} \ident \lambd[f][fMN].source

Intuitively it is a function that accepts a function, and applies that function to the two elements of the pair. Following this idea we have this constructor, which takes two terms and returns the pair containing them:

Pairλmn.λf.fmn\fn{Pair} \ident \lambd[mn][\lambd[f][fmn]]source

Given a pair, we also want to recover its elements. For this we need two access functions, which accept a pair as argument and return the first or second elements in it:

Fstλp.p(λmn.m)Sndλp.p(λmn.n)\fn{Fst} & \ident \lambd[p][p(\lambd[mn][m])]\\ \fn{Snd} & \ident \lambd[p][p(\lambd[mn][n])]source

Exercise on pair access functions

Explain why the access functions Fst\fn{Fst}source and Snd\fn{Snd}source work.

Now with pairs we can lambda-define the predecessor function:

Predλn.Fst(n(λp.Sndp,Succ(Sndp))0¯,0¯)\fn{Pred} \ident \lambd[n][\fn{Fst}(n (\lambd[p][\tuple{\fn{Snd}\, {p}, \fn{Succ}(\fn{Snd}\, {p})}]) \tuple{\num 0, \num 0})]source

Remember that n¯fx\num n\, f xsource reduces to fn(x)f^{n}(x)source; in this case ffsource is a function that accepts a pair ppsource and returns a new pair containing the second component of ppsource and the successor of the second component; xxsource is the pair 0,0\tuple{0,0}source. Thus, the result is 0,0\tuple{0,0}source for n=0n=0source, and n1¯,n¯\tuple{\num{n-1}, \num n}source otherwise. Pred\fn{Pred}source then returns the first component of the result.

Subtraction can be defined as Pred\fn{Pred}source applied to aasource, bbsource times:

Subλab.bPreda.\fn{Sub} \ident \lambd[ab][b \fn{Pred}\, a].source

Source file content/lambda-calculus/lambda-definability/truth-values.tex

Truth Values and Relations

We can encode truth values in the pure lambda calculus as follows:

trueλx.λy.xfalseλx.λy.y\fn{true} & \ident \lambd[x][\lambd[y][x]]\\ \fn{false} & \ident \lambd[x][\lambd[y][y]]source

Truth values are represented as selectors, i.e., functions that accept two arguments and returning one of them. The truth value true\fn{true}source selects its first argument, and false\fn{false}source its second. For example, trueMN\fn{true}\, M Nsource always reduces to MMsource, while falseMN\fn{false}\, M Nsource always reduces to NNsource.

Definition of a lambda-definable relation

We call a relation RnR \subseteq \Nat^nsource lambda-definable if there is a term RRsource such that

Rn1¯nk¯βtruewhenever R(n1,,nk) andRn1¯nk¯βfalseR\, \num{n_1} \dots \num{n_k} & \bred \fn{true} \intertext{whenever $R(n_1, \dots, n_k)$ and} R\, \num{n_1} \dots \num{n_k} & \bred \fn{false}source

otherwise.

For instance, the relation IsZero={0}\fn{IsZero} = \{0\}source which holds of 00source and 00source only, is lambda-definable by

IsZeroλn.n(λx.false)true.\fn{IsZero} \ident \lambd[n][n (\lambd[x][\fn{false}])\, \fn{true}].source

How does it work? Since Church numerals are defined as iterators (functions which apply their first argument nnsource times to the second), we set the initial value to be true\fn{true}source, and for every step of iteration, we return false\fn{false}source regardless of the result of the last iteration. This step will be applied to the initial value nnsource times, and the result will be true\fn{true}source if and only if the step is not applied at all, i.e., when n=0n = 0source.

On the basis of this representation of truth values, we can further define some truth functions. Here are two, the representations of negation and conjunction:

Notλx.xfalsetrueAndλx.λy.xyfalse\fn{Not} & \ident \lambd[x][x\, \fn{false}\, \fn{true}]\\ \fn{And} & \ident \lambd[x][\lambd[y][xy \,\fn{false}]]source

The function “Not\fn{Not}source” accepts one argument, and returns true\fn{true}source if the argument is false\fn{false}source, and false\fn{false}source if the argument is true\fn{true}source. The function “And\fn{And}source” accepts two truth values as arguments, and should return true\fn{true}source iff both arguments are true\fn{true}source. Truth values are represented as selectors (described above), so when xxsource is a truth value and is applied to two arguments, the result will be the first argument if xxsource is true\fn{true}source and the second argument otherwise. Now And\fn{And}source takes its two arguments xxsource and yysource, and in return passes yysource and false\fn{false}source to its first argument xxsource. Assuming xxsource is a truth value, the result will evaluate to yysource if xxsource is true\fn{true}source, and to false\fn{false}source if xxsource is false\fn{false}source, which is just what is desired.

Note that we assume here that only truth values are used as arguments to And\fn{And}source. If it is passed other terms, the result (i.e., the normal form, if it exists) may well not be a truth value.

Exercise defining disjunction and exclusive disjunction

Define the functions Or\fn{Or}source and Xor\fn{Xor}source representing the truth functions of inclusive and exclusive disjunction using the encoding of truth values as λ\lambdsource-terms.

Source file content/lambda-calculus/lambda-definability/primitive-recursive-functions.tex

Primitive Recursive Functions are lambda definable

Recall that the primitive recursive functions are those that can be defined from the basic functions zero\Zerosource, succ\Succsource, and Pin\Proj{n}{i}source by composition and primitive recursion.

Basic primitive recursive functions are lambda definable

The basic primitive recursive functions zero\Zerosource, succ\Succsource, and projections Pin\Proj{n}{i}source are lambda-definable.

Proof

They are lambda-defined by the following terms:

Zeroλa.λfx.xSuccλa.λfx.f(afx)Projinλx0xn1.xi\fn{Zero} & \ident \lambd[a][\lambd[fx][x]]\\ \fn{Succ} & \ident \lambd[a][\lambd[fx][f (a f x)]] \\ \fn{Proj}^n_i & \ident \lambd[x_0\dots x_{n-1}][x_i]source

Closure of lambda-definable total functions under composition

Suppose the kksource-ary function ffsource, and nnsource-ary functions g0,,gk1g_0, \dots, g_{k-1}source, are lambda-definable by terms FFsource, G0G_0source, dots, GkG_ksource, and hhsource is defined from them by composition. Then HHsource is lambda-definable.

Proof

hhsource can be lambda-defined by the term

Hλx0xn1.F(G0x0xn1)(Gk1x0xn1)H \ident \lambd[x_0 \dots x_{n-1}][F\, (G_0 x_0 \dots x_{n-1}) \dots (G_{k-1} x_0 \dots x_{n-1})]source

We leave verification of this fact as an exercise.

Exercise verifying the composition term

Complete the proof of the lemma on closure under composition by showing that Hn0¯nn1¯h(n0,,nn1)¯H\num{n_0}\dots\num{n_{n-1}} \red \num{h(n_0, \dots, n_{n-1})}source.

Note that the lemma on closure under composition did not require that ffsource and g0g_0source, dots, gk1g_{k-1}source are primitive recursive; it is only required that they are total and lambda-definable.

Closure under primitive recursion

Suppose ffsource is an nnsource-ary function and ggsource is an n+2n+2source-ary function, they are lambda-definable by terms FFsource and GGsource, and the function hhsource is defined from ffsource and ggsource by primitive recursion. Then hhsource is also lambda-definable.

Proof

Recall that hhsource is defined by

h(x1,,xn,0)=f(x1,,xn)h(x1,,xn,y+1)=h(x1,,xn,y,h(x1,,xn,y)).h(x_1, \dots, x_n, 0) &= f(x_1, \dots, x_n)\\ h(x_1, \dots, x_n, y+1) & = h(x_1, \dots, x_n, y, h(x_1, \dots, x_n, y)).source

Informally speaking, the primitive recursive definition iterates the application of the function hhsource yysource times and applies it to f(x1,,xn)f(x_1, \dots, x_n)source. This is reminiscent of the definition of Church numerals, which is also defined as a iterator.

For simplicity, we give the definition and proof for a single additional argument xxsource. The function hhsource is lambda-defined by:

Hλx.λy.Snd(yD0¯,Fx)where Dλp.Succ(Fstp),(Gx(Fstp)(Sndp))H \ident & \lambd[x][\lambd[y][\fn{Snd} (y D \tuple{\num{0}, F x})]] \intertext{where } D \ident & \lambd[p][\tuple{\fn{Succ} (\fn{Fst}\, p), (G x (\fn{Fst}\, p) (\fn{Snd}\, p))}]source

The iteration state we maintain is a pair, the first of which is the current yysource and the second is the corresponding value of hhsource. For every step of iteration we create a pair of new values of yysource and hhsource; after the iteration is done we return the second part of the pair and that's the final hhsource value. We now prove this is indeed a representation of primitive recursion.

We want to prove that for any nnsource and mmsource, Hn¯m¯h(n,m)¯H\,\num{n}\,\num{m} \red \num{h(n,m)}source. To do this we first show that if DnD[n¯/x]D_n \ident \Subst{D}{\num{n}}{x}source, then Dnm0¯,Fn¯m¯,h(n,m)¯D_n^m \tuple{\num{0}, F\, \num{n}} \red \tuple{\num{m}, \num{h(n, m)}}source We proceed by induction on mmsource.

If m=0m=0source, we want Dn00¯,Fn¯0¯,h(n,0)¯D_n^0 \tuple{\num{0}, F\, \num{n}} \red \tuple{\num{0}, \num{h(n, 0)}}source. But Dn00¯,Fn¯D_n^0 \tuple{\num{0}, F\, \num{n}}source just is 0¯,Fn¯\tuple{\num{0}, F\, \num{n}}source. Since FFsource lambda-defines ffsource, this reduces to 0¯,f(n)¯\tuple{\num{0}, \num{f(n)}}source, and since f(n)=h(n,0)f(n) = h(n, 0)source, this is 0¯,h(n,0)¯\tuple{\num{0}, \num{h(n,0)}}source

Now suppose that Dnm0¯,Fn¯m¯,h(n,m)¯D_n^m \tuple{\num{0}, F\, \num{n}} \red \tuple{\num{m}, \num{h(n, m)}}source. We want to show that Dnm+10¯,Fn¯m+1¯,h(n,m+1)¯D_n^{m+1} \tuple{\num{0}, F\, \num{n}} \red \tuple{\num{m+1}, \num{h(n, m+1)}}source.

Dnm+10¯,Fn¯Dn(Dnm0¯,Fn¯)Dnm¯,h(n,m)¯ (by IH)(λp.Succ(Fstp),(Gn¯(Fstp)(Sndp)))m¯,h(n,m)¯Succ(Fstm¯,h(n,m)¯),(Gn¯(Fstm¯,h(n,m)¯)(Sndm¯,h(n,m)¯))Succm¯,(Gn¯m¯h(n,m)¯)m+1¯,g(n,m,h(n,m))¯D_n^{m+1} \tuple{\num{0}, F\, \num{n}} & \ident D_n(D_n^{m} \tuple{\num{0}, F\, \num{n}})\\ & \red D_n\,\tuple{\num{m}, \num{h(n, m)}} \text{\qquad (by IH)}\\ & \ident (\lambd[p][ \tuple{ \fn{Succ} (\fn{Fst}\, p), (G \, \num{n} (\fn{Fst}\, p) (\fn{Snd}\, p)) }]) \tuple{\num{m}, \num{h(n,m)}}\\ & \redone \tuple{\fn{Succ} (\fn{Fst}\, \tuple{\num{m}, \num{h(n,m)}}),\\ & \qquad (G \, \num{n} (\fn{Fst}\, \tuple{\num{m}, \num{h(n,m)}}) (\fn{Snd}\, \tuple{\num{m}, \num{h(n,m)}}))}\\ & \red \tuple{\fn{Succ}\, \num{m}, (G \, \num{n} \,\num{m} \, \num{h(n,m)})}\\ & \red \tuple{\num{m+1}, \num{g(n, m, h(n, m))}}source

Since g(n,m,h(n,m))=h(n,m+1)g(n, m, h(n, m)) = h(n, m+1)source, we are done.

Finally, consider

Hn¯m¯λx.λy.Snd(y(λp.Succ(Fstp),(Gx(Fstp)(Sndp)))0¯,Fx)n¯m¯Snd(m¯(λp.Succ(Fstp),(Gn¯(Fstp)(Sndp)))Dn0¯,Fn¯)Snd(m¯Dn0¯,Fn¯)Snd(Dnm0¯,Fn¯)Sndm¯,h(n,m)¯h(n,m)¯.H\,\num n\,\num m &\ident \lambd[x][\lambd[y][\fn{Snd} (y (\lambd[p].\tuple{\fn{Succ} (\fn{Fst}\, p), (G\, x\, (\fn{Fst}\, p)\, (\fn{Snd}\, p))}) \tuple{\num{0}, F x})]]\\ & \qquad\,\num n\, \num m\\ & \red \fn{Snd} (\num m \, \underbrace{(\lambd[p].\tuple{\fn{Succ} (\fn{Fst}\, p), (G \,\num n\, (\fn{Fst}\, p) (\fn{Snd}\, p))})}_{D_n} \tuple{\num{0}, F \num n})\\ & \ident \fn{Snd} (\num m \, D_n\, \tuple{\num{0}, F \num n})\\ & \red \fn{Snd} \,(D_n^m \tuple{\num{0}, F \num n})\\ & \red \fn{Snd} \,\tuple{\num{m}, \num{h(n, m)}} \\ & \red \num{h(n,m)}.source

Every primitive recursive function is lambda definable

Every primitive recursive function is lambda-definable.

Proof

By the lemma on lambda definability of the basic functions, all basic functions are lambda-definable, and by the lemma on closure under composition and the lemma on closure under primitive recursion, the lambda-definable functions are closed under composition and primitive recursion.

Source file content/lambda-calculus/lambda-definability/fixpoints.tex

Fixpoints

Suppose we wanted to define the factorial function by recursion as a term Fac\fn{Fac}source with the following property:

Facλn.IsZeron1¯(Multn(Fac(Predn)))\fn{Fac} \ident \lambd[n][\fn{IsZero}\, n\, \num 1 (\fn{Mult}\, n (\fn{Fac}(\fn{Pred} \, n)))]source

That is, the factorial of nnsource is 11source if n=0n = 0source, and nnsource times the factorial of n1n-1source otherwise. Of course, we cannot define the term Fac\fn{Fac}source this way since Fac\fn{Fac}source itself occurs in the right-hand side. Such recursive definitions involving self-reference are not part of the lambda calculus. Defining a term, e.g., by

Multλab.a(Adda)0\fn{Mult} \ident \lambd[ab][a (\fn{Add}\, a) 0]source

only involves previously defined terms in the right-hand side, such as Add\fn{Add}source. We can always remove Add\fn{Add}source by replacing it with its defining term. This would give the term Mult\fn{Mult}source as a pure lambda term; if Add\fn{Add}source itself involved defined terms (as, e.g., Add\fn{Add}'source does), we could continue this process and finally arrive at a pure lambda term.

However this is not true in the case of recursive definitions like the one of Fac\fn{Fac}source above. If we replace the occurrence of Fac\fn{Fac}source on the right-hand side with the definition of Fac\fn{Fac}source itself, we get:

Facλn.IsZeron1¯(Multn((λn.IsZeron1¯(Multn(Fac(Predn))))(Predn)))\fn{Fac} & \ident \lambd[n][\fn{IsZero}\, n\, \num{1}] \\ & \qquad (\fn{Mult}\, n ((\lambd[n][\fn{IsZero} \, n \, \num 1\, (\fn{Mult}\, n\, (\fn{Fac} (\fn{Pred}\, n)))]) (\fn{Pred}\, n)))source

and we still haven't gotten rid of Fac\fn{Fac}source on the right-hand side. Clearly, if we repeat this process, the definition keeps growing longer and the process never results in a pure lambda term. Thus this way of defining factorial (or more generally recursive functions) is not feasible.

The recursive definition does tell us something, though: If ffsource were a term representing the factorial function, then the term

Facλg.λn.IsZeron1¯(Multn(g(Predn)))\fn{Fac}' \ident \lambd[g][\lambd[n][\fn{IsZero} \, n \, \num 1\, (\fn{Mult} \, n\, (g (\fn{Pred} n)))]]source

applied to the term ffsource, i.e., Facf\fn{Fac}'\,fsource, also represents the factorial function. That is, if we regard Fac\fn{Fac}'source as a function accepting a function and returning a function, the value of Facf\fn{Fac'}\, fsource is just ffsource, provided ffsource is the factorial. A function ffsource with the property that Facf=βf\fn{Fac}' \, f \equal[\beta] fsource is called a fixpoint of Fac\fn{Fac}'source. So, the factorial is a fixpoint of Fac\fn{Fac}'source.

There are terms in the lambda calculus that compute the fixpoints of a given term, and these terms can then be used to turn a term like Fac\fn{Fac}'source into the definition of the factorial.

Definition of Turing's fixpoint combinator

The Y-combinator is the term:

Y(λux.x(uux))(λux.x(uux)).Y \ident (\lambd[ux][x(uux)])(\lambd[ux][x(uux)]).source

Turing combinator produces a fixpoint

YYsource has the property that Ygg(Yg)Yg \red g(Yg)source for any term ggsource. Thus, YgYgsource is always a fixpoint of ggsource.

Proof

Let's abbreviate (λux.x(uux))(\lambd[ux][x(uux)])source by UUsource, so that YUUY \ident UUsource. Then

Yg(λux.x(uux))Ug(λx.x(UUx))gg(UUg)g(Yg).Y g &\ident (\lambd[ux][x(uux)])U\,g \\ &\red (\lambd[x][x(UUx)])g\\ & \red g(UUg) \ident g(Yg).source

Since g(Yg)g(Yg)source and YgYgsource both reduce to g(Yg)g(Yg)source, g(Yg)=βYgg(Yg) \equal[\beta] Ygsource, so YgYgsource is a fixpoint of ggsource.

Of course, since YgYgsource is a redex, the reduction can continue indefinitely:

Ygg(Yg)g(g(Yg))g(g(g(Yg)))Y g &\red g (Y g) \\ &\red g (g (Y g)) \\ &\red g(g (g (Y g)))\\ &\ldotssource

So we can think of YgYgsource as ggsource applied to itself infinitely many times. If we apply ggsource to it one additional time, we---so to speak---aren't doing anything extra; ggsource applied to ggsource applied infinitely many times to YgYgsource is still ggsource applied to YgYgsource infinitely many times.

Note that the above sequence of β\betasource-reduction steps starting with YgYgsource is infinite. So if we apply YgYgsource to some term, i.e., consider (Yg)N(Yg)Nsource, that term will also reduce to infinitely many different terms, namely (g(Yg))N(g(Yg))Nsource, (g(g(Yg)))N(g(g(Yg)))Nsource, dots. It is nevertheless possible that some other sequence of reduction steps does terminate in a normal form.

Take the factorial for instance. Define Fac\fn{Fac}source as YFacY\, \fn{Fac}'source (i.e., a fixpoint of Fac\fn{Fac'}source). Then:

Fac3¯YFac3¯Fac(YFac)3¯(λx.λn.IsZeron1¯(Multn(x(Predn))))Fac3¯IsZero3¯1¯(Mult3¯(Fac(Pred3¯)))Mult3¯(Fac2¯).Similarly,Fac2¯Mult2¯(Fac1¯)Fac1¯Mult1¯(Fac0¯)butFac0¯Fac(YFac)0¯(λx.λn.IsZeron1¯(Multn(x(Predn))))Fac0¯IsZero0¯1¯(Mult0¯(Fac(Pred0¯))).1¯.So togetherFac3¯Mult3¯(Mult2¯(Mult1¯1¯)).\fn{Fac}\,\num 3 &\red Y\, \fn{Fac}' \, \num 3 \\ &\red \fn{Fac}' (Y \,\fn{Fac}') \, \num 3 \\ &\ident (\lambd[x][\lambd[n][\fn{IsZero} \, n\, \num 1\, (\fn{Mult}\, n\, (x (\fn{Pred}\, n)))]]) \, \fn{Fac} \, \num 3\\ & \red \fn{IsZero} \, \num 3\, \num 1\, (\fn{Mult}\, \num 3\, (\fn{Fac} (\fn{Pred}\, \num 3))) \\ &\red \fn{Mult}\, \num 3 \, (\fn{Fac} \, \num 2). \intertext{Similarly,} \fn{Fac}\,\num 2 &\red \fn{Mult}\, \num 2 \, (\fn{Fac} \, \num 1) \\ \fn{Fac}\,\num 1 &\red \fn{Mult}\, \num 1 \, (\fn{Fac} \, \num 0) \intertext{but} \fn{Fac}\,\num 0 &\red \fn{Fac}' (Y \,\fn{Fac}') \, \num 0 \\ &\ident (\lambd[x][\lambd[n][\fn{IsZero} \, n\, \num 1\, (\fn{Mult}\, n\, (x (\fn{Pred}\, n)))]]) \, \fn{Fac} \, \num 0\\ & \red \fn{IsZero} \, \num 0\, \num 1\, (\fn{Mult}\, \num 0\, (\fn{Fac} (\fn{Pred}\, \num 0))).\\ &\red \num 1. \intertext{So together} \fn{Fac}\,\num 3 &\red \fn{Mult}\, \num 3 \, (\fn{Mult} \, \num 2\, (\fn{Mult} \, \num 1\, \num 1)).source

What goes for Fac\fn{Fac'}source goes for any recursive definition. Suppose we have a recursive equation

gx1xn=βNwhere N may contain g and x1, , xn. Then there is always a term G(Yλg.λx1xn.N) such thatGx1xn=βN[G/g].For by the fixpoint theorem,G(Yλg.λx1xn.N)λg.λx1xn.N(Yλg.λx1xn.N)(λg.λx1xn.N)Gand consequentlyGx1xn(λg.λx1xn.N)Gx1xn(λx1xn.N[G/g])x1xnN[G/g].g\,x_1\dots x_n & \equal[\beta] N \intertext{where $N$ may contain~$g$ and $x_1$, \dots,~$x_n$. Then there is always a term~$G \ident (Y \lambd[g][\lambd[x_1\dots x_n][N]])$ such that} G\,x_1 \dots x_n & \equal[\beta] \Subst{N}{G}{g}. \intertext{For by the fixpoint theorem,} G \ident (Y \lambd[g][\lambd[x_1\dots x_n][N]]) & \red \lambd[g][\lambd[x_1\dots x_n][N]](Y \lambd[g][\lambd[x_1\dots x_n][N]])\\ & \ident (\lambd[g][\lambd[x_1\dots x_n][N]])\,G \intertext{and consequently} G\,x_1 \dots x_n & \red (\lambd[g][\lambd[x_1\dots x_n][N]])\,G\,x_1\dots x_n\\ & \red (\lambd[x_1\dots x_n][\Subst{N}{G}{g}])\,x_1\dots x_n\\ & \red \Subst{N}{G}{g}.source

The YYsource combinator of the definition of Turing's fixpoint combinator is due to Alan Turing. Alonzo Church had proposed a different version which we'll call YCY_Csource:

YCλg.(λx.g(xx))(λx.g(xx)).Y_C \ident \lambd[g][(\lambd[x][g(xx)])(\lambd[x][g(xx)])].source

Church's combinator is a bit weaker than Turing's in that Yg=βg(Yg)Yg \equal[\beta] g(Yg)source but not Ygβg(Yg)Yg \bred g(Yg)source. Let VVsource be the term λx.g(xx)\lambd[x][g(xx)]source, so that YCλg.VVY_C \ident \lambd[g][VV]source. Then

VV(λx.g(xx))Vg(VV) and thusYCg(λg.VV)gVVg(VV), but alsog(YCg)g((λg.VV)g)g(VV).VV & \ident (\lambd[x][g(xx)])V \red g(VV) \text{ and thus}\\ Y_C g & \ident (\lambd[g][VV])g \red VV \red g(VV), \text{ but also}\\ g(Y_C g) & \ident g((\lambd[g][VV])g) \red g(VV).source

In other words, YCgY_Cgsource and g(YCg)g(Y_Cg)source reduce to a common term g(VV)g(VV)source; so YCg=βg(YCg)Y_Cg \equal[\beta] g(Y_Cg)source. This is often enough for applications.

Source file content/lambda-calculus/lambda-definability/minimization.tex

Minimization

The general recursive functions are those that can be obtained from the basic functions zero\Zerosource, succ\Succsource, Pin\Proj{n}{i}source by composition, primitive recursion, and regular minimization. To show that all general recursive functions are lambda-definable we have to show that any function defined by regular minimization from a lambda definable function is itself lambda-definable.

Closure under regular minimization

If f(x1,,xk,y)f(x_1, \dots, x_k, y)source is regular and lambda-definable, then ggsource defined by

g(x1,,xk)=μyf(x1,,xk,y)=0g(x_1, \dots, x_k) = \umin{y}{f(x_1,\dots,x_k, y) = 0}source

is also lambda-definable.

Proof

Suppose the lambda term FFsource λ\lambdasource-defines the regular function f(x,y)f(\vec x, y)source. To lambda-define hhsource we use a search function and a fixpoint combinator:

Searchλg.λfxy.IsZero(fxy)y(gx(Succy)Hλx.(YSearch)Fx0¯,\fn{Search} & \ident \lambd[g][\lambd[f\,\vec{x}\,y][ \fn{IsZero} (f\, \vec{x}\, y)\, y\, (g\, \vec{x} (\fn{Succ}\, y)]]\\ H & \ident \lambd[\vec x][(Y \, \fn{Search}) F\, \vec{x}\, \num{0}],source

where YYsource is any fixpoint combinator. Informally speaking, Search\fn{Search}source is a self-referencing function: starting with yysource, test whether fxyf\, \vec x\, ysource is zero: if so, return yysource, otherwise call itself with Succy\fn{Succ}\, ysource. Thus (YSearch)Fn1¯nk¯0¯(Y \, \fn{Search}) F \num{n_1}\dots\num{n_k}\,\num{0}source returns the least mmsource for which f(n1,,nk,m)=0f(n_1, \dots, n_k, m) = 0source.

Specifically, observe that

(YSearch)Fn1¯nk¯m¯m¯if f(n1,,nk,m)=0, or(YSearch)Fn1¯nk¯m+1¯otherwise. Since f is regular, f(n1,,nk,y)=0 for some y, and so(YSearch)Fn1¯nk¯0¯h(n1,,nk)¯.(Y \, \fn{Search}) F \num{n_1} \dots\num{n_k}\, \num{m} & \red \num{m} \intertext{if $f(n_1, \dots, n_k, m) = 0$, or} & \red (Y \, \fn{Search}) F\, \num{n_1} \dots\num{n_k}\, \num{m+1} \intertext{otherwise. Since $f$ is regular, $f(n_1, \dots, n_k, y) = 0$ for some $y$, and so} (Y \, \fn{Search}) F \num{n_1} \dots\num{n_k}\,\num{0} & \red \num{h(n_1, \dots, n_k)}.source

Every general recursive function is lambda definable

Every general recursive function is lambda-definable.

Proof

By the lemma on lambda definability of the basic functions, all basic functions are lambda-definable, and by the lemma on closure under composition, the lemma on closure under primitive recursion, and the lemma on closure under regular minimization, the lambda-definable functions are closed under composition, primitive recursion, and regular minimization.

Source file content/lambda-calculus/lambda-definability/partial-recursive-functions.tex

Partial Recursive Functions are lambda definable

Partial recursive functions are those obtained from the basic functions by composition, primitive recursion, and unbounded minimization. They differ from general recursive function in that the functions used in unbounded search are not required to be regular. Not requiring regularity means that functions defined by minimization may sometimes not be defined.

At first glance it might seem that the same methods used to show that the (total) general recursive functions are all lambda-definable can be used to prove that all partial recursive functions are lambda-definable. For instance, the composition of ffsource with ggsource is lambda-defined by λx.F(Gx)\lambd[x][F (G x)]source if ffsource and ggsource are lambda-defined by terms FFsource and GGsource, respectively. However, when the functions are partial, this is problematic. When g(x)g(x)source is undefined, meaning GxG xsource has no normal form. In most cases this means that F(Gx)F (G x)source has no normal forms either, which is what we want. But consider when FFsource is λx.λy.y\lambd[x][\lambd[y][y]]source, in which case F(Gx)F (G x)source does have a normal form (λy.y\lambd[y][y]source).

This problem is not insurmountable, and there are ways to lambda-define all partial recursive functions in such a way that undefined values are represented by terms without a normal form. These ways are, however, somewhat more complicated and less intuitive than the approach we have taken for general recursive functions. We record the theorem here without proof:

Partial recursive functions are lambda definable

All partial recursive functions are lambda-definable.

Source file content/lambda-calculus/lambda-definability/lambda-definable-recursive.tex

lambda definable Functions are Recursive

Not only are all partial recursive functions lambda-definable, the converse is true, too. That is, all lambda-definable functions are partial recursive.

Lambda-definable partial functions are partial recursive

If a partial function ffsource is lambda-definable, it is partial recursive.

Proof

We only sketch the proof. First, we arithmetize λ\lambdsource-terms, i.e., systematially assign Gödel numbers to λ\lambdsource-terms, using the usual power-of-primes coding of sequences. Then we define a partial recursive function normalize(t)\fn{normalize}(t)source operating on the Gödel number ttsource of a lambda term as argument, and which returns the Gödel number of the normal form if it has one, or is undefined otherwise. Then define two partial recursive functions toChurch\fn{toChurch}source and fromChurch\fn{fromChurch}source that maps natural numbers to and from the Gödel numbers of the corresponding Church numeral.

Using these recursive functions, we can define the function ffsource as a partial recursive function. There is a λ\lambdsource-term FFsource that lambda-defines ffsource. To compute f(n1,,nk)f(n_1, \dots, n_k)source, first obtain the Gödel numbers of the corresponding Church numerals using toChurch(ni)\fn{toChurch}(n_i)source, append these to #F#\Gn{F}source to obtain the Gödel number of the term Fn1¯nk¯F \num{n_1}\dots\num{n_k}source. Now use normalize\fn{normalize}source on this Gödel number. If f(n1,,nk)f(n_1, \dots, n_k)source is defined, Fn1¯nk¯F \num{n_1}\dots\num{n_k}source has a normal form (which must be a Church numeral), and otherwise it has no normal form (and so

normalize(#Fn1¯nk¯#)\fn{normalize}(\Gn{F\num{n_1}\dots\num{n_k}})source

is undefined). Finally, use fromChurch\fn{fromChurch}source on the Gödel number of the normalized term.

Source disclosures