Y M = M (Y M)One possible definition of such Y is

Y = \x. (\y. x(y y)) (\y. x(y y))In fact, Y M = (\y. M(y y)) (\y. M(y y)) = M((\y. M(y y)) (\y. M(y y))) = M (Y M).

We can therefore define

f = Y [F]It remains to define [F]. To this purpose, we need to define the booleans and the if-then-else operator, the test is_zero, and the predecessor.

[true] = \x y. xNote that we have:

[false] = \x y. y

[true] M N = MTherefore it is sufficient to define

[false] M N = N

[if_then_else] = \x y z . x y zIn fact we will have: [if_then_else] C M N = M if C = [true], and [if_then_else] C M N = N if C = [false].

[is_zero] = \x. x ([true][false])[true]In fact, [n] M N = M

Pair = \x y z. z x yNote that this definition is a correct encoding, since we have First (Pair M N) = M and Second (Pair M N) = N. Now define the encoding of the predecessor function as follows:

First = \p. p [true]

Second = \p. p [false]

Aux = \x. Pair ([S] (First x)) (First x)Where [S] = \z x y. x (z x y) is the usual encoding of the successor function. To see how the definition works, remember that [n] M N = M

Predecessor = \z. Second (z Aux (Pair [0] [0]))

Note that the reason why we need pairing is that the Aux function needs returning a pair of values. The pair is a particular case of the concept of record in Pascal and of structure in C/C++.

An alternative definition of the predecessor function, which does not make use of pairs, is the following:

Predecessor' = \z x y. z S2 Z2 Iwhere

S2 = \z w. w (z x)We leave to the interested reader to show that also Predecessor' is a correct encoding of the predecessor function.

Z2 = \w. y

I = \u. u

Note: both Predecessor and Predecessor', when applied to [0], give result [0].

f(nwe can rewrite it in ML as follows:_{1},...,n_{k}) = mu n. (g(n,n_{1},...,n_{k}) = 0)

val rec h = fn n => fn nWe apply now the method seen for primitive recursion (which works for every kind of recursive definition): Define H as_{1}=> ... fn n_{k}=> if g(n,n_{1},...,n_{k}) = 0 then n else h (n+1) n_{1}...n_{k}

val f(n_{1},...,n_{k}) = h 0 n_{1}...n_{k}

H = fn h' => fn n => fn nThen we can define_{1}=> ... fn n_{k}=> if g(n,n_{1},...,n_{k}) = 0 then n else h' (n+1) n_{1}...n_{k}

[h] = Y [H]

[f] = [h][0]

**Note:** In the classical proof of the Klenee's theorem,
the encoding of primitive recursion does not use the fixpoint operator.
The fixpoint operator in fact is necessary for expressing minimalization (and
general recursion), but not for primitive recursion. The classic
encoding of primitive recursion is done by using "definite iteration"
(i.e. iteration repeated a number of times known a-priori)
and pairs to store the partial result of the computation.
We give here the translation in ML and we leave the translation
from ML to the Lambda Calculus as an exercise.

Let f be defined by primitive recursion as:

f(0) = gthen f can be defined in pseudo-ML by the following declarations:

f(n+1) = h(n,f(n))

fun phi(m,n) = (m+1,h(m,n))Where second is the projection on the second element of a pair, i.e. second (a,b) = b. To see that the above is a correct definition, note that phi(m,f(m)) = (m+1,f(m+1)), hence phi

fun f(n) = second (phi^{n}(0,g))

In order to translate from ML to the Lambda Calculus,
we need to encode pairs and the projection operators like we did for the
predecessor function. As for definite iteration, needed for
representing phi^{n}, remember
that in the Lambda Calculus we have M^{n}N = [n] M N.
Hence we can define

[f] = \z. Second (z [phi] (Pair [0] [g]))