Lecture 13 - Assignment and the environments model Chapter 3

Slides:



Advertisements
Similar presentations
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
Advertisements

1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. Environment Model 3.2, pages
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Environment Model 3.2, pages
6.001 SICP SICP – October environment Trevor Darrell 32-D512 Office Hour: W web page:
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Dotted tail notation (define (proc m1 m2. opt) ) Mandatory Arguments: m1, m2 Mandatory Arguments: m1, m2.
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
1 Lecture 15: More about assignment and the Environment Model (EM)
1 Saves repeated renaming and substitution: explicit substitution is replaced by variable bindings using new data structures (frame, environment). Can.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
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.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
1 Lecture 14: Assignment and the Environment Model (EM)
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
1 Data Abstraction. Pairs and Lists. (SICP Sections – 2.2.1)
מבוא מורחב שיעור 7 1 Lecture 7 Data Abstraction. Pairs and Lists. (Sections – 2.2.1)
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
Operational Semantics of Scheme
Functional Programming
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Edited by Original material by Eric Grimson
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
6.001 SICP Variations on a Scheme
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
6.001 SICP Object Oriented Programming
The interpreter.
The role of abstractions
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Env. Model Implementation
Original material by Eric Grimson
Class 19: Think Globally, Mutate Locally CS150: Computer Science
6.001 SICP Data abstractions
Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things are in.
The Metacircular Evaluator
Lecture 18 Infinite Streams and
Lecture 15: Tables and OOP
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Lecture #6 section pages pages72-77
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
6.001 SICP Environment model
Lecture 16: Tables and OOP
The Metacircular Evaluator (Continued)
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 12: Message passing The Environment Model
6.001 SICP Environment model
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Material in the textbook Sections to 1.2.1
6.001 SICP Variations on a Scheme
6.001 SICP Data Mutation Primitive and Compound Data Mutators
Lecture 14: The environment model (cont
Lecture 2 מבוא מורחב.
6.001 SICP Interpretation Parts of an interpreter
6.001 SICP Environment model
Lecture 13: Assignment and the Environment Model (EM)
Introduction to the Lab
topics interpreters meta-linguistic abstraction eval and apply
Lecture 2 מבוא מורחב.
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Principles of Programming Languages
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

Lecture 13 - Assignment and the environments model Chapter 3 מבוא מורחב - שיעור 13

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

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

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

Procedures with local state Suppose we want to implement counters (define ca (make-counter 0)) (ca) ==> 1 (ca) ==> 2 (define cb (make-counter 0)) (cb) ==> 1 (ca) ==> 3 How can we define such procedures? מבוא מורחב - שיעור 13

A counter: an object with state (define make-counter (lambda (n) (lambda ()(set! n (+ n 1)) n ))) set! does assignment to an already defined variable. It is a special form. (define ca (make-counter 0)) ca is a procedure. Variable n is its local state. מבוא מורחב - שיעור 13

Can the substitution model explain this behavior? (define make-counter (lambda (n) (lambda () (set! n (+ n 1)) n ))) (define ca (make-counter 0)) ca ==> (lambda () (set! 0 (+ 0 1)) 0) (ca) ((set! 0 (+ 0 1)) 0) ==> ? The substitution model is not adequate for assignment! מבוא מורחב - שיעור 13

The Environments Model Name Value Environment Table 23 score Substitution model: a single global environment Environments model: many environments. Generalizes the substitution model. 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. מבוא מורחב - שיעור 13

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: מבוא מורחב - שיעור 13

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: מבוא מורחב - שיעור 13

Evaluation in the environment model All evaluations occur 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 מבוא מורחב - שיעור 13

The Environment Model 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 מבוא מורחב - שיעור 13

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 ==> 10 z | E1 ==> 10 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: מבוא מורחב - שיעור 13

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 מבוא מורחב - שיעור 13

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 מבוא מורחב - שיעור 13

Your turn: evaluate the following in order (+ z 1) | E1 ==> (set! z (+ z 1)) | E1 (modify EM) (define z (+ z 1)) | E1 (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 מבוא מורחב - שיעור 13

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

Lambda-rule A lambda special form evaluated in environment E creates a procedure whose environment pointer points to 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) מבוא מורחב - שיעור 13

To apply a compound procedure P to arguments (Application Rule) 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 We recommend you memorize these four steps מבוא מורחב - שיעור 13

(square 4) | GE square | GE ==> ==> 16 * | E1 ==> x | E1 *: #[prim] GE square: E1 parameters: x body: (* x x) A x: 4 square | GE ==> frame becomes inaccessible (* x x) | E1 ==> 16 * | E1 ==> x | E1 ==> 4 מבוא מורחב - שיעור 13

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

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

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

Example: explaining make-counter Counter: an object that counts up starting from some integer (define make-counter (lambda (n) (lambda () (set! n (+ n 1)) n ; returned and retained value ))) (define ca (make-counter 0)) (ca) ==> 1 (ca) ==> 2 (define cb (make-counter 0)) (cb) ==> 1 (ca) ==> 3 (cb) ==> 2 מבוא מורחב - שיעור 13

(define ca (make-counter 0)) | GE p: n b:(lambda () (set! n (+ n 1)) n) make-counter: ca: E1 E1 is pointed at so does not disappear! n: 0 environment pointer points to E1 because the lambda was evaluated in E1 p: b:(set! n (+ n 1)) n (lambda () (set! n (+ n 1)) n) | E1 מבוא מורחב - שיעור 13

(ca) | GE ==> 1 make-counter: GE ca: E1 n: 0 1 p: n b:(lambda () (set! n (+ n 1)) n) p: b:(set! n (+ n 1)) n E2 Empty (no args) (set! n (+ n 1)) | E2 n | E2 ==> 1 מבוא מורחב - שיעור 13

(ca) | GE ==> 2 make-counter: GE ca: E1 n: 0 1 p: n b:(lambda () (set! n (+ n 1)) n) 2 p: b:(set! n (+ n 1)) n E3 empty (set! n (+ n 1)) | E3 n | E3 ==> 2 מבוא מורחב - שיעור 13

Example: explaining make-counter Counter: something which counts up from a number (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 (cb) ==> 2

(define ca (make-counter 0)) | GE p: n b:(lambda () (set! n (+ n 1)) n) make-counter: ca: E1 n: 0 environment pointer points to E1 because the lambda was evaluated in E1 p: b:(set! n (+ n 1)) n (lambda () (set! n (+ n 1)) n) | E1

(ca) | GE ==> 1 GE p: n b:(lambda () (set! n (+ n 1)) n) make-counter: E1 n: 0 p: b:(set! n (+ n 1)) n ca: 1 E2 empty (set! n (+ n 1)) | E2 n | E2 ==> 1

(ca) | GE ==> 2 GE p: n b:(lambda () (set! n (+ n 1)) n) make-counter: E1 n: 0 p: b:(set! n (+ n 1)) n ca: 1 2 E3 empty (set! n (+ n 1)) | E3 n | E3 ==> 2

(define cb (make-counter 0)) | GE p: n b:(lambda () (set! n (+ n 1)) n) make-counter: E1 n: 2 p: b:(set! n (+ n 1)) n ca: E3 cb: E4 n: 0 p: b:(set! n (+ n 1)) n (lambda () (set! n (+ n 1)) n) | E4

(cb) | GE ==> 1 n: 0 E4 p: b:(set! n (+ n 1)) n cb: GE p: n b:(lambda () (set! n (+ n 1)) n) make-counter: E1 n: 2 p: b:(set! n (+ n 1)) n ca: E2 1 E5

Capturing state in local frames & procedures p: b:(set! n (+ n 1)) n cb: GE p: n b:(lambda () (set! n (+ n 1)) n) make-counter: E1 n: 2 p: b:(set! n (+ n 1)) n ca: E2

Lessons from the make-counter example Environment diagrams get complicated very quickly Rules are meant for the computer to follow, not to help humans A lambda inside a procedure body captures the frame that was active when the lambda was evaluated this effect can be used to store local state

Explaining Nested Definitions Nested definitions : block structure (define (sqrt x) (define (good-enough? guess) (< (abs (- (square guess) x)) 0.001)) (define (improve guess) (average guess (/ x guess))) (define (sqrt-iter guess) (if (good-enough? guess) guess (sqrt-iter (improve guess)))) (sqrt-iter 1.0)) מבוא מורחב - שיעור 13

p: x b:(define good-enou ..) (define improve ..) sqrt: (sqrt 2) | GE GE p: x b:(define good-enou ..) (define improve ..) (define sqrt-iter ..) (sqrt-iter 1.0) sqrt: E1 x: 2 good-enough: p: guess b:(< (abs ….) The same x in all subprocedures improve: sqrt-iter: guess: 1 sqrt-iter guess: 1 good-enou? מבוא מורחב - שיעור 13

message passing example (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)) This code was modeled on the board. Recall that we explained that this example shows how Scheme’s “static scoping” comes to play. By this we mean that even though the procedure dispatch Pointed to by “a” is called from within the car procedure, our static scoping model that looks for variables in the env the procedure was defined in, causes us to look for x in the env where dispatch was Created, and thus we find the correct variable x to return, the one with value 1. (define a (cons 1 2)) (car a) מבוא מורחב - שיעור 13

(define a (cons 1 2)) | GE cons: car: cdr: GE a: E1 x: 1 y: 2 p: x y b:(define (dispatch op) ..) dispatch cons: car: cdr: a: E1 x: 1 y: 2 p: x b:(x ‘car) p: x b:(x ‘cdr) dispatch: p: op b:(cond ((eq? op 'car) x) .... ) מבוא מורחב - שיעור 13

(car a) | GE ==> 1 cons: car: cdr: GE a: E1 x: 1 y: 2 dispatch: E2 p: x y b:(define (dispatch op) ..) dispatch cons: car: cdr: a: x: E2 E1 x: 1 y: 2 p: x b:(x ‘car) p: x b:(x ‘cdr) dispatch: op: ‘car E3 p: op b:(cond ((eq? op 'car) x) .... ) (x ‘car) | E2 (cond ..) | E3 ==> 1 מבוא מורחב - שיעור 13