GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/fmf/model_engine.cpp Lines: 137 195 70.3 %
Date: 2021-09-29 Branches: 233 677 34.4 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Morgan Deters, Kshitij Bansal
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 model engine class.
14
 */
15
16
#include "theory/quantifiers/fmf/model_engine.h"
17
18
#include "options/quantifiers_options.h"
19
#include "theory/quantifiers/first_order_model.h"
20
#include "theory/quantifiers/fmf/full_model_check.h"
21
#include "theory/quantifiers/instantiate.h"
22
#include "theory/quantifiers/quant_rep_bound_ext.h"
23
#include "theory/quantifiers/quantifiers_attributes.h"
24
#include "theory/quantifiers/term_database.h"
25
26
using namespace cvc5::kind;
27
using namespace cvc5::context;
28
29
namespace cvc5 {
30
namespace theory {
31
namespace quantifiers {
32
33
//Model Engine constructor
34
892
ModelEngine::ModelEngine(Env& env,
35
                         QuantifiersState& qs,
36
                         QuantifiersInferenceManager& qim,
37
                         QuantifiersRegistry& qr,
38
                         TermRegistry& tr,
39
892
                         QModelBuilder* builder)
40
    : QuantifiersModule(env, qs, qim, qr, tr),
41
      d_incomplete_check(true),
42
      d_addedLemmas(0),
43
      d_triedLemmas(0),
44
      d_totalLemmas(0),
45
892
      d_builder(builder)
46
{
47
48
892
}
49
50
1784
ModelEngine::~ModelEngine() {
51
52
1784
}
53
54
15476
bool ModelEngine::needsCheck( Theory::Effort e ) {
55
15476
  return e==Theory::EFFORT_LAST_CALL;
56
}
57
58
1446
QuantifiersModule::QEffort ModelEngine::needsModel(Theory::Effort e)
59
{
60
1446
  if( options::mbqiInterleave() ){
61
    return QEFFORT_STANDARD;
62
  }else{
63
1446
    return QEFFORT_MODEL;
64
  }
65
}
66
67
9762
void ModelEngine::reset_round( Theory::Effort e ) {
68
9762
  d_incomplete_check = true;
69
9762
}
70
4367
void ModelEngine::check(Theory::Effort e, QEffort quant_e)
71
{
72
4367
  bool doCheck = false;
73
4367
  if( options::mbqiInterleave() ){
74
    doCheck = quant_e == QEFFORT_STANDARD && d_qim.hasPendingLemma();
75
  }
76
4367
  if( !doCheck ){
77
4367
    doCheck = quant_e == QEFFORT_MODEL;
78
  }
79
4367
  if( doCheck ){
80
1402
    Assert(!d_qstate.isInConflict());
81
1402
    int addedLemmas = 0;
82
83
    //the following will test that the model satisfies all asserted universal quantifiers by
84
    // (model-based) exhaustive instantiation.
85
1402
    double clSet = 0;
86
1402
    if( Trace.isOn("model-engine") ){
87
      Trace("model-engine") << "---Model Engine Round---" << std::endl;
88
      clSet = double(clock())/double(CLOCKS_PER_SEC);
89
    }
90
1402
    Trace("model-engine-debug") << "Check model..." << std::endl;
91
1402
    d_incomplete_check = false;
92
    // print debug
93
1402
    if (Trace.isOn("fmf-model-complete"))
94
    {
95
      Trace("fmf-model-complete") << std::endl;
96
      debugPrint("fmf-model-complete");
97
    }
98
    // successfully built an acceptable model, now check it
99
1402
    addedLemmas += checkModel();
100
101
1402
    if( Trace.isOn("model-engine") ){
102
      double clSet2 = double(clock())/double(CLOCKS_PER_SEC);
103
      Trace("model-engine") << "Finished model engine, time = " << (clSet2-clSet) << std::endl;
104
    }
105
106
1402
    if( addedLemmas==0 ){
107
900
      Trace("model-engine-debug")
108
450
          << "No lemmas added, incomplete = "
109
450
          << (d_incomplete_check || !d_incompleteQuants.empty()) << std::endl;
110
      // cvc5 will answer SAT or unknown
111
450
      if( Trace.isOn("fmf-consistent") ){
112
        Trace("fmf-consistent") << std::endl;
113
        debugPrint("fmf-consistent");
114
      }
115
    }
116
  }
117
4367
}
118
119
388
bool ModelEngine::checkComplete(IncompleteId& incId)
120
{
121
388
  if (d_incomplete_check)
122
  {
123
    incId = IncompleteId::QUANTIFIERS_FMF;
124
    return false;
125
  }
126
388
  return true;
127
}
128
129
1110
bool ModelEngine::checkCompleteFor( Node q ) {
130
1110
  return d_incompleteQuants.find(q) == d_incompleteQuants.end();
131
}
132
133
1787
void ModelEngine::registerQuantifier( Node f ){
134
1787
  if( Trace.isOn("fmf-warn") ){
135
    bool canHandle = true;
136
    for( unsigned i=0; i<f[0].getNumChildren(); i++ ){
137
      TypeNode tn = f[0][i].getType();
138
      if( !tn.isSort() ){
139
        if (!d_qstate.isFiniteType(tn))
140
        {
141
          if( tn.isInteger() ){
142
            if( !options::fmfBound() ){
143
              canHandle = false;
144
            }
145
          }else{
146
            canHandle = false;
147
          }
148
        }
149
      }
150
    }
151
    if( !canHandle ){
152
      Trace("fmf-warn") << "Warning : Model Engine : may not be able to answer SAT because of formula : " << f << std::endl;
153
    }
154
  }
155
1787
}
156
157
1402
int ModelEngine::checkModel(){
158
1402
  FirstOrderModel* fm = d_treg.getModel();
159
160
  //for debugging, setup
161
6413
  for (std::map<TypeNode, std::vector<Node> >::iterator it =
162
1402
           fm->getRepSetPtr()->d_type_reps.begin();
163
7815
       it != fm->getRepSetPtr()->d_type_reps.end();
164
       ++it)
165
  {
166
6413
    if( it->first.isSort() ){
167
1433
      Trace("model-engine") << "Cardinality( " << it->first << " )" << " = " << it->second.size() << std::endl;
168
1433
      Trace("model-engine-debug") << "        Reps : ";
169
3912
      for( size_t i=0; i<it->second.size(); i++ ){
170
2479
        Trace("model-engine-debug") << it->second[i] << "  ";
171
      }
172
1433
      Trace("model-engine-debug") << std::endl;
173
1433
      Trace("model-engine-debug") << "   Term reps : ";
174
3912
      for( size_t i=0; i<it->second.size(); i++ ){
175
4958
        Node r = fm->getInternalRepresentative(it->second[i], Node::null(), 0);
176
2479
        if (r.isNull())
177
        {
178
          // there was an invalid equivalence class
179
          d_incomplete_check = true;
180
        }
181
2479
        Trace("model-engine-debug") << r << " ";
182
      }
183
1433
      Trace("model-engine-debug") << std::endl;
184
2866
      Node mbt = fm->getModelBasisTerm(it->first);
185
1433
      Trace("model-engine-debug") << "  Basis term : " << mbt << std::endl;
186
    }
187
  }
188
189
1402
  d_triedLemmas = 0;
190
1402
  d_addedLemmas = 0;
191
1402
  d_totalLemmas = 0;
192
  //for statistics
193
1402
  if( Trace.isOn("model-engine") ){
194
    for( unsigned i=0; i<fm->getNumAssertedQuantifiers(); i++ ){
195
      Node f = fm->getAssertedQuantifier( i );
196
      if (fm->isQuantifierActive(f) && shouldProcess(f))
197
      {
198
        int totalInst = 1;
199
        for( unsigned j=0; j<f[0].getNumChildren(); j++ ){
200
          TypeNode tn = f[0][j].getType();
201
          if (fm->getRepSet()->hasType(tn))
202
          {
203
            totalInst =
204
                totalInst * (int)fm->getRepSet()->getNumRepresentatives(tn);
205
          }
206
        }
207
        d_totalLemmas += totalInst;
208
      }
209
    }
210
  }
211
212
1402
  Trace("model-engine-debug") << "Do exhaustive instantiation..." << std::endl;
213
  // FMC uses two sub-effort levels
214
1402
  int e_max = options::mbqiMode() == options::MbqiMode::FMC
215
1727
                  ? 2
216
1727
                  : (options::mbqiMode() == options::MbqiMode::TRUST ? 0 : 1);
217
2597
  for( int e=0; e<e_max; e++) {
218
2147
    d_incompleteQuants.clear();
219
12537
    for( unsigned i=0; i<fm->getNumAssertedQuantifiers(); i++ ){
220
20371
      Node q = fm->getAssertedQuantifier( i, true );
221
10390
      Trace("fmf-exh-inst") << "-> Exhaustive instantiate " << q << ", effort = " << e << "..." << std::endl;
222
      //determine if we should check this quantifier
223
10400
      if (!fm->isQuantifierActive(q))
224
      {
225
10
        Trace("fmf-exh-inst") << "-> Inactive : " << q << std::endl;
226
10
        continue;
227
      }
228
10779
      if (!shouldProcess(q))
229
      {
230
399
        Trace("fmf-exh-inst") << "-> Not processed : " << q << std::endl;
231
399
        d_incompleteQuants.insert(q);
232
399
        continue;
233
      }
234
9981
      exhaustiveInstantiate(q, e);
235
9981
      if (d_qstate.isInConflict())
236
      {
237
        break;
238
      }
239
    }
240
2147
    if( d_addedLemmas>0 ){
241
952
      break;
242
    }else{
243
1195
      Assert(!d_qstate.isInConflict());
244
    }
245
  }
246
247
  //print debug information
248
1402
  if (d_qstate.isInConflict())
249
  {
250
    Trace("model-engine") << "Conflict, added lemmas = ";
251
  }else{
252
1402
    Trace("model-engine") << "Added Lemmas = ";
253
  }
254
1402
  Trace("model-engine") << d_addedLemmas << " / " << d_triedLemmas << " / ";
255
1402
  Trace("model-engine") << d_totalLemmas << std::endl;
256
1402
  return d_addedLemmas;
257
}
258
259
260
261
9981
void ModelEngine::exhaustiveInstantiate( Node f, int effort ){
262
  //first check if the builder can do the exhaustive instantiation
263
9981
  unsigned prev_alem = d_builder->getNumAddedLemmas();
264
9981
  unsigned prev_tlem = d_builder->getNumTriedLemmas();
265
9981
  FirstOrderModel* fm = d_treg.getModel();
266
9981
  int retEi = d_builder->doExhaustiveInstantiation(fm, f, effort);
267
9981
  if( retEi!=0 ){
268
9799
    if( retEi<0 ){
269
      Trace("fmf-exh-inst") << "-> Builder determined complete instantiation was impossible." << std::endl;
270
      d_incompleteQuants.insert(f);
271
    }else{
272
9799
      Trace("fmf-exh-inst") << "-> Builder determined instantiation(s)." << std::endl;
273
    }
274
9799
    d_triedLemmas += d_builder->getNumTriedLemmas() - prev_tlem;
275
9799
    d_addedLemmas += d_builder->getNumAddedLemmas() - prev_alem;
276
  }else{
277
182
    if( Trace.isOn("fmf-exh-inst-debug") ){
278
      Trace("fmf-exh-inst-debug") << "   Instantiation Constants: ";
279
      for( size_t i=0; i<f[0].getNumChildren(); i++ ){
280
        Trace("fmf-exh-inst-debug")
281
            << d_qreg.getInstantiationConstant(f, i) << " ";
282
      }
283
      Trace("fmf-exh-inst-debug") << std::endl;
284
    }
285
182
    QuantifiersBoundInference& qbi = d_qreg.getQuantifiersBoundInference();
286
    //create a rep set iterator and iterate over the (relevant) domain of the quantifier
287
364
    QRepBoundExt qrbe(qbi, fm);
288
364
    RepSetIterator riter(fm->getRepSet(), &qrbe);
289
182
    if( riter.setQuantifier( f ) ){
290
182
      Trace("fmf-exh-inst") << "...exhaustive instantiation set, incomplete=" << riter.isIncomplete() << "..." << std::endl;
291
182
      if( !riter.isIncomplete() ){
292
168
        int triedLemmas = 0;
293
168
        int addedLemmas = 0;
294
168
        Instantiate* inst = d_qim.getInstantiate();
295
1156
        while( !riter.isFinished() && ( addedLemmas==0 || !options::fmfOneInstPerRound() ) ){
296
          //instantiation was not shown to be true, construct the match
297
988
          InstMatch m( f );
298
1506
          for (unsigned i = 0; i < riter.getNumTerms(); i++)
299
          {
300
1012
            m.set(d_qstate, i, riter.getCurrentTerm(i));
301
          }
302
494
          Debug("fmf-model-eval") << "* Add instantiation " << m << std::endl;
303
494
          triedLemmas++;
304
          //add as instantiation
305
988
          if (inst->addInstantiation(f,
306
                                     m.d_vals,
307
                                     InferenceId::QUANTIFIERS_INST_FMF_EXH,
308
988
                                     Node::null(),
309
                                     true))
310
          {
311
236
            addedLemmas++;
312
236
            if (d_qstate.isInConflict())
313
            {
314
              break;
315
            }
316
          }else{
317
258
            Debug("fmf-model-eval") << "* Failed Add instantiation " << m << std::endl;
318
          }
319
494
          riter.increment();
320
        }
321
168
        d_addedLemmas += addedLemmas;
322
168
        d_triedLemmas += triedLemmas;
323
      }
324
    }else{
325
      Trace("fmf-exh-inst") << "...exhaustive instantiation did set, incomplete=" << riter.isIncomplete() << "..." << std::endl;
326
    }
327
    //if the iterator is incomplete, we will return unknown instead of sat if no instantiations are added this round
328
182
    if( riter.isIncomplete() ){
329
22
      d_incompleteQuants.insert(f);
330
    }
331
  }
332
9981
}
333
334
void ModelEngine::debugPrint( const char* c ){
335
  if (Trace.isOn(c))
336
  {
337
    Trace(c) << "Quantifiers: " << std::endl;
338
    FirstOrderModel* m = d_treg.getModel();
339
    for (size_t i = 0, nquant = m->getNumAssertedQuantifiers(); i < nquant; i++)
340
    {
341
      Node q = m->getAssertedQuantifier(i);
342
      if (d_qreg.hasOwnership(q, this))
343
      {
344
        Trace(c) << "   ";
345
        if (!m->isQuantifierActive(q))
346
        {
347
          Trace(c) << "*Inactive* ";
348
        }
349
        else
350
        {
351
          Trace(c) << "           ";
352
        }
353
        Trace(c) << q << std::endl;
354
      }
355
    }
356
  }
357
}
358
359
10380
bool ModelEngine::shouldProcess(Node q)
360
{
361
10380
  if (!d_qreg.hasOwnership(q, this))
362
  {
363
389
    return false;
364
  }
365
  // if finite model finding or fmf bound is on, we process everything
366
9991
  if (options::finiteModelFind() || options::fmfBound())
367
  {
368
8151
    return true;
369
  }
370
  // otherwise, we are only using model-based instantiation for internal
371
  // quantified formulas
372
1840
  QuantAttributes& qattr = d_qreg.getQuantAttributes();
373
1840
  return qattr.isInternal(q);
374
}
375
376
}  // namespace quantifiers
377
}  // namespace theory
378
22746
}  // namespace cvc5