% $Id: Environment.oz,v 1.9 2009/01/10 18:12:30 leavens Exp $ % Environments (finite functions from atoms to locations) % AUTHOR: Gary T. Leavens % Representation: % ::= env( >) \insert 'CommaSeparate.oz' \insert 'UniqueNames.oz' \insert 'FiniteFun.oz' declare local un(uniquename:UniqueName extendname:ExtendName) = {UniqueNames &e} in fun {InitEnv} %% ENSURES: Result is an empty environment env({UniqueName} {EmptyFF}) end fun {NameOfEnv env(Name _)} %% ENSURES: Result is the name of the argument environment Name end fun {ApplyEnv env(_ FF) V} %% REQUIRES: V is in the domain of the first argument %% ENSURES: Result is the association of V in the argument envrionment {ApplyFF FF V} end fun {HasEnv env(_ FF) V} %% ENSURES: Result is true when the first argument has V in its domain {HasFF FF V} end fun {ExtendEnv env(Name FF) V X} %% ENSURES: Result extends the first argument mapping V to X env({ExtendName Name} {ExtendFF FF V X}) end fun {ExtendEnvList env(Name FF) Vs Xs} %% REQUIRES: {Length Vs} == {Length Xs} %% ENSURES: Result extends the first argument each V in Vs to %% the corresponding X in Xs env({ExtendName Name} {ExtendFFList FF Vs Xs}) end local EqualFF = {EqualFFMaker fun {$ L1 L2} L1 == L2 end} in fun {EqualEnv env(_ FF1) env(_ FF2)} %% ENSURES: Result is true when the two arguments are equal {EqualFF FF1 FF2} end end fun {NameLoc L} %% ENSURES: Result is a printable virtual string for location L 'x'#L end fun {StringForEnv env(_ FF)} %% ENSURES: Result is a virtual string representing the environment fun {Mappings Dom} case Dom of nil then nil [] V|Vs then V # '-->' # {NameLoc {ApplyFF FF V}} | {Mappings Vs} end end in '{' # {VSCommaSeparate {Reverse {Mappings {DomainFF FF}}}} # '}' end end