[n] = \x y. xnywhere, for generic lambda terms M and N, MnN represents the term M(...(M(M N))...), i.e. the application of M to N repeated n times. These terms [n] are called Church's numerals.
[f][n1]...[nk] = [f(n1,...,nk)]The equality here is the "lambda-convertibility" (see previous lecture), i.e. the equality in the theory of Lambda Calculus. Note that [f] is curried, i.e. it takes its arguments one by one.
(f o (g1,...,gk)) (n1,...,nh) = f(g1(n1,...,nh),..., gk(n1,...,nh))Example: the function f(n) = n + 2 can be defined as the composition of the successor function with itself, namely f = S o S.
f(0,n1,...,nk) = g(n1,...,nk)Examples:
f(n+1,n1,...,nk) = h(n, f(n,n1,...,nk), n1,...,nk)
f(n1,...,nk) = mu n. (g(n,n1,...,nk) = 0)where mu is the minimalization operator: mu n. P(n) returns the least natural number n such that P(n) holds. If such n does not exists, then the result is undefined.
Note that f could be computed by general iteration, i.e. using a while loop of the form
n := 0; while (g(n,n1,...,nk) <> 0) do n := n+1;
Example: Consider the function log2 n = k, where k is the number, if it exists, such that 2k = n (the result is undefined otherwise). Then log2 can be defined by minimalization from the function g(k,n) = | 2k - n |, where | m | represents the absolute value of m.
Definition The set of Recursive Functions is the smallest set which contains the initial functions and is closed w.r.t. composition, primitive recursion, and minimalization.
If we exclude minimalization from this construction, we get the set of Primitive Recursive Fuctions, which are all total. Minimalization is the only operator which introduces non-termination, i.e. partiality.
Theorem (Kleene, 1936) Every recursive function is lambda-definable.
[S][n] = (\z.\x y. x(z x y))[n] = \x y. x([n] x y) = \x y. x(xn y) = [n+1]
[f o (g1,...,gk)] = \x1...xh. [f]([g1]x1...xh) ... ([gk]x1...xh)
f(0) = g (constant)In ML we could define this function as:
f(n+1) = h(n,f(n))
fun f(n) = if n = 0 then g else h(n-1,f(n-1))or equivalently (remember that the syntax for \n. M[n] in ML is fn n => M[n]) :
val rec f = fn n => if n = 0 then g else h(n-1,f(n-1))The meaning of this declaration is that f is a fixpoint of the following functional F:
F = fn f' => fn n => if n = 0 then g else h(n-1,f'(n-1))Namely, f satisfies the equation
f = F(f)
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 = MnN, hence [is_zero][n] = [n]([true][false])[true] = ([true][false])n[true] and the latter is equal (lambda-convertible) to [true] if n=0, and to [false] otherwise.
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 = Mn N.
Predecessor = \z. Second (z Aux (Pair  ))
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 , give result .
f(n1,...,nk) = mu n. (g(n,n1,...,nk) = 0)we can rewrite it in ML as follows:
val rec h = fn n => fn n1 => ... fn nk => if g(n,n1,...,nk) = 0 then n else h (n+1) n1...nkWe apply now the method seen for primitive recursion (which works for every kind of recursive definition): Define H as
val f(n1,...,nk) = h 0 n1...nk
H = fn h' => fn n => fn n1 => ... fn nk => if g(n,n1,...,nk) = 0 then n else h' (n+1) n1...nkThen we can define
[h] = Y [H]
[f] = [h]
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 phin (0,g) = (n,f(n)).
fun f(n) = second (phin (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 phin, remember that in the Lambda Calculus we have MnN = [n] M N. Hence we can define
[f] = \z. Second (z [phi] (Pair  [g]))