GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/sygus/sygus_eval_unfold.cpp Lines: 170 181 93.9 %
Date: 2021-11-05 Branches: 366 918 39.9 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Mathias Preiner, 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 sygus_eval_unfold.
14
 */
15
16
#include "theory/quantifiers/sygus/sygus_eval_unfold.h"
17
18
#include "expr/dtype_cons.h"
19
#include "expr/sygus_datatype.h"
20
#include "options/quantifiers_options.h"
21
#include "theory/datatypes/sygus_datatype_utils.h"
22
#include "theory/quantifiers/sygus/term_database_sygus.h"
23
#include "theory/rewriter.h"
24
25
using namespace std;
26
using namespace cvc5::kind;
27
using namespace cvc5::context;
28
29
namespace cvc5 {
30
namespace theory {
31
namespace quantifiers {
32
33
1901
SygusEvalUnfold::SygusEvalUnfold(TermDbSygus* tds) : d_tds(tds) {}
34
35
343646
void SygusEvalUnfold::registerEvalTerm(Node n)
36
{
37
343646
  Assert(options::sygusEvalUnfold());
38
  // is this a sygus evaluation function application?
39
343646
  if (n.getKind() != DT_SYGUS_EVAL)
40
  {
41
677399
    return;
42
  }
43
7317
  if (d_eval_processed.find(n) != d_eval_processed.end())
44
  {
45
4741
    return;
46
  }
47
5152
  Trace("sygus-eval-unfold")
48
2576
      << "SygusEvalUnfold: register eval term : " << n << std::endl;
49
2576
  d_eval_processed.insert(n);
50
5152
  TypeNode tn = n[0].getType();
51
  // since n[0] is an evaluation head, we know tn is a sygus datatype
52
2576
  Assert(tn.isDatatype());
53
2576
  const DType& dt = tn.getDType();
54
2576
  Assert(dt.isSygus());
55
2576
  if (n[0].getKind() == APPLY_CONSTRUCTOR)
56
  {
57
    // constructors should be unfolded and reduced already
58
    Assert(false);
59
    return;
60
  }
61
  // register this evaluation term with its head
62
2576
  d_evals[n[0]].push_back(n);
63
5152
  Node var_list = dt.getSygusVarList();
64
2576
  d_eval_args[n[0]].push_back(std::vector<Node>());
65
9771
  for (unsigned j = 1, size = n.getNumChildren(); j < size; j++)
66
  {
67
7195
    d_eval_args[n[0]].back().push_back(n[j]);
68
  }
69
5152
  Node a = TermDbSygus::getAnchor(n[0]);
70
2576
  d_subterms[a].insert(n[0]);
71
}
72
73
10936
void SygusEvalUnfold::registerModelValue(Node a,
74
                                         Node v,
75
                                         std::vector<Node>& terms,
76
                                         std::vector<Node>& vals,
77
                                         std::vector<Node>& exps)
78
{
79
10936
  std::map<Node, std::unordered_set<Node> >::iterator its = d_subterms.find(a);
80
10936
  if (its == d_subterms.end())
81
  {
82
3452
    return;
83
  }
84
7484
  NodeManager* nm = NodeManager::currentNM();
85
7484
  SygusExplain* sy_exp = d_tds->getExplain();
86
14968
  Trace("sygus-eval-unfold")
87
14968
      << "SygusEvalUnfold: " << a << ", has " << its->second.size()
88
7484
      << " registered subterms." << std::endl;
89
25802
  for (const Node& n : its->second)
90
  {
91
18318
    Trace("sygus-eval-unfold-debug") << "...process : " << n << std::endl;
92
    std::map<Node, std::vector<std::vector<Node> > >::iterator it =
93
18318
        d_eval_args.find(n);
94
18318
    if (it != d_eval_args.end() && !it->second.empty())
95
    {
96
36636
      TNode at = a;
97
36636
      TNode vt = v;
98
36636
      Node vn = n.substitute(at, vt);
99
18318
      vn = Rewriter::rewrite(vn);
100
18318
      unsigned start = d_node_mv_args_proc[n][vn];
101
      // get explanation in terms of testers
102
36636
      std::vector<Node> antec_exp;
103
18318
      sy_exp->getExplanationForEquality(n, vn, antec_exp);
104
      Node antec =
105
36636
          antec_exp.size() == 1 ? antec_exp[0] : nm->mkNode(AND, antec_exp);
106
      // Node antec = n.eqNode( vn );
107
36636
      TypeNode tn = n.getType();
108
      // Check if the sygus type has any symbolic constructors. This will
109
      // impact how the unfolding is computed below.
110
18318
      SygusTypeInfo& sti = d_tds->getTypeInfo(tn);
111
18318
      bool hasSymCons = sti.hasSubtermSymbolicCons();
112
      // n occurs as an evaluation head, thus it has sygus datatype type
113
18318
      Assert(tn.isDatatype());
114
18318
      const DType& dt = tn.getDType();
115
18318
      Assert(dt.isSygus());
116
36636
      Trace("sygus-eval-unfold")
117
18318
          << "SygusEvalUnfold: Register model value : " << vn << " for " << n
118
18318
          << std::endl;
119
18318
      unsigned curr_size = it->second.size();
120
36636
      Trace("sygus-eval-unfold")
121
18318
          << "...it has " << curr_size << " evaluations, already processed "
122
18318
          << start << "." << std::endl;
123
36636
      Node bTerm = d_tds->sygusToBuiltin(vn, tn);
124
18318
      Trace("sygus-eval-unfold") << "Built-in term : " << bTerm << std::endl;
125
36636
      std::vector<Node> vars;
126
36636
      Node var_list = dt.getSygusVarList();
127
72107
      for (const Node& var : var_list)
128
      {
129
53789
        vars.push_back(var);
130
      }
131
      // evaluation children
132
36636
      std::vector<Node> eval_children;
133
18318
      eval_children.push_back(n);
134
      // for each evaluation
135
44594
      for (unsigned i = start; i < curr_size; i++)
136
      {
137
52552
        Node res;
138
52552
        Node expn;
139
        // should we unfold?
140
26276
        bool do_unfold = false;
141
26276
        if (options::sygusEvalUnfoldBool())
142
        {
143
52552
          Node bTermUse = bTerm;
144
26276
          if (bTerm.getKind() == APPLY_UF)
145
          {
146
            // if the builtin term is non-beta-reduced application of lambda,
147
            // we look at the body of the lambda.
148
            Node bTermOp = bTerm.getOperator();
149
            if (bTermOp.getKind() == LAMBDA)
150
            {
151
              bTermUse = bTermOp[0];
152
            }
153
          }
154
26276
          if (bTermUse.getKind() == ITE || bTermUse.getType().isBoolean())
155
          {
156
7249
            do_unfold = true;
157
          }
158
        }
159
26276
        if (do_unfold || hasSymCons)
160
        {
161
          // note that this is replicated for different values
162
16642
          std::map<Node, Node> vtm;
163
16642
          std::vector<Node> exp;
164
8321
          vtm[n] = vn;
165
8321
          eval_children.insert(
166
16642
              eval_children.end(), it->second[i].begin(), it->second[i].end());
167
16642
          Node eval_fun = nm->mkNode(DT_SYGUS_EVAL, eval_children);
168
8321
          eval_children.resize(1);
169
          // If we explicitly asked to unfold, we use single step, otherwise
170
          // we use multi step.
171
8321
          res = unfold(eval_fun, vtm, exp, true, !do_unfold);
172
8321
          Trace("sygus-eval-unfold") << "Unfold returns " << res << std::endl;
173
16642
          expn = exp.size() == 1 ? exp[0] : nm->mkNode(AND, exp);
174
        }
175
        else
176
        {
177
35910
          EvalSygusInvarianceTest esit;
178
17955
          eval_children.insert(
179
35910
              eval_children.end(), it->second[i].begin(), it->second[i].end());
180
35910
          Node conj = nm->mkNode(DT_SYGUS_EVAL, eval_children);
181
17955
          eval_children[0] = vn;
182
35910
          Node eval_fun = nm->mkNode(DT_SYGUS_EVAL, eval_children);
183
17955
          res = d_tds->rewriteNode(eval_fun);
184
35910
          Trace("sygus-eval-unfold")
185
17955
              << "Evaluate with unfolding returns " << res << std::endl;
186
17955
          esit.init(conj, n, res);
187
17955
          eval_children.resize(1);
188
17955
          eval_children[0] = n;
189
190
          // evaluate with minimal explanation
191
35910
          std::vector<Node> mexp;
192
17955
          sy_exp->getExplanationFor(n, vn, mexp, esit);
193
17955
          Assert(!mexp.empty());
194
17955
          expn = mexp.size() == 1 ? mexp[0] : nm->mkNode(AND, mexp);
195
        }
196
26276
        Assert(!res.isNull());
197
26276
        terms.push_back(d_evals[n][i]);
198
26276
        vals.push_back(res);
199
26276
        exps.push_back(expn);
200
52552
        Trace("sygus-eval-unfold")
201
26276
            << "Conclude : " << d_evals[n][i] << " == " << res << std::endl;
202
26276
        Trace("sygus-eval-unfold") << "   from " << expn << std::endl;
203
      }
204
18318
      d_node_mv_args_proc[n][vn] = curr_size;
205
    }
206
  }
207
}
208
209
10881
Node SygusEvalUnfold::unfold(Node en,
210
                             std::map<Node, Node>& vtm,
211
                             std::vector<Node>& exp,
212
                             bool track_exp,
213
                             bool doRec)
214
{
215
10881
  if (en.getKind() != DT_SYGUS_EVAL)
216
  {
217
    Assert(en.isConst());
218
    return en;
219
  }
220
21762
  Trace("sygus-eval-unfold-debug")
221
10881
      << "Unfold : " << en << ", track exp is " << track_exp << ", doRec is "
222
10881
      << doRec << std::endl;
223
21762
  Node ev = en[0];
224
10881
  if (track_exp)
225
  {
226
10881
    std::map<Node, Node>::iterator itv = vtm.find(en[0]);
227
10881
    Assert(itv != vtm.end());
228
10881
    if (itv != vtm.end())
229
    {
230
10881
      ev = itv->second;
231
    }
232
10881
    Assert(en[0].getType() == ev.getType());
233
10881
    Assert(ev.isConst());
234
  }
235
21762
  Trace("sygus-eval-unfold-debug")
236
10881
      << "Unfold model value is : " << ev << std::endl;
237
10881
  AlwaysAssert(ev.getKind() == APPLY_CONSTRUCTOR);
238
21762
  std::vector<Node> args;
239
36133
  for (unsigned i = 1, nchild = en.getNumChildren(); i < nchild; i++)
240
  {
241
25252
    args.push_back(en[i]);
242
  }
243
244
21762
  TypeNode headType = en[0].getType();
245
10881
  NodeManager* nm = NodeManager::currentNM();
246
10881
  const DType& dt = headType.getDType();
247
10881
  unsigned i = datatypes::utils::indexOf(ev.getOperator());
248
10881
  if (track_exp)
249
  {
250
    // explanation
251
21762
    Node ee = nm->mkNode(APPLY_TESTER, dt[i].getTester(), en[0]);
252
10881
    if (std::find(exp.begin(), exp.end(), ee) == exp.end())
253
    {
254
10881
      exp.push_back(ee);
255
    }
256
  }
257
  // if we are a symbolic constructor, unfolding returns the subterm itself
258
21762
  Node sop = dt[i].getSygusOp();
259
10881
  if (sop.getAttribute(SygusAnyConstAttribute()))
260
  {
261
1980
    Trace("sygus-eval-unfold-debug")
262
990
        << "...it is an any-constant constructor" << std::endl;
263
990
    Assert(dt[i].getNumArgs() == 1);
264
    // If the argument to evaluate is itself concrete, then we use its
265
    // argument; otherwise we return its selector.
266
990
    if (en[0].getKind() == APPLY_CONSTRUCTOR)
267
    {
268
      Trace("sygus-eval-unfold-debug")
269
          << "...return (from constructor) " << en[0][0] << std::endl;
270
      return en[0][0];
271
    }
272
    else
273
    {
274
      Node ret = nm->mkNode(
275
1980
          APPLY_SELECTOR_TOTAL, dt[i].getSelectorInternal(headType, 0), en[0]);
276
1980
      Trace("sygus-eval-unfold-debug")
277
990
          << "...return (from constructor) " << ret << std::endl;
278
990
      return ret;
279
    }
280
  }
281
282
9891
  Assert(!dt.isParametric());
283
19782
  std::map<int, Node> pre;
284
27184
  for (unsigned j = 0, nargs = dt[i].getNumArgs(); j < nargs; j++)
285
  {
286
34586
    std::vector<Node> cc;
287
34586
    Node s;
288
    // get the j^th subfield of en
289
17293
    if (en[0].getKind() == APPLY_CONSTRUCTOR)
290
    {
291
      // if it is a concrete constructor application, as an optimization,
292
      // just return the argument
293
      s = en[0][j];
294
    }
295
    else
296
    {
297
51879
      s = nm->mkNode(
298
34586
          APPLY_SELECTOR_TOTAL, dt[i].getSelectorInternal(headType, j), en[0]);
299
    }
300
17293
    cc.push_back(s);
301
17293
    if (track_exp)
302
    {
303
      // update vtm map
304
17293
      vtm[s] = ev[j];
305
    }
306
17293
    cc.insert(cc.end(), args.begin(), args.end());
307
34586
    Node argj = nm->mkNode(DT_SYGUS_EVAL, cc);
308
17293
    if (doRec)
309
    {
310
2560
      Trace("sygus-eval-unfold-debug") << "Recurse on " << s << std::endl;
311
      // evaluate recursively
312
2560
      argj = unfold(argj, vtm, exp, track_exp, doRec);
313
    }
314
17293
    pre[j] = argj;
315
  }
316
19782
  Node ret = d_tds->mkGeneric(dt, i, pre);
317
  // apply the appropriate substitution to ret
318
9891
  ret = datatypes::utils::applySygusArgs(dt, sop, ret, args);
319
19782
  Trace("sygus-eval-unfold-debug")
320
9891
      << "Applied sygus args : " << ret << std::endl;
321
  // rewrite
322
9891
  ret = Rewriter::rewrite(ret);
323
9891
  Trace("sygus-eval-unfold-debug") << "Rewritten : " << ret << std::endl;
324
9891
  return ret;
325
}
326
327
}  // namespace quantifiers
328
}  // namespace theory
329
31125
}  // namespace cvc5