GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/printer/printer.cpp Lines: 59 251 23.5 %
Date: 2021-09-29 Branches: 63 419 15.0 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Abdalrhman Mohamed, Andrew Reynolds, 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
 * Base of the pretty-printer interface.
14
 */
15
#include "printer/printer.h"
16
17
#include <string>
18
19
#include "options/base_options.h"
20
#include "options/language.h"
21
#include "printer/ast/ast_printer.h"
22
#include "printer/smt2/smt2_printer.h"
23
#include "printer/tptp/tptp_printer.h"
24
#include "proof/unsat_core.h"
25
#include "smt/command.h"
26
#include "smt/node_command.h"
27
#include "theory/quantifiers/instantiation_list.h"
28
29
using namespace std;
30
31
namespace cvc5 {
32
33
unique_ptr<Printer>
34
15164
    Printer::d_printers[static_cast<size_t>(Language::LANG_MAX)];
35
36
4339
unique_ptr<Printer> Printer::makePrinter(Language lang)
37
{
38
4339
  switch(lang) {
39
4093
    case Language::LANG_SMTLIB_V2_6:
40
      return unique_ptr<Printer>(
41
4093
          new printer::smt2::Smt2Printer(printer::smt2::smt2_6_variant));
42
43
41
    case Language::LANG_TPTP:
44
41
      return unique_ptr<Printer>(new printer::tptp::TptpPrinter());
45
46
201
    case Language::LANG_SYGUS_V2:
47
      // sygus version 2.0 does not have discrepancies with smt2, hence we use
48
      // a normal smt2 variant here.
49
      return unique_ptr<Printer>(
50
201
          new printer::smt2::Smt2Printer(printer::smt2::smt2_6_variant));
51
52
4
    case Language::LANG_AST:
53
4
      return unique_ptr<Printer>(new printer::ast::AstPrinter());
54
55
    default: Unhandled() << lang;
56
  }
57
}
58
59
29
void Printer::toStream(std::ostream& out, const smt::Model& m) const
60
{
61
  // print the declared sorts
62
29
  const std::vector<TypeNode>& dsorts = m.getDeclaredSorts();
63
35
  for (const TypeNode& tn : dsorts)
64
  {
65
6
    toStreamModelSort(out, tn, m.getDomainElements(tn));
66
  }
67
  // print the declared terms
68
29
  const std::vector<Node>& dterms = m.getDeclaredTerms();
69
63
  for (const Node& n : dterms)
70
  {
71
34
    toStreamModelTerm(out, n, m.getValue(n));
72
  }
73
29
}
74
75
void Printer::toStreamUsing(Language lang,
76
                            std::ostream& out,
77
                            const smt::Model& m) const
78
{
79
  getPrinter(lang)->toStream(out, m);
80
}
81
82
void Printer::toStream(std::ostream& out, const UnsatCore& core) const
83
{
84
  for(UnsatCore::iterator i = core.begin(); i != core.end(); ++i) {
85
    toStreamCmdAssert(out, *i);
86
    out << std::endl;
87
  }
88
}/* Printer::toStream(UnsatCore) */
89
90
5
void Printer::toStream(std::ostream& out, const InstantiationList& is) const
91
{
92
5
  out << "(instantiations " << is.d_quant << std::endl;
93
10
  for (const InstantiationVec& i : is.d_inst)
94
  {
95
5
    out << "  ";
96
5
    if (i.d_id != theory::InferenceId::UNKNOWN)
97
    {
98
      out << "(! ";
99
    }
100
5
    out << "( ";
101
10
    for (const Node& n : i.d_vec)
102
    {
103
5
      out << n << " ";
104
    }
105
5
    out << ")";
106
5
    if (i.d_id != theory::InferenceId::UNKNOWN)
107
    {
108
      out << " :source " << i.d_id;
109
      if (!i.d_pfArg.isNull())
110
      {
111
        out << " " << i.d_pfArg;
112
      }
113
      out << ")";
114
    }
115
5
    out << std::endl;
116
  }
117
5
  out << ")" << std::endl;
118
5
}
119
120
3
void Printer::toStream(std::ostream& out, const SkolemList& sks) const
121
{
122
3
  out << "(skolem " << sks.d_quant << std::endl;
123
3
  out << "  ( ";
124
6
  for (const Node& n : sks.d_sks)
125
  {
126
3
    out << n << " ";
127
  }
128
3
  out << ")" << std::endl;
129
3
  out << ")" << std::endl;
130
3
}
131
132
243198
Printer* Printer::getPrinter(Language lang)
133
{
134
243198
  if (lang == Language::LANG_AUTO)
135
  {
136
    // Infer the language to use for output.
137
    //
138
    // Options can be null in certain circumstances (e.g., when printing
139
    // the singleton "null" expr.  So we guard against segfault
140
2353
    if (not Options::isCurrentNull())
141
    {
142
2343
      if (Options::current().base.outputLanguageWasSetByUser)
143
      {
144
15
        lang = options::outputLanguage();
145
      }
146
2343
      if (lang == Language::LANG_AUTO
147
2343
          && Options::current().base.inputLanguageWasSetByUser)
148
      {
149
19
        lang = options::inputLanguage();
150
      }
151
    }
152
2353
    if (lang == Language::LANG_AUTO)
153
    {
154
2319
      lang = Language::LANG_SMTLIB_V2_6;  // default
155
    }
156
  }
157
243198
  if (d_printers[static_cast<size_t>(lang)] == nullptr)
158
  {
159
4339
    d_printers[static_cast<size_t>(lang)] = makePrinter(lang);
160
  }
161
243198
  return d_printers[static_cast<size_t>(lang)].get();
162
}
163
164
void Printer::printUnknownCommand(std::ostream& out,
165
                                  const std::string& name) const
166
{
167
  out << "ERROR: don't know how to print " << name << " command" << std::endl;
168
}
169
170
void Printer::toStreamCmdEmpty(std::ostream& out, const std::string& name) const
171
{
172
  printUnknownCommand(out, "empty");
173
}
174
175
void Printer::toStreamCmdEcho(std::ostream& out,
176
                              const std::string& output) const
177
{
178
  printUnknownCommand(out, "echo");
179
}
180
181
void Printer::toStreamCmdAssert(std::ostream& out, Node n) const
182
{
183
  printUnknownCommand(out, "assert");
184
}
185
186
void Printer::toStreamCmdPush(std::ostream& out) const
187
{
188
  printUnknownCommand(out, "push");
189
}
190
191
void Printer::toStreamCmdPop(std::ostream& out) const
192
{
193
  printUnknownCommand(out, "pop");
194
}
195
196
void Printer::toStreamCmdDeclareFunction(std::ostream& out,
197
                                         const std::string& id,
198
                                         TypeNode type) const
199
{
200
  printUnknownCommand(out, "declare-fun");
201
}
202
203
5
void Printer::toStreamCmdDeclareFunction(std::ostream& out, const Node& v) const
204
{
205
10
  std::stringstream vs;
206
5
  vs << v;
207
5
  toStreamCmdDeclareFunction(out, vs.str(), v.getType());
208
5
}
209
210
void Printer::toStreamCmdDeclarePool(std::ostream& out,
211
                                     const std::string& id,
212
                                     TypeNode type,
213
                                     const std::vector<Node>& initValue) const
214
{
215
  printUnknownCommand(out, "declare-pool");
216
}
217
218
void Printer::toStreamCmdDeclareType(std::ostream& out,
219
                                     TypeNode type) const
220
{
221
  printUnknownCommand(out, "declare-sort");
222
}
223
224
void Printer::toStreamCmdDefineType(std::ostream& out,
225
                                    const std::string& id,
226
                                    const std::vector<TypeNode>& params,
227
                                    TypeNode t) const
228
{
229
  printUnknownCommand(out, "define-sort");
230
}
231
232
void Printer::toStreamCmdDefineFunction(std::ostream& out,
233
                                        const std::string& id,
234
                                        const std::vector<Node>& formals,
235
                                        TypeNode range,
236
                                        Node formula) const
237
{
238
  printUnknownCommand(out, "define-fun");
239
}
240
241
void Printer::toStreamCmdDefineFunction(std::ostream& out,
242
                                        Node v,
243
                                        Node lambda) const
244
{
245
  std::stringstream vs;
246
  vs << v;
247
  std::vector<Node> formals;
248
  Node body = lambda;
249
  TypeNode rangeType = v.getType();
250
  if (body.getKind() == kind::LAMBDA)
251
  {
252
    formals.insert(formals.end(), lambda[0].begin(), lambda[0].end());
253
    body = lambda[1];
254
    Assert(rangeType.isFunction());
255
    rangeType = rangeType.getRangeType();
256
  }
257
  toStreamCmdDefineFunction(out, vs.str(), formals, rangeType, body);
258
}
259
260
void Printer::toStreamCmdDefineFunctionRec(
261
    std::ostream& out,
262
    const std::vector<Node>& funcs,
263
    const std::vector<std::vector<Node>>& formals,
264
    const std::vector<Node>& formulas) const
265
{
266
  printUnknownCommand(out, "define-fun-rec");
267
}
268
269
void Printer::toStreamCmdDefineFunctionRec(
270
    std::ostream& out,
271
    const std::vector<Node>& funcs,
272
    const std::vector<Node>& lambdas) const
273
{
274
  std::vector<std::vector<Node>> formals;
275
  std::vector<Node> formulas;
276
  for (const Node& l : lambdas)
277
  {
278
    std::vector<Node> formalsVec;
279
    Node formula;
280
    if (l.getKind() == kind::LAMBDA)
281
    {
282
      formalsVec.insert(formalsVec.end(), l[0].begin(), l[0].end());
283
      formula = l[1];
284
    }
285
    else
286
    {
287
      formula = l;
288
    }
289
    formals.emplace_back(formalsVec);
290
    formulas.emplace_back(formula);
291
  }
292
  toStreamCmdDefineFunctionRec(out, funcs, formals, formulas);
293
}
294
295
void Printer::toStreamCmdSetUserAttribute(std::ostream& out,
296
                                          const std::string& attr,
297
                                          Node n) const
298
{
299
  printUnknownCommand(out, "set-user-attribute");
300
}
301
302
void Printer::toStreamCmdCheckSat(std::ostream& out, Node n) const
303
{
304
  printUnknownCommand(out, "check-sat");
305
}
306
307
void Printer::toStreamCmdCheckSatAssuming(std::ostream& out,
308
                                          const std::vector<Node>& nodes) const
309
{
310
  printUnknownCommand(out, "check-sat-assuming");
311
}
312
313
void Printer::toStreamCmdQuery(std::ostream& out, Node n) const
314
{
315
  printUnknownCommand(out, "query");
316
}
317
318
void Printer::toStreamCmdDeclareVar(std::ostream& out,
319
                                    Node var,
320
                                    TypeNode type) const
321
{
322
  printUnknownCommand(out, "declare-var");
323
}
324
325
void Printer::toStreamCmdSynthFun(std::ostream& out,
326
                                  Node f,
327
                                  const std::vector<Node>& vars,
328
                                  bool isInv,
329
                                  TypeNode sygusType) const
330
{
331
  printUnknownCommand(out, isInv ? "synth-inv" : "synth-fun");
332
}
333
334
void Printer::toStreamCmdConstraint(std::ostream& out, Node n) const
335
{
336
  printUnknownCommand(out, "constraint");
337
}
338
339
void Printer::toStreamCmdAssume(std::ostream& out, Node n) const
340
{
341
  printUnknownCommand(out, "assume");
342
}
343
344
void Printer::toStreamCmdInvConstraint(
345
    std::ostream& out, Node inv, Node pre, Node trans, Node post) const
346
{
347
  printUnknownCommand(out, "inv-constraint");
348
}
349
350
void Printer::toStreamCmdCheckSynth(std::ostream& out) const
351
{
352
  printUnknownCommand(out, "check-synth");
353
}
354
355
void Printer::toStreamCmdSimplify(std::ostream& out, Node n) const
356
{
357
  printUnknownCommand(out, "simplify");
358
}
359
360
void Printer::toStreamCmdGetValue(std::ostream& out,
361
                                  const std::vector<Node>& nodes) const
362
{
363
  printUnknownCommand(out, "get-value");
364
}
365
366
void Printer::toStreamCmdGetAssignment(std::ostream& out) const
367
{
368
  printUnknownCommand(out, "get-assignment");
369
}
370
371
void Printer::toStreamCmdGetModel(std::ostream& out) const
372
{
373
  printUnknownCommand(out, "ge-model");
374
}
375
376
void Printer::toStreamCmdBlockModel(std::ostream& out) const
377
{
378
  printUnknownCommand(out, "block-model");
379
}
380
381
void Printer::toStreamCmdBlockModelValues(std::ostream& out,
382
                                          const std::vector<Node>& nodes) const
383
{
384
  printUnknownCommand(out, "block-model-values");
385
}
386
387
void Printer::toStreamCmdGetProof(std::ostream& out) const
388
{
389
  printUnknownCommand(out, "get-proof");
390
}
391
392
void Printer::toStreamCmdGetInstantiations(std::ostream& out) const
393
{
394
  printUnknownCommand(out, "get-instantiations");
395
}
396
397
void Printer::toStreamCmdGetInterpol(std::ostream& out,
398
                                     const std::string& name,
399
                                     Node conj,
400
                                     TypeNode sygusType) const
401
{
402
  printUnknownCommand(out, "get-interpol");
403
}
404
405
void Printer::toStreamCmdGetAbduct(std::ostream& out,
406
                                   const std::string& name,
407
                                   Node conj,
408
                                   TypeNode sygusType) const
409
{
410
  printUnknownCommand(out, "get-abduct");
411
}
412
413
void Printer::toStreamCmdGetQuantifierElimination(std::ostream& out,
414
                                                  Node n) const
415
{
416
  printUnknownCommand(out, "get-quantifier-elimination");
417
}
418
419
void Printer::toStreamCmdGetUnsatAssumptions(std::ostream& out) const
420
{
421
  printUnknownCommand(out, "get-unsat-assumption");
422
}
423
424
void Printer::toStreamCmdGetUnsatCore(std::ostream& out) const
425
{
426
  printUnknownCommand(out, "get-unsat-core");
427
}
428
429
void Printer::toStreamCmdGetDifficulty(std::ostream& out) const
430
{
431
  printUnknownCommand(out, "get-difficulty");
432
}
433
434
void Printer::toStreamCmdGetAssertions(std::ostream& out) const
435
{
436
  printUnknownCommand(out, "get-assertions");
437
}
438
439
void Printer::toStreamCmdSetBenchmarkLogic(std::ostream& out,
440
                                           const std::string& logic) const
441
{
442
  printUnknownCommand(out, "set-logic");
443
}
444
445
void Printer::toStreamCmdSetInfo(std::ostream& out,
446
                                 const std::string& flag,
447
                                 const std::string& value) const
448
{
449
  printUnknownCommand(out, "set-info");
450
}
451
452
void Printer::toStreamCmdGetInfo(std::ostream& out,
453
                                 const std::string& flag) const
454
{
455
  printUnknownCommand(out, "get-info");
456
}
457
458
void Printer::toStreamCmdSetOption(std::ostream& out,
459
                                   const std::string& flag,
460
                                   const std::string& value) const
461
{
462
  printUnknownCommand(out, "set-option");
463
}
464
465
void Printer::toStreamCmdGetOption(std::ostream& out,
466
                                   const std::string& flag) const
467
{
468
  printUnknownCommand(out, "get-option");
469
}
470
471
void Printer::toStreamCmdSetExpressionName(std::ostream& out,
472
                                           Node n,
473
                                           const std::string& name) const
474
{
475
  printUnknownCommand(out, "set-expression-name");
476
}
477
478
void Printer::toStreamCmdDatatypeDeclaration(
479
    std::ostream& out, const std::vector<TypeNode>& datatypes) const
480
{
481
  printUnknownCommand(
482
      out, datatypes.size() == 1 ? "declare-datatype" : "declare-datatypes");
483
}
484
485
void Printer::toStreamCmdReset(std::ostream& out) const
486
{
487
  printUnknownCommand(out, "reset");
488
}
489
490
void Printer::toStreamCmdResetAssertions(std::ostream& out) const
491
{
492
  printUnknownCommand(out, "reset-assertions");
493
}
494
495
void Printer::toStreamCmdQuit(std::ostream& out) const
496
{
497
  printUnknownCommand(out, "quit");
498
}
499
500
void Printer::toStreamCmdDeclareHeap(std::ostream& out,
501
                                     TypeNode locType,
502
                                     TypeNode dataType) const
503
{
504
  printUnknownCommand(out, "declare-heap");
505
}
506
507
void Printer::toStreamCmdCommandSequence(
508
    std::ostream& out, const std::vector<Command*>& sequence) const
509
{
510
  printUnknownCommand(out, "sequence");
511
}
512
513
void Printer::toStreamCmdDeclarationSequence(
514
    std::ostream& out, const std::vector<Command*>& sequence) const
515
{
516
  printUnknownCommand(out, "sequence");
517
}
518
519
22746
}  // namespace cvc5