34.3.1 Arithmetic Constraints

?Expr RelOp ?Expr
defines an arithmetic constraint. The syntax for Expr and RelOp is defined by a grammar (see Syntax of Arithmetic Expressions). Note that the expressions are not restricted to being linear. Constraints over non-linear 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 interval-consistency and their reified versions detect interval-entailment and -disentailment; see The Constraint System.

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 Xs RelOp Value. Cannot be reified.
scalar_product(+Coeffs, +Xs, +RelOp, ?Value)
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 Coeffs*Xs RelOp Value. Cannot be reified.

The following constraint is a special case of scalar_product/4 where RelOp is #=, and which is domain-consistent in the Xs:

knapsack(+Coeffs, +Xs, ?Value)
where Coeffs is a list of length n of non-negative integers, Xs is a list of length n of non-negative integers or domain variables, and Value is an integer or a domain variable. Any domain variables must have finite bounds. True if Coeffs*Xs = Value. Cannot be reified.