dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
PredicateAbstractor.cpp
1
6#include "PredicateAbstractor.h"
7
8#include <set>
9#include <sstream>
10#include <utility>
11
12#include "dlinear/symbolic/LinearFormulaFlattener.h"
13#include "dlinear/util/Stats.h"
14#include "dlinear/util/Timer.h"
15
16namespace dlinear {
17
23Formula PredicateAbstractor::operator()(const Formula &f) { return Process(f); }
24
25Formula PredicateAbstractor::Process(const std::vector<Formula> &formulas) {
26 return Process(make_conjunction(std::set<Formula>{formulas.begin(), formulas.end()}));
27}
28Formula PredicateAbstractor::operator()(const std::vector<Formula> &formulas) { return Process(formulas); }
29
31 const Formula flattened_f{flattener_.Flatten(f)};
32 const bool is_negated = is_negation(flattened_f);
33 const Formula &unary_f = is_negated ? get_operand(flattened_f) : flattened_f;
34
35 // First check if we processed this formula before.
36 const auto it = formula_to_var_map_.find(unary_f);
37 if (it == formula_to_var_map_.cend()) {
38 // No, we haven't processed it before.
39 return FormulaVisitor::VisitFormula(flattened_f);
40 } else {
41 // Yes, we have processed this formula before.
42 return is_negated ? !Formula{it->second} : Formula{it->second};
43 }
44}
45
46Formula PredicateAbstractor::VisitAtomic(const Formula &f) const {
47 // Flatten linear formulas to make sure they have the standard form (ax + by <=> c).
48 const Formula flattened_f{flattener_.Flatten(f)};
49 const bool is_negated = is_negation(flattened_f);
50 const Formula &unary_f = is_negated ? get_operand(flattened_f) : flattened_f;
51
52 auto it = formula_to_var_map_.find(unary_f);
53 // Leaf case: create a new Boolean variable `bvar` and record the relation between `bvar` and `f`.
54 if (it == formula_to_var_map_.end()) {
55 const Variable bvar{(std::stringstream{} << "b(" << unary_f << ")").str(), Variable::Type::BOOLEAN};
56 var_to_formula_map_.emplace(bvar, unary_f);
57 formula_to_var_map_.emplace(unary_f, bvar);
58 return is_negated ? !Formula{bvar} : Formula{bvar};
59 }
60 return is_negated ? !Formula{it->second} : Formula{it->second};
61}
62
63Formula PredicateAbstractor::VisitEqualTo(const Formula &f) const { return VisitAtomic(f); }
64Formula PredicateAbstractor::VisitNotEqualTo(const Formula &f) const { return VisitAtomic(f); }
65Formula PredicateAbstractor::VisitLessThan(const Formula &f) const { return VisitAtomic(f); }
66Formula PredicateAbstractor::VisitLessThanOrEqualTo(const Formula &f) const { return VisitAtomic(f); }
67Formula PredicateAbstractor::VisitGreaterThan(const Formula &f) const { return VisitAtomic(f); }
68Formula PredicateAbstractor::VisitGreaterThanOrEqualTo(const Formula &f) const { return VisitAtomic(f); }
69
70Formula PredicateAbstractor::VisitNegation(const Formula &f) const { return !VisitFormula(get_operand(f)); }
71Formula PredicateAbstractor::VisitForall(const Formula &f) const { return VisitAtomic(f); }
72Formula PredicateAbstractor::VisitConjunction(const Formula &f) const {
73 const std::set<Formula> operands{
74 map(get_operands(f), [this](const Formula &formula) { return VisitFormula(formula); })};
75 return make_conjunction(operands);
76}
77Formula PredicateAbstractor::VisitDisjunction(const Formula &f) const {
78 const std::set<Formula> operands{
79 map(get_operands(f), [this](const Formula &formula) { return VisitFormula(formula); })};
80 return make_disjunction(operands);
81}
82
83} // namespace dlinear
void Increase()
Increase the iteration counter by one.
Definition Stats.cpp:39
Formula Flatten(const Formula &formula)
Flatten the given formula.
Formula Process(const Formula &f)
Convert a first-order logic formula f into a Boolean formula by predicate abstraction.
Formula VisitFormula(const Formula &f) const override
Visit an atomic formula.
std::unordered_map< Formula, Variable > formula_to_var_map_
Map from previously converted formula to Boolean variable.
std::unordered_map< Variable, Formula > var_to_formula_map_
Map from Boolean variable to previously converted formula.
LinearFormulaFlattener flattener_
Linear formula flattener.
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.
@ BOOLEAN
A BOOLEAN variable takes a bool value.
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
Formula make_disjunction(const std::vector< Formula > &formulas)
Make disjunction of formulas.
Definition symbolic.cpp:493