An *environment* `E`

associates a variable with a value, i.e. `Environment=Var→Val`

.
A common way to write an environment is as a set, e.g. if

1≤i≤n, x_{i}∈Var∧v_{i}∈Val

then the following is an environment:

{(x_{1},v_{1}),(x_{2},v_{2}),...,(x_{n},v_{n})}.

Environments often need to be combined.
If `E`

and _{1}`E`

are environments, and _{2}`x`

is a variable,
then the combination of the environments,
`E`

, is defined as follows
_{1}E_{2}

`E`

if defined, otherwise_{1}E_{2}(x) = E_{2}(x)`E`

if defined, otherwise undefined._{1}(x)

Here we see that `E`

overrides _{2}`E`

.
_{1}

The following is a simple implementation of environments written in Barry's Prolog but it should work on most other Prolog systems.

%%% %%% env_empty(?E) %%% %%% Succeeds if E is the empty environment. %%% env_empty([]). %%% %%% env_singleton(?Var, ?Val, +E) %%% env_singleton(+Var, +Val, ?E) %%% %%% Succeeds if E is an environment with a single binding of Var-Val. %%% env_singleton(Var, Val, [Var-Val]). %%% %%% env_compose(+E1, +E2, -E) %%% %%% Succeeds and unifies E to the composition of E1 and E2. %%% The bindings of E2 override the bindings of E1 in E. %%% env_compose(E1, E2, E) :- append(E2, E1, E). %%% %%% env_lookup(+Var, +Env, -Val) %%% %%% Succeeds and unifies Val to the value associated with Var if Var-Val is a binding %%% in Env. Fails otherwise. %%% env_lookup(Var, Env, Val) :- member(Var-Val, Env).

R. Burstall. *Language Semantics and Implementation. Course Notes.* 1994.

Copyright © 2014 Barry Watson. All rights reserved.