DRAWING ENVIRONMENT DIAGRAMS

Similar documents
61A Lecture 2. Wednesday, September 4, 2013

ENVIRONMENT DIAGRAMS AND RECURSION 2

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

61A Lecture 2. Friday, August 28, 2015

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

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

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

CS 61A Higher Order Functions and Recursion Fall 2018 Discussion 2: June 26, Higher Order Functions. HOFs in Environment Diagrams

ENVIRONMENT DIAGRAMS AND RECURSION 2

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

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

Environment Diagrams and Recursion Fall 2017 Discussion 2: September 6, 2017 Solutions. 1 More Environment Diagrams

Intro. Scheme Basics. scm> 5 5. scm>

(scheme-1) has lambda but NOT define

Understand Execution of a Program

We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

CS 61A Higher Order Functions and Recursion Fall 2018 Discussion 2: June 26, 2018 Solutions. 1 Higher Order Functions. HOFs in Environment Diagrams

Structure and Interpretation of Computer Programs

CONTROL AND ENVIRONMENTS 1

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

How to Design Programs

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

CMSC 330: Organization of Programming Languages. Operational Semantics

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

Structure and Interpretation of Computer Programs

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

Lexical vs. Dynamic Scope

CS61A Notes Week 13: Interpreters

CS 415 Midterm Exam Fall 2003

Lecture #3: Environments

Structure and Interpretation of Computer Programs

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

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))

Tail Recursion. ;; a recursive program for factorial (define fact (lambda (m) ;; m is non-negative (if (= m 0) 1 (* m (fact (- m 1))))))

Structure and Interpretation of Computer Programs Fall 2012 Alternate Midterm 1

Adding Machine Run 2

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

Structure and Interpretation of Computer Programs Summer 2014 Midterm 1

61A Lecture 4. Monday, September 9

Environments

Programming Languages

Control and Environments Fall 2017 Discussion 1: August 30, Control. If statements. Boolean Operators

Next: What s in a name? Programming Languages. Data model in functional PL. What s in a name? CSE 130 : Fall Lecture 13: What s in a Name?

Racket: Modules, Contracts, Languages

Lecture #5: Higher-Order Functions. A Simple Recursion. Avoiding Recalculation. Redundant Calculation. Tail Recursion and Repetition

COMP520 - GoLite Type Checking Specification

Structure and Interpretation of Computer Programs

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

TAIL RECURSION, SCOPE, AND PROJECT 4 11

CS 231 Data Structures and Algorithms, Fall 2016

CSE 341 Section Handout #6 Cheat Sheet

ENVIRONMENT MODEL: FUNCTIONS, DATA 18

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

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

CS1 Recitation. Week 2

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

WYSE Academic Challenge 2002 Computer Science Test (Sectional) SOLUTION

Box-and-arrow Diagrams

61A Lecture 9. Friday, September 20

CS 61A Discussion 8: Scheme. March 23, 2017

Berkeley Scheme s OOP

Overloading המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון

Fall Lecture 3 September 4. Stephen Brookes

Structure and Interpretation of Computer Programs

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

Control and Environments Fall 2017 Discussion 1: August 30, 2017 Solutions. 1 Control. If statements. Boolean Operators

Space War Class Diagram. Elements of OOP. How to design interactions between objects. Space War Class Diagram with Inheritance

An Introduction to Scheme

Func%onal Programming in Scheme and Lisp

1999 Midterm & Solutions

Programming in C++ PART 2

CmpSci 187: Programming with Data Structures Spring 2015

CSC324 Functional Programming Efficiency Issues, Parameter Lists

CIS192 Python Programming

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

low and not larger than high. 18 points

Func%onal Programming in Scheme and Lisp

TUPLES AND RECURSIVE LISTS 5

CSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion

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

CS 320 Midterm Exam Solution

20-CS Programming Languages Fall Final Exam! Answer all questions Be sure to put your name on the paper in the space provided!

(Refer Slide Time: 01:01)

Reminder About Functions

SCHEME AND CALCULATOR 5b

Magma Tutorial for pairing cryptographers. Part I - Introduction

Lecture Overview Code generation in milestone 2 o Code generation for array indexing o Some rational implementation Over Express Over o Creating

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides)

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Functional Programming. Pure Functional Programming

Chapter 1. Fundamentals of Higher Order Programming

Case by Case. Chapter 3

You ve encountered other ways of signalling errors. For example, if you lookup an unbound key in a hashtable, Java (and Scala) produce nulls:

Inheritance - Assignment5

61A Lecture 3. Friday, September 5

Discussion 4. Data Abstraction and Sequences

Transcription:

DRAWING ENVIRONMENT DIAGRAMS COMPUTER SCIENCE 61A September 10, 2012 0.1 Background A frame is a location where variable bindings are stored A binding is a connection between a name and a value. The name is the name of the variable, and the value can be an integer, a function, or any other data type. It is important to keep track of which frame is the current frame. The current frame is the frame in which code is being executed currently. The frame number is a unique identifier we give to frames if necessary. Note that we only add frame numbers to frames when it is necessary to refer back to them. Therefore, many frames will not end up with frame numbers. 0.2 Starting Your Diagram First draw the global frame. Designate that it is the global frame by writing global frame in the upper-left corner. Note that the current frame is now this global frame. Figure 1: The global frame Step thorough the code evaluating one line at a time. 1

DISCUSSION : DRAWING ENVIRONMENT DIAGRAMS Page 2 0.3 When you Encounter an Assignment Statement: Evaluate the code to the right of the = while continuing to follow the procedure for environment diagrams (making new frames as necessary, etc.) Bind the name on the left of the = to the result of evaluating the code to the right of the =. Write the name to the left of the equal sign in the current frame. If the value of the right hand side is a number, string, or boolean value, write this value next to the variable name. Figure 2: The result of evaluating x, y = 6, "hello" in the global frame If the value of the right hand side is something else (for example, a function), then draw an arrow from the variable name to the value. Figure 3: The result of evaluating f = lambda x: x*x in the global frame 0.4 When you Encounter an Import Statement: Bind all the imported values within the current frame. Figure 4: The result of evaluating from operator import add in the global frame. Note that the argument list for built in functions is usually represented as... even if the function only accepts a finite number of arguments.

DISCUSSION : DRAWING ENVIRONMENT DIAGRAMS Page 3 0.5 When you Encounter a Function Call for a User-Defined Function: Evaluate the operator Evaluate the operands Create a new frame Label the upper left corner of the frame with the name of the function we are calling. Leave space to the left of this label because we may need to add a frame number here later. If the function that we re calling has a parent (i.e. the function has [parent=f<frame number>] written after it in its environment diagram representation), then write [parent=f<frame number>] in the upper-right corner of our new frame, where <frame number> is the frame number of the parent of the function we re calling. Figure 5: The frame resulting from calling a function whose name is adder parent is f2 i.e. a function whose representation in the environment diagram is something like func adder(y) [parent=f2]. Note that in this diagram, the parameters are not yet bound For each of the function s arguments, add a binding in our new frame from the argument name to the value that the operand evaluated to. The current frame is now the frame we just created. Evaluate the body of the function, stepping through the code as you had been before, but with the current frame now the new one you just created. When you reach the return statement, bind return value to the result of evaluating the right side of the return expression. Now, return from the function and take note that the current frame is now what was previously the current frame in the stack. 0.6 When you Encounter a Define Statement: Create the new function by writing the signature of the new function to the right. This signature is of the form: func <intrinsic_name>(<args1>,... <argn>)

DISCUSSION : DRAWING ENVIRONMENT DIAGRAMS Page 4 where <intrinsic_name> is the name of the function we give it in the define statement and <arg1>... <argn> are the names of the arguments this function takes If the current frame is not the global frame: If the current frame does not yet have a frame number, then select a new one that has not yet been used and write f<frame number>: in the space we left in the upper-left corner of the frame when we created it. Now, add [parent=f<frame number>] to the signature representing our new function on the right. This indicates that the function s parent is the frame in which the function is defined (the current frame) Bind the name of the function to in the current frame to the function signature on the right by drawing an arrow. Figure 6: The result of evaluating a function definition for a function named squarer from within a frame named make_squarer. 0.7 When you encounter a lambda expression: Create the new function by writing the signature of the new function to the right. This signature is of the same form as it is for a define statement, except the <intrinsic_name> is replaced with λ, because lambda functions don t have intrinsic names. Note that evaluating the lambda function itself does not involve binding any names. 0.8 Looking up a name: Start in the current frame. If the name you are looking for is not in the current frame, then continue looking in the current frame s parent. Note that if no parent is listed for a frame, then it is implied that the parent is the global frame. Continue this process until you have arrived at the global frame. If you still can t find the name you are looking for then a NameError exception is thrown.

DISCUSSION : DRAWING ENVIRONMENT DIAGRAMS Page 5 Figure 7: Note that in this diagram the result of looking up droids in the find_droids frame is we re the droids, and the result of looking up n in the same frame is 8. The lookup order passes from parent to child moving from the bottom frame to f2 to the global frame. 0.9 Dos and Don ts Never draw an arrow from one variable name to another. It is not necessary to draw environments for built in functions such as sum(...) or print(...). Remember to follow the sequence of frames from parent to child not simply from bottom to top. Remember write down the parent of every function or frame whenever this parent is not the global frame.