- Documentation
- Reference manual
- The SWI-Prolog library
- library(aggregate): Aggregation operators on backtrackable predicates
- library(apply): Apply predicates on a list
- library(assoc): Association lists
- library(broadcast): Broadcast and receive event notifications
- library(charsio): I/O on Lists of Character Codes
- library(check): Consistency checking
- library(clpb): CLP(B): Constraint Logic Programming over Boolean Variables
- library(clpfd): CLP(FD): Constraint Logic Programming over Finite Domains
- library(clpqr): Constraint Logic Programming over Rationals and Reals
- library(csv): Process CSV (Comma-Separated Values) data
- library(debug): Print debug messages and test assertions
- library(error): Error generating support
- library(gensym): Generate unique identifiers
- library(iostream): Utilities to deal with streams
- library(lists): List Manipulation
- library(main): Provide entry point for scripts
- library(nb_set): Non-backtrackable set
- library(www_browser): Activating your Web-browser
- library(option): Option list processing
- library(optparse): command line parsing
- library(ordsets): Ordered set manipulation
- library(pairs): Operations on key-value lists
- library(persistency): Provide persistent dynamic predicates
- library(pio): Pure I/O
- library(predicate_options): Declare option-processing of predicates
- library(prolog_pack): A package manager for Prolog
- library(prolog_xref): Cross-reference data collection library
- library(quasi_quotations): Define Quasi Quotation syntax
- library(random): Random numbers
- library(readutil): Reading lines, streams and files
- library(record): Access named fields in a term
- library(registry): Manipulating the Windows registry
- library(simplex): Solve linear programming problems
- library(solution_sequences): Modify solution sequences
- library(tabling): Tabled execution (SLG)
- library(thread_pool): Resource bounded thread management
- library(ugraphs): Unweighted Graphs
- library(url): Analysing and constructing URL
- library(varnumbers): Utilities for numbered terms
- library(yall): Lambda expressions

- The SWI-Prolog library
- Packages

- Reference manual

- author
- Markus Triska

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(_X_) scheme (section 7), 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: **github.com/triska/clpb**

We recommend the following reference (PDF: metalevel.at/swiclpb.pdf) for citing this library in scientific publications:

@inproceedings{Triska2016, author = "Markus Triska", title = "The {Boolean} Constraint Solver of {SWI-Prolog}: System Description", booktitle = "FLOPS", series = "LNCS", volume = 9613, year = 2016, pages = "45--61" }

A *Boolean expression* is one of:

`0`

false `1`

true variableunknown truth value atomuniversally quantified variable `~`

Exprlogical NOT Expr+Exprlogical OR Expr*Exprlogical AND Expr#Exprexclusive OR Var`^`

Exprexistential quantification Expr`=:=`

Exprequality Expr`=\=`

Exprdisequality (same as #) Expr`=<`

Exprless or equal (implication) Expr`>=`

Exprgreater or equal Expr<Exprless than Expr>Exprgreater 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.

The most frequently used CLP(B) predicates 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. If`T`= 1`Expr`cannot be satisfied, succeeds with. Otherwise, it fails.`T`= 0 **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)`

.

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.
We 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, we 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).

In the default execution mode, CLP(B) constraints are *not*
monotonic. This means that *adding* constraints can yield new
solutions. For example:

?- sat(X=:=1), X = 1+0. false. ?- X = 1+0, sat(X=:=1), X = 1+0. X = 1+0.

This behaviour is highly problematic from a logical point of view,
and it may render **declarative
debugging** techniques inapplicable.

Set the flag `clpb_monotonic`

to `true`

to make
CLP(B) **monotonic**. If this mode is enabled, then you must wrap
CLP(B) variables with the functor `v/1`

. For example:

?- set_prolog_flag(clpb_monotonic, true). true. ?- sat(v(X)=:=1#1). X = 0.

In this example, we are attempting to place *I* pigeons into *J*
holes in such a way that each hole contains at most one pigeon. One
interesting property of this task is that it can be formulated using
only *cardinality constraints* (`card/2`

). Another
interesting aspect is that this task has no short resolution refutations
in general.

In the following, we use **Prolog
DCG notation** to describe a list `Cs` of CLP(B)
constraints that must all be satisfied.

:- use_module(library(clpb)). :- use_module(library(clpfd)). pigeon(I, J, Rows, Cs) :- length(Rows, I), length(Row, J), maplist(same_length(Row), Rows), transpose(Rows, TRows), phrase((all_card1(Rows),all_max1(TRows)), Cs). all_card1([]) --> []. all_card1([Ls|Lss]) --> [card([1],Ls)], all_card1(Lss). all_max1([]) --> []. all_max1([Ls|Lss]) --> [card([0,1],Ls)], all_max1(Lss).

Example queries:

?- pigeon(9, 8, Rows, Cs), sat(*(Cs)). false. ?- pigeon(2, 3, Rows, Cs), sat(*(Cs)), append(Rows, Vs), labeling(Vs), maplist(writeln, Rows). [0,0,1] [0,1,0]

Consider a Boolean circuit that express the Boolean function `XOR`

with 4 `NAND`

gates. We can model such a circuit with CLP(B)
constraints as follows:

:- use_module(library(clpb)). nand_gate(X, Y, Z) :- sat(Z =:= ~(X*Y)). xor(X, Y, Z) :- nand_gate(X, Y, T1), nand_gate(X, T1, T2), nand_gate(Y, T1, T3), nand_gate(T2, T3, Z).

Using universally quantified variables, we can show that the circuit
does compute `XOR`

as intended:

?- xor(x, y, Z). sat(Z=:=x#y).

The interface predicates of this library follow the example of
**SICStus Prolog**.

Use SICStus Prolog for higher performance in many cases.

In the following, each CLP(B) predicate is described in more detail.

We recommend the following link to refer to this manual:

http://eu.swi-prolog.org/man/clpb.html

- [semidet]
**sat**(`+Expr`) - True iff
`Expr`is a satisfiable Boolean expression. - [semidet]
**taut**(`+Expr, -T`) - Tautology check. 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. - [multi]
**labeling**(`+Vs`) - Enumerate concrete solutions. Assigns truth values to the Boolean
variables
`Vs`such that all stated constraints are satisfied. - [det]
**sat_count**(`+Expr, -Count`) `Count`the number of admissible assignments.`Count`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.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.

- [multi]
**weighted_maximum**(`+Weights, +Vs, -Maximum`) - Enumerate weighted optima over admissible assignments. Maximize a linear
objective function over Boolean variables
`Vs`with integer coefficients`Weights`. This predicate assigns 0 and 1 to the variables in`Vs`such that all stated constraints are satisfied, and`Maximum`is the maximum of`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.

- [det]
**random_labeling**(`+Seed, +Vs`) - Select a single random solution. An admissible assignment of truth
values to the Boolean variables in
`Vs`is chosen in such a way that each admissible assignment is equally likely.`Seed`is an integer, used as the initial seed for the random number generator.