Skip to main content

Chapter 3: Church Numerals: Increment

Church Numerals

The most important data in computer programs are integers.

Let's define natural integers.

0=λs.λz.z1=λs.λz.s(z)2=λs.λz.s(s(z))3=λs.λz.s(s(s(z)))n=λs.λz.sn(z)0=\lambda s.\lambda z.z\\ 1=\lambda s.\lambda z.s(z)\\ 2=\lambda s.\lambda z.s(s(z))\\ 3=\lambda s.\lambda z.s(s(s(z)))\\ \vdots\\ n=\lambda s.\lambda z.s^n(z)

Why should we define integers like this? What does ss and zz mean here?

Let's take 22 for example to watch how church numerals could be converted back to normal ones. let s=λx.(x+1),z=0s=\lambda x.(x+1), z=0, then:

2sz=(λs.λz.s(s(z)))sz=s(s(z))=s(s(0))=s(0+1)=1+1=2\begin{array}{} 2sz&=&(\lambda s.\lambda z.s(s(z)))sz\\ &=&s(s(z))\\ &=&s(s(0))\\ &=&s(0+1)\\ &=&1+1\\ &=&2\\ \end{array}

It is clear now: ss stands for succussor and zz stands for zero. It is built base on recursion. And yes, it is exactly how natural numbers are defined mathematically.

Successor

To get the successor of a church numeral, just simply wrap an ss around it.

S=λn.λs.λz.(s(nsz))Sn=n+1S=\lambda n.\lambda s.\lambda z.(s(nsz))\\ Sn=n+1

Proof:

Sn=(λn.λs.λz.(s(nsz)))(λs.λz.sn(z))=(λs.λz.(s((λs.λz.sn(z))sz)))=(λs.λz.(s(sn(z))))=(λs.λz.(sn+1(z)))=(λs.λz.(sn+1(z)))=n+1\begin{array}{} Sn&=&(\lambda n.\lambda s.\lambda z.(s(nsz)))(\lambda s.\lambda z.s^n(z))\\ &=&(\lambda s'.\lambda z'.(s'((\lambda s.\lambda z.s^n(z))s'z')))\\ &=&(\lambda s'.\lambda z'.(s'(s'^n(z'))))\\ &=&(\lambda s'.\lambda z'.(s'^{n+1}(z')))\\ &=&(\lambda s.\lambda z.(s^{n+1}(z)))\\ &=&n+1\\ \end{array}

Addition

A Church numeral nn is actually a recursion builder repeating a function for nn times. Making use of this the addition operation of church numerals could be deduced.

add=λmn.(mSn)add mn=m+n\text{add}=\lambda mn.(mSn)\\ \text{add }mn=m+n

It reads as if "evaluating mthm^\text{th} successor of nn".

Proof:

add mn=(λmn.(mSn))mn=(λs.λz.sm(z))S(λs.λz.sn(z))=λz.Sm(z)(λs.λz.sn(z))=Sm(λs.λz.sn(z))=λs.λz.sm+n(z)=m+n\begin{array}{} \text{add }mn&=&(\lambda mn.(mSn))mn\\ &=&(\lambda s.\lambda z.s^m(z))S(\lambda s.\lambda z.s^n(z))\\ &=&\lambda z.S^m(z)(\lambda s.\lambda z.s^n(z))\\ &=&S^m(\lambda s.\lambda z.s^n(z))\\ &=&\lambda s.\lambda z.s^{m+n}(z)\\ &=&m+n\\ \end{array}

Actually, there is a direct definition of addition.

add=λm.λn.λs.λz.ms(nsz)\text{add}=\lambda m.\lambda n.\lambda s.\lambda z.ms(nsz)\\

It reads as if "evaluating mthm^\text{th} successor of 'nthn^\text{th} successor of 00'".

Proof is similar to indirect one.

Multiplication

Indirect definition:

mul=λmn.m(add n)0mul mn=m×n\text{mul}=\lambda mn.m(\text{add }n)0\\ \text{mul }mn=m\times n

It reads as if "add nn for mm times to 00".

Direct definition:

mul=λm.λn.λs.λz.m(ns)z\text{mul}=\lambda m.\lambda n.\lambda s.\lambda z.m(ns)z

It reads as if "'evaluating nthn^\text{th} successor' for mm times of 00".

Proofs are similar to the addition operation.

Exponentiation

Indirect definition:

exp=λmn.n(mul m)1exp mn=mn\text{exp}=\lambda mn.n(\text{mul }m)1\\ \text{exp }mn=m^n

It reads as if "multiplies mm for nn times to 11".

Proof is similar to the multiplication operation.

Direct definition:

exp=λm.λn.λs.λz.nmsz\text{exp}=\lambda m.\lambda n.\lambda s.\lambda z.nmsz

Or just

exp=λm.λn.nm\text{exp}=\lambda m.\lambda n.nm

for short.

Proof:

exp mn=nm=(λs.λz.sn(z))m=λz.mn(z)=λs.mn(s)=λs.λz.mn(s)z=λs.λz.m(m(mn timess))z=λs.λz.m(m(mn1 times(λz1.sm(z1))apply s for m times))z=λs.λz.m(m(mn2 times(λz2.(λz1.sm(z1))m(z2)))apply ‘apply s for m times’ for m times))z=λs.λz.m(m(mn2 times(λz2.(λz1.sm(z1))m(z2)))apply s for m×m=m2 times))z==λs.λz.λzn.((λz2.(λz1.sm(z1))m(z2)))m(zn)apply s for m×m××mn times=mn timesz=λs.λz.smn(z)=mn\begin{array}{} \text{exp }mn&=&nm\\ &=&(\lambda s.\lambda z.s^n(z))m\\ &=&\lambda z.m^n(z)\\ &=&\lambda s.m^n(s)\\ &=&\lambda s.\lambda z.m^n(s)z\\ &=&\lambda s.\lambda z.\underbrace{m(\cdots m(m}_{n \text{ times}}s))z\\ &=&\lambda s.\lambda z.\underbrace{m(\cdots m(m}_{n-1 \text{ times}}\underbrace{(\lambda z_1.s^m(z_1))}_{\text{apply } s \text{ for } m \text{ times}}))z\\ &=&\lambda s.\lambda z.\underbrace{m(\cdots m(m}_{n-2 \text{ times}}\underbrace{(\lambda z_2.(\lambda z_1.s^m(z_1))^m(z_2)))}_{\text{apply `apply } s \text{ for } m \text{ times' for } m \text{ times}}))z\\ &=&\lambda s.\lambda z.\underbrace{m(\cdots m(m}_{n-2 \text{ times}}\underbrace{(\lambda z_2.(\lambda z_1.s^m(z_1))^m(z_2)))}_{\text{apply } s \text{ for } m\times m=m^2 \text{ times}}))z\\ &=&\cdots\\ &=&\lambda s.\lambda z.\underbrace{\lambda z_n.(\cdots(\lambda z_2.(\lambda z_1.s^m(z_1))^m(z_2))\cdots)^m(z_n)}_{\text{apply } s \text{ for } \underbrace{m\times m \times\cdots \times m}_{n \text{ times}}=m^n \text{ times}}z\\ &=&\lambda s.\lambda z.s^{m^n}(z)\\ &=&m^n \end{array}

Sequences

Observe the architecture of sequences and church numerals:

SequencesChurch NumeralsInitial terma0zRecurrence functionfan1ans\begin{array}{l} &\text{Sequences}&\text{Church Numerals}\\ \text{Initial term}&a_0 & z\\ \text{Recurrence function}&f_{a_{n-1}\rightarrow a_n} & s \end{array}

And yes, natural numbers, are no more than a special arithmetic sequence with initial 00 and successor fucntion λx.(x+1)\lambda x.(x+1).

Church numerals, however, are unnecessary to be mere natural numbers, not even mere arithmetic sequence, but whatever sequence you want! e. g.

  • s=λx.(x+d),z=a0s=\lambda x.(x+d), z=a_0 makes a arithmetic sequence.
  • s=λx.(x×q),z=a0(q0)s=\lambda x.(x\times q), z=a_0 (q\ne0) makes a geometric sequence.

Let's prove obscure direct definition of exponentiation via geometric sequence:

exp mn=λs.λz.nsz=mn\text{exp }mn=\lambda s.\lambda z.ns'z'=m^n

ss' should be a multiplier of mm, so it should evaluate mthm^{\text{th}} succussor to 00, regardless of succussor function, which represents previous result of evaluation.

s=λx.λz.(m×x+z)=λx.λz.(x+x++xm times+z)=λs.λz.sm(z)=m\begin{array}{} s'&=&\lambda x.\lambda z.(m\times x+z)\\ &=&\lambda x.\lambda z.(\underbrace{x+x+\cdots+x}_{m \text{ times}}+z)\\ &=&\lambda s.\lambda z.s^m(z)\\ &=&m \end{array}

zz' should be one.

z=szz'=sz

Neat.