Our make-recursive
function is usually called the fixpoint operator
or the Y combinator.
It looks really simple when using the lazy version (remember: our version is the eager one):
Note that if we do allow a recursive definition for Y itself, then the definition can follow the definition that we’ve seen:
(define (Y f) (f (Y f)))
And this all comes from the loop generated by:
This expression, which is also called Omega (the (lambda (x) (x x))
part by itself is usually called omega and then (omega omega)
is
Omega), is also the idea behind many deep mathematical facts. As an
example for what it does, follow the next rule:
(Note the usage of colon for the first and quotes for the second — what is the equivalent of that in the lambda expression?)
By itself, this just gets you stuck in an infinite loop, as Omega does,
and the Y combinator adds F
to that to get an infinite chain of
applications — which is similar to:
Sidenote: see this SO question and my answer, which came from the PLQ implementation.
fact-step
is a function that given any limited factorial, will
generate a factorial that is good for one more integer input. Start
with 777
, which is a factorial that is good for nothing (because it’s
not a function), and you can get fact0
as
and that’s a good factorial function only for an input of 0
. Use that
with fact-step
again, and you get
which is the factorial function when you only look at input values of
0
or 1
. In a similar way
is good for 0
…2
— and we can continue as much as we want, except
that we need to have an infinite number of applications — in the
general case, we have:
which is good for 0
…n
. The real factorial would be the result
of running fact-step
on itself infinitely, it is fact-infinity
.
In other words (here fact
is the real factorial):
but note that since this is really infinity, then
so we get an equation:
and a solution for this is going to be the real factorial. The solution
is the fixed-point of the fact-step
function, in the same sense that
0
is the fixed point of the sin
function because
And the Y combinator does just that — it has this property:
or, using the more common name:
This property encapsulates the real magical power of Y. You can see how
it works: since (Y f) = (f (Y f))
, we can add an f
application to
both sides, giving us (f (Y f)) = (f (f (Y f)))
, so we get:
and we can conclude that
Here’s another explanation of how the Y combinator works. Remember that
our fact-step
function was actually a function that generates a
factorial function based on some input, which is supposed to be the
factorial function:
As we’ve seen, you can apply this function on a version of factorial
that is good for inputs up to some n, and the result will be a factorial
that is good for those values up to n+1. The question is what is the
fixpoint of fact-step
? And the answer is that if it maps factₙ
factorial to factₙ₊₁, then the input will be equal to the output on the
infinitieth fact
, which is the actual factorial. Since Y is a
fixpoint combinator, it gives us exactly that answer:
Typing the Y combinator is a tricky issue. For example, in standard ML you must write a new type definition to do this:
Can you find a pattern in the places where
T
is used? — Roughly speaking, that type definition is;; `t' is the type name, `T' is the constructor (aka the variant)
(define-type (RecTypeOf t)
[T ((RecTypeOf t) -> t)])First note that the two
fn a => ...
parts are the same as our protection, so ignoring that we get:val y = fn f => (fn (T x) => (f (x (T x))))
(T (fn (T x) => (f (x (T x)))))if you now replace
T
withQuote
, things make more sense:val y = fn f => (fn (Quote x) => (f (x (Quote x))))
(Quote (fn (Quote x) => (f (x (Quote x)))))and with our syntax, this would be:
(define (Y f)
((lambda (qx)
(cases qx
[(Quote x) (f (x qx))]))
(Quote
(lambda (qx)
(cases qx
[(Quote x) (f (x qx))])))))it’s not really quotation — but the analogy should help: it uses
Quote
to distinguish functions as values that are applied (thex
s) from functions that are passed as arguments.
In OCaml, this looks a little different:
but OCaml has also a -rectypes
command line argument, which will make
it infer the type by itself:
The translation of this to #lang pl
is a little verbose because we
don’t have auto-currying, and because we need to declare input types to
functions, but it’s essentially a direct translation of the above:
It is also possible to write this expression in “plain” Typed Racket,
without a user-defined type — and we need to start with a proper type
definition. First of all, the type of Y should be straightforward: it
is a fixpoint operation, so it takes a T -> T
function and produces
its fixpoint. The fixpoint itself is some T
(such that applying the
function on it results in itself). So this gives us:
However, in our case make-recursive
computes a functional fixpoint,
for unary S -> T
functions, so we should narrow down the type
Now, in the body of make-recursive
we need to add a type for the x
argument which is behaving in a weird way: it is used both as a function
and as its own argument. (Remember — I will say the next sentence
twice: “I will say the next sentence twice”.) We need a recursive type
definition helper (not a new type) for that:
This type is tailored for our use of x
: it is a type for a function
that will consume itself (hence the Rec
) and spit out the value that
the f
argument consumes — an S -> T
function.
The resulting full version of the code:
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.