GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/smt/command.cpp Lines: 690 1231 56.1 %
Date: 2021-11-07 Branches: 489 1497 32.7 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Tim King, Abdalrhman Mohamed, Morgan Deters
4
 *
5
 * This file is part of the cvc5 project.
6
 *
7
 * Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
8
 * in the top-level source directory and their institutional affiliations.
9
 * All rights reserved.  See the file COPYING in the top-level source
10
 * directory for licensing information.
11
 * ****************************************************************************
12
 *
13
 * Implementation of command objects.
14
 */
15
16
#include "smt/command.h"
17
18
#include <exception>
19
#include <iostream>
20
#include <iterator>
21
#include <sstream>
22
#include <utility>
23
#include <vector>
24
25
#include "api/cpp/cvc5.h"
26
#include "base/check.h"
27
#include "base/modal_exception.h"
28
#include "base/output.h"
29
#include "expr/expr_iomanip.h"
30
#include "expr/node.h"
31
#include "expr/symbol_manager.h"
32
#include "expr/type_node.h"
33
#include "options/main_options.h"
34
#include "options/options.h"
35
#include "options/printer_options.h"
36
#include "options/smt_options.h"
37
#include "printer/printer.h"
38
#include "proof/unsat_core.h"
39
#include "smt/model.h"
40
#include "util/smt2_quote_string.h"
41
#include "util/unsafe_interrupt_exception.h"
42
#include "util/utility.h"
43
44
using namespace std;
45
46
namespace cvc5 {
47
48
8664
std::string sexprToString(api::Term sexpr)
49
{
50
  // if sexpr is a constant string, return the stored constant string. We don't
51
  // call Term::toString as its result depends on the output language.
52
  // Notice that we only check for string constants. The sexprs generated by the
53
  // parser don't contains other constants, so we can ignore them.
54
8664
  if (sexpr.isStringValue())
55
  {
56
    // convert std::wstring to std::string
57
17290
    std::wstring wstring = sexpr.getStringValue();
58
8645
    return std::string(wstring.cbegin(), wstring.cend());
59
  }
60
61
  // if sexpr is not a spec constant, make sure it is an array of sub-sexprs
62
19
  Assert(sexpr.getKind() == api::SEXPR);
63
64
38
  std::stringstream ss;
65
38
  auto it = sexpr.begin();
66
67
  // recursively print the sub-sexprs
68
19
  ss << '(' << sexprToString(*it);
69
19
  ++it;
70
75
  while (it != sexpr.end())
71
  {
72
28
    ss << ' ' << sexprToString(*it);
73
28
    ++it;
74
  }
75
19
  ss << ')';
76
77
19
  return ss.str();
78
}
79
80
10379
const int CommandPrintSuccess::s_iosIndex = std::ios_base::xalloc();
81
10379
const CommandSuccess* CommandSuccess::s_instance = new CommandSuccess();
82
10379
const CommandInterrupted* CommandInterrupted::s_instance =
83
10379
    new CommandInterrupted();
84
85
22
std::ostream& operator<<(std::ostream& out, const Command& c)
86
{
87
44
  c.toStream(out,
88
22
             Node::setdepth::getDepth(out),
89
             Node::dag::getDag(out),
90
22
             Node::setlanguage::getLanguage(out));
91
22
  return out;
92
}
93
94
11
ostream& operator<<(ostream& out, const Command* c)
95
{
96
11
  if (c == NULL)
97
  {
98
    out << "null";
99
  }
100
  else
101
  {
102
11
    out << *c;
103
  }
104
11
  return out;
105
}
106
107
224651
std::ostream& operator<<(std::ostream& out, const CommandStatus& s)
108
{
109
224651
  s.toStream(out, Node::setlanguage::getLanguage(out));
110
224651
  return out;
111
}
112
113
ostream& operator<<(ostream& out, const CommandStatus* s)
114
{
115
  if (s == NULL)
116
  {
117
    out << "null";
118
  }
119
  else
120
  {
121
    out << *s;
122
  }
123
  return out;
124
}
125
126
/* output stream insertion operator for benchmark statuses */
127
std::ostream& operator<<(std::ostream& out, BenchmarkStatus status)
128
{
129
  switch (status)
130
  {
131
    case SMT_SATISFIABLE: return out << "sat";
132
133
    case SMT_UNSATISFIABLE: return out << "unsat";
134
135
    case SMT_UNKNOWN: return out << "unknown";
136
137
    default: return out << "BenchmarkStatus::[UNKNOWNSTATUS!]";
138
  }
139
}
140
141
/* -------------------------------------------------------------------------- */
142
/* class CommandPrintSuccess                                                  */
143
/* -------------------------------------------------------------------------- */
144
145
142
void CommandPrintSuccess::applyPrintSuccess(std::ostream& out)
146
{
147
142
  out.iword(s_iosIndex) = d_printSuccess;
148
142
}
149
150
224583
bool CommandPrintSuccess::getPrintSuccess(std::ostream& out)
151
{
152
224583
  return out.iword(s_iosIndex);
153
}
154
155
void CommandPrintSuccess::setPrintSuccess(std::ostream& out, bool printSuccess)
156
{
157
  out.iword(s_iosIndex) = printSuccess;
158
}
159
160
142
std::ostream& operator<<(std::ostream& out, CommandPrintSuccess cps)
161
{
162
142
  cps.applyPrintSuccess(out);
163
142
  return out;
164
}
165
166
/* -------------------------------------------------------------------------- */
167
/* class Command                                                              */
168
/* -------------------------------------------------------------------------- */
169
170
242774
Command::Command() : d_commandStatus(nullptr), d_muted(false) {}
171
172
Command::Command(const Command& cmd)
173
{
174
  d_commandStatus =
175
      (cmd.d_commandStatus == NULL) ? NULL : &cmd.d_commandStatus->clone();
176
  d_muted = cmd.d_muted;
177
}
178
179
485548
Command::~Command()
180
{
181
242774
  if (d_commandStatus != NULL && d_commandStatus != CommandSuccess::instance())
182
  {
183
68
    delete d_commandStatus;
184
  }
185
242774
}
186
187
242415
bool Command::ok() const
188
{
189
  // either we haven't run the command yet, or it ran successfully
190
242415
  return d_commandStatus == NULL
191
242415
         || dynamic_cast<const CommandSuccess*>(d_commandStatus) != NULL;
192
}
193
194
242428
bool Command::fail() const
195
{
196
242428
  return d_commandStatus != NULL
197
242428
         && dynamic_cast<const CommandFailure*>(d_commandStatus) != NULL;
198
}
199
200
236281
bool Command::interrupted() const
201
{
202
236281
  return d_commandStatus != NULL
203
236281
         && dynamic_cast<const CommandInterrupted*>(d_commandStatus) != NULL;
204
}
205
206
242430
void Command::invoke(api::Solver* solver, SymbolManager* sm, std::ostream& out)
207
{
208
242430
  invoke(solver, sm);
209
242430
  if (!(isMuted() && ok()))
210
  {
211
236359
    printResult(
212
        out,
213
472718
        std::stoul(solver->getOption("command-verbosity:" + getCommandName())));
214
  }
215
242430
}
216
217
6
std::string Command::toString() const
218
{
219
12
  std::stringstream ss;
220
6
  toStream(ss);
221
12
  return ss.str();
222
}
223
224
226628
void CommandStatus::toStream(std::ostream& out, Language language) const
225
{
226
226628
  Printer::getPrinter(language)->toStream(out, this);
227
226628
}
228
229
224746
void Command::printResult(std::ostream& out, uint32_t verbosity) const
230
{
231
224746
  if (d_commandStatus != NULL)
232
  {
233
224660
    if ((!ok() && verbosity >= 1) || verbosity >= 2)
234
    {
235
224651
      out << *d_commandStatus;
236
    }
237
  }
238
224746
}
239
240
43
void Command::resetSolver(api::Solver* solver)
241
{
242
86
  std::unique_ptr<Options> opts = std::make_unique<Options>();
243
43
  opts->copyValues(*solver->d_originalOptions);
244
  // This reconstructs a new solver object at the same memory location as the
245
  // current one. Note that this command does not own the solver object!
246
  // It may be safer to instead make the ResetCommand a special case in the
247
  // CommandExecutor such that this reconstruction can be done within the
248
  // CommandExecutor, who actually owns the solver.
249
43
  solver->~Solver();
250
43
  new (solver) api::Solver(std::move(opts));
251
43
}
252
253
28
Node Command::termToNode(const api::Term& term) { return term.getNode(); }
254
255
20
std::vector<Node> Command::termVectorToNodes(
256
    const std::vector<api::Term>& terms)
257
{
258
20
  return api::Term::termVectorToNodes(terms);
259
}
260
261
22
TypeNode Command::sortToTypeNode(const api::Sort& sort)
262
{
263
22
  return sort.getTypeNode();
264
}
265
266
2
std::vector<TypeNode> Command::sortVectorToTypeNodes(
267
    const std::vector<api::Sort>& sorts)
268
{
269
2
  return api::Sort::sortVectorToTypeNodes(sorts);
270
}
271
272
1
TypeNode Command::grammarToTypeNode(api::Grammar* grammar)
273
{
274
  return grammar == nullptr ? TypeNode::null()
275
1
                            : sortToTypeNode(grammar->resolve());
276
}
277
278
/* -------------------------------------------------------------------------- */
279
/* class EmptyCommand                                                         */
280
/* -------------------------------------------------------------------------- */
281
282
84
EmptyCommand::EmptyCommand(std::string name) : d_name(name) {}
283
std::string EmptyCommand::getName() const { return d_name; }
284
84
void EmptyCommand::invoke(api::Solver* solver, SymbolManager* sm)
285
{
286
  /* empty commands have no implementation */
287
84
  d_commandStatus = CommandSuccess::instance();
288
84
}
289
290
Command* EmptyCommand::clone() const { return new EmptyCommand(d_name); }
291
168
std::string EmptyCommand::getCommandName() const { return "empty"; }
292
293
void EmptyCommand::toStream(std::ostream& out,
294
                            int toDepth,
295
                            size_t dag,
296
                            Language language) const
297
{
298
  Printer::getPrinter(language)->toStreamCmdEmpty(out, d_name);
299
}
300
301
/* -------------------------------------------------------------------------- */
302
/* class EchoCommand                                                          */
303
/* -------------------------------------------------------------------------- */
304
305
9
EchoCommand::EchoCommand(std::string output) : d_output(output) {}
306
307
std::string EchoCommand::getOutput() const { return d_output; }
308
309
void EchoCommand::invoke(api::Solver* solver, SymbolManager* sm)
310
{
311
  /* we don't have an output stream here, nothing to do */
312
  d_commandStatus = CommandSuccess::instance();
313
}
314
315
9
void EchoCommand::invoke(api::Solver* solver,
316
                         SymbolManager* sm,
317
                         std::ostream& out)
318
{
319
9
  out << cvc5::quoteString(d_output) << std::endl;
320
18
  Trace("dtview::command") << "* ~COMMAND: echo |" << d_output << "|~"
321
9
                           << std::endl;
322
9
  d_commandStatus = CommandSuccess::instance();
323
18
  printResult(
324
      out,
325
18
      std::stoul(solver->getOption("command-verbosity:" + getCommandName())));
326
9
}
327
328
Command* EchoCommand::clone() const { return new EchoCommand(d_output); }
329
330
18
std::string EchoCommand::getCommandName() const { return "echo"; }
331
332
void EchoCommand::toStream(std::ostream& out,
333
                           int toDepth,
334
                           size_t dag,
335
                           Language language) const
336
{
337
  Printer::getPrinter(language)->toStreamCmdEcho(out, d_output);
338
}
339
340
/* -------------------------------------------------------------------------- */
341
/* class AssertCommand                                                        */
342
/* -------------------------------------------------------------------------- */
343
344
81119
AssertCommand::AssertCommand(const api::Term& t) : d_term(t) {}
345
346
api::Term AssertCommand::getTerm() const { return d_term; }
347
81087
void AssertCommand::invoke(api::Solver* solver, SymbolManager* sm)
348
{
349
  try
350
  {
351
81087
    solver->assertFormula(d_term);
352
81086
    d_commandStatus = CommandSuccess::instance();
353
  }
354
  catch (UnsafeInterruptException& e)
355
  {
356
    d_commandStatus = new CommandInterrupted();
357
  }
358
2
  catch (exception& e)
359
  {
360
1
    d_commandStatus = new CommandFailure(e.what());
361
  }
362
81087
}
363
364
Command* AssertCommand::clone() const { return new AssertCommand(d_term); }
365
366
162174
std::string AssertCommand::getCommandName() const { return "assert"; }
367
368
7
void AssertCommand::toStream(std::ostream& out,
369
                             int toDepth,
370
                             size_t dag,
371
                             Language language) const
372
{
373
7
  Printer::getPrinter(language)->toStreamCmdAssert(out, termToNode(d_term));
374
7
}
375
376
/* -------------------------------------------------------------------------- */
377
/* class PushCommand                                                          */
378
/* -------------------------------------------------------------------------- */
379
380
3418
void PushCommand::invoke(api::Solver* solver, SymbolManager* sm)
381
{
382
  try
383
  {
384
3418
    solver->push();
385
3418
    d_commandStatus = CommandSuccess::instance();
386
  }
387
  catch (UnsafeInterruptException& e)
388
  {
389
    d_commandStatus = new CommandInterrupted();
390
  }
391
  catch (exception& e)
392
  {
393
    d_commandStatus = new CommandFailure(e.what());
394
  }
395
3418
}
396
397
Command* PushCommand::clone() const { return new PushCommand(); }
398
6826
std::string PushCommand::getCommandName() const { return "push"; }
399
400
void PushCommand::toStream(std::ostream& out,
401
                           int toDepth,
402
                           size_t dag,
403
                           Language language) const
404
{
405
  Printer::getPrinter(language)->toStreamCmdPush(out);
406
}
407
408
/* -------------------------------------------------------------------------- */
409
/* class PopCommand                                                           */
410
/* -------------------------------------------------------------------------- */
411
412
2876
void PopCommand::invoke(api::Solver* solver, SymbolManager* sm)
413
{
414
  try
415
  {
416
2876
    solver->pop();
417
2876
    d_commandStatus = CommandSuccess::instance();
418
  }
419
  catch (UnsafeInterruptException& e)
420
  {
421
    d_commandStatus = new CommandInterrupted();
422
  }
423
  catch (exception& e)
424
  {
425
    d_commandStatus = new CommandFailure(e.what());
426
  }
427
2876
}
428
429
Command* PopCommand::clone() const { return new PopCommand(); }
430
5596
std::string PopCommand::getCommandName() const { return "pop"; }
431
432
void PopCommand::toStream(std::ostream& out,
433
                          int toDepth,
434
                          size_t dag,
435
                          Language language) const
436
{
437
  Printer::getPrinter(language)->toStreamCmdPop(out);
438
}
439
440
/* -------------------------------------------------------------------------- */
441
/* class CheckSatCommand                                                      */
442
/* -------------------------------------------------------------------------- */
443
444
9451
CheckSatCommand::CheckSatCommand() {}
445
446
9441
void CheckSatCommand::invoke(api::Solver* solver, SymbolManager* sm)
447
{
448
18882
  Trace("dtview::command") << "* ~COMMAND: " << getCommandName() << "~"
449
9441
                           << std::endl;
450
  try
451
  {
452
9441
    d_result = solver->checkSat();
453
9421
    d_commandStatus = CommandSuccess::instance();
454
  }
455
40
  catch (exception& e)
456
  {
457
20
    d_commandStatus = new CommandFailure(e.what());
458
  }
459
9441
}
460
461
9441
api::Result CheckSatCommand::getResult() const { return d_result; }
462
463
9441
void CheckSatCommand::printResult(std::ostream& out, uint32_t verbosity) const
464
{
465
9441
  if (!ok())
466
  {
467
20
    this->Command::printResult(out, verbosity);
468
  }
469
  else
470
  {
471
9421
    Trace("dtview::command") << "* RESULT: " << d_result << std::endl;
472
9421
    out << d_result << endl;
473
  }
474
9441
}
475
476
Command* CheckSatCommand::clone() const
477
{
478
  CheckSatCommand* c = new CheckSatCommand();
479
  c->d_result = d_result;
480
  return c;
481
}
482
483
28323
std::string CheckSatCommand::getCommandName() const { return "check-sat"; }
484
485
1
void CheckSatCommand::toStream(std::ostream& out,
486
                               int toDepth,
487
                               size_t dag,
488
                               Language language) const
489
{
490
1
  Printer::getPrinter(language)->toStreamCmdCheckSat(out);
491
1
}
492
493
/* -------------------------------------------------------------------------- */
494
/* class CheckSatAssumingCommand                                              */
495
/* -------------------------------------------------------------------------- */
496
497
CheckSatAssumingCommand::CheckSatAssumingCommand(api::Term term)
498
    : d_terms({term})
499
{
500
}
501
502
1563
CheckSatAssumingCommand::CheckSatAssumingCommand(
503
1563
    const std::vector<api::Term>& terms)
504
1563
    : d_terms(terms)
505
{
506
1563
}
507
508
const std::vector<api::Term>& CheckSatAssumingCommand::getTerms() const
509
{
510
  return d_terms;
511
}
512
513
1563
void CheckSatAssumingCommand::invoke(api::Solver* solver, SymbolManager* sm)
514
{
515
3126
  Trace("dtview::command") << "* ~COMMAND: (check-sat-assuming ( " << d_terms
516
1563
                           << " )~" << std::endl;
517
  try
518
  {
519
1563
    d_result = solver->checkSatAssuming(d_terms);
520
1560
    d_commandStatus = CommandSuccess::instance();
521
  }
522
6
  catch (exception& e)
523
  {
524
3
    d_commandStatus = new CommandFailure(e.what());
525
  }
526
1563
}
527
528
1563
api::Result CheckSatAssumingCommand::getResult() const
529
{
530
1563
  Trace("dtview::command") << "* ~RESULT: " << d_result << "~" << std::endl;
531
1563
  return d_result;
532
}
533
534
1563
void CheckSatAssumingCommand::printResult(std::ostream& out,
535
                                          uint32_t verbosity) const
536
{
537
1563
  if (!ok())
538
  {
539
3
    this->Command::printResult(out, verbosity);
540
  }
541
  else
542
  {
543
1560
    out << d_result << endl;
544
  }
545
1563
}
546
547
Command* CheckSatAssumingCommand::clone() const
548
{
549
  CheckSatAssumingCommand* c = new CheckSatAssumingCommand(d_terms);
550
  c->d_result = d_result;
551
  return c;
552
}
553
554
3126
std::string CheckSatAssumingCommand::getCommandName() const
555
{
556
3126
  return "check-sat-assuming";
557
}
558
559
2
void CheckSatAssumingCommand::toStream(std::ostream& out,
560
                                       int toDepth,
561
                                       size_t dag,
562
                                       Language language) const
563
{
564
4
  Printer::getPrinter(language)->toStreamCmdCheckSatAssuming(
565
4
      out, termVectorToNodes(d_terms));
566
2
}
567
568
/* -------------------------------------------------------------------------- */
569
/* class QueryCommand                                                         */
570
/* -------------------------------------------------------------------------- */
571
572
23
QueryCommand::QueryCommand(const api::Term& t) : d_term(t) {}
573
574
api::Term QueryCommand::getTerm() const { return d_term; }
575
23
void QueryCommand::invoke(api::Solver* solver, SymbolManager* sm)
576
{
577
  try
578
  {
579
23
    d_result = solver->checkEntailed(d_term);
580
22
    d_commandStatus = CommandSuccess::instance();
581
  }
582
2
  catch (exception& e)
583
  {
584
1
    d_commandStatus = new CommandFailure(e.what());
585
  }
586
23
}
587
588
23
api::Result QueryCommand::getResult() const { return d_result; }
589
23
void QueryCommand::printResult(std::ostream& out, uint32_t verbosity) const
590
{
591
23
  if (!ok())
592
  {
593
1
    this->Command::printResult(out, verbosity);
594
  }
595
  else
596
  {
597
22
    out << d_result << endl;
598
  }
599
23
}
600
601
Command* QueryCommand::clone() const
602
{
603
  QueryCommand* c = new QueryCommand(d_term);
604
  c->d_result = d_result;
605
  return c;
606
}
607
608
46
std::string QueryCommand::getCommandName() const { return "query"; }
609
610
void QueryCommand::toStream(std::ostream& out,
611
                            int toDepth,
612
                            size_t dag,
613
                            Language language) const
614
{
615
  Printer::getPrinter(language)->toStreamCmdQuery(out, termToNode(d_term));
616
}
617
618
/* -------------------------------------------------------------------------- */
619
/* class DeclareSygusVarCommand */
620
/* -------------------------------------------------------------------------- */
621
622
619
DeclareSygusVarCommand::DeclareSygusVarCommand(const std::string& id,
623
                                               api::Term var,
624
619
                                               api::Sort sort)
625
619
    : DeclarationDefinitionCommand(id), d_var(var), d_sort(sort)
626
{
627
619
}
628
629
api::Term DeclareSygusVarCommand::getVar() const { return d_var; }
630
api::Sort DeclareSygusVarCommand::getSort() const { return d_sort; }
631
632
619
void DeclareSygusVarCommand::invoke(api::Solver* solver, SymbolManager* sm)
633
{
634
619
  d_commandStatus = CommandSuccess::instance();
635
619
}
636
637
Command* DeclareSygusVarCommand::clone() const
638
{
639
  return new DeclareSygusVarCommand(d_symbol, d_var, d_sort);
640
}
641
642
1238
std::string DeclareSygusVarCommand::getCommandName() const
643
{
644
1238
  return "declare-var";
645
}
646
647
1
void DeclareSygusVarCommand::toStream(std::ostream& out,
648
                                      int toDepth,
649
                                      size_t dag,
650
                                      Language language) const
651
{
652
2
  Printer::getPrinter(language)->toStreamCmdDeclareVar(
653
2
      out, termToNode(d_var), sortToTypeNode(d_sort));
654
1
}
655
656
/* -------------------------------------------------------------------------- */
657
/* class SynthFunCommand                                                      */
658
/* -------------------------------------------------------------------------- */
659
660
681
SynthFunCommand::SynthFunCommand(const std::string& id,
661
                                 api::Term fun,
662
                                 const std::vector<api::Term>& vars,
663
                                 api::Sort sort,
664
                                 bool isInv,
665
681
                                 api::Grammar* g)
666
    : DeclarationDefinitionCommand(id),
667
      d_fun(fun),
668
      d_vars(vars),
669
      d_sort(sort),
670
      d_isInv(isInv),
671
681
      d_grammar(g)
672
{
673
681
}
674
675
api::Term SynthFunCommand::getFunction() const { return d_fun; }
676
677
const std::vector<api::Term>& SynthFunCommand::getVars() const
678
{
679
  return d_vars;
680
}
681
682
api::Sort SynthFunCommand::getSort() const { return d_sort; }
683
bool SynthFunCommand::isInv() const { return d_isInv; }
684
685
const api::Grammar* SynthFunCommand::getGrammar() const { return d_grammar; }
686
687
681
void SynthFunCommand::invoke(api::Solver* solver, SymbolManager* sm)
688
{
689
681
  sm->addFunctionToSynthesize(d_fun);
690
681
  d_commandStatus = CommandSuccess::instance();
691
681
}
692
693
Command* SynthFunCommand::clone() const
694
{
695
  return new SynthFunCommand(
696
      d_symbol, d_fun, d_vars, d_sort, d_isInv, d_grammar);
697
}
698
699
1362
std::string SynthFunCommand::getCommandName() const
700
{
701
1362
  return d_isInv ? "synth-inv" : "synth-fun";
702
}
703
704
2
void SynthFunCommand::toStream(std::ostream& out,
705
                               int toDepth,
706
                               size_t dag,
707
                               Language language) const
708
{
709
4
  std::vector<Node> nodeVars = termVectorToNodes(d_vars);
710
6
  Printer::getPrinter(language)->toStreamCmdSynthFun(
711
      out,
712
4
      termToNode(d_fun),
713
      nodeVars,
714
2
      d_isInv,
715
4
      d_grammar == nullptr ? TypeNode::null() : grammarToTypeNode(d_grammar));
716
2
}
717
718
/* -------------------------------------------------------------------------- */
719
/* class SygusConstraintCommand */
720
/* -------------------------------------------------------------------------- */
721
722
1250
SygusConstraintCommand::SygusConstraintCommand(const api::Term& t,
723
1250
                                               bool isAssume)
724
1250
    : d_term(t), d_isAssume(isAssume)
725
{
726
1250
}
727
728
1250
void SygusConstraintCommand::invoke(api::Solver* solver, SymbolManager* sm)
729
{
730
  try
731
  {
732
1250
    if (d_isAssume)
733
    {
734
4
      solver->addSygusAssume(d_term);
735
    }
736
    else
737
    {
738
1246
      solver->addSygusConstraint(d_term);
739
    }
740
1250
    d_commandStatus = CommandSuccess::instance();
741
  }
742
  catch (exception& e)
743
  {
744
    d_commandStatus = new CommandFailure(e.what());
745
  }
746
1250
}
747
748
api::Term SygusConstraintCommand::getTerm() const { return d_term; }
749
750
Command* SygusConstraintCommand::clone() const
751
{
752
  return new SygusConstraintCommand(d_term, d_isAssume);
753
}
754
755
2500
std::string SygusConstraintCommand::getCommandName() const
756
{
757
2500
  return d_isAssume ? "assume" : "constraint";
758
}
759
760
1
void SygusConstraintCommand::toStream(std::ostream& out,
761
                                      int toDepth,
762
                                      size_t dag,
763
                                      Language language) const
764
{
765
1
  if (d_isAssume)
766
  {
767
    Printer::getPrinter(language)->toStreamCmdAssume(out, termToNode(d_term));
768
  }
769
  else
770
  {
771
2
    Printer::getPrinter(language)->toStreamCmdConstraint(out,
772
2
                                                         termToNode(d_term));
773
  }
774
1
}
775
776
/* -------------------------------------------------------------------------- */
777
/* class SygusInvConstraintCommand */
778
/* -------------------------------------------------------------------------- */
779
780
27
SygusInvConstraintCommand::SygusInvConstraintCommand(
781
27
    const std::vector<api::Term>& predicates)
782
27
    : d_predicates(predicates)
783
{
784
27
}
785
786
SygusInvConstraintCommand::SygusInvConstraintCommand(const api::Term& inv,
787
                                                     const api::Term& pre,
788
                                                     const api::Term& trans,
789
                                                     const api::Term& post)
790
    : SygusInvConstraintCommand(std::vector<api::Term>{inv, pre, trans, post})
791
{
792
}
793
794
27
void SygusInvConstraintCommand::invoke(api::Solver* solver, SymbolManager* sm)
795
{
796
  try
797
  {
798
108
    solver->addSygusInvConstraint(
799
108
        d_predicates[0], d_predicates[1], d_predicates[2], d_predicates[3]);
800
27
    d_commandStatus = CommandSuccess::instance();
801
  }
802
  catch (exception& e)
803
  {
804
    d_commandStatus = new CommandFailure(e.what());
805
  }
806
27
}
807
808
const std::vector<api::Term>& SygusInvConstraintCommand::getPredicates() const
809
{
810
  return d_predicates;
811
}
812
813
Command* SygusInvConstraintCommand::clone() const
814
{
815
  return new SygusInvConstraintCommand(d_predicates);
816
}
817
818
54
std::string SygusInvConstraintCommand::getCommandName() const
819
{
820
54
  return "inv-constraint";
821
}
822
823
1
void SygusInvConstraintCommand::toStream(std::ostream& out,
824
                                         int toDepth,
825
                                         size_t dag,
826
                                         Language language) const
827
{
828
2
  Printer::getPrinter(language)->toStreamCmdInvConstraint(
829
      out,
830
2
      termToNode(d_predicates[0]),
831
2
      termToNode(d_predicates[1]),
832
2
      termToNode(d_predicates[2]),
833
2
      termToNode(d_predicates[3]));
834
1
}
835
836
/* -------------------------------------------------------------------------- */
837
/* class CheckSynthCommand                                                    */
838
/* -------------------------------------------------------------------------- */
839
840
378
void CheckSynthCommand::invoke(api::Solver* solver, SymbolManager* sm)
841
{
842
  try
843
  {
844
378
    d_result = solver->checkSynth();
845
364
    d_commandStatus = CommandSuccess::instance();
846
364
    d_solution.clear();
847
    // check whether we should print the status
848
728
    if (!d_result.isUnsat()
849
357
        || options::sygusOut() == options::SygusSolutionOutMode::STATUS_AND_DEF
850
721
        || options::sygusOut() == options::SygusSolutionOutMode::STATUS)
851
    {
852
356
      if (options::sygusOut() == options::SygusSolutionOutMode::STANDARD)
853
      {
854
        d_solution << "fail" << endl;
855
      }
856
      else
857
      {
858
356
        d_solution << d_result << endl;
859
      }
860
    }
861
    // check whether we should print the solution
862
728
    if (d_result.isUnsat()
863
364
        && options::sygusOut() != options::SygusSolutionOutMode::STATUS)
864
    {
865
16
      std::vector<api::Term> synthFuns = sm->getFunctionsToSynthesize();
866
8
      d_solution << "(" << std::endl;
867
8
      Printer* p = Printer::getPrinter(Language::LANG_SYGUS_V2);
868
18
      for (api::Term& f : synthFuns)
869
      {
870
20
        api::Term sol = solver->getSynthSolution(f);
871
20
        std::vector<api::Term> formals;
872
10
        if (sol.getKind() == api::LAMBDA)
873
        {
874
8
          formals.insert(formals.end(), sol[0].begin(), sol[0].end());
875
8
          sol = sol[1];
876
        }
877
20
        api::Sort rangeSort = f.getSort();
878
10
        if (rangeSort.isFunction())
879
        {
880
8
          rangeSort = rangeSort.getFunctionCodomainSort();
881
        }
882
10
        p->toStreamCmdDefineFunction(d_solution,
883
20
                                     f.toString(),
884
20
                                     termVectorToNodes(formals),
885
20
                                     sortToTypeNode(rangeSort),
886
20
                                     termToNode(sol));
887
      }
888
8
      d_solution << ")" << std::endl;
889
    }
890
  }
891
28
  catch (exception& e)
892
  {
893
14
    d_commandStatus = new CommandFailure(e.what());
894
  }
895
378
}
896
897
api::Result CheckSynthCommand::getResult() const { return d_result; }
898
378
void CheckSynthCommand::printResult(std::ostream& out, uint32_t verbosity) const
899
{
900
378
  if (!ok())
901
  {
902
14
    this->Command::printResult(out, verbosity);
903
  }
904
  else
905
  {
906
364
    out << d_solution.str();
907
  }
908
378
}
909
910
Command* CheckSynthCommand::clone() const { return new CheckSynthCommand(); }
911
912
756
std::string CheckSynthCommand::getCommandName() const { return "check-synth"; }
913
914
1
void CheckSynthCommand::toStream(std::ostream& out,
915
                                 int toDepth,
916
                                 size_t dag,
917
                                 Language language) const
918
{
919
1
  Printer::getPrinter(language)->toStreamCmdCheckSynth(out);
920
1
}
921
922
/* -------------------------------------------------------------------------- */
923
/* class ResetCommand                                                         */
924
/* -------------------------------------------------------------------------- */
925
926
43
void ResetCommand::invoke(api::Solver* solver, SymbolManager* sm)
927
{
928
  try
929
  {
930
43
    sm->reset();
931
43
    resetSolver(solver);
932
43
    d_commandStatus = CommandSuccess::instance();
933
  }
934
  catch (exception& e)
935
  {
936
    d_commandStatus = new CommandFailure(e.what());
937
  }
938
43
}
939
940
Command* ResetCommand::clone() const { return new ResetCommand(); }
941
86
std::string ResetCommand::getCommandName() const { return "reset"; }
942
943
void ResetCommand::toStream(std::ostream& out,
944
                            int toDepth,
945
                            size_t dag,
946
                            Language language) const
947
{
948
  Printer::getPrinter(language)->toStreamCmdReset(out);
949
}
950
951
/* -------------------------------------------------------------------------- */
952
/* class ResetAssertionsCommand                                               */
953
/* -------------------------------------------------------------------------- */
954
955
36
void ResetAssertionsCommand::invoke(api::Solver* solver, SymbolManager* sm)
956
{
957
  try
958
  {
959
36
    sm->resetAssertions();
960
36
    solver->resetAssertions();
961
36
    d_commandStatus = CommandSuccess::instance();
962
  }
963
  catch (exception& e)
964
  {
965
    d_commandStatus = new CommandFailure(e.what());
966
  }
967
36
}
968
969
Command* ResetAssertionsCommand::clone() const
970
{
971
  return new ResetAssertionsCommand();
972
}
973
974
72
std::string ResetAssertionsCommand::getCommandName() const
975
{
976
72
  return "reset-assertions";
977
}
978
979
void ResetAssertionsCommand::toStream(std::ostream& out,
980
                                      int toDepth,
981
                                      size_t dag,
982
                                      Language language) const
983
{
984
  Printer::getPrinter(language)->toStreamCmdResetAssertions(out);
985
}
986
987
/* -------------------------------------------------------------------------- */
988
/* class QuitCommand                                                          */
989
/* -------------------------------------------------------------------------- */
990
991
739
void QuitCommand::invoke(api::Solver* solver, SymbolManager* sm)
992
{
993
739
  d_commandStatus = CommandSuccess::instance();
994
739
}
995
996
Command* QuitCommand::clone() const { return new QuitCommand(); }
997
1478
std::string QuitCommand::getCommandName() const { return "exit"; }
998
999
void QuitCommand::toStream(std::ostream& out,
1000
                           int toDepth,
1001
                           size_t dag,
1002
                           Language language) const
1003
{
1004
  Printer::getPrinter(language)->toStreamCmdQuit(out);
1005
}
1006
1007
/* -------------------------------------------------------------------------- */
1008
/* class CommandSequence                                                      */
1009
/* -------------------------------------------------------------------------- */
1010
1011
156
CommandSequence::CommandSequence() : d_index(0) {}
1012
468
CommandSequence::~CommandSequence()
1013
{
1014
521
  for (unsigned i = d_index; i < d_commandSequence.size(); ++i)
1015
  {
1016
365
    delete d_commandSequence[i];
1017
  }
1018
312
}
1019
1020
365
void CommandSequence::addCommand(Command* cmd)
1021
{
1022
365
  d_commandSequence.push_back(cmd);
1023
365
}
1024
1025
void CommandSequence::clear() { d_commandSequence.clear(); }
1026
void CommandSequence::invoke(api::Solver* solver, SymbolManager* sm)
1027
{
1028
  for (; d_index < d_commandSequence.size(); ++d_index)
1029
  {
1030
    d_commandSequence[d_index]->invoke(solver, sm);
1031
    if (!d_commandSequence[d_index]->ok())
1032
    {
1033
      // abort execution
1034
      d_commandStatus = d_commandSequence[d_index]->getCommandStatus();
1035
      return;
1036
    }
1037
    delete d_commandSequence[d_index];
1038
  }
1039
1040
  AlwaysAssert(d_commandStatus == NULL);
1041
  d_commandStatus = CommandSuccess::instance();
1042
}
1043
1044
void CommandSequence::invoke(api::Solver* solver,
1045
                             SymbolManager* sm,
1046
                             std::ostream& out)
1047
{
1048
  for (; d_index < d_commandSequence.size(); ++d_index)
1049
  {
1050
    d_commandSequence[d_index]->invoke(solver, sm, out);
1051
    if (!d_commandSequence[d_index]->ok())
1052
    {
1053
      // abort execution
1054
      d_commandStatus = d_commandSequence[d_index]->getCommandStatus();
1055
      return;
1056
    }
1057
    delete d_commandSequence[d_index];
1058
  }
1059
1060
  AlwaysAssert(d_commandStatus == NULL);
1061
  d_commandStatus = CommandSuccess::instance();
1062
}
1063
1064
Command* CommandSequence::clone() const
1065
{
1066
  CommandSequence* seq = new CommandSequence();
1067
  for (const_iterator i = begin(); i != end(); ++i)
1068
  {
1069
    seq->addCommand((*i)->clone());
1070
  }
1071
  seq->d_index = d_index;
1072
  return seq;
1073
}
1074
1075
CommandSequence::const_iterator CommandSequence::begin() const
1076
{
1077
  return d_commandSequence.begin();
1078
}
1079
1080
CommandSequence::const_iterator CommandSequence::end() const
1081
{
1082
  return d_commandSequence.end();
1083
}
1084
1085
140
CommandSequence::iterator CommandSequence::begin()
1086
{
1087
140
  return d_commandSequence.begin();
1088
}
1089
1090
492
CommandSequence::iterator CommandSequence::end()
1091
{
1092
492
  return d_commandSequence.end();
1093
}
1094
1095
std::string CommandSequence::getCommandName() const { return "sequence"; }
1096
1097
void CommandSequence::toStream(std::ostream& out,
1098
                               int toDepth,
1099
                               size_t dag,
1100
                               Language language) const
1101
{
1102
  Printer::getPrinter(language)->toStreamCmdCommandSequence(out,
1103
                                                            d_commandSequence);
1104
}
1105
1106
/* -------------------------------------------------------------------------- */
1107
/* class DeclarationSequence                                                  */
1108
/* -------------------------------------------------------------------------- */
1109
1110
void DeclarationSequence::toStream(std::ostream& out,
1111
                                   int toDepth,
1112
                                   size_t dag,
1113
                                   Language language) const
1114
{
1115
  Printer::getPrinter(language)->toStreamCmdDeclarationSequence(
1116
      out, d_commandSequence);
1117
}
1118
1119
/* -------------------------------------------------------------------------- */
1120
/* class DeclarationDefinitionCommand                                         */
1121
/* -------------------------------------------------------------------------- */
1122
1123
118826
DeclarationDefinitionCommand::DeclarationDefinitionCommand(
1124
118826
    const std::string& id)
1125
118826
    : d_symbol(id)
1126
{
1127
118826
}
1128
1129
std::string DeclarationDefinitionCommand::getSymbol() const { return d_symbol; }
1130
1131
/* -------------------------------------------------------------------------- */
1132
/* class DeclareFunctionCommand                                               */
1133
/* -------------------------------------------------------------------------- */
1134
1135
105878
DeclareFunctionCommand::DeclareFunctionCommand(const std::string& id,
1136
                                               api::Term func,
1137
105878
                                               api::Sort sort)
1138
105878
    : DeclarationDefinitionCommand(id), d_func(func), d_sort(sort)
1139
{
1140
105878
}
1141
1142
api::Term DeclareFunctionCommand::getFunction() const { return d_func; }
1143
api::Sort DeclareFunctionCommand::getSort() const { return d_sort; }
1144
1145
105810
void DeclareFunctionCommand::invoke(api::Solver* solver, SymbolManager* sm)
1146
{
1147
  // mark that it will be printed in the model
1148
105810
  sm->addModelDeclarationTerm(d_func);
1149
105810
  d_commandStatus = CommandSuccess::instance();
1150
105810
}
1151
1152
Command* DeclareFunctionCommand::clone() const
1153
{
1154
  DeclareFunctionCommand* dfc =
1155
      new DeclareFunctionCommand(d_symbol, d_func, d_sort);
1156
  return dfc;
1157
}
1158
1159
211620
std::string DeclareFunctionCommand::getCommandName() const
1160
{
1161
211620
  return "declare-fun";
1162
}
1163
1164
7
void DeclareFunctionCommand::toStream(std::ostream& out,
1165
                                      int toDepth,
1166
                                      size_t dag,
1167
                                      Language language) const
1168
{
1169
14
  Printer::getPrinter(language)->toStreamCmdDeclareFunction(
1170
14
      out, d_symbol, sortToTypeNode(d_func.getSort()));
1171
7
}
1172
1173
/* -------------------------------------------------------------------------- */
1174
/* class DeclareFunctionCommand                                               */
1175
/* -------------------------------------------------------------------------- */
1176
1177
3
DeclarePoolCommand::DeclarePoolCommand(const std::string& id,
1178
                                       api::Term func,
1179
                                       api::Sort sort,
1180
3
                                       const std::vector<api::Term>& initValue)
1181
    : DeclarationDefinitionCommand(id),
1182
      d_func(func),
1183
      d_sort(sort),
1184
3
      d_initValue(initValue)
1185
{
1186
3
}
1187
1188
api::Term DeclarePoolCommand::getFunction() const { return d_func; }
1189
api::Sort DeclarePoolCommand::getSort() const { return d_sort; }
1190
const std::vector<api::Term>& DeclarePoolCommand::getInitialValue() const
1191
{
1192
  return d_initValue;
1193
}
1194
1195
3
void DeclarePoolCommand::invoke(api::Solver* solver, SymbolManager* sm)
1196
{
1197
  // Notice that the pool is already declared by the parser so that it the
1198
  // symbol is bound eagerly. This is analogous to DeclareSygusVarCommand.
1199
  // Hence, we do nothing here.
1200
3
  d_commandStatus = CommandSuccess::instance();
1201
3
}
1202
1203
Command* DeclarePoolCommand::clone() const
1204
{
1205
  DeclarePoolCommand* dfc =
1206
      new DeclarePoolCommand(d_symbol, d_func, d_sort, d_initValue);
1207
  return dfc;
1208
}
1209
1210
6
std::string DeclarePoolCommand::getCommandName() const
1211
{
1212
6
  return "declare-pool";
1213
}
1214
1215
void DeclarePoolCommand::toStream(std::ostream& out,
1216
                                  int toDepth,
1217
                                  size_t dag,
1218
                                  Language language) const
1219
{
1220
  Printer::getPrinter(language)->toStreamCmdDeclarePool(
1221
      out,
1222
      d_func.toString(),
1223
      sortToTypeNode(d_sort),
1224
      termVectorToNodes(d_initValue));
1225
}
1226
1227
/* -------------------------------------------------------------------------- */
1228
/* class DeclareSortCommand                                                   */
1229
/* -------------------------------------------------------------------------- */
1230
1231
3958
DeclareSortCommand::DeclareSortCommand(const std::string& id,
1232
                                       size_t arity,
1233
3958
                                       api::Sort sort)
1234
3958
    : DeclarationDefinitionCommand(id), d_arity(arity), d_sort(sort)
1235
{
1236
3958
}
1237
1238
size_t DeclareSortCommand::getArity() const { return d_arity; }
1239
api::Sort DeclareSortCommand::getSort() const { return d_sort; }
1240
3940
void DeclareSortCommand::invoke(api::Solver* solver, SymbolManager* sm)
1241
{
1242
  // mark that it will be printed in the model
1243
3940
  sm->addModelDeclarationSort(d_sort);
1244
3940
  d_commandStatus = CommandSuccess::instance();
1245
3940
}
1246
1247
Command* DeclareSortCommand::clone() const
1248
{
1249
  return new DeclareSortCommand(d_symbol, d_arity, d_sort);
1250
}
1251
1252
7880
std::string DeclareSortCommand::getCommandName() const
1253
{
1254
7880
  return "declare-sort";
1255
}
1256
1257
void DeclareSortCommand::toStream(std::ostream& out,
1258
                                  int toDepth,
1259
                                  size_t dag,
1260
                                  Language language) const
1261
{
1262
  Printer::getPrinter(language)->toStreamCmdDeclareType(out,
1263
                                                        sortToTypeNode(d_sort));
1264
}
1265
1266
/* -------------------------------------------------------------------------- */
1267
/* class DefineSortCommand                                                    */
1268
/* -------------------------------------------------------------------------- */
1269
1270
DefineSortCommand::DefineSortCommand(const std::string& id, api::Sort sort)
1271
    : DeclarationDefinitionCommand(id), d_params(), d_sort(sort)
1272
{
1273
}
1274
1275
217
DefineSortCommand::DefineSortCommand(const std::string& id,
1276
                                     const std::vector<api::Sort>& params,
1277
217
                                     api::Sort sort)
1278
217
    : DeclarationDefinitionCommand(id), d_params(params), d_sort(sort)
1279
{
1280
217
}
1281
1282
const std::vector<api::Sort>& DefineSortCommand::getParameters() const
1283
{
1284
  return d_params;
1285
}
1286
1287
api::Sort DefineSortCommand::getSort() const { return d_sort; }
1288
217
void DefineSortCommand::invoke(api::Solver* solver, SymbolManager* sm)
1289
{
1290
217
  d_commandStatus = CommandSuccess::instance();
1291
217
}
1292
1293
Command* DefineSortCommand::clone() const
1294
{
1295
  return new DefineSortCommand(d_symbol, d_params, d_sort);
1296
}
1297
1298
434
std::string DefineSortCommand::getCommandName() const { return "define-sort"; }
1299
1300
void DefineSortCommand::toStream(std::ostream& out,
1301
                                 int toDepth,
1302
                                 size_t dag,
1303
                                 Language language) const
1304
{
1305
  Printer::getPrinter(language)->toStreamCmdDefineType(
1306
      out, d_symbol, sortVectorToTypeNodes(d_params), sortToTypeNode(d_sort));
1307
}
1308
1309
/* -------------------------------------------------------------------------- */
1310
/* class DefineFunctionCommand                                                */
1311
/* -------------------------------------------------------------------------- */
1312
1313
4935
DefineFunctionCommand::DefineFunctionCommand(const std::string& id,
1314
                                             api::Sort sort,
1315
4935
                                             api::Term formula)
1316
    : DeclarationDefinitionCommand(id),
1317
      d_formals(),
1318
      d_sort(sort),
1319
4935
      d_formula(formula)
1320
{
1321
4935
}
1322
1323
2535
DefineFunctionCommand::DefineFunctionCommand(
1324
    const std::string& id,
1325
    const std::vector<api::Term>& formals,
1326
    api::Sort sort,
1327
2535
    api::Term formula)
1328
    : DeclarationDefinitionCommand(id),
1329
      d_formals(formals),
1330
      d_sort(sort),
1331
2535
      d_formula(formula)
1332
{
1333
2535
}
1334
1335
const std::vector<api::Term>& DefineFunctionCommand::getFormals() const
1336
{
1337
  return d_formals;
1338
}
1339
1340
api::Sort DefineFunctionCommand::getSort() const { return d_sort; }
1341
1342
api::Term DefineFunctionCommand::getFormula() const { return d_formula; }
1343
1344
7469
void DefineFunctionCommand::invoke(api::Solver* solver, SymbolManager* sm)
1345
{
1346
  try
1347
  {
1348
7469
    bool global = sm->getGlobalDeclarations();
1349
    api::Term fun =
1350
14938
        solver->defineFun(d_symbol, d_formals, d_sort, d_formula, global);
1351
7469
    sm->getSymbolTable()->bind(fun.toString(), fun, global);
1352
7469
    d_commandStatus = CommandSuccess::instance();
1353
  }
1354
  catch (exception& e)
1355
  {
1356
    d_commandStatus = new CommandFailure(e.what());
1357
  }
1358
7469
}
1359
1360
Command* DefineFunctionCommand::clone() const
1361
{
1362
  return new DefineFunctionCommand(d_symbol, d_formals, d_sort, d_formula);
1363
}
1364
1365
14938
std::string DefineFunctionCommand::getCommandName() const
1366
{
1367
14938
  return "define-fun";
1368
}
1369
1370
3
void DefineFunctionCommand::toStream(std::ostream& out,
1371
                                     int toDepth,
1372
                                     size_t dag,
1373
                                     Language language) const
1374
{
1375
6
  Printer::getPrinter(language)->toStreamCmdDefineFunction(
1376
      out,
1377
      d_symbol,
1378
6
      termVectorToNodes(d_formals),
1379
6
      sortToTypeNode(d_sort),
1380
6
      termToNode(d_formula));
1381
3
}
1382
1383
/* -------------------------------------------------------------------------- */
1384
/* class DefineFunctionRecCommand                                             */
1385
/* -------------------------------------------------------------------------- */
1386
1387
89
DefineFunctionRecCommand::DefineFunctionRecCommand(
1388
89
    api::Term func, const std::vector<api::Term>& formals, api::Term formula)
1389
{
1390
89
  d_funcs.push_back(func);
1391
89
  d_formals.push_back(formals);
1392
89
  d_formulas.push_back(formula);
1393
89
}
1394
1395
37
DefineFunctionRecCommand::DefineFunctionRecCommand(
1396
    const std::vector<api::Term>& funcs,
1397
    const std::vector<std::vector<api::Term>>& formals,
1398
37
    const std::vector<api::Term>& formulas)
1399
37
    : d_funcs(funcs), d_formals(formals), d_formulas(formulas)
1400
{
1401
37
}
1402
1403
const std::vector<api::Term>& DefineFunctionRecCommand::getFunctions() const
1404
{
1405
  return d_funcs;
1406
}
1407
1408
const std::vector<std::vector<api::Term>>&
1409
DefineFunctionRecCommand::getFormals() const
1410
{
1411
  return d_formals;
1412
}
1413
1414
const std::vector<api::Term>& DefineFunctionRecCommand::getFormulas() const
1415
{
1416
  return d_formulas;
1417
}
1418
1419
126
void DefineFunctionRecCommand::invoke(api::Solver* solver, SymbolManager* sm)
1420
{
1421
  try
1422
  {
1423
126
    bool global = sm->getGlobalDeclarations();
1424
126
    solver->defineFunsRec(d_funcs, d_formals, d_formulas, global);
1425
123
    d_commandStatus = CommandSuccess::instance();
1426
  }
1427
6
  catch (exception& e)
1428
  {
1429
3
    d_commandStatus = new CommandFailure(e.what());
1430
  }
1431
126
}
1432
1433
Command* DefineFunctionRecCommand::clone() const
1434
{
1435
  return new DefineFunctionRecCommand(d_funcs, d_formals, d_formulas);
1436
}
1437
1438
252
std::string DefineFunctionRecCommand::getCommandName() const
1439
{
1440
252
  return "define-fun-rec";
1441
}
1442
1443
void DefineFunctionRecCommand::toStream(std::ostream& out,
1444
                                        int toDepth,
1445
                                        size_t dag,
1446
                                        Language language) const
1447
{
1448
  std::vector<std::vector<Node>> formals;
1449
  formals.reserve(d_formals.size());
1450
  for (const std::vector<api::Term>& formal : d_formals)
1451
  {
1452
    formals.push_back(termVectorToNodes(formal));
1453
  }
1454
1455
  Printer::getPrinter(language)->toStreamCmdDefineFunctionRec(
1456
      out, termVectorToNodes(d_funcs), formals, termVectorToNodes(d_formulas));
1457
}
1458
/* -------------------------------------------------------------------------- */
1459
/* class DeclareHeapCommand                                                   */
1460
/* -------------------------------------------------------------------------- */
1461
86
DeclareHeapCommand::DeclareHeapCommand(api::Sort locSort, api::Sort dataSort)
1462
86
    : d_locSort(locSort), d_dataSort(dataSort)
1463
{
1464
86
}
1465
1466
api::Sort DeclareHeapCommand::getLocationSort() const { return d_locSort; }
1467
api::Sort DeclareHeapCommand::getDataSort() const { return d_dataSort; }
1468
1469
86
void DeclareHeapCommand::invoke(api::Solver* solver, SymbolManager* sm)
1470
{
1471
86
  solver->declareSepHeap(d_locSort, d_dataSort);
1472
86
}
1473
1474
Command* DeclareHeapCommand::clone() const
1475
{
1476
  return new DeclareHeapCommand(d_locSort, d_dataSort);
1477
}
1478
1479
172
std::string DeclareHeapCommand::getCommandName() const
1480
{
1481
172
  return "declare-heap";
1482
}
1483
1484
void DeclareHeapCommand::toStream(std::ostream& out,
1485
                                  int toDepth,
1486
                                  size_t dag,
1487
                                  Language language) const
1488
{
1489
  Printer::getPrinter(language)->toStreamCmdDeclareHeap(
1490
      out, sortToTypeNode(d_locSort), sortToTypeNode(d_dataSort));
1491
}
1492
1493
/* -------------------------------------------------------------------------- */
1494
/* class SimplifyCommand                                                      */
1495
/* -------------------------------------------------------------------------- */
1496
1497
SimplifyCommand::SimplifyCommand(api::Term term) : d_term(term) {}
1498
api::Term SimplifyCommand::getTerm() const { return d_term; }
1499
void SimplifyCommand::invoke(api::Solver* solver, SymbolManager* sm)
1500
{
1501
  try
1502
  {
1503
    d_result = solver->simplify(d_term);
1504
    d_commandStatus = CommandSuccess::instance();
1505
  }
1506
  catch (UnsafeInterruptException& e)
1507
  {
1508
    d_commandStatus = new CommandInterrupted();
1509
  }
1510
  catch (exception& e)
1511
  {
1512
    d_commandStatus = new CommandFailure(e.what());
1513
  }
1514
}
1515
1516
api::Term SimplifyCommand::getResult() const { return d_result; }
1517
void SimplifyCommand::printResult(std::ostream& out, uint32_t verbosity) const
1518
{
1519
  if (!ok())
1520
  {
1521
    this->Command::printResult(out, verbosity);
1522
  }
1523
  else
1524
  {
1525
    out << d_result << endl;
1526
  }
1527
}
1528
1529
Command* SimplifyCommand::clone() const
1530
{
1531
  SimplifyCommand* c = new SimplifyCommand(d_term);
1532
  c->d_result = d_result;
1533
  return c;
1534
}
1535
1536
std::string SimplifyCommand::getCommandName() const { return "simplify"; }
1537
1538
void SimplifyCommand::toStream(std::ostream& out,
1539
                               int toDepth,
1540
                               size_t dag,
1541
                               Language language) const
1542
{
1543
  Printer::getPrinter(language)->toStreamCmdSimplify(out, termToNode(d_term));
1544
}
1545
1546
/* -------------------------------------------------------------------------- */
1547
/* class GetValueCommand                                                      */
1548
/* -------------------------------------------------------------------------- */
1549
1550
GetValueCommand::GetValueCommand(api::Term term) : d_terms()
1551
{
1552
  d_terms.push_back(term);
1553
}
1554
1555
68
GetValueCommand::GetValueCommand(const std::vector<api::Term>& terms)
1556
68
    : d_terms(terms)
1557
{
1558
68
  PrettyCheckArgument(
1559
      terms.size() >= 1, terms, "cannot get-value of an empty set of terms");
1560
68
}
1561
1562
const std::vector<api::Term>& GetValueCommand::getTerms() const
1563
{
1564
  return d_terms;
1565
}
1566
68
void GetValueCommand::invoke(api::Solver* solver, SymbolManager* sm)
1567
{
1568
  try
1569
  {
1570
132
    std::vector<api::Term> result = solver->getValue(d_terms);
1571
64
    Assert(result.size() == d_terms.size());
1572
156
    for (int i = 0, size = d_terms.size(); i < size; i++)
1573
    {
1574
184
      api::Term request = d_terms[i];
1575
184
      api::Term value = result[i];
1576
92
      result[i] = solver->mkTerm(api::SEXPR, request, value);
1577
    }
1578
64
    d_result = solver->mkTerm(api::SEXPR, result);
1579
64
    d_commandStatus = CommandSuccess::instance();
1580
  }
1581
8
  catch (api::CVC5ApiRecoverableException& e)
1582
  {
1583
4
    d_commandStatus = new CommandRecoverableFailure(e.what());
1584
  }
1585
  catch (UnsafeInterruptException& e)
1586
  {
1587
    d_commandStatus = new CommandInterrupted();
1588
  }
1589
  catch (exception& e)
1590
  {
1591
    d_commandStatus = new CommandFailure(e.what());
1592
  }
1593
68
}
1594
1595
api::Term GetValueCommand::getResult() const { return d_result; }
1596
68
void GetValueCommand::printResult(std::ostream& out, uint32_t verbosity) const
1597
{
1598
68
  if (!ok())
1599
  {
1600
4
    this->Command::printResult(out, verbosity);
1601
  }
1602
  else
1603
  {
1604
128
    expr::ExprDag::Scope scope(out, false);
1605
64
    out << d_result << endl;
1606
  }
1607
68
}
1608
1609
Command* GetValueCommand::clone() const
1610
{
1611
  GetValueCommand* c = new GetValueCommand(d_terms);
1612
  c->d_result = d_result;
1613
  return c;
1614
}
1615
1616
136
std::string GetValueCommand::getCommandName() const { return "get-value"; }
1617
1618
void GetValueCommand::toStream(std::ostream& out,
1619
                               int toDepth,
1620
                               size_t dag,
1621
                               Language language) const
1622
{
1623
  Printer::getPrinter(language)->toStreamCmdGetValue(
1624
      out, termVectorToNodes(d_terms));
1625
}
1626
1627
/* -------------------------------------------------------------------------- */
1628
/* class GetAssignmentCommand                                                 */
1629
/* -------------------------------------------------------------------------- */
1630
1631
10
GetAssignmentCommand::GetAssignmentCommand() {}
1632
10
void GetAssignmentCommand::invoke(api::Solver* solver, SymbolManager* sm)
1633
{
1634
  try
1635
  {
1636
20
    std::map<api::Term, std::string> enames = sm->getExpressionNames();
1637
20
    std::vector<api::Term> terms;
1638
20
    std::vector<std::string> names;
1639
22
    for (const std::pair<const api::Term, std::string>& e : enames)
1640
    {
1641
12
      terms.push_back(e.first);
1642
12
      names.push_back(e.second);
1643
    }
1644
    // Must use vector version of getValue to ensure error is thrown regardless
1645
    // of whether terms is empty.
1646
18
    std::vector<api::Term> values = solver->getValue(terms);
1647
8
    Assert(values.size() == names.size());
1648
16
    std::vector<api::Term> sexprs;
1649
20
    for (size_t i = 0, nterms = terms.size(); i < nterms; i++)
1650
    {
1651
      // Treat the expression name as a variable name as opposed to a string
1652
      // constant to avoid printing double quotes around the name.
1653
24
      api::Term name = solver->mkVar(solver->getBooleanSort(), names[i]);
1654
12
      sexprs.push_back(solver->mkTerm(api::SEXPR, name, values[i]));
1655
    }
1656
8
    d_result = solver->mkTerm(api::SEXPR, sexprs);
1657
8
    d_commandStatus = CommandSuccess::instance();
1658
  }
1659
4
  catch (api::CVC5ApiRecoverableException& e)
1660
  {
1661
2
    d_commandStatus = new CommandRecoverableFailure(e.what());
1662
  }
1663
  catch (UnsafeInterruptException& e)
1664
  {
1665
    d_commandStatus = new CommandInterrupted();
1666
  }
1667
  catch (exception& e)
1668
  {
1669
    d_commandStatus = new CommandFailure(e.what());
1670
  }
1671
10
}
1672
1673
api::Term GetAssignmentCommand::getResult() const { return d_result; }
1674
10
void GetAssignmentCommand::printResult(std::ostream& out,
1675
                                       uint32_t verbosity) const
1676
{
1677
10
  if (!ok())
1678
  {
1679
2
    this->Command::printResult(out, verbosity);
1680
  }
1681
  else
1682
  {
1683
8
    out << d_result << endl;
1684
  }
1685
10
}
1686
1687
Command* GetAssignmentCommand::clone() const
1688
{
1689
  GetAssignmentCommand* c = new GetAssignmentCommand();
1690
  c->d_result = d_result;
1691
  return c;
1692
}
1693
1694
20
std::string GetAssignmentCommand::getCommandName() const
1695
{
1696
20
  return "get-assignment";
1697
}
1698
1699
void GetAssignmentCommand::toStream(std::ostream& out,
1700
                                    int toDepth,
1701
                                    size_t dag,
1702
                                    Language language) const
1703
{
1704
  Printer::getPrinter(language)->toStreamCmdGetAssignment(out);
1705
}
1706
1707
/* -------------------------------------------------------------------------- */
1708
/* class GetModelCommand                                                      */
1709
/* -------------------------------------------------------------------------- */
1710
1711
30
GetModelCommand::GetModelCommand() {}
1712
30
void GetModelCommand::invoke(api::Solver* solver, SymbolManager* sm)
1713
{
1714
  try
1715
  {
1716
60
    std::vector<api::Sort> declareSorts = sm->getModelDeclareSorts();
1717
60
    std::vector<api::Term> declareTerms = sm->getModelDeclareTerms();
1718
30
    d_result = solver->getModel(declareSorts, declareTerms);
1719
25
    d_commandStatus = CommandSuccess::instance();
1720
  }
1721
10
  catch (api::CVC5ApiRecoverableException& e)
1722
  {
1723
5
    d_commandStatus = new CommandRecoverableFailure(e.what());
1724
  }
1725
  catch (UnsafeInterruptException& e)
1726
  {
1727
    d_commandStatus = new CommandInterrupted();
1728
  }
1729
  catch (exception& e)
1730
  {
1731
    d_commandStatus = new CommandFailure(e.what());
1732
  }
1733
30
}
1734
1735
30
void GetModelCommand::printResult(std::ostream& out, uint32_t verbosity) const
1736
{
1737
30
  if (!ok())
1738
  {
1739
5
    this->Command::printResult(out, verbosity);
1740
  }
1741
  else
1742
  {
1743
25
    out << d_result;
1744
  }
1745
30
}
1746
1747
Command* GetModelCommand::clone() const
1748
{
1749
  GetModelCommand* c = new GetModelCommand;
1750
  c->d_result = d_result;
1751
  return c;
1752
}
1753
1754
60
std::string GetModelCommand::getCommandName() const { return "get-model"; }
1755
1756
void GetModelCommand::toStream(std::ostream& out,
1757
                               int toDepth,
1758
                               size_t dag,
1759
                               Language language) const
1760
{
1761
  Printer::getPrinter(language)->toStreamCmdGetModel(out);
1762
}
1763
1764
/* -------------------------------------------------------------------------- */
1765
/* class BlockModelCommand */
1766
/* -------------------------------------------------------------------------- */
1767
1768
14
BlockModelCommand::BlockModelCommand() {}
1769
14
void BlockModelCommand::invoke(api::Solver* solver, SymbolManager* sm)
1770
{
1771
  try
1772
  {
1773
14
    solver->blockModel();
1774
14
    d_commandStatus = CommandSuccess::instance();
1775
  }
1776
  catch (api::CVC5ApiRecoverableException& e)
1777
  {
1778
    d_commandStatus = new CommandRecoverableFailure(e.what());
1779
  }
1780
  catch (UnsafeInterruptException& e)
1781
  {
1782
    d_commandStatus = new CommandInterrupted();
1783
  }
1784
  catch (exception& e)
1785
  {
1786
    d_commandStatus = new CommandFailure(e.what());
1787
  }
1788
14
}
1789
1790
Command* BlockModelCommand::clone() const
1791
{
1792
  BlockModelCommand* c = new BlockModelCommand();
1793
  return c;
1794
}
1795
1796
28
std::string BlockModelCommand::getCommandName() const { return "block-model"; }
1797
1798
void BlockModelCommand::toStream(std::ostream& out,
1799
                                 int toDepth,
1800
                                 size_t dag,
1801
                                 Language language) const
1802
{
1803
  Printer::getPrinter(language)->toStreamCmdBlockModel(out);
1804
}
1805
1806
/* -------------------------------------------------------------------------- */
1807
/* class BlockModelValuesCommand */
1808
/* -------------------------------------------------------------------------- */
1809
1810
8
BlockModelValuesCommand::BlockModelValuesCommand(
1811
8
    const std::vector<api::Term>& terms)
1812
8
    : d_terms(terms)
1813
{
1814
8
  PrettyCheckArgument(terms.size() >= 1,
1815
                      terms,
1816
                      "cannot block-model-values of an empty set of terms");
1817
8
}
1818
1819
const std::vector<api::Term>& BlockModelValuesCommand::getTerms() const
1820
{
1821
  return d_terms;
1822
}
1823
8
void BlockModelValuesCommand::invoke(api::Solver* solver, SymbolManager* sm)
1824
{
1825
  try
1826
  {
1827
8
    solver->blockModelValues(d_terms);
1828
8
    d_commandStatus = CommandSuccess::instance();
1829
  }
1830
  catch (api::CVC5ApiRecoverableException& e)
1831
  {
1832
    d_commandStatus = new CommandRecoverableFailure(e.what());
1833
  }
1834
  catch (UnsafeInterruptException& e)
1835
  {
1836
    d_commandStatus = new CommandInterrupted();
1837
  }
1838
  catch (exception& e)
1839
  {
1840
    d_commandStatus = new CommandFailure(e.what());
1841
  }
1842
8
}
1843
1844
Command* BlockModelValuesCommand::clone() const
1845
{
1846
  BlockModelValuesCommand* c = new BlockModelValuesCommand(d_terms);
1847
  return c;
1848
}
1849
1850
16
std::string BlockModelValuesCommand::getCommandName() const
1851
{
1852
16
  return "block-model-values";
1853
}
1854
1855
void BlockModelValuesCommand::toStream(std::ostream& out,
1856
                                       int toDepth,
1857
                                       size_t dag,
1858
                                       Language language) const
1859
{
1860
  Printer::getPrinter(language)->toStreamCmdBlockModelValues(
1861
      out, termVectorToNodes(d_terms));
1862
}
1863
1864
/* -------------------------------------------------------------------------- */
1865
/* class GetProofCommand                                                      */
1866
/* -------------------------------------------------------------------------- */
1867
1868
2
GetProofCommand::GetProofCommand() {}
1869
2
void GetProofCommand::invoke(api::Solver* solver, SymbolManager* sm)
1870
{
1871
  try
1872
  {
1873
2
    d_result = solver->getProof();
1874
2
    d_commandStatus = CommandSuccess::instance();
1875
  }
1876
  catch (api::CVC5ApiRecoverableException& e)
1877
  {
1878
    d_commandStatus = new CommandRecoverableFailure(e.what());
1879
  }
1880
  catch (exception& e)
1881
  {
1882
    d_commandStatus = new CommandFailure(e.what());
1883
  }
1884
2
}
1885
1886
2
void GetProofCommand::printResult(std::ostream& out, uint32_t verbosity) const
1887
{
1888
2
  if (ok())
1889
  {
1890
2
    out << d_result;
1891
  }
1892
  else
1893
  {
1894
    this->Command::printResult(out, verbosity);
1895
  }
1896
2
}
1897
1898
Command* GetProofCommand::clone() const
1899
{
1900
  GetProofCommand* c = new GetProofCommand();
1901
  return c;
1902
}
1903
1904
4
std::string GetProofCommand::getCommandName() const { return "get-proof"; }
1905
1906
void GetProofCommand::toStream(std::ostream& out,
1907
                               int toDepth,
1908
                               size_t dag,
1909
                               Language language) const
1910
{
1911
  Printer::getPrinter(language)->toStreamCmdGetProof(out);
1912
}
1913
1914
/* -------------------------------------------------------------------------- */
1915
/* class GetInstantiationsCommand                                             */
1916
/* -------------------------------------------------------------------------- */
1917
1918
15
GetInstantiationsCommand::GetInstantiationsCommand() : d_solver(nullptr) {}
1919
147
bool GetInstantiationsCommand::isEnabled(api::Solver* solver,
1920
                                         const api::Result& res)
1921
{
1922
147
  return (res.isSat()
1923
147
          || (res.isSatUnknown()
1924
              && res.getUnknownExplanation() == api::Result::INCOMPLETE))
1925
294
         || res.isUnsat() || res.isEntailed();
1926
}
1927
15
void GetInstantiationsCommand::invoke(api::Solver* solver, SymbolManager* sm)
1928
{
1929
  try
1930
  {
1931
15
    d_solver = solver;
1932
15
    d_commandStatus = CommandSuccess::instance();
1933
  }
1934
  catch (exception& e)
1935
  {
1936
    d_commandStatus = new CommandFailure(e.what());
1937
  }
1938
15
}
1939
1940
15
void GetInstantiationsCommand::printResult(std::ostream& out,
1941
                                           uint32_t verbosity) const
1942
{
1943
15
  if (!ok())
1944
  {
1945
    this->Command::printResult(out, verbosity);
1946
  }
1947
  else
1948
  {
1949
15
    d_solver->printInstantiations(out);
1950
  }
1951
15
}
1952
1953
Command* GetInstantiationsCommand::clone() const
1954
{
1955
  GetInstantiationsCommand* c = new GetInstantiationsCommand();
1956
  // c->d_result = d_result;
1957
  c->d_solver = d_solver;
1958
  return c;
1959
}
1960
1961
30
std::string GetInstantiationsCommand::getCommandName() const
1962
{
1963
30
  return "get-instantiations";
1964
}
1965
1966
void GetInstantiationsCommand::toStream(std::ostream& out,
1967
                                        int toDepth,
1968
                                        size_t dag,
1969
                                        Language language) const
1970
{
1971
  Printer::getPrinter(language)->toStreamCmdGetInstantiations(out);
1972
}
1973
1974
/* -------------------------------------------------------------------------- */
1975
/* class GetInterpolCommand                                                   */
1976
/* -------------------------------------------------------------------------- */
1977
1978
GetInterpolCommand::GetInterpolCommand(const std::string& name, api::Term conj)
1979
    : d_name(name), d_conj(conj), d_resultStatus(false)
1980
{
1981
}
1982
8
GetInterpolCommand::GetInterpolCommand(const std::string& name,
1983
                                       api::Term conj,
1984
8
                                       api::Grammar* g)
1985
8
    : d_name(name), d_conj(conj), d_sygus_grammar(g), d_resultStatus(false)
1986
{
1987
8
}
1988
1989
api::Term GetInterpolCommand::getConjecture() const { return d_conj; }
1990
1991
const api::Grammar* GetInterpolCommand::getGrammar() const
1992
{
1993
  return d_sygus_grammar;
1994
}
1995
1996
api::Term GetInterpolCommand::getResult() const { return d_result; }
1997
1998
8
void GetInterpolCommand::invoke(api::Solver* solver, SymbolManager* sm)
1999
{
2000
  try
2001
  {
2002
8
    if (d_sygus_grammar == nullptr)
2003
    {
2004
7
      d_resultStatus = solver->getInterpolant(d_conj, d_result);
2005
    }
2006
    else
2007
    {
2008
1
      d_resultStatus =
2009
1
          solver->getInterpolant(d_conj, *d_sygus_grammar, d_result);
2010
    }
2011
8
    d_commandStatus = CommandSuccess::instance();
2012
  }
2013
  catch (exception& e)
2014
  {
2015
    d_commandStatus = new CommandFailure(e.what());
2016
  }
2017
8
}
2018
2019
8
void GetInterpolCommand::printResult(std::ostream& out,
2020
                                     uint32_t verbosity) const
2021
{
2022
8
  if (!ok())
2023
  {
2024
    this->Command::printResult(out, verbosity);
2025
  }
2026
  else
2027
  {
2028
16
    expr::ExprDag::Scope scope(out, false);
2029
8
    if (d_resultStatus)
2030
    {
2031
8
      out << "(define-fun " << d_name << " () Bool " << d_result << ")"
2032
8
          << std::endl;
2033
    }
2034
    else
2035
    {
2036
      out << "none" << std::endl;
2037
    }
2038
  }
2039
8
}
2040
2041
Command* GetInterpolCommand::clone() const
2042
{
2043
  GetInterpolCommand* c =
2044
      new GetInterpolCommand(d_name, d_conj, d_sygus_grammar);
2045
  c->d_result = d_result;
2046
  c->d_resultStatus = d_resultStatus;
2047
  return c;
2048
}
2049
2050
16
std::string GetInterpolCommand::getCommandName() const
2051
{
2052
16
  return "get-interpol";
2053
}
2054
2055
void GetInterpolCommand::toStream(std::ostream& out,
2056
                                  int toDepth,
2057
                                  size_t dag,
2058
                                  Language language) const
2059
{
2060
  Printer::getPrinter(language)->toStreamCmdGetInterpol(
2061
      out, d_name, termToNode(d_conj), grammarToTypeNode(d_sygus_grammar));
2062
}
2063
2064
/* -------------------------------------------------------------------------- */
2065
/* class GetAbductCommand                                                     */
2066
/* -------------------------------------------------------------------------- */
2067
2068
GetAbductCommand::GetAbductCommand(const std::string& name, api::Term conj)
2069
    : d_name(name), d_conj(conj), d_resultStatus(false)
2070
{
2071
}
2072
38
GetAbductCommand::GetAbductCommand(const std::string& name,
2073
                                   api::Term conj,
2074
38
                                   api::Grammar* g)
2075
38
    : d_name(name), d_conj(conj), d_sygus_grammar(g), d_resultStatus(false)
2076
{
2077
38
}
2078
2079
api::Term GetAbductCommand::getConjecture() const { return d_conj; }
2080
2081
const api::Grammar* GetAbductCommand::getGrammar() const
2082
{
2083
  return d_sygus_grammar;
2084
}
2085
2086
std::string GetAbductCommand::getAbductName() const { return d_name; }
2087
api::Term GetAbductCommand::getResult() const { return d_result; }
2088
2089
38
void GetAbductCommand::invoke(api::Solver* solver, SymbolManager* sm)
2090
{
2091
  try
2092
  {
2093
38
    if (d_sygus_grammar == nullptr)
2094
    {
2095
30
      d_resultStatus = solver->getAbduct(d_conj, d_result);
2096
    }
2097
    else
2098
    {
2099
8
      d_resultStatus = solver->getAbduct(d_conj, *d_sygus_grammar, d_result);
2100
    }
2101
34
    d_commandStatus = CommandSuccess::instance();
2102
  }
2103
8
  catch (exception& e)
2104
  {
2105
4
    d_commandStatus = new CommandFailure(e.what());
2106
  }
2107
38
}
2108
2109
38
void GetAbductCommand::printResult(std::ostream& out, uint32_t verbosity) const
2110
{
2111
38
  if (!ok())
2112
  {
2113
4
    this->Command::printResult(out, verbosity);
2114
  }
2115
  else
2116
  {
2117
68
    expr::ExprDag::Scope scope(out, false);
2118
34
    if (d_resultStatus)
2119
    {
2120
30
      out << "(define-fun " << d_name << " () Bool " << d_result << ")"
2121
30
          << std::endl;
2122
    }
2123
    else
2124
    {
2125
4
      out << "none" << std::endl;
2126
    }
2127
  }
2128
38
}
2129
2130
Command* GetAbductCommand::clone() const
2131
{
2132
  GetAbductCommand* c = new GetAbductCommand(d_name, d_conj, d_sygus_grammar);
2133
  c->d_result = d_result;
2134
  c->d_resultStatus = d_resultStatus;
2135
  return c;
2136
}
2137
2138
76
std::string GetAbductCommand::getCommandName() const { return "get-abduct"; }
2139
2140
void GetAbductCommand::toStream(std::ostream& out,
2141
                                int toDepth,
2142
                                size_t dag,
2143
                                Language language) const
2144
{
2145
  Printer::getPrinter(language)->toStreamCmdGetAbduct(
2146
      out, d_name, termToNode(d_conj), grammarToTypeNode(d_sygus_grammar));
2147
}
2148
2149
/* -------------------------------------------------------------------------- */
2150
/* class GetQuantifierEliminationCommand                                      */
2151
/* -------------------------------------------------------------------------- */
2152
2153
GetQuantifierEliminationCommand::GetQuantifierEliminationCommand()
2154
    : d_term(), d_doFull(true)
2155
{
2156
}
2157
9
GetQuantifierEliminationCommand::GetQuantifierEliminationCommand(
2158
9
    const api::Term& term, bool doFull)
2159
9
    : d_term(term), d_doFull(doFull)
2160
{
2161
9
}
2162
2163
api::Term GetQuantifierEliminationCommand::getTerm() const { return d_term; }
2164
bool GetQuantifierEliminationCommand::getDoFull() const { return d_doFull; }
2165
9
void GetQuantifierEliminationCommand::invoke(api::Solver* solver,
2166
                                             SymbolManager* sm)
2167
{
2168
  try
2169
  {
2170
9
    if (d_doFull)
2171
    {
2172
8
      d_result = solver->getQuantifierElimination(d_term);
2173
    }
2174
    else
2175
    {
2176
1
      d_result = solver->getQuantifierEliminationDisjunct(d_term);
2177
    }
2178
9
    d_commandStatus = CommandSuccess::instance();
2179
  }
2180
  catch (exception& e)
2181
  {
2182
    d_commandStatus = new CommandFailure(e.what());
2183
  }
2184
9
}
2185
2186
api::Term GetQuantifierEliminationCommand::getResult() const
2187
{
2188
  return d_result;
2189
}
2190
9
void GetQuantifierEliminationCommand::printResult(std::ostream& out,
2191
                                                  uint32_t verbosity) const
2192
{
2193
9
  if (!ok())
2194
  {
2195
    this->Command::printResult(out, verbosity);
2196
  }
2197
  else
2198
  {
2199
9
    out << d_result << endl;
2200
  }
2201
9
}
2202
2203
Command* GetQuantifierEliminationCommand::clone() const
2204
{
2205
  GetQuantifierEliminationCommand* c =
2206
      new GetQuantifierEliminationCommand(d_term, d_doFull);
2207
  c->d_result = d_result;
2208
  return c;
2209
}
2210
2211
18
std::string GetQuantifierEliminationCommand::getCommandName() const
2212
{
2213
18
  return d_doFull ? "get-qe" : "get-qe-disjunct";
2214
}
2215
2216
void GetQuantifierEliminationCommand::toStream(std::ostream& out,
2217
                                               int toDepth,
2218
                                               size_t dag,
2219
                                               Language language) const
2220
{
2221
  Printer::getPrinter(language)->toStreamCmdGetQuantifierElimination(
2222
      out, termToNode(d_term), d_doFull);
2223
}
2224
2225
/* -------------------------------------------------------------------------- */
2226
/* class GetUnsatAssumptionsCommand                                           */
2227
/* -------------------------------------------------------------------------- */
2228
2229
14
GetUnsatAssumptionsCommand::GetUnsatAssumptionsCommand() {}
2230
2231
14
void GetUnsatAssumptionsCommand::invoke(api::Solver* solver, SymbolManager* sm)
2232
{
2233
  try
2234
  {
2235
14
    d_result = solver->getUnsatAssumptions();
2236
14
    d_commandStatus = CommandSuccess::instance();
2237
  }
2238
  catch (api::CVC5ApiRecoverableException& e)
2239
  {
2240
    d_commandStatus = new CommandRecoverableFailure(e.what());
2241
  }
2242
  catch (exception& e)
2243
  {
2244
    d_commandStatus = new CommandFailure(e.what());
2245
  }
2246
14
}
2247
2248
std::vector<api::Term> GetUnsatAssumptionsCommand::getResult() const
2249
{
2250
  return d_result;
2251
}
2252
2253
14
void GetUnsatAssumptionsCommand::printResult(std::ostream& out,
2254
                                             uint32_t verbosity) const
2255
{
2256
14
  if (!ok())
2257
  {
2258
    this->Command::printResult(out, verbosity);
2259
  }
2260
  else
2261
  {
2262
14
    container_to_stream(out, d_result, "(", ")\n", " ");
2263
  }
2264
14
}
2265
2266
Command* GetUnsatAssumptionsCommand::clone() const
2267
{
2268
  GetUnsatAssumptionsCommand* c = new GetUnsatAssumptionsCommand;
2269
  c->d_result = d_result;
2270
  return c;
2271
}
2272
2273
28
std::string GetUnsatAssumptionsCommand::getCommandName() const
2274
{
2275
28
  return "get-unsat-assumptions";
2276
}
2277
2278
void GetUnsatAssumptionsCommand::toStream(std::ostream& out,
2279
                                          int toDepth,
2280
                                          size_t dag,
2281
                                          Language language) const
2282
{
2283
  Printer::getPrinter(language)->toStreamCmdGetUnsatAssumptions(out);
2284
}
2285
2286
/* -------------------------------------------------------------------------- */
2287
/* class GetUnsatCoreCommand                                                  */
2288
/* -------------------------------------------------------------------------- */
2289
2290
14
GetUnsatCoreCommand::GetUnsatCoreCommand() : d_sm(nullptr) {}
2291
14
void GetUnsatCoreCommand::invoke(api::Solver* solver, SymbolManager* sm)
2292
{
2293
  try
2294
  {
2295
14
    d_sm = sm;
2296
14
    d_result = solver->getUnsatCore();
2297
2298
12
    d_commandStatus = CommandSuccess::instance();
2299
  }
2300
4
  catch (api::CVC5ApiRecoverableException& e)
2301
  {
2302
2
    d_commandStatus = new CommandRecoverableFailure(e.what());
2303
  }
2304
  catch (exception& e)
2305
  {
2306
    d_commandStatus = new CommandFailure(e.what());
2307
  }
2308
14
}
2309
2310
14
void GetUnsatCoreCommand::printResult(std::ostream& out,
2311
                                      uint32_t verbosity) const
2312
{
2313
14
  if (!ok())
2314
  {
2315
2
    this->Command::printResult(out, verbosity);
2316
  }
2317
  else
2318
  {
2319
12
    if (options::printUnsatCoresFull())
2320
    {
2321
      // use the assertions
2322
6
      UnsatCore ucr(termVectorToNodes(d_result));
2323
3
      ucr.toStream(out);
2324
    }
2325
    else
2326
    {
2327
      // otherwise, use the names
2328
18
      std::vector<std::string> names;
2329
9
      d_sm->getExpressionNames(d_result, names, true);
2330
18
      UnsatCore ucr(names);
2331
9
      ucr.toStream(out);
2332
    }
2333
  }
2334
14
}
2335
2336
const std::vector<api::Term>& GetUnsatCoreCommand::getUnsatCore() const
2337
{
2338
  // of course, this will be empty if the command hasn't been invoked yet
2339
  return d_result;
2340
}
2341
2342
Command* GetUnsatCoreCommand::clone() const
2343
{
2344
  GetUnsatCoreCommand* c = new GetUnsatCoreCommand;
2345
  c->d_sm = d_sm;
2346
  c->d_result = d_result;
2347
  return c;
2348
}
2349
2350
28
std::string GetUnsatCoreCommand::getCommandName() const
2351
{
2352
28
  return "get-unsat-core";
2353
}
2354
2355
void GetUnsatCoreCommand::toStream(std::ostream& out,
2356
                                   int toDepth,
2357
                                   size_t dag,
2358
                                   Language language) const
2359
{
2360
  Printer::getPrinter(language)->toStreamCmdGetUnsatCore(out);
2361
}
2362
2363
/* -------------------------------------------------------------------------- */
2364
/* class GetDifficultyCommand */
2365
/* -------------------------------------------------------------------------- */
2366
2367
GetDifficultyCommand::GetDifficultyCommand() : d_sm(nullptr) {}
2368
void GetDifficultyCommand::invoke(api::Solver* solver, SymbolManager* sm)
2369
{
2370
  try
2371
  {
2372
    d_sm = sm;
2373
    d_result = solver->getDifficulty();
2374
2375
    d_commandStatus = CommandSuccess::instance();
2376
  }
2377
  catch (api::CVC5ApiRecoverableException& e)
2378
  {
2379
    d_commandStatus = new CommandRecoverableFailure(e.what());
2380
  }
2381
  catch (exception& e)
2382
  {
2383
    d_commandStatus = new CommandFailure(e.what());
2384
  }
2385
}
2386
2387
void GetDifficultyCommand::printResult(std::ostream& out,
2388
                                       uint32_t verbosity) const
2389
{
2390
  if (!ok())
2391
  {
2392
    this->Command::printResult(out, verbosity);
2393
  }
2394
  else
2395
  {
2396
    out << "(" << std::endl;
2397
    for (const std::pair<const api::Term, api::Term>& d : d_result)
2398
    {
2399
      out << "(";
2400
      // use name if it has one
2401
      std::string name;
2402
      if (d_sm->getExpressionName(d.first, name, true))
2403
      {
2404
        out << name;
2405
      }
2406
      else
2407
      {
2408
        out << d.first;
2409
      }
2410
      out << " " << d.second << ")" << std::endl;
2411
    }
2412
    out << ")" << std::endl;
2413
  }
2414
}
2415
2416
const std::map<api::Term, api::Term>& GetDifficultyCommand::getDifficultyMap()
2417
    const
2418
{
2419
  return d_result;
2420
}
2421
2422
Command* GetDifficultyCommand::clone() const
2423
{
2424
  GetDifficultyCommand* c = new GetDifficultyCommand;
2425
  c->d_sm = d_sm;
2426
  c->d_result = d_result;
2427
  return c;
2428
}
2429
2430
std::string GetDifficultyCommand::getCommandName() const
2431
{
2432
  return "get-difficulty";
2433
}
2434
2435
void GetDifficultyCommand::toStream(std::ostream& out,
2436
                                    int toDepth,
2437
                                    size_t dag,
2438
                                    Language language) const
2439
{
2440
  Printer::getPrinter(language)->toStreamCmdGetDifficulty(out);
2441
}
2442
2443
/* -------------------------------------------------------------------------- */
2444
/* class GetAssertionsCommand                                                 */
2445
/* -------------------------------------------------------------------------- */
2446
2447
GetAssertionsCommand::GetAssertionsCommand() {}
2448
void GetAssertionsCommand::invoke(api::Solver* solver, SymbolManager* sm)
2449
{
2450
  try
2451
  {
2452
    stringstream ss;
2453
    const vector<api::Term> v = solver->getAssertions();
2454
    ss << "(\n";
2455
    copy(v.begin(), v.end(), ostream_iterator<api::Term>(ss, "\n"));
2456
    ss << ")\n";
2457
    d_result = ss.str();
2458
    d_commandStatus = CommandSuccess::instance();
2459
  }
2460
  catch (exception& e)
2461
  {
2462
    d_commandStatus = new CommandFailure(e.what());
2463
  }
2464
}
2465
2466
std::string GetAssertionsCommand::getResult() const { return d_result; }
2467
void GetAssertionsCommand::printResult(std::ostream& out,
2468
                                       uint32_t verbosity) const
2469
{
2470
  if (!ok())
2471
  {
2472
    this->Command::printResult(out, verbosity);
2473
  }
2474
  else
2475
  {
2476
    out << d_result;
2477
  }
2478
}
2479
2480
Command* GetAssertionsCommand::clone() const
2481
{
2482
  GetAssertionsCommand* c = new GetAssertionsCommand();
2483
  c->d_result = d_result;
2484
  return c;
2485
}
2486
2487
std::string GetAssertionsCommand::getCommandName() const
2488
{
2489
  return "get-assertions";
2490
}
2491
2492
void GetAssertionsCommand::toStream(std::ostream& out,
2493
                                    int toDepth,
2494
                                    size_t dag,
2495
                                    Language language) const
2496
{
2497
  Printer::getPrinter(language)->toStreamCmdGetAssertions(out);
2498
}
2499
2500
/* -------------------------------------------------------------------------- */
2501
/* class SetBenchmarkLogicCommand                                             */
2502
/* -------------------------------------------------------------------------- */
2503
2504
6564
SetBenchmarkLogicCommand::SetBenchmarkLogicCommand(std::string logic)
2505
6564
    : d_logic(logic)
2506
{
2507
6564
}
2508
2509
std::string SetBenchmarkLogicCommand::getLogic() const { return d_logic; }
2510
6520
void SetBenchmarkLogicCommand::invoke(api::Solver* solver, SymbolManager* sm)
2511
{
2512
  try
2513
  {
2514
6520
    solver->setLogic(d_logic);
2515
6520
    d_commandStatus = CommandSuccess::instance();
2516
  }
2517
  catch (exception& e)
2518
  {
2519
    d_commandStatus = new CommandFailure(e.what());
2520
  }
2521
6520
}
2522
2523
Command* SetBenchmarkLogicCommand::clone() const
2524
{
2525
  return new SetBenchmarkLogicCommand(d_logic);
2526
}
2527
2528
13040
std::string SetBenchmarkLogicCommand::getCommandName() const
2529
{
2530
13040
  return "set-logic";
2531
}
2532
2533
6
void SetBenchmarkLogicCommand::toStream(std::ostream& out,
2534
                                        int toDepth,
2535
                                        size_t dag,
2536
                                        Language language) const
2537
{
2538
6
  Printer::getPrinter(language)->toStreamCmdSetBenchmarkLogic(out, d_logic);
2539
6
}
2540
2541
/* -------------------------------------------------------------------------- */
2542
/* class SetInfoCommand                                                       */
2543
/* -------------------------------------------------------------------------- */
2544
2545
5876
SetInfoCommand::SetInfoCommand(const std::string& flag,
2546
5876
                               const std::string& value)
2547
5876
    : d_flag(flag), d_value(value)
2548
{
2549
5876
}
2550
2551
const std::string& SetInfoCommand::getFlag() const { return d_flag; }
2552
const std::string& SetInfoCommand::getValue() const { return d_value; }
2553
5872
void SetInfoCommand::invoke(api::Solver* solver, SymbolManager* sm)
2554
{
2555
  try
2556
  {
2557
5872
    solver->setInfo(d_flag, d_value);
2558
5860
    d_commandStatus = CommandSuccess::instance();
2559
  }
2560
24
  catch (api::CVC5ApiUnsupportedException&)
2561
  {
2562
    // As per SMT-LIB spec, silently accept unknown set-info keys
2563
12
    d_commandStatus = CommandSuccess::instance();
2564
  }
2565
  catch (api::CVC5ApiRecoverableException& e)
2566
  {
2567
    d_commandStatus = new CommandRecoverableFailure(e.getMessage());
2568
  }
2569
  catch (exception& e)
2570
  {
2571
    d_commandStatus = new CommandFailure(e.what());
2572
  }
2573
5872
}
2574
2575
Command* SetInfoCommand::clone() const
2576
{
2577
  return new SetInfoCommand(d_flag, d_value);
2578
}
2579
2580
11744
std::string SetInfoCommand::getCommandName() const { return "set-info"; }
2581
2582
void SetInfoCommand::toStream(std::ostream& out,
2583
                              int toDepth,
2584
                              size_t dag,
2585
                              Language language) const
2586
{
2587
  Printer::getPrinter(language)->toStreamCmdSetInfo(out, d_flag, d_value);
2588
}
2589
2590
/* -------------------------------------------------------------------------- */
2591
/* class GetInfoCommand                                                       */
2592
/* -------------------------------------------------------------------------- */
2593
2594
20
GetInfoCommand::GetInfoCommand(std::string flag) : d_flag(flag) {}
2595
std::string GetInfoCommand::getFlag() const { return d_flag; }
2596
20
void GetInfoCommand::invoke(api::Solver* solver, SymbolManager* sm)
2597
{
2598
  try
2599
  {
2600
40
    std::vector<api::Term> v;
2601
20
    v.push_back(solver->mkString(":" + d_flag));
2602
20
    v.push_back(solver->mkString(solver->getInfo(d_flag)));
2603
13
    d_result = sexprToString(solver->mkTerm(api::SEXPR, v));
2604
13
    d_commandStatus = CommandSuccess::instance();
2605
  }
2606
8
  catch (api::CVC5ApiUnsupportedException&)
2607
  {
2608
4
    d_commandStatus = new CommandUnsupported();
2609
  }
2610
6
  catch (api::CVC5ApiRecoverableException& e)
2611
  {
2612
3
    d_commandStatus = new CommandRecoverableFailure(e.getMessage());
2613
  }
2614
  catch (exception& e)
2615
  {
2616
    d_commandStatus = new CommandFailure(e.what());
2617
  }
2618
20
}
2619
2620
std::string GetInfoCommand::getResult() const { return d_result; }
2621
20
void GetInfoCommand::printResult(std::ostream& out, uint32_t verbosity) const
2622
{
2623
20
  if (!ok())
2624
  {
2625
7
    this->Command::printResult(out, verbosity);
2626
  }
2627
13
  else if (d_result != "")
2628
  {
2629
13
    out << d_result << endl;
2630
  }
2631
20
}
2632
2633
Command* GetInfoCommand::clone() const
2634
{
2635
  GetInfoCommand* c = new GetInfoCommand(d_flag);
2636
  c->d_result = d_result;
2637
  return c;
2638
}
2639
2640
29
std::string GetInfoCommand::getCommandName() const { return "get-info"; }
2641
2642
11
void GetInfoCommand::toStream(std::ostream& out,
2643
                              int toDepth,
2644
                              size_t dag,
2645
                              Language language) const
2646
{
2647
11
  Printer::getPrinter(language)->toStreamCmdGetInfo(out, d_flag);
2648
11
}
2649
2650
/* -------------------------------------------------------------------------- */
2651
/* class SetOptionCommand                                                     */
2652
/* -------------------------------------------------------------------------- */
2653
2654
8653
SetOptionCommand::SetOptionCommand(const std::string& flag,
2655
8653
                                   const std::string& value)
2656
8653
    : d_flag(flag), d_value(value)
2657
{
2658
8653
}
2659
2660
const std::string& SetOptionCommand::getFlag() const { return d_flag; }
2661
const std::string& SetOptionCommand::getValue() const { return d_value; }
2662
8653
void SetOptionCommand::invoke(api::Solver* solver, SymbolManager* sm)
2663
{
2664
  try
2665
  {
2666
8653
    solver->setOption(d_flag, d_value);
2667
8651
    d_commandStatus = CommandSuccess::instance();
2668
  }
2669
  catch (api::CVC5ApiUnsupportedException&)
2670
  {
2671
    d_commandStatus = new CommandUnsupported();
2672
  }
2673
  catch (api::CVC5ApiRecoverableException& e)
2674
  {
2675
    d_commandStatus = new CommandRecoverableFailure(e.getMessage());
2676
  }
2677
4
  catch (exception& e)
2678
  {
2679
2
    d_commandStatus = new CommandFailure(e.what());
2680
  }
2681
8653
}
2682
2683
Command* SetOptionCommand::clone() const
2684
{
2685
  return new SetOptionCommand(d_flag, d_value);
2686
}
2687
2688
11401
std::string SetOptionCommand::getCommandName() const { return "set-option"; }
2689
2690
4
void SetOptionCommand::toStream(std::ostream& out,
2691
                                int toDepth,
2692
                                size_t dag,
2693
                                Language language) const
2694
{
2695
4
  Printer::getPrinter(language)->toStreamCmdSetOption(out, d_flag, d_value);
2696
4
}
2697
2698
/* -------------------------------------------------------------------------- */
2699
/* class GetOptionCommand                                                     */
2700
/* -------------------------------------------------------------------------- */
2701
2702
51
GetOptionCommand::GetOptionCommand(std::string flag) : d_flag(flag) {}
2703
std::string GetOptionCommand::getFlag() const { return d_flag; }
2704
51
void GetOptionCommand::invoke(api::Solver* solver, SymbolManager* sm)
2705
{
2706
  try
2707
  {
2708
51
    d_result = solver->getOption(d_flag);
2709
51
    d_commandStatus = CommandSuccess::instance();
2710
  }
2711
  catch (api::CVC5ApiUnsupportedException&)
2712
  {
2713
    d_commandStatus = new CommandUnsupported();
2714
  }
2715
  catch (exception& e)
2716
  {
2717
    d_commandStatus = new CommandFailure(e.what());
2718
  }
2719
51
}
2720
2721
std::string GetOptionCommand::getResult() const { return d_result; }
2722
51
void GetOptionCommand::printResult(std::ostream& out, uint32_t verbosity) const
2723
{
2724
51
  if (!ok())
2725
  {
2726
    this->Command::printResult(out, verbosity);
2727
  }
2728
51
  else if (d_result != "")
2729
  {
2730
51
    out << d_result << endl;
2731
  }
2732
51
}
2733
2734
Command* GetOptionCommand::clone() const
2735
{
2736
  GetOptionCommand* c = new GetOptionCommand(d_flag);
2737
  c->d_result = d_result;
2738
  return c;
2739
}
2740
2741
102
std::string GetOptionCommand::getCommandName() const { return "get-option"; }
2742
2743
void GetOptionCommand::toStream(std::ostream& out,
2744
                                int toDepth,
2745
                                size_t dag,
2746
                                Language language) const
2747
{
2748
  Printer::getPrinter(language)->toStreamCmdGetOption(out, d_flag);
2749
}
2750
2751
/* -------------------------------------------------------------------------- */
2752
/* class DatatypeDeclarationCommand                                           */
2753
/* -------------------------------------------------------------------------- */
2754
2755
DatatypeDeclarationCommand::DatatypeDeclarationCommand(
2756
    const api::Sort& datatype)
2757
    : d_datatypes()
2758
{
2759
  d_datatypes.push_back(datatype);
2760
}
2761
2762
1168
DatatypeDeclarationCommand::DatatypeDeclarationCommand(
2763
1168
    const std::vector<api::Sort>& datatypes)
2764
1168
    : d_datatypes(datatypes)
2765
{
2766
1168
}
2767
2768
const std::vector<api::Sort>& DatatypeDeclarationCommand::getDatatypes() const
2769
{
2770
  return d_datatypes;
2771
}
2772
2773
1168
void DatatypeDeclarationCommand::invoke(api::Solver* solver, SymbolManager* sm)
2774
{
2775
1168
  d_commandStatus = CommandSuccess::instance();
2776
1168
}
2777
2778
Command* DatatypeDeclarationCommand::clone() const
2779
{
2780
  return new DatatypeDeclarationCommand(d_datatypes);
2781
}
2782
2783
2336
std::string DatatypeDeclarationCommand::getCommandName() const
2784
{
2785
2336
  return "declare-datatypes";
2786
}
2787
2788
2
void DatatypeDeclarationCommand::toStream(std::ostream& out,
2789
                                          int toDepth,
2790
                                          size_t dag,
2791
                                          Language language) const
2792
{
2793
4
  Printer::getPrinter(language)->toStreamCmdDatatypeDeclaration(
2794
4
      out, sortVectorToTypeNodes(d_datatypes));
2795
2
}
2796
2797
31137
}  // namespace cvc5