Aggregates: gathering predicate solutions

Author(s): Richard A. O'Keefe (first version), David H.D. Warren (first version), Mats Carlsson (changes), Daniel Cabeza, Manuel Hermenegildo.

This module implements the standard solution aggregation predicates.

When there are many solutions to a problem, and when all those solutions are required to be collected together, this can be achieved by repeatedly backtracking and gradually building up a list of the solutions. The following built-in predicates are provided to automate this process.

Usage and interface

Documentation on exports

PREDICATEsetof/3
setof(Template,Generator,Set)

Finds the Set of instances of the Template satisfying Generator. The set is in ascending order (see Comparing terms for a definition of this order) without duplicates, and is non-empty. If there are no solutions, setof fails. setof may succeed in more than one way, binding free variables in Generator to different values. This can be avoided by using existential quantifiers on the free variables in front of Generator, using ^/2. For example, given the clauses:

father(bill, tom).
father(bill, ann).
father(bill, john).
father(harry, july).
father(harry, daniel).

The following query produces two alternative solutions via backtracking:

?- setof(X,father(F,X),Sons).

F = bill,
Sons = [ann,john,tom] ? ;

F = harry,
Sons = [daniel,july] ? ;

no
?-

Usage:ISO

Meta-predicate with arguments: setof(?,goal,?).
Other properties:
setof(X,Y,Z)

  • The following properties hold globally:
    (basic_props:native/2)This predicate is understood natively by CiaoPP as findall(X,Y,Z).

PREDICATEbagof/3
bagof(Template,Generator,Bag)

Finds all the instances of the Template produced by the Generator, and returns them in the Bag in the order in which they were found. If the Generator contains free variables which are not bound in the Template, it assumes that this is like any other Prolog question and that you want bindings for those variables. This can be avoided by using existential quantifiers on the free variables in front of the Generator, using ^/2.

Usage:ISO

Meta-predicate with arguments: bagof(?,goal,?).
Other properties:
bagof(X,Y,Z)

  • The following properties hold globally:
    (basic_props:native/2)This predicate is understood natively by CiaoPP as findall(X,Y,Z).

PREDICATEfindall/3
findall(Template,Generator,List)

A special case of bagof, where all free variables in the Generator are taken to be existentially quantified. Faster than the other aggregation predicates.

Usage:ISO

Meta-predicate with arguments: findall(?,goal,?).

PREDICATEfindall/4

Usage:

As findall/3, but returning in Tail the tail of List (findall(Template, Generator, List, Tail)).

Meta-predicate with arguments: findall(?,goal,?,?).

PREDICATEfindnsols/4
findnsols(N,Template,Generator,List)

As findall/3, but generating at most N solutions of Generator. Thus, the length of List will not be greater than N. If N=<0, returns directly an empty list. This predicate is especially useful if Generator may have an infinite number of solutions.

Usage:

Meta-predicate with arguments: findnsols(?,?,goal,?).

PREDICATEfindnsols/5
findnsols(N,Template,Generator,List,Tail)

As findnsols/4, but returning in Tail the tail of List.

Usage:

Meta-predicate with arguments: findnsols(?,?,goal,?,?).

PREDICATE^/2

Usage:X^P

Existential quantification: X is existentially quantified in P. E.g., in A^p(A,B), A is existentially quantified. Used only within aggregation predicates. In all other contexts, simply, execute the procedure call P.

  • The following properties should hold at call time:
    (term_typing:var/1)X is a free variable.
    (basic_props:cgoal/1)P is a term which represents a goal, i.e., an atom or a structure.
Meta-predicate with arguments: ? ^goal.
Other properties:
_X^Y

  • The following properties hold globally:
    (basic_props:native/2)This predicate is understood natively by CiaoPP as call(Y).

Documentation on imports

This module has the following direct dependencies: