Programming Language Concepts
Tuesdays, Thursdays 14:00-15:20
University Crossings 151
In this assignment, you will modify the metacircular interpreter we saw in class. Successfully completing this assignment requires reading and understanding a medium-sized program written by someone else. If you do not have a good understanding of how the interpreter works, please review the material covered in lecture and in the book. Diving straight in to the homework without this understanding is going to make your task much more difficult.
Your solution must be based on the metacircular interpreter from SICP, available here: ch4-mceval.scm.
This assignment is worth 125 points. There are 136 possible points.
Please submit all files for this assignment by checking them in to your cs360
git repository at
~/cs360/git/hw3. Be sure to commit your work to the
A complete submission will include two files:
Your code must run on
Code that is not valid Scheme will not be graded and will receive a score of zero.
Note: Problem 1 is due in week 1, separately from problems 2–7. However, it is recommended that you complete at least problems 1–3 the first week, and preferably problems 1–4.
Start by saving a copy of the SICP metacircular interpreter as
hw3 folder. Commit your work periodically as you complete the problems. I
recommend commiting the
mceval.scm from SICP as soon as you download it so
that you can use
git diff to easily see the changes you have made.
Begin by uncommenting the definition of
the-global-environment at the end of
the file. You can start
mit-scheme with your metacircular interpreter loaded
by executing the following command at the prompt (the prompt below is assumed to
$ mit-scheme --load mceval.scm
There are then two ways you can test the evaluator:
(driver-loop) from the
mit-scheme REPL. The driver loop will
read in Scheme expressions and pass them to your interpreter for evaluation.
eval function with an expression and a global environment from
mit-scheme REPL, like this:
I used the second approach. I also made judicious use of
to debug my implementation.
There are three ways to extend the interpreter:
You should only add a special form when it is absolutely necessary. Most of the
time, the standard Scheme evaluation rules are exactly what you want. Solving a
problem by adding a definition rather than a new special form is also much
easier and avoids cluttering up your
newline to print out intermediate expressions! This is
extremely helpful when debugging.
Submit the solutions to this problem only in a file named
problem1.txt in the
hw3 subdirectory of your git repository.
In the previous homework, you implemented environments as a list of frames, where each frame was a list of bindings, and a binding was a list of two elements, a symbol and its value.
The metacircular interpreter uses a different representation. What is it? Please be specific—an English description such as that given above for the Homework 2 representation will suffice. However, your answer will be stronger if you also provide examples.
define contains the list of primitives supported by the
metacircular interpreter? Please name the variable.
This is Exercise 4.14 from SICP.
Eva Lu Ator and Louis Reasoner are each experimenting with the metacircular
evaluator. Eva types in the definition of
map, and runs some test programs
that use it. They work fine. Louis, in contrast, has installed the system
map as a primitive for the metacircular evaluator. When he tries
it, things go terribly wrong. Explain why Louis’s
map fails even though Eva’s
This is Exercise 4.2a from SICP.
Louis Reasoner plans to reorder the
cond clauses in
eval so that the clause
for procedure applications appears before the clause for assignments. He argues
that this will make the interpreter more efficient: Since programs usually
contain more applications than assignments, definitions, and so on, his modified
eval will usually check fewer clauses than the original
identifying the type of an expression.
What is wrong with Louis’s plan? (Hint: What will Louis’s evaluator do with
(define x 3)?)
setup-environment is used to create the initial global
environment used by the metacircular interpreter. For later problems, it will be
convenient to add your own definitions to the initial global environment. The
most convenient way to do this is to call
eval-definition with the appropriate
arguments from within the function
setup-environment. If you were to add a
definition in this manner, what arguments would you pass to
add the following top-level
define to the initial global environment? You may
give your answer in the form of a Scheme expression.
Add the following primitives:
error. 1 point each.
error primitive should take no arguments and abort the interpreter with
the message “Metacircular Interpreter Aborted” (without the quotes).
or(20 points total)
Add support for
or to your interpreter (10 points each). Be sure
your implementation adheres to the Scheme language standard (see
in terms of how the arguments to
or are evaluated and in terms of
what value is returned.
You will probably want to use the
Remember that the metacircular interpreter cannot interpret
let(20 points total)
This is Exercise 4.6 from SICP.
Let expressions are derived expressions, because
is equivalent to
Implement a syntactic transformation
let->combination that reduces evaluating
let expressions to evaluating combinations of the type shown above, and add the
appropriate clause to
eval to handle let expressions.
display to print out the result of
let->combination to make sure
you get it right! I did not get it right the first time…
delay(40 points total)
Add support for
delay to your interpreter, where
expressions are only evaluated once when
It is recommended that you implement the call-by-name version first, since this is much easier. You will receive 20 points for a fully correct call-by-name implementation.
For full credit, you must support call-by-need evaluation, which only evaluates
delay‘ed expressions once.
There are two ways to solve this problem, both of which we covered in class.
You can implement
delay in such a way that it will memoize the function that
wraps the delayed expression.
This approach adopts some of the techniques from the lazy evaluator we saw in
class for use in your evaluator. You may use code from the lazy evaluator,
which you can find here. Note
that you cannot simply copy the lazy interpreter to implement the call-by-need
force—that would make the entire language
call-by-need! Your interpreter must implement applicative order evaluation.
You will want to make
force a special form, and you will want to introduce the
evaluated-thunk tags and some of the associated machinery that are
used in the lazy evaluator. Since thunks, whether or not they are evaluated, can
only legally appear as arguments to
force, you should only have to modify a
small part of the interpreter, in contrast to the more significant surgery
needed for general call-by-need.
Be sure you use
set-cdr! as needed to turn a
thunk into an
evaluated-thunk once it has been evaluated by
Add support for the following stream functions to your interpreter:
You should be able to complete this problem with either the call-by-name or
call-by-need implementation of
delay. That is, you can receive
full credit for this problem even if you did not receive full credit for Problem
Hint: the hints given for Problem 5 apply to this problem!
How long did it take you to complete problems 2–6? Please tell us in a comment
mceval.scm. You must tell us how long each problem took you to receive the