GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/smt/command.cpp Lines: 670 1233 54.3 %
Date: 2021-09-07 Branches: 487 1491 32.7 %

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