A very useful data structure that can be implemented with the blackboard is a stack that survives failure but still allows the programmer to use some of the nice properties of logical variables.
The main operations are push/3
that saves a term to the blackboard
and pushes it to a named stack, pop/3
that removes the top element from a named stack and unifies it with its third argument and stack/3
that
simply gives access to the list on the blackboard representing the stack.
The only operation that uses copying is push/3
, although if the term that is pushed to the stack has already some subterms on the blackboard, such subterms will not be copied again.
The implementation is straightforward:
push(Type,S,X):- default_val(Type,S,Xs,[]), saved([X|Xs],T), set(Type,S,T). pop(Type,S,X):- default_val(Type,S,V,[]), V=[X|Xs], set(Type,S,Xs). stack(Type,S,Xs):-val(Type,S,Xs). default_val(X,Y,V,_):-val(X,Y,V),!. default_val(X,Y,D,D):-def(X,Y,D).