Slide 8.8: Reduction rules for integer arithmetic expressions (cont.) Slide 9.2: Environments and assignments Home |
a := 2 + 3; b := a * 4; a := b – 5results in the bindings
b=20
and a=15
when it finishes, and so the set of values representing the semantics of the program is {a=15, b=20}
.
Such a set is essentially a function from identifiers to integer values, with all identifiers that have not been assigned a value undefined.
Such a function is called an environment, which is a collection of bindings of values to variables.
An environment is written as
Env: Identifier → Integer ∪ {undef}
Env
.
For example, the Env
function given by the program example can be defined as follows:
Env(I) =
|
15 if I = a 20 if I = b undef otherwise |
I
in an environment Env
is then simply described by function evaluation Env(I)
.
The operation of adding a new value binding to Env
can also be defined in functional terms.
We will use the notation Env & {I = n}
to denote the adding of the new value n
for I
to Env
.
In terms of functions,
(Env & {I=n})(J) =
|
n if J = I Env(J) otherwise |
Env0
:
Env0(I) = undef for all I