                      MASSACHUSETTS INSTITUTE OF TECHNOLOGY

            Department of Electrical Engineering and Computer Science 

             6.001 Structure and Interpretation of Computer Programs

			       Fall Semester 1984

				  Problem Set 5

Issued: Tuesday, October 16, 1984

Due:  on Wednesday, October 24, 1984
              for recitations meeting at 9:00, 10:00 and 11:00
      on Friday, October 26, 1984
              for recitations meeting at 12:00, 1:00 and 2:00

Reading Assignment: Sections 2.4 and 3.1

Write up and turn in the following exercises from the text:

	o Exercise 2.50
	o Exercise 2.51
	o Exercise 2.54



	      Programming Assignment -- The DOCTOR Program

In this assignment, we shall consider a drastically-simplified version of a 
famous program called DOCTOR that was developed in the early 1960's by Prof.
Joseph Weisenbaum.  The DOCTOR program engages in a "conversation" with the
computer user, in which it mimics a non-directive style of psychotherapy.
In the following sample conversation, the user's responses are preceded by
the symbol "**".

(HELLO, BILL)
(WHAT SEEMS TO BE THE PROBLEM?)

**(everyone hates me)
(WHY DO YOU SAY EVERYONE HATES YOU)

**(whenever i enter a room no one will look me in the eye)
(MANY PEOPLE HAVE THE SAME SORTS OF FEELINGS)

**(they laugh at me behind my back)
(YOU SEEM TO THINK THEY LAUGH AT YOU BEHIND YOUR BACK)

**(they get together and talk about how they hate me)
(MANY OF MY PATIENTS HAVE TOLD ME THE SAME THING)

**(even my mother hates me)
(YOU FEEL THAT EVEN YOUR MOTHER HATES YOU)

**(even when i was a baby she said i was ugly and stupid)
(PLEASE CONTINUE)

**(she hit me all the time)
(WHY DO YOU BELIEVE SHE HIT YOU ALL THE TIME)


In order to simplify the program, most punctuation has been omitted and the
responses of both user and machine are represented as Lisp lists.


Description of the DOCTOR Program:

Although at first glance the DOCTOR program seems to understand and reply to
the user's remarks, in fact the program has two simple methods for generating a
response.  The first method involves taking the user's reply, changing some
common words like "I", "me", "myself", and "am" to the corresponding second-
person words, and appending the transformed response to some qualifying phrase
such as "why do you say" or "you seem to think".  The second method is to
ignore what the user types and simply respond with some sort of hedge like
"please continue" or "many people have the same sorts of feelings".  The
program chooses one of these methods at random.

Every interactive program, including the Lisp interpreter itself, has a
distinguished procedure called a "driver loop".  The procedure DOCTOR-DRIVER-
LOOP below is the driver loop for the DOCTOR program.  A driver loop accepts
input, determines how to process that input, produces the output, and repeats
listening for input.

   (define (visit-doctor name)
     (print (list 'hello, name))
     (print '(what seems to be the trouble?))
     (doctor-driver-loop name))

   (define (doctor-driver-loop name)
     (newline) (princ '**)
     (let ((user-response (read)))
       (cond ((equal? user-response '(goodbye))
              (print (list 'goodbye, name))
	      (print '(and a pleasant day to you)))
	     (else (print-reply user-response)
		   (doctor-driver-loop name)))))

   (define (print-reply user-response)
     (cond ((fifty-fifty)
	    (print (append (qualifier)
			   (change-person user-response))))
	   (else (print (hedge)))))

The VISIT-DOCTOR procedure first greets the user, then asks an initial question
and starts the driver loop.  This reads in the user's response.  If the user
says "(goodbye)", then the program terminates.  Otherwise, the program 
generates a reply according to one of the two methods described.  The predicate
FIFTY-FIFTY used in the cond is a procedure (given below) that returns true or
false with equal probability.  Qualifiers and hedging remarks are generated by
selecting items at random from appropriate lists:

   (define (qualifier)
     (pick-random '((you seem to think)
   		    (you feel that)
		    (why do you believe)
		    (why do you say))))
   (define (hedge)
     (pick-random '((please go on)
		    (many people have the same sorts of feelings)
		    (many of my patients have told me the same thing)
		    (please continue))))

The basis for the procedure that changes selected first person words to
second person is the following REPLACE procedure, which changes all
occurrences of a given PATTERN to a REPLACEMENT in a list LST:

   (define (replace pattern replacement lst)
     (cond ((null? lst) '())
	   ((equal? (car lst) pattern)
	    (cons replacement
		  (replace pattern replacement (cdr lst))))
	   (else (cons (car lst)
		       (replace pattern replacement (cdr lst))))))

This is used to define a procedure MANY-REPLACE, which takes as inputs a list
LST together with a list of REPLACEMENT-PAIRS

   ((P1 R1) (P2 R2) . . .)

It replaces in LST all occurrences of P1 by R1, P2 by R2, and so on.

   (define (many-replace replacement-pairs lst)
     (cond ((null? replacement-pairs) lst)
	   (else (replace (caar replacement-pairs)
			  (cadar replacement-pairs)
			  (many-replace (cdr replacement-pairs)
				        lst)))))

Finally, changing the selected words is accomplished by an appropriate call to
MANY-REPLACE:

   (define (change-person phrase)
     (many-replace '((i you) (me you) (am are) (myself yourself))
	           phrase))

The next procedure PICK-RANDOM is used by QUALIFIER and HEDGE.  It picks a 
random element from its list argument:

   (define (pick-random lst)
     (nth (random (length lst)) lst))

PICK-RANDOM is also used to implement the FIFTY-FIFTY predicate:

   (define (fifty-fifty) (pick-random '(t nil)))

The following diagram shows the pattern of procedure calls indicated in the
text of the DOCTOR program:

				visit-doctor

			     doctor-driver-loop

				print-reply


          fifty-fifty       qualifier      hedge      change-person


                        pick-random                   many-replace

							replace


PROBLEM 1:
     Load the file for Problem Set 5 onto your floppy disk.  Now edit the
QUALIFIER and HEDGE procedures to increase the doctor's repertoire of
qualifying and hedging phrases.  Run through a brief session with the 
modified program.  You need not turn in your output to this part.

PROBLEM 2:
     The DOCTOR program can be improved by having it not only change first
person words to second person, but also second person to first.  For
instance, if the user types

   **(you are not being very helpful to me)

the program should respond with something like

   (YOU FEEL THAT I AM NOT BEING VERY HELPFUL TO YOU)

So, for instance, "are" should be replaced by "am", "you" by "i", "yourself"
by "myself", and so on.  (Let's agree to ignore the problem of having the
program decide whether "you" should be replaced by "i" or "me".)  One way to
accomplish this replacement is simply to add the pairs

   (are am) (you i) (yourself myself) . . .

to the list of pairs in the CHANGE-PERSON procedure.  Edit the procedure to do
this.  Now try evaluating

   (change-person '(you are not being very helpful to me))

What does the modified procedure return?

PROBLEM 3:
    Describe as best you can the nature of the bug uncovered above.  Implement
a correct replacement strategy.  Be sure to test your improved version of the
DOCTOR program.  Turn in a brief description of your strategy as well as
listings of any procedures you write or modify.

PROBLEM 4:
    Add a data structure called RECORD to the DOCTOR program that keeps track
of the list of user inputs by an appropriate modification of the driver loop.
Since the doctor can be expected to see several patients each day, attach
NAME as a label to each input, so that after the sequence given at the 
beginning RECORD would have the value

   ((BILL (EVERYONE HATES ME))
    (BILL (WHENEVER I ENTER A ROOM NO ONE WILL LOOK ME IN THE EYE))
    (BILL (THEY LAUGH AT ME BEHIND MY BACK))
    (BILL (THEY GET TOGETHER AND TALK ABOUT HOW THEY HATE ME))
				.
				.

    (BILL (SHE HIT ME ALL THE TIME)))

If BILL says goodbye and the next patient's name is GEORGE, then George's first
complaint should be appended to the end of Bill's list with the label GEORGE.


PROBLEM 6:
    Modify PRINT-REPLY so that occasionally the program deviates from the two
methods of response that it now has to pick at random an item from RECORD.  It
then should modify person as appropriate and return the previous phrase with
a qualifier such as "earlier you said that . . ." or "you remind me of one
of my other patients who said . . ." depending on whether the name associated
with the phrase is the same or different from that of the current user.

PROBLEM 7:
    After you have finished at the terminal, think of another improvement to
the DOCTOR program.  Give a brief description of how you could go about
implementing it.  (You are welcome to actually carry out the implementation,
but you need not turn in any listings.

OPTIONAL
    Do you think it is feasible to improve the program to the point where its
responses are essentially indistinguishable from those of a real person?  Some
people have advocated using such programs in the treatment of psychiatric
patients.  Others maintain that this would be unethical.  Some patients who
have participated in experiments claim to have been helped!  What do you
think?  For a further discussion of these points, see Prof. Weizenbaum's
book "Computer Power and Human Reason".