CS 334
|
Variable has 6 components
Scope and Lifetime same in some languages - clearly different in some (FORTRAN)
N := N + 1First N refers to location (l-value), second to value (r-value).
Obtaining value of variable called dereferencing. (going from reference or location to value)
E.g., name can change (via call by reference parameter)
Aliasing:
Similarly assignment of variables (e.g., x := y) can be by copying or by sharing.
In copy semantics, target variable retains its location, but copies new value from source variable.
In sharing semantics, target variable gets location of source variable, so both share the same location (like objects in Java).
Constants have values, but no location.
One way of classifying a language is according to sorts of entities that can be bound to an identifier. Called denotable values.
In Pascal, objects which can be bound to id's and corresponding declarations are:
Static vs Dynamic
Scope is associated with the static text of program.
Can determine scope by looking at structure of program rather than execution path telling how got there.
May have holes in scope of variable
program ... var M : integer; .... procedure A ... var M : array [1..10] of real; begin ... end; begin ... end.Variable M declared in main program is not visible in procedure A, since new declaration of M "shades" old declaration.
Symbol table keeps track of which declarations are currently visible.
Think of symbol table as stack. When enter a new scope, new declarations are pushed on and can shade old ones. When exit scope, declarations arising from the scope are popped off.
In particular an occurrence of an identifier in a procedure may be associated with different variables at different times in the execution of the program.
Example:
program ... var A : integer; procedure Y(B: integer); begin ...; B := A + B; ... end; {Y} procedure Z(...); var A: integer; begin ...; Y(...); ... end; {Z} begin {main} ...; Z(...); ... end.Question: Which variable with name A is used when Y is called from Z?
Push and pop entries when enter and exit scopes at run-time.
For obvious reasons, dynamic scoping usually implies dynamic typing!
LISP and APL use dynamic scoping (though SCHEME has default of static)
In Pascal or C, when enter procedure any local variables are allocated and are then deallocated when exit. (Called dynamic allocation of variables).
In block-structured language (e.g., Pascal, C, Modula-2, etc.):
Use run-time stack to allocate space for local variables and parameters when enter a new unit (procedure, functions, etc.). Space is called an activation record.
Pop it off run-time stack when exit unit.
Note that a procedure may have several activation records on stack if called recursively.
Even without recursion may have several distinct variables on stack with same name!
When pointers are used, utilize another kind of memory, called "heap".
When do "new(p)" operation where p is of type pointer to T, sufficient memory is allocated from the heap to hold a value of type p and p is assigned the location of that memory. The value is accessed by writing p^ (in Pascal).
This memory does not follow the stack discipline. The lifetime of the heap-allocated memory is determined manually by "new" and "dispose" commands. Entering or exiting a scope has no impact on the allocation or deallocation of this memory.
Therefore in Pascal and C (for example) there are three kinds of memory: static (occupied by global variables), stack-based or automatic (occupied by parameters and local variables of procedures and functions), and heap-based or manually allocated.
In ML, everything comes off of heap. But automatically allocated when needed and deallocated (by garbage collector) when no way of accessing it. Java is similar.
More implementation details later.
If freeze at compilation, then program constant
const size = 100; doubleSize = 2 * size {called manifest constant}In other languages allow:
procedure ... (n : integer) is var x: constant integer := 3 * n - 2; {value bound & frozen on procedure entry} A: array[1..n] of real;Some language allow variables to be initialized at declaration.
var x : integer := 5;But when is binding done - only first time procedure is entered or every time ? FORTRAN only first time, Java every time.
Postpone discussion of binding time for types until later.
This can occur especially easily when using var parameters.
Ex: If p(x,y) is a procedure where x and y are both var parameters, then the call p(z,z) makes x and y aliases in the body of p.
If the body of p(x,y) first increases x by one and then y by one, what is the result of the call to p(z,z)? Get z increased by 2! Aliasing often producing surprising (undesirable) behavior in functions.
Also arises when global variable, x, is used inside procedure where x was used as an actual parameter for a var parameter.
Also easy to get aliasing with pointers:
var x, y: ^ int; ... x := y;Then x^ and y^ are aliases - any change to one, changes the other!
In languages with assignment by sharing (e.g., Java), get aliasing automatically with all assignments.
"Pointers have been lumped with the goto statement as a marvelous way to create impossible to understand programs."
Kernighan & Ritchie, The C Programming Language
(In fairness, they then go on to defend the use of pointers.)
S x T = {<s,t> | s in S , t in T}.Can also write as PRODi in I Si = S1 x S2 x ... x Sn. If all are the same, write Sn.
Tuples of ML: type point = int * int
How many elts in product?
What if have So? Called unit in ML.
Differ from Cartesian product since fields associated with labels
E.g.
record record x : integer; /= a : integer; y : real b : real end; end
Operations and relations: selection ".", :=, =.
Can use generalized product notation: PRODl in Lab T(l)
Ex. in first example above, Lab = {x,y}, T(x) = integer, T(y) = real.