GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/smt/command.cpp Lines: 686 1234 55.6 %
Date: 2021-09-30 Branches: 490 1494 32.8 %

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