`bagof(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 `bagof`

can be successfully evaluated multiple times - for each possible values of the uninstantiated variables. The elements in `L`

will appear in the order they were found and may 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).

` ?- bagof(X,x(X,Y,Z),L).`

L = [a,w,d,a]

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

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

no

?- bagof(Y, (member(X,[6,3,7,2,5,4,3]), X<4, Y is X*X), L).

L = [9,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).

` ?- bagof(X, p(X,Y), List).`

List = [a]

X = UNINSTANTIATED VARIABLE

Y = 1

yes;

List = [b,d,d]

X = UNINSTANTIATED VARIABLE

Y = 2

yes;

List = [c]

X = UNINSTANTIATED VARIABLE

Y = 3

yes

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