1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Abdalrhman Mohamed, Andrew Reynolds, Morgan Deters |
4 |
|
* |
5 |
|
* This file is part of the cvc5 project. |
6 |
|
* |
7 |
|
* Copyright (c) 2009-2021 by the authors listed in the file AUTHORS |
8 |
|
* in the top-level source directory and their institutional affiliations. |
9 |
|
* All rights reserved. See the file COPYING in the top-level source |
10 |
|
* directory for licensing information. |
11 |
|
* **************************************************************************** |
12 |
|
* |
13 |
|
* Base of the pretty-printer interface. |
14 |
|
*/ |
15 |
|
#include "printer/printer.h" |
16 |
|
|
17 |
|
#include <string> |
18 |
|
|
19 |
|
#include "options/base_options.h" |
20 |
|
#include "options/language.h" |
21 |
|
#include "printer/ast/ast_printer.h" |
22 |
|
#include "printer/smt2/smt2_printer.h" |
23 |
|
#include "printer/tptp/tptp_printer.h" |
24 |
|
#include "proof/unsat_core.h" |
25 |
|
#include "smt/command.h" |
26 |
|
#include "theory/quantifiers/instantiation_list.h" |
27 |
|
|
28 |
|
using namespace std; |
29 |
|
|
30 |
|
namespace cvc5 { |
31 |
|
|
32 |
|
unique_ptr<Printer> |
33 |
20758 |
Printer::d_printers[static_cast<size_t>(Language::LANG_MAX)]; |
34 |
|
|
35 |
7028 |
unique_ptr<Printer> Printer::makePrinter(Language lang) |
36 |
|
{ |
37 |
7028 |
switch(lang) { |
38 |
6595 |
case Language::LANG_SMTLIB_V2_6: |
39 |
|
return unique_ptr<Printer>( |
40 |
6595 |
new printer::smt2::Smt2Printer(printer::smt2::smt2_6_variant)); |
41 |
|
|
42 |
42 |
case Language::LANG_TPTP: |
43 |
42 |
return unique_ptr<Printer>(new printer::tptp::TptpPrinter()); |
44 |
|
|
45 |
387 |
case Language::LANG_SYGUS_V2: |
46 |
|
// sygus version 2.0 does not have discrepancies with smt2, hence we use |
47 |
|
// a normal smt2 variant here. |
48 |
|
return unique_ptr<Printer>( |
49 |
387 |
new printer::smt2::Smt2Printer(printer::smt2::smt2_6_variant)); |
50 |
|
|
51 |
4 |
case Language::LANG_AST: |
52 |
4 |
return unique_ptr<Printer>(new printer::ast::AstPrinter()); |
53 |
|
|
54 |
|
default: Unhandled() << lang; |
55 |
|
} |
56 |
|
} |
57 |
|
|
58 |
29 |
void Printer::toStream(std::ostream& out, const smt::Model& m) const |
59 |
|
{ |
60 |
|
// print the declared sorts |
61 |
29 |
const std::vector<TypeNode>& dsorts = m.getDeclaredSorts(); |
62 |
35 |
for (const TypeNode& tn : dsorts) |
63 |
|
{ |
64 |
6 |
toStreamModelSort(out, tn, m.getDomainElements(tn)); |
65 |
|
} |
66 |
|
// print the declared terms |
67 |
29 |
const std::vector<Node>& dterms = m.getDeclaredTerms(); |
68 |
63 |
for (const Node& n : dterms) |
69 |
|
{ |
70 |
34 |
toStreamModelTerm(out, n, m.getValue(n)); |
71 |
|
} |
72 |
29 |
} |
73 |
|
|
74 |
|
void Printer::toStreamUsing(Language lang, |
75 |
|
std::ostream& out, |
76 |
|
const smt::Model& m) const |
77 |
|
{ |
78 |
|
getPrinter(lang)->toStream(out, m); |
79 |
|
} |
80 |
|
|
81 |
|
void Printer::toStream(std::ostream& out, const UnsatCore& core) const |
82 |
|
{ |
83 |
|
for(UnsatCore::iterator i = core.begin(); i != core.end(); ++i) { |
84 |
|
toStreamCmdAssert(out, *i); |
85 |
|
out << std::endl; |
86 |
|
} |
87 |
|
}/* Printer::toStream(UnsatCore) */ |
88 |
|
|
89 |
15 |
void Printer::toStream(std::ostream& out, const InstantiationList& is) const |
90 |
|
{ |
91 |
15 |
out << "(instantiations " << is.d_quant << std::endl; |
92 |
30 |
for (const InstantiationVec& i : is.d_inst) |
93 |
|
{ |
94 |
15 |
out << " "; |
95 |
15 |
if (i.d_id != theory::InferenceId::UNKNOWN) |
96 |
|
{ |
97 |
|
out << "(! "; |
98 |
|
} |
99 |
15 |
out << "( "; |
100 |
30 |
for (const Node& n : i.d_vec) |
101 |
|
{ |
102 |
15 |
out << n << " "; |
103 |
|
} |
104 |
15 |
out << ")"; |
105 |
15 |
if (i.d_id != theory::InferenceId::UNKNOWN) |
106 |
|
{ |
107 |
|
out << " :source " << i.d_id; |
108 |
|
if (!i.d_pfArg.isNull()) |
109 |
|
{ |
110 |
|
out << " " << i.d_pfArg; |
111 |
|
} |
112 |
|
out << ")"; |
113 |
|
} |
114 |
15 |
out << std::endl; |
115 |
|
} |
116 |
15 |
out << ")" << std::endl; |
117 |
15 |
} |
118 |
|
|
119 |
9 |
void Printer::toStream(std::ostream& out, const SkolemList& sks) const |
120 |
|
{ |
121 |
9 |
out << "(skolem " << sks.d_quant << std::endl; |
122 |
9 |
out << " ( "; |
123 |
18 |
for (const Node& n : sks.d_sks) |
124 |
|
{ |
125 |
9 |
out << n << " "; |
126 |
|
} |
127 |
9 |
out << ")" << std::endl; |
128 |
9 |
out << ")" << std::endl; |
129 |
9 |
} |
130 |
|
|
131 |
483288 |
Printer* Printer::getPrinter(Language lang) |
132 |
|
{ |
133 |
483288 |
if (lang == Language::LANG_AUTO) |
134 |
|
{ |
135 |
|
// Infer the language to use for output. |
136 |
|
// |
137 |
|
// Options can be null in certain circumstances (e.g., when printing |
138 |
|
// the singleton "null" expr. So we guard against segfault |
139 |
2367 |
if (not Options::isCurrentNull()) |
140 |
|
{ |
141 |
2357 |
if (Options::current().base.outputLanguageWasSetByUser) |
142 |
|
{ |
143 |
25 |
lang = options::outputLanguage(); |
144 |
|
} |
145 |
2357 |
if (lang == Language::LANG_AUTO |
146 |
2357 |
&& Options::current().base.inputLanguageWasSetByUser) |
147 |
|
{ |
148 |
16 |
lang = options::inputLanguage(); |
149 |
|
} |
150 |
|
} |
151 |
2367 |
if (lang == Language::LANG_AUTO) |
152 |
|
{ |
153 |
2326 |
lang = Language::LANG_SMTLIB_V2_6; // default |
154 |
|
} |
155 |
|
} |
156 |
483288 |
if (d_printers[static_cast<size_t>(lang)] == nullptr) |
157 |
|
{ |
158 |
7028 |
d_printers[static_cast<size_t>(lang)] = makePrinter(lang); |
159 |
|
} |
160 |
483288 |
return d_printers[static_cast<size_t>(lang)].get(); |
161 |
|
} |
162 |
|
|
163 |
|
void Printer::printUnknownCommand(std::ostream& out, |
164 |
|
const std::string& name) const |
165 |
|
{ |
166 |
|
out << "ERROR: don't know how to print " << name << " command" << std::endl; |
167 |
|
} |
168 |
|
|
169 |
|
void Printer::toStreamCmdEmpty(std::ostream& out, const std::string& name) const |
170 |
|
{ |
171 |
|
printUnknownCommand(out, "empty"); |
172 |
|
} |
173 |
|
|
174 |
|
void Printer::toStreamCmdEcho(std::ostream& out, |
175 |
|
const std::string& output) const |
176 |
|
{ |
177 |
|
printUnknownCommand(out, "echo"); |
178 |
|
} |
179 |
|
|
180 |
|
void Printer::toStreamCmdAssert(std::ostream& out, Node n) const |
181 |
|
{ |
182 |
|
printUnknownCommand(out, "assert"); |
183 |
|
} |
184 |
|
|
185 |
|
void Printer::toStreamCmdPush(std::ostream& out) const |
186 |
|
{ |
187 |
|
printUnknownCommand(out, "push"); |
188 |
|
} |
189 |
|
|
190 |
|
void Printer::toStreamCmdPop(std::ostream& out) const |
191 |
|
{ |
192 |
|
printUnknownCommand(out, "pop"); |
193 |
|
} |
194 |
|
|
195 |
|
void Printer::toStreamCmdDeclareFunction(std::ostream& out, |
196 |
|
const std::string& id, |
197 |
|
TypeNode type) const |
198 |
|
{ |
199 |
|
printUnknownCommand(out, "declare-fun"); |
200 |
|
} |
201 |
|
|
202 |
2 |
void Printer::toStreamCmdDeclareFunction(std::ostream& out, const Node& v) const |
203 |
|
{ |
204 |
4 |
std::stringstream vs; |
205 |
2 |
vs << v; |
206 |
2 |
toStreamCmdDeclareFunction(out, vs.str(), v.getType()); |
207 |
2 |
} |
208 |
|
|
209 |
|
void Printer::toStreamCmdDeclarePool(std::ostream& out, |
210 |
|
const std::string& id, |
211 |
|
TypeNode type, |
212 |
|
const std::vector<Node>& initValue) const |
213 |
|
{ |
214 |
|
printUnknownCommand(out, "declare-pool"); |
215 |
|
} |
216 |
|
|
217 |
|
void Printer::toStreamCmdDeclareType(std::ostream& out, |
218 |
|
TypeNode type) const |
219 |
|
{ |
220 |
|
printUnknownCommand(out, "declare-sort"); |
221 |
|
} |
222 |
|
|
223 |
|
void Printer::toStreamCmdDefineType(std::ostream& out, |
224 |
|
const std::string& id, |
225 |
|
const std::vector<TypeNode>& params, |
226 |
|
TypeNode t) const |
227 |
|
{ |
228 |
|
printUnknownCommand(out, "define-sort"); |
229 |
|
} |
230 |
|
|
231 |
|
void Printer::toStreamCmdDefineFunction(std::ostream& out, |
232 |
|
const std::string& id, |
233 |
|
const std::vector<Node>& formals, |
234 |
|
TypeNode range, |
235 |
|
Node formula) const |
236 |
|
{ |
237 |
|
printUnknownCommand(out, "define-fun"); |
238 |
|
} |
239 |
|
|
240 |
|
void Printer::toStreamCmdDefineFunction(std::ostream& out, |
241 |
|
Node v, |
242 |
|
Node lambda) const |
243 |
|
{ |
244 |
|
std::stringstream vs; |
245 |
|
vs << v; |
246 |
|
std::vector<Node> formals; |
247 |
|
Node body = lambda; |
248 |
|
TypeNode rangeType = v.getType(); |
249 |
|
if (body.getKind() == kind::LAMBDA) |
250 |
|
{ |
251 |
|
formals.insert(formals.end(), lambda[0].begin(), lambda[0].end()); |
252 |
|
body = lambda[1]; |
253 |
|
Assert(rangeType.isFunction()); |
254 |
|
rangeType = rangeType.getRangeType(); |
255 |
|
} |
256 |
|
toStreamCmdDefineFunction(out, vs.str(), formals, rangeType, body); |
257 |
|
} |
258 |
|
|
259 |
|
void Printer::toStreamCmdDefineFunctionRec( |
260 |
|
std::ostream& out, |
261 |
|
const std::vector<Node>& funcs, |
262 |
|
const std::vector<std::vector<Node>>& formals, |
263 |
|
const std::vector<Node>& formulas) const |
264 |
|
{ |
265 |
|
printUnknownCommand(out, "define-fun-rec"); |
266 |
|
} |
267 |
|
|
268 |
|
void Printer::toStreamCmdDefineFunctionRec( |
269 |
|
std::ostream& out, |
270 |
|
const std::vector<Node>& funcs, |
271 |
|
const std::vector<Node>& lambdas) const |
272 |
|
{ |
273 |
|
std::vector<std::vector<Node>> formals; |
274 |
|
std::vector<Node> formulas; |
275 |
|
for (const Node& l : lambdas) |
276 |
|
{ |
277 |
|
std::vector<Node> formalsVec; |
278 |
|
Node formula; |
279 |
|
if (l.getKind() == kind::LAMBDA) |
280 |
|
{ |
281 |
|
formalsVec.insert(formalsVec.end(), l[0].begin(), l[0].end()); |
282 |
|
formula = l[1]; |
283 |
|
} |
284 |
|
else |
285 |
|
{ |
286 |
|
formula = l; |
287 |
|
} |
288 |
|
formals.emplace_back(formalsVec); |
289 |
|
formulas.emplace_back(formula); |
290 |
|
} |
291 |
|
toStreamCmdDefineFunctionRec(out, funcs, formals, formulas); |
292 |
|
} |
293 |
|
|
294 |
|
void Printer::toStreamCmdSetUserAttribute(std::ostream& out, |
295 |
|
const std::string& attr, |
296 |
|
Node n) const |
297 |
|
{ |
298 |
|
printUnknownCommand(out, "set-user-attribute"); |
299 |
|
} |
300 |
|
|
301 |
|
void Printer::toStreamCmdCheckSat(std::ostream& out) const |
302 |
|
{ |
303 |
|
printUnknownCommand(out, "check-sat"); |
304 |
|
} |
305 |
|
|
306 |
|
void Printer::toStreamCmdCheckSatAssuming(std::ostream& out, |
307 |
|
const std::vector<Node>& nodes) const |
308 |
|
{ |
309 |
|
printUnknownCommand(out, "check-sat-assuming"); |
310 |
|
} |
311 |
|
|
312 |
|
void Printer::toStreamCmdQuery(std::ostream& out, Node n) const |
313 |
|
{ |
314 |
|
printUnknownCommand(out, "query"); |
315 |
|
} |
316 |
|
|
317 |
|
void Printer::toStreamCmdDeclareVar(std::ostream& out, |
318 |
|
Node var, |
319 |
|
TypeNode type) const |
320 |
|
{ |
321 |
|
printUnknownCommand(out, "declare-var"); |
322 |
|
} |
323 |
|
|
324 |
|
void Printer::toStreamCmdSynthFun(std::ostream& out, |
325 |
|
Node f, |
326 |
|
const std::vector<Node>& vars, |
327 |
|
bool isInv, |
328 |
|
TypeNode sygusType) const |
329 |
|
{ |
330 |
|
printUnknownCommand(out, isInv ? "synth-inv" : "synth-fun"); |
331 |
|
} |
332 |
|
|
333 |
|
void Printer::toStreamCmdConstraint(std::ostream& out, Node n) const |
334 |
|
{ |
335 |
|
printUnknownCommand(out, "constraint"); |
336 |
|
} |
337 |
|
|
338 |
|
void Printer::toStreamCmdAssume(std::ostream& out, Node n) const |
339 |
|
{ |
340 |
|
printUnknownCommand(out, "assume"); |
341 |
|
} |
342 |
|
|
343 |
|
void Printer::toStreamCmdInvConstraint( |
344 |
|
std::ostream& out, Node inv, Node pre, Node trans, Node post) const |
345 |
|
{ |
346 |
|
printUnknownCommand(out, "inv-constraint"); |
347 |
|
} |
348 |
|
|
349 |
|
void Printer::toStreamCmdCheckSynth(std::ostream& out) const |
350 |
|
{ |
351 |
|
printUnknownCommand(out, "check-synth"); |
352 |
|
} |
353 |
|
|
354 |
|
void Printer::toStreamCmdSimplify(std::ostream& out, Node n) const |
355 |
|
{ |
356 |
|
printUnknownCommand(out, "simplify"); |
357 |
|
} |
358 |
|
|
359 |
|
void Printer::toStreamCmdGetValue(std::ostream& out, |
360 |
|
const std::vector<Node>& nodes) const |
361 |
|
{ |
362 |
|
printUnknownCommand(out, "get-value"); |
363 |
|
} |
364 |
|
|
365 |
|
void Printer::toStreamCmdGetAssignment(std::ostream& out) const |
366 |
|
{ |
367 |
|
printUnknownCommand(out, "get-assignment"); |
368 |
|
} |
369 |
|
|
370 |
|
void Printer::toStreamCmdGetModel(std::ostream& out) const |
371 |
|
{ |
372 |
|
printUnknownCommand(out, "ge-model"); |
373 |
|
} |
374 |
|
|
375 |
|
void Printer::toStreamCmdBlockModel(std::ostream& out) const |
376 |
|
{ |
377 |
|
printUnknownCommand(out, "block-model"); |
378 |
|
} |
379 |
|
|
380 |
|
void Printer::toStreamCmdBlockModelValues(std::ostream& out, |
381 |
|
const std::vector<Node>& nodes) const |
382 |
|
{ |
383 |
|
printUnknownCommand(out, "block-model-values"); |
384 |
|
} |
385 |
|
|
386 |
|
void Printer::toStreamCmdGetProof(std::ostream& out) const |
387 |
|
{ |
388 |
|
printUnknownCommand(out, "get-proof"); |
389 |
|
} |
390 |
|
|
391 |
|
void Printer::toStreamCmdGetInstantiations(std::ostream& out) const |
392 |
|
{ |
393 |
|
printUnknownCommand(out, "get-instantiations"); |
394 |
|
} |
395 |
|
|
396 |
|
void Printer::toStreamCmdGetInterpol(std::ostream& out, |
397 |
|
const std::string& name, |
398 |
|
Node conj, |
399 |
|
TypeNode sygusType) const |
400 |
|
{ |
401 |
|
printUnknownCommand(out, "get-interpol"); |
402 |
|
} |
403 |
|
|
404 |
|
void Printer::toStreamCmdGetAbduct(std::ostream& out, |
405 |
|
const std::string& name, |
406 |
|
Node conj, |
407 |
|
TypeNode sygusType) const |
408 |
|
{ |
409 |
|
printUnknownCommand(out, "get-abduct"); |
410 |
|
} |
411 |
|
|
412 |
|
void Printer::toStreamCmdGetQuantifierElimination(std::ostream& out, |
413 |
|
Node n, |
414 |
|
bool doFull) const |
415 |
|
{ |
416 |
|
printUnknownCommand(out, "get-quantifier-elimination"); |
417 |
|
} |
418 |
|
|
419 |
|
void Printer::toStreamCmdGetUnsatAssumptions(std::ostream& out) const |
420 |
|
{ |
421 |
|
printUnknownCommand(out, "get-unsat-assumption"); |
422 |
|
} |
423 |
|
|
424 |
|
void Printer::toStreamCmdGetUnsatCore(std::ostream& out) const |
425 |
|
{ |
426 |
|
printUnknownCommand(out, "get-unsat-core"); |
427 |
|
} |
428 |
|
|
429 |
|
void Printer::toStreamCmdGetDifficulty(std::ostream& out) const |
430 |
|
{ |
431 |
|
printUnknownCommand(out, "get-difficulty"); |
432 |
|
} |
433 |
|
|
434 |
|
void Printer::toStreamCmdGetAssertions(std::ostream& out) const |
435 |
|
{ |
436 |
|
printUnknownCommand(out, "get-assertions"); |
437 |
|
} |
438 |
|
|
439 |
|
void Printer::toStreamCmdSetBenchmarkLogic(std::ostream& out, |
440 |
|
const std::string& logic) const |
441 |
|
{ |
442 |
|
printUnknownCommand(out, "set-logic"); |
443 |
|
} |
444 |
|
|
445 |
|
void Printer::toStreamCmdSetInfo(std::ostream& out, |
446 |
|
const std::string& flag, |
447 |
|
const std::string& value) const |
448 |
|
{ |
449 |
|
printUnknownCommand(out, "set-info"); |
450 |
|
} |
451 |
|
|
452 |
|
void Printer::toStreamCmdGetInfo(std::ostream& out, |
453 |
|
const std::string& flag) const |
454 |
|
{ |
455 |
|
printUnknownCommand(out, "get-info"); |
456 |
|
} |
457 |
|
|
458 |
|
void Printer::toStreamCmdSetOption(std::ostream& out, |
459 |
|
const std::string& flag, |
460 |
|
const std::string& value) const |
461 |
|
{ |
462 |
|
printUnknownCommand(out, "set-option"); |
463 |
|
} |
464 |
|
|
465 |
|
void Printer::toStreamCmdGetOption(std::ostream& out, |
466 |
|
const std::string& flag) const |
467 |
|
{ |
468 |
|
printUnknownCommand(out, "get-option"); |
469 |
|
} |
470 |
|
|
471 |
|
void Printer::toStreamCmdSetExpressionName(std::ostream& out, |
472 |
|
Node n, |
473 |
|
const std::string& name) const |
474 |
|
{ |
475 |
|
printUnknownCommand(out, "set-expression-name"); |
476 |
|
} |
477 |
|
|
478 |
|
void Printer::toStreamCmdDatatypeDeclaration( |
479 |
|
std::ostream& out, const std::vector<TypeNode>& datatypes) const |
480 |
|
{ |
481 |
|
printUnknownCommand( |
482 |
|
out, datatypes.size() == 1 ? "declare-datatype" : "declare-datatypes"); |
483 |
|
} |
484 |
|
|
485 |
|
void Printer::toStreamCmdReset(std::ostream& out) const |
486 |
|
{ |
487 |
|
printUnknownCommand(out, "reset"); |
488 |
|
} |
489 |
|
|
490 |
|
void Printer::toStreamCmdResetAssertions(std::ostream& out) const |
491 |
|
{ |
492 |
|
printUnknownCommand(out, "reset-assertions"); |
493 |
|
} |
494 |
|
|
495 |
|
void Printer::toStreamCmdQuit(std::ostream& out) const |
496 |
|
{ |
497 |
|
printUnknownCommand(out, "quit"); |
498 |
|
} |
499 |
|
|
500 |
|
void Printer::toStreamCmdDeclareHeap(std::ostream& out, |
501 |
|
TypeNode locType, |
502 |
|
TypeNode dataType) const |
503 |
|
{ |
504 |
|
printUnknownCommand(out, "declare-heap"); |
505 |
|
} |
506 |
|
|
507 |
|
void Printer::toStreamCmdCommandSequence( |
508 |
|
std::ostream& out, const std::vector<Command*>& sequence) const |
509 |
|
{ |
510 |
|
printUnknownCommand(out, "sequence"); |
511 |
|
} |
512 |
|
|
513 |
|
void Printer::toStreamCmdDeclarationSequence( |
514 |
|
std::ostream& out, const std::vector<Command*>& sequence) const |
515 |
|
{ |
516 |
|
printUnknownCommand(out, "sequence"); |
517 |
|
} |
518 |
|
|
519 |
31137 |
} // namespace cvc5 |