Lecture 17: Environments CS200: Computer Science

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Advertisements

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 CS150: Computer Science University of Virginia Computer Science Lecture 31: Types of Types.
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.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 28: Implementing Interpreters.
Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 22: Objectifying Objects.
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.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
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.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Mutation M. C. Escher, Day and.
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.
(Thunking about Thunks)
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
Edited by Original material by Eric Grimson
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Lecture 13: Quicksorting CS200: 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.
COP4020 Programming Languages
Class 19: Think Globally, Mutate Locally CS150: Computer Science
Lecture 8: Recursion Practice CS200: Computer Science
Lecture 16: Quickest Sorting CS150: Computer Science
Lecture 6: Programming with Data CS150: Computer Science
Mini Language Interpreter Programming Languages (CS 550)
Lecture 21: Inheritance CS200: Computer Science University of Virginia
Introduction to C++ Programming
The Metacircular Evaluator
Lecture 11: All Sorts CS200: Computer Science University of Virginia
David Evans Lecture 9: The Great Lambda Tree of Infinite Knowledge and Ultimate Power CS200: Computer Science University.
Lecture 28: Types of Types
The Metacircular Evaluator
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
Lecture 22: P = NP? CS200: Computer Science University of Virginia
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
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 Variations on a Scheme
Lecture 14: The environment model (cont
Dan Grossman / Eric Mullen Autumn 2017
Announcements Quiz 5 HW6 due October 23
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
Lecture 11: Sorting Grounds and Bubbles
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Lecture 8: Recursing Lists CS150: Computer Science
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

David Evans http://www.cs.virginia.edu/evans Lecture 17: Environments CS200: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/evans

Menu Environments Evaluation Rules Exam 1 26 February 2003 CS 200 Spring 2003

Environments 26 February 2003 CS 200 Spring 2003

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

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 26 February 2003 CS 200 Spring 2003

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? 26 February 2003 CS 200 Spring 2003

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 one parent frame 26 February 2003 CS 200 Spring 2003

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) 26 February 2003 CS 200 Spring 2003

Procedures > (define double (lambda (x) (+ x x))) + : #<primitive:+> global environment null? : #<primitive:null?> x : 3 double: ?? > (define double (lambda (x) (+ x x))) 26 February 2003 CS 200 Spring 2003

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) 26 February 2003 CS 200 Spring 2003

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) 26 February 2003 CS 200 Spring 2003

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. 26 February 2003 CS 200 Spring 2003

Application 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 26 February 2003 CS 200 Spring 2003

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 26 February 2003 CS 200 Spring 2003

(nest 3) (define nest (lambda (x) (+ x x)))) > ((nest 3) 4) global environment (define nest (lambda (x) (+ x x)))) > ((nest 3) 4) + : #<primitive:+> nest: x : 3 parameters: x body: (lambda (x) (+ x x)) x : 3 (nest 3) ((lambda (x) (+ x x)) 4) x : 4 26 February 2003 CS 200 Spring 2003 (+ 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). 26 February 2003 CS 200 Spring 2003

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! 26 February 2003 CS 200 Spring 2003

How does CS200 compare to other CS classes? 26 February 2003 CS 200 Spring 2003

CS 101 (Spring 2001) 7. Why do programmers comment? (a) Give compiler extra instructions (b) Aid readability (c) To hide code for debugging purposes (d) (b) and (c) (e) none of the above “Correct” answer 26 February 2003 CS 200 Spring 2003

Why Comment? To assign credit, blame or (non) liability /* I was drunk when I wrote this. */ To document external assumptions (e.g., this parameter must be a sorted list) To remind you what you are doing (; This is a bug!) To keep code readers entertained with jokes, poems, and stories To make sure your employer has to rehire you to fix the code (comments in foreign languages are especially effective) Because you are taking a course where programs are judged based on the number of comments in it, not the clarity and economy of your code 26 February 2003 CS 200 Spring 2003

CS 201: Software Development Methods (Fall 2001) 5. [13 points total] Recursion. The “SuperCool” function is defined for positive integers greater than or equal to zero as: supercool(i) = supercool(i-1) * supercool(i-2); supercool(0) = 1; supercool(1) = 2; For example: supercool(2) = 2 * 1 = 2 supercool(3) = 2 * 2 = 4 supercool(4) = 4 * 2 = 8 supercool(5) = 8 * 4 = 32 Write a recursive function that calculates supercool. Your function must be recursive to receive any credit. 26 February 2003 CS 200 Spring 2003

CS 415: Programming Languages (Spring 2002) 3. Define a function (zip op x y) that takes lists (x1 x2 x3 ....) and (y1 y2 y3 ...) and evaluated to the list ((op x1 y1) (op x2 y2) (op x3 y3) ...). Examples: > (zip + '(1 2 3 4) '(5 10 15 20)) (6 12 18 24) > (zip list '(a b c d) '(6 7 8 9)) ((a 6) (b 7) (c 8) (d 9)) 26 February 2003 CS 200 Spring 2003

zip (define zip map) (define (zip f list1 list2) (if (null? list1) null (cons (f (car list1) (car list2)) (zip f (cdr list1) (cdr list2))))) (define zip map) (Easy way) 26 February 2003 CS 200 Spring 2003

CS588: Cryptology QPIV AUH DKV UA PKEXHE QA ATHU QPKU QPH AQPHE AUH Problem Set 1, question 1: QPIV AUH DKV UA PKEXHE QA ATHU QPKU QPH AQPHE AUH - DIVH CZO THIS ONE WAS NO HARDER TO OPEN THAN THE OTHER ONE - WISE GUY (From Safecracker Meets Safecracker in “Surely You're Joking, Mr. Feynman!: Adventures of a Curious Character”, Richard P. Feynman.) 26 February 2003 CS 200 Spring 2003

CS588: Cryptology Midterm, last question: The following statements were taken from Microsoft's recently released "Safe Internet: Microsoft Privacy & Security Fundamentals" web site and their "The Ten Immutable Laws of Security" page). For each of these statements, indicate whether the statement is true, misleading or an outright falsehood. Support your answer with 1-3 sentences. An incorrect, but well-supported answer is worth more credit that a correct, poorly-supported answer. Some parts of the statements are bold to focus your attention. a. (7) Unless your e-mail application provides encryption features such as the use of digital signatures, your messages are about as private as a letter sent in an unsealed envelope. 26 February 2003 CS 200 Spring 2003

CS588: Cryptology Midterm, last question: Outright Falsehood. Digital signatures (like physicial signatures) provide authenticity, not privacy. Anyone can read a signed message by decrypting using the signer's public key. 26 February 2003 CS 200 Spring 2003

CS 655 (Spring 2001) 1. Mergering a. Write a Scheme procedure listadder that combines two lists by adding their elements. For example, (listadder '(3 1 3) '(3 4 2)) should produce (6 5 5). 26 February 2003 CS 200 Spring 2003

listadder (define (listadder list1 list2) (if (null? list1) '() (cons (+ (car list1) (car list2)) (listadder (cdr list1) (cdr list2))))) 26 February 2003 CS 200 Spring 2003

PhD Qualifying Exam Write a procedure posfilter that takes a list of integers as input and evaluates to the list with all the non-positive elements removed. For example, (posfilter (list 34 -2 50 0 23)) evaluates to (34 50 23) (define posfilter enronize) 26 February 2003 CS 200 Spring 2003

If you don’t already have enronize… (define (posfilter lst) (if (null? lst) null (if (> (car lst) 0) (cons (car lst) (posfilter (cdr lst))) (posfilter (cdr lst))))) If you got a 7 or better on question 5, you did better than more than half of our PhD students! (18 of you got 10/10) 26 February 2003 CS 200 Spring 2003

Part d (of that question) Write a procedure filter that takes a list of integers and a filter function as input and evaluates to the list with only the elements for which the filter function evaluates to true. (define (filter lst f) (if (null? lst) null (if (f (car lst)) (filter (cdr lst)) (cons (car lst) (filter (cdr lst)))))) 26 February 2003 CS 200 Spring 2003

make-enronize Why couldn’t I ask the PhD students to implement make-enronize? 26 February 2003 CS 200 Spring 2003

PhD Qualifying Exam Define a procedure that takes a binary tree as input, and produces as output the mirror image of that tree, swapping the left and right sides at every node. How much work is your procedure? 12 of you did better on this than our best PhD student this year did! 26 February 2003 CS 200 Spring 2003

Charge No class Friday – enjoy your “break”! But that doesn’t mean you have no work to do… Mutation makes evaluation rules more complicated Environment diagrams quickly get complicated – but like substitution evaluations, just follow rules mechanically Don’t wait until after Spring Break to start PS5 Finish GEB part I before next class. 26 February 2003 CS 200 Spring 2003