20-CS-4003-001 Organization of Programming Languages Fall 2017
Object Oriented Style

Lambda calculus, Type theory, Formal semantics, Program analysis

    Prev     Next     All lectures        Code

Doctor's Office Simulation

;; subclasses timed-object
(define person-maker
  (lambda (name time)
    (let ((timed-object (timed-object-maker time)))
      (lambda msg
	(case (1st msg)
	  ('name name)
	  (else (apply timed-object msg)))))))

;; subclasses person
(define patient-maker
  (lambda (name time)
    (let ((person (person-maker name time)))
      (lambda msg
	(case (1st msg)
	  ('show 
           (list 'patient: name 'time-to-sick: time))
	  (else (apply person msg)))))))

;; subclasses person
;; called like this: (doctor-maker "name") 
;; but other possibilities are left open
;; by using (lambda args instead of (lambda (arg)
(define doctor-maker
  (lambda args
    (let ((person (person-maker (1st args) 0)))
      (lambda msg 
	(case (1st msg) 
	  ('show 
 	     (list 'doctor: (car args) 
		   (if (null? (cdr args))
		       '*unassigned*
		       (let ((patient (2nd args)))
			 'patient: (patient 'name) 
                         'time: (patient 'time)))))
	  (else (apply person msg)))))))

(define res2
  (let* 
    ((docQ (queue-maker '()))
     (docQ (docQ 'enqueue (doctor-maker 'Lisa)))
     (docQ (docQ 'enqueue (doctor-maker 'Larry)))
     (docQ (docQ 'enqueue (doctor-maker 'Lory)))
     (patQ (queue-maker '()))
     (patQ (patQ 'insert (patient-maker 'John 12)))
     (patQ (patQ 'insert (patient-maker 'Jill 26)))
     (patQ (patQ 'insert (patient-maker 'Jack 9))))
    (list 'doc-queue: (docQ 'show) 
          'pat-queue: (patQ 'show))))
 -  Doctors and patients are people and people have names. Hence a person class is defined with the doctor and patient classes as subclasses. Observe also that the person class subclasses the timed-object class and therefore can be inserted into a priority queue. The
  (let ((person (person-maker name time)))...)
binding in the patient class and a similar one in the doctor class allow for the inheritance of the name and time methods from the person and timed-object classes. If time is in a message sent from a patient object the message will be sent to the person superclass via
  (else (apply person msg))
but then the
  (else (apply timed-object msg))
invocation results in the value of time being returned in the timed-object superclass. Observe that the show method of the patient class overrides the show method of the person superclass. Add the code to the left to the code of the previous slide. Do the following:
  prompt> res2
  ;Value: 
  (doc-queue: 
   ((doctor: lisa *unassigned*) 
    (doctor: larry *unassigned*) 
    (doctor: lory *unassigned*)) 
   pat-queue: 
    ((patient: jack time-to-sick: 9) 
     (patient: john time-to-sick: 12) 
     (patient: jill time-to-sick: 26)))