%% Print a search path showPath(Path) :- Path=[First|_], last(Path,Last), nl, write('A solution from '), showState(First), write(' to '), showState(Last), nl, foreach1(member(S,Path),(write(' '),showState(S), nl)). % call Action for each way to prove P. foreach1(P,Action) :- P,once(Action),fail. foreach1(_,_). %% once/1 is pre-defined in SWI-Prolog, but not in Sicstus Prolog. %% once(P) execute's P just once. %% once(P) :- call(P), !. %% showState(S) will call writeState(S) if it has been defined, otherwise %% it will just write out S. showState(S) :- isDefined(writeState(_)) -> writeState(S); write(S). %% isDefined(P) is true if P has been defined as a predicate. isDefined(P) :- clause(P,_).