SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
Conversions.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 #include <boost/variant.hpp>
5 
6 #include "Common.h"
7 
8 #include "TheoryTypes.h"
9 
10 namespace smtrat {
11 namespace parser {
12 namespace conversion {
13 
14 template<typename To>
15 struct Converter {
16  template<typename From>
17  bool operator()(const From&, To&) const {
18  return false;
19  }
20  bool operator()(const To& from, To& to) const {
21  to = from;
22  return true;
23  }
24 };
25 
26 template<>
27 struct Converter<types::BVTerm> {
28  template<typename From>
29  bool operator()(const From&, types::BVTerm&) const {
30  return false;
31  }
32  bool operator()(const types::BVVariable& from, types::BVTerm& to) const {
33  to = types::BVTerm(carl::BVTermType::VARIABLE, from);
34  return true;
35  }
36  bool operator()(const types::BVTerm& from, types::BVTerm& to) const {
37  to = from;
38  return true;
39  }
40  bool operator()(const FixedWidthConstant<Integer>& from, types::BVTerm& to) const {
41  carl::BVValue value(from.width);
42  Integer v = from.value;
43 
44  assert(v >= 0);
45  for (std::size_t i = 0; v > 0; i++) {
46  if (carl::mod(v, 2) == 1) {
47  value[i] = true;
48  v = carl::div(v-1, Integer(2));
49  } else {
50  v = carl::div(v, Integer(2));
51  }
52  }
53  to = types::BVTerm(carl::BVTermType::CONSTANT, value);
54  return true;
55  }
56 };
57 
58 template<>
59 struct Converter<Poly> {
60  template<typename From>
61  bool operator()(const From&, Poly&) const {
62  return false;
63  }
64  bool operator()(const Poly& from, Poly& to) const {
65  to = from;
66  return true;
67  }
68  bool operator()(const carl::Variable& from, Poly& to) const {
69  to = Poly(from);
70  return true;
71  }
72  bool operator()(const Rational& from, Poly& to) const {
73  to = Poly(from);
74  return true;
75  }
76 };
77 
78 template<>
80  template<typename From>
81  bool operator()(const From&, FormulaT&) const {
82  return false;
83  }
84  bool operator()(const FormulaT& from, FormulaT& to) const {
85  to = from;
86  return true;
87  }
88  bool operator()(const carl::Variable& from, FormulaT& to) const {
89  if (from.type() != carl::VariableType::VT_BOOL) return false;
90  to = FormulaT(from);
91  return true;
92  }
93 };
94 
95 /**
96  * Converts variants to some type using the Converter class.
97  */
98 template<typename Res>
99 struct VariantConverter: public boost::static_visitor<> {
100  typedef bool result_type;
101  Res result;
103  template<typename T>
104  bool operator()(const T& t) {
105  return converter(t, result);
106  }
107  template<BOOST_VARIANT_ENUM_PARAMS(typename T)>
108  bool operator()(const boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>& t) {
109  return boost::apply_visitor(*this, t);
110  }
111  template<BOOST_VARIANT_ENUM_PARAMS(typename T)>
112  bool operator()(const boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>& t, Res& r) {
113  if ((*this)(t)) {
114  r = result;
115  return true;
116  }
117  return false;
118  }
119  template<typename T>
120  Res convert(const T& t) {
121  if ((*this)(t)) return result;
122  else {
123  SMTRAT_LOG_ERROR("smtrat.parser", "Failed to convert \"" << t << "\" to " << typeid(T).name() << ".");
124  return Res();
125  }
126  }
127 };
128 
129 /**
130  * Converts variants to another variant type not using the Converter class.
131  */
132 template<typename Res>
133 struct VariantVariantConverter: public boost::static_visitor<> {
134  typedef Res result_type;
135  template<typename T>
136  Res operator()(const T& t) {
137  return Res(t);
138  }
139  template<typename Variant>
140  Res convert(const Variant& t) {
141  return boost::apply_visitor(*this, t);
142  }
143 };
144 
145 
146 /**
147  * Converts a vector of variants to a vector of some type using the Converter class.
148  */
149 template<typename Res>
151  typedef Res result_type;
152  template<BOOST_VARIANT_ENUM_PARAMS(typename T)>
153  bool operator()(const std::vector<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>>& v, std::vector<Res>& result) const {
154  result.clear();
156  for (const auto& val: v) {
157  if (vc(val)) result.push_back(vc.result);
158  else return false;
159  }
160  return true;
161  }
162  template<BOOST_VARIANT_ENUM_PARAMS(typename T)>
163  bool operator()(const std::vector<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>>& v, std::vector<Res>& result, TheoryError& errors) const {
164  result.clear();
166  for (const auto& val: v) {
167  if (vc(val)) result.push_back(vc.result);
168  else {
169  errors.next() << "Failed to convert \"" << val << "\" to " << typeid(Res).name() << ".";
170  return false;
171  }
172  }
173  return true;
174  }
175 };
176 
177 }
178 }
179 }
Numeric mod(const Numeric &_valueA, const Numeric &_valueB)
Calculates the result of the first argument modulo the second argument.
Definition: Numeric.cpp:288
carl::BVVariable BVVariable
Definition: TheoryTypes.h:78
carl::BVTerm BVTerm
Typedef for bitvector term.
Definition: TheoryTypes.h:77
Class to create the formulas for axioms.
carl::Formula< Poly > FormulaT
Definition: types.h:37
carl::MultivariatePolynomial< Rational > Poly
Definition: types.h:25
mpq_class Rational
Definition: types.h:19
carl::IntegralType< Rational >::type Integer
Definition: types.h:21
#define SMTRAT_LOG_ERROR(channel, msg)
Definition: logging.h:32
Represents a constant of a fixed width.
Definition: TheoryTypes.h:38
TheoryError & next()
Definition: Common.h:25
bool operator()(const From &, FormulaT &) const
Definition: Conversions.h:81
bool operator()(const FormulaT &from, FormulaT &to) const
Definition: Conversions.h:84
bool operator()(const carl::Variable &from, FormulaT &to) const
Definition: Conversions.h:88
bool operator()(const Rational &from, Poly &to) const
Definition: Conversions.h:72
bool operator()(const Poly &from, Poly &to) const
Definition: Conversions.h:64
bool operator()(const From &, Poly &) const
Definition: Conversions.h:61
bool operator()(const carl::Variable &from, Poly &to) const
Definition: Conversions.h:68
bool operator()(const types::BVVariable &from, types::BVTerm &to) const
Definition: Conversions.h:32
bool operator()(const FixedWidthConstant< Integer > &from, types::BVTerm &to) const
Definition: Conversions.h:40
bool operator()(const types::BVTerm &from, types::BVTerm &to) const
Definition: Conversions.h:36
bool operator()(const From &, types::BVTerm &) const
Definition: Conversions.h:29
bool operator()(const To &from, To &to) const
Definition: Conversions.h:20
bool operator()(const From &, To &) const
Definition: Conversions.h:17
Converts variants to some type using the Converter class.
Definition: Conversions.h:99
bool operator()(const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T)> &t, Res &r)
Definition: Conversions.h:112
bool operator()(const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T)> &t)
Definition: Conversions.h:108
Converts variants to another variant type not using the Converter class.
Definition: Conversions.h:133
Converts a vector of variants to a vector of some type using the Converter class.
Definition: Conversions.h:150
bool operator()(const std::vector< boost::variant< BOOST_VARIANT_ENUM_PARAMS(T)>> &v, std::vector< Res > &result, TheoryError &errors) const
Definition: Conversions.h:163
bool operator()(const std::vector< boost::variant< BOOST_VARIANT_ENUM_PARAMS(T)>> &v, std::vector< Res > &result) const
Definition: Conversions.h:153