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 מבוא.

Slides:



Advertisements
Similar presentations
1 Lecture 16: Tables and OOP. 2 Tables -- get and put.
Advertisements

Fall 2008Programming Development Techniques 1 Topic 12 Multiple Representations of Abstract Data – Complex Numbers Section
Structure and Interpretation of Computer Programs* Section 2.4 – 2.5 Presenter: Xiang Zhang 05/04/ * Harold A., Gerald J. S., Julie S.. Structure.
데이타를 여러방식으로 구현할 때 Multiple Representations 데이터 구현방식마다 꼬리표를 붙이고. 데이터 속내용 감추기 원리를 유지하면서. 예를 들어 complex number data 구현방안들을 생각해 보자 – complex number 만들기 make-from-real-imag:
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
11.2 Geometric Representations of Complex Numbers.
Fall 2008Programming Development Techniques 1 Topic 15 Generic Arithmetic Operations Section &
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7. Data directed programming Section 2.4, pages ,2.5.2 pages (but with a different example)
SICP Data abstraction revisited Data structures: association list, vector, hash table Table abstract data type No implementation of an ADT is necessarily.
מבוא מורחב 1 Lecture #13. מבוא מורחב 2 Multiple representations of data.
6.001 SICP SICP – October Introduction Trevor Darrell 32-D512 Office Hour: W web page:
מבוא מורחב - שיעור 12 1 Lecture 12 Data directed programming Message passing dotted-tail notation & apply Section 2.4, pages ,2.5.2 pages.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. Data directed programming Message passing Section 2.4, pages ,2.5.2 pages (but with.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7. Outline Symbols Data Directed Programming, Message Passing.
Scheme Tutorial. Goals Combine several simple ideas into one compound idea to obtain complex ideas Bring two ideas together to obtain relations Seperate.
Trees! =) Write up-add tree which behaves in this manner: 1 31 / \ / \ 3 5  21 9 / | \ \ / | \ \
CS220 Programming Principles 프로그래밍의 이해 2002 년 가을학기 Class 8 한 태숙.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
Fall 2008Programming Development Techniques 1 Topic 13 Multiple Representations of Abstract Data – Tagged Data Section
1/38. 2/38 Tagged Data Tag: a symbol in a data structure that identifies its type Why we need tags Extended example: evaluating arithmetic expressions.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
SICP Tagged data Why do we need tags Concept of tags Extended example.
Fall 2008Programming Development Techniques 1 Topic 5 Data Abstraction Note: This represents a change in order. We are skipping to chapter 2 without finishing.
Fall 2008Programming Development Techniques 1 Topic 14 Multiple Representations of Abstract Data – Data Directed Programming and Additivity Section
מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real i 13 atan(2/3)
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 14: Object Oriented Programming 한 태숙.
1 Data Abstraction. Pairs and Lists. (SICP Sections – 2.2.1)
1/38 Tagged Data Tag: a symbol in a data structure that identifies its type Why we need tags Extended example: evaluating arithmetic expressions.
מבוא מורחב שיעור 7 1 Lecture 7 Data Abstraction. Pairs and Lists. (Sections – 2.2.1)
CS61A Lecture Colleen Lewis. Clicker poll How do you think the midterm compared to previous midterms online? A)Harder B)A little harder.
SICP Tagged data Why do we need tags Concept of tags Extended example.
Operational Semantics of Scheme
Functional Programming
Tagged Data Tag: a symbol in a data structure that identifies its type
Introduction to Parsing (adapted from CS 164 at Berkeley)
6.001 Jeopardy.
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
6.001 SICP Object Oriented Programming
The interpreter.
6.001: Structure and Interpretation of Computer Programs
The role of abstractions
Complex Number Arithmetic
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Env. Model Implementation
6.001 SICP Data abstractions
Functions BIS1523 – Lecture 17.
The Metacircular Evaluator
Lecture 18 Infinite Streams and
Lecture 15: Tables and OOP
Data abstraction, revisited
Dynamic Scoping Lazy Evaluation
Lecture #6 section pages pages72-77
Lecture 16: Tables and OOP
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
Lecture #9 מבוא מורחב.
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Lecture #6 section pages pages72-77
6.001 SICP Variations on a Scheme
Mutators for compound data Stack Queue
Lecture 14: The environment model (cont
Lecture #7 מבוא מורחב.
6.001 SICP Interpretation Parts of an interpreter
Lecture 13: Assignment and the Environment Model (EM)
Complex Numbers MAΘ
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Presentation transcript:

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 מבוא מורחב

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 מבוא מורחב

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(+)) מבוא מורחב

המימוש שמדמיין אריאל 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)))) מבוא מורחב

המימוש מהזווית של מני 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)))) מבוא מורחב

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

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

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

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

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

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

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: מבוא מורחב

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 …))) מבוא מורחב

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 מבוא מורחב

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

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.

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)

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

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

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

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 מבוא מורחב

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 מבוא מורחב

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