Data-Directed Programming Topic 14 Consider ; takes a tagged complex number and returns Multiple Representations of ; the real part Abstract Data – Data Directed (define (real-part z) (cond ((rectangular? z) Programming and Additivity (real-part-rectangular (contents z))) ((polar? z) (real-part-polar (contents z))) Section 2.4.3 (else (error "data type unknown to REAL-PART" z)))) Fall 2008 Programming Development 1 Fall 2008 Programming Development 2 Techniques Techniques The data-directed Problems with that code programming technique • Each interface procedure needs to know about all representations possible • As more representation types are added, the procedure has to be rewritten • Replaces the conditional statement with a lookup table • Procedure gets longer • The chunks of code that were in the branches of the • Procedure gets more complicated conditional are now indexed by the two dimensions of • Procedure is one big conditional statement the lookup table (procedure X representation type) • Each representation alternative must be coded with no name conflicts with any other representation alternative. Fall 2008 Programming Development 3 Fall 2008 Programming Development 4 Techniques Techniques Lookup table for complex A prototype implementat of numbers lookup table Operations Representation types polar rectangular • Basic operations are put and get • Book's implementation isn't until the next chapter real-part real-part-polar real-part-rectangular • Our implementation is simple, suitable for rapid imag-part imag-part-polar imag-part-rectangular prototyping, but not the most efficient magnitude magnitude-polar magnitude-rectangular • Can be replaced later by the book's better angle angle-polar angle-rectangular implementation without changing code built on top of it (named procedures or lambda expressions) Fall 2008 Programming Development 5 Fall 2008 Programming Development 6 Techniques Techniques 1
And now for the Basic idea of our implementation implementation ; operation-table will hold the triples of ; operations, their type, and the associated action • Lookup table is just a list of triples of the form (define operation-table empty) (operation type action) ; takes an operation, the type of data it acts on, and an • Simulates a sparse array of infinite size ; action that implements the operation on the type given. • Put adds a triple to the list ; Adds the triple to the operation table • Get searches the list for the right triple and returns (define (put operation type action) the action part (set! operation-table • The type is a list of the representation types of the (cons (list operation type action) operation's arguments operation-table ))) (Forget you saw set!, the reassignment operator, until the next chapter.) Fall 2008 Programming Development 7 Fall 2008 Programming Development 8 Techniques Techniques The get procedure So? What do we do with it? ; takes an operator and a type, and returns the ; action that implements the operator for that type • Define a collection of procedures or a package for each representation ; in the operation-table (define (get operator type) • These are installed in the table (define (get-aux list) (cond ((null? list) #f) • Complex-arithmetic selectors access the table by ((and (equal? operator (caar list)) means of a general “operation” procedure called (equal? type (cadar list))) apply-generic (caddar list)) (else (get-aux (cdr list))))) (get-aux operation-table)) Fall 2008 Programming Development 9 Fall 2008 Programming Development 10 Techniques Techniques Each representation type in its Rectangular package continued own package ; define the selectors in the rectangular representation (put 'angle ; each selector is defined as it was originally – and '(rectangular) ; must put installed into the operation table (lambda (z) (atan (cdr z) (car z)))) (define (install-rectangular-package) (put 'make-from-real-imag (put 'real-part '(rectangular) car) 'rectangular (put 'imag-part '(rectangular) cdr) (lambda (x y) (put 'magnitude (attach-tag 'rectangular '(rectangular) (cons x y)))) (lambda (z) (sqrt (+ (square (car z)) (square (cdr z)))))) Fall 2008 Programming Development 11 Fall 2008 Programming Development 12 Techniques Techniques 2
part 3 Polar package ; define the selectors in the polar representation (put 'make-from-mag-ang ; each selector is defined as it was originally -- 'rectangular ; and must put installed into the operation (lambda (r a) (define (install-polar-package) (attach-tag 'rectangular (put 'magnitude '(polar) car) (cons (* r (cos a)) (put 'angle '(polar) cdr) (* r (sin a)))))) (put 'real-part 'done) '(polar) (lambda (z) (* (car z) (cos (cdr z))))) Fall 2008 Programming Development 13 Fall 2008 Programming Development 14 Techniques Techniques Polar package continued Part 3 (put 'imag-part (put 'make-from-real-imag '(polar) 'polar (lambda (z) (lambda (x y) (* (car z) (sin (cdr z))))) (attach-tag (put 'make-from-mag-ang 'polar 'polar (cons (sqrt (+ (square x) (lambda (r a) (square y))) (attach-tag 'polar (cons r a)))) (atan y x))))) 'done) Fall 2008 Programming Development 15 Fall 2008 Programming Development 16 Techniques Techniques The constructors Generic operation call ;;; general operation will implement selector ;; THE CONSTRUCTORS ;; functions for complex numbers (define (make-from-real-imag x y) (define (apply-generic op . args) ((get 'make-from-real-imag 'rectangular) (let ((type-tags (map type-tag args))) x (let ((proc (get op type-tags))) y)) (if proc (apply proc (map contents args)) (define (make-from-mag-ang r a) (error ((get 'make-from-mag-ang 'polar) r a)) "APPLY-GENERIC failed" (list op type-tags)))))) (note: operations can have more than one argument) Fall 2008 Programming Development 17 Fall 2008 Programming Development 18 Techniques Techniques 3
The selectors Three programming styles ; selectors are implemented in terms of the 1) Data-directed programming, where code for each ; generic operation operator and representation type combination is (define (real-part z) stored in a 2-dimensional operation table (apply-generic 'real-part z)) (define (imag-part z) 2) Conventional-style programming, where each (apply-generic 'imag-part z)) operator decides what code to run by testing the (define (magnitude z) representation types of its arguments (apply-generic 'magnitude z)) (define (angle z) In effect, each operator has one row of operation table built into it. (apply-generic 'angle z)) Fall 2008 Programming Development 19 Fall 2008 Programming Development 20 Techniques Techniques A message passing version Continued ; message passing version takes a real and imaginary ; part of a complex number and returns an intelligent data 3) Message passing, where each data object decides ; object that can dispatch its operations what code to run by testing the name of the operation (define (make-from-real-imag-mp x y) it is being asked to perform (define (dispatch op) (cond ((eq? op 'real-part) x) In effect, each data object has one column of the ((eq? op 'imag-part y) operation table built into it. ((eq? op 'magnitude)(sqrt (+ (square x) (square y)))) ((eq? op 'angle) (atan y x)) Instead of intelligent operations that know what kind of (else data they work on, each data object is intelligent and (error "MAKE-FROM-REAL-IMAG failed" can dispatch its own operations. op))))) dispatch) Fall 2008 Programming Development 21 Fall 2008 Programming Development 22 Techniques Techniques Uses a different apply-generic Advantage ; the individual parts are defined as above e.g. ;(define (real-part z) The main advantage of the message passing ; (apply-generic 'real-part z)) programming style is that it more effectively hides the ; etc. internal structure of data objects so that programmers are not tempted to access the insides of the data ; apply-generic for message passing version objects with cars and cdrs. ; notice that this function simply calls the data The programing style for classes in C+ + most closely ; object as an function applied to the operator resembles message passing. (define (apply-generic op arg) (arg op)) ;or more directly, (define (real-part-mp z) (z 'real-part)) Fall 2008 Programming Development 23 Fall 2008 Programming Development 24 Techniques Techniques 4
Recommend
More recommend