carl  26.08
Computer ARithmetic Library
QEPCADStream.h
Go to the documentation of this file.
1 #pragma once
2 
12 #include <carl-formula/sort/Sort.h>
13 
14 #include <iostream>
15 #include <sstream>
16 #include <type_traits>
17 
18 namespace carl::io {
19 
20 class QEPCADStream {
21 private:
22  std::stringstream mStream;
23 
24  void declare(Variable v) {
25  *this << "(E " << v << ") ";
26  }
27 
28  template<typename Pol>
29  void write(const Constraint<Pol>& c) {
30  *this << c.lhs() << " " << c.relation() << " 0";
31  }
32 
33  template<typename Pol>
34  void write(const Formulas<Pol>& f, const std::string& op) {
35  *this << carl::stream_joined(" " + op + " ", f);
36  }
37 
38  template<typename Pol>
39  void write(const Formula<Pol>& f) {
40  switch (f.type()) {
41  case FormulaType::AND:
42  write(f.subformulas(), "/\\");
43  break;
44  case FormulaType::OR:
45  write(f.subformulas(), "\\/");
46  break;
47  case FormulaType::IFF:
48  write(f.subformulas(), "<==>");
49  break;
50  case FormulaType::XOR:
51  assert(false);
52  break;
54  assert(f.subformulas().size() == 2);
55  write(f.subformulas(), "==>");
56  break;
57  case FormulaType::ITE:
58  assert(false);
59  case FormulaType::NOT:
60  *this << "~ " << f.subformula();
61  break;
62  case FormulaType::BOOL:
63  *this << f.boolean();
64  break;
66  *this << f.constraint();
67  break;
69  *this << f.variable_comparison();
70  break;
72  *this << f.variable_assignment();
73  break;
75  CARL_LOG_ERROR("carl.qepcadstream", "Bitvectors are not supported by QEPCAD.");
76  break;
77  case FormulaType::TRUE:
78  case FormulaType::FALSE:
79  *this << f.type();
80  break;
81  case FormulaType::UEQ:
82  CARL_LOG_ERROR("carl.qepcadstream", "Uninterpreted equalities are not supported by QEPCAD.");
83  break;
87  CARL_LOG_ERROR("carl.qepcadstream", "Printing exists or forall is not implemented yet.");
88  break;
89  }
90  }
91 
92  void write(const Monomial::Arg& m) {
93  if (m) *this << *m;
94  else *this << "1";
95  }
96  void write(const Monomial::Content::value_type& m) {
97  if (m.second == 0) *this << "1";
98  else if (m.second == 1) *this << m.first;
99  else {
100  for (std::size_t i = 0; i < m.second; ++i) *this << " " << m.first;
101  }
102  }
103  void write(const Monomial& m) {
104  if (m.exponents().empty()) *this << "1";
105  else if (m.exponents().size() == 1) *this << m.exponents().front();
106  else {
107  *this << " " << carl::stream_joined(" ", m.exponents());
108  }
109  }
110 
111  template<typename Coeff>
113  if (carl::is_zero(mp)) *this << "0";
114  else if (mp.nr_terms() == 1) *this << mp.lterm();
115  else {
116  for (auto it = mp.rbegin(); it != mp.rend(); ++it) {
117  if (it != mp.rbegin()) *this << " + ";
118  *this << *it;
119  }
120  }
121  }
122 
123  void write(Relation r) {
124  switch (r) {
125  case Relation::EQ: *this << "="; break;
126  case Relation::NEQ: *this << "/="; break;
127  case Relation::LESS: *this << "<"; break;
128  case Relation::LEQ: *this << "<="; break;
129  case Relation::GREATER: *this << ">"; break;
130  case Relation::GEQ: *this << ">="; break;
131  }
132  }
133 
134  template<typename Coeff>
135  void write(const Term<Coeff>& t) {
136  if (!t.monomial()) *this << "(" << t.coeff() << ")";
137  else {
138  if (carl::is_one(t.coeff())) {
139  *this << t.monomial();
140  } else {
141  *this << "(" << t.coeff() << ") " << t.monomial();
142  }
143  }
144  }
145 
146  template<typename Coeff>
148  if (up.is_constant()) *this << up.constant_part();
149  else {
150  for (std::size_t i = 0; i < up.coefficients().size(); ++i) {
151  if (i > 0) *this << " + ";
152  std::size_t exp = up.coefficients().size() - i - 1;
153  const auto& coeff = up.coefficients()[exp];
154  if (exp == 0) *this << " " << coeff;
155  else *this << "(" << coeff << ") " << Monomial(up.main_var(), exp);
156  }
157  }
158  }
159 
160  void write(const Variable& v) {
161  *this << v.name();
162  }
163  void write(const VariableType& vt) {
164  switch (vt) {
165  case VariableType::VT_BOOL: *this << "Bool"; break;
166  case VariableType::VT_REAL: *this << "Real"; break;
167  case VariableType::VT_INT: *this << "Int"; break;
168  case VariableType::VT_UNINTERPRETED: *this << "?_Uninterpreted"; break;
169  case VariableType::VT_BITVECTOR: *this << "?_Bitvector"; break;
170  default: *this << "?"; break;
171  }
172  }
173 
174  template<typename T>
175  void write(T&& t) {
176  mStream << t;
177  }
178 
179 public:
181  }
182 
183  void initialize(const carlVariables& vars) {
184  for (const auto& v: vars) {
185  declare(v);
186  }
187  }
188 
189  template<typename Pol>
190  void initialize(std::initializer_list<Formula<Pol>> formulas) {
191  carlVariables vars;
192  for (const auto& f: formulas) {
193  carl::variables(f,vars);
194  }
195  initialize(vars);
196  }
197 
198  template<typename Pol>
199  void assertFormula(const Formula<Pol>& formula) {
200  *this << formula;
201  }
202 
203  template<typename T>
205  write(static_cast<const std::decay_t<T>&>(t));
206  return *this;
207  }
208  //
209  QEPCADStream& operator<<(std::ostream& (*os)(std::ostream&)) {
210  write(os);
211  return *this;
212  }
213 
214  auto content() const {
215  return mStream.rdbuf();
216  }
217 };
218 
219 inline std::ostream& operator<<(std::ostream& os, const QEPCADStream& qs) {
220  return os << qs.content();
221 }
222 
223 }
#define CARL_LOG_ERROR(channel, msg)
Definition: carl-logging.h:40
@ CONSTRAINT
@ BITVECTOR
@ VARCOMPARE
@ AUX_EXISTS
@ VARASSIGN
std::vector< Formula< Poly > > Formulas
Interval< Number > exp(const Interval< Number > &i)
Definition: Exponential.h:10
bool is_zero(const Interval< Number > &i)
Check if this interval is a point-interval containing 0.
Definition: Interval.h:1453
VariableType
Several types of variables are supported.
Definition: Variable.h:28
@ GREATER
Definition: SignCondition.h:15
auto stream_joined(const std::string &glue, const T &v)
Allows to easily output some container with all elements separated by some string.
Relation
Definition: Relation.h:20
void variables(const BasicConstraint< Pol > &c, carlVariables &vars)
bool is_one(const Interval< Number > &i)
Check if this interval is a point-interval containing 1.
Definition: Interval.h:1462
std::ostream & operator<<(std::ostream &os, const MapleStream &ms)
Definition: MapleStream.h:198
A Variable represents an algebraic variable that can be used throughout carl.
Definition: Variable.h:85
std::string name() const
Retrieves the name of the variable.
Definition: Variable.cpp:8
This class represents a univariate polynomial with coefficients of an arbitrary type.
bool is_constant() const
Checks whether the polynomial is constant with respect to the main variable.
const std::vector< Coefficient > & coefficients() const &
Retrieves the coefficients defining this polynomial.
Variable main_var() const
Retrieves the main variable of this polynomial.
NumberType constant_part() const
Returns the constant part of this polynomial.
The general-purpose multivariate polynomial class.
const Term< Coeff > & lterm() const
The leading term.
std::size_t nr_terms() const
Calculate the number of terms.
The general-purpose monomials.
Definition: Monomial.h:59
std::shared_ptr< const Monomial > Arg
Definition: Monomial.h:62
const Content & exponents() const
Definition: Monomial.h:189
Coefficient & coeff()
Get the coefficient.
Definition: Term.h:80
Monomial::Arg & monomial()
Get the monomial.
Definition: Term.h:91
Represent a polynomial (in)equality against zero.
Definition: Constraint.h:62
Relation relation() const
Definition: Constraint.h:116
const Pol & lhs() const
Definition: Constraint.h:109
Represent an SMT formula, which can be an atom for some background theory or a boolean combination of...
Definition: Formula.h:47
const VariableAssignment< Pol > & variable_assignment() const
Definition: Formula.h:443
const Formula & subformula() const
Definition: Formula.h:335
const VariableComparison< Pol > & variable_comparison() const
Definition: Formula.h:438
carl::Variable::Arg boolean() const
Definition: Formula.h:458
const Constraint< Pol > & constraint() const
Definition: Formula.h:432
const Formulas< Pol > & subformulas() const
Definition: Formula.h:422
FormulaType type() const
Definition: Formula.h:262
void write(const Monomial::Arg &m)
Definition: QEPCADStream.h:92
QEPCADStream & operator<<(T &&t)
Definition: QEPCADStream.h:204
void write(const Formula< Pol > &f)
Definition: QEPCADStream.h:39
void declare(Variable v)
Definition: QEPCADStream.h:24
QEPCADStream & operator<<(std::ostream &(*os)(std::ostream &))
Definition: QEPCADStream.h:209
void initialize(const carlVariables &vars)
Definition: QEPCADStream.h:183
void write(const Formulas< Pol > &f, const std::string &op)
Definition: QEPCADStream.h:34
void write(const Variable &v)
Definition: QEPCADStream.h:160
std::stringstream mStream
Definition: QEPCADStream.h:22
void write(Relation r)
Definition: QEPCADStream.h:123
void initialize(std::initializer_list< Formula< Pol >> formulas)
Definition: QEPCADStream.h:190
void write(const MultivariatePolynomial< Coeff > &mp)
Definition: QEPCADStream.h:112
void write(const Constraint< Pol > &c)
Definition: QEPCADStream.h:29
void assertFormula(const Formula< Pol > &formula)
Definition: QEPCADStream.h:199
void write(const Term< Coeff > &t)
Definition: QEPCADStream.h:135
void write(const VariableType &vt)
Definition: QEPCADStream.h:163
void write(const Monomial &m)
Definition: QEPCADStream.h:103
void write(const UnivariatePolynomial< Coeff > &up)
Definition: QEPCADStream.h:147
void write(const Monomial::Content::value_type &m)
Definition: QEPCADStream.h:96