GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/api/cpp/cvc5.h Lines: 13 18 72.2 %
Date: 2021-05-22 Branches: 1 2 50.0 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Aina Niemetz, Andrew Reynolds, Abdalrhman Mohamed
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
 * The cvc5 C++ API.
14
 */
15
16
#include "cvc5_export.h"
17
18
#ifndef CVC5__API__CVC5_H
19
#define CVC5__API__CVC5_H
20
21
#include <map>
22
#include <memory>
23
#include <set>
24
#include <sstream>
25
#include <string>
26
#include <unordered_map>
27
#include <unordered_set>
28
#include <vector>
29
30
#include "api/cpp/cvc5_kind.h"
31
32
namespace cvc5 {
33
34
#ifndef DOXYGEN_SKIP
35
template <bool ref_count>
36
class NodeTemplate;
37
typedef NodeTemplate<true> Node;
38
#endif
39
40
class Command;
41
class DType;
42
class DTypeConstructor;
43
class DTypeSelector;
44
class NodeManager;
45
class SmtEngine;
46
class TypeNode;
47
class Options;
48
class Random;
49
class Result;
50
class StatisticsRegistry;
51
52
namespace api {
53
54
class Solver;
55
class Statistics;
56
struct APIStatistics;
57
58
/* -------------------------------------------------------------------------- */
59
/* Exception                                                                  */
60
/* -------------------------------------------------------------------------- */
61
62
/**
63
 * Base class for all API exceptions.
64
 * If thrown, all API objects may be in an unsafe state.
65
 */
66
1324
class CVC5_EXPORT CVC5ApiException : public std::exception
67
{
68
 public:
69
  /**
70
   * Construct with message from a string.
71
   * @param str The error message.
72
   */
73
1324
  CVC5ApiException(const std::string& str) : d_msg(str) {}
74
  /**
75
   * Construct with message from a string stream.
76
   * @param stream The error message.
77
   */
78
  CVC5ApiException(const std::stringstream& stream) : d_msg(stream.str()) {}
79
  /**
80
   * Retrieve the message from this exception.
81
   * @return The error message.
82
   */
83
  const std::string& getMessage() const { return d_msg; }
84
  /**
85
   * Retrieve the message as a C-style array.
86
   * @return The error message.
87
   */
88
53
  const char* what() const noexcept override { return d_msg.c_str(); }
89
90
 private:
91
  /** The stored error message. */
92
  std::string d_msg;
93
};
94
95
/**
96
 * A recoverable API exception.
97
 * If thrown, API objects can still be used.
98
 */
99
57
class CVC5_EXPORT CVC5ApiRecoverableException : public CVC5ApiException
100
{
101
 public:
102
  /**
103
   * Construct with message from a string.
104
   * @param str The error message.
105
   */
106
57
  CVC5ApiRecoverableException(const std::string& str) : CVC5ApiException(str) {}
107
  /**
108
   * Construct with message from a string stream.
109
   * @param stream The error message.
110
   */
111
  CVC5ApiRecoverableException(const std::stringstream& stream)
112
      : CVC5ApiException(stream.str())
113
  {
114
  }
115
};
116
117
/* -------------------------------------------------------------------------- */
118
/* Result                                                                     */
119
/* -------------------------------------------------------------------------- */
120
121
/**
122
 * Encapsulation of a three-valued solver result, with explanations.
123
 */
124
404324
class CVC5_EXPORT Result
125
{
126
  friend class Solver;
127
128
 public:
129
  enum UnknownExplanation
130
  {
131
    REQUIRES_FULL_CHECK,
132
    INCOMPLETE,
133
    TIMEOUT,
134
    RESOURCEOUT,
135
    MEMOUT,
136
    INTERRUPTED,
137
    NO_STATUS,
138
    UNSUPPORTED,
139
    OTHER,
140
    UNKNOWN_REASON
141
  };
142
143
  /** Constructor. */
144
  Result();
145
146
  /**
147
   * Return true if Result is empty, i.e., a nullary Result, and not an actual
148
   * result returned from a checkSat() (and friends) query.
149
   */
150
  bool isNull() const;
151
152
  /**
153
   * Return true if query was a satisfiable checkSat() or checkSatAssuming()
154
   * query.
155
   */
156
  bool isSat() const;
157
158
  /**
159
   * Return true if query was an unsatisfiable checkSat() or
160
   * checkSatAssuming() query.
161
   */
162
  bool isUnsat() const;
163
164
  /**
165
   * Return true if query was a checkSat() or checkSatAssuming() query and
166
   * cvc5 was not able to determine (un)satisfiability.
167
   */
168
  bool isSatUnknown() const;
169
170
  /**
171
   * Return true if corresponding query was an entailed checkEntailed() query.
172
   */
173
  bool isEntailed() const;
174
175
  /**
176
   * Return true if corresponding query was a checkEntailed() query that is
177
   * not entailed.
178
   */
179
  bool isNotEntailed() const;
180
181
  /**
182
   * Return true if query was a checkEntailed() () query and cvc5 was not able
183
   * to determine if it is entailed.
184
   */
185
  bool isEntailmentUnknown() const;
186
187
  /**
188
   * Operator overloading for equality of two results.
189
   * @param r the result to compare to for equality
190
   * @return true if the results are equal
191
   */
192
  bool operator==(const Result& r) const;
193
194
  /**
195
   * Operator overloading for disequality of two results.
196
   * @param r the result to compare to for disequality
197
   * @return true if the results are disequal
198
   */
199
  bool operator!=(const Result& r) const;
200
201
  /**
202
   * @return an explanation for an unknown query result.
203
   */
204
  UnknownExplanation getUnknownExplanation() const;
205
206
  /**
207
   * @return a string representation of this result.
208
   */
209
  std::string toString() const;
210
211
 private:
212
  /**
213
   * Constructor.
214
   * @param r the internal result that is to be wrapped by this result
215
   * @return the Result
216
   */
217
  Result(const cvc5::Result& r);
218
219
  /**
220
   * The interal result wrapped by this result.
221
   * Note: This is a shared_ptr rather than a unique_ptr since cvc5::Result is
222
   *       not ref counted.
223
   */
224
  std::shared_ptr<cvc5::Result> d_result;
225
};
226
227
/**
228
 * Serialize a Result to given stream.
229
 * @param out the output stream
230
 * @param r the result to be serialized to the given output stream
231
 * @return the output stream
232
 */
233
std::ostream& operator<<(std::ostream& out, const Result& r) CVC5_EXPORT;
234
235
/**
236
 * Serialize an UnknownExplanation to given stream.
237
 * @param out the output stream
238
 * @param e the explanation to be serialized to the given output stream
239
 * @return the output stream
240
 */
241
std::ostream& operator<<(std::ostream& out,
242
                         enum Result::UnknownExplanation e) CVC5_EXPORT;
243
244
/* -------------------------------------------------------------------------- */
245
/* Sort                                                                       */
246
/* -------------------------------------------------------------------------- */
247
248
class Datatype;
249
250
/**
251
 * The sort of a cvc5 term.
252
 */
253
8892227
class CVC5_EXPORT Sort
254
{
255
  friend class cvc5::Command;
256
  friend class DatatypeConstructor;
257
  friend class DatatypeConstructorDecl;
258
  friend class DatatypeSelector;
259
  friend class DatatypeDecl;
260
  friend class Op;
261
  friend class Solver;
262
  friend class Grammar;
263
  friend struct std::hash<Sort>;
264
  friend class Term;
265
266
 public:
267
  /**
268
   * Constructor.
269
   */
270
  Sort();
271
272
  /**
273
   * Destructor.
274
   */
275
  ~Sort();
276
277
  /**
278
   * Comparison for structural equality.
279
   * @param s the sort to compare to
280
   * @return true if the sorts are equal
281
   */
282
  bool operator==(const Sort& s) const;
283
284
  /**
285
   * Comparison for structural disequality.
286
   * @param s the sort to compare to
287
   * @return true if the sorts are not equal
288
   */
289
  bool operator!=(const Sort& s) const;
290
291
  /**
292
   * Comparison for ordering on sorts.
293
   * @param s the sort to compare to
294
   * @return true if this sort is less than s
295
   */
296
  bool operator<(const Sort& s) const;
297
298
  /**
299
   * Comparison for ordering on sorts.
300
   * @param s the sort to compare to
301
   * @return true if this sort is greater than s
302
   */
303
  bool operator>(const Sort& s) const;
304
305
  /**
306
   * Comparison for ordering on sorts.
307
   * @param s the sort to compare to
308
   * @return true if this sort is less than or equal to s
309
   */
310
  bool operator<=(const Sort& s) const;
311
312
  /**
313
   * Comparison for ordering on sorts.
314
   * @param s the sort to compare to
315
   * @return true if this sort is greater than or equal to s
316
   */
317
  bool operator>=(const Sort& s) const;
318
319
  /**
320
   * @return true if this Sort is a null sort.
321
   */
322
  bool isNull() const;
323
324
  /**
325
   * Is this a Boolean sort?
326
   * @return true if the sort is a Boolean sort
327
   */
328
  bool isBoolean() const;
329
330
  /**
331
   * Is this a integer sort?
332
   * @return true if the sort is a integer sort
333
   */
334
  bool isInteger() const;
335
336
  /**
337
   * Is this a real sort?
338
   * @return true if the sort is a real sort
339
   */
340
  bool isReal() const;
341
342
  /**
343
   * Is this a string sort?
344
   * @return true if the sort is the string sort
345
   */
346
  bool isString() const;
347
348
  /**
349
   * Is this a regexp sort?
350
   * @return true if the sort is the regexp sort
351
   */
352
  bool isRegExp() const;
353
354
  /**
355
   * Is this a rounding mode sort?
356
   * @return true if the sort is the rounding mode sort
357
   */
358
  bool isRoundingMode() const;
359
360
  /**
361
   * Is this a bit-vector sort?
362
   * @return true if the sort is a bit-vector sort
363
   */
364
  bool isBitVector() const;
365
366
  /**
367
   * Is this a floating-point sort?
368
   * @return true if the sort is a floating-point sort
369
   */
370
  bool isFloatingPoint() const;
371
372
  /**
373
   * Is this a datatype sort?
374
   * @return true if the sort is a datatype sort
375
   */
376
  bool isDatatype() const;
377
378
  /**
379
   * Is this a parametric datatype sort?
380
   * @return true if the sort is a parametric datatype sort
381
   */
382
  bool isParametricDatatype() const;
383
384
  /**
385
   * Is this a constructor sort?
386
   * @return true if the sort is a constructor sort
387
   */
388
  bool isConstructor() const;
389
390
  /**
391
   * Is this a selector sort?
392
   * @return true if the sort is a selector sort
393
   */
394
  bool isSelector() const;
395
396
  /**
397
   * Is this a tester sort?
398
   * @return true if the sort is a tester sort
399
   */
400
  bool isTester() const;
401
  /**
402
   * Is this a datatype updater sort?
403
   * @return true if the sort is a datatype updater sort
404
   */
405
  bool isUpdater() const;
406
  /**
407
   * Is this a function sort?
408
   * @return true if the sort is a function sort
409
   */
410
  bool isFunction() const;
411
412
  /**
413
   * Is this a predicate sort?
414
   * That is, is this a function sort mapping to Boolean? All predicate
415
   * sorts are also function sorts.
416
   * @return true if the sort is a predicate sort
417
   */
418
  bool isPredicate() const;
419
420
  /**
421
   * Is this a tuple sort?
422
   * @return true if the sort is a tuple sort
423
   */
424
  bool isTuple() const;
425
426
  /**
427
   * Is this a record sort?
428
   * @return true if the sort is a record sort
429
   */
430
  bool isRecord() const;
431
432
  /**
433
   * Is this an array sort?
434
   * @return true if the sort is a array sort
435
   */
436
  bool isArray() const;
437
438
  /**
439
   * Is this a Set sort?
440
   * @return true if the sort is a Set sort
441
   */
442
  bool isSet() const;
443
444
  /**
445
   * Is this a Bag sort?
446
   * @return true if the sort is a Bag sort
447
   */
448
  bool isBag() const;
449
450
  /**
451
   * Is this a Sequence sort?
452
   * @return true if the sort is a Sequence sort
453
   */
454
  bool isSequence() const;
455
456
  /**
457
   * Is this a sort kind?
458
   * @return true if this is a sort kind
459
   */
460
  bool isUninterpretedSort() const;
461
462
  /**
463
   * Is this a sort constructor kind?
464
   * @return true if this is a sort constructor kind
465
   */
466
  bool isSortConstructor() const;
467
468
  /**
469
   * Is this a first-class sort?
470
   * First-class sorts are sorts for which:
471
   * (1) we handle equalities between terms of that type, and
472
   * (2) they are allowed to be parameters of parametric sorts (e.g. index or
473
   *     element sorts of arrays).
474
   *
475
   * Examples of sorts that are not first-class include sort constructor sorts
476
   * and regular expression sorts.
477
   *
478
   * @return true if this is a first-class sort
479
   */
480
  bool isFirstClass() const;
481
482
  /**
483
   * Is this a function-LIKE sort?
484
   *
485
   * Anything function-like except arrays (e.g., datatype selectors) is
486
   * considered a function here. Function-like terms can not be the argument
487
   * or return value for any term that is function-like.
488
   * This is mainly to avoid higher order.
489
   *
490
   * Note that arrays are explicitly not considered function-like here.
491
   *
492
   * @return true if this is a function-like sort
493
   */
494
  bool isFunctionLike() const;
495
496
  /**
497
   * Is this sort a subsort of the given sort?
498
   * @return true if this sort is a subsort of s
499
   */
500
  bool isSubsortOf(const Sort& s) const;
501
502
  /**
503
   * Is this sort comparable to the given sort (i.e., do they share
504
   * a common ancestor in the subsort tree)?
505
   * @return true if this sort is comparable to s
506
   */
507
  bool isComparableTo(const Sort& s) const;
508
509
  /**
510
   * @return the underlying datatype of a datatype sort
511
   */
512
  Datatype getDatatype() const;
513
514
  /**
515
   * Instantiate a parameterized datatype/sort sort.
516
   * Create sorts parameter with Solver::mkParamSort().
517
   * @param params the list of sort parameters to instantiate with
518
   */
519
  Sort instantiate(const std::vector<Sort>& params) const;
520
521
  /**
522
   * Substitution of Sorts.
523
   * @param sort the subsort to be substituted within this sort.
524
   * @param replacement the sort replacing the substituted subsort.
525
   */
526
  Sort substitute(const Sort& sort, const Sort& replacement) const;
527
528
  /**
529
   * Simultaneous substitution of Sorts.
530
   * @param sorts the subsorts to be substituted within this sort.
531
   * @param replacements the sort replacing the substituted subsorts.
532
   */
533
  Sort substitute(const std::vector<Sort>& sorts,
534
                  const std::vector<Sort>& replacements) const;
535
536
  /**
537
   * Output a string representation of this sort to a given stream.
538
   * @param out the output stream
539
   */
540
  void toStream(std::ostream& out) const;
541
542
  /**
543
   * @return a string representation of this sort
544
   */
545
  std::string toString() const;
546
547
  /* Constructor sort ------------------------------------------------------- */
548
549
  /**
550
   * @return the arity of a constructor sort
551
   */
552
  size_t getConstructorArity() const;
553
554
  /**
555
   * @return the domain sorts of a constructor sort
556
   */
557
  std::vector<Sort> getConstructorDomainSorts() const;
558
559
  /**
560
   * @return the codomain sort of a constructor sort
561
   */
562
  Sort getConstructorCodomainSort() const;
563
564
  /* Selector sort ------------------------------------------------------- */
565
566
  /**
567
   * @return the domain sort of a selector sort
568
   */
569
  Sort getSelectorDomainSort() const;
570
571
  /**
572
   * @return the codomain sort of a selector sort
573
   */
574
  Sort getSelectorCodomainSort() const;
575
576
  /* Tester sort ------------------------------------------------------- */
577
578
  /**
579
   * @return the domain sort of a tester sort
580
   */
581
  Sort getTesterDomainSort() const;
582
583
  /**
584
   * @return the codomain sort of a tester sort, which is the Boolean sort
585
   */
586
  Sort getTesterCodomainSort() const;
587
588
  /* Function sort ------------------------------------------------------- */
589
590
  /**
591
   * @return the arity of a function sort
592
   */
593
  size_t getFunctionArity() const;
594
595
  /**
596
   * @return the domain sorts of a function sort
597
   */
598
  std::vector<Sort> getFunctionDomainSorts() const;
599
600
  /**
601
   * @return the codomain sort of a function sort
602
   */
603
  Sort getFunctionCodomainSort() const;
604
605
  /* Array sort ---------------------------------------------------------- */
606
607
  /**
608
   * @return the array index sort of an array sort
609
   */
610
  Sort getArrayIndexSort() const;
611
612
  /**
613
   * @return the array element sort of an array element sort
614
   */
615
  Sort getArrayElementSort() const;
616
617
  /* Set sort ------------------------------------------------------------ */
618
619
  /**
620
   * @return the element sort of a set sort
621
   */
622
  Sort getSetElementSort() const;
623
624
  /* Bag sort ------------------------------------------------------------ */
625
626
  /**
627
   * @return the element sort of a bag sort
628
   */
629
  Sort getBagElementSort() const;
630
631
  /* Sequence sort ------------------------------------------------------- */
632
633
  /**
634
   * @return the element sort of a sequence sort
635
   */
636
  Sort getSequenceElementSort() const;
637
638
  /* Uninterpreted sort -------------------------------------------------- */
639
640
  /**
641
   * @return the name of an uninterpreted sort
642
   */
643
  std::string getUninterpretedSortName() const;
644
645
  /**
646
   * @return true if an uninterpreted sort is parameterezied
647
   */
648
  bool isUninterpretedSortParameterized() const;
649
650
  /**
651
   * @return the parameter sorts of an uninterpreted sort
652
   */
653
  std::vector<Sort> getUninterpretedSortParamSorts() const;
654
655
  /* Sort constructor sort ----------------------------------------------- */
656
657
  /**
658
   * @return the name of a sort constructor sort
659
   */
660
  std::string getSortConstructorName() const;
661
662
  /**
663
   * @return the arity of a sort constructor sort
664
   */
665
  size_t getSortConstructorArity() const;
666
667
  /* Bit-vector sort ----------------------------------------------------- */
668
669
  /**
670
   * @return the bit-width of the bit-vector sort
671
   */
672
  uint32_t getBVSize() const;
673
674
  /* Floating-point sort ------------------------------------------------- */
675
676
  /**
677
   * @return the bit-width of the exponent of the floating-point sort
678
   */
679
  uint32_t getFPExponentSize() const;
680
681
  /**
682
   * @return the width of the significand of the floating-point sort
683
   */
684
  uint32_t getFPSignificandSize() const;
685
686
  /* Datatype sort ------------------------------------------------------- */
687
688
  /**
689
   * @return the parameter sorts of a datatype sort
690
   */
691
  std::vector<Sort> getDatatypeParamSorts() const;
692
693
  /**
694
   * @return the arity of a datatype sort
695
   */
696
  size_t getDatatypeArity() const;
697
698
  /* Tuple sort ---------------------------------------------------------- */
699
700
  /**
701
   * @return the length of a tuple sort
702
   */
703
  size_t getTupleLength() const;
704
705
  /**
706
   * @return the element sorts of a tuple sort
707
   */
708
  std::vector<Sort> getTupleSorts() const;
709
710
 private:
711
  /** @return the internal wrapped TypeNode of this sort. */
712
  const cvc5::TypeNode& getTypeNode(void) const;
713
714
  /** Helper to convert a set of Sorts to internal TypeNodes. */
715
  std::set<TypeNode> static sortSetToTypeNodes(const std::set<Sort>& sorts);
716
  /** Helper to convert a vector of Sorts to internal TypeNodes. */
717
  std::vector<TypeNode> static sortVectorToTypeNodes(
718
      const std::vector<Sort>& sorts);
719
  /** Helper to convert a vector of internal TypeNodes to Sorts. */
720
  std::vector<Sort> static typeNodeVectorToSorts(
721
      const Solver* slv, const std::vector<TypeNode>& types);
722
723
  /**
724
   * Constructor.
725
   * @param slv the associated solver object
726
   * @param t the internal type that is to be wrapped by this sort
727
   * @return the Sort
728
   */
729
  Sort(const Solver* slv, const cvc5::TypeNode& t);
730
731
  /**
732
   * Helper for isNull checks. This prevents calling an API function with
733
   * CVC5_API_CHECK_NOT_NULL
734
   */
735
  bool isNullHelper() const;
736
737
  /**
738
   * The associated solver object.
739
   */
740
  const Solver* d_solver;
741
742
  /**
743
   * The interal type wrapped by this sort.
744
   * Note: This is a shared_ptr rather than a unique_ptr to avoid overhead due
745
   *       to memory allocation (cvc5::Type is already ref counted, so this
746
   *       could be a unique_ptr instead).
747
   */
748
  std::shared_ptr<cvc5::TypeNode> d_type;
749
};
750
751
/**
752
 * Serialize a sort to given stream.
753
 * @param out the output stream
754
 * @param s the sort to be serialized to the given output stream
755
 * @return the output stream
756
 */
757
std::ostream& operator<<(std::ostream& out, const Sort& s) CVC5_EXPORT;
758
759
}  // namespace api
760
}  // namespace cvc5
761
762
namespace std {
763
764
/**
765
 * Hash function for Sorts.
766
 */
767
template <>
768
struct CVC5_EXPORT hash<cvc5::api::Sort>
769
{
770
  size_t operator()(const cvc5::api::Sort& s) const;
771
};
772
773
}  // namespace std
774
775
namespace cvc5::api {
776
777
/* -------------------------------------------------------------------------- */
778
/* Op                                                                     */
779
/* -------------------------------------------------------------------------- */
780
781
/**
782
 * A cvc5 operator.
783
 * An operator is a term that represents certain operators, instantiated
784
 * with its required parameters, e.g., a term of kind BITVECTOR_EXTRACT.
785
 */
786
367943
class CVC5_EXPORT Op
787
{
788
  friend class Solver;
789
  friend class Term;
790
  friend struct std::hash<Op>;
791
792
 public:
793
  /**
794
   * Constructor.
795
   */
796
  Op();
797
798
  /**
799
   * Destructor.
800
   */
801
  ~Op();
802
803
  /**
804
   * Syntactic equality operator.
805
   * Return true if both operators are syntactically identical.
806
   * Both operators must belong to the same solver object.
807
   * @param t the operator to compare to for equality
808
   * @return true if the operators are equal
809
   */
810
  bool operator==(const Op& t) const;
811
812
  /**
813
   * Syntactic disequality operator.
814
   * Return true if both operators differ syntactically.
815
   * Both terms must belong to the same solver object.
816
   * @param t the operator to compare to for disequality
817
   * @return true if operators are disequal
818
   */
819
  bool operator!=(const Op& t) const;
820
821
  /**
822
   * @return the kind of this operator
823
   */
824
  Kind getKind() const;
825
826
  /**
827
   * @return true if this operator is a null term
828
   */
829
  bool isNull() const;
830
831
  /**
832
   * @return true iff this operator is indexed
833
   */
834
  bool isIndexed() const;
835
836
  /**
837
   * @return the number of indices of this op
838
   */
839
  size_t getNumIndices() const;
840
841
  /**
842
   * Get the indices used to create this Op.
843
   * Supports the following template arguments:
844
   *   - string
845
   *   - Kind
846
   *   - uint32_t
847
   *   - pair<uint32_t, uint32_t>
848
   * Check the Op Kind with getKind() to determine which argument to use.
849
   * @return the indices used to create this Op
850
   */
851
  template <typename T>
852
  T getIndices() const;
853
854
  /**
855
   * @return a string representation of this operator
856
   */
857
  std::string toString() const;
858
859
 private:
860
  /**
861
   * Constructor for a single kind (non-indexed operator).
862
   * @param slv the associated solver object
863
   * @param k the kind of this Op
864
   */
865
  Op(const Solver* slv, const Kind k);
866
867
  /**
868
   * Constructor.
869
   * @param slv the associated solver object
870
   * @param k the kind of this Op
871
   * @param n the internal node that is to be wrapped by this term
872
   * @return the Term
873
   */
874
  Op(const Solver* slv, const Kind k, const cvc5::Node& n);
875
876
  /**
877
   * Helper for isNull checks. This prevents calling an API function with
878
   * CVC5_API_CHECK_NOT_NULL
879
   */
880
  bool isNullHelper() const;
881
882
  /**
883
   * Note: An indexed operator has a non-null internal node, d_node
884
   * Note 2: We use a helper method to avoid having API functions call
885
   *         other API functions (we need to call this internally)
886
   * @return true iff this Op is indexed
887
   */
888
  bool isIndexedHelper() const;
889
890
  /**
891
   * The associated solver object.
892
   */
893
  const Solver* d_solver;
894
895
  /** The kind of this operator. */
896
  Kind d_kind;
897
898
  /**
899
   * The internal node wrapped by this operator.
900
   * Note: This is a shared_ptr rather than a unique_ptr to avoid overhead due
901
   *       to memory allocation (cvc5::Node is already ref counted, so this
902
   *       could be a unique_ptr instead).
903
   */
904
  std::shared_ptr<cvc5::Node> d_node;
905
};
906
907
/**
908
 * Serialize an operator to given stream.
909
 * @param out the output stream
910
 * @param t the operator to be serialized to the given output stream
911
 * @return the output stream
912
 */
913
std::ostream& operator<<(std::ostream& out, const Op& t) CVC5_EXPORT;
914
915
}  // namespace cvc5::api
916
917
namespace std {
918
/**
919
 * Hash function for Ops.
920
 */
921
template <>
922
struct CVC5_EXPORT hash<cvc5::api::Op>
923
{
924
  size_t operator()(const cvc5::api::Op& t) const;
925
};
926
}  // namespace std
927
928
namespace cvc5::api {
929
/* -------------------------------------------------------------------------- */
930
/* Term                                                                       */
931
/* -------------------------------------------------------------------------- */
932
933
/**
934
 * A cvc5 Term.
935
 */
936
51428503
class CVC5_EXPORT Term
937
{
938
  friend class cvc5::Command;
939
  friend class Datatype;
940
  friend class DatatypeConstructor;
941
  friend class DatatypeSelector;
942
  friend class Solver;
943
  friend class Grammar;
944
  friend struct std::hash<Term>;
945
946
 public:
947
  /**
948
   * Constructor for a null term.
949
   */
950
  Term();
951
952
  /**
953
   * Destructor.
954
   */
955
  ~Term();
956
957
  /**
958
   * Syntactic equality operator.
959
   * Return true if both terms are syntactically identical.
960
   * Both terms must belong to the same solver object.
961
   * @param t the term to compare to for equality
962
   * @return true if the terms are equal
963
   */
964
  bool operator==(const Term& t) const;
965
966
  /**
967
   * Syntactic disequality operator.
968
   * Return true if both terms differ syntactically.
969
   * Both terms must belong to the same solver object.
970
   * @param t the term to compare to for disequality
971
   * @return true if terms are disequal
972
   */
973
  bool operator!=(const Term& t) const;
974
975
  /**
976
   * Comparison for ordering on terms by their id.
977
   * @param t the term to compare to
978
   * @return true if this term is less than t
979
   */
980
  bool operator<(const Term& t) const;
981
982
  /**
983
   * Comparison for ordering on terms by their id.
984
   * @param t the term to compare to
985
   * @return true if this term is greater than t
986
   */
987
  bool operator>(const Term& t) const;
988
989
  /**
990
   * Comparison for ordering on terms by their id.
991
   * @param t the term to compare to
992
   * @return true if this term is less than or equal to t
993
   */
994
  bool operator<=(const Term& t) const;
995
996
  /**
997
   * Comparison for ordering on terms by their id.
998
   * @param t the term to compare to
999
   * @return true if this term is greater than or equal to t
1000
   */
1001
  bool operator>=(const Term& t) const;
1002
1003
  /** @return the number of children of this term  */
1004
  size_t getNumChildren() const;
1005
1006
  /**
1007
   * Get the child term at a given index.
1008
   * @param index the index of the child term to return
1009
   * @return the child term with the given index
1010
   */
1011
  Term operator[](size_t index) const;
1012
1013
  /**
1014
   * @return the id of this term
1015
   */
1016
  uint64_t getId() const;
1017
1018
  /**
1019
   * @return the kind of this term
1020
   */
1021
  Kind getKind() const;
1022
1023
  /**
1024
   * @return the sort of this term
1025
   */
1026
  Sort getSort() const;
1027
1028
  /**
1029
   * @return the result of replacing 'term' by 'replacement' in this term
1030
   */
1031
  Term substitute(const Term& term, const Term& replacement) const;
1032
1033
  /**
1034
   * @return the result of simulatenously replacing 'terms' by 'replacements'
1035
   * in this term
1036
   */
1037
  Term substitute(const std::vector<Term>& terms,
1038
                  const std::vector<Term>& replacements) const;
1039
1040
  /**
1041
   * @return true iff this term has an operator
1042
   */
1043
  bool hasOp() const;
1044
1045
  /**
1046
   * @return the Op used to create this term
1047
   * Note: This is safe to call when hasOp() returns true.
1048
   */
1049
  Op getOp() const;
1050
1051
  /**
1052
   * @return true if this Term is a null term
1053
   */
1054
  bool isNull() const;
1055
1056
  /**
1057
   * Boolean negation.
1058
   * @return the Boolean negation of this term
1059
   */
1060
  Term notTerm() const;
1061
1062
  /**
1063
   * Boolean and.
1064
   * @param t a Boolean term
1065
   * @return the conjunction of this term and the given term
1066
   */
1067
  Term andTerm(const Term& t) const;
1068
1069
  /**
1070
   * Boolean or.
1071
   * @param t a Boolean term
1072
   * @return the disjunction of this term and the given term
1073
   */
1074
  Term orTerm(const Term& t) const;
1075
1076
  /**
1077
   * Boolean exclusive or.
1078
   * @param t a Boolean term
1079
   * @return the exclusive disjunction of this term and the given term
1080
   */
1081
  Term xorTerm(const Term& t) const;
1082
1083
  /**
1084
   * Equality.
1085
   * @param t a Boolean term
1086
   * @return the Boolean equivalence of this term and the given term
1087
   */
1088
  Term eqTerm(const Term& t) const;
1089
1090
  /**
1091
   * Boolean implication.
1092
   * @param t a Boolean term
1093
   * @return the implication of this term and the given term
1094
   */
1095
  Term impTerm(const Term& t) const;
1096
1097
  /**
1098
   * If-then-else with this term as the Boolean condition.
1099
   * @param then_t the 'then' term
1100
   * @param else_t the 'else' term
1101
   * @return the if-then-else term with this term as the Boolean condition
1102
   */
1103
  Term iteTerm(const Term& then_t, const Term& else_t) const;
1104
1105
  /**
1106
   * @return a string representation of this term
1107
   */
1108
  std::string toString() const;
1109
1110
  /**
1111
   * Iterator for the children of a Term.
1112
   * Note: This treats uninterpreted functions as Term just like any other term
1113
   *       for example, the term f(x, y) will have Kind APPLY_UF and three
1114
   *       children: f, x, and y
1115
   */
1116
520
  class const_iterator : public std::iterator<std::input_iterator_tag, Term>
1117
  {
1118
    friend class Term;
1119
1120
   public:
1121
    /**
1122
     * Null Constructor.
1123
     */
1124
    const_iterator();
1125
1126
    /**
1127
     * Constructor
1128
     * @param slv the associated solver object
1129
     * @param e a shared pointer to the node that we're iterating over
1130
     * @param p the position of the iterator (e.g. which child it's on)
1131
     */
1132
    const_iterator(const Solver* slv,
1133
                   const std::shared_ptr<cvc5::Node>& e,
1134
                   uint32_t p);
1135
1136
    /**
1137
     * Copy constructor.
1138
     */
1139
    const_iterator(const const_iterator& it);
1140
1141
    /**
1142
     * Assignment operator.
1143
     * @param it the iterator to assign to
1144
     * @return the reference to the iterator after assignment
1145
     */
1146
    const_iterator& operator=(const const_iterator& it);
1147
1148
    /**
1149
     * Equality operator.
1150
     * @param it the iterator to compare to for equality
1151
     * @return true if the iterators are equal
1152
     */
1153
    bool operator==(const const_iterator& it) const;
1154
1155
    /**
1156
     * Disequality operator.
1157
     * @param it the iterator to compare to for disequality
1158
     * @return true if the iterators are disequal
1159
     */
1160
    bool operator!=(const const_iterator& it) const;
1161
1162
    /**
1163
     * Increment operator (prefix).
1164
     * @return a reference to the iterator after incrementing by one
1165
     */
1166
    const_iterator& operator++();
1167
1168
    /**
1169
     * Increment operator (postfix).
1170
     * @return a reference to the iterator after incrementing by one
1171
     */
1172
    const_iterator operator++(int);
1173
1174
    /**
1175
     * Dereference operator.
1176
     * @return the term this iterator points to
1177
     */
1178
    Term operator*() const;
1179
1180
   private:
1181
    /**
1182
     * The associated solver object.
1183
     */
1184
    const Solver* d_solver;
1185
    /** The original node to be iterated over. */
1186
    std::shared_ptr<cvc5::Node> d_origNode;
1187
    /** Keeps track of the iteration position. */
1188
    uint32_t d_pos;
1189
  };
1190
1191
  /**
1192
   * @return an iterator to the first child of this Term
1193
   */
1194
  const_iterator begin() const;
1195
1196
  /**
1197
   * @return an iterator to one-off-the-last child of this Term
1198
   */
1199
  const_iterator end() const;
1200
1201
  /**
1202
   * @return true if the term is an integer value that fits within int32_t.
1203
   */
1204
  bool isInt32Value() const;
1205
  /**
1206
   * Asserts isInt32Value().
1207
   * @return the integer term as a int32_t.
1208
   */
1209
  int32_t getInt32Value() const;
1210
  /**
1211
   * @return true if the term is an integer value that fits within uint32_t.
1212
   */
1213
  bool isUInt32Value() const;
1214
  /**
1215
   * Asserts isUInt32Value().
1216
   * @return the integer term as a uint32_t.
1217
   */
1218
  uint32_t getUInt32Value() const;
1219
  /**
1220
   * @return true if the term is an integer value that fits within int64_t.
1221
   */
1222
  bool isInt64Value() const;
1223
  /**
1224
   * Asserts isInt64Value().
1225
   * @return the integer term as a int64_t.
1226
   */
1227
  int64_t getInt64Value() const;
1228
  /**
1229
   * @return true if the term is an integer value that fits within uint64_t.
1230
   */
1231
  bool isUInt64Value() const;
1232
  /**
1233
   * Asserts isUInt64Value().
1234
   * @return the integer term as a uint64_t.
1235
   */
1236
  uint64_t getUInt64Value() const;
1237
  /**
1238
   * @return true if the term is an integer value.
1239
   */
1240
  bool isIntegerValue() const;
1241
  /**
1242
   * Asserts isIntegerValue().
1243
   * @return the integer term in (decimal) string representation.
1244
   */
1245
  std::string getIntegerValue() const;
1246
1247
  /**
1248
   * @return true if the term is a string value.
1249
   */
1250
  bool isStringValue() const;
1251
  /**
1252
   * Note: This method is not to be confused with toString() which returns
1253
   * the term in some string representation, whatever data it may hold. Asserts
1254
   * isStringValue().
1255
   * @return the string term as a native string value.
1256
   */
1257
  std::wstring getStringValue() const;
1258
1259
  /**
1260
   * @return true if the term is a rational value whose numerator and
1261
   * denominator fit within int32_t and uint32_t, respectively.
1262
   */
1263
  bool isReal32Value() const;
1264
  /**
1265
   * Asserts isReal32Value().
1266
   * @return the representation of a rational value as a pair of its numerator
1267
   * and denominator.
1268
   */
1269
  std::pair<int32_t, uint32_t> getReal32Value() const;
1270
  /**
1271
   * @return true if the term is a rational value whose numerator and
1272
   * denominator fit within int64_t and uint64_t, respectively.
1273
   */
1274
  bool isReal64Value() const;
1275
  /**
1276
   * Asserts isReal64Value().
1277
   * @return the representation of a rational value as a pair of its numerator
1278
   * and denominator.
1279
   */
1280
  std::pair<int64_t, uint64_t> getReal64Value() const;
1281
  /**
1282
   * @return true if the term is a rational value.
1283
   */
1284
  bool isRealValue() const;
1285
  /**
1286
   * Asserts isRealValue().
1287
   * @return the representation of a rational value as a (decimal) string.
1288
   */
1289
  std::string getRealValue() const;
1290
1291
  /**
1292
   * @return true if the term is a constant array.
1293
   */
1294
  bool isConstArray() const;
1295
  /**
1296
   * Asserts isConstArray().
1297
   * @return the base (element stored at all indices) of a constant array
1298
   */
1299
  Term getConstArrayBase() const;
1300
1301
  /**
1302
   * @return true if the term is a Boolean value.
1303
   */
1304
  bool isBooleanValue() const;
1305
  /**
1306
   * Asserts isBooleanValue().
1307
   * @return the representation of a Boolean value as a native Boolean value.
1308
   */
1309
  bool getBooleanValue() const;
1310
1311
  /**
1312
   * @return true if the term is a bit-vector value.
1313
   */
1314
  bool isBitVectorValue() const;
1315
  /**
1316
   * Asserts isBitVectorValue().
1317
   * @return the representation of a bit-vector value in string representation.
1318
   * Supported bases are 2 (bit string), 10 (decimal string) or 16 (hexadecimal
1319
   * string).
1320
   */
1321
  std::string getBitVectorValue(uint32_t base = 2) const;
1322
1323
  /**
1324
   * @return true if the term is an abstract value.
1325
   */
1326
  bool isAbstractValue() const;
1327
  /**
1328
   * Asserts isAbstractValue().
1329
   * @return the representation of an abstract value as a string.
1330
   */
1331
  std::string getAbstractValue() const;
1332
1333
  /**
1334
   * @return true if the term is a tuple value.
1335
   */
1336
  bool isTupleValue() const;
1337
  /**
1338
   * Asserts isTupleValue().
1339
   * @return the representation of a tuple value as a vector of terms.
1340
   */
1341
  std::vector<Term> getTupleValue() const;
1342
1343
  /**
1344
   * @return true if the term is the floating-point value for positive zero.
1345
   */
1346
  bool isFloatingPointPosZero() const;
1347
  /**
1348
   * @return true if the term is the floating-point value for negative zero.
1349
   */
1350
  bool isFloatingPointNegZero() const;
1351
  /**
1352
   * @return true if the term is the floating-point value for positive
1353
   * infinity.
1354
   */
1355
  bool isFloatingPointPosInf() const;
1356
  /**
1357
   * @return true if the term is the floating-point value for negative
1358
   * infinity.
1359
   */
1360
  bool isFloatingPointNegInf() const;
1361
  /**
1362
   * @return true if the term is the floating-point value for not a number.
1363
   */
1364
  bool isFloatingPointNaN() const;
1365
  /**
1366
   * @return true if the term is a floating-point value.
1367
   */
1368
  bool isFloatingPointValue() const;
1369
  /**
1370
   * Asserts isFloatingPointValue().
1371
   * @return the representation of a floating-point value as a tuple of the
1372
   * exponent width, the significand width and a bit-vector value.
1373
   */
1374
  std::tuple<uint32_t, uint32_t, Term> getFloatingPointValue() const;
1375
1376
  /**
1377
   * @return true if the term is a set value.
1378
   */
1379
  bool isSetValue() const;
1380
  /**
1381
   * Asserts isSetValue().
1382
   * @return the representation of a set value as a set of terms.
1383
   */
1384
  std::set<Term> getSetValue() const;
1385
1386
  /**
1387
   * @return true if the term is a sequence value.
1388
   */
1389
  bool isSequenceValue() const;
1390
  /**
1391
   * Asserts isSequenceValue().
1392
   * Note that it is usually necessary for sequences to call
1393
   * `Solver::simplify()` to turn a sequence that is constructed by, e.g.,
1394
   * concatenation of unit sequences, into a sequence value.
1395
   * @return the representation of a sequence value as a vector of terms.
1396
   */
1397
  std::vector<Term> getSequenceValue() const;
1398
1399
  /**
1400
   * @return true if the term is a value from an uninterpreted sort.
1401
   */
1402
  bool isUninterpretedValue() const;
1403
  /**
1404
  bool @return() const;
1405
   * Asserts isUninterpretedValue().
1406
   * @return the representation of an uninterpreted value as a pair of its
1407
  sort and its
1408
   * index.
1409
   */
1410
  std::pair<Sort, int32_t> getUninterpretedValue() const;
1411
1412
 protected:
1413
  /**
1414
   * The associated solver object.
1415
   */
1416
  const Solver* d_solver;
1417
1418
 private:
1419
  /** Helper to convert a vector of Terms to internal Nodes. */
1420
  std::vector<Node> static termVectorToNodes(const std::vector<Term>& terms);
1421
1422
  /** Helper method to collect all elements of a set. */
1423
  static void collectSet(std::set<Term>& set,
1424
                         const cvc5::Node& node,
1425
                         const Solver* slv);
1426
  /** Helper method to collect all elements of a sequence. */
1427
  static void collectSequence(std::vector<Term>& seq,
1428
                              const cvc5::Node& node,
1429
                              const Solver* slv);
1430
1431
  /**
1432
   * Constructor.
1433
   * @param slv the associated solver object
1434
   * @param n the internal node that is to be wrapped by this term
1435
   * @return the Term
1436
   */
1437
  Term(const Solver* slv, const cvc5::Node& n);
1438
1439
  /** @return the internal wrapped Node of this term. */
1440
  const cvc5::Node& getNode(void) const;
1441
1442
  /**
1443
   * Helper for isNull checks. This prevents calling an API function with
1444
   * CVC5_API_CHECK_NOT_NULL
1445
   */
1446
  bool isNullHelper() const;
1447
1448
  /**
1449
   * Helper function that returns the kind of the term, which takes into
1450
   * account special cases of the conversion for internal to external kinds.
1451
   * @return the kind of this term
1452
   */
1453
  Kind getKindHelper() const;
1454
1455
  /**
1456
   * @return true if the current term is a constant integer that is casted into
1457
   * real using the operator CAST_TO_REAL, and returns false otherwise
1458
   */
1459
  bool isCastedReal() const;
1460
  /**
1461
   * The internal node wrapped by this term.
1462
   * Note: This is a shared_ptr rather than a unique_ptr to avoid overhead due
1463
   *       to memory allocation (cvc5::Node is already ref counted, so this
1464
   *       could be a unique_ptr instead).
1465
   */
1466
  std::shared_ptr<cvc5::Node> d_node;
1467
};
1468
1469
/**
1470
 * Serialize a term to given stream.
1471
 * @param out the output stream
1472
 * @param t the term to be serialized to the given output stream
1473
 * @return the output stream
1474
 */
1475
std::ostream& operator<<(std::ostream& out, const Term& t) CVC5_EXPORT;
1476
1477
/**
1478
 * Serialize a vector of terms to given stream.
1479
 * @param out the output stream
1480
 * @param vector the vector of terms to be serialized to the given stream
1481
 * @return the output stream
1482
 */
1483
std::ostream& operator<<(std::ostream& out,
1484
                         const std::vector<Term>& vector) CVC5_EXPORT;
1485
1486
/**
1487
 * Serialize a set of terms to the given stream.
1488
 * @param out the output stream
1489
 * @param set the set of terms to be serialized to the given stream
1490
 * @return the output stream
1491
 */
1492
std::ostream& operator<<(std::ostream& out,
1493
                         const std::set<Term>& set) CVC5_EXPORT;
1494
1495
/**
1496
 * Serialize an unordered_set of terms to the given stream.
1497
 *
1498
 * @param out the output stream
1499
 * @param unordered_set the set of terms to be serialized to the given stream
1500
 * @return the output stream
1501
 */
1502
std::ostream& operator<<(std::ostream& out,
1503
                         const std::unordered_set<Term>& unordered_set)
1504
    CVC5_EXPORT;
1505
1506
/**
1507
 * Serialize a map of terms to the given stream.
1508
 *
1509
 * @param out the output stream
1510
 * @param map the map of terms to be serialized to the given stream
1511
 * @return the output stream
1512
 */
1513
template <typename V>
1514
std::ostream& operator<<(std::ostream& out,
1515
                         const std::map<Term, V>& map) CVC5_EXPORT;
1516
1517
/**
1518
 * Serialize an unordered_map of terms to the given stream.
1519
 *
1520
 * @param out the output stream
1521
 * @param unordered_map the map of terms to be serialized to the given stream
1522
 * @return the output stream
1523
 */
1524
template <typename V>
1525
std::ostream& operator<<(std::ostream& out,
1526
                         const std::unordered_map<Term, V>& unordered_map)
1527
    CVC5_EXPORT;
1528
1529
}  // namespace cvc5::api
1530
1531
namespace std {
1532
/**
1533
 * Hash function for Terms.
1534
 */
1535
template <>
1536
struct CVC5_EXPORT hash<cvc5::api::Term>
1537
{
1538
  size_t operator()(const cvc5::api::Term& t) const;
1539
};
1540
}  // namespace std
1541
1542
namespace cvc5::api {
1543
1544
/* -------------------------------------------------------------------------- */
1545
/* Datatypes                                                                  */
1546
/* -------------------------------------------------------------------------- */
1547
1548
class DatatypeConstructorIterator;
1549
class DatatypeIterator;
1550
1551
/**
1552
 * A cvc5 datatype constructor declaration.
1553
 */
1554
20
class CVC5_EXPORT DatatypeConstructorDecl
1555
{
1556
  friend class DatatypeDecl;
1557
  friend class Solver;
1558
1559
 public:
1560
  /** Constructor.  */
1561
  DatatypeConstructorDecl();
1562
1563
  /**
1564
   * Destructor.
1565
   */
1566
  ~DatatypeConstructorDecl();
1567
1568
  /**
1569
   * Add datatype selector declaration.
1570
   * @param name the name of the datatype selector declaration to add
1571
   * @param sort the range sort of the datatype selector declaration to add
1572
   */
1573
  void addSelector(const std::string& name, const Sort& sort);
1574
  /**
1575
   * Add datatype selector declaration whose range type is the datatype itself.
1576
   * @param name the name of the datatype selector declaration to add
1577
   */
1578
  void addSelectorSelf(const std::string& name);
1579
1580
  /**
1581
   * @return true if this DatatypeConstructorDecl is a null declaration.
1582
   */
1583
  bool isNull() const;
1584
1585
  /**
1586
   * @return a string representation of this datatype constructor declaration
1587
   */
1588
  std::string toString() const;
1589
1590
 private:
1591
  /**
1592
   * Constructor.
1593
   * @param slv the associated solver object
1594
   * @param name the name of the datatype constructor
1595
   * @return the DatatypeConstructorDecl
1596
   */
1597
  DatatypeConstructorDecl(const Solver* slv, const std::string& name);
1598
1599
  /**
1600
   * Helper for isNull checks. This prevents calling an API function with
1601
   * CVC5_API_CHECK_NOT_NULL
1602
   */
1603
  bool isNullHelper() const;
1604
1605
  /**
1606
   * The associated solver object.
1607
   */
1608
  const Solver* d_solver;
1609
1610
  /**
1611
   * The internal (intermediate) datatype constructor wrapped by this
1612
   * datatype constructor declaration.
1613
   * Note: This is a shared_ptr rather than a unique_ptr since
1614
   *       cvc5::DTypeConstructor is not ref counted.
1615
   */
1616
  std::shared_ptr<cvc5::DTypeConstructor> d_ctor;
1617
};
1618
1619
class Solver;
1620
1621
/**
1622
 * A cvc5 datatype declaration.
1623
 */
1624
1736
class CVC5_EXPORT DatatypeDecl
1625
{
1626
  friend class DatatypeConstructorArg;
1627
  friend class Solver;
1628
  friend class Grammar;
1629
1630
 public:
1631
  /** Constructor.  */
1632
  DatatypeDecl();
1633
1634
  /**
1635
   * Destructor.
1636
   */
1637
  ~DatatypeDecl();
1638
1639
  /**
1640
   * Add datatype constructor declaration.
1641
   * @param ctor the datatype constructor declaration to add
1642
   */
1643
  void addConstructor(const DatatypeConstructorDecl& ctor);
1644
1645
  /** Get the number of constructors (so far) for this Datatype declaration. */
1646
  size_t getNumConstructors() const;
1647
1648
  /** Is this Datatype declaration parametric? */
1649
  bool isParametric() const;
1650
1651
  /**
1652
   * @return true if this DatatypeDecl is a null object
1653
   */
1654
  bool isNull() const;
1655
1656
  /**
1657
   * @return a string representation of this datatype declaration
1658
   */
1659
  std::string toString() const;
1660
1661
  /** @return the name of this datatype declaration. */
1662
  std::string getName() const;
1663
1664
 private:
1665
  /**
1666
   * Constructor.
1667
   * @param slv the associated solver object
1668
   * @param name the name of the datatype
1669
   * @param isCoDatatype true if a codatatype is to be constructed
1670
   * @return the DatatypeDecl
1671
   */
1672
  DatatypeDecl(const Solver* slv,
1673
               const std::string& name,
1674
               bool isCoDatatype = false);
1675
1676
  /**
1677
   * Constructor for parameterized datatype declaration.
1678
   * Create sorts parameter with Solver::mkParamSort().
1679
   * @param slv the associated solver object
1680
   * @param name the name of the datatype
1681
   * @param param the sort parameter
1682
   * @param isCoDatatype true if a codatatype is to be constructed
1683
   */
1684
  DatatypeDecl(const Solver* slv,
1685
               const std::string& name,
1686
               const Sort& param,
1687
               bool isCoDatatype = false);
1688
1689
  /**
1690
   * Constructor for parameterized datatype declaration.
1691
   * Create sorts parameter with Solver::mkParamSort().
1692
   * @param slv the associated solver object
1693
   * @param name the name of the datatype
1694
   * @param params a list of sort parameters
1695
   * @param isCoDatatype true if a codatatype is to be constructed
1696
   */
1697
  DatatypeDecl(const Solver* slv,
1698
               const std::string& name,
1699
               const std::vector<Sort>& params,
1700
               bool isCoDatatype = false);
1701
1702
  /** @return the internal wrapped Dtype of this datatype declaration. */
1703
  cvc5::DType& getDatatype(void) const;
1704
1705
  /**
1706
   * Helper for isNull checks. This prevents calling an API function with
1707
   * CVC5_API_CHECK_NOT_NULL
1708
   */
1709
  bool isNullHelper() const;
1710
1711
  /**
1712
   * The associated solver object.
1713
   */
1714
  const Solver* d_solver;
1715
1716
  /**
1717
   * The internal (intermediate) datatype wrapped by this datatype
1718
   * declaration.
1719
   * Note: This is a shared_ptr rather than a unique_ptr since cvc5::DType is
1720
   *       not ref counted.
1721
   */
1722
  std::shared_ptr<cvc5::DType> d_dtype;
1723
};
1724
1725
/**
1726
 * A cvc5 datatype selector.
1727
 */
1728
class CVC5_EXPORT DatatypeSelector
1729
{
1730
  friend class Datatype;
1731
  friend class DatatypeConstructor;
1732
  friend class Solver;
1733
1734
 public:
1735
  /**
1736
   * Constructor.
1737
   */
1738
  DatatypeSelector();
1739
1740
  /**
1741
   * Destructor.
1742
   */
1743
  ~DatatypeSelector();
1744
1745
  /** @return the name of this Datatype selector. */
1746
  std::string getName() const;
1747
1748
  /**
1749
   * Get the selector operator of this datatype selector.
1750
   * @return the selector term
1751
   */
1752
  Term getSelectorTerm() const;
1753
1754
  /**
1755
   * Get the upater operator of this datatype selector.
1756
   * @return the updater term
1757
   */
1758
  Term getUpdaterTerm() const;
1759
1760
  /** @return the range sort of this argument. */
1761
  Sort getRangeSort() const;
1762
1763
  /**
1764
   * @return true if this DatatypeSelector is a null object
1765
   */
1766
  bool isNull() const;
1767
1768
  /**
1769
   * @return a string representation of this datatype selector
1770
   */
1771
  std::string toString() const;
1772
1773
 private:
1774
  /**
1775
   * Constructor.
1776
   * @param slv the associated solver object
1777
   * @param stor the internal datatype selector to be wrapped
1778
   * @return the DatatypeSelector
1779
   */
1780
  DatatypeSelector(const Solver* slv, const cvc5::DTypeSelector& stor);
1781
1782
  /**
1783
   * Helper for isNull checks. This prevents calling an API function with
1784
   * CVC5_API_CHECK_NOT_NULL
1785
   */
1786
  bool isNullHelper() const;
1787
1788
  /**
1789
   * The associated solver object.
1790
   */
1791
  const Solver* d_solver;
1792
1793
  /**
1794
   * The internal datatype selector wrapped by this datatype selector.
1795
   * Note: This is a shared_ptr rather than a unique_ptr since cvc5::DType is
1796
   *       not ref counted.
1797
   */
1798
  std::shared_ptr<cvc5::DTypeSelector> d_stor;
1799
};
1800
1801
/**
1802
 * A cvc5 datatype constructor.
1803
 */
1804
class CVC5_EXPORT DatatypeConstructor
1805
{
1806
  friend class Datatype;
1807
  friend class Solver;
1808
1809
 public:
1810
  /**
1811
   * Constructor.
1812
   */
1813
  DatatypeConstructor();
1814
1815
  /**
1816
   * Destructor.
1817
   */
1818
  ~DatatypeConstructor();
1819
1820
  /** @return the name of this Datatype constructor. */
1821
  std::string getName() const;
1822
1823
  /**
1824
   * Get the constructor operator of this datatype constructor.
1825
   * @return the constructor term
1826
   */
1827
  Term getConstructorTerm() const;
1828
1829
  /**
1830
   * Get the constructor operator of this datatype constructor whose return
1831
   * type is retSort. This method is intended to be used on constructors of
1832
   * parametric datatypes and can be seen as returning the constructor
1833
   * term that has been explicitly cast to the given sort.
1834
   *
1835
   * This method is required for constructors of parametric datatypes whose
1836
   * return type cannot be determined by type inference. For example, given:
1837
   *   (declare-datatype List (par (T) ((nil) (cons (head T) (tail (List T))))))
1838
   * The type of nil terms need to be provided by the user. In SMT version 2.6,
1839
   * this is done via the syntax for qualified identifiers:
1840
   *   (as nil (List Int))
1841
   * This method is equivalent of applying the above, where this
1842
   * DatatypeConstructor is the one corresponding to nil, and retSort is
1843
   * (List Int).
1844
   *
1845
   * Furthermore note that the returned constructor term t is an operator,
1846
   * while Solver::mkTerm(APPLY_CONSTRUCTOR, t) is used to construct the above
1847
   * (nullary) application of nil.
1848
   *
1849
   * @param retSort the desired return sort of the constructor
1850
   * @return the constructor term
1851
   */
1852
  Term getSpecializedConstructorTerm(const Sort& retSort) const;
1853
1854
  /**
1855
   * Get the tester operator of this datatype constructor.
1856
   * @return the tester operator
1857
   */
1858
  Term getTesterTerm() const;
1859
1860
  /**
1861
   * @return the number of selectors (so far) of this Datatype constructor.
1862
   */
1863
  size_t getNumSelectors() const;
1864
1865
  /** @return the i^th DatatypeSelector. */
1866
  DatatypeSelector operator[](size_t index) const;
1867
  /**
1868
   * Get the datatype selector with the given name.
1869
   * This is a linear search through the selectors, so in case of
1870
   * multiple, similarly-named selectors, the first is returned.
1871
   * @param name the name of the datatype selector
1872
   * @return the first datatype selector with the given name
1873
   */
1874
  DatatypeSelector operator[](const std::string& name) const;
1875
  DatatypeSelector getSelector(const std::string& name) const;
1876
1877
  /**
1878
   * Get the term representation of the datatype selector with the given name.
1879
   * This is a linear search through the arguments, so in case of multiple,
1880
   * similarly-named arguments, the selector for the first is returned.
1881
   * @param name the name of the datatype selector
1882
   * @return a term representing the datatype selector with the given name
1883
   */
1884
  Term getSelectorTerm(const std::string& name) const;
1885
1886
  /**
1887
   * @return true if this DatatypeConstructor is a null object
1888
   */
1889
  bool isNull() const;
1890
1891
  /**
1892
   * @return a string representation of this datatype constructor
1893
   */
1894
  std::string toString() const;
1895
1896
  /**
1897
   * Iterator for the selectors of a datatype constructor.
1898
   */
1899
  class const_iterator
1900
      : public std::iterator<std::input_iterator_tag, DatatypeConstructor>
1901
  {
1902
    friend class DatatypeConstructor;  // to access constructor
1903
1904
   public:
1905
    /** Nullary constructor (required for Cython). */
1906
    const_iterator();
1907
1908
    /**
1909
     * Assignment operator.
1910
     * @param it the iterator to assign to
1911
     * @return the reference to the iterator after assignment
1912
     */
1913
    const_iterator& operator=(const const_iterator& it);
1914
1915
    /**
1916
     * Equality operator.
1917
     * @param it the iterator to compare to for equality
1918
     * @return true if the iterators are equal
1919
     */
1920
    bool operator==(const const_iterator& it) const;
1921
1922
    /**
1923
     * Disequality operator.
1924
     * @param it the iterator to compare to for disequality
1925
     * @return true if the iterators are disequal
1926
     */
1927
    bool operator!=(const const_iterator& it) const;
1928
1929
    /**
1930
     * Increment operator (prefix).
1931
     * @return a reference to the iterator after incrementing by one
1932
     */
1933
    const_iterator& operator++();
1934
1935
    /**
1936
     * Increment operator (postfix).
1937
     * @return a reference to the iterator after incrementing by one
1938
     */
1939
    const_iterator operator++(int);
1940
1941
    /**
1942
     * Dereference operator.
1943
     * @return a reference to the selector this iterator points to
1944
     */
1945
    const DatatypeSelector& operator*() const;
1946
1947
    /**
1948
     * Dereference operator.
1949
     * @return a pointer to the selector this iterator points to
1950
     */
1951
    const DatatypeSelector* operator->() const;
1952
1953
   private:
1954
    /**
1955
     * Constructor.
1956
     * @param slv the associated Solver object
1957
     * @param ctor the internal datatype constructor to iterate over
1958
     * @param begin true if this is a begin() iterator
1959
     */
1960
    const_iterator(const Solver* slv,
1961
                   const cvc5::DTypeConstructor& ctor,
1962
                   bool begin);
1963
1964
    /**
1965
     * The associated solver object.
1966
     */
1967
    const Solver* d_solver;
1968
1969
    /**
1970
     * A pointer to the list of selectors of the internal datatype
1971
     * constructor to iterate over.
1972
     * This pointer is maintained for operators == and != only.
1973
     */
1974
    const void* d_int_stors;
1975
1976
    /** The list of datatype selector (wrappers) to iterate over. */
1977
    std::vector<DatatypeSelector> d_stors;
1978
1979
    /** The current index of the iterator. */
1980
    size_t d_idx;
1981
  };
1982
1983
  /**
1984
   * @return an iterator to the first selector of this constructor
1985
   */
1986
  const_iterator begin() const;
1987
1988
  /**
1989
   * @return an iterator to one-off-the-last selector of this constructor
1990
   */
1991
  const_iterator end() const;
1992
1993
 private:
1994
  /**
1995
   * Constructor.
1996
   * @param slv the associated solver instance
1997
   * @param ctor the internal datatype constructor to be wrapped
1998
   * @return the DatatypeConstructor
1999
   */
2000
  DatatypeConstructor(const Solver* slv, const cvc5::DTypeConstructor& ctor);
2001
2002
  /**
2003
   * Return selector for name.
2004
   * @param name The name of selector to find
2005
   * @return the selector object for the name
2006
   */
2007
  DatatypeSelector getSelectorForName(const std::string& name) const;
2008
2009
  /**
2010
   * Helper for isNull checks. This prevents calling an API function with
2011
   * CVC5_API_CHECK_NOT_NULL
2012
   */
2013
  bool isNullHelper() const;
2014
2015
  /**
2016
   * The associated solver object.
2017
   */
2018
  const Solver* d_solver;
2019
2020
  /**
2021
   * The internal datatype constructor wrapped by this datatype constructor.
2022
   * Note: This is a shared_ptr rather than a unique_ptr since cvc5::DType is
2023
   *       not ref counted.
2024
   */
2025
  std::shared_ptr<cvc5::DTypeConstructor> d_ctor;
2026
};
2027
2028
/**
2029
 * A cvc5 datatype.
2030
 */
2031
class CVC5_EXPORT Datatype
2032
{
2033
  friend class Solver;
2034
  friend class Sort;
2035
2036
 public:
2037
  /** Constructor. */
2038
  Datatype();
2039
2040
  /**
2041
   * Destructor.
2042
   */
2043
  ~Datatype();
2044
2045
  /**
2046
   * Get the datatype constructor at a given index.
2047
   * @param idx the index of the datatype constructor to return
2048
   * @return the datatype constructor with the given index
2049
   */
2050
  DatatypeConstructor operator[](size_t idx) const;
2051
2052
  /**
2053
   * Get the datatype constructor with the given name.
2054
   * This is a linear search through the constructors, so in case of multiple,
2055
   * similarly-named constructors, the first is returned.
2056
   * @param name the name of the datatype constructor
2057
   * @return the datatype constructor with the given name
2058
   */
2059
  DatatypeConstructor operator[](const std::string& name) const;
2060
  DatatypeConstructor getConstructor(const std::string& name) const;
2061
2062
  /**
2063
   * Get a term representing the datatype constructor with the given name.
2064
   * This is a linear search through the constructors, so in case of multiple,
2065
   * similarly-named constructors, the
2066
   * first is returned.
2067
   */
2068
  Term getConstructorTerm(const std::string& name) const;
2069
2070
  /**
2071
   * Get the datatype constructor with the given name.
2072
   * This is a linear search through the constructors and their selectors, so
2073
   * in case of multiple, similarly-named selectors, the first is returned.
2074
   * @param name the name of the datatype selector
2075
   * @return the datatype selector with the given name
2076
   */
2077
  DatatypeSelector getSelector(const std::string& name) const;
2078
2079
  /** @return the name of this Datatype. */
2080
  std::string getName() const;
2081
2082
  /** @return the number of constructors for this Datatype. */
2083
  size_t getNumConstructors() const;
2084
2085
  /** @return true if this datatype is parametric */
2086
  bool isParametric() const;
2087
2088
  /** @return true if this datatype corresponds to a co-datatype */
2089
  bool isCodatatype() const;
2090
2091
  /** @return true if this datatype corresponds to a tuple */
2092
  bool isTuple() const;
2093
2094
  /** @return true if this datatype corresponds to a record */
2095
  bool isRecord() const;
2096
2097
  /** @return true if this datatype is finite */
2098
  bool isFinite() const;
2099
2100
  /**
2101
   * Is this datatype well-founded? If this datatype is not a codatatype,
2102
   * this returns false if there are no values of this datatype that are of
2103
   * finite size.
2104
   *
2105
   * @return true if this datatype is well-founded
2106
   */
2107
  bool isWellFounded() const;
2108
2109
  /**
2110
   * Does this datatype have nested recursion? This method returns false if a
2111
   * value of this datatype includes a subterm of its type that is nested
2112
   * beneath a non-datatype type constructor. For example, a datatype
2113
   * T containing a constructor having a selector with range type (Set T) has
2114
   * nested recursion.
2115
   *
2116
   * @return true if this datatype has nested recursion
2117
   */
2118
  bool hasNestedRecursion() const;
2119
2120
  /**
2121
   * @return true if this Datatype is a null object
2122
   */
2123
  bool isNull() const;
2124
2125
  /**
2126
   * @return a string representation of this datatype
2127
   */
2128
  std::string toString() const;
2129
2130
  /**
2131
   * Iterator for the constructors of a datatype.
2132
   */
2133
  class const_iterator : public std::iterator<std::input_iterator_tag, Datatype>
2134
  {
2135
    friend class Datatype;  // to access constructor
2136
2137
   public:
2138
    /** Nullary constructor (required for Cython). */
2139
    const_iterator();
2140
2141
    /**
2142
     * Assignment operator.
2143
     * @param it the iterator to assign to
2144
     * @return the reference to the iterator after assignment
2145
     */
2146
    const_iterator& operator=(const const_iterator& it);
2147
2148
    /**
2149
     * Equality operator.
2150
     * @param it the iterator to compare to for equality
2151
     * @return true if the iterators are equal
2152
     */
2153
    bool operator==(const const_iterator& it) const;
2154
2155
    /**
2156
     * Disequality operator.
2157
     * @param it the iterator to compare to for disequality
2158
     * @return true if the iterators are disequal
2159
     */
2160
    bool operator!=(const const_iterator& it) const;
2161
2162
    /**
2163
     * Increment operator (prefix).
2164
     * @return a reference to the iterator after incrementing by one
2165
     */
2166
    const_iterator& operator++();
2167
2168
    /**
2169
     * Increment operator (postfix).
2170
     * @return a reference to the iterator after incrementing by one
2171
     */
2172
    const_iterator operator++(int);
2173
2174
    /**
2175
     * Dereference operator.
2176
     * @return a reference to the constructor this iterator points to
2177
     */
2178
    const DatatypeConstructor& operator*() const;
2179
2180
    /**
2181
     * Dereference operator.
2182
     * @return a pointer to the constructor this iterator points to
2183
     */
2184
    const DatatypeConstructor* operator->() const;
2185
2186
   private:
2187
    /**
2188
     * Constructor.
2189
     * @param slv the associated Solver object
2190
     * @param dtype the internal datatype to iterate over
2191
     * @param begin true if this is a begin() iterator
2192
     */
2193
    const_iterator(const Solver* slv, const cvc5::DType& dtype, bool begin);
2194
2195
    /**
2196
     * The associated solver object.
2197
     */
2198
    const Solver* d_solver;
2199
2200
    /**
2201
     * A pointer to the list of constructors of the internal datatype
2202
     * to iterate over.
2203
     * This pointer is maintained for operators == and != only.
2204
     */
2205
    const void* d_int_ctors;
2206
2207
    /** The list of datatype constructor (wrappers) to iterate over. */
2208
    std::vector<DatatypeConstructor> d_ctors;
2209
2210
    /** The current index of the iterator. */
2211
    size_t d_idx;
2212
  };
2213
2214
  /**
2215
   * @return an iterator to the first constructor of this datatype
2216
   */
2217
  const_iterator begin() const;
2218
2219
  /**
2220
   * @return an iterator to one-off-the-last constructor of this datatype
2221
   */
2222
  const_iterator end() const;
2223
2224
 private:
2225
  /**
2226
   * Constructor.
2227
   * @param slv the associated solver instance
2228
   * @param dtype the internal datatype to be wrapped
2229
   * @return the Datatype
2230
   */
2231
  Datatype(const Solver* slv, const cvc5::DType& dtype);
2232
2233
  /**
2234
   * Return constructor for name.
2235
   * @param name The name of constructor to find
2236
   * @return the constructor object for the name
2237
   */
2238
  DatatypeConstructor getConstructorForName(const std::string& name) const;
2239
2240
  /**
2241
   * Return selector for name.
2242
   * @param name The name of selector to find
2243
   * @return the selector object for the name
2244
   */
2245
  DatatypeSelector getSelectorForName(const std::string& name) const;
2246
2247
  /**
2248
   * Helper for isNull checks. This prevents calling an API function with
2249
   * CVC5_API_CHECK_NOT_NULL
2250
   */
2251
  bool isNullHelper() const;
2252
2253
  /**
2254
   * The associated solver object.
2255
   */
2256
  const Solver* d_solver;
2257
2258
  /**
2259
   * The internal datatype wrapped by this datatype.
2260
   * Note: This is a shared_ptr rather than a unique_ptr since cvc5::DType is
2261
   *       not ref counted.
2262
   */
2263
  std::shared_ptr<cvc5::DType> d_dtype;
2264
};
2265
2266
/**
2267
 * Serialize a datatype declaration to given stream.
2268
 * @param out the output stream
2269
 * @param dtdecl the datatype declaration to be serialized to the given stream
2270
 * @return the output stream
2271
 */
2272
std::ostream& operator<<(std::ostream& out,
2273
                         const DatatypeDecl& dtdecl) CVC5_EXPORT;
2274
2275
/**
2276
 * Serialize a datatype constructor declaration to given stream.
2277
 * @param out the output stream
2278
 * @param ctordecl the datatype constructor declaration to be serialized
2279
 * @return the output stream
2280
 */
2281
std::ostream& operator<<(std::ostream& out,
2282
                         const DatatypeConstructorDecl& ctordecl) CVC5_EXPORT;
2283
2284
/**
2285
 * Serialize a vector of datatype constructor declarations to given stream.
2286
 * @param out the output stream
2287
 * @param vector the vector of datatype constructor declarations to be
2288
 * serialized to the given stream
2289
 * @return the output stream
2290
 */
2291
std::ostream& operator<<(std::ostream& out,
2292
                         const std::vector<DatatypeConstructorDecl>& vector)
2293
    CVC5_EXPORT;
2294
2295
/**
2296
 * Serialize a datatype to given stream.
2297
 * @param out the output stream
2298
 * @param dtype the datatype to be serialized to given stream
2299
 * @return the output stream
2300
 */
2301
std::ostream& operator<<(std::ostream& out, const Datatype& dtype) CVC5_EXPORT;
2302
2303
/**
2304
 * Serialize a datatype constructor to given stream.
2305
 * @param out the output stream
2306
 * @param ctor the datatype constructor to be serialized to given stream
2307
 * @return the output stream
2308
 */
2309
std::ostream& operator<<(std::ostream& out,
2310
                         const DatatypeConstructor& ctor) CVC5_EXPORT;
2311
2312
/**
2313
 * Serialize a datatype selector to given stream.
2314
 * @param out the output stream
2315
 * @param stor the datatype selector to be serialized to given stream
2316
 * @return the output stream
2317
 */
2318
std::ostream& operator<<(std::ostream& out,
2319
                         const DatatypeSelector& stor) CVC5_EXPORT;
2320
2321
/* -------------------------------------------------------------------------- */
2322
/* Grammar                                                                    */
2323
/* -------------------------------------------------------------------------- */
2324
2325
/**
2326
 * A Sygus Grammar.
2327
 */
2328
177
class CVC5_EXPORT Grammar
2329
{
2330
  friend class cvc5::Command;
2331
  friend class Solver;
2332
2333
 public:
2334
  /**
2335
   * Add \p rule to the set of rules corresponding to \p ntSymbol.
2336
   * @param ntSymbol the non-terminal to which the rule is added
2337
   * @param rule the rule to add
2338
   */
2339
  void addRule(const Term& ntSymbol, const Term& rule);
2340
2341
  /**
2342
   * Add \p rules to the set of rules corresponding to \p ntSymbol.
2343
   * @param ntSymbol the non-terminal to which the rules are added
2344
   * @param rules the rules to add
2345
   */
2346
  void addRules(const Term& ntSymbol, const std::vector<Term>& rules);
2347
2348
  /**
2349
   * Allow \p ntSymbol to be an arbitrary constant.
2350
   * @param ntSymbol the non-terminal allowed to be any constant
2351
   */
2352
  void addAnyConstant(const Term& ntSymbol);
2353
2354
  /**
2355
   * Allow \p ntSymbol to be any input variable to corresponding
2356
   * synth-fun/synth-inv with the same sort as \p ntSymbol.
2357
   * @param ntSymbol the non-terminal allowed to be any input constant
2358
   */
2359
  void addAnyVariable(const Term& ntSymbol);
2360
2361
  /**
2362
   * @return a string representation of this grammar.
2363
   */
2364
  std::string toString() const;
2365
2366
  /**
2367
   * Nullary constructor. Needed for the Cython API.
2368
   */
2369
  Grammar();
2370
2371
 private:
2372
  /**
2373
   * Constructor.
2374
   * @param slv the solver that created this grammar
2375
   * @param sygusVars the input variables to synth-fun/synth-var
2376
   * @param ntSymbols the non-terminals of this grammar
2377
   */
2378
  Grammar(const Solver* slv,
2379
          const std::vector<Term>& sygusVars,
2380
          const std::vector<Term>& ntSymbols);
2381
2382
  /**
2383
   * @return the resolved datatype of the Start symbol of the grammar
2384
   */
2385
  Sort resolve();
2386
2387
  /**
2388
   * Adds a constructor to sygus datatype <dt> whose sygus operator is <term>.
2389
   *
2390
   * \p ntsToUnres contains a mapping from non-terminal symbols to the
2391
   * unresolved sorts they correspond to. This map indicates how the argument
2392
   * <term> should be interpreted (instances of symbols from the domain of
2393
   * \p ntsToUnres correspond to constructor arguments).
2394
   *
2395
   * The sygus operator that is actually added to <dt> corresponds to replacing
2396
   * each occurrence of non-terminal symbols from the domain of \p ntsToUnres
2397
   * with bound variables via purifySygusGTerm, and binding these variables
2398
   * via a lambda.
2399
   *
2400
   * @param dt the non-terminal's datatype to which a constructor is added
2401
   * @param term the sygus operator of the constructor
2402
   * @param ntsToUnres mapping from non-terminals to their unresolved sorts
2403
   */
2404
  void addSygusConstructorTerm(
2405
      DatatypeDecl& dt,
2406
      const Term& term,
2407
      const std::unordered_map<Term, Sort>& ntsToUnres) const;
2408
2409
  /**
2410
   * Purify SyGuS grammar term.
2411
   *
2412
   * This returns a term where all occurrences of non-terminal symbols (those
2413
   * in the domain of \p ntsToUnres) are replaced by fresh variables. For
2414
   * each variable replaced in this way, we add the fresh variable it is
2415
   * replaced with to \p args, and the unresolved sorts corresponding to the
2416
   * non-terminal symbol to \p cargs (constructor args). In other words,
2417
   * \p args contains the free variables in the term returned by this method
2418
   * (which should be bound by a lambda), and \p cargs contains the sorts of
2419
   * the arguments of the sygus constructor.
2420
   *
2421
   * @param term the term to purify
2422
   * @param args the free variables in the term returned by this method
2423
   * @param cargs the sorts of the arguments of the sygus constructor
2424
   * @param ntsToUnres mapping from non-terminals to their unresolved sorts
2425
   * @return the purfied term
2426
   */
2427
  Term purifySygusGTerm(const Term& term,
2428
                        std::vector<Term>& args,
2429
                        std::vector<Sort>& cargs,
2430
                        const std::unordered_map<Term, Sort>& ntsToUnres) const;
2431
2432
  /**
2433
   * This adds constructors to \p dt for sygus variables in \p d_sygusVars
2434
   * whose sort is argument \p sort. This method should be called when the
2435
   * sygus grammar term (Variable sort) is encountered.
2436
   *
2437
   * @param dt the non-terminal's datatype to which the constructors are added
2438
   * @param sort the sort of the sygus variables to add
2439
   */
2440
  void addSygusConstructorVariables(DatatypeDecl& dt, const Sort& sort) const;
2441
2442
  /**
2443
   * Check if \p rule contains variables that are neither parameters of
2444
   * the corresponding synthFun/synthInv nor non-terminals.
2445
   * @param rule the non-terminal allowed to be any constant
2446
   * @return true if \p rule contains free variables and false otherwise
2447
   */
2448
  bool containsFreeVariables(const Term& rule) const;
2449
2450
  /** The solver that created this grammar. */
2451
  const Solver* d_solver;
2452
  /** Input variables to the corresponding function/invariant to synthesize.*/
2453
  std::vector<Term> d_sygusVars;
2454
  /** The non-terminal symbols of this grammar. */
2455
  std::vector<Term> d_ntSyms;
2456
  /** The mapping from non-terminal symbols to their production terms. */
2457
  std::unordered_map<Term, std::vector<Term>> d_ntsToTerms;
2458
  /** The set of non-terminals that can be arbitrary constants. */
2459
  std::unordered_set<Term> d_allowConst;
2460
  /** The set of non-terminals that can be sygus variables. */
2461
  std::unordered_set<Term> d_allowVars;
2462
  /** Did we call resolve() before? */
2463
  bool d_isResolved;
2464
};
2465
2466
/**
2467
 * Serialize a grammar to given stream.
2468
 * @param out the output stream
2469
 * @param g the grammar to be serialized to the given output stream
2470
 * @return the output stream
2471
 */
2472
std::ostream& operator<<(std::ostream& out, const Grammar& g) CVC5_EXPORT;
2473
2474
/* -------------------------------------------------------------------------- */
2475
/* Rounding Mode for Floating-Points                                          */
2476
/* -------------------------------------------------------------------------- */
2477
2478
/**
2479
 * Rounding modes for floating-point numbers.
2480
 *
2481
 * For many floating-point operations, infinitely precise results may not be
2482
 * representable with the number of available bits. Thus, the results are
2483
 * rounded in a certain way to one of the representable floating-point numbers.
2484
 *
2485
 * \verbatim embed:rst:leading-asterisk
2486
 * These rounding modes directly follow the SMT-LIB theory for floating-point
2487
 * arithmetic, which in turn is based on IEEE Standard 754 :cite:`IEEE754`.
2488
 * The rounding modes are specified in Sections 4.3.1 and 4.3.2 of the IEEE
2489
 * Standard 754.
2490
 * \endverbatim
2491
 */
2492
enum CVC5_EXPORT RoundingMode
2493
{
2494
  /**
2495
   * Round to the nearest even number.
2496
   * If the two nearest floating-point numbers bracketing an unrepresentable
2497
   * infinitely precise result are equally near, the one with an even least
2498
   * significant digit will be delivered.
2499
   */
2500
  ROUND_NEAREST_TIES_TO_EVEN,
2501
  /**
2502
   * Round towards positive infinity (+oo).
2503
   * The result shall be the format’s floating-point number (possibly +oo)
2504
   * closest to and no less than the infinitely precise result.
2505
   */
2506
  ROUND_TOWARD_POSITIVE,
2507
  /**
2508
   * Round towards negative infinity (-oo).
2509
   * The result shall be the format’s floating-point number (possibly -oo)
2510
   * closest to and no less than the infinitely precise result.
2511
   */
2512
  ROUND_TOWARD_NEGATIVE,
2513
  /**
2514
   * Round towards zero.
2515
   * The result shall be the format’s floating-point number closest to and no
2516
   * greater in magnitude than the infinitely precise result.
2517
   */
2518
  ROUND_TOWARD_ZERO,
2519
  /**
2520
   * Round to the nearest number away from zero.
2521
   * If the two nearest floating-point numbers bracketing an unrepresentable
2522
   * infinitely precise result are equally near, the one with larger magnitude
2523
   * will be selected.
2524
   */
2525
  ROUND_NEAREST_TIES_TO_AWAY,
2526
};
2527
2528
}  // namespace cvc5::api
2529
2530
namespace std {
2531
2532
/**
2533
 * Hash function for RoundingModes.
2534
 */
2535
template <>
2536
struct CVC5_EXPORT hash<cvc5::api::RoundingMode>
2537
{
2538
  size_t operator()(cvc5::api::RoundingMode rm) const;
2539
};
2540
}  // namespace std
2541
namespace cvc5::api {
2542
2543
/* -------------------------------------------------------------------------- */
2544
/* Statistics                                                                 */
2545
/* -------------------------------------------------------------------------- */
2546
2547
/**
2548
 * Represents a snapshot of a single statistic value.
2549
 * A value can be of type `int64_t`, `double`, `std::string` or a histogram
2550
 * (`std::map<std::string, uint64_t>`).
2551
 * The value type can be queried (using `isInt()`, `isDouble()`, etc.) and
2552
 * the stored value can be accessed (using `getInt()`, `getDouble()`, etc.).
2553
 * It is possible to query whether this statistic is an expert statistic by
2554
 * `isExpert()` and whether its value is the default value by `isDefault()`.
2555
 */
2556
class CVC5_EXPORT Stat
2557
{
2558
  struct StatData;
2559
2560
 public:
2561
  friend class Statistics;
2562
  friend std::ostream& operator<<(std::ostream& os, const Stat& sv);
2563
  /** Representation of a histogram: maps names to frequencies. */
2564
  using HistogramData = std::map<std::string, uint64_t>;
2565
  /** Can only be obtained from a `Statistics` object. */
2566
  Stat() = delete;
2567
  /** Copy constructor */
2568
  Stat(const Stat& s);
2569
  /** Destructor */
2570
  ~Stat();
2571
  /** Copy assignment */
2572
  Stat& operator=(const Stat& s);
2573
2574
  /**
2575
   * Is this value intended for experts only?
2576
   * @return Whether this is an expert statistic.
2577
   */
2578
  bool isExpert() const;
2579
  /**
2580
   * Does this value hold the default value?
2581
   * @return Whether this is a defaulted statistic.
2582
   */
2583
  bool isDefault() const;
2584
2585
  /**
2586
   * Is this value an integer?
2587
   * @return Whether the value is an integer.
2588
   */
2589
  bool isInt() const;
2590
  /**
2591
   * Return the integer value.
2592
   * @return The integer value.
2593
   */
2594
  int64_t getInt() const;
2595
  /**
2596
   * Is this value a double?
2597
   * @return Whether the value is a double.
2598
   */
2599
  bool isDouble() const;
2600
  /**
2601
   * Return the double value.
2602
   * @return The double value.
2603
   */
2604
  double getDouble() const;
2605
  /**
2606
   * Is this value a string?
2607
   * @return Whether the value is a string.
2608
   */
2609
  bool isString() const;
2610
  /**
2611
   * Return the string value.
2612
   * @return The string value.
2613
   */
2614
  const std::string& getString() const;
2615
  /**
2616
   * Is this value a histogram?
2617
   * @return Whether the value is a histogram.
2618
   */
2619
  bool isHistogram() const;
2620
  /**
2621
   * Return the histogram value.
2622
   * @return The histogram value.
2623
   */
2624
  const HistogramData& getHistogram() const;
2625
2626
 private:
2627
  Stat(bool expert, bool def, StatData&& sd);
2628
  /** Whether this statistic is only meant for experts */
2629
  bool d_expert;
2630
  /** Whether this statistic has the default value */
2631
  bool d_default;
2632
  std::unique_ptr<StatData> d_data;
2633
};
2634
2635
/**
2636
 * Print a `Stat` object to an ``std::ostream``.
2637
 */
2638
std::ostream& operator<<(std::ostream& os, const Stat& sv) CVC5_EXPORT;
2639
2640
/**
2641
 * Represents a snapshot of the solver statistics.
2642
 * Once obtained, an instance of this class is independent of the `Solver`
2643
 * object: it will not change when the solvers internal statistics do, it
2644
 * will not be invalidated if the solver is destroyed.
2645
 * Iterating on this class (via `begin()` and `end()`) shows only public
2646
 * statistics that have been changed. By passing appropriate flags to
2647
 * `begin()`, statistics that are expert, defaulted, or both, can be
2648
 * included as well. A single statistic value is represented as `Stat`.
2649
 */
2650
class CVC5_EXPORT Statistics
2651
{
2652
 public:
2653
  friend class Solver;
2654
  /** How the statistics are stored internally. */
2655
  using BaseType = std::map<std::string, Stat>;
2656
2657
  /** Custom iterator to hide certain statistics from regular iteration */
2658
  class iterator
2659
  {
2660
   public:
2661
    friend class Statistics;
2662
    BaseType::const_reference operator*() const;
2663
    BaseType::const_pointer operator->() const;
2664
    iterator& operator++();
2665
    iterator operator++(int);
2666
    iterator& operator--();
2667
    iterator operator--(int);
2668
    bool operator==(const iterator& rhs) const;
2669
    bool operator!=(const iterator& rhs) const;
2670
2671
   private:
2672
    iterator(BaseType::const_iterator it,
2673
             const BaseType& base,
2674
             bool expert,
2675
             bool defaulted);
2676
    bool isVisible() const;
2677
    BaseType::const_iterator d_it;
2678
    const BaseType* d_base;
2679
    bool d_showExpert = false;
2680
    bool d_showDefault = false;
2681
  };
2682
2683
  /**
2684
   * Retrieve the statistic with the given name.
2685
   * Asserts that a statistic with the given name actually exists and throws
2686
   * a `CVC4ApiRecoverableException` if it does not.
2687
   * @param name Name of the statistic.
2688
   * @return The statistic with the given name.
2689
   */
2690
  const Stat& get(const std::string& name);
2691
  /**
2692
   * Begin iteration over the statistics values.
2693
   * By default, only entries that are public (non-expert) and have been set
2694
   * are visible while the others are skipped.
2695
   * @param expert If set to true, expert statistics are shown as well.
2696
   * @param defaulted If set to true, defaulted statistics are shown as well.
2697
   */
2698
  iterator begin(bool expert = false, bool defaulted = false) const;
2699
  /** End iteration */
2700
  iterator end() const;
2701
2702
 private:
2703
  Statistics() = default;
2704
  Statistics(const StatisticsRegistry& reg);
2705
  /** Internal data */
2706
  BaseType d_stats;
2707
};
2708
std::ostream& operator<<(std::ostream& out,
2709
                         const Statistics& stats) CVC5_EXPORT;
2710
2711
/* -------------------------------------------------------------------------- */
2712
/* Solver                                                                     */
2713
/* -------------------------------------------------------------------------- */
2714
2715
/**
2716
 * A cvc5 solver.
2717
 */
2718
class CVC5_EXPORT Solver
2719
{
2720
  friend class Datatype;
2721
  friend class DatatypeDecl;
2722
  friend class DatatypeConstructor;
2723
  friend class DatatypeConstructorDecl;
2724
  friend class DatatypeSelector;
2725
  friend class Grammar;
2726
  friend class Op;
2727
  friend class cvc5::Command;
2728
  friend class Sort;
2729
  friend class Term;
2730
2731
 public:
2732
  /* .................................................................... */
2733
  /* Constructors/Destructors                                             */
2734
  /* .................................................................... */
2735
2736
  /**
2737
   * Constructor.
2738
   * @param opts an optional pointer to a solver options object
2739
   * @return the Solver
2740
   */
2741
  Solver(Options* opts = nullptr);
2742
2743
  /**
2744
   * Destructor.
2745
   */
2746
  ~Solver();
2747
2748
  /**
2749
   * Disallow copy/assignment.
2750
   */
2751
  Solver(const Solver&) = delete;
2752
  Solver& operator=(const Solver&) = delete;
2753
2754
  /* .................................................................... */
2755
  /* Solver Configuration                                                 */
2756
  /* .................................................................... */
2757
2758
  bool supportsFloatingPoint() const;
2759
2760
  /* .................................................................... */
2761
  /* Sorts Handling                                                       */
2762
  /* .................................................................... */
2763
2764
  /**
2765
   * @return sort null
2766
   */
2767
  Sort getNullSort() const;
2768
2769
  /**
2770
   * @return sort Boolean
2771
   */
2772
  Sort getBooleanSort() const;
2773
2774
  /**
2775
   * @return sort Integer (in cvc5, Integer is a subtype of Real)
2776
   */
2777
  Sort getIntegerSort() const;
2778
2779
  /**
2780
   * @return sort Real
2781
   */
2782
  Sort getRealSort() const;
2783
2784
  /**
2785
   * @return sort RegExp
2786
   */
2787
  Sort getRegExpSort() const;
2788
2789
  /**
2790
   * @return sort RoundingMode
2791
   */
2792
  Sort getRoundingModeSort() const;
2793
2794
  /**
2795
   * @return sort String
2796
   */
2797
  Sort getStringSort() const;
2798
2799
  /**
2800
   * Create an array sort.
2801
   * @param indexSort the array index sort
2802
   * @param elemSort the array element sort
2803
   * @return the array sort
2804
   */
2805
  Sort mkArraySort(const Sort& indexSort, const Sort& elemSort) const;
2806
2807
  /**
2808
   * Create a bit-vector sort.
2809
   * @param size the bit-width of the bit-vector sort
2810
   * @return the bit-vector sort
2811
   */
2812
  Sort mkBitVectorSort(uint32_t size) const;
2813
2814
  /**
2815
   * Create a floating-point sort.
2816
   * @param exp the bit-width of the exponent of the floating-point sort.
2817
   * @param sig the bit-width of the significand of the floating-point sort.
2818
   */
2819
  Sort mkFloatingPointSort(uint32_t exp, uint32_t sig) const;
2820
2821
  /**
2822
   * Create a datatype sort.
2823
   * @param dtypedecl the datatype declaration from which the sort is created
2824
   * @return the datatype sort
2825
   */
2826
  Sort mkDatatypeSort(const DatatypeDecl& dtypedecl) const;
2827
2828
  /**
2829
   * Create a vector of datatype sorts. The names of the datatype declarations
2830
   * must be distinct.
2831
   *
2832
   * @param dtypedecls the datatype declarations from which the sort is created
2833
   * @return the datatype sorts
2834
   */
2835
  std::vector<Sort> mkDatatypeSorts(
2836
      const std::vector<DatatypeDecl>& dtypedecls) const;
2837
2838
  /**
2839
   * Create a vector of datatype sorts using unresolved sorts. The names of
2840
   * the datatype declarations in dtypedecls must be distinct.
2841
   *
2842
   * This method is called when the DatatypeDecl objects dtypedecls have been
2843
   * built using "unresolved" sorts.
2844
   *
2845
   * We associate each sort in unresolvedSorts with exacly one datatype from
2846
   * dtypedecls. In particular, it must have the same name as exactly one
2847
   * datatype declaration in dtypedecls.
2848
   *
2849
   * When constructing datatypes, unresolved sorts are replaced by the datatype
2850
   * sort constructed for the datatype declaration it is associated with.
2851
   *
2852
   * @param dtypedecls the datatype declarations from which the sort is created
2853
   * @param unresolvedSorts the list of unresolved sorts
2854
   * @return the datatype sorts
2855
   */
2856
  std::vector<Sort> mkDatatypeSorts(
2857
      const std::vector<DatatypeDecl>& dtypedecls,
2858
      const std::set<Sort>& unresolvedSorts) const;
2859
2860
  /**
2861
   * Create function sort.
2862
   * @param domain the sort of the fuction argument
2863
   * @param codomain the sort of the function return value
2864
   * @return the function sort
2865
   */
2866
  Sort mkFunctionSort(const Sort& domain, const Sort& codomain) const;
2867
2868
  /**
2869
   * Create function sort.
2870
   * @param sorts the sort of the function arguments
2871
   * @param codomain the sort of the function return value
2872
   * @return the function sort
2873
   */
2874
  Sort mkFunctionSort(const std::vector<Sort>& sorts,
2875
                      const Sort& codomain) const;
2876
2877
  /**
2878
   * Create a sort parameter.
2879
   * @param symbol the name of the sort
2880
   * @return the sort parameter
2881
   */
2882
  Sort mkParamSort(const std::string& symbol) const;
2883
2884
  /**
2885
   * Create a predicate sort.
2886
   * @param sorts the list of sorts of the predicate
2887
   * @return the predicate sort
2888
   */
2889
  Sort mkPredicateSort(const std::vector<Sort>& sorts) const;
2890
2891
  /**
2892
   * Create a record sort
2893
   * @param fields the list of fields of the record
2894
   * @return the record sort
2895
   */
2896
  Sort mkRecordSort(
2897
      const std::vector<std::pair<std::string, Sort>>& fields) const;
2898
2899
  /**
2900
   * Create a set sort.
2901
   * @param elemSort the sort of the set elements
2902
   * @return the set sort
2903
   */
2904
  Sort mkSetSort(const Sort& elemSort) const;
2905
2906
  /**
2907
   * Create a bag sort.
2908
   * @param elemSort the sort of the bag elements
2909
   * @return the bag sort
2910
   */
2911
  Sort mkBagSort(const Sort& elemSort) const;
2912
2913
  /**
2914
   * Create a sequence sort.
2915
   * @param elemSort the sort of the sequence elements
2916
   * @return the sequence sort
2917
   */
2918
  Sort mkSequenceSort(const Sort& elemSort) const;
2919
2920
  /**
2921
   * Create an uninterpreted sort.
2922
   * @param symbol the name of the sort
2923
   * @return the uninterpreted sort
2924
   */
2925
  Sort mkUninterpretedSort(const std::string& symbol) const;
2926
2927
  /**
2928
   * Create a sort constructor sort.
2929
   * @param symbol the symbol of the sort
2930
   * @param arity the arity of the sort
2931
   * @return the sort constructor sort
2932
   */
2933
  Sort mkSortConstructorSort(const std::string& symbol, size_t arity) const;
2934
2935
  /**
2936
   * Create a tuple sort.
2937
   * @param sorts of the elements of the tuple
2938
   * @return the tuple sort
2939
   */
2940
  Sort mkTupleSort(const std::vector<Sort>& sorts) const;
2941
2942
  /* .................................................................... */
2943
  /* Create Terms                                                         */
2944
  /* .................................................................... */
2945
2946
  /**
2947
   * Create 0-ary term of given kind.
2948
   * @param kind the kind of the term
2949
   * @return the Term
2950
   */
2951
  Term mkTerm(Kind kind) const;
2952
2953
  /**
2954
   * Create a unary term of given kind.
2955
   * @param kind the kind of the term
2956
   * @param child the child of the term
2957
   * @return the Term
2958
   */
2959
  Term mkTerm(Kind kind, const Term& child) const;
2960
2961
  /**
2962
   * Create binary term of given kind.
2963
   * @param kind the kind of the term
2964
   * @param child1 the first child of the term
2965
   * @param child2 the second child of the term
2966
   * @return the Term
2967
   */
2968
  Term mkTerm(Kind kind, const Term& child1, const Term& child2) const;
2969
2970
  /**
2971
   * Create ternary term of given kind.
2972
   * @param kind the kind of the term
2973
   * @param child1 the first child of the term
2974
   * @param child2 the second child of the term
2975
   * @param child3 the third child of the term
2976
   * @return the Term
2977
   */
2978
  Term mkTerm(Kind kind,
2979
              const Term& child1,
2980
              const Term& child2,
2981
              const Term& child3) const;
2982
2983
  /**
2984
   * Create n-ary term of given kind.
2985
   * @param kind the kind of the term
2986
   * @param children the children of the term
2987
   * @return the Term
2988
   */
2989
  Term mkTerm(Kind kind, const std::vector<Term>& children) const;
2990
2991
  /**
2992
   * Create nullary term of given kind from a given operator.
2993
   * Create operators with mkOp().
2994
   * @param op the operator
2995
   * @return the Term
2996
   */
2997
  Term mkTerm(const Op& op) const;
2998
2999
  /**
3000
   * Create unary term of given kind from a given operator.
3001
   * Create operators with mkOp().
3002
   * @param op the operator
3003
   * @param child the child of the term
3004
   * @return the Term
3005
   */
3006
  Term mkTerm(const Op& op, const Term& child) const;
3007
3008
  /**
3009
   * Create binary term of given kind from a given operator.
3010
   * Create operators with mkOp().
3011
   * @param op the operator
3012
   * @param child1 the first child of the term
3013
   * @param child2 the second child of the term
3014
   * @return the Term
3015
   */
3016
  Term mkTerm(const Op& op, const Term& child1, const Term& child2) const;
3017
3018
  /**
3019
   * Create ternary term of given kind from a given operator.
3020
   * Create operators with mkOp().
3021
   * @param op the operator
3022
   * @param child1 the first child of the term
3023
   * @param child2 the second child of the term
3024
   * @param child3 the third child of the term
3025
   * @return the Term
3026
   */
3027
  Term mkTerm(const Op& op,
3028
              const Term& child1,
3029
              const Term& child2,
3030
              const Term& child3) const;
3031
3032
  /**
3033
   * Create n-ary term of given kind from a given operator.
3034
   * Create operators with mkOp().
3035
   * @param op the operator
3036
   * @param children the children of the term
3037
   * @return the Term
3038
   */
3039
  Term mkTerm(const Op& op, const std::vector<Term>& children) const;
3040
3041
  /**
3042
   * Create a tuple term. Terms are automatically converted if sorts are
3043
   * compatible.
3044
   * @param sorts The sorts of the elements in the tuple
3045
   * @param terms The elements in the tuple
3046
   * @return the tuple Term
3047
   */
3048
  Term mkTuple(const std::vector<Sort>& sorts,
3049
               const std::vector<Term>& terms) const;
3050
3051
  /* .................................................................... */
3052
  /* Create Operators                                                     */
3053
  /* .................................................................... */
3054
3055
  /**
3056
   * Create an operator for a builtin Kind
3057
   * The Kind may not be the Kind for an indexed operator
3058
   *   (e.g. BITVECTOR_EXTRACT)
3059
   * Note: in this case, the Op simply wraps the Kind.
3060
   * The Kind can be used in mkTerm directly without
3061
   *   creating an op first.
3062
   * @param kind the kind to wrap
3063
   */
3064
  Op mkOp(Kind kind) const;
3065
3066
  /**
3067
   * Create operator of kind:
3068
   *   - DIVISIBLE (to support arbitrary precision integers)
3069
   * See enum Kind for a description of the parameters.
3070
   * @param kind the kind of the operator
3071
   * @param arg the string argument to this operator
3072
   */
3073
  Op mkOp(Kind kind, const std::string& arg) const;
3074
3075
  /**
3076
   * Create operator of kind:
3077
   *   - DIVISIBLE
3078
   *   - BITVECTOR_REPEAT
3079
   *   - BITVECTOR_ZERO_EXTEND
3080
   *   - BITVECTOR_SIGN_EXTEND
3081
   *   - BITVECTOR_ROTATE_LEFT
3082
   *   - BITVECTOR_ROTATE_RIGHT
3083
   *   - INT_TO_BITVECTOR
3084
   *   - FLOATINGPOINT_TO_UBV
3085
   *   - FLOATINGPOINT_TO_UBV_TOTAL
3086
   *   - FLOATINGPOINT_TO_SBV
3087
   *   - FLOATINGPOINT_TO_SBV_TOTAL
3088
   *   - TUPLE_UPDATE
3089
   * See enum Kind for a description of the parameters.
3090
   * @param kind the kind of the operator
3091
   * @param arg the uint32_t argument to this operator
3092
   */
3093
  Op mkOp(Kind kind, uint32_t arg) const;
3094
3095
  /**
3096
   * Create operator of Kind:
3097
   *   - BITVECTOR_EXTRACT
3098
   *   - FLOATINGPOINT_TO_FP_IEEE_BITVECTOR
3099
   *   - FLOATINGPOINT_TO_FP_FLOATINGPOINT
3100
   *   - FLOATINGPOINT_TO_FP_REAL
3101
   *   - FLOATINGPOINT_TO_FP_SIGNED_BITVECTOR
3102
   *   - FLOATINGPOINT_TO_FP_UNSIGNED_BITVECTOR
3103
   *   - FLOATINGPOINT_TO_FP_GENERIC
3104
   * See enum Kind for a description of the parameters.
3105
   * @param kind the kind of the operator
3106
   * @param arg1 the first uint32_t argument to this operator
3107
   * @param arg2 the second uint32_t argument to this operator
3108
   */
3109
  Op mkOp(Kind kind, uint32_t arg1, uint32_t arg2) const;
3110
3111
  /**
3112
   * Create operator of Kind:
3113
   *   - TUPLE_PROJECT
3114
   * See enum Kind for a description of the parameters.
3115
   * @param kind the kind of the operator
3116
   * @param args the arguments (indices) of the operator
3117
   */
3118
  Op mkOp(Kind kind, const std::vector<uint32_t>& args) const;
3119
3120
  /* .................................................................... */
3121
  /* Create Constants                                                     */
3122
  /* .................................................................... */
3123
3124
  /**
3125
   * Create a Boolean true constant.
3126
   * @return the true constant
3127
   */
3128
  Term mkTrue() const;
3129
3130
  /**
3131
   * Create a Boolean false constant.
3132
   * @return the false constant
3133
   */
3134
  Term mkFalse() const;
3135
3136
  /**
3137
   * Create a Boolean constant.
3138
   * @return the Boolean constant
3139
   * @param val the value of the constant
3140
   */
3141
  Term mkBoolean(bool val) const;
3142
3143
  /**
3144
   * Create a constant representing the number Pi.
3145
   * @return a constant representing Pi
3146
   */
3147
  Term mkPi() const;
3148
  /**
3149
   * Create an integer constant from a string.
3150
   * @param s the string representation of the constant, may represent an
3151
   *          integer (e.g., "123").
3152
   * @return a constant of sort Integer assuming 's' represents an integer)
3153
   */
3154
  Term mkInteger(const std::string& s) const;
3155
3156
  /**
3157
   * Create an integer constant from a c++ int.
3158
   * @param val the value of the constant
3159
   * @return a constant of sort Integer
3160
   */
3161
  Term mkInteger(int64_t val) const;
3162
3163
  /**
3164
   * Create a real constant from a string.
3165
   * @param s the string representation of the constant, may represent an
3166
   *          integer (e.g., "123") or real constant (e.g., "12.34" or "12/34").
3167
   * @return a constant of sort Real
3168
   */
3169
  Term mkReal(const std::string& s) const;
3170
3171
  /**
3172
   * Create a real constant from an integer.
3173
   * @param val the value of the constant
3174
   * @return a constant of sort Integer
3175
   */
3176
  Term mkReal(int64_t val) const;
3177
3178
  /**
3179
   * Create a real constant from a rational.
3180
   * @param num the value of the numerator
3181
   * @param den the value of the denominator
3182
   * @return a constant of sort Real
3183
   */
3184
  Term mkReal(int64_t num, int64_t den) const;
3185
3186
  /**
3187
   * Create a regular expression empty term.
3188
   * @return the empty term
3189
   */
3190
  Term mkRegexpEmpty() const;
3191
3192
  /**
3193
   * Create a regular expression sigma term.
3194
   * @return the sigma term
3195
   */
3196
  Term mkRegexpSigma() const;
3197
3198
  /**
3199
   * Create a constant representing an empty set of the given sort.
3200
   * @param sort the sort of the set elements.
3201
   * @return the empty set constant
3202
   */
3203
  Term mkEmptySet(const Sort& sort) const;
3204
3205
  /**
3206
   * Create a constant representing an empty bag of the given sort.
3207
   * @param sort the sort of the bag elements.
3208
   * @return the empty bag constant
3209
   */
3210
  Term mkEmptyBag(const Sort& sort) const;
3211
3212
  /**
3213
   * Create a separation logic nil term.
3214
   * @param sort the sort of the nil term
3215
   * @return the separation logic nil term
3216
   */
3217
  Term mkSepNil(const Sort& sort) const;
3218
3219
  /**
3220
   * Create a String constant from a `std::string` which may contain SMT-LIB
3221
   * compatible escape sequences like `\u1234` to encode unicode characters.
3222
   * @param s the string this constant represents
3223
   * @param useEscSequences determines whether escape sequences in `s` should
3224
   * be converted to the corresponding unicode character
3225
   * @return the String constant
3226
   */
3227
  Term mkString(const std::string& s, bool useEscSequences = false) const;
3228
3229
  /**
3230
   * Create a String constant from a `std::wstring`.
3231
   * This method does not support escape sequences as `std::wstring` already
3232
   * supports unicode characters.
3233
   * @param s the string this constant represents
3234
   * @return the String constant
3235
   */
3236
  Term mkString(const std::wstring& s) const;
3237
3238
  /**
3239
   * Create an empty sequence of the given element sort.
3240
   * @param sort The element sort of the sequence.
3241
   * @return the empty sequence with given element sort.
3242
   */
3243
  Term mkEmptySequence(const Sort& sort) const;
3244
3245
  /**
3246
   * Create a universe set of the given sort.
3247
   * @param sort the sort of the set elements
3248
   * @return the universe set constant
3249
   */
3250
  Term mkUniverseSet(const Sort& sort) const;
3251
3252
  /**
3253
   * Create a bit-vector constant of given size and value.
3254
   * @param size the bit-width of the bit-vector sort
3255
   * @param val the value of the constant
3256
   * @return the bit-vector constant
3257
   */
3258
  Term mkBitVector(uint32_t size, uint64_t val = 0) const;
3259
3260
  /**
3261
   * Create a bit-vector constant from a given string of base 2, 10 or 16.
3262
   *
3263
   * The size of resulting bit-vector is
3264
   * - base  2: the size of the binary string
3265
   * - base 10: the min. size required to represent the decimal as a bit-vector
3266
   * - base 16: the max. size required to represent the hexadecimal as a
3267
   *            bit-vector (4 * size of the given value string)
3268
   *
3269
   * @param s the string representation of the constant
3270
   * @param base the base of the string representation (2, 10, or 16)
3271
   * @return the bit-vector constant
3272
   */
3273
  Term mkBitVector(const std::string& s, uint32_t base = 2) const;
3274
3275
  /**
3276
   * Create a bit-vector constant of a given bit-width from a given string of
3277
   * base 2, 10 or 16.
3278
   * @param size the bit-width of the constant
3279
   * @param s the string representation of the constant
3280
   * @param base the base of the string representation (2, 10, or 16)
3281
   * @return the bit-vector constant
3282
   */
3283
  Term mkBitVector(uint32_t size, const std::string& s, uint32_t base) const;
3284
3285
  /**
3286
   * Create a constant array with the provided constant value stored at every
3287
   * index
3288
   * @param sort the sort of the constant array (must be an array sort)
3289
   * @param val the constant value to store (must match the sort's element sort)
3290
   * @return the constant array term
3291
   */
3292
  Term mkConstArray(const Sort& sort, const Term& val) const;
3293
3294
  /**
3295
   * Create a positive infinity floating-point constant. Requires cvc5 to be
3296
   * compiled with SymFPU support.
3297
   * @param exp Number of bits in the exponent
3298
   * @param sig Number of bits in the significand
3299
   * @return the floating-point constant
3300
   */
3301
  Term mkPosInf(uint32_t exp, uint32_t sig) const;
3302
3303
  /**
3304
   * Create a negative infinity floating-point constant. Requires cvc5 to be
3305
   * compiled with SymFPU support.
3306
   * @param exp Number of bits in the exponent
3307
   * @param sig Number of bits in the significand
3308
   * @return the floating-point constant
3309
   */
3310
  Term mkNegInf(uint32_t exp, uint32_t sig) const;
3311
3312
  /**
3313
   * Create a not-a-number (NaN) floating-point constant. Requires cvc5 to be
3314
   * compiled with SymFPU support.
3315
   * @param exp Number of bits in the exponent
3316
   * @param sig Number of bits in the significand
3317
   * @return the floating-point constant
3318
   */
3319
  Term mkNaN(uint32_t exp, uint32_t sig) const;
3320
3321
  /**
3322
   * Create a positive zero (+0.0) floating-point constant. Requires cvc5 to be
3323
   * compiled with SymFPU support.
3324
   * @param exp Number of bits in the exponent
3325
   * @param sig Number of bits in the significand
3326
   * @return the floating-point constant
3327
   */
3328
  Term mkPosZero(uint32_t exp, uint32_t sig) const;
3329
3330
  /**
3331
   * Create a negative zero (-0.0) floating-point constant. Requires cvc5 to be
3332
   * compiled with SymFPU support.
3333
   * @param exp Number of bits in the exponent
3334
   * @param sig Number of bits in the significand
3335
   * @return the floating-point constant
3336
   */
3337
  Term mkNegZero(uint32_t exp, uint32_t sig) const;
3338
3339
  /**
3340
   * Create a roundingmode constant.
3341
   * @param rm the floating point rounding mode this constant represents
3342
   */
3343
  Term mkRoundingMode(RoundingMode rm) const;
3344
3345
  /**
3346
   * Create uninterpreted constant.
3347
   * @param sort Sort of the constant
3348
   * @param index Index of the constant
3349
   */
3350
  Term mkUninterpretedConst(const Sort& sort, int32_t index) const;
3351
3352
  /**
3353
   * Create an abstract value constant.
3354
   * The given index needs to be a positive integer in base 10.
3355
   * @param index Index of the abstract value
3356
   */
3357
  Term mkAbstractValue(const std::string& index) const;
3358
3359
  /**
3360
   * Create an abstract value constant.
3361
   * The given index needs to be positive.
3362
   * @param index Index of the abstract value
3363
   */
3364
  Term mkAbstractValue(uint64_t index) const;
3365
3366
  /**
3367
   * Create a floating-point constant (requires cvc5 to be compiled with symFPU
3368
   * support).
3369
   * @param exp Size of the exponent
3370
   * @param sig Size of the significand
3371
   * @param val Value of the floating-point constant as a bit-vector term
3372
   */
3373
  Term mkFloatingPoint(uint32_t exp, uint32_t sig, Term val) const;
3374
3375
  /* .................................................................... */
3376
  /* Create Variables                                                     */
3377
  /* .................................................................... */
3378
3379
  /**
3380
   * Create (first-order) constant (0-arity function symbol).
3381
   * SMT-LIB:
3382
   * \verbatim
3383
   *   ( declare-const <symbol> <sort> )
3384
   *   ( declare-fun <symbol> ( ) <sort> )
3385
   * \endverbatim
3386
   *
3387
   * @param sort the sort of the constant
3388
   * @param symbol the name of the constant
3389
   * @return the first-order constant
3390
   */
3391
  Term mkConst(const Sort& sort, const std::string& symbol) const;
3392
  /**
3393
   * Create (first-order) constant (0-arity function symbol), with a default
3394
   * symbol name.
3395
   *
3396
   * @param sort the sort of the constant
3397
   * @return the first-order constant
3398
   */
3399
  Term mkConst(const Sort& sort) const;
3400
3401
  /**
3402
   * Create a bound variable to be used in a binder (i.e. a quantifier, a
3403
   * lambda, or a witness binder).
3404
   * @param sort the sort of the variable
3405
   * @param symbol the name of the variable
3406
   * @return the variable
3407
   */
3408
  Term mkVar(const Sort& sort, const std::string& symbol = std::string()) const;
3409
3410
  /* .................................................................... */
3411
  /* Create datatype constructor declarations                             */
3412
  /* .................................................................... */
3413
3414
  DatatypeConstructorDecl mkDatatypeConstructorDecl(const std::string& name);
3415
3416
  /* .................................................................... */
3417
  /* Create datatype declarations                                         */
3418
  /* .................................................................... */
3419
3420
  /**
3421
   * Create a datatype declaration.
3422
   * @param name the name of the datatype
3423
   * @param isCoDatatype true if a codatatype is to be constructed
3424
   * @return the DatatypeDecl
3425
   */
3426
  DatatypeDecl mkDatatypeDecl(const std::string& name,
3427
                              bool isCoDatatype = false);
3428
3429
  /**
3430
   * Create a datatype declaration.
3431
   * Create sorts parameter with Solver::mkParamSort().
3432
   * @param name the name of the datatype
3433
   * @param param the sort parameter
3434
   * @param isCoDatatype true if a codatatype is to be constructed
3435
   * @return the DatatypeDecl
3436
   */
3437
  DatatypeDecl mkDatatypeDecl(const std::string& name,
3438
                              Sort param,
3439
                              bool isCoDatatype = false);
3440
3441
  /**
3442
   * Create a datatype declaration.
3443
   * Create sorts parameter with Solver::mkParamSort().
3444
   * @param name the name of the datatype
3445
   * @param params a list of sort parameters
3446
   * @param isCoDatatype true if a codatatype is to be constructed
3447
   * @return the DatatypeDecl
3448
   */
3449
  DatatypeDecl mkDatatypeDecl(const std::string& name,
3450
                              const std::vector<Sort>& params,
3451
                              bool isCoDatatype = false);
3452
3453
  /* .................................................................... */
3454
  /* Formula Handling                                                     */
3455
  /* .................................................................... */
3456
3457
  /**
3458
   * Simplify a formula without doing "much" work.  Does not involve
3459
   * the SAT Engine in the simplification, but uses the current
3460
   * definitions, assertions, and the current partial model, if one
3461
   * has been constructed.  It also involves theory normalization.
3462
   * @param t the formula to simplify
3463
   * @return the simplified formula
3464
   */
3465
  Term simplify(const Term& t);
3466
3467
  /**
3468
   * Assert a formula.
3469
   * SMT-LIB:
3470
   * \verbatim
3471
   *   ( assert <term> )
3472
   * \endverbatim
3473
   * @param term the formula to assert
3474
   */
3475
  void assertFormula(const Term& term) const;
3476
3477
  /**
3478
   * Check satisfiability.
3479
   * SMT-LIB:
3480
   * \verbatim
3481
   *   ( check-sat )
3482
   * \endverbatim
3483
   * @return the result of the satisfiability check.
3484
   */
3485
  Result checkSat() const;
3486
3487
  /**
3488
   * Check satisfiability assuming the given formula.
3489
   * SMT-LIB:
3490
   * \verbatim
3491
   *   ( check-sat-assuming ( <prop_literal> ) )
3492
   * \endverbatim
3493
   * @param assumption the formula to assume
3494
   * @return the result of the satisfiability check.
3495
   */
3496
  Result checkSatAssuming(const Term& assumption) const;
3497
3498
  /**
3499
   * Check satisfiability assuming the given formulas.
3500
   * SMT-LIB:
3501
   * \verbatim
3502
   *   ( check-sat-assuming ( <prop_literal>+ ) )
3503
   * \endverbatim
3504
   * @param assumptions the formulas to assume
3505
   * @return the result of the satisfiability check.
3506
   */
3507
  Result checkSatAssuming(const std::vector<Term>& assumptions) const;
3508
3509
  /**
3510
   * Check entailment of the given formula w.r.t. the current set of assertions.
3511
   * @param term the formula to check entailment for
3512
   * @return the result of the entailment check.
3513
   */
3514
  Result checkEntailed(const Term& term) const;
3515
3516
  /**
3517
   * Check entailment of the given set of given formulas w.r.t. the current
3518
   * set of assertions.
3519
   * @param terms the terms to check entailment for
3520
   * @return the result of the entailmentcheck.
3521
   */
3522
  Result checkEntailed(const std::vector<Term>& terms) const;
3523
3524
  /**
3525
   * Create datatype sort.
3526
   * SMT-LIB:
3527
   * \verbatim
3528
   *   ( declare-datatype <symbol> <datatype_decl> )
3529
   * \endverbatim
3530
   * @param symbol the name of the datatype sort
3531
   * @param ctors the constructor declarations of the datatype sort
3532
   * @return the datatype sort
3533
   */
3534
  Sort declareDatatype(const std::string& symbol,
3535
                       const std::vector<DatatypeConstructorDecl>& ctors) const;
3536
3537
  /**
3538
   * Declare n-ary function symbol.
3539
   * SMT-LIB:
3540
   * \verbatim
3541
   *   ( declare-fun <symbol> ( <sort>* ) <sort> )
3542
   * \endverbatim
3543
   * @param symbol the name of the function
3544
   * @param sorts the sorts of the parameters to this function
3545
   * @param sort the sort of the return value of this function
3546
   * @return the function
3547
   */
3548
  Term declareFun(const std::string& symbol,
3549
                  const std::vector<Sort>& sorts,
3550
                  const Sort& sort) const;
3551
3552
  /**
3553
   * Declare uninterpreted sort.
3554
   * SMT-LIB:
3555
   * \verbatim
3556
   *   ( declare-sort <symbol> <numeral> )
3557
   * \endverbatim
3558
   * @param symbol the name of the sort
3559
   * @param arity the arity of the sort
3560
   * @return the sort
3561
   */
3562
  Sort declareSort(const std::string& symbol, uint32_t arity) const;
3563
3564
  /**
3565
   * Define n-ary function.
3566
   * SMT-LIB:
3567
   * \verbatim
3568
   *   ( define-fun <function_def> )
3569
   * \endverbatim
3570
   * @param symbol the name of the function
3571
   * @param bound_vars the parameters to this function
3572
   * @param sort the sort of the return value of this function
3573
   * @param term the function body
3574
   * @param global determines whether this definition is global (i.e. persists
3575
   *               when popping the context)
3576
   * @return the function
3577
   */
3578
  Term defineFun(const std::string& symbol,
3579
                 const std::vector<Term>& bound_vars,
3580
                 const Sort& sort,
3581
                 const Term& term,
3582
                 bool global = false) const;
3583
  /**
3584
   * Define n-ary function.
3585
   * SMT-LIB:
3586
   * \verbatim
3587
   * ( define-fun <function_def> )
3588
   * \endverbatim
3589
   * Create parameter 'fun' with mkConst().
3590
   * @param fun the sorted function
3591
   * @param bound_vars the parameters to this function
3592
   * @param term the function body
3593
   * @param global determines whether this definition is global (i.e. persists
3594
   *               when popping the context)
3595
   * @return the function
3596
   */
3597
  Term defineFun(const Term& fun,
3598
                 const std::vector<Term>& bound_vars,
3599
                 const Term& term,
3600
                 bool global = false) const;
3601
3602
  /**
3603
   * Define recursive function.
3604
   * SMT-LIB:
3605
   * \verbatim
3606
   * ( define-fun-rec <function_def> )
3607
   * \endverbatim
3608
   * @param symbol the name of the function
3609
   * @param bound_vars the parameters to this function
3610
   * @param sort the sort of the return value of this function
3611
   * @param term the function body
3612
   * @param global determines whether this definition is global (i.e. persists
3613
   *               when popping the context)
3614
   * @return the function
3615
   */
3616
  Term defineFunRec(const std::string& symbol,
3617
                    const std::vector<Term>& bound_vars,
3618
                    const Sort& sort,
3619
                    const Term& term,
3620
                    bool global = false) const;
3621
3622
  /**
3623
   * Define recursive function.
3624
   * SMT-LIB:
3625
   * \verbatim
3626
   * ( define-fun-rec <function_def> )
3627
   * \endverbatim
3628
   * Create parameter 'fun' with mkConst().
3629
   * @param fun the sorted function
3630
   * @param bound_vars the parameters to this function
3631
   * @param term the function body
3632
   * @param global determines whether this definition is global (i.e. persists
3633
   *               when popping the context)
3634
   * @return the function
3635
   */
3636
  Term defineFunRec(const Term& fun,
3637
                    const std::vector<Term>& bound_vars,
3638
                    const Term& term,
3639
                    bool global = false) const;
3640
3641
  /**
3642
   * Define recursive functions.
3643
   * SMT-LIB:
3644
   * \verbatim
3645
   *   ( define-funs-rec ( <function_decl>^{n+1} ) ( <term>^{n+1} ) )
3646
   * \endverbatim
3647
   * Create elements of parameter 'funs' with mkConst().
3648
   * @param funs the sorted functions
3649
   * @param bound_vars the list of parameters to the functions
3650
   * @param terms the list of function bodies of the functions
3651
   * @param global determines whether this definition is global (i.e. persists
3652
   *               when popping the context)
3653
   * @return the function
3654
   */
3655
  void defineFunsRec(const std::vector<Term>& funs,
3656
                     const std::vector<std::vector<Term>>& bound_vars,
3657
                     const std::vector<Term>& terms,
3658
                     bool global = false) const;
3659
3660
  /**
3661
   * Echo a given string to the given output stream.
3662
   * SMT-LIB:
3663
   * \verbatim
3664
   * ( echo <std::string> )
3665
   * \endverbatim
3666
   * @param out the output stream
3667
   * @param str the string to echo
3668
   */
3669
  void echo(std::ostream& out, const std::string& str) const;
3670
3671
  /**
3672
   * Get the list of asserted formulas.
3673
   * SMT-LIB:
3674
   * \verbatim
3675
   * ( get-assertions )
3676
   * \endverbatim
3677
   * @return the list of asserted formulas
3678
   */
3679
  std::vector<Term> getAssertions() const;
3680
3681
  /**
3682
   * Get info from the solver.
3683
   * SMT-LIB: \verbatim( get-info <info_flag> )\verbatim
3684
   * @return the info
3685
   */
3686
  std::string getInfo(const std::string& flag) const;
3687
3688
  /**
3689
   * Get the value of a given option.
3690
   * SMT-LIB:
3691
   * \verbatim
3692
   * ( get-option <keyword> )
3693
   * \endverbatim
3694
   * @param option the option for which the value is queried
3695
   * @return a string representation of the option value
3696
   */
3697
  std::string getOption(const std::string& option) const;
3698
3699
  /**
3700
   * Get the set of unsat ("failed") assumptions.
3701
   * SMT-LIB:
3702
   * \verbatim
3703
   * ( get-unsat-assumptions )
3704
   * \endverbatim
3705
   * Requires to enable option 'produce-unsat-assumptions'.
3706
   * @return the set of unsat assumptions.
3707
   */
3708
  std::vector<Term> getUnsatAssumptions() const;
3709
3710
  /**
3711
   * Get the unsatisfiable core.
3712
   * SMT-LIB:
3713
   * \verbatim
3714
   * ( get-unsat-core )
3715
   * \endverbatim
3716
   * Requires to enable option 'produce-unsat-cores'.
3717
   * @return a set of terms representing the unsatisfiable core
3718
   */
3719
  std::vector<Term> getUnsatCore() const;
3720
3721
  /**
3722
   * Get the value of the given term.
3723
   * SMT-LIB:
3724
   * \verbatim
3725
   * ( get-value ( <term> ) )
3726
   * \endverbatim
3727
   * @param term the term for which the value is queried
3728
   * @return the value of the given term
3729
   */
3730
  Term getValue(const Term& term) const;
3731
  /**
3732
   * Get the values of the given terms.
3733
   * SMT-LIB:
3734
   * \verbatim
3735
   * ( get-value ( <term>+ ) )
3736
   * \endverbatim
3737
   * @param terms the terms for which the value is queried
3738
   * @return the values of the given terms
3739
   */
3740
  std::vector<Term> getValue(const std::vector<Term>& terms) const;
3741
3742
  /**
3743
   * Do quantifier elimination.
3744
   * SMT-LIB:
3745
   * \verbatim
3746
   * ( get-qe <q> )
3747
   * \endverbatim
3748
   * Requires a logic that supports quantifier elimination. Currently, the only
3749
   * logics supported by quantifier elimination is LRA and LIA.
3750
   * @param q a quantified formula of the form:
3751
   *   Q x1...xn. P( x1...xn, y1...yn )
3752
   * where P( x1...xn, y1...yn ) is a quantifier-free formula
3753
   * @return a formula ret such that, given the current set of formulas A
3754
   * asserted to this solver:
3755
   *   - ( A ^ q ) and ( A ^ ret ) are equivalent
3756
   *   - ret is quantifier-free formula containing only free variables in
3757
   *     y1...yn.
3758
   */
3759
  Term getQuantifierElimination(const Term& q) const;
3760
3761
  /**
3762
   * Do partial quantifier elimination, which can be used for incrementally
3763
   * computing the result of a quantifier elimination.
3764
   * SMT-LIB:
3765
   * \verbatim
3766
   * ( get-qe-disjunct <q> )
3767
   * \endverbatim
3768
   * Requires a logic that supports quantifier elimination. Currently, the only
3769
   * logics supported by quantifier elimination is LRA and LIA.
3770
   * @param q a quantified formula of the form:
3771
   *   Q x1...xn. P( x1...xn, y1...yn )
3772
   * where P( x1...xn, y1...yn ) is a quantifier-free formula
3773
   * @return a formula ret such that, given the current set of formulas A
3774
   * asserted to this solver:
3775
   *   - (A ^ q) => (A ^ ret) if Q is forall or (A ^ ret) => (A ^ q) if Q is
3776
   *     exists,
3777
   *   - ret is quantifier-free formula containing only free variables in
3778
   *     y1...yn,
3779
   *   - If Q is exists, let A^Q_n be the formula
3780
   *       A ^ ~ret^Q_1 ^ ... ^ ~ret^Q_n
3781
   *     where for each i=1,...n, formula ret^Q_i is the result of calling
3782
   *     getQuantifierEliminationDisjunct for q with the set of assertions
3783
   *     A^Q_{i-1}. Similarly, if Q is forall, then let A^Q_n be
3784
   *       A ^ ret^Q_1 ^ ... ^ ret^Q_n
3785
   *     where ret^Q_i is the same as above. In either case, we have
3786
   *     that ret^Q_j will eventually be true or false, for some finite j.
3787
   */
3788
  Term getQuantifierEliminationDisjunct(const Term& q) const;
3789
3790
  /**
3791
   * When using separation logic, this sets the location sort and the
3792
   * datatype sort to the given ones. This method should be invoked exactly
3793
   * once, before any separation logic constraints are provided.
3794
   * @param locSort The location sort of the heap
3795
   * @param dataSort The data sort of the heap
3796
   */
3797
  void declareSeparationHeap(const Sort& locSort, const Sort& dataSort) const;
3798
3799
  /**
3800
   * When using separation logic, obtain the term for the heap.
3801
   * @return The term for the heap
3802
   */
3803
  Term getSeparationHeap() const;
3804
3805
  /**
3806
   * When using separation logic, obtain the term for nil.
3807
   * @return The term for nil
3808
   */
3809
  Term getSeparationNilTerm() const;
3810
3811
  /**
3812
   * Declare a symbolic pool of terms with the given initial value.
3813
   * SMT-LIB:
3814
   * \verbatim
3815
   * ( declare-pool <symbol> <sort> ( <term>* ) )
3816
   * \endverbatim
3817
   * @param symbol The name of the pool
3818
   * @param sort The sort of the elements of the pool.
3819
   * @param initValue The initial value of the pool
3820
   */
3821
  Term declarePool(const std::string& symbol,
3822
                   const Sort& sort,
3823
                   const std::vector<Term>& initValue) const;
3824
  /**
3825
   * Pop (a) level(s) from the assertion stack.
3826
   * SMT-LIB:
3827
   * \verbatim
3828
   * ( pop <numeral> )
3829
   * \endverbatim
3830
   * @param nscopes the number of levels to pop
3831
   */
3832
  void pop(uint32_t nscopes = 1) const;
3833
3834
  /**
3835
   * Get an interpolant
3836
   * SMT-LIB:
3837
   * \verbatim
3838
   * ( get-interpol <conj> )
3839
   * \endverbatim
3840
   * Requires to enable option 'produce-interpols'.
3841
   * @param conj the conjecture term
3842
   * @param output a Term I such that A->I and I->B are valid, where A is the
3843
   *        current set of assertions and B is given in the input by conj.
3844
   * @return true if it gets I successfully, false otherwise.
3845
   */
3846
  bool getInterpolant(const Term& conj, Term& output) const;
3847
3848
  /**
3849
   * Get an interpolant
3850
   * SMT-LIB:
3851
   * \verbatim
3852
   * ( get-interpol <conj> <g> )
3853
   * \endverbatim
3854
   * Requires to enable option 'produce-interpols'.
3855
   * @param conj the conjecture term
3856
   * @param grammar the grammar for the interpolant I
3857
   * @param output a Term I such that A->I and I->B are valid, where A is the
3858
   *        current set of assertions and B is given in the input by conj.
3859
   * @return true if it gets I successfully, false otherwise.
3860
   */
3861
  bool getInterpolant(const Term& conj, Grammar& grammar, Term& output) const;
3862
3863
  /**
3864
   * Get an abduct.
3865
   * SMT-LIB:
3866
   * \verbatim
3867
   * ( get-abduct <conj> )
3868
   * \endverbatim
3869
   * Requires enabling option 'produce-abducts'
3870
   * @param conj the conjecture term
3871
   * @param output a term C such that A^C is satisfiable, and A^~B^C is
3872
   *        unsatisfiable, where A is the current set of assertions and B is
3873
   *        given in the input by conj
3874
   * @return true if it gets C successfully, false otherwise
3875
   */
3876
  bool getAbduct(const Term& conj, Term& output) const;
3877
3878
  /**
3879
   * Get an abduct.
3880
   * SMT-LIB:
3881
   * \verbatim
3882
   * ( get-abduct <conj> <g> )
3883
   * \endverbatim
3884
   * Requires enabling option 'produce-abducts'
3885
   * @param conj the conjecture term
3886
   * @param grammar the grammar for the abduct C
3887
   * @param output a term C such that A^C is satisfiable, and A^~B^C is
3888
   *        unsatisfiable, where A is the current set of assertions and B is
3889
   *        given in the input by conj
3890
   * @return true if it gets C successfully, false otherwise
3891
   */
3892
  bool getAbduct(const Term& conj, Grammar& grammar, Term& output) const;
3893
3894
  /**
3895
   * Block the current model. Can be called only if immediately preceded by a
3896
   * SAT or INVALID query.
3897
   * SMT-LIB:
3898
   * \verbatim
3899
   * ( block-model )
3900
   * \endverbatim
3901
   * Requires enabling 'produce-models' option and setting 'block-models' option
3902
   * to a mode other than "none".
3903
   */
3904
  void blockModel() const;
3905
3906
  /**
3907
   * Block the current model values of (at least) the values in terms. Can be
3908
   * called only if immediately preceded by a SAT or NOT_ENTAILED query.
3909
   * SMT-LIB:
3910
   * \verbatim
3911
   * ( block-model-values ( <terms>+ ) )
3912
   * \endverbatim
3913
   * Requires enabling 'produce-models' option and setting 'block-models' option
3914
   * to a mode other than "none".
3915
   */
3916
  void blockModelValues(const std::vector<Term>& terms) const;
3917
3918
  /**
3919
   * Print all instantiations made by the quantifiers module.
3920
   * @param out the output stream
3921
   */
3922
  void printInstantiations(std::ostream& out) const;
3923
3924
  /**
3925
   * Push (a) level(s) to the assertion stack.
3926
   * SMT-LIB:
3927
   * \verbatim
3928
   * ( push <numeral> )
3929
   * \endverbatim
3930
   * @param nscopes the number of levels to push
3931
   */
3932
  void push(uint32_t nscopes = 1) const;
3933
3934
  /**
3935
   * Remove all assertions.
3936
   * SMT-LIB:
3937
   * \verbatim
3938
   * ( reset-assertions )
3939
   * \endverbatim
3940
   */
3941
  void resetAssertions() const;
3942
3943
  /**
3944
   * Set info.
3945
   * SMT-LIB:
3946
   * \verbatim
3947
   * ( set-info <attribute> )
3948
   * \endverbatim
3949
   * @param keyword the info flag
3950
   * @param value the value of the info flag
3951
   */
3952
  void setInfo(const std::string& keyword, const std::string& value) const;
3953
3954
  /**
3955
   * Set logic.
3956
   * SMT-LIB:
3957
   * \verbatim
3958
   * ( set-logic <symbol> )
3959
   * \endverbatim
3960
   * @param logic the logic to set
3961
   */
3962
  void setLogic(const std::string& logic) const;
3963
3964
  /**
3965
   * Set option.
3966
   * SMT-LIB:
3967
   * \verbatim
3968
   *   ( set-option <option> )
3969
   * \endverbatim
3970
   * @param option the option name
3971
   * @param value the option value
3972
   */
3973
  void setOption(const std::string& option, const std::string& value) const;
3974
3975
  /**
3976
   * If needed, convert this term to a given sort. Note that the sort of the
3977
   * term must be convertible into the target sort. Currently only Int to Real
3978
   * conversions are supported.
3979
   * @param t the term
3980
   * @param s the target sort
3981
   * @return the term wrapped into a sort conversion if needed
3982
   */
3983
  Term ensureTermSort(const Term& t, const Sort& s) const;
3984
3985
  /**
3986
   * Append \p symbol to the current list of universal variables.
3987
   * SyGuS v2:
3988
   * \verbatim
3989
   *   ( declare-var <symbol> <sort> )
3990
   * \endverbatim
3991
   * @param sort the sort of the universal variable
3992
   * @param symbol the name of the universal variable
3993
   * @return the universal variable
3994
   */
3995
  Term mkSygusVar(const Sort& sort,
3996
                  const std::string& symbol = std::string()) const;
3997
3998
  /**
3999
   * Create a Sygus grammar. The first non-terminal is treated as the starting
4000
   * non-terminal, so the order of non-terminals matters.
4001
   *
4002
   * @param boundVars the parameters to corresponding synth-fun/synth-inv
4003
   * @param ntSymbols the pre-declaration of the non-terminal symbols
4004
   * @return the grammar
4005
   */
4006
  Grammar mkSygusGrammar(const std::vector<Term>& boundVars,
4007
                         const std::vector<Term>& ntSymbols) const;
4008
4009
  /**
4010
   * Synthesize n-ary function.
4011
   * SyGuS v2:
4012
   * \verbatim
4013
   *   ( synth-fun <symbol> ( <boundVars>* ) <sort> )
4014
   * \endverbatim
4015
   * @param symbol the name of the function
4016
   * @param boundVars the parameters to this function
4017
   * @param sort the sort of the return value of this function
4018
   * @return the function
4019
   */
4020
  Term synthFun(const std::string& symbol,
4021
                const std::vector<Term>& boundVars,
4022
                const Sort& sort) const;
4023
4024
  /**
4025
   * Synthesize n-ary function following specified syntactic constraints.
4026
   * SyGuS v2:
4027
   * \verbatim
4028
   *   ( synth-fun <symbol> ( <boundVars>* ) <sort> <g> )
4029
   * \endverbatim
4030
   * @param symbol the name of the function
4031
   * @param boundVars the parameters to this function
4032
   * @param sort the sort of the return value of this function
4033
   * @param grammar the syntactic constraints
4034
   * @return the function
4035
   */
4036
  Term synthFun(const std::string& symbol,
4037
                const std::vector<Term>& boundVars,
4038
                Sort sort,
4039
                Grammar& grammar) const;
4040
4041
  /**
4042
   * Synthesize invariant.
4043
   * SyGuS v2:
4044
   * \verbatim
4045
   *   ( synth-inv <symbol> ( <boundVars>* ) )
4046
   * \endverbatim
4047
   * @param symbol the name of the invariant
4048
   * @param boundVars the parameters to this invariant
4049
   * @return the invariant
4050
   */
4051
  Term synthInv(const std::string& symbol,
4052
                const std::vector<Term>& boundVars) const;
4053
4054
  /**
4055
   * Synthesize invariant following specified syntactic constraints.
4056
   * SyGuS v2:
4057
   * \verbatim
4058
   *   ( synth-inv <symbol> ( <boundVars>* ) <g> )
4059
   * \endverbatim
4060
   * @param symbol the name of the invariant
4061
   * @param boundVars the parameters to this invariant
4062
   * @param grammar the syntactic constraints
4063
   * @return the invariant
4064
   */
4065
  Term synthInv(const std::string& symbol,
4066
                const std::vector<Term>& boundVars,
4067
                Grammar& grammar) const;
4068
4069
  /**
4070
   * Add a forumla to the set of Sygus constraints.
4071
   * SyGuS v2:
4072
   * \verbatim
4073
   *   ( constraint <term> )
4074
   * \endverbatim
4075
   * @param term the formula to add as a constraint
4076
   */
4077
  void addSygusConstraint(const Term& term) const;
4078
4079
  /**
4080
   * Add a set of Sygus constraints to the current state that correspond to an
4081
   * invariant synthesis problem.
4082
   * SyGuS v2:
4083
   * \verbatim
4084
   *   ( inv-constraint <inv> <pre> <trans> <post> )
4085
   * \endverbatim
4086
   * @param inv the function-to-synthesize
4087
   * @param pre the pre-condition
4088
   * @param trans the transition relation
4089
   * @param post the post-condition
4090
   */
4091
  void addSygusInvConstraint(Term inv, Term pre, Term trans, Term post) const;
4092
4093
  /**
4094
   * Try to find a solution for the synthesis conjecture corresponding to the
4095
   * current list of functions-to-synthesize, universal variables and
4096
   * constraints.
4097
   * SyGuS v2:
4098
   * \verbatim
4099
   *   ( check-synth )
4100
   * \endverbatim
4101
   * @return the result of the synthesis conjecture.
4102
   */
4103
  Result checkSynth() const;
4104
4105
  /**
4106
   * Get the synthesis solution of the given term. This method should be called
4107
   * immediately after the solver answers unsat for sygus input.
4108
   * @param term the term for which the synthesis solution is queried
4109
   * @return the synthesis solution of the given term
4110
   */
4111
  Term getSynthSolution(Term term) const;
4112
4113
  /**
4114
   * Get the synthesis solutions of the given terms. This method should be
4115
   * called immediately after the solver answers unsat for sygus input.
4116
   * @param terms the terms for which the synthesis solutions is queried
4117
   * @return the synthesis solutions of the given terms
4118
   */
4119
  std::vector<Term> getSynthSolutions(const std::vector<Term>& terms) const;
4120
4121
  // !!! This is only temporarily available until the parser is fully migrated
4122
  // to the new API. !!!
4123
  SmtEngine* getSmtEngine(void) const;
4124
4125
  // !!! This is only temporarily available until options are refactored at
4126
  // the driver level. !!!
4127
  Options& getOptions(void);
4128
4129
  /**
4130
   * Returns a snapshot of the current state of the statistic values of this
4131
   * solver. The returned object is completely decoupled from the solver and
4132
   * will not change when the solver is used again.
4133
   */
4134
  Statistics getStatistics() const;
4135
4136
 private:
4137
  /** @return the node manager of this solver */
4138
  NodeManager* getNodeManager(void) const;
4139
  /** Reset the API statistics */
4140
  void resetStatistics();
4141
4142
  /** Helper to check for API misuse in mkOp functions. */
4143
  void checkMkTerm(Kind kind, uint32_t nchildren) const;
4144
  /** Helper for mk-functions that call d_nodeMgr->mkConst(). */
4145
  template <typename T>
4146
  Term mkValHelper(T t) const;
4147
  /** Helper for mkReal functions that take a string as argument. */
4148
  Term mkRealFromStrHelper(const std::string& s) const;
4149
  /** Helper for mkBitVector functions that take a string as argument. */
4150
  Term mkBVFromStrHelper(const std::string& s, uint32_t base) const;
4151
  /**
4152
   * Helper for mkBitVector functions that take a string and a size as
4153
   * arguments.
4154
   */
4155
  Term mkBVFromStrHelper(uint32_t size,
4156
                         const std::string& s,
4157
                         uint32_t base) const;
4158
  /** Helper for mkBitVector functions that take an integer as argument. */
4159
  Term mkBVFromIntHelper(uint32_t size, uint64_t val) const;
4160
  /** Helper for functions that create tuple sorts. */
4161
  Sort mkTupleSortHelper(const std::vector<Sort>& sorts) const;
4162
  /** Helper for mkTerm functions that create Term from a Kind */
4163
  Term mkTermFromKind(Kind kind) const;
4164
  /** Helper for mkChar functions that take a string as argument. */
4165
  Term mkCharFromStrHelper(const std::string& s) const;
4166
  /** Get value helper, which accounts for subtyping */
4167
  Term getValueHelper(const Term& term) const;
4168
4169
  /**
4170
   * Helper function that ensures that a given term is of sort real (as opposed
4171
   * to being of sort integer).
4172
   * @param t a term of sort integer or real
4173
   * @return a term of sort real
4174
   */
4175
  Term ensureRealSort(const Term& t) const;
4176
4177
  /**
4178
   * Create n-ary term of given kind. This handles the cases of left/right
4179
   * associative operators, chainable operators, and cases when the number of
4180
   * children exceeds the maximum arity for the kind.
4181
   * @param kind the kind of the term
4182
   * @param children the children of the term
4183
   * @return the Term
4184
   */
4185
  Term mkTermHelper(Kind kind, const std::vector<Term>& children) const;
4186
4187
  /**
4188
   * Create n-ary term of given kind from a given operator.
4189
   * @param op the operator
4190
   * @param children the children of the term
4191
   * @return the Term
4192
   */
4193
  Term mkTermHelper(const Op& op, const std::vector<Term>& children) const;
4194
4195
  /**
4196
   * Create a vector of datatype sorts, using unresolved sorts.
4197
   * @param dtypedecls the datatype declarations from which the sort is created
4198
   * @param unresolvedSorts the list of unresolved sorts
4199
   * @return the datatype sorts
4200
   */
4201
  std::vector<Sort> mkDatatypeSortsInternal(
4202
      const std::vector<DatatypeDecl>& dtypedecls,
4203
      const std::set<Sort>& unresolvedSorts) const;
4204
4205
  /**
4206
   * Synthesize n-ary function following specified syntactic constraints.
4207
   * SMT-LIB:
4208
   * \verbatim
4209
   *   ( synth-fun <symbol> ( <boundVars>* ) <sort> <g>? )
4210
   * \endverbatim
4211
   * @param symbol the name of the function
4212
   * @param boundVars the parameters to this function
4213
   * @param sort the sort of the return value of this function
4214
   * @param isInv determines whether this is synth-fun or synth-inv
4215
   * @param grammar the syntactic constraints
4216
   * @return the function
4217
   */
4218
  Term synthFunHelper(const std::string& symbol,
4219
                      const std::vector<Term>& boundVars,
4220
                      const Sort& sort,
4221
                      bool isInv = false,
4222
                      Grammar* grammar = nullptr) const;
4223
4224
  /** Check whether string s is a valid decimal integer. */
4225
  bool isValidInteger(const std::string& s) const;
4226
4227
  /** Increment the term stats counter. */
4228
  void increment_term_stats(Kind kind) const;
4229
  /** Increment the vars stats (if 'is_var') or consts stats counter. */
4230
  void increment_vars_consts_stats(const Sort& sort, bool is_var) const;
4231
4232
  /** Keep a copy of the original option settings (for resets). */
4233
  std::unique_ptr<Options> d_originalOptions;
4234
  /** The node manager of this solver. */
4235
  std::unique_ptr<NodeManager> d_nodeMgr;
4236
  /** The statistics collected on the Api level. */
4237
  std::unique_ptr<APIStatistics> d_stats;
4238
  /** The SMT engine of this solver. */
4239
  std::unique_ptr<SmtEngine> d_smtEngine;
4240
  /** The random number generator of this solver. */
4241
  std::unique_ptr<Random> d_rng;
4242
};
4243
4244
}  // namespace cvc5::api
4245
4246
#endif