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

the environment model of evaluation
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

The Environment Model of Evaluation

Jim Royer

CIS 352

March 22, 2019

CIS 352 The Environment Model of Evaluation 1 / 31

slide-2
SLIDE 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

slide-3
SLIDE 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

  • the λ-calculus

| let x = E in E 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

slide-4
SLIDE 4

Application via substitution and its problems

Call by name

⇓-cbn: E1, s ⇓ λx.E′

1, s′

E′

1[E2/x], s′ ⇓ V, s′′

(E1 E2), s ⇓ V, s′′ Call by value

⇓-cbv: E1, s ⇓ λx.E′

1, s′

E2, s′ ⇓ V2, s′′ E′

1[V2/x], s′′ ⇓ V, s′′′

(E1 E2), 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

slide-5
SLIDE 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

slide-6
SLIDE 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: ρ ⊢ e1, s ⇓V v1, s′

ρ[x → v1] ⊢ e2, s′ ⇓V v2, s′′ ρ ⊢ let x = e1 in e2, s ⇓V v2, 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

slide-7
SLIDE 7

Revising call-by-value big-step semantics, 2

Preliminary versions of these rules:

App: ρ ⊢ e1, s ⇓V λx.e′

1, s′

ρ ⊢ e2, s′ ⇓V v2, s′′ ρ[x → v2] ⊢ e′

1, s′′

⇓V v, s′′′ ρ ⊢ (e1 e2), s ⇓V v, s′′′ Fun: ρ ⊢ λx.e, s ⇓V λx.e, s

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

slide-8
SLIDE 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; for (int i = 1; i<11; i++) { ...} System.out.println(i); ... }

the outer i’s scope the inner i’s scope

CIS 352 The Environment Model of Evaluation 8 / 31

slide-9
SLIDE 9

Dynamic Scoping, 1

Re: λ-expressions, functions, procedures, etc., there are two sorts of environments you have to worry about:

1

The environment in force when the function was created.

2

The environment in force when the function is applied.

Dynamic-App: ρ ⊢ e1, s ⇓V λx.e′

1, s′

ρ ⊢ e2, s′ ⇓V v2, s′′

☞ ρ[x → v2] ⊢ e′

1, s′′

⇓V v, s′′′ ρ ⊢ (e1 e2), s ⇓V v, s′′′

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

slide-10
SLIDE 10

Dynamic Scoping, 2

Dynamic-App: ρ ⊢ e1, s ⇓V λx.e′

1, s′

ρ ⊢ e2, s′ ⇓V v2, s′′

☞ ρ[x → v2] ⊢ e′

1, s′′

⇓V v, , s′′′ ρ ⊢ (e1 e2), s ⇓V v, s′′′

Under dynamic scoping, when you apply a function in environment ((λx.e′

1) e2)

in environment ρ you evaluate e′

1 in environment ρ[x → v2].

Question: Is this a bug or a feature?

CIS 352 The Environment Model of Evaluation 10 / 31

slide-11
SLIDE 11

Dynamic Scoping, 3

Dynamic-App: ρ ⊢ e1, s ⇓V λx.e′

1, s′

ρ ⊢ e2, s′ ⇓V v2, s′′

☞ ρ[x → v2] ⊢ e′

1, s′′

⇓V v, s′′′ ρ ⊢ (e1 e2), s ⇓V (v, s′′′

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

slide-12
SLIDE 12

Lexical Scoping, 1

Re: λ-expressions, functions, procedures, etc., there are two sorts of environments you have to worry about:

1

The environment in force when the function is created.

2

The environment in force when the function is applied. 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

slide-13
SLIDE 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.

Lexical-App: ρ ⊢ e1, s ⇓V (λx.e′

1)ρ′ 1, s′

ρ ⊢ e2, s′ ⇓V v2, s′′ ρ′

1[x → v2] ⊢ e′ 1, s′′

⇓V v, s′′′ ρ ⊢ (e1 e2), s ⇓V (v, s′′′

CIS 352 The Environment Model of Evaluation 13 / 31

slide-14
SLIDE 14

Lexical Scoping, 3

Lexical-Fun: ρ ⊢ λx.e, s ⇓V (λx.e)ρ

a closure

, s Lexical-App: ρ ⊢ e1, s ⇓V

  • a closure
  • (λx.e′

1)ρ′ 1, s′

ρ ⊢ e2, s′ ⇓V v2, s′′ ρ′

1[x → v2] ⊢ e′ 1, s′′

⇓V v, s′′′ ρ ⊢ (e1 e2), s ⇓V (v, s′′′

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

slide-15
SLIDE 15

Puzzle 1

ρ1 =[a → 1, b → 2] e1 = let q = λa.(a + b) in let a = 5 ∗ b in let b = a ∗ b in (q 100) What the value of e1 in environment ρ1 under call-by-value with

(a)

lexical scoping?

(b)

dynamic scoping?

CIS 352 The Environment Model of Evaluation 15 / 31

slide-16
SLIDE 16

Puzzle 1(a): Call-by-value, lexical scoping

ρ1 =[a → 1, b → 2] e1 = let q = λa.(a + b) in let a = 5 ∗ b in let b = a ∗ b in (q 100)

tag Environment Expression ρ1: a → 1 b → 2 let q = . . . ↑ ρ2: q → (λa.(a + b))ρ1 let a = . . . ↑ ρ3: a → 10 let b = . . . ↑ ρ4: b → 20 (q 100) ρ5: a → 100 → ρ1 (a + b)

value of e1ρ1: 102

CIS 352 The Environment Model of Evaluation 16 / 31

slide-17
SLIDE 17

Puzzle 1(b): Call-by-value, dynamic scoping

ρ1 =[a → 1, b → 2] e1 = let q = λa.(a + b) in let a = 5 ∗ b in let b = a ∗ b in (q 100)

tag Environment Expression ρ1: a → 1 b → 2 let q = . . . ↑ ρ2: q → (λa.(a + b)) let a = . . . ↑ ρ3: a → 10 let b = . . . ↑ ρ4: b → 20 (q 100) ↑ ρ5: a → 100 (a + b)

value of e1ρ1: 120

CIS 352 The Environment Model of Evaluation 17 / 31

slide-18
SLIDE 18

Puzzle 2

ρ1 = [a → 1, b → 2] e2 = let p = λa.(a + b) in let q = λb.(a + (p b)) in let a = 10 in let b = 20 in (q 100) What is the value of e2 in environment ρ1 under call-by-value with

(a)

lexical scoping?

(b)

dynamic scoping?

CIS 352 The Environment Model of Evaluation 18 / 31

slide-19
SLIDE 19

Puzzle 2(a): Call-by-value, lexical scoping

ρ1 = [a → 1, b → 2] e2 = let p = λa.(a + b) in let q = λb.(a + (p b)) in let a = 10 in let b = 20 in (q 100)

tag Environment Expression ρ1: a → 1 b → 2 let p = . . . ↑ ρ2: p → (λa.(a + b))ρ1 let q = . . . ↑ ρ3: q → (λb.(a + (p b)))ρ2 let a = . . . ↑ ρ4: a → 10 let b = . . . ↑ ρ5: b → 20 (q 100) ρ6: b → 100 → ρ2 a + (p b) ρ7: a → 100 → ρ1 (a + b)

value of e2ρ1: 1+(100+2) = 103

CIS 352 The Environment Model of Evaluation 19 / 31

slide-20
SLIDE 20

Puzzle 2(b): Call-by-value, dynamic scoping

ρ1 = [a → 1, b → 2] e2 = let p = λa.(a + b) in let q = λb.(a + (p b)) in let a = 10 in let b = 20 in (q 100)

tag Environment Expression ρ1: a → 1 b → 2 let p = . . . ↑ ρ2: p → (λa.(a + b)) let q = . . . ↑ ρ3: q → (λb.(a + (p b))) let a = . . . ↑ ρ4: a → 10 let b = . . . ↑ ρ5: b → 20 (q 100) ↑ ρ6: b → 100 a + (p b) ↑ ρ7: a → 100 (a + b)

value of e2ρ1: 10+(100+100) = 210

CIS 352 The Environment Model of Evaluation 20 / 31

slide-21
SLIDE 21

Lexical Scoping, 4: Closures + States = Objects

Suppose (new v) returns a fresh location initialize to v. Warning: The following is tormented LFP; return is as in HW10.

let mkbox = λx.(let bx = (new x) in (λy.{ bx : =!bx + y; return !bx })); in let u = (mxbox 10); in let v = (mxbox (100 + (u 5))) in ((u 0) + (v 0)) [Trace this thing]

In more familiar notation, mkbox is roughly:

function mxbox(x) = { var bx = (new x); return (function foo(v) { bx :=!bx + v; return !bx }); } In Java terms: • box is a class

  • mkbox is a box-constructor
  • u and v are instance methods
  • bx is an instance variable.

CIS 352 The Environment Model of Evaluation 21 / 31

slide-22
SLIDE 22

Lexical Scoping, 5: What about call-by-name?

Call by name

Subst-App-cbn: E1, s ⇓N λx.E′

1, s′

E′

1[E2/x], s′ ⇓N V, s′′

(E1 E2), s ⇓N V, s′′ Question: With environments, how do we simulate substituting the unevaluated E2 for x in E′

1 that call-by-name requires?

Answer: Thunks ≡ closures of arbitrary expressions, not just λ-expressions.

History of the term: http://www.retrologic.com/jargon/T/thunk.html

CIS 352 The Environment Model of Evaluation 22 / 31

slide-23
SLIDE 23

Lexical Scoping, 6

The Call-By-Name Version

Lexical-App: ρ ⊢ e1, s ⇓N

a closure

  • (λx.e′

1)ρ′ 1, s′

ρ[x →

thunk

  • e2ρ ] ⊢ e′

1, s′ ⇓N v, s′′

ρ ⊢ (e1 e2), s ⇓N (v, s′′ Var: ρ′ ⊢ e′, s ⇓N v′, s′ ρ ⊢ x, s ⇓N v′, s′

  • e′ρ′ = lookup(ρ, x)
  • Call-by-name/dynamic-scoping makes very little sense,

. . . but we are implementing it any way in Homework 10.

CIS 352 The Environment Model of Evaluation 23 / 31

slide-24
SLIDE 24

Puzzle 3

ρ0 = ∅ s0 = [ ℓ → 0] e0 = let g = λx.{ ℓ : =!ℓ + 1; return x }; in let z = (g 100) in (z + z + z) Consider ρ0 ⊢ (e0, s0) ⇓? (v1, s1). What are v1 and s1 we use lexical scoping and

(a)

call-by-value evaluation?

(b)

call-by-name evaluation?

CIS 352 The Environment Model of Evaluation 24 / 31

slide-25
SLIDE 25

Puzzle 3(a): Call-by-value

ρ0 = ∅ s0 = [ ℓ → 0] e0 = let g = λx.{ ℓ : =!ℓ + 1; return x }; in let z = (g 100) in (z + z + z) What are v1 and s1 in ρ0 ⊢ (e0, s0) ⇓V (v1, s1)?

tag Environment Exp./State ρ0: let g = . . . ↑ [ℓ → 0] ρ1 : g → (λx.{ . . . })ρ0 let z = (g 100) ↑ [ℓ → 1] ρ2 : z → 100 z + z + z [ℓ → 1] v1 = 300 s1 = [ℓ → 1]

CIS 352 The Environment Model of Evaluation 25 / 31

slide-26
SLIDE 26

Puzzle 3(b): Call-by-name

ρ0 = ∅ s0 = [ ℓ → 0] e0 = let g = λx.{ ℓ : =!ℓ + 1; return x }; in let z = (g 100) in (z + z + z) What are v1 and s1 in ρ0 ⊢ (e0, s0) ⇓N (v1, s1)?

tag Environment Exp./State ρ0: let g = . . . ↑ [ℓ → 0] ρ1 : g → (λx.{ . . . })ρ0 let z = (g 100) ↑ [ℓ → 0] ρ2 : z → (g 100)ρ1 z + z + z [ℓ → 3] v1 = 300 s1 = [ℓ → 3]

CIS 352 The Environment Model of Evaluation 26 / 31

slide-27
SLIDE 27

Puzzle 4

ρ0 = ∅ s0 = [ ℓ → 0] e0 = let g = λx.{ ℓ : =!ℓ + 1; return x }; in let h = λy.2; in (h (g 89)) Consider ρ0 ⊢ (e0, s0) ⇓? (v1, s1). What are v1 and s1 we use lexical scoping and

(a)

call-by-value evaluation?

(b)

call-by-name evaluation?

CIS 352 The Environment Model of Evaluation 27 / 31

slide-28
SLIDE 28

Puzzle 4(a): Call-by-value

ρ0 = ∅ s0 = [ ℓ → 0] e0 = let g = λx.{ ℓ : =!ℓ + 1; return x }; in let h = λy.2 in (h (g 89)) What are v1 and s1 in ρ0 ⊢ (e0, s0) ⇓V (v1, s1)?

tag Environment Exp./State ρ0: let g = . . . ↑ [ℓ → 0] ρ1 : g → (λx.{ . . . })ρ0 let h = . . . ↑ [ℓ → 0] ρ2 : h → (λy.2)ρ1 (h(g 89)) [ℓ → 0] ρ3: x → 89 → ρ0 { ℓ : =!ℓ + 1; return x} [ℓ → 1] ρ4: y → 89 → ρ1 2 [ℓ → 1] v1 = 2 s1 = [ℓ → 1]

CIS 352 The Environment Model of Evaluation 28 / 31

slide-29
SLIDE 29

Puzzle 4(b): Call-by-name

ρ0 = ∅ s0 = [ ℓ → 0] e0 = let g = λx.{ ℓ : =!ℓ + 1; return x }; in let h = λy.2 in (h (g 89)) What are v1 and s1 in ρ0 ⊢ (e0, s0) ⇓V (v1, s1)?

tag Environment Exp./State ρ0: let g = . . . ↑ [ℓ → 0] ρ1 : g → (λx.{ . . . })ρ0 let h = . . . ↑ [ℓ → 0] ρ2 : h → (λy.2)ρ1 (h(g 89)) [ℓ → 0] ρ4: y → (g 89)ρ2 → ρ1 2 [ℓ → 0] v1 = 2 s1 = [ℓ → 0]

CIS 352 The Environment Model of Evaluation 29 / 31

slide-30
SLIDE 30

Recursion under lexical scoping, 1

Recall: E ::= . . . | rec x.E Informally: “rec x.E” reads recursively define x to be E. The big-step operational semantics is given by:

unfoldingsubst: E[(rec x.E)/x], s ⇓ V, s′

rec x.E, s ⇓ V, s′

CIS 352 The Environment Model of Evaluation 30 / 31

slide-31
SLIDE 31

Recursion under lexical scoping, 2

The substitution-based version of unfold

unfoldingsubst: E[(rec x.E)/x], s ⇓ V, s′

rec x.E, s ⇓ V, s′

An environment-based version of unfold

(There are better ways!)

unfoldingenv: ρ[x → (rec x.E)] ⊢ E, s ⇓ V, s′

ρ ⊢ rec x.E, s ⇓ V, s′ Try: ⊢ rec z.(if !ℓ > 0 then (ℓ : =!ℓ − 1; z) else skip), { ℓ → 2 } ⇓ ??

CIS 352 The Environment Model of Evaluation 31 / 31