Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.

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

Class 21: Imperative Programming University of Virginia cs1120 David Evans.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Cs1120 Fall 2009 David Evans Lecture 15: Running Practice.
Cs1120 Fall 2009 David Evans Lecture 16: Power Analysis.
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 Lecture 16: Quicker Sorting.
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.
David Evans Class 13: Quicksort, Problems and Procedures CS150: Computer Science University of Virginia Computer Science.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 2: Orders of Growth
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 22: Objectifying Objects.
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.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 30: Laziness.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
Class 8: Recursing on Lists David Evans cs1120 Fall 2009.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 29: Typed Scheme MC Escher, Liberation.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 12: QuickSorting Queen’s University,
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 Lecture 19: Environments.
Cs1120 Fall 2009 David Evans Lecture 18: Changing State Sounds of Colossus:
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.
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
Class 7: List Procedures David Evans cs1120 Fall 2009.
Lecture 3: Rules of Evaluation CS150: Computer Science
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 14: P = NP?
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
Lecture 17: Environments CS200: Computer Science
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Lecture 13: Quicksorting CS200: Computer Science
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.
Class 19: Think Globally, Mutate Locally CS150: Computer Science
Lecture 16: Quickest Sorting CS150: Computer Science
Mini Language Interpreter Programming Languages (CS 550)
The Metacircular Evaluator
Lecture 28: Types of Types
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
Lecture 10: Quicker Sorting CS150: Computer Science
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
Streams, Delayed Evaluation and a Normal Order Interpreter
Searching, Sorting, and Asymptotic Complexity
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
6.001 SICP Environment model
Lecture 9: The Great Lambda Tree of Knowledge and Power
6.001 SICP Interpretation Parts of an interpreter
6.001 SICP Environment model
Cs1120 Fall 2009 David Evans Lecture 10: Fracturing Fractals cs1120 Fall 2009 David Evans
Lecture 3: Rules of Evaluation CS200: Computer Science
topics interpreters meta-linguistic abstraction eval and apply
Lecture 15: Quicker Sorting CS150: Computer Science
Lecture 8: Recursing Lists CS150: Computer Science
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation

Menu Stateful Evaluation Rules Exam 1 2

Names and Places A name is a place for storing a value. define creates a new place (set! name expr) changes the value in the place name to the value of expr mcons creates a mutable pair containing two new places (set-mcar! pair expr) changes the value in the mcar place of pair to the value of expr (set-mcdr! pair expr) changes the value in the mcdr place of pair to the value of expr 3

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 How are these places different? x : 3 x : 4 4

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

Environments global environment > (define x 3) + : # null? : # The global environment points to the outermost frame. It starts with all Scheme built-ins defined. x : 3 6

Stateful Definition Evaluation Rule A definition creates a new place with the definition’s name in the frame associated with the evaluation environment. The value in the place is value of the definition’s expression. If there is already a place with the name in the current frame, the definition replaces the old place with a new place and value. 7

Stateful Name Evaluation Rule To evaluate a name expression, search the evaluation environment’s frame for a place with a name that matches the name in the expression. If such a place exists, the value of the name expression is the value in that place. Otherwise, the value of the name expression is the result of evaluating the name expression in the parent environment. If the evaluation environment has no parent, the name is not defined and the name expression evaluates to an error. 8

Evaluating Names To evaluate a name expression, search the evaluation environment’s frame for a place with a name that matches the name in the expression. If such a place exists, the value of the name expression is the value in that place. Otherwise, the value of the name expression is the result of evaluating the name expression in the parent environment. If the evaluation environment has no parent, the name is not defined and the name expression evaluates to an error. global env x : 3 x : 17 y : 3 How are environments like this created? 9

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

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 environment: parameters: x body: (+ x x) 11

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

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

Application Old rule: (Substitution model) Apply Rule 2: Constructed Procedures. To apply a constructed procedure, evaluate the body of the procedure with each formal parameter replaced by the corresponding actual argument expression value. 14

Stateful Application Rule (Constructed Procedures) To apply a constructed procedure: 1.Construct a new environment, whose parent is the environment of the applied procedure. 2.For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment or the application and initialize the value in each place to the value of the corresponding operand expression. 3.Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application. 15

1.Construct a new environment, parent is procedure’s environment pointer 2.Make places in that frame with the names of each parameter, and operand values 3.Evaluate the body in the new environment global environment > (double 4) 8 + : # x : 3 x : 4 (+ x x) double: environment: parameters: x body: (+ x x) 16

17 x : 3 x : 17 y : 3 What would create this environment? Think about this, we’ll discuss in Monday’s class...

Exam 1 Overall: very good – Average: 93 – Average for Q’s 3,4,8 (defining procs): 8.9 – Average for Q’s 5,6,7,9 (analysis): 7.8 Main complaints: – Hard to program without Scheme interpreter – Too much emphasis on runtime analysis 18

Question 10: count-unique Define a procedure, count-unique, that takes as input a list of numbers. It produces as output a number that indicates the number of unique numbers in the input list. So, (count-unique (list )) should evaluate to 3. (count-unique (list 2 2 2)) should evaluate to 1. (count-unique (list )) should evaluate to 2. For full credit, your procedure must work correctly for all possible inputs that are Lists of numbers. 19

count-unique: hard and slow way (define (count-unique p) (if (null? p) 0 (+ 1 (count-unique (list-filter (lambda (el) (not (= el (car p)))) (cdr p)))))) (define (list-filter test p) (if (null? p) null (if (test (car p)) (cons (car p) (list-filter test (cdr p))) (list-filter test (cdr p))))) From Chapter 5: Running time is in  (N 2 ) where N is number of elements in p. Worst case: no duplicates. There are N recursive calls, each calls list-filter which has running time in  (N). Assumes = is constant time: only true if elements of p are bounded (always below some max value) 20

count-unique: easier, faster way Observe: if elements are sorted, don’t need to search entire list to find duplicates (define (count-unique p) (- (length p) (count-repeats (sort p <)))) Running time is in  (N log N ). Body of count-unique applies sort (to a list of length N), count-repeats (to a list of length N), and length (to a list of length <= N):  (N log N ) +  (N) +  (N) =  (N log N ) Assumes: all values in p below some fixed constant C (needed for < to be constant time). 21

count-unique: “fastest” way Assumes: all values in p below some fixed constant C (needed for < to be constant time). (define C 100) (define (count-unique p) (length (list-filter (lambda (n) (list-contains? p n)) (intsto C)))) C executions of list-contains? which has running time in  (N). Running time is in  (N). Is this really the fastest? 22

Do you trust your classmates to follow the honor expectations in this class? ___ Yes, I trust them completely ___ I worry that there may be a few transgressions, but I believe the vast majority of the class is honorable and it is fair and beneficial to rely on this. ___ I think this class places too high a burden on students’ honor, and there are enough dishonorable students that it is unfair on the honorable students. ___ I have direct knowledge of other students violating the honor policy on problem sets. ___ I have direct knowledge of other students violating the honor policy on this exam

Honor Expectations 24

Exam 1 Distribution 25

Charge Return Exam1 and PS4 now Read the Exam1 Comments – If there are things that don’t make sense after reading them, come to office hours or send me You know everything you need for PS5 now Next week: programming with mutation 26