carl  24.04
Computer ARithmetic Library
ModelSubstitution.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 #include <map>
5 #include <memory>
6 
7 #include <optional>
8 
11 #include "ModelValue.h"
12 #include "ModelVariable.h"
13 
14 namespace carl {
15  template<typename Rational, typename Poly> class Model;
16  template<typename Rational, typename Poly> class ModelFormulaSubstitution;
17  template<typename Rational, typename Poly> class ModelMVRootSubstitution;
18  template<typename Rational, typename Poly> class ModelPolynomialSubstitution;
19 
20  /**
21  * Represent a expression for a ModelValue with variables as placeholders,
22  * where the final expression's value depends on the bindings/values of these
23  * variables. The values are given in the (abstract) form of a "carl::Model".
24  */
25  template<typename Rational, typename Poly>
27  private:
28  mutable std::optional<ModelValue<Rational, Poly>> mCachedValue;
29 
30  protected:
31  /// Evaluate this substitution with respect to the given model.
33  public:
34  ModelSubstitution() = default;
35  virtual ~ModelSubstitution() noexcept = default;
36 
37  const ModelValue<Rational, Poly>& evaluate(const Model<Rational, Poly>& model) const {
38  if (mCachedValue == std::nullopt) {
40  }
41  return *mCachedValue;
42  }
43  void resetCache() const {
44  mCachedValue = std::nullopt;
45  }
46 
47  /// Check if this substitution needs the given model variable.
48  virtual bool dependsOn(const ModelVariable&) const {
49  return true;
50  }
51  /// Print this substitution to the given output stream.
52  virtual void print(std::ostream& os) const {
53  os << "substitution";
54  }
55  /// Multiply this model substitution by a rational.
56  virtual void multiplyBy( const Rational& _number ) = 0;
57  /// Add a rational to this model substitution.
58  virtual void add( const Rational& _number ) = 0;
59 
60  /// Create a copy of this model substitution.
62 
64 
65  template<typename Iterator>
67  {
68  ModelValue<Rational,Poly>& mv = _mvit->second;
69  if( mv.isSubstitution() )
70  {
71  mv = mv.asSubstitution()->evaluate( _model );
72  }
73  return mv;
74  }
75  };
76  template<typename Rational, typename Poly>
77  inline std::ostream& operator<<(std::ostream& os, const ModelSubstitution<Rational,Poly>& ms) {
78  ms.print(os);
79  return os;
80  }
81  template<typename Rational, typename Poly>
82  inline std::ostream& operator<<(std::ostream& os, const ModelSubstitutionPtr<Rational,Poly>& ms) {
83  ms->print(os);
84  return os;
85  }
86 
87 
88  template<typename Rational, typename Poly, typename Substitution, typename... Args>
90  return ModelValue<Rational,Poly>(std::make_unique<Substitution>(std::forward<Args>(args)...));
91  }
92  template<typename Rational, typename Poly, typename Substitution, typename... Args>
94  return std::make_unique<Substitution>(std::forward<Args>(args)...);
95  }
96  template<typename Rational, typename Poly>
98  return createSubstitution<Rational,Poly,ModelMVRootSubstitution<Rational,Poly>>(mr);
99  }
100 }
101 
Represent a dynamic root, also known as a "root expression".
mpq_class Rational
Definition: HornerTest.cpp:12
carl is the main namespace for the library.
ModelValue< Rational, Poly > createSubstitution(Args &&... args)
std::ostream & operator<<(std::ostream &os, const BasicConstraint< Poly > &c)
Prints the given constraint on the given stream.
ModelSubstitutionPtr< Rational, Poly > createSubstitutionPtr(Args &&... args)
std::unique_ptr< ModelSubstitution< Rational, Poly > > ModelSubstitutionPtr
Definition: ModelValue.h:24
PositionIteratorType Iterator
Definition: OPBImporter.cpp:23
Represent a collection of assignments/mappings from variables to values.
Definition: Model.h:19
Represent a expression for a ModelValue with variables as placeholders, where the final expression's ...
virtual ModelSubstitutionPtr< Rational, Poly > clone() const =0
Create a copy of this model substitution.
virtual ~ModelSubstitution() noexcept=default
virtual bool dependsOn(const ModelVariable &) const
Check if this substitution needs the given model variable.
virtual Formula< Poly > representingFormula(const ModelVariable &mv)=0
virtual void add(const Rational &_number)=0
Add a rational to this model substitution.
const ModelValue< Rational, Poly > & evaluate(const Model< Rational, Poly > &model) const
virtual void print(std::ostream &os) const
Print this substitution to the given output stream.
std::optional< ModelValue< Rational, Poly > > mCachedValue
virtual void multiplyBy(const Rational &_number)=0
Multiply this model substitution by a rational.
const ModelValue< Rational, Poly > & getModelValue(Iterator _mvit, Model< Rational, Poly > &_model)
virtual ModelValue< Rational, Poly > evaluateSubstitution(const Model< Rational, Poly > &model) const =0
Evaluate this substitution with respect to the given model.
Represent a sum type/variant over the different kinds of values that can be assigned to the different...
Definition: ModelValue.h:56
const ModelSubstitutionPtr< Rational, Poly > & asSubstitution() const
Definition: ModelValue.h:273
bool isSubstitution() const
Definition: ModelValue.h:202
Represent a sum type/variant over the different kinds of variables that exist in CARL to use them in ...
Definition: ModelVariable.h:20