GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/sygus/sygus_explain.cpp Lines: 157 164 95.7 %
Date: 2021-08-17 Branches: 292 786 37.2 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Aina Niemetz
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 techniques for sygus explanations.
14
 */
15
16
#include "theory/quantifiers/sygus/sygus_explain.h"
17
18
#include "expr/dtype.h"
19
#include "expr/dtype_cons.h"
20
#include "smt/logic_exception.h"
21
#include "theory/datatypes/sygus_datatype_utils.h"
22
#include "theory/datatypes/theory_datatypes_utils.h"
23
#include "theory/quantifiers/sygus/sygus_invariance.h"
24
#include "theory/quantifiers/sygus/term_database_sygus.h"
25
26
using namespace cvc5::kind;
27
using namespace std;
28
29
namespace cvc5 {
30
namespace theory {
31
namespace quantifiers {
32
33
70134
void TermRecBuild::addTerm(Node n)
34
{
35
70134
  d_term.push_back(n);
36
140268
  std::vector<Node> currc;
37
70134
  d_kind.push_back(n.getKind());
38
70134
  if (n.getMetaKind() == kind::metakind::PARAMETERIZED)
39
  {
40
70110
    currc.push_back(n.getOperator());
41
70110
    d_has_op.push_back(true);
42
  }
43
  else
44
  {
45
24
    d_has_op.push_back(false);
46
  }
47
126271
  for (unsigned i = 0; i < n.getNumChildren(); i++)
48
  {
49
56137
    currc.push_back(n[i]);
50
  }
51
70134
  d_children.push_back(currc);
52
70134
}
53
54
16088
void TermRecBuild::init(Node n)
55
{
56
16088
  Assert(d_term.empty());
57
16088
  addTerm(n);
58
16088
}
59
60
54046
void TermRecBuild::push(unsigned p)
61
{
62
54046
  Assert(!d_term.empty());
63
54046
  unsigned curr = d_term.size() - 1;
64
54046
  Assert(d_pos.size() == curr);
65
54046
  Assert(d_pos.size() + 1 == d_children.size());
66
54046
  Assert(p < d_term[curr].getNumChildren());
67
54046
  addTerm(d_term[curr][p]);
68
54046
  d_pos.push_back(p);
69
54046
}
70
71
54046
void TermRecBuild::pop()
72
{
73
54046
  Assert(!d_pos.empty());
74
54046
  d_pos.pop_back();
75
54046
  d_kind.pop_back();
76
54046
  d_has_op.pop_back();
77
54046
  d_children.pop_back();
78
54046
  d_term.pop_back();
79
54046
}
80
81
110147
void TermRecBuild::replaceChild(unsigned i, Node r)
82
{
83
110147
  Assert(!d_term.empty());
84
110147
  unsigned curr = d_term.size() - 1;
85
110147
  unsigned o = d_has_op[curr] ? 1 : 0;
86
110147
  d_children[curr][i + o] = r;
87
110147
}
88
89
Node TermRecBuild::getChild(unsigned i)
90
{
91
  unsigned curr = d_term.size() - 1;
92
  unsigned o = d_has_op[curr] ? 1 : 0;
93
  return d_children[curr][i + o];
94
}
95
96
90025
Node TermRecBuild::build(unsigned d)
97
{
98
90025
  Assert(d_pos.size() + 1 == d_term.size());
99
90025
  Assert(d < d_term.size());
100
90025
  int p = d < d_pos.size() ? d_pos[d] : -2;
101
180050
  std::vector<Node> children;
102
90025
  unsigned o = d_has_op[d] ? 1 : 0;
103
360939
  for (unsigned i = 0; i < d_children[d].size(); i++)
104
  {
105
541828
    Node nc;
106
270914
    if (p + o == i)
107
    {
108
33924
      nc = build(d + 1);
109
    }
110
    else
111
    {
112
236990
      nc = d_children[d][i];
113
    }
114
270914
    children.push_back(nc);
115
  }
116
180050
  return NodeManager::currentNM()->mkNode(d_kind[d], children);
117
}
118
119
30757
void SygusExplain::getExplanationForEquality(Node n,
120
                                             Node vn,
121
                                             std::vector<Node>& exp)
122
{
123
61514
  std::map<unsigned, bool> cexc;
124
30757
  getExplanationForEquality(n, vn, exp, cexc);
125
30757
}
126
127
31082
void SygusExplain::getExplanationForEquality(Node n,
128
                                             Node vn,
129
                                             std::vector<Node>& exp,
130
                                             std::map<unsigned, bool>& cexc)
131
{
132
  // since builtin types occur in grammar, types are comparable but not
133
  // necessarily equal
134
31082
  Assert(n.getType().isComparableTo(n.getType()));
135
31082
  if (n == vn)
136
  {
137
367
    return;
138
  }
139
61797
  TypeNode tn = n.getType();
140
31082
  if (!tn.isDatatype())
141
  {
142
    // sygus datatype fields that are not sygus datatypes are treated as
143
    // abstractions only, hence we disregard this field
144
367
    return;
145
  }
146
30715
  Assert(vn.getKind() == kind::APPLY_CONSTRUCTOR);
147
30715
  const DType& dt = tn.getDType();
148
30715
  int i = datatypes::utils::indexOf(vn.getOperator());
149
61430
  Node tst = datatypes::utils::mkTester(n, i, dt);
150
30715
  exp.push_back(tst);
151
50255
  for (unsigned j = 0; j < vn.getNumChildren(); j++)
152
  {
153
19540
    if (cexc.find(j) == cexc.end())
154
    {
155
      Node sel = NodeManager::currentNM()->mkNode(
156
39080
          kind::APPLY_SELECTOR_TOTAL, dt[i].getSelectorInternal(tn, j), n);
157
19540
      getExplanationForEquality(sel, vn[j], exp);
158
    }
159
  }
160
}
161
162
325
Node SygusExplain::getExplanationForEquality(Node n, Node vn)
163
{
164
650
  std::map<unsigned, bool> cexc;
165
650
  return getExplanationForEquality(n, vn, cexc);
166
}
167
168
325
Node SygusExplain::getExplanationForEquality(Node n,
169
                                             Node vn,
170
                                             std::map<unsigned, bool>& cexc)
171
{
172
650
  std::vector<Node> exp;
173
325
  getExplanationForEquality(n, vn, exp, cexc);
174
325
  Assert(!exp.empty());
175
425
  return exp.size() == 1 ? exp[0]
176
750
                         : NodeManager::currentNM()->mkNode(kind::AND, exp);
177
}
178
179
// we have ( n = vn => eval( n ) = bvr ) ^ vn != vnr , returns exp such that exp
180
// => ( eval( n ) = bvr ^ vn != vnr )
181
70134
void SygusExplain::getExplanationFor(TermRecBuild& trb,
182
                                     Node n,
183
                                     Node vn,
184
                                     std::vector<Node>& exp,
185
                                     std::map<TypeNode, int>& var_count,
186
                                     SygusInvarianceTest& et,
187
                                     Node vnr,
188
                                     Node& vnr_exp,
189
                                     int& sz)
190
{
191
70134
  Assert(vnr.isNull() || vn != vnr);
192
70134
  Assert(n.getType().isComparableTo(vn.getType()));
193
140208
  TypeNode ntn = n.getType();
194
70134
  if (!ntn.isDatatype())
195
  {
196
    // SyGuS datatype fields that are not sygus datatypes are treated as
197
    // abstractions only, hence we disregard this field. It is important
198
    // that users of this method pay special attention to any constants,
199
    // otherwise the explanation n.eqNode(vn) is necessary here. For example,
200
    // any lemma schema that blocks the current value of an enumerator should
201
    // not make any assumptions about the value of the arguments of its any
202
    // constant constructors, since their explanation is not included here.
203
60
    return;
204
  }
205
70074
  Assert(vn.getKind() == APPLY_CONSTRUCTOR);
206
70074
  Assert(vnr.isNull() || vnr.getKind() == APPLY_CONSTRUCTOR);
207
140148
  std::map<unsigned, bool> cexc;
208
  // for each child,
209
  // check whether replacing that child by a fresh variable
210
  // also satisfies the invariance test.
211
126175
  for (unsigned i = 0; i < vn.getNumChildren(); i++)
212
  {
213
112202
    TypeNode xtn = vn[i].getType();
214
112202
    Node x = d_tdb->getFreeVarInc(xtn, var_count);
215
56101
    trb.replaceChild(i, x);
216
112202
    Node nvn = trb.build();
217
56101
    Assert(nvn.getKind() == kind::APPLY_CONSTRUCTOR);
218
56101
    if (et.is_invariant(d_tdb, nvn, x))
219
    {
220
2055
      cexc[i] = true;
221
      // we are tracking term size if positive
222
2055
      if (sz >= 0)
223
      {
224
189
        int s = datatypes::utils::getSygusTermSize(vn[i]);
225
189
        sz = sz - s;
226
      }
227
    }
228
    else
229
    {
230
      // revert
231
54046
      trb.replaceChild(i, vn[i]);
232
    }
233
  }
234
70074
  const DType& dt = ntn.getDType();
235
70074
  int cindex = datatypes::utils::indexOf(vn.getOperator());
236
70074
  Assert(cindex >= 0 && cindex < (int)dt.getNumConstructors());
237
140148
  Node tst = datatypes::utils::mkTester(n, cindex, dt);
238
70074
  exp.push_back(tst);
239
  // if the operator of vn is different than vnr, then disunification obligation
240
  // is met
241
70074
  if (!vnr.isNull())
242
  {
243
3129
    if (vnr.getOperator() != vn.getOperator())
244
    {
245
1726
      vnr = Node::null();
246
1726
      vnr_exp = NodeManager::currentNM()->mkConst(true);
247
    }
248
  }
249
126175
  for (unsigned i = 0; i < vn.getNumChildren(); i++)
250
  {
251
    Node sel = NodeManager::currentNM()->mkNode(
252
112202
        kind::APPLY_SELECTOR_TOTAL, dt[cindex].getSelectorInternal(ntn, i), n);
253
112202
    Node vnr_c = vnr.isNull() ? vnr : (vn[i] == vnr[i] ? Node::null() : vnr[i]);
254
56101
    if (cexc.find(i) == cexc.end())
255
    {
256
54046
      trb.push(i);
257
108092
      Node vnr_exp_c;
258
54046
      getExplanationFor(
259
          trb, sel, vn[i], exp, var_count, et, vnr_c, vnr_exp_c, sz);
260
54046
      trb.pop();
261
54046
      if (!vnr_c.isNull())
262
      {
263
1400
        Assert(!vnr_exp_c.isNull());
264
1400
        if (vnr_exp_c.isConst() || vnr_exp.isNull())
265
        {
266
          // recursively satisfied the disunification obligation
267
1400
          if (vnr_exp_c.isConst())
268
          {
269
            // was successful, don't consider further
270
1400
            vnr = Node::null();
271
          }
272
1400
          vnr_exp = vnr_exp_c;
273
        }
274
      }
275
    }
276
    else
277
    {
278
      // if excluded, we may need to add the explanation for this
279
2055
      if (vnr_exp.isNull() && !vnr_c.isNull())
280
      {
281
4
        vnr_exp = getExplanationForEquality(sel, vnr[i]);
282
      }
283
    }
284
  }
285
}
286
287
void SygusExplain::getExplanationFor(Node n,
288
                                     Node vn,
289
                                     std::vector<Node>& exp,
290
                                     SygusInvarianceTest& et,
291
                                     Node vnr,
292
                                     unsigned& sz)
293
{
294
  std::map<TypeNode, int> var_count;
295
  return getExplanationFor(n, vn, exp, et, vnr, var_count, sz);
296
}
297
298
1729
void SygusExplain::getExplanationFor(Node n,
299
                                     Node vn,
300
                                     std::vector<Node>& exp,
301
                                     SygusInvarianceTest& et,
302
                                     Node vnr,
303
                                     std::map<TypeNode, int>& var_count,
304
                                     unsigned& sz)
305
{
306
  // naive :
307
  // return getExplanationForEquality( n, vn, exp );
308
309
  // set up the recursion object;
310
3458
  TermRecBuild trb;
311
1729
  trb.init(vn);
312
3458
  Node vnr_exp;
313
1729
  int sz_use = sz;
314
1729
  getExplanationFor(trb, n, vn, exp, var_count, et, vnr, vnr_exp, sz_use);
315
1729
  Assert(sz_use >= 0);
316
1729
  sz = sz_use;
317
1729
  Assert(vnr.isNull() || !vnr_exp.isNull());
318
1729
  if (!vnr_exp.isNull() && !vnr_exp.isConst())
319
  {
320
3
    exp.push_back(vnr_exp.negate());
321
  }
322
1729
}
323
324
8967
void SygusExplain::getExplanationFor(Node n,
325
                                     Node vn,
326
                                     std::vector<Node>& exp,
327
                                     SygusInvarianceTest& et,
328
                                     bool strict)
329
{
330
17934
  std::map<TypeNode, int> var_count;
331
8967
  getExplanationFor(n, vn, exp, et, var_count, strict);
332
8967
}
333
334
17163
void SygusExplain::getExplanationFor(Node n,
335
                                     Node vn,
336
                                     std::vector<Node>& exp,
337
                                     SygusInvarianceTest& et,
338
                                     std::map<TypeNode, int>& var_count,
339
                                     bool strict)
340
{
341
17163
  if (!strict)
342
  {
343
    // check if it is invariant over the entire node
344
13588
    TypeNode vtn = vn.getType();
345
13588
    Node x = d_tdb->getFreeVarInc(vtn, var_count);
346
8196
    if (et.is_invariant(d_tdb, x, x))
347
    {
348
2804
      return;
349
    }
350
5392
    var_count[vtn]--;
351
  }
352
14359
  int sz = -1;
353
28718
  TermRecBuild trb;
354
14359
  trb.init(vn);
355
28718
  Node vnr;
356
28718
  Node vnr_exp;
357
14359
  getExplanationFor(trb, n, vn, exp, var_count, et, vnr, vnr_exp, sz);
358
}
359
360
}  // namespace quantifiers
361
}  // namespace theory
362
29337
}  // namespace cvc5