1 / 31

Lecture 12: Data directed programming Message passing

Lecture 12: Data directed programming Message passing. Section 2.4, pages 169-187 2.5.1,2.5.2 pages 187-197. ‘’Complex’’ interface. Constructors: (make-from-real-imag x y) (make-from-mag-ang r a) Selectors: (real-part z) (imag-part z) (magnitude z) (angle z) Merthods:

hildav
Download Presentation

Lecture 12: Data directed programming Message passing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 12: Data directed programming Message passing Section 2.4, pages 169-187 2.5.1,2.5.2 pages 187-197 מבוא מורחב

  2. ‘’Complex’’ interface Constructors: (make-from-real-imag x y) (make-from-mag-ang r a) Selectors: (real-part z) (imag-part z) (magnitude z) (angle z) Merthods: (add-complex z1 z2) (sub-complex z1 z2) (mul-complex z1 z2) (div-complex z1 z2) מבוא מורחב

  3. a+bi imaginary 3 + 2i 2 real 3 One possible implementation: Rectungular representation. (define (make-from-real-imag x y) (cons x y)) (define (make-from-mag-ang r a) (cons (* r (cos a)) (* r (sin a)))) (define (real-part z) (car z)) (define (imag-part z) (cdr z)) (define (magnitude z) (sqrt (+ (square (real-part z)) (square (imag-part z))))) (define (angle z) (atan (imag-part z) (real-part z))) מבוא מורחב

  4. a+bi = r(cos  + i sin ) = r ei imaginary 3 + 2i 2 r= 13  =atan(2/3) real 3 One possible implementation: Polar representation. מבוא מורחב

  5. One possible implementation: Polar representation. (define (make-from-mag-ang r a) (cons r a)) (define (make-from-real-imag x y) (cons (sqrt (+ (square x) (square y))) (atan y x))) (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)))) מבוא מורחב

  6. Suppose we want to use both representation One way to that is define a new set of procedures for each representation. (make-from-real-imag-polar x y) (make-from-mag-ang-polar r a) (real-part-polar z) (imag-part-polar z) (magnitude-polar z) (angle-polar z) (make-from-real-imag-rectangular x y) (make-from-mag-ang-rectangular r a) (real-part-rectangular z) (imag-part-rectangular z) (magnitude-rectangular z) (angle-rectangular z) מבוא מורחב

  7. Our solution is cumbersome Problem: Each time we handle an element we must remember whether it is in polar or rectangular representation. Solution: We will add a tag to every element, telling what representation is used. Using the tag we will automatically choose the write procedure. מבוא מורחב

  8. Attaching tags. We tag 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) (if (pair? datum) (cdr datum) (error "Bad tagged datum -- CONTENTS" datum))) מבוא מורחב

  9. Constructos (define (make-from-real-imag-polar x y) (attach-tag 'polar (cons (sqrt (+ (square x) (square y))) (atan y x)))) (define (make-from-mag-ang-polar r a) (attach-tag 'polar (cons r a))) (define (make-from-real-imag-rectangular x y) (attach-tag 'rectangular (cons x y))) (define (make-from-mag-ang-rectangular r a) (attach-tag 'rectangular (cons (* r (cos a)) (* r (sin a))))) מבוא מורחב

  10. Dispatch on type Selectors (define (real-part z) (cond ((rectangular? z) (real-part-rectangular (contents z))) ((polar? z) (real-part-polar (contents z))) (else (error "Unknown type" z)))) (define (rectangular? z) (eq? (type-tag z) 'rectangular)) (define (polar? z) (eq? (type-tag z) 'polar)) מבוא מורחב

  11. Two data types vs. One tagged data-type • Instead of having two different data types: • rectangular-complex • polar-complex We have 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. polar representation Rectangular representation Tagged data as an abstraction barrier. Outer world applications add-complex, sub-complex Methods: real-part imag-part magnitude angle Interface: Representation: מבוא מורחב

  13. But There Are Difficulties The system is bureaucratic and not modular • The generic selectors must know about all representations. • 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-rectangular (contents z))) ((polar? z) (real-part-polar (contents z))) (else (error "Unknown type" z)))) מבוא מורחב

  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 real-part-polar imag-part-polar magnitude-polar angle-polar operations מבוא מורחב

  15. Data-directed programming (Cont) Assume we have (put <op> <type> <item>) (get <op> <type>) will implement them later in the course. מבוא מורחב

  16. Rectangular implementation (define (install-rectangular-package) ;; internal procedures ... (define (make-from-mag-ang r a) (cons (* r (cos a)) (* r (sin a)))) (define (real-part z) (car z)) (define (imag-part z) (cdr z)) (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)

  17. Polar implementation (define (install-polar-package) ;; internal procedures ... (define (make-from-real-imag x y) (cons (sqrt (+ (square x) (square y))) (atan y x))) (define (magnitude z) (car z)) (define (angle z) (cdr z)) (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 (add-complex z1 z2) (apply-generic 'add z1 z2)) ... • Consider (apply-generic 'add z1 z2) • apply-generic first: • Finds out the tags of z1 and z2. • Say they are 'polar and 'rectangular. • 2. Then, apply-generic looks at the table to find • the method that deals with 'polar+'rectangular. • 3. Finally, it applied the method on the arguments. Notice that apply-generic does not know in advance the number of arguments it is going to get. מבוא מורחב

  19. Handling a varying number of arguments We can define: (define (f x y . z) <body>) And this means f expects at least two (or more) arguments. Examples: (define (f x y . z) <body>) (f 1 2 3 4 5 6) -> x=1 y=2 z=(3 4 5 6) (define (g . w) <body>) (g 1 2 3 4 5 6) -> w=(1 2 3 4 5 6) מבוא מורחב

  20. Apply – A way to use such arguments. If we know in advance the number of arguments, We can give them names and use them. Example: (define (f proc x y) (proc x y)) To apply a procedure on an unknown number of Arguments, we use apply. Example: (define (f proc . args) (apply proc args)) מבוא מורחב

  21. dot notation Apply does the equivalent of (proc cont-arg1 cont-arg2…) which we cannot directly write in the procedure… Generic selectors (define (add-complex z1 z2) (apply-generic 'add z1 z2)) (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)))))) מבוא מורחב

  22. Constructors 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)) מבוא מורחב

  23. Summary: Data Directed programming 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 earlier source code. • 2. Changes are local. • install-polar-package • install-rectangular-package • .. מבוא מורחב

  24. Message Passing Programming Data Directed: Intelligent methods that work with different data types. Message Passing: Intelligent data types that work with different methods. In message passing the idea is that the data gets a message that tells it which method to invoke, and returns the answer. מבוא מורחב

  25. types Polar Rectangular real-part imag-part magnitude angle real-part-rectangular imag-part-rectangular magnitude-rectangular angle-rectangular real-part-polar imag-part-polar magnitude-polar angle-polar Message Passing for Numbers Package Data Directed: for each method we look for the type. Search decompose the table by row. Message Passing: for each type we look for the method. Search decompose the table by column. operations מבוא מורחב

  26. Message passing style – Rectangular Complex The constructor has it all. (define (make-from-real-imag x y) (define (dispatch op) (cond ((eq? op 'real-part) x) ((eq? op 'imag-part) y) ((eq? op 'magnitude) (sqrt (+ (square x) (square y)))) ((eq? op 'angle) (atan y x)) (else (error "Unknown op -- MAKE-FROM-REAL-IMAG" op)))) dispatch) מבוא מורחב

  27. Message passing style – Polar Complex The constructor has it all. (define (make-from-mag-ang r a) (define (dispatch op) (lambda (op) (cond ((eq? op 'real-part) (* r (cos a))) ((eq? op 'imag-part) (* r (sin a))) ((eq? op 'magnitude) r) ((eq? op 'angle) a) (else (error . . . ))))) dispatch) מבוא מורחב

  28. Methods and New Apply Generic Shared by both! (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 (apply-generic op arg) (arg op)) מבוא מורחב

  29. Example (define x (make-from-real-imag 3 2)) (define x(lambda (op) (cond ((eq? op 'real-part) 3) ((eq? op 'imag-part) 2) ((eq? op 'magnitude) (sqrt (+ (square 3) (square 2)))) ((eq? op 'angle) (atan 2 3)) (else (error . . . ))))) (real-part x) (x 'real-part) ==> 3 מבוא מורחב

  30. Another example. Pair in Message passing style (define (cons x y) (define (dispatch op) (cond ((eq? op 'car) x) ((eq? op 'cdr) y) (else (error "Unknown op -- CONS" op)))) dispatch) (define (car x) (x 'car)) (define (cdr x) (x 'cdr)) מבוא מורחב

  31. Sending a Message (define a (cons 1 2)) (car a) (a 'car) ((cond ((eq? op 'car) 1) ((eq? op 'cdr) 2) (else (error "Unknown op -- CONS" op))) 'car) 1 מבוא מורחב

More Related