View
37
Download
0
Category
Tags:
Preview:
DESCRIPTION
Lexical Scoping and Closures. COS 441 Princeton University Fall 2004. 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 - PowerPoint PPT Presentation
Citation preview
Lexical Scoping and Closures
COS 441
Princeton University
Fall 2004
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)
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)
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
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
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
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
Example: Lexical Scope
({},(((x.(y.x+y)) 1) 2)) V
({},(x.(y.x+y)) 1)) ??
({},2) 2
(????) V
Example: Lexical Scope
({},(((x.(y.x+y)) 1) 2)) V
({},(x.(y.x+y)) 1)) ??
({},2) 2
(????) V
Example: Lexical Scope
({},(x.(y.x+y)) 1)) ??
({},(x.(y.x+y)) (x.(y.x+y))[{}]
({},1) 1
({}[x1],(y.x+y)) ??
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}]
Example: Lexical Scope
({},(((x.(y.x+y)) 1) 2)) V
({},(x.(y.x+y)) 1)) ??
({},2) 2
(????) V
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
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
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
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
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]
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?
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)
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)
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)
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)
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)
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)
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,²)
Recommended