Download SICStus Prolog User`s Manual

Transcript
Chapter 10: The Prolog Library
569
Finally, reified constraints can be used as terms inside arithmetic expression. The value of
the term is 1 if the constraint is true, and 0 otherwise. For example:
| ?- X #= 10, B #= (X#>=2) + (X#>=4) + (X#>=8).
B = 3,
X = 10
10.35.4 Available Constraints
This section describes constraints that can be used with this solver, organized into classes.
Unless documented otherwise, constraints are not reifiable and don’t guarantee any particular level of consistency. Whenever a domain variable is required in the argument of a
constraint, a small integer can be given instead.
10.35.4.1 Arithmetic Constraints
?Expr RelOp ?Expr
reifiable
defines an arithmetic constraint. The syntax for Expr and RelOp is defined by a
grammar (see Section 10.35.13.2 [Syntax of Arithmetic Expressions], page 627).
Note that the expressions are not restricted to being linear. Constraints over
nonlinear expressions, however, will usually yield less constraint propagation
than constraints over linear expressions.
Arithmetic constraints can be reified as e.g.:
| ?- X in 1..2, Y in 3..5, X#=<Y #<=> B.
B = 1,
X in 1..2,
Y in 3..5
Linear arithmetic constraints, except equalities, maintain bounds-consistency. Their reified
versions detect bounds-entailment and -disentailment.
The following constraints are among the library constraints that general arithmetic constraints compile to. They express a relation between a sum or a scalar product and a
value, using a dedicated algorithm, which avoids creating any temporary variables holding
intermediate values. If you are computing a sum or a scalar product, it can be much more
efficient to compute lists of coefficients and variables and post a single sum or scalar product
constraint than to post a sequence of elementary constraints.
sum(+Xs, +RelOp, ?Value )
where Xs is a list of integers or domain variables, RelOp is a relational symbol
as above, and Value is an integer or a domain variable. True if sum(Xs ) RelOp
Value . Corresponds roughly to sumlist/2 in library(lists).
scalar_product(+Coeffs, +Xs, +RelOp, ?Value )
scalar_product(+Coeffs, +Xs, +RelOp, ?Value, +Options )
where Coeffs is a list of length n of integers, Xs is a list of length n of integers
or domain variables, RelOp is a relational symbol as above, and Value is an
integer or a domain variable. True if sum(Coeffs*Xs ) RelOp Value .