dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
PrefixPrinter.cpp
1
6#include "PrefixPrinter.h"
7
8#include <map>
9#include <set>
10#include <sstream> // IWYU pragma: keep for std::stringstream
11#include <stdexcept>
12#include <utility>
13
14#include "dlinear/libs/libgmp.h"
15#include "dlinear/symbolic/literal.h"
16
17namespace dlinear {
18
19PrefixPrinter::PrefixPrinter(std::ostream &os, const Config &config)
20 : GenericFormulaVisitor<std::ostream &>(config, "PrefixPrinter"),
21 GenericExpressionVisitor<std::ostream &>(config, "PrefixPrinter"),
22 os_{os},
23 old_precision_{os.precision()} {}
24
26
27std::ostream &PrefixPrinter::Print(const Expression &e) const { return GenericExpressionVisitor::VisitExpression(e); }
28std::ostream &PrefixPrinter::operator()(const Expression &e) const { return Print(e); }
29
30std::ostream &PrefixPrinter::Print(const Formula &f) const { return VisitFormula(f); }
31std::ostream &PrefixPrinter::operator()(const Formula &f) const { return Print(f); }
32
33std::ostream &PrefixPrinter::VisitVariable(const Expression &e) const { return os_ << get_variable(e); }
34
35std::ostream &PrefixPrinter::VisitConstant(const Expression &e) const {
36 const mpq_class &constant{get_constant_value(e)};
37 bool print_den = constant.get_den() != 1;
38 if (print_den) {
39 os_ << "(/ ";
40 }
41 os_ << constant.get_num();
42 if (print_den) {
43 os_ << " ";
44 os_ << constant.get_den();
45 os_ << ")";
46 }
47 return os_;
48}
49
50std::ostream &PrefixPrinter::VisitUnaryFunction(const std::string &name, const Expression &e) const {
51 os_ << "(" << name << " ";
52 Print(get_argument(e));
53 return os_ << ")";
54}
55
56std::ostream &PrefixPrinter::VisitBinaryFunction(const std::string &name, const Expression &e) const {
57 os_ << "(" << name << " ";
58 Print(get_first_argument(e));
59 os_ << " ";
60 Print(get_second_argument(e));
61 return os_ << ")";
62}
63
64std::ostream &PrefixPrinter::VisitAddition(const Expression &e) const {
65 const mpq_class &constant{get_constant_in_addition(e)};
66 os_ << "(+";
67 if (constant != 0.0) {
68 os_ << " ";
69 VisitConstant(constant);
70 }
71 for (const auto &p : get_expr_to_coeff_map_in_addition(e)) {
72 const Expression &e_i{p.first};
73 const mpq_class &c_i{p.second};
74 os_ << " ";
75 if (c_i == 1.0) {
76 Print(e_i);
77 } else {
78 os_ << "(* ";
79 VisitConstant(c_i);
80 os_ << " ";
81 Print(e_i);
82 os_ << ")";
83 }
84 }
85 return os_ << ")";
86}
87
88std::ostream &PrefixPrinter::VisitMultiplication(const Expression &e) const {
89 const mpq_class &constant{get_constant_in_multiplication(e)};
90 os_ << "(*";
91 if (constant != 1.0) {
92 os_ << " ";
93 VisitConstant(constant);
94 }
95 for (const auto &p : get_base_to_exponent_map_in_multiplication(e)) {
96 const Expression &b_i{p.first};
97 const Expression &e_i{p.second};
98 os_ << " ";
99 if (is_one(e_i)) {
100 Print(b_i);
101 } else {
102 os_ << "(^ ";
103 Print(b_i);
104 os_ << " ";
105 Print(e_i);
106 os_ << ")";
107 }
108 }
109 return os_ << ")";
110}
111
112std::ostream &PrefixPrinter::VisitDivision(const Expression &e) const { return VisitBinaryFunction("/", e); }
113
114std::ostream &PrefixPrinter::VisitLog(const Expression &e) const { return VisitUnaryFunction("log", e); }
115
116std::ostream &PrefixPrinter::VisitAbs(const Expression &e) const { return VisitUnaryFunction("abs", e); }
117
118std::ostream &PrefixPrinter::VisitExp(const Expression &e) const { return VisitUnaryFunction("exp", e); }
119
120std::ostream &PrefixPrinter::VisitSqrt(const Expression &e) const { return VisitUnaryFunction("sqrt", e); }
121
122std::ostream &PrefixPrinter::VisitPow(const Expression &e) const { return VisitBinaryFunction("^", e); }
123
124std::ostream &PrefixPrinter::VisitSin(const Expression &e) const { return VisitUnaryFunction("sin", e); }
125
126std::ostream &PrefixPrinter::VisitCos(const Expression &e) const { return VisitUnaryFunction("cos", e); }
127
128std::ostream &PrefixPrinter::VisitTan(const Expression &e) const { return VisitUnaryFunction("tan", e); }
129
130std::ostream &PrefixPrinter::VisitAsin(const Expression &e) const { return VisitUnaryFunction("asin", e); }
131
132std::ostream &PrefixPrinter::VisitAcos(const Expression &e) const { return VisitUnaryFunction("acos", e); }
133
134std::ostream &PrefixPrinter::VisitAtan(const Expression &e) const { return VisitUnaryFunction("atan", e); }
135
136std::ostream &PrefixPrinter::VisitAtan2(const Expression &e) const { return VisitBinaryFunction("atan2", e); }
137
138std::ostream &PrefixPrinter::VisitSinh(const Expression &e) const { return VisitUnaryFunction("sinh", e); }
139
140std::ostream &PrefixPrinter::VisitCosh(const Expression &e) const { return VisitUnaryFunction("cosh", e); }
141
142std::ostream &PrefixPrinter::VisitTanh(const Expression &e) const { return VisitUnaryFunction("tanh", e); }
143
144std::ostream &PrefixPrinter::VisitMin(const Expression &e) const { return VisitBinaryFunction("min", e); }
145
146std::ostream &PrefixPrinter::VisitMax(const Expression &e) const { return VisitBinaryFunction("max", e); }
147
148std::ostream &PrefixPrinter::VisitIfThenElse(const Expression &e) const {
149 os_ << "(ite ";
150 Print(get_conditional_formula(e));
151 os_ << " ";
152 Print(get_then_expression(e));
153 os_ << " ";
154 Print(get_else_expression(e));
155 return os_ << ")";
156}
157
158std::ostream &PrefixPrinter::VisitUninterpretedFunction(const Expression &) const {
159 throw std::runtime_error("Not implemented.");
160}
161
162std::ostream &PrefixPrinter::VisitFalse(const Formula &) const { return os_ << "false"; }
163
164std::ostream &PrefixPrinter::VisitTrue(const Formula &) const { return os_ << "true"; }
165
166std::ostream &PrefixPrinter::VisitVariable(const Formula &f) const { return os_ << get_variable(f); }
167
168std::ostream &PrefixPrinter::VisitEqualTo(const Formula &f) const {
169 os_ << "(= ";
170 Print(get_lhs_expression(f));
171 os_ << " ";
172 Print(get_rhs_expression(f));
173 return os_ << ")";
174}
175
176std::ostream &PrefixPrinter::VisitNotEqualTo(const Formula &f) const {
177 os_ << "(not (= ";
178 Print(get_lhs_expression(f));
179 os_ << " ";
180 Print(get_rhs_expression(f));
181 return os_ << "))";
182}
183
184std::ostream &PrefixPrinter::VisitGreaterThan(const Formula &f) const {
185 os_ << "(> ";
186 Print(get_lhs_expression(f));
187 os_ << " ";
188 Print(get_rhs_expression(f));
189 return os_ << ")";
190}
191
192std::ostream &PrefixPrinter::VisitGreaterThanOrEqualTo(const Formula &f) const {
193 os_ << "(>= ";
194 Print(get_lhs_expression(f));
195 os_ << " ";
196 Print(get_rhs_expression(f));
197 return os_ << ")";
198}
199
200std::ostream &PrefixPrinter::VisitLessThan(const Formula &f) const {
201 os_ << "(< ";
202 Print(get_lhs_expression(f));
203 os_ << " ";
204 Print(get_rhs_expression(f));
205 return os_ << ")";
206}
207
208std::ostream &PrefixPrinter::VisitLessThanOrEqualTo(const Formula &f) const {
209 os_ << "(<= ";
210 Print(get_lhs_expression(f));
211 os_ << " ";
212 Print(get_rhs_expression(f));
213 return os_ << ")";
214}
215
216std::ostream &PrefixPrinter::VisitConjunction(const Formula &f) const {
217 os_ << "(and";
218 for (const auto &f_i : get_operands(f)) {
219 os_ << " ";
220 Print(f_i);
221 }
222 return os_ << ")";
223}
224
225std::ostream &PrefixPrinter::VisitDisjunction(const Formula &f) const {
226 os_ << "(or";
227 for (const auto &f_i : get_operands(f)) {
228 os_ << " ";
229 Print(f_i);
230 }
231 return os_ << ")";
232}
233
234std::ostream &PrefixPrinter::VisitNegation(const Formula &f) const {
235 os_ << "(not ";
236 Print(get_operand(f));
237 return os_ << ")";
238}
239
240std::ostream &PrefixPrinter::VisitForall(const Formula &) const { throw std::runtime_error("Not implemented."); }
241
242std::string ToPrefix(const Expression &e) {
243 std::ostringstream oss;
244 static Config config;
245 config.m_with_timings() = false;
246 PrefixPrinter pp{oss, config};
247 pp.Print(e);
248 return oss.str();
249}
250
251std::string ToPrefix(const Formula &f) {
252 std::ostringstream oss;
253 static Config config;
254 config.m_with_timings() = false;
255 PrefixPrinter pp{oss, config};
256 pp.Print(f);
257 return oss.str();
258}
259
260} // namespace dlinear
Simple dataclass used to store the configuration of the program.
Definition Config.h:38
OptionValue< bool > & m_with_timings()
Get read-only access to the with_timings parameter of the configuration.
Definition Config.h:210
Generic expression visitor implementing the visitor pattern.
Generic formula visitor implementing the visitor pattern.
Print expressions and formulas in prefix-form.
PrefixPrinter(std::ostream &os, const Config &config=Config{})
Constructs a new PrefixPrinter object with os.
std::ostream & os_
Stream to print to.
const std::streamsize old_precision_
Original precision of the stream.
std::ostream & Print(const Expression &e) const
Print the prefix form of the expression e to the ostream.
~PrefixPrinter() override
Restore the original precision of the ostream.
Represents a symbolic form of an expression.
Represents a symbolic form of a first-order logic formula.
Global namespace for the dlinear library.
std::string ToPrefix(const Expression &e)
Produce the prefix-string representation of the expression e.
STL namespace.