GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/sygus/sygus_eval_unfold.cpp Lines: 172 181 95.0 %
Date: 2021-09-18 Branches: 370 918 40.3 %

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
1233
SygusEvalUnfold::SygusEvalUnfold(TermDbSygus* tds) : d_tds(tds) {}
34
35
188865
void SygusEvalUnfold::registerEvalTerm(Node n)
36
{
37
188865
  Assert(options::sygusEvalUnfold());
38
  // is this a sygus evaluation function application?
39
188865
  if (n.getKind() != DT_SYGUS_EVAL)
40
  {
41
373095
    return;
42
  }
43
3379
  if (d_eval_processed.find(n) != d_eval_processed.end())
44
  {
45
2123
    return;
46
  }
47
2512
  Trace("sygus-eval-unfold")
48
1256
      << "SygusEvalUnfold: register eval term : " << n << std::endl;
49
1256
  d_eval_processed.insert(n);
50
2512
  TypeNode tn = n[0].getType();
51
  // since n[0] is an evaluation head, we know tn is a sygus datatype
52
1256
  Assert(tn.isDatatype());
53
1256
  const DType& dt = tn.getDType();
54
1256
  Assert(dt.isSygus());
55
1256
  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
1256
  d_evals[n[0]].push_back(n);
63
2512
  Node var_list = dt.getSygusVarList();
64
1256
  d_eval_args[n[0]].push_back(std::vector<Node>());
65
4218
  for (unsigned j = 1, size = n.getNumChildren(); j < size; j++)
66
  {
67
2962
    d_eval_args[n[0]].back().push_back(n[j]);
68
  }
69
2512
  Node a = TermDbSygus::getAnchor(n[0]);
70
1256
  d_subterms[a].insert(n[0]);
71
}
72
73
6547
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
6547
  std::map<Node, std::unordered_set<Node> >::iterator its = d_subterms.find(a);
80
6547
  if (its == d_subterms.end())
81
  {
82
2568
    return;
83
  }
84
3979
  NodeManager* nm = NodeManager::currentNM();
85
3979
  SygusExplain* sy_exp = d_tds->getExplain();
86
7958
  Trace("sygus-eval-unfold")
87
7958
      << "SygusEvalUnfold: " << a << ", has " << its->second.size()
88
3979
      << " registered subterms." << std::endl;
89
12428
  for (const Node& n : its->second)
90
  {
91
8449
    Trace("sygus-eval-unfold-debug") << "...process : " << n << std::endl;
92
    std::map<Node, std::vector<std::vector<Node> > >::iterator it =
93
8449
        d_eval_args.find(n);
94
8449
    if (it != d_eval_args.end() && !it->second.empty())
95
    {
96
16898
      TNode at = a;
97
16898
      TNode vt = v;
98
16898
      Node vn = n.substitute(at, vt);
99
8449
      vn = Rewriter::rewrite(vn);
100
8449
      unsigned start = d_node_mv_args_proc[n][vn];
101
      // get explanation in terms of testers
102
16898
      std::vector<Node> antec_exp;
103
8449
      sy_exp->getExplanationForEquality(n, vn, antec_exp);
104
      Node antec =
105
16898
          antec_exp.size() == 1 ? antec_exp[0] : nm->mkNode(AND, antec_exp);
106
      // Node antec = n.eqNode( vn );
107
16898
      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
8449
      SygusTypeInfo& sti = d_tds->getTypeInfo(tn);
111
8449
      bool hasSymCons = sti.hasSubtermSymbolicCons();
112
      // n occurs as an evaluation head, thus it has sygus datatype type
113
8449
      Assert(tn.isDatatype());
114
8449
      const DType& dt = tn.getDType();
115
8449
      Assert(dt.isSygus());
116
16898
      Trace("sygus-eval-unfold")
117
8449
          << "SygusEvalUnfold: Register model value : " << vn << " for " << n
118
8449
          << std::endl;
119
8449
      unsigned curr_size = it->second.size();
120
16898
      Trace("sygus-eval-unfold")
121
8449
          << "...it has " << curr_size << " evaluations, already processed "
122
8449
          << start << "." << std::endl;
123
16898
      Node bTerm = d_tds->sygusToBuiltin(vn, tn);
124
8449
      Trace("sygus-eval-unfold") << "Built-in term : " << bTerm << std::endl;
125
16898
      std::vector<Node> vars;
126
16898
      Node var_list = dt.getSygusVarList();
127
29645
      for (const Node& var : var_list)
128
      {
129
21196
        vars.push_back(var);
130
      }
131
      // evaluation children
132
16898
      std::vector<Node> eval_children;
133
8449
      eval_children.push_back(n);
134
      // for each evaluation
135
17748
      for (unsigned i = start; i < curr_size; i++)
136
      {
137
18598
        Node res;
138
18598
        Node expn;
139
        // should we unfold?
140
9299
        bool do_unfold = false;
141
9299
        if (options::sygusEvalUnfoldBool())
142
        {
143
18598
          Node bTermUse = bTerm;
144
9299
          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
4
            Node bTermOp = bTerm.getOperator();
149
2
            if (bTermOp.getKind() == LAMBDA)
150
            {
151
              bTermUse = bTermOp[0];
152
            }
153
          }
154
9299
          if (bTermUse.getKind() == ITE || bTermUse.getType().isBoolean())
155
          {
156
2317
            do_unfold = true;
157
          }
158
        }
159
9299
        if (do_unfold || hasSymCons)
160
        {
161
          // note that this is replicated for different values
162
5838
          std::map<Node, Node> vtm;
163
5838
          std::vector<Node> exp;
164
2919
          vtm[n] = vn;
165
2919
          eval_children.insert(
166
5838
              eval_children.end(), it->second[i].begin(), it->second[i].end());
167
5838
          Node eval_fun = nm->mkNode(DT_SYGUS_EVAL, eval_children);
168
2919
          eval_children.resize(1);
169
          // If we explicitly asked to unfold, we use single step, otherwise
170
          // we use multi step.
171
2919
          res = unfold(eval_fun, vtm, exp, true, !do_unfold);
172
2919
          Trace("sygus-eval-unfold") << "Unfold returns " << res << std::endl;
173
5838
          expn = exp.size() == 1 ? exp[0] : nm->mkNode(AND, exp);
174
        }
175
        else
176
        {
177
12760
          EvalSygusInvarianceTest esit;
178
6380
          eval_children.insert(
179
12760
              eval_children.end(), it->second[i].begin(), it->second[i].end());
180
12760
          Node conj = nm->mkNode(DT_SYGUS_EVAL, eval_children);
181
6380
          eval_children[0] = vn;
182
12760
          Node eval_fun = nm->mkNode(DT_SYGUS_EVAL, eval_children);
183
6380
          res = d_tds->rewriteNode(eval_fun);
184
12760
          Trace("sygus-eval-unfold")
185
6380
              << "Evaluate with unfolding returns " << res << std::endl;
186
6380
          esit.init(conj, n, res);
187
6380
          eval_children.resize(1);
188
6380
          eval_children[0] = n;
189
190
          // evaluate with minimal explanation
191
12760
          std::vector<Node> mexp;
192
6380
          sy_exp->getExplanationFor(n, vn, mexp, esit);
193
6380
          Assert(!mexp.empty());
194
6380
          expn = mexp.size() == 1 ? mexp[0] : nm->mkNode(AND, mexp);
195
        }
196
9299
        Assert(!res.isNull());
197
9299
        terms.push_back(d_evals[n][i]);
198
9299
        vals.push_back(res);
199
9299
        exps.push_back(expn);
200
18598
        Trace("sygus-eval-unfold")
201
9299
            << "Conclude : " << d_evals[n][i] << " == " << res << std::endl;
202
9299
        Trace("sygus-eval-unfold") << "   from " << expn << std::endl;
203
      }
204
8449
      d_node_mv_args_proc[n][vn] = curr_size;
205
    }
206
  }
207
}
208
209
4300
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
4300
  if (en.getKind() != DT_SYGUS_EVAL)
216
  {
217
    Assert(en.isConst());
218
    return en;
219
  }
220
8600
  Trace("sygus-eval-unfold-debug")
221
4300
      << "Unfold : " << en << ", track exp is " << track_exp << ", doRec is "
222
4300
      << doRec << std::endl;
223
8600
  Node ev = en[0];
224
4300
  if (track_exp)
225
  {
226
4300
    std::map<Node, Node>::iterator itv = vtm.find(en[0]);
227
4300
    Assert(itv != vtm.end());
228
4300
    if (itv != vtm.end())
229
    {
230
4300
      ev = itv->second;
231
    }
232
4300
    Assert(en[0].getType() == ev.getType());
233
4300
    Assert(ev.isConst());
234
  }
235
8600
  Trace("sygus-eval-unfold-debug")
236
4300
      << "Unfold model value is : " << ev << std::endl;
237
4300
  AlwaysAssert(ev.getKind() == APPLY_CONSTRUCTOR);
238
8600
  std::vector<Node> args;
239
13880
  for (unsigned i = 1, nchild = en.getNumChildren(); i < nchild; i++)
240
  {
241
9580
    args.push_back(en[i]);
242
  }
243
244
8600
  TypeNode headType = en[0].getType();
245
4300
  NodeManager* nm = NodeManager::currentNM();
246
4300
  const DType& dt = headType.getDType();
247
4300
  unsigned i = datatypes::utils::indexOf(ev.getOperator());
248
4300
  if (track_exp)
249
  {
250
    // explanation
251
8600
    Node ee = nm->mkNode(APPLY_TESTER, dt[i].getTester(), en[0]);
252
4300
    if (std::find(exp.begin(), exp.end(), ee) == exp.end())
253
    {
254
4300
      exp.push_back(ee);
255
    }
256
  }
257
  // if we are a symbolic constructor, unfolding returns the subterm itself
258
8600
  Node sop = dt[i].getSygusOp();
259
4300
  if (sop.getAttribute(SygusAnyConstAttribute()))
260
  {
261
1096
    Trace("sygus-eval-unfold-debug")
262
548
        << "...it is an any-constant constructor" << std::endl;
263
548
    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
548
    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
1096
          APPLY_SELECTOR_TOTAL, dt[i].getSelectorInternal(headType, 0), en[0]);
276
1096
      Trace("sygus-eval-unfold-debug")
277
548
          << "...return (from constructor) " << ret << std::endl;
278
548
      return ret;
279
    }
280
  }
281
282
3752
  Assert(!dt.isParametric());
283
7504
  std::map<int, Node> pre;
284
9722
  for (unsigned j = 0, nargs = dt[i].getNumArgs(); j < nargs; j++)
285
  {
286
11940
    std::vector<Node> cc;
287
11940
    Node s;
288
    // get the j^th subfield of en
289
5970
    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
17910
      s = nm->mkNode(
298
11940
          APPLY_SELECTOR_TOTAL, dt[i].getSelectorInternal(headType, j), en[0]);
299
    }
300
5970
    cc.push_back(s);
301
5970
    if (track_exp)
302
    {
303
      // update vtm map
304
5970
      vtm[s] = ev[j];
305
    }
306
5970
    cc.insert(cc.end(), args.begin(), args.end());
307
11940
    Node argj = nm->mkNode(DT_SYGUS_EVAL, cc);
308
5970
    if (doRec)
309
    {
310
1381
      Trace("sygus-eval-unfold-debug") << "Recurse on " << s << std::endl;
311
      // evaluate recursively
312
1381
      argj = unfold(argj, vtm, exp, track_exp, doRec);
313
    }
314
5970
    pre[j] = argj;
315
  }
316
7504
  Node ret = d_tds->mkGeneric(dt, i, pre);
317
  // apply the appropriate substitution to ret
318
3752
  ret = datatypes::utils::applySygusArgs(dt, sop, ret, args);
319
7504
  Trace("sygus-eval-unfold-debug")
320
3752
      << "Applied sygus args : " << ret << std::endl;
321
  // rewrite
322
3752
  ret = Rewriter::rewrite(ret);
323
3752
  Trace("sygus-eval-unfold-debug") << "Rewritten : " << ret << std::endl;
324
3752
  return ret;
325
}
326
327
}  // namespace quantifiers
328
}  // namespace theory
329
29574
}  // namespace cvc5