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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
<!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>
Logic, control and exceptions
</TITLE>
</HEAD>
<BODY TEXT=black BGCOLOR=white>
<A HREF="gprolog040.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="gprolog023.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="gprolog042.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>
<H3 CLASS="subsection"><A NAME="htoc182">7.18</A> Logic, control and exceptions</H3><UL>
<LI><A HREF="gprolog041.html#toc143"><TT>abort/0</TT>,
<TT>stop/0</TT>,
<TT>top_level/0</TT>,
<TT>break/0</TT>,
<TT>halt/1</TT>,
<TT>halt/0</TT></A>
<LI><A HREF="gprolog041.html#toc144"><TT>once/1</TT>, <TT>(\+)/1</TT> - not provable,
<TT>call/2-11</TT>,
<TT>call_with_args/1-11</TT>, <TT>call_det/2</TT></A>
<LI><A HREF="gprolog041.html#toc145"><TT>repeat/0</TT></A>
<LI><A HREF="gprolog041.html#toc146"><TT>for/3</TT></A>
</UL>
<A NAME="toc143"></A>
<H4 CLASS="subsubsection"><A NAME="htoc183">7.18.1</A> <TT>abort/0</TT>,
<TT>stop/0</TT>,
<TT>top_level/0</TT>,
<TT>break/0</TT>,
<TT>halt/1</TT>,
<TT>halt/0</TT></H4>
<A NAME="abort/0"></A>
<B>Templates</B>
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>
abort<BR>
stop<BR>
top_level<BR>
break<BR>
halt(+integer)<BR>
halt</TT></DL>
<B>Description</B><BR>
<BR>
<TT>abort</TT> aborts the current execution. If this execution was initiated
under a top-level the control is given back to the top-level and the
message <TT>{execution aborted}</TT> is displayed. Otherwise,
e.g. execution started by a <TT>initialization/1</TT> directive
(section <A HREF="gprolog021.html#initialization/1">6.1.13</A>), <TT>abort/0</TT> is equivalent to
<TT>halt(1)</TT> (see below).<BR>
<BR>
<TT>stop</TT> stops the current execution. If this execution was initiated
under a top-level the control is given back to the
top-level. Otherwise, <TT>stop/0</TT> is equivalent to <TT>halt(0)</TT>
(see below).<BR>
<BR>
<TT>top_level</TT> starts a new recursive top-level (including the
banner display). To end this new top-level simply type the end-of-file
key sequence (<TT>Ctl-D</TT>) or its term representation:
<TT>end_of_file.</TT><BR>
<BR>
<TT>break</TT> invokes a recursive top-level (no banner is displayed). To
end this new level simply type the end-of-file key sequence (<TT>Ctl-D</TT>)
or its term representation: <TT>end_of_file.</TT><BR>
<BR>
<TT>halt(Status)</TT> causes the GNU Prolog process to immediately exit back to
the shell with the return code <TT>Status</TT>.<BR>
<BR>
<TT>halt</TT> is equivalent to <TT>halt(0)</TT>.<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>Status</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>Status</TT> is neither a variable nor an integer</TD>
<TD VALIGN=top ALIGN=center NOWRAP> </TD>
<TD VALIGN=top ALIGN=left><TT>type_error(integer, Status)</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>
<TT>halt/1</TT> and <TT>halt/0</TT> are ISO predicates. <TT>abort/0</TT>,
<TT>stop/0</TT>, <TT>top_level/0</TT> and <TT>break/0</TT> are GNU Prolog
predicates.<BR>
<BR>
<A NAME="toc144"></A>
<H4 CLASS="subsubsection"><A NAME="htoc184">7.18.2</A> <TT>once/1</TT>, <TT>(\+)/1</TT> - not provable,
<TT>call/2-11</TT>,
<TT>call_with_args/1-11</TT>, <TT>call_det/2</TT></H4>
<B>Templates</B>
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>
once(+callable_term)<BR>
\+(+callable_term)<BR>
call(+callable_term, +term,..., +term)<BR>
call_with_args(+atom, +term,..., +term)<BR>
call_det(+callable_term, ?boolean)</TT></DL>
<B>Description</B><BR>
<BR>
<TT>once(Goal)</TT> succeeds if <TT>call(Goal)</TT> succeeds. However
<TT>once/1</TT> is not re-executable on backtracking since all alternatives
of <TT>Goal</TT> are cut. <TT>once(Goal)</TT> is equivalent to
<TT>call(Goal), !</TT>.<TT><BR>
<BR>
\+ Goal</TT> succeeds if <TT>call(Goal)</TT> fails and fails
otherwise. This built-in predicate gives negation by failure.<BR>
<BR>
<TT>call(Closure, Arg1,..., ArgN)</TT> calls the goal <TT>call(Goal)</TT>
where <TT>Goal</TT> is constructed by appending <TT>Arg1,..., ArgN</TT>
(1 ≤ <TT>N</TT> ≤ 10) additional arguments to the arguments (if any)
of <TT>Closure</TT>.<BR>
<BR>
<TT>call_with_args(Functor, Arg1,..., ArgN)</TT> calls the goal
whose functor is <TT>Functor</TT> and whose arguments are
<TT>Arg1</TT>,..., <TT>ArgN</TT> (0 ≤ <TT>N</TT> ≤ 10).<BR>
<BR>
<TT>call_det(Goal, Deterministic)</TT> succeeds if <TT>call(Goal)</TT>
succeeds and unifies <TT>Deterministic</TT> with <TT>true</TT> if
<TT>Goal</TT> has not created any choice-points, with <TT>false</TT>
otherwise.<TT><BR>
<BR>
\+</TT> is a predefined prefix operator (section <A HREF="gprolog037.html#op/3:(Term-input/output)">7.14.10</A>).<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>Functor</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>Functor</TT> is neither a variable nor an atom</TD>
<TD VALIGN=top ALIGN=center NOWRAP> </TD>
<TD VALIGN=top ALIGN=left><TT>type_error(atom, Functor)</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>Deterministic</TT> is neither a variable nor a boolean</TD>
<TD VALIGN=top ALIGN=center NOWRAP> </TD>
<TD VALIGN=top ALIGN=left><TT>type_error(boolean, Deterministic)</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>for <TT>call/2-11</TT> the resulting arity of <TT>Goal</TT> (arity of <TT>Closure</TT> + <TT>N</TT>) is an integer > <TT>max_arity</TT> flag (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>representation_error(max_arity)</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>
<TT>once/1</TT> and <TT>(\+)/1</TT> are ISO predicates, <TT>call/2-11</TT>, <TT>call_with_args/1-11</TT> and <TT>call_det/2</TT> are GNU Prolog predicates.<BR>
<BR>
<A NAME="toc145"></A>
<H4 CLASS="subsubsection"><A NAME="htoc185">7.18.3</A> <TT>repeat/0</TT></H4>
<B>Templates</B>
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>
repeat</TT></DL>
<B>Description</B><BR>
<BR>
<TT>repeat</TT> generates an infinite sequence of backtracking choices. The
purpose is to repeatedly perform some action on elements which are somehow
generated, e.g. by reading them from a stream, until some test becomes true.
Repeat loops cannot contribute to the logic of the program. They are only
meaningful if the action involves side-effects. The only reason for using
repeat loops instead of a more natural tail-recursive formulation is
efficiency: when the test fails back, the Prolog engine immediately reclaims
any working storage consumed since the call to <TT>repeat/0</TT>.<BR>
<BR>
<B>Errors</B><BR>
<BR>
None.<BR>
<BR>
<B>Portability</B><BR>
<BR>
ISO predicate. <BR>
<BR>
<A NAME="toc146"></A>
<H4 CLASS="subsubsection"><A NAME="htoc186">7.18.4</A> <TT>for/3</TT></H4>
<B>Templates</B>
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>
for(?integer, +integer, +integer)</TT></DL>
<B>Description</B><BR>
<BR>
<TT>for(Counter, Lower, Upper)</TT> generates an sequence of backtracking
choices instantiating <TT>Counter</TT> to the values <TT>Lower</TT>,
<TT>Lower+1</TT>,..., <TT>Upper</TT>. <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>Counter</TT> is neither a variable nor an integer</TD>
<TD VALIGN=top ALIGN=center NOWRAP> </TD>
<TD VALIGN=top ALIGN=left><TT>type_error(integer, Counter)</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>Lower</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>Lower</TT> is neither a variable nor an integer</TD>
<TD VALIGN=top ALIGN=center NOWRAP> </TD>
<TD VALIGN=top ALIGN=left><TT>type_error(integer, Lower)</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>Upper</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>Upper</TT> is neither a variable nor an integer</TD>
<TD VALIGN=top ALIGN=center NOWRAP> </TD>
<TD VALIGN=top ALIGN=left><TT>type_error(integer, Upper)</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>
GNU Prolog predicate.<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="gprolog040.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="gprolog023.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="gprolog042.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
|