1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Dejan Jovanovic, Liana Hadarean, Mathias Preiner |
4 |
|
* |
5 |
|
* This file is part of the cvc5 project. |
6 |
|
* |
7 |
|
* Copyright (c) 2009-2021 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. |
11 |
|
* **************************************************************************** |
12 |
|
* |
13 |
|
* SAT Solver. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "cvc5_private.h" |
17 |
|
|
18 |
|
#ifndef CVC5__PROP__SAT_SOLVER_H |
19 |
|
#define CVC5__PROP__SAT_SOLVER_H |
20 |
|
|
21 |
|
#include <string> |
22 |
|
|
23 |
|
#include "context/cdlist.h" |
24 |
|
#include "context/context.h" |
25 |
|
#include "expr/node.h" |
26 |
|
#include "proof/clause_id.h" |
27 |
|
#include "proof/proof_node_manager.h" |
28 |
|
#include "prop/sat_solver_types.h" |
29 |
|
#include "util/statistics_stats.h" |
30 |
|
|
31 |
|
namespace cvc5 { |
32 |
|
|
33 |
|
namespace prop { |
34 |
|
|
35 |
|
class TheoryProxy; |
36 |
|
|
37 |
22653 |
class SatSolver { |
38 |
|
|
39 |
|
public: |
40 |
|
|
41 |
|
/** Virtual destructor */ |
42 |
22643 |
virtual ~SatSolver() { } |
43 |
|
|
44 |
|
/** Assert a clause in the solver. */ |
45 |
|
virtual ClauseId addClause(SatClause& clause, |
46 |
|
bool removable) = 0; |
47 |
|
|
48 |
|
/** Return true if the solver supports native xor resoning */ |
49 |
|
virtual bool nativeXor() { return false; } |
50 |
|
|
51 |
|
/** Add a clause corresponding to rhs = l1 xor .. xor ln */ |
52 |
|
virtual ClauseId addXorClause(SatClause& clause, bool rhs, bool removable) = 0; |
53 |
|
|
54 |
|
/** |
55 |
|
* Create a new boolean variable in the solver. |
56 |
|
* @param isTheoryAtom is this a theory atom that needs to be asserted to theory |
57 |
|
* @param preRegister whether to preregister the atom with the theory |
58 |
|
* @param canErase whether the sat solver can safely eliminate this variable |
59 |
|
* |
60 |
|
*/ |
61 |
|
virtual SatVariable newVar(bool isTheoryAtom, bool preRegister, bool canErase) = 0; |
62 |
|
|
63 |
|
/** Create a new (or return an existing) boolean variable representing the constant true */ |
64 |
|
virtual SatVariable trueVar() = 0; |
65 |
|
|
66 |
|
/** Create a new (or return an existing) boolean variable representing the constant false */ |
67 |
|
virtual SatVariable falseVar() = 0; |
68 |
|
|
69 |
|
/** Check the satisfiability of the added clauses */ |
70 |
|
virtual SatValue solve() = 0; |
71 |
|
|
72 |
|
/** Check the satisfiability of the added clauses */ |
73 |
|
virtual SatValue solve(long unsigned int&) = 0; |
74 |
|
|
75 |
|
/** Check satisfiability under assumptions */ |
76 |
|
virtual SatValue solve(const std::vector<SatLiteral>& assumptions) |
77 |
|
{ |
78 |
|
Unimplemented() << "Solving under assumptions not implemented"; |
79 |
|
return SAT_VALUE_UNKNOWN; |
80 |
|
}; |
81 |
|
|
82 |
|
/** |
83 |
|
* Tell SAT solver to only do propagation on next solve(). |
84 |
|
* |
85 |
|
* @return true if feature is supported, otherwise false. |
86 |
|
*/ |
87 |
|
virtual bool setPropagateOnly() { return false; } |
88 |
|
|
89 |
|
/** Interrupt the solver */ |
90 |
|
virtual void interrupt() = 0; |
91 |
|
|
92 |
|
/** Call value() during the search.*/ |
93 |
|
virtual SatValue value(SatLiteral l) = 0; |
94 |
|
|
95 |
|
/** Call modelValue() when the search is done.*/ |
96 |
|
virtual SatValue modelValue(SatLiteral l) = 0; |
97 |
|
|
98 |
|
/** Get the current assertion level */ |
99 |
|
virtual unsigned getAssertionLevel() const = 0; |
100 |
|
|
101 |
|
/** Check if the solver is in an inconsistent state */ |
102 |
|
virtual bool ok() const = 0; |
103 |
|
|
104 |
|
/** |
105 |
|
* Get list of unsatisfiable assumptions. |
106 |
|
* |
107 |
|
* The returned assumptions are a subset of the assumptions provided to |
108 |
|
* the solve method. |
109 |
|
* Can only be called if satisfiability check under assumptions was used and |
110 |
|
* if it returned SAT_VALUE_FALSE. |
111 |
|
*/ |
112 |
|
virtual void getUnsatAssumptions(std::vector<SatLiteral>& unsat_assumptions) |
113 |
|
{ |
114 |
|
Unimplemented() << "getUnsatAssumptions not implemented"; |
115 |
|
} |
116 |
|
|
117 |
|
};/* class SatSolver */ |
118 |
|
|
119 |
|
|
120 |
15339 |
class CDCLTSatSolverInterface : public SatSolver |
121 |
|
{ |
122 |
|
public: |
123 |
15334 |
virtual ~CDCLTSatSolverInterface(){}; |
124 |
|
|
125 |
|
virtual void initialize(context::Context* context, |
126 |
|
prop::TheoryProxy* theoryProxy, |
127 |
|
cvc5::context::UserContext* userContext, |
128 |
|
ProofNodeManager* pnm) = 0; |
129 |
|
|
130 |
|
virtual void push() = 0; |
131 |
|
|
132 |
|
virtual void pop() = 0; |
133 |
|
|
134 |
|
/* |
135 |
|
* Reset the decisions in the DPLL(T) SAT solver at the current assertion |
136 |
|
* level. |
137 |
|
*/ |
138 |
|
virtual void resetTrail() = 0; |
139 |
|
|
140 |
|
virtual bool properExplanation(SatLiteral lit, SatLiteral expl) const = 0; |
141 |
|
|
142 |
|
virtual void requirePhase(SatLiteral lit) = 0; |
143 |
|
|
144 |
|
virtual bool isDecision(SatVariable decn) const = 0; |
145 |
|
|
146 |
|
/** |
147 |
|
* Return the current decision level of `lit`. |
148 |
|
*/ |
149 |
|
virtual int32_t getDecisionLevel(SatVariable v) const { return -1; } |
150 |
|
|
151 |
|
/** |
152 |
|
* Return the user-context level when `lit` was introduced.. |
153 |
|
*/ |
154 |
|
virtual int32_t getIntroLevel(SatVariable v) const { return -1; } |
155 |
|
|
156 |
|
virtual std::shared_ptr<ProofNode> getProof() = 0; |
157 |
|
|
158 |
|
}; /* class CDCLTSatSolverInterface */ |
159 |
|
|
160 |
|
inline std::ostream& operator <<(std::ostream& out, prop::SatLiteral lit) { |
161 |
|
out << lit.toString(); |
162 |
|
return out; |
163 |
|
} |
164 |
|
|
165 |
|
inline std::ostream& operator <<(std::ostream& out, const prop::SatClause& clause) { |
166 |
|
out << "clause:"; |
167 |
|
for(unsigned i = 0; i < clause.size(); ++i) { |
168 |
|
out << " " << clause[i]; |
169 |
|
} |
170 |
|
out << ";"; |
171 |
|
return out; |
172 |
|
} |
173 |
|
|
174 |
|
inline std::ostream& operator <<(std::ostream& out, prop::SatValue val) { |
175 |
|
std::string str; |
176 |
|
switch(val) { |
177 |
|
case prop::SAT_VALUE_UNKNOWN: |
178 |
|
str = "_"; |
179 |
|
break; |
180 |
|
case prop::SAT_VALUE_TRUE: |
181 |
|
str = "1"; |
182 |
|
break; |
183 |
|
case prop::SAT_VALUE_FALSE: |
184 |
|
str = "0"; |
185 |
|
break; |
186 |
|
default: |
187 |
|
str = "Error"; |
188 |
|
break; |
189 |
|
} |
190 |
|
|
191 |
|
out << str; |
192 |
|
return out; |
193 |
|
} |
194 |
|
|
195 |
|
} // namespace prop |
196 |
|
} // namespace cvc5 |
197 |
|
|
198 |
|
#endif /* CVC5__PROP__SAT_MODULE_H */ |