Builtin evaluation functions for sygus (#1991)
[cvc5.git] / src / expr / datatype.cpp
1 /********************* */
2 /*! \file datatype.cpp
3 ** \verbatim
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-2017 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
11 **
12 ** \brief A class representing a Datatype definition
13 **
14 ** A class representing a Datatype definition for the theory of
15 ** inductive datatypes.
16 **/
17 #include "expr/datatype.h"
18
19 #include <string>
20 #include <sstream>
21
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"
31 #include "options/datatypes_options.h"
32
33 using namespace std;
34
35 namespace CVC4 {
36
37 namespace expr {
38 namespace attr {
39 struct DatatypeIndexTag {};
40 struct DatatypeConsIndexTag {};
41 struct DatatypeFiniteTag {};
42 struct DatatypeFiniteComputedTag {};
43 struct DatatypeUFiniteTag {};
44 struct DatatypeUFiniteComputedTag {};
45 }/* CVC4::expr::attr namespace */
46 }/* CVC4::expr namespace */
47
48 typedef expr::Attribute<expr::attr::DatatypeIndexTag, uint64_t> DatatypeIndexAttr;
49 typedef expr::Attribute<expr::attr::DatatypeConsIndexTag, uint64_t> DatatypeConsIndexAttr;
50 typedef expr::Attribute<expr::attr::DatatypeFiniteTag, bool> DatatypeFiniteAttr;
51 typedef expr::Attribute<expr::attr::DatatypeFiniteComputedTag, bool> DatatypeFiniteComputedAttr;
52 typedef expr::Attribute<expr::attr::DatatypeUFiniteTag, bool> DatatypeUFiniteAttr;
53 typedef expr::Attribute<expr::attr::DatatypeUFiniteComputedTag, bool> DatatypeUFiniteComputedAttr;
54
55 Datatype::~Datatype(){
56 delete d_record;
57 }
58
59 const Datatype& Datatype::datatypeOf(Expr item) {
60 ExprManagerScope ems(item);
61 TypeNode t = Node::fromExpr(item).getType();
62 switch(t.getKind()) {
63 case kind::CONSTRUCTOR_TYPE:
64 return DatatypeType(t[t.getNumChildren() - 1].toType()).getDatatype();
65 case kind::SELECTOR_TYPE:
66 case kind::TESTER_TYPE:
67 return DatatypeType(t[0].toType()).getDatatype();
68 default:
69 Unhandled("arg must be a datatype constructor, selector, or tester");
70 }
71 }
72
73 size_t Datatype::indexOf(Expr item) {
74 ExprManagerScope ems(item);
75 PrettyCheckArgument(item.getType().isConstructor() ||
76 item.getType().isTester() ||
77 item.getType().isSelector(),
78 item,
79 "arg must be a datatype constructor, selector, or tester");
80 return indexOfInternal(item);
81 }
82
83 size_t Datatype::indexOfInternal(Expr item)
84 {
85 TNode n = Node::fromExpr(item);
86 if( item.getKind()==kind::APPLY_TYPE_ASCRIPTION ){
87 return indexOf( item[0] );
88 }else{
89 Assert(n.hasAttribute(DatatypeIndexAttr()));
90 return n.getAttribute(DatatypeIndexAttr());
91 }
92 }
93
94 size_t Datatype::cindexOf(Expr item) {
95 ExprManagerScope ems(item);
96 PrettyCheckArgument(item.getType().isSelector(),
97 item,
98 "arg must be a datatype selector");
99 return cindexOfInternal(item);
100 }
101 size_t Datatype::cindexOfInternal(Expr item)
102 {
103 TNode n = Node::fromExpr(item);
104 if( item.getKind()==kind::APPLY_TYPE_ASCRIPTION ){
105 return cindexOf( item[0] );
106 }else{
107 Assert(n.hasAttribute(DatatypeConsIndexAttr()));
108 return n.getAttribute(DatatypeConsIndexAttr());
109 }
110 }
111
112 void Datatype::resolve(ExprManager* em,
113 const std::map<std::string, DatatypeType>& resolutions,
114 const std::vector<Type>& placeholders,
115 const std::vector<Type>& replacements,
116 const std::vector< SortConstructorType >& paramTypes,
117 const std::vector< DatatypeType >& paramReplacements)
118 {
119 PrettyCheckArgument(em != NULL, em, "cannot resolve a Datatype with a NULL expression manager");
120 PrettyCheckArgument(!d_resolved, this, "cannot resolve a Datatype twice");
121 PrettyCheckArgument(resolutions.find(d_name) != resolutions.end(), resolutions,
122 "Datatype::resolve(): resolutions doesn't contain me!");
123 PrettyCheckArgument(placeholders.size() == replacements.size(), placeholders,
124 "placeholders and replacements must be the same size");
125 PrettyCheckArgument(paramTypes.size() == paramReplacements.size(), paramTypes,
126 "paramTypes and paramReplacements must be the same size");
127 PrettyCheckArgument(getNumConstructors() > 0, *this, "cannot resolve a Datatype that has no constructors");
128 DatatypeType self = (*resolutions.find(d_name)).second;
129 PrettyCheckArgument(&self.getDatatype() == this, resolutions, "Datatype::resolve(): resolutions doesn't contain me!");
130 d_resolved = true;
131 size_t index = 0;
132 for(std::vector<DatatypeConstructor>::iterator i = d_constructors.begin(), i_end = d_constructors.end(); i != i_end; ++i) {
133 (*i).resolve(em, self, resolutions, placeholders, replacements, paramTypes, paramReplacements, index);
134 Node::fromExpr((*i).d_constructor).setAttribute(DatatypeIndexAttr(), index);
135 Node::fromExpr((*i).d_tester).setAttribute(DatatypeIndexAttr(), index++);
136 }
137 d_self = self;
138
139 d_involvesExt = false;
140 d_involvesUt = false;
141 for(const_iterator i = begin(); i != end(); ++i) {
142 if( (*i).involvesExternalType() ){
143 d_involvesExt = true;
144 }
145 if( (*i).involvesUninterpretedType() ){
146 d_involvesUt = true;
147 }
148 }
149
150 if( d_isRecord ){
151 std::vector< std::pair<std::string, Type> > fields;
152 for( unsigned i=0; i<(*this)[0].getNumArgs(); i++ ){
153 fields.push_back( std::pair<std::string, Type>( (*this)[0][i].getName(), (*this)[0][i].getRangeType() ) );
154 }
155 d_record = new Record(fields);
156 }
157
158 //make the sygus evaluation function
159 if( isSygus() ){
160 PrettyCheckArgument(d_params.empty(), this, "sygus types cannot be parametric");
161 NodeManager* nm = NodeManager::fromExprManager(em);
162 std::string name = "eval_" + getName();
163 std::vector<TypeNode> evalType;
164 evalType.push_back(TypeNode::fromType(d_self));
165 if( !d_sygus_bvl.isNull() ){
166 for(size_t j = 0; j < d_sygus_bvl.getNumChildren(); ++j) {
167 evalType.push_back(TypeNode::fromType(d_sygus_bvl[j].getType()));
168 }
169 }
170 evalType.push_back(TypeNode::fromType(d_sygus_type));
171 TypeNode eval_func_type = nm->mkFunctionType(evalType);
172 d_sygus_eval = nm->mkSkolem(name, eval_func_type, "sygus evaluation function").toExpr();
173 }
174 }
175
176 void Datatype::addConstructor(const DatatypeConstructor& c) {
177 PrettyCheckArgument(!d_resolved, this,
178 "cannot add a constructor to a finalized Datatype");
179 d_constructors.push_back(c);
180 }
181
182
183 void Datatype::setSygus( Type st, Expr bvl, bool allow_const, bool allow_all ){
184 PrettyCheckArgument(!d_resolved, this,
185 "cannot set sygus type to a finalized Datatype");
186 d_sygus_type = st;
187 d_sygus_bvl = bvl;
188 d_sygus_allow_const = allow_const || allow_all;
189 d_sygus_allow_all = allow_all;
190 }
191
192 void Datatype::addSygusConstructor(Expr op,
193 std::string& cname,
194 std::vector<Type>& cargs,
195 std::shared_ptr<SygusPrintCallback> spc,
196 int weight)
197 {
198 Debug("dt-sygus") << "--> Add constructor " << cname << " to " << getName() << std::endl;
199 Debug("dt-sygus") << " sygus op : " << op << std::endl;
200 std::string name = getName() + "_" + cname;
201 std::string testerId("is-");
202 testerId.append(name);
203 unsigned cweight = weight >= 0 ? weight : (cargs.empty() ? 0 : 1);
204 DatatypeConstructor c(name, testerId, cweight);
205 c.setSygus(op, spc);
206 for( unsigned j=0; j<cargs.size(); j++ ){
207 Debug("parser-sygus-debug") << " arg " << j << " : " << cargs[j] << std::endl;
208 std::stringstream sname;
209 sname << name << "_" << j;
210 c.addArg(sname.str(), cargs[j]);
211 }
212 addConstructor(c);
213 }
214
215 void Datatype::setTuple() {
216 PrettyCheckArgument(!d_resolved, this, "cannot set tuple to a finalized Datatype");
217 d_isTuple = true;
218 }
219
220 void Datatype::setRecord() {
221 PrettyCheckArgument(!d_resolved, this, "cannot set record to a finalized Datatype");
222 d_isRecord = true;
223 }
224
225 Cardinality Datatype::getCardinality(Type t) const
226 {
227 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
228 Assert( t.isDatatype() && ((DatatypeType)t).getDatatype()==*this );
229 std::vector< Type > processing;
230 computeCardinality( t, processing );
231 return d_card;
232 }
233
234 Cardinality Datatype::getCardinality() const
235 {
236 PrettyCheckArgument(!isParametric(), this, "for getCardinality, this datatype cannot be parametric");
237 return getCardinality( d_self );
238 }
239
240 Cardinality Datatype::computeCardinality(Type t,
241 std::vector<Type>& processing) const
242 {
243 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
244 if( std::find( processing.begin(), processing.end(), d_self )!=processing.end() ){
245 d_card = Cardinality::INTEGERS;
246 }else{
247 processing.push_back( d_self );
248 Cardinality c = 0;
249 for(const_iterator i = begin(), i_end = end(); i != i_end; ++i) {
250 c += (*i).computeCardinality( t, processing );
251 }
252 d_card = c;
253 processing.pop_back();
254 }
255 return d_card;
256 }
257
258 bool Datatype::isRecursiveSingleton(Type t) const
259 {
260 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
261 Assert( t.isDatatype() && ((DatatypeType)t).getDatatype()==*this );
262 if( d_card_rec_singleton.find( t )==d_card_rec_singleton.end() ){
263 if( isCodatatype() ){
264 Assert( d_card_u_assume[t].empty() );
265 std::vector< Type > processing;
266 if( computeCardinalityRecSingleton( t, processing, d_card_u_assume[t] ) ){
267 d_card_rec_singleton[t] = 1;
268 }else{
269 d_card_rec_singleton[t] = -1;
270 }
271 if( d_card_rec_singleton[t]==1 ){
272 Trace("dt-card") << "Datatype " << getName() << " is recursive singleton, dependent upon " << d_card_u_assume[t].size() << " uninterpreted sorts: " << std::endl;
273 for( unsigned i=0; i<d_card_u_assume[t].size(); i++ ){
274 Trace("dt-card") << " " << d_card_u_assume[t][i] << std::endl;
275 }
276 Trace("dt-card") << std::endl;
277 }
278 }else{
279 d_card_rec_singleton[t] = -1;
280 }
281 }
282 return d_card_rec_singleton[t]==1;
283 }
284
285 bool Datatype::isRecursiveSingleton() const
286 {
287 PrettyCheckArgument(!isParametric(), this, "for isRecursiveSingleton, this datatype cannot be parametric");
288 return isRecursiveSingleton( d_self );
289 }
290
291 unsigned Datatype::getNumRecursiveSingletonArgTypes(Type t) const
292 {
293 Assert( d_card_rec_singleton.find( t )!=d_card_rec_singleton.end() );
294 Assert( isRecursiveSingleton( t ) );
295 return d_card_u_assume[t].size();
296 }
297
298 unsigned Datatype::getNumRecursiveSingletonArgTypes() const
299 {
300 PrettyCheckArgument(!isParametric(), this, "for getNumRecursiveSingletonArgTypes, this datatype cannot be parametric");
301 return getNumRecursiveSingletonArgTypes( d_self );
302 }
303
304 Type Datatype::getRecursiveSingletonArgType(Type t, unsigned i) const
305 {
306 Assert( d_card_rec_singleton.find( t )!=d_card_rec_singleton.end() );
307 Assert( isRecursiveSingleton( t ) );
308 return d_card_u_assume[t][i];
309 }
310
311 Type Datatype::getRecursiveSingletonArgType(unsigned i) const
312 {
313 PrettyCheckArgument(!isParametric(), this, "for getRecursiveSingletonArgType, this datatype cannot be parametric");
314 return getRecursiveSingletonArgType( d_self, i );
315 }
316
317 bool Datatype::computeCardinalityRecSingleton(Type t,
318 std::vector<Type>& processing,
319 std::vector<Type>& u_assume) const
320 {
321 if( std::find( processing.begin(), processing.end(), d_self )!=processing.end() ){
322 return true;
323 }else{
324 if( d_card_rec_singleton[t]==0 ){
325 //if not yet computed
326 if( d_constructors.size()==1 ){
327 bool success = false;
328 processing.push_back( d_self );
329 for(unsigned i = 0; i<d_constructors[0].getNumArgs(); i++ ) {
330 Type tc = ((SelectorType)d_constructors[0][i].getType()).getRangeType();
331 //if it is an uninterpreted sort, then we depend on it having cardinality one
332 if( tc.isSort() ){
333 if( std::find( u_assume.begin(), u_assume.end(), tc )==u_assume.end() ){
334 u_assume.push_back( tc );
335 }
336 //if it is a datatype, recurse
337 }else if( tc.isDatatype() ){
338 const Datatype & dt = ((DatatypeType)tc).getDatatype();
339 if( !dt.computeCardinalityRecSingleton( t, processing, u_assume ) ){
340 return false;
341 }else{
342 success = true;
343 }
344 //if it is a builtin type, it must have cardinality one
345 }else if( !tc.getCardinality().isOne() ){
346 return false;
347 }
348 }
349 processing.pop_back();
350 return success;
351 }else{
352 return false;
353 }
354 }else if( d_card_rec_singleton[t]==-1 ){
355 return false;
356 }else{
357 for( unsigned i=0; i<d_card_u_assume[t].size(); i++ ){
358 if( std::find( u_assume.begin(), u_assume.end(), d_card_u_assume[t][i] )==u_assume.end() ){
359 u_assume.push_back( d_card_u_assume[t][i] );
360 }
361 }
362 return true;
363 }
364 }
365 }
366
367 bool Datatype::isFinite(Type t) const
368 {
369 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
370 Assert( t.isDatatype() && ((DatatypeType)t).getDatatype()==*this );
371
372 // we're using some internals, so we have to set up this library context
373 ExprManagerScope ems(d_self);
374 TypeNode self = TypeNode::fromType(d_self);
375 // is this already in the cache ?
376 if(self.getAttribute(DatatypeFiniteComputedAttr())) {
377 return self.getAttribute(DatatypeFiniteAttr());
378 }
379 for(const_iterator i = begin(), i_end = end(); i != i_end; ++i) {
380 if(! (*i).isFinite( t )) {
381 self.setAttribute(DatatypeFiniteComputedAttr(), true);
382 self.setAttribute(DatatypeFiniteAttr(), false);
383 return false;
384 }
385 }
386 self.setAttribute(DatatypeFiniteComputedAttr(), true);
387 self.setAttribute(DatatypeFiniteAttr(), true);
388 return true;
389 }
390 bool Datatype::isFinite() const
391 {
392 PrettyCheckArgument(isResolved() && !isParametric(), this, "this datatype must be resolved and not parametric");
393 return isFinite( d_self );
394 }
395
396 bool Datatype::isInterpretedFinite(Type t) const
397 {
398 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
399 Assert( t.isDatatype() && ((DatatypeType)t).getDatatype()==*this );
400 // we're using some internals, so we have to set up this library context
401 ExprManagerScope ems(d_self);
402 TypeNode self = TypeNode::fromType(d_self);
403 // is this already in the cache ?
404 if(self.getAttribute(DatatypeUFiniteComputedAttr())) {
405 return self.getAttribute(DatatypeUFiniteAttr());
406 }
407 //start by assuming it is not
408 self.setAttribute(DatatypeUFiniteComputedAttr(), true);
409 self.setAttribute(DatatypeUFiniteAttr(), false);
410 for(const_iterator i = begin(), i_end = end(); i != i_end; ++i) {
411 if(! (*i).isInterpretedFinite( t )) {
412 return false;
413 }
414 }
415 self.setAttribute(DatatypeUFiniteComputedAttr(), true);
416 self.setAttribute(DatatypeUFiniteAttr(), true);
417 return true;
418 }
419 bool Datatype::isInterpretedFinite() const
420 {
421 PrettyCheckArgument(isResolved() && !isParametric(), this, "this datatype must be resolved and not parametric");
422 return isInterpretedFinite( d_self );
423 }
424
425 bool Datatype::isWellFounded() const
426 {
427 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
428 if( d_well_founded==0 ){
429 // we're using some internals, so we have to set up this library context
430 ExprManagerScope ems(d_self);
431 std::vector< Type > processing;
432 if( computeWellFounded( processing ) ){
433 d_well_founded = 1;
434 }else{
435 d_well_founded = -1;
436 }
437 }
438 return d_well_founded==1;
439 }
440
441 bool Datatype::computeWellFounded(std::vector<Type>& processing) const
442 {
443 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
444 if( std::find( processing.begin(), processing.end(), d_self )!=processing.end() ){
445 return d_isCo;
446 }else{
447 processing.push_back( d_self );
448 for(const_iterator i = begin(), i_end = end(); i != i_end; ++i) {
449 if( (*i).computeWellFounded( processing ) ){
450 processing.pop_back();
451 return true;
452 }else{
453 Trace("dt-wf") << "Constructor " << (*i).getName() << " is not well-founded." << std::endl;
454 }
455 }
456 processing.pop_back();
457 Trace("dt-wf") << "Datatype " << getName() << " is not well-founded." << std::endl;
458 return false;
459 }
460 }
461
462 Expr Datatype::mkGroundTerm(Type t) const
463 {
464 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
465 ExprManagerScope ems(d_self);
466 Debug("datatypes") << "mkGroundTerm of type " << t << std::endl;
467 // is this already in the cache ?
468 std::map< Type, Expr >::iterator it = d_ground_term.find( t );
469 if( it != d_ground_term.end() ){
470 Debug("datatypes") << "\nin cache: " << d_self << " => " << it->second << std::endl;
471 return it->second;
472 } else {
473 std::vector< Type > processing;
474 Expr groundTerm = computeGroundTerm( t, processing );
475 if(!groundTerm.isNull() ) {
476 // we found a ground-term-constructing constructor!
477 d_ground_term[t] = groundTerm;
478 Debug("datatypes") << "constructed: " << getName() << " => " << groundTerm << std::endl;
479 }
480 if( groundTerm.isNull() ){
481 if( !d_isCo ){
482 // if we get all the way here, we aren't well-founded
483 IllegalArgument(*this, "datatype is not well-founded, cannot construct a ground term!");
484 }else{
485 return groundTerm;
486 }
487 }else{
488 return groundTerm;
489 }
490 }
491 }
492
493 Expr getSubtermWithType( Expr e, Type t, bool isTop ){
494 if( !isTop && e.getType()==t ){
495 return e;
496 }else{
497 for( unsigned i=0; i<e.getNumChildren(); i++ ){
498 Expr se = getSubtermWithType( e[i], t, false );
499 if( !se.isNull() ){
500 return se;
501 }
502 }
503 return Expr();
504 }
505 }
506
507 Expr Datatype::computeGroundTerm(Type t, std::vector<Type>& processing) const
508 {
509 if( std::find( processing.begin(), processing.end(), t )==processing.end() ){
510 processing.push_back( t );
511 for( unsigned r=0; r<2; r++ ){
512 for(const_iterator i = begin(), i_end = end(); i != i_end; ++i) {
513 //do nullary constructors first
514 if( ((*i).getNumArgs()==0)==(r==0)){
515 Debug("datatypes") << "Try constructing for " << (*i).getName() << ", processing = " << processing.size() << std::endl;
516 Expr e = (*i).computeGroundTerm( t, processing, d_ground_term );
517 if( !e.isNull() ){
518 //must check subterms for the same type to avoid infinite loops in type enumeration
519 Expr se = getSubtermWithType( e, t, true );
520 if( !se.isNull() ){
521 Debug("datatypes") << "Take subterm " << se << std::endl;
522 e = se;
523 }
524 processing.pop_back();
525 return e;
526 }else{
527 Debug("datatypes") << "...failed." << std::endl;
528 }
529 }
530 }
531 }
532 processing.pop_back();
533 }else{
534 Debug("datatypes") << "...already processing " << t << " " << d_self << std::endl;
535 }
536 return Expr();
537 }
538
539 DatatypeType Datatype::getDatatypeType() const
540 {
541 PrettyCheckArgument(isResolved(), *this, "Datatype must be resolved to get its DatatypeType");
542 PrettyCheckArgument(!d_self.isNull(), *this);
543 return DatatypeType(d_self);
544 }
545
546 DatatypeType Datatype::getDatatypeType(const std::vector<Type>& params) const
547 {
548 PrettyCheckArgument(isResolved(), *this, "Datatype must be resolved to get its DatatypeType");
549 PrettyCheckArgument(!d_self.isNull() && DatatypeType(d_self).isParametric(), this);
550 return DatatypeType(d_self).instantiate(params);
551 }
552
553 bool Datatype::operator==(const Datatype& other) const
554 {
555 // two datatypes are == iff the name is the same and they have
556 // exactly matching constructors (in the same order)
557
558 if(this == &other) {
559 return true;
560 }
561
562 if(isResolved() != other.isResolved()) {
563 return false;
564 }
565
566 if( d_name != other.d_name ||
567 getNumConstructors() != other.getNumConstructors() ) {
568 return false;
569 }
570 for(const_iterator i = begin(), j = other.begin(); i != end(); ++i, ++j) {
571 Assert(j != other.end());
572 // two constructors are == iff they have the same name, their
573 // constructors and testers are equal and they have exactly
574 // matching args (in the same order)
575 if((*i).getName() != (*j).getName() ||
576 (*i).getNumArgs() != (*j).getNumArgs()) {
577 return false;
578 }
579 // testing equivalence of constructors and testers is harder b/c
580 // this constructor might not be resolved yet; only compare them
581 // if they are both resolved
582 Assert(isResolved() == !(*i).d_constructor.isNull() &&
583 isResolved() == !(*i).d_tester.isNull() &&
584 (*i).d_constructor.isNull() == (*j).d_constructor.isNull() &&
585 (*i).d_tester.isNull() == (*j).d_tester.isNull());
586 if(!(*i).d_constructor.isNull() && (*i).d_constructor != (*j).d_constructor) {
587 return false;
588 }
589 if(!(*i).d_tester.isNull() && (*i).d_tester != (*j).d_tester) {
590 return false;
591 }
592 for(DatatypeConstructor::const_iterator k = (*i).begin(), l = (*j).begin(); k != (*i).end(); ++k, ++l) {
593 Assert(l != (*j).end());
594 if((*k).getName() != (*l).getName()) {
595 return false;
596 }
597 // testing equivalence of selectors is harder b/c args might not
598 // be resolved yet
599 Assert(isResolved() == (*k).isResolved() &&
600 (*k).isResolved() == (*l).isResolved());
601 if((*k).isResolved()) {
602 // both are resolved, so simply compare the selectors directly
603 if((*k).d_selector != (*l).d_selector) {
604 return false;
605 }
606 } else {
607 // neither is resolved, so compare their (possibly unresolved)
608 // types; we don't know if they'll be resolved the same way,
609 // so we can't ever say unresolved types are equal
610 if(!(*k).d_selector.isNull() && !(*l).d_selector.isNull()) {
611 if((*k).d_selector.getType() != (*l).d_selector.getType()) {
612 return false;
613 }
614 } else {
615 if((*k).isUnresolvedSelf() && (*l).isUnresolvedSelf()) {
616 // Fine, the selectors are equal if the rest of the
617 // enclosing datatypes are equal...
618 } else {
619 return false;
620 }
621 }
622 }
623 }
624 }
625 return true;
626 }
627
628 const DatatypeConstructor& Datatype::operator[](size_t index) const {
629 PrettyCheckArgument(index < getNumConstructors(), index, "index out of bounds");
630 return d_constructors[index];
631 }
632
633 const DatatypeConstructor& Datatype::operator[](std::string name) const {
634 for(const_iterator i = begin(); i != end(); ++i) {
635 if((*i).getName() == name) {
636 return *i;
637 }
638 }
639 IllegalArgument(name, "No such constructor `%s' of datatype `%s'", name.c_str(), d_name.c_str());
640 }
641
642
643 Expr Datatype::getSharedSelector( Type dtt, Type t, unsigned index ) const{
644 PrettyCheckArgument(isResolved(), this, "this datatype is not yet resolved");
645 std::map< Type, std::map< Type, std::map< unsigned, Expr > > >::iterator itd = d_shared_sel.find( dtt );
646 if( itd!=d_shared_sel.end() ){
647 std::map< Type, std::map< unsigned, Expr > >::iterator its = itd->second.find( t );
648 if( its!=itd->second.end() ){
649 std::map< unsigned, Expr >::iterator it = its->second.find( index );
650 if( it!=its->second.end() ){
651 return it->second;
652 }
653 }
654 }
655 //make the shared selector
656 Expr s;
657 NodeManager* nm = NodeManager::fromExprManager( d_self.getExprManager() );
658 std::stringstream ss;
659 ss << "sel_" << index;
660 s = nm->mkSkolem(ss.str(), nm->mkSelectorType(TypeNode::fromType(dtt), TypeNode::fromType(t)), "is a shared selector", NodeManager::SKOLEM_NO_NOTIFY).toExpr();
661 d_shared_sel[dtt][t][index] = s;
662 Trace("dt-shared-sel") << "Made " << s << " of type " << dtt << " -> " << t << std::endl;
663 return s;
664 }
665
666 Expr Datatype::getConstructor(std::string name) const {
667 return (*this)[name].getConstructor();
668 }
669
670 Type Datatype::getSygusType() const {
671 return d_sygus_type;
672 }
673
674 Expr Datatype::getSygusVarList() const {
675 return d_sygus_bvl;
676 }
677
678 bool Datatype::getSygusAllowConst() const {
679 return d_sygus_allow_const;
680 }
681
682 bool Datatype::getSygusAllowAll() const {
683 return d_sygus_allow_all;
684 }
685
686 Expr Datatype::getSygusEvaluationFunc() const {
687 return d_sygus_eval;
688 }
689
690 bool Datatype::involvesExternalType() const{
691 return d_involvesExt;
692 }
693
694 bool Datatype::involvesUninterpretedType() const{
695 return d_involvesUt;
696 }
697
698 void DatatypeConstructor::resolve(ExprManager* em, DatatypeType self,
699 const std::map<std::string, DatatypeType>& resolutions,
700 const std::vector<Type>& placeholders,
701 const std::vector<Type>& replacements,
702 const std::vector< SortConstructorType >& paramTypes,
703 const std::vector< DatatypeType >& paramReplacements, size_t cindex)
704 {
705 PrettyCheckArgument(em != NULL, em, "cannot resolve a Datatype with a NULL expression manager");
706 PrettyCheckArgument(!isResolved(),
707 "cannot resolve a Datatype constructor twice; "
708 "perhaps the same constructor was added twice, "
709 "or to two datatypes?");
710
711 // we're using some internals, so we have to set up this library context
712 ExprManagerScope ems(*em);
713
714 NodeManager* nm = NodeManager::fromExprManager(em);
715 TypeNode selfTypeNode = TypeNode::fromType(self);
716 size_t index = 0;
717 for(std::vector<DatatypeConstructorArg>::iterator i = d_args.begin(), i_end = d_args.end(); i != i_end; ++i) {
718 if((*i).d_selector.isNull()) {
719 // the unresolved type wasn't created here; do name resolution
720 string typeName = (*i).d_name.substr((*i).d_name.find('\0') + 1);
721 (*i).d_name.resize((*i).d_name.find('\0'));
722 if(typeName == "") {
723 (*i).d_selector = nm->mkSkolem((*i).d_name, nm->mkSelectorType(selfTypeNode, selfTypeNode), "is a selector", NodeManager::SKOLEM_EXACT_NAME | NodeManager::SKOLEM_NO_NOTIFY).toExpr();
724 } else {
725 map<string, DatatypeType>::const_iterator j = resolutions.find(typeName);
726 if(j == resolutions.end()) {
727 stringstream msg;
728 msg << "cannot resolve type \"" << typeName << "\" "
729 << "in selector \"" << (*i).d_name << "\" "
730 << "of constructor \"" << d_name << "\"";
731 throw DatatypeResolutionException(msg.str());
732 } else {
733 (*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();
734 }
735 }
736 } else {
737 // the type for the selector already exists; may need
738 // complex-type substitution
739 Type range = (*i).d_selector.getType();
740 if(!placeholders.empty()) {
741 range = range.substitute(placeholders, replacements);
742 }
743 if(!paramTypes.empty() ) {
744 range = doParametricSubstitution( range, paramTypes, paramReplacements );
745 }
746 (*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();
747 }
748 Node::fromExpr((*i).d_selector).setAttribute(DatatypeConsIndexAttr(), cindex);
749 Node::fromExpr((*i).d_selector).setAttribute(DatatypeIndexAttr(), index++);
750 (*i).d_resolved = true;
751 }
752
753 Assert(index == getNumArgs());
754
755 // Set constructor/tester last, since DatatypeConstructor::isResolved()
756 // returns true when d_tester is not the null Expr. If something
757 // fails above, we want Constuctor::isResolved() to remain "false".
758 // Further, mkConstructorType() iterates over the selectors, so
759 // should get the results of any resolutions we did above.
760 d_tester = nm->mkSkolem(getTesterName(), nm->mkTesterType(selfTypeNode), "is a tester", NodeManager::SKOLEM_EXACT_NAME | NodeManager::SKOLEM_NO_NOTIFY).toExpr();
761 d_constructor = nm->mkSkolem(getName(), nm->mkConstructorType(*this, selfTypeNode), "is a constructor", NodeManager::SKOLEM_EXACT_NAME | NodeManager::SKOLEM_NO_NOTIFY).toExpr();
762 // associate constructor with all selectors
763 for(std::vector<DatatypeConstructorArg>::iterator i = d_args.begin(), i_end = d_args.end(); i != i_end; ++i) {
764 (*i).d_constructor = d_constructor;
765 }
766 }
767
768 Type DatatypeConstructor::doParametricSubstitution( Type range,
769 const std::vector< SortConstructorType >& paramTypes,
770 const std::vector< DatatypeType >& paramReplacements ) {
771 TypeNode typn = TypeNode::fromType( range );
772 if(typn.getNumChildren() == 0) {
773 return range;
774 } else {
775 std::vector< Type > origChildren;
776 std::vector< Type > children;
777 for(TypeNode::const_iterator i = typn.begin(), iend = typn.end();i != iend; ++i) {
778 origChildren.push_back( (*i).toType() );
779 children.push_back( doParametricSubstitution( (*i).toType(), paramTypes, paramReplacements ) );
780 }
781 for( unsigned i = 0; i < paramTypes.size(); ++i ) {
782 if( paramTypes[i].getArity() == origChildren.size() ) {
783 Type tn = paramTypes[i].instantiate( origChildren );
784 if( range == tn ) {
785 return paramReplacements[i].instantiate( children );
786 }
787 }
788 }
789 NodeBuilder<> nb(typn.getKind());
790 for( unsigned i = 0; i < children.size(); ++i ) {
791 nb << TypeNode::fromType( children[i] );
792 }
793 return nb.constructTypeNode().toType();
794 }
795 }
796
797 DatatypeConstructor::DatatypeConstructor(std::string name)
798 : // We don't want to introduce a new data member, because eventually
799 // we're going to be a constant stuffed inside a node. So we stow
800 // the tester name away inside the constructor name until
801 // resolution.
802 d_name(name + '\0' + "is_" + name), // default tester name is "is_FOO"
803 d_tester(),
804 d_args(),
805 d_sygus_pc(nullptr),
806 d_weight(1)
807 {
808 PrettyCheckArgument(name != "", name, "cannot construct a datatype constructor without a name");
809 }
810
811 DatatypeConstructor::DatatypeConstructor(std::string name,
812 std::string tester,
813 unsigned weight)
814 : // We don't want to introduce a new data member, because eventually
815 // we're going to be a constant stuffed inside a node. So we stow
816 // the tester name away inside the constructor name until
817 // resolution.
818 d_name(name + '\0' + tester),
819 d_tester(),
820 d_args(),
821 d_sygus_pc(nullptr),
822 d_weight(weight)
823 {
824 PrettyCheckArgument(name != "", name, "cannot construct a datatype constructor without a name");
825 PrettyCheckArgument(!tester.empty(), tester, "cannot construct a datatype constructor without a tester");
826 }
827
828 void DatatypeConstructor::setSygus(Expr op,
829 std::shared_ptr<SygusPrintCallback> spc)
830 {
831 PrettyCheckArgument(
832 !isResolved(), this, "cannot modify a finalized Datatype constructor");
833 d_sygus_op = op;
834 d_sygus_pc = spc;
835 }
836
837 void DatatypeConstructor::addArg(std::string selectorName, Type selectorType) {
838 // We don't want to introduce a new data member, because eventually
839 // we're going to be a constant stuffed inside a node. So we stow
840 // the selector type away inside a var until resolution (when we can
841 // create the proper selector type)
842 PrettyCheckArgument(!isResolved(), this, "cannot modify a finalized Datatype constructor");
843 PrettyCheckArgument(!selectorType.isNull(), selectorType, "cannot add a null selector type");
844
845 // we're using some internals, so we have to set up this library context
846 ExprManagerScope ems(selectorType);
847
848 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();
849 Debug("datatypes") << type << endl;
850 d_args.push_back(DatatypeConstructorArg(selectorName, type));
851 }
852
853 void DatatypeConstructor::addArg(std::string selectorName, DatatypeUnresolvedType selectorType) {
854 // We don't want to introduce a new data member, because eventually
855 // we're going to be a constant stuffed inside a node. So we stow
856 // the selector type away after a NUL in the name string until
857 // resolution (when we can create the proper selector type)
858 PrettyCheckArgument(!isResolved(), this, "cannot modify a finalized Datatype constructor");
859 PrettyCheckArgument(selectorType.getName() != "", selectorType, "cannot add a null selector type");
860 d_args.push_back(DatatypeConstructorArg(selectorName + '\0' + selectorType.getName(), Expr()));
861 }
862
863 void DatatypeConstructor::addArg(std::string selectorName, DatatypeSelfType) {
864 // We don't want to introduce a new data member, because eventually
865 // we're going to be a constant stuffed inside a node. So we mark
866 // the name string with a NUL to indicate that we have a
867 // self-selecting selector until resolution (when we can create the
868 // proper selector type)
869 PrettyCheckArgument(!isResolved(), this, "cannot modify a finalized Datatype constructor");
870 d_args.push_back(DatatypeConstructorArg(selectorName + '\0', Expr()));
871 }
872
873 std::string DatatypeConstructor::getName() const
874 {
875 return d_name.substr(0, d_name.find('\0'));
876 }
877
878 std::string DatatypeConstructor::getTesterName() const
879 {
880 return d_name.substr(d_name.find('\0') + 1);
881 }
882
883 Expr DatatypeConstructor::getConstructor() const {
884 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
885 return d_constructor;
886 }
887
888 Type DatatypeConstructor::getSpecializedConstructorType(Type returnType) const {
889 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
890 PrettyCheckArgument(returnType.isDatatype(), this, "cannot get specialized constructor type for non-datatype type");
891 ExprManagerScope ems(d_constructor);
892 const Datatype& dt = Datatype::datatypeOf(d_constructor);
893 PrettyCheckArgument(dt.isParametric(), this, "this datatype constructor is not parametric");
894 DatatypeType dtt = dt.getDatatypeType();
895 Matcher m(dtt);
896 m.doMatching( TypeNode::fromType(dtt), TypeNode::fromType(returnType) );
897 vector<Type> subst;
898 m.getMatches(subst);
899 vector<Type> params = dt.getParameters();
900 return d_constructor.getType().substitute(params, subst);
901 }
902
903 Expr DatatypeConstructor::getTester() const {
904 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
905 return d_tester;
906 }
907
908 Expr DatatypeConstructor::getSygusOp() const {
909 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
910 return d_sygus_op;
911 }
912
913 bool DatatypeConstructor::isSygusIdFunc() const {
914 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
915 return (d_sygus_op.getKind() == kind::LAMBDA
916 && d_sygus_op[0].getNumChildren() == 1
917 && d_sygus_op[0][0] == d_sygus_op[1]);
918 }
919
920 unsigned DatatypeConstructor::getWeight() const
921 {
922 PrettyCheckArgument(
923 isResolved(), this, "this datatype constructor is not yet resolved");
924 return d_weight;
925 }
926
927 std::shared_ptr<SygusPrintCallback> DatatypeConstructor::getSygusPrintCallback() const
928 {
929 PrettyCheckArgument(
930 isResolved(), this, "this datatype constructor is not yet resolved");
931 return d_sygus_pc;
932 }
933
934 Cardinality DatatypeConstructor::getCardinality(Type t) const
935 {
936 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
937
938 Cardinality c = 1;
939
940 for(const_iterator i = begin(), i_end = end(); i != i_end; ++i) {
941 c *= SelectorType((*i).getSelector().getType()).getRangeType().getCardinality();
942 }
943
944 return c;
945 }
946
947 /** compute the cardinality of this datatype */
948 Cardinality DatatypeConstructor::computeCardinality(
949 Type t, std::vector<Type>& processing) const
950 {
951 Cardinality c = 1;
952 std::vector< Type > instTypes;
953 std::vector< Type > paramTypes;
954 if( DatatypeType(t).isParametric() ){
955 paramTypes = DatatypeType(t).getDatatype().getParameters();
956 instTypes = DatatypeType(t).getParamTypes();
957 }
958 for(const_iterator i = begin(), i_end = end(); i != i_end; ++i) {
959 Type tc = SelectorType((*i).getSelector().getType()).getRangeType();
960 if( DatatypeType(t).isParametric() ){
961 tc = tc.substitute( paramTypes, instTypes );
962 }
963 if( tc.isDatatype() ){
964 const Datatype& dt = ((DatatypeType)tc).getDatatype();
965 c *= dt.computeCardinality( t, processing );
966 }else{
967 c *= tc.getCardinality();
968 }
969 }
970 return c;
971 }
972
973 bool DatatypeConstructor::computeWellFounded(
974 std::vector<Type>& processing) const
975 {
976 for(const_iterator i = begin(), i_end = end(); i != i_end; ++i) {
977 Type t = SelectorType((*i).getSelector().getType()).getRangeType();
978 if( t.isDatatype() ){
979 const Datatype& dt = ((DatatypeType)t).getDatatype();
980 if( !dt.computeWellFounded( processing ) ){
981 return false;
982 }
983 }
984 }
985 return true;
986 }
987
988 bool DatatypeConstructor::isFinite(Type t) const
989 {
990 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
991
992 // we're using some internals, so we have to set up this library context
993 ExprManagerScope ems(d_constructor);
994 TNode self = Node::fromExpr(d_constructor);
995 // is this already in the cache ?
996 if(self.getAttribute(DatatypeFiniteComputedAttr())) {
997 return self.getAttribute(DatatypeFiniteAttr());
998 }
999 std::vector< Type > instTypes;
1000 std::vector< Type > paramTypes;
1001 if( DatatypeType(t).isParametric() ){
1002 paramTypes = DatatypeType(t).getDatatype().getParameters();
1003 instTypes = DatatypeType(t).getParamTypes();
1004 }
1005 for(const_iterator i = begin(), i_end = end(); i != i_end; ++i) {
1006 Type tc = (*i).getRangeType();
1007 if( DatatypeType(t).isParametric() ){
1008 tc = tc.substitute( paramTypes, instTypes );
1009 }
1010 if(! tc.getCardinality().isFinite()) {
1011 self.setAttribute(DatatypeFiniteComputedAttr(), true);
1012 self.setAttribute(DatatypeFiniteAttr(), false);
1013 return false;
1014 }
1015 }
1016 self.setAttribute(DatatypeFiniteComputedAttr(), true);
1017 self.setAttribute(DatatypeFiniteAttr(), true);
1018 return true;
1019 }
1020
1021 bool DatatypeConstructor::isInterpretedFinite(Type t) const
1022 {
1023 PrettyCheckArgument(isResolved(), this, "this datatype constructor is not yet resolved");
1024 // we're using some internals, so we have to set up this library context
1025 ExprManagerScope ems(d_constructor);
1026 TNode self = Node::fromExpr(d_constructor);
1027 // is this already in the cache ?
1028 if(self.getAttribute(DatatypeUFiniteComputedAttr())) {
1029 return self.getAttribute(DatatypeUFiniteAttr());
1030 }
1031 std::vector< Type > instTypes;
1032 std::vector< Type > paramTypes;
1033 if( DatatypeType(t).isParametric() ){
1034 paramTypes = DatatypeType(t).getDatatype().getParameters();
1035 instTypes = DatatypeType(t).getParamTypes();
1036 }
1037 for(const_iterator i = begin(), i_end = end(); i != i_end; ++i) {
1038 Type tc = (*i).getRangeType();
1039 if( DatatypeType(t).isParametric() ){
1040 tc = tc.substitute( paramTypes, instTypes );
1041 }
1042 TypeNode tcn = TypeNode::fromType( tc );
1043 if(!tcn.isInterpretedFinite()) {
1044 self.setAttribute(DatatypeUFiniteComputedAttr(), true);
1045 self.setAttribute(DatatypeUFiniteAttr(), false);
1046 return false;
1047 }
1048 }
1049 self.setAttribute(DatatypeUFiniteComputedAttr(), true);
1050 self.setAttribute(DatatypeUFiniteAttr(), true);
1051 return true;
1052 }
1053
1054 Expr DatatypeConstructor::computeGroundTerm(Type t,
1055 std::vector<Type>& processing,
1056 std::map<Type, Expr>& gt) const
1057 {
1058 // we're using some internals, so we have to set up this library context
1059 ExprManagerScope ems(d_constructor);
1060
1061 std::vector<Expr> groundTerms;
1062 groundTerms.push_back(getConstructor());
1063
1064 // for each selector, get a ground term
1065 std::vector< Type > instTypes;
1066 std::vector< Type > paramTypes;
1067 if( DatatypeType(t).isParametric() ){
1068 paramTypes = DatatypeType(t).getDatatype().getParameters();
1069 instTypes = DatatypeType(t).getParamTypes();
1070 }
1071 for(const_iterator i = begin(), i_end = end(); i != i_end; ++i) {
1072 Type selType = SelectorType((*i).getSelector().getType()).getRangeType();
1073 if( DatatypeType(t).isParametric() ){
1074 selType = selType.substitute( paramTypes, instTypes );
1075 }
1076 Expr arg;
1077 if( selType.isDatatype() ){
1078 std::map< Type, Expr >::iterator itgt = gt.find( selType );
1079 if( itgt != gt.end() ){
1080 arg = itgt->second;
1081 }else{
1082 const Datatype & dt = DatatypeType(selType).getDatatype();
1083 arg = dt.computeGroundTerm( selType, processing );
1084 }
1085 }else{
1086 arg = selType.mkGroundTerm();
1087 }
1088 if( arg.isNull() ){
1089 Debug("datatypes") << "...unable to construct arg of " << (*i).getName() << std::endl;
1090 return Expr();
1091 }else{
1092 Debug("datatypes") << "...constructed arg " << arg.getType() << std::endl;
1093 groundTerms.push_back(arg);
1094 }
1095 }
1096
1097 Expr groundTerm = getConstructor().getExprManager()->mkExpr(kind::APPLY_CONSTRUCTOR, groundTerms);
1098 if( groundTerm.getType()!=t ){
1099 Assert( Datatype::datatypeOf( d_constructor ).isParametric() );
1100 //type is ambiguous, must apply type ascription
1101 Debug("datatypes-gt") << "ambiguous type for " << groundTerm << ", ascribe to " << t << std::endl;
1102 groundTerms[0] = getConstructor().getExprManager()->mkExpr(kind::APPLY_TYPE_ASCRIPTION,
1103 getConstructor().getExprManager()->mkConst(AscriptionType(getSpecializedConstructorType(t))),
1104 groundTerms[0]);
1105 groundTerm = getConstructor().getExprManager()->mkExpr(kind::APPLY_CONSTRUCTOR, groundTerms);
1106 }
1107 return groundTerm;
1108 }
1109
1110 void DatatypeConstructor::computeSharedSelectors( Type domainType ) const {
1111 if( d_shared_selectors[domainType].size()<getNumArgs() ){
1112 TypeNode ctype;
1113 if( DatatypeType(domainType).isParametric() ){
1114 ctype = TypeNode::fromType( getSpecializedConstructorType( domainType ) );
1115 }else{
1116 ctype = TypeNode::fromType( d_constructor.getType() );
1117 }
1118 Assert( ctype.isConstructor() );
1119 Assert( ctype.getNumChildren()-1==getNumArgs() );
1120 //compute the shared selectors
1121 const Datatype& dt = Datatype::datatypeOf(d_constructor);
1122 std::map< TypeNode, unsigned > counter;
1123 for( unsigned j=0; j<ctype.getNumChildren()-1; j++ ){
1124 TypeNode t = ctype[j];
1125 Expr ss = dt.getSharedSelector( domainType, t.toType(), counter[t] );
1126 d_shared_selectors[domainType].push_back( ss );
1127 Assert( d_shared_selector_index[domainType].find( ss )==d_shared_selector_index[domainType].end() );
1128 d_shared_selector_index[domainType][ss] = j;
1129 counter[t]++;
1130 }
1131 }
1132 }
1133
1134
1135 const DatatypeConstructorArg& DatatypeConstructor::operator[](size_t index) const {
1136 PrettyCheckArgument(index < getNumArgs(), index, "index out of bounds");
1137 return d_args[index];
1138 }
1139
1140 const DatatypeConstructorArg& DatatypeConstructor::operator[](std::string name) const {
1141 for(const_iterator i = begin(); i != end(); ++i) {
1142 if((*i).getName() == name) {
1143 return *i;
1144 }
1145 }
1146 IllegalArgument(name, "No such arg `%s' of constructor `%s'", name.c_str(), d_name.c_str());
1147 }
1148
1149 Expr DatatypeConstructor::getSelector(std::string name) const {
1150 return (*this)[name].getSelector();
1151 }
1152
1153 Type DatatypeConstructor::getArgType(unsigned index) const
1154 {
1155 PrettyCheckArgument(index < getNumArgs(), index, "index out of bounds");
1156 return static_cast<SelectorType>((*this)[index].getType()).getRangeType();
1157 }
1158
1159 bool DatatypeConstructor::involvesExternalType() const{
1160 for(const_iterator i = begin(); i != end(); ++i) {
1161 if(! SelectorType((*i).getSelector().getType()).getRangeType().isDatatype()) {
1162 return true;
1163 }
1164 }
1165 return false;
1166 }
1167
1168 bool DatatypeConstructor::involvesUninterpretedType() const{
1169 for(const_iterator i = begin(); i != end(); ++i) {
1170 if(SelectorType((*i).getSelector().getType()).getRangeType().isSort()) {
1171 return true;
1172 }
1173 }
1174 return false;
1175 }
1176
1177 DatatypeConstructorArg::DatatypeConstructorArg(std::string name, Expr selector) :
1178 d_name(name),
1179 d_selector(selector),
1180 d_resolved(false) {
1181 PrettyCheckArgument(name != "", name, "cannot construct a datatype constructor arg without a name");
1182 }
1183
1184 std::string DatatypeConstructorArg::getName() const
1185 {
1186 string name = d_name;
1187 const size_t nul = name.find('\0');
1188 if(nul != string::npos) {
1189 name.resize(nul);
1190 }
1191 return name;
1192 }
1193
1194 Expr DatatypeConstructorArg::getSelector() const {
1195 PrettyCheckArgument(isResolved(), this, "cannot get a selector for an unresolved datatype constructor");
1196 return d_selector;
1197 }
1198
1199 Expr DatatypeConstructor::getSelectorInternal( Type domainType, size_t index ) const {
1200 PrettyCheckArgument(isResolved(), this, "cannot get an internal selector for an unresolved datatype constructor");
1201 PrettyCheckArgument(index < getNumArgs(), index, "index out of bounds");
1202 if( options::dtSharedSelectors() ){
1203 computeSharedSelectors( domainType );
1204 Assert( d_shared_selectors[domainType].size()==getNumArgs() );
1205 return d_shared_selectors[domainType][index];
1206 }else{
1207 return d_args[index].getSelector();
1208 }
1209 }
1210
1211 int DatatypeConstructor::getSelectorIndexInternal( Expr sel ) const {
1212 PrettyCheckArgument(isResolved(), this, "cannot get an internal selector index for an unresolved datatype constructor");
1213 if( options::dtSharedSelectors() ){
1214 Assert( sel.getType().isSelector() );
1215 Type domainType = ((SelectorType)sel.getType()).getDomain();
1216 computeSharedSelectors( domainType );
1217 std::map< Expr, unsigned >::iterator its = d_shared_selector_index[domainType].find( sel );
1218 if( its!=d_shared_selector_index[domainType].end() ){
1219 return (int)its->second;
1220 }
1221 }else{
1222 unsigned sindex = Datatype::indexOf(sel);
1223 if( getNumArgs() > sindex && d_args[sindex].getSelector() == sel ){
1224 return (int)sindex;
1225 }
1226 }
1227 return -1;
1228 }
1229
1230 Expr DatatypeConstructorArg::getConstructor() const {
1231 PrettyCheckArgument(isResolved(), this,
1232 "cannot get a associated constructor for argument of an unresolved datatype constructor");
1233 return d_constructor;
1234 }
1235
1236 SelectorType DatatypeConstructorArg::getType() const {
1237 return getSelector().getType();
1238 }
1239
1240 Type DatatypeConstructorArg::getRangeType() const {
1241 return getType().getRangeType();
1242 }
1243
1244 bool DatatypeConstructorArg::isUnresolvedSelf() const
1245 {
1246 return d_selector.isNull() && d_name.size() == d_name.find('\0') + 1;
1247 }
1248
1249 static const int s_printDatatypeNamesOnly = std::ios_base::xalloc();
1250
1251 std::string DatatypeConstructorArg::getTypeName() const {
1252 Type t;
1253 if(isResolved()) {
1254 t = SelectorType(d_selector.getType()).getRangeType();
1255 } else {
1256 if(d_selector.isNull()) {
1257 string typeName = d_name.substr(d_name.find('\0') + 1);
1258 return (typeName == "") ? "[self]" : typeName;
1259 } else {
1260 t = d_selector.getType();
1261 }
1262 }
1263
1264 // Unfortunately, in the case of complex selector types, we can
1265 // enter nontrivial recursion here. Make sure that doesn't happen.
1266 stringstream ss;
1267 ss << language::SetLanguage(language::output::LANG_CVC4);
1268 ss.iword(s_printDatatypeNamesOnly) = 1;
1269 t.toStream(ss);
1270 return ss.str();
1271 }
1272
1273 std::ostream& operator<<(std::ostream& os, const Datatype& dt) {
1274 // These datatype things are recursive! Be very careful not to
1275 // print an infinite chain of them.
1276 long& printNameOnly = os.iword(s_printDatatypeNamesOnly);
1277 Debug("datatypes-output") << "printNameOnly is " << printNameOnly << std::endl;
1278 if(printNameOnly) {
1279 return os << dt.getName();
1280 }
1281
1282 class Scope {
1283 long& d_ref;
1284 long d_oldValue;
1285 public:
1286 Scope(long& ref, long value) : d_ref(ref), d_oldValue(ref) { d_ref = value; }
1287 ~Scope() { d_ref = d_oldValue; }
1288 } scope(printNameOnly, 1);
1289 // when scope is destructed, the value pops back
1290
1291 Debug("datatypes-output") << "printNameOnly is now " << printNameOnly << std::endl;
1292
1293 // can only output datatypes in the CVC4 native language
1294 language::SetLanguage::Scope ls(os, language::output::LANG_CVC4);
1295
1296 os << "DATATYPE " << dt.getName();
1297 if(dt.isParametric()) {
1298 os << '[';
1299 for(size_t i = 0; i < dt.getNumParameters(); ++i) {
1300 if(i > 0) {
1301 os << ',';
1302 }
1303 os << dt.getParameter(i);
1304 }
1305 os << ']';
1306 }
1307 os << " =" << endl;
1308 Datatype::const_iterator i = dt.begin(), i_end = dt.end();
1309 if(i != i_end) {
1310 os << " ";
1311 do {
1312 os << *i << endl;
1313 if(++i != i_end) {
1314 os << "| ";
1315 }
1316 } while(i != i_end);
1317 }
1318 os << "END;" << endl;
1319
1320 return os;
1321 }
1322
1323 std::ostream& operator<<(std::ostream& os, const DatatypeConstructor& ctor) {
1324 // can only output datatypes in the CVC4 native language
1325 language::SetLanguage::Scope ls(os, language::output::LANG_CVC4);
1326
1327 os << ctor.getName();
1328
1329 DatatypeConstructor::const_iterator i = ctor.begin(), i_end = ctor.end();
1330 if(i != i_end) {
1331 os << "(";
1332 do {
1333 os << *i;
1334 if(++i != i_end) {
1335 os << ", ";
1336 }
1337 } while(i != i_end);
1338 os << ")";
1339 }
1340
1341 return os;
1342 }
1343
1344 std::ostream& operator<<(std::ostream& os, const DatatypeConstructorArg& arg) {
1345 // can only output datatypes in the CVC4 native language
1346 language::SetLanguage::Scope ls(os, language::output::LANG_CVC4);
1347
1348 os << arg.getName() << ": " << arg.getTypeName();
1349
1350 return os;
1351 }
1352
1353 DatatypeIndexConstant::DatatypeIndexConstant(unsigned index) : d_index(index) {}
1354 std::ostream& operator<<(std::ostream& out, const DatatypeIndexConstant& dic) {
1355 return out << "index_" << dic.getIndex();
1356 }
1357
1358 }/* CVC4 namespace */