GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/datatypes/theory_datatypes_utils.cpp Lines: 86 102 84.3 %
Date: 2021-09-29 Branches: 186 485 38.4 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Aina Niemetz, Morgan Deters
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
 * Implementation of rewriter for the theory of (co)inductive datatypes.
14
 */
15
16
#include "theory/datatypes/theory_datatypes_utils.h"
17
18
#include "expr/ascription_type.h"
19
#include "expr/dtype.h"
20
#include "expr/dtype_cons.h"
21
22
using namespace cvc5;
23
using namespace cvc5::kind;
24
25
namespace cvc5 {
26
namespace theory {
27
namespace datatypes {
28
namespace utils {
29
30
/** get instantiate cons */
31
92206
Node getInstCons(Node n, const DType& dt, size_t index)
32
{
33
92206
  Assert(index < dt.getNumConstructors());
34
184412
  std::vector<Node> children;
35
92206
  NodeManager* nm = NodeManager::currentNM();
36
184412
  TypeNode tn = n.getType();
37
158168
  for (unsigned i = 0, nargs = dt[index].getNumArgs(); i < nargs; i++)
38
  {
39
    Node nc = nm->mkNode(
40
131924
        APPLY_SELECTOR_TOTAL, dt[index].getSelectorInternal(tn, i), n);
41
65962
    children.push_back(nc);
42
  }
43
92206
  Node n_ic = mkApplyCons(tn, dt, index, children);
44
92206
  Assert(n_ic.getType() == tn);
45
92206
  Assert(static_cast<size_t>(isInstCons(n, n_ic, dt)) == index);
46
184412
  return n_ic;
47
}
48
49
93043
Node mkApplyCons(TypeNode tn,
50
                 const DType& dt,
51
                 size_t index,
52
                 const std::vector<Node>& children)
53
{
54
93043
  Assert(tn.isDatatype());
55
93043
  Assert(index < dt.getNumConstructors());
56
93043
  Assert(dt[index].getNumArgs() == children.size());
57
93043
  NodeManager* nm = NodeManager::currentNM();
58
186086
  std::vector<Node> cchildren;
59
93043
  cchildren.push_back(dt[index].getConstructor());
60
93043
  cchildren.insert(cchildren.end(), children.begin(), children.end());
61
93043
  if (dt.isParametric())
62
  {
63
    // add type ascription for ambiguous constructor types
64
116
    Debug("datatypes-parametric")
65
58
        << "Constructor is " << dt[index] << std::endl;
66
116
    TypeNode tspec = dt[index].getSpecializedConstructorType(tn);
67
116
    Debug("datatypes-parametric")
68
58
        << "Type specification is " << tspec << std::endl;
69
174
    cchildren[0] = nm->mkNode(APPLY_TYPE_ASCRIPTION,
70
116
                              nm->mkConst(AscriptionType(tspec)),
71
58
                              cchildren[0]);
72
  }
73
186086
  return nm->mkNode(APPLY_CONSTRUCTOR, cchildren);
74
}
75
76
92206
int isInstCons(Node t, Node n, const DType& dt)
77
{
78
92206
  if (n.getKind() == APPLY_CONSTRUCTOR)
79
  {
80
92206
    int index = indexOf(n.getOperator());
81
92206
    const DTypeConstructor& c = dt[index];
82
184412
    TypeNode tn = n.getType();
83
158168
    for (unsigned i = 0, size = n.getNumChildren(); i < size; i++)
84
    {
85
197886
      if (n[i].getKind() != APPLY_SELECTOR_TOTAL
86
197886
          || n[i].getOperator() != c.getSelectorInternal(tn, i) || n[i][0] != t)
87
      {
88
        return -1;
89
      }
90
    }
91
92206
    return index;
92
  }
93
  return -1;
94
}
95
96
2088155
int isTester(Node n, Node& a)
97
{
98
2088155
  if (n.getKind() == APPLY_TESTER)
99
  {
100
1175496
    a = n[0];
101
1175496
    return indexOf(n.getOperator());
102
  }
103
912659
  return -1;
104
}
105
106
89774
int isTester(Node n)
107
{
108
89774
  if (n.getKind() == APPLY_TESTER)
109
  {
110
89774
    return indexOf(n.getOperator());
111
  }
112
  return -1;
113
}
114
115
3230136
size_t indexOf(Node n) { return DType::indexOf(n); }
116
117
2040
size_t cindexOf(Node n) { return DType::cindexOf(n); }
118
119
475855
const DType& datatypeOf(Node n)
120
{
121
951710
  TypeNode t = n.getType();
122
475855
  switch (t.getKind())
123
  {
124
292753
    case CONSTRUCTOR_TYPE: return t[t.getNumChildren() - 1].getDType();
125
183102
    case SELECTOR_TYPE:
126
    case TESTER_TYPE:
127
183102
    case UPDATER_TYPE: return t[0].getDType();
128
    default:
129
      Unhandled() << "arg must be a datatype constructor, selector, or tester";
130
  }
131
}
132
133
169154
Node mkTester(Node n, int i, const DType& dt)
134
{
135
169154
  return NodeManager::currentNM()->mkNode(APPLY_TESTER, dt[i].getTester(), n);
136
}
137
138
1339
Node mkSplit(Node n, const DType& dt)
139
{
140
2678
  std::vector<Node> splits;
141
8390
  for (unsigned i = 0, ncons = dt.getNumConstructors(); i < ncons; i++)
142
  {
143
14102
    Node test = mkTester(n, i, dt);
144
7051
    splits.push_back(test);
145
  }
146
1339
  NodeManager* nm = NodeManager::currentNM();
147
2678
  return splits.size() == 1 ? splits[0] : nm->mkNode(OR, splits);
148
}
149
150
bool isNullaryApplyConstructor(Node n)
151
{
152
  Assert(n.getKind() == APPLY_CONSTRUCTOR);
153
  for (const Node& nc : n)
154
  {
155
    if (nc.getType().isDatatype())
156
    {
157
      return false;
158
    }
159
  }
160
  return true;
161
}
162
163
bool isNullaryConstructor(const DTypeConstructor& c)
164
{
165
  for (unsigned j = 0, nargs = c.getNumArgs(); j < nargs; j++)
166
  {
167
    if (c[j].getType().getRangeType().isDatatype())
168
    {
169
      return false;
170
    }
171
  }
172
  return true;
173
}
174
175
123181
bool checkClash(Node n1, Node n2, std::vector<Node>& rew)
176
{
177
246362
  Trace("datatypes-rewrite-debug")
178
123181
      << "Check clash : " << n1 << " " << n2 << std::endl;
179
123181
  if (n1.getKind() == APPLY_CONSTRUCTOR && n2.getKind() == APPLY_CONSTRUCTOR)
180
  {
181
28378
    if (n1.getOperator() != n2.getOperator())
182
    {
183
2990
      Trace("datatypes-rewrite-debug")
184
2990
          << "Clash operators : " << n1 << " " << n2 << " " << n1.getOperator()
185
1495
          << " " << n2.getOperator() << std::endl;
186
1495
      return true;
187
    }
188
26883
    Assert(n1.getNumChildren() == n2.getNumChildren());
189
64859
    for (unsigned i = 0, size = n1.getNumChildren(); i < size; i++)
190
    {
191
43330
      if (checkClash(n1[i], n2[i], rew))
192
      {
193
5354
        return true;
194
      }
195
    }
196
  }
197
94803
  else if (n1 != n2)
198
  {
199
91814
    if (n1.isConst() && n2.isConst())
200
    {
201
558
      Trace("datatypes-rewrite-debug")
202
279
          << "Clash constants : " << n1 << " " << n2 << std::endl;
203
279
      return true;
204
    }
205
    else
206
    {
207
183070
      Node eq = NodeManager::currentNM()->mkNode(EQUAL, n1, n2);
208
91535
      rew.push_back(eq);
209
    }
210
  }
211
116053
  return false;
212
}
213
214
}  // namespace utils
215
}  // namespace datatypes
216
}  // namespace theory
217
22746
}  // namespace cvc5