Class 19: Think Globally, Mutate Locally CS150: Computer Science

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Advertisements

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.
Cs1120 Fall 2009 David Evans Lecture 16: Power Analysis.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. Environment Model 3.2, pages
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 31: Types of Types.
6.001 SICP SICP – October environment Trevor Darrell 32-D512 Office Hour: W web page:
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.
Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
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.
Lecture 31: Laziness University of Virginia cs1120 Fall 2009 David Evans.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
David Evans CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
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 29: Typed Scheme MC Escher, Liberation.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: The Value of Everything.
David Evans CS200: Computer Science University of Virginia Computer Science Class 16: Mutation M. C. Escher, Day and Night.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
1 Lecture 14: Assignment and the Environment Model (EM)
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Mutation M. C. Escher, Day and.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
Class 28: Interpreters cs1120 Fall 2009 David Evans.
Lecture 3: Rules of Evaluation CS150: Computer Science
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: Programming with Data.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 27: Types of Types “It would appear.
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
Environments and the Contour Model
Lecture 17: Environments CS200: Computer Science
Edited by Original material by Eric Grimson
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Class 27: Universal Turing Machines CS150: Computer Science
The interpreter.
Lecture 7: List Recursion CS200: Computer Science
The Environment Model*
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
The Metacircular Evaluator
Lecture 28: Types of Types
Functional Programming
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.
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
6.001 SICP Environment model
Lecture 24: Metalinguistics CS200: Computer Science
Lecture 27: In Praise of Idleness CS200: Computer Science
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
6.001 SICP Variations on a Scheme
Lecture 14: The environment model (cont
Class 31: Universal Turing Machines CS200: Computer Science
Class 34: Models of Computation CS200: Computer Science
6.001 SICP Interpretation Parts of an interpreter
6.001 SICP Environment model
Lecture 3: Rules of Evaluation CS200: Computer Science
Lecture 13: Assignment and the Environment Model (EM)
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:

David Evans http://www.cs.virginia.edu/evans Class 19: Think Globally, Mutate Locally CS150: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/evans

(define nest (lambda (x) (+ x x)))) > ((nest 3) 4) 8 Does the substitution model of evaluation tell us how to evaluate this?

Review: Names, Places, Mutation A name is a place for storing a value. define creates a new place cons creates two new places, the car and the cdr (set! name expr) changes the value in the place name to the value of expr (set-car! pair expr) changes the value in the car place of pair to the value of expr (set-cdr! pair expr) changes the value in the cdr place of pair to the value of expr

Lambda and Places (lambda (x) …) also creates a new place named x The passed argument is put in that place > (define x 3) > ((lambda (x) x) 4) 4 > x 3 x : 3 x : 4 How are these places different?

Location, Location, Location Places live in frames An environment is a pointer to a frame We start in the global environment Application creates a new frame All frames except the global frame have exactly one parent frame, global frame has no parent

Environments > (define x 3) + : #<primitive:+> global environment null? : #<primitive:null?> x : 3 The global environment points to the outermost frame. It starts with all Scheme primitives. > (define x 3)

Procedures > (define double (lambda (x) (+ x x))) + : #<primitive:+> global environment null? : #<primitive:null?> x : 3 double: ?? > (define double (lambda (x) (+ x x)))

How to Draw a Procedure A procedure needs both code and an environment We’ll see why soon We draw procedures like this: Environment pointer Code pointer parameters: x body: (+ x x)

How to Draw a Procedure (for artists only) Environment pointer x (+ x x) Input parameters (in mouth) Procedure Body

Procedures > (define double (lambda (x) (+ x x))) + : #<primitive:+> global environment null? : #<primitive:null?> x : 3 double: > (define double (lambda (x) (+ x x))) parameters: x body: (+ x x)

Application Old rule: (Substitution model) Apply Rule 2: Compounds. If the procedure is a compound procedure, evaluate the body of the procedure with each formal parameter replaced by the corresponding actual argument expression value.

New Rule: Application Construct a new frame, enclosed in the environment of this procedure Create places in that frame with the names of each parameter Put the values of the parameters in those places Evaluate the body in the new environment

global environment Construct a new frame, enclosed in the environment of this procedure Make places in that frame with the names of each parameter Put the values of the parameters in those places Evaluate the body in the new environment + : #<primitive:+> double: x : 3 parameters: x body: (+ x x) x : 4 > (double 4) 8 (+ 4 4) (+ x x) 8

(define nest (lambda (x) (+ x x)))) > ((nest 3) 4) global environment + : #<primitive:+> nest: x : 3 ((lambda (x) (+ x x)) 4) parameter: x body: (lambda (x) (+ x x)) x : 3 x : 4 (+ x x)

Evaluation Rule 2 (Names) If the expression is a name, it evaluates to the value associated with that name. To find the value associated with a name, look for the name in the frame pointed to by the evaluation environment. If it contains a place with that name, use the value in that place. If it doesn’t, evaluate the name using the frame’s parent environment as the new evaluation environment. If the frame has no parent, error (name is not a place).

evaluate-name (define (evaluate-name name env) (if (null? env) (error “Undefined name: …”) (if (frame-contains name (get-frame env)) (lookup name (get-frame env)) (evaluate-name name (parent-environment (get-frame env)))))) Hmm…maybe we can define a Scheme interpreter in Scheme!

Exam 1 Return now Full comments will be posted on the web soon (and distributed on paper Monday)

Question 4 Draw a recursive transition network that describes the same language as the Backus-Naur Form grammar below produces starting from Expression: Expression ::= ( Expression ExpressionList ) Expression ::= Name ExpressionList ::= Expression ExpressionList ExpressionList ::= The terminals are Name and the parentheses.

Question 7 For any two functions f and g is it always the case that at least one of the two possibilities, f is O (g) or g is O (f ) is true? Prove or disprove using a precise argument. Bonus: what are the constraints on f and g needed to make this true? (Being monotonic is not quite enough!)

Charge Mutation makes evaluation rules more complicated Environment diagrams quickly get complicated – but like substitution evaluations, just follow rules mechanically Monday, we’ll finish Colossus (don’t worry, the Allies still won)