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
		     Quiz 2   Spring Term   1993

			      Open Book


		      Report to Provost Writeoff

The Provost has posed a number of questions to help him understand the
model.  He has kindly provided this structured notebook in which we
are required to respond.  Please write clear and concise answers to
the questions posed in the spaces provided in this booklet.  You may
use extra paper if needed, but the spaces we provide are surely big
enough for the simple answers that the President is looking for.  You
must be brief but clear -- the people in the Administration have
enough problems to worry about these days without having to dig your
pearls of wisdom out of a rubble of disorganized thoughts.  We will
certainly give you no credit for an answer that Mr. Writeoff's staff
cannot understand.


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 quiz, 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    |      |         |
-------------------------
|Total |      |         |
-------------------------
Name:______________________________________            Page 1

Problem 1.  Ben Bitdiddle says that it is possible to reformulate the
entire simulator code as a stream process.  One can represent the time
progression of the occupancy of a room as a stream of lists of the
occupants of the room at successive times.  This stream is produced from
a stream of events representing people entering and leaving the room.
For example, if the room is initially empty and the stream of events is

(enter gjs lou) (enter psz) (leave lou psz) (enter bkph joe) (leave gjs) ...

then the stream of occupancies (lists of occupants) will be:

() (gjs lou) (gjs lou psz) (gjs) (gjs bkph joe) (bkph joe) ...


Given an event and a list of occupants Ben produces a new list of
occupants with the following procedure:

(define (process-an-event event occupants)
  (cond ((eq? (car event) 'enter)
	 (list-union occupants (cdr event)))
	((eq? (car event) 'leave)
	 (list-difference occupants (cdr event)))
	(else (error "Illegal Command"))))


Using PROCESS-AN-EVENT, it is possible to define a new procedure,
PROCESS-EVENTS, that takes an initial list of occupants and a stream of
events and produces the required stream of lists of occupants.  Assume
that the stream of events is the value of the variable EVENT-STREAM.  The
stream of lists of occupants is the value of the expression

		  (process-events '() event-stream)

where process-events is incompletely defined as follows:

(define (process-events occupants events)
  (cons-stream <A> (process-events <B> <C>)))


In the spaces provided below give expressions that complete the above
definition:


<A>:



<B>:



<C>:



Name:______________________________________            Page 2

Problem 1 (continued).


Another way of producing the stream of occupancies (lists of occupants)
makes use of delayed evaluation to define the stream implicitly.  If we
define MAP-2-STREAMS as follows:

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

We may use it to make the required stream-of-occupancies by the following
incomplete definition:

(define stream-of-occupancies
  (cons-stream <D>
    (map-2-streams <E>
		   event-stream
		   <F>)))



In the spaces provided below give expressions that complete the given
definition:


<D>:



<E>:



<F>:



Name:______________________________________            Page 3

Problem 2. The following sequence of commands will make a room and an
occupant for that room.

(define gjs-office (make-place "NE43-408"))
(define gjs (make-person "Gerald Jay Sussman" gjs-office))

Creating the person will cause an I-AM-ARRIVING message to be sent to the
room.  The incomplete environment diagram shown below describes the
situation in the execution of the I-AM-ARRIVING method where the MEMQ
procedure is called.  There are eight pointers that are incomplete.  The
stubs are labeled with a letter and a question mark (as in "Z?").  Such a
pointer may point to a procedure or to an environment frame.  Procedures
are labeled Pn and environment frames are labeled En (as in "P2" or
"E3").  Complete the diagram by extending the pointer stubs to the
correct places.  For each stub, identify the procedure or the environment
pointed to by the stub.  Summarize your answers by completing the table
at the bottom of the page.  Only the table will be graded.  An extra copy
of the environment diagram, for scratch work, is given on the facing
page.



































Your Answers:	S:______  T:______  U:______  V:______


		W:______  X:______  Y:______  Z:______

                                                       Page 4
Name:______________________________________            Page 5

Problem 3.  The inefficiency experts want to model the fact that MIT
people are gregarious -- when one meets a colleague the two may travel
together to the next destination.  To model this we may add a method to
MAKE-PERSON, so that when a modeled person is sent a GO-TO-GREGARIOUSLY
message the modeled person tries to find another person to take along.
This may be implemented by adding a clause to the message-dispatch
conditional in MAKE-PERSON as follows:

((eq? message 'go-to-gregariously)
 (lambda (sender new-place)
   (let ((potentials (delq me (send place 'who-is-here? me))))
     (if (null? potentials)
	 (send me 'go-to sender new-place)
	 (let ((fellow-traveller (choose-from potentials)))
	   (say-to-all 'good-bye "good bye" (list me fellow-traveller))
           (write-line 
             (list name 'says 'come-along 
                   (send fellow-traveller 'name? me)))
	   (send place 'i-am-leaving me)                              ;*
	   (send fellow-traveller 'go-to me new-place)                ;*
	   (send new-place 'i-am-arriving me)                         ;*
	   (set! place new-place)
	   (say-to-all 'hello "hello" (list me fellow-traveller)))))))

The lines marked with "*" in the code above may be arranged in various
orders.  For each of the possible orders (A-F below) there is a segment
of a transcript of a run of that code (1-6 on the facing page).  You are
to fill in the table below.  Write the number of the transcript segment
next to the letter of the corresponding code-order fragment.

Your answers:


    A:_____    B:_____    C:_____    D:_____    E:_____    F:_____



The code orders are as follows:    

Order A. (send fellow-traveller 'go-to me new-place)
	 (send place 'i-am-leaving me)
	 (send new-place 'i-am-arriving me)

Order B. (send place 'i-am-leaving me)
	 (send fellow-traveller 'go-to me new-place)
	 (send new-place 'i-am-arriving me)

Order C. (send place 'i-am-leaving me)
	 (send new-place 'i-am-arriving me)
	 (send fellow-traveller 'go-to me new-place)

Order D. (send new-place 'i-am-arriving me)
	 (send fellow-traveller 'go-to me new-place)
	 (send place 'i-am-leaving me)

Order E. (send fellow-traveller 'go-to me new-place)
	 (send new-place 'i-am-arriving me)
	 (send place 'i-am-leaving me)

Order F. (send new-place 'i-am-arriving me)
	 (send place 'i-am-leaving me)
	 (send fellow-traveller 'go-to me new-place)
Name:______________________________________            Page 6

Problem 3 (continued).

Transcript 1.
    ("Joseph E. Stoy" says come-along "Peter Szolovits")
    Peter Szolovits says good bye to Joseph E. Stoy 
    ("Peter Szolovits" leaving "10-250")
    ("Peter Szolovits" arriving "NE43-408")
    ("Joseph E. Stoy" arriving "NE43-408")
    ("Joseph E. Stoy" leaving "10-250")

Transcript 2.
    ("Gerald Jay Sussman" says come-along "Louis D. Braida")
    ("Gerald Jay Sussman" leaving "10-250")
    ("Louis D. Braida" leaving "10-250")
    ("Louis D. Braida" arriving "NE43-210")
    ("Gerald Jay Sussman" arriving "NE43-210")

Transcript 3.
    ("Gerald Jay Sussman" says come-along "Peter Szolovits")
    Peter Szolovits says good bye to Gerald Jay Sussman 
    ("Peter Szolovits" leaving "10-250")
    ("Peter Szolovits" arriving "NE43-210")
    ("Gerald Jay Sussman" leaving "10-250")
    ("Gerald Jay Sussman" arriving "NE43-210")

Transcript 4.
    ("Joseph E. Stoy" says come-along "Louis D. Braida")
    ("Joseph E. Stoy" leaving "10-250")
    ("Joseph E. Stoy" arriving "NE43-210")
    ("Louis D. Braida" leaving "10-250")
    ("Louis D. Braida" arriving "NE43-210")
    Louis D. Braida says hello to Joseph E. Stoy 

Transcript 5.
    ("Joseph E. Stoy" says come-along "Louis D. Braida")
    ("Joseph E. Stoy" arriving "NE43-408")
    Louis D. Braida says good bye to Joseph E. Stoy 
    ("Louis D. Braida" leaving "10-250")
    ("Louis D. Braida" arriving "NE43-408")
    Louis D. Braida says hello to Joseph E. Stoy 
    ("Joseph E. Stoy" leaving "10-250")

Transcript 6.
    ("Peter Szolovits" says come-along "Gerald Jay Sussman")
    ("Peter Szolovits" arriving "10-250")
    ("Peter Szolovits" leaving "NE43-416")
    ("Gerald Jay Sussman" leaving "NE43-416")
    ("Gerald Jay Sussman" arriving "10-250")
    Gerald Jay Sussman says hello to Peter Szolovits 
Name:______________________________________            Page 7

Problem 4: The MAKE-PLACE procedure was defined as follows:

  Code fragment A:
     (define make-place
       (lambda (name)
	 (let ((occupants '()) (exits '()))
	   (define (here message)
	     ...)
	   here)))

Two alternative definitions are shown in B and C below.

  Code fragment B:
     (define make-place
       (let ((occupants '()) (exits '()))
	 (lambda (name)
	   (define (here message)
	     ...)
	   here)))

  Code fragment C:
     (define make-place
       (lambda (name)
	 (define (here message)
	   (let ((occupants '()) (exits '()))
	     ...))
	 here))

We have executed the room setup code:

     (define lecture (make-place "10-250"))
     (define gjs-office (make-place "NE43-408"))
     (define lou-office (make-place "36-747"))

     (send lecture 'new-exit 'physical-plant gjs-office)
     (send lecture 'new-exit 'physical-plant lou-office)
     (send lou-office 'new-exit 'physical-plant lecture)
     (send gjs-office 'new-exit 'physical-plant lecture)
     (send gjs-office 'new-exit 'physical-plant lou-office)

For each of the three ways of defining MAKE-PLACE above, show what is
printed as a consequence of evaluating the expression:

			(print-exits lecture)

Write your answers in the spaces provided below:

  Fragment A:


  Fragment B:


  Fragment C:
Name:______________________________________            Page 8

Problem 5.

The code in the handout is chock-full of bugs that may arise if the
agents are executed concurrently, as suggested by the code under the
comment ";;; Asynchronous Simulation".  Louis Reasoner has been
assigned to clear up these bugs.  He sees that there are a number of
shared variables where access needs to be serialized.  He has written
a new version of MAKE-PLACE that he claims addresses these problems,
as follows (changed code is marked with "*" and an identifying
number):

(define (make-place name)
  (let ((occupants '())
        (exits '())
	(s-occupants (make-serializer))	                    ;* 1 
        (s-exits (make-serializer)))			    ;* 2
    (define (here message)
      (cond ((eq? message 'name?)
	     (lambda (who-asked) name))
	    ((eq? message 'who-is-here?)
	     (s-occupants (lambda (who) occupants)))	    ;* 3
	    ((eq? message 'exits?)
	     (s-occupants (lambda (who) exits)))	    ;* 4
	    ((eq? message 'new-exit)
	     (s-exits					    ;* 5
	      (lambda (who new-exit)
		(set! exits (cons new-exit exits)))))
	    ((eq? message 'i-am-leaving)
	     (lambda (who)
	       (if (memq who occupants)
		   ((s-occupants			    ;* 6
		     (lambda ()
		       (set! occupants (delq who occupants)))))
		   (error "Ghostly departure" here who))
	       (write-line
		(list (send who 'name? here) 'leaving name))
	       (for-each (lambda (occ)
			   (send occ 'departed here who))
			 occupants)))
	    ((eq? message 'i-am-arriving)
	     (s-occupants				    ;* 7
	      (lambda (who)
		(if (memq who occupants)
		    (error "Already here" here who)
		    (set! occupants (cons who occupants)))
		(write-line
		 (list (send who 'name? here) 'arriving name))
		(for-each (lambda (occ)
			    (send occ 'arrived here who))
			  occupants))))
	    (else
	     (error "Unknown message -- PLACE"
		    message)))) 
    here))

Name:______________________________________            Page 9

Problem 5 (continued).

For each of the following sentences indicate whether it is true or
false by circling the correct response.


A. It is necessary to serialize some reads and writes of the EXITS 
   variable in concurrent execution of the agents.

       Circle the correct answer:   True   False


B. It is necessary to serialize some reads and writes of the OCCUPANTS
   variable in concurrent execution of the agents.

       Circle the correct answer:   True   False


C. It is necessary to use the OCCUPANTS serializer to control read
   access to the EXITS in the line marked "* 4".

       Circle the correct answer:   True   False


D. If it is necessary to serialize some reads and writes of the EXITS 
   variable, then the S-EXITS expression beginning on the line marked
   "* 5" would be a correct way to do it.

       Circle the correct answer:   True   False


E. It is necessary that the critical region beginning on the line
   marked "* 6" be serialized with the same serializer as the region 
   beginning on the line marked "* 7".

       Circle the correct answer:   True   False


F. The critical region beginning on the line marked "* 7" is larger
   than is necessary to prevent the development of inconsistency.

       Circle the correct answer:   True   False
