carl  26.08
Computer ARithmetic Library
MapleStream.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 MapleStream {
21 private:
22  std::stringstream mStream;
23 
24  template<typename Pol>
25  void write(const Constraint<Pol>& c) {
26  *this << c.lhs() << " " << c.relation() << " 0";
27  }
28 
29  template<typename Pol>
30  void write(const Formulas<Pol>& f, const std::string& op) {
31  *this << carl::stream_joined(" " + op + " ", f);
32  }
33 
34  template<typename Pol>
35  void write(const Formula<Pol>& f) {
36  switch (f.type()) {
37  case FormulaType::AND:
38  write(f.subformulas(), "and");
39  break;
40  case FormulaType::OR:
41  write(f.subformulas(), "or");
42  break;
43  case FormulaType::IFF:
44  write(f.subformulas(), "iff");
45  break;
46  case FormulaType::XOR:
47  write(f.subformulas(), "xor");
48  break;
50  assert(f.subformulas().size() == 2);
51  write(f.subformulas(), "=>");
52  break;
53  case FormulaType::ITE:
54  assert(false);
55  case FormulaType::NOT:
56  *this << "not(" << f.subformula() << ")";
57  break;
58  case FormulaType::BOOL:
59  *this << f.boolean();
60  break;
62  *this << f.constraint();
63  break;
65  *this << f.variable_comparison();
66  break;
68  *this << f.variable_assignment();
69  break;
71  CARL_LOG_ERROR("carl.maplestream", "Bitvectors are not supported by Maple.");
72  break;
73  case FormulaType::TRUE:
74  case FormulaType::FALSE:
75  *this << f.type();
76  break;
77  case FormulaType::UEQ:
78  CARL_LOG_ERROR("carl.maplestream", "Uninterpreted equalities are not supported by Maple.");
79  break;
83  CARL_LOG_ERROR("carl.maplestream", "Printing exists or forall is not implemented yet.");
84  break;
85  }
86  }
87 
88  void write(const Monomial::Arg& m) {
89  if (m) *this << *m;
90  else *this << "1";
91  }
92  void write(const Monomial::Content::value_type& m) {
93  if (m.second == 0) *this << "1";
94  else if (m.second == 1) *this << m.first;
95  else *this << m.first << "^" << m.second;
96  }
97  void write(const Monomial& m) {
98  if (m.exponents().empty()) *this << "1";
99  else if (m.exponents().size() == 1) *this << m.exponents().front();
100  else {
101  *this << carl::stream_joined("*", m.exponents());
102  }
103  }
104 
105  template<typename Coeff>
107  if (carl::is_zero(mp)) *this << "0";
108  else if (mp.nr_terms() == 1) *this << mp.lterm();
109  else {
110  for (auto it = mp.rbegin(); it != mp.rend(); ++it) {
111  if (it != mp.rbegin()) *this << " + ";
112  *this << *it;
113  }
114  }
115  }
116 
117  void write(Relation r) {
118  switch (r) {
119  case Relation::EQ: *this << "="; break;
120  case Relation::NEQ: *this << "<>"; break;
121  case Relation::LESS: *this << "<"; break;
122  case Relation::LEQ: *this << "<="; break;
123  case Relation::GREATER: *this << ">"; break;
124  case Relation::GEQ: *this << ">="; break;
125  }
126  }
127 
128  template<typename Coeff>
129  void write(const Term<Coeff>& t) {
130  if (!t.monomial()) *this << "(" << t.coeff() << ")";
131  else {
132  if (carl::is_one(t.coeff())) {
133  *this << t.monomial();
134  } else {
135  *this << "(" << t.coeff() << ")*" << t.monomial();
136  }
137  }
138  }
139 
140  template<typename Coeff>
142  if (up.is_constant()) *this << up.constant_part();
143  else {
144  for (std::size_t i = 0; i < up.coefficients().size(); ++i) {
145  if (i > 0) *this << " + ";
146  std::size_t exp = up.coefficients().size() - i - 1;
147  const auto& coeff = up.coefficients()[exp];
148  if (exp == 0) *this << " " << coeff;
149  else *this << "(" << coeff << ")*" << Monomial(up.main_var(), exp);
150  }
151  }
152  }
153 
154  void write(const Variable& v) {
155  *this << v.name();
156  }
157  void write(const VariableType& vt) {
158  switch (vt) {
159  case VariableType::VT_BOOL: *this << "Bool"; break;
160  case VariableType::VT_REAL: *this << "Real"; break;
161  case VariableType::VT_INT: *this << "Int"; break;
162  case VariableType::VT_UNINTERPRETED: *this << "?_Uninterpreted"; break;
163  case VariableType::VT_BITVECTOR: *this << "?_Bitvector"; break;
164  default: *this << "?"; break;
165  }
166  }
167 
168  template<typename T>
169  void write(T&& t) {
170  mStream << t;
171  }
172 
173 public:
175  }
176 
177  template<typename Pol>
178  void assertFormula(const Formula<Pol>& formula) {
179  *this << formula;
180  }
181 
182  template<typename T>
184  write(static_cast<const std::decay_t<T>&>(t));
185  return *this;
186  }
187  //
188  MapleStream& operator<<(std::ostream& (*os)(std::ostream&)) {
189  write(os);
190  return *this;
191  }
192 
193  auto content() const {
194  return mStream.rdbuf();
195  }
196 };
197 
198 inline std::ostream& operator<<(std::ostream& os, const MapleStream& ms) {
199  return os << ms.content();
200 }
201 
202 }
#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
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::Content::value_type &m)
Definition: MapleStream.h:92
void write(const VariableType &vt)
Definition: MapleStream.h:157
MapleStream & operator<<(std::ostream &(*os)(std::ostream &))
Definition: MapleStream.h:188
void write(const Monomial::Arg &m)
Definition: MapleStream.h:88
void write(const UnivariatePolynomial< Coeff > &up)
Definition: MapleStream.h:141
void write(const Formula< Pol > &f)
Definition: MapleStream.h:35
void write(const Monomial &m)
Definition: MapleStream.h:97
void write(const Formulas< Pol > &f, const std::string &op)
Definition: MapleStream.h:30
std::stringstream mStream
Definition: MapleStream.h:22
void write(const Constraint< Pol > &c)
Definition: MapleStream.h:25
void write(const MultivariatePolynomial< Coeff > &mp)
Definition: MapleStream.h:106
void write(const Variable &v)
Definition: MapleStream.h:154
void write(Relation r)
Definition: MapleStream.h:117
void assertFormula(const Formula< Pol > &formula)
Definition: MapleStream.h:178
auto content() const
Definition: MapleStream.h:193
void write(const Term< Coeff > &t)
Definition: MapleStream.h:129
MapleStream & operator<<(T &&t)
Definition: MapleStream.h:183