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.
17 #include "expr/datatype.h"
22 #include "base/check.h"
23 #include "expr/attribute.h"
24 #include "expr/expr_manager.h"
25 #include "expr/expr_manager_scope.h"
26 #include "expr/node.h"
27 #include "expr/node_algorithm.h"
28 #include "expr/node_manager.h"
29 #include "expr/type.h"
30 #include "expr/type_matcher.h"
31 #include "options/datatypes_options.h"
32 #include "options/set_language.h"
33 #include "theory/type_enumerator.h"
41 struct DatatypeIndexTag
{};
42 struct DatatypeConsIndexTag
{};
43 struct DatatypeFiniteTag
{};
44 struct DatatypeFiniteComputedTag
{};
45 struct DatatypeUFiniteTag
{};
46 struct DatatypeUFiniteComputedTag
{};
47 }/* CVC4::expr::attr namespace */
48 }/* CVC4::expr namespace */
50 typedef expr::Attribute
<expr::attr::DatatypeIndexTag
, uint64_t> DatatypeIndexAttr
;
51 typedef expr::Attribute
<expr::attr::DatatypeConsIndexTag
, uint64_t> DatatypeConsIndexAttr
;
52 typedef expr::Attribute
<expr::attr::DatatypeFiniteTag
, bool> DatatypeFiniteAttr
;
53 typedef expr::Attribute
<expr::attr::DatatypeFiniteComputedTag
, bool> DatatypeFiniteComputedAttr
;
54 typedef expr::Attribute
<expr::attr::DatatypeUFiniteTag
, bool> DatatypeUFiniteAttr
;
55 typedef expr::Attribute
<expr::attr::DatatypeUFiniteComputedTag
, bool> DatatypeUFiniteComputedAttr
;
57 Datatype::~Datatype(){
61 const Datatype
& Datatype::datatypeOf(Expr item
) {
62 ExprManagerScope
ems(item
);
63 TypeNode t
= Node::fromExpr(item
).getType();
65 case kind::CONSTRUCTOR_TYPE
:
66 return DatatypeType(t
[t
.getNumChildren() - 1].toType()).getDatatype();
67 case kind::SELECTOR_TYPE
:
68 case kind::TESTER_TYPE
:
69 return DatatypeType(t
[0].toType()).getDatatype();
71 Unhandled() << "arg must be a datatype constructor, selector, or tester";
75 size_t Datatype::indexOf(Expr item
) {
76 ExprManagerScope
ems(item
);
77 PrettyCheckArgument(item
.getType().isConstructor() ||
78 item
.getType().isTester() ||
79 item
.getType().isSelector(),
81 "arg must be a datatype constructor, selector, or tester");
82 return indexOfInternal(item
);
85 size_t Datatype::indexOfInternal(Expr item
)
87 TNode n
= Node::fromExpr(item
);
88 if( item
.getKind()==kind::APPLY_TYPE_ASCRIPTION
){
89 return indexOf( item
[0] );
91 Assert(n
.hasAttribute(DatatypeIndexAttr()));
92 return n
.getAttribute(DatatypeIndexAttr());
96 size_t Datatype::cindexOf(Expr item
) {
97 ExprManagerScope
ems(item
);
98 PrettyCheckArgument(item
.getType().isSelector(),
100 "arg must be a datatype selector");
101 return cindexOfInternal(item
);
103 size_t Datatype::cindexOfInternal(Expr item
)
105 TNode n
= Node::fromExpr(item
);
106 if( item
.getKind()==kind::APPLY_TYPE_ASCRIPTION
){
107 return cindexOf( item
[0] );
109 Assert(n
.hasAttribute(DatatypeConsIndexAttr()));
110 return n
.getAttribute(DatatypeConsIndexAttr());
114 void Datatype::resolve(ExprManager
* em
,
115 const std::map
<std::string
, DatatypeType
>& resolutions
,
116 const std::vector
<Type
>& placeholders
,
117 const std::vector
<Type
>& replacements
,
118 const std::vector
< SortConstructorType
>& paramTypes
,
119 const std::vector
< DatatypeType
>& paramReplacements
)
121 PrettyCheckArgument(em
!= NULL
, em
, "cannot resolve a Datatype with a NULL expression manager");
122 PrettyCheckArgument(!d_resolved
, this, "cannot resolve a Datatype twice");
123 PrettyCheckArgument(resolutions
.find(d_name
) != resolutions
.end(), resolutions
,
124 "Datatype::resolve(): resolutions doesn't contain me!");
125 PrettyCheckArgument(placeholders
.size() == replacements
.size(), placeholders
,
126 "placeholders and replacements must be the same size");
127 PrettyCheckArgument(paramTypes
.size() == paramReplacements
.size(), paramTypes
,
128 "paramTypes and paramReplacements must be the same size");
129 PrettyCheckArgument(getNumConstructors() > 0, *this, "cannot resolve a Datatype that has no constructors");
130 DatatypeType self
= (*resolutions
.find(d_name
)).second
;
131 PrettyCheckArgument(&self
.getDatatype() == this, resolutions
, "Datatype::resolve(): resolutions doesn't contain me!");
134 for(std::vector
<DatatypeConstructor
>::iterator i
= d_constructors
.begin(), i_end
= d_constructors
.end(); i
!= i_end
; ++i
) {
135 (*i
).resolve(em
, self
, resolutions
, placeholders
, replacements
, paramTypes
, paramReplacements
, index
);
136 Node::fromExpr((*i
).d_constructor
).setAttribute(DatatypeIndexAttr(), index
);
137 Node::fromExpr((*i
).d_tester
).setAttribute(DatatypeIndexAttr(), index
++);
141 d_involvesExt
= false;
142 d_involvesUt
= false;
143 for(const_iterator i
= begin(); i
!= end(); ++i
) {
144 if( (*i
).involvesExternalType() ){
145 d_involvesExt
= true;
147 if( (*i
).involvesUninterpretedType() ){
153 std::vector
< std::pair
<std::string
, Type
> > fields
;
154 for( unsigned i
=0; i
<(*this)[0].getNumArgs(); i
++ ){
155 fields
.push_back( std::pair
<std::string
, Type
>( (*this)[0][i
].getName(), (*this)[0][i
].getRangeType() ) );
157 d_record
= new Record(fields
);
162 // all datatype constructors should be sygus and have sygus operators whose
163 // free variables are subsets of sygus bound var list.
164 Node sbvln
= Node::fromExpr(d_sygus_bvl
);
165 std::unordered_set
<Node
, NodeHashFunction
> svs
;
166 for (const Node
& sv
: sbvln
)
170 for (unsigned i
= 0, ncons
= d_constructors
.size(); i
< ncons
; i
++)
172 Expr sop
= d_constructors
[i
].getSygusOp();
173 PrettyCheckArgument(!sop
.isNull(),
175 "Sygus datatype contains a non-sygus constructor");
176 Node sopn
= Node::fromExpr(sop
);
177 std::unordered_set
<Node
, NodeHashFunction
> fvs
;
178 expr::getFreeVariables(sopn
, fvs
);
179 for (const Node
& v
: fvs
)
182 svs
.find(v
) != svs
.end(),
184 "Sygus constructor has an operator with a free variable that is "
185 "not in the formal argument list of the function-to-synthesize");
191 void Datatype::addConstructor(const DatatypeConstructor
& c
) {
192 PrettyCheckArgument(!d_resolved
, this,
193 "cannot add a constructor to a finalized Datatype");
194 d_constructors
.push_back(c
);
198 void Datatype::setSygus( Type st
, Expr bvl
, bool allow_const
, bool allow_all
){
199 PrettyCheckArgument(!d_resolved
, this,
200 "cannot set sygus type to a finalized Datatype");
203 d_sygus_allow_const
= allow_const
|| allow_all
;
204 d_sygus_allow_all
= allow_all
;
207 void Datatype::addSygusConstructor(Expr op
,
208 const std::string
& cname
,
209 const std::vector
<Type
>& cargs
,
210 std::shared_ptr
<SygusPrintCallback
> spc
,
213 Debug("dt-sygus") << "--> Add constructor " << cname
<< " to " << getName() << std::endl
;
214 Debug("dt-sygus") << " sygus op : " << op
<< std::endl
;
215 // avoid name clashes
216 std::stringstream ss
;
217 ss
<< getName() << "_" << getNumConstructors() << "_" << cname
;
218 std::string name
= ss
.str();
219 std::string
testerId("is-");
220 testerId
.append(name
);
221 unsigned cweight
= weight
>= 0 ? weight
: (cargs
.empty() ? 0 : 1);
222 DatatypeConstructor
c(name
, testerId
, cweight
);
224 for( unsigned j
=0; j
<cargs
.size(); j
++ ){
225 Debug("parser-sygus-debug") << " arg " << j
<< " : " << cargs
[j
] << std::endl
;
226 std::stringstream sname
;
227 sname
<< name
<< "_" << j
;
228 c
.addArg(sname
.str(), cargs
[j
]);
233 void Datatype::setTuple() {
234 PrettyCheckArgument(!d_resolved
, this, "cannot set tuple to a finalized Datatype");
238 void Datatype::setRecord() {
239 PrettyCheckArgument(!d_resolved
, this, "cannot set record to a finalized Datatype");
243 Cardinality
Datatype::getCardinality(Type t
) const
245 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
246 Assert(t
.isDatatype() && ((DatatypeType
)t
).getDatatype() == *this);
247 std::vector
< Type
> processing
;
248 computeCardinality( t
, processing
);
252 Cardinality
Datatype::getCardinality() const
254 PrettyCheckArgument(!isParametric(), this, "for getCardinality, this datatype cannot be parametric");
255 return getCardinality( d_self
);
258 Cardinality
Datatype::computeCardinality(Type t
,
259 std::vector
<Type
>& processing
) const
261 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
262 if( std::find( processing
.begin(), processing
.end(), d_self
)!=processing
.end() ){
263 d_card
= Cardinality::INTEGERS
;
265 processing
.push_back( d_self
);
267 for(const_iterator i
= begin(), i_end
= end(); i
!= i_end
; ++i
) {
268 c
+= (*i
).computeCardinality( t
, processing
);
271 processing
.pop_back();
276 bool Datatype::isRecursiveSingleton(Type t
) const
278 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
279 Assert(t
.isDatatype() && ((DatatypeType
)t
).getDatatype() == *this);
280 if( d_card_rec_singleton
.find( t
)==d_card_rec_singleton
.end() ){
281 if( isCodatatype() ){
282 Assert(d_card_u_assume
[t
].empty());
283 std::vector
< Type
> processing
;
284 if( computeCardinalityRecSingleton( t
, processing
, d_card_u_assume
[t
] ) ){
285 d_card_rec_singleton
[t
] = 1;
287 d_card_rec_singleton
[t
] = -1;
289 if( d_card_rec_singleton
[t
]==1 ){
290 Trace("dt-card") << "Datatype " << getName() << " is recursive singleton, dependent upon " << d_card_u_assume
[t
].size() << " uninterpreted sorts: " << std::endl
;
291 for( unsigned i
=0; i
<d_card_u_assume
[t
].size(); i
++ ){
292 Trace("dt-card") << " " << d_card_u_assume
[t
][i
] << std::endl
;
294 Trace("dt-card") << std::endl
;
297 d_card_rec_singleton
[t
] = -1;
300 return d_card_rec_singleton
[t
]==1;
303 bool Datatype::isRecursiveSingleton() const
305 PrettyCheckArgument(!isParametric(), this, "for isRecursiveSingleton, this datatype cannot be parametric");
306 return isRecursiveSingleton( d_self
);
309 unsigned Datatype::getNumRecursiveSingletonArgTypes(Type t
) const
311 Assert(d_card_rec_singleton
.find(t
) != d_card_rec_singleton
.end());
312 Assert(isRecursiveSingleton(t
));
313 return d_card_u_assume
[t
].size();
316 unsigned Datatype::getNumRecursiveSingletonArgTypes() const
318 PrettyCheckArgument(!isParametric(), this, "for getNumRecursiveSingletonArgTypes, this datatype cannot be parametric");
319 return getNumRecursiveSingletonArgTypes( d_self
);
322 Type
Datatype::getRecursiveSingletonArgType(Type t
, unsigned i
) const
324 Assert(d_card_rec_singleton
.find(t
) != d_card_rec_singleton
.end());
325 Assert(isRecursiveSingleton(t
));
326 return d_card_u_assume
[t
][i
];
329 Type
Datatype::getRecursiveSingletonArgType(unsigned i
) const
331 PrettyCheckArgument(!isParametric(), this, "for getRecursiveSingletonArgType, this datatype cannot be parametric");
332 return getRecursiveSingletonArgType( d_self
, i
);
335 bool Datatype::computeCardinalityRecSingleton(Type t
,
336 std::vector
<Type
>& processing
,
337 std::vector
<Type
>& u_assume
) const
339 if( std::find( processing
.begin(), processing
.end(), d_self
)!=processing
.end() ){
342 if( d_card_rec_singleton
[t
]==0 ){
343 //if not yet computed
344 if( d_constructors
.size()==1 ){
345 bool success
= false;
346 processing
.push_back( d_self
);
347 for(unsigned i
= 0; i
<d_constructors
[0].getNumArgs(); i
++ ) {
348 Type tc
= ((SelectorType
)d_constructors
[0][i
].getType()).getRangeType();
349 //if it is an uninterpreted sort, then we depend on it having cardinality one
351 if( std::find( u_assume
.begin(), u_assume
.end(), tc
)==u_assume
.end() ){
352 u_assume
.push_back( tc
);
354 //if it is a datatype, recurse
355 }else if( tc
.isDatatype() ){
356 const Datatype
& dt
= ((DatatypeType
)tc
).getDatatype();
357 if( !dt
.computeCardinalityRecSingleton( t
, processing
, u_assume
) ){
362 //if it is a builtin type, it must have cardinality one
363 }else if( !tc
.getCardinality().isOne() ){
367 processing
.pop_back();
372 }else if( d_card_rec_singleton
[t
]==-1 ){
375 for( unsigned i
=0; i
<d_card_u_assume
[t
].size(); i
++ ){
376 if( std::find( u_assume
.begin(), u_assume
.end(), d_card_u_assume
[t
][i
] )==u_assume
.end() ){
377 u_assume
.push_back( d_card_u_assume
[t
][i
] );
385 bool Datatype::isFinite(Type t
) const
387 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
388 Assert(t
.isDatatype() && ((DatatypeType
)t
).getDatatype() == *this);
390 // we're using some internals, so we have to set up this library context
391 ExprManagerScope
ems(d_self
);
392 TypeNode self
= TypeNode::fromType(d_self
);
393 // is this already in the cache ?
394 if(self
.getAttribute(DatatypeFiniteComputedAttr())) {
395 return self
.getAttribute(DatatypeFiniteAttr());
397 for(const_iterator i
= begin(), i_end
= end(); i
!= i_end
; ++i
) {
398 if(! (*i
).isFinite( t
)) {
399 self
.setAttribute(DatatypeFiniteComputedAttr(), true);
400 self
.setAttribute(DatatypeFiniteAttr(), false);
404 self
.setAttribute(DatatypeFiniteComputedAttr(), true);
405 self
.setAttribute(DatatypeFiniteAttr(), true);
408 bool Datatype::isFinite() const
410 PrettyCheckArgument(isResolved() && !isParametric(), this, "this datatype must be resolved and not parametric");
411 return isFinite( d_self
);
414 bool Datatype::isInterpretedFinite(Type t
) const
416 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
417 Assert(t
.isDatatype() && ((DatatypeType
)t
).getDatatype() == *this);
418 // we're using some internals, so we have to set up this library context
419 ExprManagerScope
ems(d_self
);
420 TypeNode self
= TypeNode::fromType(d_self
);
421 // is this already in the cache ?
422 if(self
.getAttribute(DatatypeUFiniteComputedAttr())) {
423 return self
.getAttribute(DatatypeUFiniteAttr());
425 //start by assuming it is not
426 self
.setAttribute(DatatypeUFiniteComputedAttr(), true);
427 self
.setAttribute(DatatypeUFiniteAttr(), false);
428 for(const_iterator i
= begin(), i_end
= end(); i
!= i_end
; ++i
) {
429 if(! (*i
).isInterpretedFinite( t
)) {
433 self
.setAttribute(DatatypeUFiniteComputedAttr(), true);
434 self
.setAttribute(DatatypeUFiniteAttr(), true);
437 bool Datatype::isInterpretedFinite() const
439 PrettyCheckArgument(isResolved() && !isParametric(), this, "this datatype must be resolved and not parametric");
440 return isInterpretedFinite( d_self
);
443 bool Datatype::isWellFounded() const
445 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
446 if( d_well_founded
==0 ){
447 // we're using some internals, so we have to set up this library context
448 ExprManagerScope
ems(d_self
);
449 std::vector
< Type
> processing
;
450 if( computeWellFounded( processing
) ){
456 return d_well_founded
==1;
459 bool Datatype::computeWellFounded(std::vector
<Type
>& processing
) const
461 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
462 if( std::find( processing
.begin(), processing
.end(), d_self
)!=processing
.end() ){
465 processing
.push_back( d_self
);
466 for(const_iterator i
= begin(), i_end
= end(); i
!= i_end
; ++i
) {
467 if( (*i
).computeWellFounded( processing
) ){
468 processing
.pop_back();
471 Trace("dt-wf") << "Constructor " << (*i
).getName() << " is not well-founded." << std::endl
;
474 processing
.pop_back();
475 Trace("dt-wf") << "Datatype " << getName() << " is not well-founded." << std::endl
;
480 Expr
Datatype::mkGroundTerm(Type t
) const
482 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
483 return mkGroundTermInternal(t
, false);
486 Expr
Datatype::mkGroundValue(Type t
) const
488 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
489 return mkGroundTermInternal(t
, true);
492 Expr
Datatype::mkGroundTermInternal(Type t
, bool isValue
) const
494 ExprManagerScope
ems(d_self
);
495 Debug("datatypes") << "mkGroundTerm of type " << t
496 << ", isValue = " << isValue
<< std::endl
;
497 // is this already in the cache ?
498 std::map
<Type
, Expr
>& cache
= isValue
? d_ground_value
: d_ground_term
;
499 std::map
<Type
, Expr
>::iterator it
= cache
.find(t
);
500 if (it
!= cache
.end())
502 Debug("datatypes") << "\nin cache: " << d_self
<< " => " << it
->second
<< std::endl
;
505 std::vector
<Type
> processing
;
506 Expr groundTerm
= computeGroundTerm(t
, processing
, isValue
);
507 if (!groundTerm
.isNull())
509 // we found a ground-term-constructing constructor!
510 cache
[t
] = groundTerm
;
511 Debug("datatypes") << "constructed: " << getName() << " => " << groundTerm
514 if (groundTerm
.isNull())
518 // if we get all the way here, we aren't well-founded
521 "datatype is not well-founded, cannot construct a ground term!");
527 Expr
getSubtermWithType( Expr e
, Type t
, bool isTop
){
528 if( !isTop
&& e
.getType()==t
){
531 for( unsigned i
=0; i
<e
.getNumChildren(); i
++ ){
532 Expr se
= getSubtermWithType( e
[i
], t
, false );
541 Expr
Datatype::computeGroundTerm(Type t
,
542 std::vector
<Type
>& processing
,
545 if( std::find( processing
.begin(), processing
.end(), t
)==processing
.end() ){
546 processing
.push_back( t
);
547 for( unsigned r
=0; r
<2; r
++ ){
548 for(const_iterator i
= begin(), i_end
= end(); i
!= i_end
; ++i
) {
549 //do nullary constructors first
550 if( ((*i
).getNumArgs()==0)==(r
==0)){
551 Debug("datatypes") << "Try constructing for " << (*i
).getName() << ", processing = " << processing
.size() << std::endl
;
553 (*i
).computeGroundTerm(t
, processing
, d_ground_term
, isValue
);
555 //must check subterms for the same type to avoid infinite loops in type enumeration
556 Expr se
= getSubtermWithType( e
, t
, true );
558 Debug("datatypes") << "Take subterm " << se
<< std::endl
;
561 processing
.pop_back();
564 Debug("datatypes") << "...failed." << std::endl
;
569 processing
.pop_back();
571 Debug("datatypes") << "...already processing " << t
<< " " << d_self
<< std::endl
;
576 DatatypeType
Datatype::getDatatypeType() const
578 PrettyCheckArgument(isResolved(), *this, "Datatype must be resolved to get its DatatypeType");
579 PrettyCheckArgument(!d_self
.isNull(), *this);
580 return DatatypeType(d_self
);
583 DatatypeType
Datatype::getDatatypeType(const std::vector
<Type
>& params
) const
585 PrettyCheckArgument(isResolved(), *this, "Datatype must be resolved to get its DatatypeType");
586 PrettyCheckArgument(!d_self
.isNull() && DatatypeType(d_self
).isParametric(), this);
587 return DatatypeType(d_self
).instantiate(params
);
590 bool Datatype::operator==(const Datatype
& other
) const
592 // two datatypes are == iff the name is the same and they have
593 // exactly matching constructors (in the same order)
599 if(isResolved() != other
.isResolved()) {
603 if( d_name
!= other
.d_name
||
604 getNumConstructors() != other
.getNumConstructors() ) {
607 for(const_iterator i
= begin(), j
= other
.begin(); i
!= end(); ++i
, ++j
) {
608 Assert(j
!= other
.end());
609 // two constructors are == iff they have the same name, their
610 // constructors and testers are equal and they have exactly
611 // matching args (in the same order)
612 if((*i
).getName() != (*j
).getName() ||
613 (*i
).getNumArgs() != (*j
).getNumArgs()) {
616 // testing equivalence of constructors and testers is harder b/c
617 // this constructor might not be resolved yet; only compare them
618 // if they are both resolved
619 Assert(isResolved() == !(*i
).d_constructor
.isNull()
620 && isResolved() == !(*i
).d_tester
.isNull()
621 && (*i
).d_constructor
.isNull() == (*j
).d_constructor
.isNull()
622 && (*i
).d_tester
.isNull() == (*j
).d_tester
.isNull());
623 if(!(*i
).d_constructor
.isNull() && (*i
).d_constructor
!= (*j
).d_constructor
) {
626 if(!(*i
).d_tester
.isNull() && (*i
).d_tester
!= (*j
).d_tester
) {
629 for(DatatypeConstructor::const_iterator k
= (*i
).begin(), l
= (*j
).begin(); k
!= (*i
).end(); ++k
, ++l
) {
630 Assert(l
!= (*j
).end());
631 if((*k
).getName() != (*l
).getName()) {
634 // testing equivalence of selectors is harder b/c args might not
636 Assert(isResolved() == (*k
).isResolved()
637 && (*k
).isResolved() == (*l
).isResolved());
638 if((*k
).isResolved()) {
639 // both are resolved, so simply compare the selectors directly
640 if((*k
).d_selector
!= (*l
).d_selector
) {
644 // neither is resolved, so compare their (possibly unresolved)
645 // types; we don't know if they'll be resolved the same way,
646 // so we can't ever say unresolved types are equal
647 if(!(*k
).d_selector
.isNull() && !(*l
).d_selector
.isNull()) {
648 if((*k
).d_selector
.getType() != (*l
).d_selector
.getType()) {
652 if((*k
).isUnresolvedSelf() && (*l
).isUnresolvedSelf()) {
653 // Fine, the selectors are equal if the rest of the
654 // enclosing datatypes are equal...
665 const DatatypeConstructor
& Datatype::operator[](size_t index
) const {
666 PrettyCheckArgument(index
< getNumConstructors(), index
, "index out of bounds");
667 return d_constructors
[index
];
670 const DatatypeConstructor
& Datatype::operator[](std::string name
) const {
671 for(const_iterator i
= begin(); i
!= end(); ++i
) {
672 if((*i
).getName() == name
) {
676 IllegalArgument(name
, "No such constructor `%s' of datatype `%s'", name
.c_str(), d_name
.c_str());
680 Expr
Datatype::getSharedSelector( Type dtt
, Type t
, unsigned index
) const{
681 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
682 std::map
< Type
, std::map
< Type
, std::map
< unsigned, Expr
> > >::iterator itd
= d_shared_sel
.find( dtt
);
683 if( itd
!=d_shared_sel
.end() ){
684 std::map
< Type
, std::map
< unsigned, Expr
> >::iterator its
= itd
->second
.find( t
);
685 if( its
!=itd
->second
.end() ){
686 std::map
< unsigned, Expr
>::iterator it
= its
->second
.find( index
);
687 if( it
!=its
->second
.end() ){
692 //make the shared selector
694 NodeManager
* nm
= NodeManager::fromExprManager( d_self
.getExprManager() );
695 std::stringstream ss
;
696 ss
<< "sel_" << index
;
697 s
= nm
->mkSkolem(ss
.str(), nm
->mkSelectorType(TypeNode::fromType(dtt
), TypeNode::fromType(t
)), "is a shared selector", NodeManager::SKOLEM_NO_NOTIFY
).toExpr();
698 d_shared_sel
[dtt
][t
][index
] = s
;
699 Trace("dt-shared-sel") << "Made " << s
<< " of type " << dtt
<< " -> " << t
<< std::endl
;
703 Expr
Datatype::getConstructor(std::string name
) const {
704 return (*this)[name
].getConstructor();
707 Type
Datatype::getSygusType() const {
711 Expr
Datatype::getSygusVarList() const {
715 bool Datatype::getSygusAllowConst() const {
716 return d_sygus_allow_const
;
719 bool Datatype::getSygusAllowAll() const {
720 return d_sygus_allow_all
;
723 bool Datatype::involvesExternalType() const{
724 return d_involvesExt
;
727 bool Datatype::involvesUninterpretedType() const{
731 const std::vector
<DatatypeConstructor
>* Datatype::getConstructors() const
733 return &d_constructors
;
736 void DatatypeConstructor::resolve(ExprManager
* em
, DatatypeType self
,
737 const std::map
<std::string
, DatatypeType
>& resolutions
,
738 const std::vector
<Type
>& placeholders
,
739 const std::vector
<Type
>& replacements
,
740 const std::vector
< SortConstructorType
>& paramTypes
,
741 const std::vector
< DatatypeType
>& paramReplacements
, size_t cindex
)
743 PrettyCheckArgument(em
!= NULL
, em
, "cannot resolve a Datatype with a NULL expression manager");
744 PrettyCheckArgument(!isResolved(),
745 "cannot resolve a Datatype constructor twice; "
746 "perhaps the same constructor was added twice, "
747 "or to two datatypes?");
749 // we're using some internals, so we have to set up this library context
750 ExprManagerScope
ems(*em
);
752 NodeManager
* nm
= NodeManager::fromExprManager(em
);
753 TypeNode selfTypeNode
= TypeNode::fromType(self
);
755 for(std::vector
<DatatypeConstructorArg
>::iterator i
= d_args
.begin(), i_end
= d_args
.end(); i
!= i_end
; ++i
) {
756 if((*i
).d_selector
.isNull()) {
757 // the unresolved type wasn't created here; do name resolution
758 string typeName
= (*i
).d_name
.substr((*i
).d_name
.find('\0') + 1);
759 (*i
).d_name
.resize((*i
).d_name
.find('\0'));
761 (*i
).d_selector
= nm
->mkSkolem((*i
).d_name
, nm
->mkSelectorType(selfTypeNode
, selfTypeNode
), "is a selector", NodeManager::SKOLEM_EXACT_NAME
| NodeManager::SKOLEM_NO_NOTIFY
).toExpr();
763 map
<string
, DatatypeType
>::const_iterator j
= resolutions
.find(typeName
);
764 if(j
== resolutions
.end()) {
766 msg
<< "cannot resolve type \"" << typeName
<< "\" "
767 << "in selector \"" << (*i
).d_name
<< "\" "
768 << "of constructor \"" << d_name
<< "\"";
769 throw DatatypeResolutionException(msg
.str());
771 (*i
).d_selector
= nm
->mkSkolem((*i
).d_name
, nm
->mkSelectorType(selfTypeNode
, TypeNode::fromType((*j
).second
)), "is a selector", NodeManager::SKOLEM_EXACT_NAME
| NodeManager::SKOLEM_NO_NOTIFY
).toExpr();
775 // the type for the selector already exists; may need
776 // complex-type substitution
777 Type range
= (*i
).d_selector
.getType();
778 if(!placeholders
.empty()) {
779 range
= range
.substitute(placeholders
, replacements
);
781 if(!paramTypes
.empty() ) {
782 range
= doParametricSubstitution( range
, paramTypes
, paramReplacements
);
784 (*i
).d_selector
= nm
->mkSkolem((*i
).d_name
, nm
->mkSelectorType(selfTypeNode
, TypeNode::fromType(range
)), "is a selector", NodeManager::SKOLEM_EXACT_NAME
| NodeManager::SKOLEM_NO_NOTIFY
).toExpr();
786 Node::fromExpr((*i
).d_selector
).setAttribute(DatatypeConsIndexAttr(), cindex
);
787 Node::fromExpr((*i
).d_selector
).setAttribute(DatatypeIndexAttr(), index
++);
788 (*i
).d_resolved
= true;
791 Assert(index
== getNumArgs());
793 // Set constructor/tester last, since DatatypeConstructor::isResolved()
794 // returns true when d_tester is not the null Expr. If something
795 // fails above, we want Constuctor::isResolved() to remain "false".
796 // Further, mkConstructorType() iterates over the selectors, so
797 // should get the results of any resolutions we did above.
798 d_tester
= nm
->mkSkolem(getTesterName(), nm
->mkTesterType(selfTypeNode
), "is a tester", NodeManager::SKOLEM_EXACT_NAME
| NodeManager::SKOLEM_NO_NOTIFY
).toExpr();
799 d_constructor
= nm
->mkSkolem(getName(), nm
->mkConstructorType(*this, selfTypeNode
), "is a constructor", NodeManager::SKOLEM_EXACT_NAME
| NodeManager::SKOLEM_NO_NOTIFY
).toExpr();
800 // associate constructor with all selectors
801 for(std::vector
<DatatypeConstructorArg
>::iterator i
= d_args
.begin(), i_end
= d_args
.end(); i
!= i_end
; ++i
) {
802 (*i
).d_constructor
= d_constructor
;
806 Type
DatatypeConstructor::doParametricSubstitution( Type range
,
807 const std::vector
< SortConstructorType
>& paramTypes
,
808 const std::vector
< DatatypeType
>& paramReplacements
) {
809 TypeNode typn
= TypeNode::fromType( range
);
810 if(typn
.getNumChildren() == 0) {
813 std::vector
< Type
> origChildren
;
814 std::vector
< Type
> children
;
815 for(TypeNode::const_iterator i
= typn
.begin(), iend
= typn
.end();i
!= iend
; ++i
) {
816 origChildren
.push_back( (*i
).toType() );
817 children
.push_back( doParametricSubstitution( (*i
).toType(), paramTypes
, paramReplacements
) );
819 for( unsigned i
= 0; i
< paramTypes
.size(); ++i
) {
820 if( paramTypes
[i
].getArity() == origChildren
.size() ) {
821 Type tn
= paramTypes
[i
].instantiate( origChildren
);
823 return paramReplacements
[i
].instantiate( children
);
827 NodeBuilder
<> nb(typn
.getKind());
828 for( unsigned i
= 0; i
< children
.size(); ++i
) {
829 nb
<< TypeNode::fromType( children
[i
] );
831 return nb
.constructTypeNode().toType();
835 DatatypeConstructor::DatatypeConstructor(std::string name
)
836 : // We don't want to introduce a new data member, because eventually
837 // we're going to be a constant stuffed inside a node. So we stow
838 // the tester name away inside the constructor name until
840 d_name(name
+ '\0' + "is_" + name
), // default tester name is "is_FOO"
846 PrettyCheckArgument(name
!= "", name
, "cannot construct a datatype constructor without a name");
849 DatatypeConstructor::DatatypeConstructor(std::string name
,
852 : // We don't want to introduce a new data member, because eventually
853 // we're going to be a constant stuffed inside a node. So we stow
854 // the tester name away inside the constructor name until
856 d_name(name
+ '\0' + tester
),
862 PrettyCheckArgument(name
!= "", name
, "cannot construct a datatype constructor without a name");
863 PrettyCheckArgument(!tester
.empty(), tester
, "cannot construct a datatype constructor without a tester");
866 void DatatypeConstructor::setSygus(Expr op
,
867 std::shared_ptr
<SygusPrintCallback
> spc
)
870 !isResolved(), this, "cannot modify a finalized Datatype constructor");
875 const std::vector
<DatatypeConstructorArg
>* DatatypeConstructor::getArgs()
881 void DatatypeConstructor::addArg(std::string selectorName
, Type selectorType
) {
882 // We don't want to introduce a new data member, because eventually
883 // we're going to be a constant stuffed inside a node. So we stow
884 // the selector type away inside a var until resolution (when we can
885 // create the proper selector type)
886 PrettyCheckArgument(!isResolved(), this, "cannot modify a finalized Datatype constructor");
887 PrettyCheckArgument(!selectorType
.isNull(), selectorType
, "cannot add a null selector type");
889 // we're using some internals, so we have to set up this library context
890 ExprManagerScope
ems(selectorType
);
892 Expr type
= NodeManager::currentNM()->mkSkolem("unresolved_" + selectorName
, TypeNode::fromType(selectorType
), "is an unresolved selector type placeholder", NodeManager::SKOLEM_EXACT_NAME
| NodeManager::SKOLEM_NO_NOTIFY
).toExpr();
893 Debug("datatypes") << type
<< endl
;
894 d_args
.push_back(DatatypeConstructorArg(selectorName
, type
));
897 void DatatypeConstructor::addArg(std::string selectorName
, DatatypeUnresolvedType selectorType
) {
898 // We don't want to introduce a new data member, because eventually
899 // we're going to be a constant stuffed inside a node. So we stow
900 // the selector type away after a NUL in the name string until
901 // resolution (when we can create the proper selector type)
902 PrettyCheckArgument(!isResolved(), this, "cannot modify a finalized Datatype constructor");
903 PrettyCheckArgument(selectorType
.getName() != "", selectorType
, "cannot add a null selector type");
904 d_args
.push_back(DatatypeConstructorArg(selectorName
+ '\0' + selectorType
.getName(), Expr()));
907 void DatatypeConstructor::addArg(std::string selectorName
, DatatypeSelfType
) {
908 // We don't want to introduce a new data member, because eventually
909 // we're going to be a constant stuffed inside a node. So we mark
910 // the name string with a NUL to indicate that we have a
911 // self-selecting selector until resolution (when we can create the
912 // proper selector type)
913 PrettyCheckArgument(!isResolved(), this, "cannot modify a finalized Datatype constructor");
914 d_args
.push_back(DatatypeConstructorArg(selectorName
+ '\0', Expr()));
917 std::string
DatatypeConstructor::getName() const
919 return d_name
.substr(0, d_name
.find('\0'));
922 std::string
DatatypeConstructor::getTesterName() const
924 return d_name
.substr(d_name
.find('\0') + 1);
927 Expr
DatatypeConstructor::getConstructor() const {
928 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
929 return d_constructor
;
932 Type
DatatypeConstructor::getSpecializedConstructorType(Type returnType
) const {
933 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
934 PrettyCheckArgument(returnType
.isDatatype(), this, "cannot get specialized constructor type for non-datatype type");
935 ExprManagerScope
ems(d_constructor
);
936 const Datatype
& dt
= Datatype::datatypeOf(d_constructor
);
937 PrettyCheckArgument(dt
.isParametric(), this, "this datatype constructor is not parametric");
938 TypeNode dtt
= TypeNode::fromType(dt
.getDatatypeType());
940 m
.doMatching(dtt
, TypeNode::fromType(returnType
));
941 std::vector
<TypeNode
> sns
;
943 std::vector
<Type
> subst
;
944 for (TypeNode
& s
: sns
)
946 subst
.push_back(s
.toType());
948 vector
<Type
> params
= dt
.getParameters();
949 return d_constructor
.getType().substitute(params
, subst
);
952 Expr
DatatypeConstructor::getTester() const {
953 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
957 Expr
DatatypeConstructor::getSygusOp() const {
958 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
962 bool DatatypeConstructor::isSygusIdFunc() const {
963 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
964 return (d_sygus_op
.getKind() == kind::LAMBDA
965 && d_sygus_op
[0].getNumChildren() == 1
966 && d_sygus_op
[0][0] == d_sygus_op
[1]);
969 unsigned DatatypeConstructor::getWeight() const
972 isResolved(), this, "this datatype constructor is not yet resolved");
976 std::shared_ptr
<SygusPrintCallback
> DatatypeConstructor::getSygusPrintCallback() const
979 isResolved(), this, "this datatype constructor is not yet resolved");
983 Cardinality
DatatypeConstructor::getCardinality(Type t
) const
985 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
989 for(const_iterator i
= begin(), i_end
= end(); i
!= i_end
; ++i
) {
990 c
*= SelectorType((*i
).getSelector().getType()).getRangeType().getCardinality();
996 /** compute the cardinality of this datatype */
997 Cardinality
DatatypeConstructor::computeCardinality(
998 Type t
, std::vector
<Type
>& processing
) const
1001 std::vector
< Type
> instTypes
;
1002 std::vector
< Type
> paramTypes
;
1003 if( DatatypeType(t
).isParametric() ){
1004 paramTypes
= DatatypeType(t
).getDatatype().getParameters();
1005 instTypes
= DatatypeType(t
).getParamTypes();
1007 for(const_iterator i
= begin(), i_end
= end(); i
!= i_end
; ++i
) {
1008 Type tc
= SelectorType((*i
).getSelector().getType()).getRangeType();
1009 if( DatatypeType(t
).isParametric() ){
1010 tc
= tc
.substitute( paramTypes
, instTypes
);
1012 if( tc
.isDatatype() ){
1013 const Datatype
& dt
= ((DatatypeType
)tc
).getDatatype();
1014 c
*= dt
.computeCardinality( t
, processing
);
1016 c
*= tc
.getCardinality();
1022 bool DatatypeConstructor::computeWellFounded(
1023 std::vector
<Type
>& processing
) const
1025 for(const_iterator i
= begin(), i_end
= end(); i
!= i_end
; ++i
) {
1026 Type t
= SelectorType((*i
).getSelector().getType()).getRangeType();
1027 if( t
.isDatatype() ){
1028 const Datatype
& dt
= ((DatatypeType
)t
).getDatatype();
1029 if( !dt
.computeWellFounded( processing
) ){
1037 bool DatatypeConstructor::isFinite(Type t
) const
1039 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
1041 // we're using some internals, so we have to set up this library context
1042 ExprManagerScope
ems(d_constructor
);
1043 TNode self
= Node::fromExpr(d_constructor
);
1044 // is this already in the cache ?
1045 if(self
.getAttribute(DatatypeFiniteComputedAttr())) {
1046 return self
.getAttribute(DatatypeFiniteAttr());
1048 std::vector
< Type
> instTypes
;
1049 std::vector
< Type
> paramTypes
;
1050 if( DatatypeType(t
).isParametric() ){
1051 paramTypes
= DatatypeType(t
).getDatatype().getParameters();
1052 instTypes
= DatatypeType(t
).getParamTypes();
1054 for(const_iterator i
= begin(), i_end
= end(); i
!= i_end
; ++i
) {
1055 Type tc
= (*i
).getRangeType();
1056 if( DatatypeType(t
).isParametric() ){
1057 tc
= tc
.substitute( paramTypes
, instTypes
);
1061 self
.setAttribute(DatatypeFiniteComputedAttr(), true);
1062 self
.setAttribute(DatatypeFiniteAttr(), false);
1066 self
.setAttribute(DatatypeFiniteComputedAttr(), true);
1067 self
.setAttribute(DatatypeFiniteAttr(), true);
1071 bool DatatypeConstructor::isInterpretedFinite(Type t
) const
1073 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
1074 // we're using some internals, so we have to set up this library context
1075 ExprManagerScope
ems(d_constructor
);
1076 TNode self
= Node::fromExpr(d_constructor
);
1077 // is this already in the cache ?
1078 if(self
.getAttribute(DatatypeUFiniteComputedAttr())) {
1079 return self
.getAttribute(DatatypeUFiniteAttr());
1081 std::vector
< Type
> instTypes
;
1082 std::vector
< Type
> paramTypes
;
1083 if( DatatypeType(t
).isParametric() ){
1084 paramTypes
= DatatypeType(t
).getDatatype().getParameters();
1085 instTypes
= DatatypeType(t
).getParamTypes();
1087 for(const_iterator i
= begin(), i_end
= end(); i
!= i_end
; ++i
) {
1088 Type tc
= (*i
).getRangeType();
1089 if( DatatypeType(t
).isParametric() ){
1090 tc
= tc
.substitute( paramTypes
, instTypes
);
1092 TypeNode tcn
= TypeNode::fromType( tc
);
1093 if(!tcn
.isInterpretedFinite()) {
1094 self
.setAttribute(DatatypeUFiniteComputedAttr(), true);
1095 self
.setAttribute(DatatypeUFiniteAttr(), false);
1099 self
.setAttribute(DatatypeUFiniteComputedAttr(), true);
1100 self
.setAttribute(DatatypeUFiniteAttr(), true);
1104 Expr
DatatypeConstructor::computeGroundTerm(Type t
,
1105 std::vector
<Type
>& processing
,
1106 std::map
<Type
, Expr
>& gt
,
1109 // we're using some internals, so we have to set up this library context
1110 ExprManagerScope
ems(d_constructor
);
1112 std::vector
<Expr
> groundTerms
;
1113 groundTerms
.push_back(getConstructor());
1115 // for each selector, get a ground term
1116 std::vector
< Type
> instTypes
;
1117 std::vector
< Type
> paramTypes
;
1118 bool isParam
= static_cast<DatatypeType
>(t
).isParametric();
1121 paramTypes
= DatatypeType(t
).getDatatype().getParameters();
1122 instTypes
= DatatypeType(t
).getParamTypes();
1124 for(const_iterator i
= begin(), i_end
= end(); i
!= i_end
; ++i
) {
1125 Type selType
= SelectorType((*i
).getSelector().getType()).getRangeType();
1128 selType
= selType
.substitute( paramTypes
, instTypes
);
1131 if( selType
.isDatatype() ){
1132 std::map
< Type
, Expr
>::iterator itgt
= gt
.find( selType
);
1133 if( itgt
!= gt
.end() ){
1136 const Datatype
& dt
= DatatypeType(selType
).getDatatype();
1137 arg
= dt
.computeGroundTerm(selType
, processing
, isValue
);
1142 // call mkGroundValue or mkGroundTerm based on isValue
1143 arg
= isValue
? selType
.mkGroundValue() : selType
.mkGroundTerm();
1146 Debug("datatypes") << "...unable to construct arg of " << (*i
).getName() << std::endl
;
1149 Debug("datatypes") << "...constructed arg " << arg
.getType() << std::endl
;
1150 groundTerms
.push_back(arg
);
1154 Expr groundTerm
= getConstructor().getExprManager()->mkExpr(kind::APPLY_CONSTRUCTOR
, groundTerms
);
1157 Assert( Datatype::datatypeOf( d_constructor
).isParametric() );
1158 // type is parametric, must apply type ascription
1159 Debug("datatypes-gt") << "ambiguous type for " << groundTerm
<< ", ascribe to " << t
<< std::endl
;
1160 groundTerms
[0] = getConstructor().getExprManager()->mkExpr(kind::APPLY_TYPE_ASCRIPTION
,
1161 getConstructor().getExprManager()->mkConst(AscriptionType(getSpecializedConstructorType(t
))),
1163 groundTerm
= getConstructor().getExprManager()->mkExpr(kind::APPLY_CONSTRUCTOR
, groundTerms
);
1168 void DatatypeConstructor::computeSharedSelectors( Type domainType
) const {
1169 if( d_shared_selectors
[domainType
].size()<getNumArgs() ){
1171 if( DatatypeType(domainType
).isParametric() ){
1172 ctype
= TypeNode::fromType( getSpecializedConstructorType( domainType
) );
1174 ctype
= TypeNode::fromType( d_constructor
.getType() );
1176 Assert(ctype
.isConstructor());
1177 Assert(ctype
.getNumChildren() - 1 == getNumArgs());
1178 //compute the shared selectors
1179 const Datatype
& dt
= Datatype::datatypeOf(d_constructor
);
1180 std::map
< TypeNode
, unsigned > counter
;
1181 for( unsigned j
=0; j
<ctype
.getNumChildren()-1; j
++ ){
1182 TypeNode t
= ctype
[j
];
1183 Expr ss
= dt
.getSharedSelector( domainType
, t
.toType(), counter
[t
] );
1184 d_shared_selectors
[domainType
].push_back( ss
);
1185 Assert(d_shared_selector_index
[domainType
].find(ss
)
1186 == d_shared_selector_index
[domainType
].end());
1187 d_shared_selector_index
[domainType
][ss
] = j
;
1194 const DatatypeConstructorArg
& DatatypeConstructor::operator[](size_t index
) const {
1195 PrettyCheckArgument(index
< getNumArgs(), index
, "index out of bounds");
1196 return d_args
[index
];
1199 const DatatypeConstructorArg
& DatatypeConstructor::operator[](std::string name
) const {
1200 for(const_iterator i
= begin(); i
!= end(); ++i
) {
1201 if((*i
).getName() == name
) {
1205 IllegalArgument(name
, "No such arg `%s' of constructor `%s'", name
.c_str(), d_name
.c_str());
1208 Expr
DatatypeConstructor::getSelector(std::string name
) const {
1209 return (*this)[name
].getSelector();
1212 Type
DatatypeConstructor::getArgType(unsigned index
) const
1214 PrettyCheckArgument(index
< getNumArgs(), index
, "index out of bounds");
1215 return static_cast<SelectorType
>((*this)[index
].getType()).getRangeType();
1218 bool DatatypeConstructor::involvesExternalType() const{
1219 for(const_iterator i
= begin(); i
!= end(); ++i
) {
1220 if(! SelectorType((*i
).getSelector().getType()).getRangeType().isDatatype()) {
1227 bool DatatypeConstructor::involvesUninterpretedType() const{
1228 for(const_iterator i
= begin(); i
!= end(); ++i
) {
1229 if(SelectorType((*i
).getSelector().getType()).getRangeType().isSort()) {
1236 DatatypeConstructorArg::DatatypeConstructorArg(std::string name
, Expr selector
) :
1238 d_selector(selector
),
1240 PrettyCheckArgument(name
!= "", name
, "cannot construct a datatype constructor arg without a name");
1243 std::string
DatatypeConstructorArg::getName() const
1245 string name
= d_name
;
1246 const size_t nul
= name
.find('\0');
1247 if(nul
!= string::npos
) {
1253 Expr
DatatypeConstructorArg::getSelector() const {
1254 PrettyCheckArgument(isResolved(), this, "cannot get a selector for an unresolved datatype constructor");
1258 Expr
DatatypeConstructor::getSelectorInternal( Type domainType
, size_t index
) const {
1259 PrettyCheckArgument(isResolved(), this, "cannot get an internal selector for an unresolved datatype constructor");
1260 PrettyCheckArgument(index
< getNumArgs(), index
, "index out of bounds");
1261 if( options::dtSharedSelectors() ){
1262 computeSharedSelectors( domainType
);
1263 Assert(d_shared_selectors
[domainType
].size() == getNumArgs());
1264 return d_shared_selectors
[domainType
][index
];
1266 return d_args
[index
].getSelector();
1270 int DatatypeConstructor::getSelectorIndexInternal( Expr sel
) const {
1271 PrettyCheckArgument(isResolved(), this, "cannot get an internal selector index for an unresolved datatype constructor");
1272 if( options::dtSharedSelectors() ){
1273 Assert(sel
.getType().isSelector());
1274 Type domainType
= ((SelectorType
)sel
.getType()).getDomain();
1275 computeSharedSelectors( domainType
);
1276 std::map
< Expr
, unsigned >::iterator its
= d_shared_selector_index
[domainType
].find( sel
);
1277 if( its
!=d_shared_selector_index
[domainType
].end() ){
1278 return (int)its
->second
;
1281 unsigned sindex
= Datatype::indexOf(sel
);
1282 if( getNumArgs() > sindex
&& d_args
[sindex
].getSelector() == sel
){
1289 Expr
DatatypeConstructorArg::getConstructor() const {
1290 PrettyCheckArgument(isResolved(), this,
1291 "cannot get a associated constructor for argument of an unresolved datatype constructor");
1292 return d_constructor
;
1295 SelectorType
DatatypeConstructorArg::getType() const {
1296 return getSelector().getType();
1299 Type
DatatypeConstructorArg::getRangeType() const {
1300 return getType().getRangeType();
1303 bool DatatypeConstructorArg::isUnresolvedSelf() const
1305 return d_selector
.isNull() && d_name
.size() == d_name
.find('\0') + 1;
1308 std::ostream
& operator<<(std::ostream
& os
, const Datatype
& dt
)
1310 // can only output datatypes in the CVC4 native language
1311 language::SetLanguage::Scope
ls(os
, language::output::LANG_CVC4
);
1316 void Datatype::toStream(std::ostream
& out
) const
1318 out
<< "DATATYPE " << getName();
1322 for (size_t i
= 0; i
< getNumParameters(); ++i
)
1327 out
<< getParameter(i
);
1331 out
<< " =" << endl
;
1332 Datatype::const_iterator i
= begin(), i_end
= end();
1340 } while(i
!= i_end
);
1342 out
<< "END;" << endl
;
1345 std::ostream
& operator<<(std::ostream
& os
, const DatatypeConstructor
& ctor
) {
1346 // can only output datatypes in the CVC4 native language
1347 language::SetLanguage::Scope
ls(os
, language::output::LANG_CVC4
);
1352 void DatatypeConstructor::toStream(std::ostream
& out
) const
1356 DatatypeConstructor::const_iterator i
= begin(), i_end
= end();
1364 } while(i
!= i_end
);
1369 std::ostream
& operator<<(std::ostream
& os
, const DatatypeConstructorArg
& arg
) {
1370 // can only output datatypes in the CVC4 native language
1371 language::SetLanguage::Scope
ls(os
, language::output::LANG_CVC4
);
1376 void DatatypeConstructorArg::toStream(std::ostream
& out
) const
1378 out
<< getName() << ": ";
1385 else if (d_selector
.isNull())
1387 string typeName
= d_name
.substr(d_name
.find('\0') + 1);
1388 out
<< ((typeName
== "") ? "[self]" : typeName
);
1393 t
= d_selector
.getType();
1398 DatatypeIndexConstant::DatatypeIndexConstant(unsigned index
) : d_index(index
) {}
1399 std::ostream
& operator<<(std::ostream
& out
, const DatatypeIndexConstant
& dic
) {
1400 return out
<< "index_" << dic
.getIndex();
1403 }/* CVC4 namespace */