CS 3520 Homework 6 - Due October 11
Exercise 6.1, Adding Recursive Bindings to the Toy Language
Your task: Extend your interpreter from HW5 with a new letrec-like form.The syntax of the language is extended as follows:
<expr> = ... ;; same as HW5
| {[<expr>], <expr>} ;; recursive local binding
To evaluate the expression{[expr1], expr2}
the environment for evaluating expr2 is created by extending the current environment with a closure. That closure's body is expr1, and the closure's environment starts with a binding for itself. The result of evaluating expr2 in its environment is the result of the entire expression.In other words, the above expression is analogous to a Scheme expression
(letrec ([F (lambda (x) expr1)])
expr2)
or, in the book's language,
letrec F = proc(x)expr1
in expr2
except, of course, that the variables F and x are not named. Instead. lexical numbers are used to access the variable bindings.Note that a curly brace deepens lexical variable bindings, just like a square bracket.- {[42], @0} => (a-closure (recursively-extended-env-rec (lit-exp 42) (empty-env-rec)) (lit-exp 42))
- {[42], call(@0, 43)} => 42
- {[@0], call(@0, 43)} => 43
- {[@1], call(@0, 43)} => (a-closure (recursively-extended-env-rec (lookup-exp 1) (empty-env-rec)) (lookup-exp 1))
- call([{[42], @1}], 43) => 43
- call([{[42], call(@0, @1)}], 43) => 42
- {[ifzero(@0,0,+(@0,call(@1,-(@0,1))))],call(@0,10)} => 55
Implement the new form with the following steps:- Extend the language's grammar with the new expression form, letrec-exp.
- Extend the environment implementation to handle recursive bindings:
- Add a new variant to the environment datatype called recursively-extended-env-rec.
- Define extend-env-recursively.
- Update apply-env to handle the new variant.
- Update eval-expression to implement the new expression form using extend-env-recursively.
Last update: Tuesday, October 3rd, 2000mflatt@cs.utah.edu |