Page 0 of 1 (0-11)


		MASSACHVSETTS INSTITVTE OF TECHNOLOGY

      Department of Electrical Engineering and Computer Science

      6.001 -- Structure and Interpretation of Computer Programs
		   Final Exam   Spring Term   1993

			      Open Book







Put your name here:_______________________________________________


Put your tutor's name here:_______________________________________


Recitation instructor:____________________________________________

Also, put your name at the top of each page.
If you have any comments about this exam, please write them here:










Please do not write below this line -- reserved for administrivia.
__________________________________________________________________


Problem  Grade   Grader          Grader's Comments:
-------------------------
| 1    |      |         |
-------------------------
| 2    |      |         |
-------------------------
| 3    |      |         |
-------------------------
| 4    |      |         |
-------------------------
| 5    |      |         |
-------------------------
| 6    |      |         |
-------------------------
|Total |      |         |
-------------------------
Name:______________________________________            Page 1

Problem 1. Let's warm up on mutable data: Suppose we make the
following definitions:

    (define x (list 'b))
    (define y (list (cons 'a x) x))

A. In the space provided below, draw a box-and-pointer diagram
representing the values of the variables x and y.  Be sure to label
the pointers from x and y in your diagram.





















Now the following sequence of expressions is evaluated:

(set-car! x 'c)
(set-cdr! (car y) (list 'c))
(set-car! x 'b)

B. Show, in the space provided below, the printed representation of
the value of y after executing the expression (set-car! x 'c).





C. Show, in the space provided below, the printed representation of
the value of y after executing the expression (set-cdr! (car y) (list 'c)).





D. Show, in the space provided below, the printed representation of
the value of y after executing the expression (set-car! x 'b).

Name:______________________________________            Page 2

Problem 2. It is proposed to add infinite streams to the language
implemented by the interpreter in the handout.  The stream operations
CONS-STREAM, HEAD, and TAIL are implemented by directly modifying the
interpreter as follows: CONS cells are used for the elements of the
stream.  The head of a stream is just its CAR, as in Scheme.  The tail
of a stream is either a pointer to the next element, or a procedure of
no arguments to compute that element.  This implementation works
correctly for infinite streams, but may not work if the TAIL of a
stream is not a stream, a situation not considered in this problem.

The HEAD and TAIL operations are implemented as Scheme procedures and
then added to the list of primitives imported into the interpreted
language.  But the CONS-STREAM operation has to be implemented as a
special form.  The following incomplete code is intended to be added
to the meta-circular description of the evaluator:

(define (head x) (car x))

(define (tail x)
  (let ((d (cdr x)))
    (if (mini-procedure? d)
	(let ((tl (mini-apply d '())))         ;*A*
	  (set-cdr! x tl)                      ;*B*
	  tl)
	d)))

(define (eval-cons-stream exp env)
  (mini-eval <smudge>                          ;*C*
             env))

(add-syntax! 'cons-stream eval-cons-stream)

(define primitive-procedure-names
  '(+ - * / = < > 1+ -1+ cons car cdr pair? eq? null? not
    user-write-line user-display user-load
    head tail))

A. Unfortunately the expression marked "*C*" in the definition of
EVAL-CONS-STREAM has been smudged.  Write an appropriate expression to
complete the definition in the space provided below:

<smudge>:







B. Louis Reasoner claims that the expression "(mini-apply d '())" in
the line marked "*A*" could be simplified to read "(d)".  Is Louis
correct?  Circle the correct answer, and explain, in a few well-chosen
sentences, why you believe your answer to be correct.

		    TRUE     FALSE
Name:______________________________________            Page 3

Problem 2 (continued).

Using the streams implemented, above, a user can write the following:

(define (map-2-streams proc s1 s2)
  (cons-stream (proc (head s1) (head s2))
	       (map-2-streams proc (tail s1) (tail s2))))

(define ones (cons-stream 1 ones))

(define natural-numbers
  (cons-stream 1 (map-2-streams + ones natural-numbers)))

(define factorials 
  (cons-stream 1 (map-2-streams * natural-numbers factorials)))

(define (stream-ref s n)
  (cond ((= n 0) (head s))
	(else (stream-ref (tail s) (- n 1)))))


C. Assume that we have evaluated (STREAM-REF ONES 3).  Draw a
box-and-pointer diagram representing the value of the variable ONES in
the space provided below.









D. How many calls to the multiplication procedure (the value of the
symbol "*") are made in obtaining the value (120) of the expression
(STREAM-REF FACTORIALS 5)?  Write your answer in the space provided
below.





E. Suppose we deleted the line marked "*B*" from our implementation,
and we then reexecuted the definitions of the streams ONES,
NATURAL-NUMBERS, and FACTORIALS.  How many calls to the multiplication
procedure (the value of the symbol "*") are made in obtaining the
value (120) of the expression (STREAM-REF FACTORIALS 5)?  Write your
answer in the space provided below.

Name:______________________________________            Page 4

Problem 3. One nice application of macros is to define the special
form FOR, which increments an INDEX from a LOW to a HIGH value,
executing a given expression for each value of the index.  For
example, to print out the values of (FACTORIAL 3) ... (FACTORIAL 7)
(assuming that FACTORIAL names a procedure to compute factorials) we
may write the following code

     (FOR i 3 7 (user-write-line (factorial i)))

Two possible implementations the FOR form as macros are provided
below.  Complete the definition of your choice.  You need only
complete one of the definitions:

Implementation A:

     (define-macro (for index low high exp)
       ((lambda (g) (g g low))
	(lambda (loop index)
	  (cond (<SMUDGE1> 'done)
		(else <SMUDGE2>
		      (loop <SMUDGE3> <SMUDGE4>))))))

Your completions for A.

   <SMUDGE1>:

   <SMUDGE2>:

   <SMUDGE3>:

   <SMUDGE4>:



Implementation B:

     (define-macro (for index low high exp)
       ((lambda ()
	  (define (loop index)
	    (cond (<MESS1> 'done)
		  (else <MESS2>
		        (<MESS3> <MESS4>))))
          (loop low)))

Your completions for B.

   <MESS1>:

   <MESS2>:

   <MESS3>:

   <MESS4>:

Name:______________________________________            Page 5

Problem 4. The first two parts of this problem are concerned with the
representation of environments given in the handout.  The remaining
parts are concerned with a modification suggested by Ben Bitdiddle.

A. In the space provided below draw a box-and-pointer diagram of the
value returned by the EXTEND-ENVIRONMENT procedure given in the
handout, when evaluating the expression:

          ((lambda (x y) (+ x (* x y))) 3 4)

You need not draw the structure of the global environment -- a pointer
to a label by that name will be sufficient.

Your answer:











B. In the space provided below, write a short procedure,
COST-OF-ENVIRONMENT, that, when given an environment constructed by
the interpreter in the handout, returns the number calls to CONS that
had to be made to construct that environment.  Be careful: The calls
to CONS we are talking about are exactly those that were made in the
call to EXTEND-ENVIRONMENT that constructed the given environment.  Do
not count the CONSs that are needed to represent the values of the
variables bound in the environment.








Name:______________________________________            Page 6

Problem 4 (continued).

Ben Bitdiddle suggests that it is advantageous to represent
environments differently than done in the evaluator in the handout.
He suggests that we could use 2-RAIL-LOOKUP as the way of looking up a
variable in a frame, rather than using ASSQ.  Ben's suggestion is to
replace the description of the frame data structure (on the last page
of the interpreter listing) with the following code:

(define (make-frame variables values)
  (cond ((not (= (length variables) (length values)))
	 (error "Wrong number of arguments supplied" 
                variables values))
        (else (cons variables values))))

(define (adjoin-binding var val frame)
  (cons (cons var (car frame))
	(cons val (cdr frame))))

(define (binding-in-frame var frame)
  (2-rail-lookup var (car frame) (cdr frame)))

(define (2-rail-lookup key keys values)
  (cond ((null? keys) false)
	((eq? key (car keys)) values)
	(else (2-rail-lookup key (cdr keys) (cdr values)))))

(define (found-binding? b)
  (not (eq? b no-binding)))

(define no-binding false)

(define (binding-value binding)
  (car binding))

(define (set-binding-value! binding value)
  (set-car! binding value))


C. In the space provided below draw a box-and-pointer diagram of the
value returned by the EXTEND-ENVIRONMENT procedure using the new
implementation of frames, when evaluating the expression:

          ((lambda (x y) (+ x (* x y))) 3 4)

As in part A you need not draw the structure of the global environment
-- a pointer to a label by that name will be sufficient.

Your answer:

Name:______________________________________            Page 7

Problem 4 (continued).

D. In comparing Ben's representation with the one supplied in the
handout one of the following sentences is true.  Circle the number
next to the true sentence.

   1. Ben's representation always takes fewer CONS operations to
      construct the environment than the representation in the 
      handout takes.

   2. Ben's representation sometimes takes fewer CONS operations to
      construct the environment than the representation in the 
      handout takes, but never takes more.

   3. Ben's representation always takes the same number of CONS 
      operations to construct the environment as the representation 
      in the handout takes.

   4. The representation in the handout sometimes takes fewer CONS 
      operations to construct the environment than Ben's
      representation takes, but never takes more.

   5. The representation in the handout always takes fewer CONS
      operations to construct the environment than Ben's
      representation takes.


Explain your answer to the question in one or two well-chosen
sentences.  Illustrate your explanation with examples.

Your answer: 

Name:______________________________________            Page 8

Problem 5. This question concerns the compilation of the SUBSTITUTE
procedure provided in the handout.  You may assume that the compiler
is the same as the one provided in the notes (and used in PS9).

A.  In the space provided below write the name of the procedure that
    is called by executing the GOTO instruction in line 87 of the
    compilation listing.

    The procedure is named:___________________


B.  Circle the number(s) of the correct continuation(s) of the
    following sentence fragment:

       The label AFTER-LAMBDA12 on line 27 of the listing is

       1.  "dead code" that cannot be reached.  It should have been 
	   removed by the optimizer.

       2.  jumped to exactly once, when the procedure SUBSTITUTE is
	   defined.

       3.  jumped to each time the procedure SUBSTITUTE is entered.

       4.  jumped to each time the procedure TAKE-VALUE-IF-AVAILABLE 
	   is entered .


C.  Circle the number(s) of the correct continuation(s) of the
    following sentence fragment:

       The code in lines 75-84 of the listing

       1. implements the expression (CAR TEMPLATE).

       2. can always be replaced by the following three lines:

	      (assign val (lookup-variable-value 'template (fetch env)))
	      (assign val (car (fetch val)))
	      (assign argl (cons (fetch val) '()))

       3. can be replaced by the following three lines

	      (assign val (lookup-variable-value 'template (fetch env)))
	      (assign val (car (fetch val)))
	      (assign argl (cons (fetch val) '()))

	  only if the value of CAR is never changed.


D.  The argument list constructed in line 108 by the instruction
    (ASSIGN ARGL (CONS (FETCH VAL) (FETCH ARGL))) is being prepared
    for a particular procedure.  In the space provided below write the
    name of that procedure.

    The procedure is named:___________________

Name:______________________________________            Page 9

Problem 5 (continued).

E.  We see that the compiled code contains no subsequence of the form:

		     ...
		     (save env)
		     (restore env)
		     ...

    Circle the number(s) of the correct statement(s) below:

    1.  The compiler will never emit such a subsequence for the ENV
        register because it is never needed, but it will emit such
        a subsequence for other registers.

    2.  The particular SUBSTITUTE procedure compiled does not require
        such a subsequence, but the compiler will emit such a
        subsequence under other conditions.

    3.  The compiler will never emit such a subsequence for any
        register because such a subsequence is never needed.

Write a short explanation of your answer in the space provided below:









F.  We see that the compiled code contains no subsequence of the form:

		     ...
		     (restore env)
		     (save env)
		     ...

    Circle the number(s) of the correct statement(s) below:

    1.  The compiler will never emit such a subsequence for the ENV
        register because it is never needed, but it will emit such
        a subsequence for other registers.

    2.  The particular SUBSTITUTE procedure compiled does not require
        such a subsequence, but the compiler will emit such a
        subsequence under other conditions.

    3.  The compiler will never emit such a subsequence for any
        register because such a subsequence is never needed.

Write a short explanation of your answer in the space provided below:


Name:______________________________________            Page 10

Problem 6. Users of the interpreter are still unhappy with its
performance.  Ben Bitdiddle suggests that it would be effective to
build some special-purpose register machine code to implement the
2-RAIL-LOOKUP subroutine for use with his environment structure in the
explicit-control evaluator.  The contract for this subroutine is as
follows: When the interpreter needs to do a 2-RAIL-LOOKUP it will set
up the arguments in new registers KEY, KEYS, and VALUES, and it will
put the place to return to in CONTINUE.  It will then jump to the
label 2-RAIL-LOOKUP.  When the subroutine finishes it should put its
result in the register VAL and jump to the label passed in the
CONTINUE register.  The subroutine should leave all other parts of the
interpreter state unchanged.  To make this easier we provide you with
a private extra register, TEMP.

On the following page we provide a "coding form" for you to fill out
to implement Ben's suggestion.  You are to select instructions and
labels from the list on this page.  Indicate your answer by putting
the number of the instruction you select in the appropriate place in
the coding form.

You should not write out any instructions (all such written
instructions will be ignored by the graders); rather enter instruction
numbers in the lettered space in the form.  There are more than enough
candidate instructions to choose from in the list below, and more than
enough places for instructions in the form.  You need not use all of
the lines in the form.

Number  Instruction

 1.     (assign values (cdr (fetch values)))
 2.     (assign val '())
 3.     (assign val (fetch values))
 4.     (assign val (fetch temp))
 5.     (assign val false)
 6.     (assign keys (cdr (fetch keys)))
 7.     (assign temp '())
 8.     (assign temp (car (fetch keys)))
 9.	(assign temp (cdr (fetch keys)))
10.	(assign temp (cdr (fetch values)))
11.	(assign temp false)
12.     (assign continue loop)
13.     (assign continue base-case)
14.	(assign continue eq-case)
15.     (branch (eq? (fetch key) (fetch temp)) eq-case)
16.     (branch (eq? (fetch keys) (fetch temp)) eq-case)
17.     (branch (null? (fetch keys)) base-case)
18.     (goto base-case)
19.	(goto eq-case)
20.     (goto loop)
21.     (goto (fetch continue))
22.     (save continue)
23.     (restore continue)
24.     (save val)
25.     (restore val)
26.     (save temp)
27.     (restore temp)
28.     loop
29.     base-case
30.     eq-case
31.     2-rail-lookup
Name:______________________________________            Page 11

Problem 6 (continued).

Here is the coding form for you to fill out.  We give you the first
answer by indicating that the first line is the label 2-RAIL-LOOKUP
for entry to the subroutine.

      Your answers                 Scratch work (not graded)

    a. ___31________       ___________________________________

    b. ___________       ___________________________________

    c. ___________       ___________________________________

    d. ___________       ___________________________________

    e. ___________       ___________________________________

    f. ___________       ___________________________________

    g. ___________       ___________________________________

    h. ___________       ___________________________________

    i. ___________       ___________________________________

    j. ___________       ___________________________________

    k. ___________       ___________________________________

    l. ___________       ___________________________________

    m. ___________       ___________________________________

    n. ___________       ___________________________________

    o. ___________       ___________________________________

    p. ___________       ___________________________________

    q. ___________       ___________________________________

    r. ___________       ___________________________________

    s. ___________       ___________________________________

    t. ___________       ___________________________________

    u. ___________       ___________________________________

    v. ___________       ___________________________________

    w. ___________       ___________________________________

    x. ___________       ___________________________________

    y. ___________       ___________________________________

    z. ___________       ___________________________________
