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::dag::getDag(out
),
57 Node::setlanguage::getLanguage(out
));
61 ostream
& operator<<(ostream
& out
, const Command
* c
)
74 std::ostream
& operator<<(std::ostream
& out
, const CommandStatus
& s
)
76 s
.toStream(out
, Node::setlanguage::getLanguage(out
));
80 ostream
& operator<<(ostream
& out
, const CommandStatus
* s
)
93 /* output stream insertion operator for benchmark statuses */
94 std::ostream
& operator<<(std::ostream
& out
, BenchmarkStatus status
)
98 case SMT_SATISFIABLE
: return out
<< "sat";
100 case SMT_UNSATISFIABLE
: return out
<< "unsat";
102 case SMT_UNKNOWN
: return out
<< "unknown";
104 default: return out
<< "BenchmarkStatus::[UNKNOWNSTATUS!]";
108 /* -------------------------------------------------------------------------- */
109 /* class CommandPrintSuccess */
110 /* -------------------------------------------------------------------------- */
112 void CommandPrintSuccess::applyPrintSuccess(std::ostream
& out
)
114 out
.iword(s_iosIndex
) = d_printSuccess
;
117 bool CommandPrintSuccess::getPrintSuccess(std::ostream
& out
)
119 return out
.iword(s_iosIndex
);
122 void CommandPrintSuccess::setPrintSuccess(std::ostream
& out
, bool printSuccess
)
124 out
.iword(s_iosIndex
) = printSuccess
;
127 std::ostream
& operator<<(std::ostream
& out
, CommandPrintSuccess cps
)
129 cps
.applyPrintSuccess(out
);
133 /* -------------------------------------------------------------------------- */
135 /* -------------------------------------------------------------------------- */
137 Command::Command() : d_commandStatus(nullptr), d_muted(false) {}
139 Command::Command(const api::Solver
* solver
)
140 : d_commandStatus(nullptr), d_muted(false)
144 Command::Command(const Command
& cmd
)
147 (cmd
.d_commandStatus
== NULL
) ? NULL
: &cmd
.d_commandStatus
->clone();
148 d_muted
= cmd
.d_muted
;
153 if (d_commandStatus
!= NULL
&& d_commandStatus
!= CommandSuccess::instance())
155 delete d_commandStatus
;
159 bool Command::ok() const
161 // either we haven't run the command yet, or it ran successfully
162 return d_commandStatus
== NULL
163 || dynamic_cast<const CommandSuccess
*>(d_commandStatus
) != NULL
;
166 bool Command::fail() const
168 return d_commandStatus
!= NULL
169 && dynamic_cast<const CommandFailure
*>(d_commandStatus
) != NULL
;
172 bool Command::interrupted() const
174 return d_commandStatus
!= NULL
175 && dynamic_cast<const CommandInterrupted
*>(d_commandStatus
) != NULL
;
178 void Command::invoke(api::Solver
* solver
, std::ostream
& out
)
181 if (!(isMuted() && ok()))
185 std::stoul(solver
->getOption("command-verbosity:" + getCommandName())));
189 std::string
Command::toString() const
191 std::stringstream ss
;
196 void CommandStatus::toStream(std::ostream
& out
, OutputLanguage language
) const
198 Printer::getPrinter(language
)->toStream(out
, this);
201 void Command::printResult(std::ostream
& out
, uint32_t verbosity
) const
203 if (d_commandStatus
!= NULL
)
205 if ((!ok() && verbosity
>= 1) || verbosity
>= 2)
207 out
<< *d_commandStatus
;
212 /* -------------------------------------------------------------------------- */
213 /* class EmptyCommand */
214 /* -------------------------------------------------------------------------- */
216 EmptyCommand::EmptyCommand(std::string name
) : d_name(name
) {}
217 std::string
EmptyCommand::getName() const { return d_name
; }
218 void EmptyCommand::invoke(api::Solver
* solver
)
220 /* empty commands have no implementation */
221 d_commandStatus
= CommandSuccess::instance();
224 Command
* EmptyCommand::clone() const { return new EmptyCommand(d_name
); }
225 std::string
EmptyCommand::getCommandName() const { return "empty"; }
227 void EmptyCommand::toStream(std::ostream
& out
,
231 OutputLanguage language
) const
233 Printer::getPrinter(language
)->toStreamCmdEmpty(out
, d_name
);
236 /* -------------------------------------------------------------------------- */
237 /* class EchoCommand */
238 /* -------------------------------------------------------------------------- */
240 EchoCommand::EchoCommand(std::string output
) : d_output(output
) {}
241 std::string
EchoCommand::getOutput() const { return d_output
; }
242 void EchoCommand::invoke(api::Solver
* solver
)
244 /* we don't have an output stream here, nothing to do */
245 d_commandStatus
= CommandSuccess::instance();
248 void EchoCommand::invoke(api::Solver
* solver
, std::ostream
& out
)
250 out
<< d_output
<< std::endl
;
251 Trace("dtview::command") << "* ~COMMAND: echo |" << d_output
<< "|~"
253 d_commandStatus
= CommandSuccess::instance();
256 std::stoul(solver
->getOption("command-verbosity:" + getCommandName())));
259 Command
* EchoCommand::clone() const { return new EchoCommand(d_output
); }
260 std::string
EchoCommand::getCommandName() const { return "echo"; }
262 void EchoCommand::toStream(std::ostream
& out
,
266 OutputLanguage language
) const
268 Printer::getPrinter(language
)->toStreamCmdEcho(out
, d_output
);
271 /* -------------------------------------------------------------------------- */
272 /* class AssertCommand */
273 /* -------------------------------------------------------------------------- */
275 AssertCommand::AssertCommand(const api::Term
& t
, bool inUnsatCore
)
276 : d_term(t
), d_inUnsatCore(inUnsatCore
)
280 api::Term
AssertCommand::getTerm() const { return d_term
; }
281 void AssertCommand::invoke(api::Solver
* solver
)
285 solver
->getSmtEngine()->assertFormula(d_term
.getNode(), d_inUnsatCore
);
286 d_commandStatus
= CommandSuccess::instance();
288 catch (UnsafeInterruptException
& e
)
290 d_commandStatus
= new CommandInterrupted();
294 d_commandStatus
= new CommandFailure(e
.what());
298 Command
* AssertCommand::clone() const
300 return new AssertCommand(d_term
, d_inUnsatCore
);
303 std::string
AssertCommand::getCommandName() const { return "assert"; }
305 void AssertCommand::toStream(std::ostream
& out
,
309 OutputLanguage language
) const
311 Printer::getPrinter(language
)->toStreamCmdAssert(out
, d_term
.getNode());
314 /* -------------------------------------------------------------------------- */
315 /* class PushCommand */
316 /* -------------------------------------------------------------------------- */
318 void PushCommand::invoke(api::Solver
* solver
)
323 d_commandStatus
= CommandSuccess::instance();
325 catch (UnsafeInterruptException
& e
)
327 d_commandStatus
= new CommandInterrupted();
331 d_commandStatus
= new CommandFailure(e
.what());
335 Command
* PushCommand::clone() const { return new PushCommand(); }
336 std::string
PushCommand::getCommandName() const { return "push"; }
338 void PushCommand::toStream(std::ostream
& out
,
342 OutputLanguage language
) const
344 Printer::getPrinter(language
)->toStreamCmdPush(out
);
347 /* -------------------------------------------------------------------------- */
348 /* class PopCommand */
349 /* -------------------------------------------------------------------------- */
351 void PopCommand::invoke(api::Solver
* solver
)
356 d_commandStatus
= CommandSuccess::instance();
358 catch (UnsafeInterruptException
& e
)
360 d_commandStatus
= new CommandInterrupted();
364 d_commandStatus
= new CommandFailure(e
.what());
368 Command
* PopCommand::clone() const { return new PopCommand(); }
369 std::string
PopCommand::getCommandName() const { return "pop"; }
371 void PopCommand::toStream(std::ostream
& out
,
375 OutputLanguage language
) const
377 Printer::getPrinter(language
)->toStreamCmdPop(out
);
380 /* -------------------------------------------------------------------------- */
381 /* class CheckSatCommand */
382 /* -------------------------------------------------------------------------- */
384 CheckSatCommand::CheckSatCommand() : d_term() {}
386 CheckSatCommand::CheckSatCommand(const api::Term
& term
) : d_term(term
) {}
388 api::Term
CheckSatCommand::getTerm() const { return d_term
; }
389 void CheckSatCommand::invoke(api::Solver
* solver
)
391 Trace("dtview::command") << "* ~COMMAND: " << getCommandName() << "~"
396 d_term
.isNull() ? solver
->checkSat() : solver
->checkSatAssuming(d_term
);
398 d_commandStatus
= CommandSuccess::instance();
402 d_commandStatus
= new CommandFailure(e
.what());
406 api::Result
CheckSatCommand::getResult() const { return d_result
; }
407 void CheckSatCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
411 this->Command::printResult(out
, verbosity
);
415 Trace("dtview::command") << "* RESULT: " << d_result
<< std::endl
;
416 out
<< d_result
<< endl
;
420 Command
* CheckSatCommand::clone() const
422 CheckSatCommand
* c
= new CheckSatCommand(d_term
);
423 c
->d_result
= d_result
;
427 std::string
CheckSatCommand::getCommandName() const { return "check-sat"; }
429 void CheckSatCommand::toStream(std::ostream
& out
,
433 OutputLanguage language
) const
435 Printer::getPrinter(language
)->toStreamCmdCheckSat(out
, d_term
.getNode());
438 /* -------------------------------------------------------------------------- */
439 /* class CheckSatAssumingCommand */
440 /* -------------------------------------------------------------------------- */
442 CheckSatAssumingCommand::CheckSatAssumingCommand(api::Term term
)
447 CheckSatAssumingCommand::CheckSatAssumingCommand(
448 const std::vector
<api::Term
>& terms
)
453 const std::vector
<api::Term
>& CheckSatAssumingCommand::getTerms() const
458 void CheckSatAssumingCommand::invoke(api::Solver
* solver
)
460 Trace("dtview::command") << "* ~COMMAND: (check-sat-assuming ( " << d_terms
461 << " )~" << std::endl
;
464 d_result
= solver
->checkSatAssuming(d_terms
);
465 d_commandStatus
= CommandSuccess::instance();
469 d_commandStatus
= new CommandFailure(e
.what());
473 api::Result
CheckSatAssumingCommand::getResult() const
475 Trace("dtview::command") << "* ~RESULT: " << d_result
<< "~" << std::endl
;
479 void CheckSatAssumingCommand::printResult(std::ostream
& out
,
480 uint32_t verbosity
) const
484 this->Command::printResult(out
, verbosity
);
488 out
<< d_result
<< endl
;
492 Command
* CheckSatAssumingCommand::clone() const
494 CheckSatAssumingCommand
* c
= new CheckSatAssumingCommand(d_terms
);
495 c
->d_result
= d_result
;
499 std::string
CheckSatAssumingCommand::getCommandName() const
501 return "check-sat-assuming";
504 void CheckSatAssumingCommand::toStream(std::ostream
& out
,
508 OutputLanguage language
) const
510 Printer::getPrinter(language
)->toStreamCmdCheckSatAssuming(
511 out
, api::termVectorToNodes(d_terms
));
514 /* -------------------------------------------------------------------------- */
515 /* class QueryCommand */
516 /* -------------------------------------------------------------------------- */
518 QueryCommand::QueryCommand(const api::Term
& t
, bool inUnsatCore
)
519 : d_term(t
), d_inUnsatCore(inUnsatCore
)
523 api::Term
QueryCommand::getTerm() const { return d_term
; }
524 void QueryCommand::invoke(api::Solver
* solver
)
528 d_result
= solver
->checkEntailed(d_term
);
529 d_commandStatus
= CommandSuccess::instance();
533 d_commandStatus
= new CommandFailure(e
.what());
537 api::Result
QueryCommand::getResult() const { return d_result
; }
538 void QueryCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
542 this->Command::printResult(out
, verbosity
);
546 out
<< d_result
<< endl
;
550 Command
* QueryCommand::clone() const
552 QueryCommand
* c
= new QueryCommand(d_term
, d_inUnsatCore
);
553 c
->d_result
= d_result
;
557 std::string
QueryCommand::getCommandName() const { return "query"; }
559 void QueryCommand::toStream(std::ostream
& out
,
563 OutputLanguage language
) const
565 Printer::getPrinter(language
)->toStreamCmdQuery(out
, d_term
.getNode());
568 /* -------------------------------------------------------------------------- */
569 /* class DeclareSygusVarCommand */
570 /* -------------------------------------------------------------------------- */
572 DeclareSygusVarCommand::DeclareSygusVarCommand(const std::string
& id
,
575 : DeclarationDefinitionCommand(id
), d_var(var
), d_sort(sort
)
579 api::Term
DeclareSygusVarCommand::getVar() const { return d_var
; }
580 api::Sort
DeclareSygusVarCommand::getSort() const { return d_sort
; }
582 void DeclareSygusVarCommand::invoke(api::Solver
* solver
)
584 d_commandStatus
= CommandSuccess::instance();
587 Command
* DeclareSygusVarCommand::clone() const
589 return new DeclareSygusVarCommand(d_symbol
, d_var
, d_sort
);
592 std::string
DeclareSygusVarCommand::getCommandName() const
594 return "declare-var";
597 void DeclareSygusVarCommand::toStream(std::ostream
& out
,
601 OutputLanguage language
) const
603 Printer::getPrinter(language
)->toStreamCmdDeclareVar(
604 out
, d_var
.getNode(), d_sort
.getTypeNode());
607 /* -------------------------------------------------------------------------- */
608 /* class SynthFunCommand */
609 /* -------------------------------------------------------------------------- */
611 SynthFunCommand::SynthFunCommand(const std::string
& id
,
613 const std::vector
<api::Term
>& vars
,
617 : DeclarationDefinitionCommand(id
),
626 api::Term
SynthFunCommand::getFunction() const { return d_fun
; }
628 const std::vector
<api::Term
>& SynthFunCommand::getVars() const
633 api::Sort
SynthFunCommand::getSort() const { return d_sort
; }
634 bool SynthFunCommand::isInv() const { return d_isInv
; }
636 const api::Grammar
* SynthFunCommand::getGrammar() const { return d_grammar
; }
638 void SynthFunCommand::invoke(api::Solver
* solver
)
640 d_commandStatus
= CommandSuccess::instance();
643 Command
* SynthFunCommand::clone() const
645 return new SynthFunCommand(
646 d_symbol
, d_fun
, d_vars
, d_sort
, d_isInv
, d_grammar
);
649 std::string
SynthFunCommand::getCommandName() const
651 return d_isInv
? "synth-inv" : "synth-fun";
654 void SynthFunCommand::toStream(std::ostream
& out
,
658 OutputLanguage language
) const
660 std::vector
<Node
> nodeVars
= termVectorToNodes(d_vars
);
661 Printer::getPrinter(language
)->toStreamCmdSynthFun(
665 d_sort
.getTypeNode(),
667 d_grammar
->resolve().getTypeNode());
670 /* -------------------------------------------------------------------------- */
671 /* class SygusConstraintCommand */
672 /* -------------------------------------------------------------------------- */
674 SygusConstraintCommand::SygusConstraintCommand(const api::Term
& t
) : d_term(t
)
678 void SygusConstraintCommand::invoke(api::Solver
* solver
)
682 solver
->addSygusConstraint(d_term
);
683 d_commandStatus
= CommandSuccess::instance();
687 d_commandStatus
= new CommandFailure(e
.what());
691 api::Term
SygusConstraintCommand::getTerm() const { return d_term
; }
693 Command
* SygusConstraintCommand::clone() const
695 return new SygusConstraintCommand(d_term
);
698 std::string
SygusConstraintCommand::getCommandName() const
703 void SygusConstraintCommand::toStream(std::ostream
& out
,
707 OutputLanguage language
) const
709 Printer::getPrinter(language
)->toStreamCmdConstraint(out
, d_term
.getNode());
712 /* -------------------------------------------------------------------------- */
713 /* class SygusInvConstraintCommand */
714 /* -------------------------------------------------------------------------- */
716 SygusInvConstraintCommand::SygusInvConstraintCommand(
717 const std::vector
<api::Term
>& predicates
)
718 : d_predicates(predicates
)
722 SygusInvConstraintCommand::SygusInvConstraintCommand(const api::Term
& inv
,
723 const api::Term
& pre
,
724 const api::Term
& trans
,
725 const api::Term
& post
)
726 : SygusInvConstraintCommand(std::vector
<api::Term
>{inv
, pre
, trans
, post
})
730 void SygusInvConstraintCommand::invoke(api::Solver
* solver
)
734 solver
->addSygusInvConstraint(
735 d_predicates
[0], d_predicates
[1], d_predicates
[2], d_predicates
[3]);
736 d_commandStatus
= CommandSuccess::instance();
740 d_commandStatus
= new CommandFailure(e
.what());
744 const std::vector
<api::Term
>& SygusInvConstraintCommand::getPredicates() const
749 Command
* SygusInvConstraintCommand::clone() const
751 return new SygusInvConstraintCommand(d_predicates
);
754 std::string
SygusInvConstraintCommand::getCommandName() const
756 return "inv-constraint";
759 void SygusInvConstraintCommand::toStream(std::ostream
& out
,
763 OutputLanguage language
) const
765 Printer::getPrinter(language
)->toStreamCmdInvConstraint(
767 d_predicates
[0].getNode(),
768 d_predicates
[1].getNode(),
769 d_predicates
[2].getNode(),
770 d_predicates
[3].getNode());
773 /* -------------------------------------------------------------------------- */
774 /* class CheckSynthCommand */
775 /* -------------------------------------------------------------------------- */
777 void CheckSynthCommand::invoke(api::Solver
* solver
)
781 d_result
= solver
->checkSynth();
782 d_commandStatus
= CommandSuccess::instance();
784 // check whether we should print the status
785 if (!d_result
.isUnsat()
786 || options::sygusOut() == options::SygusSolutionOutMode::STATUS_AND_DEF
787 || options::sygusOut() == options::SygusSolutionOutMode::STATUS
)
789 if (options::sygusOut() == options::SygusSolutionOutMode::STANDARD
)
791 d_solution
<< "(fail)" << endl
;
795 d_solution
<< d_result
<< endl
;
798 // check whether we should print the solution
799 if (d_result
.isUnsat()
800 && options::sygusOut() != options::SygusSolutionOutMode::STATUS
)
802 // printing a synthesis solution is a non-constant
803 // method, since it invokes a sophisticated algorithm
804 // (Figure 5 of Reynolds et al. CAV 2015).
805 // Hence, we must call here print solution here,
806 // instead of during printResult.
807 solver
->printSynthSolution(d_solution
);
812 d_commandStatus
= new CommandFailure(e
.what());
816 api::Result
CheckSynthCommand::getResult() const { return d_result
; }
817 void CheckSynthCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
821 this->Command::printResult(out
, verbosity
);
825 out
<< d_solution
.str();
829 Command
* CheckSynthCommand::clone() const { return new CheckSynthCommand(); }
831 std::string
CheckSynthCommand::getCommandName() const { return "check-synth"; }
833 void CheckSynthCommand::toStream(std::ostream
& out
,
837 OutputLanguage language
) const
839 Printer::getPrinter(language
)->toStreamCmdCheckSynth(out
);
842 /* -------------------------------------------------------------------------- */
843 /* class ResetCommand */
844 /* -------------------------------------------------------------------------- */
846 void ResetCommand::invoke(api::Solver
* solver
)
850 solver
->getSmtEngine()->reset();
851 d_commandStatus
= CommandSuccess::instance();
855 d_commandStatus
= new CommandFailure(e
.what());
859 Command
* ResetCommand::clone() const { return new ResetCommand(); }
860 std::string
ResetCommand::getCommandName() const { return "reset"; }
862 void ResetCommand::toStream(std::ostream
& out
,
866 OutputLanguage language
) const
868 Printer::getPrinter(language
)->toStreamCmdReset(out
);
871 /* -------------------------------------------------------------------------- */
872 /* class ResetAssertionsCommand */
873 /* -------------------------------------------------------------------------- */
875 void ResetAssertionsCommand::invoke(api::Solver
* solver
)
879 solver
->resetAssertions();
880 d_commandStatus
= CommandSuccess::instance();
884 d_commandStatus
= new CommandFailure(e
.what());
888 Command
* ResetAssertionsCommand::clone() const
890 return new ResetAssertionsCommand();
893 std::string
ResetAssertionsCommand::getCommandName() const
895 return "reset-assertions";
898 void ResetAssertionsCommand::toStream(std::ostream
& out
,
902 OutputLanguage language
) const
904 Printer::getPrinter(language
)->toStreamCmdResetAssertions(out
);
907 /* -------------------------------------------------------------------------- */
908 /* class QuitCommand */
909 /* -------------------------------------------------------------------------- */
911 void QuitCommand::invoke(api::Solver
* solver
)
913 Dump("benchmark") << *this;
914 d_commandStatus
= CommandSuccess::instance();
917 Command
* QuitCommand::clone() const { return new QuitCommand(); }
918 std::string
QuitCommand::getCommandName() const { return "exit"; }
920 void QuitCommand::toStream(std::ostream
& out
,
924 OutputLanguage language
) const
926 Printer::getPrinter(language
)->toStreamCmdQuit(out
);
929 /* -------------------------------------------------------------------------- */
930 /* class CommentCommand */
931 /* -------------------------------------------------------------------------- */
933 CommentCommand::CommentCommand(std::string comment
) : d_comment(comment
) {}
934 std::string
CommentCommand::getComment() const { return d_comment
; }
935 void CommentCommand::invoke(api::Solver
* solver
)
937 Dump("benchmark") << *this;
938 d_commandStatus
= CommandSuccess::instance();
941 Command
* CommentCommand::clone() const { return new CommentCommand(d_comment
); }
942 std::string
CommentCommand::getCommandName() const { return "comment"; }
944 void CommentCommand::toStream(std::ostream
& out
,
948 OutputLanguage language
) const
950 Printer::getPrinter(language
)->toStreamCmdComment(out
, d_comment
);
953 /* -------------------------------------------------------------------------- */
954 /* class CommandSequence */
955 /* -------------------------------------------------------------------------- */
957 CommandSequence::CommandSequence() : d_index(0) {}
958 CommandSequence::~CommandSequence()
960 for (unsigned i
= d_index
; i
< d_commandSequence
.size(); ++i
)
962 delete d_commandSequence
[i
];
966 void CommandSequence::addCommand(Command
* cmd
)
968 d_commandSequence
.push_back(cmd
);
971 void CommandSequence::clear() { d_commandSequence
.clear(); }
972 void CommandSequence::invoke(api::Solver
* solver
)
974 for (; d_index
< d_commandSequence
.size(); ++d_index
)
976 d_commandSequence
[d_index
]->invoke(solver
);
977 if (!d_commandSequence
[d_index
]->ok())
980 d_commandStatus
= d_commandSequence
[d_index
]->getCommandStatus();
983 delete d_commandSequence
[d_index
];
986 AlwaysAssert(d_commandStatus
== NULL
);
987 d_commandStatus
= CommandSuccess::instance();
990 void CommandSequence::invoke(api::Solver
* solver
, std::ostream
& out
)
992 for (; d_index
< d_commandSequence
.size(); ++d_index
)
994 d_commandSequence
[d_index
]->invoke(solver
, out
);
995 if (!d_commandSequence
[d_index
]->ok())
998 d_commandStatus
= d_commandSequence
[d_index
]->getCommandStatus();
1001 delete d_commandSequence
[d_index
];
1004 AlwaysAssert(d_commandStatus
== NULL
);
1005 d_commandStatus
= CommandSuccess::instance();
1008 Command
* CommandSequence::clone() const
1010 CommandSequence
* seq
= new CommandSequence();
1011 for (const_iterator i
= begin(); i
!= end(); ++i
)
1013 seq
->addCommand((*i
)->clone());
1015 seq
->d_index
= d_index
;
1019 CommandSequence::const_iterator
CommandSequence::begin() const
1021 return d_commandSequence
.begin();
1024 CommandSequence::const_iterator
CommandSequence::end() const
1026 return d_commandSequence
.end();
1029 CommandSequence::iterator
CommandSequence::begin()
1031 return d_commandSequence
.begin();
1034 CommandSequence::iterator
CommandSequence::end()
1036 return d_commandSequence
.end();
1039 std::string
CommandSequence::getCommandName() const { return "sequence"; }
1041 void CommandSequence::toStream(std::ostream
& out
,
1045 OutputLanguage language
) const
1047 Printer::getPrinter(language
)->toStreamCmdCommandSequence(out
,
1051 /* -------------------------------------------------------------------------- */
1052 /* class DeclarationSequence */
1053 /* -------------------------------------------------------------------------- */
1055 void DeclarationSequence::toStream(std::ostream
& out
,
1059 OutputLanguage language
) const
1061 Printer::getPrinter(language
)->toStreamCmdDeclarationSequence(
1062 out
, d_commandSequence
);
1065 /* -------------------------------------------------------------------------- */
1066 /* class DeclarationDefinitionCommand */
1067 /* -------------------------------------------------------------------------- */
1069 DeclarationDefinitionCommand::DeclarationDefinitionCommand(
1070 const std::string
& id
)
1075 std::string
DeclarationDefinitionCommand::getSymbol() const { return d_symbol
; }
1077 /* -------------------------------------------------------------------------- */
1078 /* class DeclareFunctionCommand */
1079 /* -------------------------------------------------------------------------- */
1081 DeclareFunctionCommand::DeclareFunctionCommand(const std::string
& id
,
1084 : DeclarationDefinitionCommand(id
),
1087 d_printInModel(true),
1088 d_printInModelSetByUser(false)
1092 api::Term
DeclareFunctionCommand::getFunction() const { return d_func
; }
1093 api::Sort
DeclareFunctionCommand::getSort() const { return d_sort
; }
1094 bool DeclareFunctionCommand::getPrintInModel() const { return d_printInModel
; }
1095 bool DeclareFunctionCommand::getPrintInModelSetByUser() const
1097 return d_printInModelSetByUser
;
1100 void DeclareFunctionCommand::setPrintInModel(bool p
)
1103 d_printInModelSetByUser
= true;
1106 void DeclareFunctionCommand::invoke(api::Solver
* solver
)
1108 d_commandStatus
= CommandSuccess::instance();
1111 Command
* DeclareFunctionCommand::clone() const
1113 DeclareFunctionCommand
* dfc
=
1114 new DeclareFunctionCommand(d_symbol
, d_func
, d_sort
);
1115 dfc
->d_printInModel
= d_printInModel
;
1116 dfc
->d_printInModelSetByUser
= d_printInModelSetByUser
;
1120 std::string
DeclareFunctionCommand::getCommandName() const
1122 return "declare-fun";
1125 void DeclareFunctionCommand::toStream(std::ostream
& out
,
1129 OutputLanguage language
) const
1131 Printer::getPrinter(language
)->toStreamCmdDeclareFunction(
1132 out
, d_func
.toString(), d_sort
.getTypeNode());
1135 /* -------------------------------------------------------------------------- */
1136 /* class DeclareSortCommand */
1137 /* -------------------------------------------------------------------------- */
1139 DeclareSortCommand::DeclareSortCommand(const std::string
& id
,
1142 : DeclarationDefinitionCommand(id
), d_arity(arity
), d_sort(sort
)
1146 size_t DeclareSortCommand::getArity() const { return d_arity
; }
1147 api::Sort
DeclareSortCommand::getSort() const { return d_sort
; }
1148 void DeclareSortCommand::invoke(api::Solver
* solver
)
1150 d_commandStatus
= CommandSuccess::instance();
1153 Command
* DeclareSortCommand::clone() const
1155 return new DeclareSortCommand(d_symbol
, d_arity
, d_sort
);
1158 std::string
DeclareSortCommand::getCommandName() const
1160 return "declare-sort";
1163 void DeclareSortCommand::toStream(std::ostream
& out
,
1167 OutputLanguage language
) const
1169 Printer::getPrinter(language
)->toStreamCmdDeclareType(
1170 out
, d_sort
.toString(), d_arity
, d_sort
.getTypeNode());
1173 /* -------------------------------------------------------------------------- */
1174 /* class DefineSortCommand */
1175 /* -------------------------------------------------------------------------- */
1177 DefineSortCommand::DefineSortCommand(const std::string
& id
, api::Sort sort
)
1178 : DeclarationDefinitionCommand(id
), d_params(), d_sort(sort
)
1182 DefineSortCommand::DefineSortCommand(const std::string
& id
,
1183 const std::vector
<api::Sort
>& params
,
1185 : DeclarationDefinitionCommand(id
), d_params(params
), d_sort(sort
)
1189 const std::vector
<api::Sort
>& DefineSortCommand::getParameters() const
1194 api::Sort
DefineSortCommand::getSort() const { return d_sort
; }
1195 void DefineSortCommand::invoke(api::Solver
* solver
)
1197 d_commandStatus
= CommandSuccess::instance();
1200 Command
* DefineSortCommand::clone() const
1202 return new DefineSortCommand(d_symbol
, d_params
, d_sort
);
1205 std::string
DefineSortCommand::getCommandName() const { return "define-sort"; }
1207 void DefineSortCommand::toStream(std::ostream
& out
,
1211 OutputLanguage language
) const
1213 Printer::getPrinter(language
)->toStreamCmdDefineType(
1216 api::sortVectorToTypeNodes(d_params
),
1217 d_sort
.getTypeNode());
1220 /* -------------------------------------------------------------------------- */
1221 /* class DefineFunctionCommand */
1222 /* -------------------------------------------------------------------------- */
1224 DefineFunctionCommand::DefineFunctionCommand(const std::string
& id
,
1228 : DeclarationDefinitionCommand(id
),
1236 DefineFunctionCommand::DefineFunctionCommand(
1237 const std::string
& id
,
1239 const std::vector
<api::Term
>& formals
,
1242 : DeclarationDefinitionCommand(id
),
1250 api::Term
DefineFunctionCommand::getFunction() const { return d_func
; }
1251 const std::vector
<api::Term
>& DefineFunctionCommand::getFormals() const
1256 api::Term
DefineFunctionCommand::getFormula() const { return d_formula
; }
1257 void DefineFunctionCommand::invoke(api::Solver
* solver
)
1261 if (!d_func
.isNull())
1263 solver
->defineFun(d_func
, d_formals
, d_formula
, d_global
);
1265 d_commandStatus
= CommandSuccess::instance();
1267 catch (exception
& e
)
1269 d_commandStatus
= new CommandFailure(e
.what());
1273 Command
* DefineFunctionCommand::clone() const
1275 return new DefineFunctionCommand(
1276 d_symbol
, d_func
, d_formals
, d_formula
, d_global
);
1279 std::string
DefineFunctionCommand::getCommandName() const
1281 return "define-fun";
1284 void DefineFunctionCommand::toStream(std::ostream
& out
,
1288 OutputLanguage language
) const
1290 Printer::getPrinter(language
)->toStreamCmdDefineFunction(
1293 api::termVectorToNodes(d_formals
),
1294 d_func
.getNode().getType().getRangeType(),
1295 d_formula
.getNode());
1298 /* -------------------------------------------------------------------------- */
1299 /* class DefineNamedFunctionCommand */
1300 /* -------------------------------------------------------------------------- */
1302 DefineNamedFunctionCommand::DefineNamedFunctionCommand(
1304 const std::string
& id
,
1306 const std::vector
<api::Term
>& formals
,
1309 : DefineFunctionCommand(id
, func
, formals
, formula
, global
)
1313 void DefineNamedFunctionCommand::invoke(api::Solver
* solver
)
1315 this->DefineFunctionCommand::invoke(solver
);
1316 if (!d_func
.isNull() && d_func
.getSort().isBoolean())
1318 solver
->getSmtEngine()->addToAssignment(d_func
.getExpr());
1320 d_commandStatus
= CommandSuccess::instance();
1323 Command
* DefineNamedFunctionCommand::clone() const
1325 return new DefineNamedFunctionCommand(
1326 d_symbol
, d_func
, d_formals
, d_formula
, d_global
);
1329 void DefineNamedFunctionCommand::toStream(std::ostream
& out
,
1333 OutputLanguage language
) const
1335 Printer::getPrinter(language
)->toStreamCmdDefineNamedFunction(
1338 api::termVectorToNodes(d_formals
),
1339 d_func
.getSort().getFunctionCodomainSort().getTypeNode(),
1340 d_formula
.getNode());
1343 /* -------------------------------------------------------------------------- */
1344 /* class DefineFunctionRecCommand */
1345 /* -------------------------------------------------------------------------- */
1347 DefineFunctionRecCommand::DefineFunctionRecCommand(
1350 const std::vector
<api::Term
>& formals
,
1355 d_funcs
.push_back(func
);
1356 d_formals
.push_back(formals
);
1357 d_formulas
.push_back(formula
);
1360 DefineFunctionRecCommand::DefineFunctionRecCommand(
1362 const std::vector
<api::Term
>& funcs
,
1363 const std::vector
<std::vector
<api::Term
>>& formals
,
1364 const std::vector
<api::Term
>& formulas
,
1366 : d_funcs(funcs
), d_formals(formals
), d_formulas(formulas
), d_global(global
)
1370 const std::vector
<api::Term
>& DefineFunctionRecCommand::getFunctions() const
1375 const std::vector
<std::vector
<api::Term
>>&
1376 DefineFunctionRecCommand::getFormals() const
1381 const std::vector
<api::Term
>& DefineFunctionRecCommand::getFormulas() const
1386 void DefineFunctionRecCommand::invoke(api::Solver
* solver
)
1390 solver
->defineFunsRec(d_funcs
, d_formals
, d_formulas
, d_global
);
1391 d_commandStatus
= CommandSuccess::instance();
1393 catch (exception
& e
)
1395 d_commandStatus
= new CommandFailure(e
.what());
1399 Command
* DefineFunctionRecCommand::clone() const
1401 return new DefineFunctionRecCommand(d_funcs
, d_formals
, d_formulas
, d_global
);
1404 std::string
DefineFunctionRecCommand::getCommandName() const
1406 return "define-fun-rec";
1409 void DefineFunctionRecCommand::toStream(std::ostream
& out
,
1413 OutputLanguage language
) const
1415 std::vector
<std::vector
<Node
>> formals
;
1416 formals
.reserve(d_formals
.size());
1417 for (const std::vector
<api::Term
>& formal
: d_formals
)
1419 formals
.push_back(api::termVectorToNodes(formal
));
1422 Printer::getPrinter(language
)->toStreamCmdDefineFunctionRec(
1424 api::termVectorToNodes(d_funcs
),
1426 api::termVectorToNodes(d_formulas
));
1428 /* -------------------------------------------------------------------------- */
1429 /* class DeclareHeapCommand */
1430 /* -------------------------------------------------------------------------- */
1431 DeclareHeapCommand::DeclareHeapCommand(api::Sort locSort
, api::Sort dataSort
)
1432 : d_locSort(locSort
), d_dataSort(dataSort
)
1436 api::Sort
DeclareHeapCommand::getLocationSort() const { return d_locSort
; }
1437 api::Sort
DeclareHeapCommand::getDataSort() const { return d_dataSort
; }
1439 void DeclareHeapCommand::invoke(api::Solver
* solver
)
1441 solver
->declareSeparationHeap(d_locSort
, d_dataSort
);
1444 Command
* DeclareHeapCommand::clone() const
1446 return new DeclareHeapCommand(d_locSort
, d_dataSort
);
1449 std::string
DeclareHeapCommand::getCommandName() const
1451 return "declare-heap";
1454 void DeclareHeapCommand::toStream(std::ostream
& out
,
1457 OutputLanguage language
) const
1459 Printer::getPrinter(language
)->toStreamCmdDeclareHeap(
1460 out
, d_locSort
.getTypeNode(), d_dataSort
.getTypeNode());
1463 /* -------------------------------------------------------------------------- */
1464 /* class SetUserAttributeCommand */
1465 /* -------------------------------------------------------------------------- */
1467 SetUserAttributeCommand::SetUserAttributeCommand(
1468 const std::string
& attr
,
1470 const std::vector
<api::Term
>& termValues
,
1471 const std::string
& strValue
)
1472 : d_attr(attr
), d_term(term
), d_termValues(termValues
), d_strValue(strValue
)
1476 SetUserAttributeCommand::SetUserAttributeCommand(const std::string
& attr
,
1478 : SetUserAttributeCommand(attr
, term
, {}, "")
1482 SetUserAttributeCommand::SetUserAttributeCommand(
1483 const std::string
& attr
,
1485 const std::vector
<api::Term
>& values
)
1486 : SetUserAttributeCommand(attr
, term
, values
, "")
1490 SetUserAttributeCommand::SetUserAttributeCommand(const std::string
& attr
,
1492 const std::string
& value
)
1493 : SetUserAttributeCommand(attr
, term
, {}, value
)
1497 void SetUserAttributeCommand::invoke(api::Solver
* solver
)
1501 if (!d_term
.isNull())
1503 solver
->getSmtEngine()->setUserAttribute(
1506 api::termVectorToExprs(d_termValues
),
1509 d_commandStatus
= CommandSuccess::instance();
1511 catch (exception
& e
)
1513 d_commandStatus
= new CommandFailure(e
.what());
1517 Command
* SetUserAttributeCommand::clone() const
1519 return new SetUserAttributeCommand(d_attr
, d_term
, d_termValues
, d_strValue
);
1522 std::string
SetUserAttributeCommand::getCommandName() const
1524 return "set-user-attribute";
1527 void SetUserAttributeCommand::toStream(std::ostream
& out
,
1531 OutputLanguage language
) const
1533 Printer::getPrinter(language
)->toStreamCmdSetUserAttribute(
1534 out
, d_attr
, d_term
.getNode());
1537 /* -------------------------------------------------------------------------- */
1538 /* class SimplifyCommand */
1539 /* -------------------------------------------------------------------------- */
1541 SimplifyCommand::SimplifyCommand(api::Term term
) : d_term(term
) {}
1542 api::Term
SimplifyCommand::getTerm() const { return d_term
; }
1543 void SimplifyCommand::invoke(api::Solver
* solver
)
1547 d_result
= solver
->simplify(d_term
);
1548 d_commandStatus
= CommandSuccess::instance();
1550 catch (UnsafeInterruptException
& e
)
1552 d_commandStatus
= new CommandInterrupted();
1554 catch (exception
& e
)
1556 d_commandStatus
= new CommandFailure(e
.what());
1560 api::Term
SimplifyCommand::getResult() const { return d_result
; }
1561 void SimplifyCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
1565 this->Command::printResult(out
, verbosity
);
1569 out
<< d_result
<< endl
;
1573 Command
* SimplifyCommand::clone() const
1575 SimplifyCommand
* c
= new SimplifyCommand(d_term
);
1576 c
->d_result
= d_result
;
1580 std::string
SimplifyCommand::getCommandName() const { return "simplify"; }
1582 void SimplifyCommand::toStream(std::ostream
& out
,
1586 OutputLanguage language
) const
1588 Printer::getPrinter(language
)->toStreamCmdSimplify(out
, d_term
.getNode());
1591 /* -------------------------------------------------------------------------- */
1592 /* class GetValueCommand */
1593 /* -------------------------------------------------------------------------- */
1595 GetValueCommand::GetValueCommand(api::Term term
) : d_terms()
1597 d_terms
.push_back(term
);
1600 GetValueCommand::GetValueCommand(const std::vector
<api::Term
>& terms
)
1603 PrettyCheckArgument(
1604 terms
.size() >= 1, terms
, "cannot get-value of an empty set of terms");
1607 const std::vector
<api::Term
>& GetValueCommand::getTerms() const
1611 void GetValueCommand::invoke(api::Solver
* solver
)
1615 std::vector
<api::Term
> result
= solver
->getValue(d_terms
);
1616 Assert(result
.size() == d_terms
.size());
1617 for (int i
= 0, size
= d_terms
.size(); i
< size
; i
++)
1619 api::Term request
= d_terms
[i
];
1620 api::Term value
= result
[i
];
1621 result
[i
] = solver
->mkTerm(api::SEXPR
, request
, value
);
1623 d_result
= solver
->mkTerm(api::SEXPR
, result
);
1624 d_commandStatus
= CommandSuccess::instance();
1626 catch (api::CVC4ApiRecoverableException
& e
)
1628 d_commandStatus
= new CommandRecoverableFailure(e
.what());
1630 catch (UnsafeInterruptException
& e
)
1632 d_commandStatus
= new CommandInterrupted();
1634 catch (exception
& e
)
1636 d_commandStatus
= new CommandFailure(e
.what());
1640 api::Term
GetValueCommand::getResult() const { return d_result
; }
1641 void GetValueCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
1645 this->Command::printResult(out
, verbosity
);
1649 expr::ExprDag::Scope
scope(out
, false);
1650 out
<< d_result
<< endl
;
1654 Command
* GetValueCommand::clone() const
1656 GetValueCommand
* c
= new GetValueCommand(d_terms
);
1657 c
->d_result
= d_result
;
1661 std::string
GetValueCommand::getCommandName() const { return "get-value"; }
1663 void GetValueCommand::toStream(std::ostream
& out
,
1667 OutputLanguage language
) const
1669 Printer::getPrinter(language
)->toStreamCmdGetValue(
1670 out
, api::termVectorToNodes(d_terms
));
1673 /* -------------------------------------------------------------------------- */
1674 /* class GetAssignmentCommand */
1675 /* -------------------------------------------------------------------------- */
1677 GetAssignmentCommand::GetAssignmentCommand() {}
1678 void GetAssignmentCommand::invoke(api::Solver
* solver
)
1682 std::vector
<std::pair
<Expr
, Expr
>> assignments
=
1683 solver
->getSmtEngine()->getAssignment();
1684 vector
<SExpr
> sexprs
;
1685 for (const auto& p
: assignments
)
1688 v
.emplace_back(SExpr::Keyword(p
.first
.toString()));
1689 v
.emplace_back(SExpr::Keyword(p
.second
.toString()));
1690 sexprs
.emplace_back(v
);
1692 d_result
= SExpr(sexprs
);
1693 d_commandStatus
= CommandSuccess::instance();
1695 catch (RecoverableModalException
& e
)
1697 d_commandStatus
= new CommandRecoverableFailure(e
.what());
1699 catch (UnsafeInterruptException
& e
)
1701 d_commandStatus
= new CommandInterrupted();
1703 catch (exception
& e
)
1705 d_commandStatus
= new CommandFailure(e
.what());
1709 SExpr
GetAssignmentCommand::getResult() const { return d_result
; }
1710 void GetAssignmentCommand::printResult(std::ostream
& out
,
1711 uint32_t verbosity
) const
1715 this->Command::printResult(out
, verbosity
);
1719 out
<< d_result
<< endl
;
1723 Command
* GetAssignmentCommand::clone() const
1725 GetAssignmentCommand
* c
= new GetAssignmentCommand();
1726 c
->d_result
= d_result
;
1730 std::string
GetAssignmentCommand::getCommandName() const
1732 return "get-assignment";
1735 void GetAssignmentCommand::toStream(std::ostream
& out
,
1739 OutputLanguage language
) const
1741 Printer::getPrinter(language
)->toStreamCmdGetAssignment(out
);
1744 /* -------------------------------------------------------------------------- */
1745 /* class GetModelCommand */
1746 /* -------------------------------------------------------------------------- */
1748 GetModelCommand::GetModelCommand() : d_result(nullptr) {}
1749 void GetModelCommand::invoke(api::Solver
* solver
)
1753 d_result
= solver
->getSmtEngine()->getModel();
1754 d_commandStatus
= CommandSuccess::instance();
1756 catch (RecoverableModalException
& e
)
1758 d_commandStatus
= new CommandRecoverableFailure(e
.what());
1760 catch (UnsafeInterruptException
& e
)
1762 d_commandStatus
= new CommandInterrupted();
1764 catch (exception
& e
)
1766 d_commandStatus
= new CommandFailure(e
.what());
1770 /* Model is private to the library -- for now
1771 Model* GetModelCommand::getResult() const {
1776 void GetModelCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
1780 this->Command::printResult(out
, verbosity
);
1788 Command
* GetModelCommand::clone() const
1790 GetModelCommand
* c
= new GetModelCommand();
1791 c
->d_result
= d_result
;
1795 std::string
GetModelCommand::getCommandName() const { return "get-model"; }
1797 void GetModelCommand::toStream(std::ostream
& out
,
1801 OutputLanguage language
) const
1803 Printer::getPrinter(language
)->toStreamCmdGetModel(out
);
1806 /* -------------------------------------------------------------------------- */
1807 /* class BlockModelCommand */
1808 /* -------------------------------------------------------------------------- */
1810 BlockModelCommand::BlockModelCommand() {}
1811 void BlockModelCommand::invoke(api::Solver
* solver
)
1815 solver
->blockModel();
1816 d_commandStatus
= CommandSuccess::instance();
1818 catch (api::CVC4ApiRecoverableException
& e
)
1820 d_commandStatus
= new CommandRecoverableFailure(e
.what());
1822 catch (UnsafeInterruptException
& e
)
1824 d_commandStatus
= new CommandInterrupted();
1826 catch (exception
& e
)
1828 d_commandStatus
= new CommandFailure(e
.what());
1832 Command
* BlockModelCommand::clone() const
1834 BlockModelCommand
* c
= new BlockModelCommand();
1838 std::string
BlockModelCommand::getCommandName() const { return "block-model"; }
1840 void BlockModelCommand::toStream(std::ostream
& out
,
1844 OutputLanguage language
) const
1846 Printer::getPrinter(language
)->toStreamCmdBlockModel(out
);
1849 /* -------------------------------------------------------------------------- */
1850 /* class BlockModelValuesCommand */
1851 /* -------------------------------------------------------------------------- */
1853 BlockModelValuesCommand::BlockModelValuesCommand(
1854 const std::vector
<api::Term
>& terms
)
1857 PrettyCheckArgument(terms
.size() >= 1,
1859 "cannot block-model-values of an empty set of terms");
1862 const std::vector
<api::Term
>& BlockModelValuesCommand::getTerms() const
1866 void BlockModelValuesCommand::invoke(api::Solver
* solver
)
1870 solver
->blockModelValues(d_terms
);
1871 d_commandStatus
= CommandSuccess::instance();
1873 catch (RecoverableModalException
& e
)
1875 d_commandStatus
= new CommandRecoverableFailure(e
.what());
1877 catch (UnsafeInterruptException
& e
)
1879 d_commandStatus
= new CommandInterrupted();
1881 catch (exception
& e
)
1883 d_commandStatus
= new CommandFailure(e
.what());
1887 Command
* BlockModelValuesCommand::clone() const
1889 BlockModelValuesCommand
* c
= new BlockModelValuesCommand(d_terms
);
1893 std::string
BlockModelValuesCommand::getCommandName() const
1895 return "block-model-values";
1898 void BlockModelValuesCommand::toStream(std::ostream
& out
,
1902 OutputLanguage language
) const
1904 Printer::getPrinter(language
)->toStreamCmdBlockModelValues(
1905 out
, api::termVectorToNodes(d_terms
));
1908 /* -------------------------------------------------------------------------- */
1909 /* class GetProofCommand */
1910 /* -------------------------------------------------------------------------- */
1912 GetProofCommand::GetProofCommand() {}
1913 void GetProofCommand::invoke(api::Solver
* solver
)
1915 Unimplemented() << "Unimplemented get-proof\n";
1918 Command
* GetProofCommand::clone() const
1920 GetProofCommand
* c
= new GetProofCommand();
1924 std::string
GetProofCommand::getCommandName() const { return "get-proof"; }
1926 void GetProofCommand::toStream(std::ostream
& out
,
1930 OutputLanguage language
) const
1932 Printer::getPrinter(language
)->toStreamCmdGetProof(out
);
1935 /* -------------------------------------------------------------------------- */
1936 /* class GetInstantiationsCommand */
1937 /* -------------------------------------------------------------------------- */
1939 GetInstantiationsCommand::GetInstantiationsCommand() : d_solver(nullptr) {}
1940 void GetInstantiationsCommand::invoke(api::Solver
* solver
)
1945 d_commandStatus
= CommandSuccess::instance();
1947 catch (exception
& e
)
1949 d_commandStatus
= new CommandFailure(e
.what());
1953 void GetInstantiationsCommand::printResult(std::ostream
& out
,
1954 uint32_t verbosity
) const
1958 this->Command::printResult(out
, verbosity
);
1962 d_solver
->printInstantiations(out
);
1966 Command
* GetInstantiationsCommand::clone() const
1968 GetInstantiationsCommand
* c
= new GetInstantiationsCommand();
1969 // c->d_result = d_result;
1970 c
->d_solver
= d_solver
;
1974 std::string
GetInstantiationsCommand::getCommandName() const
1976 return "get-instantiations";
1979 void GetInstantiationsCommand::toStream(std::ostream
& out
,
1983 OutputLanguage language
) const
1985 Printer::getPrinter(language
)->toStreamCmdGetInstantiations(out
);
1988 /* -------------------------------------------------------------------------- */
1989 /* class GetSynthSolutionCommand */
1990 /* -------------------------------------------------------------------------- */
1992 GetSynthSolutionCommand::GetSynthSolutionCommand() : d_solver(nullptr) {}
1993 void GetSynthSolutionCommand::invoke(api::Solver
* solver
)
1998 d_commandStatus
= CommandSuccess::instance();
2000 catch (exception
& e
)
2002 d_commandStatus
= new CommandFailure(e
.what());
2006 void GetSynthSolutionCommand::printResult(std::ostream
& out
,
2007 uint32_t verbosity
) const
2011 this->Command::printResult(out
, verbosity
);
2015 d_solver
->printSynthSolution(out
);
2019 Command
* GetSynthSolutionCommand::clone() const
2021 GetSynthSolutionCommand
* c
= new GetSynthSolutionCommand();
2022 c
->d_solver
= d_solver
;
2026 std::string
GetSynthSolutionCommand::getCommandName() const
2028 return "get-synth-solution";
2031 void GetSynthSolutionCommand::toStream(std::ostream
& out
,
2035 OutputLanguage language
) const
2037 Printer::getPrinter(language
)->toStreamCmdGetSynthSolution(out
);
2040 /* -------------------------------------------------------------------------- */
2041 /* class GetInterpolCommand */
2042 /* -------------------------------------------------------------------------- */
2044 GetInterpolCommand::GetInterpolCommand(const std::string
& name
, api::Term conj
)
2045 : d_name(name
), d_conj(conj
), d_resultStatus(false)
2048 GetInterpolCommand::GetInterpolCommand(const std::string
& name
,
2051 : d_name(name
), d_conj(conj
), d_sygus_grammar(g
), d_resultStatus(false)
2055 api::Term
GetInterpolCommand::getConjecture() const { return d_conj
; }
2057 const api::Grammar
* GetInterpolCommand::getGrammar() const
2059 return d_sygus_grammar
;
2062 api::Term
GetInterpolCommand::getResult() const { return d_result
; }
2064 void GetInterpolCommand::invoke(api::Solver
* solver
)
2068 if (d_sygus_grammar
== nullptr)
2070 d_resultStatus
= solver
->getInterpolant(d_conj
, d_result
);
2075 solver
->getInterpolant(d_conj
, *d_sygus_grammar
, d_result
);
2077 d_commandStatus
= CommandSuccess::instance();
2079 catch (exception
& e
)
2081 d_commandStatus
= new CommandFailure(e
.what());
2085 void GetInterpolCommand::printResult(std::ostream
& out
,
2086 uint32_t verbosity
) const
2090 this->Command::printResult(out
, verbosity
);
2094 expr::ExprDag::Scope
scope(out
, false);
2097 out
<< "(define-fun " << d_name
<< " () Bool " << d_result
<< ")"
2102 out
<< "none" << std::endl
;
2107 Command
* GetInterpolCommand::clone() const
2109 GetInterpolCommand
* c
=
2110 new GetInterpolCommand(d_name
, d_conj
, d_sygus_grammar
);
2111 c
->d_result
= d_result
;
2112 c
->d_resultStatus
= d_resultStatus
;
2116 std::string
GetInterpolCommand::getCommandName() const
2118 return "get-interpol";
2121 void GetInterpolCommand::toStream(std::ostream
& out
,
2125 OutputLanguage language
) const
2127 Printer::getPrinter(language
)->toStreamCmdGetInterpol(
2128 out
, d_name
, d_conj
.getNode(), d_sygus_grammar
->resolve().getTypeNode());
2131 /* -------------------------------------------------------------------------- */
2132 /* class GetAbductCommand */
2133 /* -------------------------------------------------------------------------- */
2135 GetAbductCommand::GetAbductCommand(const std::string
& name
, api::Term conj
)
2136 : d_name(name
), d_conj(conj
), d_resultStatus(false)
2139 GetAbductCommand::GetAbductCommand(const std::string
& name
,
2142 : d_name(name
), d_conj(conj
), d_sygus_grammar(g
), d_resultStatus(false)
2146 api::Term
GetAbductCommand::getConjecture() const { return d_conj
; }
2148 const api::Grammar
* GetAbductCommand::getGrammar() const
2150 return d_sygus_grammar
;
2153 std::string
GetAbductCommand::getAbductName() const { return d_name
; }
2154 api::Term
GetAbductCommand::getResult() const { return d_result
; }
2156 void GetAbductCommand::invoke(api::Solver
* solver
)
2160 if (d_sygus_grammar
== nullptr)
2162 d_resultStatus
= solver
->getAbduct(d_conj
, d_result
);
2166 d_resultStatus
= solver
->getAbduct(d_conj
, *d_sygus_grammar
, d_result
);
2168 d_commandStatus
= CommandSuccess::instance();
2170 catch (exception
& e
)
2172 d_commandStatus
= new CommandFailure(e
.what());
2176 void GetAbductCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
2180 this->Command::printResult(out
, verbosity
);
2184 expr::ExprDag::Scope
scope(out
, false);
2187 out
<< "(define-fun " << d_name
<< " () Bool " << d_result
<< ")"
2192 out
<< "none" << std::endl
;
2197 Command
* GetAbductCommand::clone() const
2199 GetAbductCommand
* c
= new GetAbductCommand(d_name
, d_conj
, d_sygus_grammar
);
2200 c
->d_result
= d_result
;
2201 c
->d_resultStatus
= d_resultStatus
;
2205 std::string
GetAbductCommand::getCommandName() const { return "get-abduct"; }
2207 void GetAbductCommand::toStream(std::ostream
& out
,
2211 OutputLanguage language
) const
2213 Printer::getPrinter(language
)->toStreamCmdGetAbduct(
2214 out
, d_name
, d_conj
.getNode(), d_sygus_grammar
->resolve().getTypeNode());
2217 /* -------------------------------------------------------------------------- */
2218 /* class GetQuantifierEliminationCommand */
2219 /* -------------------------------------------------------------------------- */
2221 GetQuantifierEliminationCommand::GetQuantifierEliminationCommand()
2222 : d_term(), d_doFull(true)
2225 GetQuantifierEliminationCommand::GetQuantifierEliminationCommand(
2226 const api::Term
& term
, bool doFull
)
2227 : d_term(term
), d_doFull(doFull
)
2231 api::Term
GetQuantifierEliminationCommand::getTerm() const { return d_term
; }
2232 bool GetQuantifierEliminationCommand::getDoFull() const { return d_doFull
; }
2233 void GetQuantifierEliminationCommand::invoke(api::Solver
* solver
)
2239 d_result
= solver
->getQuantifierElimination(d_term
);
2243 d_result
= solver
->getQuantifierEliminationDisjunct(d_term
);
2245 d_commandStatus
= CommandSuccess::instance();
2247 catch (exception
& e
)
2249 d_commandStatus
= new CommandFailure(e
.what());
2253 api::Term
GetQuantifierEliminationCommand::getResult() const
2257 void GetQuantifierEliminationCommand::printResult(std::ostream
& out
,
2258 uint32_t verbosity
) const
2262 this->Command::printResult(out
, verbosity
);
2266 out
<< d_result
<< endl
;
2270 Command
* GetQuantifierEliminationCommand::clone() const
2272 GetQuantifierEliminationCommand
* c
=
2273 new GetQuantifierEliminationCommand(d_term
, d_doFull
);
2274 c
->d_result
= d_result
;
2278 std::string
GetQuantifierEliminationCommand::getCommandName() const
2280 return d_doFull
? "get-qe" : "get-qe-disjunct";
2283 void GetQuantifierEliminationCommand::toStream(std::ostream
& out
,
2287 OutputLanguage language
) const
2289 Printer::getPrinter(language
)->toStreamCmdGetQuantifierElimination(
2290 out
, d_term
.getNode());
2293 /* -------------------------------------------------------------------------- */
2294 /* class GetUnsatAssumptionsCommand */
2295 /* -------------------------------------------------------------------------- */
2297 GetUnsatAssumptionsCommand::GetUnsatAssumptionsCommand() {}
2299 void GetUnsatAssumptionsCommand::invoke(api::Solver
* solver
)
2303 d_result
= solver
->getUnsatAssumptions();
2304 d_commandStatus
= CommandSuccess::instance();
2306 catch (api::CVC4ApiRecoverableException
& e
)
2308 d_commandStatus
= new CommandRecoverableFailure(e
.what());
2310 catch (exception
& e
)
2312 d_commandStatus
= new CommandFailure(e
.what());
2316 std::vector
<api::Term
> GetUnsatAssumptionsCommand::getResult() const
2321 void GetUnsatAssumptionsCommand::printResult(std::ostream
& out
,
2322 uint32_t verbosity
) const
2326 this->Command::printResult(out
, verbosity
);
2330 container_to_stream(out
, d_result
, "(", ")\n", " ");
2334 Command
* GetUnsatAssumptionsCommand::clone() const
2336 GetUnsatAssumptionsCommand
* c
= new GetUnsatAssumptionsCommand
;
2337 c
->d_result
= d_result
;
2341 std::string
GetUnsatAssumptionsCommand::getCommandName() const
2343 return "get-unsat-assumptions";
2346 void GetUnsatAssumptionsCommand::toStream(std::ostream
& out
,
2350 OutputLanguage language
) const
2352 Printer::getPrinter(language
)->toStreamCmdGetUnsatAssumptions(out
);
2355 /* -------------------------------------------------------------------------- */
2356 /* class GetUnsatCoreCommand */
2357 /* -------------------------------------------------------------------------- */
2359 GetUnsatCoreCommand::GetUnsatCoreCommand() {}
2360 void GetUnsatCoreCommand::invoke(api::Solver
* solver
)
2365 d_result
= solver
->getUnsatCore();
2367 d_commandStatus
= CommandSuccess::instance();
2369 catch (api::CVC4ApiRecoverableException
& e
)
2371 d_commandStatus
= new CommandRecoverableFailure(e
.what());
2373 catch (exception
& e
)
2375 d_commandStatus
= new CommandFailure(e
.what());
2379 void GetUnsatCoreCommand::printResult(std::ostream
& out
,
2380 uint32_t verbosity
) const
2384 this->Command::printResult(out
, verbosity
);
2388 UnsatCore
ucr(d_solver
->getSmtEngine(), api::termVectorToNodes(d_result
));
2393 const std::vector
<api::Term
>& GetUnsatCoreCommand::getUnsatCore() const
2395 // of course, this will be empty if the command hasn't been invoked yet
2399 Command
* GetUnsatCoreCommand::clone() const
2401 GetUnsatCoreCommand
* c
= new GetUnsatCoreCommand
;
2402 c
->d_solver
= d_solver
;
2403 c
->d_result
= d_result
;
2407 std::string
GetUnsatCoreCommand::getCommandName() const
2409 return "get-unsat-core";
2412 void GetUnsatCoreCommand::toStream(std::ostream
& out
,
2416 OutputLanguage language
) const
2418 Printer::getPrinter(language
)->toStreamCmdGetUnsatCore(out
);
2421 /* -------------------------------------------------------------------------- */
2422 /* class GetAssertionsCommand */
2423 /* -------------------------------------------------------------------------- */
2425 GetAssertionsCommand::GetAssertionsCommand() {}
2426 void GetAssertionsCommand::invoke(api::Solver
* solver
)
2431 const vector
<api::Term
> v
= solver
->getAssertions();
2433 copy(v
.begin(), v
.end(), ostream_iterator
<api::Term
>(ss
, "\n"));
2435 d_result
= ss
.str();
2436 d_commandStatus
= CommandSuccess::instance();
2438 catch (exception
& e
)
2440 d_commandStatus
= new CommandFailure(e
.what());
2444 std::string
GetAssertionsCommand::getResult() const { return d_result
; }
2445 void GetAssertionsCommand::printResult(std::ostream
& out
,
2446 uint32_t verbosity
) const
2450 this->Command::printResult(out
, verbosity
);
2458 Command
* GetAssertionsCommand::clone() const
2460 GetAssertionsCommand
* c
= new GetAssertionsCommand();
2461 c
->d_result
= d_result
;
2465 std::string
GetAssertionsCommand::getCommandName() const
2467 return "get-assertions";
2470 void GetAssertionsCommand::toStream(std::ostream
& out
,
2474 OutputLanguage language
) const
2476 Printer::getPrinter(language
)->toStreamCmdGetAssertions(out
);
2479 /* -------------------------------------------------------------------------- */
2480 /* class SetBenchmarkStatusCommand */
2481 /* -------------------------------------------------------------------------- */
2483 SetBenchmarkStatusCommand::SetBenchmarkStatusCommand(BenchmarkStatus status
)
2488 BenchmarkStatus
SetBenchmarkStatusCommand::getStatus() const
2493 void SetBenchmarkStatusCommand::invoke(api::Solver
* solver
)
2499 solver
->setInfo("status", ss
.str());
2500 d_commandStatus
= CommandSuccess::instance();
2502 catch (exception
& e
)
2504 d_commandStatus
= new CommandFailure(e
.what());
2508 Command
* SetBenchmarkStatusCommand::clone() const
2510 return new SetBenchmarkStatusCommand(d_status
);
2513 std::string
SetBenchmarkStatusCommand::getCommandName() const
2518 void SetBenchmarkStatusCommand::toStream(std::ostream
& out
,
2522 OutputLanguage language
) const
2524 Result::Sat status
= Result::SAT_UNKNOWN
;
2527 case BenchmarkStatus::SMT_SATISFIABLE
: status
= Result::SAT
; break;
2528 case BenchmarkStatus::SMT_UNSATISFIABLE
: status
= Result::UNSAT
; break;
2529 case BenchmarkStatus::SMT_UNKNOWN
: status
= Result::SAT_UNKNOWN
; break;
2532 Printer::getPrinter(language
)->toStreamCmdSetBenchmarkStatus(out
, status
);
2535 /* -------------------------------------------------------------------------- */
2536 /* class SetBenchmarkLogicCommand */
2537 /* -------------------------------------------------------------------------- */
2539 SetBenchmarkLogicCommand::SetBenchmarkLogicCommand(std::string logic
)
2544 std::string
SetBenchmarkLogicCommand::getLogic() const { return d_logic
; }
2545 void SetBenchmarkLogicCommand::invoke(api::Solver
* solver
)
2549 solver
->setLogic(d_logic
);
2550 d_commandStatus
= CommandSuccess::instance();
2552 catch (exception
& e
)
2554 d_commandStatus
= new CommandFailure(e
.what());
2558 Command
* SetBenchmarkLogicCommand::clone() const
2560 return new SetBenchmarkLogicCommand(d_logic
);
2563 std::string
SetBenchmarkLogicCommand::getCommandName() const
2568 void SetBenchmarkLogicCommand::toStream(std::ostream
& out
,
2572 OutputLanguage language
) const
2574 Printer::getPrinter(language
)->toStreamCmdSetBenchmarkLogic(out
, d_logic
);
2577 /* -------------------------------------------------------------------------- */
2578 /* class SetInfoCommand */
2579 /* -------------------------------------------------------------------------- */
2581 SetInfoCommand::SetInfoCommand(std::string flag
, const SExpr
& sexpr
)
2582 : d_flag(flag
), d_sexpr(sexpr
)
2586 std::string
SetInfoCommand::getFlag() const { return d_flag
; }
2587 SExpr
SetInfoCommand::getSExpr() const { return d_sexpr
; }
2588 void SetInfoCommand::invoke(api::Solver
* solver
)
2592 solver
->getSmtEngine()->setInfo(d_flag
, d_sexpr
);
2593 d_commandStatus
= CommandSuccess::instance();
2595 catch (UnrecognizedOptionException
&)
2597 // As per SMT-LIB spec, silently accept unknown set-info keys
2598 d_commandStatus
= CommandSuccess::instance();
2600 catch (exception
& e
)
2602 d_commandStatus
= new CommandFailure(e
.what());
2606 Command
* SetInfoCommand::clone() const
2608 return new SetInfoCommand(d_flag
, d_sexpr
);
2611 std::string
SetInfoCommand::getCommandName() const { return "set-info"; }
2613 void SetInfoCommand::toStream(std::ostream
& out
,
2617 OutputLanguage language
) const
2619 Printer::getPrinter(language
)->toStreamCmdSetInfo(out
, d_flag
, d_sexpr
);
2622 /* -------------------------------------------------------------------------- */
2623 /* class GetInfoCommand */
2624 /* -------------------------------------------------------------------------- */
2626 GetInfoCommand::GetInfoCommand(std::string flag
) : d_flag(flag
) {}
2627 std::string
GetInfoCommand::getFlag() const { return d_flag
; }
2628 void GetInfoCommand::invoke(api::Solver
* solver
)
2633 v
.push_back(SExpr(SExpr::Keyword(string(":") + d_flag
)));
2634 v
.emplace_back(solver
->getSmtEngine()->getInfo(d_flag
));
2636 if (d_flag
== "all-options" || d_flag
== "all-statistics")
2638 ss
<< PrettySExprs(true);
2641 d_result
= ss
.str();
2642 d_commandStatus
= CommandSuccess::instance();
2644 catch (UnrecognizedOptionException
&)
2646 d_commandStatus
= new CommandUnsupported();
2648 catch (RecoverableModalException
& e
)
2650 d_commandStatus
= new CommandRecoverableFailure(e
.what());
2652 catch (exception
& e
)
2654 d_commandStatus
= new CommandFailure(e
.what());
2658 std::string
GetInfoCommand::getResult() const { return d_result
; }
2659 void GetInfoCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
2663 this->Command::printResult(out
, verbosity
);
2665 else if (d_result
!= "")
2667 out
<< d_result
<< endl
;
2671 Command
* GetInfoCommand::clone() const
2673 GetInfoCommand
* c
= new GetInfoCommand(d_flag
);
2674 c
->d_result
= d_result
;
2678 std::string
GetInfoCommand::getCommandName() const { return "get-info"; }
2680 void GetInfoCommand::toStream(std::ostream
& out
,
2684 OutputLanguage language
) const
2686 Printer::getPrinter(language
)->toStreamCmdGetInfo(out
, d_flag
);
2689 /* -------------------------------------------------------------------------- */
2690 /* class SetOptionCommand */
2691 /* -------------------------------------------------------------------------- */
2693 SetOptionCommand::SetOptionCommand(std::string flag
, const SExpr
& sexpr
)
2694 : d_flag(flag
), d_sexpr(sexpr
)
2698 std::string
SetOptionCommand::getFlag() const { return d_flag
; }
2699 SExpr
SetOptionCommand::getSExpr() const { return d_sexpr
; }
2700 void SetOptionCommand::invoke(api::Solver
* solver
)
2704 solver
->getSmtEngine()->setOption(d_flag
, d_sexpr
);
2705 d_commandStatus
= CommandSuccess::instance();
2707 catch (UnrecognizedOptionException
&)
2709 d_commandStatus
= new CommandUnsupported();
2711 catch (exception
& e
)
2713 d_commandStatus
= new CommandFailure(e
.what());
2717 Command
* SetOptionCommand::clone() const
2719 return new SetOptionCommand(d_flag
, d_sexpr
);
2722 std::string
SetOptionCommand::getCommandName() const { return "set-option"; }
2724 void SetOptionCommand::toStream(std::ostream
& out
,
2728 OutputLanguage language
) const
2730 Printer::getPrinter(language
)->toStreamCmdSetOption(out
, d_flag
, d_sexpr
);
2733 /* -------------------------------------------------------------------------- */
2734 /* class GetOptionCommand */
2735 /* -------------------------------------------------------------------------- */
2737 GetOptionCommand::GetOptionCommand(std::string flag
) : d_flag(flag
) {}
2738 std::string
GetOptionCommand::getFlag() const { return d_flag
; }
2739 void GetOptionCommand::invoke(api::Solver
* solver
)
2743 d_result
= solver
->getOption(d_flag
);
2744 d_commandStatus
= CommandSuccess::instance();
2746 catch (UnrecognizedOptionException
&)
2748 d_commandStatus
= new CommandUnsupported();
2750 catch (exception
& e
)
2752 d_commandStatus
= new CommandFailure(e
.what());
2756 std::string
GetOptionCommand::getResult() const { return d_result
; }
2757 void GetOptionCommand::printResult(std::ostream
& out
, uint32_t verbosity
) const
2761 this->Command::printResult(out
, verbosity
);
2763 else if (d_result
!= "")
2765 out
<< d_result
<< endl
;
2769 Command
* GetOptionCommand::clone() const
2771 GetOptionCommand
* c
= new GetOptionCommand(d_flag
);
2772 c
->d_result
= d_result
;
2776 std::string
GetOptionCommand::getCommandName() const { return "get-option"; }
2778 void GetOptionCommand::toStream(std::ostream
& out
,
2782 OutputLanguage language
) const
2784 Printer::getPrinter(language
)->toStreamCmdGetOption(out
, d_flag
);
2787 /* -------------------------------------------------------------------------- */
2788 /* class SetExpressionNameCommand */
2789 /* -------------------------------------------------------------------------- */
2791 SetExpressionNameCommand::SetExpressionNameCommand(api::Term term
,
2793 : d_term(term
), d_name(name
)
2797 void SetExpressionNameCommand::invoke(api::Solver
* solver
)
2799 solver
->getSmtEngine()->setExpressionName(d_term
.getExpr(), d_name
);
2800 d_commandStatus
= CommandSuccess::instance();
2803 Command
* SetExpressionNameCommand::clone() const
2805 SetExpressionNameCommand
* c
= new SetExpressionNameCommand(d_term
, d_name
);
2809 std::string
SetExpressionNameCommand::getCommandName() const
2811 return "set-expr-name";
2814 void SetExpressionNameCommand::toStream(std::ostream
& out
,
2818 OutputLanguage language
) const
2820 Printer::getPrinter(language
)->toStreamCmdSetExpressionName(
2821 out
, d_term
.getNode(), d_name
);
2824 /* -------------------------------------------------------------------------- */
2825 /* class DatatypeDeclarationCommand */
2826 /* -------------------------------------------------------------------------- */
2828 DatatypeDeclarationCommand::DatatypeDeclarationCommand(
2829 const api::Sort
& datatype
)
2832 d_datatypes
.push_back(datatype
);
2835 DatatypeDeclarationCommand::DatatypeDeclarationCommand(
2836 const std::vector
<api::Sort
>& datatypes
)
2837 : d_datatypes(datatypes
)
2841 const std::vector
<api::Sort
>& DatatypeDeclarationCommand::getDatatypes() const
2846 void DatatypeDeclarationCommand::invoke(api::Solver
* solver
)
2848 d_commandStatus
= CommandSuccess::instance();
2851 Command
* DatatypeDeclarationCommand::clone() const
2853 return new DatatypeDeclarationCommand(d_datatypes
);
2856 std::string
DatatypeDeclarationCommand::getCommandName() const
2858 return "declare-datatypes";
2861 void DatatypeDeclarationCommand::toStream(std::ostream
& out
,
2865 OutputLanguage language
) const
2867 Printer::getPrinter(language
)->toStreamCmdDatatypeDeclaration(
2868 out
, api::sortVectorToTypeNodes(d_datatypes
));