1 /********************* */
4 ** Top contributors (to current version):
5 ** Andrew Reynolds, Morgan Deters, Tim King
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2019 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.\endverbatim
12 ** \brief A class representing a Datatype definition
14 ** A class representing a Datatype definition for the theory of
15 ** inductive datatypes.
18 #include "cvc4_public.h"
20 #ifndef CVC4__DATATYPE_H
21 #define CVC4__DATATYPE_H
30 // messy; Expr needs Datatype (because it's the payload of a
31 // CONSTANT-kinded expression), and Datatype needs Expr.
32 //class CVC4_PUBLIC Datatype;
33 class CVC4_PUBLIC DatatypeIndexConstant
;
36 #include "base/exception.h"
37 #include "expr/expr.h"
38 #include "expr/type.h"
39 #include "util/hash.h"
44 class CVC4_PUBLIC ExprManager
;
46 class CVC4_PUBLIC DatatypeConstructor
;
47 class CVC4_PUBLIC DatatypeConstructorArg
;
49 class CVC4_PUBLIC DatatypeConstructorIterator
{
50 const std::vector
<DatatypeConstructor
>* d_v
;
53 friend class Datatype
;
55 DatatypeConstructorIterator(const std::vector
<DatatypeConstructor
>& v
, bool start
) : d_v(&v
), d_i(start
? 0 : v
.size()) {
59 typedef const DatatypeConstructor
& value_type
;
60 const DatatypeConstructor
& operator*() const { return (*d_v
)[d_i
]; }
61 const DatatypeConstructor
* operator->() const { return &(*d_v
)[d_i
]; }
62 DatatypeConstructorIterator
& operator++() { ++d_i
; return *this; }
63 DatatypeConstructorIterator
operator++(int) { DatatypeConstructorIterator
i(*this); ++d_i
; return i
; }
64 bool operator==(const DatatypeConstructorIterator
& other
) const { return d_v
== other
.d_v
&& d_i
== other
.d_i
; }
65 bool operator!=(const DatatypeConstructorIterator
& other
) const { return d_v
!= other
.d_v
|| d_i
!= other
.d_i
; }
66 };/* class DatatypeConstructorIterator */
68 class CVC4_PUBLIC DatatypeConstructorArgIterator
{
69 const std::vector
<DatatypeConstructorArg
>* d_v
;
72 friend class DatatypeConstructor
;
74 DatatypeConstructorArgIterator(const std::vector
<DatatypeConstructorArg
>& v
, bool start
) : d_v(&v
), d_i(start
? 0 : v
.size()) {
78 typedef const DatatypeConstructorArg
& value_type
;
79 const DatatypeConstructorArg
& operator*() const { return (*d_v
)[d_i
]; }
80 const DatatypeConstructorArg
* operator->() const { return &(*d_v
)[d_i
]; }
81 DatatypeConstructorArgIterator
& operator++() { ++d_i
; return *this; }
82 DatatypeConstructorArgIterator
operator++(int) { DatatypeConstructorArgIterator
i(*this); ++d_i
; return i
; }
83 bool operator==(const DatatypeConstructorArgIterator
& other
) const { return d_v
== other
.d_v
&& d_i
== other
.d_i
; }
84 bool operator!=(const DatatypeConstructorArgIterator
& other
) const { return d_v
!= other
.d_v
|| d_i
!= other
.d_i
; }
85 };/* class DatatypeConstructorArgIterator */
88 * An exception that is thrown when a datatype resolution fails.
90 class CVC4_PUBLIC DatatypeResolutionException
: public Exception
{
92 inline DatatypeResolutionException(std::string msg
);
93 };/* class DatatypeResolutionException */
96 * A holder type (used in calls to DatatypeConstructor::addArg())
97 * to allow a Datatype to refer to itself. Self-typed fields of
98 * Datatypes will be properly typed when a Type is created for the
99 * Datatype by the ExprManager (which calls Datatype::resolve()).
101 class CVC4_PUBLIC DatatypeSelfType
{
102 };/* class DatatypeSelfType */
107 * An unresolved type (used in calls to
108 * DatatypeConstructor::addArg()) to allow a Datatype to refer to
109 * itself or to other mutually-recursive Datatypes. Unresolved-type
110 * fields of Datatypes will be properly typed when a Type is created
111 * for the Datatype by the ExprManager (which calls
112 * Datatype::resolve()).
114 class CVC4_PUBLIC DatatypeUnresolvedType
{
117 inline DatatypeUnresolvedType(std::string name
);
118 inline std::string
getName() const;
119 };/* class DatatypeUnresolvedType */
122 * A Datatype constructor argument (i.e., a Datatype field).
124 class CVC4_PUBLIC DatatypeConstructorArg
{
125 friend class DatatypeConstructor
;
126 friend class Datatype
;
129 /** Get the name of this constructor argument. */
130 std::string
getName() const;
133 * Get the selector for this constructor argument; this call is
134 * only permitted after resolution.
136 Expr
getSelector() const;
139 * Get the associated constructor for this constructor argument;
140 * this call is only permitted after resolution.
142 Expr
getConstructor() const;
145 * Get the type of the selector for this constructor argument;
146 * this call is only permitted after resolution.
148 SelectorType
getType() const;
151 * Get the range type of this argument.
153 Type
getRangeType() const;
156 * Returns true iff this constructor argument has been resolved.
158 bool isResolved() const;
160 /** prints this datatype constructor argument to stream */
161 void toStream(std::ostream
& out
) const;
164 /** The internal representation */
165 std::shared_ptr
<DTypeSelector
> d_internal
;
168 /** is this selector unresolved? */
169 bool isUnresolvedSelf() const;
171 DatatypeConstructorArg(std::string name
, Expr selector
);
172 };/* class DatatypeConstructorArg */
176 /** sygus datatype constructor printer
178 * This is a virtual class that is used to specify
179 * a custom printing callback for sygus terms. This is
180 * useful for sygus grammars that include defined
181 * functions or let expressions.
183 class CVC4_PUBLIC SygusPrintCallback
186 SygusPrintCallback() {}
187 virtual ~SygusPrintCallback() {}
189 * Writes the term that sygus datatype expression e
190 * encodes to stream out. p is the printer that
191 * requested that expression e be written on output
192 * stream out. Calls may be made to p to print
195 virtual void toStreamSygus(const Printer
* p
,
200 class DTypeConstructor
;
203 * A constructor for a Datatype.
205 class CVC4_PUBLIC DatatypeConstructor
{
206 friend class Datatype
;
209 /** The type for iterators over constructor arguments. */
210 typedef DatatypeConstructorArgIterator iterator
;
211 /** The (const) type for iterators over constructor arguments. */
212 typedef DatatypeConstructorArgIterator const_iterator
;
215 * Create a new Datatype constructor with the given name for the
216 * constructor and the same name (prefixed with "is_") for the
217 * tester. The actual constructor and tester (meaning, the Exprs
218 * representing operators for these entities) aren't created until
221 explicit DatatypeConstructor(std::string name
);
224 * Create a new Datatype constructor with the given name for the
225 * constructor and the given name for the tester. The actual
226 * constructor and tester aren't created until resolution time.
227 * weight is the value that this constructor carries when computing size.
228 * For example, if A, B, C have weights 0, 1, and 3 respectively, then
229 * C( B( A() ), B( A() ) ) has size 5.
231 DatatypeConstructor(std::string name
,
233 unsigned weight
= 1);
235 ~DatatypeConstructor() {}
237 * Add an argument (i.e., a data field) of the given name and type
238 * to this Datatype constructor. Selector names need not be unique;
239 * they are for convenience and pretty-printing only.
241 void addArg(std::string selectorName
, Type selectorType
);
244 * Add an argument (i.e., a data field) of the given name to this
245 * Datatype constructor that refers to an as-yet-unresolved
246 * Datatype (which may be mutually-recursive). Selector names need
247 * not be unique; they are for convenience and pretty-printing only.
249 void addArg(std::string selectorName
, DatatypeUnresolvedType selectorType
);
252 * Add a self-referential (i.e., a data field) of the given name
253 * to this Datatype constructor that refers to the enclosing
254 * Datatype. For example, using the familiar "nat" Datatype, to
255 * create the "pred" field for "succ" constructor, one uses
256 * succ::addArg("pred", DatatypeSelfType())---the actual Type
257 * cannot be passed because the Datatype is still under
258 * construction. Selector names need not be unique; they are for
259 * convenience and pretty-printing only.
261 * This is a special case of
262 * DatatypeConstructor::addArg(std::string, DatatypeUnresolvedType).
264 void addArg(std::string selectorName
, DatatypeSelfType
);
266 /** Get the name of this Datatype constructor. */
267 std::string
getName() const;
270 * Get the constructor operator of this Datatype constructor. The
271 * Datatype must be resolved.
273 Expr
getConstructor() const;
276 * Get the tester operator of this Datatype constructor. The
277 * Datatype must be resolved.
279 Expr
getTester() const;
283 * This method returns the operator or
284 * term that this constructor represents
285 * in the sygus encoding. This may be a
286 * builtin operator, defined function, variable,
287 * or constant that this constructor encodes in this
290 Expr
getSygusOp() const;
291 /** is this a sygus identity function?
293 * This returns true if the sygus operator of this datatype constructor is
294 * of the form (lambda (x) x).
296 bool isSygusIdFunc() const;
297 /** get sygus print callback
299 * This class stores custom ways of printing
300 * sygus datatype constructors, for instance,
301 * to handle defined or let expressions that
302 * appear in user-provided grammars.
304 std::shared_ptr
<SygusPrintCallback
> getSygusPrintCallback() const;
307 * Get the weight of this constructor. This value is used when computing the
308 * size of datatype terms that involve this constructor.
310 unsigned getWeight() const;
313 * Get the tester name for this Datatype constructor.
315 std::string
getTesterName() const;
318 * Get the number of arguments (so far) of this Datatype constructor.
320 size_t getNumArgs() const;
323 * Get the specialized constructor type for a parametric
324 * constructor; this call is only permitted after resolution.
325 * Given a (concrete) returnType, the constructor's concrete
326 * type in this parametric datatype is returned.
328 * For instance, if the datatype is list[T], with constructor
329 * "cons[T]" of type "T -> list[T] -> list[T]", then calling
330 * this function with "list[int]" will return the concrete
331 * "cons" constructor type for lists of int---namely,
332 * "int -> list[int] -> list[int]".
334 Type
getSpecializedConstructorType(Type returnType
) const;
337 * Return the cardinality of this constructor (the product of the
338 * cardinalities of its arguments).
340 Cardinality
getCardinality(Type t
) const;
343 * Return true iff this constructor is finite (it is nullary or
344 * each of its argument types are finite). This function can
345 * only be called for resolved constructors.
347 bool isFinite(Type t
) const;
349 * Return true iff this constructor is finite (it is nullary or
350 * each of its argument types are finite) under assumption
351 * uninterpreted sorts are finite. This function can
352 * only be called for resolved constructors.
354 bool isInterpretedFinite(Type t
) const;
357 * Returns true iff this Datatype constructor has already been
360 bool isResolved() const;
362 /** Get the beginning iterator over DatatypeConstructor args. */
364 /** Get the ending iterator over DatatypeConstructor args. */
366 /** Get the beginning const_iterator over DatatypeConstructor args. */
367 const_iterator
begin() const;
368 /** Get the ending const_iterator over DatatypeConstructor args. */
369 const_iterator
end() const;
371 /** Get the ith DatatypeConstructor arg. */
372 const DatatypeConstructorArg
& operator[](size_t index
) const;
375 * Get the DatatypeConstructor arg named. This is a linear search
376 * through the arguments, so in the case of multiple,
377 * similarly-named arguments, the first is returned.
379 const DatatypeConstructorArg
& operator[](std::string name
) const;
382 * Get the selector named. This is a linear search
383 * through the arguments, so in the case of multiple,
384 * similarly-named arguments, the selector for the first
387 Expr
getSelector(std::string name
) const;
389 * Get argument type. Returns the return type of the i^th selector of this
392 Type
getArgType(unsigned i
) const;
394 /** get selector internal
396 * This gets selector for the index^th argument
397 * of this constructor. The type dtt is the datatype
398 * type whose datatype is the owner of this constructor,
399 * where this type may be an instantiated parametric datatype.
401 * If shared selectors are enabled,
402 * this returns a shared (constructor-agnotic) selector, which
403 * in the terminology of "Datatypes with Shared Selectors", is:
404 * sel_{dtt}^{T,atos(T,C,index)}
405 * where C is this constructor, and T is the type
406 * of the index^th field of this constructor.
407 * The semantics of sel_{dtt}^{T,n}( t ) is the n^th field of
408 * type T of constructor term t if one exists, or is
409 * unconstrained otherwise.
411 Expr
getSelectorInternal(Type dtt
, size_t index
) const;
413 /** get selector index internal
415 * This gets the argument number of this constructor
416 * that the selector sel accesses. It returns -1 if the
417 * selector sel is not a selector for this constructor.
419 * In the terminology of "Datatypes with Shared Selectors",
420 * if sel is sel_{dtt}^{T,index} for some (T, index), where
421 * dtt is the datatype type whose datatype is the owner
422 * of this constructor, then this method returns
425 int getSelectorIndexInternal( Expr sel
) const;
427 /** involves external type
429 * Get whether this constructor has a subfield
430 * in any constructor that is not a datatype type.
432 bool involvesExternalType() const;
433 /** involves external type
435 * Get whether this constructor has a subfield
436 * in any constructor that is an uninterpreted type.
438 bool involvesUninterpretedType() const;
442 * Set that this constructor is a sygus datatype constructor that encodes
443 * operator op. spc is the sygus callback of this datatype constructor,
444 * which is stored in a shared pointer.
446 void setSygus(Expr op
, std::shared_ptr
<SygusPrintCallback
> spc
);
449 * Get the list of arguments to this constructor.
451 const std::vector
<DatatypeConstructorArg
>* getArgs() const;
453 /** prints this datatype constructor to stream */
454 void toStream(std::ostream
& out
) const;
457 /** The internal representation */
458 std::shared_ptr
<DTypeConstructor
> d_internal
;
459 /** the name of the tester */
460 std::string d_testerName
;
461 /** The constructor */
463 /** the arguments of this constructor */
464 std::vector
<DatatypeConstructorArg
> d_args
;
465 /** sygus print callback */
466 std::shared_ptr
<SygusPrintCallback
> d_sygus_pc
;
467 };/* class DatatypeConstructor */
472 * The representation of an inductive datatype.
474 * This is far more complicated than it first seems. Consider this
475 * datatype definition:
482 * You cannot define "nat" until you have a Type for it, but you
483 * cannot have a Type for it until you fill in the type of the "pred"
484 * selector, which needs the Type. So we have a chicken-and-egg
485 * problem. It's even more complicated when we have mutual recursion
486 * between datatypes, since the CVC presentation language does not
487 * require forward-declarations. Here, we define trees of lists that
488 * contain trees of lists (etc):
491 * tree = node(left: tree, right: tree) | leaf(list),
492 * list = cons(car: tree, cdr: list) | nil
495 * Note that while parsing the "tree" definition, we have to take it
496 * on faith that "list" is a valid type. We build Datatype objects to
497 * describe "tree" and "list", and their constructors and constructor
498 * arguments, but leave any unknown types (including self-references)
499 * in an "unresolved" state. After parsing the whole DATATYPE block,
500 * we create a DatatypeType through
501 * ExprManager::mkMutualDatatypeTypes(). The ExprManager creates a
502 * DatatypeType for each, but before "releasing" this type into the
503 * wild, it does a round of in-place "resolution" on each Datatype by
504 * calling Datatype::resolve() with a map of string -> DatatypeType to
505 * allow the datatype to construct the necessary testers and
508 * An additional point to make is that we want to ease the burden on
509 * both the parser AND the users of the CVC4 API, so this class takes
510 * on the task of generating its own selectors and testers, for
511 * instance. That means that, after reifying the Datatype with the
512 * ExprManager, the parser needs to go through the (now-resolved)
513 * Datatype and request the constructor, selector, and tester terms.
514 * See src/parser/parser.cpp for how this is done. For API usage
515 * ideas, see test/unit/util/datatype_black.h.
517 * Datatypes may also be defined parametrically, such as this example:
520 * list[T] = cons(car : T, cdr : list[T]) | null,
521 * tree = node(children : list[tree]) | leaf
524 * Here, the definition of the parametric datatype list, where T is a type variable.
525 * In other words, this defines a family of types list[C] where C is any concrete
526 * type. Datatypes can be parameterized over multiple type variables using the
527 * syntax sym[ T1, ..., Tn ] = ...,
530 class CVC4_PUBLIC Datatype
{
531 friend class DatatypeConstructor
;
532 friend class ExprManager
; // for access to resolve()
533 friend class NodeManager
; // temporary, for access to d_internal
536 * Get the datatype of a constructor, selector, or tester operator.
538 static const Datatype
& datatypeOf(Expr item
) CVC4_PUBLIC
;
541 * Get the index of a constructor or tester in its datatype, or the
542 * index of a selector in its constructor. (Zero is always the
545 static size_t indexOf(Expr item
) CVC4_PUBLIC
;
548 * Get the index of constructor corresponding to selector. (Zero is
549 * always the first index.)
551 static size_t cindexOf(Expr item
) CVC4_PUBLIC
;
554 * Same as above, but without checks. These methods should be used by
555 * internal (Node-level) code.
557 static size_t indexOfInternal(Expr item
);
558 static size_t cindexOfInternal(Expr item
);
560 /** The type for iterators over constructors. */
561 typedef DatatypeConstructorIterator iterator
;
562 /** The (const) type for iterators over constructors. */
563 typedef DatatypeConstructorIterator const_iterator
;
565 /** Create a new Datatype of the given name. */
566 explicit Datatype(ExprManager
* em
, std::string name
, bool isCo
= false);
569 * Create a new Datatype of the given name, with the given
572 Datatype(ExprManager
* em
,
574 const std::vector
<Type
>& params
,
579 /** Add a constructor to this Datatype.
581 * Notice that constructor names need not
582 * be unique; they are for convenience and pretty-printing only.
584 void addConstructor(const DatatypeConstructor
& c
);
588 * This marks this datatype as a sygus datatype.
589 * A sygus datatype is one that represents terms of type st
590 * via a deep embedding described in Section 4 of
591 * Reynolds et al. CAV 2015. We say that this sygus datatype
592 * "encodes" its sygus type st in the following.
594 * st : the type this datatype encodes (this can be Int, Bool, etc.),
595 * bvl : the list of arguments for the synth-fun
596 * allow_const : whether all constants are (implicitly) allowed by the
598 * allow_all : whether all terms are (implicitly) allowed by the datatype
600 * Notice that allow_const/allow_all do not reflect the constructors
601 * for this datatype, and instead are used solely for relaxing constraints
602 * when doing solution reconstruction (Figure 5 of Reynolds et al.
605 void setSygus( Type st
, Expr bvl
, bool allow_const
, bool allow_all
);
606 /** add sygus constructor
608 * This adds a sygus constructor to this datatype, where
609 * this datatype should be currently unresolved.
611 * op : the builtin operator, constant, or variable that
612 * this constructor encodes
613 * cname : the name of the constructor (for printing only)
614 * cargs : the arguments of the constructor
615 * spc : an (optional) callback that is used for custom printing. This is
616 * to accomodate user-provided grammars in the sygus format.
618 * It should be the case that cargs are sygus datatypes that
619 * encode the arguments of op. For example, a sygus constructor
620 * with op = PLUS should be such that cargs.size()>=2 and
621 * the sygus type of cargs[i] is Real/Int for each i.
623 * weight denotes the value added by the constructor when computing the size
624 * of datatype terms. Passing a value <0 denotes the default weight for the
625 * constructor, which is 0 for nullary constructors and 1 for non-nullary
628 void addSygusConstructor(Expr op
,
629 const std::string
& cname
,
630 const std::vector
<Type
>& cargs
,
631 std::shared_ptr
<SygusPrintCallback
> spc
= nullptr,
634 /** set that this datatype is a tuple */
637 /** set that this datatype is a record */
640 /** Get the name of this Datatype. */
641 std::string
getName() const;
643 /** Get the number of constructors (so far) for this Datatype. */
644 size_t getNumConstructors() const;
646 /** Is this datatype parametric? */
647 bool isParametric() const;
649 /** Get the nubmer of type parameters */
650 size_t getNumParameters() const;
653 Type
getParameter(unsigned int i
) const;
655 /** Get parameters */
656 std::vector
<Type
> getParameters() const;
658 /** is this a co-datatype? */
659 bool isCodatatype() const;
661 /** is this a sygus datatype? */
662 bool isSygus() const;
664 /** is this a tuple datatype? */
665 bool isTuple() const;
667 /** is this a record datatype? */
668 bool isRecord() const;
670 /** get the record representation for this datatype */
671 Record
* getRecord() const;
674 * Return the cardinality of this datatype.
675 * The Datatype must be resolved.
677 * The version of this method that takes Type t is required
678 * for parametric datatypes, where t is an instantiated
679 * parametric datatype type whose datatype is this class.
681 Cardinality
getCardinality(Type t
) const;
682 Cardinality
getCardinality() const;
685 * Return true iff this Datatype has finite cardinality. If the
686 * datatype is not well-founded, this method returns false. The
687 * Datatype must be resolved or an exception is thrown.
689 * The version of this method that takes Type t is required
690 * for parametric datatypes, where t is an instantiated
691 * parametric datatype type whose datatype is this class.
693 bool isFinite(Type t
) const;
694 bool isFinite() const;
697 * Return true iff this Datatype is finite (all constructors are
698 * finite, i.e., there are finitely many ground terms) under the
699 * assumption unintepreted sorts are finite. If the
700 * datatype is not well-founded, this method returns false. The
701 * Datatype must be resolved or an exception is thrown.
703 * The versions of these methods that takes Type t is required
704 * for parametric datatypes, where t is an instantiated
705 * parametric datatype type whose datatype is this class.
707 bool isInterpretedFinite(Type t
) const;
708 bool isInterpretedFinite() const;
712 * Return true iff this datatype is well-founded (there exist finite
713 * values of this type).
714 * This datatype must be resolved or an exception is thrown.
716 bool isWellFounded() const;
718 /** is recursive singleton
720 * Return true iff this datatype is a recursive singleton
721 * (a recursive singleton is a recursive datatype with only
722 * one infinite value). For details, see Reynolds et al. CADE 2015.
724 * The versions of these methods that takes Type t is required
725 * for parametric datatypes, where t is an instantiated
726 * parametric datatype type whose datatype is this class.
728 bool isRecursiveSingleton(Type t
) const;
729 bool isRecursiveSingleton() const;
731 /** recursive single arguments
733 * Get recursive singleton argument types (uninterpreted sorts that the
734 * cardinality of this datatype is dependent upon). For example, for :
735 * stream := cons( head1 : U1, head2 : U2, tail : stream )
736 * Then, the recursive singleton argument types of stream are { U1, U2 },
737 * since if U1 and U2 have cardinality one, then stream has cardinality
740 * The versions of these methods that takes Type t is required
741 * for parametric datatypes, where t is an instantiated
742 * parametric datatype type whose datatype is this class.
744 unsigned getNumRecursiveSingletonArgTypes(Type t
) const;
745 Type
getRecursiveSingletonArgType(Type t
, unsigned i
) const;
746 unsigned getNumRecursiveSingletonArgTypes() const;
747 Type
getRecursiveSingletonArgType(unsigned i
) const;
750 * Construct and return a ground term of this Datatype. The
751 * Datatype must be both resolved and well-founded, or else an
752 * exception is thrown.
754 * This method takes a Type t, which is a datatype type whose
755 * datatype is this class, which may be an instantiated datatype
756 * type if this datatype is parametric.
758 Expr
mkGroundTerm(Type t
) const;
759 /** Make ground value
761 * Same as above, but constructs a constant value instead of a ground term.
762 * These two notions typically coincide. However, for uninterpreted sorts,
763 * they do not: mkGroundTerm returns a fresh variable whereas mkValue returns
764 * an uninterpreted constant. The motivation for mkGroundTerm is that
765 * unintepreted constants should never appear in lemmas. The motivation for
766 * mkGroundValue is for things like type enumeration and model construction.
768 Expr
mkGroundValue(Type t
) const;
771 * Get the DatatypeType associated to this Datatype. Can only be
772 * called post-resolution.
774 DatatypeType
getDatatypeType() const;
777 * Get the DatatypeType associated to this (parameterized) Datatype. Can only be
778 * called post-resolution.
780 DatatypeType
getDatatypeType(const std::vector
<Type
>& params
) const;
783 * Return true iff the two Datatypes are the same.
785 * We need == for mkConst(Datatype) to properly work---since if the
786 * Datatype Expr requested is the same as an already-existing one,
787 * we need to return that one. For that, we have a hash and
788 * operator==. We provide != for symmetry. We don't provide
789 * operator<() etc. because given two Datatype Exprs, you could
790 * simply compare those rather than the (bare) Datatypes. This
791 * means, though, that Datatype cannot be stored in a sorted list or
792 * RB tree directly, so maybe we can consider adding these
793 * comparison operators later on.
795 bool operator==(const Datatype
& other
) const;
796 /** Return true iff the two Datatypes are not the same. */
797 bool operator!=(const Datatype
& other
) const;
799 /** Return true iff this Datatype has already been resolved. */
800 bool isResolved() const;
802 /** Get the beginning iterator over DatatypeConstructors. */
804 /** Get the ending iterator over DatatypeConstructors. */
806 /** Get the beginning const_iterator over DatatypeConstructors. */
807 const_iterator
begin() const;
808 /** Get the ending const_iterator over DatatypeConstructors. */
809 const_iterator
end() const;
811 /** Get the ith DatatypeConstructor. */
812 const DatatypeConstructor
& operator[](size_t index
) const;
815 * Get the DatatypeConstructor named. This is a linear search
816 * through the constructors, so in the case of multiple,
817 * similarly-named constructors, the first is returned.
819 const DatatypeConstructor
& operator[](std::string name
) const;
822 * Get the constructor operator for the named constructor.
823 * This is a linear search through the constructors, so in
824 * the case of multiple, similarly-named constructors, the
827 * This Datatype must be resolved.
829 Expr
getConstructor(std::string name
) const;
832 * This gets the built-in type associated with
833 * this sygus datatype, i.e. the type of the
834 * term that this sygus datatype encodes.
836 Type
getSygusType() const;
838 /** get sygus var list
839 * This gets the variable list of the function
840 * to synthesize using this sygus datatype.
841 * For example, if we are synthesizing a binary
842 * function f where solutions are of the form:
843 * f = (lambda (xy) t[x,y])
844 * In this case, this method returns the
845 * bound variable list containing x and y.
847 Expr
getSygusVarList() const;
848 /** get sygus allow constants
850 * Does this sygus datatype allow constants?
851 * Notice that this is not a property of the
852 * constructors of this datatype. Instead, it is
853 * an auxiliary flag (provided in the call
856 bool getSygusAllowConst() const;
857 /** get sygus allow all
859 * Does this sygus datatype allow all terms?
860 * Notice that this is not a property of the
861 * constructors of this datatype. Instead, it is
862 * an auxiliary flag (provided in the call
865 bool getSygusAllowAll() const;
867 /** involves external type
868 * Get whether this datatype has a subfield
869 * in any constructor that is not a datatype type.
871 bool involvesExternalType() const;
872 /** involves uninterpreted type
873 * Get whether this datatype has a subfield
874 * in any constructor that is an uninterpreted type.
876 bool involvesUninterpretedType() const;
879 * Get the list of constructors.
881 const std::vector
<DatatypeConstructor
>* getConstructors() const;
883 /** prints this datatype to stream */
884 void toStream(std::ostream
& out
) const;
887 /** The expression manager that created this datatype */
889 /** The internal representation */
890 std::shared_ptr
<DType
> d_internal
;
893 /** the data of the record for this datatype (if applicable) */
895 /** whether the datatype is a record */
897 /** the constructors of this datatype */
898 std::vector
<DatatypeConstructor
> d_constructors
;
900 * Datatypes refer to themselves, recursively, and we have a
901 * chicken-and-egg problem. The DatatypeType around the Datatype
902 * cannot exist until the Datatype is finalized, and the Datatype
903 * cannot refer to the DatatypeType representing itself until it
904 * exists. resolve() is called by the ExprManager when a Type is
905 * ultimately requested of the Datatype specification (that is, when
906 * ExprManager::mkDatatypeType() or ExprManager::mkMutualDatatypeTypes()
907 * is called). Has the effect of freezing the object, too; that is,
908 * addConstructor() will fail after a call to resolve().
910 * The basic goal of resolution is to assign constructors, selectors,
911 * and testers. To do this, any UnresolvedType/SelfType references
912 * must be cleared up. This is the purpose of the "resolutions" map;
913 * it includes any mutually-recursive datatypes that are currently
914 * under resolution. The four vectors come in two pairs (so, really
915 * they are two maps). placeholders->replacements give type variables
916 * that should be resolved in the case of parametric datatypes.
918 * @param resolutions a map of strings to DatatypeTypes currently under
920 * @param placeholders the types in these Datatypes under resolution that must
922 * @param replacements the corresponding replacements
923 * @param paramTypes the sort constructors in these Datatypes under resolution
924 * that must be replaced
925 * @param paramReplacements the corresponding (parametric) DatatypeTypes
927 void resolve(const std::map
<std::string
, DatatypeType
>& resolutions
,
928 const std::vector
<Type
>& placeholders
,
929 const std::vector
<Type
>& replacements
,
930 const std::vector
<SortConstructorType
>& paramTypes
,
931 const std::vector
<DatatypeType
>& paramReplacements
);
932 };/* class Datatype */
935 * A hash function for Datatypes. Needed to store them in hash sets
938 struct CVC4_PUBLIC DatatypeHashFunction
{
939 inline size_t operator()(const Datatype
& dt
) const {
940 return std::hash
<std::string
>()(dt
.getName());
942 inline size_t operator()(const Datatype
* dt
) const {
943 return std::hash
<std::string
>()(dt
->getName());
945 inline size_t operator()(const DatatypeConstructor
& dtc
) const {
946 return std::hash
<std::string
>()(dtc
.getName());
948 inline size_t operator()(const DatatypeConstructor
* dtc
) const {
949 return std::hash
<std::string
>()(dtc
->getName());
951 };/* struct DatatypeHashFunction */
955 /* stores an index to Datatype residing in NodeManager */
956 class CVC4_PUBLIC DatatypeIndexConstant
{
958 DatatypeIndexConstant(unsigned index
);
960 unsigned getIndex() const { return d_index
; }
961 bool operator==(const DatatypeIndexConstant
& uc
) const
963 return d_index
== uc
.d_index
;
965 bool operator!=(const DatatypeIndexConstant
& uc
) const
967 return !(*this == uc
);
969 bool operator<(const DatatypeIndexConstant
& uc
) const
971 return d_index
< uc
.d_index
;
973 bool operator<=(const DatatypeIndexConstant
& uc
) const
975 return d_index
<= uc
.d_index
;
977 bool operator>(const DatatypeIndexConstant
& uc
) const
979 return !(*this <= uc
);
981 bool operator>=(const DatatypeIndexConstant
& uc
) const
983 return !(*this < uc
);
986 const unsigned d_index
;
987 };/* class DatatypeIndexConstant */
989 std::ostream
& operator<<(std::ostream
& out
, const DatatypeIndexConstant
& dic
) CVC4_PUBLIC
;
991 struct CVC4_PUBLIC DatatypeIndexConstantHashFunction
{
992 inline size_t operator()(const DatatypeIndexConstant
& dic
) const {
993 return IntegerHashFunction()(dic
.getIndex());
995 };/* struct DatatypeIndexConstantHashFunction */
999 // FUNCTION DECLARATIONS FOR OUTPUT STREAMS
1001 std::ostream
& operator<<(std::ostream
& os
, const Datatype
& dt
) CVC4_PUBLIC
;
1002 std::ostream
& operator<<(std::ostream
& os
, const DatatypeConstructor
& ctor
) CVC4_PUBLIC
;
1003 std::ostream
& operator<<(std::ostream
& os
, const DatatypeConstructorArg
& arg
) CVC4_PUBLIC
;
1007 inline DatatypeResolutionException::DatatypeResolutionException(std::string msg
) :
1011 inline DatatypeUnresolvedType::DatatypeUnresolvedType(std::string name
) :
1015 inline std::string
DatatypeUnresolvedType::getName() const { return d_name
; }
1018 }/* CVC4 namespace */
1020 #endif /* CVC4__DATATYPE_H */