1 / 25

Lexical Scoping and Closures

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

Download Presentation

Lexical Scoping and Closures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lexical Scoping and Closures COS 441 Princeton University Fall 2004

  2. 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)

  3. Lexical Scoping Values are closures An expression and a environment that that holds bindings for all the free variables of the expression

  4. (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

  5. (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

  6. (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

  7. 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]

  8. Example: Lexical Scope ({},(((x.(y.x+y)) 1) 2))  V ({},(x.(y.x+y)) 1))  ?? ({},2)  2 (????)  V

  9. Example: Lexical Scope ({},(((x.(y.x+y)) 1) 2))  V ({},(x.(y.x+y)) 1))  ?? ({},2)  2 (????)  V

  10. Example: Lexical Scope ({},(x.(y.x+y)) 1))  ?? ({},(x.(y.x+y))  (x.(y.x+y))[{}] ({},1)  1 ({}[x1],(y.x+y))  ??

  11. Example: Lexical Scope ({},(x.(y.x+y)) 1))  (y.x+y)[{x1}] ({},(x.(y.x+y))  (x.(y.x+y))[{}] ({},1)  1 ({x1},(y.x+y))  (y.x+y)[{x1}]

  12. Example: Lexical Scope ({},(((x.(y.x+y)) 1) 2))  V ({},(x.(y.x+y)) 1))  ?? ({},2)  2 (????)  V

  13. Example: Lexical Scope ({},(((x.(y.x+y)) 1) 2))  V ({},(x.(y.x+y)) 1))  (y.x+y)[{x2}] ({},2)  2 ({x1}[y2],x+y)  V

  14. Example: Lexical Scope ({},(((x.(y.x+y)) 1) 2))  V ({},(x.(y.x+y)) 1))  (y.x+y)[{x2}] ({},2)  2 ({x1,y2},x+y)  V

  15. Example: Lexical Scope ({},(((x.(y.x+y)) 1) 2))  3 ({},(x.(y.x+y)) 1))  (y.x+y)[{x2}] ({},2)  2 ({x1,y2},3)  3

  16. 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

  17. Example: lwith Numbers

  18. Example: lwith Numbers • Machine frames contain machine values • Not all machine frames capture an environment Why?

  19. E-Machine Rules

  20. E-Machine Rules

  21. E-Machine Rules

  22. E-Machine Rules

  23. E-Machine Rules

  24. E-Machine Rules

  25. 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²,{x1},(y.+(x,y))) E((y.+(x,y))[{x1}],(¤ 2)[{}] B²) E(((y.+(x,y))[{x1}] ¤)[{}] B²,{},2) E (2,((y.+(x,y))[{x1}] ¤)[{}] B²) E (²,{x1,y2},+(x,y)) … E(3,²)

More Related