1 / 9

The role of environments in scoping

The role of environments in scoping. Nested scopes. Whenever scopes can be nested we have the “feature” that a variable may occur more than once in the same scope. Resolving that ambiguity is important f x y = let x = 9 in y + x What value does (f 3 7) return?.

binta
Télécharger la présentation

The role of environments in scoping

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. The role of environmentsin scoping

  2. Nested scopes • Whenever scopes can be nested we have the “feature” that a variable may occur more than once in the same scope. • Resolving that ambiguity is important f x y = let x = 9 in y + x What value does (f 3 7) return? Which x does this refer to

  3. Local functions • This can also happen with local functions y = 99 f w y = let g x = y + x in g w f 33 7 Which y does this refer to

  4. The role of the environment • In our definitional interpreters, the environment maps names to locations. • To determine which one of a number of possible binding sites a variable uses, we must study how the environment is changed.

  5. The fun-arg problem • This problem is called the fun-arg problem y = 99 f w y = let g x = y + x in g w f 33 7 It’s resolution depends upon how the body of functions (like g) are evaluated

  6. Similar problem We don’t need local functions to have this problem. (global y 99) (fun g (x) (+ y x)) (fun f (w) (local (y 3) (@ g w))) (@ f 33) Which y does this refer to

  7. At definition site Local scope Function body Formal args elab :: Def -> (Env(EnvAddr, [Vname], Exp) , EnvAddr, State) -> IO (Env (EnvAddr, [Vname], Exp), EnvAddr, State) elab (FunDef f vs e) (funs,vars,state) = return ( extend f (vars,vs,e) funs, vars, state ) elab (GlobalDef v e) (funs,vars,state) = do { (value,state2) <- interpE funs vars state e ; let (addr,state3) = alloc value state2 ; return(funs, extend v addr vars,state3)}

  8. At call site run state (term@(At f args)) = case lookUp funs f of NotFound -> error … Found (vars2,formals,body) -> do { when (length args /= length formals) (error …) ; (vs,state2) <- interpList funs vars state args ; let (pairs,state3) = bind formals vs state2 ; (v,state4) <- interpE funs (push pairs vars2) state3 body ; return(v,state4) }

  9. A closure • We call a function object that binds its free variables in the scope of definition (rather than use) a closure. • Closures are key components in static scoping. • It is interesting that in functional languages, where functions may return functions, variables may now outlive their scope. • Who can give an example? • What does this imply about implementations?

More Related