# 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: `call`s 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:

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

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:

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:

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

magic-env = rec(add-fact)          ; def. of magic-env
= 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 `let`s 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]
[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}}}
=> 4)
(test (run "{with {add3 {fun {x} {+ x 3}}}
{with {add1 {fun {x} {+ x 1}}}
{with {x 3}
=> 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:

• We never used more than one expression in a function body because there was no point in it, but now there is. To evaluate a sequence of Racket expressions, you wrap them in a `begin` expression.

• In most places you don’t actually need to use `begin` — these are places that are said to have an implicit `begin`: the body of a function (or any lambda expression), the body of a `let` (and `let`-relatives), the consequence positions in `cond`, `match`, and `cases` clauses and more. One of the common places where a `begin` is used is in an `if` expression (and some people prefer using `cond` instead when there is more than a single expression).

• `cond` without an `else` in the end can make sense, if all you’re using it it for is side-effects.

• `if` could get a single expression which is executed when the condition is true (and an unspecified value is used otherwise), but our language (as well as the default Racket language) always forbids this — there are convenient special forms for a one-sided `if`s: `when` & `unless`, and they can have any number of expressions (they have an implicit `begin`). They have an advantage of saying “this code does some side-effects here” more explicit.

• There is a function called `for-each` which is just like `map`, except that it doesn’t collect the list of results, it is used only for performing side effects.

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)

# Types for Boxes

Obviously, `Any` is not too great — it is the most generic type, so it provides the least information. For example, notice that

(unbox (second foo))

returns the right list, which is equal to `foo` itself — but if we try to grab some part of the resulting list:

(second (unbox (second foo)))

we get a type error, because the result of the `unbox` is `Any`, so Typed Racket knows nothing about it, and won’t allow you to treat it as a list. It is not too surprising that the type constructor that can help in this case is `Rec` which we have already seen — it allows a type that can refer to itself:

#lang pl
(: foo : (Rec this (List Number (Boxof (U #f this)))))
(define foo (list 1 (box #f)))
(set-box! (second foo) foo)

Note that either `foo` or the value in the box are both printed with a `Rec` type — the value in the box can’t just have a `(U #f this)` type, since `this` doesn’t mean anything in there, so the whole type needs to still be present.

There is another issue to be aware of with `Boxof` types. For most type constructors (like `Listof`), if `T1` is a subtype of `T2`, then we also know that`(Listof T1)` is a subtype of `(Listof T2)`. This makes the following code typecheck:

#lang pl
(: foo : (Listof Number) -> Number)
(define (foo l)
(first l))
(: bar : Integer -> Number)
(define (bar x)
(foo (list x)))

Since the `(Listof Integer)` is a subtype of the `(Listof Number)` input for `foo`, the application typechecks. But this is not the same for the output type, for example — if we change the `bar` type to:

(: bar : Integer -> Integer)

we get a type error since `Number` is not a subtype of `Integer`. So subtypes are required to “go up” on the input side and “down” on the other. So, in a sense, the fact that boxes are mutable means that their contents can be considered to be on the other side of the arrow, which is why for such `T1` subtype of `T2`, it is `(Boxof T2)` that is a subtype of `(Boxof T1)`, instead of the usual. For example, this doesn’t work:

#lang pl
(: foo : (Boxof Number) -> Number)
(define (foo b)
(unbox b))
(: bar : Integer -> Number)
(define (bar x)
(: b : (Boxof Integer))
(define b (box x))
(foo b))

And you can see why this is the case — the marked line is fine given a `Number` contents, so if the type checker allows passing in a box holding an integer, then that expression would mutate the contents and make it an invalid value.

However, boxes are not only mutable, they hold a value that can be read too, which means that they’re on both sides of the arrow, and this means that `(Boxof T1)` is a subtype of `(Boxof T2)` if `T2` is a subtype of `T1` and `T1` is a subtype of `T2` — in other words, this happens only when `T1` and `T2` are the same type. (See below for an extended demonstration of all of this.)

Note also that this demonstration requires that extra `b` definition, if it’s skipped:

(define (bar x)
(foo (box x)))

then this will typecheck again — Typed Racket will just consider the context that requires a box holding a `Number`, and it is still fine to initialize such a box with an `Integer` value.

As a side comment, this didn’t always work. Earlier in its existence, Typed Racket would always choose a specific type for values, which would lead to confusing errors with boxes. For example, the above would need to be written as

(define (bar x)
(foo (box (ann x : Number))))

to prevent Typed Racket from inferring a specific type. This is no longer the case, but there can still be some surprises. A similar annotation was needed in the case of a list holding a self-referential box, to avoid the initial `#f` from getting a specific-but-wrong type.

# `Boxof`’s Lack of Subtyping

The lack of any subtype relations between `(Boxof T)` and `(Boxof S)` regardless of `S` and `T` can roughly be explained as follows.

First, a box is a container that you can pull a value out of — which makes it similar to lists. In the case of lists, we have:

if:          S  subtype-of          T
then: (Listof S)  subtype-of  (Listof T)

This is true for all such containers that you can pull a value out of: if you expect to pull a `T` but you’re given a container of a subtype `S`, then things are still fine. Such “containers” include functions that produce a value — for example:

if:        S  subtype-of      T
then:  Q -> S  subtype-of  Q -> T

However, functions also have the other side, where things are different — instead of a side of some produced value, it’s the side of the consumed value. We get the opposite rule there:

if:    T      subtype-of  S
then:  S -> Q  subtype-of  T -> Q

To see why this is right, use `Number` and `Integer` for `S` and `T`:

if:    Integer      subtype-of  Number
then:  Number -> Q  subtype-of  Integer -> Q

so — if you expect a function that takes a number is a subtype of one that takes an integer; in other words, every function that takes a number is also a function that takes an integer, but not the other way.

To summarize all of this, when you make the output type of a function “smaller” (more constrained), the resulting type is smaller, but on the input side things are flipped — a bigger input type means a more constrained function.

Now, a `(Boxof T)` is a producer of `T` when you pull a value out of the box, but it’s also a consumer of `T` when you put such a value in it. This means that — using the above analogy — the `T` is on both sides of the arrow. This means that

if:    S subtype-of T  *and*  T subtype-of S
then:  (Boxof S) subtype-of (Boxof T)

which is actually:

if:          S  is-the-same-type-as        T
then:  (Boxof S)  is-the-same-type-as  (Boxof T)

A different way to look at this conclusion is to consider the function type of `(A -> A)`: when is it a subtype of some other `(B -> B)`? Only when `A` is a subtype of `B` and `B` is a subtype of `A`, which means that this happens only when `A` and `B` are the same type.

(Side note: this is related to the fact that in logic, `P => Q` is roughly equivalent to `not(P) or Q` — the left side, `P`, is inside a negation. It also explains why in `((S -> T) -> Q)` the `S` obeys the first rule, as if it was on the right side — because it’s negated twice.)

The following piece of code makes the analogy to function types more formally. Boxes behave as if their contents is on both sides of a function arrow — on the right because they’re readable, and on the left because they’re writable, which the conclusion that a `(Boxof A)` type is a subtype of itself and no other `(Boxof B)`.

#lang pl

;; a type for a "read-only" box
(define-type (Boxof/R A) = (-> A))
;; Boxof/R constructor
(: box/r : (All (A) A -> (Boxof/R A)))
(define (box/r x) (lambda () x))
;; we can see that (Boxof/R T1) is a subtype of (Boxof/R T2)
;; if T1 is a subtype of T2 (this is not surprising, since
;; these boxes are similar to any other container, like lists):
(: foo1 : Integer -> (Boxof/R Integer))
(define (foo1 b) (box/r b))
(: bar1 : (Boxof/R Number) -> Number)
(define (bar1 b) (b))
(test (bar1 (foo1 123)) => 123)

;; a type for a "write-only" box
(define-type (Boxof/W A) = (A -> Void))
;; Boxof/W constructor
(: box/w : (All (A) A -> (Boxof/W A)))
(define (box/w x) (lambda (new) (set! x new)))
;; in contrast to the above, (Boxof/W T1) is a subtype of
;; (Boxof/W T2) if T2 is a subtype of T1, *not* the other way
;; (and note how this is related to A being on the *left* side
;; of the arrow in the `Boxof/W' type):
(: foo2 : Number -> (Boxof/W Number))
(define (foo2 b) (box/w b))
(: bar2 : (Boxof/W Integer) Integer -> Void)
(define (bar2 b new) (b new))
(test (bar2 (foo2 123) 456))

;; combining the above two into a type for a "read/write" box
(define-type (Boxof/RW A) = (A -> A))
;; Boxof/RW constructor
(: box/rw : (All (A) A -> (Boxof/RW A)))
(define (box/rw x) (lambda (new) (let ([old x]) (set! x new) old)))
;; this combines the above two: `A' appears on both sides of the
;; arrow, so (Boxof/RW T1) is a subtype of (Boxof/RW T2) if T1
;; is a subtype of T2 (because there's an A on the right) *and*
;; if T2 is a subtype of T1 (because there's another A on the
;; left) -- and that can happen only when T1 and T2 are the same
;; type.  So this is a type error:
;;  (: foo3 : Integer -> (Boxof/RW Integer))
;;  (define (foo3 b) (box/rw b))
;;  (: bar3 : (Boxof/RW Number) Number -> Number)
;;  (define (bar3 b new) (b new))
;;  (test (bar3 (foo3 123) 456) => 123)
;;  ** Expected (Number -> Number), but got (Integer -> Integer)
;; And this a type error too:
;;  (: foo3 : Number -> (Boxof/RW Number))
;;  (define (foo3 b) (box/rw b))
;;  (: bar3 : (Boxof/RW Integer) Integer -> Integer)
;;  (define (bar3 b new) (b new))
;;  (test (bar3 (foo3 123) 456) => 123)
;;  ** Expected (Integer -> Integer), but got (Number -> Number)
;; The two types must be the same for this to work:
(: foo3 : Integer -> (Boxof/RW Integer))
(define (foo3 b) (box/rw b))
(: bar3 : (Boxof/RW Integer) Integer -> Integer)
(define (bar3 b new) (b new))
(test (bar3 (foo3 123) 456) => 123)

# Implementing a Circular Environment

We now use this to implement `rec` in the following way:

1. Change environments so that instead of values they hold boxes of values: `(Boxof VAL)` instead of `VAL`, and whenever `lookup` is used, the resulting boxed value is unboxed,

2. In the `WRec` case, create the new environment with some temporary binding for the identifier — any value will do since it should not be used (when named expressions are always `fun` expressions),

3. Evaluate the expression in the new environment,

4. Change the binding of the identifier (the box) to the result of this evaluation.

The resulting definition is:

(: 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)
(let ([new-cell (box (NumV 42))])
(let ([new-env (Extend id new-cell rest-env)])
(let ([value (eval expr new-env)])
(set-box! new-cell value)
new-env))))

Racket has another `let` relative for such cases of multiple-nested `let`s — `let*`. This form is a derived form — it is defined as a shorthand for using nested `let`s. The above is therefore exactly the same as this code:

(: 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)
(let* ([new-cell (box (NumV 42))]
[new-env  (Extend id new-cell rest-env)]
[value    (eval expr new-env)])
(set-box! new-cell value)
new-env))

This `let*` form can be read almost as a C/Java-ish kind of code:

fun extend_rec(id, expr, rest_env) {
new_cell  = new NumV(42);
new_env  = Extend(id, new_cell, rest_env);
value    = eval(expr, new_env);
*new_cell = value;
return new_env;
}

The code can be simpler if we fold the evaluation into the `set-box!` (since `value` is used just there), and if use `lookup` to do the mutation — since this way there is no need to hold onto the box. This is a bit more expensive, but since the binding is guaranteed to be the first one in the environment, the addition is just one quick step. The only binding that we need is the one for the new environment, which we can do as an internal definition, leaving us with:

(: extend-rec : Symbol FLANG ENV -> ENV)
(define (extend-rec id expr rest-env)
(define new-env (Extend id (box (NumV 42)) rest-env))
(set-box! (lookup id new-env) (eval expr new-env))
new-env)

A complete rehacked version of FLANG with a `rec` binding follows:

#lang pl

(define-type FLANG
[Num  Number]
[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 (or 'with 'rec) more)
(match sexpr
[(list 'with (list (symbol: name) named) body)
(With name (parse-sexpr named) (parse-sexpr body))]
[(list 'rec (list (symbol: name) named) body)
(WRec name (parse-sexpr named) (parse-sexpr body))]
[(cons x more)
(error 'parse-sexpr "bad `~s' syntax in ~s" x 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 ENV
[EmptyEnv]
[Extend Symbol (Boxof VAL) ENV])

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

(: lookup : Symbol ENV -> (Boxof VAL))
;; lookup a symbol in an environment, return its value or throw an
;; error if it isn't bound
(define (lookup name env)
(cases env
[(EmptyEnv) (error 'lookup "no binding for ~s" name)]
[(Extend id boxed-val rest-env)
(if (eq? id name) boxed-val (lookup name rest-env))]))

(: 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)
(define new-env (Extend id (box (NumV 42)) rest-env))
(set-box! (lookup id new-env) (eval expr new-env))
new-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 (box (eval named-expr env)) env))]
[(WRec bound-id named-expr bound-body)
(eval bound-body
(extend-rec bound-id named-expr env))]
[(Id name) (unbox (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 (box (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}}}
=> 4)
(test (run "{with {add3 {fun {x} {+ x 3}}}
{with {add1 {fun {x} {+ x 1}}}
{with {x 3}
=> 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)