1 /******************************************************************************
2 * Top contributors (to current version):
3 * Morgan Deters, Abdalrhman Mohamed, Andrew Reynolds
5 * This file is part of the cvc5 project.
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 * ****************************************************************************
13 * The pretty-printer interface for the AST output language.
15 #include "printer/ast/ast_printer.h"
22 #include "expr/node_manager_attributes.h" // for VarNameAttr
23 #include "expr/node_visitor.h"
24 #include "options/language.h" // for LANG_AST
25 #include "printer/let_binding.h"
26 #include "smt/command.h"
34 void AstPrinter::toStream(std::ostream
& out
,
40 LetBinding
lbind(dag
+ 1);
41 toStreamWithLetify(out
, n
, toDepth
, &lbind
);
43 toStream(out
, n
, toDepth
);
47 void AstPrinter::toStream(std::ostream
& out
,
50 LetBinding
* lbind
) const
53 if(n
.getKind() == kind::NULL_EXPR
) {
59 if(n
.getMetaKind() == kind::metakind::VARIABLE
) {
61 if(n
.getAttribute(expr::VarNameAttr(), s
)) {
64 out
<< "var_" << n
.getId();
69 out
<< '(' << n
.getKind();
70 if(n
.getMetaKind() == kind::metakind::CONSTANT
) {
75 else if (n
.isClosure())
77 for (size_t i
= 0, nchild
= n
.getNumChildren(); i
< nchild
; i
++)
79 // body is re-letified
82 toStreamWithLetify(out
, n
[i
], toDepth
, lbind
);
85 toStream(out
, n
[i
], toDepth
< 0 ? toDepth
: toDepth
- 1, lbind
);
91 if(n
.getMetaKind() == kind::metakind::PARAMETERIZED
) {
95 out
, n
.getOperator(), toDepth
< 0 ? toDepth
: toDepth
- 1, lbind
);
100 for(TNode::iterator i
= n
.begin(),
108 toStream(out
, *i
, toDepth
< 0 ? toDepth
: toDepth
- 1, lbind
);
115 }/* AstPrinter::toStream(TNode) */
118 static bool tryToStream(std::ostream
& out
, const Command
* c
);
121 static bool tryToStream(std::ostream
& out
, const CommandStatus
* s
);
123 void AstPrinter::toStream(std::ostream
& out
, const CommandStatus
* s
) const
125 if(tryToStream
<CommandSuccess
>(out
, s
) ||
126 tryToStream
<CommandFailure
>(out
, s
) ||
127 tryToStream
<CommandUnsupported
>(out
, s
) ||
128 tryToStream
<CommandInterrupted
>(out
, s
)) {
132 out
<< "ERROR: don't know how to print a CommandStatus of class: "
133 << typeid(*s
).name() << endl
;
135 }/* AstPrinter::toStream(CommandStatus*) */
137 void AstPrinter::toStream(std::ostream
& out
, const smt::Model
& m
) const
142 void AstPrinter::toStreamModelSort(std::ostream
& out
,
144 const std::vector
<Node
>& elements
) const
146 // shouldn't be called; only the non-Command* version above should be
150 void AstPrinter::toStreamModelTerm(std::ostream
& out
,
152 const Node
& value
) const
154 // shouldn't be called; only the non-Command* version above should be
158 void AstPrinter::toStreamCmdEmpty(std::ostream
& out
,
159 const std::string
& name
) const
161 out
<< "EmptyCommand(" << name
<< ')' << std::endl
;
164 void AstPrinter::toStreamCmdEcho(std::ostream
& out
,
165 const std::string
& output
) const
167 out
<< "EchoCommand(" << output
<< ')' << std::endl
;
170 void AstPrinter::toStreamCmdAssert(std::ostream
& out
, Node n
) const
172 out
<< "Assert(" << n
<< ')' << std::endl
;
175 void AstPrinter::toStreamCmdPush(std::ostream
& out
) const
177 out
<< "Push()" << std::endl
;
180 void AstPrinter::toStreamCmdPop(std::ostream
& out
) const {
181 out
<< "Pop()" << std::endl
;
184 void AstPrinter::toStreamCmdCheckSat(std::ostream
& out
) const
186 out
<< "CheckSat()" << std::endl
;
189 void AstPrinter::toStreamCmdCheckSatAssuming(
190 std::ostream
& out
, const std::vector
<Node
>& nodes
) const
192 out
<< "CheckSatAssuming( << ";
193 copy(nodes
.begin(), nodes
.end(), ostream_iterator
<Node
>(out
, ", "));
194 out
<< ">> )" << std::endl
;
197 void AstPrinter::toStreamCmdQuery(std::ostream
& out
, Node n
) const
199 out
<< "Query(" << n
<< ')' << std::endl
;
202 void AstPrinter::toStreamCmdReset(std::ostream
& out
) const
204 out
<< "Reset()" << std::endl
;
207 void AstPrinter::toStreamCmdResetAssertions(std::ostream
& out
) const
209 out
<< "ResetAssertions()" << std::endl
;
212 void AstPrinter::toStreamCmdQuit(std::ostream
& out
) const
214 out
<< "Quit()" << std::endl
;
217 void AstPrinter::toStreamCmdDeclarationSequence(
218 std::ostream
& out
, const std::vector
<Command
*>& sequence
) const
220 out
<< "DeclarationSequence[" << endl
;
221 for (CommandSequence::const_iterator i
= sequence
.cbegin();
222 i
!= sequence
.cend();
227 out
<< "]" << std::endl
;
230 void AstPrinter::toStreamCmdCommandSequence(
231 std::ostream
& out
, const std::vector
<Command
*>& sequence
) const
233 out
<< "CommandSequence[" << endl
;
234 for (CommandSequence::const_iterator i
= sequence
.cbegin();
235 i
!= sequence
.cend();
240 out
<< "]" << std::endl
;
243 void AstPrinter::toStreamCmdDeclareFunction(std::ostream
& out
,
244 const std::string
& id
,
247 out
<< "Declare(" << id
<< "," << type
<< ')' << std::endl
;
250 void AstPrinter::toStreamCmdDefineFunction(std::ostream
& out
,
251 const std::string
& id
,
252 const std::vector
<Node
>& formals
,
256 out
<< "DefineFunction( \"" << id
<< "\", [";
257 if (formals
.size() > 0)
259 copy(formals
.begin(), formals
.end() - 1, ostream_iterator
<Node
>(out
, ", "));
260 out
<< formals
.back();
262 out
<< "], << " << formula
<< " >> )" << std::endl
;
265 void AstPrinter::toStreamCmdDeclareType(std::ostream
& out
,
268 out
<< "DeclareType(" << type
<< ')' << std::endl
;
271 void AstPrinter::toStreamCmdDefineType(std::ostream
& out
,
272 const std::string
& id
,
273 const std::vector
<TypeNode
>& params
,
276 out
<< "DefineType(" << id
<< ",[";
277 if (params
.size() > 0)
281 ostream_iterator
<TypeNode
>(out
, ", "));
282 out
<< params
.back();
284 out
<< "]," << t
<< ')' << std::endl
;
287 void AstPrinter::toStreamCmdSimplify(std::ostream
& out
, Node n
) const
289 out
<< "Simplify( << " << n
<< " >> )" << std::endl
;
292 void AstPrinter::toStreamCmdGetValue(std::ostream
& out
,
293 const std::vector
<Node
>& nodes
) const
295 out
<< "GetValue( << ";
296 copy(nodes
.begin(), nodes
.end(), ostream_iterator
<Node
>(out
, ", "));
297 out
<< ">> )" << std::endl
;
300 void AstPrinter::toStreamCmdGetModel(std::ostream
& out
) const
302 out
<< "GetModel()" << std::endl
;
305 void AstPrinter::toStreamCmdGetAssignment(std::ostream
& out
) const
307 out
<< "GetAssignment()" << std::endl
;
310 void AstPrinter::toStreamCmdGetAssertions(std::ostream
& out
) const
312 out
<< "GetAssertions()" << std::endl
;
315 void AstPrinter::toStreamCmdGetProof(std::ostream
& out
) const
317 out
<< "GetProof()" << std::endl
;
320 void AstPrinter::toStreamCmdGetUnsatCore(std::ostream
& out
) const
322 out
<< "GetUnsatCore()" << std::endl
;
325 void AstPrinter::toStreamCmdSetBenchmarkLogic(std::ostream
& out
,
326 const std::string
& logic
) const
328 out
<< "SetBenchmarkLogic(" << logic
<< ')' << std::endl
;
331 void AstPrinter::toStreamCmdSetInfo(std::ostream
& out
,
332 const std::string
& flag
,
333 const std::string
& value
) const
335 out
<< "SetInfo(" << flag
<< ", " << value
<< ')' << std::endl
;
338 void AstPrinter::toStreamCmdGetInfo(std::ostream
& out
,
339 const std::string
& flag
) const
341 out
<< "GetInfo(" << flag
<< ')' << std::endl
;
344 void AstPrinter::toStreamCmdSetOption(std::ostream
& out
,
345 const std::string
& flag
,
346 const std::string
& value
) const
348 out
<< "SetOption(" << flag
<< ", " << value
<< ')' << std::endl
;
351 void AstPrinter::toStreamCmdGetOption(std::ostream
& out
,
352 const std::string
& flag
) const
354 out
<< "GetOption(" << flag
<< ')' << std::endl
;
357 void AstPrinter::toStreamCmdDatatypeDeclaration(
358 std::ostream
& out
, const std::vector
<TypeNode
>& datatypes
) const
360 out
<< "DatatypeDeclarationCommand([";
361 for (const TypeNode
& t
: datatypes
)
363 out
<< t
<< ";" << endl
;
365 out
<< "])" << std::endl
;
368 void AstPrinter::toStreamWithLetify(std::ostream
& out
,
371 LetBinding
* lbind
) const
373 if (lbind
== nullptr)
375 toStream(out
, n
, toDepth
);
378 std::stringstream cparen
;
379 std::vector
<Node
> letList
;
380 lbind
->letify(n
, letList
);
381 if (!letList
.empty())
383 std::map
<Node
, uint32_t>::const_iterator it
;
387 for (size_t i
= 0, nlets
= letList
.size(); i
< nlets
; i
++)
397 Node nl
= letList
[i
];
398 uint32_t id
= lbind
->getId(nl
);
399 out
<< "_let_" << id
<< " := ";
400 Node nlc
= lbind
->convert(nl
, "_let_", false);
401 toStream(out
, nlc
, toDepth
, lbind
);
405 Node nc
= lbind
->convert(n
, "_let_");
406 // print the body, passing the lbind object
407 toStream(out
, nc
, toDepth
, lbind
);
413 static bool tryToStream(std::ostream
& out
, const Command
* c
)
415 if(typeid(*c
) == typeid(T
)) {
416 toStream(out
, dynamic_cast<const T
*>(c
));
422 static void toStream(std::ostream
& out
, const CommandSuccess
* s
)
424 if(Command::printsuccess::getPrintSuccess(out
)) {
429 static void toStream(std::ostream
& out
, const CommandInterrupted
* s
)
431 out
<< "INTERRUPTED" << endl
;
434 static void toStream(std::ostream
& out
, const CommandUnsupported
* s
)
436 out
<< "UNSUPPORTED" << endl
;
439 static void toStream(std::ostream
& out
, const CommandFailure
* s
)
441 out
<< s
->getMessage() << endl
;
445 static bool tryToStream(std::ostream
& out
, const CommandStatus
* s
)
447 if(typeid(*s
) == typeid(T
)) {
448 toStream(out
, dynamic_cast<const T
*>(s
));
455 } // namespace printer