GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/sygus/sygus_pbe.cpp Lines: 122 123 99.2 %
Date: 2021-09-07 Branches: 224 512 43.8 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Haniel Barbosa, 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
 * Utility for processing programming by examples synthesis conjectures.
14
 */
15
#include "theory/quantifiers/sygus/sygus_pbe.h"
16
17
#include "options/quantifiers_options.h"
18
#include "theory/datatypes/sygus_datatype_utils.h"
19
#include "theory/quantifiers/sygus/example_infer.h"
20
#include "theory/quantifiers/sygus/sygus_unif_io.h"
21
#include "theory/quantifiers/sygus/synth_conjecture.h"
22
#include "theory/quantifiers/sygus/term_database_sygus.h"
23
#include "theory/quantifiers/term_util.h"
24
#include "util/random.h"
25
26
using namespace cvc5;
27
using namespace cvc5::kind;
28
29
namespace cvc5 {
30
namespace theory {
31
namespace quantifiers {
32
33
1191
SygusPbe::SygusPbe(QuantifiersState& qs,
34
                   QuantifiersInferenceManager& qim,
35
                   TermDbSygus* tds,
36
1191
                   SynthConjecture* p)
37
1191
    : SygusModule(qs, qim, tds, p)
38
{
39
1191
  d_true = NodeManager::currentNM()->mkConst(true);
40
1191
  d_false = NodeManager::currentNM()->mkConst(false);
41
1191
  d_is_pbe = false;
42
1191
}
43
44
2378
SygusPbe::~SygusPbe() {}
45
46
241
bool SygusPbe::initialize(Node conj,
47
                          Node n,
48
                          const std::vector<Node>& candidates)
49
{
50
241
  Trace("sygus-pbe") << "Initialize PBE : " << n << std::endl;
51
241
  NodeManager* nm = NodeManager::currentNM();
52
53
241
  if (!options::sygusUnifPbe())
54
  {
55
    // we are not doing unification
56
31
    return false;
57
  }
58
59
  // check if all candidates are valid examples
60
210
  ExampleInfer* ei = d_parent->getExampleInfer();
61
210
  d_is_pbe = true;
62
257
  for (const Node& c : candidates)
63
  {
64
    // if it has no examples or the output of the examples is invalid
65
213
    if (ei->getNumExamples(c) == 0 || !ei->hasExamplesOut(c))
66
    {
67
166
      d_is_pbe = false;
68
166
      return false;
69
    }
70
  }
71
91
  for (const Node& c : candidates)
72
  {
73
47
    Assert(ei->hasExamples(c));
74
47
    d_sygus_unif[c].reset(new SygusUnifIo(d_parent));
75
94
    Trace("sygus-pbe") << "Initialize unif utility for " << c << "..."
76
47
                       << std::endl;
77
94
    std::map<Node, std::vector<Node>> strategy_lemmas;
78
94
    d_sygus_unif[c]->initializeCandidate(
79
47
        d_tds, c, d_candidate_to_enum[c], strategy_lemmas);
80
47
    Assert(!d_candidate_to_enum[c].empty());
81
94
    Trace("sygus-pbe") << "Initialize " << d_candidate_to_enum[c].size()
82
47
                       << " enumerators for " << c << "..." << std::endl;
83
    // collect list per type of strategy points with strategy lemmas
84
94
    std::map<TypeNode, std::vector<Node>> tn_to_strategy_pt;
85
101
    for (const std::pair<const Node, std::vector<Node>>& p : strategy_lemmas)
86
    {
87
108
      TypeNode tnsp = p.first.getType();
88
54
      tn_to_strategy_pt[tnsp].push_back(p.first);
89
    }
90
    // initialize the enumerators
91
110
    for (const Node& e : d_candidate_to_enum[c])
92
    {
93
126
      TypeNode etn = e.getType();
94
63
      d_tds->registerEnumerator(e, c, d_parent, ROLE_ENUM_POOL);
95
63
      d_enum_to_candidate[e] = c;
96
126
      TNode te = e;
97
      // initialize static symmetry breaking lemmas for it
98
      // we register only one "master" enumerator per type
99
      // thus, the strategy lemmas (which are for individual strategy points)
100
      // are applicable (disjunctively) to the master enumerator
101
      std::map<TypeNode, std::vector<Node>>::iterator itt =
102
63
          tn_to_strategy_pt.find(etn);
103
63
      if (itt != tn_to_strategy_pt.end())
104
      {
105
72
        std::vector<Node> disj;
106
88
        for (const Node& sp : itt->second)
107
        {
108
          std::map<Node, std::vector<Node>>::iterator itsl =
109
52
              strategy_lemmas.find(sp);
110
52
          Assert(itsl != strategy_lemmas.end());
111
52
          if (!itsl->second.empty())
112
          {
113
104
            TNode tsp = sp;
114
98
            Node lem = itsl->second.size() == 1 ? itsl->second[0]
115
150
                                                : nm->mkNode(AND, itsl->second);
116
52
            if (tsp != te)
117
            {
118
16
              lem = lem.substitute(tsp, te);
119
            }
120
52
            if (std::find(disj.begin(), disj.end(), lem) == disj.end())
121
            {
122
38
              disj.push_back(lem);
123
            }
124
          }
125
        }
126
        // add its active guard
127
72
        Node ag = d_tds->getActiveGuardForEnumerator(e);
128
36
        Assert(!ag.isNull());
129
36
        disj.push_back(ag.negate());
130
72
        Node lem = disj.size() == 1 ? disj[0] : nm->mkNode(OR, disj);
131
        // Apply extended rewriting on the lemma. This helps utilities like
132
        // SygusEnumerator more easily recognize the shape of this lemma, e.g.
133
        // ( ~is-ite(x) or ( ~is-ite(x) ^ P ) ) --> ~is-ite(x).
134
36
        lem = d_tds->getExtRewriter()->extendedRewrite(lem);
135
72
        Trace("sygus-pbe") << "  static redundant op lemma : " << lem
136
36
                           << std::endl;
137
        // Register as a symmetry breaking lemma with the term database.
138
        // This will either be processed via a lemma on the output channel
139
        // of the sygus extension of the datatypes solver, or internally
140
        // encoded as a constraint to an active enumerator.
141
36
        d_tds->registerSymBreakLemma(e, lem, etn, 0, false);
142
      }
143
    }
144
  }
145
44
  return true;
146
}
147
148
// ------------------------------------------- solution construction from enumeration
149
150
3744
void SygusPbe::getTermList(const std::vector<Node>& candidates,
151
                           std::vector<Node>& terms)
152
{
153
7582
  for( unsigned i=0; i<candidates.size(); i++ ){
154
7676
    Node v = candidates[i];
155
    std::map<Node, std::vector<Node> >::iterator it =
156
3838
        d_candidate_to_enum.find(v);
157
3838
    if (it != d_candidate_to_enum.end())
158
    {
159
3838
      terms.insert(terms.end(), it->second.begin(), it->second.end());
160
    }
161
  }
162
3744
}
163
164
3744
bool SygusPbe::allowPartialModel() { return !options::sygusPbeMultiFair(); }
165
166
518
bool SygusPbe::constructCandidates(const std::vector<Node>& enums,
167
                                   const std::vector<Node>& enum_values,
168
                                   const std::vector<Node>& candidates,
169
                                   std::vector<Node>& candidate_values)
170
{
171
518
  Assert(enums.size() == enum_values.size());
172
518
  if( !enums.empty() ){
173
518
    unsigned min_term_size = 0;
174
518
    Trace("sygus-pbe-enum") << "Register new enumerated values : " << std::endl;
175
1036
    std::vector<unsigned> szs;
176
1246
    for (unsigned i = 0, esize = enums.size(); i < esize; i++)
177
    {
178
728
      Trace("sygus-pbe-enum") << "  " << enums[i] << " -> ";
179
728
      TermDbSygus::toStreamSygus("sygus-pbe-enum", enum_values[i]);
180
728
      Trace("sygus-pbe-enum") << std::endl;
181
728
      if (!enum_values[i].isNull())
182
      {
183
584
        unsigned sz = datatypes::utils::getSygusTermSize(enum_values[i]);
184
584
        szs.push_back(sz);
185
584
        if (i == 0 || sz < min_term_size)
186
        {
187
487
          min_term_size = sz;
188
        }
189
      }
190
      else
191
      {
192
144
        szs.push_back(0);
193
      }
194
    }
195
    // Assume two enumerators of types T1 and T2.
196
    // If options::sygusPbeMultiFair() is true,
197
    // we ensure that all values of type T1 and size n are enumerated before
198
    // any term of type T2 of size n+d, and vice versa, where d is
199
    // set by options::sygusPbeMultiFairDiff(). If d is zero, then our
200
    // enumeration is such that all terms of T1 or T2 of size n are considered
201
    // before any term of size n+1.
202
518
    int diffAllow = options::sygusPbeMultiFairDiff();
203
1036
    std::vector<unsigned> enum_consider;
204
1246
    for (unsigned i = 0, esize = enums.size(); i < esize; i++)
205
    {
206
728
      if (!enum_values[i].isNull())
207
      {
208
584
        Assert(szs[i] >= min_term_size);
209
584
        int diff = szs[i] - min_term_size;
210
584
        if (!options::sygusPbeMultiFair() || diff <= diffAllow)
211
        {
212
584
          enum_consider.push_back(i);
213
        }
214
      }
215
    }
216
217
    // only consider the enumerators that are at minimum size (for fairness)
218
518
    Trace("sygus-pbe-enum") << "...register " << enum_consider.size() << " / " << enums.size() << std::endl;
219
518
    NodeManager* nm = NodeManager::currentNM();
220
1102
    for (unsigned i = 0, ecsize = enum_consider.size(); i < ecsize; i++)
221
    {
222
584
      unsigned j = enum_consider[i];
223
1168
      Node e = enums[j];
224
1168
      Node v = enum_values[j];
225
584
      Assert(d_enum_to_candidate.find(e) != d_enum_to_candidate.end());
226
1168
      Node c = d_enum_to_candidate[e];
227
1168
      std::vector<Node> enum_lems;
228
584
      d_sygus_unif[c]->notifyEnumeration(e, v, enum_lems);
229
584
      if (!enum_lems.empty())
230
      {
231
        // the lemmas must be guarded by the active guard of the enumerator
232
14
        Node g = d_tds->getActiveGuardForEnumerator(e);
233
7
        Assert(!g.isNull());
234
14
        for (unsigned k = 0, size = enum_lems.size(); k < size; k++)
235
        {
236
14
          Node lem = nm->mkNode(OR, g.negate(), enum_lems[k]);
237
7
          d_qim.addPendingLemma(lem,
238
                                InferenceId::QUANTIFIERS_SYGUS_PBE_EXCLUDE);
239
        }
240
      }
241
    }
242
  }
243
569
  for( unsigned i=0; i<candidates.size(); i++ ){
244
577
    Node c = candidates[i];
245
    //build decision tree for candidate
246
577
    std::vector<Node> sol;
247
577
    std::vector<Node> lems;
248
526
    bool solSuccess = d_sygus_unif[c]->constructSolution(sol, lems);
249
526
    for (const Node& lem : lems)
250
    {
251
      d_qim.addPendingLemma(lem,
252
                            InferenceId::QUANTIFIERS_SYGUS_PBE_CONSTRUCT_SOL);
253
    }
254
526
    if (solSuccess)
255
    {
256
51
      Assert(sol.size() == 1);
257
51
      candidate_values.push_back(sol[0]);
258
    }
259
    else
260
    {
261
475
      return false;
262
    }
263
  }
264
43
  return true;
265
}
266
267
}
268
}
269
29502
}  // namespace cvc5