a. (make-sim-object 'book)
> (define book1 (make-sim-object 'book))
> (define book2 (make-sim-object 'book))
> book1
#<procedure:24:5>
Note that a procedure created by a lambda is displayed as the line and column number where the procedure was made.
In this case, line 24 of object.ss is (lambda (message).> book2
#<procedure:24:5>
> (eq? book1 book1)
#t
> (eq? book1 book2)
#f
> (eq? + +)
#t
> (define dog (make-sim-object 'spot))You can use (display sym) to output one symbol. The output in this example would be produced by (display 'wuff).
> (ask dog 'utter 'wuff)
wuff
(define make-sim-object
(lambda (name)
(lambda (message)
(if (eq? message 'object?)
(lambda (self) #t)
(if (eq? message 'class)
(lambda (self) 'object)
(if (eq? message 'name)
(lambda (self) name)
(if (eq? message 'say)
(lambda (self list-of-stuff)
(if (not (null? list-of-stuff))
(display-message list-of-stuff))
(void))
(if (eq? message 'install)
(lambda (self . args) 'installed)
(if (eq? message 'utter)
(lambda (self utterance)
(display utterance))
#f)))))))))
(define (make-professor name)
(make-subobject
(make-lecturer name)
(lambda (message)
(if (eq? message 'profess)
(lambda (self message)
(ask self 'say (list "It is intuitively obvious that"))
(ask self 'lecture message))
#f))))
Some of the students in Charlottansville have a strange habit of getting undressed and running around the Green, so students have an instance variable is-dressesed that indicates whether or not the student is clothed. Initally, all students are dressed. The get-undressed! that changes the state of is-dressed to #f. A student also has a method get-dressed! that changes the state of is-dressed to #t. A student also has a method is-dressed? that evaluates to #t if the student is dressed and #f otherwise.
(define (make-student name)
(make-subobject
(make-person name)
(let ((is-dressed #t))
(lambda (message)
(case message
((class) (lambda (self) 'student))
((student?) (lambda (self) #t))
((get-undressed!) (lambda (self)
(ask self 'say (list "brrrrr...its cold!"))
(set! is-dressed #f)))
((get-dressed!) (lambda (self)
(ask self 'say (list "I feel much better now."))
(set! is-dressed #t)))
((is-dressed?) (lambda (self) is-dressed))
(else #f))))))
(define (make-police-officer name)
(make-subobject
(let ((super (make-person name)))
(ask super 'make-restless 2) ;;; Police officers are quite restless
super)
(lambda (message)
(case message
((class) (lambda (self) 'police-officer))
((police-officer?) (lambda (self) #t))
((arrest)
(lambda (self arrestee)
(if (eq? (ask self 'location) (ask arrestee 'location))
(begin
(ask self 'say (list (ask arrestee 'name) ", you are under arrest!"))
(ask arrestee 'move-to Jail)
(ask self 'say
(list "You have the right to remain silent, "
"call methods and mutate instance variables.")))
(ask self 'say (list (ask arrestee 'name) " is not here")))))
((clock-tick)
(lambda (self)
(let ((streakers
(filter (lambda (person)
(not (ask person 'is-dressed?)))
(other-people-at-place self (ask self 'location)))))
(if (null? streakers)
(begin
(ask self 'say (list "No one to arrest. Must find donuts."))
(ask (ask self 'super) 'clock-tick))
(map (lambda (streaker)
(ask self 'arrest streaker))
streakers)))))
(else #f)))))
Note that the (ask super 'make-restless 2) is done in the expression that is evaluated to produce the first parameter to make-subobject. That means it happens when
(make-police-officer 'officer-krumpke) is evaluated, but does
not happen every time a message is sent to the police officer.
Input: An initial state consisting of a set places in the Charlottansville world, a student object (as described in Question 5) and a police officer object (whose clock-tick method may contain any code).You should assume the results of random are completely determined (that is, you can always predict what an application of random evaluates to).Output: If there is any sequence of actions the student object can take to streak from the Recursa to the Bart Statue without getting arrested at any time during the game, output true. Otherwise, output false.
Is the streakability problem computable or uncomputable? If you claim it is computable, you should argue convincingly that you could define a procedure that solves it for all possible inputs. If you claim it is uncomputable, you should argue convincingly that it is uncomputable by showing how a solution to the streakability problem could be used to solve another problem that is already known to be uncomputable.
Here's how:
(define (halts? P)
;;; halts? evaluates to true if evaluating (P) would terminate, and
;;; evaluates to false if evaulating (P) would never terminate.
;;; An arresting-police-officer inherits from police-officer except before
;;; making an arrest it evaluates (P). Hence, if (P) terminates,
;;; the arresting-police-officer makes the arrest normally. If (P) does
;;; not terminate, the arresting-police-officer arrests before making the
;;; arrest, and hence never arrests anyone.
(define (make-arresting-police-officer name)
(make-subobject
(make-police-officer name)
(lambda (message)
(if (eq? message 'arrest)
(lambda (self arrestee)
(apply-procedure P)
(ask super 'arrest arrestee))
(get-method super message)))))
(let ((aph (make-student 'alyssa-p-hacker)))
(set-up-charlottansville) ;;; We'll use Charlottansville as our world
(install-object (make-arresting-police-officer 'officer-halty) Green)
(install-object aph Green)
(ask aph 'get-undressed!)
(not (streakable?))))
So, on the first clock-tick message, Officer Halty will find
Alyssa undressed on the Green and attempt to arrest her by invoking
the arrest method of make-arresting-police-officer.
If (apply-procedrue P) terminates, then Alyssa will be arrested.
Hence, streakable? would evaluate to false and (halts? P) should evaluate to true. It does, since we have defined
halts? to evaluate to (not (streakable?)). On the
other hand, if (P) does not terminate, Officer Halty
will never arrect Alyssa. Hence (not (streakable?))
evaluates to false. Thus, we have defined an algorithm for
halts? assuming we have an algorithm for
streakable?. But we know we cannot define an algorithm for
halts? so streakable? must be uncomputable.
Note that the fact that streakable? is uncomputable does not mean that it is safe or unsafe to streak. For a particular world state, it may be possible to prove that there is a sequence of moves which enables a student to streak without getting arrested (or to know that the student will be arrested). The undecidability of streakability just means that it is impossible to define an algorithm that always determines this for any possible input.