Download presentation
Presentation is loading. Please wait.
Published byShannon Beasley Modified over 9 years ago
1
CS220 Programming Principles 프로그래밍의 이해 2002 년 가을학기 Class 8 한 태숙
2
2 Strategies for Managing Data Complexity n Separate specification from implementation n Procedural interface to data (enables alternative representations) n Manifest typing (enables multiple representations) n Generic operations –Dispatch on type –Table-driven generic interface (Data-Directed Programming)
3
3 n Packages n Closure n Coercion n Message-Passing
4
4 Rectangular and Polar Coordinates n Data-abstraction barriers in complex-number +c, -c, *c, /c Complex-arithmetic package Rectangular representation Polar representation List structure and primitive machine arithmetic Programs that use complex numbers
5
5 Selector and Constructor n Constructor (make-rectangle (real-part z) (imag-part z)) (make-polar (magnitude z) (angle z)) n Selector real-part, imag-part magnitude, angle
6
6 Complex Numbers - Operators Complex Number Arithmetic (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))))
7
7 Dual Implementation(I) n Ben’s representation - rectangular coordinates (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)))
8
8 Dual Implementation(II) n Alyssa’s representation - Polar coordinates (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)))) (define (make-polar r a) (cons r a)) (define (magnitude z) (car z)) (define (angle z) (cdr z))
9
9 Tagged Data n In order to support both types of complex numbers we need to be able to guarantee that, –selectors return appropriate values, independent of their implementation n We need TYPED data object, where the types can be seen by inspection. –Constructor - attach-tag –Selector - type-tag, contents –Predicate - is-the-type?
10
10 Type Tag - Implementation (define (attach-tag type-tag contents) (cons type-tag contents) (define (type-tag datum) (if (pair? datum) (car datum) (error “Bad datum --TYPE-TAG” datum))) (define (contents datum) (if (pair? datum) (cdr datum) (error “Bad datum --CONSTENS” datum)))
11
11 Complex Numbers - Rectangular? Polar? n Two types (representations) - rectangular and polar (define (rectangular? z) (eq? (type-tag z) ’rectangular)) (define (polar? z) (eq? (type-tag z) ’polar)) n We must add on a type tag whenever we construct complex numbers. n Since both representations coexist, we need to change the names of the procedures so as to avoid name conflicts.
12
12 Revised rectangular representation(I) ;Rectangular = ’rectangular x RepRect (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)))))
13
13 Revised rectangular representation(II) ; Rectangular -> Sch-Num (define (real-part-rectangular z) (car (contents z))) (define (imag-part-rectangular z) (cdr (contents 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)))
14
14 Revised polar representation(I) ;Polar = ’polar x RepPolar (define (make-from-mag-ang-polar r a) (attach-tag ’polar (cons r a))) (define (make-from-real-imag-polar x y) (attach-tag ’polar (cons (sqrt (+ (square x) (square y))) (atan y x))))
15
15 Revised polar representation(II) ; Polar -> Sch-Num (define (magnitude-polar z) (car (contents z))) (define (angle-polar z) (cdr (contents z))) (define (real-part-polar z) (* (magnitude-polar z)) (cos(angle-polar z)))) (define (imag-part-polar z) (* (magnitude-polar z) (sin(angle-polar z))))
16
16 Generic Operators and Constructors(I) ;; Complex = Rectangular U Polar ;; Selectors: Complex ->Sch-Num (define (real-part z) (cond ((rectangular? z) (real-part-rectangular z)) ((polar? z) (real-part-polar z)) (else (error “Unknown type-REAL-PART” z))))
17
17 Generic Operators and Constructors(II) ;; Complex = Rectangular U Polar ;; Selectors: Complex ->Sch-Num (define (imag-part z) (cond ((rectangular? z) (imag-part-rectangular z)) ((polar? z) (imag-part-polar z)) (else (error “Unknown type-IMAG-PART” z))))
18
18 Generic Operators and Constructors(III) ;; Complex = Rectangular U Polar ;; Selectors: Complex ->Sch-Num (define (magnitude z) (cond ((rectangular? z) (magnitude-rectangular z)) ((polar? z) (magnitude-polar z)) (else (error “Unknown type-MAGNITUDE” z))))
19
19 Generic Operators and Constructors(IV) ;; Complex = Rectangular U Polar ;; Selectors: Complex ->Sch-Num (define (angle z) (cond ((rectangular? z) (angle-rectangular z)) ((polar? z) (angle-polar z)) (else (error “Unknown type-ANGLE” z))))
20
20 Generic Operators and Constructors(V) ;; make-rectangular type Complex ;; Constructor: Sch-Num, Sch-Num -> Complex (define (make-rectangular x y) (make-from-real-imag-rectangular x y)) ;; make-polar type Complex ;; Constructor: Sch-Num, Sch-Num -> Complex (define (make-polar r a) (make-from-mag-angle-polar r a))
21
21 Generic Complex-Arithmetic System n Data-abstraction barriers in complex-number +c, -c, *c, /c Complex-arithmetic package Rectangular representation Polar representation List structure and primitive machine arithmetic Programs that use complex numbers real-part imag-part magnitude angle
22
22 Problems with this Approach n Generic Selectors depending on Type Tag –Dispatching on Type n Generic interface procedures must know about all the different representation. –need to add a clause for the new tag n No two procedures in the entire system have the same name. –can be programmed separately, but not independent n ==> Not Additive!
23
23 Data-Directed Programming n Table Driven Generic Interface Table of complex-number system Types Polar Rectangular real-part real-part-polar real-part-rectangular imag-part imag-part-polar imag-part-rectangular magnitude magnitude-polar magnitude-rectangular anlge angle-polar angle-rectangular Operations
24
24 Implementing Table Driven Operations n We need two table-manipulating operations and generic application of procedure. (put op type item) – installs the item in the table, indexed by the op and the type (get op type) –looks up the op, type entry in the table and returns the item found there. If no item is found, get returns false.
25
25 Ben’s Rectangular Package (p182) (define (install-rectangular-package) ;; internal procedures ;; RepRec = Sch-Num x Sch-Num (define (real-part z) (car z)) (define (imag-part z) (cdr z)) (define (make-from-real-imag x y)(cons x y)) (define (magnitude z) (sqrt (+ (square (real-part z)) (square (imag-part z))))) (define (angle z) (atan (imag-part z)(real-part z))) (define (make-from-mag-ang r a) (cons (* r (cos a)) (* r (sin a))))
26
26 ;;interface to the rest of the system (define (tag x) (attach-tag ’rectangular x)) (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)
27
27 Alyssa’s Polar Package (p183) (define (install-polar-packaage) ;; internal procedures (define (magnitude z) (car z)) (define (angle z) (cdr z)) (define (make-from-mag-ang r a)(cons r a)) (define (real-part z) (* (magnitude z) (cos (angle z)))) (define (imag-part z) (* (magnitude z) (sin (angle z)))) (define (make-from-real-imag x y) (cons (sqrt (+ (square x) (square y))) (atan y x)))
28
28 ;;interface to the rest of the system (define (tag x) (attach-tag ’polar x)) (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)
29
29 General “operation” procedure n Apply-generic –applies a generic operation to some argument –searches table with type and operation and applies the resulting procedure if one is present (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- apply”)))))
30
30 Generic Selectors Using Apply-generic (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))
31
31 Constructors Exported ; export constructor outside of the package (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))
32
32 Examples to be traced (define my-z (make-from-real-imag 3 4)) (real-part my-z)
33
33 Message passing n Decompose table into columns n Make object as dispatch procedures –dispatch on operation names by typed object n A data object receives the requested operation name as a “message.”
34
34 Message passing (Example) (define (make-from-real-immag 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” op)))) dispatch) (define (apply-generic op arg) (arg op))
35
35 System with Generic Operations +c, -c, *c, /c Complex-arithmetic Rectangular representation Polar representation List structure and primitive machine arithmetic Programs that use numbers real-part imag-part magnitude angle + - * / add-rat sub-rat mul-rat div-rat Rational arithmetic Ordinary arithmetic add sub mul div Generic arithmetic package
36
36 Adding Other kinds of Numbers n Ordinary numbers: (define (make-scheme-number n) (attach-tag ’scheme-number n)) (define (scheme-number? n) (eq? (type-tag n) ’scheme-number)) (define (add-scheme-number n1 n2) (make-scheme-number (+ n1 n2))) (define (sub-scheme-number n1 n2) (make-scheme-number (- n1 n2))) and ditto for mul div
37
37 Generic add using dispatch on type n complex, rationals, ordinary number (define add x y) (cond ((and (complex? x) (complex? y)) (add-generic-complex (contents x) (contents y))) ((and (rational? x) (rational? y)) (add-rat (contents x)(contents y)) ((and (scheme-number? x) (scheme-number? y)) (add-scheme-number (contents x) (contents y)))))
38
38 Generic Arithmetic Operation n add –add-rat on rational numbers –add-complex on complex numbers..... (define (add x y) (apply-generic ’add x y)) (define (sub x y) (apply-generic ’sub x y)) (define (mul x y) (apply-generic ’mul x y)) (define (div x y) (apply-generic ’div x y))
39
39 Ordinary Numbers (p189) (defne (install-scheme-number-package) ;; Internal rep : RepNum = Sch-Num ;; internal procedures - just use scheme! ;; External rep: Scheme-Number = ;; ’scheme-number x RepNum (define (tag x) (attach-tag ’scheme-number x)) (put ’add ’(scheme-number scheme-number) (lambda (x y) (tag (+ x y)))) (put ’sub ’(scheme-number scheme-number) (lambda (x y) (tag (- x y)))) (put ’mul ’(scheme-number scheme-number) (lambda (x y) (tag (* x y))))
40
40 (put ’div ’(scheme-number scheme-number) (lambda (x y) (tag (/ x y)))) (put ’make ’scheme-number (lambda (x) (tag x))) ’done) ;; External constructor for ordinary numbers: ;; Sch-Num --> Number (define (make-scheme-number n) ((get ’make ’scheme-number) n))
41
41 Rational Number - generic number arith. (define (install-rational-package) ;;Internal rep: RepRat = Generic-Num x Generic-Num ;; internal procedure on RepRat (define (make-rat n d) (cons n d)) (define (numer x) (car x)) (define (denom x) (cdr x)) (define (add-rat x y) (make-rat (add (mul (numer x)(denom y)) (mul (denom x)(numer y))) (mul (denom x)(denom y)))) (define (sub-rat x y) (make-rat (sub (mul (numer x)(denom y)) (mul (denom x)(numer y))) (mul (denom x)(denom y))))
42
42 (define (mul-rat x y) (make-rat (mul (numer x)(numer y)) (mul (denom x)(denom y)))) (define (div-rat x y) (make-rat (mul (numer x)(denom y)) (mul (denom x)(numer y)))) ;; External rep: Rational = ’rational x RepRat (define (tag x) (attach-tag ’rational x)) (put ’make ’rational (lambda (n d) (tag (make-rat n d)))) (put ’add ’(rational rational) (lambda (x y) (tag (add-rat x y)))) (put ’sub ’(rational rational) (lambda (x y) (tag (sub-rat x y))))
43
43 (put ’mul ’(rational rational) (lambda (x y) (tag (mul-rat x y)))) (put ’div ’(rational rational) (lambda (x y) (tag (div-rat x y)))) ‘done) ;; External constructor interface (define (make-rational n d) ((get ’make ’rational) n d))
44
44 Complex Package (p191) (define (install-complex-package) ;; Internal Rep: RepComplex = Rectangular U Polar ;; import from rectangular and polar packaages (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)) ;; internal definitions.... (define (+c z1 z2) (make-from-real-imag (add (real-part z1)(real-part z2)) (add (imag-part z1)(imag-part z2))))
45
45 (define (-c z1 z2) (make-from-real-imag (sub (real-part z1)(real-part z2)) (sub (imag-part z1)(imag-part z2)))) (define (*c z1 z2) (make-from-mag-ang (mul (magnitude z1)(magnitude z2)) (add (angle z1)(angle z2)))) (define (/c z1 z2) (make-from-mag-ang (div (magnitude z1)(magnitude z2)) (sub (angle z1)(angle z2))))
46
46 ;; interface to rest of system - export to table (define (tag z) (attach-tag ’complex)) (put ’add ’(complex complex) (lambda (z1 z2) (tag (+c z1 z2)))) (put ’sub ’(complex complex) (lambda (z1 z2) (tag (-c z1 z2)))) (put ’mul ’(complex complex) (lambda (z1 z2) (tag (*c z1 z2)))) (put ’div ’(complex complex) (lambda (z1 z2) (tag (/c z1 z2)))) (put ’make-from-real-imag ’compex (lambda (x y) (tag (make-from-real-imag x y)))) (put ’make-from-mag-ang ’compex (lambda (r a) (tag (make-from-mag-ang r a)))) ’done)
47
47 ;; Exterrnal constructor interface (define (make-complex-from-real-imag x y) ((get ’make-from-real-imag ’compex) x y)) (define (make-complex-from-mag-ang r a) ((get ’make-from-mag-ang ’compex) r a)) complexrectangular34 3+4i
48
48 Generic add using dispatch on type n complex, rationals, ordinary number (define add x y) (cond ((and (complex? x) (complex? y)) (add-generic-complex (contents x) (contents y))) ((and (rational? x) (rational? y)) (add-rat (contents x)(contents y)) ((and (scheme-number? x) (scheme-number? y)) (add-scheme-number (contents x) (contents y)))))
49
49 Generic Operation in Generic operation? n Generic call to ADD, MUL extend our system to deal with arbitrarily complex data types. (define (+c z1 z2) (make-from-real-imag (ADD (real-part z1)(real-part z2)) (ADD (imag-part z1)(imag-part z2)))) (define (add-rat x y) (make-rat (ADD (MUL (numer x) (denom y)) (MUL (numer y) (denom x))) (MUL (denom x) (denom y))))
50
50 Example to be traced (define 2+5i (make-complex-from-real-imag (make-scheme-number 2) (make-scheme-number 5))) (define 4+6i (make-complex-from-real-imag (make-scheme-number 4) (make-scheme-number 6))) (define 1+3i (make-complex-from-real-imag (make-scheme-number 1) (make-scheme-number 3))) (define 2+3i (make-complex-from-real-imag (make-scheme-number 2) (make-scheme-number 3))) (define 2+5i/4+6i (make-rational 2+5i 4+6i) (define 1+3i/2+3i (make-rational 1+3i 2+3i)
51
51 Now try manipulating..... (add 2+5i/4+6i 1+3i/2+3i) (apply-generic ’add ’(rational (complex (rect (scheme-num 2)(scheme-num 5))) (complex (rect (scheme-num 4)(scheme-num 6)))) ’(rational (complex (rect (scheme-num 1)(scheme-num 3))) (complex (rect (scheme-num 2)(scheme-num 3)))) ==> applying ’add and ’(rational rational)
52
52 (attach-tag ’rational (add-rat ’((complex (rect (scheme-num 2)(scheme-num 5))) (complex (rect (scheme-num 4)(scheme-num 6)))) ’((complex (rect (scheme-num 1)(scheme-num 3))) (complex (rect (scheme-num 2)(scheme-num 3)))))).... (attach-tag ’rational (make-rat (add ’(complex (polar (scheme-num 19.42)(scheme-num 124.4))) (complex (polar(scheme-num 14.14)(scheme-num 135.0)))) ’(complex (polar (scheme-num 36.76)(scheme-num 112.6)))))
53
53 Combining Data of Different Types n adding complex and scheme-number? ;; to be included in the complex package (define (add-complex-to-schemenum z x) (make-from-real-imag (+ (real-part z) x) (imag-part z))) (put ’add ’(complex scheme-number) (lambda (z x) (tag (add-complex-to-schemenum z x)))) How may entries in the table???
54
54 Coercion n Forcing a data object of one type to convert to a data object of a different type: (define (scheme-number->complex n) (make-complex-from-real-imag (contents n) 0) Then install the procedure in a special coercion table indexed under the names of the two type (put-coercion ’scheme-number ’complex scheme-number->complex)
55
55 Revised Apply-general for coercion n See text-book page 196 n When does coercion work well, and when does it fail?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.