南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)04-Environment Diagrams

Environment Diagrams
Environment Diagrams

What are Environment Diagrams? A visual tool to keep track of bindings state of a computer program In this class,we use Python as our primary language o The diagrams we teach can be applied to similar languages
What are Environment Diagrams? ● A visual tool to keep track of bindings & state of a computer program ● In this class, we use Python as our primary language ○ The diagrams we teach can be applied to similar languages

Why do we use Environment Diagrams? Environment Diagrams are conceptual understand why programs work the way they do confidently predict how a program will behave Environment Diagrams are helpful for debugging When you're really stuck, diagramming code staring at lines of code Environment Diagrams will be used in future courses CS 61C (Machine Structures) CS 164 (Programming Languages and Compilers)
Why do we use Environment Diagrams? • Environment Diagrams are conceptual • understand why programs work the way they do • confidently predict how a program will behave • Environment Diagrams are helpful for debugging • When you're really stuck, diagramming code > staring at lines of code • Environment Diagrams will be used in future courses • CS 61C (Machine Structures) • CS 164 (Programming Languages and Compilers)

What do we've seen so far Assignment Statements Global frame X=1 X 3 X =XX+X Def Statements Global frame function def square(x): square(x)】 square return xx square Call Expressions X 4 square(4) Frame Return 16 value
What do we've seen so far Assignment Statements x = 1 x = x + x + x Def Statements def square(x): return x * x Call Expressions square(4) Frame

Terminology:Frames Global frame function square(x) A frame keeps track of varia square Every call expression has a corr square Global,a.k.a.the global fr 4 arting frame. It doesn't correspond to a speci Return 1. 16 Parent frames value The parent of a function is the frame in which it was defined. If you can't find a variable in the current frame,you check it's parent,and so on.If you can't find the variable,NameError Demo
Terminology: Frames A frame keeps track of variable-to-value bindings. ● Every call expression has a corresponding frame. Global, a.k.a. the global frame, is the starting frame. ● It doesn't correspond to a specific call expression. Parent frames ● The parent of a function is the frame in which it was defined. ● If you can't find a variable in the current frame, you check it's parent, and so on. If you can't find the variable, NameError Demo

Check Your Understanding Draw the environment diagram def square(x): return xX def sum_of_squares(x,y): return square(x)+square(y) sum_of_squares(3,4)
Draw the environment diagram def square(x): return x * x def sum_of_squares(x, y): return square(x) + square(y) sum_of_squares(3, 4) Check Your Understanding

Review:Evaluation Order Demo Remember to evaluate the operator,then the operand(s),then apply the operator onto the operand(s). def add_one(x): Evaluate the operator.A Evaluate the operand.. y=X+1 function value is returned return y Returns 10 Returns 100 def square(x): Evaluate the operator.A Evaluate the operand.Now we have evaluate another return xx function value is returned expression. What will the environment diagram look like?(When are frames created?) The environment diagram should reflect Python's evaluation
square(add_one(9)) Review: Evaluation Order def add_one(x): y = x + 1 return y def square(x): return x * x Remember to evaluate the operator, then the operand(s), then apply the operator onto the operand(s). What will the environment diagram look like? (When are frames created?) The environment diagram should reflect Python’s evaluation. Evaluate the operator. A function value is returned Evaluate the operand. Now we have evaluate another expression. Evaluate the operator. A function value is returned Evaluate the operand.. Returns 10 Returns 100 Demo

Variable Lookup
Variable Lookup

Local Names Variable Lookup: Lookup name in the current frame 。 Lookup name in parent frame,its parent frame,etc.. Stop at the global frame If not found,an error is thrown Global frame >func f(x.y)[parent=Global] An error is thrown func g(z)[parent=Global] g f1:f【parent=Global] def f(x,y): Name"x"is not found,again ×5 y10 return g(x) Name"x"is not found 2 8 parent=Global] z5 def g(z): Important:There was no lookup done in f1 since return z x the parent of f2 was Global What happens here? result f(5,10)
Where do we look next? Local Names Variable Lookup: • Lookup name in the current frame • Lookup name in parent frame, its parent frame, etc.. • Stop at the global frame • If not found, an error is thrown def f(x, y): return g(x) def g(z): return z + x result = f(5, 10) Name “x” is not found Name “x” is not found, again An error is thrown What happens here? Important: There was no lookup done in f1 since the parent of f2 was Global

Demo Evaluation vs Apply def a_plus_bc(a,b,c): I II I >>> a-p1us_bc(2,3,4)#2+3*4 14 Apply operator How many frames are a_plus_bc function to created? n operand 4,3,81. In what order? a_plus_bc(square(2),3,square(square(3))) Apply operator Apply operator square function to Apply operator square function to operand 2. square function to operand 9. operand 3
Evaluation vs Apply def a_plus_bc(a, b, c): """ >>> a_plus_bc(2, 3, 4) # 2 + 3 * 4 14 """ bc = b * c a_plus_bc(square( return a 2+ ), bc 3, square(square(3))) Demo How many frames are created? In what order? Apply operator square function to operand 2. Apply operator square function to operand 3. Apply operator square function to operand 9. Apply operator a_plus_bc function to operand 4, 3, 81
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)03-Control.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)02-Names & Functions.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)01-Introduction(主讲:冯新宇).pptx
- 《计算机科学》相关教学资源(PPT课件讲稿)Modular Verification of Concurrent Assembly Code with Dynamic Thread Creation and Termination.ppt
- 《计算机科学》相关教学资源(PPT课件讲稿)Modular Verification of Assembly Code with Stack-Based Control Abstractions.ppt
- 《计算机科学》相关教学资源(PPT课件讲稿)An Open Framework for Foundational Proof-Carrying Code.ppt
- 《计算机科学》相关教学资源(PPT课件讲稿)Certifying Low-Level Programs with Hardware Interrupts and Preemptive Threads.ppt
- 《计算机科学》相关教学资源(PPT课件讲稿)On the Relationship between Concurrent Separation Logic and Assume-Guarantee Reasoning.ppt
- 《计算机科学》相关教学资源(参考文献)Technical Report TTIC-TR-2008-1(Local Rely-Guarantee Reasoning).pdf
- 《计算机科学》相关教学资源(参考文献)Deny-Guarantee Reasoning.pdf
- 《计算机科学》相关教学资源(参考文献)A Rely-Guarantee-Based Simulation for Verifying Concurrent Program Transformations.pdf
- 《计算机科学》相关教学资源(参考文献)Modular Verification of Linearizability with Non-Fixed Linearization Points.pdf
- 《计算机科学》相关教学资源(参考文献)Characterizing Progress Properties of Concurrent Objects via Contextual Refinements.pdf
- 《计算机科学》相关教学资源(参考文献)Rely-Guarantee-Based Simulation for Compositional Verification of Concurrent Program Transformations.pdf
- 《计算机科学》相关教学资源(参考文献)Compositional Verification of Termination-Preserving Refinement of Concurrent Programs.pdf
- 《计算机科学》相关教学资源(参考文献)A Program Logic for Concurrent Objects under Fair Scheduling.pdf
- 《计算机科学》相关教学资源(参考文献)A Practical Verification Framework for Preemptive OS Kernels.pdf
- 《计算机科学》相关教学资源(参考文献)Progress of Concurrent Objects with Partial Methods.pdf
- 《计算机科学》相关教学资源(参考文献)POMP:Protocol Oblivious SDN Programming with Automatic Multi-Table Pipelining.pdf
- 《计算机科学》相关教学资源(参考文献)Decay of Correlation in Spin Systems.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)05-Higher-Order Functions.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)06-Recursion.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)07-Recursion Examples.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)08-Containers.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)09-Data-Abstractions.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)10-Trees.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)11-Mutable-Values.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)12-Mutable Functions & Growth.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)13-Iterators.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)13-Iterators & Generators.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)14-Object-Oriented Programming.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)15-Inheritance.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)16-Linked Lists & Mutable Trees.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)17-Interfaces.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)18-Scheme.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)19-More-Scheme.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)20-Interpreters.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)21-Macros.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)22-Streams.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)23-SQL-I.pptx