PL: Lecture #12  Tuesday, February 19th
(text file)

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:

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

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

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

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

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

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

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

Using this, we can define a few useful numbers:

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

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

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

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

(->nat identity)

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

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

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

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

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

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

or equivalently:

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

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

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

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

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

An alternative approach is to consider

(lambda (x) (n f x))

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

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

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

(n f)

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

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

which is the same as

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

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

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

And there is a similar alternative here too —

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

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

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 forms 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
      (add1 (length (cdr l))))))
(->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 1 (add1 0))
(define 2 (add1 1))
(define 3 (add1 2))
(define 4 (add1 3))
(define 5 (add1 4))
(test (->nat (add1 (add1 5))) => '7)

(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
      (add1 (length (cdr l))))))
(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)                      ))))))

|#

Alternative Church Encoding

Finally, note that this is just one way to encode things — other encodings are possible. One alternative encoding is in the following code — it uses a list of N falses as the encoding for N. This encoding makes it easier to add1 (just cons another #f), and to sub1 (simply cdr). The tradeoff is that some arithmetics operations becomes more complicated, for example, the definition of + requires the fixpoint combinator. (As expected, some people want to see what can we do with a language without recursion, so they don’t like jumping to Y too fast.)

;; An alternative "Church" encoding: use lists to encode numbers

#lang pl schlac

(define identity (lambda (x) x))

;; Booleans (same as before)

(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 (->bool (if #t #f #t))
      => '#f)
(test (->bool (if #f ((lambda (x) (x x)) (lambda (x) (x x))) #t))
      => '#t)

(define and (lambda (a b) (a b a)))
(define or  (lambda (a b) (a a b)))
(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)

;; Lists (same as before)

(define cons (lambda (x y s) (s x y)))
(define car (lambda (x) (x #t)))
(define cdr (lambda (x) (x #f)))

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

;; Natural numbers (alternate encoding)

(define 0 identity)

(define add1 (lambda (n) (cons #f n)))

(define zero? car) ; tricky

(define sub1 cdr) ; this becomes very simple

;; Note that we could have used something more straightforward:
;; (define 0 null)
;; (define add1 (lambda (n) (cons #t n))) ; cons anything
;; (define zero? null?)
;; (define sub1 (lambda (l) (if (zero? l) l (cdr l))))

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

(test (->nat* (add1 (add1 5))) => '7)
(test (->nat* (sub1 (sub1 (add1 (add1 5))))) => '5)
(test (->bool (and (zero? 0) (not (zero? 3)))) => '#t)
(test (->bool (zero? (sub1 (sub1 (sub1 3))))) => '#t)

;; list-of-numbers tests
(define l123 (cons 1 (cons 2 (cons 3 null))))
(test (->listof ->nat* l123) => '(1 2 3))
(test (->listof (->listof ->nat*) (cons l123 (cons l123 null)))
      => '((1 2 3) (1 2 3)))

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

;; note that this example is doing something silly now
(define/rec length
  (lambda (l)
    (if (null? l)
      0
      (add1 (length (cdr l))))))
(test (->nat* (length l123)) => '3)

;; addition becomes hard since it requires a recursive definition
;; (define/rec +
;;  (lambda (m n) (if (zero? n) m (+ (add1 m) (sub1 n)))))
;; (test (->nat* (+ 4 5)) => '9)

;; faster alternative:
(define/rec +
  (lambda (m n)
    (if (zero? m) n
        (if (zero? n) m
            (add1 (add1 (+ (sub1 m) (sub1 n))))))))
(test (->nat* (+ 4 5)) => '9)

;; subtraction is similar to addition
;; (define/rec -
;;  (lambda (m n) (if (zero? n) m (- (sub1 m) (sub1 n)))))
;; (test (->nat* (- (+ 4 5) 4)) => '5)
;; but this is not "natural subtraction": doesn't work when n>m,
;; because (sub1 0) does not return 0.

;; a solution is like alternative form of +:
(define/rec -
  (lambda (m n)
    (if (zero? m) 0
        (if (zero? n) m
            (- (sub1 m) (sub1 n))))))
(test (->nat* (- (+ 4 5) 4)) => '5)
(test (->nat* (- 2 5)) => '0)
;; alternatively, could change sub1 above:
;; (define sub1 (lambda (n) (if (zero? n) n (cdr n))))

;; we can do multiplication in a similar way
(define/rec *
  (lambda (m n)
    (if (zero? m) 0
        (+ n (* (sub1 m) n)))))
(test (->nat* (* 4 5)) => '20)
(test (->nat* (+ 4 (* (+ 2 5) 5))) => '39)

;; and the rest of the 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 (sub1 x)) (fib (sub1 (sub1 x)))))))
(test (->nat* (fib (* 5 2))) => '89)

#|
;; Fully-expanded Fibonacci (note: much shorter than the previous
;; encoding, but see how Y appears twice -- two "((lambda" pairs)
(define fib((lambda(f)((lambda(x)(x x))(lambda(x)(f(x x)))))(lambda(
f)(lambda(x)(((((x(lambda(x y)x))(x(lambda(x y)x)))((x(lambda(x y)y)
)(lambda(x y)x)))(lambda(s)(s(lambda(x y)y)(lambda(x)x))))((((lambda
(f)((lambda(x)(x x))(lambda(x)(f(x x))))) (lambda(f)(lambda(m n)((m(
lambda(x y)x))n (((n(lambda(x y)x)) m)(lambda(s)((s (lambda(x y)y))(
lambda(s)((s (lambda(x y)y))((f(m(lambda(x y)y)))(n(lambda(x y)y))))
))))))))(f(x(lambda(x y)y))))(f((x(lambda(x y)y))(lambda(x y)y))))))
)))
|#

Another interesting way to implement lists follows the pattern matching approach, where both pairs and the null value are represented by a function that serves as a kind of a match dispatcher. This function takes in two inputs — if it is the representation of null then it will return the first input, and if it is a pair, then it will apply the second input on the two parts of the pair. This is implemented as follows (with type comments to make it clear):

;; null : List
(define null
  (lambda (n p)
    n))

;; cons : A List -> List
(define cons
  (lambda (x y)
    (lambda (n p)
      (p x y))))

This might seem awkward, but it follows the intended use of pairs and null as a match-like construct. Here is an example, with the equivalent Racket code on the side:

;; Sums up a list of numbers
(define (sum l)
  (l                    ; (match l
  0                    ;  ['() 0]
  (lambda (x xs)      ;  [(cons x xs)
    (+ x (sum xs)))))  ;    (+ x (sum xs))])

In fact, it’s easy to implement our selectors and predicate using this:

(define null? (lambda (l) (l #t (lambda (x xs) #f))))
(define car  (lambda (l) (l #f (lambda (x y) x))))
(define cdr  (lambda (l) (l #f (lambda (x y) y))))
;; in the above `#f' is really any value, since it
;; should be an error alternatively:
(define car (lambda (l)
              (l ((lambda (x) (x x)) (lambda (x) (x x))) ; "error"
                (lambda (x y) x))))

The same approach can be used to define any kind of new data type in a way that looks like our own define-type definitions. For example, consider a much-simplified definition of the AE type we’ve seen early in the semester, and a matching eval definition as an example for using cases:

(define-type AE
  [Num Number]
  [Add AE AE])
(: eval : AE -> Number)
(define (eval expr)
  (cases expr
    [(Num n)  n]
    [(Add l r) (+ (eval l) (eval r))]))

We can follow the above approach now to write Schlac code that more than being equivalent, it’s also very similar in nature. Note that the type definition is replaced by two definitions for the two constructors:

(define Num (lambda (n)  (lambda (num add) (num n  ))))
(define Add (lambda (l r) (lambda (num add) (add l r))))
(define eval
  (lambda (expr) ; `expr` is always a (lambda (num add) ...), and it
                ; expects a unary `num` argument and a binary `add`
    (expr (lambda (n)  n)
          (lambda (l r) (+ (eval l) (eval r))))))

Recursive Environments

PLAI §11.5

What we really need for recursion, is a special kind of an environment, one that can refer to itself. So instead of doing (note: calls removed for readability):

{with {fact {fun {n}
              {if {zero? n} 1 {* n {fact {- n 1}}}}}}
  {fact 5}}

which does not work for the usual reasons, we want to use some

{rec {fact {fun {n}
            {if {zero? n} 1 {* n {fact {- n 1}}}}}}
  {fact 5}}

that will do the necessary magic.

One way to achieve this is using the Y combinator as we have seen — a kind of a “constructor” for recursive functions. We can do that in a similar way to the rewrite rule that we have seen in Schlac — translate the above expression to:

{with {fact {make-rec
              {fun {fact}
                {fun {n}
                  {if {zero? n} 1 {* n {fact {- n 1}}}}}}}}
  {fact 5}}

or even:

{with {fact {{fun {f} {{fun {x} {f {x x}}} {fun {x} {f {x x}}}}}
            {fun {fact}
              {fun {n}
                {if {zero? n} 1 {* n {fact {- n 1}}}}}}}}
  {fact 5}}

Now, we will see how it can be used in our code to implement a recursive environment.

If we look at what with does in

{with {fact {fun {n}
              {if {zero? n} 1 {* n {call fact {- n 1}}}}}}
  {call fact 5}}

then we can say that to evaluate this expression, we evaluate the body expression in an extended environment that contains fact, even if a bogus one that is good for 0 only — the new environment is created with something like this:

extend("fact", make-fact-closure(), env)

so we can take this whole thing as an operation over env

add-fact(env) := extend("fact", make-fact-closure(), env)

This gives us the first-level fact. But fact itself is still undefined in env, so it cannot call itself. We can try this:

add-fact(add-fact(env))

but that still doesn’t work, and it will never work no matter how far we go:

add-fact(add-fact(add-fact(add-fact(add-fact(...env...)))))

What we really want is infinity: a place where add-fact works and the result is the same as what we’ve started with — we want to create a “magical” environment that makes this possible:

let magic-env = ???
such that:
  add-fact(magic-env) = magic-env

which basically gives us the illusion of being at the infinity point. This magic-env thing is exactly the fixed-point of the add-fact operation. We can use:

magic-env = rec(add-fact)

and following the main property of the Y combinator, we know that:

magic-env = rec(add-fact)          ; def. of magic-env
          = add-fact(rec(add-fact)) ; Y(f) = f(Y(f))
          = add-fact(magic-env)    ; def. of magic-env

What does all this mean? It means that if we have a fixed-point operator at the level of the implementation of our environments, then we can use it to implement a recursive binder. In our case, this means that a fixpoint in Racket can be used to implement a recursive language. But we have that — Racket does have recursive functions, so we should be able to use that to implement our recursive binder.

There are two ways that make it possible to write recursive functions in Racket. One is to define a function, and use its name to do a recursive call — using the Racket formal rules, we can see that we said that we mark that we now know that a variable is bound to a value. This is essentially a side-effect — we modify what we know, which corresponds to modifying the global environment. The second way is a new form: letrec. This form is similar to let, except that the scope that is established includes the named expressions — it is exactly what we want rec to do. A third way is using recursive local definitions, but that is equivalent to using letrec, more on this soon.

Recursion: Racket’s letrec

So we want to add recursion to our language, practically. We already know that Racket makes it possible to write recursive functions, which is possible because of the way it implements its “global environment”: our evaluator can only extend an environment, while Racket modifies its global environment. This means that whenever a function is defined in the global environment, the resulting closure will have it as its environment “pointer”, but the global environment was not extended — it stays the same, and was just modified with one additional binding.

But Racket has another, a bit more organized way of using recursion: there is a special local-binding construct that is similar to let, but allows a function to refer to itself. It is called letrec:

(letrec ([fact (lambda (n)
                (if (zero? n)
                  1
                  (* n (fact (- n 1)))))])
  (fact 5))

Some people may remember that there was a third way for creating recursive functions: using local definition in function bodies. For example, we have seen things like:

(define (length list)
  (define (helper list len)
    (if (null? list)
      len
      (helper (rest list) (+ len 1))))
  (helper list 0))

This looks like the same kind of environment magic that happens with a global define — but actually, Racket defines the meaning of internal definitions using letrec — so the above code is exactly the same as:

(define (length list)
  (letrec ([helper (lambda (list len)
                    (if (null? list)
                      len
                      (helper (rest list) (+ len 1))))])
    (helper list 0)))

The scoping rules for a letrec is that the scope of the bound name covers both the body and the named expression. Furthermore, multiple names can be bound to multiple expressions, and the scope of each name covers all named expression as well as the body. This makes it easy to define mutually recursive functions, such as:

(letrec ([even? (lambda (n) (if (zero? n) #t (odd?  (- n 1))))]
        [odd?  (lambda (n) (if (zero? n) #f (even? (- n 1))))])
  (even? 99))

But it is not a required functionality — it could be done with a single recursive binding that contains several functions:

(letrec ([even+odd
          (list (lambda (n)
                  (if (zero? n) #t ((second even+odd) (- n 1))))
                (lambda (n)
                  (if (zero? n) #f ((first  even+odd) (- n 1)))))])
  ((first even+odd) 99))

This is basically the same problem we face if we want to use the Y combinator for mutually recursive bindings. The above solution is inconvenient, but it can be improved using more lets to have easier name access. For example:

(letrec ([even+odd
          (list (lambda (n)
                  (let ([even? (first  even+odd)]
                        [odd?  (second even+odd)])
                    (if (zero? n) #t (odd? (- n 1)))))
                (lambda (n)
                  (let ([even? (first  even+odd)]
                        [odd?  (second even+odd)])
                    (if (zero? n) #f (even? (- n 1))))))])
  (let ([even? (first  even+odd)]
        [odd?  (second even+odd)])
    (even? 99)))

Implementing Recursion using letrec

We will see how to add a similar construct to our language — for simplicity, we will add a rec form that handles a single binding:

{rec {fact {fun {n}
            {if {= 0 n}
              1
              {* n {fact {- n 1}}}}}}
  {fact 5}}

Using this, things can get a little tricky. What should we get if we do:

{rec {x x} x}

? Currently, it seems like there is no point in using any expression except for a function expression in a rec expression, so we will handle only these cases.

(BTW, under what circumstances would non-function values be useful in a letrec?)


One way to achieve this is to use the same trick that we have recently seen: instead of re-implementing language features, we can use existing features in our own language, which hopefully has the right functionality in a form that can be re-used to in our evaluator.

Previously, we have seen a way to implement environments using Racket closures:

;; Define a type for functional environments
(define-type ENV = Symbol -> VAL)

(: EmptyEnv : -> ENV)
(define (EmptyEnv)
  (lambda (id) (error 'lookup "no binding for ~s" id)))

(: lookup : Symbol ENV -> VAL)
(define (lookup name env)
  (env name))

(: Extend : Symbol VAL ENV -> ENV)
(define (Extend id val rest-env)
  (lambda (name)
    (if (eq? name id)
      val
      (rest-env name))))

We can use this implementation, and create circular environments using Racket’s letrec. The code for handling a with expressions is:

[(With bound-id named-expr bound-body)
(eval bound-body
      (Extend bound-id (eval named-expr env) env))]

It looks like we should be able to handle rec in a similar way (the AST constructor name is WRec (“with-rec”) so it doesn’t collide with TR’s Rec constructor for recursive types):

[(WRec bound-id named-expr bound-body)
(eval bound-body
      (Extend bound-id (eval named-expr env) env))]

but this won’t work because the named expression is evaluated prematurely, in the previous environment. Instead, we will move everything that needs to be done, including evaluation, to a separate extend-rec function:

[(WRec bound-id named-expr bound-body)
(eval bound-body
      (extend-rec bound-id named-expr env))]

Now, the extend-rec function needs to provide the new, “magically circular” environment. Following what we know about the arguments to extend-rec, and the fact that it returns a new environment (= a lookup function), we can sketch a rough definition:

(: extend-rec : Symbol FLANG ENV -> ENV) ; FLANG, not VAL!
;; extend an environment with a new binding that is the result of
;; evaluating an expression in the same environment as the extended
;; result
(define (extend-rec id expr rest-env)
  (lambda (name)
    (if (eq? name id)
      ... something that uses expr to get a value ...
      (rest-env name))))

What should the missing expression be? It can simply evaluate the object given itself:

(define (extend-rec id expr rest-env)
  (lambda (name)
    (if (eq? name id)
      (eval expr ...this environment...)
      (rest-env name))))

But how do we get this environment, before it is defined? Well, the environment is itself a Racket function, so we can use Racket’s letrec to make the function refer to itself recursively:

(define (extend-rec id expr rest-env)
  (letrec ([rec-env (lambda (name)
                      (if (eq? name id)
                        (eval expr rec-env)
                        (rest-env name)))])
    rec-env))

It’s a little more convenient to use an internal definition, and add a type for clarity:

(define (extend-rec id expr rest-env)
  (: rec-env : Symbol -> VAL)
  (define (rec-env name)
    (if (eq? name id)
      (eval expr rec-env)
      (rest-env name)))
  rec-env)

This works, but there are several problems:

  1. First, we no longer do a simple lookup in the new environment. Instead, we evaluate the expression on every such lookup. This seems like a technical point, because we do not have side-effects in our language (also because we said that we want to handle only function expressions). Still, it wastes space since each evaluation will allocate a new closure.

  2. Second, a related problem — what happens if we try to run this:

    {rec {x x} x}

    ? Well, we do that stuff to extend the current environment, then evaluate the body in the new environment, this body is a single variable reference:

    (eval (Id 'x) the-new-env)

    so we look up the value:

    (lookup 'x the-new-env)

    which is:

    (the-new-env 'x)

    which goes into the function which implements this environment, there we see that name is the same as name1, so we return:

    (eval expr rec-env)

    but the expr here is the original named-expression which is itself (Id 'x), and we’re in an infinite loop.

We can try to get over these problems using another binding. Racket allows several bindings in a single letrec expression or multiple internal function definitions, so we change extend-rec to use the newly-created environment:

(define (extend-rec id expr rest-env)
  (: rec-env : Symbol -> VAL)
  (define (rec-env name)
    (if (eq? name id)
      val
      (rest-env name)))
  (: val : VAL)
  (define val (eval expr rec-env))
  rec-env)

This runs into an interesting type error, which complains about possibly getting some Undefined value. It does work if we switch to the untyped language for now (using #lang pl untyped) — and it seems to run fine too. But it raises more questions, beginning with: what is the meaning of:

(letrec ([x ...]
        [y ...x...])
  ...)

or equivalently, an internal block of

(define x ...)
(define y ...x...)

? Well, DrRacket seems to do the “right thing” in this case, but what about:

(letrec ([y ...x...]
        [x ...])
  ...)

? As a hint, see what happens when we now try to evaluate the problematic

{rec {x x} x}

expression, and compare that with the result that you’d get from Racket. This also clarifies the type error that we received.

It should be clear now why we want to restrict usage to just binding recursive functions. There are no problems with such definitions because when we evaluate a fun expression, there is no evaluation of the body, which is the only place where there are potential references to the same function that is defined — a function’s body is delayed, and executed only when the function is applied later.

But the biggest question that is still open: we just implemented a circular environment using Racket’s own circular environment implementation, and that does not explain how they are actually implemented. The cycle of pointers that we’ve implemented depends on the cycle of pointers that Racket uses, and that is a black box we want to open up.

For reference, the complete code is below.

#lang pl

#|
The grammar:
  <FLANG> ::= <num>
            | { + <FLANG> <FLANG> }
            | { - <FLANG> <FLANG> }
            | { * <FLANG> <FLANG> }
            | { / <FLANG> <FLANG> }
            | { with { <id> <FLANG> } <FLANG> }
            | { rec { <id> <FLANG> } <FLANG> }
            | <id>
            | { fun { <id> } <FLANG> }
            | { call <FLANG> <FLANG> }

Evaluation rules:
  eval(N,env)                = N
  eval({+ E1 E2},env)        = eval(E1,env) + eval(E2,env)
  eval({- E1 E2},env)        = eval(E1,env) - eval(E2,env)
  eval({* E1 E2},env)        = eval(E1,env) * eval(E2,env)
  eval({/ E1 E2},env)        = eval(E1,env) / eval(E2,env)
  eval(x,env)                = lookup(x,env)
  eval({with {x E1} E2},env) = eval(E2,extend(x,eval(E1,env),env))
  eval({rec {x E1} E2},env)  = ???
  eval({fun {x} E},env)      = <{fun {x} E}, env>
  eval({call E1 E2},env1)
          = eval(Ef,extend(x,eval(E2,env1),env2))
                            if eval(E1,env1) = <{fun {x} Ef}, env2>
          = error!          otherwise
|#

(define-type FLANG
  [Num  Number]
  [Add  FLANG FLANG]
  [Sub  FLANG FLANG]
  [Mul  FLANG FLANG]
  [Div  FLANG FLANG]
  [Id  Symbol]
  [With Symbol FLANG FLANG]
  [WRec Symbol FLANG FLANG]
  [Fun  Symbol FLANG]
  [Call FLANG FLANG])

(: parse-sexpr : Sexpr -> FLANG)
;; parses s-expressions into FLANGs
(define (parse-sexpr sexpr)
  (match sexpr
    [(number: n)    (Num n)]
    [(symbol: name) (Id name)]
    [(cons 'with more)
    (match sexpr
      [(list 'with (list (symbol: name) named) body)
        (With name (parse-sexpr named) (parse-sexpr body))]
      [else (error 'parse-sexpr "bad `with' syntax in ~s" sexpr)])]
    [(cons 'rec more)
    (match sexpr
      [(list 'rec (list (symbol: name) named) body)
        (WRec name (parse-sexpr named) (parse-sexpr body))]
      [else (error 'parse-sexpr "bad `rec' syntax in ~s" sexpr)])]
    [(cons 'fun more)
    (match sexpr
      [(list 'fun (list (symbol: name)) body)
        (Fun name (parse-sexpr body))]
      [else (error 'parse-sexpr "bad `fun' syntax in ~s" sexpr)])]
    [(list '+ lhs rhs) (Add (parse-sexpr lhs) (parse-sexpr rhs))]
    [(list '- lhs rhs) (Sub (parse-sexpr lhs) (parse-sexpr rhs))]
    [(list '* lhs rhs) (Mul (parse-sexpr lhs) (parse-sexpr rhs))]
    [(list '/ lhs rhs) (Div (parse-sexpr lhs) (parse-sexpr rhs))]
    [(list 'call fun arg)
                      (Call (parse-sexpr fun) (parse-sexpr arg))]
    [else (error 'parse-sexpr "bad syntax in ~s" sexpr)]))

(: parse : String -> FLANG)
;; parses a string containing a FLANG expression to a FLANG AST
(define (parse str)
  (parse-sexpr (string->sexpr str)))

;; Types for environments, values, and a lookup function

(define-type VAL
  [NumV Number]
  [FunV Symbol FLANG ENV])

;; Define a type for functional environments
(define-type ENV = Symbol -> VAL)

(: EmptyEnv : -> ENV)
(define (EmptyEnv)
  (lambda (id) (error 'lookup "no binding for ~s" id)))

(: lookup : Symbol ENV -> VAL)
;; lookup a symbol in an environment, return its value or throw an
;; error if it isn't bound
(define (lookup name env)
  (env name))

(: Extend : Symbol VAL ENV -> ENV)
;; extend a given environment cache with a new binding
(define (Extend id val rest-env)
  (lambda (name)
    (if (eq? name id)
      val
      (rest-env name))))

(: extend-rec : Symbol FLANG ENV -> ENV)
;; extend an environment with a new binding that is the result of
;; evaluating an expression in the same environment as the extended
;; result
(define (extend-rec id expr rest-env)
  (: rec-env : Symbol -> VAL)
  (define (rec-env name)
    (if (eq? name id)
      val
      (rest-env name)))
  (: val : VAL)
  (define val (eval expr rec-env))
  rec-env)

(: NumV->number : VAL -> Number)
;; convert a FLANG runtime numeric value to a Racket one
(define (NumV->number val)
  (cases val
    [(NumV n) n]
    [else (error 'arith-op "expected a number, got: ~s" val)]))

(: arith-op : (Number Number -> Number) VAL VAL -> VAL)
;; gets a Racket numeric binary operator, and uses it within a NumV
;; wrapper
(define (arith-op op val1 val2)
  (NumV (op (NumV->number val1) (NumV->number val2))))

(: eval : FLANG ENV -> VAL)
;; evaluates FLANG expressions by reducing them to values
(define (eval expr env)
  (cases expr
    [(Num n) (NumV n)]
    [(Add l r) (arith-op + (eval l env) (eval r env))]
    [(Sub l r) (arith-op - (eval l env) (eval r env))]
    [(Mul l r) (arith-op * (eval l env) (eval r env))]
    [(Div l r) (arith-op / (eval l env) (eval r env))]
    [(With bound-id named-expr bound-body)
    (eval bound-body
          (Extend bound-id (eval named-expr env) env))]
    [(WRec bound-id named-expr bound-body)
    (eval bound-body
          (extend-rec bound-id named-expr env))]
    [(Id name) (lookup name env)]
    [(Fun bound-id bound-body)
    (FunV bound-id bound-body env)]
    [(Call fun-expr arg-expr)
    (let ([fval (eval fun-expr env)])
      (cases fval
        [(FunV bound-id bound-body f-env)
          (eval bound-body
                (Extend bound-id (eval arg-expr env) f-env))]
        [else (error 'eval "`call' expects a function, got: ~s"
                            fval)]))]))

(: run : String -> Number)
;; evaluate a FLANG program contained in a string
(define (run str)
  (let ([result (eval (parse str) (EmptyEnv))])
    (cases result
      [(NumV n) n]
      [else (error 'run "evaluation returned a non-number: ~s"
                  result)])))

;; tests
(test (run "{call {fun {x} {+ x 1}} 4}")
      => 5)
(test (run "{with {add3 {fun {x} {+ x 3}}}
              {call add3 1}}")
      => 4)
(test (run "{with {add3 {fun {x} {+ x 3}}}
              {with {add1 {fun {x} {+ x 1}}}
                {with {x 3}
                  {call add1 {call add3 x}}}}}")
      => 7)
(test (run "{with {identity {fun {x} x}}
              {with {foo {fun {x} {+ x 1}}}
                {call {call identity foo} 123}}}")
      => 124)
(test (run "{with {x 3}
              {with {f {fun {y} {+ x y}}}
                {with {x 5}
                  {call f 4}}}}")
      => 7)
(test (run "{call {with {x 3}
                    {fun {y} {+ x y}}}
                  4}")
      => 7)
(test (run "{with {f {with {x 3} {fun {y} {+ x y}}}}
              {with {x 100}
                {call f 4}}}")
      => 7)
(test (run "{call {call {fun {x} {call x 1}}
                        {fun {x} {fun {y} {+ x y}}}}
                  123}")
      => 124)

Implementing rec Using Cyclic Structures

PLAI §10

Looking at the arrows in the environment diagrams, what we’re really looking for is a closure that has an environment pointer which is the same environment in which it was defined. This will make it possible for fact to be bound to a closure that can refer to itself since its environment is the same one in which it is defined. However, so far we have no tools that makes it possible to do this.

What we need is to create a “cycle of pointers”, and so far we do not have a way of achieving that: when we create a closure, we begin with an environment which is saved in the slot’s environment slot, but we want that closure to be the value of a binding in that same environment.

Boxes and Mutation

To actually implement a circular structure, we will now use side-effects, using a new kind of Racket value which supports mutation: a box. A box value is built with the box constructor:

(define my-thing (box 7))

the value is retrieved with the `unbox’ function,

(* 6 (unbox my-thing))

and finally, the value can be changed with the set-box! function.

(set-box! my-thing 17)
(* 6 (unbox my-thing))

An important thing to note is that set-box! is much like display etc, it returns a value that is not printed in the Racket REPL, because there is no point in using the result of a set-box!, it is called for the side-effect it generates. (Languages like C blur this distinction between returning a value and a side-effect with its assignment statement.)

As a side note, we now have side effects of two kinds: mutation of state, and I/O (at least the O part). (Actually, there is also infinite looping that can be viewed as another form of a side effect.) This means that we’re now in a completely different world, and lots of new things can make sense now. A few things that you should know about:

When any one of these things is used (in Racket or other languages), you can tell that side-effects are involved, because there is no point in any of them otherwise. In addition, any name that ends with a ! (“bang”) is used to mark a function that changes state (usually a function that only changes state).

So how do we create a cycle? Simple, boxes can have any value, and they can be put in other values like lists, so we can do this:

#lang pl untyped
(define foo (list 1 (box 3)))
(set-box! (second foo) foo)

and we get a circular value. (Note how it is printed.) And with types:

#lang pl
(: foo : (List Number (Boxof Any)))
(define foo (list 1 (box 3)))
(set-box! (second foo) foo)