PL: Lecture #12  Tuesday, February 22nd

Lambda Calculus — Schlac

PLAI §22 (we do much more)

We know that many constructs that are usually thought of as primitives are not really needed — we can implement them ourselves given enough tools. The question is how far can we go?

The answer: as far as we want. For example:

(define foo((lambda(f)((lambda(x)(x x))(lambda(x)(f(x x)))))(lambda(
f)(lambda(x)(((x(lambda(x)(lambda(x y)y))(lambda(x y)x))(x(lambda(x)
(lambda(x y)y))(lambda(x y)x))(((x(lambda (p)(lambda(s)(s(p(lambda(x
y)y))(lambda(f x)(f((p(lambda(x y)y))f x))))))(lambda(s) (s(lambda(f
x)x)(lambda(f x)x))))(lambda(x y)x))(lambda(x)(lambda(x y)y))(lambda
(x y)x)))(lambda(f x)(f x))((f((x(lambda(p)(lambda(s)(s(p(lambda(x y
)y))(lambda(f x)(f((p(lambda(x y)y))f x))))))(lambda(y s)(s(lambda(f
x)x)(lambda(f x)x))))(lambda(x y)x)))(lambda(n)(lambda(f x)(f(n f x)
)))(f((((x(lambda(p)(lambda(s)(s(p (lambda(x y)y))(lambda(f x)(f((p(
lambda(x y)y))f x))))))(lambda(s)(s(lambda(f x) x)(lambda(f x)x))))(
lambda(x y)x))(lambda(p)(lambda(s)(s(p(lambda(x y)y))(lambda(f x)(f(
(p(lambda(x y)y))f x))))))(lambda(s)(s(lambda(f x)x)(lambda(f x)x)))
)(lambda(x y)x)))))))))

We begin with a very minimal language, which is based on the Lambda Calculus. In this language we get a very minimal set of constructs and values.

In DrRacket, this we will use the Schlac language level (stands for “SchemeRacket as Lambda Calculus”). This language has a Racket-like syntax, but don’t be confused — it is very different from Racket. The only constructs that are available in this language are: lambda expressions of at least one argument, function application (again, at least one argument), and simple definition forms which are similar to the ones in the “Broken define” language — definitions are used as shorthand, and cannot be used for recursive function definition. They’re also only allowed at the toplevel — no local helpers, and a definition is not an expression that can appear anywhere. The BNF is therefore:

<SCHLAC>      ::= <SCHLAC-TOP> ...

                | (define <id> <SCHLAC-EXPR>)

<SCHLAC-EXPR> ::= <id>
                | (lambda (<id> <id> ...) <SCHLAC-EXPR>)
                | (<SCHLAC-EXPR> <SCHLAC-EXPR> <SCHLAC-EXPR> ...)

Since this language has no primitive values (other than functions), Racket numbers and booleans are also considered identifiers, and have no built-in value that come with the language. In addition, all functions and function calls are curried, so

(lambda (x y z) (z y x))

is actually shorthand for

(lambda (x) (lambda (y) (lambda (z) ((z y) x))))

The rules for evaluation are simple, there is one very important rule for evaluation which is called “beta reduction”:

((lambda (x) E1) E2) --> E1[E2/x]

where substitution in this context requires being careful so you won’t capture names. This requires you to be able to do another kind of transformation which is called “alpha conversion”, which basically says that you can rename identifiers as long as you keep the same binding structure (eg, a valid renaming does not change the de-Bruijn form of the expression). There is one more rule that can be used, eta conversion which says that (lambda (x) (f x)) is the same as f (we used this rule above when deriving the Y combinator).

One last difference between Schlac and Racket is that Schlac is a lazy language. This will be important since we do not have any built-in special forms like if.

Here is a Schlac definition for the identity function:

(define identity (lambda (x) x))

and there is not much that we can do with this now:

> identity
> (identity identity)
> (identity identity identity)

(In the last expression, note that (id id id) is shorthand for ((id id) id), and since (id id) is the identity, applying that on id returns it again.)

Church Numerals

So far, it seems like it is impossible to do anything useful in this language, since all we have are functions and applications. We know how to write the identity function, but what about other values? For example, can you write code that evaluates to zero?

What’s zero? I only know how to write functions!

(Turing Machine programmer: “What’s a function? — I only know how to write 0s and 1s!”)

The first thing we therefore need is to be able to encode numbers as functions. For zero, we will use a function of two arguments that simply returns its second value:

(define 0 (lambda (f) (lambda (x) x)))

or, more concisely

(define 0 (lambda (f x) x))

This is the first step in an encoding that is known as Church Numerals: an encoding of natural numbers as functions. The number zero is encoded as a function that takes in a function and a second value, and applies the function zero times on the argument (which is really what the above definition is doing). Following this view, the number one is going to be a function of two arguments, that applies the first on the second one time:

(define 1 (lambda (f x) (f x)))

and note that 1 is just like the identity function (as long as you give it a function as its first input, but this is always the case in Schlac). The next number on the list is two — which applies the first argument on the second one twice:

(define 2 (lambda (f x) (f (f x))))

We can go on doing this, but what we really want is a way to perform arbitrary arithmetic. The first requirement for that is an add1 function that increments its input (an encoded natural number) by one. To do this, we write a function that expects an encoded number:

(define add1 (lambda (n) ...))

and this function is expected to return an encoded number, which is always a function of f and x:

(define add1 (lambda (n) (lambda (f x) ...)))

Now, in the body, we need to apply f on x n+1 times — but remember that n is a function that will do n applications of its first argument on its second:

(define add1 (lambda (n) (lambda (f x) ... (n f x) ...)))

and all we have left to do now is to apply f one more time, yielding this definition for add1:

(define add1 (lambda (n) (lambda (f x) (f (n f x)))))

Using this, we can define a few useful numbers:

(define 1 (add1 0))
(define 2 (add1 1))
(define 3 (add1 2))
(define 4 (add1 3))
(define 5 (add1 4))

This is all nice theoretically, but how can we make sure that it is correct? Well, Schlac has a few additional built-in functions that translate Church numerals into Racket numbers. To try our definitions we use the ->nat (read: to natural number):

(->nat 0)
(->nat 5)
(->nat (add1 (add1 5)))

You can now verify that the identity function is really the same as the number 1:

(->nat identity)

We can even write a test case, since Schlac contains the test special form, but we have to be careful in that — first of all, we cannot test whether functions are equal (why?) so we must use ->nat, but

(test (->nat (add1 (add1 5))) => 7)

will not work since 7 is undefined. To overcome this, Schlac has a back-door for primitive Racket values — just use a quote:

(test (->nat (add1 (add1 5))) => '7)

We can now define natural number addition — one simple idea is to get two encoded numbers m and n, then start with x, apply f on it n times by using it as a function, then apply f m more times on the result in the same way:

(define + (lambda (m n) (lambda (f x) (m f (n f x)))))

or equivalently:

(define + (lambda (m n f x) (m f (n f x))))

Another idea is to use add1 and increment n by m using add1:

(define + (lambda (m n) (m add1 n)))
(->nat (+ 4 5))

We can also define multiplication of m and n quite easily — begin with addition — (lambda (x) (+ n x)) is a function that expects an x and returns (+ x n) — it’s an increment-by-n function. But since all functions and applications are curried, this is actually the same as (lambda (x) ((+ n) x)) which is the same as (+ n). Now, what we want to do is repeat this operation m times over zero, which will add n to zero m times, resulting in m * n. The definition is therefore:

(define * (lambda (m n) (m (+ n) 0)))
(->nat (* 4 5))
(->nat (+ 4 (* (+ 2 5) 5)))

An alternative approach is to consider

(lambda (x) (n f x))

for some encoded number n and a function f — this function is like f^n (f composed n times with itself). But remember that this is shorthand for

(lambda (x) ((n f) x))

and we know that (lambda (x) (foo x)) is just like foo (if it is a function), so this is equivalent to just

(n f)

So (n f) is f^n, and in the same way (m g) is g^m — if we use (n f) for g, we get (m (n f)) which is n self-compositions of f, self-composed m times. In other words, (m (n f)) is a function that is like m*n applications of f, so we can define multiplication as:

(define * (lambda (m n) (lambda (f) (m (n f)))))

which is the same as

(define * (lambda (m n f) (m (n f))))

The same principle can be used to define exponentiation (but now we have to be careful with the order since exponentiation is not commutative):

(define ^ (lambda (m n) (n (* m) 1)))
(->nat (^ 3 4))

And there is a similar alternative here too —

which basically says that any number encoding n is also the ?^n operation.

All of this is was not too complicated — but all so far all we did is write functions that increment their inputs in various ways. What about sub1? For that, we need to do some more work — we will need to encode booleans.