SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
Core.cpp
Go to the documentation of this file.
1 #include "Core.h"
2 #include "ParserState.h"
3 
4 #include <boost/algorithm/string/predicate.hpp>
5 
6 namespace smtrat {
7 namespace parser {
8 namespace core {
9  inline bool convertTerm(const types::TermType& term, FormulaT& result) {
10  if (boost::get<FormulaT>(&term) != nullptr) {
11  result = boost::get<FormulaT>(term);
12  return true;
13  } else if (boost::get<carl::Variable>(&term) != nullptr) {
14  if (boost::get<carl::Variable>(term).type() == carl::VariableType::VT_BOOL) {
15  result = FormulaT(boost::get<carl::Variable>(term));
16  return true;
17  } else {
18  return false;
19  }
20  } else {
21  return false;
22  }
23  }
24 
25  inline bool convertArguments(const std::vector<types::TermType>& arguments, std::vector<FormulaT>& result, TheoryError& errors) {
26  result.clear();
27  for (std::size_t i = 0; i < arguments.size(); i++) {
28  FormulaT res;
29  if (!convertTerm(arguments[i], res)) {
30  errors.next() << "Arguments are expected to be formulas, but argument " << (i+1) << " is not: \"" << arguments[i] << "\".";
31  return false;
32  }
33  result.push_back(res);
34  }
35  return true;
36  }
37 }
38 
40  bool operator()(const std::vector<types::TermType>& arguments, types::TermType& result, TheoryError& errors) const {
41  std::vector<FormulaT> args;
42  if (!convert(arguments, args, errors)) return false;
43  return apply(args, result, errors);
44  }
45  virtual bool apply(const std::vector<FormulaT>& arguments, types::TermType& result, TheoryError& errors) const = 0;
46  };
47  template<carl::FormulaType type>
49  bool apply(const std::vector<FormulaT>& arguments, types::TermType& result, TheoryError& ) const {
50  result = FormulaT(type, arguments);
51  return true;
52  }
53  };
55  bool apply(const std::vector<FormulaT>& arguments, types::TermType& result, TheoryError& errors) const {
56  if (arguments.size() != 1) {
57  errors.next() << "Operator \"not\" may only have a single argument.";
58  return false;
59  }
60  result = FormulaT(carl::FormulaType::NOT, arguments[0]);
61  return true;
62  }
63  };
65  bool apply(const std::vector<FormulaT>& arguments, types::TermType& result, TheoryError& errors) const {
66  if (arguments.size() != 2) {
67  errors.next() << "Operator \"implies\" may only have a single argument.";
68  return false;
69  }
70  result = FormulaT(carl::FormulaType::IMPLIES, {arguments[0], arguments[1]});
71  return true;
72  }
73  };
74 
75  void CoreTheory::addSimpleSorts(qi::symbols<char, carl::Sort>& sorts) {
76  carl::SortManager& sm = carl::SortManager::getInstance();
77  sorts.add("Bool", sm.getInterpreted(carl::VariableType::VT_BOOL));
78  }
79  void CoreTheory::addConstants(qi::symbols<char, types::ConstType>& constants) {
80  constants.add("true", types::ConstType(FormulaT(carl::FormulaType::TRUE)));
81  constants.add("false", types::ConstType(FormulaT(carl::FormulaType::FALSE)));
82  }
83 
85  carl::SortManager& sm = carl::SortManager::getInstance();
86  sm.addInterpretedSort("Bool", carl::VariableType::VT_BOOL);
87 
95  }
96 
97  bool CoreTheory::declareVariable(const std::string& name, const carl::Sort& sort, types::VariableType& result, TheoryError& errors) {
98  carl::SortManager& sm = carl::SortManager::getInstance();
99  switch (sm.getType(sort)) {
100  case carl::VariableType::VT_BOOL: {
101  assert(state->isSymbolFree(name));
102  carl::Variable var = carl::fresh_variable(name, carl::VariableType::VT_BOOL);
103  state->variables[name] = var;
104  result = var;
105  return true;
106  }
107  default:
108  errors.next() << "The requested sort was not \"Bool\" but \"" << sort << "\".";
109  return false;
110  }
111  }
112 
113  bool CoreTheory::handleITE(const FormulaT& ifterm, const types::TermType& thenterm, const types::TermType& elseterm, types::TermType& result, TheoryError& errors) {
114  FormulaT thenf;
115  FormulaT elsef;
116  if (!core::convertTerm(thenterm, thenf)) {
117  errors.next() << "Failed to construct ITE, the then-term \"" << thenterm << "\" is unsupported.";
118  return false;
119  }
120  if (!core::convertTerm(elseterm, elsef)) {
121  errors.next() << "Failed to construct ITE, the else-term \"" << elseterm << "\" is unsupported.";
122  return false;
123  }
124  result = FormulaT(carl::FormulaType::ITE, {ifterm, thenf, elsef});
125  return true;
126  }
127  bool CoreTheory::handleDistinct(const std::vector<types::TermType>& arguments, types::TermType& result, TheoryError& errors) {
128  std::vector<FormulaT> args;
129  if (!core::convertArguments(arguments, args, errors)) return false;
130  result = expandDistinct(args, [](const FormulaT& a, const FormulaT& b){
131  return FormulaT(carl::FormulaType::XOR, {a, b});
132  });
133  return true;
134  }
135 
136  bool CoreTheory::functionCall(const Identifier& identifier, const std::vector<types::TermType>& arguments, types::TermType& result, TheoryError& errors) {
137  std::vector<FormulaT> args;
138  if (!core::convertArguments(arguments, args, errors)) return false;
139 
140  if (boost::iequals(identifier.symbol, "=")) {
141  result = FormulaT(carl::FormulaType::IFF, FormulasT(args.begin(), args.end()));
142  return true;
143  }
144  return false;
145  }
146 
147 }
148 }
void sort(T *array, int size, LessThan lt)
Definition: Sort.h:67
int var(Lit p)
Definition: SolverTypes.h:64
bool convertTerm(const types::TermType &term, FormulaT &result)
Definition: Core.cpp:9
bool convertArguments(const std::vector< types::TermType > &arguments, std::vector< FormulaT > &result, TheoryError &errors)
Definition: Core.cpp:25
carl::mpl_variant_of< TermTypes >::type TermType
Variant type for all terms.
Definition: TheoryTypes.h:160
carl::mpl_variant_of< ConstTypes >::type ConstType
Variant type for all constants.
Definition: TheoryTypes.h:118
carl::mpl_variant_of< VariableTypes >::type VariableType
Variant type for all variables.
Definition: TheoryTypes.h:132
QuantifierType type(const std::pair< QuantifierType, std::vector< carl::Variable >> &p)
Definition: QEQuery.h:39
Class to create the formulas for axioms.
carl::Formula< Poly > FormulaT
Definition: types.h:37
carl::Formulas< Poly > FormulasT
Definition: types.h:39
Base class for all theories.
FormulaT expandDistinct(const std::vector< T > &values, const Builder &neqBuilder)
virtual bool apply(const std::vector< FormulaT > &arguments, types::TermType &result, TheoryError &errors) const =0
bool operator()(const std::vector< types::TermType > &arguments, types::TermType &result, TheoryError &errors) const
Definition: Core.cpp:40
static void addConstants(qi::symbols< char, types::ConstType > &constants)
Definition: Core.cpp:79
bool handleITE(const FormulaT &ifterm, const types::TermType &thenterm, const types::TermType &elseterm, types::TermType &result, TheoryError &errors)
Resolve an if-then-else operator.
Definition: Core.cpp:113
static void addSimpleSorts(qi::symbols< char, carl::Sort > &sorts)
Definition: Core.cpp:75
CoreTheory(ParserState *state)
Definition: Core.cpp:84
bool declareVariable(const std::string &name, const carl::Sort &sort, types::VariableType &result, TheoryError &errors)
Declare a new variable with the given name and the given sort.
Definition: Core.cpp:97
bool handleDistinct(const std::vector< types::TermType > &arguments, types::TermType &result, TheoryError &errors)
Resolve a distinct operator.
Definition: Core.cpp:127
bool functionCall(const Identifier &identifier, const std::vector< types::TermType > &arguments, types::TermType &result, TheoryError &errors)
Resolve another unknown function call.
Definition: Core.cpp:136
bool convert(const std::vector< types::TermType > &from, std::vector< T > &to) const
std::string symbol
Definition: Common.h:37
bool apply(const std::vector< FormulaT > &arguments, types::TermType &result, TheoryError &errors) const
Definition: Core.cpp:65
bool apply(const std::vector< FormulaT > &arguments, types::TermType &result, TheoryError &) const
Definition: Core.cpp:49
bool apply(const std::vector< FormulaT > &arguments, types::TermType &result, TheoryError &errors) const
Definition: Core.cpp:55
void registerFunction(const std::string &name, const FunctionInstantiator *fi)
Definition: ParserState.h:163
bool isSymbolFree(const std::string &name, bool output=true)
Definition: ParserState.h:129
std::map< std::string, types::VariableType > variables
Definition: ParserState.h:66
TheoryError & next()
Definition: Common.h:25