carl  24.04
Computer ARithmetic Library
Model.h
Go to the documentation of this file.
1 #pragma once
2 
4 
5 #include "ModelVariable.h"
6 #include "ModelValue.h"
7 
8 namespace carl
9 {
10  /**
11  * Represent a collection of assignments/mappings from variables to values.
12  * We use a ModelVariable to abstract over the different kinds of variables
13  * in CARL, and a ModelValue to abstract over the different kinds of values
14  * for these variables.
15  * Most notably, a value can be a "carl::ModelSubstitution" whose value depends
16  * on the values of other variables in the Model.
17  */
18  template<typename Rational, typename Poly>
19  class Model {
20  public:
23  using Map = std::map<key_type,mapped_type>;
24  static_assert(std::is_same<key_type, typename Map::key_type>::value, "Should be the same type");
25  static_assert(std::is_same<mapped_type, typename Map::mapped_type>::value, "Should be the same type");
26  private:
28  std::map<key_type, std::size_t> mUsedInSubstitution;
29  void resetCaches() const {
30  for (const auto& d: mData) {
31  if (d.second.isSubstitution()) {
32  d.second.asSubstitution()->resetCache();
33  }
34  }
35  }
36  public:
37  // Element access
38  const auto& at(const key_type& key) const {
39  return mData.at(key);
40  }
41 
42  // Iterators
43  auto begin() const {
44  return mData.begin();
45  }
46  auto end() const {
47  return mData.end();
48  }
49  // Capacity
50  auto empty() const {
51  return mData.empty();
52  }
53  auto size() const {
54  return mData.size();
55  }
56  // Modifiers
57  void clear() {
58  mData.clear();
59  }
60  template<typename P>
61  auto insert(const P& pair) {
62  resetCaches();
63  return mData.insert(pair);
64  }
65  template<typename P>
66  auto insert(typename Map::const_iterator it, const P& pair) {
67  resetCaches();
68  return mData.insert(it, pair);
69  }
70  template<typename... Args>
71  auto emplace(const key_type& key, Args&& ...args) {
72  resetCaches();
73  return mData.emplace(key,std::forward<Args>(args)...);
74  }
75  template<typename... Args>
76  auto emplace_hint(typename Map::const_iterator it, const key_type& key, Args&& ...args) {
77  resetCaches();
78  return mData.emplace_hint(it, key,std::forward<Args>(args)...);
79  }
80  typename Map::iterator erase(const ModelVariable& variable) {
81  resetCaches();
82  return erase(mData.find(variable));
83  }
84  typename Map::iterator erase(const typename Map::iterator& it) {
85  resetCaches();
86  return erase(typename Map::const_iterator(it));
87  }
88  typename Map::iterator erase(const typename Map::const_iterator& it) {
89  if (it == mData.end()) return mData.end();
90  return mData.erase(it);
91  }
92  void clean() {
93  for (auto& m: mData) {
94  const auto& val = m.second;
95  if (!val.isSubstitution()) continue;
96  const auto& subs = val.asSubstitution();
97  CARL_LOG_DEBUG("carl.formula.model", "Evaluating " << m.first << " -> " << subs << " as.");
98  m.second = subs->evaluate(*this);
99  }
100  }
101  // Lookup
102  auto find(const typename Map::key_type& key) const {
103  return mData.find(key);
104  }
105  auto find(const typename Map::key_type& key) {
106  return mData.find(key);
107  }
108 
109  // Additional (w.r.t. std::map)
110  Model() = default;
111  Model(const std::map<Variable, Rational>& assignment) {
112  for (const auto& a: assignment) {
113  mData.emplace(a.first, a.second);
114  }
115  }
116  template<typename Container>
117  bool contains(const Container& c) const {
118  for (const auto& var: c) {
119  if (mData.find(var) == mData.end()) return false;
120  }
121  return true;
122  }
123  template<typename T>
124  void assign(const typename Map::key_type& key, const T& t) {
125  auto it = mData.find(key);
126  if (it == mData.end()) mData.emplace(key, t);
127  else it->second = t;
128  }
129  void update(const Model& model, bool disjoint = true) {
130  for (const auto& m: model) {
131  auto res = mData.insert(m);
132  if (disjoint) {
133  assert(res.second);
134  } else {
135  if (!res.second) {
136  res.first->second = m.second;
137  }
138  }
139  }
140  }
141  /**
142  * Return the ModelValue for the given key, evaluated if it's a ModelSubstitution and evaluatable,
143  * otherwise return it raw.
144  * @param key The model must contain an assignment with the given key.
145  */
146  const ModelValue<Rational,Poly>& evaluated(const typename Map::key_type& key) const {
147  const auto& it = at(key);
148  if (it.isSubstitution()) return it.asSubstitution()->evaluate(*this);
149  else return it;
150  }
151  void print(std::ostream& os, bool simple = true) const {
152  os << "(model" << std::endl;
153  for (const auto& ass: mData) {
154  auto value = ass.second;
155  if (simple) value = evaluated(ass.first);
156 
157  if (ass.first.is_variable()) {
158  os << "\t(define-fun " << ass.first << " () " << ass.first.asVariable().type() << std::endl;
159  os << "\t\t" << value << ")" << std::endl;
160  } else if (ass.first.isBVVariable()) {
161  os << "\t(define-fun " << ass.first << " () " << ass.first.asBVVariable().sort() << std::endl;
162  os << "\t\t" << value << ")" << std::endl;
163  } else if (ass.first.isUVariable()) {
164  os << "\t(define-fun " << ass.first << " () " << ass.first.asUVariable().domain() << std::endl;
165  os << "\t\t" << value << ")" << std::endl;
166  } else if (ass.first.isFunction()) {
167  os << value;
168  } else {
169  CARL_LOG_ERROR("carl.model", "Encountered an unknown type of ModelVariable: " << ass.first);
170  assert(false);
171  }
172  }
173  os << ")" << std::endl;
174  }
175  void printOneline(std::ostream& os, bool simple = false) const {
176  os << "{";
177  bool first = true;
178  for (const auto& ass: mData) {
179  if (!first) os << ", ";
180  auto value = ass.second;
181  if (simple) value = evaluated(ass.first);
182 
183  if (ass.first.is_variable()) {
184  os << ass.first << " = " << value;
185  } else if (ass.first.isBVVariable()) {
186  os << ass.first << " = " << ass.first.asBVVariable().sort() << "(" << value << ")";
187  } else if (ass.first.isUVariable()) {
188  os << ass.first << " = " << ass.first.asUVariable().domain() << "(" << value << ")";
189  } else if (ass.first.isFunction()) {
190  os << value;
191  } else {
192  CARL_LOG_ERROR("carl.model", "Encountered an unknown type of ModelVariable: " << ass.first);
193  assert(false);
194  }
195  first = false;
196  }
197  os << "}";
198  }
199  };
200 
201  template<typename Rational, typename Poly>
202  std::ostream& operator<<(std::ostream& os, const Model<Rational,Poly>& model) {
203  model.printOneline(os);
204  return os;
205  }
206 }
207 
208 #include "ModelSubstitution.h"
A small wrapper that configures logging for carl.
#define CARL_LOG_ERROR(channel, msg)
Definition: carl-logging.h:40
#define CARL_LOG_DEBUG(channel, msg)
Definition: carl-logging.h:43
carl is the main namespace for the library.
std::ostream & operator<<(std::ostream &os, const BasicConstraint< Poly > &c)
Prints the given constraint on the given stream.
Represent a collection of assignments/mappings from variables to values.
Definition: Model.h:19
Map mData
Definition: Model.h:24
std::map< key_type, std::size_t > mUsedInSubstitution
Definition: Model.h:28
void clean()
Definition: Model.h:92
auto find(const typename Map::key_type &key) const
Definition: Model.h:102
Map::iterator erase(const typename Map::iterator &it)
Definition: Model.h:84
Map::iterator erase(const typename Map::const_iterator &it)
Definition: Model.h:88
void update(const Model &model, bool disjoint=true)
Definition: Model.h:129
std::map< key_type, mapped_type > Map
Definition: Model.h:23
void printOneline(std::ostream &os, bool simple=false) const
Definition: Model.h:175
Model(const std::map< Variable, Rational > &assignment)
Definition: Model.h:111
auto empty() const
Definition: Model.h:50
auto emplace_hint(typename Map::const_iterator it, const key_type &key, Args &&...args)
Definition: Model.h:76
void print(std::ostream &os, bool simple=true) const
Definition: Model.h:151
auto emplace(const key_type &key, Args &&...args)
Definition: Model.h:71
void resetCaches() const
Definition: Model.h:29
auto insert(const P &pair)
Definition: Model.h:61
auto begin() const
Definition: Model.h:43
auto end() const
Definition: Model.h:46
auto size() const
Definition: Model.h:53
Model()=default
void assign(const typename Map::key_type &key, const T &t)
Definition: Model.h:124
void clear()
Definition: Model.h:57
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
auto insert(typename Map::const_iterator it, const P &pair)
Definition: Model.h:66
Map::iterator erase(const ModelVariable &variable)
Definition: Model.h:80
auto find(const typename Map::key_type &key)
Definition: Model.h:105
const auto & at(const key_type &key) const
Definition: Model.h:38
Represent a sum type/variant over the different kinds of values that can be assigned to the different...
Definition: ModelValue.h:56
Represent a sum type/variant over the different kinds of variables that exist in CARL to use them in ...
Definition: ModelVariable.h:20