1 /********************* */
4 ** Top contributors (to current version):
5 ** Morgan Deters, Andrew Reynolds, Francois Bobot
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2017 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.\endverbatim
12 ** \brief Implementation of command objects.
14 ** Implementation of command objects.
17 #include "smt/command.h"
26 #include "base/cvc4_assert.h"
27 #include "base/output.h"
28 #include "expr/expr_iomanip.h"
29 #include "expr/node.h"
30 #include "options/options.h"
31 #include "options/smt_options.h"
32 #include "printer/printer.h"
34 #include "smt/model.h"
35 #include "smt/smt_engine.h"
36 #include "smt/smt_engine_scope.h"
37 #include "util/sexpr.h"
45 std::vector
<Expr
> ExportTo(ExprManager
* exprManager
,
46 ExprManagerMapCollection
& variableMap
,
47 const std::vector
<Expr
>& exprs
)
49 std::vector
<Expr
> exported
;
50 exported
.reserve(exprs
.size());
51 for (const Expr
& expr
: exprs
)
53 exported
.push_back(expr
.exportTo(exprManager
, variableMap
));
60 const int CommandPrintSuccess::s_iosIndex
= std::ios_base::xalloc();
61 const CommandSuccess
* CommandSuccess::s_instance
= new CommandSuccess();
62 const CommandInterrupted
* CommandInterrupted::s_instance
=
63 new CommandInterrupted();
65 std::ostream
& operator<<(std::ostream
& out
, const Command
& c
)
68 Node::setdepth::getDepth(out
),
69 Node::printtypes::getPrintTypes(out
),
70 Node::dag::getDag(out
),
71 Node::setlanguage::getLanguage(out
));
75 ostream
& operator<<(ostream
& out
, const Command
* c
)
88 std::ostream
& operator<<(std::ostream
& out
, const CommandStatus
& s
)
90 s
.toStream(out
, Node::setlanguage::getLanguage(out
));
94 ostream
& operator<<(ostream
& out
, const CommandStatus
* s
)
107 /* class CommandPrintSuccess */
109 void CommandPrintSuccess::applyPrintSuccess(std::ostream
& out
)
111 out
.iword(s_iosIndex
) = d_printSuccess
;
114 bool CommandPrintSuccess::getPrintSuccess(std::ostream
& out
)
116 return out
.iword(s_iosIndex
);
119 void CommandPrintSuccess::setPrintSuccess(std::ostream
& out
, bool printSuccess
)
121 out
.iword(s_iosIndex
) = printSuccess
;
124 std::ostream
& operator<<(std::ostream
& out
, CommandPrintSuccess cps
)
126 cps
.applyPrintSuccess(out
);
132 Command::Command() : d_commandStatus(NULL
), d_muted(false) {}
133 Command::Command(const Command
& cmd
)
136 (cmd
.d_commandStatus
== NULL
) ? NULL
: &cmd
.d_commandStatus
->clone();
137 d_muted
= cmd
.d_muted
;
142 if (d_commandStatus
!= NULL
&& d_commandStatus
!= CommandSuccess::instance())
144 delete d_commandStatus
;
148 bool Command::ok() const
150 // either we haven't run the command yet, or it ran successfully
151 return d_commandStatus
== NULL
152 || dynamic_cast<const CommandSuccess
*>(d_commandStatus
) != NULL
;
155 bool Command::fail() const
157 return d_commandStatus
!= NULL
158 && dynamic_cast<const CommandFailure
*>(d_commandStatus
) != NULL
;
161 bool Command::interrupted() const
163 return d_commandStatus
!= NULL
164 && dynamic_cast<const CommandInterrupted
*>(d_commandStatus
) != NULL
;
167 void Command::invoke(SmtEngine
* smtEngine
, std::ostream
& out
)
170 if (!(isMuted() && ok()))
173 smtEngine
->getOption("command-verbosity:" + getCommandName())
179 std::string
Command::toString() const
181 std::stringstream ss
;
186 void Command::toStream(std::ostream
& out
,
190 OutputLanguage language
) const
192 Printer::getPrinter(language
)->toStream(out
, this, toDepth
, types
, dag
);
195 void CommandStatus::toStream(std::ostream
& out
, OutputLanguage language
) const
197 Printer::getPrinter(language
)->toStream(out
, this);
200 void Command::printResult(std::ostream
& out
, uint32_t verbosity
) const
202 if (d_commandStatus
!= NULL
)
204 if ((!ok() && verbosity
>= 1) || verbosity
>= 2)
206 out
<< *d_commandStatus
;
211 /* class EmptyCommand */
213 EmptyCommand::EmptyCommand(std::string name
) : d_name(name
) {}
214 std::string
EmptyCommand::getName() const { return d_name
; }
215 void EmptyCommand::invoke(SmtEngine
* smtEngine
)
217 /* empty commands have no implementation */
218 d_commandStatus
= CommandSuccess::instance();
221 Command
* EmptyCommand::exportTo(ExprManager
* exprManager
,
222 ExprManagerMapCollection
& variableMap
)
224 return new EmptyCommand(d_name
);
227 Command
* EmptyCommand::clone() const { return new EmptyCommand(d_name
); }
228 std::string
EmptyCommand::getCommandName() const { return "empty"; }
229 /* class EchoCommand */
231 EchoCommand::EchoCommand(std::string output
) : d_output(output
) {}
232 std::string
EchoCommand::getOutput() const { return d_output
; }
233 void EchoCommand::invoke(SmtEngine
* smtEngine
)
235 /* we don't have an output stream here, nothing to do */
236 d_commandStatus
= CommandSuccess::instance();
239 void EchoCommand::invoke(SmtEngine
* smtEngine
, std::ostream
& out
)
241 out
<< d_output
<< std::endl
;
242 d_commandStatus
= CommandSuccess::instance();
244 smtEngine
->getOption("command-verbosity:" + getCommandName())
249 Command
* EchoCommand::exportTo(ExprManager
* exprManager
,
250 ExprManagerMapCollection
& variableMap
)
252 return new EchoCommand(d_output
);
255 Command
* EchoCommand::clone() const { return new EchoCommand(d_output
); }
256 std::string
EchoCommand::getCommandName() const { return "echo"; }
257 /* class AssertCommand */
259 AssertCommand::AssertCommand(const Expr
& e
, bool inUnsatCore
)
260 : d_expr(e
), d_inUnsatCore(inUnsatCore
)
264 Expr
AssertCommand::getExpr() const { return d_expr
; }
265 void AssertCommand::invoke(SmtEngine
* smtEngine
)
269 smtEngine
->assertFormula(d_expr
, d_inUnsatCore
);
270 d_commandStatus
= CommandSuccess::instance();
272 catch (UnsafeInterruptException
& e
)
274 d_commandStatus
= new CommandInterrupted();
278 d_commandStatus
= new CommandFailure(e
.what());
282 Command
* AssertCommand::exportTo(ExprManager
* exprManager
,
283 ExprManagerMapCollection
& variableMap
)
285 return new AssertCommand(d_expr
.exportTo(exprManager
, variableMap
),
289 Command
* AssertCommand::clone() const
291 return new AssertCommand(d_expr
, d_inUnsatCore
);
294 std::string
AssertCommand::getCommandName() const { return "assert"; }
295 /* class PushCommand */
297 void PushCommand::invoke(SmtEngine
* smtEngine
)
302 d_commandStatus
= CommandSuccess::instance();
304 catch (UnsafeInterruptException
& e
)
306 d_commandStatus
= new CommandInterrupted();
310 d_commandStatus
= new CommandFailure(e
.what());
314 Command
* PushCommand::exportTo(ExprManager
* exprManager
,
315 ExprManagerMapCollection
& variableMap
)
317 return new PushCommand();
320 Command
* PushCommand::clone() const { return new PushCommand(); }
321 std::string
PushCommand::getCommandName() const { return "push"; }
322 /* class PopCommand */
324 void PopCommand::invoke(SmtEngine
* smtEngine
)
329 d_commandStatus
= CommandSuccess::instance();
331 catch (UnsafeInterruptException
& e
)
333 d_commandStatus
= new CommandInterrupted();
337 d_commandStatus
= new CommandFailure(e
.what());
341 Command
* PopCommand::exportTo(ExprManager
* exprManager
,
342 ExprManagerMapCollection
& variableMap
)
344 return new PopCommand();
347 Command
* PopCommand::clone() const { return new PopCommand(); }
348 std::string
PopCommand::getCommandName() const { return "pop"; }
349 /* class CheckSatCommand */
351 CheckSatCommand::CheckSatCommand() : d_expr() {}
352 CheckSatCommand::CheckSatCommand(const Expr
& expr
, bool inUnsatCore
)
353 : d_expr(expr
), d_inUnsatCore(inUnsatCore
)
357 Expr
CheckSatCommand::getExpr() const { return d_expr
; }
358 void CheckSatCommand::invoke(SmtEngine
* smtEngine
)
362 d_result
= smtEngine
->checkSat(d_expr
);
363 d_commandStatus
= CommandSuccess::instance();
367 d_commandStatus
= new CommandFailure(e
.what());
371 Result
CheckSatCommand::getResult() const { return d_result
; }
372 void CheckSatCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
376 this->Command::printResult(out
, verbosity
);
380 out
<< d_result
<< endl
;
384 Command
* CheckSatCommand::exportTo(ExprManager
* exprManager
,
385 ExprManagerMapCollection
& variableMap
)
387 CheckSatCommand
* c
= new CheckSatCommand(
388 d_expr
.exportTo(exprManager
, variableMap
), d_inUnsatCore
);
389 c
->d_result
= d_result
;
393 Command
* CheckSatCommand::clone() const
395 CheckSatCommand
* c
= new CheckSatCommand(d_expr
, d_inUnsatCore
);
396 c
->d_result
= d_result
;
400 std::string
CheckSatCommand::getCommandName() const { return "check-sat"; }
401 /* class QueryCommand */
403 QueryCommand::QueryCommand(const Expr
& e
, bool inUnsatCore
)
404 : d_expr(e
), d_inUnsatCore(inUnsatCore
)
408 Expr
QueryCommand::getExpr() const { return d_expr
; }
409 void QueryCommand::invoke(SmtEngine
* smtEngine
)
413 d_result
= smtEngine
->query(d_expr
);
414 d_commandStatus
= CommandSuccess::instance();
418 d_commandStatus
= new CommandFailure(e
.what());
422 Result
QueryCommand::getResult() const { return d_result
; }
423 void QueryCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
427 this->Command::printResult(out
, verbosity
);
431 out
<< d_result
<< endl
;
435 Command
* QueryCommand::exportTo(ExprManager
* exprManager
,
436 ExprManagerMapCollection
& variableMap
)
438 QueryCommand
* c
= new QueryCommand(d_expr
.exportTo(exprManager
, variableMap
),
440 c
->d_result
= d_result
;
444 Command
* QueryCommand::clone() const
446 QueryCommand
* c
= new QueryCommand(d_expr
, d_inUnsatCore
);
447 c
->d_result
= d_result
;
451 std::string
QueryCommand::getCommandName() const { return "query"; }
452 /* class CheckSynthCommand */
454 CheckSynthCommand::CheckSynthCommand() : d_expr() {}
455 CheckSynthCommand::CheckSynthCommand(const Expr
& expr
) : d_expr(expr
) {}
456 Expr
CheckSynthCommand::getExpr() const { return d_expr
; }
457 void CheckSynthCommand::invoke(SmtEngine
* smtEngine
)
461 d_result
= smtEngine
->checkSynth(d_expr
);
462 d_commandStatus
= CommandSuccess::instance();
463 smt::SmtScope
scope(smtEngine
);
465 // check whether we should print the status
466 if (d_result
.asSatisfiabilityResult() != Result::UNSAT
467 || options::sygusOut() == SYGUS_SOL_OUT_STATUS_AND_DEF
468 || options::sygusOut() == SYGUS_SOL_OUT_STATUS
)
470 if (options::sygusOut() == SYGUS_SOL_OUT_STANDARD
)
472 d_solution
<< "(fail)" << endl
;
476 d_solution
<< d_result
<< endl
;
479 // check whether we should print the solution
480 if (d_result
.asSatisfiabilityResult() == Result::UNSAT
481 && options::sygusOut() != SYGUS_SOL_OUT_STATUS
)
483 // printing a synthesis solution is a non-constant
484 // method, since it invokes a sophisticated algorithm
485 // (Figure 5 of Reynolds et al. CAV 2015).
486 // Hence, we must call here print solution here,
487 // instead of during printResult.
488 smtEngine
->printSynthSolution(d_solution
);
493 d_commandStatus
= new CommandFailure(e
.what());
497 Result
CheckSynthCommand::getResult() const { return d_result
; }
498 void CheckSynthCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
502 this->Command::printResult(out
, verbosity
);
506 out
<< d_solution
.str();
510 Command
* CheckSynthCommand::exportTo(ExprManager
* exprManager
,
511 ExprManagerMapCollection
& variableMap
)
513 CheckSynthCommand
* c
=
514 new CheckSynthCommand(d_expr
.exportTo(exprManager
, variableMap
));
515 c
->d_result
= d_result
;
519 Command
* CheckSynthCommand::clone() const
521 CheckSynthCommand
* c
= new CheckSynthCommand(d_expr
);
522 c
->d_result
= d_result
;
526 std::string
CheckSynthCommand::getCommandName() const { return "check-synth"; }
527 /* class ResetCommand */
529 void ResetCommand::invoke(SmtEngine
* smtEngine
)
534 d_commandStatus
= CommandSuccess::instance();
538 d_commandStatus
= new CommandFailure(e
.what());
542 Command
* ResetCommand::exportTo(ExprManager
* exprManager
,
543 ExprManagerMapCollection
& variableMap
)
545 return new ResetCommand();
548 Command
* ResetCommand::clone() const { return new ResetCommand(); }
549 std::string
ResetCommand::getCommandName() const { return "reset"; }
550 /* class ResetAssertionsCommand */
552 void ResetAssertionsCommand::invoke(SmtEngine
* smtEngine
)
556 smtEngine
->resetAssertions();
557 d_commandStatus
= CommandSuccess::instance();
561 d_commandStatus
= new CommandFailure(e
.what());
565 Command
* ResetAssertionsCommand::exportTo(ExprManager
* exprManager
,
566 ExprManagerMapCollection
& variableMap
)
568 return new ResetAssertionsCommand();
571 Command
* ResetAssertionsCommand::clone() const
573 return new ResetAssertionsCommand();
576 std::string
ResetAssertionsCommand::getCommandName() const
578 return "reset-assertions";
581 /* class QuitCommand */
583 void QuitCommand::invoke(SmtEngine
* smtEngine
)
585 Dump("benchmark") << *this;
586 d_commandStatus
= CommandSuccess::instance();
589 Command
* QuitCommand::exportTo(ExprManager
* exprManager
,
590 ExprManagerMapCollection
& variableMap
)
592 return new QuitCommand();
595 Command
* QuitCommand::clone() const { return new QuitCommand(); }
596 std::string
QuitCommand::getCommandName() const { return "exit"; }
597 /* class CommentCommand */
599 CommentCommand::CommentCommand(std::string comment
) : d_comment(comment
) {}
600 std::string
CommentCommand::getComment() const { return d_comment
; }
601 void CommentCommand::invoke(SmtEngine
* smtEngine
)
603 Dump("benchmark") << *this;
604 d_commandStatus
= CommandSuccess::instance();
607 Command
* CommentCommand::exportTo(ExprManager
* exprManager
,
608 ExprManagerMapCollection
& variableMap
)
610 return new CommentCommand(d_comment
);
613 Command
* CommentCommand::clone() const { return new CommentCommand(d_comment
); }
614 std::string
CommentCommand::getCommandName() const { return "comment"; }
615 /* class CommandSequence */
617 CommandSequence::CommandSequence() : d_index(0) {}
618 CommandSequence::~CommandSequence()
620 for (unsigned i
= d_index
; i
< d_commandSequence
.size(); ++i
)
622 delete d_commandSequence
[i
];
626 void CommandSequence::addCommand(Command
* cmd
)
628 d_commandSequence
.push_back(cmd
);
631 void CommandSequence::clear() { d_commandSequence
.clear(); }
632 void CommandSequence::invoke(SmtEngine
* smtEngine
)
634 for (; d_index
< d_commandSequence
.size(); ++d_index
)
636 d_commandSequence
[d_index
]->invoke(smtEngine
);
637 if (!d_commandSequence
[d_index
]->ok())
640 d_commandStatus
= d_commandSequence
[d_index
]->getCommandStatus();
643 delete d_commandSequence
[d_index
];
646 AlwaysAssert(d_commandStatus
== NULL
);
647 d_commandStatus
= CommandSuccess::instance();
650 void CommandSequence::invoke(SmtEngine
* smtEngine
, std::ostream
& out
)
652 for (; d_index
< d_commandSequence
.size(); ++d_index
)
654 d_commandSequence
[d_index
]->invoke(smtEngine
, out
);
655 if (!d_commandSequence
[d_index
]->ok())
658 d_commandStatus
= d_commandSequence
[d_index
]->getCommandStatus();
661 delete d_commandSequence
[d_index
];
664 AlwaysAssert(d_commandStatus
== NULL
);
665 d_commandStatus
= CommandSuccess::instance();
668 Command
* CommandSequence::exportTo(ExprManager
* exprManager
,
669 ExprManagerMapCollection
& variableMap
)
671 CommandSequence
* seq
= new CommandSequence();
672 for (iterator i
= begin(); i
!= end(); ++i
)
674 Command
* cmd_to_export
= *i
;
675 Command
* cmd
= cmd_to_export
->exportTo(exprManager
, variableMap
);
676 seq
->addCommand(cmd
);
677 Debug("export") << "[export] so far converted: " << seq
<< endl
;
679 seq
->d_index
= d_index
;
683 Command
* CommandSequence::clone() const
685 CommandSequence
* seq
= new CommandSequence();
686 for (const_iterator i
= begin(); i
!= end(); ++i
)
688 seq
->addCommand((*i
)->clone());
690 seq
->d_index
= d_index
;
694 CommandSequence::const_iterator
CommandSequence::begin() const
696 return d_commandSequence
.begin();
699 CommandSequence::const_iterator
CommandSequence::end() const
701 return d_commandSequence
.end();
704 CommandSequence::iterator
CommandSequence::begin()
706 return d_commandSequence
.begin();
709 CommandSequence::iterator
CommandSequence::end()
711 return d_commandSequence
.end();
714 std::string
CommandSequence::getCommandName() const { return "sequence"; }
715 /* class DeclarationSequenceCommand */
717 /* class DeclarationDefinitionCommand */
719 DeclarationDefinitionCommand::DeclarationDefinitionCommand(
720 const std::string
& id
)
725 std::string
DeclarationDefinitionCommand::getSymbol() const { return d_symbol
; }
726 /* class DeclareFunctionCommand */
728 DeclareFunctionCommand::DeclareFunctionCommand(const std::string
& id
,
731 : DeclarationDefinitionCommand(id
),
734 d_printInModel(true),
735 d_printInModelSetByUser(false)
739 Expr
DeclareFunctionCommand::getFunction() const { return d_func
; }
740 Type
DeclareFunctionCommand::getType() const { return d_type
; }
741 bool DeclareFunctionCommand::getPrintInModel() const { return d_printInModel
; }
742 bool DeclareFunctionCommand::getPrintInModelSetByUser() const
744 return d_printInModelSetByUser
;
747 void DeclareFunctionCommand::setPrintInModel(bool p
)
750 d_printInModelSetByUser
= true;
753 void DeclareFunctionCommand::invoke(SmtEngine
* smtEngine
)
755 d_commandStatus
= CommandSuccess::instance();
758 Command
* DeclareFunctionCommand::exportTo(ExprManager
* exprManager
,
759 ExprManagerMapCollection
& variableMap
)
761 DeclareFunctionCommand
* dfc
=
762 new DeclareFunctionCommand(d_symbol
,
763 d_func
.exportTo(exprManager
, variableMap
),
764 d_type
.exportTo(exprManager
, variableMap
));
765 dfc
->d_printInModel
= d_printInModel
;
766 dfc
->d_printInModelSetByUser
= d_printInModelSetByUser
;
770 Command
* DeclareFunctionCommand::clone() const
772 DeclareFunctionCommand
* dfc
=
773 new DeclareFunctionCommand(d_symbol
, d_func
, d_type
);
774 dfc
->d_printInModel
= d_printInModel
;
775 dfc
->d_printInModelSetByUser
= d_printInModelSetByUser
;
779 std::string
DeclareFunctionCommand::getCommandName() const
781 return "declare-fun";
784 /* class DeclareTypeCommand */
786 DeclareTypeCommand::DeclareTypeCommand(const std::string
& id
,
789 : DeclarationDefinitionCommand(id
), d_arity(arity
), d_type(t
)
793 size_t DeclareTypeCommand::getArity() const { return d_arity
; }
794 Type
DeclareTypeCommand::getType() const { return d_type
; }
795 void DeclareTypeCommand::invoke(SmtEngine
* smtEngine
)
797 d_commandStatus
= CommandSuccess::instance();
800 Command
* DeclareTypeCommand::exportTo(ExprManager
* exprManager
,
801 ExprManagerMapCollection
& variableMap
)
803 return new DeclareTypeCommand(
804 d_symbol
, d_arity
, d_type
.exportTo(exprManager
, variableMap
));
807 Command
* DeclareTypeCommand::clone() const
809 return new DeclareTypeCommand(d_symbol
, d_arity
, d_type
);
812 std::string
DeclareTypeCommand::getCommandName() const
814 return "declare-sort";
817 /* class DefineTypeCommand */
819 DefineTypeCommand::DefineTypeCommand(const std::string
& id
, Type t
)
820 : DeclarationDefinitionCommand(id
), d_params(), d_type(t
)
824 DefineTypeCommand::DefineTypeCommand(const std::string
& id
,
825 const std::vector
<Type
>& params
,
827 : DeclarationDefinitionCommand(id
), d_params(params
), d_type(t
)
831 const std::vector
<Type
>& DefineTypeCommand::getParameters() const
836 Type
DefineTypeCommand::getType() const { return d_type
; }
837 void DefineTypeCommand::invoke(SmtEngine
* smtEngine
)
839 d_commandStatus
= CommandSuccess::instance();
842 Command
* DefineTypeCommand::exportTo(ExprManager
* exprManager
,
843 ExprManagerMapCollection
& variableMap
)
846 transform(d_params
.begin(),
848 back_inserter(params
),
849 ExportTransformer(exprManager
, variableMap
));
850 Type type
= d_type
.exportTo(exprManager
, variableMap
);
851 return new DefineTypeCommand(d_symbol
, params
, type
);
854 Command
* DefineTypeCommand::clone() const
856 return new DefineTypeCommand(d_symbol
, d_params
, d_type
);
859 std::string
DefineTypeCommand::getCommandName() const { return "define-sort"; }
860 /* class DefineFunctionCommand */
862 DefineFunctionCommand::DefineFunctionCommand(const std::string
& id
,
865 : DeclarationDefinitionCommand(id
),
872 DefineFunctionCommand::DefineFunctionCommand(const std::string
& id
,
874 const std::vector
<Expr
>& formals
,
876 : DeclarationDefinitionCommand(id
),
883 Expr
DefineFunctionCommand::getFunction() const { return d_func
; }
884 const std::vector
<Expr
>& DefineFunctionCommand::getFormals() const
889 Expr
DefineFunctionCommand::getFormula() const { return d_formula
; }
890 void DefineFunctionCommand::invoke(SmtEngine
* smtEngine
)
894 if (!d_func
.isNull())
896 smtEngine
->defineFunction(d_func
, d_formals
, d_formula
);
898 d_commandStatus
= CommandSuccess::instance();
902 d_commandStatus
= new CommandFailure(e
.what());
906 Command
* DefineFunctionCommand::exportTo(ExprManager
* exprManager
,
907 ExprManagerMapCollection
& variableMap
)
909 Expr func
= d_func
.exportTo(
910 exprManager
, variableMap
, /* flags = */ ExprManager::VAR_FLAG_DEFINED
);
911 vector
<Expr
> formals
;
912 transform(d_formals
.begin(),
914 back_inserter(formals
),
915 ExportTransformer(exprManager
, variableMap
));
916 Expr formula
= d_formula
.exportTo(exprManager
, variableMap
);
917 return new DefineFunctionCommand(d_symbol
, func
, formals
, formula
);
920 Command
* DefineFunctionCommand::clone() const
922 return new DefineFunctionCommand(d_symbol
, d_func
, d_formals
, d_formula
);
925 std::string
DefineFunctionCommand::getCommandName() const
930 /* class DefineNamedFunctionCommand */
932 DefineNamedFunctionCommand::DefineNamedFunctionCommand(
933 const std::string
& id
,
935 const std::vector
<Expr
>& formals
,
937 : DefineFunctionCommand(id
, func
, formals
, formula
)
941 void DefineNamedFunctionCommand::invoke(SmtEngine
* smtEngine
)
943 this->DefineFunctionCommand::invoke(smtEngine
);
944 if (!d_func
.isNull() && d_func
.getType().isBoolean())
946 smtEngine
->addToAssignment(
947 d_func
.getExprManager()->mkExpr(kind::APPLY
, d_func
));
949 d_commandStatus
= CommandSuccess::instance();
952 Command
* DefineNamedFunctionCommand::exportTo(
953 ExprManager
* exprManager
, ExprManagerMapCollection
& variableMap
)
955 Expr func
= d_func
.exportTo(exprManager
, variableMap
);
956 vector
<Expr
> formals
;
957 transform(d_formals
.begin(),
959 back_inserter(formals
),
960 ExportTransformer(exprManager
, variableMap
));
961 Expr formula
= d_formula
.exportTo(exprManager
, variableMap
);
962 return new DefineNamedFunctionCommand(d_symbol
, func
, formals
, formula
);
965 Command
* DefineNamedFunctionCommand::clone() const
967 return new DefineNamedFunctionCommand(d_symbol
, d_func
, d_formals
, d_formula
);
970 /* class DefineFunctionRecCommand */
972 DefineFunctionRecCommand::DefineFunctionRecCommand(
973 Expr func
, const std::vector
<Expr
>& formals
, Expr formula
)
975 d_funcs
.push_back(func
);
976 d_formals
.push_back(formals
);
977 d_formulas
.push_back(formula
);
980 DefineFunctionRecCommand::DefineFunctionRecCommand(
981 const std::vector
<Expr
>& funcs
,
982 const std::vector
<std::vector
<Expr
> >& formals
,
983 const std::vector
<Expr
>& formulas
)
985 d_funcs
.insert(d_funcs
.end(), funcs
.begin(), funcs
.end());
986 d_formals
.insert(d_formals
.end(), formals
.begin(), formals
.end());
987 d_formulas
.insert(d_formulas
.end(), formulas
.begin(), formulas
.end());
990 const std::vector
<Expr
>& DefineFunctionRecCommand::getFunctions() const
995 const std::vector
<std::vector
<Expr
> >& DefineFunctionRecCommand::getFormals()
1001 const std::vector
<Expr
>& DefineFunctionRecCommand::getFormulas() const
1006 void DefineFunctionRecCommand::invoke(SmtEngine
* smtEngine
)
1010 smtEngine
->defineFunctionsRec(d_funcs
, d_formals
, d_formulas
);
1011 d_commandStatus
= CommandSuccess::instance();
1013 catch (exception
& e
)
1015 d_commandStatus
= new CommandFailure(e
.what());
1019 Command
* DefineFunctionRecCommand::exportTo(
1020 ExprManager
* exprManager
, ExprManagerMapCollection
& variableMap
)
1022 std::vector
<Expr
> funcs
;
1023 for (unsigned i
= 0, size
= d_funcs
.size(); i
< size
; i
++)
1025 Expr func
= d_funcs
[i
].exportTo(
1026 exprManager
, variableMap
, /* flags = */ ExprManager::VAR_FLAG_DEFINED
);
1027 funcs
.push_back(func
);
1029 std::vector
<std::vector
<Expr
> > formals
;
1030 for (unsigned i
= 0, size
= d_formals
.size(); i
< size
; i
++)
1032 std::vector
<Expr
> formals_c
;
1033 transform(d_formals
[i
].begin(),
1035 back_inserter(formals_c
),
1036 ExportTransformer(exprManager
, variableMap
));
1037 formals
.push_back(formals_c
);
1039 std::vector
<Expr
> formulas
;
1040 for (unsigned i
= 0, size
= d_formulas
.size(); i
< size
; i
++)
1042 Expr formula
= d_formulas
[i
].exportTo(exprManager
, variableMap
);
1043 formulas
.push_back(formula
);
1045 return new DefineFunctionRecCommand(funcs
, formals
, formulas
);
1048 Command
* DefineFunctionRecCommand::clone() const
1050 return new DefineFunctionRecCommand(d_funcs
, d_formals
, d_formulas
);
1053 std::string
DefineFunctionRecCommand::getCommandName() const
1055 return "define-fun-rec";
1058 /* class SetUserAttribute */
1060 SetUserAttributeCommand::SetUserAttributeCommand(
1061 const std::string
& attr
,
1063 const std::vector
<Expr
>& expr_values
,
1064 const std::string
& str_value
)
1067 d_expr_values(expr_values
),
1068 d_str_value(str_value
)
1072 SetUserAttributeCommand::SetUserAttributeCommand(const std::string
& attr
,
1074 : SetUserAttributeCommand(attr
, expr
, {}, "")
1078 SetUserAttributeCommand::SetUserAttributeCommand(
1079 const std::string
& attr
, Expr expr
, const std::vector
<Expr
>& values
)
1080 : SetUserAttributeCommand(attr
, expr
, values
, "")
1084 SetUserAttributeCommand::SetUserAttributeCommand(const std::string
& attr
,
1086 const std::string
& value
)
1087 : SetUserAttributeCommand(attr
, expr
, {}, value
)
1091 void SetUserAttributeCommand::invoke(SmtEngine
* smtEngine
)
1095 if (!d_expr
.isNull())
1097 smtEngine
->setUserAttribute(d_attr
, d_expr
, d_expr_values
, d_str_value
);
1099 d_commandStatus
= CommandSuccess::instance();
1101 catch (exception
& e
)
1103 d_commandStatus
= new CommandFailure(e
.what());
1107 Command
* SetUserAttributeCommand::exportTo(
1108 ExprManager
* exprManager
, ExprManagerMapCollection
& variableMap
)
1110 Expr expr
= d_expr
.exportTo(exprManager
, variableMap
);
1111 return new SetUserAttributeCommand(d_attr
, expr
, d_expr_values
, d_str_value
);
1114 Command
* SetUserAttributeCommand::clone() const
1116 return new SetUserAttributeCommand(
1117 d_attr
, d_expr
, d_expr_values
, d_str_value
);
1120 std::string
SetUserAttributeCommand::getCommandName() const
1122 return "set-user-attribute";
1125 /* class SimplifyCommand */
1127 SimplifyCommand::SimplifyCommand(Expr term
) : d_term(term
) {}
1128 Expr
SimplifyCommand::getTerm() const { return d_term
; }
1129 void SimplifyCommand::invoke(SmtEngine
* smtEngine
)
1133 d_result
= smtEngine
->simplify(d_term
);
1134 d_commandStatus
= CommandSuccess::instance();
1136 catch (UnsafeInterruptException
& e
)
1138 d_commandStatus
= new CommandInterrupted();
1140 catch (exception
& e
)
1142 d_commandStatus
= new CommandFailure(e
.what());
1146 Expr
SimplifyCommand::getResult() const { return d_result
; }
1147 void SimplifyCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
1151 this->Command::printResult(out
, verbosity
);
1155 out
<< d_result
<< endl
;
1159 Command
* SimplifyCommand::exportTo(ExprManager
* exprManager
,
1160 ExprManagerMapCollection
& variableMap
)
1162 SimplifyCommand
* c
=
1163 new SimplifyCommand(d_term
.exportTo(exprManager
, variableMap
));
1164 c
->d_result
= d_result
.exportTo(exprManager
, variableMap
);
1168 Command
* SimplifyCommand::clone() const
1170 SimplifyCommand
* c
= new SimplifyCommand(d_term
);
1171 c
->d_result
= d_result
;
1175 std::string
SimplifyCommand::getCommandName() const { return "simplify"; }
1176 /* class ExpandDefinitionsCommand */
1178 ExpandDefinitionsCommand::ExpandDefinitionsCommand(Expr term
) : d_term(term
) {}
1179 Expr
ExpandDefinitionsCommand::getTerm() const { return d_term
; }
1180 void ExpandDefinitionsCommand::invoke(SmtEngine
* smtEngine
)
1182 d_result
= smtEngine
->expandDefinitions(d_term
);
1183 d_commandStatus
= CommandSuccess::instance();
1186 Expr
ExpandDefinitionsCommand::getResult() const { return d_result
; }
1187 void ExpandDefinitionsCommand::printResult(std::ostream
& out
,
1188 uint32_t verbosity
) const
1192 this->Command::printResult(out
, verbosity
);
1196 out
<< d_result
<< endl
;
1200 Command
* ExpandDefinitionsCommand::exportTo(
1201 ExprManager
* exprManager
, ExprManagerMapCollection
& variableMap
)
1203 ExpandDefinitionsCommand
* c
=
1204 new ExpandDefinitionsCommand(d_term
.exportTo(exprManager
, variableMap
));
1205 c
->d_result
= d_result
.exportTo(exprManager
, variableMap
);
1209 Command
* ExpandDefinitionsCommand::clone() const
1211 ExpandDefinitionsCommand
* c
= new ExpandDefinitionsCommand(d_term
);
1212 c
->d_result
= d_result
;
1216 std::string
ExpandDefinitionsCommand::getCommandName() const
1218 return "expand-definitions";
1221 /* class GetValueCommand */
1223 GetValueCommand::GetValueCommand(Expr term
) : d_terms()
1225 d_terms
.push_back(term
);
1228 GetValueCommand::GetValueCommand(const std::vector
<Expr
>& terms
)
1231 PrettyCheckArgument(
1232 terms
.size() >= 1, terms
, "cannot get-value of an empty set of terms");
1235 const std::vector
<Expr
>& GetValueCommand::getTerms() const { return d_terms
; }
1236 void GetValueCommand::invoke(SmtEngine
* smtEngine
)
1240 vector
<Expr
> result
;
1241 ExprManager
* em
= smtEngine
->getExprManager();
1242 NodeManager
* nm
= NodeManager::fromExprManager(em
);
1243 for (std::vector
<Expr
>::const_iterator i
= d_terms
.begin();
1247 Assert(nm
== NodeManager::fromExprManager((*i
).getExprManager()));
1248 smt::SmtScope
scope(smtEngine
);
1249 Node request
= Node::fromExpr(
1250 options::expandDefinitions() ? smtEngine
->expandDefinitions(*i
) : *i
);
1251 Node value
= Node::fromExpr(smtEngine
->getValue(*i
));
1252 if (value
.getType().isInteger() && request
.getType() == nm
->realType())
1254 // Need to wrap in special marker so that output printers know this
1255 // is an integer-looking constant that really should be output as
1256 // a rational. Necessary for SMT-LIB standards compliance, but ugly.
1257 value
= nm
->mkNode(kind::APPLY_TYPE_ASCRIPTION
,
1258 nm
->mkConst(AscriptionType(em
->realType())),
1261 result
.push_back(nm
->mkNode(kind::SEXPR
, request
, value
).toExpr());
1263 d_result
= em
->mkExpr(kind::SEXPR
, result
);
1264 d_commandStatus
= CommandSuccess::instance();
1266 catch (RecoverableModalException
& e
)
1268 d_commandStatus
= new CommandRecoverableFailure(e
.what());
1270 catch (UnsafeInterruptException
& e
)
1272 d_commandStatus
= new CommandInterrupted();
1274 catch (exception
& e
)
1276 d_commandStatus
= new CommandFailure(e
.what());
1280 Expr
GetValueCommand::getResult() const { return d_result
; }
1281 void GetValueCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
1285 this->Command::printResult(out
, verbosity
);
1289 expr::ExprDag::Scope
scope(out
, false);
1290 out
<< d_result
<< endl
;
1294 Command
* GetValueCommand::exportTo(ExprManager
* exprManager
,
1295 ExprManagerMapCollection
& variableMap
)
1297 vector
<Expr
> exportedTerms
;
1298 for (std::vector
<Expr
>::const_iterator i
= d_terms
.begin();
1302 exportedTerms
.push_back((*i
).exportTo(exprManager
, variableMap
));
1304 GetValueCommand
* c
= new GetValueCommand(exportedTerms
);
1305 c
->d_result
= d_result
.exportTo(exprManager
, variableMap
);
1309 Command
* GetValueCommand::clone() const
1311 GetValueCommand
* c
= new GetValueCommand(d_terms
);
1312 c
->d_result
= d_result
;
1316 std::string
GetValueCommand::getCommandName() const { return "get-value"; }
1317 /* class GetAssignmentCommand */
1319 GetAssignmentCommand::GetAssignmentCommand() {}
1320 void GetAssignmentCommand::invoke(SmtEngine
* smtEngine
)
1324 std::vector
<std::pair
<Expr
,Expr
>> assignments
= smtEngine
->getAssignment();
1325 vector
<SExpr
> sexprs
;
1326 for (const auto& p
: assignments
)
1329 if (p
.first
.getKind() == kind::APPLY
)
1331 v
.emplace_back(SExpr::Keyword(p
.first
.getOperator().toString()));
1335 v
.emplace_back(SExpr::Keyword(p
.first
.toString()));
1337 v
.emplace_back(SExpr::Keyword(p
.second
.toString()));
1338 sexprs
.emplace_back(v
);
1340 d_result
= SExpr(sexprs
);
1341 d_commandStatus
= CommandSuccess::instance();
1343 catch (RecoverableModalException
& e
)
1345 d_commandStatus
= new CommandRecoverableFailure(e
.what());
1347 catch (UnsafeInterruptException
& e
)
1349 d_commandStatus
= new CommandInterrupted();
1351 catch (exception
& e
)
1353 d_commandStatus
= new CommandFailure(e
.what());
1357 SExpr
GetAssignmentCommand::getResult() const { return d_result
; }
1358 void GetAssignmentCommand::printResult(std::ostream
& out
,
1359 uint32_t verbosity
) const
1363 this->Command::printResult(out
, verbosity
);
1367 out
<< d_result
<< endl
;
1371 Command
* GetAssignmentCommand::exportTo(ExprManager
* exprManager
,
1372 ExprManagerMapCollection
& variableMap
)
1374 GetAssignmentCommand
* c
= new GetAssignmentCommand();
1375 c
->d_result
= d_result
;
1379 Command
* GetAssignmentCommand::clone() const
1381 GetAssignmentCommand
* c
= new GetAssignmentCommand();
1382 c
->d_result
= d_result
;
1386 std::string
GetAssignmentCommand::getCommandName() const
1388 return "get-assignment";
1391 /* class GetModelCommand */
1393 GetModelCommand::GetModelCommand() : d_result(nullptr), d_smtEngine(nullptr) {}
1394 void GetModelCommand::invoke(SmtEngine
* smtEngine
)
1398 d_result
= smtEngine
->getModel();
1399 d_smtEngine
= smtEngine
;
1400 d_commandStatus
= CommandSuccess::instance();
1402 catch (RecoverableModalException
& e
)
1404 d_commandStatus
= new CommandRecoverableFailure(e
.what());
1406 catch (UnsafeInterruptException
& e
)
1408 d_commandStatus
= new CommandInterrupted();
1410 catch (exception
& e
)
1412 d_commandStatus
= new CommandFailure(e
.what());
1416 /* Model is private to the library -- for now
1417 Model* GetModelCommand::getResult() const {
1422 void GetModelCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
1426 this->Command::printResult(out
, verbosity
);
1434 Command
* GetModelCommand::exportTo(ExprManager
* exprManager
,
1435 ExprManagerMapCollection
& variableMap
)
1437 GetModelCommand
* c
= new GetModelCommand();
1438 c
->d_result
= d_result
;
1439 c
->d_smtEngine
= d_smtEngine
;
1443 Command
* GetModelCommand::clone() const
1445 GetModelCommand
* c
= new GetModelCommand();
1446 c
->d_result
= d_result
;
1447 c
->d_smtEngine
= d_smtEngine
;
1451 std::string
GetModelCommand::getCommandName() const { return "get-model"; }
1452 /* class GetProofCommand */
1454 GetProofCommand::GetProofCommand() : d_smtEngine(nullptr), d_result(nullptr) {}
1455 void GetProofCommand::invoke(SmtEngine
* smtEngine
)
1459 d_smtEngine
= smtEngine
;
1460 d_result
= &smtEngine
->getProof();
1461 d_commandStatus
= CommandSuccess::instance();
1463 catch (RecoverableModalException
& e
)
1465 d_commandStatus
= new CommandRecoverableFailure(e
.what());
1467 catch (UnsafeInterruptException
& e
)
1469 d_commandStatus
= new CommandInterrupted();
1471 catch (exception
& e
)
1473 d_commandStatus
= new CommandFailure(e
.what());
1477 const Proof
& GetProofCommand::getResult() const { return *d_result
; }
1478 void GetProofCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
1482 this->Command::printResult(out
, verbosity
);
1486 smt::SmtScope
scope(d_smtEngine
);
1487 d_result
->toStream(out
);
1491 Command
* GetProofCommand::exportTo(ExprManager
* exprManager
,
1492 ExprManagerMapCollection
& variableMap
)
1494 GetProofCommand
* c
= new GetProofCommand();
1495 c
->d_result
= d_result
;
1496 c
->d_smtEngine
= d_smtEngine
;
1500 Command
* GetProofCommand::clone() const
1502 GetProofCommand
* c
= new GetProofCommand();
1503 c
->d_result
= d_result
;
1504 c
->d_smtEngine
= d_smtEngine
;
1508 std::string
GetProofCommand::getCommandName() const { return "get-proof"; }
1509 /* class GetInstantiationsCommand */
1511 GetInstantiationsCommand::GetInstantiationsCommand() : d_smtEngine(nullptr) {}
1512 void GetInstantiationsCommand::invoke(SmtEngine
* smtEngine
)
1516 d_smtEngine
= smtEngine
;
1517 d_commandStatus
= CommandSuccess::instance();
1519 catch (exception
& e
)
1521 d_commandStatus
= new CommandFailure(e
.what());
1525 void GetInstantiationsCommand::printResult(std::ostream
& out
,
1526 uint32_t verbosity
) const
1530 this->Command::printResult(out
, verbosity
);
1534 d_smtEngine
->printInstantiations(out
);
1538 Command
* GetInstantiationsCommand::exportTo(
1539 ExprManager
* exprManager
, ExprManagerMapCollection
& variableMap
)
1541 GetInstantiationsCommand
* c
= new GetInstantiationsCommand();
1542 // c->d_result = d_result;
1543 c
->d_smtEngine
= d_smtEngine
;
1547 Command
* GetInstantiationsCommand::clone() const
1549 GetInstantiationsCommand
* c
= new GetInstantiationsCommand();
1550 // c->d_result = d_result;
1551 c
->d_smtEngine
= d_smtEngine
;
1555 std::string
GetInstantiationsCommand::getCommandName() const
1557 return "get-instantiations";
1560 /* class GetSynthSolutionCommand */
1562 GetSynthSolutionCommand::GetSynthSolutionCommand() : d_smtEngine(nullptr) {}
1563 void GetSynthSolutionCommand::invoke(SmtEngine
* smtEngine
)
1567 d_smtEngine
= smtEngine
;
1568 d_commandStatus
= CommandSuccess::instance();
1570 catch (exception
& e
)
1572 d_commandStatus
= new CommandFailure(e
.what());
1576 void GetSynthSolutionCommand::printResult(std::ostream
& out
,
1577 uint32_t verbosity
) const
1581 this->Command::printResult(out
, verbosity
);
1585 d_smtEngine
->printSynthSolution(out
);
1589 Command
* GetSynthSolutionCommand::exportTo(
1590 ExprManager
* exprManager
, ExprManagerMapCollection
& variableMap
)
1592 GetSynthSolutionCommand
* c
= new GetSynthSolutionCommand();
1593 c
->d_smtEngine
= d_smtEngine
;
1597 Command
* GetSynthSolutionCommand::clone() const
1599 GetSynthSolutionCommand
* c
= new GetSynthSolutionCommand();
1600 c
->d_smtEngine
= d_smtEngine
;
1604 std::string
GetSynthSolutionCommand::getCommandName() const
1606 return "get-instantiations";
1609 /* class GetQuantifierEliminationCommand */
1611 GetQuantifierEliminationCommand::GetQuantifierEliminationCommand() : d_expr() {}
1612 GetQuantifierEliminationCommand::GetQuantifierEliminationCommand(
1613 const Expr
& expr
, bool doFull
)
1614 : d_expr(expr
), d_doFull(doFull
)
1618 Expr
GetQuantifierEliminationCommand::getExpr() const { return d_expr
; }
1619 bool GetQuantifierEliminationCommand::getDoFull() const { return d_doFull
; }
1620 void GetQuantifierEliminationCommand::invoke(SmtEngine
* smtEngine
)
1624 d_result
= smtEngine
->doQuantifierElimination(d_expr
, d_doFull
);
1625 d_commandStatus
= CommandSuccess::instance();
1627 catch (exception
& e
)
1629 d_commandStatus
= new CommandFailure(e
.what());
1633 Expr
GetQuantifierEliminationCommand::getResult() const { return d_result
; }
1634 void GetQuantifierEliminationCommand::printResult(std::ostream
& out
,
1635 uint32_t verbosity
) const
1639 this->Command::printResult(out
, verbosity
);
1643 out
<< d_result
<< endl
;
1647 Command
* GetQuantifierEliminationCommand::exportTo(
1648 ExprManager
* exprManager
, ExprManagerMapCollection
& variableMap
)
1650 GetQuantifierEliminationCommand
* c
= new GetQuantifierEliminationCommand(
1651 d_expr
.exportTo(exprManager
, variableMap
), d_doFull
);
1652 c
->d_result
= d_result
;
1656 Command
* GetQuantifierEliminationCommand::clone() const
1658 GetQuantifierEliminationCommand
* c
=
1659 new GetQuantifierEliminationCommand(d_expr
, d_doFull
);
1660 c
->d_result
= d_result
;
1664 std::string
GetQuantifierEliminationCommand::getCommandName() const
1666 return d_doFull
? "get-qe" : "get-qe-disjunct";
1669 /* class GetUnsatCoreCommand */
1671 GetUnsatCoreCommand::GetUnsatCoreCommand() {}
1672 void GetUnsatCoreCommand::invoke(SmtEngine
* smtEngine
)
1676 d_result
= smtEngine
->getUnsatCore();
1677 d_commandStatus
= CommandSuccess::instance();
1679 catch (RecoverableModalException
& e
)
1681 d_commandStatus
= new CommandRecoverableFailure(e
.what());
1683 catch (exception
& e
)
1685 d_commandStatus
= new CommandFailure(e
.what());
1689 void GetUnsatCoreCommand::printResult(std::ostream
& out
,
1690 uint32_t verbosity
) const
1694 this->Command::printResult(out
, verbosity
);
1698 d_result
.toStream(out
);
1702 const UnsatCore
& GetUnsatCoreCommand::getUnsatCore() const
1704 // of course, this will be empty if the command hasn't been invoked yet
1708 Command
* GetUnsatCoreCommand::exportTo(ExprManager
* exprManager
,
1709 ExprManagerMapCollection
& variableMap
)
1711 GetUnsatCoreCommand
* c
= new GetUnsatCoreCommand
;
1712 c
->d_result
= d_result
;
1716 Command
* GetUnsatCoreCommand::clone() const
1718 GetUnsatCoreCommand
* c
= new GetUnsatCoreCommand
;
1719 c
->d_result
= d_result
;
1723 std::string
GetUnsatCoreCommand::getCommandName() const
1725 return "get-unsat-core";
1728 /* class GetAssertionsCommand */
1730 GetAssertionsCommand::GetAssertionsCommand() {}
1731 void GetAssertionsCommand::invoke(SmtEngine
* smtEngine
)
1736 const vector
<Expr
> v
= smtEngine
->getAssertions();
1738 copy(v
.begin(), v
.end(), ostream_iterator
<Expr
>(ss
, "\n"));
1740 d_result
= ss
.str();
1741 d_commandStatus
= CommandSuccess::instance();
1743 catch (exception
& e
)
1745 d_commandStatus
= new CommandFailure(e
.what());
1749 std::string
GetAssertionsCommand::getResult() const { return d_result
; }
1750 void GetAssertionsCommand::printResult(std::ostream
& out
,
1751 uint32_t verbosity
) const
1755 this->Command::printResult(out
, verbosity
);
1763 Command
* GetAssertionsCommand::exportTo(ExprManager
* exprManager
,
1764 ExprManagerMapCollection
& variableMap
)
1766 GetAssertionsCommand
* c
= new GetAssertionsCommand();
1767 c
->d_result
= d_result
;
1771 Command
* GetAssertionsCommand::clone() const
1773 GetAssertionsCommand
* c
= new GetAssertionsCommand();
1774 c
->d_result
= d_result
;
1778 std::string
GetAssertionsCommand::getCommandName() const
1780 return "get-assertions";
1783 /* class SetBenchmarkStatusCommand */
1785 SetBenchmarkStatusCommand::SetBenchmarkStatusCommand(BenchmarkStatus status
)
1790 BenchmarkStatus
SetBenchmarkStatusCommand::getStatus() const
1795 void SetBenchmarkStatusCommand::invoke(SmtEngine
* smtEngine
)
1801 SExpr status
= SExpr(ss
.str());
1802 smtEngine
->setInfo("status", status
);
1803 d_commandStatus
= CommandSuccess::instance();
1805 catch (exception
& e
)
1807 d_commandStatus
= new CommandFailure(e
.what());
1811 Command
* SetBenchmarkStatusCommand::exportTo(
1812 ExprManager
* exprManager
, ExprManagerMapCollection
& variableMap
)
1814 return new SetBenchmarkStatusCommand(d_status
);
1817 Command
* SetBenchmarkStatusCommand::clone() const
1819 return new SetBenchmarkStatusCommand(d_status
);
1822 std::string
SetBenchmarkStatusCommand::getCommandName() const
1827 /* class SetBenchmarkLogicCommand */
1829 SetBenchmarkLogicCommand::SetBenchmarkLogicCommand(std::string logic
)
1834 std::string
SetBenchmarkLogicCommand::getLogic() const { return d_logic
; }
1835 void SetBenchmarkLogicCommand::invoke(SmtEngine
* smtEngine
)
1839 smtEngine
->setLogic(d_logic
);
1840 d_commandStatus
= CommandSuccess::instance();
1842 catch (exception
& e
)
1844 d_commandStatus
= new CommandFailure(e
.what());
1848 Command
* SetBenchmarkLogicCommand::exportTo(
1849 ExprManager
* exprManager
, ExprManagerMapCollection
& variableMap
)
1851 return new SetBenchmarkLogicCommand(d_logic
);
1854 Command
* SetBenchmarkLogicCommand::clone() const
1856 return new SetBenchmarkLogicCommand(d_logic
);
1859 std::string
SetBenchmarkLogicCommand::getCommandName() const
1864 /* class SetInfoCommand */
1866 SetInfoCommand::SetInfoCommand(std::string flag
, const SExpr
& sexpr
)
1867 : d_flag(flag
), d_sexpr(sexpr
)
1871 std::string
SetInfoCommand::getFlag() const { return d_flag
; }
1872 SExpr
SetInfoCommand::getSExpr() const { return d_sexpr
; }
1873 void SetInfoCommand::invoke(SmtEngine
* smtEngine
)
1877 smtEngine
->setInfo(d_flag
, d_sexpr
);
1878 d_commandStatus
= CommandSuccess::instance();
1880 catch (UnrecognizedOptionException
&)
1882 // As per SMT-LIB spec, silently accept unknown set-info keys
1883 d_commandStatus
= CommandSuccess::instance();
1885 catch (exception
& e
)
1887 d_commandStatus
= new CommandFailure(e
.what());
1891 Command
* SetInfoCommand::exportTo(ExprManager
* exprManager
,
1892 ExprManagerMapCollection
& variableMap
)
1894 return new SetInfoCommand(d_flag
, d_sexpr
);
1897 Command
* SetInfoCommand::clone() const
1899 return new SetInfoCommand(d_flag
, d_sexpr
);
1902 std::string
SetInfoCommand::getCommandName() const { return "set-info"; }
1903 /* class GetInfoCommand */
1905 GetInfoCommand::GetInfoCommand(std::string flag
) : d_flag(flag
) {}
1906 std::string
GetInfoCommand::getFlag() const { return d_flag
; }
1907 void GetInfoCommand::invoke(SmtEngine
* smtEngine
)
1912 v
.push_back(SExpr(SExpr::Keyword(string(":") + d_flag
)));
1913 v
.push_back(smtEngine
->getInfo(d_flag
));
1915 if (d_flag
== "all-options" || d_flag
== "all-statistics")
1917 ss
<< PrettySExprs(true);
1920 d_result
= ss
.str();
1921 d_commandStatus
= CommandSuccess::instance();
1923 catch (UnrecognizedOptionException
&)
1925 d_commandStatus
= new CommandUnsupported();
1927 catch (exception
& e
)
1929 d_commandStatus
= new CommandFailure(e
.what());
1933 std::string
GetInfoCommand::getResult() const { return d_result
; }
1934 void GetInfoCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
1938 this->Command::printResult(out
, verbosity
);
1940 else if (d_result
!= "")
1942 out
<< d_result
<< endl
;
1946 Command
* GetInfoCommand::exportTo(ExprManager
* exprManager
,
1947 ExprManagerMapCollection
& variableMap
)
1949 GetInfoCommand
* c
= new GetInfoCommand(d_flag
);
1950 c
->d_result
= d_result
;
1954 Command
* GetInfoCommand::clone() const
1956 GetInfoCommand
* c
= new GetInfoCommand(d_flag
);
1957 c
->d_result
= d_result
;
1961 std::string
GetInfoCommand::getCommandName() const { return "get-info"; }
1962 /* class SetOptionCommand */
1964 SetOptionCommand::SetOptionCommand(std::string flag
, const SExpr
& sexpr
)
1965 : d_flag(flag
), d_sexpr(sexpr
)
1969 std::string
SetOptionCommand::getFlag() const { return d_flag
; }
1970 SExpr
SetOptionCommand::getSExpr() const { return d_sexpr
; }
1971 void SetOptionCommand::invoke(SmtEngine
* smtEngine
)
1975 smtEngine
->setOption(d_flag
, d_sexpr
);
1976 d_commandStatus
= CommandSuccess::instance();
1978 catch (UnrecognizedOptionException
&)
1980 d_commandStatus
= new CommandUnsupported();
1982 catch (exception
& e
)
1984 d_commandStatus
= new CommandFailure(e
.what());
1988 Command
* SetOptionCommand::exportTo(ExprManager
* exprManager
,
1989 ExprManagerMapCollection
& variableMap
)
1991 return new SetOptionCommand(d_flag
, d_sexpr
);
1994 Command
* SetOptionCommand::clone() const
1996 return new SetOptionCommand(d_flag
, d_sexpr
);
1999 std::string
SetOptionCommand::getCommandName() const { return "set-option"; }
2000 /* class GetOptionCommand */
2002 GetOptionCommand::GetOptionCommand(std::string flag
) : d_flag(flag
) {}
2003 std::string
GetOptionCommand::getFlag() const { return d_flag
; }
2004 void GetOptionCommand::invoke(SmtEngine
* smtEngine
)
2008 SExpr res
= smtEngine
->getOption(d_flag
);
2009 d_result
= res
.toString();
2010 d_commandStatus
= CommandSuccess::instance();
2012 catch (UnrecognizedOptionException
&)
2014 d_commandStatus
= new CommandUnsupported();
2016 catch (exception
& e
)
2018 d_commandStatus
= new CommandFailure(e
.what());
2022 std::string
GetOptionCommand::getResult() const { return d_result
; }
2023 void GetOptionCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
2027 this->Command::printResult(out
, verbosity
);
2029 else if (d_result
!= "")
2031 out
<< d_result
<< endl
;
2035 Command
* GetOptionCommand::exportTo(ExprManager
* exprManager
,
2036 ExprManagerMapCollection
& variableMap
)
2038 GetOptionCommand
* c
= new GetOptionCommand(d_flag
);
2039 c
->d_result
= d_result
;
2043 Command
* GetOptionCommand::clone() const
2045 GetOptionCommand
* c
= new GetOptionCommand(d_flag
);
2046 c
->d_result
= d_result
;
2050 std::string
GetOptionCommand::getCommandName() const { return "get-option"; }
2051 /* class SetExpressionNameCommand */
2053 SetExpressionNameCommand::SetExpressionNameCommand(Expr expr
, std::string name
)
2054 : d_expr(expr
), d_name(name
)
2058 void SetExpressionNameCommand::invoke(SmtEngine
* smtEngine
)
2060 smtEngine
->setExpressionName(d_expr
, d_name
);
2061 d_commandStatus
= CommandSuccess::instance();
2064 Command
* SetExpressionNameCommand::exportTo(
2065 ExprManager
* exprManager
, ExprManagerMapCollection
& variableMap
)
2067 SetExpressionNameCommand
* c
= new SetExpressionNameCommand(
2068 d_expr
.exportTo(exprManager
, variableMap
), d_name
);
2072 Command
* SetExpressionNameCommand::clone() const
2074 SetExpressionNameCommand
* c
= new SetExpressionNameCommand(d_expr
, d_name
);
2078 std::string
SetExpressionNameCommand::getCommandName() const
2080 return "set-expr-name";
2083 /* class DatatypeDeclarationCommand */
2085 DatatypeDeclarationCommand::DatatypeDeclarationCommand(
2086 const DatatypeType
& datatype
)
2089 d_datatypes
.push_back(datatype
);
2092 DatatypeDeclarationCommand::DatatypeDeclarationCommand(
2093 const std::vector
<DatatypeType
>& datatypes
)
2094 : d_datatypes(datatypes
)
2098 const std::vector
<DatatypeType
>& DatatypeDeclarationCommand::getDatatypes()
2104 void DatatypeDeclarationCommand::invoke(SmtEngine
* smtEngine
)
2106 d_commandStatus
= CommandSuccess::instance();
2109 Command
* DatatypeDeclarationCommand::exportTo(
2110 ExprManager
* exprManager
, ExprManagerMapCollection
& variableMap
)
2112 throw ExportUnsupportedException(
2113 "export of DatatypeDeclarationCommand unsupported");
2116 Command
* DatatypeDeclarationCommand::clone() const
2118 return new DatatypeDeclarationCommand(d_datatypes
);
2121 std::string
DatatypeDeclarationCommand::getCommandName() const
2123 return "declare-datatypes";
2126 /* class RewriteRuleCommand */
2128 RewriteRuleCommand::RewriteRuleCommand(const std::vector
<Expr
>& vars
,
2129 const std::vector
<Expr
>& guards
,
2132 const Triggers
& triggers
)
2137 d_triggers(triggers
)
2141 RewriteRuleCommand::RewriteRuleCommand(const std::vector
<Expr
>& vars
,
2144 : d_vars(vars
), d_head(head
), d_body(body
)
2148 const std::vector
<Expr
>& RewriteRuleCommand::getVars() const { return d_vars
; }
2149 const std::vector
<Expr
>& RewriteRuleCommand::getGuards() const
2154 Expr
RewriteRuleCommand::getHead() const { return d_head
; }
2155 Expr
RewriteRuleCommand::getBody() const { return d_body
; }
2156 const RewriteRuleCommand::Triggers
& RewriteRuleCommand::getTriggers() const
2161 void RewriteRuleCommand::invoke(SmtEngine
* smtEngine
)
2165 ExprManager
* em
= smtEngine
->getExprManager();
2166 /** build vars list */
2167 Expr vars
= em
->mkExpr(kind::BOUND_VAR_LIST
, d_vars
);
2168 /** build guards list */
2170 if (d_guards
.size() == 0)
2171 guards
= em
->mkConst
<bool>(true);
2172 else if (d_guards
.size() == 1)
2173 guards
= d_guards
[0];
2175 guards
= em
->mkExpr(kind::AND
, d_guards
);
2176 /** build expression */
2178 if (d_triggers
.empty())
2180 expr
= em
->mkExpr(kind::RR_REWRITE
, vars
, guards
, d_head
, d_body
);
2184 /** build triggers list */
2185 std::vector
<Expr
> vtriggers
;
2186 vtriggers
.reserve(d_triggers
.size());
2187 for (Triggers::const_iterator i
= d_triggers
.begin(),
2188 end
= d_triggers
.end();
2192 vtriggers
.push_back(em
->mkExpr(kind::INST_PATTERN
, *i
));
2194 Expr triggers
= em
->mkExpr(kind::INST_PATTERN_LIST
, vtriggers
);
2196 em
->mkExpr(kind::RR_REWRITE
, vars
, guards
, d_head
, d_body
, triggers
);
2198 smtEngine
->assertFormula(expr
);
2199 d_commandStatus
= CommandSuccess::instance();
2201 catch (exception
& e
)
2203 d_commandStatus
= new CommandFailure(e
.what());
2207 Command
* RewriteRuleCommand::exportTo(ExprManager
* exprManager
,
2208 ExprManagerMapCollection
& variableMap
)
2210 /** Convert variables */
2211 VExpr vars
= ExportTo(exprManager
, variableMap
, d_vars
);
2212 /** Convert guards */
2213 VExpr guards
= ExportTo(exprManager
, variableMap
, d_guards
);
2214 /** Convert triggers */
2216 triggers
.reserve(d_triggers
.size());
2217 for (const std::vector
<Expr
>& trigger_list
: d_triggers
)
2219 triggers
.push_back(ExportTo(exprManager
, variableMap
, trigger_list
));
2221 /** Convert head and body */
2222 Expr head
= d_head
.exportTo(exprManager
, variableMap
);
2223 Expr body
= d_body
.exportTo(exprManager
, variableMap
);
2224 /** Create the converted rules */
2225 return new RewriteRuleCommand(vars
, guards
, head
, body
, triggers
);
2228 Command
* RewriteRuleCommand::clone() const
2230 return new RewriteRuleCommand(d_vars
, d_guards
, d_head
, d_body
, d_triggers
);
2233 std::string
RewriteRuleCommand::getCommandName() const
2235 return "rewrite-rule";
2238 /* class PropagateRuleCommand */
2240 PropagateRuleCommand::PropagateRuleCommand(const std::vector
<Expr
>& vars
,
2241 const std::vector
<Expr
>& guards
,
2242 const std::vector
<Expr
>& heads
,
2244 const Triggers
& triggers
,
2250 d_triggers(triggers
),
2251 d_deduction(deduction
)
2255 PropagateRuleCommand::PropagateRuleCommand(const std::vector
<Expr
>& vars
,
2256 const std::vector
<Expr
>& heads
,
2259 : d_vars(vars
), d_heads(heads
), d_body(body
), d_deduction(deduction
)
2263 const std::vector
<Expr
>& PropagateRuleCommand::getVars() const
2268 const std::vector
<Expr
>& PropagateRuleCommand::getGuards() const
2273 const std::vector
<Expr
>& PropagateRuleCommand::getHeads() const
2278 Expr
PropagateRuleCommand::getBody() const { return d_body
; }
2279 const PropagateRuleCommand::Triggers
& PropagateRuleCommand::getTriggers() const
2284 bool PropagateRuleCommand::isDeduction() const { return d_deduction
; }
2285 void PropagateRuleCommand::invoke(SmtEngine
* smtEngine
)
2289 ExprManager
* em
= smtEngine
->getExprManager();
2290 /** build vars list */
2291 Expr vars
= em
->mkExpr(kind::BOUND_VAR_LIST
, d_vars
);
2292 /** build guards list */
2294 if (d_guards
.size() == 0)
2295 guards
= em
->mkConst
<bool>(true);
2296 else if (d_guards
.size() == 1)
2297 guards
= d_guards
[0];
2299 guards
= em
->mkExpr(kind::AND
, d_guards
);
2300 /** build heads list */
2302 if (d_heads
.size() == 1)
2305 heads
= em
->mkExpr(kind::AND
, d_heads
);
2306 /** build expression */
2308 if (d_triggers
.empty())
2310 expr
= em
->mkExpr(kind::RR_REWRITE
, vars
, guards
, heads
, d_body
);
2314 /** build triggers list */
2315 std::vector
<Expr
> vtriggers
;
2316 vtriggers
.reserve(d_triggers
.size());
2317 for (Triggers::const_iterator i
= d_triggers
.begin(),
2318 end
= d_triggers
.end();
2322 vtriggers
.push_back(em
->mkExpr(kind::INST_PATTERN
, *i
));
2324 Expr triggers
= em
->mkExpr(kind::INST_PATTERN_LIST
, vtriggers
);
2326 em
->mkExpr(kind::RR_REWRITE
, vars
, guards
, heads
, d_body
, triggers
);
2328 smtEngine
->assertFormula(expr
);
2329 d_commandStatus
= CommandSuccess::instance();
2331 catch (exception
& e
)
2333 d_commandStatus
= new CommandFailure(e
.what());
2337 Command
* PropagateRuleCommand::exportTo(ExprManager
* exprManager
,
2338 ExprManagerMapCollection
& variableMap
)
2340 /** Convert variables */
2341 VExpr vars
= ExportTo(exprManager
, variableMap
, d_vars
);
2342 /** Convert guards */
2343 VExpr guards
= ExportTo(exprManager
, variableMap
, d_guards
);
2344 /** Convert heads */
2345 VExpr heads
= ExportTo(exprManager
, variableMap
, d_heads
);
2346 /** Convert triggers */
2348 triggers
.reserve(d_triggers
.size());
2349 for (const std::vector
<Expr
>& trigger_list
: d_triggers
)
2351 triggers
.push_back(ExportTo(exprManager
, variableMap
, trigger_list
));
2353 /** Convert head and body */
2354 Expr body
= d_body
.exportTo(exprManager
, variableMap
);
2355 /** Create the converted rules */
2356 return new PropagateRuleCommand(vars
, guards
, heads
, body
, triggers
);
2359 Command
* PropagateRuleCommand::clone() const
2361 return new PropagateRuleCommand(
2362 d_vars
, d_guards
, d_heads
, d_body
, d_triggers
);
2365 std::string
PropagateRuleCommand::getCommandName() const
2367 return "propagate-rule";
2370 /* output stream insertion operator for benchmark statuses */
2371 std::ostream
& operator<<(std::ostream
& out
, BenchmarkStatus status
)
2375 case SMT_SATISFIABLE
: return out
<< "sat";
2377 case SMT_UNSATISFIABLE
: return out
<< "unsat";
2379 case SMT_UNKNOWN
: return out
<< "unknown";
2381 default: return out
<< "BenchmarkStatus::[UNKNOWNSTATUS!]";
2385 } /* CVC4 namespace */