carl  24.04
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;
82  CARL_LOG_ERROR("carl.maplestream", "Printing exists or forall is not implemented yet.");
83  break;
84  }
85  }
86 
87  void write(const Monomial::Arg& m) {
88  if (m) *this << *m;
89  else *this << "1";
90  }
91  void write(const Monomial::Content::value_type& m) {
92  if (m.second == 0) *this << "1";
93  else if (m.second == 1) *this << m.first;
94  else *this << m.first << "^" << m.second;
95  }
96  void write(const Monomial& m) {
97  if (m.exponents().empty()) *this << "1";
98  else if (m.exponents().size() == 1) *this << m.exponents().front();
99  else {
100  *this << carl::stream_joined("*", m.exponents());
101  }
102  }
103 
104  template<typename Coeff>
106  if (carl::is_zero(mp)) *this << "0";
107  else if (mp.nr_terms() == 1) *this << mp.lterm();
108  else {
109  for (auto it = mp.rbegin(); it != mp.rend(); ++it) {
110  if (it != mp.rbegin()) *this << " + ";
111  *this << *it;
112  }
113  }
114  }
115 
116  void write(Relation r) {
117  switch (r) {
118  case Relation::EQ: *this << "="; break;
119  case Relation::NEQ: *this << "<>"; break;
120  case Relation::LESS: *this << "<"; break;
121  case Relation::LEQ: *this << "<="; break;
122  case Relation::GREATER: *this << ">"; break;
123  case Relation::GEQ: *this << ">="; break;
124  }
125  }
126 
127  template<typename Coeff>
128  void write(const Term<Coeff>& t) {
129  if (!t.monomial()) *this << "(" << t.coeff() << ")";
130  else {
131  if (carl::is_one(t.coeff())) {
132  *this << t.monomial();
133  } else {
134  *this << "(" << t.coeff() << ")*" << t.monomial();
135  }
136  }
137  }
138 
139  template<typename Coeff>
141  if (up.is_constant()) *this << up.constant_part();
142  else {
143  for (std::size_t i = 0; i < up.coefficients().size(); ++i) {
144  if (i > 0) *this << " + ";
145  std::size_t exp = up.coefficients().size() - i - 1;
146  const auto& coeff = up.coefficients()[exp];
147  if (exp == 0) *this << " " << coeff;
148  else *this << "(" << coeff << ")*" << Monomial(up.main_var(), exp);
149  }
150  }
151  }
152 
153  void write(const Variable& v) {
154  *this << v.name();
155  }
156  void write(const VariableType& vt) {
157  switch (vt) {
158  case VariableType::VT_BOOL: *this << "Bool"; break;
159  case VariableType::VT_REAL: *this << "Real"; break;
160  case VariableType::VT_INT: *this << "Int"; break;
161  case VariableType::VT_UNINTERPRETED: *this << "?_Uninterpreted"; break;
162  case VariableType::VT_BITVECTOR: *this << "?_Bitvector"; break;
163  default: *this << "?"; break;
164  }
165  }
166 
167  template<typename T>
168  void write(T&& t) {
169  mStream << t;
170  }
171 
172 public:
174  }
175 
176  template<typename Pol>
177  void assertFormula(const Formula<Pol>& formula) {
178  *this << formula;
179  }
180 
181  template<typename T>
183  write(static_cast<const std::decay_t<T>&>(t));
184  return *this;
185  }
186  //
187  MapleStream& operator<<(std::ostream& (*os)(std::ostream&)) {
188  write(os);
189  return *this;
190  }
191 
192  auto content() const {
193  return mStream.rdbuf();
194  }
195 };
196 
197 inline std::ostream& operator<<(std::ostream& os, const MapleStream& ms) {
198  return os << ms.content();
199 }
200 
201 }
#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
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::Content::value_type &m)
Definition: MapleStream.h:91
void write(const VariableType &vt)
Definition: MapleStream.h:156
MapleStream & operator<<(std::ostream &(*os)(std::ostream &))
Definition: MapleStream.h:187
void write(const Monomial::Arg &m)
Definition: MapleStream.h:87
void write(const UnivariatePolynomial< Coeff > &up)
Definition: MapleStream.h:140
void write(const Formula< Pol > &f)
Definition: MapleStream.h:35
void write(const Monomial &m)
Definition: MapleStream.h:96
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:105
void write(const Variable &v)
Definition: MapleStream.h:153
void write(Relation r)
Definition: MapleStream.h:116
void assertFormula(const Formula< Pol > &formula)
Definition: MapleStream.h:177
auto content() const
Definition: MapleStream.h:192
void write(const Term< Coeff > &t)
Definition: MapleStream.h:128
MapleStream & operator<<(T &&t)
Definition: MapleStream.h:182