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.
CLP(B) is an instance of the general CLP(.) scheme, extending logic programming with reasoning over specialised domains.
The implementation is based on reduced and ordered Binary Decision Diagrams (BDDs).
Usage examples of this library are available in a public git repository: https://github.com/triska/clpb
A Boolean expression is one of:
0
false 1
true variable unknown truth value atom universally quantified variable ~
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.
Atoms denote parametric values that are universally quantified. All universal quantifiers appear implicitly in front of the entire expression. In residual goals, universally quantified variables always appear on the right-hand side of equations. Therefore, they can be used to express functional dependencies on input variables.
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(X=:=X*Y), sat(Y=:=Y*Z). ?- sat(1#X#a#b). sat(X=:=a#b).
The pending residual goals constrain remaining variables to Boolean expressions and are declaratively equivalent to the original query. The last example illustrates that when applicable, remaining variables are expressed as functions of universally quantified variables.
By default, CLP(B) residual goals appear in (approximately) algebraic
normal form (ANF). This projection is often computationally expensive.
You can set the Prolog flag clpb_residuals
to the value bdd
to see the BDD representation of all constraints. This results in faster
projection to residual goals, and is also useful for learning more about
BDDs. For example:
?- set_prolog_flag(clpb_residuals, bdd). true. ?- sat(X#Y). node(3)- (v(X, 0)->node(2);node(1)), node(1)- (v(Y, 1)->true;false), node(2)- (v(Y, 1)->false;true).
Note that this representation cannot be pasted back on the toplevel, and its details are subject to change. Use copy_term/3 to obtain such answers as Prolog terms.
The variable order of the BDD is determined by the order in which the variables first appear in constraints. To obtain different orders, you can for example use:
?- sat(+[1,Y,X]), sat(X#Y). node(3)- (v(Y, 0)->node(2);node(1)), node(1)- (v(X, 1)->true;false), node(2)- (v(X, 1)->false;true).
A common form of invocation is sat_count(+[1|Vs], Count)
:
This counts the number of admissible assignments to Vs
without imposing any further constraints.
Examples:
?- sat(A =< B), Vs = [A,B], sat_count(+[1|Vs], Count). Vs = [A, B], Count = 3, sat(A=:=A*B). ?- length(Vs, 120), sat_count(+Vs, CountOr), sat_count(*(Vs), CountAnd). Vs = [...], CountOr = 1329227995784915872903807060280344575, CountAnd = 1.
sum(Weight_i*V_i)
over
all admissible assignments. On backtracking, all admissible assignments
that attain the optimum are generated.
This predicate can also be used to minimize a linear Boolean program, since negative integers can appear in Weights.
Example:
?- sat(A#B), weighted_maximum([1,2,1], [A,B,C], Maximum). A = 0, B = 1, C = 1, Maximum = 3.