% --------------------------------------------------------------------------
%   FILE:       ENVIRON.PL
%   AUTHOR:     Tim Duncan
%   UPDATED:    Sun Aug 23 19:41:03 BST 1987
%   PURPOSE:    Utilities for environment used by Prosenet.
% --------------------------------------------------------------------------


:- op(100,fx,*).
:- op(100,xfy,:).


/* Display the current environment for debugging purposes */

env :- recorded(env,(K,L),_), write(K),write(' ::: '), write(L),nl,fail.
env.


/* Initialise the environment to empty */  

initenv :- recorded(env,_,R), erase(R), fail.
initenv.              



/* pushenv : save a copy of the environment */

pushenv :-
    ( fast_setof(K:V,recorded(env,(K,V),_),Env) ; Env = [] ),
    initenv,
    setenv(pushed,Env), !.




/* popenv : restore a pushed environment if one exists */

popenv :-
    recorded(env,(pushed,Env),_),
    initenv,
    popenv(Env), !.
popenv.

popenv([]).
popenv([K:V|Rest]) :-
    setenv(K,V), !,
    popenv(Rest).



/* setenv : make this the only value under key (assumes Val is list) */

setenv(Key,Val) :-
    do(delenv(Key,_)),
    recorda(env,(Key,Val),_), !.



/* delenv : remove all values for key */

delenv(Key,Val) :-
    recorded(env,(Key,Val),R), erase(R), fail.
delenv(_,_).



/* getenv : get a value (or nil), others on backtracking */

getenv(Key,Return) :-
    recorded(env,(Key,Return),_).
getenv(Key,[]) :-
    not(recorded(env,(Key,_),_)).
