2013-02-06 - Recursion, Recursion, Recursion - Recursion without the Magic - The Core of `make-recursive' ======================================================================== >>> Recursion, Recursion, Recursion [[[ PLAI Chapter 9 ]]] There is one major feature that is still missing from our language: we have no way to perform recursion (therefore no kind of loops). So far, we could only use recursion when we had *names*. In FLANG, the only way we can have names is through `with' which not good enough for recursion. To discuss the issue of recursion, we switch to a "broken" version of (untyped) Racket -- one where a `define' has a different scoping rules: the scope of the defined name does *not* cover the defined expression. Specifically, in this language, this doesn't work: #lang pl broken (define (fact n) (if (zero? n) 1 (* n (fact (- n 1))))) (fact 5) In our language, this translation would also not work (assuming we have `if' etc): {with {fact {fun {n} {if {= n 0} 1 {* n {call fact {- n 1}}}}}} {call fact 5}} And similarly, in plain Racket this won't work if `let' is the only tool you use to create bindings: (let ([fact (lambda (n) (if (zero? n) 1 (* n (fact (- n 1)))))]) (fact 5)) In the broken-scope language, the `define' form is more similar to a mathematical definition. For example, when we write: (define (F x) x) (define (G y) (F y)) (G F) it is actually shorthand for (define F (lambda (x) x)) (define G (lambda (y) (F y))) (G F) we can then replace defined names with their definitions: (define F (lambda (x) x)) (define G (lambda (y) (F y))) ((lambda (y) (F y)) (lambda (x) x)) and this can go on, until we get to the actual code that we wrote: ((lambda (y) ((lambda (x) x) y)) (lambda (x) x)) This means that the above `fact' definition is similar to writing: fact := (lambda (n) (if (zero? n) 1 (* n (fact (- n 1))))) (fact 5) which is not a well-formed definition -- it is *meaningless* (this is a formal use of the word "meaningless"). What we'd really want, is to take the *equation* (using `=' instead of `:=') fact = (lambda (n) (if (zero? n) 1 (* n (fact (- n 1))))) and find a solution which will be a value for `fact' that makes this true. If you look at the Racket evaluation rules handout on the web page, you will see that this problem is related to the way that we introduced the Racket `define': there is a hand-wavy explanation that talks about *knowing* things. The big question is: can we define recursive functions without Racket's magical `define' form? (Note: This question is a little different than the question of implementing recursion in our language -- in the Racket case we have no control over the implementation of the language. As it will eventually turn out, implementing recursion in our own language will be quite easy when we use mutation in a specific way. So the question that we're now facing can be phrased as either "can we get recursion in Racket without Racket's magical definition forms?" or "can we get recursion in our interpreter without mutation?".) ======================================================================== >>> Recursion without the Magic [[[ PLAI Chapter 20 (the later part; but do this in much more depth) ]]] [Note: This explanation is similar to the one you can find in "The Why of Y", by Richard Gabriel.] To implement recursion without the `define' magic, we first make an observation: this problem does *not* come up in a dynamically-scoped language. Consider the `let'-version of the problem: #lang pl dynamic (let ([fact (lambda (n) (if (zero? n) 1 (* n (fact (- n 1)))))]) (fact 5)) This works fine -- because by the time we get to evaluate the body of the function, `fact' is already bound to itself in the current dynamic scope. (This is another reason why dynamic scope is perceived as a convenient approach in new languages.) Regardless, the problem that we have with lexical scope is still there, but the way things work in a dynamic scope suggest a solution that we can use now. Just like in the dynamic scope case, when `fact' is called, it does have a value -- the only problem is that this value is inaccessible in the lexical scope of its body. Instead of trying to get the value in via lexical scope, we can imitate what happens in the dynamically scoped language by passing the `fact' value to itself so it can call itself (going back to the original code in the broken-scope language): (define (fact self n) ;*** (if (zero? n) 1 (* n (self (- n 1))))) (fact fact 5) ;*** except that now the recursive call should still send itself along: (define (fact self n) (if (zero? n) 1 (* n (self self (- n 1))))) ;*** (fact fact 5) The problem is that this required rewriting calls to `fact' -- both outside and recursive calls inside. To make this an acceptable solution, calls from both places should not change. Eventually, we should be able to get a working `fact' definition that uses just (lambda (n) (if (zero? n) 1 (* n (fact (- n 1))))) The first step in resolving this problem is to curry the `fact' definition. (define (fact self) ;*** (lambda (n) ;*** (if (zero? n) 1 (* n ((self self) (- n 1)))))) ;*** ((fact fact) 5) ;*** Now `fact' is no longer our factorial function -- it's a function that constructs it. So call it `make-fact', and bind `fact' to the actual factorial function. (define (make-fact self) ;*** (lambda (n) (if (zero? n) 1 (* n ((self self) (- n 1)))))) (define fact (make-fact make-fact)) ;*** (fact 5) ;*** We can try to do the same thing in the body of the factorial function: instead of calling (self self), just bind `fact' to it: (define (make-fact self) (lambda (n) (let ([fact (self self)]) ;*** (if (zero? n) 1 (* n (fact (- n 1))))))) ;*** (define fact (make-fact make-fact)) (fact 5) This works fine, but if we consider our original goal, we need to get that local `fact' binding outside of the (lambda (n) ...) -- so we're left with a definition that uses the factorial expression as is. So, swap the two lines: (define (make-fact self) (let ([fact (self self)]) ;*** (lambda (n) ;*** (if (zero? n) 1 (* n (fact (- n 1))))))) (define fact (make-fact make-fact)) (fact 5) But the problem is that this gets us into an infinite loop because we're trying to evaluate "(self self)" too early. In fact, if we ignore the body of the `let' and other details, we basically do this: (define (make-fact self) (self self)) (make-fact make-fact) --reduce-sugar--> (define make-fact (lambda (self) (self self))) (make-fact make-fact) --replace-definition--> ((lambda (self) (self self)) (lambda (self) (self self))) --rename-identifiers--> ((lambda (x) (x x)) (lambda (x) (x x))) And this expression has an interesting property: it reduces to itself, so evaluating it gets stuck in an infinite loop. So how do we solve this? Well, we know that (self self) *should* be the same value that is the factorial function itself -- so it must be a one-argument function. If it's such a function, we can use a value that is equivalent, except that it will not get evaluated until it is needed, when the function is called. The trick here is the observation that (lambda (n) (add1 n)) is really the same function as `add1', except that the `add1' part doesn't get evaluated until the function is called. Applying this trick to our code produces a version that does not get stuck in the same infinite loop: (define (make-fact self) (let ([fact (lambda (n) ((self self) n))]) ;*** (lambda (n) (if (zero? n) 1 (* n (fact (- n 1))))))) (define fact (make-fact make-fact)) (fact 5) Continuing from here -- we know that (let ([x v]) e) is the same as ((lambda (x) e) v) (remember how we derived `fun' from a `with'), so we can turn that `let' into the equivalent function application form: (define (make-fact self) ((lambda (fact) ;*** (lambda (n) (if (zero? n) 1 (* n (fact (- n 1)))))) (lambda (n) ((self self) n)))) ;*** (define fact (make-fact make-fact)) (fact 5) And note now that the (lambda (fact) ...) expression is everything that we need for a recursive definition of `fact' -- it has the proper factorial body with a plain recursive call. It's almost like the usual value that we'd want to define `fact' as, except that we still have to abstract on the recursive value itself. So lets move this code into a separate definition for `fact-core': (define fact-core ;*** (lambda (fact) (lambda (n) (if (zero? n) 1 (* n (fact (- n 1))))))) (define (make-fact self) (fact-core ;*** (lambda (n) ((self self) n)))) (define fact (make-fact make-fact)) (fact 5) We can now proceed by moving the (make-fact make-fact) self-application into its own function which is what creates the real factorial: (define fact-core (lambda (fact) (lambda (n) (if (zero? n) 1 (* n (fact (- n 1))))))) (define (make-fact self) (fact-core (lambda (n) ((self self) n)))) (define (make-real-fact) (make-fact make-fact)) ;*** (define fact (make-real-fact)) ;*** (fact 5) Rewrite the `make-fact' definition using an explicit `lambda': (define fact-core (lambda (fact) (lambda (n) (if (zero? n) 1 (* n (fact (- n 1))))))) (define make-fact ;*** (lambda (self) ;*** (fact-core (lambda (n) ((self self) n))))) (define (make-real-fact) (make-fact make-fact)) (define fact (make-real-fact)) (fact 5) and fold the functionality of `make-fact' and `make-real-fact' into a single `make-fact' function by just using the value of `make-fact' explicitly instead of through a definition: (define fact-core (lambda (fact) (lambda (n) (if (zero? n) 1 (* n (fact (- n 1))))))) (define (make-real-fact) (let ([make (lambda (self) ;*** (fact-core ;*** (lambda (n) ((self self) n))))]) ;*** (make make))) (define fact (make-real-fact)) (fact 5) We can now observe that `make-real-fact' has nothing that is specific to factorial -- we can make it take a "core function" as an argument: (define fact-core (lambda (fact) (lambda (n) (if (zero? n) 1 (* n (fact (- n 1))))))) (define (make-real-fact core) ;*** (let ([make (lambda (self) (core ;*** (lambda (n) ((self self) n))))]) (make make))) (define fact (make-real-fact fact-core)) ;*** (fact 5) and call it `make-recursive': (define fact-core (lambda (fact) (lambda (n) (if (zero? n) 1 (* n (fact (- n 1))))))) (define (make-recursive core) ;*** (let ([make (lambda (self) (core (lambda (n) ((self self) n))))]) (make make))) (define fact (make-recursive fact-core)) ;*** (fact 5) We're almost done now -- there's no real need for a separate `fact-core' definition, just use the value for the definition of `face': (define (make-recursive core) (let ([make (lambda (self) (core (lambda (n) ((self self) n))))]) (make make))) (define fact (make-recursive (lambda (fact) ;*** (lambda (n) (if (zero? n) 1 (* n (fact (- n 1)))))))) ;*** (fact 5) turn the `let' into a function form: (define (make-recursive core) ((lambda (make) (make make)) ;*** (lambda (self) ;*** (core (lambda (n) ((self self) n)))))) ;*** (define fact (make-recursive (lambda (fact) (lambda (n) (if (zero? n) 1 (* n (fact (- n 1)))))))) (fact 5) do some renamings to make things simpler -- `make' and `self' turn to `x', and `core' to `f': (define (make-recursive f) ;*** ((lambda (x) (x x)) ;*** (lambda (x) (f (lambda (n) ((x x) n)))))) ;*** (define fact (make-recursive (lambda (fact) (lambda (n) (if (zero? n) 1 (* n (fact (- n 1)))))))) (fact 5) or we can manually expand that first (lambda (x) (x x)) application to make the symmetry more obvious (not really surprising because it started with a `let' whose purpose was to do a self-application): (define (make-recursive f) ((lambda (x) (f (lambda (n) ((x x) n)))) ;*** (lambda (x) (f (lambda (n) ((x x) n)))))) ;*** (define fact (make-recursive (lambda (fact) (lambda (n) (if (zero? n) 1 (* n (fact (- n 1)))))))) (fact 5) And we finally got what we were looking for: a general way to define *any* recursive function without any magical `define' tricks. This also work for other recursive functions: #lang pl broken (define (make-recursive f) ((lambda (x) (f (lambda (n) ((x x) n)))) (lambda (x) (f (lambda (n) ((x x) n)))))) (define fact (make-recursive (lambda (fact) (lambda (n) (if (zero? n) 1 (* n (fact (- n 1)))))))) (fact 5) (define fib (make-recursive (lambda (fib) (lambda (n) (if (<= n 1) n (+ (fib (- n 1)) (fib (- n 2)))))))) (fib 8) (define length (make-recursive (lambda (length) (lambda (l) (if (null? l) 0 (+ (length (rest l)) 1)))))) (length '(x y z)) A convenient tool that people often use on paper is to perform a kind of a syntactic abstraction: "assume that whenever I write (twice foo) I really meant to write (foo foo)". This can often be done as plain abstractions (that is, using functions), but in some cases -- for example, if we want to abstract over definitions -- we just want such a rewrite rule. (More on this towards the end of the course.) The broken-scope language does provide such a tool -- `rewrite' extends the language with a rewrite rule. Using this, and our `make-recursive', we can make up a recursive definition form: (rewrite (define/rec (f x) E) => (define f (make-recursive (lambda (f) (lambda (x) E))))) In other words, we've created our own "magical definition" form. The above code can now be written in almost the same way it is written in plain Racket: #lang pl broken (define (make-recursive f) ((lambda (x) (f (lambda (n) ((x x) n)))) (lambda (x) (f (lambda (n) ((x x) n)))))) (rewrite (define/rec (f x) E) => (define f (make-recursive (lambda (f) (lambda (x) E))))) ;; examples (define/rec (fact n) (if (zero? n) 1 (* n (fact (- n 1))))) (fact 5) (define/rec (fib n) (if (<= n 1) n (+ (fib (- n 1)) (fib (- n 2))))) (fib 8) (define/rec (length l) (if (null? l) 0 (+ (length (rest l)) 1))) (length '(x y z)) Finally, note that make-recursive is limited to 1-argument functions only because of the protection from eager evaluation. In any case, it can be used in any way you want, for example, (make-recursive (lambda (f) (lambda (x) f))) is a function that *returns* itself rather than calling itself. Using the rewrite rule, this would be: (define/rec (f x) f) which is the same as: (define (f x) f) in plain Racket. ======================================================================== >>> The Core of `make-recursive' As in Racket, being able to express recursive functions is a fundamental property of the language. It means that we can have loops in our language, and that's the essence of making a language powerful enough to be TM-equivalent -- able to express undecidable problems, where we don't know whether there is an answer or not. The core of what makes this possible is the expression that we have seen in our derivation: ((lambda (x) (x x)) (lambda (x) (x x))) which reduces to itself, and therefore has no value: trying to evaluate it gets stuck in an infinite loop. (This expression is often called "Omega".) This is the key for creating a loop -- we use it to make recursion possible. Looking at our final `make-recursive' definition and ignoring for a moment the "protection" that we need against being stuck prematurely in an infinite loop: (define (make-recursive f) ((lambda (x) (x x)) (lambda (x) (f (x x))))) we can see that this is almost the same as the Omega expression -- the only difference is that application of `f'. Indeed, this expression (the result of (make-recursive F) for some `F') reduces in a similar way to Omega: ((lambda (x) (x x)) (lambda (x) (F (x x)))) ((lambda (x) (F (x x))) (lambda (x) (F (x x)))) (F ((lambda (x) (F (x x))) (lambda (x) (F (x x))))) (F (F ((lambda (x) (F (x x))) (lambda (x) (F (x x)))))) (F (F (F ((lambda (x) (F (x x))) (lambda (x) (F (x x))))))) ... which means that the actual value of this expression is: (F (F (F ...forever...))) This definition would be sufficient if we had a lazy language, but to get things working in a strict one we need to bring back the protection. This makes things a little different -- if we use (protect f) to be a shorthand for the protection trick, (rewrite (protect f) => (lambda (x) (f x))) then we have: (define (make-recursive f) ((lambda (x) (x x)) (lambda (x) (f (protect (x x)))))) which makes the (make-recursive F) evaluation reduce to (F (protect (F (protect (F (protect (...forever...))))))) and this is still the same result (as long as `F' is a single-argument function). (Note that `protect' cannot be implemented as a plain function!) ========================================================================