bindings (review) Topic 18 Environment Model of Evaluation bindings (review) frames (review) frames (review) frames (review) x: 10 y: #f x: 10

Similar documents
Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

CS61A Summer 2010 George Wang, Jonathan Kotker, Seshadri Mahalingam, Eric Tzeng, Steven Tang

Building up a language SICP Variations on a Scheme. Meval. The Core Evaluator. Eval. Apply. 2. syntax procedures. 1.

Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Parsing Scheme (+ (* 2 3) 1) * 1

Class 6: Efficiency in Scheme

Functional Programming. Pure Functional Programming

An Introduction to Scheme

Discussion 12 The MCE (solutions)

Write a procedure powerset which takes as its only argument a set S and returns the powerset of S.

CS61A Midterm 2 Review (v1.1)

cs61amt2_4 CS 61A Midterm #2 ver March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream?

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

Lexical vs. Dynamic Scope

6.001 Notes: Section 15.1

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

This lecture covers: Prolog s execution strategy explained more precisely. Revision of the elementary Prolog data types

CS450 - Structure of Higher Level Languages

TAIL RECURSION, SCOPE, AND PROJECT 4 11

;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for

Defining Functions III: Nested Definitions

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

Normal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation?

CS 61A Midterm #2 ver March 2, 1998 Exam version: A. Your name. login: cs61a- Discussion section number. TA's name

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

Not really what happens in the computer. The Environment Model. (set-car! l 'z) (car l) z

Streams and Evalutation Strategies

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

6.001 Notes: Section 6.1

SCHEME AND CALCULATOR 5b

CS 153 Design of Operating Systems Winter 2016

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

DRAWING ENVIRONMENT DIAGRAMS

Deferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action.

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

Recap: Functions as first-class values

CSCC24 Functional Programming Scheme Part 2

Turtles All The Way Down

Environments

CS 314 Principles of Programming Languages. Lecture 16

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work?

CSc 520 Principles of Programming Languages

Sample Final Exam Questions

Discussion 4. Data Abstraction and Sequences

Project 5 - The Meta-Circular Evaluator

Project 2: Scheme Interpreter

Discussion 11. Streams

Code example: sfact SICP Explicit-control evaluator. Example register machine: instructions. Goal: a tail-recursive evaluator.

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

Project 5 - The Meta-Circular Evaluator

Review Analyzing Evaluator. CS61A Lecture 25. What gets returned by mc-eval? (not analyzing eval) REVIEW What is a procedure?

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

Procedural abstraction SICP Data abstractions. The universe of procedures forsqrt. Procedural abstraction example: sqrt

CS 314 Principles of Programming Languages

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

COP4020 Programming Assignment 1 - Spring 2011

Implementing Recursion

Lecture 4. Part 1. More on "First-Class" Procedures

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

An Explicit-Continuation Metacircular Evaluator

Putting the fun in functional programming

Lecture 12: Demand Paging

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

Comp 311: Sample Midterm Examination

Adding Machine Run 2

News. CSE 130: Programming Languages. Environments & Closures. Functions are first-class values. Recap: Functions as first-class values

Python in 10 (50) minutes

CSE 120 Principles of Operating Systems Spring 2017

6.001 Notes: Section 8.1

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

FOFL and FOBS: First-Order Functions

C311 Lab #3 Representation Independence: Representation Independent Interpreters

CS61A Notes Week 9: Muta4on (solu4ons) Revenge of the Box- and- pointers

Programming Languages. Function-Closure Idioms. Adapted from Dan Grossman's PL class, U. of Washington

CSCI B522 Lecture 11 Naming and Scope 8 Oct, 2009

Dynamic Memory Allocation

6.001: Structure and Interpretation of Computer Programs

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Programming Languages. Streams Wrapup, Memoization, Type Systems, and Some Monty Python

This exam is worth 70 points, or about 23% of your total course grade. The exam contains 15 questions.

Copying Data. Contents. Steven J. Zeil. November 13, Destructors 2

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

Principles of Programming Languages

HIGHER ORDER FUNCTIONS 2

Intermediate Programming, Spring 2017*

Transcription:

Topic 18 Environment Model of Evaluation Section 3.2 bindings (review) a binding is an association between a name and a Scheme value names: variable names, procedure names formal parameters of procedures in let statements: (let ((name value)...)...) values: any Scheme value Acknowledgement: This lecture (and also much of the previous one) were taken from an old CalTech Course Page 1 2 bindings (review) frames (review) a binding is an association between a name and a Scheme value examples: name: x value: 10 name: y value: #f name: square value: (lambda (x) (* x x)) a frame is a collection of bindings: y: #f (lambda (x) (* x x)) 3 4 frames (review) frames (review) frames are used to look up the value corresponding to a name x =? y =? square =? y: #f (lambda (x) (* x x)) frames have an enclosing to parent frame which will be another frame (parent frame) if lookup fails, go to parent frame and try again then to its parent frame etc. y: #f (lambda (x) (* x x)) 5 6 1

s (review) s (review) an is a linked chain of frames ending in the every frame defines an starting from it E1 E2 () y: 20 the is there when Scheme interpreter starts up evaluating code can create new frames and thus new s all code being evaluated does so in the context of an because names must be looked up called the ironment E3 z: 30 7 8 rule 1: define rule 1: define define creates a new binding in the current (current frame) example: (define x 10) () define creates a new binding in the current (current frame) example: (define x 10) () 9 10 rule 2: set! rule 2: set! set! changes an old binding in the current NEVER creates a new binding example: (set! x 'foo) () set! changes an old binding in the current NEVER creates a new binding example: (set! x 'foo) () x: foo 11 12 2

rule 2: set! set! can change a binding in a different frame (if no binding in current frame) example: (set! x 24) no x in so change in global env () x: foo rule 2: set! set! can change a binding in a different frame (if no binding in current frame) example: (set! x 24) no x in so change in global env () x: 24 y: 13 y: 13 (ironment) 13 (ironment) 14 rule 3: lambda rule 3: lambda a lambda expression (procedure) is a pair: the text of the lambda expression a pointer to the in which the lambda expression was evaluated (created) evaluate: (lambda (x) (* x x)) text of lambda 15 pointer to where lambda evaluated 16 with define rule 4: evaluation (define square (lambda (x) (* x x))) 17 changes the way we model apply To apply a procedure: 1. construct a new frame 2. bind the formal parameters of the procedure to the arguments of the procedure call 3. the new frame s parent is the associated with the called procedure not the calling procedure 4. evaluate the body in the new 18 3

evaluating (square 10) evaluating (square 10) new frame 19 ironment 20 evaluating (square 10) evaluating (square 10) new frame current env ironment 21 evaluate: (* x x) 22 evaluating (square 10) evaluating (square 10) current env result: 100 current env new frame "goes away" (* x x) => (* 10 10) => 100 23 24 4

accumulator (revisited) That was WAY too easy! Let's try something harder... (define (make-accum value) (lambda (x) (set! value (+ value x)) value)) N.B. body of lambda, define, cond clause is implicitly a begin block. 25 26 accumulator accumulator (another look) (define (make-accum value) (lambda (x) (set! value (+ value x)) value)) (define make-accum (lambda (value) (lambda (x) (set! value (+ value x)) value)) 27 28 (define a! (make-accum 0)) evaluate (make-accum 0) create frame bind arguments accumulator accumulator evaluate (make-accum 0) create, bind evaluate body (lambda (x) ) 29 30 5

rule 3: when we create a procedure (evaluate a lambda expression) its is the in which the lambda expression is evaluated accumulator evaluate (make-accum 0) create, bind evaluate body (lambda (x) ) result is lambda with env binding 31 32 accumulator after (define a! ) (define a! (make-accum 0)) evaluate (make-accum 0) result is (lambda (x) ) with env ptr make binding for a! in its 33 34 after (define a! ) create frame bind values using a! "trapped" frame persists 35 36 6

using a! using a! create frame bind values evaluate body (begin (set! value ) ) (set! value (+ x value)) (set! value (+ 1 0)) (set! value 1) Create frame Bind values Evaluate body (begin (set! value ),,,,) (set! value (+ x value)) (set! value (+ 1 0)) (set! value 1) 37 38 after (a! 1) trapped frame 2 3 more usage 39 40 accumulator 2 accumulator 2 (define b! (make-accum 0)) evaluate (make-accum 0) create bind (define b! (make-accum 0)) evaluate (make-accum 0) result is (lambda (x) ) w/ env ptr make binding for b! 41 42 7

resulting using b! a! and b! have their own value (b! 1) frame 43 44 using b! using a! again (b! 1) frame set! frame 45 46 using a! again using b! frame set! (b! 1) 2 3 4 (b! 1) 3 47 48 8

make-accum creates a unique frame for each call the lambda traps the frame and keeps a pointer to it only that lambda has access to that frame procedure procedure value is a local variable that is owned (and accessible) exclusively by the resulting lambda 49 50 a! and b! contrast a! and b! have unique procedure frames have unique local variables: value don t affect each other remember own state important form of encapsulation limits unwanted interactions (define astate 0) (define (accum! x) (set! astate (+ x astate)) astate)) (define (toggle!) (if (= astate 0) (set! astate 1) (set! astate 0))) 51 52 interaction encapsulation (accum! 1) 1 (toggle!) ;; astate: 0 (accum! 1) 1 (accum! 1) 2 (toggle!) ;; astate: 0 (accum! 1) 1 toggle! and accum! share a variable not independent generally a bad idea to put state in the global pollutes namespace gives other routines access even ones that don't need it can get bad interactions even accidental ones 53 54 9

disciplined use of side-effects moral of the story: there are many things we can do with side-effects that we really should not do. keep your state private! nobody else has to know controlling access What if I want multiple procedures to access the same state? want specificity give multiple procedures access but not all procedures 55 56 code and data message-passing encapsulate data with associated (limited) code to manipulate code mediates kinds of access familiar message-passing associated data and code ; returns a procedure that will act as an accumulator ; or will return its value or reset the value (define (mp-make-accum value) (lambda (op) (cond ((eq? op 'accum) (lambda (x) (set! value (+ value x)))) ((eq? op 'value) value) ((eq? op 'reset) (set! value 0)) (else (error unknown op: op))))) 57 58 message-passing use (a2 'value) 0 ((a2 'accum) 1) (a2 'value) 1 59 60 10

(a2 accum) (a2 value) 61 62 usage usage during: ((a2 accum) 1) during: ((a2 accum) 1) 63 64 usage during: ((a2 accum) 1) after: ((a2 accum) 1) usage 1 (set! value (+ value x)) (set! value (+ 0 1)) (set! value 1) 65 66 11

Big Ideas procedures point to at the place where they're evaluated procedures can have local state allows us to create procedure-specific (object-specific) state allows us to encapsulate data avoiding conflict control usage 67 12