Skip to main content

Chapter 4: Church Numerals: Decrement

Predecessor

The implementation of the predecessor function is much harder than previous ones. In fact, Church thought for a long time that it might not be possible, until his student Kleene found it. In fact, there is a legend that Kleene conceived the idea while visiting his dentist, which is why the trick is called the wisdom tooth trick.

Wisdom Tooth Trick

Let's define a helper function Φ\Phi:

Φ=λp.π(S(π1p))(π1p)\Phi=\lambda p.\pi(S(\pi_1p))(\pi_1p)

Initially, we have:

Φ(π0 0)=(π1 0)\Phi(\pi0\space0)=(\pi1\space0)

By means of mathematical induction, we get:

Φn(π0 0)=(πn (n1))\Phi^n(\pi0\space0)=(\pi n\space (n-1))

With the help of ordered pair, predecessor function could be defined as:

pred=λn.π2(nΦ(π0 0))pred n=n1\text{pred}=\lambda n.\pi_2(n\Phi(\pi0\space 0))\\ \text{pred }n=n-1

Note that pred 0=0\text{pred }0=0. It is a limit of church numerals. Is it possible to define negative numbers or even rational numbers? Of course! Remember what I mentioned in Data Abstraction part? Build one if it doesn't exist yet!

Subtraction

Subtraction compared to predecessor is similar to addition compared to successuor.

sub=λmn.n pred msub mn=mn\text{sub}=\lambda mn.n\text{ pred }m\\ \text{sub }mn=m-n

It reads as if "evaluating nthn^\text{th} predecessor of mm".

Proof is similar to the addition operation.

Note: if m<nm<n, sub mn=0\text{sub }mn=0 according to the definition of predecessor function.

Zero Predicate

Let's review the definitions of false and zero:

F=λxy.y0=λs.λz.zF=\lambda xy.y\\ 0=\lambda s.\lambda z.z

With α\alpha reduction, it is clear that:

F=0=λx.IF=0=\lambda x.I

where I=λx.xI=\lambda x.x, called identity function, which returns its parameter unchanged.

Then, 00 predicate could be defined as:

is-zero=λn.nF not F\text{is-zero}=\lambda n.nF\text{ not }F\\

Proof:

For n=0n=0:

is-zero 0=is-zero F=(λn.nF not F)F=FF not F=not F=T\begin{array}{} \text{is-zero }0&=&\text{is-zero }F\\ &=&(\lambda n.nF\text{ not }F)F\\ &=&FF\text{ not }F\\ &=&\text{not }F\\ &=&T \end{array}\\

For n>0n>0:

is-zero n=(λn.nF not F)n=nF not F=Fn(not)F=(λx.I)n(not)F=(λx.I)n1(I)F==IF=F\begin{array}{} \text{is-zero }n&=&(\lambda n.nF\text{ not }F)n\\ &=&nF\text{ not }F\\ &=&F^n(\text{not})F\\ &=&(\lambda x.I)^n(\text{not})F\\ &=&(\lambda x.I)^{n-1}(I)F\\ &=&\cdots\\ &=&IF\\ &=&F \end{array}

Comparison

Subtraction is a fundamental approach to compare two numbers. With the help of the fact that if mnm\leq n, sub mn=0\text{sub }mn=0, less-equal function could be defined as:

:le=λmn.is-zero(sub mn)\le:\text{le}=\lambda mn.\text{is-zero}(\text{sub }mn)

Since:

xyyxx=yx\le y\land y \le x \rightarrow x=y

Equality function could be defined as:

=:eq=λmn.(and(le mn)(le nm))=:\text{eq}=\lambda mn.(\text{and}(\text{le }mn)(\text{le }nm))

Rest part of work should be easy now.

:ge=λmn.le nm>:gt=λmn.not(le mn)<:lt=λmn.gt nm:ne=λmn.not(eq mn)\begin{array}{} \ge:&\text{ge}&=&\lambda mn.\text{le }nm\\ >:&\text{gt}&=&\lambda mn.\text{not}(\text{le }mn)\\ <:&\text{lt}&=&\lambda mn.\text{gt }nm\\ \ne:&\text{ne}&=&\lambda mn.\text{not}(\text{eq }mn) \end{array}