Fall 2008Programming Development Techniques 1 Topic 15 Generic Arithmetic Operations Section 2.5.1 & 2.5.2.

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.
Intermediate Code Generation
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
Compiler Construction
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7. Data directed programming Section 2.4, pages ,2.5.2 pages (but with a different example)
מבוא מורחב 1 Lecture #13. מבוא מורחב 2 Multiple representations of data.
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
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.
1 Functional languages (e.g. Scheme, ML) Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition.
מבוא מורחב למדעי המחשב בשפת 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.
Trees! =) Write up-add tree which behaves in this manner: 1 31 / \ / \ 3 5  21 9 / | \ \ / | \ \
CS220 Programming Principles 프로그래밍의 이해 2002 년 가을학기 Class 8 한 태숙.
14-October-2002cse Lists © 2002 University of Washington1 Lists CSE 413, Autumn 2002 Programming Languages
Today’s topic: Abstraction Compound Data Data Abstractions: Isolate use of data abstraction from details of implementation Relationship between data abstraction.
Fall 2008Programming Development Techniques 1 Topic 13 Multiple Representations of Abstract Data – Tagged Data Section
FRACTIONSFRACTIONS Definition: A fraction is a part of a whole.
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)
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 한 태숙.
Fall 2008Programming Development Techniques 1 Topic 20 Concurrency Section 3.4.
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)
Forms Writing your own procedures CS 480/680 – Comparative Languages.
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.
Abstraction A way of managing complexity for large programs A means of separating details of computation from use of computation Types of Abstraction Data.
Additional Scheme examples
Tagged Data Tag: a symbol in a data structure that identifies its type
6.001 Jeopardy.
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
Env. Model Implementation
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 #6 מבוא מורחב.
Lecture #8 מבוא מורחב.
Lecture 16: Tables and OOP
6.001 SICP Further Variations on a Scheme
Lecture 12: Message passing The Environment Model
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 11: Multiple representations of abstract data Message passing Overloading Section 2.4, pages ,2.5.2 pages מבוא.
6.001 SICP Data abstractions
Lecture #7 מבוא מורחב.
Today’s topics Abstractions Procedural Data
6.001 SICP Interpretation Parts of an interpreter
Lecture # , , , , מבוא מורחב.
Lecture 13: Assignment and the Environment Model (EM)
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Presentation transcript:

Fall 2008Programming Development Techniques 1 Topic 15 Generic Arithmetic Operations Section & 2.5.2

Fall 2008Programming Development Techniques 2 Systems with Generic Operations Previous section: designed systems in which data objects can be represented in more than one way Key idea: link the code that specifies the data operations to the several representations via generic interface procedures. Extend this idea: define operations that are generic over different representations AND over differet kinds of arguments. Several different arithmetic packages: regular, rational, complex – let’s put them all together

Fall 2008Programming Development Techniques 3 Generic Use of Numbers

Fall 2008Programming Development Techniques 4 Generic operations We want to do arithmetic with any combination of ordinary numbers, rational numbers and complex numbers We'll use the data-directed programming style Ordinary Scheme numbers will have to be represented and tagged like all the rest

Fall 2008Programming Development Techniques 5 Basic arithmetic operations ; generic operations definitions to use we would need to ; attach a tag to each kind of number and cause the ; generic procedure to dispatch the appropriate package ; for the data types of its arugments (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))

Fall 2008Programming Development Techniques 6 Scheme number package ; package for handling ordinary scheme numbers ; note the key is (scheme-number scheme-number) ; since each takes two arguments, both of which ; are ordinary scheme-numbers (define (install-scheme-number-package) (define (tag x) (attach-tag 'scheme-number x)) (put 'add '(scheme-number scheme-number) (lambda (x y) (tag (+ x y))))

Fall 2008Programming Development Techniques 7 continued (put 'sub '(scheme-number scheme-number) (lambda (x y) (tag (- x y)))) (put 'mul '(scheme-number scheme-number) (lambda (x y) (tag (* x y)))) (put 'div '(scheme-number scheme-number) (lambda (x y) (tag (/ x y)))) (put 'make 'scheme-number (lambda (x) (tag x))) 'done)

Fall 2008Programming Development Techniques 8 Scheme-number constructor ; user of the scheme-number package will create ; tagged ordinary numbers by means of the make ; procedure (define (make-scheme-number n) ((get 'make 'scheme-number) n))

Fall 2008Programming Development Techniques 9 Rational number package ; package for performing rational arithmetic (define (install-rational-package) ; internal procedures (define numer car) (define denom cdr) (define (make-rat n d) (let ((g (gcd n d))) (cons (/ n g) (/ d g)))) (define (tag x) (attach-tag 'rational x))

Fall 2008Programming Development Techniques 10 rational package continued ; interface to rest of system (put 'add '(rational rational) (lambda (x y) (tag (make-rat (+ (* (numer x) (denom y)) (* (numer y) (denom x))) (* (denom x) (denom y))))))

Fall 2008Programming Development Techniques 11 subtraction (put 'sub '(rational rational) (lambda (x y) (tag (make-rat (- (* (numer x) (denom y)) (* (numer y) (denom x))) (* (denom x) (denom y))))))

Fall 2008Programming Development Techniques 12 multiplication (put 'mul '(rational rational) (lambda (x y) (tag (make-rat (* (numer x) (numer y)) (* (denom x) (denom y))))))

Fall 2008Programming Development Techniques 13 division (put 'div '(rational rational) (lambda (x y) (tag (make-rat (* (numer x) (denom y)) (* (denom x) (numer y))))))

Fall 2008Programming Development Techniques 14 constructor code (put 'make 'rational (lambda (n d) (tag (make-rat n d)))) 'done) (define (make-rational n d) ((get 'make 'rational) n d))

Fall 2008Programming Development Techniques 15 Complex number package ; package for handling complex numbers (define (install-complex-package) ; imported procedures from rectangular and ; polar packages (define (make-from-real-imag x y) ((get 'make-from-real-imag 'rectangular) x y)) ; interface to rest of the system (define (make-from-mag-ang r a) ((get 'make-from-mag-ang 'polar) r a)) (define (tag z) (attach-tag 'complex z))

Fall 2008Programming Development Techniques 16 Addition (put 'add '(complex complex) (lambda (z1 z2) (tag (make-from-real-imag (+ (real-part z1) (real-part z2)) (+ (imag-part z1) (imag-part z2))))))

Fall 2008Programming Development Techniques 17 Subtraction (put 'sub '(complex complex) (lambda (z1 z2) (tag (make-from-real-imag (- (real-part z1) (real-part z2)) (- (imag-part z1) (imag-part z2))))))

Fall 2008Programming Development Techniques 18 Multiplication (put 'mul '(complex complex) (lambda (z1 z2) (tag (make-from-mag-ang (* (magnitude z1) (magnitude z2)) (+ (angle z1) (angle z2))))))

Fall 2008Programming Development Techniques 19 Division (put 'div '(complex complex) (lambda (z1 z2) (tag (make-from-mag-ang (/ (magnitude z1) (magnitude z2)) (- (angle z1) (angle z2))))))

Fall 2008Programming Development Techniques 20 Constructors (put 'make-from-real-imag 'complex (lambda (x y) (tag (make-from-real-imag x y)))) (put 'make-from-mag-ang 'complex (lambda (r a) (tag (make-from-mag-ang r a)))) 'done)

Fall 2008Programming Development Techniques 21 continued ; exporting complex numbers to outside world (define (make-complex-from-real-imag x y) ((get 'make-from-real-imag 'complex) x y)) (define (make-complex-from-mag-ang r a) ((get 'make-from-mag-ang 'complex) r a))

Fall 2008Programming Development Techniques 22 Complex Numbers – two levels of export Notice that complex numbers for a two-level tag system. A typical complex number e.g., 1 + 2i in rectangular form will be represented as (complex (rectangular 1. 2)) First tag directs to complex number package, once there, second tag directs to rectangular package.

Fall 2008Programming Development Techniques 23 Installing it all ; run the procedures to set up the operations table ; operation-table will hold the triples of ; operations, their type, and the associated action (install-scheme-number-package) (install-rational-package) (install-rectangular-package) (install-polar-package) (install-complex-package)

Fall 2008Programming Development Techniques 24 Combining Data of Different Types One approach: (put 'add '(complex scheme-number) (lambda (z x) (tag (make-from-real-imag (+ (real-part z) x) (imag-part z)))))) Awkward when there are many combinations

Fall 2008Programming Development Techniques 25 A better way – transform objects of one type into another type ;; to allow operations between mixed types ;; must allow coercion between types ; puts a procedure for coercing a scheme-number ; into a rational number (put 'coerce 'scheme-number (lambda (n) (make-rational (contents n) 1)))

Fall 2008Programming Development Techniques 26 Coerce a rational number into a complex one ; puts a procedure for coercing a rational ; number into a complex number (put 'coerce 'rational (lambda (r) (make-complex-from-real-imag (/ (car (contents r)) (cdr (contents r)))

Fall 2008Programming Development Techniques 27 Precedence info to control coercion When a mixed procedure is encountered, must try to coerce arguments. Thus, must know which way coercion can occur. To do this, put precedence information in the operations table. ;; must put precedence information in the operation ;; table to control coercion (put 'scheme-number 'rational 'precedence) (put 'scheme-number 'complex 'precedence) (put 'rational 'complex 'precedence)

Fall 2008Programming Development Techniques 28 Revised apply-generic ; new apply-generic procedure attempts to ; do type coercion if no operator with the ; appropriate type is defined in the operation ; table (define (apply-generic op. args) (let ((type-tags (map type-tag args))) (let ((proc (get op type-tags)))

Fall 2008Programming Development Techniques 29 continued (if proc ; if procedure of appropraite type ; exists apply that procedure (apply proc (map contents args))

Fall 2008Programming Development Techniques 30 ; otherwise, attempt to do coercion if ; there are two arguments (if (= (length args) 2) (let ((t1 (car type-tags)) (t2 (cadr type-tags)) (a1 (car args)) (a2 (cadr args))) (let ((t1up (get 'coerce t1)) (t2up (get 'coerce t2)) (p1 (get t1 t2)) (p2 (get t2 t1)))

Fall 2008Programming Development Techniques 31 ; t1up and t2up contain coercion ; procedures if they exist ; p1 and p2 tell whether precedence is correct (cond (p1 (apply-generic op (t1up a1) a2)) (p2 (apply-generic op a1 (t2up a2)))

Fall 2008Programming Development Techniques 32 (else (error "no method" (list op type-tags)))))) (error "no method" (list op type-tags)))))))