carl  24.04
Computer ARithmetic Library
SortManager.h
Go to the documentation of this file.
1 /**
2  * @file SortManager.h
3  * @author Gereon Kremer <gereon.kremer@cs.rwth-aachen.de>
4  * @author Florian Corzilius <corzilius@cs.rwth-aachen.de>
5  * @since 2014-10-30
6  * @version 2014-10-30
7  */
8 
9 #pragma once
10 
13 #include "Sort.h"
15 
16 #include <cassert>
17 #include <ostream>
18 #include <map>
19 #include <memory>
20 #include <utility>
21 #include <vector>
22 
23 namespace carl {
24 
25 /// The actual content of a sort.
26 struct SortContent {
27  /// The sort's name.
28  std::string name;
29  /// The sort's argument types. It is nullptr, if the sort's arity is zero.
30  std::unique_ptr<std::vector<Sort>> parameters;
31  /// The sort's indices. A sort can be indexed with the "_" operator. It is nullptr, if no indices are present.
32  std::unique_ptr<std::vector<std::size_t>> indices;
33 
34  SortContent() = delete; // The default constructor is disabled.
35 
36  /**
37  * Constructs a sort content.
38  * @param _name The name of the sort content to construct.
39  */
40  explicit SortContent(std::string _name) noexcept
41  : name(std::move(_name))
42  {}
43 
44  /**
45  * Constructs a sort content.
46  * @param _name The name of the sort content to construct.
47  * @param _parameters The sorts of the arguments of the sort content to construct.
48  */
49  explicit SortContent(std::string _name, const std::vector<Sort>& _parameters)
50  : name(std::move(_name)),
51  parameters(std::make_unique<std::vector<Sort>>(_parameters))
52  {}
53  explicit SortContent(std::string _name, std::vector<Sort>&& _parameters)
54  : name(std::move(_name)),
55  parameters(std::make_unique<std::vector<Sort>>(std::move(_parameters)))
56  {}
57 
59  : name(sc.name)
60  {
61  if (sc.parameters) parameters = std::make_unique<std::vector<Sort>>(*sc.parameters);
62  if (sc.indices) indices = std::make_unique<std::vector<std::size_t>>(*sc.indices);
63  }
64 
65  /// Destructs a sort content.
66  ~SortContent() noexcept = default;
67 
68  SortContent& operator=(const SortContent& sc) = delete;
69  SortContent(SortContent&& sc) noexcept = default;
70  SortContent& operator=(SortContent&& sc) = default;
71 
72  /**
73  * Return a copy of this SortContent without any indices.
74  */
76  if (parameters == nullptr) return SortContent(name);
77  return SortContent(name, *parameters);
78  }
79 };
80 
81 /**
82  * @param lhs Left SortContent
83  * @param rhs Right SortContent
84  * @return `lhs < rhs`
85  */
86 inline bool operator<(const SortContent& lhs, const SortContent& rhs) {
87  if (lhs.name != rhs.name) return lhs.name < rhs.name;
88 
89  if (!lhs.parameters && rhs.parameters) return true;
90  if (lhs.parameters && !rhs.parameters) return false;
91  if (lhs.parameters && rhs.parameters) {
92  if (*lhs.parameters != *rhs.parameters) return *lhs.parameters < *rhs.parameters;
93  }
94  if (!lhs.indices && rhs.indices) return true;
95  if (lhs.indices && !rhs.indices) return false;
96  if (lhs.indices && rhs.indices) {
97  if (*lhs.indices != *rhs.indices) return *lhs.indices < *rhs.indices;
98  }
99  return false;
100 }
101 
102 /**
103  * Implements a manager for sorts, containing the actual contents of these sort and allocating their ids.
104  */
105 class SortManager : public Singleton<SortManager> {
107 
108 public:
109  /// The type of a sort template, define by define-sort.
110  using SortTemplate = std::pair<std::vector<std::string>, Sort>;
111 
112 private:
113  // Members.
114 
115  /// Maps the unique ids to the sort content.
116  std::vector<std::unique_ptr<SortContent>> mSorts;
117  /// Maps the unique ids to the sort types.
118  std::vector<VariableType> mSortTypes;
119  /// Maps the sort contents to unique ids.
120  std::map<const SortContent*, std::size_t, carl::less<const SortContent*>> mSortMap;
121  /// Stores all sort declarations invoked by a declare-sort.
122  std::map<std::string, std::size_t> mDeclarations;
123  /// Stores all sort definitions invoked by a define-sort.
124  std::map<std::string, SortTemplate> mDefinitions;
125  /// Stores all sorts that may become interpreted when indexed.
126  std::map<Sort, std::pair<std::size_t, VariableType>> mIndexable;
127  /// Maps variable types to actual sorts.
128  std::map<VariableType, Sort> mInterpreted;
129 
130  /**
131  * Constructs a sort manager.
132  */
134  clear();
135  }
136 
137  const SortContent& getContent(const Sort& sort) const {
138  assert(sort.id() > 0);
139  assert(sort.id() < mSorts.size());
140  return *mSorts.at(sort.id());
141  }
142  bool isSymbolFree(const std::string& name) const {
143  for (const auto& s : mSorts) {
144  if (s == nullptr) continue;
145  if (s->name == name) return false;
146  }
147  if (mDeclarations.find(name) != mDeclarations.end()) return false;
148  if (mDefinitions.find(name) != mDefinitions.end()) return false;
149  return true;
150  }
151  Sort getSort(std::unique_ptr<SortContent>&& content, VariableType type) {
152  auto it = mSortMap.find(content.get());
153  if (it != mSortMap.end()) {
154  return Sort(it->second);
155  }
156  return Sort(addSortContent(std::move(content), type));
157  }
158 
159  std::size_t addSortContent(std::unique_ptr<SortContent>&& content, VariableType type) {
160  mSorts.emplace_back(std::move(content));
161  mSortTypes.push_back(type);
162  mSortMap[mSorts.back().get()] = mSorts.size() - 1;
163  return mSorts.size() - 1;
164  }
165 
166  VariableType checkIndices(const SortContent& content, std::size_t count) const {
167  auto sortIt = mSortMap.find(&content);
168  if (sortIt == mSortMap.end()) return VariableType::VT_UNINTERPRETED;
169  Sort baseSort(sortIt->second);
170  auto it = mIndexable.find(baseSort);
171  if (it == mIndexable.end()) return VariableType::VT_UNINTERPRETED;
172  if (it->second.first != count) return VariableType::VT_UNINTERPRETED;
173  return it->second.second;
174  }
175 
176 public:
177  SortManager(const SortManager&) = delete;
179  SortManager& operator=(const SortManager&) = delete;
181  ~SortManager() noexcept override = default;
182 
183  void clear() {
184  mSorts.clear();
185  mSortTypes.clear();
186  mSortMap.clear();
187  mDeclarations.clear();
188  mDefinitions.clear();
189  mIndexable.clear();
190  mInterpreted.clear();
191  mSorts.emplace_back(nullptr); // default value
193  }
194 
195  /**
196  * @param sort A sort.
197  * @return The name if the given sort.
198  */
199  const std::string& get_name(const Sort& sort) const {
200  return getContent(sort).name;
201  }
202  const std::vector<Sort>* getParameters(const Sort& sort) const {
203  return getContent(sort).parameters.get();
204  }
205  const std::vector<std::size_t>* getIndices(const Sort& sort) const {
206  return getContent(sort).indices.get();
207  }
208  VariableType getType(const Sort& sort) const {
209  assert(sort.id() > 0);
210  assert(sort.id() < mSortTypes.size());
211  return mSortTypes.at(sort.id());
212  }
213 
214  /**
215  * Prints the given sort on the given output stream.
216  * @param os The output stream to print the given sort on.
217  * @param sort The sort to print.
218  * @return The output stream after printing the given sort on it.
219  */
220  std::ostream& print(std::ostream& os, const Sort& sort) const;
221 
222  void exportDefinitions(std::ostream& os) const;
223 
225  assert(mInterpreted.find(type) != mInterpreted.end());
226  return mInterpreted.at(type);
227  }
228 
229  /**
230  * Recursively replaces sorts within the given sort according to the mapping of sort names to sorts as declared by the given map.
231  * @param sort The sort to replace sorts by sorts in.
232  * @param parameters The map of sort names to sorts.
233  * @return The resulting sort.
234  */
235  Sort replace(const Sort& sort, const std::map<std::string, Sort>& parameters);
236 
237  /**
238  * Adds a sort declaration.
239  * @param name The name of the declared sort.
240  * @param arity The arity of the declared sort.
241  * @return true, if the given sort declaration has not been added before;
242  * false, otherwise.
243  */
244  bool declare(const std::string& name, std::size_t arity);
245 
246  /**
247  * Adds a sort template definitions.
248  * @param name The name of the defined sort template.
249  * @param params The template parameter of the defined sort.
250  * @param sort The sort to instantiate into.
251  * @return true, if the given sort template definition has not been added before;
252  * false, otherwise.
253  */
254  bool define(const std::string& name, const std::vector<std::string>& params, const Sort& sort);
255 
256  /**
257  * @param sort The sort to get the arity for.
258  * @return The arity of the given sort.
259  */
260  std::size_t getArity(const Sort& sort) const;
261 
263  assert(mInterpreted.find(type) == mInterpreted.end());
264  mInterpreted[type] = sort;
265  return sort;
266  }
267  Sort addInterpretedSort(const std::string& name, VariableType type) {
268  return addInterpretedMapping(addSort(name, type), type);
269  }
270  Sort addInterpretedSort(const std::string& name, const std::vector<Sort>& parameters, VariableType type) {
271  return addInterpretedMapping(addSort(name, parameters, type), type);
272  }
273  Sort addSort(const std::string& name, VariableType type = VariableType::VT_UNINTERPRETED);
274  Sort addSort(const std::string& name, const std::vector<Sort>& parameters, VariableType type = VariableType::VT_UNINTERPRETED);
275  void makeSortIndexable(const Sort& sort, std::size_t indices, VariableType type);
276 
277  /**
278  * @param sort A sort.
279  * @return true, if the given sort is interpreted.
280  */
281  bool isInterpreted(const Sort& sort) const {
282  return getType(sort) != VariableType::VT_UNINTERPRETED;
283  }
284 
285  Sort index(const Sort& sort, const std::vector<std::size_t>& indices);
286 
287  /**
288  * Gets the sort with arity zero (thus it is maybe interpreted) corresponding the given name.
289  * @param name The name of the sort to get.
290  * @return The resulting sort.
291  */
292  Sort getSort(const std::string& name);
293 
294  /**
295  * Gets the sort with arity greater than zero corresponding the given name and having the arguments
296  * of the given sorts.
297  * @param name The name of the sort to get.
298  * @param params The sort of the arguments of the sort to get.
299  * @return The resulting sort.
300  */
301  Sort getSort(const std::string& name, const std::vector<Sort>& params);
302 
303  Sort getSort(const std::string& name, const std::vector<std::size_t>& indices);
304 
305  Sort getSort(const std::string& name, const std::vector<std::size_t>& indices, const std::vector<Sort>& params);
306 };
307 
308 /**
309  * Gets the sort specified by the arguments.
310  * Forwards to SortManager::getSort().
311  */
312 template<typename... Args>
313 inline Sort getSort(Args&&... args) {
314  return SortManager::getInstance().getSort(std::forward<Args>(args)...);
315 }
316 
317 } // namespace carl
carl is the main namespace for the library.
Coeff content(const UnivariatePolynomial< Coeff > &p)
The content of a polynomial is the gcd of the coefficients of the normal part of a polynomial.
Definition: Content.h:22
bool operator<(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
Sort getSort(Args &&... args)
Gets the sort specified by the arguments.
Definition: SortManager.h:313
VariableType
Several types of variables are supported.
Definition: Variable.h:28
Base class that implements a singleton.
Definition: Singleton.h:24
static SortManager & getInstance()
Returns the single instance of this class by reference.
Definition: Singleton.h:45
Implements a sort (for defining types of variables and functions).
Definition: Sort.h:20
std::size_t id() const
Definition: Sort.h:48
The actual content of a sort.
Definition: SortManager.h:26
std::unique_ptr< std::vector< std::size_t > > indices
The sort's indices. A sort can be indexed with the "_" operator. It is nullptr, if no indices are pre...
Definition: SortManager.h:32
SortContent()=delete
SortContent(std::string _name) noexcept
Constructs a sort content.
Definition: SortManager.h:40
SortContent(std::string _name, std::vector< Sort > &&_parameters)
Definition: SortManager.h:53
SortContent getUnindexed() const
Return a copy of this SortContent without any indices.
Definition: SortManager.h:75
std::string name
The sort's name.
Definition: SortManager.h:28
std::unique_ptr< std::vector< Sort > > parameters
The sort's argument types. It is nullptr, if the sort's arity is zero.
Definition: SortManager.h:30
SortContent(std::string _name, const std::vector< Sort > &_parameters)
Constructs a sort content.
Definition: SortManager.h:49
SortContent(const SortContent &sc)
Definition: SortManager.h:58
~SortContent() noexcept=default
Destructs a sort content.
Implements a manager for sorts, containing the actual contents of these sort and allocating their ids...
Definition: SortManager.h:105
bool declare(const std::string &name, std::size_t arity)
Adds a sort declaration.
Definition: SortManager.cpp:54
const std::vector< std::size_t > * getIndices(const Sort &sort) const
Definition: SortManager.h:205
Sort replace(const Sort &sort, const std::map< std::string, Sort > &parameters)
Recursively replaces sorts within the given sort according to the mapping of sort names to sorts as d...
Definition: SortManager.cpp:41
SortManager & operator=(SortManager &&)=delete
void makeSortIndexable(const Sort &sort, std::size_t indices, VariableType type)
Definition: SortManager.cpp:81
const std::string & get_name(const Sort &sort) const
Definition: SortManager.h:199
bool isInterpreted(const Sort &sort) const
Definition: SortManager.h:281
Sort index(const Sort &sort, const std::vector< std::size_t > &indices)
Definition: SortManager.cpp:85
SortManager(const SortManager &)=delete
bool isSymbolFree(const std::string &name) const
Definition: SortManager.h:142
std::pair< std::vector< std::string >, Sort > SortTemplate
The type of a sort template, define by define-sort.
Definition: SortManager.h:110
std::map< Sort, std::pair< std::size_t, VariableType > > mIndexable
Stores all sorts that may become interpreted when indexed.
Definition: SortManager.h:126
std::map< const SortContent *, std::size_t, carl::less< const SortContent * > > mSortMap
Maps the sort contents to unique ids.
Definition: SortManager.h:120
const SortContent & getContent(const Sort &sort) const
Definition: SortManager.h:137
SortManager(SortManager &&)=delete
void exportDefinitions(std::ostream &os) const
Definition: SortManager.cpp:31
Sort addSort(const std::string &name, VariableType type=VariableType::VT_UNINTERPRETED)
Definition: SortManager.cpp:73
Sort getSort(std::unique_ptr< SortContent > &&content, VariableType type)
Definition: SortManager.h:151
std::size_t getArity(const Sort &sort) const
Definition: SortManager.cpp:67
VariableType checkIndices(const SortContent &content, std::size_t count) const
Definition: SortManager.h:166
Sort getInterpreted(VariableType type) const
Definition: SortManager.h:224
Sort addInterpretedMapping(const Sort &sort, VariableType type)
Definition: SortManager.h:262
std::map< VariableType, Sort > mInterpreted
Maps variable types to actual sorts.
Definition: SortManager.h:128
std::map< std::string, SortTemplate > mDefinitions
Stores all sort definitions invoked by a define-sort.
Definition: SortManager.h:124
std::vector< VariableType > mSortTypes
Maps the unique ids to the sort types.
Definition: SortManager.h:118
Sort addInterpretedSort(const std::string &name, VariableType type)
Definition: SortManager.h:267
VariableType getType(const Sort &sort) const
Definition: SortManager.h:208
Sort addInterpretedSort(const std::string &name, const std::vector< Sort > &parameters, VariableType type)
Definition: SortManager.h:270
SortManager()
Constructs a sort manager.
Definition: SortManager.h:133
std::vector< std::unique_ptr< SortContent > > mSorts
Maps the unique ids to the sort content.
Definition: SortManager.h:116
bool define(const std::string &name, const std::vector< std::string > &params, const Sort &sort)
Adds a sort template definitions.
Definition: SortManager.cpp:61
std::ostream & print(std::ostream &os, const Sort &sort) const
Prints the given sort on the given output stream.
Definition: SortManager.cpp:13
~SortManager() noexcept override=default
std::size_t addSortContent(std::unique_ptr< SortContent > &&content, VariableType type)
Definition: SortManager.h:159
SortManager & operator=(const SortManager &)=delete
const std::vector< Sort > * getParameters(const Sort &sort) const
Definition: SortManager.h:202
std::map< std::string, std::size_t > mDeclarations
Stores all sort declarations invoked by a declare-sort.
Definition: SortManager.h:122