summaryrefslogtreecommitdiff
path: root/src/theory/quantifiers/inst_strategy_enumerative.cpp
blob: 7a2f62864f9e40cbab36fd9668cddbd3a6d1ad89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*********************                                                        */
/*! \file inst_strategy_enumerative.cpp
 ** \verbatim
 ** Top contributors (to current version):
 **   Andrew Reynolds, Morgan Deters
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2019 by the authors listed in the file AUTHORS
 ** in the top-level source directory) and their institutional affiliations.
 ** All rights reserved.  See the file COPYING in the top-level source
 ** directory for licensing information.\endverbatim
 **
 ** \brief Implementation of an enumerative instantiation strategy.
 **/

#include "theory/quantifiers/inst_strategy_enumerative.h"

#include "options/quantifiers_options.h"
#include "theory/quantifiers/instantiate.h"
#include "theory/quantifiers/relevant_domain.h"
#include "theory/quantifiers/term_database.h"
#include "theory/quantifiers/term_util.h"

namespace CVC4 {

using namespace kind;
using namespace context;

namespace theory {

using namespace inst;

namespace quantifiers {

InstStrategyEnum::InstStrategyEnum(QuantifiersEngine* qe)
    : QuantifiersModule(qe)
{
}

bool InstStrategyEnum::needsCheck(Theory::Effort e)
{
  if (options::fullSaturateInterleave())
  {
    if (d_quantEngine->getInstWhenNeedsCheck(e))
    {
      return true;
    }
  }
  if (options::fullSaturateQuant())
  {
    if (e >= Theory::EFFORT_LAST_CALL)
    {
      return true;
    }
  }
  return false;
}

void InstStrategyEnum::reset_round(Theory::Effort e) {}
void InstStrategyEnum::check(Theory::Effort e, QEffort quant_e)
{
  bool doCheck = false;
  bool fullEffort = false;
  if (options::fullSaturateInterleave())
  {
    // we only add when interleaved with other strategies
    doCheck = quant_e == QEFFORT_STANDARD && d_quantEngine->hasAddedLemma();
  }
  if (options::fullSaturateQuant() && !doCheck)
  {
    doCheck = quant_e == QEFFORT_LAST_CALL;
    fullEffort = !d_quantEngine->hasAddedLemma();
  }
  if (!doCheck)
  {
    return;
  }
  double clSet = 0;
  if (Trace.isOn("fs-engine"))
  {
    clSet = double(clock()) / double(CLOCKS_PER_SEC);
    Trace("fs-engine") << "---Full Saturation Round, effort = " << e << "---"
                       << std::endl;
  }
  unsigned rstart = options::fullSaturateQuantRd() ? 0 : 1;
  unsigned rend = fullEffort ? 1 : rstart;
  unsigned addedLemmas = 0;
  // First try in relevant domain of all quantified formulas, if no
  // instantiations exist, try arbitrary ground terms.
  // Notice that this stratification of effort levels makes it so that some
  // quantified formulas may not be instantiated (if they have no instances
  // at effort level r=0 but another quantified formula does). We prefer
  // this stratification since effort level r=1 may be highly expensive in the
  // case where we have a quantified formula with many entailed instances.
  FirstOrderModel* fm = d_quantEngine->getModel();
  RelevantDomain* rd = d_quantEngine->getRelevantDomain();
  unsigned nquant = fm->getNumAssertedQuantifiers();
  std::map<Node, bool> alreadyProc;
  for (unsigned r = rstart; r <= rend; r++)
  {
    if (rd || r > 0)
    {
      if (r == 0)
      {
        Trace("inst-alg") << "-> Relevant domain instantiate..." << std::endl;
        Trace("inst-alg-debug") << "Compute relevant domain..." << std::endl;
        rd->compute();
        Trace("inst-alg-debug") << "...finished" << std::endl;
      }
      else
      {
        Trace("inst-alg") << "-> Ground term instantiate..." << std::endl;
      }
      for (unsigned i = 0; i < nquant; i++)
      {
        Node q = fm->getAssertedQuantifier(i, true);
        bool doProcess = d_quantEngine->hasOwnership(q, this)
                         && fm->isQuantifierActive(q)
                         && alreadyProc.find(q) == alreadyProc.end();
        if (doProcess)
        {
          if (process(q, fullEffort, r == 0))
          {
            // don't need to mark this if we are not stratifying
            if (!options::fullSaturateStratify())
            {
              alreadyProc[q] = true;
            }
            // added lemma
            addedLemmas++;
            if (d_quantEngine->inConflict())
            {
              break;
            }
          }
        }
      }
      if (d_quantEngine->inConflict()
          || (addedLemmas > 0 && options::fullSaturateStratify()))
      {
        // we break if we are in conflict, or if we added any lemma at this
        // effort level and we stratify effort levels.
        break;
      }
    }
  }
  if (Trace.isOn("fs-engine"))
  {
    Trace("fs-engine") << "Added lemmas = " << addedLemmas << std::endl;
    double clSet2 = double(clock()) / double(CLOCKS_PER_SEC);
    Trace("fs-engine") << "Finished full saturation engine, time = "
                       << (clSet2 - clSet) << std::endl;
  }
}

bool InstStrategyEnum::process(Node f, bool fullEffort, bool isRd)
{
  // ignore if constant true (rare case of non-standard quantifier whose body is
  // rewritten to true)
  if (f[1].isConst() && f[1].getConst<bool>())
  {
    return false;
  }
  RelevantDomain* rd = d_quantEngine->getRelevantDomain();
  unsigned final_max_i = 0;
  std::vector<unsigned> maxs;
  std::vector<bool> max_zero;
  bool has_zero = false;
  std::map<TypeNode, std::vector<Node> > term_db_list;
  std::vector<TypeNode> ftypes;
  TermDb* tdb = d_quantEngine->getTermDatabase();
  EqualityQuery* qy = d_quantEngine->getEqualityQuery();
  // iterate over substitutions for variables
  for (unsigned i = 0; i < f[0].getNumChildren(); i++)
  {
    TypeNode tn = f[0][i].getType();
    ftypes.push_back(tn);
    unsigned ts;
    if (isRd)
    {
      ts = rd->getRDomain(f, i)->d_terms.size();
    }
    else
    {
      ts = tdb->getNumTypeGroundTerms(tn);
      std::map<TypeNode, std::vector<Node> >::iterator ittd =
          term_db_list.find(tn);
      if (ittd == term_db_list.end())
      {
        std::map<Node, Node> reps_found;
        for (unsigned j = 0; j < ts; j++)
        {
          Node gt = tdb->getTypeGroundTerm(ftypes[i], j);
          if (!options::cbqi() || !quantifiers::TermUtil::hasInstConstAttr(gt))
          {
            Node rep = qy->getRepresentative(gt);
            if (reps_found.find(rep) == reps_found.end())
            {
              reps_found[rep] = gt;
              term_db_list[tn].push_back(gt);
            }
          }
        }
        ts = term_db_list[tn].size();
      }
      else
      {
        ts = ittd->second.size();
      }
    }
    // consider a default value if at full effort
    max_zero.push_back(fullEffort && ts == 0);
    ts = (fullEffort && ts == 0) ? 1 : ts;
    Trace("inst-alg-rd") << "Variable " << i << " has " << ts
                         << " in relevant domain." << std::endl;
    if (ts == 0)
    {
      has_zero = true;
      break;
    }
    maxs.push_back(ts);
    if (ts > final_max_i)
    {
      final_max_i = ts;
    }
  }
  if (!has_zero)
  {
    Trace("inst-alg-rd") << "Will do " << final_max_i
                         << " stages of instantiation." << std::endl;
    unsigned max_i = 0;
    bool success;
    Instantiate* ie = d_quantEngine->getInstantiate();
    while (max_i <= final_max_i)
    {
      Trace("inst-alg-rd") << "Try stage " << max_i << "..." << std::endl;
      std::vector<unsigned> childIndex;
      int index = 0;
      do
      {
        while (index >= 0 && index < (int)f[0].getNumChildren())
        {
          if (index == static_cast<int>(childIndex.size()))
          {
            childIndex.push_back(-1);
          }
          else
          {
            Assert(index == static_cast<int>(childIndex.size()) - 1);
            unsigned nv = childIndex[index] + 1;
            if (nv < maxs[index] && nv <= max_i)
            {
              childIndex[index] = nv;
              index++;
            }
            else
            {
              childIndex.pop_back();
              index--;
            }
          }
        }
        success = index >= 0;
        if (success)
        {
          if (Trace.isOn("inst-alg-rd"))
          {
            Trace("inst-alg-rd") << "Try instantiation { ";
            for (unsigned i : childIndex)
            {
              Trace("inst-alg-rd") << i << " ";
            }
            Trace("inst-alg-rd") << "}" << std::endl;
          }
          // try instantiation
          std::vector<Node> terms;
          for (unsigned i = 0, nchild = f[0].getNumChildren(); i < nchild; i++)
          {
            if (max_zero[i])
            {
              // no terms available, will report incomplete instantiation
              terms.push_back(Node::null());
              Trace("inst-alg-rd") << "  null" << std::endl;
            }
            else if (isRd)
            {
              terms.push_back(rd->getRDomain(f, i)->d_terms[childIndex[i]]);
              Trace("inst-alg-rd")
                  << "  " << rd->getRDomain(f, i)->d_terms[childIndex[i]]
                  << std::endl;
            }
            else
            {
              Assert(childIndex[i] < term_db_list[ftypes[i]].size());
              terms.push_back(term_db_list[ftypes[i]][childIndex[i]]);
              Trace("inst-alg-rd")
                  << "  " << term_db_list[ftypes[i]][childIndex[i]]
                  << std::endl;
            }
          }
          if (ie->addInstantiation(f, terms))
          {
            Trace("inst-alg-rd") << "Success!" << std::endl;
            ++(d_quantEngine->d_statistics.d_instantiations_guess);
            return true;
          }
          else
          {
            index--;
          }
        }
      } while (success);
      max_i++;
    }
  }
  // TODO : term enumerator instantiation?
  return false;
}

} /* CVC4::theory::quantifiers namespace */
} /* CVC4::theory namespace */
} /* CVC4 namespace */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback