CSE 130 : Fall 2008 Programmg Languages Lecture 4: Variables, Environments, Scope News PA 1 due Frida 5pm PA 2 out esterda (due net Fi) Fri) Ranjit Jhala UC San Diego Variables and Bdgs Q: How to use variables L? Q: How to assign to a variable? # = 2+2 val : t = 4 = e Bd the value of epression e to the variable Variables and Bdgs # = 2+2 val : t = 4 # = * * val : t = 64 # z = [;;+] val z : t list = [4;64;68] Later declared epressions can use ost recent bound value used for evaluation Sounds like C/Java? NO!
Environments ( Phone Book ) How L deals with variables Variables = names Values = phone number Environments and Evaluation L begs a top-level environment Some names bound = e z 4 : t 64 : t [4;64;68] : t list 8 : t L program = Sequence of variable bdgs Program evaluated b evaluatg bdgs order 1. Evaluate epr e current env to get value v : t 2. Etend env to bd to v : t (Repeat with net bdg) Environments Phone book Variables = names Values = phone number 1. Evaluate: Fd and use most recent value of variable 2. Etend: Add new bdg at end of phone book Eample # = 2+2 val : t = 4 # = * * val : t = 64 4 : t 4 : t 64 : t # z = [;;+] val z : t list = [4;64;68] 4 : t # = + val : t = 8 New bdg! z z 64 : t [4;64;68] : t list 4 : t 64 : t [4;64;68] : t list 8 : t
Environments 1. Evaluate: Use most recent bound value of var 2. Etend: Add new bdg at end How is this different from C/Java s store? Environments 1. Evaluate: Use most recent bound value of var 2. Etend: Add new bdg at end How is this different from C/Java s store? # = 2+2 val : t = 4 4 : t # f = fun -> + ; val f : t -> t = fn 4 : t f fn <code, >: t->t # = + ; val : t = 8 # f 0; val it : t = 4 New bdg: No change or mutation Old bdg frozen f # = 2+2; val : t = 4 4 : t # f = fun -> + ; val f : t -> t = fn 4 : t f fn <code, >: t->t # = + ; val : t = 8; 4 : t # f 0; f fn <code, >: t->t val it : t = 4 8 : t Environments 1. Evaluate: Use most recent bound value of var 2. Etend: Add new bdg at end How is this different from C/Java s store? # = 2+2; val : t = 4 # f = fun -> + val f : t -> t = fn Bdg used to eval (f ) # = + ; val : t = 8 4 : t f fn <code, >: t->t # f 0; 8 : t val it : t = 4 Bdg for subsequent Cannot change the world Cannot assign to variables Can etend the env b addg a fresh bdg Does not affect previous uses of variable Environment at fun declaration frozen side fun value Frozen env used to evaluate application (f ) Q: Wh is this a good thg? # = 2+2 Bdg used to eval (f ) val : t = 4 # f = fun -> + val f : t -> t = fn # = + val : t = 8; # f 0 val it : t = 4 4 : t f fn <code,, >: t->t 8 : t Bdg for subsequent
Cannot change the world Q: Wh is this a good thg? A: Function behavior frozen at declaration Nothg entered afterwards changes function Same puts alwas produce same outputs So? Localizes debuggg Localizes testg/reasong g about program No sharg means no evil aliasg effects Eamples of no sharg Remember: No addresses, no sharg. Each variable is bound to a fresh stance of a value Tuples, Lists Efficient implementation without sharg? There is sharg and poters but hidden from ou Compiler s job is to optimize code to efficientl implement these no-sharg sharg semantics Your job is to use the simplified no-sharg semantics to write correct, cleaner, readable, etendable sstems Function bdgs Functions are values, can bd usg fname = fun -> e Problem: Can t defe recursive functions! fname is bound after computg rhs value no (or old ) bdg for occurences of fname side id e rec fname = e Occurences of fname side e bound to this defition rec fac = if <=1 then 1 else *fac (-1) Local bdgs: - So far: bdgs that rema until a re-bdg ( global ) Local, temporar variables are useful side functions Avoid repeatg computations ake functions more readable = e1 e2 Let- is an epression! Evaluatg - env E: 1. Evaluate epr e1 env E to get value v : t 2. Use etended E [ a v : t] (onl) to evaluate e2
Local bdgs Evaluatg - env E: 1. Evaluate epr e1 env E to get value v : t 2. Use etended E [ a v : t] to evaluate e2 Let- is an epression! Evaluatg - env E: 1. Evaluate epr e1 env E to get value v : t 2. Use etended E [ a v : t] to evaluate e2 = 10 * 10 : t = = 10 * 10 : t 100 : t Nested bdgs Evaluatg - env E: 1. Evaluate epr e1 env E to get value v : t 2. Use etended E [ a v : t] to evaluate e2 = 10 10 : t ( = 20 * ) 10 : t + 20 : t 10 : t Nested bdgs = 10 = 20 * = 10 = 20 * Correct Formattg
Eample Nested function bdgs rec filter f s = if s = [] then [] else h = hd s t = filter f (tl s) if (f h) then h::t else t a = 20 f = = 10 g z = + z a + (g ) Env frozen with function Used to evaluate fun appl. Vl Values appl. are frozen env at defition f 0; Recap Recap Variables are names for values Environment: dictionar/phonebook ost recent bdg used Entries never changed, new entries added Build comple epressions with local bdgs - epression The -bdg is visible ( scope) side -epression Elsewhere the bdg is not visible Environment frozen at fun defition Re-bdg variables cannot change a function Same I/O behavior at ever call
Static/Leical Scopg Net: Functions For each occurrence of a variable, there is a unique place program tet where the variable was defed ost recent bdg environment Static/Leical: Determed from the program tet Without eecutg the program Epressions Tpes Values Ver useful for readabilit, debuggg: Don t have to figure out where a variable got assigned Unique, staticall known defition for each occurrence Q: What s the value of a function?