3 #include "../Formula.h"
9 namespace formula_to_cnf {
18 template<
typename Poly>
20 std::vector<Formula<Poly>> res;
22 for (
const auto& sub: rhs_and) {
23 subs.emplace_back(!sub);
30 template<
typename Poly>
32 template<
typename Poly>
39 template<
typename Poly>
46 std::vector<Formula<Poly>> subformula_queue = { f };
47 while (!subformula_queue.empty()) {
48 auto current = subformula_queue.
back();
49 CARL_LOG_DEBUG(
"carl.formula.cnf",
"Processing " << current <<
" from " << subformula_queue);
50 subformula_queue.pop_back();
52 switch (current.type()) {
62 subformulas.emplace_back(current);
66 if (simplify_combinations) {
68 CARL_LOG_DEBUG(
"carl.formula.cnf",
"Adding " << current <<
" to constraint bounds yielded a tautology");
72 subformulas.emplace_back(current);
78 if (resolved.is_literal()) {
79 subformulas.emplace_back(resolved);
81 subformula_queue.emplace_back(resolved);
87 subformula_queue.emplace_back(!current.premise());
88 subformula_queue.emplace_back(current.conclusion());
94 !current.condition(), current.first_case()
97 current.condition(), current.second_case()
105 for (
const auto& sub: current.subformulas()) {
106 subA.emplace_back(sub);
107 subB.emplace_back(!sub);
116 const auto& rhs = current.subformulas().back();
123 for (
const auto& sub: current.subformulas()) {
124 subformula_queue.emplace_back(sub);
130 if (tseitin_equivalence) {
135 subformulas.emplace_back(tseitinVar);
140 CARL_LOG_ERROR(
"carl.formula.cnf",
"Cannot transform quantified formula to CNF");
147 }
else if (subformulas.empty()) {
150 }
else if (subformulas.size() == 1) {
151 return subformulas.front();
166 template<
typename Poly>
169 if (keep_constraints) {
184 std::vector<Formula<Poly>> subformula_queue = { f };
185 while (!subformula_queue.empty()) {
186 auto current = subformula_queue.
back();
187 CARL_LOG_DEBUG(
"carl.formula.cnf",
"Processing " << current <<
" from " << subformula_queue);
188 subformula_queue.pop_back();
190 switch (current.type()) {
200 subformulas.emplace_back(current);
204 if (simplify_combinations) {
206 CARL_LOG_DEBUG(
"carl.formula.cnf",
"Adding " << current <<
" to constraint bounds yielded a conflict");
210 subformulas.emplace_back(current);
216 if (resolved.is_literal()) {
217 subformulas.emplace_back(resolved);
219 subformula_queue.emplace_back(resolved);
226 !current.premise(), current.conclusion()
232 !current.condition(), current.first_case()
235 current.condition(), current.second_case()
239 if (current.subformulas().size() == 2) {
240 const auto& lhs = current.subformulas().front();
241 const auto& rhs = current.subformulas().back();
244 subformula_queue.insert(subformula_queue.end(), tmp.begin(), tmp.end());
247 subformula_queue.insert(subformula_queue.end(), tmp.begin(), tmp.end());
257 for (
const auto& sub: current.subformulas()) {
258 subA.emplace_back(sub);
259 subB.emplace_back(!sub);
270 const auto& rhs = current.subformulas().back();
277 for (
const auto& sub: current.subformulas()) {
278 subformula_queue.emplace_back(sub);
285 if (res.is_false()) {
288 subformula_queue.insert(subformula_queue.end(), tseitin.begin(), tseitin.end());
289 subformulas.emplace_back(res);
294 CARL_LOG_ERROR(
"carl.formula.cnf",
"Cannot transform quantified formula to CNF");
301 }
else if (subformulas.empty()) {
303 }
else if (subformulas.size() == 1) {
304 return subformulas.front();
#define CARL_LOG_ERROR(channel, msg)
#define CARL_LOG_DEBUG(channel, msg)
carl is the main namespace for the library.
bool swapConstraintBounds(ConstraintBounds< Pol > &_constraintBounds, Formulas< Pol > &_intoFormulas, bool _inConjunction)
Stores for every polynomial for which we determined bounds for given constraints a minimal set of con...
Formula< Pol > addConstraintBound(ConstraintBounds< Pol > &_constraintBounds, const Formula< Pol > &_constraint, bool _inConjunction)
Adds the bound to the bounds of the polynomial specified by this constraint.
static constexpr Condition PROP_IS_IN_CNF
std::vector< Formula< Poly > > Formulas
Formula< Pol > resolve_negation(const Formula< Pol > &f, bool _keepConstraint=true, bool resolve_varcomp=false)
Resolves the outermost negation of this formula.
Formula< Poly > to_cnf(const Formula< Poly > &f, bool keep_constraints=true, bool simplify_combinations=false, bool tseitin_equivalence=true)
Converts the given formula to CNF.
std::unordered_map< T1, T2, std::hash< T1 > > FastMap
Formula< Pol > connectPrecedingSubformulas(const Formula< Pol > &f)
[Auxiliary method]
std::vector< Formula< Poly > > construct_iff(const Formula< Poly > &lhs, const std::vector< Formula< Poly >> &rhs_and)
Constructs the equivalent of (iff lhs (and *rhs_and))) The result is the list (=> lhs (and *rhs_and))...
FastMap< Poly, std::map< typename Poly::NumberType, std::pair< Relation, Formula< Poly > >> > ConstraintBounds
std::vector< Formula< Poly > > TseitinConstraints
Formula< Poly > to_cnf_or(const Formula< Poly > &f, bool keep_constraints, bool simplify_combinations, bool tseitin_equivalence, TseitinConstraints< Poly > &tseitin)
Converts an OR to cnf.
static FormulaPool< Pol > & getInstance()
Returns the single instance of this class by reference.
bool property_holds(const Condition &_property) const
Checks if the given property holds for this formula.
const Formula & back() const