250 likes | 462 Views
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
E N D
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 closures An expression and a environment that that holds bindings for all the free variables of the expression
(Env,E1) lam(X’.E’)[Env’] (Env,E2) V’ (Env’[X’V’], E’) V eval-X eval-L (Env,lam(X.E)) lam(X.E)[Env] (Env,X) Env(X) eval-A (Env,apply(E1,E2)) V Lexical Scoping
(Env,E1) lam(X’.E’)[Env’] (Env,E2) V’ (Env’[X’ V’], E’) V eval-X eval-L (Env,lam(X.E)) lam(X.E)[Env] (Env,X) Env(X) eval-A (Env,apply(E1,E2)) V Lexical Scoping Capture current environment
(Env,E1) lam(X’.E’)[Env’] (Env,E2) V’ (Env’[X’ V’], E’) V eval-X eval-L (Env,lam(X.E)) lam(X.E)[Env] (Env,X) Env(X) eval-A (Env,apply(E1,E2)) V Lexical Scoping 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 X1,…, Xn2 FN(lam(X.E)) Envmin = {X1 Env(X1),…,Xn Env(Xn)} eval-L (Env,lam(X.E)) lam(X.E)[Envmin]
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: lwith Numbers • Machine frames contain machine values • Not all machine frames capture an environment Why?
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,²)