1 /********************* */
4 ** Top contributors (to current version):
5 ** Tim King, Morgan Deters, Andrew Reynolds
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2020 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 "api/cvc4cpp.h"
27 #include "base/check.h"
28 #include "base/output.h"
29 #include "expr/expr_iomanip.h"
30 #include "expr/node.h"
31 #include "expr/type.h"
32 #include "options/options.h"
33 #include "options/smt_options.h"
34 #include "printer/printer.h"
35 #include "proof/unsat_core.h"
37 #include "smt/model.h"
38 #include "smt/smt_engine.h"
39 #include "smt/smt_engine_scope.h"
40 #include "util/sexpr.h"
41 #include "util/utility.h"
47 const int CommandPrintSuccess::s_iosIndex
= std::ios_base::xalloc();
48 const CommandSuccess
* CommandSuccess::s_instance
= new CommandSuccess();
49 const CommandInterrupted
* CommandInterrupted::s_instance
=
50 new CommandInterrupted();
52 std::ostream
& operator<<(std::ostream
& out
, const Command
& c
)
55 Node::setdepth::getDepth(out
),
56 Node::printtypes::getPrintTypes(out
),
57 Node::dag::getDag(out
),
58 Node::setlanguage::getLanguage(out
));
62 ostream
& operator<<(ostream
& out
, const Command
* c
)
75 std::ostream
& operator<<(std::ostream
& out
, const CommandStatus
& s
)
77 s
.toStream(out
, Node::setlanguage::getLanguage(out
));
81 ostream
& operator<<(ostream
& out
, const CommandStatus
* s
)
94 /* output stream insertion operator for benchmark statuses */
95 std::ostream
& operator<<(std::ostream
& out
, BenchmarkStatus status
)
99 case SMT_SATISFIABLE
: return out
<< "sat";
101 case SMT_UNSATISFIABLE
: return out
<< "unsat";
103 case SMT_UNKNOWN
: return out
<< "unknown";
105 default: return out
<< "BenchmarkStatus::[UNKNOWNSTATUS!]";
109 /* -------------------------------------------------------------------------- */
110 /* class CommandPrintSuccess */
111 /* -------------------------------------------------------------------------- */
113 void CommandPrintSuccess::applyPrintSuccess(std::ostream
& out
)
115 out
.iword(s_iosIndex
) = d_printSuccess
;
118 bool CommandPrintSuccess::getPrintSuccess(std::ostream
& out
)
120 return out
.iword(s_iosIndex
);
123 void CommandPrintSuccess::setPrintSuccess(std::ostream
& out
, bool printSuccess
)
125 out
.iword(s_iosIndex
) = printSuccess
;
128 std::ostream
& operator<<(std::ostream
& out
, CommandPrintSuccess cps
)
130 cps
.applyPrintSuccess(out
);
134 /* -------------------------------------------------------------------------- */
136 /* -------------------------------------------------------------------------- */
138 Command::Command() : d_commandStatus(nullptr), d_muted(false) {}
140 Command::Command(const api::Solver
* solver
)
141 : d_commandStatus(nullptr), d_muted(false)
145 Command::Command(const Command
& cmd
)
148 (cmd
.d_commandStatus
== NULL
) ? NULL
: &cmd
.d_commandStatus
->clone();
149 d_muted
= cmd
.d_muted
;
154 if (d_commandStatus
!= NULL
&& d_commandStatus
!= CommandSuccess::instance())
156 delete d_commandStatus
;
160 bool Command::ok() const
162 // either we haven't run the command yet, or it ran successfully
163 return d_commandStatus
== NULL
164 || dynamic_cast<const CommandSuccess
*>(d_commandStatus
) != NULL
;
167 bool Command::fail() const
169 return d_commandStatus
!= NULL
170 && dynamic_cast<const CommandFailure
*>(d_commandStatus
) != NULL
;
173 bool Command::interrupted() const
175 return d_commandStatus
!= NULL
176 && dynamic_cast<const CommandInterrupted
*>(d_commandStatus
) != NULL
;
179 void Command::invoke(api::Solver
* solver
, std::ostream
& out
)
182 if (!(isMuted() && ok()))
186 std::stoul(solver
->getOption("command-verbosity:" + getCommandName())));
190 std::string
Command::toString() const
192 std::stringstream ss
;
197 void CommandStatus::toStream(std::ostream
& out
, OutputLanguage language
) const
199 Printer::getPrinter(language
)->toStream(out
, this);
202 void Command::printResult(std::ostream
& out
, uint32_t verbosity
) const
204 if (d_commandStatus
!= NULL
)
206 if ((!ok() && verbosity
>= 1) || verbosity
>= 2)
208 out
<< *d_commandStatus
;
213 /* -------------------------------------------------------------------------- */
214 /* class EmptyCommand */
215 /* -------------------------------------------------------------------------- */
217 EmptyCommand::EmptyCommand(std::string name
) : d_name(name
) {}
218 std::string
EmptyCommand::getName() const { return d_name
; }
219 void EmptyCommand::invoke(api::Solver
* solver
)
221 /* empty commands have no implementation */
222 d_commandStatus
= CommandSuccess::instance();
225 Command
* EmptyCommand::clone() const { return new EmptyCommand(d_name
); }
226 std::string
EmptyCommand::getCommandName() const { return "empty"; }
228 void EmptyCommand::toStream(std::ostream
& out
,
232 OutputLanguage language
) const
234 Printer::getPrinter(language
)->toStreamCmdEmpty(out
, d_name
);
237 /* -------------------------------------------------------------------------- */
238 /* class EchoCommand */
239 /* -------------------------------------------------------------------------- */
241 EchoCommand::EchoCommand(std::string output
) : d_output(output
) {}
242 std::string
EchoCommand::getOutput() const { return d_output
; }
243 void EchoCommand::invoke(api::Solver
* solver
)
245 /* we don't have an output stream here, nothing to do */
246 d_commandStatus
= CommandSuccess::instance();
249 void EchoCommand::invoke(api::Solver
* solver
, std::ostream
& out
)
251 out
<< d_output
<< std::endl
;
252 Trace("dtview::command") << "* ~COMMAND: echo |" << d_output
<< "|~"
254 d_commandStatus
= CommandSuccess::instance();
257 std::stoul(solver
->getOption("command-verbosity:" + getCommandName())));
260 Command
* EchoCommand::clone() const { return new EchoCommand(d_output
); }
261 std::string
EchoCommand::getCommandName() const { return "echo"; }
263 void EchoCommand::toStream(std::ostream
& out
,
267 OutputLanguage language
) const
269 Printer::getPrinter(language
)->toStreamCmdEcho(out
, d_output
);
272 /* -------------------------------------------------------------------------- */
273 /* class AssertCommand */
274 /* -------------------------------------------------------------------------- */
276 AssertCommand::AssertCommand(const api::Term
& t
, bool inUnsatCore
)
277 : d_term(t
), d_inUnsatCore(inUnsatCore
)
281 api::Term
AssertCommand::getTerm() const { return d_term
; }
282 void AssertCommand::invoke(api::Solver
* solver
)
286 solver
->getSmtEngine()->assertFormula(d_term
.getNode(), d_inUnsatCore
);
287 d_commandStatus
= CommandSuccess::instance();
289 catch (UnsafeInterruptException
& e
)
291 d_commandStatus
= new CommandInterrupted();
295 d_commandStatus
= new CommandFailure(e
.what());
299 Command
* AssertCommand::clone() const
301 return new AssertCommand(d_term
, d_inUnsatCore
);
304 std::string
AssertCommand::getCommandName() const { return "assert"; }
306 void AssertCommand::toStream(std::ostream
& out
,
310 OutputLanguage language
) const
312 Printer::getPrinter(language
)->toStreamCmdAssert(out
, d_term
.getNode());
315 /* -------------------------------------------------------------------------- */
316 /* class PushCommand */
317 /* -------------------------------------------------------------------------- */
319 void PushCommand::invoke(api::Solver
* solver
)
324 d_commandStatus
= CommandSuccess::instance();
326 catch (UnsafeInterruptException
& e
)
328 d_commandStatus
= new CommandInterrupted();
332 d_commandStatus
= new CommandFailure(e
.what());
336 Command
* PushCommand::clone() const { return new PushCommand(); }
337 std::string
PushCommand::getCommandName() const { return "push"; }
339 void PushCommand::toStream(std::ostream
& out
,
343 OutputLanguage language
) const
345 Printer::getPrinter(language
)->toStreamCmdPush(out
);
348 /* -------------------------------------------------------------------------- */
349 /* class PopCommand */
350 /* -------------------------------------------------------------------------- */
352 void PopCommand::invoke(api::Solver
* solver
)
357 d_commandStatus
= CommandSuccess::instance();
359 catch (UnsafeInterruptException
& e
)
361 d_commandStatus
= new CommandInterrupted();
365 d_commandStatus
= new CommandFailure(e
.what());
369 Command
* PopCommand::clone() const { return new PopCommand(); }
370 std::string
PopCommand::getCommandName() const { return "pop"; }
372 void PopCommand::toStream(std::ostream
& out
,
376 OutputLanguage language
) const
378 Printer::getPrinter(language
)->toStreamCmdPop(out
);
381 /* -------------------------------------------------------------------------- */
382 /* class CheckSatCommand */
383 /* -------------------------------------------------------------------------- */
385 CheckSatCommand::CheckSatCommand() : d_term() {}
387 CheckSatCommand::CheckSatCommand(const api::Term
& term
) : d_term(term
) {}
389 api::Term
CheckSatCommand::getTerm() const { return d_term
; }
390 void CheckSatCommand::invoke(api::Solver
* solver
)
392 Trace("dtview::command") << "* ~COMMAND: " << getCommandName() << "~"
397 d_term
.isNull() ? solver
->checkSat() : solver
->checkSatAssuming(d_term
);
399 d_commandStatus
= CommandSuccess::instance();
403 d_commandStatus
= new CommandFailure(e
.what());
407 api::Result
CheckSatCommand::getResult() const { return d_result
; }
408 void CheckSatCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
412 this->Command::printResult(out
, verbosity
);
416 Trace("dtview::command") << "* RESULT: " << d_result
<< std::endl
;
417 out
<< d_result
<< endl
;
421 Command
* CheckSatCommand::clone() const
423 CheckSatCommand
* c
= new CheckSatCommand(d_term
);
424 c
->d_result
= d_result
;
428 std::string
CheckSatCommand::getCommandName() const { return "check-sat"; }
430 void CheckSatCommand::toStream(std::ostream
& out
,
434 OutputLanguage language
) const
436 Printer::getPrinter(language
)->toStreamCmdCheckSat(out
, d_term
.getNode());
439 /* -------------------------------------------------------------------------- */
440 /* class CheckSatAssumingCommand */
441 /* -------------------------------------------------------------------------- */
443 CheckSatAssumingCommand::CheckSatAssumingCommand(api::Term term
)
448 CheckSatAssumingCommand::CheckSatAssumingCommand(
449 const std::vector
<api::Term
>& terms
)
454 const std::vector
<api::Term
>& CheckSatAssumingCommand::getTerms() const
459 void CheckSatAssumingCommand::invoke(api::Solver
* solver
)
461 Trace("dtview::command") << "* ~COMMAND: (check-sat-assuming ( " << d_terms
462 << " )~" << std::endl
;
465 d_result
= solver
->checkSatAssuming(d_terms
);
466 d_commandStatus
= CommandSuccess::instance();
470 d_commandStatus
= new CommandFailure(e
.what());
474 api::Result
CheckSatAssumingCommand::getResult() const
476 Trace("dtview::command") << "* ~RESULT: " << d_result
<< "~" << std::endl
;
480 void CheckSatAssumingCommand::printResult(std::ostream
& out
,
481 uint32_t verbosity
) const
485 this->Command::printResult(out
, verbosity
);
489 out
<< d_result
<< endl
;
493 Command
* CheckSatAssumingCommand::clone() const
495 CheckSatAssumingCommand
* c
= new CheckSatAssumingCommand(d_terms
);
496 c
->d_result
= d_result
;
500 std::string
CheckSatAssumingCommand::getCommandName() const
502 return "check-sat-assuming";
505 void CheckSatAssumingCommand::toStream(std::ostream
& out
,
509 OutputLanguage language
) const
511 Printer::getPrinter(language
)->toStreamCmdCheckSatAssuming(
512 out
, api::termVectorToNodes(d_terms
));
515 /* -------------------------------------------------------------------------- */
516 /* class QueryCommand */
517 /* -------------------------------------------------------------------------- */
519 QueryCommand::QueryCommand(const api::Term
& t
, bool inUnsatCore
)
520 : d_term(t
), d_inUnsatCore(inUnsatCore
)
524 api::Term
QueryCommand::getTerm() const { return d_term
; }
525 void QueryCommand::invoke(api::Solver
* solver
)
529 d_result
= solver
->checkEntailed(d_term
);
530 d_commandStatus
= CommandSuccess::instance();
534 d_commandStatus
= new CommandFailure(e
.what());
538 api::Result
QueryCommand::getResult() const { return d_result
; }
539 void QueryCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
543 this->Command::printResult(out
, verbosity
);
547 out
<< d_result
<< endl
;
551 Command
* QueryCommand::clone() const
553 QueryCommand
* c
= new QueryCommand(d_term
, d_inUnsatCore
);
554 c
->d_result
= d_result
;
558 std::string
QueryCommand::getCommandName() const { return "query"; }
560 void QueryCommand::toStream(std::ostream
& out
,
564 OutputLanguage language
) const
566 Printer::getPrinter(language
)->toStreamCmdQuery(out
, d_term
.getNode());
569 /* -------------------------------------------------------------------------- */
570 /* class DeclareSygusVarCommand */
571 /* -------------------------------------------------------------------------- */
573 DeclareSygusVarCommand::DeclareSygusVarCommand(const std::string
& id
,
576 : DeclarationDefinitionCommand(id
), d_var(var
), d_sort(sort
)
580 api::Term
DeclareSygusVarCommand::getVar() const { return d_var
; }
581 api::Sort
DeclareSygusVarCommand::getSort() const { return d_sort
; }
583 void DeclareSygusVarCommand::invoke(api::Solver
* solver
)
585 d_commandStatus
= CommandSuccess::instance();
588 Command
* DeclareSygusVarCommand::clone() const
590 return new DeclareSygusVarCommand(d_symbol
, d_var
, d_sort
);
593 std::string
DeclareSygusVarCommand::getCommandName() const
595 return "declare-var";
598 void DeclareSygusVarCommand::toStream(std::ostream
& out
,
602 OutputLanguage language
) const
604 Printer::getPrinter(language
)->toStreamCmdDeclareVar(
605 out
, d_var
.getNode(), TypeNode::fromType(d_sort
.getType()));
608 /* -------------------------------------------------------------------------- */
609 /* class SynthFunCommand */
610 /* -------------------------------------------------------------------------- */
612 SynthFunCommand::SynthFunCommand(const std::string
& id
,
614 const std::vector
<api::Term
>& vars
,
618 : DeclarationDefinitionCommand(id
),
627 api::Term
SynthFunCommand::getFunction() const { return d_fun
; }
629 const std::vector
<api::Term
>& SynthFunCommand::getVars() const
634 api::Sort
SynthFunCommand::getSort() const { return d_sort
; }
635 bool SynthFunCommand::isInv() const { return d_isInv
; }
637 const api::Grammar
* SynthFunCommand::getGrammar() const { return d_grammar
; }
639 void SynthFunCommand::invoke(api::Solver
* solver
)
641 d_commandStatus
= CommandSuccess::instance();
644 Command
* SynthFunCommand::clone() const
646 return new SynthFunCommand(
647 d_symbol
, d_fun
, d_vars
, d_sort
, d_isInv
, d_grammar
);
650 std::string
SynthFunCommand::getCommandName() const
652 return d_isInv
? "synth-inv" : "synth-fun";
655 void SynthFunCommand::toStream(std::ostream
& out
,
659 OutputLanguage language
) const
661 std::vector
<Node
> nodeVars
= termVectorToNodes(d_vars
);
662 Printer::getPrinter(language
)->toStreamCmdSynthFun(
666 TypeNode::fromType(d_sort
.getType()),
668 TypeNode::fromType(d_grammar
->resolve().getType()));
671 /* -------------------------------------------------------------------------- */
672 /* class SygusConstraintCommand */
673 /* -------------------------------------------------------------------------- */
675 SygusConstraintCommand::SygusConstraintCommand(const api::Term
& t
) : d_term(t
)
679 void SygusConstraintCommand::invoke(api::Solver
* solver
)
683 solver
->addSygusConstraint(d_term
);
684 d_commandStatus
= CommandSuccess::instance();
688 d_commandStatus
= new CommandFailure(e
.what());
692 api::Term
SygusConstraintCommand::getTerm() const { return d_term
; }
694 Command
* SygusConstraintCommand::clone() const
696 return new SygusConstraintCommand(d_term
);
699 std::string
SygusConstraintCommand::getCommandName() const
704 void SygusConstraintCommand::toStream(std::ostream
& out
,
708 OutputLanguage language
) const
710 Printer::getPrinter(language
)->toStreamCmdConstraint(out
, d_term
.getNode());
713 /* -------------------------------------------------------------------------- */
714 /* class SygusInvConstraintCommand */
715 /* -------------------------------------------------------------------------- */
717 SygusInvConstraintCommand::SygusInvConstraintCommand(
718 const std::vector
<api::Term
>& predicates
)
719 : d_predicates(predicates
)
723 SygusInvConstraintCommand::SygusInvConstraintCommand(const api::Term
& inv
,
724 const api::Term
& pre
,
725 const api::Term
& trans
,
726 const api::Term
& post
)
727 : SygusInvConstraintCommand(std::vector
<api::Term
>{inv
, pre
, trans
, post
})
731 void SygusInvConstraintCommand::invoke(api::Solver
* solver
)
735 solver
->addSygusInvConstraint(
736 d_predicates
[0], d_predicates
[1], d_predicates
[2], d_predicates
[3]);
737 d_commandStatus
= CommandSuccess::instance();
741 d_commandStatus
= new CommandFailure(e
.what());
745 const std::vector
<api::Term
>& SygusInvConstraintCommand::getPredicates() const
750 Command
* SygusInvConstraintCommand::clone() const
752 return new SygusInvConstraintCommand(d_predicates
);
755 std::string
SygusInvConstraintCommand::getCommandName() const
757 return "inv-constraint";
760 void SygusInvConstraintCommand::toStream(std::ostream
& out
,
764 OutputLanguage language
) const
766 Printer::getPrinter(language
)->toStreamCmdInvConstraint(
768 d_predicates
[0].getNode(),
769 d_predicates
[1].getNode(),
770 d_predicates
[2].getNode(),
771 d_predicates
[3].getNode());
774 /* -------------------------------------------------------------------------- */
775 /* class CheckSynthCommand */
776 /* -------------------------------------------------------------------------- */
778 void CheckSynthCommand::invoke(api::Solver
* solver
)
782 d_result
= solver
->checkSynth();
783 d_commandStatus
= CommandSuccess::instance();
785 // check whether we should print the status
786 if (!d_result
.isUnsat()
787 || options::sygusOut() == options::SygusSolutionOutMode::STATUS_AND_DEF
788 || options::sygusOut() == options::SygusSolutionOutMode::STATUS
)
790 if (options::sygusOut() == options::SygusSolutionOutMode::STANDARD
)
792 d_solution
<< "(fail)" << endl
;
796 d_solution
<< d_result
<< endl
;
799 // check whether we should print the solution
800 if (d_result
.isUnsat()
801 && options::sygusOut() != options::SygusSolutionOutMode::STATUS
)
803 // printing a synthesis solution is a non-constant
804 // method, since it invokes a sophisticated algorithm
805 // (Figure 5 of Reynolds et al. CAV 2015).
806 // Hence, we must call here print solution here,
807 // instead of during printResult.
808 solver
->printSynthSolution(d_solution
);
813 d_commandStatus
= new CommandFailure(e
.what());
817 api::Result
CheckSynthCommand::getResult() const { return d_result
; }
818 void CheckSynthCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
822 this->Command::printResult(out
, verbosity
);
826 out
<< d_solution
.str();
830 Command
* CheckSynthCommand::clone() const { return new CheckSynthCommand(); }
832 std::string
CheckSynthCommand::getCommandName() const { return "check-synth"; }
834 void CheckSynthCommand::toStream(std::ostream
& out
,
838 OutputLanguage language
) const
840 Printer::getPrinter(language
)->toStreamCmdCheckSynth(out
);
843 /* -------------------------------------------------------------------------- */
844 /* class ResetCommand */
845 /* -------------------------------------------------------------------------- */
847 void ResetCommand::invoke(api::Solver
* solver
)
851 solver
->getSmtEngine()->reset();
852 d_commandStatus
= CommandSuccess::instance();
856 d_commandStatus
= new CommandFailure(e
.what());
860 Command
* ResetCommand::clone() const { return new ResetCommand(); }
861 std::string
ResetCommand::getCommandName() const { return "reset"; }
863 void ResetCommand::toStream(std::ostream
& out
,
867 OutputLanguage language
) const
869 Printer::getPrinter(language
)->toStreamCmdReset(out
);
872 /* -------------------------------------------------------------------------- */
873 /* class ResetAssertionsCommand */
874 /* -------------------------------------------------------------------------- */
876 void ResetAssertionsCommand::invoke(api::Solver
* solver
)
880 solver
->resetAssertions();
881 d_commandStatus
= CommandSuccess::instance();
885 d_commandStatus
= new CommandFailure(e
.what());
889 Command
* ResetAssertionsCommand::clone() const
891 return new ResetAssertionsCommand();
894 std::string
ResetAssertionsCommand::getCommandName() const
896 return "reset-assertions";
899 void ResetAssertionsCommand::toStream(std::ostream
& out
,
903 OutputLanguage language
) const
905 Printer::getPrinter(language
)->toStreamCmdResetAssertions(out
);
908 /* -------------------------------------------------------------------------- */
909 /* class QuitCommand */
910 /* -------------------------------------------------------------------------- */
912 void QuitCommand::invoke(api::Solver
* solver
)
914 Dump("benchmark") << *this;
915 d_commandStatus
= CommandSuccess::instance();
918 Command
* QuitCommand::clone() const { return new QuitCommand(); }
919 std::string
QuitCommand::getCommandName() const { return "exit"; }
921 void QuitCommand::toStream(std::ostream
& out
,
925 OutputLanguage language
) const
927 Printer::getPrinter(language
)->toStreamCmdQuit(out
);
930 /* -------------------------------------------------------------------------- */
931 /* class CommentCommand */
932 /* -------------------------------------------------------------------------- */
934 CommentCommand::CommentCommand(std::string comment
) : d_comment(comment
) {}
935 std::string
CommentCommand::getComment() const { return d_comment
; }
936 void CommentCommand::invoke(api::Solver
* solver
)
938 Dump("benchmark") << *this;
939 d_commandStatus
= CommandSuccess::instance();
942 Command
* CommentCommand::clone() const { return new CommentCommand(d_comment
); }
943 std::string
CommentCommand::getCommandName() const { return "comment"; }
945 void CommentCommand::toStream(std::ostream
& out
,
949 OutputLanguage language
) const
951 Printer::getPrinter(language
)->toStreamCmdComment(out
, d_comment
);
954 /* -------------------------------------------------------------------------- */
955 /* class CommandSequence */
956 /* -------------------------------------------------------------------------- */
958 CommandSequence::CommandSequence() : d_index(0) {}
959 CommandSequence::~CommandSequence()
961 for (unsigned i
= d_index
; i
< d_commandSequence
.size(); ++i
)
963 delete d_commandSequence
[i
];
967 void CommandSequence::addCommand(Command
* cmd
)
969 d_commandSequence
.push_back(cmd
);
972 void CommandSequence::clear() { d_commandSequence
.clear(); }
973 void CommandSequence::invoke(api::Solver
* solver
)
975 for (; d_index
< d_commandSequence
.size(); ++d_index
)
977 d_commandSequence
[d_index
]->invoke(solver
);
978 if (!d_commandSequence
[d_index
]->ok())
981 d_commandStatus
= d_commandSequence
[d_index
]->getCommandStatus();
984 delete d_commandSequence
[d_index
];
987 AlwaysAssert(d_commandStatus
== NULL
);
988 d_commandStatus
= CommandSuccess::instance();
991 void CommandSequence::invoke(api::Solver
* solver
, std::ostream
& out
)
993 for (; d_index
< d_commandSequence
.size(); ++d_index
)
995 d_commandSequence
[d_index
]->invoke(solver
, out
);
996 if (!d_commandSequence
[d_index
]->ok())
999 d_commandStatus
= d_commandSequence
[d_index
]->getCommandStatus();
1002 delete d_commandSequence
[d_index
];
1005 AlwaysAssert(d_commandStatus
== NULL
);
1006 d_commandStatus
= CommandSuccess::instance();
1009 Command
* CommandSequence::clone() const
1011 CommandSequence
* seq
= new CommandSequence();
1012 for (const_iterator i
= begin(); i
!= end(); ++i
)
1014 seq
->addCommand((*i
)->clone());
1016 seq
->d_index
= d_index
;
1020 CommandSequence::const_iterator
CommandSequence::begin() const
1022 return d_commandSequence
.begin();
1025 CommandSequence::const_iterator
CommandSequence::end() const
1027 return d_commandSequence
.end();
1030 CommandSequence::iterator
CommandSequence::begin()
1032 return d_commandSequence
.begin();
1035 CommandSequence::iterator
CommandSequence::end()
1037 return d_commandSequence
.end();
1040 std::string
CommandSequence::getCommandName() const { return "sequence"; }
1042 void CommandSequence::toStream(std::ostream
& out
,
1046 OutputLanguage language
) const
1048 Printer::getPrinter(language
)->toStreamCmdCommandSequence(out
,
1052 /* -------------------------------------------------------------------------- */
1053 /* class DeclarationSequence */
1054 /* -------------------------------------------------------------------------- */
1056 void DeclarationSequence::toStream(std::ostream
& out
,
1060 OutputLanguage language
) const
1062 Printer::getPrinter(language
)->toStreamCmdDeclarationSequence(
1063 out
, d_commandSequence
);
1066 /* -------------------------------------------------------------------------- */
1067 /* class DeclarationDefinitionCommand */
1068 /* -------------------------------------------------------------------------- */
1070 DeclarationDefinitionCommand::DeclarationDefinitionCommand(
1071 const std::string
& id
)
1076 std::string
DeclarationDefinitionCommand::getSymbol() const { return d_symbol
; }
1078 /* -------------------------------------------------------------------------- */
1079 /* class DeclareFunctionCommand */
1080 /* -------------------------------------------------------------------------- */
1082 DeclareFunctionCommand::DeclareFunctionCommand(const std::string
& id
,
1085 : DeclarationDefinitionCommand(id
),
1088 d_printInModel(true),
1089 d_printInModelSetByUser(false)
1093 api::Term
DeclareFunctionCommand::getFunction() const { return d_func
; }
1094 api::Sort
DeclareFunctionCommand::getSort() const { return d_sort
; }
1095 bool DeclareFunctionCommand::getPrintInModel() const { return d_printInModel
; }
1096 bool DeclareFunctionCommand::getPrintInModelSetByUser() const
1098 return d_printInModelSetByUser
;
1101 void DeclareFunctionCommand::setPrintInModel(bool p
)
1104 d_printInModelSetByUser
= true;
1107 void DeclareFunctionCommand::invoke(api::Solver
* solver
)
1109 d_commandStatus
= CommandSuccess::instance();
1112 Command
* DeclareFunctionCommand::clone() const
1114 DeclareFunctionCommand
* dfc
=
1115 new DeclareFunctionCommand(d_symbol
, d_func
, d_sort
);
1116 dfc
->d_printInModel
= d_printInModel
;
1117 dfc
->d_printInModelSetByUser
= d_printInModelSetByUser
;
1121 std::string
DeclareFunctionCommand::getCommandName() const
1123 return "declare-fun";
1126 void DeclareFunctionCommand::toStream(std::ostream
& out
,
1130 OutputLanguage language
) const
1132 Printer::getPrinter(language
)->toStreamCmdDeclareFunction(
1133 out
, d_func
.toString(), TypeNode::fromType(d_sort
.getType()));
1136 /* -------------------------------------------------------------------------- */
1137 /* class DeclareSortCommand */
1138 /* -------------------------------------------------------------------------- */
1140 DeclareSortCommand::DeclareSortCommand(const std::string
& id
,
1143 : DeclarationDefinitionCommand(id
), d_arity(arity
), d_sort(sort
)
1147 size_t DeclareSortCommand::getArity() const { return d_arity
; }
1148 api::Sort
DeclareSortCommand::getSort() const { return d_sort
; }
1149 void DeclareSortCommand::invoke(api::Solver
* solver
)
1151 d_commandStatus
= CommandSuccess::instance();
1154 Command
* DeclareSortCommand::clone() const
1156 return new DeclareSortCommand(d_symbol
, d_arity
, d_sort
);
1159 std::string
DeclareSortCommand::getCommandName() const
1161 return "declare-sort";
1164 void DeclareSortCommand::toStream(std::ostream
& out
,
1168 OutputLanguage language
) const
1170 Printer::getPrinter(language
)->toStreamCmdDeclareType(
1171 out
, d_sort
.toString(), d_arity
, TypeNode::fromType(d_sort
.getType()));
1174 /* -------------------------------------------------------------------------- */
1175 /* class DefineSortCommand */
1176 /* -------------------------------------------------------------------------- */
1178 DefineSortCommand::DefineSortCommand(const std::string
& id
, api::Sort sort
)
1179 : DeclarationDefinitionCommand(id
), d_params(), d_sort(sort
)
1183 DefineSortCommand::DefineSortCommand(const std::string
& id
,
1184 const std::vector
<api::Sort
>& params
,
1186 : DeclarationDefinitionCommand(id
), d_params(params
), d_sort(sort
)
1190 const std::vector
<api::Sort
>& DefineSortCommand::getParameters() const
1195 api::Sort
DefineSortCommand::getSort() const { return d_sort
; }
1196 void DefineSortCommand::invoke(api::Solver
* solver
)
1198 d_commandStatus
= CommandSuccess::instance();
1201 Command
* DefineSortCommand::clone() const
1203 return new DefineSortCommand(d_symbol
, d_params
, d_sort
);
1206 std::string
DefineSortCommand::getCommandName() const { return "define-sort"; }
1208 void DefineSortCommand::toStream(std::ostream
& out
,
1212 OutputLanguage language
) const
1214 Printer::getPrinter(language
)->toStreamCmdDefineType(
1217 api::sortVectorToTypeNodes(d_params
),
1218 TypeNode::fromType(d_sort
.getType()));
1221 /* -------------------------------------------------------------------------- */
1222 /* class DefineFunctionCommand */
1223 /* -------------------------------------------------------------------------- */
1225 DefineFunctionCommand::DefineFunctionCommand(const std::string
& id
,
1229 : DeclarationDefinitionCommand(id
),
1237 DefineFunctionCommand::DefineFunctionCommand(
1238 const std::string
& id
,
1240 const std::vector
<api::Term
>& formals
,
1243 : DeclarationDefinitionCommand(id
),
1251 api::Term
DefineFunctionCommand::getFunction() const { return d_func
; }
1252 const std::vector
<api::Term
>& DefineFunctionCommand::getFormals() const
1257 api::Term
DefineFunctionCommand::getFormula() const { return d_formula
; }
1258 void DefineFunctionCommand::invoke(api::Solver
* solver
)
1262 if (!d_func
.isNull())
1264 solver
->defineFun(d_func
, d_formals
, d_formula
, d_global
);
1266 d_commandStatus
= CommandSuccess::instance();
1268 catch (exception
& e
)
1270 d_commandStatus
= new CommandFailure(e
.what());
1274 Command
* DefineFunctionCommand::clone() const
1276 return new DefineFunctionCommand(
1277 d_symbol
, d_func
, d_formals
, d_formula
, d_global
);
1280 std::string
DefineFunctionCommand::getCommandName() const
1282 return "define-fun";
1285 void DefineFunctionCommand::toStream(std::ostream
& out
,
1289 OutputLanguage language
) const
1291 Printer::getPrinter(language
)->toStreamCmdDefineFunction(
1294 api::termVectorToNodes(d_formals
),
1295 d_func
.getNode().getType().getRangeType(),
1296 d_formula
.getNode());
1299 /* -------------------------------------------------------------------------- */
1300 /* class DefineNamedFunctionCommand */
1301 /* -------------------------------------------------------------------------- */
1303 DefineNamedFunctionCommand::DefineNamedFunctionCommand(
1305 const std::string
& id
,
1307 const std::vector
<api::Term
>& formals
,
1310 : DefineFunctionCommand(id
, func
, formals
, formula
, global
)
1314 void DefineNamedFunctionCommand::invoke(api::Solver
* solver
)
1316 this->DefineFunctionCommand::invoke(solver
);
1317 if (!d_func
.isNull() && d_func
.getSort().isBoolean())
1319 solver
->getSmtEngine()->addToAssignment(d_func
.getExpr());
1321 d_commandStatus
= CommandSuccess::instance();
1324 Command
* DefineNamedFunctionCommand::clone() const
1326 return new DefineNamedFunctionCommand(
1327 d_symbol
, d_func
, d_formals
, d_formula
, d_global
);
1330 void DefineNamedFunctionCommand::toStream(std::ostream
& out
,
1334 OutputLanguage language
) const
1336 Printer::getPrinter(language
)->toStreamCmdDefineNamedFunction(
1339 api::termVectorToNodes(d_formals
),
1340 TypeNode::fromType(d_func
.getSort().getFunctionCodomainSort().getType()),
1341 d_formula
.getNode());
1344 /* -------------------------------------------------------------------------- */
1345 /* class DefineFunctionRecCommand */
1346 /* -------------------------------------------------------------------------- */
1348 DefineFunctionRecCommand::DefineFunctionRecCommand(
1351 const std::vector
<api::Term
>& formals
,
1356 d_funcs
.push_back(func
);
1357 d_formals
.push_back(formals
);
1358 d_formulas
.push_back(formula
);
1361 DefineFunctionRecCommand::DefineFunctionRecCommand(
1363 const std::vector
<api::Term
>& funcs
,
1364 const std::vector
<std::vector
<api::Term
>>& formals
,
1365 const std::vector
<api::Term
>& formulas
,
1367 : d_funcs(funcs
), d_formals(formals
), d_formulas(formulas
), d_global(global
)
1371 const std::vector
<api::Term
>& DefineFunctionRecCommand::getFunctions() const
1376 const std::vector
<std::vector
<api::Term
>>&
1377 DefineFunctionRecCommand::getFormals() const
1382 const std::vector
<api::Term
>& DefineFunctionRecCommand::getFormulas() const
1387 void DefineFunctionRecCommand::invoke(api::Solver
* solver
)
1391 solver
->defineFunsRec(d_funcs
, d_formals
, d_formulas
, d_global
);
1392 d_commandStatus
= CommandSuccess::instance();
1394 catch (exception
& e
)
1396 d_commandStatus
= new CommandFailure(e
.what());
1400 Command
* DefineFunctionRecCommand::clone() const
1402 return new DefineFunctionRecCommand(d_funcs
, d_formals
, d_formulas
, d_global
);
1405 std::string
DefineFunctionRecCommand::getCommandName() const
1407 return "define-fun-rec";
1410 void DefineFunctionRecCommand::toStream(std::ostream
& out
,
1414 OutputLanguage language
) const
1416 std::vector
<std::vector
<Node
>> formals
;
1417 formals
.reserve(d_formals
.size());
1418 for (const std::vector
<api::Term
>& formal
: d_formals
)
1420 formals
.push_back(api::termVectorToNodes(formal
));
1423 Printer::getPrinter(language
)->toStreamCmdDefineFunctionRec(
1425 api::termVectorToNodes(d_funcs
),
1427 api::termVectorToNodes(d_formulas
));
1430 /* -------------------------------------------------------------------------- */
1431 /* class SetUserAttribute */
1432 /* -------------------------------------------------------------------------- */
1434 SetUserAttributeCommand::SetUserAttributeCommand(
1435 const std::string
& attr
,
1437 const std::vector
<api::Term
>& termValues
,
1438 const std::string
& strValue
)
1439 : d_attr(attr
), d_term(term
), d_termValues(termValues
), d_strValue(strValue
)
1443 SetUserAttributeCommand::SetUserAttributeCommand(const std::string
& attr
,
1445 : SetUserAttributeCommand(attr
, term
, {}, "")
1449 SetUserAttributeCommand::SetUserAttributeCommand(
1450 const std::string
& attr
,
1452 const std::vector
<api::Term
>& values
)
1453 : SetUserAttributeCommand(attr
, term
, values
, "")
1457 SetUserAttributeCommand::SetUserAttributeCommand(const std::string
& attr
,
1459 const std::string
& value
)
1460 : SetUserAttributeCommand(attr
, term
, {}, value
)
1464 void SetUserAttributeCommand::invoke(api::Solver
* solver
)
1468 if (!d_term
.isNull())
1470 solver
->getSmtEngine()->setUserAttribute(
1473 api::termVectorToExprs(d_termValues
),
1476 d_commandStatus
= CommandSuccess::instance();
1478 catch (exception
& e
)
1480 d_commandStatus
= new CommandFailure(e
.what());
1484 Command
* SetUserAttributeCommand::clone() const
1486 return new SetUserAttributeCommand(d_attr
, d_term
, d_termValues
, d_strValue
);
1489 std::string
SetUserAttributeCommand::getCommandName() const
1491 return "set-user-attribute";
1494 void SetUserAttributeCommand::toStream(std::ostream
& out
,
1498 OutputLanguage language
) const
1500 Printer::getPrinter(language
)->toStreamCmdSetUserAttribute(
1501 out
, d_attr
, d_term
.getNode());
1504 /* -------------------------------------------------------------------------- */
1505 /* class SimplifyCommand */
1506 /* -------------------------------------------------------------------------- */
1508 SimplifyCommand::SimplifyCommand(api::Term term
) : d_term(term
) {}
1509 api::Term
SimplifyCommand::getTerm() const { return d_term
; }
1510 void SimplifyCommand::invoke(api::Solver
* solver
)
1514 d_result
= solver
->simplify(d_term
);
1515 d_commandStatus
= CommandSuccess::instance();
1517 catch (UnsafeInterruptException
& e
)
1519 d_commandStatus
= new CommandInterrupted();
1521 catch (exception
& e
)
1523 d_commandStatus
= new CommandFailure(e
.what());
1527 api::Term
SimplifyCommand::getResult() const { return d_result
; }
1528 void SimplifyCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
1532 this->Command::printResult(out
, verbosity
);
1536 out
<< d_result
<< endl
;
1540 Command
* SimplifyCommand::clone() const
1542 SimplifyCommand
* c
= new SimplifyCommand(d_term
);
1543 c
->d_result
= d_result
;
1547 std::string
SimplifyCommand::getCommandName() const { return "simplify"; }
1549 void SimplifyCommand::toStream(std::ostream
& out
,
1553 OutputLanguage language
) const
1555 Printer::getPrinter(language
)->toStreamCmdSimplify(out
, d_term
.getNode());
1558 /* -------------------------------------------------------------------------- */
1559 /* class GetValueCommand */
1560 /* -------------------------------------------------------------------------- */
1562 GetValueCommand::GetValueCommand(api::Term term
) : d_terms()
1564 d_terms
.push_back(term
);
1567 GetValueCommand::GetValueCommand(const std::vector
<api::Term
>& terms
)
1570 PrettyCheckArgument(
1571 terms
.size() >= 1, terms
, "cannot get-value of an empty set of terms");
1574 const std::vector
<api::Term
>& GetValueCommand::getTerms() const
1578 void GetValueCommand::invoke(api::Solver
* solver
)
1582 std::vector
<api::Term
> result
= solver
->getValue(d_terms
);
1583 Assert(result
.size() == d_terms
.size());
1584 for (int i
= 0, size
= d_terms
.size(); i
< size
; i
++)
1586 api::Term request
= d_terms
[i
];
1587 api::Term value
= result
[i
];
1588 if (value
.getSort().isInteger()
1589 && request
.getSort() == solver
->getRealSort())
1591 // Need to wrap in division-by-one so that output printers know this
1592 // is an integer-looking constant that really should be output as
1593 // a rational. Necessary for SMT-LIB standards compliance.
1594 value
= solver
->mkTerm(api::DIVISION
, value
, solver
->mkReal(1));
1596 result
[i
] = solver
->mkTerm(api::SEXPR
, request
, value
);
1598 d_result
= solver
->mkTerm(api::SEXPR
, result
);
1599 d_commandStatus
= CommandSuccess::instance();
1601 catch (api::CVC4ApiRecoverableException
& e
)
1603 d_commandStatus
= new CommandRecoverableFailure(e
.what());
1605 catch (UnsafeInterruptException
& e
)
1607 d_commandStatus
= new CommandInterrupted();
1609 catch (exception
& e
)
1611 d_commandStatus
= new CommandFailure(e
.what());
1615 api::Term
GetValueCommand::getResult() const { return d_result
; }
1616 void GetValueCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
1620 this->Command::printResult(out
, verbosity
);
1624 expr::ExprDag::Scope
scope(out
, false);
1625 out
<< d_result
<< endl
;
1629 Command
* GetValueCommand::clone() const
1631 GetValueCommand
* c
= new GetValueCommand(d_terms
);
1632 c
->d_result
= d_result
;
1636 std::string
GetValueCommand::getCommandName() const { return "get-value"; }
1638 void GetValueCommand::toStream(std::ostream
& out
,
1642 OutputLanguage language
) const
1644 Printer::getPrinter(language
)->toStreamCmdGetValue(
1645 out
, api::termVectorToNodes(d_terms
));
1648 /* -------------------------------------------------------------------------- */
1649 /* class GetAssignmentCommand */
1650 /* -------------------------------------------------------------------------- */
1652 GetAssignmentCommand::GetAssignmentCommand() {}
1653 void GetAssignmentCommand::invoke(api::Solver
* solver
)
1657 std::vector
<std::pair
<Expr
, Expr
>> assignments
=
1658 solver
->getSmtEngine()->getAssignment();
1659 vector
<SExpr
> sexprs
;
1660 for (const auto& p
: assignments
)
1663 v
.emplace_back(SExpr::Keyword(p
.first
.toString()));
1664 v
.emplace_back(SExpr::Keyword(p
.second
.toString()));
1665 sexprs
.emplace_back(v
);
1667 d_result
= SExpr(sexprs
);
1668 d_commandStatus
= CommandSuccess::instance();
1670 catch (RecoverableModalException
& e
)
1672 d_commandStatus
= new CommandRecoverableFailure(e
.what());
1674 catch (UnsafeInterruptException
& e
)
1676 d_commandStatus
= new CommandInterrupted();
1678 catch (exception
& e
)
1680 d_commandStatus
= new CommandFailure(e
.what());
1684 SExpr
GetAssignmentCommand::getResult() const { return d_result
; }
1685 void GetAssignmentCommand::printResult(std::ostream
& out
,
1686 uint32_t verbosity
) const
1690 this->Command::printResult(out
, verbosity
);
1694 out
<< d_result
<< endl
;
1698 Command
* GetAssignmentCommand::clone() const
1700 GetAssignmentCommand
* c
= new GetAssignmentCommand();
1701 c
->d_result
= d_result
;
1705 std::string
GetAssignmentCommand::getCommandName() const
1707 return "get-assignment";
1710 void GetAssignmentCommand::toStream(std::ostream
& out
,
1714 OutputLanguage language
) const
1716 Printer::getPrinter(language
)->toStreamCmdGetAssignment(out
);
1719 /* -------------------------------------------------------------------------- */
1720 /* class GetModelCommand */
1721 /* -------------------------------------------------------------------------- */
1723 GetModelCommand::GetModelCommand() : d_result(nullptr) {}
1724 void GetModelCommand::invoke(api::Solver
* solver
)
1728 d_result
= solver
->getSmtEngine()->getModel();
1729 d_commandStatus
= CommandSuccess::instance();
1731 catch (RecoverableModalException
& e
)
1733 d_commandStatus
= new CommandRecoverableFailure(e
.what());
1735 catch (UnsafeInterruptException
& e
)
1737 d_commandStatus
= new CommandInterrupted();
1739 catch (exception
& e
)
1741 d_commandStatus
= new CommandFailure(e
.what());
1745 /* Model is private to the library -- for now
1746 Model* GetModelCommand::getResult() const {
1751 void GetModelCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
1755 this->Command::printResult(out
, verbosity
);
1763 Command
* GetModelCommand::clone() const
1765 GetModelCommand
* c
= new GetModelCommand();
1766 c
->d_result
= d_result
;
1770 std::string
GetModelCommand::getCommandName() const { return "get-model"; }
1772 void GetModelCommand::toStream(std::ostream
& out
,
1776 OutputLanguage language
) const
1778 Printer::getPrinter(language
)->toStreamCmdGetModel(out
);
1781 /* -------------------------------------------------------------------------- */
1782 /* class BlockModelCommand */
1783 /* -------------------------------------------------------------------------- */
1785 BlockModelCommand::BlockModelCommand() {}
1786 void BlockModelCommand::invoke(api::Solver
* solver
)
1790 solver
->blockModel();
1791 d_commandStatus
= CommandSuccess::instance();
1793 catch (api::CVC4ApiRecoverableException
& e
)
1795 d_commandStatus
= new CommandRecoverableFailure(e
.what());
1797 catch (UnsafeInterruptException
& e
)
1799 d_commandStatus
= new CommandInterrupted();
1801 catch (exception
& e
)
1803 d_commandStatus
= new CommandFailure(e
.what());
1807 Command
* BlockModelCommand::clone() const
1809 BlockModelCommand
* c
= new BlockModelCommand();
1813 std::string
BlockModelCommand::getCommandName() const { return "block-model"; }
1815 void BlockModelCommand::toStream(std::ostream
& out
,
1819 OutputLanguage language
) const
1821 Printer::getPrinter(language
)->toStreamCmdBlockModel(out
);
1824 /* -------------------------------------------------------------------------- */
1825 /* class BlockModelValuesCommand */
1826 /* -------------------------------------------------------------------------- */
1828 BlockModelValuesCommand::BlockModelValuesCommand(
1829 const std::vector
<api::Term
>& terms
)
1832 PrettyCheckArgument(terms
.size() >= 1,
1834 "cannot block-model-values of an empty set of terms");
1837 const std::vector
<api::Term
>& BlockModelValuesCommand::getTerms() const
1841 void BlockModelValuesCommand::invoke(api::Solver
* solver
)
1845 solver
->blockModelValues(d_terms
);
1846 d_commandStatus
= CommandSuccess::instance();
1848 catch (RecoverableModalException
& e
)
1850 d_commandStatus
= new CommandRecoverableFailure(e
.what());
1852 catch (UnsafeInterruptException
& e
)
1854 d_commandStatus
= new CommandInterrupted();
1856 catch (exception
& e
)
1858 d_commandStatus
= new CommandFailure(e
.what());
1862 Command
* BlockModelValuesCommand::clone() const
1864 BlockModelValuesCommand
* c
= new BlockModelValuesCommand(d_terms
);
1868 std::string
BlockModelValuesCommand::getCommandName() const
1870 return "block-model-values";
1873 void BlockModelValuesCommand::toStream(std::ostream
& out
,
1877 OutputLanguage language
) const
1879 Printer::getPrinter(language
)->toStreamCmdBlockModelValues(
1880 out
, api::termVectorToNodes(d_terms
));
1883 /* -------------------------------------------------------------------------- */
1884 /* class GetProofCommand */
1885 /* -------------------------------------------------------------------------- */
1887 GetProofCommand::GetProofCommand() {}
1888 void GetProofCommand::invoke(api::Solver
* solver
)
1890 Unimplemented() << "Unimplemented get-proof\n";
1893 Command
* GetProofCommand::clone() const
1895 GetProofCommand
* c
= new GetProofCommand();
1899 std::string
GetProofCommand::getCommandName() const { return "get-proof"; }
1901 void GetProofCommand::toStream(std::ostream
& out
,
1905 OutputLanguage language
) const
1907 Printer::getPrinter(language
)->toStreamCmdGetProof(out
);
1910 /* -------------------------------------------------------------------------- */
1911 /* class GetInstantiationsCommand */
1912 /* -------------------------------------------------------------------------- */
1914 GetInstantiationsCommand::GetInstantiationsCommand() : d_solver(nullptr) {}
1915 void GetInstantiationsCommand::invoke(api::Solver
* solver
)
1920 d_commandStatus
= CommandSuccess::instance();
1922 catch (exception
& e
)
1924 d_commandStatus
= new CommandFailure(e
.what());
1928 void GetInstantiationsCommand::printResult(std::ostream
& out
,
1929 uint32_t verbosity
) const
1933 this->Command::printResult(out
, verbosity
);
1937 d_solver
->printInstantiations(out
);
1941 Command
* GetInstantiationsCommand::clone() const
1943 GetInstantiationsCommand
* c
= new GetInstantiationsCommand();
1944 // c->d_result = d_result;
1945 c
->d_solver
= d_solver
;
1949 std::string
GetInstantiationsCommand::getCommandName() const
1951 return "get-instantiations";
1954 void GetInstantiationsCommand::toStream(std::ostream
& out
,
1958 OutputLanguage language
) const
1960 Printer::getPrinter(language
)->toStreamCmdGetInstantiations(out
);
1963 /* -------------------------------------------------------------------------- */
1964 /* class GetSynthSolutionCommand */
1965 /* -------------------------------------------------------------------------- */
1967 GetSynthSolutionCommand::GetSynthSolutionCommand() : d_solver(nullptr) {}
1968 void GetSynthSolutionCommand::invoke(api::Solver
* solver
)
1973 d_commandStatus
= CommandSuccess::instance();
1975 catch (exception
& e
)
1977 d_commandStatus
= new CommandFailure(e
.what());
1981 void GetSynthSolutionCommand::printResult(std::ostream
& out
,
1982 uint32_t verbosity
) const
1986 this->Command::printResult(out
, verbosity
);
1990 d_solver
->printSynthSolution(out
);
1994 Command
* GetSynthSolutionCommand::clone() const
1996 GetSynthSolutionCommand
* c
= new GetSynthSolutionCommand();
1997 c
->d_solver
= d_solver
;
2001 std::string
GetSynthSolutionCommand::getCommandName() const
2003 return "get-synth-solution";
2006 void GetSynthSolutionCommand::toStream(std::ostream
& out
,
2010 OutputLanguage language
) const
2012 Printer::getPrinter(language
)->toStreamCmdGetSynthSolution(out
);
2015 /* -------------------------------------------------------------------------- */
2016 /* class GetInterpolCommand */
2017 /* -------------------------------------------------------------------------- */
2019 GetInterpolCommand::GetInterpolCommand(const std::string
& name
, api::Term conj
)
2020 : d_name(name
), d_conj(conj
), d_resultStatus(false)
2023 GetInterpolCommand::GetInterpolCommand(const std::string
& name
,
2026 : d_name(name
), d_conj(conj
), d_sygus_grammar(g
), d_resultStatus(false)
2030 api::Term
GetInterpolCommand::getConjecture() const { return d_conj
; }
2032 const api::Grammar
* GetInterpolCommand::getGrammar() const
2034 return d_sygus_grammar
;
2037 api::Term
GetInterpolCommand::getResult() const { return d_result
; }
2039 void GetInterpolCommand::invoke(api::Solver
* solver
)
2043 if (d_sygus_grammar
== nullptr)
2045 d_resultStatus
= solver
->getInterpolant(d_conj
, d_result
);
2050 solver
->getInterpolant(d_conj
, *d_sygus_grammar
, d_result
);
2052 d_commandStatus
= CommandSuccess::instance();
2054 catch (exception
& e
)
2056 d_commandStatus
= new CommandFailure(e
.what());
2060 void GetInterpolCommand::printResult(std::ostream
& out
,
2061 uint32_t verbosity
) const
2065 this->Command::printResult(out
, verbosity
);
2069 expr::ExprDag::Scope
scope(out
, false);
2072 out
<< "(define-fun " << d_name
<< " () Bool " << d_result
<< ")"
2077 out
<< "none" << std::endl
;
2082 Command
* GetInterpolCommand::clone() const
2084 GetInterpolCommand
* c
=
2085 new GetInterpolCommand(d_name
, d_conj
, d_sygus_grammar
);
2086 c
->d_result
= d_result
;
2087 c
->d_resultStatus
= d_resultStatus
;
2091 std::string
GetInterpolCommand::getCommandName() const
2093 return "get-interpol";
2096 void GetInterpolCommand::toStream(std::ostream
& out
,
2100 OutputLanguage language
) const
2102 Printer::getPrinter(language
)->toStreamCmdGetInterpol(
2106 TypeNode::fromType(d_sygus_grammar
->resolve().getType()));
2109 /* -------------------------------------------------------------------------- */
2110 /* class GetAbductCommand */
2111 /* -------------------------------------------------------------------------- */
2113 GetAbductCommand::GetAbductCommand(const std::string
& name
, api::Term conj
)
2114 : d_name(name
), d_conj(conj
), d_resultStatus(false)
2117 GetAbductCommand::GetAbductCommand(const std::string
& name
,
2120 : d_name(name
), d_conj(conj
), d_sygus_grammar(g
), d_resultStatus(false)
2124 api::Term
GetAbductCommand::getConjecture() const { return d_conj
; }
2126 const api::Grammar
* GetAbductCommand::getGrammar() const
2128 return d_sygus_grammar
;
2131 std::string
GetAbductCommand::getAbductName() const { return d_name
; }
2132 api::Term
GetAbductCommand::getResult() const { return d_result
; }
2134 void GetAbductCommand::invoke(api::Solver
* solver
)
2138 if (d_sygus_grammar
== nullptr)
2140 d_resultStatus
= solver
->getAbduct(d_conj
, d_result
);
2144 d_resultStatus
= solver
->getAbduct(d_conj
, *d_sygus_grammar
, d_result
);
2146 d_commandStatus
= CommandSuccess::instance();
2148 catch (exception
& e
)
2150 d_commandStatus
= new CommandFailure(e
.what());
2154 void GetAbductCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
2158 this->Command::printResult(out
, verbosity
);
2162 expr::ExprDag::Scope
scope(out
, false);
2165 out
<< "(define-fun " << d_name
<< " () Bool " << d_result
<< ")"
2170 out
<< "none" << std::endl
;
2175 Command
* GetAbductCommand::clone() const
2177 GetAbductCommand
* c
= new GetAbductCommand(d_name
, d_conj
, d_sygus_grammar
);
2178 c
->d_result
= d_result
;
2179 c
->d_resultStatus
= d_resultStatus
;
2183 std::string
GetAbductCommand::getCommandName() const { return "get-abduct"; }
2185 void GetAbductCommand::toStream(std::ostream
& out
,
2189 OutputLanguage language
) const
2191 Printer::getPrinter(language
)->toStreamCmdGetAbduct(
2195 TypeNode::fromType(d_sygus_grammar
->resolve().getType()));
2198 /* -------------------------------------------------------------------------- */
2199 /* class GetQuantifierEliminationCommand */
2200 /* -------------------------------------------------------------------------- */
2202 GetQuantifierEliminationCommand::GetQuantifierEliminationCommand()
2203 : d_term(), d_doFull(true)
2206 GetQuantifierEliminationCommand::GetQuantifierEliminationCommand(
2207 const api::Term
& term
, bool doFull
)
2208 : d_term(term
), d_doFull(doFull
)
2212 api::Term
GetQuantifierEliminationCommand::getTerm() const { return d_term
; }
2213 bool GetQuantifierEliminationCommand::getDoFull() const { return d_doFull
; }
2214 void GetQuantifierEliminationCommand::invoke(api::Solver
* solver
)
2220 d_result
= solver
->getQuantifierElimination(d_term
);
2224 d_result
= solver
->getQuantifierEliminationDisjunct(d_term
);
2226 d_commandStatus
= CommandSuccess::instance();
2228 catch (exception
& e
)
2230 d_commandStatus
= new CommandFailure(e
.what());
2234 api::Term
GetQuantifierEliminationCommand::getResult() const
2238 void GetQuantifierEliminationCommand::printResult(std::ostream
& out
,
2239 uint32_t verbosity
) const
2243 this->Command::printResult(out
, verbosity
);
2247 out
<< d_result
<< endl
;
2251 Command
* GetQuantifierEliminationCommand::clone() const
2253 GetQuantifierEliminationCommand
* c
=
2254 new GetQuantifierEliminationCommand(d_term
, d_doFull
);
2255 c
->d_result
= d_result
;
2259 std::string
GetQuantifierEliminationCommand::getCommandName() const
2261 return d_doFull
? "get-qe" : "get-qe-disjunct";
2264 void GetQuantifierEliminationCommand::toStream(std::ostream
& out
,
2268 OutputLanguage language
) const
2270 Printer::getPrinter(language
)->toStreamCmdGetQuantifierElimination(
2271 out
, d_term
.getNode());
2274 /* -------------------------------------------------------------------------- */
2275 /* class GetUnsatAssumptionsCommand */
2276 /* -------------------------------------------------------------------------- */
2278 GetUnsatAssumptionsCommand::GetUnsatAssumptionsCommand() {}
2280 void GetUnsatAssumptionsCommand::invoke(api::Solver
* solver
)
2284 d_result
= solver
->getUnsatAssumptions();
2285 d_commandStatus
= CommandSuccess::instance();
2287 catch (api::CVC4ApiRecoverableException
& e
)
2289 d_commandStatus
= new CommandRecoverableFailure(e
.what());
2291 catch (exception
& e
)
2293 d_commandStatus
= new CommandFailure(e
.what());
2297 std::vector
<api::Term
> GetUnsatAssumptionsCommand::getResult() const
2302 void GetUnsatAssumptionsCommand::printResult(std::ostream
& out
,
2303 uint32_t verbosity
) const
2307 this->Command::printResult(out
, verbosity
);
2311 container_to_stream(out
, d_result
, "(", ")\n", " ");
2315 Command
* GetUnsatAssumptionsCommand::clone() const
2317 GetUnsatAssumptionsCommand
* c
= new GetUnsatAssumptionsCommand
;
2318 c
->d_result
= d_result
;
2322 std::string
GetUnsatAssumptionsCommand::getCommandName() const
2324 return "get-unsat-assumptions";
2327 void GetUnsatAssumptionsCommand::toStream(std::ostream
& out
,
2331 OutputLanguage language
) const
2333 Printer::getPrinter(language
)->toStreamCmdGetUnsatAssumptions(out
);
2336 /* -------------------------------------------------------------------------- */
2337 /* class GetUnsatCoreCommand */
2338 /* -------------------------------------------------------------------------- */
2340 GetUnsatCoreCommand::GetUnsatCoreCommand() {}
2341 void GetUnsatCoreCommand::invoke(api::Solver
* solver
)
2346 d_result
= solver
->getUnsatCore();
2348 d_commandStatus
= CommandSuccess::instance();
2350 catch (api::CVC4ApiRecoverableException
& e
)
2352 d_commandStatus
= new CommandRecoverableFailure(e
.what());
2354 catch (exception
& e
)
2356 d_commandStatus
= new CommandFailure(e
.what());
2360 void GetUnsatCoreCommand::printResult(std::ostream
& out
,
2361 uint32_t verbosity
) const
2365 this->Command::printResult(out
, verbosity
);
2369 UnsatCore
ucr(d_solver
->getSmtEngine(), api::termVectorToNodes(d_result
));
2374 const std::vector
<api::Term
>& GetUnsatCoreCommand::getUnsatCore() const
2376 // of course, this will be empty if the command hasn't been invoked yet
2380 Command
* GetUnsatCoreCommand::clone() const
2382 GetUnsatCoreCommand
* c
= new GetUnsatCoreCommand
;
2383 c
->d_solver
= d_solver
;
2384 c
->d_result
= d_result
;
2388 std::string
GetUnsatCoreCommand::getCommandName() const
2390 return "get-unsat-core";
2393 void GetUnsatCoreCommand::toStream(std::ostream
& out
,
2397 OutputLanguage language
) const
2399 Printer::getPrinter(language
)->toStreamCmdGetUnsatCore(out
);
2402 /* -------------------------------------------------------------------------- */
2403 /* class GetAssertionsCommand */
2404 /* -------------------------------------------------------------------------- */
2406 GetAssertionsCommand::GetAssertionsCommand() {}
2407 void GetAssertionsCommand::invoke(api::Solver
* solver
)
2412 const vector
<api::Term
> v
= solver
->getAssertions();
2414 copy(v
.begin(), v
.end(), ostream_iterator
<api::Term
>(ss
, "\n"));
2416 d_result
= ss
.str();
2417 d_commandStatus
= CommandSuccess::instance();
2419 catch (exception
& e
)
2421 d_commandStatus
= new CommandFailure(e
.what());
2425 std::string
GetAssertionsCommand::getResult() const { return d_result
; }
2426 void GetAssertionsCommand::printResult(std::ostream
& out
,
2427 uint32_t verbosity
) const
2431 this->Command::printResult(out
, verbosity
);
2439 Command
* GetAssertionsCommand::clone() const
2441 GetAssertionsCommand
* c
= new GetAssertionsCommand();
2442 c
->d_result
= d_result
;
2446 std::string
GetAssertionsCommand::getCommandName() const
2448 return "get-assertions";
2451 void GetAssertionsCommand::toStream(std::ostream
& out
,
2455 OutputLanguage language
) const
2457 Printer::getPrinter(language
)->toStreamCmdGetAssertions(out
);
2460 /* -------------------------------------------------------------------------- */
2461 /* class SetBenchmarkStatusCommand */
2462 /* -------------------------------------------------------------------------- */
2464 SetBenchmarkStatusCommand::SetBenchmarkStatusCommand(BenchmarkStatus status
)
2469 BenchmarkStatus
SetBenchmarkStatusCommand::getStatus() const
2474 void SetBenchmarkStatusCommand::invoke(api::Solver
* solver
)
2480 solver
->setInfo("status", ss
.str());
2481 d_commandStatus
= CommandSuccess::instance();
2483 catch (exception
& e
)
2485 d_commandStatus
= new CommandFailure(e
.what());
2489 Command
* SetBenchmarkStatusCommand::clone() const
2491 return new SetBenchmarkStatusCommand(d_status
);
2494 std::string
SetBenchmarkStatusCommand::getCommandName() const
2499 void SetBenchmarkStatusCommand::toStream(std::ostream
& out
,
2503 OutputLanguage language
) const
2505 Result::Sat status
= Result::SAT_UNKNOWN
;
2508 case BenchmarkStatus::SMT_SATISFIABLE
: status
= Result::SAT
; break;
2509 case BenchmarkStatus::SMT_UNSATISFIABLE
: status
= Result::UNSAT
; break;
2510 case BenchmarkStatus::SMT_UNKNOWN
: status
= Result::SAT_UNKNOWN
; break;
2513 Printer::getPrinter(language
)->toStreamCmdSetBenchmarkStatus(out
, status
);
2516 /* -------------------------------------------------------------------------- */
2517 /* class SetBenchmarkLogicCommand */
2518 /* -------------------------------------------------------------------------- */
2520 SetBenchmarkLogicCommand::SetBenchmarkLogicCommand(std::string logic
)
2525 std::string
SetBenchmarkLogicCommand::getLogic() const { return d_logic
; }
2526 void SetBenchmarkLogicCommand::invoke(api::Solver
* solver
)
2530 solver
->setLogic(d_logic
);
2531 d_commandStatus
= CommandSuccess::instance();
2533 catch (exception
& e
)
2535 d_commandStatus
= new CommandFailure(e
.what());
2539 Command
* SetBenchmarkLogicCommand::clone() const
2541 return new SetBenchmarkLogicCommand(d_logic
);
2544 std::string
SetBenchmarkLogicCommand::getCommandName() const
2549 void SetBenchmarkLogicCommand::toStream(std::ostream
& out
,
2553 OutputLanguage language
) const
2555 Printer::getPrinter(language
)->toStreamCmdSetBenchmarkLogic(out
, d_logic
);
2558 /* -------------------------------------------------------------------------- */
2559 /* class SetInfoCommand */
2560 /* -------------------------------------------------------------------------- */
2562 SetInfoCommand::SetInfoCommand(std::string flag
, const SExpr
& sexpr
)
2563 : d_flag(flag
), d_sexpr(sexpr
)
2567 std::string
SetInfoCommand::getFlag() const { return d_flag
; }
2568 SExpr
SetInfoCommand::getSExpr() const { return d_sexpr
; }
2569 void SetInfoCommand::invoke(api::Solver
* solver
)
2573 solver
->getSmtEngine()->setInfo(d_flag
, d_sexpr
);
2574 d_commandStatus
= CommandSuccess::instance();
2576 catch (UnrecognizedOptionException
&)
2578 // As per SMT-LIB spec, silently accept unknown set-info keys
2579 d_commandStatus
= CommandSuccess::instance();
2581 catch (exception
& e
)
2583 d_commandStatus
= new CommandFailure(e
.what());
2587 Command
* SetInfoCommand::clone() const
2589 return new SetInfoCommand(d_flag
, d_sexpr
);
2592 std::string
SetInfoCommand::getCommandName() const { return "set-info"; }
2594 void SetInfoCommand::toStream(std::ostream
& out
,
2598 OutputLanguage language
) const
2600 Printer::getPrinter(language
)->toStreamCmdSetInfo(out
, d_flag
, d_sexpr
);
2603 /* -------------------------------------------------------------------------- */
2604 /* class GetInfoCommand */
2605 /* -------------------------------------------------------------------------- */
2607 GetInfoCommand::GetInfoCommand(std::string flag
) : d_flag(flag
) {}
2608 std::string
GetInfoCommand::getFlag() const { return d_flag
; }
2609 void GetInfoCommand::invoke(api::Solver
* solver
)
2614 v
.push_back(SExpr(SExpr::Keyword(string(":") + d_flag
)));
2615 v
.emplace_back(solver
->getSmtEngine()->getInfo(d_flag
));
2617 if (d_flag
== "all-options" || d_flag
== "all-statistics")
2619 ss
<< PrettySExprs(true);
2622 d_result
= ss
.str();
2623 d_commandStatus
= CommandSuccess::instance();
2625 catch (UnrecognizedOptionException
&)
2627 d_commandStatus
= new CommandUnsupported();
2629 catch (RecoverableModalException
& e
)
2631 d_commandStatus
= new CommandRecoverableFailure(e
.what());
2633 catch (exception
& e
)
2635 d_commandStatus
= new CommandFailure(e
.what());
2639 std::string
GetInfoCommand::getResult() const { return d_result
; }
2640 void GetInfoCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
2644 this->Command::printResult(out
, verbosity
);
2646 else if (d_result
!= "")
2648 out
<< d_result
<< endl
;
2652 Command
* GetInfoCommand::clone() const
2654 GetInfoCommand
* c
= new GetInfoCommand(d_flag
);
2655 c
->d_result
= d_result
;
2659 std::string
GetInfoCommand::getCommandName() const { return "get-info"; }
2661 void GetInfoCommand::toStream(std::ostream
& out
,
2665 OutputLanguage language
) const
2667 Printer::getPrinter(language
)->toStreamCmdGetInfo(out
, d_flag
);
2670 /* -------------------------------------------------------------------------- */
2671 /* class SetOptionCommand */
2672 /* -------------------------------------------------------------------------- */
2674 SetOptionCommand::SetOptionCommand(std::string flag
, const SExpr
& sexpr
)
2675 : d_flag(flag
), d_sexpr(sexpr
)
2679 std::string
SetOptionCommand::getFlag() const { return d_flag
; }
2680 SExpr
SetOptionCommand::getSExpr() const { return d_sexpr
; }
2681 void SetOptionCommand::invoke(api::Solver
* solver
)
2685 solver
->getSmtEngine()->setOption(d_flag
, d_sexpr
);
2686 d_commandStatus
= CommandSuccess::instance();
2688 catch (UnrecognizedOptionException
&)
2690 d_commandStatus
= new CommandUnsupported();
2692 catch (exception
& e
)
2694 d_commandStatus
= new CommandFailure(e
.what());
2698 Command
* SetOptionCommand::clone() const
2700 return new SetOptionCommand(d_flag
, d_sexpr
);
2703 std::string
SetOptionCommand::getCommandName() const { return "set-option"; }
2705 void SetOptionCommand::toStream(std::ostream
& out
,
2709 OutputLanguage language
) const
2711 Printer::getPrinter(language
)->toStreamCmdSetOption(out
, d_flag
, d_sexpr
);
2714 /* -------------------------------------------------------------------------- */
2715 /* class GetOptionCommand */
2716 /* -------------------------------------------------------------------------- */
2718 GetOptionCommand::GetOptionCommand(std::string flag
) : d_flag(flag
) {}
2719 std::string
GetOptionCommand::getFlag() const { return d_flag
; }
2720 void GetOptionCommand::invoke(api::Solver
* solver
)
2724 d_result
= solver
->getOption(d_flag
);
2725 d_commandStatus
= CommandSuccess::instance();
2727 catch (UnrecognizedOptionException
&)
2729 d_commandStatus
= new CommandUnsupported();
2731 catch (exception
& e
)
2733 d_commandStatus
= new CommandFailure(e
.what());
2737 std::string
GetOptionCommand::getResult() const { return d_result
; }
2738 void GetOptionCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
2742 this->Command::printResult(out
, verbosity
);
2744 else if (d_result
!= "")
2746 out
<< d_result
<< endl
;
2750 Command
* GetOptionCommand::clone() const
2752 GetOptionCommand
* c
= new GetOptionCommand(d_flag
);
2753 c
->d_result
= d_result
;
2757 std::string
GetOptionCommand::getCommandName() const { return "get-option"; }
2759 void GetOptionCommand::toStream(std::ostream
& out
,
2763 OutputLanguage language
) const
2765 Printer::getPrinter(language
)->toStreamCmdGetOption(out
, d_flag
);
2768 /* -------------------------------------------------------------------------- */
2769 /* class SetExpressionNameCommand */
2770 /* -------------------------------------------------------------------------- */
2772 SetExpressionNameCommand::SetExpressionNameCommand(api::Term term
,
2774 : d_term(term
), d_name(name
)
2778 void SetExpressionNameCommand::invoke(api::Solver
* solver
)
2780 solver
->getSmtEngine()->setExpressionName(d_term
.getExpr(), d_name
);
2781 d_commandStatus
= CommandSuccess::instance();
2784 Command
* SetExpressionNameCommand::clone() const
2786 SetExpressionNameCommand
* c
= new SetExpressionNameCommand(d_term
, d_name
);
2790 std::string
SetExpressionNameCommand::getCommandName() const
2792 return "set-expr-name";
2795 void SetExpressionNameCommand::toStream(std::ostream
& out
,
2799 OutputLanguage language
) const
2801 Printer::getPrinter(language
)->toStreamCmdSetExpressionName(
2802 out
, d_term
.getNode(), d_name
);
2805 /* -------------------------------------------------------------------------- */
2806 /* class DatatypeDeclarationCommand */
2807 /* -------------------------------------------------------------------------- */
2809 DatatypeDeclarationCommand::DatatypeDeclarationCommand(
2810 const api::Sort
& datatype
)
2813 d_datatypes
.push_back(datatype
);
2816 DatatypeDeclarationCommand::DatatypeDeclarationCommand(
2817 const std::vector
<api::Sort
>& datatypes
)
2818 : d_datatypes(datatypes
)
2822 const std::vector
<api::Sort
>& DatatypeDeclarationCommand::getDatatypes() const
2827 void DatatypeDeclarationCommand::invoke(api::Solver
* solver
)
2829 d_commandStatus
= CommandSuccess::instance();
2832 Command
* DatatypeDeclarationCommand::clone() const
2834 return new DatatypeDeclarationCommand(d_datatypes
);
2837 std::string
DatatypeDeclarationCommand::getCommandName() const
2839 return "declare-datatypes";
2842 void DatatypeDeclarationCommand::toStream(std::ostream
& out
,
2846 OutputLanguage language
) const
2848 Printer::getPrinter(language
)->toStreamCmdDatatypeDeclaration(
2849 out
, api::sortVectorToTypeNodes(d_datatypes
));