% THIS IS the definition of allof relation from the % textbook. Store it in a file (say, allof). % To include 'allof' it in the main file F, use % :- include('allof'). :- dynamic previndex/1. previndex(0). newindex(I1) :- retract(previndex(I)), I1 is I+1, assert(previndex(I1)). % accumulate(Goal,Init,Prev,Acc,Next,Result) % accumulates information for each success of Goal. % Init is the start. Acc is a predicate that shows % how to derive Next accumulation from Prev % accumulation. Result is the final accumulation. accumulate(G,I,P,CN,N,Res) :- newindex(Ind), accumulate(Ind,G,I,P,CN,N,Res). accumulate(Ind,G,I,P,CN,N,_) :- assert(result(Ind,I)), call(G), retract(result(Ind,P)), call(CN), assert(result(Ind,N)), fail. accumulate(Ind,_,_,_,_,_,Res) :- retract(result(Ind,Res)). % allof(X,G,Res) is true if Res is the list of all X such that goal G is true allof(X,G,Res) :- accumulate(G,L-L,P1-[X|P],true,P1-P,Res-[]).