% This LaTeX document was generated using the LaTeX backend of PlDoc, % The SWI-Prolog documentation system \subsection{Introduction} \label{sec:clpb-intro} 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(\textit{X}) scheme (\secref{clp}), extending logic programming with reasoning over specialised domains. The implementation is based on reduced and ordered Binary Decision Diagrams (BDDs). Benchmarks and usage examples of this library are available from: \href{https://www.metalevel.at/clpb/}{\textbf{https:\Sidiv{}www.metalevel.at/clpb/}} We recommend the following references for citing this library in scientific publications: \begin{code} @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" } @article{Triska2018, title = "Boolean constraints in {SWI-Prolog}: A comprehensive system description", journal = "Science of Computer Programming", volume = "164", pages = "98 - 115", year = "2018", note = "Special issue of selected papers from FLOPS 2016", issn = "0167-6423", doi = "https://doi.org/10.1016/j.scico.2018.02.001", url = "http://www.sciencedirect.com/science/article/pii/S0167642318300273", author = "Markus Triska", keywords = "CLP(B), Boolean unification, Decision diagrams, BDD" } \end{code} These papers are available from \href{https://www.metalevel.at/swiclpb.pdf}{https:\Sidiv{}www.metalevel.at/swiclpb.pdf} and \href{https://www.metalevel.at/boolean.pdf}{https:\Sidiv{}www.metalevel.at/boolean.pdf} respectively. \subsection{Boolean expressions} \label{sec:clpb-exprs} A \textit{Boolean expression} is one of: \begin{quote} \begin{tabulary}{0.9\textwidth}{|l|L|} \hline \verb$0$ & false \\ \verb$1$ & true \\ \textit{variable} & unknown truth value \\ \textit{atom} & universally quantified variable \\ \Stilde{} \textit{Expr} & logical NOT \\ \textit{Expr} + \textit{Expr} & logical OR \\ \textit{Expr} * \textit{Expr} & logical AND \\ \textit{Expr} \# \textit{Expr} & exclusive OR \\ \textit{Var} \Shat{} \textit{Expr} & existential quantification \\ \textit{Expr} \Saeq{} \textit{Expr} & equality \\ \textit{Expr} \Sane{} \textit{Expr} & disequality (same as \#) \\ \textit{Expr} \Sle{} \textit{Expr} & less or equal (implication) \\ \textit{Expr} \Sge{} \textit{Expr} & greater or equal \\ \textit{Expr} $<$ \textit{Expr} & less than \\ \textit{Expr} $>$ \textit{Expr} & greater than \\ \verb$card(Is,Exprs)$ & cardinality constraint (\textit{see below}) \\ \verb$+(Exprs)$ & n-fold disjunction (\textit{see below}) \\ \verb$*(Exprs)$ & n-fold conjunction (\textit{see below}) \\ \hline \end{tabulary} \end{quote} where \textit{Expr} again denotes a Boolean expression. The Boolean expression \verb$card(Is,Exprs)$ is true iff the number of true expressions in the list \arg{Exprs} is a member of the list \arg{Is} of integers and integer ranges of the form \verb$From-To$. For example, to state that precisely two of the three variables \arg{X}, \arg{Y} and \arg{Z} are \const{true}, you can use \verb$sat(card([2],[X,Y,Z]))$. \verb$+(Exprs)$ and \verb$*(Exprs)$ denote, respectively, the disjunction and conjunction of all elements in the list \arg{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. \subsection{Interface predicates} \label{sec:clpb-interface} The most frequently used CLP(B) predicates are: \begin{description} \termitem{sat}{+Expr} True iff the Boolean expression \arg{Expr} is satisfiable. \termitem{taut}{+Expr, -T} If \arg{Expr} is a tautology with respect to the posted constraints, succeeds with \textbf{\arg{T} = 1}. If \arg{Expr} cannot be satisfied, succeeds with \textbf{\arg{T} = 0}. Otherwise, it fails. \termitem{labeling}{+Vs} Assigns truth values to the variables \arg{Vs} such that all constraints are satisfied. \end{description} The unification of a CLP(B) variable \textit{X} with a term \textit{T} is equivalent to posting the constraint \verb$sat(X=:=T)$. \subsection{Examples} \label{sec:clpb-examples} Here is an example session with a few queries and their answers: \begin{code} ?- 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). \end{code} 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. \subsection{Obtaining BDDs} \label{sec:clpb-residual-goals} 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 \prologflag{clpb_residuals} to the value \const{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: \begin{code} ?- 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). \end{code} Note that this representation cannot be pasted back on the toplevel, and its details are subject to change. Use \predref{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: \begin{code} ?- 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). \end{code} \subsection{Enabling monotonic CLP(B)} \label{sec:clpb-monotonic} In the default execution mode, CLP(B) constraints are \textit{not} monotonic. This means that \textit{adding} constraints can yield new solutions. For example: \begin{code} ?- sat(X=:=1), X = 1+0. false. ?- X = 1+0, sat(X=:=1), X = 1+0. X = 1+0. \end{code} This behaviour is highly problematic from a logical point of view, and it may render \href{https://www.metalevel.at/prolog/debugging}{\textbf{declarative debugging}} techniques inapplicable. Set the flag \prologflag{clpb_monotonic} to \const{true} to make CLP(B) \textbf{monotonic}. If this mode is enabled, then you must wrap CLP(B) variables with the functor \predref{v}{1}. For example: \begin{code} ?- set_prolog_flag(clpb_monotonic, true). true. ?- sat(v(X)=:=1#1). X = 0. \end{code} \subsection{Example: Pigeons} \label{sec:clpb-pigeons} In this example, we are attempting to place \textit{I} pigeons into \textit{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 \textit{cardinality constraints} (\predref{card}{2}). Another interesting aspect is that this task has no short resolution refutations in general. In the following, we use \href{https://www.metalevel.at/prolog/dcg}{\textbf{Prolog DCG notation}} to describe a list \arg{Cs} of CLP(B) constraints that must all be satisfied. \begin{code} :- 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_cards(Rows,[1]),all_cards(TRows,[0,1])), Cs). all_cards([], _) --> []. all_cards([Ls|Lss], Cs) --> [card(Cs,Ls)], all_cards(Lss, Cs). \end{code} Example queries: \begin{code} ?- pigeon(9, 8, Rows, Cs), sat(*(Cs)). false. ?- pigeon(2, 3, Rows, Cs), sat(*(Cs)), append(Rows, Vs), labeling(Vs), maplist(portray_clause, Rows). [0, 0, 1]. [0, 1, 0]. etc. \end{code} \subsection{Example: Boolean circuit} \label{sec:clpb-circuit} Consider a Boolean circuit that express the Boolean function \verb$XOR$ with 4 \verb$NAND$ gates. We can model such a circuit with CLP(B) constraints as follows: \begin{code} :- 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). \end{code} Using universally quantified variables, we can show that the circuit does compute \verb$XOR$ as intended: \begin{code} ?- xor(x, y, Z). sat(Z=:=x#y). \end{code} \subsection{Acknowledgments} \label{sec:clpb-acknowledgments} The interface predicates of this library follow the example of \href{https://sicstus.sics.se}{\textbf{SICStus Prolog}}. Use SICStus Prolog for higher performance in many cases. \subsection{CLP(B) predicate index} \label{sec:clpb-predicates} In the following, each CLP(B) predicate is described in more detail. We recommend the following link to refer to this manual: \url{http://eu.swi-prolog.org/man/clpb.html} \begin{description} \predicate[semidet]{sat}{1}{+Expr} True iff \arg{Expr} is a satisfiable Boolean expression. \predicate[semidet]{taut}{2}{+Expr, -T} Tautology check. Succeeds with \arg{T} = 0 if the Boolean expression \arg{Expr} cannot be satisfied, and with \arg{T} = 1 if \arg{Expr} is always true with respect to the current constraints. Fails otherwise. \predicate[multi]{labeling}{1}{+Vs} Enumerate concrete solutions. Assigns truth values to the Boolean variables \arg{Vs} such that all stated constraints are satisfied. \predicate[det]{sat_count}{2}{+Expr, -Count} \arg{Count} the number of admissible assignments. \arg{Count} is the number of different assignments of truth values to the variables in the Boolean expression \arg{Expr}, such that \arg{Expr} is true and all posted constraints are satisfiable. A common form of invocation is \verb$sat_count(+[1|Vs], Count)$: This counts the number of admissible assignments to \arg{Vs} without imposing any further constraints. Examples: \begin{code} ?- 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. \end{code} \predicate[multi]{weighted_maximum}{3}{+Weights, +Vs, -Maximum} Enumerate weighted optima over admissible assignments. Maximize a linear objective function over Boolean variables \arg{Vs} with integer coefficients \arg{Weights}. This predicate assigns 0 and 1 to the variables in \arg{Vs} such that all stated constraints are satisfied, and \arg{Maximum} is the maximum of \verb$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 \textit{minimize} a linear Boolean program, since negative integers can appear in \arg{Weights}. Example: \begin{code} ?- sat(A#B), weighted_maximum([1,2,1], [A,B,C], Maximum). A = 0, B = 1, C = 1, Maximum = 3. \end{code} \predicate[det]{random_labeling}{2}{+Seed, +Vs} Select a single random solution. An admissible assignment of truth values to the Boolean variables in \arg{Vs} is chosen in such a way that each admissible assignment is equally likely. \arg{Seed} is an integer, used as the initial seed for the random number generator. \end{description}