Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > by-pkgid > 2fc07611b08d4a735fd34d5eb60d8e16 > files > 2211

ciao-1.10p8-3mdv2010.0.i586.rpm

<HTML>
<HEAD>
<!-- Created by texi2html 1.56k + clip patches and <A href="http://www.clip.dia.fi.upm.es/Software">lpdoc</A> from ciao.texi on 28 January 2007 -->

<LINK rel="stylesheet" href="ciao.css" type="text/css">
<TITLE>The Ciao Prolog System               - All solutions predicates</TITLE>
</HEAD>
<BODY> 
Go to the <A HREF="ciao_1.html">first</A>, <A HREF="ciao_35.html">previous</A>, <A HREF="ciao_37.html">next</A>, <A HREF="ciao_241.html">last</A> section, <A HREF="ciao_toc.html">table of contents</A>.
<P><HR><P>


<H1><A NAME="SEC193" HREF="ciao_toc.html#TOC193">All solutions predicates</A></H1>
<P>
<A NAME="IDX2684"></A>


<P>
<STRONG>Author(s):</STRONG> First version by Richard A. O'Keefe and David H.D. Warren. Changes by Mats Carlsson, Daniel Cabeza, and Manuel Hermenegildo.


<P>
<STRONG>Version:</STRONG> 1.10#7 (2006/4/26, 19:22:13 CEST)


<P>
<STRONG>Version of last change:</STRONG> 1.5#115 (2000/4/12, 12:17:22 CEST)


<P>
This module implements the standard solution aggregation predicates. 


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



<UL>
<LI><A HREF="ciao_36.html#SEC194">Usage and interface (aggregates)</A>
<LI><A HREF="ciao_36.html#SEC195">Documentation on exports (aggregates)</A>
</UL>



<H2><A NAME="SEC194" HREF="ciao_toc.html#TOC194">Usage and interface (<CODE>aggregates</CODE>)</A></H2>

<div class="cartouche">

<UL>

<LI><STRONG>Library usage:</STRONG>

<CODE>:- use_module(library(aggregates)).</CODE>

<LI><STRONG>Exports:</STRONG>


<UL>

<LI><EM>Predicates:</EM>

<A NAME="IDX2685"></A>
<CODE>setof/3</CODE>, 
<A NAME="IDX2686"></A>
<CODE>bagof/3</CODE>, 
<A NAME="IDX2687"></A>
<CODE>findall/3</CODE>, 
<A NAME="IDX2688"></A>
<CODE>findall/4</CODE>, 
<A NAME="IDX2689"></A>
<CODE>findnsols/4</CODE>, 
<A NAME="IDX2690"></A>
<CODE>findnsols/5</CODE>, 
<A NAME="IDX2691"></A>
<CODE>^/2</CODE>.

</UL>

<LI><STRONG>Other modules used:</STRONG>


<UL>

<LI><EM>System library modules:</EM>

<A NAME="IDX2692"></A>
<CODE>sort</CODE>, 
<A NAME="IDX2693"></A>
<CODE>lists</CODE>.

</UL>

</UL>

</div class="cartouche">



<H2><A NAME="SEC195" HREF="ciao_toc.html#TOC195">Documentation on exports (<CODE>aggregates</CODE>)</A></H2>
<P>
<A NAME="IDX2694"></A>
<A NAME="IDX2695"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>setof/3:</B>
<DD><A NAME="IDX2696"></A>


<P>
<CODE>setof(Template, Generator, Set)</CODE>


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

<PRE>
father(bill, tom).
father(bill, ann).
father(bill, john).
father(harry, july).
father(harry, daniel).
</PRE>

<P>
The following query produces two alternative solutions via backtracking: 

<PRE>
?- setof(X,father(F,X),Sons).

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

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

no
?- 
</PRE>

<P>
<EM>Meta-predicate</EM> with arguments: <CODE>setof(?,goal,?)</CODE>.


<P>
<STRONG>General properties:</STRONG> <CODE>setof(X, Y, Z)</CODE>

<UL>
<LI><EM>The following properties hold globally:</EM>

This predicate is understood natively by CiaoPP as <CODE>findall(X,Y,Z)</CODE>.
 (<CODE>basic_props:native/2</CODE>)
</UL>

</DL>

<P>
<A NAME="IDX2698"></A>
<A NAME="IDX2699"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>bagof/3:</B>
<DD><A NAME="IDX2700"></A>


<P>
<CODE>bagof(Template, Generator, Bag)</CODE>


<P>
Finds all the instances of the <CODE>Template</CODE> produced by the <CODE>Generator</CODE>, and returns them in the <CODE>Bag</CODE> in the order in which they were found. If the <CODE>Generator</CODE> contains free variables which are not bound in the <CODE>Template</CODE>, 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 <CODE>Generator</CODE>, using 
<A NAME="IDX2701"></A>
<CODE>^/2</CODE>.


<P>
<EM>Meta-predicate</EM> with arguments: <CODE>bagof(?,goal,?)</CODE>.


<P>
<STRONG>General properties:</STRONG> <CODE>bagof(X, Y, Z)</CODE>

<UL>
<LI><EM>The following properties hold globally:</EM>

This predicate is understood natively by CiaoPP as <CODE>findall(X,Y,Z)</CODE>.
 (<CODE>basic_props:native/2</CODE>)
</UL>

</DL>

<P>
<A NAME="IDX2702"></A>
<A NAME="IDX2703"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>findall/3:</B>
<DD><A NAME="IDX2704"></A>


<P>
<CODE>findall(Template, Generator, List)</CODE>


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


<P>
<EM>Meta-predicate</EM> with arguments: <CODE>findall(?,goal,?)</CODE>.


<P>
<STRONG>Usage:</STRONG> <CODE>findall(@term, +callable, ?list)</CODE>
<KBD> * ISO * </KBD>

<UL>
<LI><EM>The following properties hold globally:</EM>

This predicate is understood natively by CiaoPP.
 (<CODE>basic_props:native/1</CODE>)
</UL>

</DL>

<P>
<A NAME="IDX2705"></A>
<A NAME="IDX2706"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>findall/4:</B>
<DD><A NAME="IDX2707"></A>


<P>
<EM>Meta-predicate</EM> with arguments: <CODE>findall(?,goal,?,?)</CODE>.


<P>
<STRONG>Usage:</STRONG> <CODE>findall(Template, Generator, List, Tail)</CODE>

<UL>
<LI><EM>Description:</EM> As

<A NAME="IDX2708"></A>
<CODE>findall/3</CODE>, but returning in <CODE>Tail</CODE> the tail of <CODE>List</CODE>. 
</UL>

</DL>

<P>
<A NAME="IDX2709"></A>
<A NAME="IDX2710"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>findnsols/4:</B>
<DD><A NAME="IDX2711"></A>


<P>
<CODE>findnsols(N, Template, Generator, List)</CODE>


<P>
As 
<A NAME="IDX2712"></A>
<CODE>findall/3</CODE>, but generating at most <CODE>N</CODE> solutions of <CODE>Generator</CODE>. Thus, the length of <CODE>List</CODE> will not be greater than <CODE>N</CODE>. If <CODE>N</CODE>=&#60;0, returns directly an empty list. This predicate is especially useful if <CODE>Generator</CODE> may have an infinite number of solutions.


<P>
<EM>Meta-predicate</EM> with arguments: <CODE>findnsols(?,?,goal,?)</CODE>.


</DL>

<P>
<A NAME="IDX2713"></A>
<A NAME="IDX2714"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>findnsols/5:</B>
<DD><A NAME="IDX2715"></A>


<P>
<CODE>findnsols(N, Template, Generator, List, Tail)</CODE>


<P>
As 
<A NAME="IDX2716"></A>
<CODE>findnsols/4</CODE>, but returning in <CODE>Tail</CODE> the tail of <CODE>List</CODE>.


<P>
<EM>Meta-predicate</EM> with arguments: <CODE>findnsols(?,?,goal,?,?)</CODE>.


</DL>

<P>
<A NAME="IDX2717"></A>
<A NAME="IDX2718"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>^/2:</B>
<DD><A NAME="IDX2719"></A>


<P>
<STRONG>General properties:</STRONG> <CODE>_X ^ Y</CODE>

<UL>
<LI><EM>The following properties hold globally:</EM>

This predicate is understood natively by CiaoPP as <CODE>call(Y)</CODE>.
 (<CODE>basic_props:native/2</CODE>)
</UL>

<P>
<STRONG>Usage:</STRONG> <CODE>X ^ P</CODE>

<UL>
<LI><EM>Description:</EM> Existential quantification: <CODE>X</CODE> is existentially quantified in <CODE>P</CODE>. E.g., in <CODE>A^p(A,B)</CODE>, <CODE>A</CODE> is existentially quantified. Used only within

<A NAME="IDX2720"></A>
aggregation predicates. In all other contexts, simply, execute the procedure call <CODE>P</CODE>. 
</UL>

</DL>

<P><HR><P>
Go to the <A HREF="ciao_1.html">first</A>, <A HREF="ciao_35.html">previous</A>, <A HREF="ciao_37.html">next</A>, <A HREF="ciao_241.html">last</A> section, <A HREF="ciao_toc.html">table of contents</A>.
</BODY>
</HTML>