I have an environment list which keeps associations between variables and values, such as env = [("x", 1), ("y", 2), ("z", 3), ...]
. I also have another substitution list (which is returned by a pattern matching control function) that keeps associations between variables and pattern, such as s = [("v", Var_p "z"), ("w", Var_p "v"), ("u", Var_p "w"), ...]
. Let's consider the first pair ("v", Var_p "z")
: my function should check the value corresponding to z
in the environment, create a new association between v
and z
's value (i. e. ("v", 3)
) and put it into the environment. My code for this function is the following.
fun augment_env ([], e : env) = e
| augment_env (s :: srest, e : env) =
let
val (a, b) = s
in
case b of
Const_p i => [] @ augment_env_with_vars (srest, e)
| Var_p x =>
if (exists (e, x)) then (a, lookup (e, x)) :: augment_env_with_vars (srest, e)
end;
Here, s
is the substitution list and exists
and lookup
are functions which check the existence of a variable in the environment and look for the corresponding value, respectively. The problem with this function is that works fine only for direct associations (in the example, it puts the direct association between v
and 3
into the environment). It obviously doesn't work for the transitive associations unless I call it multiple times (which I don't know in advance how many they are). To recall the example, by the end of this function, my environment list should be env = [("x", 1), ("y", 2), ("z", 3), ("v", 3), ("w", 3), ("u", 3), ...]
.
Could you please give me a hint about how to modify this function to work fine with the transitive associations, too?
Thanks in advance!