Download - Lexical Scoping and Closures

Transcript
Page 1: Lexical Scoping and Closures

Lexical Scoping and Closures

COS 441

Princeton University

Fall 2004

Page 2: Lexical Scoping and Closures

Lexical Scoping

• The correct environment semantics for -calculus gives rise to lexical scoping

• Evaluates body of function in the environment where the function was created– Requires introducing notion of a closure

• Does not allow for unbound variables to occur in body of an expression

• Lexical scope is the modern trend– Scheme, SML, Java Inner Classes, Python 2.1,

Mathmetica “Module” and R (GPL S-Plus clone)

Page 3: Lexical Scoping and Closures

Lexical Scoping

Values are closuresAn expression and a environment that that

holds bindings for all the free variables of the expression

Names x 2 …

Expr’s e ::= lam(x.e) | apply(e1,e2)| x

Val’s v ::= lam(x.e)[Env]

Env’s Env ::= {} | {x1 v1,… , xn vn}

Prog’s Prg ::= (Env,e)

Page 4: Lexical Scoping and Closures

Lexical Scoping Val’s v ::= lam(x.e)[Env]

Env’s Env ::= {} | {x1 v1,… , xn vn}

Prog’s Prg ::= (Env,e)

(Env,apply(E1,E2)) V

(Env,E1) lam(X’.E’)[Env’] (Env,E2) V’ (Env’[X’ V’], E’) V

eval-A

(Env,lam(X.E)) lam(X.E)[Env]

eval-L

(Env,X) Env(X)

eval-X

Page 5: Lexical Scoping and Closures

Lexical Scoping Val’s v ::= lam(x.e)[Env]

Env’s Env ::= {} | {x1 v1,… , xn vn}

Prog’s Prg ::= (Env,e)

(Env,apply(E1,E2)) V

(Env,E1) lam(X’.E’)[Env’] (Env,E2) V’ (Env’[X’ V’], E’) V

eval-A

(Env,lam(X.E)) lam(X.E)[Env]

eval-L

(Env,X) Env(X)

eval-X Capture current

environment

Page 6: Lexical Scoping and Closures

Lexical Scoping Val’s v ::= lam(x.e)[Env]

Env’s Env ::= {} | {x1 v1,… , xn vn}

Prog’s Prg ::= (Env,e)

(Env,apply(E1,E2)) V

(Env,E1) lam(X’.E’)[Env’] (Env,E2) V’ (Env’[X’ V’], E’) V

eval-A

(Env,lam(X.E)) lam(X.E)[Env]

eval-L

(Env,X) Env(X)

eval-X

Environment captured by closure

Page 7: Lexical Scoping and Closures

Lexical Scoping

• Can modify rule eval-L to only capture minimum set of variables needed to evaluate body

• Compilers can pre-compute this set during semantic analysis

(Env,lam(X.E)) lam(X.E)[Envmin]

X1,…, Xn 2 FN(lam(X.E)) Envmin = {X1 Env(X1),…,Xn Env(Xn)}

eval-L

Page 8: Lexical Scoping and Closures

Example: Lexical Scope

({},(((x.(y.x+y)) 1) 2)) V

({},(x.(y.x+y)) 1)) ??

({},2) 2

(????) V

Page 9: Lexical Scoping and Closures

Example: Lexical Scope

({},(((x.(y.x+y)) 1) 2)) V

({},(x.(y.x+y)) 1)) ??

({},2) 2

(????) V

Page 10: Lexical Scoping and Closures

Example: Lexical Scope

({},(x.(y.x+y)) 1)) ??

({},(x.(y.x+y)) (x.(y.x+y))[{}]

({},1) 1

({}[x1],(y.x+y)) ??

Page 11: Lexical Scoping and Closures

Example: Lexical Scope

({},(x.(y.x+y)) 1)) (y.x+y)[{x1}]

({},(x.(y.x+y)) (x.(y.x+y))[{}]

({},1) 1

({x1},(y.x+y)) (y.x+y)[{x1}]

Page 12: Lexical Scoping and Closures

Example: Lexical Scope

({},(((x.(y.x+y)) 1) 2)) V

({},(x.(y.x+y)) 1)) ??

({},2) 2

(????) V

Page 13: Lexical Scoping and Closures

Example: Lexical Scope

({},(((x.(y.x+y)) 1) 2)) V

({},(x.(y.x+y)) 1)) (y.x+y)[{x2}]

({},2) 2

({x1}[y2],x+y) V

Page 14: Lexical Scoping and Closures

Example: Lexical Scope

({},(((x.(y.x+y)) 1) 2)) V

({},(x.(y.x+y)) 1)) (y.x+y)[{x2}]

({},2) 2

({x1,y2},x+y) V

Page 15: Lexical Scoping and Closures

Example: Lexical Scope

({},(((x.(y.x+y)) 1) 2)) 3

({},(x.(y.x+y)) 1)) (y.x+y)[{x2}]

({},2) 2

({x1,y2},3) 3

Page 16: Lexical Scoping and Closures

E-Machine

• E-machine is small-step semantics with environment semantics

• Stacks now must contain references to environment capture when the stack frame was push on the stack

Page 17: Lexical Scoping and Closures

Example: with Numbers

Names x 2 …

Numbers n ::= …Expresions e ::= n | +(e1,e2)

| (x.e) | (e1 e2) | x

Values v ::= x | n | (x.e)

Env’s Env ::= {} | {x1 V1,… , xn Vn}

MVal’s V ::= n | (x.e)[Env]

Page 18: Lexical Scoping and Closures

Example: with Numbers

MFrames F ::= +(¤,e2)[Env] | +(V1,¤)

| (¤ e2)[Env] | (V1 ¤)

MStacks K ::= ² | F B K

• Machine frames contain machine values

• Not all machine frames capture an environmentWhy?

Page 19: Lexical Scoping and Closures

E-Machine Rules

(K,Env,+(e1,e2)) E (+(¤,e2)[Env]BK,Env,e1)

(K,Env,(e1 e2)) E ((¤ e2)[Env]BK,Env,e1)

(K,Env,x) E (Env(x),K)

(K,Env,n) E (n,K)

(K,Env,(x.e)) E ((x.e)[Env],K)

(n2,+(n1,¤)BK) E (n1 + n2,K)

(V2,((x.e)[Env],¤)BK) E (K,Env[xV2],e)

(V1,+(¤,e2)[Env]BK) E (+(V1,¤)BK,Env,e2)

(V1,(¤ e2)[Env]BK) E ((V1 ¤)BK,Env,e2)

Page 20: Lexical Scoping and Closures

E-Machine Rules

(K,Env,+(e1,e2)) E (+(¤,e2)[Env]BK,Env,e1)

(K,Env,(e1 e2)) E ((¤ e2)[Env]BK,Env,e1)

(K,Env,x) E (Env(x),K)

(K,Env,n) E (n,K)

(K,Env,(x.e)) E ((x.e)[Env],K)

(n2,+(n1,¤)BK) E (n1 + n2,K)

(V2,((x.e)[Env],¤)BK) E (K,Env[xV2],e)

(V1,+(¤,e2)[Env]BK) E (+(V1,¤)BK,Env,e2)

(V1,(¤ e2)[Env]BK) E ((V1 ¤)BK,Env,e2)

Page 21: Lexical Scoping and Closures

E-Machine Rules

(K,Env,+(e1,e2)) E (+(¤,e2)[Env]BK,Env,e1)

(K,Env,(e1 e2)) E ((¤ e2)[Env]BK,Env,e1)

(K,Env,x) E (Env(x),K)

(K,Env,n) E (n,K)

(K,Env,(x.e)) E ((x.e)[Env],K)

(n2,+(n1,¤)BK) E (n1 + n2,K)

(V2,((x.e)[Env],¤)BK) E (K,Env[xV2],e)

(V1,+(¤,e2)[Env]BK) E (+(V1,¤)BK,Env,e2)

(V1,(¤ e2)[Env]BK) E ((V1 ¤)BK,Env,e2)

Page 22: Lexical Scoping and Closures

E-Machine Rules

(K,Env,+(e1,e2)) E (+(¤,e2)[Env]BK,Env,e1)

(K,Env,(e1 e2)) E ((¤ e2)[Env]BK,Env,e1)

(K,Env,x) E (Env(x),K)

(K,Env,n) E (n,K)

(K,Env,(x.e)) E ((x.e)[Env],K)

(n2,+(n1,¤)BK) E (n1 + n2,K)

(V2,((x.e)[Env],¤)BK) E (K,Env[xV2],e)

(V1,+(¤,e2)[Env]BK) E (+(V1,¤)BK,Env,e2)

(V1,(¤ e2)[Env]BK) E ((V1 ¤)BK,Env,e2)

Page 23: Lexical Scoping and Closures

E-Machine Rules

(K,Env,+(e1,e2)) E (+(¤,e2)[Env]BK,Env,e1)

(K,Env,(e1 e2)) E ((¤ e2)[Env]BK,Env,e1)

(K,Env,x) E (Env(x),K)

(K,Env,n) E (n,K)

(K,Env,(x.e)) E ((x.e)[Env],K)

(n2,+(n1,¤)BK) E (n1 + n2,K)

(V2,((x.e)[Env],¤)BK) E (K,Env[xV2],e)

(V1,+(¤,e2)[Env]BK) E (+(V1,¤)BK,Env,e2)

(V1,(¤ e2)[Env]BK) E ((V1 ¤)BK,Env,e2)

Page 24: Lexical Scoping and Closures

E-Machine Rules

(K,Env,+(e1,e2)) E (+(¤,e2)[Env]BK,Env,e1)

(K,Env,(e1 e2)) E ((¤ e2)[Env]BK,Env,e1)

(K,Env,x) E (Env(x),K)

(K,Env,n) E (n,K)

(K,Env,(x.e)) E ((x.e)[Env],K)

(n2,+(n1,¤)BK) E (n1 + n2,K)

(V2,((x.e)[Env],¤)BK) E (K,Env[xV2],e)

(V1,+(¤,e2)[Env]BK) E (+(V1,¤)BK,Env,e2)

(V1,(¤ e2)[Env]BK) E ((V1 ¤)BK,Env,e2)

Page 25: Lexical Scoping and Closures

Example: Evaluation

(²,{},(((x.(y.+(x,y))) 1) 2))

E ((¤ 2)[{}] B²,{},((x.(y.+(x,y))) 1))

E ((¤ 1)[{}] B(¤ 2)[{}] B²,{},(x.(y.+(x,y))))

E ((x.(y.+(x,y)))[{}],(¤ 1)[{}] B(¤ 2)[{}] B²)

E (((x.(y.+(x,y)))[{}] ¤)[{}] B(¤ 2)[{}] B²,{},1)

E (1,((x.(y.+(x,y)))[{}] ¤)[{}] B(¤ 2)[{}] B²)

E ((¤ 2)[{}] B²,{x1},(y.+(x,y)))

E ((y.+(x,y))[{x1}],(¤ 2)[{}] B²)

E (((y.+(x,y))[{x1}] ¤)[{}] B²,{},2)

E (2,((y.+(x,y))[{x1}] ¤)[{}] B²)

E (²,{x1,y2},+(x,y)) … E (3,²)