; Hal tells us, that in really large systems you don't want just the ; horizontal abstraction layers -but also vertical barriers, ; so that two absolutely different and incompatible representation- ; implementers can not interfere with each other. ; Make with case-analysis, complex number arithmetic. ; And again, let's assume that we have some kind of clouds. ; SELECTORS: ; (REAL-PART Z) ; (IMAG-PART Z) ; (MAGNITUDE Z) ; (ANGLE Z) ; CONSTRUCTORS: ; (MAKE-RECTANGULAR X Y) ; (MAKE-POLAR R A) ;(define (+c z1 z2) ; (make-rectangular ; (+ (real-part z1) (real-part z2)) ; (+ (imag-part z1) (imag-part z2)))) ;(define (-c z1 z2) ; (make-rectangular ; (- (real-part z1) (real-part z2)) ; (- (imag-part z1) (imag-part z2)))) ;(define (*c z1 z2) ; (make-polar ; (* (magnitude z1) (magnitude z2)) ; (+ (angle z1) (angle z2)))) ;(define (/c z1 z2) ; (make-polar ; (/ (magnitude z1) (magnitude z2)) ; (- (angle z1) (angle z2)))) ; Now implementing the data-representation. ;;; Hello, I am George: ;;; Representing complex numbers as pairs ;;; REAL-PART, IMAGINARY-PART ; (define (make-rectangular x y) ; (cons x y)) ; (define (real-part z) (car z)) ; (define (imag-part z) (cdr z)) ; (define (make-polar r a) ; (cons (* r (cos a)) (* r (sin a)))) ; (define (magnitude z) ; (sqrt (+ (square (car z)) ; (square (cdr z))))) ; (define (angle z) ; (atan (cdr z) (car z))) ; Now implementing the data-representation, giving the job to Martha. ;;; Hello, my name is Martha, ;;; representing complex numbers as pairs ;;; MAGNITUDE, ANGLE ; (define (make-polar r a) (cons r a)) ; (define (magnitude z) (car z)) ; (define (angle z) (cdr z)) ; (define (make-rectangular x y) ; (cons (sqrt (+ (square x) (square y))) ; (atan y x))) ; (define (real-part z) ; (* (car z) (cos (cdr z)))) ; (define (imag-part z) ; (* (car z) (sin (cdr z)))) ; 4B:(14m46s) But what is better. Hal restates an important matter. ; You don't want to decide, which is better now. You want to delay ; the actual implementation choice as long as possible because this ; is what wishful thinking is about. ; Hal introduces the notion of typed (labeled) data. ;;; Support mechanism for manifest types ; (define (attach-type type contents) ; (cons type contents)) ; (define type car) ; (define contents cdr) ; (define (rectangular? z) ; (eq? (type z) 'rectangular)) ; (define (polar? z) ; (eq? (type z) 'polar)) ;;; Rectangular package ; (define (make-rectangular x y) ; (attach-type 'rectangular (cons x y))) ; (define real-part-rectangular car) ; (define imag-part-rectangular cdr) ; (define (magnitude-rectangular z) ; (sqrt (+ (square (car z)) ; (square (cdr z))))) ; (define (angle-rectangular z) ; (atan (cdr z) (car z))) ;;; Martha does the same... 4B:(21m5s) ; Now defining the selectors. ; (define (real-part z) ; (cond ((rectangular? z) ; (real-part-rectangular ; (contents z))) ; ((polar? z) ; (real-part-polar ; (contents z))))) ; (define (imag-part z) ; (cond ((rectangular? z) ; (imag-part-rectangular ; (contents z))) ; ((polar? z) ; (imag-part-polar ; (contents z))))) ; (define (magnitude-part z) ; (cond ((rectangular? z) ; (magnitude-part-rectangular ; (contents z))) ; ((polar? z) ; (magnitude-part-polar ; (contents z))))) ; (define (angle-part z) ; (cond ((rectangular? z) ; (angle-rectangular ; (contents z))) ; ((polar? z) ; (angle-polar ; (contents z))))) ; 4B:(23m18s) We implemented generic operators now. ; For one there are George's and Martha's implementation. ; And there is also a manager. ; BREAK 4B:(25m35s) ; Hal now demonstrates: If Harry would come in, the manager would ; have to change his selectors, so that they would take Harry's new ; mechanism into account. ; 4B:(28m28s) The manager is just paper-pushing... ; -Annoying... ; "It's really annoying that the bottleneck in the system (the thing ; that is preventing flexibility and change) is completely in the ; bureaucracy. It's not in anybody who is doing the work." ; "How to get rid of bureaucracy -how to get rid of the manager?" (PUT KEY1 KEY2 VALUE) (GET KEY1 KEY2) ; WORK GEORGE HAS TO DO: ; (put 'rectangular 'real-part real-part-rectangular) ; (put 'rectangular 'imag-part imag-part-rectangular) ; (put 'rectangular 'magnitude magnitude-rectangular) ; (put 'rectangular 'angle angle-rectangular) ; WORK MARTHA HAS TO DO: ; (put 'polar 'real-part real-part-polar) ; (put 'polar 'imag-part imag-part-polar) ; (put 'polar 'magnitude magnitude-polar) ; (put 'polar 'angle angle-polar) ; WHAT IS HARRYS WORK, if he wants to bring his brilliant idea ; into the system? ; (put 'harry 'real-part harry-harry) ; (put ......) ; 4B:(34m27s) What happened to the manager? ; He was replaced by the operator. (DEFINE (OPERATE OP OBJ) (LET ((PROC (GET (TYPE OBJ) OP))) (IF (NOT (NULL? PROC)) (PROC (CONTENTS OBJ)) (ERROR "undefined OP")))) ; (define (real-part obj) ; (operate 'real-part obj)) ; (define (imag-part obj) ; (operate 'imag-part obj)) ; (define (magnitude obj) ; (operate 'magnitude obj)) ; (define (angle obj) ; (operate 'angle obj)) ; 4B:(39m41s) Hal introduced the concept of ; data-directed programming in Lisp. ; important QUESTIONS 4B:(40m33s) BREAK ; The real power of this kind of abstraction: ; implementing general systems. ; 4B:(45m12s) => 4B:(1h12m27s) (ADD COMPLEX COMPLEX) (SUB INT INT) (DIV COMPLEX COMPLEX) (MUL POLYNOMIAL POLYNOMIAL) ; Fundamental QUESTIONS 4B:(1h12m36s) (END)