 YAP 7.1.0
Collecting Solutions to a Goal

When there are several solutions to a goal, if the user wants to collect all the solutions he may be led to use the data base, because backtracking will forget previous solutions. More...

## Detailed Description

When there are several solutions to a goal, if the user wants to collect all the solutions he may be led to use the data base, because backtracking will forget previous solutions.

YAP allows the programmer to choose from several system predicates instead of writing his own routines findall/3 gives you the fastest, but crudest solution The other built-in predicates post-process the result of the query in several different ways:

## ◆ findall/3

 class findall/3

findall( T,+ G,- L)

defined in the ISO standard

findall/3 is a simplified version of bagof which has an implicit existential quantifier on every variable

Unifies L with a list that contains all the instantiations of the term T satisfying the goal G

With the following program:

a(2,1).
a(1,1).
a(2,2).

findall(X,a(X,Y),L).
findall( T,+ G,- L)
Definition: setof.yap:70

would be:

X = _32
Y = _33
L = [2,1,2];

Definition at line 1 of file setof.yap.

## ◆ findall/4

 class findall/4

findall( ?Key, +Goal, +InitialSolutions, -Solutions )

Similar to findall/3 , but appends all answers to list L0 Useful, if some answers have already been found

## ◆ setof/3

 class setof/3

setof( X,+ P,- B)

defined in the ISO standard

Similar to `bagof( _T_, _G_, _L_)` but sorts list L and keeping only one copy of each element Again, assuming the same clauses as in the examples above, the reply to the query

setof(X,a(X,Y),L).
setof( X,+ P,- B)

would be:

X = _32
Y = 1
L = [1,2];
X = _32
Y = 2
L = ;

## ◆ bagof/3

 class bagof/3

bagof( T,+ G,- L)

defined in the ISO standard

For each set of possible instances of the free variables occurring in G but not in T, generates the list L of the instances of T satisfying G Again, assuming the same clauses as in the examples above, the reply to the query

?- bagof(X,a(X,Y),L).
X = _32
Y = 1
L = [2,1];
X = _32
Y = 2
L = ;
bagof( T,+ G,- L)

And this is bagof/3

Either we have excess of variables and we need to find the solutions for each instantiation of these variables

## ◆ all/3

 class all/3

all( T,+ G,- L)

Similar to `findall( _T_, _G_, _L_)` but eliminate repeated elements Thus, assuming the same clauses as in the above example, the reply to the query

all(X,a(X,Y),L).
all( T,+ G,- L)

would be:

X = _32
Y = _33
L = [2,1];

Note that all/3 will fail if no answers are found