Page 0 of 10 (0-9)


		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:____________________________________________


PLEASE 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 Points  Grade   Grader            Grader's Comments:
--------------------------------
| 1    |  25  |      |         |
--------------------------------
| 2    |  15  |      |         |
--------------------------------
| 3    |  30  |      |         |
--------------------------------
| 4    |  15  |      |         |
--------------------------------
| 5    |  15  |      |         |
--------------------------------
|Total |      |      |         |
--------------------------------
                                                      Page 1

Problem 1. 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 structure of the stream.  The HEAD of a
stream is just its CAR, as in Scheme.  But the TAIL of a stream,
instead of always being a delayed object as in Scheme, is either a
pointer to the rest of the stream, or a procedure of no arguments to
compute the rest of the stream.  (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 mini-evaluator:

(define (head x) (car x))

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

(define (eval-cons-stream exp env)
  (mini-eval <smudge>                          ;*B*
             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 "*B*" in the definition of
EVAL-CONS-STREAM has been smudged.  Write an appropriate expression to
complete the definition in the space provided below:

<smudge>:





Name:______________________________________            Page 2

Problem 1 (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)))))


B. 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.











C. 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.







D. Suppose we deleted the line marked "*A*" 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.

                                                      Page 3

Problem 2. 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 of 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 4

Problem 3. 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, during evaluation of the following expression in the global
environment:

          ((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, complete the definition of the
procedure COST-OF-ENVIRONMENT.  When given an environment constructed
by the interpreter in the handout, COST-OF-ENVIRONMENT should return
the number of CONS cells that had to be made to construct that
environment (including all enclosing environments).  Be careful: Do
not count any cells that may have been made to construct the values of
the variables bound in the environment.

(define (cost-of-environment env)







                                                      Page 5

Problem 3 (continued).

Ben Bitdiddle suggests that it is advantageous to represent
environments differently from what was 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 implementation 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 values 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, during evaluation of the following
expression in the global environment:

          ((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 6

Problem 3 (continued).

D. To understand how the cost of evaluation of expressions can be
affected by the representation of environment structures, it is
instructive to consider the cost of application of a procedure with
different representations of environments.  In this problem we will
measure the cost in terms of the number of CONS cells that are made
in the process of evaluating expressions.

In general, this cost depends upon the number of arguments to be bound
to formal parameters of the procedure.  Assume we have already defined
the following simple procedures:

     (define arg0 (lambda () 1))

     (define arg1 (lambda (x) 1))

     (define arg2 (lambda (x y) 1))

Compare Ben's representation of the environment with the one supplied
in the handout by answering the following questions.  Be careful,
these answers cannot simply be deduced from the diagrams you have
drawn in parts A and C above.  If you wish, you may provide a brief
explanation of your answers.

  1. How many CONS cells are made in the process of evaluation of the
     expression (ARG0)?

      With the representation in the handout: ___________

		   With Ben's representation: ___________


  2. How many CONS cells are made in the process of evaluation of the
     expression (ARG1 3)?

      With the representation in the handout: ___________

		   With Ben's representation: ___________


  3. How many CONS cells are made in the process of evaluation of the
     expression (ARG2 3 4)?

      With the representation in the handout: ___________

		   With Ben's representation: ___________


  Your explanation (optional):


Name:______________________________________            Page 7

Problem 4. 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.  Circle the number(s) of the correct completion(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 .


B.  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:___________________


C.  Circle the number(s) of the correct completion(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 it is known that 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:___________________

                                                      Page 8

Problem 5.  Ben Bitdiddle suggests building some special-purpose
register-machine code to implement 2-RAIL-LOOKUP for use with his
environment structure (page 5 of this exam) in the explicit-control
evaluator.  The contract for the 2-RAIL-LOOKUP 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.

On the following page we provide a "coding form" for you to fill out
to implement Ben's suggestion.  The SCHEME code for 2-RAIL-LOOKUP is
repeated under the coding form, for your convenience.  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 need 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 keys (cdr (fetch keys)))
 2.     (assign val '())
 3.	(assign val false)              
 4.     (assign val true)
 5.     (assign val (car (fetch keys)))                
 6.     (assign val (cdr (fetch keys)))                
 7.     (assign val (cdr (fetch values)))              
 8.	(assign val (fetch val))              
 9.     (assign val (fetch values))
10.     (assign values (cdr (fetch values)))               
11.	(assign continue 2-rail-lookup)                         
12.	(assign continue base-case)
13.     (assign continue done)
14.     (assign continue eq-case)                      
15.     (branch (eq? (fetch key) (fetch val)) eq-case) 
16.	(branch (eq? (fetch keys) (fetch val)) eq-case)
17.     (branch (null? (fetch keys)) base-case)        
18.     (goto base-case)     
19.     (goto done)     
20.     (goto eq-case)                                 
21.     (goto 2-rail-lookup)                           
22.	(goto (fetch continue))                        
23.     (save continue)                                
24.     (restore continue)                             
25.     (save val)                                     
26.     (restore val)
27.     base-case    
28.     done
29.     eq-case      
30.     2-rail-lookup
Name:______________________________________            Page 9

Problem 5 (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. ___30________       ___________________________________

    b. ___________       ___________________________________

    c. ___________       ___________________________________

    d. ___________       ___________________________________

    e. ___________       ___________________________________

    f. ___________       ___________________________________

    g. ___________       ___________________________________

    h. ___________       ___________________________________

    i. ___________       ___________________________________

    j. ___________       ___________________________________

    k. ___________       ___________________________________

    l. ___________       ___________________________________

    m. ___________       ___________________________________

    n. ___________       ___________________________________

    o. ___________       ___________________________________

    p. ___________       ___________________________________

    q. ___________       ___________________________________

    r. ___________       ___________________________________

    s. ___________       ___________________________________

    t. ___________       ___________________________________

    u. ___________       ___________________________________

    v. ___________       ___________________________________

    w. ___________       ___________________________________


(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)))))
