Fall 2008Programming Development Techniques 1 Topic 14 Multiple Representations of Abstract Data – Data Directed Programming and Additivity Section 2.4.3.

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
데이타를 여러방식으로 구현할 때 Multiple Representations 데이터 구현방식마다 꼬리표를 붙이고. 데이터 속내용 감추기 원리를 유지하면서. 예를 들어 complex number data 구현방안들을 생각해 보자 – complex number 만들기 make-from-real-imag:
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Fall 2008Programming Development Techniques 1 Topic 15 Generic Arithmetic Operations Section &
Fall 2008Programming Development Techniques 1 Topic 19 Mutable Data Objects Section 3.3.
מבוא מורחב למדעי המחשב בשפת 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.
Data Abstraction… The truth comes out…. What we’re doing today… Abstraction ADT: Dotted Pair ADT: List Box and Pointer List Recursion Deep List Recursion.
מבוא מורחב 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.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Scheme Tutorial. Goals Combine several simple ideas into one compound idea to obtain complex ideas Bring two ideas together to obtain relations Seperate.
Plt /12/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.3 Representation Strategies for Data Types.
Trees! =) Write up-add tree which behaves in this manner: 1 31 / \ / \ 3 5  21 9 / | \ \ / | \ \
CS220 Programming Principles 프로그래밍의 이해 2002 년 가을학기 Class 8 한 태숙.
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.
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.
מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real i 13 atan(2/3)
6.001 SICP 1/ SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 14: Object Oriented Programming 한 태숙.
SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams Instance.
Fall 2008Programming Development Techniques 1 Topic 8 Sequences as Conventional Interfaces Section October 2008.
1 Data Abstraction. Pairs and Lists. (SICP Sections – 2.2.1)
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
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.
Tagged Data Tag: a symbol in a data structure that identifies its type
6.001 SICP Variations on a Scheme
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
6.001 SICP Object Oriented Programming
6.001 SICP Compilation Context: special purpose vs. universal machines
The interpreter.
The role of abstractions
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
COP4020 Programming Languages
Env. Model Implementation
6.001 SICP Data abstractions
The Metacircular Evaluator
Lecture 18 Infinite Streams and
Lecture 15: Tables and OOP
Data abstraction, revisited
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Lecture 16: Tables and OOP
6.001 SICP Further Variations on a Scheme
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
Data Mutation Primitive and compound data mutators set! for names
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
6.001 SICP Variations on a Scheme
Mutators for compound data Stack Queue
6.001 SICP Data Mutation Primitive and Compound Data Mutators
Lecture 14: The environment model (cont
Lecture 11: Multiple representations of abstract data Message passing Overloading Section 2.4, pages ,2.5.2 pages מבוא.
Announcements Quiz 5 HW6 due October 23
6.001 SICP Interpretation Parts of an interpreter
Lecture 13: Assignment and the Environment Model (EM)
topics interpreters meta-linguistic abstraction eval and apply
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Presentation transcript:

Fall 2008Programming Development Techniques 1 Topic 14 Multiple Representations of Abstract Data – Data Directed Programming and Additivity Section 2.4.3

Fall 2008Programming Development Techniques 2 Data-Directed Programming Consider ; takes a tagged complex number and returns ; the real part (define (real-part z) (cond ((rectangular? z) (real-part-rectangular (contents z))) ((polar? z) (real-part-polar (contents z))) (else (error "data type unknown to REAL-PART" z))))

Fall 2008Programming Development Techniques 3 Problems with that code Each interface procedure needs to know about all representations possible As more representation types are added, the procedure has to be rewritten Procedure gets longer Procedure gets more complicated Procedure is one big conditional statement Each representation alternative must be coded with no name conflicts with any other representation alternative.

Fall 2008Programming Development Techniques 4 The data-directed programming technique Replaces the conditional statement with a lookup table The chunks of code that were in the branches of the conditional are now indexed by the two dimensions of the lookup table (procedure X representation type)

Fall 2008Programming Development Techniques 5 Lookup table for complex numbers Operations Representation types polar rectangular real-part real-part-polar real-part-rectangular imag-part imag-part-polar imag-part-rectangular magnitude magnitude-polar magnitude-rectangular angle angle-polar angle-rectangular (named procedures or lambda expressions)

Fall 2008Programming Development Techniques 6 A prototype implementat of lookup table Basic operations are put and get Book's implementation isn't until the next chapter Our implementation is simple, suitable for rapid prototyping, but not the most efficient Can be replaced later by the book's better implementation without changing code built on top of it

Fall 2008Programming Development Techniques 7 Basic idea of our implementation Lookup table is just a list of triples of the form (operation type action) Simulates a sparse array of infinite size Put adds a triple to the list Get searches the list for the right triple and returns the action part The type is a list of the representation types of the operation's arguments

Fall 2008Programming Development Techniques 8 And now for the implementation ; operation-table will hold the triples of ; operations, their type, and the associated action (define operation-table empty) ; takes an operation, the type of data it acts on, and an ; action that implements the operation on the type given. ; Adds the triple to the operation table (define (put operation type action) (set! operation-table (cons (list operation type action) operation-table ))) (Forget you saw set!, the reassignment operator, until the next chapter.)

Fall 2008Programming Development Techniques 9 The get procedure ; takes an operator and a type, and returns the ; action that implements the operator for that type ; in the operation-table (define (get operator type) (define (get-aux list) (cond ((null? list) #f) ((and (equal? operator (caar list)) (equal? type (cadar list))) (caddar list)) (else (get-aux (cdr list))))) (get-aux operation-table))

Fall 2008Programming Development Techniques 10 So? What do we do with it? Define a collection of procedures or a package for each representation These are installed in the table Complex-arithmetic selectors access the table by means of a general “operation” procedure called apply-generic

Fall 2008Programming Development Techniques 11 Each representation type in its own package ; define the selectors in the rectangular representation ; each selector is defined as it was originally – and ; must put installed into the operation table (define (install-rectangular-package) (put 'real-part '(rectangular) car) (put 'imag-part '(rectangular) cdr) (put 'magnitude '(rectangular) (lambda (z) (sqrt (+ (square (car z)) (square (cdr z))))))

Fall 2008Programming Development Techniques 12 Rectangular package continued (put 'angle '(rectangular) (lambda (z) (atan (cdr z) (car z)))) (put 'make-from-real-imag 'rectangular (lambda (x y) (attach-tag 'rectangular (cons x y))))

Fall 2008Programming Development Techniques 13 part 3 (put 'make-from-mag-ang 'rectangular (lambda (r a) (attach-tag 'rectangular (cons (* r (cos a)) (* r (sin a)))))) 'done)

Fall 2008Programming Development Techniques 14 Polar package ; define the selectors in the polar representation ; each selector is defined as it was originally -- ; and must put installed into the operation (define (install-polar-package) (put 'magnitude '(polar) car) (put 'angle '(polar) cdr) (put 'real-part '(polar) (lambda (z) (* (car z) (cos (cdr z)))))

Fall 2008Programming Development Techniques 15 Polar package continued (put 'imag-part '(polar) (lambda (z) (* (car z) (sin (cdr z))))) (put 'make-from-mag-ang 'polar (lambda (r a) (attach-tag 'polar (cons r a))))

Fall 2008Programming Development Techniques 16 Part 3 (put 'make-from-real-imag 'polar (lambda (x y) (attach-tag 'polar (cons (sqrt (+ (square x) (square y))) (atan y x))))) 'done)

Fall 2008Programming Development Techniques 17 The constructors ;; THE CONSTRUCTORS (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))

Fall 2008Programming Development Techniques 18 Generic operation call ;;; general operation will implement selector ;; functions for complex numbers (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 "APPLY-GENERIC failed" (list op type-tags)))))) (note: operations can have more than one argument)

Fall 2008Programming Development Techniques 19 The selectors ; selectors are implemented in terms of the ; generic operation (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))

Fall 2008Programming Development Techniques 20 Three programming styles 1)Data-directed programming, where code for each operator and representation type combination is stored in a 2-dimensional operation table 2) Conventional-style programming, where each operator decides what code to run by testing the representation types of its arguments In effect, each operator has one row of operation table built into it.

Fall 2008Programming Development Techniques 21 Continued 3) Message passing, where each data object decides what code to run by testing the name of the operation it is being asked to perform In effect, each data object has one column of the operation table built into it. Instead of intelligent operations that know what kind of data they work on, each data object is intelligent and can dispatch its own operations.

Fall 2008Programming Development Techniques 22 A message passing version ; message passing version takes a real and imaginary ; part of a complex number and returns an intelligent data ; object that can dispatch its operations (define (make-from-real-imag-mp 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 "MAKE-FROM-REAL-IMAG failed" op))))) dispatch)

Fall 2008Programming Development Techniques 23 Uses a different apply-generic ; the individual parts are defined as above e.g. ;(define (real-part z) ; (apply-generic 'real-part z)) ; etc. ; apply-generic for message passing version ; notice that this function simply calls the data ; object as an function applied to the operator (define (apply-generic op arg) (arg op)) ;or more directly, (define (real-part-mp z) (z 'real-part))

Fall 2008Programming Development Techniques 24 Advantage The main advantage of the message passing programming style is that it more effectively hides the internal structure of data objects so that programmers are not tempted to access the insides of the data objects with cars and cdrs. The programing style for classes in C++ most closely resembles message passing.