1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="gprolog.css">
<TITLE>
All solutions
</TITLE>
</HEAD>
<BODY TEXT=black BGCOLOR=white>
<A HREF="gprolog031.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="gprolog023.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="gprolog033.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>
<H3 CLASS="subsection"><A NAME="htoc101">7.9</A> All solutions</H3><UL>
<LI><A HREF="gprolog032.html#toc71">Introduction</A>
<LI><A HREF="gprolog032.html#toc72"><TT>findall/3</TT></A>
<LI><A HREF="gprolog032.html#toc73"><TT>bagof/3</TT>,
<TT>setof/3</TT></A>
</UL>
<A NAME="toc71"></A>
<H4 CLASS="subsubsection"><A NAME="htoc102">7.9.1</A> Introduction</H4>
<A NAME="Introduction:(All-solutions)"></A>
It is sometimes useful to collect all solutions for a goal. This can be done
by repeatedly backtracking and gradually building the list of solutions. The
following built-in predicates are provided to automate this process. <BR>
<BR>
The built-in predicates described in this section invoke <TT>call/1</TT>
(section <A HREF="gprolog022.html#call/1">6.2.3</A>) on the argument <TT>Goal</TT>. When efficiency is crucial
and <TT>Goal</TT> is complex it is better to define an auxiliary predicate
which can then be compiled, and have <TT>Goal</TT> call this predicate.<BR>
<BR>
<A NAME="toc72"></A>
<H4 CLASS="subsubsection"><A NAME="htoc103">7.9.2</A> <TT>findall/3</TT></H4>
<B>Templates</B>
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>
findall(?term, +callable_term, ?list)</TT></DL>
<B>Description</B><BR>
<BR>
<TT>findall(Template, Goal, Instances)</TT> succeeds if <TT>Instances</TT>
unifies with the list of values to which a variable <TT>X</TT> not occurring
in <TT>Template</TT> or <TT>Goal</TT> would be instantiated by successive
re-executions of <TT>call(Goal), X = Template</TT> after systematic
replacement of all variables in <TT>X</TT> by new variables. Thus, the order
of the list <TT>Instances</TT> corresponds to the order in which the proofs
are found.<BR>
<BR>
<B>Errors</B><BR>
<TABLE CELLSPACING=2 CELLPADDING=0>
<TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>Goal</TT> is a variable</TD>
<TD VALIGN=top ALIGN=center NOWRAP> </TD>
<TD VALIGN=top ALIGN=left><TT>instantiation_error</TT></TD>
</TR>
<TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>Goal</TT> is neither a variable nor a callable term</TD>
<TD VALIGN=top ALIGN=center NOWRAP> </TD>
<TD VALIGN=top ALIGN=left><TT>type_error(callable, Goal)</TT></TD>
</TR>
<TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left>The predicate indicator <TT>Pred</TT> of <TT>Goal</TT> does not
correspond to an existing procedure and the value of the <TT>unknown</TT>
Prolog flag is <TT>error</TT> (section <A HREF="gprolog045.html#set-prolog-flag/2">7.22.1</A>)</TD>
<TD VALIGN=top ALIGN=center NOWRAP> </TD>
<TD VALIGN=top ALIGN=left><TT>existence_error(procedure, Pred)</TT></TD>
</TR>
<TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>Instances</TT> is neither a partial list nor a list</TD>
<TD VALIGN=top ALIGN=center NOWRAP> </TD>
<TD VALIGN=top ALIGN=left><TT>type_error(list, Instances)</TT></TD>
</TR>
<TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD>
</TR></TABLE><BR>
<B>Portability</B><BR>
<BR>
ISO predicate.<BR>
<BR>
<A NAME="toc73"></A>
<H4 CLASS="subsubsection"><A NAME="htoc104">7.9.3</A> <TT>bagof/3</TT>,
<TT>setof/3</TT></H4>
<B>Templates</B>
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>
bagof(?term, +callable_term, ?list)<BR>
setof(?term, +callable_term, ?list)</TT></DL>
<B>Description</B><BR>
<BR>
<TT>bagof(Template, Goal, Instances)</TT> assembles as a list the
set of solutions of <TT>Goal</TT> for each different instantiation of the
free variables in <TT>Goal</TT>. The elements of each list are in order of
solution, but the order in which each list is found is undefined.
This predicate is re-executable on backtracking.<BR>
<BR>
<B>Free variable set</B>: <TT>bagof/3</TT> groups the solutions of
<TT>Goal</TT> according to the free variables in <TT>Goal</TT>. This set
corresponds to all variables occurring in <TT>Goal</TT> but not in
<TT>Template</TT>. It is sometimes useful to exclude some additional
variables of <TT>Goal</TT>. For that, <TT>bagof/3</TT> recognizes a goal of
the form <TT>T^Goal</TT> and exclude all variables occuring in <TT>T</TT>
from the free variable set. <TT>(^)/2</TT> can be viewed as an
<EM>existential quantifier</EM> (the logical reading of <TT>X^Goal</TT>
being “there exists an <TT>X</TT> such that <TT>Goal</TT> is true”). The
use of this existential qualifier is superfluous outside <TT>bagof/3</TT>
(and <TT>setof/3</TT>) and then is not recognized.<BR>
<BR>
<TT>(^)/2</TT> is a predefined infix operator (section <A HREF="gprolog037.html#op/3:(Term-input/output)">7.14.10</A>).<BR>
<BR>
<TT>setof(Template, Goal, Instances)</TT> is equivalent to
<TT>bagof(Template,Goal,I), sort(I,Instances)</TT>. Each list is then a
sorted list (duplicate elements are removed).<BR>
<BR>
From the implementation point of view <TT>setof/3</TT> is as fast as
<TT>bagof/3</TT>. Both predicates use an in-place (i.e. destructive) sort
(section <A HREF="gprolog043.html#sort/2">7.20.12</A>) and require the same amount of memory.<BR>
<BR>
<B>Errors</B><BR>
<TABLE CELLSPACING=2 CELLPADDING=0>
<TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>Goal</TT> is a variable</TD>
<TD VALIGN=top ALIGN=center NOWRAP> </TD>
<TD VALIGN=top ALIGN=left><TT>instantiation_error</TT></TD>
</TR>
<TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>Goal</TT> is neither a variable nor a callable term</TD>
<TD VALIGN=top ALIGN=center NOWRAP> </TD>
<TD VALIGN=top ALIGN=left><TT>type_error(callable, Goal)</TT></TD>
</TR>
<TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left>The predicate indicator <TT>Pred</TT> of <TT>Goal</TT> does not
correspond to an existing procedure and the value of the <TT>unknown</TT>
Prolog flag is <TT>error</TT> (section <A HREF="gprolog045.html#set-prolog-flag/2">7.22.1</A>)</TD>
<TD VALIGN=top ALIGN=center NOWRAP> </TD>
<TD VALIGN=top ALIGN=left><TT>existence_error(procedure, Pred)</TT></TD>
</TR>
<TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>Instances</TT> is neither a partial list nor a list</TD>
<TD VALIGN=top ALIGN=center NOWRAP> </TD>
<TD VALIGN=top ALIGN=left><TT>type_error(list, Instances)</TT></TD>
</TR>
<TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD>
</TR></TABLE><BR>
<B>Portability</B><BR>
<BR>
ISO predicates.<BR>
<BR>
<HR SIZE=2>
Copyright (C) 1999-2007 Daniel Diaz
<BR>
<BR>
Verbatim copying and distribution of this entire article is permitted in any
medium, provided this notice is preserved. <BR>
<BR>
<A HREF="index.html#copyright">More about the copyright</A>
<HR>
<A HREF="gprolog031.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="gprolog023.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="gprolog033.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
|