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

(define 1st car)
(define 2nd cadr)
(define 3rd caddr)

(define timed-object-maker
  (lambda (time)
    (lambda msg
      (case (1st msg)
        ('show time)
        ('time time)
        (else (error msg 'no-such-method))))))

(define add-timed-object 
  (lambda (lst obj)
    (if (null? lst)
        (list obj)
        (let ((time (obj 'time)))
          (if (> time ((car lst) 'time))
              (cons (car lst) 
                    (add-timed-object (cdr lst) obj))
              (cons obj lst))))))

(define show-timed-objects
  (lambda (lst)
    (if (null? lst)
        lst
        (cons ((car lst) 'show) 
              (show-timed-objects (cdr lst))))))

(define queue-maker
  (lambda (s)
    (lambda msg
      (letrec 
        ((enqueue 
           (lambda (x) 
             (queue-maker (append s (list x)))))
         (dequeue 
           (lambda () 
             (if (null? s) s (queue-maker (cdr s)))))
         (insert 
           (lambda (x) 
             (queue-maker (add-timed-object s x))))
         (empty (lambda () (null? s)))
         (peek (lambda () (if (null? s) s (car s))))
         (show (lambda () (show-timed-objects s))))
        (case (car msg)
          ('enqueue (enqueue (2nd msg)))
          ('dequeue (dequeue))
          ('insert (insert (2nd msg)))
          ('empty (empty))
          ('peek (peek))
          ('show (show))
          (else (error msg 'no-such-method)))))))

(define res1
  (let* 
    ((timeQ (queue-maker '()))
     (timeQ (timeQ 'insert (timed-object-maker 12)))
     (timeQ (timeQ 'insert (timed-object-maker 26)))
     (timeQ (timeQ 'insert (timed-object-maker 9))))
    (timeQ 'show)))
 -  A discrete event simulation of a doctor's office in object oriented programming style. Procedures defining "classes" include queue, event-manager, person-maker, patient-maker, doctor-maker, and timed-object. Observe that a class may use the services of another class, not by inheritance, but by creating an internal object of that class. This is not unlike what happens in Java where interfaces are using to require a means of handling events when they happen.

In this simple simulation there are a fixed number of patients and a fixed number of doctors. All patients that are not under treatment are given a random time at which they will become sick. When a patient becomes sick, it visits the doctor's office. If a doctor is available the doctor is paired with the patient for a specified, random time (that is, the patient undergoes treatment). If a doctor is not available, the patient waits to be paired with an available doctor in a first-come-first-serve queue. When pairing terminates the doctor becomes available and the patient leaves the office with a new random time-to-become-sick.

This slide starts the coding with "class" queue-maker that is used to create an event queue, a patient queue, and a doctor queue. The event queue is a priority queue: events are ordered by time to occurrence. Insertion of an event into an event queue is handled by the insert function. All objects, events or otherwise, using the insert function must subclass a timed-object class which is also shown. The defines at the bottom on the left illustrate the use of these classes. After loading do this:

  prompt> res1
  ;Value: (9 12 24)