projog

3.48. setof(X,P,L) - find all solutions that satisfy the goal.

setof(X,P,L) produces a list (L) of X for each possible solution of the goal P. If P contains uninstantiated variables, other than X, it is possible that setof can be successfully evaluated multiple times - for each possible values of the uninstantiated variables. The elements in L will appear in sorted order and will not include duplicates. Fails if P has no solutions.

Examples

z(r).
z(t).
z(y).

x(a,b,c).
x(q,X,e) :- z(X).
x(1,2,3).
x(w,b,c).
x(d,b,c).
x(a,b,c).

?- setof(X,x(X,Y,Z),L).
L = [a,d,w]
X = UNINSTANTIATED VARIABLE
Y = b
Z = c

yes;
L = [q]
X = UNINSTANTIATED VARIABLE
Y = r
Z = e

yes;
L = [q]
X = UNINSTANTIATED VARIABLE
Y = t
Z = e

yes;
L = [q]
X = UNINSTANTIATED VARIABLE
Y = y
Z = e

yes;
L = [1]
X = UNINSTANTIATED VARIABLE
Y = 2
Z = 3

yes

?- setof(X,x(X,y,z),L).

no

?- setof(Y, (member(X,[6,3,7,2,5,4,3]), X<4, Y is X*X), L).
L = [9]
X = 3
Y = UNINSTANTIATED VARIABLE

yes;
L = [4]
X = 2
Y = UNINSTANTIATED VARIABLE

yes

p(a,1).
p(b,2).
p(c,3).
p(d,2).
p(d,2).

?- setof(X, p(X,Y), List).
List = [a]
X = UNINSTANTIATED VARIABLE
Y = 1

yes;
List = [b,d]
X = UNINSTANTIATED VARIABLE
Y = 2

yes;
List = [c]
X = UNINSTANTIATED VARIABLE
Y = 3

yes

TODO setof(X, Y ^ p(X,Y), List)