# The Core of `make-recursive`

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 TM-equivalent — 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:

((lambda (x) (x x)) (lambda (x) (x x)))

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 `make-recursive` definition and ignoring for a moment the “protection” that we need against being stuck prematurely in an infinite loop:

(define (make-recursive f)
((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 (make-recursive F) for some `F`) reduces in a similar way to Omega:

((lambda (x) (x x)) (lambda (x) (F (x x))))
((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:

(F (F (F ...forever...)))

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,

(rewrite (protect f) => (lambda (x) (f x)))

then we have:

(define (make-recursive f)
((lambda (x) (x x)) (lambda (x) (f (protect (x x))))))

which makes the (make-recursive F) evaluation reduce to

(F (protect (F (protect (F (protect (...forever...)))))))

and this is still the same result (as long as `F` is a single-argument 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 `make-recursive` 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 re-approach the problem from a more declarative point of view.

So, start again from the same broken code that we had (using the broken-scope language):

(define fact
(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:

(define fact
(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`:

(define 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`:

(define fact0
(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:

(define fact1
(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:

(define fact2
(lambda (n) (if (zero? n) 1 (* n (fact1 (- n 1))))))

or, in full form:

(define fact2
(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 fact0 (lambda (n) (if (zero? n) 1 (* n (777 (- n 1))))))
(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 `fact-infinity`, 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 `fact-step` that we have seen previously:

(define fact-step
(lambda (fact)
(lambda (n) (if (zero? n) 1 (* n (fact (- n 1)))))))
(define fact0 (fact-step 777))
(define fact1 (fact-step fact0))
(define fact2 (fact-step fact1))
(define fact3 (fact-step fact2))
...

which is actually:

(define fact-step
(lambda (fact)
(lambda (n) (if (zero? n) 1 (* n (fact (- n 1)))))))
(define fact0 (fact-step 777))
(define fact1 (fact-step (fact-step 777)))
(define fact2 (fact-step (fact-step (fact-step 777))))
...
(define fact
(fact-step (fact-step (fact-step (... (fact-step 777) ...)))))

Do this a little differently — rewrite `fact0` as:

(define fact0
((lambda (mk) (mk 777))
fact-step))

Similarly, `fact1` is written as:

(define fact1
((lambda (mk) (mk (mk 777)))
fact-step))

and so on, until the real factorial, which is still infinite at this stage:

(define fact
((lambda (mk) (mk (mk (... (mk 777) ...))))
fact-step))

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:

((lambda (mk) (mk 777))
fact-step)

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`:

((lambda (mk) (mk mk))
fact-step)

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`:

(* n (777 (- n 1)))

we get to apply `fact-step` there:

(* n (fact-step (- n 1)))

and this is still wrong, because `fact-step` expects a function as an input. To see what happens more clearly, write `fact-step` explicitly:

((lambda (mk) (mk mk))
(lambda (fact)
(lambda (n) (if (zero? n) 1 (* n (fact (- n 1)))))))

The problem is in what we’re going to pass into `fact-step` — 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) (mk mk))
(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) (mk mk))
(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) (mk mk))
(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 (non-magical) definition:

(define fact
((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 `fact-step`. We now need to fix this. The expression on that last line

(lambda (n) (if (zero? n) 1 (* n ((mk mk) (- n 1)))))

is close enough — it is `(fact-step (mk mk))`. So we can now try to rewrite our `fact` as:

(define fact-step
(lambda (fact)
(lambda (n) (if (zero? n) 1 (* n (fact (- n 1)))))))
(define fact
((lambda (mk) (mk mk))
(lambda (mk) (fact-step (mk mk)))))

… and would fail in a familiar way! If it’s not familiar enough, just rename all those `mk`s as `x`s:

(define fact-step
(lambda (fact)
(lambda (n) (if (zero? n) 1 (* n (fact (- n 1)))))))
(define fact
((lambda (x) (x x))
(lambda (x) (fact-step (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:

(define fact-step
(lambda (fact)
(lambda (n) (if (zero? n) 1 (* n (fact (- n 1)))))))
(define fact
((lambda (x) (x x))
(lambda (x) (fact-step (lambda (n) ((x x) n))))))

The rest should not be surprising now… Abstract the recursive making bit in a new `make-recursive` function:

(define fact-step
(lambda (fact)
(lambda (n) (if (zero? n) 1 (* n (fact (- n 1)))))))
(define (make-recursive f)
((lambda (x) (x x))
(lambda (x) (f (lambda (n) ((x x) n))))))
(define fact (make-recursive fact-step))

and now we can do the first reduction inside `make-recursive` and write the `fact-step` expression explicitly:

#lang pl broken
(define (make-recursive f)
((lambda (x) (f (lambda (n) ((x x) n))))
(lambda (x) (f (lambda (n) ((x x) n))))))
(define fact
(make-recursive
(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 `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):

(define Y
(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:

((lambda (x) (x x)) (lambda (x) (x x)))

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:
"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 say the next sentence twice:
"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

`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

fact0 == (fact-step 777)

and that’s a good factorial function only for an input of `0`. Use that with `fact-step` again, and you get

fact1 == (fact-step fact0) == (fact-step (fact-step 777))

which is the factorial function when you only look at input values of `0` or `1`. In a similar way

fact2 == (fact-step fact1)

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:

fact-n == (fact-step (fact-step (fact-step ... 777)))

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):

fact = fact-infinity == (fact-step (fact-step ...infinitely...))

but note that since this is really infinity, then

fact = (fact-step (fact-step ...infinitely...))
= (fact-step fact)

so we get an equation:

fact = (fact-step fact)

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

0 = (sin 0)

And the Y combinator does just that — it has this property:

(make-recursive f) = (f (make-recursive f))

or, using the more common name:

(Y f) = (f (Y f))

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:

(Y f) = (f (Y f)) = (f (f (Y f))) = (f (f (f (Y f)))) = ...
= (f (f (f ...)))

and we can conclude that

(Y fact-step) = (fact-step (fact-step ...infinitely...))
= fact

# Yet another explanation for Y

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:

(define fact-step
(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 `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:

(define the-real-factorial (Y fact-step))

# 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:

datatype 'a t = T of 'a t -> 'a
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)
(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` with `Quote`, 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 (the `x`s) from functions that are passed as arguments.

In OCaml, this looks a little different:

# type 'a t = T of ('a t -> 'a) ;;
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(n-1)) ;;
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:

# let y f = (fun x -> x x) (fun x -> fun z -> f (x 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(n-1)) ;;
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 auto-currying, and because we need to declare input types to functions, but it’s essentially a direct translation of the above:

(define-type (RecTypeOf t)
[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 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:

(: make-recursive : (T -> T) -> T)

However, in our case `make-recursive` computes a functional fixpoint, for unary `S -> T` functions, so we should narrow down the type

(: make-recursive : ((S -> T) -> (S -> T)) -> (S -> T))

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:

(define-type (Tau S T) = (Rec this (this -> (S -> T))))

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:

(: make-recursive : (All (S T) ((S -> T) -> (S -> T)) -> (S -> T)))
(define-type (Tau S T) = (Rec this (this -> (S -> T))))
(define (make-recursive 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 (make-recursive
(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:

(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> ...

<SCHLAC-TOP>  ::= <SCHLAC-EXPR>
| (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
#<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.)

# 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:

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:

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)

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

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:

(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 —

• 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.

# More Encodings

Our choice of encoding numbers makes sense — the idea is that the main feature of a natural number is repeating something a number of times. For booleans, the main property we’re looking for is choosing between two values. So we can encode true and false by functions of two arguments that return either the first or the second argument:

(define #t (lambda (x y) x))
(define #f (lambda (x y) y))

Note that this encoding of `#f` is really the same as the encoding of `0`, so we have to know what type to expect an use the proper operations (this is similar to C, where everything is just integers). Now that we have these two, we can define `if`:

(define if (lambda (c t e) (c t e)))

it expects a boolean which is a function of two arguments, and passes it the two expressions. The `#t` boolean will simply return the first, and the `#f` boolean will return the second. Strictly speaking, we don’t really need this definition, since instead of writing `(if c t e)`, we can simply write `(c t e)`. In any case, we need the language to be lazy for this to work. To demonstrate this, we’ll intentionally use the quote back-door to use a non-functional value, using this will normally result in an error:

(+ '1 '2)

But testing our `if` definition, things work just fine:

(if #t (+ 4 5) (+ 1 2))

and we see that DrRacket leaves the second addition expression in red, which indicates that it was not executed. We can also make sure that even when it is defined as a function, it is still working fine because the language is lazy:

(if #f ((lambda (x) (x x)) (lambda (x) (x x))) 3)

What about `and` and `or`? Simple, `or` takes two arguments, and returns either true or false if one of the inputs is true:

(define or (lambda (a b) (if a #t (if b #t #f))))

but `(if b #t #f)` is really the same as just `b` because it must be a boolean (we cannot use more than one “truty” or “falsy” values):

(define or (lambda (a b) (if a #t b)))

also, if `a` is true, we want to return `#t`, but that is exactly the value of `a`, so:

(define or (lambda (a b) (if a a b)))

and finally, we can get rid of the `if` (which is actually breaking the `if` abstraction, if we encode booleans in some other way):

(define or (lambda (a b) (a a b)))

Similarly, you can convince yourself that the definition of `and` is:

(define and (lambda (a b) (a b a)))

Schlac has to-Racket conversion functions for booleans too:

(->bool (or #f #f))
(->bool (or #f #t))
(->bool (or #t #f))
(->bool (or #t #t))

and

(->bool (and #f #f))
(->bool (and #f #t))
(->bool (and #t #f))
(->bool (and #t #t))

A `not` function is quite simple — one alternative is to choose from true and false in the usual way:

(define not (lambda (a) (a #f #t)))

and another is to return a function that switches the inputs to an input boolean:

(define not (lambda (a) (lambda (x y) (a y x))))

which is the same as

(define not (lambda (a x y) (a y x)))

We can now put numbers and booleans together: we define a `zero?` function.

(define zero? (lambda (n) (n (lambda (x) #f) #t)))
(test (->bool (and (zero? 0) (not (zero? 3)))) => '#t)

(Good question: is this fast?)

(Note that it is better to test that the value is explicitly `#t`, if we just use `(test (->bool ...))` then the test will work even if the expression in question evaluated to some bogus value.)

The idea is simple — if `n` is the encoding of zero, it will return it’s second argument which is `#t`:

(zero? 0) --> ((lambda (f n) n) (lambda (x) #f) #t) -> #t

if `n` is an encoding of a bigger number, then it is a self-composition, and the function that we give it is one that always returns `#f`, no matter how many times it is self-composed. Try `2` for example:

(zero? 2) --> ((lambda (f n) (f (f n))) (lambda (x) #f) #t)
--> ((lambda (x) #f) ((lambda (x) #f) #t))
--> #f

Now, how about an encoding for compound values? A minimal approach is what we use in Racket — a way to generate pairs (`cons`), and encode lists as chains of pairs with a special value at the end (`null`). There is a natural encoding for pairs that we have previously seen — a pair is a function that expects a selector, and will apply that on the two values:

(define cons (lambda (x y) (lambda (s) (s x y))))

Or, equivalently:

(define cons (lambda (x y s) (s x y)))

To extract the two values from a pair, we need to pass a selector that consumes two values and returns one of them. In our framework, this is exactly what the two boolean values do, so we get:

(define car (lambda (x) (x #t)))
(define cdr (lambda (x) (x #f)))

(->nat (+ (car (cons 2 3)) (cdr (cons 2 3))))

We can even do this:

(define 1st car)
(define 2nd (lambda (l) (car (cdr l))))
(define 3rd (lambda (l) (car (cdr (cdr l)))))
(define 4th (lambda (l) (car (cdr (cdr (cdr l))))))
(define 5th (lambda (l) (car (cdr (cdr (cdr (cdr l)))))))

or write a `list-ref` function:

(define list-ref (lambda (l n) (car (n cdr l))))

Note that we don’t need a recursive function for this: our encoding of natural numbers makes it easy to “iterate N times”. What we get with this encoding is essentially free natural-number recursion.

We now need a special `null` value to mark list ends. This value should have the same number of arguments as a `cons` value (one: a selector/boolean function), and it should be possible to distinguish it from other values. We choose

(define null (lambda (s) #t))

Testing the list encoding:

(define l123 (cons 1 (cons 2 (cons 3 null))))
(->nat (2nd l123))

And as with natural numbers and booleans, Schlac has built-in facility to convert encoded lists to Racket values, except that this requires specifying the type of values in a list so it’s a higher-order function:

((->listof ->nat) l123)

which (“as usual”) can be written as

(->listof ->nat l123)

We can even do this:

(->listof (->listof ->nat) (cons l123 (cons l123 null)))

Defining `null?` is now relatively easy (and it’s actually already used by the above `->listof` conversion). The following definition

(define null? (lambda (x) (x (lambda (x y) #f))))

works because if `x` is null, then it simply ignores its argument and returns `#t`, and if it’s a pair, then it uses the input selector, which always returns `#f` in its turn. Using some arbitrary `A` and `B`:

(null? (cons A B))
--> ((lambda (x) (x (lambda (x y) #f))) (lambda (s) (s A B)))
--> ((lambda (s) (s A B)) (lambda (x y) #f))
--> ((lambda (x y) #f) A B)
--> #f
(null? null)
--> ((lambda (x) (x (lambda (x y) #f))) (lambda (s) #t))
--> ((lambda (s) #t) (lambda (x y) #f))
--> #t

We can use the Y combinator to create recursive functions — we can even use the rewrite rules facility that Schlac contains (the same one that we have previously seen):

(define Y
(lambda (f)
((lambda (x) (x x)) (lambda (x) (f (x x))))))
(rewrite (define/rec f E) => (define f (Y (lambda (f) E))))

and using it:

(define/rec length
(lambda (l)
(if (null? l)
0
(->nat (length l123))

And to complete this, um, journey — we’re still missing subtraction. There are many ways to solve the problem of subtraction, and for a challenge try to come up with a solution yourself. One of the clearer solutions uses a simple idea — begin with a pair of two zeroes `<0,0>`, and repeat this transformation `n` times: `<a,b>` -> `<b,b+1>`. After `n` steps, we will have `<n-1,n>` — so we get:

(define inccons (lambda (p) (cons (cdr p) (add1 (cdr p)))))
(define sub1 (lambda (n) (car (n inccons (cons 0 0)))))
(->nat (sub1 5))

And from this the road is short to general subtraction, `m`-`n` is simply `n` applications of `sub1` on `m`:

(define - (lambda (m n) (n sub1 m)))
(test (->nat (- 3 2)) => '1)
(test (->nat (- (* 4 (* 5 5)) 5)) => '95)

We now have a normal-looking language, and we’re ready to do anything we want. Here are two popular examples:

(define/rec fact
(lambda (x)
(if (zero? x) 1 (* x (fact (sub1 x))))))
(test (->nat (fact 5)) => '120)

(define/rec fib
(lambda (x)
(if (or (zero? x) (zero? (sub1 x)))
1
(+ (fib (- x 1)) (fib (- x 2))))))
(test (->nat (fib (* 5 2))) => '89)

To get generalized arithmetic capability, Schlac has yet another built-in facility for translating Racket natural numbers into Church numerals:

(->nat (fib (nat-> '10)))

… and to get to that frightening expression in the beginning, all you need to do is replace all definitions in the `fib` definition over and over again until you’re left with nothing but lambda expressions and applications, then reformat the result into some cute shape. For extra fun, you can look for immediate applications of lambda expressions and reduce them manually.

All of this is in the following code:

;; Making Schlac into a practical language (not an interpreter)

#lang pl schlac

(define identity (lambda (x) x))

;; Natural numbers

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

(define add1 (lambda (n) (lambda (f x) (f (n f x)))))
;; same as:
;;  (define add1 (lambda (n) (lambda (f x) (n f (f x)))))

(define + (lambda (m n) (m add1 n)))
(test (->nat (+ 4 5)) => '9)

;; (define * (lambda (m n) (m (+ n) 0)))
(define * (lambda (m n f) (m (n f))))
(test (->nat (* 4 5)) => '20)
(test (->nat (+ 4 (* (+ 2 5) 5))) => '39)

;; (define ^ (lambda (m n) (n (* m) 1)))
(define ^ (lambda (m n) (n m)))
(test (->nat (^ 3 4)) => '81)

;; Booleans

(define #t (lambda (x y) x))
(define #f (lambda (x y) y))

(define if (lambda (c t e) (c t e))) ; not really needed

(test (->nat (if #t 1 2)) => '1)
(test (->nat (if #t (+ 4 5) (+ '1 '2))) => '9)

(define and (lambda (a b) (a b a)))
(define or  (lambda (a b) (a a b)))
;; (define not (lambda (a) (a #f #t)))
(define not (lambda (a x y) (a y x)))

(test (->bool (and #f #f)) => '#f)
(test (->bool (and #t #f)) => '#f)
(test (->bool (and #f #t)) => '#f)
(test (->bool (and #t #t)) => '#t)
(test (->bool (or  #f #f)) => '#f)
(test (->bool (or  #t #f)) => '#t)
(test (->bool (or  #f #t)) => '#t)
(test (->bool (or  #t #t)) => '#t)
(test (->bool (not #f)) => '#t)
(test (->bool (not #t)) => '#f)

(define zero? (lambda (n) (n (lambda (x) #f) #t)))
(test (->bool (and (zero? 0) (not (zero? 3)))) => '#t)

;; Lists

(define cons (lambda (x y s) (s x y)))

(define car (lambda (x) (x #t)))
(define cdr (lambda (x) (x #f)))

(test (->nat (+ (car (cons 2 3)) (cdr (cons 2 3)))) => '5)

(define 1st car)
(define 2nd (lambda (l) (car (cdr l))))
(define 3rd (lambda (l) (car (cdr (cdr l)))))
(define 4th (lambda (l) (car (cdr (cdr (cdr l))))))
(define 5th (lambda (l) (car (cdr (cdr (cdr (cdr l)))))))

(define null (lambda (s) #t))
(define null? (lambda (x) (x (lambda (x y) #f))))

(define l123 (cons 1 (cons 2 (cons 3 null))))
;; Note that `->listof' is a H.O. converter
(test ((->listof ->nat) l123) => '(1 2 3))
(test (->listof ->nat l123) => '(1 2 3))  ; same as the above
(test (->listof (->listof ->nat) (cons l123 (cons l123 null)))
=> '((1 2 3) (1 2 3)))

;; Subtraction is tricky

(define inccons (lambda (p) (cons (cdr p) (add1 (cdr p)))))
(define sub1 (lambda (n) (car (n inccons (cons 0 0)))))

(test (->nat (sub1 5)) => '4)

(define - (lambda (a b) (b sub1 a)))

(test (->nat (- 3 2)) => '1)
(test (->nat (- (* 4 (* 5 5)) 5)) => '95)
(test (->nat (- 2 4)) => '0) ; this is "natural subtraction"

;; Recursive functions

(define Y
(lambda (f)
((lambda (x) (x x)) (lambda (x) (f (x x))))))
(rewrite (define/rec f E) => (define f (Y (lambda (f) E))))

(define/rec length
(lambda (l)
(if (null? l)
0
(test (->nat (length l123)) => '3)

(define/rec fact
(lambda (x)
(if (zero? x) 1 (* x (fact (sub1 x))))))
(test (->nat (fact 5)) => '120)

(define/rec fib
(lambda (x)
(if (or (zero? x) (zero? (sub1 x)))
1
(+ (fib (sub1 x)) (fib (sub1 (sub1 x)))))))
(test (->nat (fib (* 5 2))) => '89)

#|
;; Fully-expanded Fibonacci
(define fib
((lambda (f)
((lambda (x) (x x)) (lambda (x) (f (x x)))))
(lambda (f)
(lambda (x)
((lambda (c t e) (c t e))
((lambda (a b) (a a b))
((lambda (n)
(n (lambda (x) (lambda (x y) y)) (lambda (x y) x)))
x)
((lambda (n)
(n (lambda (x) (lambda (x y) y)) (lambda (x y) x)))
((lambda (n)
((lambda (x) (x (lambda (x y) x)))
(n (lambda (p)
((lambda (x y s) (s x y))
((lambda (x) (x (lambda (x y) y))) p)
((lambda (n) (lambda (f x) (f (n f x))))
((lambda (x) (x (lambda (x y) y))) p))))
((lambda (x y s) (s x y))
(lambda (f x) x)
(lambda (f x) x)))))
x)))
((lambda (n) (lambda (f x) (f (n f x)))) (lambda (f x) x))
((lambda (x y)
(x (lambda (n) (lambda (f x) (f (n f x)))) y))
(f ((lambda (n)
((lambda (x) (x (lambda (x y) x)))
(n (lambda (p)
((lambda (x y s) (s x y))
((lambda (x) (x (lambda (x y) y))) p)
((lambda (n) (lambda (f x) (f (n f x))))
((lambda (x) (x (lambda (x y) y))) p))))
((lambda (x y s) (s x y))
(lambda (f x) x)
(lambda (f x) x)))))
x))
(f ((lambda (n)
((lambda (x) (x (lambda (x y) x)))
(n (lambda (p)
((lambda (x y s) (s x y))
((lambda (x) (x (lambda (x y) y))) p)
((lambda (n) (lambda (f x) (f (n f x))))
((lambda (x) (x (lambda (x y) y))) p))))
((lambda (x y s) (s x y))
(lambda (f x) x)
(lambda (f x) x)))))
((lambda (n)
((lambda (x) (x (lambda (x y) x)))
(n (lambda (p)
((lambda (x y s) (s x y))
((lambda (x) (x (lambda (x y) y))) p)
((lambda (n) (lambda (f x) (f (n f x))))
((lambda (x) (x (lambda (x y) y))) p))))
((lambda (x y s) (s x y))
(lambda (f x) x)
(lambda (f x) x)))))
x)))))))))

;; The same after reducing all immediate function applications
(define fib
((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)))))))))

;; Cute reformatting of the above:
(define fib((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))))(
;;                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;;                      `---------------(cons 0 0)---------------'
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)))))))))

;; And for extra fun:

(λ(f)(λ
(x)(((x(λ(
x)(λ(x y)y)
)(λ(x y)x))(
x(λ(x)(λ(x y)
y))(λ(x y
)x))(((
x(λ(p)(
λ(s)(s
(p (λ(
x y)y))
(λ(f x
)(f((p(
λ(x y)
y))f x
))))))(
λ(s)(s(
λ(f x)x)
(λ(f x)x)
)))(λ(x y)
x))(λ(x)(λ(
x y)y)) (λ(
x y) x)))(λ(
f x)(f x))((f
((x(λ(p )(λ (s
)(s(p(  λ(x y)
y))(λ (  f x)(f(
(p (λ(    x y)y)
)f x)))    )))(λ(
y s)(s    (λ (f x
)x)(λ(      f x)x)
)))(λ(      x y)x))
)(λ(n)        (λ (f
x)(f (n        f x)))
)(f(((        (x(λ(p)
(λ(s)(s          (p( λ(
x y )y          ))(λ(f
x) (f((          p(λ(x y
)y)) f            x)))))
)(λ(s)(            s(λ(f x
)x)(λ(              f x)x)
))) (λ              (x y)x
))(λ(p                )(λ(s)(
s(p(λ(                x y)y)
)(λ (f                  x)(f((
p(λ (x                  y)y)) f
x))))))                  (λ(s)(
s(λ (f                    x)x)(λ
(f x)x)                    )))(λ(
x y)x)                      ))))))

|#