Recursion without the Magic
PLAI §22.4 (we go much deeper)
Note: This explanation is similar to the one you can find in “The Why of Y”, by Richard Gabriel.
To implement recursion without the define
magic, we first make an
observation: this problem does not come up in a dynamicallyscoped
language. Consider the let
version of the problem:
(let ([fact (lambda (n)
(if (zero? n) 1 (* n (fact ( n 1)))))])
(fact 5))
This works fine — because by the time we get to evaluate the body of
the function, fact
is already bound to itself in the current dynamic
scope. (This is another reason why dynamic scope is perceived as a
convenient approach in new languages.)
Regardless, the problem that we have with lexical scope is still there,
but the way things work in a dynamic scope suggest a solution that we
can use now. Just like in the dynamic scope case, when fact
is called,
it does have a value — the only problem is that this value is
inaccessible in the lexical scope of its body.
Instead of trying to get the value in via lexical scope, we can imitate
what happens in the dynamically scoped language by passing the fact
value to itself so it can call itself (going back to the original code
in the brokenscope language):
(if (zero? n) 1 (* n (self ( n 1)))))
(fact fact 5) ;***
except that now the recursive call should still send itself along:
(if (zero? n) 1 (* n (self self ( n 1))))) ;***
(fact fact 5)
The problem is that this required rewriting calls to fact
— both
outside and recursive calls inside. To make this an acceptable solution,
calls from both places should not change. Eventually, we should be able
to get a working fact
definition that uses just
The first step in resolving this problem is to curry the fact
definition.
(lambda (n) ;***
(if (zero? n)
1
(* n ((self self) ( n 1)))))) ;***
((fact fact) 5) ;***
Now fact
is no longer our factorial function — it’s a function that
constructs it. So call it makefact
, and bind fact
to the actual
factorial function.
(lambda (n)
(if (zero? n) 1 (* n ((self self) ( n 1))))))
(define fact (makefact makefact)) ;***
(fact 5) ;***
We can try to do the same thing in the body of the factorial function:
instead of calling (self self)
, just bind fact
to it:
(lambda (n)
(let ([fact (self self)]) ;***
(if (zero? n)
1
(* n (fact ( n 1))))))) ;***
(define fact (makefact makefact))
(fact 5)
This works fine, but if we consider our original goal, we need to get
that local fact
binding outside of the (lambda (n) ...)
— so we’re
left with a definition that uses the factorial expression as is. So,
swap the two lines:
(let ([fact (self self)]) ;***
(lambda (n) ;***
(if (zero? n) 1 (* n (fact ( n 1)))))))
(define fact (makefact makefact))
(fact 5)
But the problem is that this gets us into an infinite loop because we’re
trying to evaluate (self self)
too ea(ge)rly. In fact, if we ignore
the body of the let
and other details, we basically do this:
reducesugar>
(define makefact (lambda (self) (self self))) (makefact makefact)
replacedefinition>
((lambda (self) (self self)) (lambda (self) (self self)))
renameidentifiers>
((lambda (x) (x x)) (lambda (x) (x x)))
And this expression has an interesting property: it reduces to itself, so evaluating it gets stuck in an infinite loop.
So how do we solve this? Well, we know that (self self)
should be
the same value that is the factorial function itself — so it must be a
oneargument function. If it’s such a function, we can use a value that
is equivalent, except that it will not get evaluated until it is needed,
when the function is called. The trick here is the observation that
(lambda (n) (add1 n))
is really the same as add1
(provided that
add1
is a oneargument function), except that the add1
part doesn’t
get evaluated until the function is called. Applying this trick to our
code produces a version that does not get stuck in the same infinite
loop:
(let ([fact (lambda (n) ((self self) n))]) ;***
(lambda (n) (if (zero? n) 1 (* n (fact ( n 1)))))))
(define fact (makefact makefact))
(fact 5)
Continuing from here — we know that
(remember how we derived fun
from a with
), so we can turn that let
into the equivalent function application form:
((lambda (fact) ;***
(lambda (n) (if (zero? n) 1 (* n (fact ( n 1))))))
(lambda (n) ((self self) n)))) ;***
(define fact (makefact makefact))
(fact 5)
And note now that the (lambda (fact) …) expression is everything that
we need for a recursive definition of fact
— it has the proper
factorial body with a plain recursive call. It’s almost like the usual
value that we’d want to define fact
as, except that we still have to
abstract on the recursive value itself. So lets move this code into a
separate definition for factstep
:
(lambda (fact)
(lambda (n) (if (zero? n) 1 (* n (fact ( n 1)))))))
(define (makefact self)
(factstep ;***
(lambda (n) ((self self) n))))
(define fact (makefact makefact))
(fact 5)
We can now proceed by moving the (makefact makefact)
self
application into its own function which is what creates the real
factorial:
(lambda (fact)
(lambda (n) (if (zero? n) 1 (* n (fact ( n 1)))))))
(define (makefact self)
(factstep
(lambda (n) ((self self) n))))
(define (makerealfact) (makefact makefact)) ;***
(define fact (makerealfact)) ;***
(fact 5)
Rewrite the makefact
definition using an explicit lambda
:
(lambda (fact)
(lambda (n) (if (zero? n) 1 (* n (fact ( n 1)))))))
(define makefact ;***
(lambda (self) ;***
(factstep
(lambda (n) ((self self) n)))))
(define (makerealfact) (makefact makefact))
(define fact (makerealfact))
(fact 5)
and fold the functionality of makefact
and makerealfact
into a
single makefact
function by just using the value of makefact
explicitly instead of through a definition:
(lambda (fact)
(lambda (n) (if (zero? n) 1 (* n (fact ( n 1)))))))
(define (makerealfact)
(let ([make (lambda (self) ;***
(factstep ;***
(lambda (n) ((self self) n))))]) ;***
(make make)))
(define fact (makerealfact))
(fact 5)
We can now observe that makerealfact
has nothing that is specific to
factorial — we can make it take a “core function” as an argument:
(lambda (fact)
(lambda (n) (if (zero? n) 1 (* n (fact ( n 1)))))))
(define (makerealfact core) ;***
(let ([make (lambda (self)
(core ;***
(lambda (n) ((self self) n))))])
(make make)))
(define fact (makerealfact factstep)) ;***
(fact 5)
and call it makerecursive
:
(lambda (fact)
(lambda (n) (if (zero? n) 1 (* n (fact ( n 1)))))))
(define (makerecursive core) ;***
(let ([make (lambda (self)
(core
(lambda (n) ((self self) n))))])
(make make)))
(define fact (makerecursive factstep)) ;***
(fact 5)
We’re almost done now — there’s no real need for a separate
factstep
definition, just use the value for the definition of fact
:
(let ([make (lambda (self)
(core
(lambda (n) ((self self) n))))])
(make make)))
(define fact
(makerecursive
(lambda (fact) ;***
(lambda (n) (if (zero? n) 1 (* n (fact ( n 1)))))))) ;***
(fact 5)
turn the let
into a function form:
((lambda (make) (make make)) ;***
(lambda (self) ;***
(core (lambda (n) ((self self) n)))))) ;***
(define fact
(makerecursive
(lambda (fact)
(lambda (n) (if (zero? n) 1 (* n (fact ( n 1))))))))
(fact 5)
do some renamings to make things simpler — make
and self
turn to
x
, and core
to f
:
((lambda (x) (x x)) ;***
(lambda (x) (f (lambda (n) ((x x) n)))))) ;***
(define fact
(makerecursive
(lambda (fact)
(lambda (n) (if (zero? n) 1 (* n (fact ( n 1))))))))
(fact 5)
or we can manually expand that first (lambda (x) (x x)) application to
make the symmetry more obvious (not really surprising because it started
with a let
whose purpose was to do a selfapplication):
((lambda (x) (f (lambda (n) ((x x) n)))) ;***
(lambda (x) (f (lambda (n) ((x x) n)))))) ;***
(define fact
(makerecursive
(lambda (fact)
(lambda (n) (if (zero? n) 1 (* n (fact ( n 1))))))))
(fact 5)
And we finally got what we were looking for: a general way to define
any recursive function without any magical define
tricks. This also
work for other recursive functions:
(define (makerecursive f)
((lambda (x) (f (lambda (n) ((x x) n))))
(lambda (x) (f (lambda (n) ((x x) n))))))
(define fact
(makerecursive
(lambda (fact)
(lambda (n) (if (zero? n) 1 (* n (fact ( n 1))))))))
(fact 5)
(define fib
(makerecursive
(lambda (fib)
(lambda (n) (if (<= n 1) n (+ (fib ( n 1)) (fib ( n 2))))))))
(fib 8)
(define length
(makerecursive
(lambda (length)
(lambda (l) (if (null? l) 0 (+ (length (rest l)) 1))))))
(length '(x y z))
A convenient tool that people often use on paper is to perform a kind of
a syntactic abstraction: “assume that whenever I write (twice foo) I
really meant to write (foo foo)”. This can often be done as plain
abstractions (that is, using functions), but in some cases — for
example, if we want to abstract over definitions — we just want such a
rewrite rule. (More on this towards the end of the course.) The
brokenscope language does provide such a tool — rewrite
extends the
language with a rewrite rule. Using this, and our makerecursive
, we
can make up a recursive definition form:
=> (define f (makerecursive (lambda (f) (lambda (x) E)))))
In other words, we’ve created our own “magical definition” form. The above code can now be written in almost the same way it is written in plain Racket:
(define (makerecursive f)
((lambda (x) (f (lambda (n) ((x x) n))))
(lambda (x) (f (lambda (n) ((x x) n))))))
(rewrite (define/rec (f x) E)
=> (define f (makerecursive (lambda (f) (lambda (x) E)))))
;; examples
(define/rec (fact n) (if (zero? n) 1 (* n (fact ( n 1)))))
(fact 5)
(define/rec (fib n) (if (<= n 1) n (+ (fib ( n 1)) (fib ( n 2)))))
(fib 8)
(define/rec (length l) (if (null? l) 0 (+ (length (rest l)) 1)))
(length '(x y z))
Finally, note that makerecursive is limited to 1argument functions only because of the protection from eager evaluation. In any case, it can be used in any way you want, for example,
is a function that returns itself rather than calling itself. Using the rewrite rule, this would be:
which is the same as:
in plain Racket.
The Core of makerecursive
As in Racket, being able to express recursive functions is a fundamental property of the language. It means that we can have loops in our language, and that’s the essence of making a language powerful enough to be TMequivalent — able to express undecidable problems, where we don’t know whether there is an answer or not.
The core of what makes this possible is the expression that we have seen in our derivation:
which reduces to itself, and therefore has no value: trying to evaluate it gets stuck in an infinite loop. (This expression is often called “Omega”.)
This is the key for creating a loop — we use it to make recursion
possible. Looking at our final makerecursive
definition and ignoring
for a moment the “protection” that we need against being stuck
prematurely in an infinite loop:
((lambda (x) (x x)) (lambda (x) (f (x x)))))
we can see that this is almost the same as the Omega expression — the
only difference is that application of f
. Indeed, this expression (the
result of (makerecursive F) for some F
) reduces in a similar way to
Omega:
((lambda (x) (F (x x))) (lambda (x) (F (x x))))
(F ((lambda (x) (F (x x))) (lambda (x) (F (x x)))))
(F (F ((lambda (x) (F (x x))) (lambda (x) (F (x x))))))
(F (F (F ((lambda (x) (F (x x))) (lambda (x) (F (x x)))))))
...
which means that the actual value of this expression is:
This definition would be sufficient if we had a lazy language, but to
get things working in a strict one we need to bring back the protection.
This makes things a little different — if we use (protect f)
to be a
shorthand for the protection trick,
then we have:
((lambda (x) (x x)) (lambda (x) (f (protect (x x))))))
which makes the (makerecursive F) evaluation reduce to
and this is still the same result (as long as F
is a singleargument
function).
(Note that protect
cannot be implemented as a plain function!)
Denotational Explanation of Recursion
Note: This explanation is similar to the one you can find in “The Little Schemer” called “(Y Y) Works!”, by Dan Friedman and Matthias Felleisen.
The explanation that we have now for how to derive the makerecursive
definition is fine — after all, we did manage to get it working. But
this explanation was done from a kind of an operational point of view:
we knew a certain trick that can make things work and we pushed things
around until we got it working like we wanted. Instead of doing this, we
can reapproach the problem from a more declarative point of view.
So, start again from the same broken code that we had (using the brokenscope language):
(lambda (n) (if (zero? n) 1 (* n (fact ( n 1))))))
This is as broken as it was when we started: the occurrence of fact
in
the body of the function is free, which means that this code is
meaningless. To avoid the compilation error that we get when we run this
code, we can substitute anything for that fact
— it’s even better
to use a replacement that will lead to a runtime error:
(lambda (n) (if (zero? n) 1 (* n (777 ( n 1)))))) ;***
This function will not work in a similar way to the original one — but
there is one case where it does work: when the input value is 0
(since then we do not reach the bogus application). We note this by
calling this function fact0
:
(lambda (n) (if (zero? n) 1 (* n (777 ( n 1))))))
Now that we have this function defined, we can use it to write fact1
which is the factorial function for arguments of 0
or 1
:
(lambda (n) (if (zero? n) 1 (* n (777 ( n 1))))))
(define fact1
(lambda (n) (if (zero? n) 1 (* n (fact0 ( n 1))))))
And remember that this is actually just shorthand for:
(lambda (n)
(if (zero? n)
1
(* n ((lambda (n)
(if (zero? n)
1
(* n (777 ( n 1)))))
( n 1))))))
We can continue in this way and write fact2
that will work for n<=2:
(lambda (n) (if (zero? n) 1 (* n (fact1 ( n 1))))))
or, in full form:
(lambda (n)
(if (zero? n)
1
(* n ((lambda (n)
(if (zero? n)
1
(* n ((lambda (n)
(if (zero? n)
1
(* n (777 ( n 1)))))
( n 1)))))
( n 1))))))
If we continue this way, we will get the true factorial function, but the problem is that to handle any possible integer argument, it will have to be an infinite definition! Here is what it is supposed to look like:
(define fact1 (lambda (n) (if (zero? n) 1 (* n (fact0 ( n 1))))))
(define fact2 (lambda (n) (if (zero? n) 1 (* n (fact1 ( n 1))))))
(define fact3 (lambda (n) (if (zero? n) 1 (* n (fact2 ( n 1))))))
...
The true factorial function is factinfinity
, with an infinite size.
So, we’re back at the original problem…
To help make things more concise, we can observe the repeated pattern in
the above, and extract a function that abstracts this pattern. This
function is the same as the factstep
that we have seen previously:
(lambda (fact)
(lambda (n) (if (zero? n) 1 (* n (fact ( n 1)))))))
(define fact0 (factstep 777))
(define fact1 (factstep fact0))
(define fact2 (factstep fact1))
(define fact3 (factstep fact2))
...
which is actually:
(lambda (fact)
(lambda (n) (if (zero? n) 1 (* n (fact ( n 1)))))))
(define fact0 (factstep 777))
(define fact1 (factstep (factstep 777)))
(define fact2 (factstep (factstep (factstep 777))))
...
(define fact
(factstep (factstep (factstep (... (factstep 777) ...)))))
Do this a little differently — rewrite fact0
as:
((lambda (mk) (mk 777))
factstep))
Similarly, fact1
is written as:
((lambda (mk) (mk (mk 777)))
factstep))
and so on, until the real factorial, which is still infinite at this stage:
((lambda (mk) (mk (mk (... (mk 777) ...))))
factstep))
Now, look at that (lambda (mk) ...)
— it is an infinite expression,
but for every actual application of the resulting factorial function we
only need a finite number of mk
applications. We can guess how many,
and as soon as we hit an application of 777
we know that our guess is
too small. So instead of 777
, we can try to use the maker function to
create and use the next.
To make things more explicit, here is the expression that is our
fact0
, without the definition form:
factstep)
This function has a very low guess — it works for 0, but with 1 it
will run into the 777
application. At this point, we want to somehow
invoke mk
again to get the next level — and since 777
does get
applied, we can just replace it with mk
:
factstep)
The resulting function works just the same for an input of 0
because
it does not attempt a recursive call — but if we give it 1
, then
instead of running into the error of applying 777
:
we get to apply factstep
there:
and this is still wrong, because factstep
expects a function as an
input. To see what happens more clearly, write factstep
explicitly:
(lambda (fact)
(lambda (n) (if (zero? n) 1 (* n (fact ( n 1)))))))
The problem is in what we’re going to pass into factstep
— its
fact
argument will not be the factorial function, but the mk
function constructor. Renaming the fact
argument as mk
will make
this more obvious (but not change the meaning):
(lambda (mk)
(lambda (n) (if (zero? n) 1 (* n (mk ( n 1)))))))
It should now be obvious that this application of mk
will not work,
instead, we need to apply it on some function and then apply the
result on ( n 1)
. To get what we had before, we can use 777
as a
bogus function:
(lambda (mk)
(lambda (n) (if (zero? n) 1 (* n ((mk 777) ( n 1)))))))
This will allow one recursive call — so the definition works for both
inputs of 0
and 1
— but not more. But that 777
is used as a
maker function now, so instead, we can just use mk
itself again:
(lambda (mk)
(lambda (n) (if (zero? n) 1 (* n ((mk mk) ( n 1)))))))
And this is a working version of the real factorial function, so make it into a (nonmagical) definition:
((lambda (mk) (mk mk))
(lambda (mk)
(lambda (n) (if (zero? n) 1 (* n ((mk mk) ( n 1))))))))
But we’re not done — we “broke” into the factorial code to insert that
(mk mk)
application — that’s why we dragged in the actual value of
factstep
. We now need to fix this. The expression on that last line
is close enough — it is (factstep (mk mk))
. So we can now try to
rewrite our fact
as:
(lambda (fact)
(lambda (n) (if (zero? n) 1 (* n (fact ( n 1)))))))
(define fact
((lambda (mk) (mk mk))
(lambda (mk) (factstep (mk mk)))))
… and would fail in a familiar way! If it’s not familiar enough, just
rename all those mk
s as x
s:
(lambda (fact)
(lambda (n) (if (zero? n) 1 (* n (fact ( n 1)))))))
(define fact
((lambda (x) (x x))
(lambda (x) (factstep (x x)))))
We’ve run into the eagerness of our language again, as we did before.
The solution is the same — the (x x)
is the factorial function, so
protect it as we did before, and we have a working version:
(lambda (fact)
(lambda (n) (if (zero? n) 1 (* n (fact ( n 1)))))))
(define fact
((lambda (x) (x x))
(lambda (x) (factstep (lambda (n) ((x x) n))))))
The rest should not be surprising now… Abstract the recursive making
bit in a new makerecursive
function:
(lambda (fact)
(lambda (n) (if (zero? n) 1 (* n (fact ( n 1)))))))
(define (makerecursive f)
((lambda (x) (x x))
(lambda (x) (f (lambda (n) ((x x) n))))))
(define fact (makerecursive factstep))
and now we can do the first reduction inside makerecursive
and write
the factstep
expression explicitly:
(define (makerecursive f)
((lambda (x) (f (lambda (n) ((x x) n))))
(lambda (x) (f (lambda (n) ((x x) n))))))
(define fact
(makerecursive
(lambda (fact)
(lambda (n) (if (zero? n) 1 (* n (fact ( n 1))))))))
and this is the same code we had before.
The Y Combinator
Our makerecursive
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):
(lambda (f)
((lambda (x) (f (x x)))
(lambda (x) (f (x x))))))
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:
"I will say the next sentence twice".
(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:
"I will hop on one foot and then say the next sentence twice".
Sidenote: see this SO question and my answer, which came from the PLQ implementation.
The main property of Y
factstep
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 factstep
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 factstep
on itself infinitely, it is factinfinity
. In
other words (here fact
is the real factorial):
but note that since this is really infinity, then
= (factstep fact)
so we get an equation:
and a solution for this is going to be the real factorial. The solution
is the fixedpoint of the factstep
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:
= (f (f (f ...)))
and we can conclude that
= fact
Yet another explanation for Y
Here’s another explanation of how the Y combinator works. Remember that
our factstep
function was actually a function that generates a
factorial function based on some input, which is supposed to be the
factorial function:
(lambda (fact)
(lambda (n) (if (zero? n) 1 (* n (fact ( n 1)))))))
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 factstep
? 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
Typing the Y combinator is a tricky issue. For example, in standard ML you must write a new type definition to do this:
val y = fn f => (fn (T x) => (f (fn a => x (T x) a)))
(T (fn (T x) => (f (fn a => x (T x) a))))
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)
(definetype (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:
type 'a t = T of ('a t > 'a)
# let y f = (fun (T x) > x (T x))
(T (fun (T x) > fun z > f (x (T x)) z)) ;;
val y : (('a > 'b) > 'a > 'b) > 'a > 'b = <fun>
# let fact = y (fun fact n > if n < 1 then 1 else n * fact(n1)) ;;
val fact : int > int = <fun>
# fact 5 ;;
 : int = 120
but OCaml has also a rectypes
command line argument, which will make
it infer the type by itself:
val y : (('a > 'b) > 'a > 'b) > 'a > 'b = <fun>
# let fact = y (fun fact n > if n < 1 then 1 else n * fact(n1)) ;;
val fact : int > int = <fun>
# fact 5 ;;
 : int = 120
The translation of this to #lang pl
is a little verbose because we
don’t have autocurrying, and because we need to declare input types to
functions, but it’s essentially a direct translation of the above:
[T ((RecTypeOf t) > t)])
(: Y : (All (A B) ((A > B) > (A > B)) > (A > B)))
(define (Y f)
((lambda ([x : (RecTypeOf (A > B))])
(cases x
[(T x) (x (T x))]))
(T (lambda ([x : (RecTypeOf (A > B))])
(cases x
[(T x) (lambda ([z : A])
((f (x (T x))) z))])))))
(define fact
(Y (lambda ([fact : (Integer > Integer)])
(lambda ([n : Integer])
(if (< n 1) 1 (* n (fact (sub1 n))))))))
(fact 5)
It is also possible to write this expression in “plain” Typed Racket,
without a userdefined 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 makerecursive
computes a functional fixpoint,
for unary S > T
functions, so we should narrow down the type
Now, in the body of makerecursive
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:
(definetype (Tau S T) = (Rec this (this > (S > T))))
(define (makerecursive f)
((lambda ([x : (Tau S T)]) (f (lambda (z) ((x x) z))))
(lambda ([x : (Tau S T)]) (f (lambda (z) ((x x) z))))))
(: fact : Number > Number)
(define fact (makerecursive
(lambda ([fact : (Number > Number)])
(lambda ([n : Number])
(if (zero? n)
1
(* n (fact ( n 1))))))))
(fact 5)
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:
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
Racketlike 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:
<SCHLACTOP> ::= <SCHLACEXPR>
 (define <id> <SCHLACEXPR>)
<SCHLACEXPR> ::= <id>
 (lambda (<id> <id> ...) <SCHLACEXPR>)
 (<SCHLACEXPR> <SCHLACEXPR> <SCHLACEXPR> ...)
Since this language has no primitive values (other than functions), Racket numbers and booleans are also considered identifiers, and have no builtin 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 deBruijn 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 builtin
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:
#<procedure:identity>
> (identity identity)
#<procedure:identity>
> (identity identity identity)
#<procedure: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.)
Something to think about: are we losing anything because we have no noargument functions?
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 / Assembly 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:
(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 builtin functions that
translate Church numerals into Racket numbers. To try our definitions we
use the >nat
(read: to natural number):
(>nat 5)
(>nat (add1 (add1 5)))
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
backdoor
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
:
(>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 incrementbyn 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:
(>nat (* 4 5))
(>nat (+ 4 (* (+ 2 5) 5)))
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 selfcompositions of
f
, selfcomposed 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):
(>nat (^ 3 4))
And there is a similar alternative here too —

a Church numeral
m
is the mselfcomposition function, 
and
(1 m)
is just likem
^1
which is the same asm
(1
=identity
) 
and
(2 m)
is just likem
^2
— it takes a functionf
, self composes itm
times, and self composes the resultm
times — for a total off
^(m*m)

and
(3 m)
is similarlyf
^(m*m*m)

so
(n m)
isf
^(m^n)
(note that the first^
is selfcompositions, and the second one is a mathematical exponent) 
so
(n m)
is a function that returnsm
^n
selfcompositions of an input function, Which means that(n m)
is the Church numeral form
^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.