All solutions

These predicates create a list of all the solutions of a goal.

1. findall/3

findall(Template, Goal, Instances) is true iff Instances unifies with the list of values to which a variable X not occurring in Template or Goal would be instantiated by successive re-executions of call(Goal), X=Template. after systematic replacement of all variables in X by new variables.

Templates and modes for the predicate are as follows:

findall(?term, +callable_term, ?list)

1.1 Example tests

Let's start with some simple tests verifying success of failure of single goals.

GoalTheorysuccess(String goal,String theory)
findall(X, (X=1;Y=2), S).nulltrue
findall(X+Y, (X=1), S).nulltrue
findall(X, fail, L).nulltrue
findall(X, (X=1;X=1), S).nulltrue
findall(X, (X=1;X=2), [X,Y]).nulltrue

GoalTheorysuccess(String goal,String theory)
findall(X, (X=2; X=1), [1, 2]).nullfalse

Tests With Results

GoalTheoryVariablesuccess(String goal,String theory,String variable)
findall(X, (X=1;Y=2), S).nullS[1,_]
findall(X+Y, (X=1), S).nullS['+'(1,_)]
findall(X, fail, L).nullL[]
findall(X, (X=1;X=1), S).nullS[1,1]
findall(X, (X=1;X=2), [X,Y]).nullX1
findall(X, (X=1;X=2), [X,Y]).nullY2
findall(X, (X=1;X=2), S).nullS[1,2]

Test With Exception

GoalTheorysuccess(String goal)Type Of Error
findall(X,Goal,S).nulltrue
findall(X,4,S).nulltrue

2. bagof/3

bagof/3 assembles as a list the solutions of a goal for each different instantiation of the free variables in that goal. The elements of each list are in order of solution, but the order in which each list is found is undefined.

Note that bagof/3 is re-executable.

Templates and modes for the predicate are as follows:

bagof(?term, +callable_term, ?list)

2.1 Example tests

Let's start with some simple tests verifying success of failure of single goals.

GoalTheorysuccess(String goal,String theory)
bagof(X,(X=1;X=2), S).nulltrue
bagof(X,(X=1;X=2), X).nulltrue
bagof(X,(X=Y;X=Z), S1).nulltrue
bagof(1,(Y=1;Y=2), L).nulltrue
bagof(f(X,Y), (X=a;Y=b), L1).nulltrue
bagof(X, Y^((X=1,Y=1);(X=2;Y=2)), L2).nulltrue
bagof(X, Y^((X=1;Y=1);(X=2;Y=2)), L3).nulltrue
bagof(X, Y^((X=1;Y=2);X=3), Si1).nulltrue
bagof(X, (X=Y;X=Z;Y=1), S3).nulltrue

GoalTheorysuccess(String goal,String theory)
bagof(X,fail,S2).nullfalse

Tests With Results

GoalTheoryVariablesuccess(String goal,String theory,String variable)
bagof(X,(X=1;X=2), S).nullS[1,2]
bagof(X,(X=1;X=2), X).nullX[1,2]
bagof(X,(X=Y;X=Z), S1).nullS1[Y_,Z_]
bagof(1,(Y=1;Y=2), L).nullL[1]
bagof(f(X,Y), (X=a;Y=b), L1).nullL1[f(a,_),f(_,b)]
bagof(X, Y^((X=1,Y=1);(X=2;Y=2)), L2).nullL2[1,2,_]
bagof(X, Y^((X=1;Y=1);(X=2;Y=2)), L3).nullL3[1,_,2,_]
bagof(X, Y^((X=1;Y=2);X=3), Si1).nullSi1[1,_,3]
bagof(X, (X=Y;X=Z;Y=1), S3).nullS3[Y_,Z_]

Tests With Exception

GoalTheorysuccess(String goal)Type Of Error
bagof(X,Y^Z,L).nulltrue
bagof(X,1,L).nulltrue
bagof(X,4,S).nulltrue

3. setof/3

setof/3 assembles as a list the solutions of a goal for each different instantiation of the free variables in that goal. Each list is a sorted list, but the order in which each list is found is undefined.

Note that bagof/3 is re-executable.

Templates and modes for the predicate are as follows:

setof(?term, +callable_term, ?list)

3.1 Example tests

Let's start with some simple tests verifying success of failure of single goals.

GoalTheorysuccess(String goal,String theory)
setof(X,(X=1;X=2),S).nulltrue
setof(X,(X=1;X=2),X).nulltrue
setof(X,(X=2;X=1),S).nulltrue
setof(X,(X=2;X=2),S).nulltrue
setof(X,(X=Y;X=Z),S).nulltrue
setof(1,(Y=2;Y=1),L).nulltrue
setof(f(X,Y),(X=a;Y=b),L).nulltrue
setof(X,Y^((X=1,Y=1);(X=2,Y=2)),S).nulltrue
setof(X,Y^((X=1;Y=1);(X=2,Y=2)),S).nulltrue
setof(X,Y^((X=1,Y=1);X=3),S).nulltrue
setof(X,(X=Y;X=Z;Y=1),S).nulltrue
setof(X,a(X,Y),L).a(1,f(_)). a(2,f(_)).true
setof(X,member(X,[f(U,b),f(V,c)]),L).nulltrue
setof(X,member(X,[f(b,U),f(c,V)]),[f(b,a),f(c,a)]).nulltrue
setof(X,member(X,[V,U,f(U),f(V)]),L).nulltrue
setof(X,member(X,[V,U,f(U),f(V)]),[a,b,f(a),f(b)]).nulltrue

GoalTheorysuccess(String goal,String theory)
setof(X,fail,S).nullfalse
setof(X,member(X,[V,U,f(U),f(V)]),[a,b,f(b),f(a)]).nullfalse

Test With Results

GoalTheoryVariablesuccess(String goal,String theory,String variable)
setof(X,(X=1;X=2),S).nullS[1,2]
setof(X,(X=1;X=2),X).nullX[1,2]
setof(X,(X=2;X=1),S).nullS[1,2]
setof(X,(X=2;X=2),S).nullS[2]
setof(X,(X=Y;X=Z),S).nullS[Y_,Z_]
setof(1,(Y=2;Y=1),L).nullL[1]
setof(f(X,Y),(X=a;Y=b),L).nullL[f(_,b),f(a,_)]
setof(X,Y^((X=1,Y=1);(X=2,Y=2)),S).nullS[1,2]
setof(X,Y^((X=1;Y=1);(X=2,Y=2)),S).nullS[_,1,2]
setof(X,Y^((X=1,Y=1);X=3),S).nullS[1,3]
setof(X,(X=Y;X=Z;Y=1),S).nullS[Y_,Z_]
setof(X,a(X,Y),L).a(1,f(_)). a(2,f(_)).L[1,2]
setof(X,member(X,[f(U,b),f(V,c)]),L).nullL[f(U_,b),f(V_,c)]
setof(X,member(X,[V,U,f(U),f(V)]),L).nullL[V_,U_,f(V_),f(U_)]
setof(X,member(X,[V,U,f(U),f(V)]),[a,b,f(a),f(b)]). nullVa