dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
NaiveCnfizer.cpp
1
7#include "NaiveCnfizer.h"
8
9#include <numeric>
10#include <set>
11
12#include "dlinear/util/exception.h"
13
14namespace dlinear {
15
16// The main function of the NaiveCnfizer:
17// - It visits each node and introduce a Boolean variable `b` for
18// each subterm `f`, and keep the relation `b ⇔ f`.
19// - Then it cnfizes each `b ⇔ f` and make a conjunction of them.
21 const TimerGuard timer_guard{&stats_.m_timer(), stats_.enabled()};
23 // TODO(soonho): Using cache if needed.
24 return VisitFormula(nnfizer_.Process(f, true));
25}
26Formula NaiveCnfizer::operator()(const Formula &f) const { return Process(f); }
27
28Formula NaiveCnfizer::VisitEqualTo(const Formula &f) const {
29 const Expression &lhs{get_lhs_expression(f)};
30 const Expression &rhs{get_rhs_expression(f)};
31 return (lhs >= rhs) && (lhs <= rhs);
32}
33Formula NaiveCnfizer::VisitNotEqualTo(const Formula &f) const {
34 const Expression &lhs{get_lhs_expression(f)};
35 const Expression &rhs{get_rhs_expression(f)};
36 return (lhs > rhs) || (lhs < rhs);
37}
38Formula NaiveCnfizer::VisitForall(const Formula &f) const {
39 // f = ∀y. φ(x, y).
40 const Variables &quantified_variables{get_quantified_variables(f)}; // y
41 const Formula &quantified_formula{get_quantified_formula(f)}; // φ(x, y)
42 return forall(quantified_variables, Process(quantified_formula));
43}
44
45Formula NaiveCnfizer::VisitConjunction(const Formula &f) const {
46 const std::set<Formula> transformed_operands{
47 map(get_operands(f), [this](const Formula &formula) { return this->VisitFormula(formula); })};
48 return make_conjunction(transformed_operands);
49}
50
51Formula NaiveCnfizer::VisitDisjunction(const Formula &f) const {
52 const std::set<Formula> &transformed_operands{
53 map(get_operands(f), [this](const Formula &formula) { return this->VisitFormula(formula); })};
54 return std::accumulate(transformed_operands.begin(), transformed_operands.end(), Formula::False(),
55 [](const Formula &cnf1, const Formula &cnf2) {
56 std::set<Formula> clauses;
57 if (is_conjunction(cnf1)) {
58 if (is_conjunction(cnf2)) {
59 // Both of cnf1 and cnf2 are conjunctions.
60 for (const Formula &c1 : get_operands(cnf1)) {
61 for (const Formula &c2 : get_operands(cnf2)) {
62 clauses.insert(c1 || c2);
63 }
64 }
65 } else {
66 // Only cnf1 is a conjunction.
67 for (const Formula &c1 : get_operands(cnf1)) {
68 clauses.insert(c1 || cnf2);
69 }
70 }
71 } else {
72 if (is_conjunction(cnf2)) {
73 // Only cnf2 is a conjunction.
74 for (const Formula &c2 : get_operands(cnf2)) {
75 clauses.insert(cnf1 || c2);
76 }
77 } else {
78 // None of them is a conjunction.
79 clauses.insert(cnf1 || cnf2);
80 }
81 }
82 return make_conjunction(clauses);
83 });
84}
85
86Formula NaiveCnfizer::VisitNegation(const Formula &f) const {
87 DLINEAR_ASSERT(is_atomic(get_operand(f)), "The formula must be atomic");
88 return f;
89}
90
91} // namespace dlinear
void Increase()
Increase the iteration counter by one.
Definition Stats.cpp:39
Formula Process(const Formula &f) const
Convert a f into an equivalent formula f' in CNF.
Nnfizer nnfizer_
NNFizer. Used to convert the formula into NNF.
Formula Process(const Formula &f, bool push_negation_into_relationals=false) const
Convert a f into an equivalent formula f' in NNF.
Definition Nnfizer.cpp:17
Timer & m_timer()
Get read-write access to the timer of the stats.
Definition Stats.h:50
bool enabled() const
Check whether the stats is enabled.
Definition Stats.h:48
The TimeGuard wraps a timer object and pauses it when the guard object is destructed.
Definition Timer.h:129
Represents a symbolic form of a first-order logic formula.
Global namespace for the dlinear library.
Formula make_conjunction(const std::vector< Formula > &formulas)
Make conjunction of formulas.
Definition symbolic.cpp:485
std::set< Formula > map(const std::set< Formula > &formulas, const std::function< Formula(const Formula &)> &func)
Given formulas = {f₁, ..., fₙ} and a func : Formula → Formula, map(formulas, func) returns a set {fun...
Definition symbolic.cpp:47
bool is_atomic(const Formula &f)
Check if f is atomic.
Definition symbolic.cpp:53