Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 12: Message passing The Environment Model

Similar presentations


Presentation on theme: "Lecture 12: Message passing The Environment Model"— Presentation transcript:

1 Lecture 12: Message passing The Environment Model
Section 2.4, pages 2.5.1,2.5.2 pages Starting Chapter 3 מבוא מורחב

2 Last Lecture: Representing Complex numbers
3 2 3 + 2i a+bi 13  =atan(2/3) = r(cos  + i sin ) = r ei r= imaginary real מבוא מורחב

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

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

5 Data directed programming (Cont)
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 מבוא מורחב

6 Data-directed programming (Cont)
We assumed we have (put <op> <type> <item>) (get <op> <type>) 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)) 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. מבוא מורחב

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

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

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

10 Today: Message Passing Programming
Data Directed: Intelligent methods that work with different data types. Message Passing: Intelligent data types that work with different methods. In message passing the idea is that the data gets a message that tells it which method to invoke, and returns the answer. מבוא מורחב

11 Message passing style The constructor has it all. (define (cons x y)
(define (dispatch op) (cond ((eq? op 'car) x) ((eq? op 'cdr) y) (else (error "Unknown op -- CONS" op)))) dispatch) (define (car x) (x 'car)) (define (cdr x) (x 'cdr)) (define a (cons 1 2)) (car a) מבוא מורחב

12 Sending a Message (car a) (a 'car) ((cond ((eq? op 'car) 1)
((eq? op 'cdr) 2) (else (error "Unknown op -- CONS" op))) 'car) 1 מבוא מורחב

13 Message Passing for Numbers Package
Data Directed: for each method we looked for the type. Search decompose the table by row. Message Passing: for each type we look for the method. Search decompose the table by column. types Polar Rectangular real-part imag-part magnitude angle real-part-polar imag-part-polar magnitude-polar angle-polar real-part-rectangular imag-part-rectangular magnitude-rectangular angle-rectangular operations מבוא מורחב

14 Message passing style The constructor has it all.
(define (make-from-real-imag 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 -- MAKE-FROM-REAL-IMAG" op)))) dispatch) (define (apply-generic op arg) (arg op)) מבוא מורחב

15 Polar Complex (define (make-from-mag-ang r a) (lambda (op)
(cond ((eq? op 'real-part) (* r (cos a))) ((eq? op 'imag-part) (* r (sin a))) ((eq? op 'magnitude) r) ((eq? op 'angle) a) (else (error ))))) מבוא מורחב

16 Reminder – Methods and New 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)) (define (apply-generic op arg) (arg op)) מבוא מורחב

17 Example (define x (make-from-real-imag 3 2)) (define x (lambda (op)
(cond ((eq? op 'real-part) 3) ((eq? op 'imag-part) 2) ((eq? op 'magnitude) (sqrt (+ (square 3) (square 2)))) ((eq? op 'angle) (atan 2 3)) (else (error ))))) (real-part x) (x 'real-part) ==> 3 מבוא מורחב

18 New Topic (Not in Exam) Assignment and the Environment Model (EM)
Chapter 3 מבוא מורחב

19 Primitive Data constructor:
(define x 10) creates a new binding for name; special form selector: x returns value bound to name mutator: (set! x "foo") changes the binding for name; special form מבוא מורחב

20 Assignment -- set! Substitution model -- functional programming: (define x 10) (+ x 5) ==> 15 - expression has same value each time it evaluated (in (+ x 5) ==> same scope as binding) With assignment: (define x 10) (+ x 5) ==> expression "value" depends on when it is evaluated (set! x 94) ... (+ x 5) ==> 99 מבוא מורחב

21 Why? To have objects with local state
The state is summarized in a set of variables. When the object changes its state the variable changes its value. מבוא מורחב

22 Example: A counter Procedure Returns the value Of the last expression (define make-counter (lambda (n) (lambda ()(set! n (+ n 1)) n ))) (define ca (make-counter 0)) (ca) ==> 1 (ca) ==> 2 (define cb (make-counter 0)) (cb) ==> 1 (ca) ==> 3 ca and cb are independent Can you figure out why this code works? מבוא מורחב

23 Lets try to understand this
(define make-counter (lambda (n) (lambda () (set! n (+ n 1)) n ))) (define ca (make-counter 0)) ca ==> (lambda () (set! n (+ 0 1)) 0) (ca) (set! n (+ 0 1)) 0) ==> ?? The substitution model is not sufficient to explain the behaviour! מבוא מורחב

24 Summary Scheme provides built-in mutators such as
set! to change a binding Mutation introduces substantial complexity !! Unexpected side effects Substitution model is no longer sufficient to explain behavior מבוא מורחב

25 Other problems with the substitution model
Nested definitions : block structure (define (sqrt x) (define (good-enough? guess x) (< (abs (- (square guess) x)) 0.001)) (define (improve guess x) (average guess (/ x guess))) (define (sqrt-iter guess x) (if (good-enough? guess x) guess (sqrt-iter (improve guess x) x))) (sqrt-iter 1.0 x)) מבוא מורחב

26 The Environment Model מבוא מורחב

27 What is the EM? A precise, completely mechanical description of:
name-rule looking up the value of a variable define-rule creating a new definition of a var set!-rule changing the value of a variable lambda-rule creating a procedure application rule applying a procedure Enables analyzing arbitrary scheme code: Example: make-counter As we noted earlier in the course, we need to change models to explain a more complex universe, in the same way that in Physics one needs to abandon the Newtonian model in order to reason effectively about sub-atomic particles. Basis for implementing a scheme interpreter for now: draw EM state with boxes and pointers later on: implement with code מבוא מורחב

28 A shift in viewpoint As we introduce the environment model, we are going to shift our viewpoint on computation Variable: OLD – name for value NEW – place into which one can store things Procedure: OLD – functional description NEW – object with inherited context מבוא מורחב

29 Frame: a table of bindings
Binding: a pairing of a name and a value Example: x is bound to 15 in frame A y is bound to (1 2) in frame A the value of the variable x in frame A is 15 2 1 x: 15 A y: מבוא מורחב

30 Environment: a sequence of frames
Environment E1 consists of frames A and B Environment E2 consists of frame B only A frame may be shared by multiple environments z: 10 B E1 this arrow is called the enclosing environment pointer E2 x: 15 A 2 1 y: מבוא מורחב

31 Evaluation in the environment model
All evaluation occurs in an environment The current environment changes when the interpreter applies a procedure The top environment is called the global environment (GE) Only the GE has no enclosing environment To evaluate a combination Evaluate the subexpressions in the current environment Apply the value of the first to the values of the rest מבוא מורחב

32 Name-rule A name X evaluated in environment E gives the value of X in the first frame of E where X is bound z | GE ==> z | E1 ==> x | E1 ==> 15 In E1, the binding of x in frame A shadows the binding of x in B x | GE ==> 3 x: 15 A 2 1 z: 10 x: 3 B E1 GE y: מבוא מורחב

33 Define-rule A define special form evaluated in environment E creates or replaces a binding in the first frame of E (define z 20) | GE (define z 25) | E1 x: 15 A 2 1 z: 10 x: 3 B E1 GE y: z: 20 z: 25 מבוא מורחב

34 Set!-rule A set! of variable X evaluated in environment E changes the binding of X in the first frame of E where X is bound (set! z 20) | GE (set! z 25) | E1 x: 15 A 2 1 z: 10 x: 3 B E1 GE y: 20 25 מבוא מורחב

35 Your turn: evaluate the following in order
(+ z 1) | E ==> (set! z (+ z 1)) | E (modify EM) (define z (+ z 1)) | E (modify EM) (set! y (+ z 1)) | GE (modify EM) 11 Error: unbound variable: y B z: 10 x: 3 11 GE A x: 15 z: 12 E1 y: 2 1 מבוא מורחב

36 Double bubble: how to draw a procedure
(lambda (x) (* x x)) eval lambda-rule A compound proc that squares its argument #[compound-...] print Environment pointer Code pointer parameters: x body: (* x x) מבוא מורחב

37 Lambda-rule A lambda special form evaluated in environment E creates a procedure whose environment pointer is E (define square (lambda (x) (* x x))) | E1 x: 15 A z: 10 x: 3 B E1 environment pointer points to frame A because the lambda was evaluated in E1 and E1  A square: Evaluating a lambda actually returns a pointer to the procedure object parameters: x body: (* x x) מבוא מורחב

38 To apply a compound procedure P to arguments:
1. Create a new frame A 2. Make A into an environment E: A's enclosing environment pointer goes to the same frame as the environment pointer of P 3. In A, bind the parameters of P to the argument values 4. Evaluate the body of P with E as the current environment You must memorize these four steps מבוא מורחב

39 (square 4) | GE square | GE ==> #[proc] ==> 16 * | E1
x: 10 *: #[prim] GE square: E1 parameters: x body: (* x x) A x: 4 square | GE ==> #[proc] (* x x) | E1 ==> 16 * | E1 ==> #[prim] x | E1 ==> 4 מבוא מורחב

40 Example: inc-square inc-square: GE square: p: x b: (* x x)
p: y b: (+ 1 (square y)) (define square (lambda (x) (* x x))) | GE (define inc-square (lambda (y) (+ 1 (square y))) | GE מבוא מורחב

41 Example cont'd: (inc-square 4) | GE
p: x b: (* x x) square: inc-square: p: y b: ( (square y)) E1 y: 4 inc-square | GE ==> #[compound-proc ...] (+ 1 (square y)) | E1 + | E1 ==> #[prim] (square y) | E1 מבוא מורחב

42 Example cont'd: (square y) | E1
GE p: x b: (* x x) square: inc-square: p: y b: ( (square y)) E1 y: 4 E2 x: 4 square | E1 ==> #[compound] y | E1 ==> 4 (* x x) | E2 ==> 16 (+ 1 16) ==> 17 * | E2 ==> #[prim] x | E2 ==> 4 מבוא מורחב

43 Lessons from the inc-square example
EM doesn't show the complete state of the interpreter missing the stack of pending operations The GE contains all standard bindings (*, cons, etc) omitted from EM drawings Useful to link environment pointer of each frame to the procedure that created it Good Luck in the MidTerm Exam! מבוא מורחב


Download ppt "Lecture 12: Message passing The Environment Model"

Similar presentations


Ads by Google