- Documentation
- Reference manual
- The SWI-Prolog library
- library(clpfd): CLP(FD): Constraint Logic Programming over Finite Domains
- Introduction
- Arithmetic constraints
- Declarative integer arithmetic
- Example: Factorial relation
- Combinatorial constraints
- Domains
- Example: Sudoku
- Residual goals
- Core relations and search
- Example: Eight queens puzzle
- Optimisation
- Reification
- Enabling monotonic CLP(FD)
- Custom constraints
- Applications
- Acknowledgments
- CLP(FD) predicate index
- Closing and opening words about CLP(FD)
- library(clpfd): CLP(FD): Constraint Logic Programming over Finite Domains
- The SWI-Prolog library
- Packages
- Reference manual
A.9.2 Arithmetic constraints
In modern Prolog systems, arithmetic constraints subsume and supersede low-level predicates over integers. The main advantage of arithmetic constraints is that they are true relations and can be used in all directions. For most programs, arithmetic constraints are the only predicates you will ever need from this library.
The most important arithmetic constraint is #=/2,
which subsumes both
(is)/2
and (=:=)/2
over integers. Use #=/2
to make your programs more general. See declarative integer arithmetic (section
A.9.3).
In total, the arithmetic constraints are:
Expr1 #=
Expr2Expr1 equals Expr2 Expr1 #\=
Expr2Expr1 is not equal to Expr2 Expr1 #>=
Expr2Expr1 is greater than or equal to Expr2 Expr1 #=<
Expr2Expr1 is less than or equal to Expr2 Expr1 #>
Expr2Expr1 is greater than Expr2 Expr1 #<
Expr2Expr1 is less than Expr2
Expr1 and Expr2 denote arithmetic expressions, which are:
integer Given value variable Unknown integer ?(variable) Unknown integer -Expr Unary minus Expr + Expr Addition Expr * Expr Multiplication Expr - Expr Subtraction Expr ^
ExprExponentiation min(Expr,Expr)
Minimum of two expressions max(Expr,Expr)
Maximum of two expressions Expr mod
ExprModulo induced by floored division Expr rem
ExprModulo induced by truncated division abs(Expr)
Absolute value Expr //
ExprTruncated integer division Expr div Expr Floored integer division
where Expr again denotes an arithmetic expression.
The bitwise operations (\)/1
, (/\)/2
, (\/)/2
, (>>)/2
,
(<<)/2
, lsb/1, msb/1, popcount/1
and (xor)/2
are also supported.