carl  24.04
Computer ARithmetic Library
ModelEvaluation_Formula.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "../Model.h"
5 
7 
9 
10 namespace carl {
11 namespace model {
12 
13  template<typename Rational, typename Poly>
15  CARL_LOG_DEBUG("carl.model.evaluation", "Evaluating " << f << " on " << m);
16  Formulas<Poly> res = f.subformulas();
17  for (auto& r: res) {
18  CARL_LOG_DEBUG("carl.model.evaluation", "Evaluating " << r << " on " << m);
19  r = substitute(r, m);
20  CARL_LOG_DEBUG("carl.model.evaluation", "Result: " << r);
21  }
22  f = Formula<Poly>(f.type(), std::move(res));
23  CARL_LOG_DEBUG("carl.model.evaluation", "Result: " << f);
24  }
25 
26  template<typename Rational, typename Poly>
28  CARL_LOG_DEBUG("carl.model.evaluation", "Evaluating " << f << " on " << m);
29  assert(f.type() == FormulaType::VARCOMPARE);
30  const auto& vc = f.variable_comparison();
31 
32  ModelValue<Rational,Poly> cmp = vc.value();
33  if (cmp.isSubstitution()) {
34  CARL_LOG_DEBUG("carl.model.evaluation", "Evaluating " << cmp.asSubstitution() << " on " << m);
35  cmp = cmp.asSubstitution()->evaluate(m);
36  if (cmp.isBool() && !cmp.asBool()) {
37  CARL_LOG_DEBUG("carl.model.evaluation", "MVRoot does not exist, returning false");
38  if (vc.negated()) {
40  } else {
42  }
43  return;
44  }
45  CARL_LOG_DEBUG("carl.model.evaluation", "Evaluated substitution to " << cmp);
46  }
47  if (cmp.isSubstitution()) {
48  CARL_LOG_DEBUG("carl.model.evaluation", "MVRoot is still a substitution, cannot evaluate.");
49  return;
50  }
51  assert(cmp.isRational() || cmp.isRAN());
52  typename Poly::RootType val = cmp.isRational() ? typename Poly::RootType(cmp.asRational()) : cmp.asRAN();
53  CARL_LOG_DEBUG("carl.model.evaluation", "rhs is " << val);
54 
55  auto it = m.find(vc.var());
56  if (it == m.end()) {
57  CARL_LOG_DEBUG("carl.model.evaluation", "Could not evaluate " << vc << " as " << vc.var() << " is not part of the model");
58  return;
59  }
60  const auto& value = m.evaluated(vc.var());
61  assert(value.isRational() || value.isRAN());
62  typename Poly::RootType reference = value.isRational() ? typename Poly::RootType(value.asRational()) : value.asRAN();
63  CARL_LOG_DEBUG("carl.model.evaluation", "Reference value: " << vc.var() << " == " << reference);
64 
66  CARL_LOG_DEBUG("carl.model.evaluation", "Comparison: " << reference << " " << vc.relation() << " " << val);
67  switch (vc.relation()) {
68  case Relation::EQ:
69  if (reference == val) f = Formula<Poly>(FormulaType::TRUE);
70  break;
71  case Relation::NEQ:
72  if (reference != val) f = Formula<Poly>(FormulaType::TRUE);
73  break;
74  case Relation::LESS:
75  if (reference < val) f = Formula<Poly>(FormulaType::TRUE);
76  break;
77  case Relation::LEQ:
78  if (reference <= val) f = Formula<Poly>(FormulaType::TRUE);
79  break;
80  case Relation::GREATER:
81  if (reference > val) f = Formula<Poly>(FormulaType::TRUE);
82  break;
83  case Relation::GEQ:
84  if (reference >= val) f = Formula<Poly>(FormulaType::TRUE);
85  break;
86  }
87  if (vc.negated()) {
88  f = f.negated();
89  CARL_LOG_DEBUG("carl.model.evaluation", "Applying negation, result is " << f);
90  }
91  }
92 
93  template<typename Rational, typename Poly>
95  assert(f.type() == FormulaType::VARASSIGN);
96  const auto& va = f.variable_assignment();
97  auto it = m.find(va.var());
98  if (it == m.end()) return;
99  const auto& value = m.evaluated(va.var());
100  const ModelValue<Rational,Poly>& vavalue = va.value();
101  if (value == vavalue) {
103  } else {
105  }
106  if (va.negated()) f = f.negated();
107  }
108 }
109 
110  /**
111  * Substitutes all variables from a model within a formula.
112  * May fail to substitute some variables, for example if the values are RANs or SqrtEx.
113  */
114  template<typename Rational, typename Poly>
116  switch (f.type()) {
117  case FormulaType::ITE: {
119  break;
120  }
121  case FormulaType::EXISTS:
122  CARL_LOG_WARN("carl.model.evaluation", "Evaluation of exists not yet implemented.");
123  break;
124  case FormulaType::FORALL:
125  CARL_LOG_WARN("carl.model.evaluation", "Evaluation of forall not yet implemented.");
126  break;
127  case FormulaType::TRUE: break;
128  case FormulaType::FALSE: break;
129  case FormulaType::BOOL: {
130  auto it = m.find(f.boolean());
131  if (it == m.end()) break;
132  if (it->second.isBool()) {
133  f = Formula<Poly>(it->second.asBool() ? FormulaType::TRUE : FormulaType::FALSE);
134  } else {
135  CARL_LOG_WARN("carl.model.evaluation", "Could not evaluate " << it->first << " as a boolean, value is " << it->second);
136  }
137  break;
138  }
139  case FormulaType::NOT: {
141  break;
142  }
143  case FormulaType::IMPLIES: {
145  break;
146  }
152  auto res = evaluate(f.constraint(), m);
153  if (res.isBool()) {
154  if (res.asBool()) f = Formula<Poly>(FormulaType::TRUE);
156  } else {
157  assert(res.isSubstitution());
158  const auto& subs = res.asSubstitution();
159  auto fsubs = static_cast<ModelFormulaSubstitution<Rational,Poly>*>(subs.get());
160  f = fsubs->getFormula();
161  }
162  break;
163  }
166  break;
167  }
168  case FormulaType::VARASSIGN: {
170  break;
171  }
172  case FormulaType::BITVECTOR: {
173  BVConstraint bvc = substitute(f.bv_constraint(), m);
176  else f = Formula<Poly>(bvc);
177  break;
178  }
179  case FormulaType::UEQ: {
180  std::set<UVariable> vars;
181  f.u_equality().gatherUVariables(vars);
182  if (m.contains(vars)) {
183  auto val = evaluate(f.u_equality(), m);
184  assert(val.isBool());
185  f = Formula<Poly>(val.asBool() ? FormulaType::TRUE : FormulaType::FALSE);
186  } else {
187  CARL_LOG_WARN("carl.model.evaluation", "Could not evaluate " << f.u_equality() << " as some variables are missing from the model.");
188  }
189  break;
190  }
191  }
192  }
193 
194  /**
195  * Evaluates a formula to a ModelValue over a Model.
196  * If evaluation can not be done for some variables, the result may actually be a ModelPolynomialSubstitution.
197  */
198  template<typename Rational, typename Poly>
200  substitute_inplace(f, m);
201  if (f.is_true()) res = true;
202  else if (f.is_false()) res = false;
203  else res = createSubstitution<Rational,Poly,ModelFormulaSubstitution<Rational,Poly>>(f);
204  }
205 
206 }
A small wrapper that configures logging for carl.
#define CARL_LOG_WARN(channel, msg)
Definition: carl-logging.h:41
#define CARL_LOG_DEBUG(channel, msg)
Definition: carl-logging.h:43
carl is the main namespace for the library.
@ CONSTRAINT
@ BITVECTOR
@ VARCOMPARE
@ VARASSIGN
std::vector< Formula< Poly > > Formulas
bool evaluate(const BasicConstraint< Poly > &c, const Assignment< Number > &m)
Definition: Evaluation.h:10
Coeff substitute(const Monomial &m, const std::map< Variable, Coeff > &substitutions)
Applies the given substitutions to a monomial.
Definition: Substitution.h:19
void evaluate_inplace(ModelValue< Rational, Poly > &res, BVTerm &bvt, const Model< Rational, Poly > &m)
Evaluates a bitvector term to a ModelValue over a Model.
@ GREATER
Definition: SignCondition.h:15
void substitute_inplace(MultivariateRoot< Poly > &mr, Variable var, const Poly &poly)
Create a copy of the underlying polynomial with the given variable replaced by the given polynomial.
void evaluateVarCompare(Formula< Poly > &f, const Model< Rational, Poly > &m)
void substituteSubformulas(Formula< Poly > &f, const Model< Rational, Poly > &m)
void evaluateVarAssign(Formula< Poly > &f, const Model< Rational, Poly > &m)
bool isAlwaysInconsistent() const
Definition: BVConstraint.h:115
bool isAlwaysConsistent() const
Definition: BVConstraint.h:111
const VariableAssignment< Pol > & variable_assignment() const
Definition: Formula.h:421
const Formula & subformula() const
Definition: Formula.h:327
const VariableComparison< Pol > & variable_comparison() const
Definition: Formula.h:416
const Formula & premise() const
Definition: Formula.h:336
const Formula & conclusion() const
Definition: Formula.h:345
carl::Variable::Arg boolean() const
Definition: Formula.h:436
const Constraint< Pol > & constraint() const
Definition: Formula.h:410
const Formulas< Pol > & subformulas() const
Definition: Formula.h:400
const Formula & first_case() const
Definition: Formula.h:363
const Formula & second_case() const
Definition: Formula.h:372
const BVConstraint & bv_constraint() const
Definition: Formula.h:426
const UEquality & u_equality() const
Definition: Formula.h:446
FormulaType type() const
Definition: Formula.h:254
bool is_false() const
Definition: Formula.h:286
Formula negated() const
Definition: Formula.h:308
bool is_true() const
Definition: Formula.h:278
const Formula & condition() const
Definition: Formula.h:354
Represent a collection of assignments/mappings from variables to values.
Definition: Model.h:19
auto find(const typename Map::key_type &key) const
Definition: Model.h:102
auto end() const
Definition: Model.h:46
const ModelValue< Rational, Poly > & evaluated(const typename Map::key_type &key) const
Return the ModelValue for the given key, evaluated if it's a ModelSubstitution and evaluatable,...
Definition: Model.h:146
bool contains(const Container &c) const
Definition: Model.h:117
Represent a sum type/variant over the different kinds of values that can be assigned to the different...
Definition: ModelValue.h:56
const Poly::RootType & asRAN() const
Definition: ModelValue.h:233
const ModelSubstitutionPtr< Rational, Poly > & asSubstitution() const
Definition: ModelValue.h:273
bool isBool() const
Definition: ModelValue.h:143
const Rational & asRational() const
Definition: ModelValue.h:217
bool isSubstitution() const
Definition: ModelValue.h:202
bool isRational() const
Definition: ModelValue.h:150
bool asBool() const
Definition: ModelValue.h:209
bool isRAN() const
Definition: ModelValue.h:164
void gatherUVariables(std::set< UVariable > &uvars) const
Definition: UEquality.cpp:22