6.001 SICP Environment model

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.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. Environment Model 3.2, pages
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Environment Model 3.2, pages
6.001 SICP SICP – September Processes, Substitution, Recusion, and Iteration Trevor Darrell 32-D web page:
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
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.
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
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.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1 Lecture 14: Assignment and the Environment Model (EM)
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
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.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
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.
(Thunking about Thunks)
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
Functional Programming
Environments and the Contour Model
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Edited by Original material by Eric Grimson
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
6.001 SICP Variations on a Scheme
6.001 SICP Object Oriented Programming
The interpreter.
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.
Variables, Environments and Closures
The Metacircular Evaluator
This Lecture Substitution model
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
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
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
Lecture 13 - Assignment and the environments model Chapter 3
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
6.001 SICP Environment model
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
6.001 SICP Variations on a Scheme
Lecture 14: The environment model (cont
This Lecture Substitution model
6.001 SICP Interpretation Parts of an interpreter
Lecture 13: Assignment and the Environment Model (EM)
Introduction to the Lab
This Lecture Substitution model
topics interpreters meta-linguistic abstraction eval and apply
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Rules of evaluation The value of a number is itself.
Principles of Programming Languages
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

6.001 SICP Environment model Models of computation Substitution model A way to figure out what happens during evaluation (define l '(a b c)) (car l)  a (define m '(1 2 3)) Not really what happens in the computer (set-car! l 'z) (car l)  z The Environment Model

Can you figure out why this code works? (define make-counter (lambda (n) (lambda () (set! n (+ n 1)) n ))) (define ca (make-counter 0)) (ca) ==> 1 (ca) ==> 2 ; not functional programming! (define cb (make-counter 0)) (cb) ==> 1 (ca) ==> 3 ; ca and cb are independent

What the EM is: 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 applying a procedure Enables analyzing more complex scheme code: Example: make-counter Basis for implementing a scheme interpreter for now: draw EM state with boxes and pointers later on: implement with code

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 Expressions Now only have meaning with respect to an environment

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:

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:

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

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:

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 | GE ==> 20 z | E1 ==> 25 z: 25

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

Define versus Set! Using defines Using set!s z: 25 z: 20 x: 15 A 2 1 B E1 GE y: 20 25 x: 15 A 2 1 z: 10 x: 3 B E1 GE y:

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

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)

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)

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

Achieving Inner Peace (and A Good Grade), Part II 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 *Om Mani Padme Hum… *

(square 4) | GE square | GE ==> #[proc] ==> 16 x: 10 *: #[prim] parameters: x body: (* x x) A x: 4 square | GE ==> #[proc] (* x x) | E1 ==> 16 * | E1 ==> #[prim] x | E1 ==> 4

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

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

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

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

Example: 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 ; not functional programming (define cb (make-counter 0)) (cb) ==> 1 (ca) ==> 3 (cb) ==> 2 ; ca and cb are independent

(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

Environments are important in other languages USA Britain New England Canada Unbound variable!! Macintosh | USA Macintosh | Britain Frappe | New England Milkshake | USA Milkshake | New England Canadian bacon | New England Canadian bacon | Canada