6.001 SICP 1 6.001 SICP – October 29 6001-environment Trevor Darrell 32-D512 Office Hour: W 11 6.001 web page:

Slides:



Advertisements
Similar presentations
(define (f x) (if (< x 0) (lambda (y) (- y x)) (lambda (y) (- x y)))) GE f: P1 para:x body:(if … )
Advertisements

Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 The metacircular evaluator Names Extend the calculator to store intermediate results as named values (define x (+ 4 5)) store result as x (+ x.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
מבוא מורחב למדעי המחשב בשפת 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:
Object System I. OO System in Scheme class private variables methods NAMED-OBJECT self: name: sicp instance x root self: TYPE IS-A NAMED-OBJECT name:
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Techniques for language design: Interpretation: eval/apply Semantics vs. syntax Syntactic.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Administrivia Project 3 Part A due 3/29 (Monday after SB) Part B due 4/5 (a week after)  Everyone with a partner that wants a partner?  Extra Office.
Introduction Even though the syntax of Scheme is simple, it can be very difficult to determine the semantics of an expression. Hacker’s approach: Run it.
Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.
The environment model evaluator and compiler 1 The env model evaluator Motivation In one word: Efficiency Saves repeated renaming and substitution: Using.
1 Saves repeated renaming and substitution: explicit substitution is replaced by variable bindings using new data structures (frame, environment). Can.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is aa good way to learn more about programming languages  Interpreters are.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
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.
The environment-based operational semantics Chapter
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/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/ SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Making the environment model concrete Defining eval defines the language –Provides.
Scheme in Scheme 2. What’s next  Adding set!  Dynamic vs. lexical variable scope  Extending mcscheme v1 with libraries  Can mcscheme execute mcscheme?
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
The Environment Model an extension of the substitution model more "operational" fully explains static scoping and the process by which variable names are.
Constructs for Data Organization and Program Control, Scope, Binding, and Parameter Passing. Expression Evaluation.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
Environments and the Contour Model
Edited by Original material by Eric Grimson
6.001 SICP Variations on a Scheme
6.001 SICP Object Oriented Programming
The interpreter.
The Environment Model*
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.
Env. Model Implementation
Original material by Eric Grimson
Class 19: Think Globally, Mutate Locally CS150: Computer Science
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
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
6.001 SICP Environment model
6.001 SICP Variations on a Scheme
Lecture 14: The environment model (cont
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
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

6.001 SICP SICP – October environment Trevor Darrell 32-D512 Office Hour: W web page: section web page: Environment Model Diagram Components Rules Let example More examples

6.001 SICP 2 Why do we need a new evaluation model? The substitution model broke down when we added side effects! In Object Oriented Programming, we need a model to represent hierarchies, shadowing, and inheritance. Looking Ahead: We'll be writing a meta-circular evaluator in Scheme. You'll see that the code we write closely relates to the rules of the environment model. It's boring, but important: It's a technical investment now, but you'll get a lot out of it soon.

6.001 SICP 3 Diagram Components Frames: We draw a frame as a box. In the box go bindings. Every frame (except the frame representing the global environment) needs to have a link to exactly one other frame. Bindings: A binding is an association between an identifier (or symbol) to a value. Procedure Objects: These are special objects (symbolized by the double bubble) created by evaluating a lambda expression as described below.

6.001 SICP 4 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 x: 15 A y:

6.001 SICP 5 Environment: a sequence of frames Environment E1 consists of frames A and B E2 x: 10 B E1 x: 15 A 2 1 y: this arrow is called the enclosing environment pointer Environment E2 consists of frame B only A frame may be shared by multiple environments

6.001 SICP 6 Rules Looking up a name Define / Set Lambda Combination / Procedure application

6.001 SICP 7 Looking up an identifier 1.Look for a value in the current frame. 2.If there is no value for that identifier in the current frame, follow the link from the current frame to the one that it is linked from. 3.Continue until we find a binding for the identifier we're looking up or until we run out of frames in our chain of links. If there's no binding in the GE, the identifier we're looking up is an unbound variable. x: 10 B E1 x: 15 A 2 1 y:

6.001 SICP 8 set and define Define (define var expression) 1.Evaluates the expression with respect to the current environment. 2.Adds a binding to the frame in which it is evaluated.

6.001 SICP 9 set and define Set! (set! var expression) 1.Evaluates the expression with respect to the current environment. 2.Lookup the identifier in the current environment 3.Rebind the identifier in the frame it was found to the value of the expression.

6.001 SICP 10 Lambda Evaluating a lambda expression will result in a two-part procedure object (two circles next to each other -- the double bubble). The pointer of the left circle points down to a list of the parameters and the body of the procedure. The pointer of the right circle points up to the environment frame in which the lambda expression was evaluated. Note that nothing else happens until we apply the procedure.

6.001 SICP 11 Procedure object: “double bubble” (lambda (x) (* x x)) Environment pointer Code pointer parameters: x body: (* x x) Points to environment in which lambda was evaluated!

6.001 SICP 12 1.Draw a new environment frame. 2.Link the new environment frame to the environment frame pointed to by the right circle of the procedure object. 3.Bind the parameters to their values in this new environment frame. 4.Evaluate the body of the procedure in this new environment frame. Combinations / Procedure application To evaluate a combination with respect to an environment, first evaluate the subexpressions with respect to the environment and then apply the value of the operator subexpression to the values of the operand ubexpressions. Applying a procedure P

6.001 SICP 13 Example1 Evaluate: (define a 5) (define foo (lambda (x) (+ a x))) (foo 4)

6.001 SICP 14 Example1 Here's what happens: 1.We bind a to 5 2.A new frame is created because of the application. It is shown with a dashed link to the procedure whose application creates the new frame. 3.In the new frame we bind the procedure's formal parameter x to the actual argument 4. 4.The new frame has as its parent the environment pointer from the procedure. Together then, the new frame and the enclosing environment define a new environment (E1). 5.Now we evaluate the body of the procedure with respect to the new environment (E1). 6.To evaluate the (+ a x) expression, we look up x and find it in the local frame to get a value 4. We look up a and find that it is NOT in the local frame. So we follow the frame parent link up to the surrounding frame (the GE), where we successfully find a binding for a whose value is 5. The addition finally returns 9.

6.001 SICP 15 (define a 5) | GE (define foo (lambda (x) (+ a x))) | GE (foo 4) | GE GE a:5a:5 Example1

6.001 SICP 16 (define a 5) (define foo (lambda (x) (+ a x))) (foo 4) GE p: x b:(+ a x) foo: E1 x: 4 a:5a:5 Procedure application: “drop a frame” 1.Draw frame 2.Set enclosing env. ptr of frame to be env. ptr of proc. 3.Assign args to vals in new frame 4.Eval body w.r.t. new frame (+ a x) (+ 5 4) ([proc] 5 4) 9 +: [proc]

6.001 SICP 17 Example2 Evaluate: (define (fact n) (if (= n 1) 1 (* n (fact (- n 1)))))) (define n 10) (fact 3) n

6.001 SICP 18 GE p: n b:(if (= 0 n) 1 (* (fact (- n 1)))) fact: n: 10 n: 1 Example2 n: 2n: 3

6.001 SICP 19 let Let just creates another frame linked from the current frame. The bindings in that frame are let-variables bound to the result of evaluating the let- expressions with respect to the original environment. (let ((n v)…) body) ==> ((  (n …) body) v … ) (let ((x (+ 2 5)) (y 7)) (* x y)) ((lambda (x y) (* x y)) (+ 2 5) 7)

6.001 SICP 20 Example3 (define (adder y) (let ((inc y)) (lambda (x) (+ inc x)))) (define add3 (adder 3)) (add3 4) (define (adder y) (lambda (inc) (lambda (x) (+ inc x)) y)) ((adder 3) 4) (define (adder y) (lambda (x) (+ y x))) (define add3 (adder 3)) (add3 4)

6.001 SICP 21 (define (adder y) (lambda (x) (+ y x))) | GE (define add3 (adder 3)) | GE (add3 4) | GE GE p: x b:(lambda (x) (+ y x)) adder: E1 x: 3 p: y b:(+ y x) add3: E2 Y: 4

6.001 SICP 22 Example4 Let’s use this technique to build a one-element buffer…write a function that returns the argument in it’s previous call… (define last-value '*first-call*) (define (prev new) (define temp last-value) (set! Last-value new) temp)

6.001 SICP 23 Example4 Now, consider the following let statement. The effect of this statement in the environment is then shown. (let ((last-value '*first-call* )) (define (prev new)... ))

6.001 SICP 24 Example4 Define instead a procedure make-prev that can be used to create the procedure prev and add it to the environment (to make completely clear the binding action of procedures) (define (make-prev last-value) (lambda (new) (define temp last-value) (set! last-value new) temp))

6.001 SICP 25 Example4 (define prev (make-prev '*first-call*)) (prev 'a) :

6.001 SICP 26 Block structure and Lexical Scoping … (define F (lambda (a b c) (define G (lambda (b a) (- a b))) (define H (lambda (a x) (* a b))) (+ (G a b) (H b a)))) (F 2 3 4) … GE Scope 1 Scope 2 Scope 3

6.001 SICP 27 Example5 - Corresponding Env. Diagram

6.001 SICP 28 What created this? (define (foo x)...) w.r.t. GE; (foo 1) w.r.t. ANY ENVIRONMENT (define (foo) (define x 1)...)

6.001 SICP 29 or this? (define bar (let ((x 1)) (lambda (...)...)))

6.001 SICP 30 this? (define (make-bar x) (lambda (…) ….) (define bar1 (make-bar 1))

6.001 SICP 31 Example6 Evaluate: (define make-count-proc-1 (lambda (f) (lambda (x) (let ((count 0)) (cond ((eq? x ‘count) count) (else (set! Cont (+ count 1)) (f x)))))) (define sqrt-c-1 (makecount-proc-1 sqrt)) (sqrt-c-1 4)==> ? (sqrt-c-1 ‘count)==> ?