1 /********************* */
2 /*! \file sat_solver_types.h
4 ** Top contributors (to current version):
5 ** Dejan Jovanovic, Alex Ozdemir, Liana Hadarean
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2020 by the authors listed in the file AUTHORS
8 ** in the top-level source directory and their institutional affiliations.
9 ** All rights reserved. See the file COPYING in the top-level source
10 ** directory for licensing information.\endverbatim
12 ** \brief This class transforms a sequence of formulas into clauses.
14 ** This class takes a sequence of formulas.
15 ** It outputs a stream of clauses that is propositionally
16 ** equi-satisfiable with the conjunction of the formulas.
17 ** This stream is maintained in an online fashion.
19 ** Unlike other parts of the system it is aware of the PropEngine's
20 ** internals such as the representation and translation of [??? -Chris]
25 #include "cvc4_private.h"
29 #include <unordered_set>
36 * Boolean values of the SAT solver.
44 /** Helper function for inverting a SatValue */
45 inline SatValue
invertValue(SatValue v
)
47 if(v
== SAT_VALUE_UNKNOWN
) return SAT_VALUE_UNKNOWN
;
48 else if(v
== SAT_VALUE_TRUE
) return SAT_VALUE_FALSE
;
49 else return SAT_VALUE_TRUE
;
54 * A variable of the SAT solver.
56 typedef uint64_t SatVariable
;
59 * Undefined SAT solver variable.
61 const SatVariable undefSatVariable
= SatVariable(-1);
64 * A SAT literal is a variable or an negated variable.
69 * The value holds the variable and a bit noting if the variable is negated.
76 * Construct an undefined SAT literal.
79 : d_value(undefSatVariable
)
83 * Construct a literal given a possible negated variable.
85 SatLiteral(SatVariable var
, bool negated
= false) {
86 d_value
= var
+ var
+ (int)negated
;
90 * Returns the variable of the literal.
92 SatVariable
getSatVariable() const {
97 * Returns true if the literal is a negated variable.
99 bool isNegated() const {
104 * Negate the given literal.
106 SatLiteral
operator ~ () const {
107 return SatLiteral(getSatVariable(), !isNegated());
111 * Compare two literals for equality.
113 bool operator == (const SatLiteral
& other
) const {
114 return d_value
== other
.d_value
;
118 * Compare two literals for dis-equality.
120 bool operator != (const SatLiteral
& other
) const {
121 return !(*this == other
);
125 * Compare two literals
127 bool operator<(const SatLiteral
& other
) const
129 return getSatVariable() == other
.getSatVariable()
130 ? isNegated() < other
.isNegated()
131 : getSatVariable() < other
.getSatVariable();
135 * Returns a string representation of the literal.
137 std::string
toString() const {
138 std::ostringstream os
;
139 os
<< (isNegated()? "~" : "") << getSatVariable() << " ";
144 * Returns the hash value of a literal.
146 size_t hash() const {
147 return (size_t)d_value
;
150 uint64_t toInt() const {
155 * Returns true if the literal is undefined.
157 bool isNull() const {
158 return getSatVariable() == undefSatVariable
;
163 * A constant representing a undefined literal.
165 const SatLiteral undefSatLiteral
= SatLiteral(undefSatVariable
);
168 * Helper for hashing the literals.
170 struct SatLiteralHashFunction
{
171 inline size_t operator() (const SatLiteral
& literal
) const {
172 return literal
.hash();
177 * A SAT clause is a vector of literals.
179 typedef std::vector
<SatLiteral
> SatClause
;
181 struct SatClauseSetHashFunction
183 inline size_t operator()(
184 const std::unordered_set
<SatLiteral
, SatLiteralHashFunction
>& clause
)
188 for (const auto& l
: clause
)
196 struct SatClauseLessThan
198 bool operator()(const SatClause
& l
, const SatClause
& r
) const;
202 * Each object in the SAT solver, such as as variables and clauses, can be assigned a life span,
203 * so that the SAT solver can (or should) remove them when the lifespan is over.
205 enum SatSolverLifespan
208 * The object should stay forever and never be removed
210 SAT_LIFESPAN_PERMANENT
,
212 * The object can be removed at any point when it becomes unnecessary.
214 SAT_LIFESPAN_REMOVABLE
,
216 * The object must be removed as soon as the SAT solver exits the search context
217 * where the object got introduced.
219 SAT_LIFESPAN_SEARCH_CONTEXT_STRICT
,
221 * The object can be removed when SAT solver exits the search context where the object
222 * got introduced, but can be kept at the solver discretion.
224 SAT_LIFESPAN_SEARCH_CONTEXT_LENIENT
,
226 * The object must be removed as soon as the SAT solver exits the user-level context where
227 * the object got introduced.
229 SAT_LIFESPAN_USER_CONTEXT_STRICT
,
231 * The object can be removed when the SAT solver exits the user-level context where
232 * the object got introduced.
234 SAT_LIFESPAN_USER_CONTEXT_LENIENT