write-you-a-scheme-v2 / scheme

Write You a Scheme
https://www.wespiser.com/writings/wyas/home.html
MIT License
552 stars 114 forks source link

Add support for mutually recursive definitions #24

Closed lambdageek closed 5 years ago

lambdageek commented 7 years ago

Once #23 is fixed, you'll find that the following will not work:

(begin
  (define (even x) (if (== x 0) #t (odd (- x 1)))
  (define (odd x) (if (== x 0) #f (even (- x 1)))
  (even 9))

Returns Unbound variable: odd rather than the expected #f

Update: Actually once #23 and #25 are fixed, you'll find that even singly-recursive functions (such as foldl from the stdlib) will not work. Just trying to get factorial working is worth thinking about.

lambdageek commented 7 years ago

Note that this is going to be slightly non-trivial.

Fortunately you're in Haskell, so you can use a knot tying trick to make a bunch of closures that all reference the environment that would exist after all of them are added to it. This will probably require scanning through the begin form to find all the definitions that precede the final expression to be evaluated. (This section 1.2.3.7 from the Racket Reference might be useful - or possibly confusing - I'm not sure.)

adamwespiser commented 7 years ago

thanks for raising issues, you really are @lambdageek ! :)

adamwespiser commented 7 years ago

For any solutions, there must be balance between correctness for the sake of implementing Scheme standards, and simplicity to satisfy the tutorials purpose of teaching programming language development. Currently, our solution gets most of Scheme lexical scoping using an incredibly simple mechanism: the local function of ReaderT and local. I believe, the solution must satisfy simplicity for the sake of the narrative, and any proposed solutions should be considerate of this goal.

adamwespiser commented 5 years ago

Since mutual recursion is supported under the current implementation of the project, I'm going to close this issue. I've referenced this issue in Issue #23 , which should be the stub for any further work on this topic.