All solutions predicates

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

PREDICATE
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

  • Call and exit should be compatible with:
    (basic_props:list/1)Set is a list.
  • The following properties should hold at call time:
    (basic_props:term/1)Template is any term.
    (basic_props:callable/1)Generator is a term which represents a goal, i.e., an atom or a structure.
  • The following properties should hold upon exit:
    (basic_props:term/1)Template is any term.
    (basic_props:list/1)Set is a list.
  • The following properties should hold globally:
    (basic_props:not_further_inst/2)Template is not further instantiated.
Meta-predicate with arguments: setof(?,goal,?).
General properties:

True: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).

PREDICATE
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

  • Call and exit should be compatible with:
    (basic_props:list/1)Bag is a list.
  • The following properties should hold at call time:
    (basic_props:term/1)Template is any term.
    (basic_props:callable/1)Generator is a term which represents a goal, i.e., an atom or a structure.
  • The following properties should hold upon exit:
    (basic_props:term/1)Template is any term.
    (basic_props:list/1)Bag is a list.
  • The following properties should hold globally:
    (basic_props:not_further_inst/2)Template is not further instantiated.
Meta-predicate with arguments: bagof(?,goal,?).
General properties:

True: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).

PREDICATE
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.

(True) Usage:ISO

  • Calls should, and exit will be compatible with:
    (basic_props:list/1)List is a list.
  • The following properties should hold at call time:
    (basic_props:term/1)Template is any term.
    (basic_props:callable/1)Generator is a term which represents a goal, i.e., an atom or a structure.
  • The following properties hold upon exit:
    (basic_props:term/1)Template is any term.
    (basic_props:list/1)List is a list.
  • The following properties hold globally:
    (basic_props:not_further_inst/2)Template is not further instantiated.
    (basic_props:native/1)This predicate is understood natively by CiaoPP.
    (native_props:not_fails/1)All the calls of the form findall(Template,Generator,List) do not fail.
    (native_props:is_det/1)All calls of the form findall(Template,Generator,List) are deterministic.
Meta-predicate with arguments: findall(?,goal,?).

PREDICATE

Usage:

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

  • Call and exit should be compatible with:
    (basic_props:term/1)Arg3 is any term.
    (basic_props:term/1)Arg4 is any term.
  • The following properties should hold at call time:
    (basic_props:term/1)Arg1 is any term.
    (basic_props:callable/1)Arg2 is a term which represents a goal, i.e., an atom or a structure.
  • The following properties should hold upon exit:
    (basic_props:term/1)Arg1 is any term.
    (basic_props:term/1)Arg3 is any term.
    (basic_props:term/1)Arg4 is any term.
  • The following properties should hold globally:
    (basic_props:not_further_inst/2)Arg1 is not further instantiated.
Meta-predicate with arguments: findall(?,goal,?,?).

PREDICATE
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:

  • Call and exit should be compatible with:
    (basic_props:list/1)List is a list.
  • The following properties should hold at call time:
    (basic_props:int/1)N is an integer.
    (basic_props:term/1)Template is any term.
    (basic_props:callable/1)Generator is a term which represents a goal, i.e., an atom or a structure.
  • The following properties should hold upon exit:
    (basic_props:term/1)Template is any term.
    (basic_props:list/1)List is a list.
  • The following properties should hold globally:
    (basic_props:not_further_inst/2)Template is not further instantiated.
Meta-predicate with arguments: findnsols(?,?,goal,?).

PREDICATE
findnsols(N,Template,Generator,List,Tail)

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

Usage:

  • The following properties should hold at call time:
    (basic_props:int/1)N is an integer.
    (basic_props:term/1)Template is any term.
    (basic_props:callable/1)Generator is a term which represents a goal, i.e., an atom or a structure.
  • The following properties should hold upon exit:
    (basic_props:term/1)Template is any term.
  • The following properties should hold globally:
    (basic_props:not_further_inst/2)Template is not further instantiated.
Meta-predicate with arguments: findnsols(?,?,goal,?,?).

PREDICATE

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:callable/1)P is a term which represents a goal, i.e., an atom or a structure.
Meta-predicate with arguments: ? ^goal.
General properties:

True:_X^Y

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

Known bugs and planned improvements

  • Run-time checks have been reported not to work with this code. That means that either the assertions here, or the code that implements the run-time checks are erroneous.