Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 11: Multiple representations of abstract data Message passing Overloading Section 2.4, pages 169-187 2.5.1,2.5.2 pages 187-197 מבוא.

Similar presentations


Presentation on theme: "Lecture 11: Multiple representations of abstract data Message passing Overloading Section 2.4, pages 169-187 2.5.1,2.5.2 pages 187-197 מבוא."— Presentation transcript:

1 Lecture 11: Multiple representations of abstract data Message passing Overloading
Section 2.4, pages 2.5.1,2.5.2 pages מבוא מורחב

2 Complex numbers a+bi = r(cos  + i sin ) = r ei r=  =atan(2/3)
13  =atan(2/3) = r(cos  + i sin ) = r ei r= imaginary real מבוא מורחב

3 Complex numbers arithmetic
(a + bi) + (c + di) = (a+c) + (b+d)i (a + bi) * (c + di) = (ac-bd)+(ad+bc)i r1 e(i) * r2 e(i) = (r1r2) e(i(+)) מבוא מורחב

4 המימוש שמדמיין אריאל Constructor:
(define (make-complex x y) (cons x y)) Selectors: (define (real-part z) (car z)) (define (imag-part z) (cdr z)) Metohds: (define (add-complex z1 z2) (make-complex (+ (real-part z1) (real-part z2)) (+ (imag-part z1) (imag-part z2)))) (define (sub-complex z1 z2) (make-complex (- (real-part z1) (real-part z2)) (- (imag-part z1) (imag-part z2)))) מבוא מורחב

5 המימוש מהזווית של מני Constructors:
(define (make-complex r a) (cons r a)) Selectors: (define (magnitude z) (car z)) (define (angle z) (cdr z)) (define (real-part z) (* (magnitude z) (cos (angle z)))) (define (imag-part z) (* (magnitude z) (sin (angle z)))) Methods: (define (mul-complex z1 z2) (make-complex (* (magnitude z1) (magnitude z2)) (+ (angle z1) (angle z2)))) מבוא מורחב

6 How do we have them both ? We tag the data: ('rectangular 3 2)
('polar (sqrt 13) (atan 2 3)) (define (attach-tag type-tag contents) (cons type-tag contents)) (define (type-tag datum) (if (pair? datum) (car datum) (error "Bad tagged datum -- TYPE-TAG" datum))) (define (contents datum) (cdr datum) (error "Bad tagged datum -- CONTENTS" datum))) The way we really tag the data: (cons ‘rectangular (cons 3 2)) מבוא מורחב

7 Complex numbers: having both reps.
(define (rectangular? z) (eq? (type-tag z) 'rectangular)) (define (polar? z) (eq? (type-tag z) 'polar)) (define (real-part z) (cond ((rectangular? z) (real-part-rectangular (contents z))) ((polar? z) (real-part-polar (contents z))) (else (error "Unknown type" z)))) מבוא מורחב

8 Complex numbers: having both reps.
(define (magnitude z) (cond ((rectangular? z) (magnitude-rectangular (contents z))) ((polar? z) (magnitude-polar (contents z))) (else (error "Unknown type“ z)))) angle, imag-part are similar Net effect: we can have the same methods in both representation. Regardless of how we implement the object. מבוא מורחב

9 and we have to rename (define (real-part-rectangular z) (car z))
(define (imag-part-rectangular z) (cdr z)) (define (magnitude-rectangular z) (sqrt (+ (square (real-part-rectangular z)) (square (imag-part-rectangular z))))) (define (angle-rectangular z) (atan (imag-part-rectangular z) (real-part-rectangular z))) (define (make-rectangular-comlex x y) (attach-tag 'rectangular (cons x y))) מבוא מורחב

10 and we have to rename (define (real-part-polar z)
(* (magnitude-polar z) (cos (angle-polar z)))) (define (imag-part-polar z) (* (magnitude-polar z) (sin (angle-polar z)))) (define (magnitude-polar z) (car z)) (define (angle-polar z) (cdr z)) (define (make-polar-complex r a) (attach-tag 'polar (cons r a))) מבוא מורחב

11 Two data types vs. One tagged data-type
We could have implemented two different data types: rectangulr-complex and polar-complex Instead we implement one data type: complex And we tag both representations. We implement the methods so that they work for both representations. As a result: whatever way we choose to represent the object, we see the same picture of the world. מבוא מורחב

12 Tagged data as an abstraction barrier.
Outer world applications Methods: add-complex, sub-complex real-part imag-part magnitude angle Interface: polar representation Rectangular Representation: מבוא מורחב

13 Difficulties The system is Not modular Bureacratic
The generic selectors must know about all reps. If we want to add a new representation we need to Add it to each of the methods, Be careful with name clashes. (define (real-part z) (cond ((rectangular? z) … real_part_polar … ((polar? z) … real_part_rectangular … (else …))) מבוא מורחב

14 Data directed programming
work with a table: types Polar Rectangular real-part imag-part magnitude angle real-part-rectangular imag-part-rectangular magnitude-rectangular angle-rectangular operations real-part-polar imag-part-polar magnitude-polar angle-polar מבוא מורחב

15 Data-directed programming (Cont)
Assume we have (put <op> <type> <item>) (get <op> <type>) will do them later in the course. Put and get work on a global table. It’s not a good way to write a code, but it will simplify things for us. The table associates labels to rows and vectors. We don’t describe how to implement it, but still we give one possible way of doing it, so that we have a concrete way to think of it. We keep list of pairs, each pair is a column name (tag) and a column. A column is a list of pairs, each pair is a row name and a value. מבוא מורחב

16 Rectangular implementation
(define (install-rectangular-package) ;; internal procedures (define (real-part z) (car z)) (define (imag-part z) (cdr z)) ... (define (make-from-mag-ang r a) (cons (* r (cos a)) (* r (sin a)))) (define (tag x) (attach-tag 'rectangular x)) ;; interface to the rest of the system (put 'real-part '(rectangular) real-part) (put 'imag-part '(rectangular) imag-part) (put 'magnitude '(rectangular) magnitude) (put 'angle '(rectangular) angle) (put 'make-from-real-imag 'rectangular (lambda (x y) (tag (make-from-real-imag x y)))) (put 'make-from-mag-ang 'rectangular (lambda (r a) (tag (make-from-mag-ang r a)))) 'done) Why do we sometimes put the type in a list??? Constructors are a special case- We tag them according to the output type rather than the input type.

17 Polar implementation (define (install-polar-package)
;; internal procedures (define (magnitude z) (car z)) (define (angle z) (cdr z)) ... (define (make-from-real-imag x y) (cons (sqrt (+ (square x) (square y))) (atan y x))) (define (tag x) (attach-tag 'polar x)) ;; interface to the rest of the system (put 'real-part '(polar) real-part) (put 'imag-part '(polar) imag-part) (put 'magnitude '(polar) magnitude) (put 'angle '(polar) angle) (put 'make-from-real-imag 'polar (lambda (x y) (tag (make-from-real-imag x y)))) (put 'make-from-mag-ang 'polar (lambda (r a) (tag (make-from-mag-ang r a)))) 'done)

18 Generic selectors Methods:
(define (real-part z) (apply-generic 'real-part z)) (define (imag-part z) (apply-generic 'imag-part z)) (define (magnitude z) (apply-generic 'magnitude z)) (define (angle z) (apply-generic 'angle z)) (define (simple-apply-generic op arg) (let ((type-tag (type-tag arg))) (let (proc (get op (list type-tag))) (if proc (proc (contents arg)) (error "No method for these types -- APPLY-GENERIC" (list op type-tag))))) מבוא מורחב

19 apply-generic f(complex-polar ,complex-rect , complex-polar)
(define (apply-generic op . args) (let ((type-tags (map type-tag args))) (let (proc (get op type-tags))) (if proc (apply proc (map contents args)) (error "No method for these types -- APPLY-GENERIC" (list op type-tags)))))) f(complex-polar ,complex-rect , complex-polar) Will look for the function f stored for tag (polar,rectangular,number) מבוא מורחב

20 Finally Constructors are a special case-
We tag them according to the output type rather than the input type. They require special handling. (define (make-from-real-imag x y) ((get 'make-from-real-imag 'rectangular) x y)) (define (make-from-mag-ang r a) ((get 'make-from-mag-ang 'polar) r a)) מבוא מורחב

21 Data directed programming.
The data (arguments) trigger the right method based on the data type. Data directed programming is more modular: To add a representation, we only need to write a package for the new representation without affecting the other source code. Changes are local. install-polar-package install-rectangular-package מבוא מורחב

22 Overloading So far: Generic methods that deal with different
representations. Next step: Generic methods that deal with different types of arguments. (+ int int) (+ real real) (+ int real) Overloading מבוא מורחב

23 Operations on mixed types
What about (+ complex real) ? We can add new methods to handle mixed types. Cumbersome Not modular. 2. Coercion. For the arithmetic example we can translate more specific types to the more general types. E.g. given (+ complex real) , view the real number as complex And apply (+ complex complex). מבוא מורחב


Download ppt "Lecture 11: Multiple representations of abstract data Message passing Overloading Section 2.4, pages 169-187 2.5.1,2.5.2 pages 187-197 מבוא."

Similar presentations


Ads by Google