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 initial dsub interpreter code as a starting point.

  2. In the file, fill in the definitions of the following two functions on environments:

  3. Evaluate the following expression in the current interp (which uses interp-d). What answer do you get? What answer should you get since Curly uses static scoping?

        (make-call 
          (make-proc 'x
    		 (make-call
                       (make-proc 'f
                                  (make-call
                                    (make-proc 'x 
                                               (make-call 'f 10))
                                    5))
                       (make-proc 'y (make-add 'x 'y)))))
         3)
    
  4. Create a data definition for closures (a proc plus an environment).

  5. Copy interp-d (to a new file) and modify the copy to return a closure in the proc? case. The closure should contain the proc and the current environment.

  6. Modify the call? case to work with a closure rather than a proc as the result of interpreting call-func.

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

    Everybody should be able to finish up to this point. If you don't get to the last question, think about it outside of lab.

  8. What's the moral of this lab? What are closures and why are they important?