CLP(FD), which stands for Constraint Logic Programming over Finite Domains, implements declarative integer arithmetic in Prolog systems. It is a pure and general replacement of lower-level arithmetic predicates and allows you to efficiently solve combinatorial problems such as planning, scheduling and allocation tasks.
CLP(FD) stands for Constraint Logic Programming over Finite Domains. Finite domain constraints are relations over integer expressions and can be used to model and solve a large variety of combinatorial problems such as planning, scheduling and allocation tasks. They are also used to obtain general and pure integer arithmetic in Prolog.
Almost all modern Prolog implementations include a CLP(FD) solver, which is either already an integral part of the system (examples: GNU Prolog and B-Prolog) or available as a library (examples: SICStus Prolog, SWI-Prolog, YAP).
The most basic use of CLP(FD) constraints is evaluation of integer expressions. For example:
?- X #= 3+5.
X = 8.
In contrast to lower-level predicates, CLP(FD) constraints can be used in all directions. For example:
?- 8 #= 3+X.
X = 5.
CLP(FD) constraints can also be used if no concrete value can yet be deduced. Here is an example of using finite domain constraints in SWI-Prolog, after loading library(clpfd)
(by entering use_module(library(clpfd)).
at the interactive toplevel). We ask for positive integers X
and Y
whose sum is 15:
?- X + Y #= 15, X #> 0, Y #> 0.
The constraint solver answers as follows:
X in 1..14,
X+Y#=15,
Y in 1..14.
In this case, the CLP(FD) solver has deduced that both variables must be integers between 1 and 14.
When binding one of the variables to concrete integers with the built-in predicate indomain/1
, the constraint solver automatically deduces a binding for the other variable so that all constraints are satisified:
?- X + Y #= 15, X #> 0, Y #> 0, indomain(X).
X = 1, Y = 14 ;
X = 2, Y = 13 ;
X = 3, Y = 12 ;
etc.
To use it, you have to import this library (in SWI Prolog):
:- use_module(library(clpfd)).
Implementations
library(clpfd)
of sicstus-prologlibrary(clpfd)
of swi-prologlibrary(finite/clpfd)
of Jekejeke Minlog (discontinued)- gnu-prolog
clpfd/clpz
for sicstus-prologlibrary(clpz)
of Scryer Prolog