the environment model of evaluation
play

The Environment Model of Evaluation Jim Royer CIS 352 March 22, - PowerPoint PPT Presentation

The Environment Model of Evaluation Jim Royer CIS 352 March 22, 2019 CIS 352 The Environment Model of Evaluation 1 / 31 References Structure and Interpretation of Computer Programs, 2/e, 3.2: The Environment Model of Evaluation , by


  1. The Environment Model of Evaluation Jim Royer CIS 352 March 22, 2019 CIS 352 The Environment Model of Evaluation 1 / 31

  2. References Structure and Interpretation of Computer Programs, 2/e, § 3.2: The Environment Model of Evaluation , by Harold Abelson and Gerald Sussman, MIT Press, 1996. https://mitpress.mit.edu/sicp/full-text/book/book.html William Cook, Anatomy of Programming Languages , Chapters 3 and 4, http://www.cs.utexas.edu/~wcook/anatomy/anatomy.htm CIS 352 The Environment Model of Evaluation 2 / 31

  3. LFP = LC + λ + function application + variables LFP Expressions E :: = n | b | ℓ | E iop E | E cop E | if E then E else E | ! E | E : = E | skip | E ; E | while E do E | x | λ x . E | E E | let x = E in E � �� � the λ -calculus where x ∈ V , an infinite set of variables n ∈ Z (integers), b ∈ B (booleans), ℓ ∈ L (locations) iop ∈ (integer-valued binary operations) cop ∈ (boolean-valued binary comparisons) We focus on the ( λ -calculus + let ) part of LFP. CIS 352 The Environment Model of Evaluation 3 / 31

  4. Application via substitution and its problems Call by name 1 , s ′ � 1 [ E 2 / x ] , s ′ � ⇓ � V , s ′′ � ⇓ -cbn: � E 1 , s � ⇓ � λ x . E ′ � E ′ � ( E 1 E 2 ) , s � ⇓ � V , s ′′ � Call by value 1 , s ′ � � E 2 , s ′ � ⇓ � V 2 , s ′′ � 1 [ V 2 / x ] , s ′′ � ⇓ � V , s ′′′ � ⇓ -cbv: � E 1 , s � ⇓ � λ x . E ′ � E ′ � ( E 1 E 2 ) , s � ⇓ � V , s ′′′ � Call-by-name and call-by-value are defined above via substitution . Substitution is: dandy for nailing down sensible meanings of application. stinko for everyday implementations. E.g., An implementation via substitution constantly needs to modify a program’s source code. Idea: In place of substituting a value v for a variable x : Keep a dictionary of variables & their values. When you need the value of x , look it up. CIS 352 The Environment Model of Evaluation 4 / 31

  5. Environments ( Warning: Scary Greek letters) Definition An environment is just a table of variables and associated values . Consider an expression e = if z then x else y + 2. With environment { x �→ 3, y �→ 4, z �→ tt } , e evaluates to 3. With environment { x �→ 8, y �→ 5, z �→ ff } , e evaluates to 7. Etc. lookup ( ρ , x ) returns the value (if any) of x in environment ρ . update ( ρ , x , v ) returns a new environment ρ [ x �→ v ] ( ρ [ x �→ v ] is just like ρ except x has value v.) Evaluating variable x in environment ρ ≡ lookup ( ρ , x ) . CIS 352 The Environment Model of Evaluation 5 / 31

  6. Revising call-by-value big-step semantics, 1 Definition ρ ⊢ � e , s � ⇓ V � v , s ′ � means that expression e with environment ρ and state s evaluates to value v and state s ′ . Var: ρ ⊢ � x , s � ⇓ V � v , s � ( v = lookup ( ρ , x )) Let: ρ ⊢ � e 1 , s � ⇓ V � v 1 , s ′ � ρ [ x �→ v 1 ] ⊢ � e 2 , s ′ � ⇓ V � v 2 , s ′′ � ρ ⊢ � let x = e 1 in e 2 , s � ⇓ V � v 2 , s ′′ � Examples/Exercises: Let ρ = { x �→ 7, y �→ 3 } . ρ ⊢ � x + y , s � ⇓ V ?? ρ ⊢ � let x = 1 in x + y , s � ⇓ V ?? ρ ⊢ � let x = 1 in ( let z = 11 in x + y + z ) , s � ⇓ V ?? CIS 352 The Environment Model of Evaluation 6 / 31

  7. Revising call-by-value big-step semantics, 2 Preliminary versions of these rules: 1 , s ′ � � λ x . e ′ ρ ⊢ � e 1 , s � ⇓ V ρ ⊢ � e 2 , s ′ � � v 2 , s ′′ � ⇓ V 1 , s ′′ � � v , s ′′′ � ρ [ x �→ v 2 ] ⊢ � e ′ ⇓ V App: Fun: ρ ⊢ � λ x . e , s � ⇓ V � λ x . e , s � � v , s ′′′ � ρ ⊢ � ( e 1 e 2 ) , s � ⇓ V Examples/Exercises: Let ρ = { x �→ 7, y �→ 3 } . ρ ⊢ � let f = λ x . ( x + y ) in ( f 10 ) , s � ⇓ V ?? !!! ρ ⊢ � let f = λ x . ( x + y ) in ( let y = 100 in ( f 10 )) , s � ⇓ V ?? CIS 352 The Environment Model of Evaluation 7 / 31

  8. Scoping Definition (Variable Scope) The scope of a variable binding/declaration is the region of a program where the binding is valid, i.e., when you use the variable, it uses that declaration for the binding (meaning) of the name. A Java example (static/lexical scoping) { int i = 23; the outer i ’s scope for (int i = 1; i<11; i++) { ... } the inner i ’s scope System.out.println(i); ... } CIS 352 The Environment Model of Evaluation 8 / 31

  9. Dynamic Scoping, 1 Re: λ -expressions, functions, procedures, etc., there are two sorts of environments you have to worry about: The environment in force when the function was created . 1 The environment in force when the function is applied . 2 1 , s ′ � � λ x . e ′ ρ ⊢ � e 1 , s � ⇓ V ρ ⊢ � e 2 , s ′ � � v 2 , s ′′ � ⇓ V 1 , s ′′ � � v , s ′′′ � ☞ ρ [ x �→ v 2 ] ⊢ � e ′ ⇓ V Dynamic-App: � v , s ′′′ � ρ ⊢ � ( e 1 e 2 ) , s � ⇓ V Example: Let ρ = { x �→ 7, y �→ 3 } and consider ρ ⊢ � let f = λ x . x + y in let g = λ y . f ( y + 100 ) in (( f 10 ) + ( g 0 )) , s � ⇓ V ?? CIS 352 The Environment Model of Evaluation 9 / 31

  10. Dynamic Scoping, 2 1 , s ′ � � λ x . e ′ ρ ⊢ � e 1 , s � ⇓ V ρ ⊢ � e 2 , s ′ � � v 2 , s ′′ � ⇓ V 1 , s ′′ � � v , , s ′′′ � ☞ ρ [ x �→ v 2 ] ⊢ � e ′ ⇓ V Dynamic-App: � v , s ′′′ � ρ ⊢ � ( e 1 e 2 ) , s � ⇓ V Under dynamic scoping, when you apply a function in environment (( λ x . e ′ 1 ) e 2 ) in environment ρ you evaluate e ′ 1 in environment ρ [ x �→ v 2 ] . Question: Is this a bug or a feature? CIS 352 The Environment Model of Evaluation 10 / 31

  11. Dynamic Scoping, 3 1 , s ′ � � λ x . e ′ ρ ⊢ � e 1 , s � ⇓ V ρ ⊢ � e 2 , s ′ � � v 2 , s ′′ � ⇓ V ☞ ρ [ x �→ v 2 ] ⊢ � e ′ 1 , s ′′ � � v , s ′′′ � ⇓ V Dynamic-App: � ( v , s ′′′ � ρ ⊢ � ( e 1 e 2 ) , s � ⇓ V What goes right under dynamic scoping? let f = λ n . if n ≤ 0 then 1 else n ∗ ( f ( n − 1 )) in ( f 3 ) History Discovered and formalized in early ( ≈ 1960s) Lisp implementations. CIS 352 The Environment Model of Evaluation 11 / 31

  12. Lexical Scoping, 1 Re: λ -expressions, functions, procedures, etc., there are two sorts of environments you have to worry about: The environment in force when the function is created . 1 The environment in force when the function is applied . 2 In human language, statements need to be understood in context: Such a fact is probable, but undoubtedly false. — Edward Gibbon in “Decline and Fall of the Roman Empire” When Gibbon was writing “probable” meant “well-recommended”. So in reading Gibbon we have to use a 1700’s English dictionary. We pull a similar trick for functions. CIS 352 The Environment Model of Evaluation 12 / 31

  13. Lexical Scoping, 2 Definition A closure, e ρ , is an expression e with an environment ρ such that fv ( e ) ⊆ domain ( ρ ) , i.e., all of e ’s free variables are in ρ ’s dictionary. Ideas: A λ -expression evaluates to a closure. When we create a λ -expression, we “close” it with its definition-time environment. Lexical-Fun: ρ ⊢ � λ x . e , s � ⇓ V � ( λ x . e ) ρ , s � When we apply a function (i.e., closure ( λ x . e ′ ) ρ ′ ), we evaluate e ′ in ρ ′ [ x �→ v ] , where v is the value of the argument. 1 , s ′ � � ( λ x . e ′ 1 ) ρ ′ ρ ⊢ � e 1 , s � ⇓ V ρ ⊢ � e 2 , s ′ � � v 2 , s ′′ � ⇓ V 1 , s ′′ � � v , s ′′′ � ρ ′ 1 [ x �→ v 2 ] ⊢ � e ′ ⇓ V Lexical-App: � ( v , s ′′′ � ρ ⊢ � ( e 1 e 2 ) , s � ⇓ V CIS 352 The Environment Model of Evaluation 13 / 31

  14. Lexical Scoping, 3 Lexical-Fun: ρ ⊢ � λ x . e , s � ⇓ V � ( λ x . e ) ρ , s � � �� � a closure a closure � �� � 1 , s ′ � ( λ x . e ′ 1 ) ρ ′ ρ ⊢ � e 1 , s � ⇓ V � ρ ⊢ � e 2 , s ′ � � v 2 , s ′′ � ⇓ V 1 , s ′′ � � v , s ′′′ � ρ ′ 1 [ x �→ v 2 ] ⊢ � e ′ ⇓ V Lexical-App: � ( v , s ′′′ � ρ ⊢ � ( e 1 e 2 ) , s � ⇓ V Examples/Exercises: Let ρ = { x �→ 7, y �→ 3 } . ρ ⊢ � let f = λ x . ( x + y ) in ( f 10 ) , s � ⇓ V ?? ρ ⊢ � let f = λ x . ( x + y ) in ( let y = 100 in ( f 10 )) , s � ⇓ V ?? ρ ⊢ � let f = λ n . if n ≤ 0 then 1 else n ∗ ( f ( n − 1 )) in ( f 3 ) , s � ⇓ V ?? CIS 352 The Environment Model of Evaluation 14 / 31

  15. Puzzle 1 ρ 1 =[ a �→ 1, b �→ 2 ] e 1 = let q = λ a . ( a + b ) in let a = 5 ∗ b in let b = a ∗ b in ( q 100 ) What the value of e 1 in environment ρ 1 under call-by-value with lexical scoping? (a) dynamic scoping? (b) CIS 352 The Environment Model of Evaluation 15 / 31

  16. Puzzle 1(a): Call-by-value, lexical scoping tag Environment Expression a �→ 1 ρ 1 : let q = . . . b �→ 2 ρ 1 =[ a �→ 1, b �→ 2 ] ↑ e 1 = let q = λ a . ( a + b ) in ρ 2 : q �→ ( λ a . ( a + b )) ρ 1 let a = . . . let a = 5 ∗ b in ↑ let b = a ∗ b in a �→ 10 let b = . . . ρ 3 : ↑ ( q 100 ) ρ 4 : b �→ 20 ( q 100 ) a �→ 100 → ρ 1 ( a + b ) ρ 5 : value of e 1 ρ 1 : 102 CIS 352 The Environment Model of Evaluation 16 / 31

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend