11.2 Collecting solutions

There may be many solutions to a query. For example, suppose we are working with the database

descend(X,Y) :- child(X,Y).          
descend(X,Y) :- child(X,Z),          

Then if we pose the query


there are four solutions (namely X=charlotte, X=caroline, X=laura, and X=rose).

However Prolog generates these solutions one by one. Sometimes we would like to have all the solutions to a query, and we would like them handed to us in a neat, usable, form. Prolog has three built-in predicates that do this: findall, bagof, and setof. Basically these predicates collect all the solutions to a query and put them in a list, but there are important differences between them, as we shall see.

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