11.2.1 findall/3

The query


produces a list List of all the objects Object that satisfy the goal Goal. Often Object is simply a variable, in which case the query can be read as: Give me a list containing all the instantiations of Object which satisfy Goal.

Here's an example. Suppose we're working with the above database (that is, with the information about child and the definition of descend). Then if we pose the query


we are asking for a list Z containing all the values of X that satisfy descend(martha,X). Prolog will respond

X = _7489  
Z = [charlotte,caroline,laura,rose] 

But Object doesn't have to be a variable, it may just contain a variable that is in Goal. For example, we might decide that we want to build a new predicate fromMartha/1 that is true only of descendants of Martha. We could do this with the query:


That is, we are asking for a list Z containing all the values of fromMartha(X) that satisfy the goal descend(martha,X). Prolog will respond

X = _7616  
Z = [fromMartha(charlotte),fromMartha(caroline),

Now, what happens, if we ask the following query?


There are no solutions for the goal descend(mary,X) in the knowledge base. So findall returns an empty list.

Note that the first two arguments of findall typically have (at least) one variable in common. When using findall, we normally want to know what solutions Prolog finds for certain variables in the goal, and we tell Prolog which variables in Goal we are interested in by building them into the first argument of findall.

You might encounter situations, however, where findall does useful work although the first two arguments don't share any variables. For example, if you are not interested in who exactly is a descendant of Martha, but only in how many descendants Martha has, you can use the follwing query to find out:

?- findall(Y,descend(martha,X),Z), length(Z,N).

Patrick Blackburn, Johan Bos and Kristina Striegnitz
Version 1.2.5 (20030212)