carl  24.04
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;
86  CARL_LOG_ERROR("carl.qepcadstream", "Printing exists or forall is not implemented yet.");
87  break;
88  }
89  }
90 
91  void write(const Monomial::Arg& m) {
92  if (m) *this << *m;
93  else *this << "1";
94  }
95  void write(const Monomial::Content::value_type& m) {
96  if (m.second == 0) *this << "1";
97  else if (m.second == 1) *this << m.first;
98  else {
99  for (std::size_t i = 0; i < m.second; ++i) *this << " " << m.first;
100  }
101  }
102  void write(const Monomial& m) {
103  if (m.exponents().empty()) *this << "1";
104  else if (m.exponents().size() == 1) *this << m.exponents().front();
105  else {
106  *this << " " << carl::stream_joined(" ", m.exponents());
107  }
108  }
109 
110  template<typename Coeff>
112  if (carl::is_zero(mp)) *this << "0";
113  else if (mp.nr_terms() == 1) *this << mp.lterm();
114  else {
115  for (auto it = mp.rbegin(); it != mp.rend(); ++it) {
116  if (it != mp.rbegin()) *this << " + ";
117  *this << *it;
118  }
119  }
120  }
121 
122  void write(Relation r) {
123  switch (r) {
124  case Relation::EQ: *this << "="; break;
125  case Relation::NEQ: *this << "/="; break;
126  case Relation::LESS: *this << "<"; break;
127  case Relation::LEQ: *this << "<="; break;
128  case Relation::GREATER: *this << ">"; break;
129  case Relation::GEQ: *this << ">="; break;
130  }
131  }
132 
133  template<typename Coeff>
134  void write(const Term<Coeff>& t) {
135  if (!t.monomial()) *this << "(" << t.coeff() << ")";
136  else {
137  if (carl::is_one(t.coeff())) {
138  *this << t.monomial();
139  } else {
140  *this << "(" << t.coeff() << ") " << t.monomial();
141  }
142  }
143  }
144 
145  template<typename Coeff>
147  if (up.is_constant()) *this << up.constant_part();
148  else {
149  for (std::size_t i = 0; i < up.coefficients().size(); ++i) {
150  if (i > 0) *this << " + ";
151  std::size_t exp = up.coefficients().size() - i - 1;
152  const auto& coeff = up.coefficients()[exp];
153  if (exp == 0) *this << " " << coeff;
154  else *this << "(" << coeff << ") " << Monomial(up.main_var(), exp);
155  }
156  }
157  }
158 
159  void write(const Variable& v) {
160  *this << v.name();
161  }
162  void write(const VariableType& vt) {
163  switch (vt) {
164  case VariableType::VT_BOOL: *this << "Bool"; break;
165  case VariableType::VT_REAL: *this << "Real"; break;
166  case VariableType::VT_INT: *this << "Int"; break;
167  case VariableType::VT_UNINTERPRETED: *this << "?_Uninterpreted"; break;
168  case VariableType::VT_BITVECTOR: *this << "?_Bitvector"; break;
169  default: *this << "?"; break;
170  }
171  }
172 
173  template<typename T>
174  void write(T&& t) {
175  mStream << t;
176  }
177 
178 public:
180  }
181 
182  void initialize(const carlVariables& vars) {
183  for (const auto& v: vars) {
184  declare(v);
185  }
186  }
187 
188  template<typename Pol>
189  void initialize(std::initializer_list<Formula<Pol>> formulas) {
190  carlVariables vars;
191  for (const auto& f: formulas) {
192  carl::variables(f,vars);
193  }
194  initialize(vars);
195  }
196 
197  template<typename Pol>
198  void assertFormula(const Formula<Pol>& formula) {
199  *this << formula;
200  }
201 
202  template<typename T>
204  write(static_cast<const std::decay_t<T>&>(t));
205  return *this;
206  }
207  //
208  QEPCADStream& operator<<(std::ostream& (*os)(std::ostream&)) {
209  write(os);
210  return *this;
211  }
212 
213  auto content() const {
214  return mStream.rdbuf();
215  }
216 };
217 
218 inline std::ostream& operator<<(std::ostream& os, const QEPCADStream& qs) {
219  return os << qs.content();
220 }
221 
222 }
#define CARL_LOG_ERROR(channel, msg)
Definition: carl-logging.h:40
@ CONSTRAINT
@ BITVECTOR
@ VARCOMPARE
@ 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:197
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:421
const Formula & subformula() const
Definition: Formula.h:327
const VariableComparison< Pol > & variable_comparison() const
Definition: Formula.h:416
carl::Variable::Arg boolean() const
Definition: Formula.h:436
const Constraint< Pol > & constraint() const
Definition: Formula.h:410
const Formulas< Pol > & subformulas() const
Definition: Formula.h:400
FormulaType type() const
Definition: Formula.h:254
void write(const Monomial::Arg &m)
Definition: QEPCADStream.h:91
QEPCADStream & operator<<(T &&t)
Definition: QEPCADStream.h:203
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:208
void initialize(const carlVariables &vars)
Definition: QEPCADStream.h:182
void write(const Formulas< Pol > &f, const std::string &op)
Definition: QEPCADStream.h:34
void write(const Variable &v)
Definition: QEPCADStream.h:159
std::stringstream mStream
Definition: QEPCADStream.h:22
void write(Relation r)
Definition: QEPCADStream.h:122
void initialize(std::initializer_list< Formula< Pol >> formulas)
Definition: QEPCADStream.h:189
void write(const MultivariatePolynomial< Coeff > &mp)
Definition: QEPCADStream.h:111
void write(const Constraint< Pol > &c)
Definition: QEPCADStream.h:29
void assertFormula(const Formula< Pol > &formula)
Definition: QEPCADStream.h:198
void write(const Term< Coeff > &t)
Definition: QEPCADStream.h:134
void write(const VariableType &vt)
Definition: QEPCADStream.h:162
void write(const Monomial &m)
Definition: QEPCADStream.h:102
void write(const UnivariatePolynomial< Coeff > &up)
Definition: QEPCADStream.h:146
void write(const Monomial::Content::value_type &m)
Definition: QEPCADStream.h:95