GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/sygus/sygus_pbe.cpp Lines: 119 119 100.0 %
Date: 2021-08-01 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/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
1151
SygusPbe::SygusPbe(QuantifiersInferenceManager& qim,
34
                   TermDbSygus* tds,
35
1151
                   SynthConjecture* p)
36
1151
    : SygusModule(qim, tds, p)
37
{
38
1151
  d_true = NodeManager::currentNM()->mkConst(true);
39
1151
  d_false = NodeManager::currentNM()->mkConst(false);
40
1151
  d_is_pbe = false;
41
1151
}
42
43
2302
SygusPbe::~SygusPbe() {}
44
45
240
bool SygusPbe::initialize(Node conj,
46
                          Node n,
47
                          const std::vector<Node>& candidates,
48
                          std::vector<Node>& lemmas)
49
{
50
240
  Trace("sygus-pbe") << "Initialize PBE : " << n << std::endl;
51
240
  NodeManager* nm = NodeManager::currentNM();
52
53
480
  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
209
  ExampleInfer* ei = d_parent->getExampleInfer();
61
209
  d_is_pbe = true;
62
256
  for (const Node& c : candidates)
63
  {
64
    // if it has no examples or the output of the examples is invalid
65
212
    if (ei->getNumExamples(c) == 0 || !ei->hasExamplesOut(c))
66
    {
67
165
      d_is_pbe = false;
68
165
      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
3720
void SygusPbe::getTermList(const std::vector<Node>& candidates,
151
                           std::vector<Node>& terms)
152
{
153
7534
  for( unsigned i=0; i<candidates.size(); i++ ){
154
7628
    Node v = candidates[i];
155
    std::map<Node, std::vector<Node> >::iterator it =
156
3814
        d_candidate_to_enum.find(v);
157
3814
    if (it != d_candidate_to_enum.end())
158
    {
159
3814
      terms.insert(terms.end(), it->second.begin(), it->second.end());
160
    }
161
  }
162
3720
}
163
164
7985
bool SygusPbe::allowPartialModel() { return !options::sygusPbeMultiFair(); }
165
166
489
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
                                   std::vector<Node>& lems)
171
{
172
489
  Assert(enums.size() == enum_values.size());
173
489
  if( !enums.empty() ){
174
489
    unsigned min_term_size = 0;
175
489
    Trace("sygus-pbe-enum") << "Register new enumerated values : " << std::endl;
176
978
    std::vector<unsigned> szs;
177
1160
    for (unsigned i = 0, esize = enums.size(); i < esize; i++)
178
    {
179
671
      Trace("sygus-pbe-enum") << "  " << enums[i] << " -> ";
180
671
      TermDbSygus::toStreamSygus("sygus-pbe-enum", enum_values[i]);
181
671
      Trace("sygus-pbe-enum") << std::endl;
182
671
      if (!enum_values[i].isNull())
183
      {
184
545
        unsigned sz = datatypes::utils::getSygusTermSize(enum_values[i]);
185
545
        szs.push_back(sz);
186
545
        if (i == 0 || sz < min_term_size)
187
        {
188
468
          min_term_size = sz;
189
        }
190
      }
191
      else
192
      {
193
126
        szs.push_back(0);
194
      }
195
    }
196
    // Assume two enumerators of types T1 and T2.
197
    // If options::sygusPbeMultiFair() is true,
198
    // we ensure that all values of type T1 and size n are enumerated before
199
    // any term of type T2 of size n+d, and vice versa, where d is
200
    // set by options::sygusPbeMultiFairDiff(). If d is zero, then our
201
    // enumeration is such that all terms of T1 or T2 of size n are considered
202
    // before any term of size n+1.
203
978
    int diffAllow = options::sygusPbeMultiFairDiff();
204
978
    std::vector<unsigned> enum_consider;
205
1160
    for (unsigned i = 0, esize = enums.size(); i < esize; i++)
206
    {
207
671
      if (!enum_values[i].isNull())
208
      {
209
545
        Assert(szs[i] >= min_term_size);
210
545
        int diff = szs[i] - min_term_size;
211
545
        if (!options::sygusPbeMultiFair() || diff <= diffAllow)
212
        {
213
545
          enum_consider.push_back(i);
214
        }
215
      }
216
    }
217
218
    // only consider the enumerators that are at minimum size (for fairness)
219
489
    Trace("sygus-pbe-enum") << "...register " << enum_consider.size() << " / " << enums.size() << std::endl;
220
489
    NodeManager* nm = NodeManager::currentNM();
221
1034
    for (unsigned i = 0, ecsize = enum_consider.size(); i < ecsize; i++)
222
    {
223
545
      unsigned j = enum_consider[i];
224
1090
      Node e = enums[j];
225
1090
      Node v = enum_values[j];
226
545
      Assert(d_enum_to_candidate.find(e) != d_enum_to_candidate.end());
227
1090
      Node c = d_enum_to_candidate[e];
228
1090
      std::vector<Node> enum_lems;
229
545
      d_sygus_unif[c]->notifyEnumeration(e, v, enum_lems);
230
545
      if (!enum_lems.empty())
231
      {
232
        // the lemmas must be guarded by the active guard of the enumerator
233
14
        Node g = d_tds->getActiveGuardForEnumerator(e);
234
7
        Assert(!g.isNull());
235
14
        for (unsigned k = 0, size = enum_lems.size(); k < size; k++)
236
        {
237
7
          enum_lems[k] = nm->mkNode(OR, g.negate(), enum_lems[k]);
238
        }
239
7
        lems.insert(lems.end(), enum_lems.begin(), enum_lems.end());
240
      }
241
    }
242
  }
243
540
  for( unsigned i=0; i<candidates.size(); i++ ){
244
548
    Node c = candidates[i];
245
    //build decision tree for candidate
246
548
    std::vector<Node> sol;
247
497
    if (d_sygus_unif[c]->constructSolution(sol, lems))
248
    {
249
51
      Assert(sol.size() == 1);
250
51
      candidate_values.push_back(sol[0]);
251
    }
252
    else
253
    {
254
446
      return false;
255
    }
256
  }
257
43
  return true;
258
}
259
260
}
261
}
262
29280
}  // namespace cvc5