A.7 library(clpb): Constraint Logic Programming over Boolean Variables

author
Markus Triska

A.7.1 Introduction

Constraint programming is a declarative formalism that lets you describe conditions a solution must satisfy. This library provides CLP(B), Constraint Logic Programming over Boolean Variables. It can be used to model and solve combinatorial problems such as circuit verification, graph colouring and allocation tasks.

The implementation is based on reduced and ordered Binary Decision Diagrams (BDDs).

A.7.2 Boolean expressions

A Boolean expression is one of:

0 false
1 true
variable unknown truth value
~ Expr logical NOT
Expr + Expr logical OR
Expr * Expr logical AND
Expr # Expr exclusive OR
Var ^ Expr existential quantification
Expr =:= Expr equality
Expr =\= Expr disequality
Expr =< Expr less or equal
Expr >= Expr greater or equal
Expr < Expr less than
Expr > Expr greater than
card(Is,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.

A.7.3 Interface predicates

Important interface predicates of CLP(B) are:

sat(+Expr)
True iff the Boolean expression Expr is satisfiable.
taut(+Expr, -T)
If Expr is a tautology with respect to the posted constraints, succeeds with T = 1. If Expr cannot be satisfied, succeeds with T = 0. Otherwise, it fails.
labeling(+Vs)
Assigns truth values to the variables Vs such that all constraints are satisfied.

The unification of a CLP(B) variable X with a term T is equivalent to posting the constraint sat(X=:=T).

A.7.4 Examples

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),
node(8)- (X->node(7);node(6)),
node(6)- (Y->node(5);true),
node(5)- (Z->true;false),
node(7)- (Y->node(5);false),
sat(Y=:=Y),
sat(Z=:=Z).

The pending residual goals constrain remaining variables to Boolean expressions, and encode a decision diagram that determines the query's truth value when further constraints are added.

[semidet]sat(+Expr)
States the constraint that Expr be a satisfiable Boolean expression. Fails if Expr cannot be satisfied.
[semidet]taut(+Expr, -T)
Succeeds with T = 0 if the Boolean expression Expr cannot be satisfied, and with T = 1 if Expr is always true with respect to the current constraints. Fails otherwise.
[nondet]labeling(+Vs)
Assigns truth values to the Boolean variables Vs such that all stated constraints are satisfied.
[det]sat_count(+Expr, -N)
N is the number of different assignments of truth values to the variables in the Boolean expression Expr, such that Expr is true and all posted constraints are satisfiable.

Example:

:- use_module(library(clpb)).

or(A, B, B+A).

Yielding:

?- length(Vs, 120), foldl(or, Vs, 0, Expr), sat_count(Expr, N).
Vs = [...], Expr = ... + ...,
N = 1329227995784915872903807060280344575.