Constraint programming is a declarative formalism that lets you state relations between terms. This library provides CLP(B), Constraint Logic Programming over Boolean Variables. It can be used to model and solve combinatorial problems such as verification, allocation and covering tasks.
The implementation is based on reduced and ordered Binary Decision Diagrams (BDDs).
A Boolean expression is one of:
0
false 1
true variable unknown truth value ~
Exprlogical NOT Expr + Expr logical OR Expr * Expr logical AND Expr # Expr exclusive OR Var ^
Exprexistential quantification Expr =:=
Exprequality Expr =\=
Exprdisequality (same as #) Expr =<
Exprless or equal (implication) Expr >=
Exprgreater or equal Expr < Expr less than Expr > Expr greater than card(Is,Exprs)
see below +(Exprs)
see below *(Exprs)
see below
where Expr again denotes a Boolean expression.
The Boolean expression card(Is,Exprs)
is true iff the
number of true expressions in the list Exprs is a member of
the list Is of integers and integer ranges of the form From-To
.
+(Exprs)
and *(Exprs)
denote, respectively,
the disjunction and conjunction of all elements in the list Exprs
of Boolean expressions.
Important interface predicates of CLP(B) are:
The unification of a CLP(B) variable X with a term T is
equivalent to posting the constraint sat(X=:=T)
.
Here is an example session with a few queries and their answers:
?- use_module(library(clpb)). true. ?- sat(X*Y). X = Y, Y = 1. ?- sat(X * ~X). false. ?- taut(X * ~X, T). T = 0, sat(X=:=X). ?- sat(X^Y^(X+Y)). sat(X=:=X), sat(Y=:=Y). ?- sat(X*Y + X*Z), labeling([X,Y,Z]). X = Z, Z = 1, Y = 0 ; X = Y, Y = 1, Z = 0 ; X = Y, Y = Z, Z = 1. ?- sat(X =< Y), sat(Y =< Z), taut(X =< Z, T). T = 1, sat(1#X#X*Y), sat(1#Y#Y*Z).
The pending residual goals constrain remaining variables to Boolean expressions and are declaratively equivalent to the original query.
Example:
?- length(Vs, 120), sat_count(+Vs, CountOr), sat_count(*(Vs), CountAnd). Vs = [...], CountOr = 1329227995784915872903807060280344575, CountAnd = 1.