carl  24.04
Computer ARithmetic Library
SortManager.cpp
Go to the documentation of this file.
1 /**
2  * @file SortManager.cpp
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 #include "SortManager.h"
10 
11 namespace carl {
12 
13 std::ostream& SortManager::print(std::ostream& os, const Sort& sort) const {
14  const SortContent& sc = getContent(sort);
15  if (sc.indices != nullptr) os << "(_ ";
16  if (sc.parameters != nullptr) os << "(";
17  os << sc.name;
18  if (sc.parameters != nullptr) {
19  for (const Sort& s : *sc.parameters)
20  os << " " << s;
21  os << ")";
22  }
23  if (sc.indices != nullptr) {
24  for (std::size_t i : *sc.indices)
25  os << " " << i;
26  os << ")";
27  }
28  return os;
29 }
30 
31 void SortManager::exportDefinitions(std::ostream& os) const {
32  for (const auto& decl : mDeclarations) {
33  os << "(declare-sort " << decl.first << " " << decl.second << ")" << std::endl;
34  }
35  ///@todo fix this
36  //for (const auto& def: mDeclarations) {
37  // os << "(define-sort " << decl.first << " " << decl.second << ")" << std::endl;
38  //}
39 }
40 
41 Sort SortManager::replace(const Sort& sort, const std::map<std::string, Sort>& parameters) {
42  const SortContent& sc = getContent(sort);
43  auto pIter = parameters.find(sc.name);
44  if (pIter != parameters.end()) return pIter->second;
45  if (sc.parameters == nullptr) return sort;
46 
47  std::vector<Sort> v;
48  v.reserve(sc.parameters->size());
49  for (const auto& sd : *sc.parameters)
50  v.push_back(replace(sd, parameters));
51  return getSort(std::make_unique<SortContent>(sc.name, std::move(v)), VariableType::VT_UNINTERPRETED);
52 }
53 
54 bool SortManager::declare(const std::string& name, std::size_t arity) {
55  if (!isSymbolFree(name)) return false;
56  mDeclarations[name] = arity;
57  if (arity == 0) addSortContent(std::make_unique<SortContent>(name), VariableType::VT_UNINTERPRETED);
58  return true;
59 }
60 
61 bool SortManager::define(const std::string& name, const std::vector<std::string>& params, const Sort& sort) {
62  if (!isSymbolFree(name)) return false;
63  mDefinitions[name] = SortTemplate(params, sort);
64  return true;
65 }
66 
67 size_t SortManager::getArity(const Sort& sort) const {
68  const SortContent& sc = getContent(sort);
69  if (sc.parameters == nullptr) return 0;
70  return sc.parameters->size();
71 }
72 
73 Sort SortManager::addSort(const std::string& name, VariableType type) {
74  assert(isSymbolFree(name));
75  return Sort(addSortContent(std::make_unique<SortContent>(name), type));
76 }
77 Sort SortManager::addSort(const std::string& name, const std::vector<Sort>& parameters, VariableType type) {
78  assert(isSymbolFree(name));
79  return Sort(addSortContent(std::make_unique<SortContent>(name, parameters), type));
80 }
81 void SortManager::makeSortIndexable(const Sort& sort, std::size_t indices, VariableType type) {
82  mIndexable[sort] = std::make_pair(indices, type);
83 }
84 
85 Sort SortManager::index(const Sort& sort, const std::vector<std::size_t>& indices) {
86  if (indices.empty()) return sort;
87  const SortContent& sc = getContent(sort);
88  auto newsc = std::make_unique<SortContent>(sc);
89  if (newsc->indices == nullptr)
90  newsc->indices = std::make_unique<std::vector<std::size_t>>(indices);
91  else
92  newsc->indices->insert(newsc->indices->end(), indices.begin(), indices.end());
93  return getSort(std::move(newsc), checkIndices(sc.getUnindexed(), newsc->indices->size()));
94 }
95 
96 Sort SortManager::getSort(const std::string& name) {
97  auto sc = std::make_unique<SortContent>(name);
98  // Find an instantiation of the given sort template.
99  auto it = mSortMap.find(sc.get());
100  if (it == mSortMap.end()) {
101  CARL_LOG_ERROR("carl.formula", "The sort " << name << " has not been declared or defined.");
102  return Sort(0);
103  }
104  return Sort(it->second);
105 }
106 
107 Sort SortManager::getSort(const std::string& name, const std::vector<Sort>& params) {
108  assert(!params.empty());
109  auto decl = mDeclarations.find(name);
110  if (decl != mDeclarations.end()) {
111  std::size_t arity = decl->second;
112  if (arity != params.size()) {
113  CARL_LOG_ERROR("carl.formula", "The sort " << name << " was declared to have " << arity << " parameters, but " << params.size() << " were given.");
114  return Sort(0);
115  }
116  auto sc = std::make_unique<SortContent>(name, params);
117  return getSort(std::move(sc), VariableType::VT_UNINTERPRETED);
118  }
119  auto def = mDefinitions.find(name);
120  if (def != mDefinitions.end()) {
121  const SortTemplate& st = def->second;
122  if (st.first.size() != params.size()) {
123  CARL_LOG_ERROR("carl.formula", "The sort " << name << " was defined to have " << st.first.size() << " parameters, but " << params.size() << " were given.");
124  return Sort(0);
125  }
126  std::map<std::string, Sort> repl;
127  for (std::size_t i = 0; i < params.size(); i++)
128  repl[st.first[i]] = params[i];
129  return replace(st.second, repl);
130  }
131  CARL_LOG_ERROR("carl.formula", "The sort " << name << " was neither declared nor defined and thus cannot be instantiated.");
132  return Sort(0);
133 }
134 
135 Sort SortManager::getSort(const std::string& name, const std::vector<std::size_t>& indices) {
136  return index(getSort(name), indices);
137 }
138 
139 Sort SortManager::getSort(const std::string& name, const std::vector<std::size_t>& indices, const std::vector<Sort>& params) {
140  return index(getSort(name, params), indices);
141 }
142 } // namespace carl
#define CARL_LOG_ERROR(channel, msg)
Definition: carl-logging.h:40
carl is the main namespace for the library.
VariableType
Several types of variables are supported.
Definition: Variable.h:28
Implements a sort (for defining types of variables and functions).
Definition: Sort.h:20
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 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
bool declare(const std::string &name, std::size_t arity)
Adds a sort declaration.
Definition: SortManager.cpp:54
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
void makeSortIndexable(const Sort &sort, std::size_t indices, VariableType type)
Definition: SortManager.cpp:81
Sort index(const Sort &sort, const std::vector< std::size_t > &indices)
Definition: SortManager.cpp:85
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
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
std::map< std::string, SortTemplate > mDefinitions
Stores all sort definitions invoked by a define-sort.
Definition: SortManager.h:124
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
std::size_t addSortContent(std::unique_ptr< SortContent > &&content, VariableType type)
Definition: SortManager.h:159
std::map< std::string, std::size_t > mDeclarations
Stores all sort declarations invoked by a declare-sort.
Definition: SortManager.h:122