CS 541 Lecture -*- Outline -*- ** Scope Rules advert: another way to categorize languages scope basic to languages with names *** basic defintions **** binding constructs introduce defintion (decl) for name or set of names in FL? (proc, rec, bind) **** scope of declaration is area of program text where declaration is in effect **** static (lexical): can determine scopes before running program **** dynamic: in general, scope (at each instant) determined at run-time FL has static scope, look at bind construct's denotation and denotation of call (where is parameter evaluated) Would it be a good idea to give bind different scope from call? how to ensure same? (syntactic sugar) Dynamic scope: (call by denotation) allows one to specify values that are not explicit parameters ***** funarg problem (bind one 1 (bind inc (proc y (+ one y)) (bind one true (call inc 2)))) *** Non-hierarchical scope **** Hierarchical scope: strict (static) nesting of scopes ***** problem: information hiding, e.g., ADTs for stacks ***** problem: managing the namespace (too many globals) have to be careful not to hide defs no structure in namespace **** Solution: values with named subparts: modules or records Abs syntax: F ::= (I E) E ::= (record F* ) | (select E I) % e.i Semantic domains v in Value = ... + Record r in Record = Envirionment Aux-funs extend-record: Record -> Identifier -> Denotable-value -> Record record-lookup: Record -> Identifier -> (Denotable-value + Unbound) null-record: Record Valuation functions valF: Field-spec* -> Environment -> (Record + Error)_{\bot} valF[[ ]] = \u . (inRecord null-record) valF[[(I E) F*]] = \u . cases valF[[F*]] of isRecord(r) -> inRecord(extend-record r I (valE[[E]]u))) etc. Questions: does the above allow information hiding? yes, e.g. (bind x E (record ...)) the value of E can be hidden! **** Imports (with E1 E2) evaluate E2 in environment extended by bindings in E1 *** Multiple namespaces may want different environments for different kinds of names: e.g., in CommonLisp: exit points, dynamic variables, lexically scoped variables, procedures, tagbodies useful if have multiple scoping mechanisms (dynamic and static) or if want to model certain kinds of values as second-class (e.g., labels)