GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/sygus/sygus_invariance.cpp Lines: 111 138 80.4 %
Date: 2021-05-22 Branches: 203 532 38.2 %

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 techniques for sygus invariance tests.
14
 */
15
16
#include "theory/quantifiers/sygus/sygus_invariance.h"
17
18
#include "theory/quantifiers/sygus/sygus_pbe.h"
19
#include "theory/quantifiers/sygus/synth_conjecture.h"
20
#include "theory/quantifiers/sygus/term_database_sygus.h"
21
#include "theory/rewriter.h"
22
23
using namespace cvc5::kind;
24
using namespace std;
25
26
namespace cvc5 {
27
namespace theory {
28
namespace quantifiers {
29
30
14522
void EvalSygusInvarianceTest::init(Node conj, Node var, Node res)
31
{
32
14522
  d_terms.clear();
33
  // simple miniscope
34
14522
  if ((conj.getKind() == AND || conj.getKind() == OR) && res.isConst())
35
  {
36
1524
    for (const Node& c : conj)
37
    {
38
1017
      d_terms.push_back(c);
39
    }
40
507
    d_kind = conj.getKind();
41
507
    d_is_conjunctive = res.getConst<bool>() == (d_kind == AND);
42
  }
43
  else
44
  {
45
14015
    d_terms.push_back(conj);
46
14015
    d_is_conjunctive = true;
47
  }
48
14522
  d_var = var;
49
14522
  d_result = res;
50
14522
}
51
52
63322
Node EvalSygusInvarianceTest::doEvaluateWithUnfolding(TermDbSygus* tds, Node n)
53
{
54
63322
  return tds->evaluateWithUnfolding(n, d_visited);
55
}
56
57
51264
bool EvalSygusInvarianceTest::invariant(TermDbSygus* tds, Node nvn, Node x)
58
{
59
102528
  TNode tnvn = nvn;
60
102528
  std::unordered_map<TNode, TNode> cache;
61
56101
  for (const Node& c : d_terms)
62
  {
63
56537
    Node conj_subs = c.substitute(d_var, tnvn, cache);
64
56537
    Node conj_subs_unfold = doEvaluateWithUnfolding(tds, conj_subs);
65
103400
    Trace("sygus-cref-eval2-debug")
66
51700
        << "  ...check unfolding : " << conj_subs_unfold << std::endl;
67
103400
    Trace("sygus-cref-eval2-debug")
68
51700
        << "  ......from : " << conj_subs << std::endl;
69
51700
    if (conj_subs_unfold != d_result)
70
    {
71
46863
      if (d_is_conjunctive)
72
      {
73
        // ti /--> true  implies and( t1, ..., tn ) /--> true, where "/-->" is
74
        // "does not evaluate to".
75
46863
        return false;
76
      }
77
    }
78
4837
    else if (!d_is_conjunctive)
79
    {
80
      // ti --> true  implies or( t1, ..., tn ) --> true
81
      return true;
82
    }
83
9674
    Trace("sygus-cref-eval2") << "Evaluation min explain : " << conj_subs
84
4837
                              << " still evaluates to " << d_result
85
4837
                              << " regardless of ";
86
4837
    Trace("sygus-cref-eval2") << x << std::endl;
87
  }
88
4401
  return d_is_conjunctive;
89
}
90
91
1695
void EquivSygusInvarianceTest::init(
92
    TermDbSygus* tds, TypeNode tn, SynthConjecture* aconj, Node e, Node bvr)
93
{
94
  // compute the current examples
95
1695
  d_bvr = bvr;
96
1695
  Assert(tds != nullptr);
97
1695
  if (aconj != nullptr)
98
  {
99
1695
    ExampleEvalCache* eec = aconj->getExampleEvalCache(e);
100
1695
    if (eec != nullptr)
101
    {
102
      // get the result of evaluating bvr on the examples of enumerator e.
103
147
      eec->evaluateVec(bvr, d_exo, false);
104
147
      d_conj = aconj;
105
147
      d_enum = e;
106
    }
107
  }
108
1695
}
109
110
5799
bool EquivSygusInvarianceTest::invariant(TermDbSygus* tds, Node nvn, Node x)
111
{
112
11598
  TypeNode tn = nvn.getType();
113
11598
  Node nbv = tds->sygusToBuiltin(nvn, tn);
114
11598
  Node nbvr = tds->getExtRewriter()->extendedRewrite(nbv);
115
11598
  Trace("sygus-sb-mexp-debug") << "  min-exp check : " << nbv << " -> " << nbvr
116
5799
                               << std::endl;
117
5799
  bool exc_arg = false;
118
  // equivalent / singular up to normalization
119
5799
  if (nbvr == d_bvr)
120
  {
121
    // gives the same result : then the explanation for the child is irrelevant
122
95
    exc_arg = true;
123
190
    Trace("sygus-sb-mexp") << "sb-min-exp : " << tds->sygusToBuiltin(nvn)
124
95
                           << " is rewritten to " << nbvr;
125
190
    Trace("sygus-sb-mexp") << " regardless of the content of "
126
95
                           << tds->sygusToBuiltin(x) << std::endl;
127
  }
128
  else
129
  {
130
5704
    if (nbvr.isVar())
131
    {
132
360
      TypeNode xtn = x.getType();
133
180
      if (xtn == tn)
134
      {
135
236
        Node bx = tds->sygusToBuiltin(x, xtn);
136
118
        Assert(bx.getType() == nbvr.getType());
137
118
        if (nbvr == bx)
138
        {
139
236
          Trace("sygus-sb-mexp") << "sb-min-exp : " << tds->sygusToBuiltin(nvn)
140
118
                                 << " always rewrites to argument " << nbvr
141
118
                                 << std::endl;
142
          // rewrites to the variable : then the explanation of this is
143
          // irrelevant as well
144
118
          exc_arg = true;
145
118
          d_bvr = nbvr;
146
        }
147
      }
148
    }
149
  }
150
  // equivalent under examples
151
5799
  if (!exc_arg)
152
  {
153
5586
    if (!d_enum.isNull())
154
    {
155
454
      bool ex_equiv = true;
156
454
      ExampleEvalCache* eec = d_conj->getExampleEvalCache(d_enum);
157
454
      Assert(eec != nullptr);
158
481
      for (unsigned j = 0, esize = d_exo.size(); j < esize; j++)
159
      {
160
505
        Node nbvr_ex = eec->evaluate(nbvr, j);
161
478
        if (nbvr_ex != d_exo[j])
162
        {
163
451
          ex_equiv = false;
164
451
          break;
165
        }
166
      }
167
454
      if (ex_equiv)
168
      {
169
3
        Trace("sygus-sb-mexp") << "sb-min-exp : " << tds->sygusToBuiltin(nvn);
170
6
        Trace("sygus-sb-mexp")
171
3
            << " is the same w.r.t. examples regardless of the content of "
172
3
            << tds->sygusToBuiltin(x) << std::endl;
173
3
        exc_arg = true;
174
      }
175
    }
176
  }
177
11598
  return exc_arg;
178
}
179
180
bool DivByZeroSygusInvarianceTest::invariant(TermDbSygus* tds, Node nvn, Node x)
181
{
182
  TypeNode tn = nvn.getType();
183
  Node nbv = tds->sygusToBuiltin(nvn, tn);
184
  Node nbvr = tds->getExtRewriter()->extendedRewrite(nbv);
185
  if (tds->involvesDivByZero(nbvr))
186
  {
187
    Trace("sygus-sb-mexp") << "sb-min-exp : " << tds->sygusToBuiltin(nvn)
188
                           << " involves div-by-zero regardless of "
189
                           << tds->sygusToBuiltin(x) << std::endl;
190
    return true;
191
  }
192
  return false;
193
}
194
195
50
void NegContainsSygusInvarianceTest::init(Node e,
196
                                          std::vector<std::vector<Node> >& ex,
197
                                          std::vector<Node>& exo,
198
                                          std::vector<unsigned>& ncind)
199
{
200
50
  Assert(ex.size() == exo.size());
201
50
  d_enum = e;
202
50
  d_ex.insert(d_ex.end(), ex.begin(), ex.end());
203
50
  d_exo.insert(d_exo.end(), exo.begin(), exo.end());
204
50
  d_neg_con_indices.insert(d_neg_con_indices.end(), ncind.begin(), ncind.end());
205
50
}
206
207
65
bool NegContainsSygusInvarianceTest::invariant(TermDbSygus* tds,
208
                                               Node nvn,
209
                                               Node x)
210
{
211
65
  if (!d_enum.isNull())
212
  {
213
127
    TypeNode tn = nvn.getType();
214
127
    Node nbv = tds->sygusToBuiltin(nvn, tn);
215
127
    Node nbvr = tds->getExtRewriter()->extendedRewrite(nbv);
216
    // if for any of the examples, it is not contained, then we can exclude
217
5967
    for (unsigned i = 0; i < d_neg_con_indices.size(); i++)
218
    {
219
5905
      unsigned ii = d_neg_con_indices[i];
220
5905
      Assert(ii < d_exo.size());
221
11807
      Node nbvre = tds->evaluateBuiltin(tn, nbvr, d_ex[ii]);
222
11807
      Node out = d_exo[ii];
223
      Node cont =
224
11807
          NodeManager::currentNM()->mkNode(kind::STRING_STRCTN, out, nbvre);
225
5905
      Trace("sygus-pbe-cterm-debug") << "Check: " << cont << std::endl;
226
11807
      Node contr = Rewriter::rewrite(cont);
227
5905
      if (!contr.isConst())
228
      {
229
5905
        if (d_isUniversal)
230
        {
231
3
          return false;
232
        }
233
      }
234
      else if (contr.getConst<bool>() == d_isUniversal)
235
      {
236
        if (Trace.isOn("sygus-pbe-cterm"))
237
        {
238
          Trace("sygus-pbe-cterm")
239
              << "PBE-cterm : enumerator : do not consider ";
240
          Trace("sygus-pbe-cterm")
241
              << nbv << " for any " << tds->sygusToBuiltin(x) << " since "
242
              << std::endl;
243
          Trace("sygus-pbe-cterm") << "   PBE-cterm :    for input example : ";
244
          for (unsigned j = 0, size = d_ex[ii].size(); j < size; j++)
245
          {
246
            Trace("sygus-pbe-cterm") << d_ex[ii][j] << " ";
247
          }
248
          Trace("sygus-pbe-cterm") << std::endl;
249
          Trace("sygus-pbe-cterm")
250
              << "   PBE-cterm :     this rewrites to : " << nbvre << std::endl;
251
          Trace("sygus-pbe-cterm")
252
              << "   PBE-cterm : and is not in output : " << out << std::endl;
253
        }
254
        return !d_isUniversal;
255
      }
256
11804
      Trace("sygus-pbe-cterm-debug2")
257
5902
          << "...check failed, rewrites to : " << contr << std::endl;
258
    }
259
  }
260
62
  return d_isUniversal;
261
}
262
263
}  // namespace quantifiers
264
}  // namespace theory
265
28194
}  // namespace cvc5