1 /********************* */
4 ** Top contributors (to current version):
5 ** Morgan Deters, Andrew Reynolds, Tim King
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2016 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/cvc4_assert.h"
23 #include "expr/attribute.h"
24 #include "expr/expr_manager.h"
25 #include "expr/expr_manager_scope.h"
26 #include "expr/matcher.h"
27 #include "expr/node.h"
28 #include "expr/node_manager.h"
29 #include "expr/type.h"
30 #include "options/set_language.h"
38 struct DatatypeIndexTag
{};
39 struct DatatypeConsIndexTag
{};
40 struct DatatypeFiniteTag
{};
41 struct DatatypeFiniteComputedTag
{};
42 struct DatatypeUFiniteTag
{};
43 struct DatatypeUFiniteComputedTag
{};
44 }/* CVC4::expr::attr namespace */
45 }/* CVC4::expr namespace */
47 typedef expr::Attribute
<expr::attr::DatatypeIndexTag
, uint64_t> DatatypeIndexAttr
;
48 typedef expr::Attribute
<expr::attr::DatatypeConsIndexTag
, uint64_t> DatatypeConsIndexAttr
;
49 typedef expr::Attribute
<expr::attr::DatatypeFiniteTag
, bool> DatatypeFiniteAttr
;
50 typedef expr::Attribute
<expr::attr::DatatypeFiniteComputedTag
, bool> DatatypeFiniteComputedAttr
;
51 typedef expr::Attribute
<expr::attr::DatatypeUFiniteTag
, bool> DatatypeUFiniteAttr
;
52 typedef expr::Attribute
<expr::attr::DatatypeUFiniteComputedTag
, bool> DatatypeUFiniteComputedAttr
;
54 Datatype::~Datatype(){
58 const Datatype
& Datatype::datatypeOf(Expr item
) {
59 ExprManagerScope
ems(item
);
60 TypeNode t
= Node::fromExpr(item
).getType();
62 case kind::CONSTRUCTOR_TYPE
:
63 return DatatypeType(t
[t
.getNumChildren() - 1].toType()).getDatatype();
64 case kind::SELECTOR_TYPE
:
65 case kind::TESTER_TYPE
:
66 return DatatypeType(t
[0].toType()).getDatatype();
68 Unhandled("arg must be a datatype constructor, selector, or tester");
72 size_t Datatype::indexOf(Expr item
) {
73 ExprManagerScope
ems(item
);
74 PrettyCheckArgument(item
.getType().isConstructor() ||
75 item
.getType().isTester() ||
76 item
.getType().isSelector(),
78 "arg must be a datatype constructor, selector, or tester");
79 TNode n
= Node::fromExpr(item
);
80 if( item
.getKind()==kind::APPLY_TYPE_ASCRIPTION
){
81 return indexOf( item
[0] );
83 Assert(n
.hasAttribute(DatatypeIndexAttr()));
84 return n
.getAttribute(DatatypeIndexAttr());
88 size_t Datatype::cindexOf(Expr item
) {
89 ExprManagerScope
ems(item
);
90 PrettyCheckArgument(item
.getType().isSelector(),
92 "arg must be a datatype selector");
93 TNode n
= Node::fromExpr(item
);
94 if( item
.getKind()==kind::APPLY_TYPE_ASCRIPTION
){
95 return cindexOf( item
[0] );
97 Assert(n
.hasAttribute(DatatypeConsIndexAttr()));
98 return n
.getAttribute(DatatypeConsIndexAttr());
102 void Datatype::resolve(ExprManager
* em
,
103 const std::map
<std::string
, DatatypeType
>& resolutions
,
104 const std::vector
<Type
>& placeholders
,
105 const std::vector
<Type
>& replacements
,
106 const std::vector
< SortConstructorType
>& paramTypes
,
107 const std::vector
< DatatypeType
>& paramReplacements
)
108 throw(IllegalArgumentException
, DatatypeResolutionException
) {
110 PrettyCheckArgument(em
!= NULL
, em
, "cannot resolve a Datatype with a NULL expression manager");
111 PrettyCheckArgument(!d_resolved
, this, "cannot resolve a Datatype twice");
112 PrettyCheckArgument(resolutions
.find(d_name
) != resolutions
.end(), resolutions
,
113 "Datatype::resolve(): resolutions doesn't contain me!");
114 PrettyCheckArgument(placeholders
.size() == replacements
.size(), placeholders
,
115 "placeholders and replacements must be the same size");
116 PrettyCheckArgument(paramTypes
.size() == paramReplacements
.size(), paramTypes
,
117 "paramTypes and paramReplacements must be the same size");
118 PrettyCheckArgument(getNumConstructors() > 0, *this, "cannot resolve a Datatype that has no constructors");
119 DatatypeType self
= (*resolutions
.find(d_name
)).second
;
120 PrettyCheckArgument(&self
.getDatatype() == this, resolutions
, "Datatype::resolve(): resolutions doesn't contain me!");
123 for(std::vector
<DatatypeConstructor
>::iterator i
= d_constructors
.begin(), i_end
= d_constructors
.end(); i
!= i_end
; ++i
) {
124 (*i
).resolve(em
, self
, resolutions
, placeholders
, replacements
, paramTypes
, paramReplacements
, index
);
125 Node::fromExpr((*i
).d_constructor
).setAttribute(DatatypeIndexAttr(), index
);
126 Node::fromExpr((*i
).d_tester
).setAttribute(DatatypeIndexAttr(), index
++);
130 d_involvesExt
= false;
131 d_involvesUt
= false;
132 for(const_iterator i
= begin(); i
!= end(); ++i
) {
133 if( (*i
).involvesExternalType() ){
134 d_involvesExt
= true;
136 if( (*i
).involvesUninterpretedType() ){
142 std::vector
< std::pair
<std::string
, Type
> > fields
;
143 for( unsigned i
=0; i
<(*this)[0].getNumArgs(); i
++ ){
144 fields
.push_back( std::pair
<std::string
, Type
>( (*this)[0][i
].getName(), (*this)[0][i
].getRangeType() ) );
146 d_record
= new Record(fields
);
150 void Datatype::addConstructor(const DatatypeConstructor
& c
) {
151 PrettyCheckArgument(!d_resolved
, this,
152 "cannot add a constructor to a finalized Datatype");
153 d_constructors
.push_back(c
);
157 void Datatype::setSygus( Type st
, Expr bvl
, bool allow_const
, bool allow_all
){
158 PrettyCheckArgument(!d_resolved
, this,
159 "cannot set sygus type to a finalized Datatype");
162 d_sygus_allow_const
= allow_const
|| allow_all
;
163 d_sygus_allow_all
= allow_all
;
166 void Datatype::setTuple() {
167 PrettyCheckArgument(!d_resolved
, this, "cannot set tuple to a finalized Datatype");
171 void Datatype::setRecord() {
172 PrettyCheckArgument(!d_resolved
, this, "cannot set record to a finalized Datatype");
176 Cardinality
Datatype::getCardinality() const throw(IllegalArgumentException
) {
177 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
178 std::vector
< Type
> processing
;
179 computeCardinality( processing
);
183 Cardinality
Datatype::computeCardinality( std::vector
< Type
>& processing
) const throw(IllegalArgumentException
){
184 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
185 if( std::find( processing
.begin(), processing
.end(), d_self
)!=processing
.end() ){
186 d_card
= Cardinality::INTEGERS
;
188 processing
.push_back( d_self
);
190 for(const_iterator i
= begin(), i_end
= end(); i
!= i_end
; ++i
) {
191 c
+= (*i
).computeCardinality( processing
);
194 processing
.pop_back();
199 bool Datatype::isRecursiveSingleton() const throw(IllegalArgumentException
) {
200 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
201 if( d_card_rec_singleton
==0 ){
202 if( isCodatatype() ){
203 Assert( d_card_u_assume
.empty() );
204 std::vector
< Type
> processing
;
205 if( computeCardinalityRecSingleton( processing
, d_card_u_assume
) ){
206 d_card_rec_singleton
= 1;
208 d_card_rec_singleton
= -1;
210 if( d_card_rec_singleton
==1 ){
211 Trace("dt-card") << "Datatype " << getName() << " is recursive singleton, dependent upon " << d_card_u_assume
.size() << " uninterpreted sorts: " << std::endl
;
212 for( unsigned i
=0; i
<d_card_u_assume
.size(); i
++ ){
213 Trace("dt-card") << " " << d_card_u_assume
[i
] << std::endl
;
215 Trace("dt-card") << std::endl
;
218 d_card_rec_singleton
= -1;
221 return d_card_rec_singleton
==1;
224 unsigned Datatype::getNumRecursiveSingletonArgTypes() const throw(IllegalArgumentException
) {
225 return d_card_u_assume
.size();
227 Type
Datatype::getRecursiveSingletonArgType( unsigned i
) const throw(IllegalArgumentException
) {
228 return d_card_u_assume
[i
];
231 bool Datatype::computeCardinalityRecSingleton( std::vector
< Type
>& processing
, std::vector
< Type
>& u_assume
) const throw(IllegalArgumentException
){
232 if( std::find( processing
.begin(), processing
.end(), d_self
)!=processing
.end() ){
235 if( d_card_rec_singleton
==0 ){
236 //if not yet computed
237 if( d_constructors
.size()==1 ){
238 bool success
= false;
239 processing
.push_back( d_self
);
240 for(unsigned i
= 0; i
<d_constructors
[0].getNumArgs(); i
++ ) {
241 Type t
= ((SelectorType
)d_constructors
[0][i
].getType()).getRangeType();
242 //if it is an uninterpreted sort, then we depend on it having cardinality one
244 if( std::find( u_assume
.begin(), u_assume
.end(), t
)==u_assume
.end() ){
245 u_assume
.push_back( t
);
247 //if it is a datatype, recurse
248 }else if( t
.isDatatype() ){
249 const Datatype
& dt
= ((DatatypeType
)t
).getDatatype();
250 if( !dt
.computeCardinalityRecSingleton( processing
, u_assume
) ){
255 //if it is a builtin type, it must have cardinality one
256 }else if( !t
.getCardinality().isOne() ){
260 processing
.pop_back();
265 }else if( d_card_rec_singleton
==-1 ){
268 for( unsigned i
=0; i
<d_card_u_assume
.size(); i
++ ){
269 if( std::find( u_assume
.begin(), u_assume
.end(), d_card_u_assume
[i
] )==u_assume
.end() ){
270 u_assume
.push_back( d_card_u_assume
[i
] );
278 bool Datatype::isFinite() const throw(IllegalArgumentException
) {
279 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
281 // we're using some internals, so we have to set up this library context
282 ExprManagerScope
ems(d_self
);
283 TypeNode self
= TypeNode::fromType(d_self
);
284 // is this already in the cache ?
285 if(self
.getAttribute(DatatypeFiniteComputedAttr())) {
286 return self
.getAttribute(DatatypeFiniteAttr());
288 for(const_iterator i
= begin(), i_end
= end(); i
!= i_end
; ++i
) {
289 if(! (*i
).isFinite()) {
290 self
.setAttribute(DatatypeFiniteComputedAttr(), true);
291 self
.setAttribute(DatatypeFiniteAttr(), false);
295 self
.setAttribute(DatatypeFiniteComputedAttr(), true);
296 self
.setAttribute(DatatypeFiniteAttr(), true);
300 bool Datatype::isInterpretedFinite() const throw(IllegalArgumentException
) {
301 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
302 // we're using some internals, so we have to set up this library context
303 ExprManagerScope
ems(d_self
);
304 TypeNode self
= TypeNode::fromType(d_self
);
305 // is this already in the cache ?
306 if(self
.getAttribute(DatatypeUFiniteComputedAttr())) {
307 return self
.getAttribute(DatatypeUFiniteAttr());
309 //start by assuming it is not
310 self
.setAttribute(DatatypeUFiniteComputedAttr(), true);
311 self
.setAttribute(DatatypeUFiniteAttr(), false);
312 for(const_iterator i
= begin(), i_end
= end(); i
!= i_end
; ++i
) {
313 if(! (*i
).isInterpretedFinite()) {
317 self
.setAttribute(DatatypeUFiniteComputedAttr(), true);
318 self
.setAttribute(DatatypeUFiniteAttr(), true);
322 bool Datatype::isWellFounded() const throw(IllegalArgumentException
) {
323 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
324 if( d_well_founded
==0 ){
325 // we're using some internals, so we have to set up this library context
326 ExprManagerScope
ems(d_self
);
327 std::vector
< Type
> processing
;
328 if( computeWellFounded( processing
) ){
334 return d_well_founded
==1;
337 bool Datatype::computeWellFounded( std::vector
< Type
>& processing
) const throw(IllegalArgumentException
) {
338 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
339 if( std::find( processing
.begin(), processing
.end(), d_self
)!=processing
.end() ){
342 processing
.push_back( d_self
);
343 for(const_iterator i
= begin(), i_end
= end(); i
!= i_end
; ++i
) {
344 if( (*i
).computeWellFounded( processing
) ){
345 processing
.pop_back();
348 Trace("dt-wf") << "Constructor " << (*i
).getName() << " is not well-founded." << std::endl
;
351 processing
.pop_back();
352 Trace("dt-wf") << "Datatype " << getName() << " is not well-founded." << std::endl
;
357 Expr
Datatype::mkGroundTerm( Type t
) const throw(IllegalArgumentException
) {
358 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
359 ExprManagerScope
ems(d_self
);
362 // is this already in the cache ?
363 std::map
< Type
, Expr
>::iterator it
= d_ground_term
.find( t
);
364 if( it
!= d_ground_term
.end() ){
365 Debug("datatypes") << "\nin cache: " << d_self
<< " => " << it
->second
<< std::endl
;
368 std::vector
< Type
> processing
;
369 Expr groundTerm
= computeGroundTerm( t
, processing
);
370 if(!groundTerm
.isNull() ) {
371 // we found a ground-term-constructing constructor!
372 d_ground_term
[t
] = groundTerm
;
373 Debug("datatypes") << "constructed: " << getName() << " => " << groundTerm
<< std::endl
;
375 if( groundTerm
.isNull() ){
377 // if we get all the way here, we aren't well-founded
378 IllegalArgument(*this, "datatype is not well-founded, cannot construct a ground term!");
388 Expr
getSubtermWithType( Expr e
, Type t
, bool isTop
){
389 if( !isTop
&& e
.getType()==t
){
392 for( unsigned i
=0; i
<e
.getNumChildren(); i
++ ){
393 Expr se
= getSubtermWithType( e
[i
], t
, false );
402 Expr
Datatype::computeGroundTerm( Type t
, std::vector
< Type
>& processing
) const throw(IllegalArgumentException
) {
403 if( std::find( processing
.begin(), processing
.end(), d_self
)==processing
.end() ){
404 processing
.push_back( d_self
);
405 for( unsigned r
=0; r
<2; r
++ ){
406 for(const_iterator i
= begin(), i_end
= end(); i
!= i_end
; ++i
) {
407 //do nullary constructors first
408 if( ((*i
).getNumArgs()==0)==(r
==0)){
409 Debug("datatypes") << "Try constructing for " << (*i
).getName() << ", processing = " << processing
.size() << std::endl
;
410 Expr e
= (*i
).computeGroundTerm( t
, processing
, d_ground_term
);
412 //must check subterms for the same type to avoid infinite loops in type enumeration
413 Expr se
= getSubtermWithType( e
, t
, true );
415 Debug("datatypes") << "Take subterm " << se
<< std::endl
;
418 processing
.pop_back();
421 Debug("datatypes") << "...failed." << std::endl
;
426 processing
.pop_back();
428 Debug("datatypes") << "...already processing " << t
<< std::endl
;
433 DatatypeType
Datatype::getDatatypeType() const throw(IllegalArgumentException
) {
434 PrettyCheckArgument(isResolved(), *this, "Datatype must be resolved to get its DatatypeType");
435 PrettyCheckArgument(!d_self
.isNull(), *this);
436 return DatatypeType(d_self
);
439 DatatypeType
Datatype::getDatatypeType(const std::vector
<Type
>& params
)
440 const throw(IllegalArgumentException
) {
441 PrettyCheckArgument(isResolved(), *this, "Datatype must be resolved to get its DatatypeType");
442 PrettyCheckArgument(!d_self
.isNull() && DatatypeType(d_self
).isParametric(), this);
443 return DatatypeType(d_self
).instantiate(params
);
446 bool Datatype::operator==(const Datatype
& other
) const throw() {
447 // two datatypes are == iff the name is the same and they have
448 // exactly matching constructors (in the same order)
454 if(isResolved() != other
.isResolved()) {
458 if( d_name
!= other
.d_name
||
459 getNumConstructors() != other
.getNumConstructors() ) {
462 for(const_iterator i
= begin(), j
= other
.begin(); i
!= end(); ++i
, ++j
) {
463 Assert(j
!= other
.end());
464 // two constructors are == iff they have the same name, their
465 // constructors and testers are equal and they have exactly
466 // matching args (in the same order)
467 if((*i
).getName() != (*j
).getName() ||
468 (*i
).getNumArgs() != (*j
).getNumArgs()) {
471 // testing equivalence of constructors and testers is harder b/c
472 // this constructor might not be resolved yet; only compare them
473 // if they are both resolved
474 Assert(isResolved() == !(*i
).d_constructor
.isNull() &&
475 isResolved() == !(*i
).d_tester
.isNull() &&
476 (*i
).d_constructor
.isNull() == (*j
).d_constructor
.isNull() &&
477 (*i
).d_tester
.isNull() == (*j
).d_tester
.isNull());
478 if(!(*i
).d_constructor
.isNull() && (*i
).d_constructor
!= (*j
).d_constructor
) {
481 if(!(*i
).d_tester
.isNull() && (*i
).d_tester
!= (*j
).d_tester
) {
484 for(DatatypeConstructor::const_iterator k
= (*i
).begin(), l
= (*j
).begin(); k
!= (*i
).end(); ++k
, ++l
) {
485 Assert(l
!= (*j
).end());
486 if((*k
).getName() != (*l
).getName()) {
489 // testing equivalence of selectors is harder b/c args might not
491 Assert(isResolved() == (*k
).isResolved() &&
492 (*k
).isResolved() == (*l
).isResolved());
493 if((*k
).isResolved()) {
494 // both are resolved, so simply compare the selectors directly
495 if((*k
).d_selector
!= (*l
).d_selector
) {
499 // neither is resolved, so compare their (possibly unresolved)
500 // types; we don't know if they'll be resolved the same way,
501 // so we can't ever say unresolved types are equal
502 if(!(*k
).d_selector
.isNull() && !(*l
).d_selector
.isNull()) {
503 if((*k
).d_selector
.getType() != (*l
).d_selector
.getType()) {
507 if((*k
).isUnresolvedSelf() && (*l
).isUnresolvedSelf()) {
508 // Fine, the selectors are equal if the rest of the
509 // enclosing datatypes are equal...
520 const DatatypeConstructor
& Datatype::operator[](size_t index
) const {
521 PrettyCheckArgument(index
< getNumConstructors(), index
, "index out of bounds");
522 return d_constructors
[index
];
525 const DatatypeConstructor
& Datatype::operator[](std::string name
) const {
526 for(const_iterator i
= begin(); i
!= end(); ++i
) {
527 if((*i
).getName() == name
) {
531 IllegalArgument(name
, "No such constructor `%s' of datatype `%s'", name
.c_str(), d_name
.c_str());
534 Expr
Datatype::getConstructor(std::string name
) const {
535 return (*this)[name
].getConstructor();
538 Type
Datatype::getSygusType() const {
542 Expr
Datatype::getSygusVarList() const {
546 bool Datatype::getSygusAllowConst() const {
547 return d_sygus_allow_const
;
550 bool Datatype::getSygusAllowAll() const {
551 return d_sygus_allow_const
;
554 bool Datatype::involvesExternalType() const{
555 return d_involvesExt
;
558 bool Datatype::involvesUninterpretedType() const{
562 void DatatypeConstructor::resolve(ExprManager
* em
, DatatypeType self
,
563 const std::map
<std::string
, DatatypeType
>& resolutions
,
564 const std::vector
<Type
>& placeholders
,
565 const std::vector
<Type
>& replacements
,
566 const std::vector
< SortConstructorType
>& paramTypes
,
567 const std::vector
< DatatypeType
>& paramReplacements
, size_t cindex
)
568 throw(IllegalArgumentException
, DatatypeResolutionException
) {
570 PrettyCheckArgument(em
!= NULL
, em
, "cannot resolve a Datatype with a NULL expression manager");
571 PrettyCheckArgument(!isResolved(),
572 "cannot resolve a Datatype constructor twice; "
573 "perhaps the same constructor was added twice, "
574 "or to two datatypes?");
576 // we're using some internals, so we have to set up this library context
577 ExprManagerScope
ems(*em
);
579 NodeManager
* nm
= NodeManager::fromExprManager(em
);
580 TypeNode selfTypeNode
= TypeNode::fromType(self
);
582 for(std::vector
<DatatypeConstructorArg
>::iterator i
= d_args
.begin(), i_end
= d_args
.end(); i
!= i_end
; ++i
) {
583 if((*i
).d_selector
.isNull()) {
584 // the unresolved type wasn't created here; do name resolution
585 string typeName
= (*i
).d_name
.substr((*i
).d_name
.find('\0') + 1);
586 (*i
).d_name
.resize((*i
).d_name
.find('\0'));
588 (*i
).d_selector
= nm
->mkSkolem((*i
).d_name
, nm
->mkSelectorType(selfTypeNode
, selfTypeNode
), "is a selector", NodeManager::SKOLEM_EXACT_NAME
| NodeManager::SKOLEM_NO_NOTIFY
).toExpr();
590 map
<string
, DatatypeType
>::const_iterator j
= resolutions
.find(typeName
);
591 if(j
== resolutions
.end()) {
593 msg
<< "cannot resolve type \"" << typeName
<< "\" "
594 << "in selector \"" << (*i
).d_name
<< "\" "
595 << "of constructor \"" << d_name
<< "\"";
596 throw DatatypeResolutionException(msg
.str());
598 (*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();
602 // the type for the selector already exists; may need
603 // complex-type substitution
604 Type range
= (*i
).d_selector
.getType();
605 if(!placeholders
.empty()) {
606 range
= range
.substitute(placeholders
, replacements
);
608 if(!paramTypes
.empty() ) {
609 range
= doParametricSubstitution( range
, paramTypes
, paramReplacements
);
611 (*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();
613 Node::fromExpr((*i
).d_selector
).setAttribute(DatatypeConsIndexAttr(), cindex
);
614 Node::fromExpr((*i
).d_selector
).setAttribute(DatatypeIndexAttr(), index
++);
615 (*i
).d_resolved
= true;
618 Assert(index
== getNumArgs());
620 // Set constructor/tester last, since DatatypeConstructor::isResolved()
621 // returns true when d_tester is not the null Expr. If something
622 // fails above, we want Constuctor::isResolved() to remain "false".
623 // Further, mkConstructorType() iterates over the selectors, so
624 // should get the results of any resolutions we did above.
625 d_tester
= nm
->mkSkolem(getTesterName(), nm
->mkTesterType(selfTypeNode
), "is a tester", NodeManager::SKOLEM_EXACT_NAME
| NodeManager::SKOLEM_NO_NOTIFY
).toExpr();
626 d_constructor
= nm
->mkSkolem(getName(), nm
->mkConstructorType(*this, selfTypeNode
), "is a constructor", NodeManager::SKOLEM_EXACT_NAME
| NodeManager::SKOLEM_NO_NOTIFY
).toExpr();
627 // associate constructor with all selectors
628 for(std::vector
<DatatypeConstructorArg
>::iterator i
= d_args
.begin(), i_end
= d_args
.end(); i
!= i_end
; ++i
) {
629 (*i
).d_constructor
= d_constructor
;
633 Type
DatatypeConstructor::doParametricSubstitution( Type range
,
634 const std::vector
< SortConstructorType
>& paramTypes
,
635 const std::vector
< DatatypeType
>& paramReplacements
) {
636 TypeNode typn
= TypeNode::fromType( range
);
637 if(typn
.getNumChildren() == 0) {
640 std::vector
< Type
> origChildren
;
641 std::vector
< Type
> children
;
642 for(TypeNode::const_iterator i
= typn
.begin(), iend
= typn
.end();i
!= iend
; ++i
) {
643 origChildren
.push_back( (*i
).toType() );
644 children
.push_back( doParametricSubstitution( (*i
).toType(), paramTypes
, paramReplacements
) );
646 for( unsigned i
= 0; i
< paramTypes
.size(); ++i
) {
647 if( paramTypes
[i
].getArity() == origChildren
.size() ) {
648 Type tn
= paramTypes
[i
].instantiate( origChildren
);
650 return paramReplacements
[i
].instantiate( children
);
654 NodeBuilder
<> nb(typn
.getKind());
655 for( unsigned i
= 0; i
< children
.size(); ++i
) {
656 nb
<< TypeNode::fromType( children
[i
] );
658 return nb
.constructTypeNode().toType();
662 DatatypeConstructor::DatatypeConstructor(std::string name
) :
663 // We don't want to introduce a new data member, because eventually
664 // we're going to be a constant stuffed inside a node. So we stow
665 // the tester name away inside the constructor name until
667 d_name(name
+ '\0' + "is_" + name
), // default tester name is "is_FOO"
670 PrettyCheckArgument(name
!= "", name
, "cannot construct a datatype constructor without a name");
673 DatatypeConstructor::DatatypeConstructor(std::string name
, std::string tester
) :
674 // We don't want to introduce a new data member, because eventually
675 // we're going to be a constant stuffed inside a node. So we stow
676 // the tester name away inside the constructor name until
678 d_name(name
+ '\0' + tester
),
681 PrettyCheckArgument(name
!= "", name
, "cannot construct a datatype constructor without a name");
682 PrettyCheckArgument(!tester
.empty(), tester
, "cannot construct a datatype constructor without a tester");
685 void DatatypeConstructor::setSygus( Expr op
, Expr let_body
, std::vector
< Expr
>& let_args
, unsigned num_let_input_args
){
687 d_sygus_let_body
= let_body
;
688 d_sygus_let_args
.insert( d_sygus_let_args
.end(), let_args
.begin(), let_args
.end() );
689 d_sygus_num_let_input_args
= num_let_input_args
;
692 void DatatypeConstructor::addArg(std::string selectorName
, Type selectorType
) {
693 // We don't want to introduce a new data member, because eventually
694 // we're going to be a constant stuffed inside a node. So we stow
695 // the selector type away inside a var until resolution (when we can
696 // create the proper selector type)
697 PrettyCheckArgument(!isResolved(), this, "cannot modify a finalized Datatype constructor");
698 PrettyCheckArgument(!selectorType
.isNull(), selectorType
, "cannot add a null selector type");
700 // we're using some internals, so we have to set up this library context
701 ExprManagerScope
ems(selectorType
);
703 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();
704 Debug("datatypes") << type
<< endl
;
705 d_args
.push_back(DatatypeConstructorArg(selectorName
, type
));
708 void DatatypeConstructor::addArg(std::string selectorName
, DatatypeUnresolvedType selectorType
) {
709 // We don't want to introduce a new data member, because eventually
710 // we're going to be a constant stuffed inside a node. So we stow
711 // the selector type away after a NUL in the name string until
712 // resolution (when we can create the proper selector type)
713 PrettyCheckArgument(!isResolved(), this, "cannot modify a finalized Datatype constructor");
714 PrettyCheckArgument(selectorType
.getName() != "", selectorType
, "cannot add a null selector type");
715 d_args
.push_back(DatatypeConstructorArg(selectorName
+ '\0' + selectorType
.getName(), Expr()));
718 void DatatypeConstructor::addArg(std::string selectorName
, DatatypeSelfType
) {
719 // We don't want to introduce a new data member, because eventually
720 // we're going to be a constant stuffed inside a node. So we mark
721 // the name string with a NUL to indicate that we have a
722 // self-selecting selector until resolution (when we can create the
723 // proper selector type)
724 PrettyCheckArgument(!isResolved(), this, "cannot modify a finalized Datatype constructor");
725 d_args
.push_back(DatatypeConstructorArg(selectorName
+ '\0', Expr()));
728 std::string
DatatypeConstructor::getName() const throw() {
729 return d_name
.substr(0, d_name
.find('\0'));
732 std::string
DatatypeConstructor::getTesterName() const throw() {
733 return d_name
.substr(d_name
.find('\0') + 1);
736 Expr
DatatypeConstructor::getConstructor() const {
737 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
738 return d_constructor
;
741 Type
DatatypeConstructor::getSpecializedConstructorType(Type returnType
) const {
742 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
743 ExprManagerScope
ems(d_constructor
);
744 const Datatype
& dt
= Datatype::datatypeOf(d_constructor
);
745 PrettyCheckArgument(dt
.isParametric(), this, "this datatype constructor is not parametric");
746 DatatypeType dtt
= dt
.getDatatypeType();
748 m
.doMatching( TypeNode::fromType(dtt
), TypeNode::fromType(returnType
) );
751 vector
<Type
> params
= dt
.getParameters();
752 return d_constructor
.getType().substitute(params
, subst
);
755 Expr
DatatypeConstructor::getTester() const {
756 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
760 Expr
DatatypeConstructor::getSygusOp() const {
761 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
765 Expr
DatatypeConstructor::getSygusLetBody() const {
766 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
767 return d_sygus_let_body
;
770 unsigned DatatypeConstructor::getNumSygusLetArgs() const {
771 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
772 return d_sygus_let_args
.size();
775 Expr
DatatypeConstructor::getSygusLetArg( unsigned i
) const {
776 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
777 return d_sygus_let_args
[i
];
780 unsigned DatatypeConstructor::getNumSygusLetInputArgs() const {
781 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
782 return d_sygus_num_let_input_args
;
785 bool DatatypeConstructor::isSygusIdFunc() const {
786 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
787 return d_sygus_let_args
.size()==1 && d_sygus_let_args
[0]==d_sygus_let_body
;
790 Cardinality
DatatypeConstructor::getCardinality() const throw(IllegalArgumentException
) {
791 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
795 for(const_iterator i
= begin(), i_end
= end(); i
!= i_end
; ++i
) {
796 c
*= SelectorType((*i
).getSelector().getType()).getRangeType().getCardinality();
802 /** compute the cardinality of this datatype */
803 Cardinality
DatatypeConstructor::computeCardinality( std::vector
< Type
>& processing
) const throw(IllegalArgumentException
){
805 for(const_iterator i
= begin(), i_end
= end(); i
!= i_end
; ++i
) {
806 Type t
= SelectorType((*i
).getSelector().getType()).getRangeType();
807 if( t
.isDatatype() ){
808 const Datatype
& dt
= ((DatatypeType
)t
).getDatatype();
809 c
*= dt
.computeCardinality( processing
);
811 c
*= t
.getCardinality();
817 bool DatatypeConstructor::computeWellFounded( std::vector
< Type
>& processing
) const throw(IllegalArgumentException
){
818 for(const_iterator i
= begin(), i_end
= end(); i
!= i_end
; ++i
) {
819 Type t
= SelectorType((*i
).getSelector().getType()).getRangeType();
820 if( t
.isDatatype() ){
821 const Datatype
& dt
= ((DatatypeType
)t
).getDatatype();
822 if( !dt
.computeWellFounded( processing
) ){
831 bool DatatypeConstructor::isFinite() const throw(IllegalArgumentException
) {
832 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
834 // we're using some internals, so we have to set up this library context
835 ExprManagerScope
ems(d_constructor
);
836 TNode self
= Node::fromExpr(d_constructor
);
837 // is this already in the cache ?
838 if(self
.getAttribute(DatatypeFiniteComputedAttr())) {
839 return self
.getAttribute(DatatypeFiniteAttr());
841 for(const_iterator i
= begin(), i_end
= end(); i
!= i_end
; ++i
) {
842 if(! (*i
).getRangeType().getCardinality().isFinite()) {
843 self
.setAttribute(DatatypeFiniteComputedAttr(), true);
844 self
.setAttribute(DatatypeFiniteAttr(), false);
848 self
.setAttribute(DatatypeFiniteComputedAttr(), true);
849 self
.setAttribute(DatatypeFiniteAttr(), true);
853 bool DatatypeConstructor::isInterpretedFinite() const throw(IllegalArgumentException
) {
854 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
855 // we're using some internals, so we have to set up this library context
856 ExprManagerScope
ems(d_constructor
);
857 TNode self
= Node::fromExpr(d_constructor
);
858 // is this already in the cache ?
859 if(self
.getAttribute(DatatypeUFiniteComputedAttr())) {
860 return self
.getAttribute(DatatypeUFiniteAttr());
862 for(const_iterator i
= begin(), i_end
= end(); i
!= i_end
; ++i
) {
863 TypeNode t
= TypeNode::fromType( (*i
).getRangeType() );
864 if(!t
.isInterpretedFinite()) {
865 self
.setAttribute(DatatypeUFiniteComputedAttr(), true);
866 self
.setAttribute(DatatypeUFiniteAttr(), false);
870 self
.setAttribute(DatatypeUFiniteComputedAttr(), true);
871 self
.setAttribute(DatatypeUFiniteAttr(), true);
875 Expr
DatatypeConstructor::computeGroundTerm( Type t
, std::vector
< Type
>& processing
, std::map
< Type
, Expr
>& gt
) const throw(IllegalArgumentException
) {
876 // we're using some internals, so we have to set up this library context
877 ExprManagerScope
ems(d_constructor
);
879 std::vector
<Expr
> groundTerms
;
880 groundTerms
.push_back(getConstructor());
882 // for each selector, get a ground term
883 std::vector
< Type
> instTypes
;
884 std::vector
< Type
> paramTypes
;
885 if( DatatypeType(t
).isParametric() ){
886 paramTypes
= DatatypeType(t
).getDatatype().getParameters();
887 instTypes
= DatatypeType(t
).getParamTypes();
889 for(const_iterator i
= begin(), i_end
= end(); i
!= i_end
; ++i
) {
890 Type selType
= SelectorType((*i
).getSelector().getType()).getRangeType();
891 if( DatatypeType(t
).isParametric() ){
892 selType
= selType
.substitute( paramTypes
, instTypes
);
895 if( selType
.isDatatype() ){
896 std::map
< Type
, Expr
>::iterator itgt
= gt
.find( selType
);
897 if( itgt
!= gt
.end() ){
900 const Datatype
& dt
= DatatypeType(selType
).getDatatype();
901 arg
= dt
.computeGroundTerm( selType
, processing
);
904 arg
= selType
.mkGroundTerm();
907 Debug("datatypes") << "...unable to construct arg of " << (*i
).getName() << std::endl
;
910 Debug("datatypes") << "...constructed arg " << arg
.getType() << std::endl
;
911 groundTerms
.push_back(arg
);
915 Expr groundTerm
= getConstructor().getExprManager()->mkExpr(kind::APPLY_CONSTRUCTOR
, groundTerms
);
916 if( groundTerm
.getType()!=t
){
917 Assert( Datatype::datatypeOf( d_constructor
).isParametric() );
918 //type is ambiguous, must apply type ascription
919 Debug("datatypes-gt") << "ambiguous type for " << groundTerm
<< ", ascribe to " << t
<< std::endl
;
920 groundTerms
[0] = getConstructor().getExprManager()->mkExpr(kind::APPLY_TYPE_ASCRIPTION
,
921 getConstructor().getExprManager()->mkConst(AscriptionType(getSpecializedConstructorType(t
))),
923 groundTerm
= getConstructor().getExprManager()->mkExpr(kind::APPLY_CONSTRUCTOR
, groundTerms
);
929 const DatatypeConstructorArg
& DatatypeConstructor::operator[](size_t index
) const {
930 PrettyCheckArgument(index
< getNumArgs(), index
, "index out of bounds");
931 return d_args
[index
];
934 const DatatypeConstructorArg
& DatatypeConstructor::operator[](std::string name
) const {
935 for(const_iterator i
= begin(); i
!= end(); ++i
) {
936 if((*i
).getName() == name
) {
940 IllegalArgument(name
, "No such arg `%s' of constructor `%s'", name
.c_str(), d_name
.c_str());
943 Expr
DatatypeConstructor::getSelector(std::string name
) const {
944 return (*this)[name
].getSelector();
947 bool DatatypeConstructor::involvesExternalType() const{
948 for(const_iterator i
= begin(); i
!= end(); ++i
) {
949 if(! SelectorType((*i
).getSelector().getType()).getRangeType().isDatatype()) {
956 bool DatatypeConstructor::involvesUninterpretedType() const{
957 for(const_iterator i
= begin(); i
!= end(); ++i
) {
958 if(SelectorType((*i
).getSelector().getType()).getRangeType().isSort()) {
965 DatatypeConstructorArg::DatatypeConstructorArg(std::string name
, Expr selector
) :
967 d_selector(selector
),
969 PrettyCheckArgument(name
!= "", name
, "cannot construct a datatype constructor arg without a name");
972 std::string
DatatypeConstructorArg::getName() const throw() {
973 string name
= d_name
;
974 const size_t nul
= name
.find('\0');
975 if(nul
!= string::npos
) {
981 Expr
DatatypeConstructorArg::getSelector() const {
982 PrettyCheckArgument(isResolved(), this, "cannot get a selector for an unresolved datatype constructor");
986 Expr
DatatypeConstructorArg::getConstructor() const {
987 PrettyCheckArgument(isResolved(), this,
988 "cannot get a associated constructor for argument of an unresolved datatype constructor");
989 return d_constructor
;
992 SelectorType
DatatypeConstructorArg::getType() const {
993 return getSelector().getType();
996 Type
DatatypeConstructorArg::getRangeType() const {
997 return getType().getRangeType();
1000 bool DatatypeConstructorArg::isUnresolvedSelf() const throw() {
1001 return d_selector
.isNull() && d_name
.size() == d_name
.find('\0') + 1;
1004 static const int s_printDatatypeNamesOnly
= std::ios_base::xalloc();
1006 std::string
DatatypeConstructorArg::getTypeName() const {
1009 t
= SelectorType(d_selector
.getType()).getRangeType();
1011 if(d_selector
.isNull()) {
1012 string typeName
= d_name
.substr(d_name
.find('\0') + 1);
1013 return (typeName
== "") ? "[self]" : typeName
;
1015 t
= d_selector
.getType();
1019 // Unfortunately, in the case of complex selector types, we can
1020 // enter nontrivial recursion here. Make sure that doesn't happen.
1022 ss
<< language::SetLanguage(language::output::LANG_CVC4
);
1023 ss
.iword(s_printDatatypeNamesOnly
) = 1;
1028 std::ostream
& operator<<(std::ostream
& os
, const Datatype
& dt
) {
1029 // These datatype things are recursive! Be very careful not to
1030 // print an infinite chain of them.
1031 long& printNameOnly
= os
.iword(s_printDatatypeNamesOnly
);
1032 Debug("datatypes-output") << "printNameOnly is " << printNameOnly
<< std::endl
;
1034 return os
<< dt
.getName();
1041 Scope(long& ref
, long value
) : d_ref(ref
), d_oldValue(ref
) { d_ref
= value
; }
1042 ~Scope() { d_ref
= d_oldValue
; }
1043 } scope(printNameOnly
, 1);
1044 // when scope is destructed, the value pops back
1046 Debug("datatypes-output") << "printNameOnly is now " << printNameOnly
<< std::endl
;
1048 // can only output datatypes in the CVC4 native language
1049 language::SetLanguage::Scope
ls(os
, language::output::LANG_CVC4
);
1051 os
<< "DATATYPE " << dt
.getName();
1052 if(dt
.isParametric()) {
1054 for(size_t i
= 0; i
< dt
.getNumParameters(); ++i
) {
1058 os
<< dt
.getParameter(i
);
1063 Datatype::const_iterator i
= dt
.begin(), i_end
= dt
.end();
1071 } while(i
!= i_end
);
1073 os
<< "END;" << endl
;
1078 std::ostream
& operator<<(std::ostream
& os
, const DatatypeConstructor
& ctor
) {
1079 // can only output datatypes in the CVC4 native language
1080 language::SetLanguage::Scope
ls(os
, language::output::LANG_CVC4
);
1082 os
<< ctor
.getName();
1084 DatatypeConstructor::const_iterator i
= ctor
.begin(), i_end
= ctor
.end();
1092 } while(i
!= i_end
);
1099 std::ostream
& operator<<(std::ostream
& os
, const DatatypeConstructorArg
& arg
) {
1100 // can only output datatypes in the CVC4 native language
1101 language::SetLanguage::Scope
ls(os
, language::output::LANG_CVC4
);
1103 os
<< arg
.getName() << ": " << arg
.getTypeName();
1108 }/* CVC4 namespace */