cs220 programming principles

55
CS220 Programming Principles 프프프프프프 프프 2002 프 프프프프 Class 8 프프

Upload: erling

Post on 22-Jan-2016

41 views

Category:

Documents


0 download

DESCRIPTION

CS220 Programming Principles. 프로그래밍의 이해 2002 년 가을학기 Class 8 한 태숙. Strategies for Managing Data Complexity. Separate specification from implementation Procedural interface to data (enables alternative representations) Manifest typing (enables multiple representations) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS220 Programming Principles

CS220Programming Principles

프로그래밍의 이해2002 년 가을학기 Class 8

한 태숙

Page 2: CS220 Programming Principles

2

Strategies for Managing Data Complexity

Separate specification from implementation Procedural interface to data

(enables alternative representations) Manifest typing

(enables multiple representations) Generic operations

– Dispatch on type– Table-driven generic interface (Data-Directed Progr

amming)

Page 3: CS220 Programming Principles

3

Packages Closure Coercion Message-Passing

Page 4: CS220 Programming Principles

4

Rectangular and Polar Coordinates

Data-abstraction barriers in complex-number

+c, -c, *c, /c

Complex-arithmetic package

Rectangularrepresentation

Polarrepresentation

List structure and primitive machine arithmetic

Programs that use complex numbers

Page 5: CS220 Programming Principles

5

Selector and Constructor

Constructor(make-rectangle (real-part z)

(imag-part z))

(make-polar (magnitude z)

(angle z))

Selectorreal-part, imag-part

magnitude, angle

Page 6: CS220 Programming Principles

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))))

Page 7: CS220 Programming Principles

7

Dual Implementation(I)

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)))

Page 8: CS220 Programming Principles

8

Dual Implementation(II)

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))

Page 9: CS220 Programming Principles

9

Tagged Data

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 We need TYPED data object, where the types

can be seen by inspection.– Constructor - attach-tag– Selector - type-tag, contents– Predicate - is-the-type?

Page 10: CS220 Programming Principles

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)))

Page 11: CS220 Programming Principles

11

Complex Numbers - Rectangular? Polar?

Two types (representations) -rectangular and polar(define (rectangular? z)

(eq? (type-tag z) ’rectangular))

(define (polar? z)

(eq? (type-tag z) ’polar))

We must add on a type tag whenever we construct complex numbers.

Since both representations coexist, we need to change the names of the procedures so as to avoid name conflicts.

Page 12: CS220 Programming Principles

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)))))

Page 13: CS220 Programming Principles

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)))

Page 14: CS220 Programming Principles

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))))

Page 15: CS220 Programming Principles

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))))

Page 16: CS220 Programming Principles

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))))

Page 17: CS220 Programming Principles

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))))

Page 18: CS220 Programming Principles

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))))

Page 19: CS220 Programming Principles

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))))

Page 20: CS220 Programming Principles

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))

Page 21: CS220 Programming Principles

21

Generic Complex-Arithmetic System

Data-abstraction barriers in complex-number

+c, -c, *c, /c

Complex-arithmetic package

Rectangularrepresentation

Polarrepresentation

List structure and primitive machine arithmetic

Programs that use complex numbers

real-part imag-partmagnitude angle

Page 22: CS220 Programming Principles

22

Problems with this Approach

Generic Selectors depending on Type Tag– Dispatching on Type

Generic interface procedures must know about all the different representation.– need to add a clause for the new tag

No two procedures in the entire system have the same name.– can be programmed separately, but not independent

==> Not Additive!

Page 23: CS220 Programming Principles

23

Data-Directed Programming

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-rectangularOperation

s

Page 24: CS220 Programming Principles

24

Implementing Table Driven Operations

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.

Page 25: CS220 Programming Principles

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))))

Page 26: CS220 Programming Principles

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)

Page 27: CS220 Programming Principles

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)))

Page 28: CS220 Programming Principles

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)

Page 29: CS220 Programming Principles

29

General “operation” procedure

Apply-generic– applies a generic operation to some argument– searches table with type and operation and applies t

he 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”)))))

Page 30: CS220 Programming Principles

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))

Page 31: CS220 Programming Principles

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))

Page 32: CS220 Programming Principles

32

Examples to be traced

(define my-z (make-from-real-imag 3 4))

(real-part my-z)

Page 33: CS220 Programming Principles

33

Message passing

Decompose table into columns Make object as dispatch procedures

– dispatch on operation names by typed object A data object receives the requested operation

name as a “message.”

Page 34: CS220 Programming Principles

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))

Page 35: CS220 Programming Principles

35

System with Generic Operations

+c, -c, *c, /c

Complex-arithmetic

Rectangularrepresentation

Polarrepresentation

List structure and primitive machine arithmetic

Programs that use numbers

real-part imag-partmagnitude angle

+ - * /add-rat sub-ratmul-rat div-rat

Rationalarithmetic

Ordinaryarithmetic

add sub mul div

Generic arithmetic package

Page 36: CS220 Programming Principles

36

Adding Other kinds of Numbers 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

Page 37: CS220 Programming Principles

37

Generic add using dispatch on type 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)))))

Page 38: CS220 Programming Principles

38

Generic Arithmetic Operation

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))

Page 39: CS220 Programming Principles

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))))

Page 40: CS220 Programming Principles

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))

Page 41: CS220 Programming Principles

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))))

Page 42: CS220 Programming Principles

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))))

Page 43: CS220 Programming Principles

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))

Page 44: CS220 Programming Principles

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))))

Page 45: CS220 Programming Principles

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))))

Page 46: CS220 Programming Principles

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)

Page 47: CS220 Programming Principles

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))

complex rectangular 3 4

3+4i

Page 48: CS220 Programming Principles

48

Generic add using dispatch on type 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)))))

Page 49: CS220 Programming Principles

49

Generic Operation in Generic operation?

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))))

Page 50: CS220 Programming Principles

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)

Page 51: CS220 Programming Principles

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)

Page 52: CS220 Programming Principles

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)))))

Page 53: CS220 Programming Principles

53

Combining Data of Different Types

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???

Page 54: CS220 Programming Principles

54

Coercion

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)

Page 55: CS220 Programming Principles

55

Revised Apply-general for coercion

See text-book page 196

When does coercion work well, and when does it fail?