GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/sygus/sygus_pbe.cpp Lines: 119 119 100.0 %
Date: 2021-05-22 Branches: 223 512 43.6 %

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