GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/ematching/instantiation_engine.cpp Lines: 115 128 89.8 %
Date: 2021-09-07 Branches: 176 362 48.6 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Morgan Deters, Tim King
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 instantiation engine class
14
 */
15
16
#include "theory/quantifiers/ematching/instantiation_engine.h"
17
18
#include "options/quantifiers_options.h"
19
#include "theory/quantifiers/ematching/inst_strategy_e_matching.h"
20
#include "theory/quantifiers/ematching/inst_strategy_e_matching_user.h"
21
#include "theory/quantifiers/ematching/trigger.h"
22
#include "theory/quantifiers/first_order_model.h"
23
#include "theory/quantifiers/quantifiers_attributes.h"
24
#include "theory/quantifiers/term_database.h"
25
#include "theory/quantifiers/term_util.h"
26
27
using namespace cvc5::kind;
28
using namespace cvc5::context;
29
using namespace cvc5::theory::quantifiers::inst;
30
31
namespace cvc5 {
32
namespace theory {
33
namespace quantifiers {
34
35
6573
InstantiationEngine::InstantiationEngine(QuantifiersState& qs,
36
                                         QuantifiersInferenceManager& qim,
37
                                         QuantifiersRegistry& qr,
38
6573
                                         TermRegistry& tr)
39
    : QuantifiersModule(qs, qim, qr, tr),
40
      d_instStrategies(),
41
      d_isup(),
42
      d_i_ag(),
43
      d_quants(),
44
      d_trdb(qs, qim, qr, tr),
45
6573
      d_quant_rel(nullptr)
46
{
47
6573
  if (options::relevantTriggers())
48
  {
49
4
    d_quant_rel.reset(new quantifiers::QuantRelevance);
50
  }
51
6573
  if (options::eMatching()) {
52
    // these are the instantiation strategies for E-matching
53
    // user-provided patterns
54
6571
    if (options::userPatternsQuant() != options::UserPatMode::IGNORE)
55
    {
56
6571
      d_isup.reset(new InstStrategyUserPatterns(d_trdb, qs, qim, qr, tr));
57
6571
      d_instStrategies.push_back(d_isup.get());
58
    }
59
60
    // auto-generated patterns
61
13142
    d_i_ag.reset(new InstStrategyAutoGenTriggers(
62
6571
        d_trdb, qs, qim, qr, tr, d_quant_rel.get()));
63
6571
    d_instStrategies.push_back(d_i_ag.get());
64
  }
65
6573
}
66
67
13140
InstantiationEngine::~InstantiationEngine() {}
68
69
7897
void InstantiationEngine::presolve() {
70
23687
  for( unsigned i=0; i<d_instStrategies.size(); ++i ){
71
15790
    d_instStrategies[i]->presolve();
72
  }
73
7897
}
74
75
2268
void InstantiationEngine::doInstantiationRound( Theory::Effort effort ){
76
2268
  size_t lastWaiting = d_qim.numPendingLemmas();
77
  //iterate over an internal effort level e
78
2268
  int e = 0;
79
2268
  int eLimit = effort==Theory::EFFORT_LAST_CALL ? 10 : 2;
80
2268
  bool finished = false;
81
  //while unfinished, try effort level=0,1,2....
82
11340
  while( !finished && e<=eLimit ){
83
4536
    Debug("inst-engine") << "IE: Prepare instantiation (" << e << ")." << std::endl;
84
4536
    finished = true;
85
    //instantiate each quantifier
86
88284
    for( unsigned i=0; i<d_quants.size(); i++ ){
87
167496
      Node q = d_quants[i];
88
83748
      Debug("inst-engine-debug") << "IE: Instantiate " << q << "..." << std::endl;
89
      //int e_use = d_quantEngine->getRelevance( q )==-1 ? e - 1 : e;
90
83748
      int e_use = e;
91
83748
      if( e_use>=0 ){
92
83748
        Trace("inst-engine-debug") << "inst-engine : " << q << std::endl;
93
        //check each instantiation strategy
94
251244
        for( unsigned j=0; j<d_instStrategies.size(); j++ ){
95
167496
          InstStrategy* is = d_instStrategies[j];
96
167496
          Trace("inst-engine-debug") << "Do " << is->identify() << " " << e_use << std::endl;
97
167496
          InstStrategyStatus quantStatus = is->process(q, effort, e_use);
98
334992
          Trace("inst-engine-debug")
99
167496
              << " -> unfinished= "
100
334992
              << (quantStatus == InstStrategyStatus::STATUS_UNFINISHED)
101
167496
              << ", conflict=" << d_qstate.isInConflict() << std::endl;
102
167496
          if (d_qstate.isInConflict())
103
          {
104
            return;
105
          }
106
167496
          else if (quantStatus == InstStrategyStatus::STATUS_UNFINISHED)
107
          {
108
70294
            finished = false;
109
          }
110
        }
111
      }
112
    }
113
    //do not consider another level if already added lemma at this level
114
4536
    if (d_qim.numPendingLemmas() > lastWaiting)
115
    {
116
1187
      finished = true;
117
    }
118
4536
    e++;
119
  }
120
}
121
122
67641
bool InstantiationEngine::needsCheck( Theory::Effort e ){
123
67641
  return d_qstate.getInstWhenNeedsCheck(e);
124
}
125
126
24236
void InstantiationEngine::reset_round( Theory::Effort e ){
127
  //if not, proceed to instantiation round
128
  //reset the instantiation strategies
129
72702
  for( unsigned i=0; i<d_instStrategies.size(); ++i ){
130
48466
    InstStrategy* is = d_instStrategies[i];
131
48466
    is->processResetInstantiationRound( e );
132
  }
133
24236
}
134
135
37480
void InstantiationEngine::check(Theory::Effort e, QEffort quant_e)
136
{
137
50331
  CodeTimer codeTimer(d_qstate.getStats().d_ematching_time);
138
37480
  if (quant_e != QEFFORT_STANDARD)
139
  {
140
24629
    return;
141
  }
142
12851
  double clSet = 0;
143
12851
  if (Trace.isOn("inst-engine"))
144
  {
145
    clSet = double(clock()) / double(CLOCKS_PER_SEC);
146
    Trace("inst-engine") << "---Instantiation Engine Round, effort = " << e
147
                         << "---" << std::endl;
148
  }
149
  // collect all active quantified formulas belonging to this
150
12851
  bool quantActive = false;
151
12851
  d_quants.clear();
152
12851
  FirstOrderModel* m = d_treg.getModel();
153
12851
  size_t nquant = m->getNumAssertedQuantifiers();
154
71487
  for (size_t i = 0; i < nquant; i++)
155
  {
156
117272
    Node q = m->getAssertedQuantifier(i, true);
157
58636
    if (shouldProcess(q) && m->isQuantifierActive(q))
158
    {
159
41874
      quantActive = true;
160
41874
      d_quants.push_back(q);
161
    }
162
  }
163
25702
  Trace("inst-engine-debug")
164
12851
      << "InstEngine: check: # asserted quantifiers " << d_quants.size() << "/";
165
12851
  Trace("inst-engine-debug") << nquant << " " << quantActive << std::endl;
166
12851
  if (quantActive)
167
  {
168
2268
    size_t lastWaiting = d_qim.numPendingLemmas();
169
2268
    doInstantiationRound(e);
170
2268
    if (d_qstate.isInConflict())
171
    {
172
      Assert(d_qim.numPendingLemmas() > lastWaiting);
173
      Trace("inst-engine") << "Conflict, added lemmas = "
174
                           << (d_qim.numPendingLemmas() - lastWaiting)
175
                           << std::endl;
176
    }
177
2268
    else if (d_qim.hasPendingLemma())
178
    {
179
2374
      Trace("inst-engine") << "Added lemmas = "
180
2374
                           << (d_qim.numPendingLemmas() - lastWaiting)
181
1187
                           << std::endl;
182
    }
183
  }
184
  else
185
  {
186
10583
    d_quants.clear();
187
  }
188
12851
  if (Trace.isOn("inst-engine"))
189
  {
190
    double clSet2 = double(clock()) / double(CLOCKS_PER_SEC);
191
    Trace("inst-engine") << "Finished instantiation engine, time = "
192
                         << (clSet2 - clSet) << std::endl;
193
  }
194
}
195
196
575
bool InstantiationEngine::checkCompleteFor( Node q ) {
197
  //TODO?
198
575
  return false;
199
}
200
201
22808
void InstantiationEngine::checkOwnership(Node q)
202
{
203
45616
  if (options::userPatternsQuant() == options::UserPatMode::STRICT
204
22808
      && q.getNumChildren() == 3)
205
  {
206
    //if strict triggers, take ownership of this quantified formula
207
    if (QuantAttributes::hasPattern(q))
208
    {
209
      d_qreg.setOwner(q, this, 1);
210
    }
211
  }
212
22808
}
213
214
22808
void InstantiationEngine::registerQuantifier(Node q)
215
{
216
22808
  if (!shouldProcess(q))
217
  {
218
3382
    return;
219
  }
220
19426
  if (d_quant_rel)
221
  {
222
344
    d_quant_rel->registerQuantifier(q);
223
  }
224
  // take into account user patterns
225
19426
  if (q.getNumChildren() == 3)
226
  {
227
7906
    Node subsPat = d_qreg.substituteBoundVariablesToInstConstants(q[2], q);
228
    // add patterns
229
8338
    for (const Node& p : subsPat)
230
    {
231
4385
      if (p.getKind() == INST_PATTERN)
232
      {
233
3884
        addUserPattern(q, p);
234
      }
235
501
      else if (p.getKind() == INST_NO_PATTERN)
236
      {
237
8
        addUserNoPattern(q, p);
238
      }
239
    }
240
  }
241
}
242
243
3884
void InstantiationEngine::addUserPattern(Node q, Node pat) {
244
3884
  if (d_isup) {
245
3884
    d_isup->addUserPattern(q, pat);
246
  }
247
3884
}
248
249
8
void InstantiationEngine::addUserNoPattern(Node q, Node pat) {
250
8
  if (d_i_ag) {
251
8
    d_i_ag->addUserNoPattern(q, pat);
252
  }
253
8
}
254
255
81444
bool InstantiationEngine::shouldProcess(Node q)
256
{
257
81444
  if (!d_qreg.hasOwnership(q, this))
258
  {
259
16700
    return false;
260
  }
261
  // also ignore internal quantifiers
262
64744
  QuantAttributes& qattr = d_qreg.getQuantAttributes();
263
64744
  if (qattr.isInternal(q))
264
  {
265
3402
    return false;
266
  }
267
61342
  return true;
268
}
269
270
}  // namespace quantifiers
271
}  // namespace theory
272
29502
}  // namespace cvc5