The following predicates do a grouping of the solutions and may succeed more than once. This grouping is based on the determination of the witnesses of a solution. The witness are the global variables of T^G where T is the template and G is the goal.

Examples:p(a,y). p(a,x). p(b,x).

?- bagof(X,p(X,Y),L).

Y = y, L = [a] ;

Y = x, L = [a, b]

?- bagof(X,Y^p(X,Y),L).

L = [a, a, b]

The predicate bagof/3 will do a sorting of the witnesses but not
of the resulting lists. The var-iation setof/3 will sort the
witnesses and the resulting lists. Finally the variation
sys_heapof/3 will neither sort the witnesses nor the resulting
lists.

p(a). p(b).

q(a). q(b). q(c).

?- forall(p(X), q(X)).

Yes

?- forall(q(X), p(X)).

No

The predicate copy_term/2 can be used to copy a term. The
predicate findall/3 can be used to collect a resulting list
without any grouping. The elements will be copied. The forall/2
performs generate and test and can be used for a bounded universal
quantification.

The following bags predicates are provided:

**bagof(T, X**_{1}^…^X_{n}^G, L): [ISO 8.10.2]- The predicate determines all the solutions to the matrix G, whereby collecting copies of the template T grouped by the witnesses in a list. The predicate then repeatedly succeeds by unifying the witnesses and when L unifies with the corresponding list.
**setof(T, X**_{1}^…^X_{n}^G, L): [ISO 8.10.2]- The predicate determines the same lists as the predicate bagof/3. But before returning them the lists are sorted by means of the predicate sort/2.
**sys_heapof(T, X**_{1}^…^X_{n}^G, L):- The predicate determines the same lists as the predicate bagof/3. But the lists are sorted by the witnesses instead of grouped by the witnesses.
**copy_term(X, Y): [ISO 8.5.4]**- The predicate creates a copy of X and succeeds when the copy
unifies with Y.

**findall(T, G, L): [ISO 8.10.1]****findall(T, G, L, R):**

- The predicate first finds all the solutions to the goal G, whereby collecting copies of the template T in a list. The predicate then succeeds when L unifies with the list.
**forall(A,B): [N208 8.10.4]**- The predicate succeeds when there is no success of A such that B fails. Otherwise the predicate fails.
**foreach(F, G):**- Calls the conjunction of those goal instances of G where F succeeds. Variables occur-ring in G and not occurring in F are shared.
**foreach(F, G, I, O):**- Calls the conjunction of those closure instances of G where F
succeeds threading them with input I and output O. Variables
occurring in G and not occurring in F are shared.