GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/sygus/sygus_explain.cpp Lines: 157 164 95.7 %
Date: 2021-09-18 Branches: 294 786 37.4 %

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
54076
void TermRecBuild::addTerm(Node n)
34
{
35
54076
  d_term.push_back(n);
36
108152
  std::vector<Node> currc;
37
54076
  d_kind.push_back(n.getKind());
38
54076
  if (n.getMetaKind() == kind::metakind::PARAMETERIZED)
39
  {
40
53994
    currc.push_back(n.getOperator());
41
53994
    d_has_op.push_back(true);
42
  }
43
  else
44
  {
45
82
    d_has_op.push_back(false);
46
  }
47
96769
  for (unsigned i = 0; i < n.getNumChildren(); i++)
48
  {
49
42693
    currc.push_back(n[i]);
50
  }
51
54076
  d_children.push_back(currc);
52
54076
}
53
54
12750
void TermRecBuild::init(Node n)
55
{
56
12750
  Assert(d_term.empty());
57
12750
  addTerm(n);
58
12750
}
59
60
41326
void TermRecBuild::push(unsigned p)
61
{
62
41326
  Assert(!d_term.empty());
63
41326
  unsigned curr = d_term.size() - 1;
64
41326
  Assert(d_pos.size() == curr);
65
41326
  Assert(d_pos.size() + 1 == d_children.size());
66
41326
  Assert(p < d_term[curr].getNumChildren());
67
41326
  addTerm(d_term[curr][p]);
68
41326
  d_pos.push_back(p);
69
41326
}
70
71
41326
void TermRecBuild::pop()
72
{
73
41326
  Assert(!d_pos.empty());
74
41326
  d_pos.pop_back();
75
41326
  d_kind.pop_back();
76
41326
  d_has_op.pop_back();
77
41326
  d_children.pop_back();
78
41326
  d_term.pop_back();
79
41326
}
80
81
83980
void TermRecBuild::replaceChild(unsigned i, Node r)
82
{
83
83980
  Assert(!d_term.empty());
84
83980
  unsigned curr = d_term.size() - 1;
85
83980
  unsigned o = d_has_op[curr] ? 1 : 0;
86
83980
  d_children[curr][i + o] = r;
87
83980
}
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
68644
Node TermRecBuild::build(unsigned d)
97
{
98
68644
  Assert(d_pos.size() + 1 == d_term.size());
99
68644
  Assert(d < d_term.size());
100
68644
  int p = d < d_pos.size() ? d_pos[d] : -2;
101
137288
  std::vector<Node> children;
102
68644
  unsigned o = d_has_op[d] ? 1 : 0;
103
275397
  for (unsigned i = 0; i < d_children[d].size(); i++)
104
  {
105
413506
    Node nc;
106
206753
    if (p + o == i)
107
    {
108
25990
      nc = build(d + 1);
109
    }
110
    else
111
    {
112
180763
      nc = d_children[d][i];
113
    }
114
206753
    children.push_back(nc);
115
  }
116
137288
  return NodeManager::currentNM()->mkNode(d_kind[d], children);
117
}
118
119
24648
void SygusExplain::getExplanationForEquality(Node n,
120
                                             Node vn,
121
                                             std::vector<Node>& exp)
122
{
123
49296
  std::map<unsigned, bool> cexc;
124
24648
  getExplanationForEquality(n, vn, exp, cexc);
125
24648
}
126
127
24971
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
24971
  Assert(n.getType().isComparableTo(n.getType()));
135
24971
  if (n == vn)
136
  {
137
439
    return;
138
  }
139
49503
  TypeNode tn = n.getType();
140
24971
  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
439
    return;
145
  }
146
24532
  Assert(vn.getKind() == kind::APPLY_CONSTRUCTOR);
147
24532
  const DType& dt = tn.getDType();
148
24532
  int i = datatypes::utils::indexOf(vn.getOperator());
149
49064
  Node tst = datatypes::utils::mkTester(n, i, dt);
150
24532
  exp.push_back(tst);
151
39847
  for (unsigned j = 0; j < vn.getNumChildren(); j++)
152
  {
153
15315
    if (cexc.find(j) == cexc.end())
154
    {
155
      Node sel = NodeManager::currentNM()->mkNode(
156
30630
          kind::APPLY_SELECTOR_TOTAL, dt[i].getSelectorInternal(tn, j), n);
157
15315
      getExplanationForEquality(sel, vn[j], exp);
158
    }
159
  }
160
}
161
162
323
Node SygusExplain::getExplanationForEquality(Node n, Node vn)
163
{
164
646
  std::map<unsigned, bool> cexc;
165
646
  return getExplanationForEquality(n, vn, cexc);
166
}
167
168
323
Node SygusExplain::getExplanationForEquality(Node n,
169
                                             Node vn,
170
                                             std::map<unsigned, bool>& cexc)
171
{
172
646
  std::vector<Node> exp;
173
323
  getExplanationForEquality(n, vn, exp, cexc);
174
323
  Assert(!exp.empty());
175
394
  return exp.size() == 1 ? exp[0]
176
717
                         : 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
54076
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
54076
  Assert(vnr.isNull() || vn != vnr);
192
54076
  Assert(n.getType().isComparableTo(vn.getType()));
193
108031
  TypeNode ntn = n.getType();
194
54076
  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
121
    return;
204
  }
205
53955
  Assert(vn.getKind() == APPLY_CONSTRUCTOR);
206
53955
  Assert(vnr.isNull() || vnr.getKind() == APPLY_CONSTRUCTOR);
207
107910
  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
96609
  for (unsigned i = 0; i < vn.getNumChildren(); i++)
212
  {
213
85308
    TypeNode xtn = vn[i].getType();
214
85308
    Node x = d_tdb->getFreeVarInc(xtn, var_count);
215
42654
    trb.replaceChild(i, x);
216
85308
    Node nvn = trb.build();
217
42654
    Assert(nvn.getKind() == kind::APPLY_CONSTRUCTOR);
218
42654
    if (et.is_invariant(d_tdb, nvn, x))
219
    {
220
1328
      cexc[i] = true;
221
      // we are tracking term size if positive
222
1328
      if (sz >= 0)
223
      {
224
186
        int s = datatypes::utils::getSygusTermSize(vn[i]);
225
186
        sz = sz - s;
226
      }
227
    }
228
    else
229
    {
230
      // revert
231
41326
      trb.replaceChild(i, vn[i]);
232
    }
233
  }
234
53955
  const DType& dt = ntn.getDType();
235
53955
  int cindex = datatypes::utils::indexOf(vn.getOperator());
236
53955
  Assert(cindex >= 0 && cindex < (int)dt.getNumConstructors());
237
107910
  Node tst = datatypes::utils::mkTester(n, cindex, dt);
238
53955
  exp.push_back(tst);
239
  // if the operator of vn is different than vnr, then disunification obligation
240
  // is met
241
53955
  if (!vnr.isNull())
242
  {
243
2964
    if (vnr.getOperator() != vn.getOperator())
244
    {
245
1645
      vnr = Node::null();
246
1645
      vnr_exp = NodeManager::currentNM()->mkConst(true);
247
    }
248
  }
249
96609
  for (unsigned i = 0; i < vn.getNumChildren(); i++)
250
  {
251
    Node sel = NodeManager::currentNM()->mkNode(
252
85308
        kind::APPLY_SELECTOR_TOTAL, dt[cindex].getSelectorInternal(ntn, i), n);
253
85308
    Node vnr_c = vnr.isNull() ? vnr : (vn[i] == vnr[i] ? Node::null() : vnr[i]);
254
42654
    if (cexc.find(i) == cexc.end())
255
    {
256
41326
      trb.push(i);
257
82652
      Node vnr_exp_c;
258
41326
      getExplanationFor(
259
          trb, sel, vn[i], exp, var_count, et, vnr_c, vnr_exp_c, sz);
260
41326
      trb.pop();
261
41326
      if (!vnr_c.isNull())
262
      {
263
1317
        Assert(!vnr_exp_c.isNull());
264
1317
        if (vnr_exp_c.isConst() || vnr_exp.isNull())
265
        {
266
          // recursively satisfied the disunification obligation
267
1317
          if (vnr_exp_c.isConst())
268
          {
269
            // was successful, don't consider further
270
1317
            vnr = Node::null();
271
          }
272
1317
          vnr_exp = vnr_exp_c;
273
        }
274
      }
275
    }
276
    else
277
    {
278
      // if excluded, we may need to add the explanation for this
279
1328
      if (vnr_exp.isNull() && !vnr_c.isNull())
280
      {
281
3
        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
1648
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
3296
  TermRecBuild trb;
311
1648
  trb.init(vn);
312
3296
  Node vnr_exp;
313
1648
  int sz_use = sz;
314
1648
  getExplanationFor(trb, n, vn, exp, var_count, et, vnr, vnr_exp, sz_use);
315
1648
  Assert(sz_use >= 0);
316
1648
  sz = sz_use;
317
1648
  Assert(vnr.isNull() || !vnr_exp.isNull());
318
1648
  if (!vnr_exp.isNull() && !vnr_exp.isConst())
319
  {
320
2
    exp.push_back(vnr_exp.negate());
321
  }
322
1648
}
323
324
6430
void SygusExplain::getExplanationFor(Node n,
325
                                     Node vn,
326
                                     std::vector<Node>& exp,
327
                                     SygusInvarianceTest& et,
328
                                     bool strict)
329
{
330
12860
  std::map<TypeNode, int> var_count;
331
6430
  getExplanationFor(n, vn, exp, et, var_count, strict);
332
6430
}
333
334
13751
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
13751
  if (!strict)
342
  {
343
    // check if it is invariant over the entire node
344
11993
    TypeNode vtn = vn.getType();
345
11993
    Node x = d_tdb->getFreeVarInc(vtn, var_count);
346
7321
    if (et.is_invariant(d_tdb, x, x))
347
    {
348
2649
      return;
349
    }
350
4672
    var_count[vtn]--;
351
  }
352
11102
  int sz = -1;
353
22204
  TermRecBuild trb;
354
11102
  trb.init(vn);
355
22204
  Node vnr;
356
22204
  Node vnr_exp;
357
11102
  getExplanationFor(trb, n, vn, exp, var_count, et, vnr, vnr_exp, sz);
358
}
359
360
}  // namespace quantifiers
361
}  // namespace theory
362
29574
}  // namespace cvc5