NP-completeness of small conflict set generation for congruence closure

The efficiency of satisfiability modulo theories (SMT) solvers is dependent on the capability of theory reasoners to provide small conflict sets, i.e. small unsatisfiable subsets from unsatisfiable sets of literals. Decision procedures for uninterpreted symbols (i.e. congruence closure algorithms) date back from the very early days of SMT. Nevertheless, to the best of our knowledge, the complexity of generating smallest conflict sets for sets of literals with uninterpreted symbols and equalities had not yet been determined, although the corresponding decision problem was believed to be NP-complete. We provide here an NP-completeness proof, using a simple reduction from SAT.


Introduction
Satisfiability modulo theory solvers are nowadays based on a cooperation between a propositional satisfiability (SAT) solver and a theory reasoner for the combination of theories supported by the SMT solver. The propositional structure of the problem is handled by the SAT solver, whereas the theory reasoner only has to deal with conjunctions of this article is to present this proof. The reason why the shortest path method is not able to find shortest conflict clauses is that the weights for congruence edges can not be accurately determined a priori. A preliminary version was presented at the SMT Workshop 2015 [4].

Preliminaries
We assume knowledge of propositional logic and quantifier-free first-order logic with equality and uninterpreted symbols, and only enumerate the notions and notations used in this article. A literal is either a propositional variable or the negation of a propositional variable. A clause is a disjunctive set of literals. A propositional variable x appears positively (negatively) in a clause C if x ∈ C (resp. ¬x ∈ C). The notations { 1 , . . . n } and 1 ∨ · · · ∨ n will be used interchangeably. A clause is tautological if and only if it contains a variable both positively and negatively. We shall tacitly assume that clauses are non-tautological, except when explicitly stated otherwise. Clauses being sets, they cannot contain multiple occurrences of the same literal. A formula in conjunctive normal form (CNF for short) is a conjunctive set of clauses. A total (partial) assignment I for a formula in propositional logic assigns a value in { , ⊥} to each (resp. some) propositional variable(s) in the formula. An assignment I for a formula F is a model of F, denoted I | F, if it makes the formula F true. A formula is satisfiable if it has a model, it is unsatisfiable otherwise. A total or partial assignment is perfectly defined by the set of literals it makes true. By default, an assignment is total unless explicitly said to be partial. A set of formulas E entails a (set of) formula(s) E , denoted E | E , if every model of E is a model of E .
We now define the necessary notions for quantifier-free first-order logic.

Definition 1 (Terms and equations)
A signature is a finite set of function symbols F equipped with an arity function F → N. A constant is a nullary function. A unary function has arity one. Given a signature , the set of terms T is the smallest set containing all constants in F and all terms of the form g(t 1 , . . . , t n ), where g is a function symbol of arity n in F and t 1 , . . . , t n are terms in T . An equation between two terms s, t in T is denoted by s = t.
Signatures commonly include predicate symbols. Everything extends smoothly to signatures with predicates, but to simplify, a quantifier-free first-order logic formula is here just a Boolean combination of equalities between terms; a literal is either an equation or the negation of an equation.
The terms t 1 , . . . , t n are direct subterms of g(t 1 , . . . , t n ). The subterm relation is the reflexive and transitive closure of the direct subterm relation. Given a set of equations E, we denote by T (E) the set of terms and subterms occurring in the equations.
An assignment I on some signature maps each constant to an element in a universe U, and each function symbol to a function of appropriate arity on U. By extension, it assigns an element in U to every term, and a value to every equation s = t, namely if I(s) = I(t) and ⊥ otherwise. Like in propositional logic, an assignment on some signature thus gives a truth value to every formula on this signature.

Definition 2 (Congruence relation) Given a set of terms T closed under the subterm relation, a relation
A congruence relation is also an equivalence relation, since it is reflexive, transitive and symmetric. Therefore a congruence relation partitions its underlying set of terms T into congruence classes, such that two terms (s, t) belong to the same class if and only if (s, t) ∈ R.
The relations {(t, t) : t ∈ T } and T ×T are trivial congruence relations. An assignment I on a signature defines a congruence relation on any subset T ⊆ T , that is, An equation s = t on terms in a set T can be seen as a singleton relation {(s, t)} ⊆ T × T . By extension, a set of equations can also be seen as a relation, i.e., the union of the singleton relations.

Definition 3 (Congruence closure)
The congruence closure E * of a set of equations E on a set of terms T closed under the subterm relation is the smallest congruence relation on T containing E.
Since congruence relations are closed under intersection, the congruence closure of a set of equations always exists. Also notice that, if (s, t) ∈ E * , then E | s = t. We say that E is an explanation for s = t.
An algorithm computing the congruence closure of a relation is also a decision procedure for the problem of satisfiability of sets of equalities and disequalities in quantifier-free firstorder logic with uninterpreted (predicates and) functions. It suffices indeed to compute the congruence closure of all equalities on the terms and subterms occurring in the literals. Then, the set of literals is satisfiable if and only if there is no disequality with both terms in the same class. A model can be built from the congruence closure, on a universe with cardinality equal to the number of classes in the congruence.

Congruence closure in practice
The algorithms we consider in the following take as input a set of literals E. Considering complexity, not only the cardinality of the set is important, but also the number of terms and subterms as well as the number of their occurrences. Congruence closure algorithms in modern SMT solvers typically represent terms with Directed Acyclic Graphs (DAGs) using maximal sharing, and not trees. The number of term and subterm occurrences does not matter, but only the number of distinct (sub)terms. The input is also typically not a set, but successive calls to an assertion function with a literal as argument: every repetition of the same literal then matters for complexity. Let us assume, however, that the input is a set E, terms are DAGs with maximal sharing (i.e. identity of atomic symbols and complex terms can be checked in constant time). Therefore, we characterize complexity results in terms of number of literals, terms and subterms of the input set, i.e. |E| and |T (E)|.
Since congruence relations are basically partitions of equivalent terms that additionally satisfy the compatibility property, it is unsurprising that practical congruence closure algorithms, or decision procedures for ground sets of first-order logic literals, are based on some kind of union-find data-structure. Terms (and subterms) are put into equivalence classes, according to the equalities in the input. The algorithms furthermore check, every time two classes of the partition are merged, whether any new equality induced by compatibility has to be taken into account. Also, it checks that the congruence is consistent with the set of disequalities. We refer the reader to [3,7,10] for more details. The complexity of those algorithms depend on the internal data-structures and on the representation of terms [3]. Algorithms typically implemented in SMT solvers have complexity O(|E|+(|T (E)|·log |T (E)|)) assuming constant time operations on the hash table being used to detect new equalities induced by compatibility.
The generation of conflict sets or explanations is based on the congruence graph: its nodes are the terms and subterms considered by the algorithm. An edge in the graph is either a full edge, linking two nodes s and t and labeled by an input equation s = t, or a congruence edge (a dotted edge in the figures in this article), linking two terms with the same leading function symbol and labeled by the compatibility-deduced equality between both terms. The graph has a path between two terms if and only if they belong to the same congruence class. The equality between two terms in the same class is a logical consequence of the set of equations labeling the path. To get an explanation for the equality of two terms in the same class, that is, a set of input equations implying the equality of the two terms, it thus suffices to collect the set of equations labeling a path, and recursively replace any compatibility equation g(t 1 , . . . , t n ) = g(s 1 , . . . , s n ) by the explanations of t 1 = s 1 ,…, t n = s n . ( f (a))))) is given on Fig. 1. Labeling equations are omitted for simplicity. There is a path between a and f (a), so both terms are equal if the input equations hold. To compute an explanation for a = f (a), it suffices to collect the equalities on the path, that is, the input f (a)))) and the compatibility equation f (a))). This last equation should then be replaced by the equation between the arguments, i.e., a = f ( f (a))) which is consequence, by transitivity, of another compatibility equation and of the other input ( f (a))))). Hence the explanation will contain both equations.

Example 1 A congruence graph for two input equations
Practical congruence closure algorithms with explanation build a congruence graph while computing the congruence closure. Every time the decision procedure merges two classes, either because of an input equation or because an equality was deduced due to compatibility, a full-or congruence-edge is added to the graph. Since edges between nodes are only added when their respective congruence classes are merged, the path between two terms in the same class is unique. The explanation that two terms are equal is also unique, but there is no guarantee that this explanation is the smallest one. Indeed, it may happen that the algorithm considers, e.g. equations a = b and b = c before a = c, merging a, b and c before considering the last equation, and thus discarding a = c as redundant: in that case, a = c would have been the smallest proof that a and c are equal, but the congruence graph would only consider the two other equalities. There is not even a guarantee that the explanation is minimal. Again, the congruence closure algorithm can prove that The redundant equality f (a) = f (b) would be recorded in the congruence graph, and thus be part of the explanation, if it is considered before a = b.
In practice, the congruence closure procedures implemented in SMT solvers produce explanations efficiently: the complexity of the explanation production is quasi-linear with respect to the explanation size, which is at most equal to the size of the input [10]. But the explanations are not optimal, i.e. they are not always the smallest. In fact, they are not even minimal. It is possible to compute minimal explanations in polynomial time; it suffices for instance to compute again the congruence closure iteratively removing every equation in the explanation, to see if it is redundant or not. One could (naively) hope to conceive a different congruence closure algorithm generating the smallest explanation in polynomial time. For example, one might attempt to modify the iterative removal algorithm; or attempt to modify shortest path algorithms and apply them to congruence graphs enriched with redundant equations as labels. However, such attempts would be futile. As proven in the next section, the corresponding decision problem is NP-hard.

NP-completeness of the small conflict set problem
The function problem of generating the smallest conflict set corresponds to the decision problem of deciding whether a conflict set with size smaller than a given k exists.
Definition 4 (Small conflict set problem) Given an unsatisfiable set E of literals in quantifierfree first-order logic with equality and k ∈ N, the small conflict set generation problem is the problem of deciding whether there exists an unsatisfiable set E ⊆ E with |E | ≤ k.
If we had a polynomial-time algorithm α capable of generating the smallest conflict set for any unsatisfiable set E, then we could decide in polynomial time any instance of the small conflict set problem by applying α to E and checking whether α(E) has size smaller than k. However, as proven below, the small conflict set problem is NP-complete and, therefore, polynomial time generation of conflict sets with minimum size is not possible (unless P = NP). Our proof reduces the problem of deciding the satisfiability of a propositional logic formula in conjunctive normal form (SAT) to the small explanation problem. In the following we describe a polynomial translation from instances of the propositional satisfiability problem to instances of the small explanation problem. The translation consists of two parts: a translation of propositional formulas, here assumed, without loss of generality, to be in CNF (as shown in Definition 6), and a translation of assignments (as shown in Definition 7). Definition 6 (CNF congruence translation) Let C be a set of propositional clauses {C 1 , . . . C n } using variables x 1 , . . . , x m . The congruence translation E C of C is defined as the set of equations The translation of clauses is illustrated by the following example.

Example 2 Consider the set of clauses
For convenience, we also define the set An assignment congruence translation is always a subset of AssignmentEqs. By extension, a subset of AssignmentEqs is said to be an assignment if it is the congruence translation of an assignment, that is, if it does not contain bothx j = j andx j = ⊥ j for some j. Figure 3 gives a graphical representation of E I , whereas AssignmentEqs is represented in Fig. 4. Notice that E C ∪ E I | c 1 = c 3 , and c 1 and c 3 are connected in the congruence graph of E C ∪ E I (Fig. 5), the path containing both full edges corresponding to equalities in E C ∪ E I , and dotted edged corresponding to equalities due to the compatibility property of the congruence relation. (⇒) If I | C, then I | C i for each clause C i ∈ C. Assume I makes true a variable x j , literal of C i (the case of the negation of a variable is handled similarly). Then E I | t i (x j ) = t i ( j ), and E I ∪ Clause i | c i = c i . This is true for each i, and thanks to the equations in Connect, one can deduce using transitivity that E I ∪ E C | c 1 = c n . Proof E I contains at most m equations, since for no j both I | x j and I | ¬x j . The set Connect contains exactly n − 1 equations. For every i, the set Clause i contains at most 2m equations, resulting in 2mn equations for all clauses. In total, we thus have |E I ∪ E C | ≤ n − 1 + m + 2mn.

Example 3 (Example 2 continued) Consider the model
E I ∪ E C contains at most 2n + 3m + 3mn terms: 2n for c i , c i , 3m forx j , j , ⊥ j and 3mn for all possible combinations of Considering again Example 3, and particularly Figure 5, any transitivity chain from c 1 to c 3 will pass through c 1 , c 2 , c 2 and c 3 . Any acyclic path from c 1 to c 3 will contain 11 edges: 3 congruence edges, 3 * 2 edges in Clause i for i = 1, 2, 3 and 2 edges from Connect.
Since every interpretation I is such that E I ⊂ AssignmentEqs, one can try to relate the propositional satisfiability problem for a set of clauses C = {C 1 , . . . C n } to finding an explanation of c 1 = c n in AssignmentEqs ∪ E C . However, it is necessary that this explanation does not setx j equal both to j and ⊥ j , i.e. at most one of the two equationsx j = j andx j = ⊥ j should be in the explanation. By restricting assignments to total ones, i.e. by enforcing that at least one of the two equationsx j = j andx j = ⊥ j belongs to the explanation, it is also possible, with a single cardinality condition on the explanation size, to require that at most one of them belong to the explanation. Proof (⇒) Consider a total model I for C. We show that there is a set E ⊂ E C , such that together with the congruence translation E I of I it follows E = E ∪ E I | c 1 = c n+m and |E | ≤ 3n + 4m − 1.

Lemma 3 A set of non-tautological clauses
The set E I contains m equations, since it is the congruence translation of a total assignment.
For each clause C i (i = 1 . . . n + m), there is a literal in C i that is satisfied by the model I. Let x j be the variable of that literal.
Suppose I | x j , then the set E contains equations c i = t i (x j ), t i ( j ) = c i of Clause i . These equations are in Clause i , because x j is the satisfying literal of C i , thus surely x j ∈ C i . From compatibility and the fact thatx j = j ∈ E I it follows that E ∪ E I | t i (x j ) = t i ( j ). Finally, from transitivity and the three equations The case I | x j is symmetric, such that via equations In total, E contains 2(n + m) of the sets Clause i , n + m − 1 equations from Connect and m equations from E I , i.e. |E| = 3n + 4m − 1.
(⇐) Suppose there is a set of equations E ⊆ AssignmentEqs ∪ E C such that E | c 1 = c n+m and |E | ≤ 3n + 4m − 1. E has to contain 2(n + m) equations from Clause i (i = 1 . . . n + m), that is one pair of equations c i = t i (.) and t i (.) = c i for every clause, and n + m − 1 equations from Connect, since by construction there is no other possibility to deduce c i = c i . Furthermore, thanks to the tautological clauses, E also has to contain at leastx j = j orx j = ⊥ j for each j ∈ {1 . . . m}. Therefore, the cardinality condition |E | ≤ 3n + 4m − 1 and the fact that E contains 3(n + m) − 1 equations from Clause i and Connect, requires that the E contains at most onex j = j orx j = ⊥ j for each j ∈ {1 . . . m}. Therefore, we have that E I = E ∩ AssignmentEqs is the congruence translation of an assignment and Lemma 1 guarantees the existence of a model for C , or equivalently for the original set of clauses C. Example 4 In Lemma 3, the input formula is augmented with tautological clauses. We demonstrate here the necessity of these extra clauses on the unsatisfiable formula ϕ = ( Figure 6 shows the congruence translation of ϕ together with a subset of AssignmentEqs that yields an explanation for c 1 = c 4 . This explanation picks, besides the necessary equations from the clause and connect parts, two equations from the AssignmentEqs part. However, this explanation maps x 1 to ⊥ and at the same time, and hence cannot correspond to a (consistent) assignment. With the addition of tautological clauses and because the number of equations in the explanation is upper bounded, spurious explanations of this kind are ruled out. This is illustrated in Fig. 7, depicting the congruence translation of ϕ conjoined with the tautological clauses (x 1 ∨ ¬x 1 ) and (x 2 ∨ ¬x 2 ), together with the same subset of AssignmentEqs used in Fig. 6. As desired, this subset is not an explanation of c 1 = c 6 , since the transitivity chain stops at t 6 (x 2 ), x 2 being unassigned. In fact, in this congruence graph, there is no explanation of c 1 = c 6 with less than 19 equations. This is as expected, since ϕ is unsatisfiable and 3n + 4m − 1 = 19 in our example with n = 4 clauses and m = 2 variables.

Corollary 1 (NP-hardness) The small explanation problem is NP-hard.
Proof Propositional satisfiability is NP-hard, and can be reduced in polynomial time to the small explanation problem. Proof Let E be a set of equations and s = t be a target equation. A solution to the explanation problem for some k ∈ N is a subset E ⊆ E, such that |E | ≤ k. Let n = |T (E)| + |E| and n = |T (E )| + |E |. We have n ≤ n, since E ⊆ E and every term in E appears also in E.
Checking whether E is an explanation of s = t can be done by computing its congruence closure, which is possible in polynomial time in n [7] and thereby also in n.

Theorem 1 (Small explanation NP-completeness)
The small explanation problem is NPcomplete.
Proof By Corollary 1 and lemma 4.

Theorem 2 (Small conflict NP-completeness) The small conflict set problem is NP-complete.
Proof The small conflict set problem is at least as hard as the small explanation problem since the small explanation problem has been showed to be reducible to the small conflict set problem. It is also in NP for exactly the same reason that the small explanation problem is.

Conclusion
The conflict set generation feature of congruence algorithms is essential for practical SMT solving. Although one could argue that the important property of the generated conflicts is minimality (i.e. no useless literal is in the conflict), it is also interesting to consider producing the smallest conflict. We have shown that the problem of deciding whether a conflict of a given size exists is NP-complete. Therefore, it is generally intractable to obtain the smallest conflict.
In [6,8,9], methods to obtain small conflicts, but not necessarily the smallest, are discussed. In practice, it pays off to prioritize speed of the congruence closure algorithm and conflict generation over succinctness of conflicts. However, other applications sensitive to proof size may benefit from other methods prioritizing small conflict size, at a cost of less efficient solving. Thanks to the NP-completeness, one option could be to iteratively encode the small conflict problem into SAT, and use a SAT-solver to find successively smaller conflicts, until the smallest is found. Perhaps an encoding of the problem can be found that differentiates between hard constraints representing relevant instantiations of the axioms of equality as well as the target equation, and soft constraints representing the inclusion of input equations to an explanation. In that case, Max-SAT solvers could be used to find small explanations, in order to leverage efforts that combine decision procedures and optimization techniques.