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