Download presentation
Presentation is loading. Please wait.
1
Catriel Beeri Pls/Winter 2004/5 environment1 1 The Environment Model Introduction and overview A look at the execution model Dynamic scoping Static scoping Some details of implementation Liveness, storage allocation and language restrictions
2
Catriel Beeri Pls/Winter 2004/5 environment1 2 Associations of variables with values are created by: definition elaboration/function application How are they handled? Substitution model: –Substitute value for variable uses in scope ( program is modified) Environment model: –Create dynamic binding, store in environment ( no change of program) –Look up uses in environment when needed Introduction and overview
3
Catriel Beeri Pls/Winter 2004/5 environment1 3 Bindings and environments : A (dynamic) value binding = a (run-time) association of a variable with a value A value: any run-time entity that can be bound to variables and manipulated by programmers: Values of base types Variables Function values Composite date (lists, tuples,..) …. We say simply binding
4
Catriel Beeri Pls/Winter 2004/5 environment1 4 A (value) environment (two equivalent views): A finite mapping from variables to value A set of bindings (at most one per variable) Terminology & Operations: Lookup (apply map) : E(x) dom(E) : the domain of E Create “small” environment (when bindings are generated) Merge with priority :
5
Catriel Beeri Pls/Winter 2004/5 environment1 5 A new environment is generated when new bindings for some variables are created When execution leaves the scope of these variables the previous environment is restored The environment used at a point: the current environment Program execution consists of code segments with a fixed current environment, separated by environment change events
6
Catriel Beeri Pls/Winter 2004/5 environment1 6 Evaluation with fixed environment : Many program phrases are evaluated w/o changing the environment: Atomic or composite values References Operation application If, while, … A lambda expression The only one that involves the environment is the evaluation of a variable – variable resolution
7
Catriel Beeri Pls/Winter 2004/5 environment1 7 Evaluation with fixed environment –rules : And similarly for if, cell allocation, assignment, …. Variable resolution: determine a value for a a variable use by a look-up –rule (env-nat-var)
8
Catriel Beeri Pls/Winter 2004/5 environment1 8 The rule for lambda depends on how functions are applied; for now: Function application, let, letrec – their evaluation involves environment changes (at entrance/exit) let* -- sequential non-recursive block = nested lets letrec* -- sequential recursive block = nested letrecs We discuss let and letrec (but examples use let*)
9
Catriel Beeri Pls/Winter 2004/5 environment1 9 Environment changing constructs : These involve Evaluation of the expressions E i to v i ECreation of new environment E that contains the bindings (and more) EEvaluation of B w.r.t. E Upon exit, restoration of previous environment Entrance to new environment, exit --- matching pairs in a computation
10
Catriel Beeri Pls/Winter 2004/5 environment1 10 Details: For function application & let, the arguments/defining expressions are evaluated in the current environment (before new environment is formed) But, for a letrec, the defining expressions are in the scope of the defined variables, must be evaluated in the new environment – a cycle Q1: how are the defining expressions evaluated, and new environment created, for a letrec? (temp) A: define only functions, the current environment is then irrelevant
11
Catriel Beeri Pls/Winter 2004/5 environment1 11 E After bindings are generated, in current environment E, need to create new environment, with new bindings and also with previous bindings For let/letrec : (evaluation in place) But, a function is applied in position unrelated to its definition; should the base environment be from the definition, or from the use Q2: which environment is the basis for the the new environment for a function application?
12
Catriel Beeri Pls/Winter 2004/5 environment1 12
13
Catriel Beeri Pls/Winter 2004/5 environment1 13 A look at the execution model I. Activations and the activation stack Activation: computation segment between matching environment change events ~ evaluation of the body of function/let/letrec (term typically used for functions, we generalize to all regions) Entrance to activation ~ binding generation and new environment creation Exit from activation ~ restoration of previous environment
14
Catriel Beeri Pls/Winter 2004/5 environment1 14 Each activation: Is associated with an environment (created for it!) Executes some segments of code Additional code may be executed in nested activations, with their own environments! Activations entrance/exit -- LIFO Activation stack (a conceptual structure ) The dynamic parent of an activation – the one below it on the stack
15
Catriel Beeri Pls/Winter 2004/5 environment1 15 An activation is : live --- is on the stack (after it has terminated, it will never be on the stack again) current – top of the stack – executing every other live activation is suspended An activation stops being live ~ its environment is exited Span of activation – from entrance to exit (when it dies) The current environment = the one associated with the current activation a one-to-one correspondence
16
Catriel Beeri Pls/Winter 2004/5 environment1 16 In an activation, some expression/statements (s) are evaluated These may contain blocks/function calls evaluation includes Direct evaluation (in activation itself) : – Evaluation of literals, variable resolution, arithmetic ops, if, assignment, loops, sequencing, … –Returning a value to calling activation Code evaluated in nested activations Remember to distinguish evaluation from direct evaluation!
17
Catriel Beeri Pls/Winter 2004/5 environment1 17 In the next example, we assume that the base environment for a function application is the one current at the place of application – rule (env-nat-applic-fun-t)
18
Catriel Beeri Pls/Winter 2004/5 environment1 18 Example: (start with empty environment) Activation directly evaluated environment of evaluation The value 7 is returned, one activation at a time, to act 0
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.