7#include "IfThenElseEliminator.h"
14#include "dlinear/libs/libgmp.h"
15#include "dlinear/symbolic/Nnfizer.h"
16#include "dlinear/util/Stats.h"
17#include "dlinear/util/Timer.h"
27 const Formula new_f{VisitFormula(f, Formula::True())};
37 const Expression new_e{VisitExpression(e, Formula::True())};
42const std::unordered_map<Expression, Variable> &IfThenElseEliminator::variables()
const {
return ite_to_var_; }
44Expression IfThenElseEliminator::VisitExpression(
const Expression &e,
const Formula &guard)
const {
45 return e.
include_ite() ? GenericExpressionVisitor::VisitExpression(e, guard) : e;
48Expression IfThenElseEliminator::VisitVariable(
const Expression &e,
const Formula &)
const {
return e; }
50Expression IfThenElseEliminator::VisitConstant(
const Expression &e,
const Formula &)
const {
return e; }
52Expression IfThenElseEliminator::VisitAddition(
const Expression &e,
const Formula &guard)
const {
54 Expression ret{get_constant_in_addition(e)};
55 for (
const auto &p : get_expr_to_coeff_map_in_addition(e)) {
56 const Expression &e_i{p.first};
57 const mpq_class &c_i{p.second};
58 ret += c_i * VisitExpression(e_i, guard);
63Expression IfThenElseEliminator::VisitMultiplication(
const Expression &e,
const Formula &guard)
const {
65 Expression ret{get_constant_in_multiplication(e)};
66 for (
const auto &p : get_base_to_exponent_map_in_multiplication(e)) {
67 const Expression &e_i1{p.first};
68 const Expression &e_i2{p.second};
69 ret *= pow(VisitExpression(e_i1, guard), VisitExpression(e_i2, guard));
74Expression IfThenElseEliminator::VisitDivision(
const Expression &e,
const Formula &guard)
const {
75 return VisitExpression(get_first_argument(e), guard) / VisitExpression(get_second_argument(e), guard);
78Expression IfThenElseEliminator::VisitLog(
const Expression &e,
const Formula &guard)
const {
79 return log(VisitExpression(get_argument(e), guard));
82Expression IfThenElseEliminator::VisitAbs(
const Expression &e,
const Formula &guard)
const {
83 return abs(VisitExpression(get_argument(e), guard));
86Expression IfThenElseEliminator::VisitExp(
const Expression &e,
const Formula &guard)
const {
87 return exp(VisitExpression(get_argument(e), guard));
90Expression IfThenElseEliminator::VisitSqrt(
const Expression &e,
const Formula &guard)
const {
91 return sqrt(VisitExpression(get_argument(e), guard));
94Expression IfThenElseEliminator::VisitPow(
const Expression &e,
const Formula &guard)
const {
95 return pow(VisitExpression(get_first_argument(e), guard), VisitExpression(get_second_argument(e), guard));
98Expression IfThenElseEliminator::VisitSin(
const Expression &e,
const Formula &guard)
const {
99 return sin(VisitExpression(get_argument(e), guard));
102Expression IfThenElseEliminator::VisitCos(
const Expression &e,
const Formula &guard)
const {
103 return cos(VisitExpression(get_argument(e), guard));
106Expression IfThenElseEliminator::VisitTan(
const Expression &e,
const Formula &guard)
const {
107 return tan(VisitExpression(get_argument(e), guard));
110Expression IfThenElseEliminator::VisitAsin(
const Expression &e,
const Formula &guard)
const {
111 return asin(VisitExpression(get_argument(e), guard));
114Expression IfThenElseEliminator::VisitAcos(
const Expression &e,
const Formula &guard)
const {
115 return acos(VisitExpression(get_argument(e), guard));
118Expression IfThenElseEliminator::VisitAtan(
const Expression &e,
const Formula &guard)
const {
119 return atan(VisitExpression(get_argument(e), guard));
122Expression IfThenElseEliminator::VisitAtan2(
const Expression &e,
const Formula &guard)
const {
123 return atan2(VisitExpression(get_first_argument(e), guard), VisitExpression(get_second_argument(e), guard));
126Expression IfThenElseEliminator::VisitSinh(
const Expression &e,
const Formula &guard)
const {
127 return sinh(VisitExpression(get_argument(e), guard));
130Expression IfThenElseEliminator::VisitCosh(
const Expression &e,
const Formula &guard)
const {
131 return cosh(VisitExpression(get_argument(e), guard));
134Expression IfThenElseEliminator::VisitTanh(
const Expression &e,
const Formula &guard)
const {
135 return tanh(VisitExpression(get_argument(e), guard));
138Expression IfThenElseEliminator::VisitMin(
const Expression &e,
const Formula &guard)
const {
139 return min(VisitExpression(get_first_argument(e), guard), VisitExpression(get_second_argument(e), guard));
142Expression IfThenElseEliminator::VisitMax(
const Expression &e,
const Formula &guard)
const {
143 return max(VisitExpression(get_first_argument(e), guard), VisitExpression(get_second_argument(e), guard));
146Expression IfThenElseEliminator::VisitIfThenElse(
const Expression &e,
const Formula &guard)
const {
153 if (get_then_expression(e).EqualTo(get_else_expression(e)))
return VisitExpression(get_then_expression(e), guard);
155 const Formula c{VisitFormula(get_conditional_formula(e), guard)};
158 if (c.EqualTo(guard))
return VisitExpression(get_then_expression(e), guard);
159 if (c.EqualTo(!guard))
return VisitExpression(get_else_expression(e), guard);
162 const Formula then_guard{guard && c};
163 const Formula else_guard{guard && !c};
164 const Expression e1{VisitExpression(get_then_expression(e), then_guard)};
165 const Expression e2{VisitExpression(get_else_expression(e), else_guard)};
173Expression IfThenElseEliminator::VisitUninterpretedFunction(
const Expression &e,
const Formula &)
const {
return e; }
175Formula IfThenElseEliminator::VisitFalse(
const Formula &f,
const Formula &)
const {
return f; }
177Formula IfThenElseEliminator::VisitTrue(
const Formula &f,
const Formula &)
const {
return f; }
179Formula IfThenElseEliminator::VisitVariable(
const Formula &f,
const Formula &)
const {
return f; }
181Formula IfThenElseEliminator::VisitEqualTo(
const Formula &f,
const Formula &guard)
const {
182 return VisitExpression(get_lhs_expression(f), guard) == VisitExpression(get_rhs_expression(f), guard);
185Formula IfThenElseEliminator::VisitNotEqualTo(
const Formula &f,
const Formula &guard)
const {
186 return VisitExpression(get_lhs_expression(f), guard) != VisitExpression(get_rhs_expression(f), guard);
189Formula IfThenElseEliminator::VisitGreaterThan(
const Formula &f,
const Formula &guard)
const {
190 return VisitExpression(get_lhs_expression(f), guard) > VisitExpression(get_rhs_expression(f), guard);
193Formula IfThenElseEliminator::VisitGreaterThanOrEqualTo(
const Formula &f,
const Formula &guard)
const {
194 return VisitExpression(get_lhs_expression(f), guard) >= VisitExpression(get_rhs_expression(f), guard);
197Formula IfThenElseEliminator::VisitLessThan(
const Formula &f,
const Formula &guard)
const {
198 return VisitExpression(get_lhs_expression(f), guard) < VisitExpression(get_rhs_expression(f), guard);
201Formula IfThenElseEliminator::VisitLessThanOrEqualTo(
const Formula &f,
const Formula &guard)
const {
202 return VisitExpression(get_lhs_expression(f), guard) <= VisitExpression(get_rhs_expression(f), guard);
205Formula IfThenElseEliminator::VisitConjunction(
const Formula &f,
const Formula &guard)
const {
207 std::set<Formula> new_conjuncts;
208 for (
const Formula &f_i : get_operands(f)) {
209 new_conjuncts.emplace(VisitFormula(f_i, guard));
214Formula IfThenElseEliminator::VisitDisjunction(
const Formula &f,
const Formula &guard)
const {
216 std::set<Formula> new_disjuncts;
217 for (
const Formula &f_i : get_operands(f)) {
218 new_disjuncts.emplace(VisitFormula(f_i, guard));
223Formula IfThenElseEliminator::VisitNegation(
const Formula &f,
const Formula &guard)
const {
224 return !VisitFormula(get_operand(f), guard);
227Formula IfThenElseEliminator::VisitForall(
const Formula &f,
const Formula &)
const {
247 Variables quantified_variables{get_quantified_variables(f)};
248 const Formula &quantified_formula{get_quantified_formula(f)};
250 const Formula eliminated{ite_eliminator_forall.Process(!quantified_formula)};
251 for (
const auto &[_, v] : ite_eliminator_forall.variables()) {
252 quantified_variables.insert(v);
std::size_t counter_
Counter for the number of introduced variables.
Formula Process(const Formula &f)
Returns a equisatisfiable formula by eliminating if-then-expressions in f by introducing new variable...
std::unordered_map< Expression, Variable > ite_to_var_
Mapping from ITE to the corresponding variable obtained by ITE elimination.
std::vector< Formula > added_formulas_
The added formulas introduced by the elimination process.
std::unordered_map< Variable, Formula > ite_var_to_formulas_
Mapping from ITE to the corresponding variable obtained by ITE elimination.
void Increase()
Increase the iteration counter by one.
The TimeGuard wraps a timer object and pauses it when the guard object is destructed.
Represents a symbolic form of an expression.
bool EqualTo(const Expression &e) const
Checks structural equality.
@ CONTINUOUS
A CONTINUOUS variable takes a mpq_class value.
Global namespace for the dlinear library.
Formula make_conjunction(const std::vector< Formula > &formulas)
Make conjunction of formulas.
Formula make_disjunction(const std::vector< Formula > &formulas)
Make disjunction of formulas.