GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/smt/command.cpp Lines: 688 1237 55.6 %
Date: 2021-09-29 Branches: 497 1506 33.0 %

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