% for yTeX
\typesize=11pt
\hsize=34pc
\vsize=50pc
\parskip 6pt plus 2pt

\def\psetheader{
\centerline{MASSACHUSETTS INSTITUTE OF TECHNOLOGY}
\centerline{Department of Electrical Engineering and Computer Science}
\centerline{6.001 Structure and Interpretation of Computer Programs}
\centerline{Fall Semester, 1986}}

\def\code#1{\beginlisp
#1
\endlisp

\vskip .1in}

\rectoleftheader={6.001 -- Fall Semester 1986}
\rectorightheader={Problem Set 8}
\onheaders
\onfooters

\null
\vskip 1truein

\psetheader

\vskip .25truein

\centerline{Problem Set 8}

\vskip 0.25truein

\vpar
Issued: 4 November 1986


\vpar
Due: 19 November 1986

\vpar
Reading:

\beginbullets
\bpar
From text: sections 4.1 through 4.4

\bpar
Programs: {\tt ps8-mceval} (attached).

\endbullets

{\bf Note:} This is a 2-week problem set, partially because of the quiz
coming up and partially because of the difficulty of the problem set
itself.  This problem set asks you to modify the Lisp evaluator to allow for
procedures that pass parameters in a ``call-by-name'' style.  The
necessary modification is outlined in section 4.2.1 of the
text.  Although this modification does not require writing a great
deal of code, you will need to have a good understanding of the
evaluator in order to do this problem set.  Read the beginning of
chapter 4 and plan your work carefully before coming to the lab.  
Also note that the style of the problem set is somewhat different from
earlier ones.  In particular, this problem set concentrates on issues of
design rather than on explicit detailed questions.  This will require
some careful thinking on your part.

{\bf Quiz announcement:} Quiz 2 is on Wednesday, November 19.
The quiz will be held in Walker Memorial Gymnasium (50-340) from 5-7PM
or 7-9PM.  You may take the quiz during either one of these two
periods, but students taking the quiz during the first period will not
be allowed to leave the room until the end of the period.  The quiz is
open book, and will cover material from the beginning of the semester through the
material presented in recitation on November 7, and through section
4.3 of the textbook. 



\chapter{1. Textbook exercises}

Write up and turn in the following exercises from chapter 4 of
the text:

\beginbullets

\bpar
Exercise 4.1 -- page 299

\bpar
Exercise 4.16 -- page 323

\bpar
Exercise 4.17 -- page  324

\endbullets

\chapter{2. Pre-lab assignment}

The following problem should be done before beginning work on the lab
assignment: 

The interpreter given in Section 4.1, unlike the full Scheme
interpreter, does not have an {\tt if} construct, only a {\tt cond}.
Louis Reasoner claims that in fact {\tt if} is unnecessary, since we
could define it as an ordinary Lisp procedure:

\beginlisp
(define (if predicate action alternative)
  (cond (predicate action)
        (else alternative)))
\endlisp

Explain why this does not work.  In particular, suppose you use this {\tt if}
procedure to define {\tt factorial}:

\beginlisp
(define (factorial n)
  (if (= n 1)
      1
      (* n (factorial (- n 1)))))
\endlisp

What happens when you attempt to evaluate {\tt (factorial 3)} ?  Why. 

We realize that you have seen this problem before, but we want you to
be thinking about this issue before going on to the rest of the
assignment.  Note also that you cannot try out the above definitions
in an ordinary Scheme, since Scheme's own definition of {\tt if} will
interfere with the one you attempt to define.  (Ordinarily, there is
nothing to stop you from re-defining Scheme primitives, but {\tt if} is
implemented in Scheme as a special form.)

\chapter{2. Programming assignment: Delayed Evaluation}

The file {\tt ps8-mceval} contains a copy of a simple Scheme interpreter
written in Scheme, essentially the same as the one in Section 4.1.
There are a few minor differences, of which the most important are:

\beginbullets
\bpar
The procedures {\tt eval} and {\tt apply} have been renamed {\tt mini-eval} and
{\tt mini-apply}, so as not to interfere with Scheme's own {\tt eval} and
{\tt apply} operators.

\bpar
The interface to the underlying Scheme system via
{\tt apply-primitive-procedure} is handled somewhat differently than in
the book.
\endbullets

See the attached code for details.

If you load this file into Scheme and type {\tt (initialize-evaluator)},
you will find yourself typing at the driver loop.  Please note that
this Scheme running in Scheme contains no error system of its own.  If
you hit an error or type {\tt ctrl-G}, you will bounce back into Scheme.
Also, you must type in procedure definitions directly to the driver
loop, since there is no interface to the editor.\footnote{$^1$}{However,
you can use
the editor to write an ordinary Scheme procedure that calls
{\tt mini-eval} in order to predefine various things in the global
environment.  Run {\tt initialize-evaluator}, then type {\tt ctrl-G} to
get back to Scheme, run your procedure to predefine things, then
re-enter the interpreter by typing {\tt (driver-loop)}.}

In order to help you keep from becoming confused, the driver loop uses
the prompt {\tt MC-EVAL==>} instead of the ordinary Scheme prompt.

Start up the interpreter and try a few simple expressions.  If you
bounce out into Scheme, you can re-enter the interpreter by typing
{\tt (driver-loop)}.  If you get hopelessly fouled up, you can run
{\tt initialize-evaluator}, but this initializes the global environment,
and you will lose any definitions you have made.

Also, it is instructive to run the interpreter while tracing
{\tt mini-eval} and/or {\tt mini-apply}, to see just how the evaluator
works.  (You will also probably need to do this while debugging your
code for this assignment.)  If you do trace these procedures, you may
want to use the Scheme procedures {\tt print-breadth} and
{\tt print-depth} to avoid printing huge listings of the {\tt env}
argument to these procedures (printed by the tracer), which is in
general a circular structure.  See the Chipmunk manual for information
on the use of {\tt print-breadth} and {\tt print-depth}.

For this assignment, you will need to modify a few of the procedures
in the interpreter, and also write a few new procedures.  To help you,
{\tt ps8-mods.scm} contains the procedures that you will need to modify.
You can load this file into an editor buffer, which you can
edit while doing this assignment.  The entire {\tt ps8-mceval} file
can be {\tt load}ed directly into Scheme without your worrying about editing
it.



\section{Lab problem 1}
Section 4.2.1 of the text sketches the modification you are to make
to the evaluator.  Filling in the details will require modifying a few
of the procedures already given, and also writing new procedures (for
example, to handle thunks).  Be sure to test your implementation.  For
all of these lab problems, turn in listings of the procedures that you
write and modify.

As part of your testing process, try the following example from p. 316
of the text:

\beginlisp
MC-EVAL==> (define (try (delayed a) (delayed b))
             (cond ((= a 0) 1)
                   (else b)))

MC-EVAL==> (try 0 (/ 1 0))
1
\endlisp

Two more hints in designing the implementation:

\beginbullets
\bpar
Be sure to tell {\tt user-print}  about thunks, so that if it is
passed a thunk to print, it will first undelay it.

\bpar
Try the example in Exercise 4.10 of the text, which
illustrates a easily-made bug in the undelaying strategy.

\endbullets

\section{Lab problem 2}
When you think your implementation is working, try defining {\tt if} as
a procedure, as shown on page 317 of the text.  Use {\tt if} in
defining {\tt factorial}, as in the pre-lab exercise.  This definition
should work.  Also, try Exercise 4.9 of the text. 

\section{Lab problem 3}
Do exercise 4.11 of the text.

Test your implementation by trying the following examples:

\beginlisp
MC-EVAL==> (define (integers-from n)
             (cons n (integers-from (+ n 1)))

MC-EVAL==> (define integers (integers-from 1))

MC-EVAL==> (car integers)
1

MC-EVAL==> (car (cdr integers))
2
\endlisp

\section{Lab problem 4}
Do Exercise 4.12 on page 319 of the text.  Only, rather than just
describing the modification in detail, actually implement this in your
interpreter. 

\section{Lab problem 5}
Design and carry out an experiment to test whether the modification
you made in lab problem 4 is working correctly.  Show the experiment,
saying how the evaluator should respond both before and after the
modification in lab problem 4.  (Hint: Think about programs that use
{\tt set!}.  Also, look at Exercise 3.44 of the text.)  Write a brief
paragraph of explanation.  (Note: illegible or incoherent paragraphs
will be graded unsympathetically, if graded at all.)

\end
