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:
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:
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
is actually shorthand for
The rules for evaluation are simple, there is one very important rule for evaluation which is called “beta reduction”:
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:
and there is not much that we can do with this now:
(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.)
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:
or, more concisely
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:
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:
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:
and this function is expected to return an encoded number, which is
always a function of f
and 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:
and all we have left to do now is to apply f
one more time, yielding
this definition for add1
:
Using this, we can define a few useful numbers:
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):
You can now verify that the identity function is really the same as the number 1:
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
will not work since 7
is undefined. To overcome this, Schlac has a
back-door
for primitive Racket values — just use a quote:
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:
or equivalently:
Another idea is to use add1
and increment n
by m
using add1
:
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:
An alternative approach is to consider
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
and we know that (lambda (x) (foo x))
is just like foo
(if it is a
function), so this is equivalent to just
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:
which is the same as
The same principle can be used to define exponentiation (but now we have to be careful with the order since exponentiation is not commutative):
And there is a similar alternative here too —
a Church numeral m
is the m-self-composition function,
and (1 m)
is just like m
^1
which is the same as m
(1
=identity
)
and (2 m)
is just like m
^2
— it takes a function f
, self
composes it m
times, and self composes the result m
times — for
a total of f
^(m*m)
and (3 m)
is similarly f
^(m*m*m)
so (n m)
is f
^(m^n)
(note that the first ^
is
self-compositions, and the second one is a mathematical exponent)
so (n m)
is a function that returns m
^n
self-compositions of an
input function,
Which means that (n m)
is the Church numeral for m
^n
, so we get:
(define ^ (lambda (m n) (n m)))
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.