CS 2135: Lab 4


Lab Motivation and Goals

This lab is designed to help you understand interpreters, environments (delayed substitutions), and closures (statically-scoped procedures).


Exercises: The Interpreter with Environments and Closures

In class, we talked about delayed substitutions (environments) and closures (procedures grouped with their environments). Your goal in this lab is to convert the interpreter with subst into one that uses environments and closures.

  1. Download the current interpreter code as a starting point.

  2. Create a data definition for dsub. We will use the term environment for list[dsub].

  3. Write the following three functions on environments:

  4. Modify eval to take an environment as an argument. Use the environment to lookup values of variables (but leave the proc? case unchanged).

  5. Evaluate the following expression in your current eval. What answer do you get? What answer should you get since Curly uses static scoping?

        (make-apply 
          (make-proc 'x
    		 (make-apply 
                       (make-proc 'f
                                  (make-apply 
                                    (make-proc 'x 
                                               (make-apply (make-var 'f) 10))
                                    5))
                       (make-proc 'y (make-plus (make-var 'x) (make-var 'y)))))
         3)
    
  6. Create a data definition for closures (a proc plus an environment).

  7. Copy eval and modify the copy to return a closure in the proc? case. The closure should contain the proc and the current environment.

  8. Modify the do-app helper function to take a closure rather than a proc as an argument.

  9. Evaluate the expression above in your modified interpreter. Do you now get different answers from the old and new evals? Does your new eval return the same answer as the subst version of eval?