File: gprolog060.html

package info (click to toggle)
gprolog 1.3.0-6.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 13,512 kB
  • ctags: 8,954
  • sloc: ansic: 57,431; perl: 16,620; sh: 5,900; makefile: 1,284
file content (315 lines) | stat: -rw-r--r-- 13,702 bytes parent folder | download | duplicates (2)
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<!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>
Arithmetic constraints
</TITLE>
</HEAD>
<BODY TEXT=black BGCOLOR=white>
<A HREF="gprolog059.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="gprolog054.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="gprolog061.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H3 CLASS="subsection"><A NAME="htoc321">8.6</A>&nbsp;&nbsp;Arithmetic constraints</H3><UL>
<LI><A HREF="gprolog060.html#toc263">FD arithmetic expressions</A>
<LI><A HREF="gprolog060.html#toc264">Partial AC: <TT>(#=)/2</TT> - constraint equal,
 <TT>(#\=)/2</TT> - constraint not equal,<BR>
<TT>(#&lt;)/2</TT> - constraint less than,
 <TT>(#=&lt;)/2</TT> - constraint less than or equal,<BR>
<TT>(#&gt;)/2</TT> - constraint greater than,
 <TT>(#&gt;=)/2</TT> - constraint greater than or equal</A>
<LI><A HREF="gprolog060.html#toc265">Full AC: <TT>(#=#)/2</TT> - constraint equal,
 <TT>(#\=#)/2</TT> - constraint not equal,<BR>
<TT>(#&lt;#)/2</TT> - constraint less than,
 <TT>(#=&lt;#)/2</TT> - constraint less than or equal,<BR>
<TT>(#&gt;#)/2</TT> - constraint greater than,
 <TT>(#&gt;=#)/2</TT> - constraint greater than or equal</A>
<LI><A HREF="gprolog060.html#toc266"><TT>fd_prime/1</TT>,
 <TT>fd_not_prime/1</TT></A>
</UL>

<A NAME="toc263"></A>
<H4 CLASS="subsubsection"><A NAME="htoc322">8.6.1</A>&nbsp;&nbsp;FD arithmetic expressions</H4>
<A NAME="FD-arithmetic-expressions"></A>
An FD arithmetic expression is a Prolog term built from integers, variables
(Prolog or FD variables), and functors (or operators) that represent
arithmetic functions. The following table details the components of an FD
arithmetic expression:<BR>
<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=1>
<TR><TD VALIGN=top ALIGN=left NOWRAP>FD Expression</TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">Result</DIV></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP>Prolog variable</TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">domain <TT>0..fd_max_integer</TT></DIV></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP>FD variable <TT>X</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">domain of <TT>X</TT></DIV></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP>integer number <TT>N</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">domain <TT>N..N</TT></DIV></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>+ E</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">same as <TT>E</TT></DIV></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>- E</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">opposite of <TT>E</TT></DIV></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 + E2</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">sum of <TT>E1</TT> and <TT>E2</TT></DIV></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 - E2</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">subtraction of <TT>E2</TT> from <TT>E1</TT></DIV></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 * E2</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">multiplication of <TT>E1</TT> by <TT>E2</TT></DIV></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 / E2</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">integer division of <TT>E1</TT> by <TT>E2</TT> (only
succeeds if the remainder is 0)</DIV></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 ** E2</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft"><TT>E1</TT> raised to the power of <TT>E2
</TT>(<TT>E1</TT> or <TT>E2</TT> must be an integer)</DIV></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>min(E1,E2)</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">minimum of <TT>E1</TT> and <TT>E2</TT></DIV></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>max(E1,E2)</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">maximum of <TT>E1</TT> and <TT>E2</TT></DIV></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>dist(E1,E2)</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">distance, i.e. |<TT>E1 - E2|</TT></DIV></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 // E2</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">quotient of the integer division of <TT>E1</TT> by
<TT>E2</TT></DIV></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 rem E2</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">remainder of the integer division of <TT>E1</TT> by
<TT>E2</TT></DIV></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>quot_rem(E1,E2,R)</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">quotient of the integer division of
<TT>E1</TT> by <TT>E2</TT>
 <BR>
(<TT>R</TT> is the remainder of the integer division of <TT>E1</TT> by
 <TT>E2</TT>)</DIV></TD>
</TR></TABLE><BR>
FD expressions are not restricted to be linear. However non-linear
constraints usually yield less constraint propagation than linear
constraints.<BR>
<BR>
<TT>+</TT>, <TT>-</TT>, <TT>*</TT>, <TT>/</TT>, <TT>//</TT>, <TT>rem</TT>
and <TT>**</TT> are predefined infix operators. <TT>+</TT> and <TT>-</TT>
are predefined prefix operators (section&nbsp;<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>a sub-expression is of the form <TT>_ ** E</TT> and <TT>E</TT> is
a variable</TD>
<TD VALIGN=top ALIGN=center NOWRAP>&nbsp;&nbsp;</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>a sub-expression <TT>E</TT> is neither a variable nor an integer
nor an FD arithmetic functor</TD>
<TD VALIGN=top ALIGN=center NOWRAP>&nbsp;&nbsp;</TD>
<TD VALIGN=top ALIGN=left><TT>type_error(fd_evaluable, E)</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>an expression is too complex</TD>
<TD VALIGN=top ALIGN=center NOWRAP>&nbsp;&nbsp;</TD>
<TD VALIGN=top ALIGN=left><TT>resource_error(too_big_fd_constraint)</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>
<A NAME="toc264"></A>
<H4 CLASS="subsubsection"><A NAME="htoc323">8.6.2</A>&nbsp;&nbsp;Partial AC: <TT>(#=)/2</TT> - constraint equal,
 <TT>(#\=)/2</TT> - constraint not equal,<BR>
<TT>(#&lt;)/2</TT> - constraint less than,
 <TT>(#=&lt;)/2</TT> - constraint less than or equal,<BR>
<TT>(#&gt;)/2</TT> - constraint greater than,
 <TT>(#&gt;=)/2</TT> - constraint greater than or equal</H4>
<A NAME="Partial-AC:-(:=)/2"></A>
 
 
 
 
 
 
<B>Templates</B>
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>
#=(?fd_evaluable, ?fd_evaluable)<BR>
#\=(?fd_evaluable, ?fd_evaluable)<BR>
#&lt;(?fd_evaluable, ?fd_evaluable)<BR>
#=&lt;(?fd_evaluable, ?fd_evaluable)<BR>
#&gt;(?fd_evaluable, ?fd_evaluable)<BR>
#&gt;=(?fd_evaluable, ?fd_evaluable)</TT></DL>
<B>Description</B><BR>
<BR>
<TT>FdExpr1 #= FdExpr2</TT> constrains <TT>FdExpr1</TT> to be
equal to <TT>FdExpr2</TT>.<BR>
<BR>
<TT>FdExpr1 #\= FdExpr2</TT> constrains <TT>FdExpr1</TT>
to be different from <TT>FdExpr2</TT>.<BR>
<BR>
<TT>FdExpr1 #&lt; FdExpr2</TT> constrains <TT>FdExpr1</TT> to
be less than <TT>FdExpr2</TT>.<BR>
<BR>
<TT>FdExpr1 #=&lt; FdExpr2</TT> constrains <TT>FdExpr1</TT>
to be less than or equal to <TT>FdExpr2</TT>.<BR>
<BR>
<TT>FdExpr1 #&gt; FdExpr2</TT> constrains <TT>FdExpr1</TT> to
be greater than <TT>FdExpr2</TT>.<BR>
<BR>
<TT>FdExpr1 #&gt;= FdExpr2</TT> constrains <TT>FdExpr1</TT>
to be greater than or equal to <TT>FdExpr2</TT>.<BR>
<BR>
<TT>FdExpr1</TT> and <TT>FdExpr2</TT> are arithmetic FD expressions
(section&nbsp;<A HREF="#FD-arithmetic-expressions">8.6.1</A>).<BR>
<BR>
<TT>#=</TT>, <TT>#\=</TT>, <TT>#&lt;</TT>, <TT>#=&lt;</TT>,
<TT>#&gt;</TT> and <TT>#&gt;=</TT> are predefined infix operators
(section&nbsp;<A HREF="gprolog037.html#op/3:(Term-input/output)">7.14.10</A>).<BR>
<BR>
These predicates post arithmetic constraints that are managed by the solver
using a partial arc-consistency algorithm to reduce the domain of involved
variables. In this scheme only the bounds of the domain of variables are
updated. This leads to less propagation than full arc-consistency techniques
(section&nbsp;<A HREF="#Full-AC:-(:=:)/2">8.6.3</A>) but is generally more efficient for
arithmetic. These arithmetic constraints can be reified (section&nbsp;<A HREF="gprolog061.html#Boolean-and-reified-constraints">8.7</A>).<BR>
<BR>
<B>Errors</B><BR>
<BR>
Refer to the syntax of arithmetic FD expressions for possible errors
(section&nbsp;<A HREF="#FD-arithmetic-expressions">8.6.1</A>).<BR>
<BR>
<B>Portability</B><BR>
<BR>
GNU Prolog predicates.<BR>
<BR>
<A NAME="toc265"></A>
<H4 CLASS="subsubsection"><A NAME="htoc324">8.6.3</A>&nbsp;&nbsp;Full AC: <TT>(#=#)/2</TT> - constraint equal,
 <TT>(#\=#)/2</TT> - constraint not equal,<BR>
<TT>(#&lt;#)/2</TT> - constraint less than,
 <TT>(#=&lt;#)/2</TT> - constraint less than or equal,<BR>
<TT>(#&gt;#)/2</TT> - constraint greater than,
 <TT>(#&gt;=#)/2</TT> - constraint greater than or equal</H4>
<A NAME="Full-AC:-(:=:)/2"></A>
 
 
 
 
 
 
<B>Templates</B>
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>
#=#(?fd_evaluable, ?fd_evaluable)<BR>
#\=#(?fd_evaluable, ?fd_evaluable)<BR>
#&lt;#(?fd_evaluable, ?fd_evaluable)<BR>
#=&lt;#(?fd_evaluable, ?fd_evaluable)<BR>
#&gt;#(?fd_evaluable, ?fd_evaluable)<BR>
#&gt;=#(?fd_evaluable, ?fd_evaluable)</TT></DL>
<B>Description</B><BR>
<BR>
<TT>FdExpr1 #=# FdExpr2</TT> constrains <TT>FdExpr1</TT> to
be equal to <TT>FdExpr2</TT>.<BR>
<BR>
<TT>FdExpr1 #\=# FdExpr2</TT> constrains <TT>FdExpr1</TT> to be
different from <TT>FdExpr2</TT>.<BR>
<BR>
<TT>FdExpr1 #&lt;# FdExpr2</TT> constrains <TT>FdExpr1</TT>
to be less than <TT>FdExpr2</TT>.<BR>
<BR>
<TT>FdExpr1 #=&lt;# FdExpr2</TT> constrains <TT>FdExpr1</TT> to be
less than or equal to <TT>FdExpr2</TT>.<BR>
<BR>
<TT>FdExpr1 #&gt;# FdExpr2</TT> constrains <TT>FdExpr1</TT>
to be greater than <TT>FdExpr2</TT>.<BR>
<BR>
<TT>FdExpr1 #&gt;=# FdExpr2</TT> constrains <TT>FdExpr1</TT> to be
greater than or equal to <TT>FdExpr2</TT>.<BR>
<BR>
<TT>FdExpr1</TT> and <TT>FdExpr2</TT> are arithmetic FD expressions
(section&nbsp;<A HREF="#FD-arithmetic-expressions">8.6.1</A>).<BR>
<BR>
<TT>#=#</TT>, <TT>#\=#</TT>, <TT>#&lt;#</TT>,
<TT>#=&lt;#</TT>, <TT>#&gt;#</TT> and <TT>#&gt;=#</TT> are
predefined infix operators (section&nbsp;<A HREF="gprolog037.html#op/3:(Term-input/output)">7.14.10</A>).<BR>
<BR>
These predicates post arithmetic constraints that are managed by the solver
using a full arc-consistency algorithm to reduce the domain of involved
variables. In this scheme the full domain of variables is updated. This
leads to more propagation than partial arc-consistency techniques (section&nbsp;<A HREF="#FD-arithmetic-expressions">8.6.1</A>) but is generally less efficient for arithmetic.
These arithmetic constraints can be reified (section&nbsp;<A HREF="gprolog061.html#Boolean-FD-expressions">8.7.1</A>).<BR>
<BR>
<B>Errors</B><BR>
<BR>
Refer to the syntax of arithmetic FD expressions for possible errors
(section&nbsp;<A HREF="#FD-arithmetic-expressions">8.6.1</A>).<BR>
<BR>
<B>Portability</B><BR>
<BR>
GNU Prolog predicates.<BR>
<BR>
<A NAME="toc266"></A>
<H4 CLASS="subsubsection"><A NAME="htoc325">8.6.4</A>&nbsp;&nbsp;<TT>fd_prime/1</TT>,
 <TT>fd_not_prime/1</TT></H4>
 
 
<B>Templates</B>
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>
fd_prime(?fd_variable)<BR>
fd_not_prime(?fd_variable)</TT></DL>
<B>Description</B><BR>
<BR>
<TT>fd_prime(X)</TT> constraints <TT>X</TT> to be a prime number between
<TT>0..vector_max</TT>.
This constraint enforces a sparse representation
for the domain of <TT>X</TT> (section&nbsp;<A HREF="gprolog055.html#Intro-FD">8.1</A>).<BR>
<BR>
<TT>fd_not_prime(X)</TT> constraints <TT>X</TT> to be a non prime number
between <TT>0..vector_max</TT>. This constraint enforces a sparse
representation for the domain of <TT>X</TT> (section&nbsp;<A HREF="gprolog055.html#Intro-FD">8.1</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>X</TT> is neither an FD variable nor an integer</TD>
<TD VALIGN=top ALIGN=center NOWRAP>&nbsp;&nbsp;</TD>
<TD VALIGN=top ALIGN=left><TT>type_error(fd_variable, X)</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 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="gprolog059.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="gprolog054.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="gprolog061.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>