GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/expr/dtype.cpp Lines: 415 473 87.7 %
Date: 2021-05-22 Branches: 718 1996 36.0 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Morgan Deters, Tim King
4
 *
5
 * This file is part of the cvc5 project.
6
 *
7
 * Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
8
 * in the top-level source directory and their institutional affiliations.
9
 * All rights reserved.  See the file COPYING in the top-level source
10
 * directory for licensing information.
11
 * ****************************************************************************
12
 *
13
 * A class representing a datatype definition.
14
 */
15
#include "expr/dtype.h"
16
17
#include <sstream>
18
19
#include "expr/dtype_cons.h"
20
#include "expr/node_algorithm.h"
21
#include "expr/skolem_manager.h"
22
#include "expr/type_matcher.h"
23
24
using namespace cvc5::kind;
25
26
namespace cvc5 {
27
28
3880
DType::DType(std::string name, bool isCo)
29
    : d_name(name),
30
      d_params(),
31
      d_isCo(isCo),
32
      d_isTuple(false),
33
      d_isRecord(false),
34
      d_constructors(),
35
      d_resolved(false),
36
      d_self(),
37
      d_involvesExt(false),
38
      d_involvesUt(false),
39
      d_sygusAllowConst(false),
40
      d_sygusAllowAll(false),
41
7760
      d_card(CardinalityUnknown()),
42
      d_wellFounded(0),
43
11640
      d_nestedRecursion(0)
44
{
45
3880
}
46
47
1282
DType::DType(std::string name, const std::vector<TypeNode>& params, bool isCo)
48
    : d_name(name),
49
      d_params(params),
50
      d_isCo(isCo),
51
      d_isTuple(false),
52
      d_isRecord(false),
53
      d_constructors(),
54
      d_resolved(false),
55
      d_self(),
56
      d_involvesExt(false),
57
      d_involvesUt(false),
58
      d_sygusAllowConst(false),
59
      d_sygusAllowAll(false),
60
2564
      d_card(CardinalityUnknown()),
61
      d_wellFounded(0),
62
3846
      d_nestedRecursion(0)
63
{
64
1282
}
65
66
24601
DType::~DType() {}
67
68
1273603
std::string DType::getName() const { return d_name; }
69
9888515
size_t DType::getNumConstructors() const { return d_constructors.size(); }
70
71
1598487
bool DType::isParametric() const { return d_params.size() > 0; }
72
8236
size_t DType::getNumParameters() const { return d_params.size(); }
73
3204
TypeNode DType::getParameter(size_t i) const
74
{
75
3204
  Assert(isParametric());
76
3204
  Assert(i < d_params.size());
77
3204
  return d_params[i];
78
}
79
80
818
std::vector<TypeNode> DType::getParameters() const
81
{
82
818
  Assert(isParametric());
83
818
  return d_params;
84
}
85
86
812209
bool DType::isCodatatype() const { return d_isCo; }
87
88
2382953
bool DType::isSygus() const { return !d_sygusType.isNull(); }
89
90
98314
bool DType::isTuple() const { return d_isTuple; }
91
92
417
bool DType::isRecord() const { return d_isRecord; }
93
94
921086
bool DType::isResolved() const { return d_resolved; }
95
96
8034
const DType& DType::datatypeOf(Node item)
97
{
98
16068
  TypeNode t = item.getType();
99
8034
  switch (t.getKind())
100
  {
101
8034
    case CONSTRUCTOR_TYPE: return t[t.getNumChildren() - 1].getDType();
102
    case SELECTOR_TYPE:
103
    case TESTER_TYPE:
104
    case UPDATER_TYPE: return t[0].getDType();
105
    default:
106
      Unhandled() << "arg must be a datatype constructor, selector, or tester";
107
  }
108
}
109
110
2825640
size_t DType::indexOf(Node item)
111
{
112
2825640
  Assert(item.getType().isConstructor() || item.getType().isTester()
113
         || item.getType().isSelector() || item.getType().isUpdater());
114
2825640
  return indexOfInternal(item);
115
}
116
117
2825640
size_t DType::indexOfInternal(Node item)
118
{
119
2825640
  if (item.getKind() == APPLY_TYPE_ASCRIPTION)
120
  {
121
5585
    return indexOf(item[0]);
122
  }
123
2820055
  Assert(item.hasAttribute(DTypeIndexAttr()));
124
2820055
  return item.getAttribute(DTypeIndexAttr());
125
}
126
127
3895
size_t DType::cindexOf(Node item)
128
{
129
3895
  Assert(item.getType().isSelector() || item.getType().isUpdater());
130
3895
  return cindexOfInternal(item);
131
}
132
3895
size_t DType::cindexOfInternal(Node item)
133
{
134
3895
  if (item.getKind() == APPLY_TYPE_ASCRIPTION)
135
  {
136
    return cindexOf(item[0]);
137
  }
138
3895
  Assert(item.hasAttribute(DTypeConsIndexAttr()));
139
3895
  return item.getAttribute(DTypeConsIndexAttr());
140
}
141
142
5152
bool DType::resolve(const std::map<std::string, TypeNode>& resolutions,
143
                    const std::vector<TypeNode>& placeholders,
144
                    const std::vector<TypeNode>& replacements,
145
                    const std::vector<TypeNode>& paramTypes,
146
                    const std::vector<TypeNode>& paramReplacements)
147
{
148
5152
  Trace("datatypes-init") << "DType::resolve: " << std::endl;
149
5152
  Assert(!d_resolved);
150
5152
  Assert(resolutions.find(d_name) != resolutions.end());
151
5152
  Assert(placeholders.size() == replacements.size());
152
5152
  Assert(paramTypes.size() == paramReplacements.size());
153
5152
  Assert(getNumConstructors() > 0);
154
10304
  TypeNode self = (*resolutions.find(d_name)).second;
155
5152
  Assert(&self.getDType() == this);
156
5152
  d_resolved = true;
157
5152
  size_t index = 0;
158
19962
  for (std::shared_ptr<DTypeConstructor> ctor : d_constructors)
159
  {
160
14810
    Trace("datatypes-init") << "DType::resolve ctor " << std::endl;
161
14810
    if (!ctor->resolve(self,
162
                       resolutions,
163
                       placeholders,
164
                       replacements,
165
                       paramTypes,
166
                       paramReplacements,
167
                       index))
168
    {
169
      return false;
170
    }
171
14810
    ctor->d_constructor.setAttribute(DTypeIndexAttr(), index);
172
14810
    ctor->d_tester.setAttribute(DTypeIndexAttr(), index++);
173
14810
    Assert(ctor->isResolved());
174
14810
    Trace("datatypes-init") << "DType::resolve ctor finished" << std::endl;
175
  }
176
5152
  d_self = self;
177
178
5152
  d_involvesExt = false;
179
5152
  d_involvesUt = false;
180
19962
  for (const std::shared_ptr<DTypeConstructor>& ctor : d_constructors)
181
  {
182
14810
    if (ctor->involvesExternalType())
183
    {
184
1446
      d_involvesExt = true;
185
    }
186
14810
    if (ctor->involvesUninterpretedType())
187
    {
188
7572
      d_involvesUt = true;
189
    }
190
  }
191
192
5152
  if (isSygus())
193
  {
194
    // all datatype constructors should be sygus and have sygus operators whose
195
    // free variables are subsets of sygus bound var list.
196
3482
    std::unordered_set<Node> svs;
197
4626
    for (const Node& sv : d_sygusBvl)
198
    {
199
2885
      svs.insert(sv);
200
    }
201
11750
    for (size_t i = 0, ncons = d_constructors.size(); i < ncons; i++)
202
    {
203
20018
      Node sop = d_constructors[i]->getSygusOp();
204
10009
      Assert(!sop.isNull())
205
          << "Sygus datatype contains a non-sygus constructor";
206
20018
      std::unordered_set<Node> fvs;
207
10009
      expr::getFreeVariables(sop, fvs);
208
11213
      for (const Node& v : fvs)
209
      {
210
1204
        if (svs.find(v) == svs.end())
211
        {
212
          // return false, indicating we should abort, since this datatype is
213
          // not well formed.
214
          return false;
215
        }
216
      }
217
    }
218
  }
219
5152
  Trace("datatypes-init") << "DType::resolve: finished" << std::endl;
220
5152
  return true;
221
}
222
223
14810
void DType::addConstructor(std::shared_ptr<DTypeConstructor> c)
224
{
225
14810
  Assert(!d_resolved);
226
14810
  d_constructors.push_back(c);
227
14810
}
228
229
9980
void DType::addSygusConstructor(Node op,
230
                                const std::string& cname,
231
                                const std::vector<TypeNode>& cargs,
232
                                int weight)
233
{
234
  // avoid name clashes
235
19960
  std::stringstream ss;
236
9980
  ss << getName() << "_" << getNumConstructors() << "_" << cname;
237
19960
  std::string name = ss.str();
238
9980
  unsigned cweight = weight >= 0 ? weight : (cargs.empty() ? 0 : 1);
239
  std::shared_ptr<DTypeConstructor> c =
240
19960
      std::make_shared<DTypeConstructor>(name, cweight);
241
9980
  c->setSygus(op);
242
21345
  for (size_t j = 0, nargs = cargs.size(); j < nargs; j++)
243
  {
244
22730
    std::stringstream sname;
245
11365
    sname << name << "_" << j;
246
11365
    c->addArg(sname.str(), cargs[j]);
247
  }
248
9980
  addConstructor(c);
249
9980
}
250
251
1741
void DType::setSygus(TypeNode st, Node bvl, bool allowConst, bool allowAll)
252
{
253
1741
  Assert(!d_resolved);
254
  // We can be in a case where the only rule specified was
255
  // (Constant T), in which case we have not yet added a constructor. We
256
  // ensure an arbitrary constant is added in this case. We additionally
257
  // add a constant if the grammar has only non-nullary constructors, since this
258
  // ensures the datatype is well-founded (see 3423).
259
  // Notice we only want to do this for sygus datatypes that are user-provided.
260
  // At the moment, the condition !allow_all implies the grammar is
261
  // user-provided and hence may require a default constant.
262
  // TODO (https://github.com/CVC4/cvc4-projects/issues/38):
263
  // In an API for SyGuS, it probably makes more sense for the user to
264
  // explicitly add the "any constant" constructor with a call instead of
265
  // passing a flag. This would make the block of code unnecessary.
266
1741
  if (allowConst && !allowAll)
267
  {
268
    // if I don't already have a constant (0-ary constructor)
269
35
    bool hasConstant = false;
270
59
    for (size_t i = 0, ncons = getNumConstructors(); i < ncons; i++)
271
    {
272
30
      if ((*this)[i].getNumArgs() == 0)
273
      {
274
6
        hasConstant = true;
275
6
        break;
276
      }
277
    }
278
35
    if (!hasConstant)
279
    {
280
      // add an arbitrary one
281
58
      Node op = st.mkGroundTerm();
282
      // use same naming convention as SygusDatatype
283
58
      std::stringstream ss;
284
29
      ss << getName() << "_" << getNumConstructors() << "_" << op;
285
      // it has zero weight
286
      std::shared_ptr<DTypeConstructor> c =
287
58
          std::make_shared<DTypeConstructor>(ss.str(), 0);
288
29
      c->setSygus(op);
289
29
      addConstructor(c);
290
    }
291
  }
292
293
1741
  d_sygusType = st;
294
1741
  d_sygusBvl = bvl;
295
1741
  d_sygusAllowConst = allowConst || allowAll;
296
1741
  d_sygusAllowAll = allowAll;
297
1741
}
298
299
1953
void DType::setTuple()
300
{
301
1953
  Assert(!d_resolved);
302
1953
  d_isTuple = true;
303
1953
}
304
305
74
void DType::setRecord()
306
{
307
74
  Assert(!d_resolved);
308
74
  d_isRecord = true;
309
74
}
310
311
3144
Cardinality DType::getCardinality(TypeNode t) const
312
{
313
3144
  Trace("datatypes-init") << "DType::getCardinality " << std::endl;
314
3144
  Assert(isResolved());
315
3144
  Assert(t.isDatatype() && t.getDType().getTypeNode() == d_self);
316
6288
  std::vector<TypeNode> processing;
317
3144
  computeCardinality(t, processing);
318
6288
  return d_card;
319
}
320
321
22
Cardinality DType::getCardinality() const
322
{
323
22
  Assert(!isParametric());
324
22
  return getCardinality(d_self);
325
}
326
327
7614
Cardinality DType::computeCardinality(TypeNode t,
328
                                      std::vector<TypeNode>& processing) const
329
{
330
7614
  Trace("datatypes-init") << "DType::computeCardinality " << std::endl;
331
7614
  Assert(isResolved());
332
22842
  if (std::find(processing.begin(), processing.end(), d_self)
333
22842
      != processing.end())
334
  {
335
263
    d_card = Cardinality::INTEGERS;
336
263
    return d_card;
337
  }
338
7351
  processing.push_back(d_self);
339
14702
  Cardinality c = 0;
340
21288
  for (std::shared_ptr<DTypeConstructor> ctor : d_constructors)
341
  {
342
13937
    c += ctor->computeCardinality(t, processing);
343
  }
344
7351
  d_card = c;
345
7351
  processing.pop_back();
346
7351
  return d_card;
347
}
348
349
177842
bool DType::isRecursiveSingleton(TypeNode t) const
350
{
351
177842
  Trace("datatypes-init") << "DType::isRecursiveSingleton " << std::endl;
352
177842
  Assert(isResolved());
353
177842
  Assert(t.isDatatype() && t.getDType().getTypeNode() == d_self);
354
177842
  if (d_cardRecSingleton.find(t) != d_cardRecSingleton.end())
355
  {
356
176442
    return d_cardRecSingleton[t] == 1;
357
  }
358
1400
  if (isCodatatype())
359
  {
360
17
    Assert(d_cardUAssume[t].empty());
361
34
    std::vector<TypeNode> processing;
362
17
    if (computeCardinalityRecSingleton(t, processing, d_cardUAssume[t]))
363
    {
364
3
      d_cardRecSingleton[t] = 1;
365
3
      if (Trace.isOn("dt-card"))
366
      {
367
        Trace("dt-card") << "DType " << getName()
368
                         << " is recursive singleton, dependent upon "
369
                         << d_cardUAssume[t].size()
370
                         << " uninterpreted sorts: " << std::endl;
371
        for (size_t i = 0; i < d_cardUAssume[t].size(); i++)
372
        {
373
          Trace("dt-card") << "  " << d_cardUAssume[t][i] << std::endl;
374
        }
375
        Trace("dt-card") << std::endl;
376
      }
377
    }
378
    else
379
    {
380
14
      d_cardRecSingleton[t] = -1;
381
    }
382
  }
383
  else
384
  {
385
1383
    d_cardRecSingleton[t] = -1;
386
  }
387
1400
  return d_cardRecSingleton[t] == 1;
388
}
389
390
bool DType::isRecursiveSingleton() const
391
{
392
  Assert(!isParametric());
393
  return isRecursiveSingleton(d_self);
394
}
395
396
4
unsigned DType::getNumRecursiveSingletonArgTypes(TypeNode t) const
397
{
398
4
  Assert(d_cardRecSingleton.find(t) != d_cardRecSingleton.end());
399
4
  Assert(isRecursiveSingleton(t));
400
4
  return d_cardUAssume[t].size();
401
}
402
403
unsigned DType::getNumRecursiveSingletonArgTypes() const
404
{
405
  Assert(!isParametric());
406
  return getNumRecursiveSingletonArgTypes(d_self);
407
}
408
409
TypeNode DType::getRecursiveSingletonArgType(TypeNode t, size_t i) const
410
{
411
  Assert(d_cardRecSingleton.find(t) != d_cardRecSingleton.end());
412
  Assert(isRecursiveSingleton(t));
413
  return d_cardUAssume[t][i];
414
}
415
416
TypeNode DType::getRecursiveSingletonArgType(size_t i) const
417
{
418
  Assert(!isParametric());
419
  return getRecursiveSingletonArgType(d_self, i);
420
}
421
422
22
bool DType::computeCardinalityRecSingleton(
423
    TypeNode t,
424
    std::vector<TypeNode>& processing,
425
    std::vector<TypeNode>& u_assume) const
426
{
427
44
  Trace("datatypes-init") << "DType::computeCardinalityRecSingleton "
428
22
                          << std::endl;
429
66
  if (std::find(processing.begin(), processing.end(), d_self)
430
66
      != processing.end())
431
  {
432
3
    return true;
433
  }
434
19
  if (d_cardRecSingleton[t] == 0)
435
  {
436
    // if not yet computed
437
19
    if (d_constructors.size() != 1)
438
    {
439
14
      return false;
440
    }
441
5
    bool success = false;
442
5
    processing.push_back(d_self);
443
8
    for (size_t i = 0, nargs = d_constructors[0]->getNumArgs(); i < nargs; i++)
444
    {
445
8
      TypeNode tc = d_constructors[0]->getArgType(i);
446
      // if it is an uninterpreted sort, then we depend on it having cardinality
447
      // one
448
5
      if (tc.isSort())
449
      {
450
        if (std::find(u_assume.begin(), u_assume.end(), tc) == u_assume.end())
451
        {
452
          u_assume.push_back(tc);
453
        }
454
        // if it is a datatype, recurse
455
      }
456
5
      else if (tc.isDatatype())
457
      {
458
5
        const DType& dt = tc.getDType();
459
5
        if (!dt.computeCardinalityRecSingleton(t, processing, u_assume))
460
        {
461
2
          return false;
462
        }
463
        else
464
        {
465
3
          success = true;
466
        }
467
        // if it is a builtin type, it must have cardinality one
468
      }
469
      else if (!tc.getCardinality().isOne())
470
      {
471
        return false;
472
      }
473
    }
474
3
    processing.pop_back();
475
3
    return success;
476
  }
477
  else if (d_cardRecSingleton[t] == -1)
478
  {
479
    return false;
480
  }
481
  for (size_t i = 0, csize = d_cardUAssume[t].size(); i < csize; i++)
482
  {
483
    if (std::find(u_assume.begin(), u_assume.end(), d_cardUAssume[t][i])
484
        == u_assume.end())
485
    {
486
      u_assume.push_back(d_cardUAssume[t][i]);
487
    }
488
  }
489
  return true;
490
}
491
492
137908
CardinalityClass DType::getCardinalityClass(TypeNode t) const
493
{
494
137908
  Trace("datatypes-init") << "DType::isFinite " << std::endl;
495
137908
  Assert(isResolved());
496
137908
  Assert(t.isDatatype() && t.getDType().getTypeNode() == d_self);
497
498
  // is this already in the cache ?
499
137908
  std::map<TypeNode, CardinalityClass>::const_iterator it = d_cardClass.find(t);
500
137908
  if (it != d_cardClass.end())
501
  {
502
135509
    return it->second;
503
  }
504
  // it is the max cardinality class of a constructor, with base case ONE
505
  // if we have one constructor and FINITE otherwise.
506
2399
  CardinalityClass c = d_constructors.size() == 1 ? CardinalityClass::ONE
507
2399
                                                  : CardinalityClass::FINITE;
508
11276
  for (std::shared_ptr<DTypeConstructor> ctor : d_constructors)
509
  {
510
8877
    CardinalityClass cc = ctor->getCardinalityClass(t);
511
8877
    c = maxCardinalityClass(c, cc);
512
  }
513
2399
  d_cardClass[t] = c;
514
2399
  return c;
515
}
516
14
CardinalityClass DType::getCardinalityClass() const
517
{
518
14
  Assert(isResolved() && !isParametric());
519
14
  return getCardinalityClass(d_self);
520
}
521
522
22
bool DType::isFinite(TypeNode t, bool fmfEnabled) const
523
{
524
22
  return isCardinalityClassFinite(getCardinalityClass(t), fmfEnabled);
525
}
526
527
22
bool DType::isFinite(bool fmfEnabled) const
528
{
529
22
  return isFinite(d_self, fmfEnabled);
530
}
531
532
68909
bool DType::isWellFounded() const
533
{
534
68909
  Assert(isResolved());
535
68909
  if (d_wellFounded != 0)
536
  {
537
    // already computed
538
65892
    return d_wellFounded == 1;
539
  }
540
3017
  Trace("datatypes-init") << "DType::isWellFounded " << getName() << std::endl;
541
6034
  std::vector<TypeNode> processing;
542
3017
  if (!computeWellFounded(processing))
543
  {
544
    // not well-founded since no ground term can be constructed
545
4
    Trace("datatypes-init") << "DType::isWellFounded: false for " << getName()
546
2
                            << " due to no ground terms." << std::endl;
547
2
    d_wellFounded = -1;
548
2
    return false;
549
  }
550
6030
  Trace("datatypes-init") << "DType::isWellFounded: true for " << getName()
551
3015
                          << std::endl;
552
3015
  d_wellFounded = 1;
553
3015
  return true;
554
}
555
556
6058
bool DType::computeWellFounded(std::vector<TypeNode>& processing) const
557
{
558
6058
  Trace("datatypes-init") << "DType::computeWellFounded " << std::endl;
559
6058
  Assert(isResolved());
560
18174
  if (std::find(processing.begin(), processing.end(), d_self)
561
18174
      != processing.end())
562
  {
563
1058
    return d_isCo;
564
  }
565
5000
  processing.push_back(d_self);
566
6104
  for (std::shared_ptr<DTypeConstructor> ctor : d_constructors)
567
  {
568
6038
    if (ctor->computeWellFounded(processing))
569
    {
570
4934
      processing.pop_back();
571
4934
      return true;
572
    }
573
    else
574
    {
575
2208
      Trace("dt-wf") << "Constructor " << ctor->getName()
576
1104
                     << " is not well-founded." << std::endl;
577
    }
578
  }
579
66
  processing.pop_back();
580
132
  Trace("dt-wf") << "DType " << getName() << " is not well-founded."
581
66
                 << std::endl;
582
66
  return false;
583
}
584
585
88989
Node DType::mkGroundTerm(TypeNode t) const
586
{
587
88989
  Trace("datatypes-init") << "DType::mkGroundTerm of type " << t << std::endl;
588
88989
  Assert(isResolved());
589
88989
  return mkGroundTermInternal(t, false);
590
}
591
592
95951
Node DType::mkGroundValue(TypeNode t) const
593
{
594
95951
  Assert(isResolved());
595
95951
  Trace("datatypes-init") << "DType::mkGroundValue of type " << t << std::endl;
596
95951
  Node v = mkGroundTermInternal(t, true);
597
95951
  return v;
598
}
599
600
184940
Node DType::mkGroundTermInternal(TypeNode t, bool isValue) const
601
{
602
369880
  Trace("datatypes-init") << "DType::mkGroundTerm of type " << t
603
184940
                          << ", isValue = " << isValue << std::endl;
604
  // is this already in the cache ?
605
184940
  std::map<TypeNode, Node>& cache = isValue ? d_groundValue : d_groundTerm;
606
184940
  std::map<TypeNode, Node>::iterator it = cache.find(t);
607
184940
  if (it != cache.end())
608
  {
609
367408
    Trace("datatypes-init")
610
183704
        << "\nin cache: " << d_self << " => " << it->second << std::endl;
611
183704
    return it->second;
612
  }
613
2472
  std::vector<TypeNode> processing;
614
2472
  Node groundTerm = computeGroundTerm(t, processing, isValue);
615
1236
  if (!groundTerm.isNull())
616
  {
617
    // we found a ground-term-constructing constructor!
618
1226
    cache[t] = groundTerm;
619
2452
    Trace("datatypes-init")
620
1226
        << "constructed: " << getName() << " => " << groundTerm << std::endl;
621
  }
622
  // if ground term is null, we are not well-founded
623
2472
  Trace("datatypes-init") << "DType::mkGroundTerm for " << t
624
1236
                          << ", isValue=" << isValue << " returns "
625
1236
                          << groundTerm << std::endl;
626
1236
  return groundTerm;
627
}
628
629
11680
void DType::getAlienSubfieldTypes(std::unordered_set<TypeNode>& types,
630
                                  std::map<TypeNode, bool>& processed,
631
                                  bool isAlienPos) const
632
{
633
11680
  std::map<TypeNode, bool>::iterator it = processed.find(d_self);
634
11680
  if (it != processed.end())
635
  {
636
8698
    if (it->second || (!isAlienPos && !it->second))
637
    {
638
      // already processed as an alien subfield type, or already processed
639
      // as a non-alien subfield type and isAlienPos is false.
640
8680
      return;
641
    }
642
  }
643
3000
  processed[d_self] = isAlienPos;
644
13702
  for (std::shared_ptr<DTypeConstructor> ctor : d_constructors)
645
  {
646
22478
    for (unsigned j = 0, nargs = ctor->getNumArgs(); j < nargs; ++j)
647
    {
648
23513
      TypeNode tn = ctor->getArgType(j);
649
11776
      if (tn.isDatatype())
650
      {
651
        // special case for datatypes, we must recurse to collect subfield types
652
9800
        if (!isAlienPos)
653
        {
654
          // since we aren't adding it to types below, we add its alien
655
          // subfield types here.
656
9770
          const DType& dt = tn.getDType();
657
9770
          dt.getAlienSubfieldTypes(types, processed, false);
658
        }
659
9800
        if (tn.isParametricDatatype() && !isAlienPos)
660
        {
661
          // (instantiated) parametric datatypes have an AST structure:
662
          //  (PARAMETRIC_DATATYPE D T1 ... Tn)
663
          // where D is the uninstantiated datatype type.  We should not view D
664
          // as an alien subfield type of tn. Thus, we need a special case here
665
          // which ignores the first child, when isAlienPos is false.
666
80
          for (unsigned i = 1, nchild = tn.getNumChildren(); i < nchild; i++)
667
          {
668
41
            expr::getComponentTypes(tn[i], types);
669
          }
670
39
          continue;
671
        }
672
      }
673
      // we are in a case where tn is not a datatype, we add all (alien)
674
      // component types to types below.
675
11737
      bool hasTn = types.find(tn) != types.end();
676
23474
      Trace("datatypes-init")
677
11737
          << "Collect subfield types " << tn << ", hasTn=" << hasTn
678
11737
          << ", isAlienPos=" << isAlienPos << std::endl;
679
11737
      expr::getComponentTypes(tn, types);
680
11737
      if (!isAlienPos && !hasTn)
681
      {
682
        // the top-level type is added by getComponentTypes, so remove it if it
683
        // was not already listed in types
684
11662
        Assert(types.find(tn) != types.end());
685
11662
        types.erase(tn);
686
      }
687
    }
688
  }
689
  // Now, go back and add all alien subfield types from datatypes if
690
  // not done so already. This is because getComponentTypes does not
691
  // recurse into subfield types of datatypes.
692
3294
  for (const TypeNode& sstn : types)
693
  {
694
294
    if (sstn.isDatatype())
695
    {
696
168
      const DType& dt = sstn.getDType();
697
168
      dt.getAlienSubfieldTypes(types, processed, true);
698
    }
699
  }
700
}
701
702
67422
bool DType::hasNestedRecursion() const
703
{
704
67422
  if (d_nestedRecursion != 0)
705
  {
706
65680
    return d_nestedRecursion == 1;
707
  }
708
3484
  Trace("datatypes-init") << "Compute simply recursive for " << getName()
709
1742
                          << std::endl;
710
  // get the alien subfield types of this datatype
711
3484
  std::unordered_set<TypeNode> types;
712
3484
  std::map<TypeNode, bool> processed;
713
1742
  getAlienSubfieldTypes(types, processed, false);
714
1742
  if (Trace.isOn("datatypes-init"))
715
  {
716
    Trace("datatypes-init") << "Alien subfield types: " << std::endl;
717
    for (const TypeNode& t : types)
718
    {
719
      Trace("datatypes-init") << "- " << t << std::endl;
720
    }
721
  }
722
  // does types contain self?
723
1742
  if (types.find(d_self) != types.end())
724
  {
725
26
    Trace("datatypes-init")
726
26
        << "DType::hasNestedRecursion: true for " << getName()
727
13
        << " due to alien subfield type" << std::endl;
728
    // has nested recursion since it has itself as an alien subfield type.
729
13
    d_nestedRecursion = 1;
730
13
    return true;
731
  }
732
  // If it is parametric, this type may match with an alien subfield type (e.g.
733
  // we may have a field (T Int) for parametric datatype (T x) where x
734
  // is a type parameter). Thus, we check whether the self type matches any
735
  // alien subfield type using the TypeMatcher utility.
736
1729
  if (isParametric())
737
  {
738
63
    for (const TypeNode& t : types)
739
    {
740
62
      TypeMatcher m(d_self);
741
31
      Trace("datatypes-init") << "  " << t << std::endl;
742
31
      if (m.doMatching(d_self, t))
743
      {
744
        Trace("datatypes-init")
745
            << "DType::hasNestedRecursion: true for " << getName()
746
            << " due to parametric strict component type, " << d_self
747
            << " matching " << t << std::endl;
748
        d_nestedRecursion = 1;
749
        return true;
750
      }
751
    }
752
  }
753
3458
  Trace("datatypes-init") << "DType::hasNestedRecursion: false for "
754
1729
                          << getName() << std::endl;
755
1729
  d_nestedRecursion = -1;
756
1729
  return false;
757
}
758
759
2805
Node getSubtermWithType(Node e, TypeNode t, bool isTop)
760
{
761
2805
  if (!isTop && e.getType() == t)
762
  {
763
2
    return e;
764
  }
765
3876
  for (const Node& ei : e)
766
  {
767
2149
    Node se = getSubtermWithType(ei, t, false);
768
1076
    if (!se.isNull())
769
    {
770
3
      return se;
771
    }
772
  }
773
2800
  return Node();
774
}
775
776
1831
Node DType::computeGroundTerm(TypeNode t,
777
                              std::vector<TypeNode>& processing,
778
                              bool isValue) const
779
{
780
1831
  if (std::find(processing.begin(), processing.end(), t) != processing.end())
781
  {
782
180
    Trace("datatypes-init")
783
90
        << "...already processing " << t << " " << d_self << std::endl;
784
90
    return Node();
785
  }
786
1741
  processing.push_back(t);
787
1741
  std::map<TypeNode, Node>& gtCache = isValue ? d_groundValue : d_groundTerm;
788
2136
  for (unsigned r = 0; r < 2; r++)
789
  {
790
2216
    for (std::shared_ptr<DTypeConstructor> ctor : d_constructors)
791
    {
792
      // do nullary constructors first
793
3049
      if ((ctor->getNumArgs() == 0) != (r == 0))
794
      {
795
1228
        continue;
796
      }
797
3642
      Trace("datatypes-init") << "Try constructing for " << ctor->getName()
798
3642
                              << ", processing = " << processing.size()
799
1821
                              << ", isValue=" << isValue << std::endl;
800
1913
      Node e = ctor->computeGroundTerm(t, processing, gtCache, isValue);
801
1821
      if (!e.isNull())
802
      {
803
        // must check subterms for the same type to avoid infinite loops in
804
        // type enumeration
805
3458
        Node se = getSubtermWithType(e, t, true);
806
1729
        if (!se.isNull())
807
        {
808
2
          Trace("datatypes-init") << "Take subterm " << se << std::endl;
809
2
          e = se;
810
        }
811
1729
        processing.pop_back();
812
1729
        return e;
813
      }
814
      else
815
      {
816
92
        Trace("datatypes-init") << "...failed." << std::endl;
817
      }
818
    }
819
  }
820
12
  processing.pop_back();
821
12
  return Node();
822
}
823
824
320640
TypeNode DType::getTypeNode() const
825
{
826
320640
  Assert(isResolved());
827
320640
  Assert(!d_self.isNull());
828
320640
  return d_self;
829
}
830
831
8
TypeNode DType::getTypeNode(const std::vector<TypeNode>& params) const
832
{
833
8
  Assert(isResolved());
834
8
  Assert(!d_self.isNull() && d_self.isParametricDatatype());
835
8
  return d_self.instantiateParametricDatatype(params);
836
}
837
838
6288998
const DTypeConstructor& DType::operator[](size_t index) const
839
{
840
6288998
  Assert(index < getNumConstructors());
841
6288998
  return *d_constructors[index];
842
}
843
844
2687
Node DType::getSharedSelector(TypeNode dtt, TypeNode t, size_t index) const
845
{
846
2687
  Assert(isResolved());
847
  std::map<TypeNode, std::map<TypeNode, std::map<unsigned, Node> > >::iterator
848
2687
      itd = d_sharedSel.find(dtt);
849
2687
  if (itd != d_sharedSel.end())
850
  {
851
    std::map<TypeNode, std::map<unsigned, Node> >::iterator its =
852
1852
        itd->second.find(t);
853
1852
    if (its != itd->second.end())
854
    {
855
1325
      std::map<unsigned, Node>::iterator it = its->second.find(index);
856
1325
      if (it != its->second.end())
857
      {
858
802
        return it->second;
859
      }
860
    }
861
  }
862
  // make the shared selector
863
3770
  Node s;
864
1885
  NodeManager* nm = NodeManager::currentNM();
865
3770
  std::stringstream ss;
866
1885
  ss << "sel_" << index;
867
1885
  SkolemManager* sm = nm->getSkolemManager();
868
3770
  TypeNode stype = nm->mkSelectorType(dtt, t);
869
3770
  Node nindex = nm->mkConst(Rational(index));
870
1885
  s = sm->mkSkolemFunction(SkolemFunId::SHARED_SELECTOR,
871
                           stype,
872
                           nindex,
873
                           NodeManager::SKOLEM_NO_NOTIFY);
874
1885
  d_sharedSel[dtt][t][index] = s;
875
3770
  Trace("dt-shared-sel") << "Made " << s << " of type " << dtt << " -> " << t
876
1885
                         << std::endl;
877
1885
  return s;
878
}
879
880
823456
TypeNode DType::getSygusType() const { return d_sygusType; }
881
882
157043
Node DType::getSygusVarList() const { return d_sygusBvl; }
883
884
1300
bool DType::getSygusAllowConst() const { return d_sygusAllowConst; }
885
886
1133
bool DType::getSygusAllowAll() const { return d_sygusAllowAll; }
887
888
bool DType::involvesExternalType() const { return d_involvesExt; }
889
890
21032
bool DType::involvesUninterpretedType() const { return d_involvesUt; }
891
892
10
const std::vector<std::shared_ptr<DTypeConstructor> >& DType::getConstructors()
893
    const
894
{
895
10
  return d_constructors;
896
}
897
898
20
std::ostream& operator<<(std::ostream& os, const DType& dt)
899
{
900
  // can only output datatypes in the cvc5 native language
901
40
  language::SetLanguage::Scope ls(os, language::output::LANG_CVC);
902
20
  dt.toStream(os);
903
40
  return os;
904
}
905
906
20
void DType::toStream(std::ostream& out) const
907
{
908
20
  out << "DATATYPE " << getName();
909
20
  if (isParametric())
910
  {
911
    out << '[';
912
    for (size_t i = 0, nparams = getNumParameters(); i < nparams; ++i)
913
    {
914
      if (i > 0)
915
      {
916
        out << ',';
917
      }
918
      out << getParameter(i);
919
    }
920
    out << ']';
921
  }
922
20
  out << " = " << std::endl;
923
20
  bool firstTime = true;
924
58
  for (std::shared_ptr<DTypeConstructor> ctor : d_constructors)
925
  {
926
38
    if (!firstTime)
927
    {
928
20
      out << " | ";
929
    }
930
38
    firstTime = false;
931
38
    out << *ctor;
932
  }
933
20
  out << " END;" << std::endl;
934
20
}
935
936
DTypeIndexConstant::DTypeIndexConstant(size_t index) : d_index(index) {}
937
std::ostream& operator<<(std::ostream& out, const DTypeIndexConstant& dic)
938
{
939
  return out << "index_" << dic.getIndex();
940
}
941
942
28194
}  // namespace cvc5