GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/fmf/full_model_check.cpp Lines: 763 802 95.1 %
Date: 2021-09-30 Branches: 1665 3243 51.3 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Mathias Preiner, 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
 * Implementation of full model check class.
14
 */
15
16
#include "theory/quantifiers/fmf/full_model_check.h"
17
18
#include "expr/skolem_manager.h"
19
#include "options/quantifiers_options.h"
20
#include "options/strings_options.h"
21
#include "options/theory_options.h"
22
#include "options/uf_options.h"
23
#include "theory/quantifiers/first_order_model.h"
24
#include "theory/quantifiers/fmf/bounded_integers.h"
25
#include "theory/quantifiers/instantiate.h"
26
#include "theory/quantifiers/quant_rep_bound_ext.h"
27
#include "theory/quantifiers/quantifiers_inference_manager.h"
28
#include "theory/quantifiers/quantifiers_registry.h"
29
#include "theory/quantifiers/quantifiers_state.h"
30
#include "theory/quantifiers/term_database.h"
31
#include "theory/quantifiers/term_util.h"
32
#include "theory/rewriter.h"
33
34
using namespace cvc5::kind;
35
using namespace cvc5::context;
36
37
namespace cvc5 {
38
namespace theory {
39
namespace quantifiers {
40
namespace fmcheck {
41
42
160006
struct ModelBasisArgSort
43
{
44
  std::vector< Node > d_terms;
45
  // number of arguments that are model-basis terms
46
  std::unordered_map<Node, unsigned> d_mba_count;
47
52289
  bool operator() (int i,int j) {
48
52289
    return (d_mba_count[d_terms[i]] < d_mba_count[d_terms[j]]);
49
  }
50
};
51
52
357099
bool EntryTrie::hasGeneralization( FirstOrderModelFmc * m, Node c, int index ) {
53
357099
  if (index==(int)c.getNumChildren()) {
54
14106
    return d_data!=-1;
55
  }else{
56
685986
    TypeNode tn = c[index].getType();
57
685986
    Node st = m->getStar(tn);
58
342993
    if(d_child.find(st)!=d_child.end()) {
59
39485
      if( d_child[st].hasGeneralization(m, c, index+1) ){
60
14517
        return true;
61
      }
62
    }
63
328476
    if( c[index]!=st && d_child.find( c[index] )!=d_child.end() ){
64
53632
      if( d_child[ c[index] ].hasGeneralization(m, c, index+1) ){
65
19952
        return true;
66
      }
67
    }
68
308524
    if( c[index].getType().isSort() ){
69
      //for star: check if all children are defined and have generalizations
70
218968
      if( c[index]==st ){     ///options::fmfFmcCoverSimplify()
71
        //check if all children exist and are complete
72
        unsigned num_child_def =
73
126832
            d_child.size() - (d_child.find(st) != d_child.end() ? 1 : 0);
74
126832
        if (num_child_def == m->getRepSet()->getNumRepresentatives(tn))
75
        {
76
8425
          bool complete = true;
77
14271
          for ( std::map<Node,EntryTrie>::iterator it = d_child.begin(); it != d_child.end(); ++it ){
78
12040
            if( !m->isStar(it->first) ){
79
11538
              if( !it->second.hasGeneralization(m, c, index+1) ){
80
6194
                complete = false;
81
6194
                break;
82
              }
83
            }
84
          }
85
8425
          if( complete ){
86
2231
            return true;
87
          }
88
        }
89
      }
90
    }
91
92
306293
    return false;
93
  }
94
}
95
96
122327
int EntryTrie::getGeneralizationIndex( FirstOrderModelFmc * m, std::vector<Node> & inst, int index ) {
97
122327
  Debug("fmc-entry-trie") << "Get generalization index " << inst.size() << " " << index << std::endl;
98
122327
  if (index==(int)inst.size()) {
99
34107
    return d_data;
100
  }else{
101
88220
    int minIndex = -1;
102
176440
    Node st = m->getStar(inst[index].getType());
103
88220
    if (d_child.find(st) != d_child.end())
104
    {
105
82947
      minIndex = d_child[st].getGeneralizationIndex(m, inst, index + 1);
106
    }
107
176440
    Node cc = inst[index];
108
88220
    if (cc != st && d_child.find(cc) != d_child.end())
109
    {
110
9427
      int gindex = d_child[cc].getGeneralizationIndex(m, inst, index + 1);
111
9427
      if (minIndex == -1 || (gindex != -1 && gindex < minIndex))
112
      {
113
6534
        minIndex = gindex;
114
      }
115
    }
116
88220
    return minIndex;
117
  }
118
}
119
120
658520
void EntryTrie::addEntry( FirstOrderModelFmc * m, Node c, Node v, int data, int index ) {
121
658520
  if (index==(int)c.getNumChildren()) {
122
241451
    if(d_data==-1) {
123
241451
      d_data = data;
124
    }
125
  }
126
  else {
127
417069
    d_child[ c[index] ].addEntry(m,c,v,data,index+1);
128
417069
    if( d_complete==0 ){
129
      d_complete = -1;
130
    }
131
  }
132
658520
}
133
134
282384
void EntryTrie::getEntries( FirstOrderModelFmc * m, Node c, std::vector<int> & compat, std::vector<int> & gen, int index, bool is_gen ) {
135
282384
  if (index==(int)c.getNumChildren()) {
136
55072
    if( d_data!=-1) {
137
55072
      if( is_gen ){
138
51020
        gen.push_back(d_data);
139
      }
140
55072
      compat.push_back(d_data);
141
    }
142
  }else{
143
227312
    if (m->isStar(c[index])) {
144
236012
      for ( std::map<Node,EntryTrie>::iterator it = d_child.begin(); it != d_child.end(); ++it ){
145
96159
        it->second.getEntries(m, c, compat, gen, index+1, is_gen );
146
      }
147
    }else{
148
174918
      Node st = m->getStar(c[index].getType());
149
87459
      if(d_child.find(st)!=d_child.end()) {
150
6008
        d_child[st].getEntries(m, c, compat, gen, index+1, false);
151
      }
152
87459
      if( d_child.find( c[index] )!=d_child.end() ){
153
28177
        d_child[ c[index] ].getEntries(m, c, compat, gen, index+1, is_gen);
154
      }
155
    }
156
157
  }
158
282384
}
159
160
252444
bool Def::addEntry( FirstOrderModelFmc * m, Node c, Node v) {
161
252444
  if (d_et.hasGeneralization(m, c)) {
162
10993
    Trace("fmc-debug") << "Already has generalization, skip." << std::endl;
163
10993
    return false;
164
  }
165
241451
  int newIndex = (int)d_cond.size();
166
241451
  if (!d_has_simplified) {
167
304080
    std::vector<int> compat;
168
304080
    std::vector<int> gen;
169
152040
    d_et.getEntries(m, c, compat, gen);
170
207112
    for( unsigned i=0; i<compat.size(); i++) {
171
55072
      if( d_status[compat[i]]==status_unk ){
172
48009
        if( d_value[compat[i]]!=v ){
173
30142
          d_status[compat[i]] = status_non_redundant;
174
        }
175
      }
176
    }
177
203060
    for( unsigned i=0; i<gen.size(); i++) {
178
51020
      if( d_status[gen[i]]==status_unk ){
179
15943
        if( d_value[gen[i]]==v ){
180
15943
          d_status[gen[i]] = status_redundant;
181
        }
182
      }
183
    }
184
152040
    d_status.push_back( status_unk );
185
  }
186
241451
  d_et.addEntry(m, c, v, newIndex);
187
241451
  d_cond.push_back(c);
188
241451
  d_value.push_back(v);
189
241451
  return true;
190
}
191
192
2491
Node Def::evaluate( FirstOrderModelFmc * m, std::vector<Node>& inst ) {
193
2491
  int gindex = d_et.getGeneralizationIndex(m, inst);
194
2491
  if (gindex!=-1) {
195
2487
    return d_value[gindex];
196
  }else{
197
4
    Trace("fmc-warn") << "Warning : evaluation came up null!" << std::endl;
198
4
    return Node::null();
199
  }
200
}
201
202
27462
int Def::getGeneralizationIndex( FirstOrderModelFmc * m, std::vector<Node>& inst ) {
203
27462
  return d_et.getGeneralizationIndex(m, inst);
204
}
205
206
54484
void Def::basic_simplify( FirstOrderModelFmc * m ) {
207
54484
  d_has_simplified = true;
208
108968
  std::vector< Node > cond;
209
54484
  cond.insert( cond.end(), d_cond.begin(), d_cond.end() );
210
54484
  d_cond.clear();
211
108968
  std::vector< Node > value;
212
54484
  value.insert( value.end(), d_value.begin(), d_value.end() );
213
54484
  d_value.clear();
214
54484
  d_et.reset();
215
159838
  for (unsigned i=0; i<d_status.size(); i++) {
216
105354
    if( d_status[i]!=status_redundant ){
217
89411
      addEntry(m, cond[i], value[i]);
218
    }
219
  }
220
54484
  d_status.clear();
221
54484
}
222
223
49158
void Def::simplify(FullModelChecker * mc, FirstOrderModelFmc * m) {
224
49158
  Trace("fmc-simplify") << "Simplify definition, #cond = " << d_cond.size() << std::endl;
225
49158
  basic_simplify( m );
226
49158
  Trace("fmc-simplify") << "post-basic simplify, #cond = " << d_cond.size() << std::endl;
227
228
  //check if the last entry is not all star, if so, we can make the last entry all stars
229
49158
  if( !d_cond.empty() ){
230
49158
    bool last_all_stars = true;
231
98316
    Node cc = d_cond[d_cond.size()-1];
232
118512
    for( unsigned i=0; i<cc.getNumChildren(); i++ ){
233
74680
      if (!m->isStar(cc[i]))
234
      {
235
5326
        last_all_stars = false;
236
5326
        break;
237
      }
238
    }
239
49158
    if( !last_all_stars ){
240
5326
      Trace("fmc-cover-simplify") << "Need to modify last entry to be all stars." << std::endl;
241
5326
      Trace("fmc-cover-simplify") << "Before: " << std::endl;
242
5326
      debugPrint("fmc-cover-simplify",Node::null(), mc);
243
5326
      Trace("fmc-cover-simplify") << std::endl;
244
10652
      std::vector< Node > cond;
245
5326
      cond.insert( cond.end(), d_cond.begin(), d_cond.end() );
246
5326
      d_cond.clear();
247
10652
      std::vector< Node > value;
248
5326
      value.insert( value.end(), d_value.begin(), d_value.end() );
249
5326
      d_value.clear();
250
5326
      d_et.reset();
251
5326
      d_has_simplified = false;
252
      //change the last to all star
253
10652
      std::vector< Node > nc;
254
5326
      nc.push_back( cc.getOperator() );
255
14141
      for( unsigned j=0; j< cc.getNumChildren(); j++){
256
8815
        nc.push_back(m->getStar(cc[j].getType()));
257
      }
258
5326
      cond[cond.size()-1] = NodeManager::currentNM()->mkNode( APPLY_UF, nc );
259
      //re-add the entries
260
15545
      for (unsigned i=0; i<cond.size(); i++) {
261
10219
        addEntry(m, cond[i], value[i]);
262
      }
263
5326
      Trace("fmc-cover-simplify") << "Finished re-adding entries." << std::endl;
264
5326
      basic_simplify( m );
265
5326
      Trace("fmc-cover-simplify") << "After: " << std::endl;
266
5326
      debugPrint("fmc-cover-simplify",Node::null(), mc);
267
5326
      Trace("fmc-cover-simplify") << std::endl;
268
    }
269
  }
270
49158
  Trace("fmc-simplify") << "finish simplify, #cond = " << d_cond.size() << std::endl;
271
49158
}
272
273
183371
void Def::debugPrint(const char * tr, Node op, FullModelChecker * m) {
274
183371
  if (!op.isNull()) {
275
31292
    Trace(tr) << "Model for " << op << " : " << std::endl;
276
  }
277
473506
  for( unsigned i=0; i<d_cond.size(); i++) {
278
    //print the condition
279
290135
    if (!op.isNull()) {
280
74295
      Trace(tr) << op;
281
    }
282
290135
    m->debugPrintCond(tr, d_cond[i], true);
283
290135
    Trace(tr) << " -> ";
284
290135
    m->debugPrint(tr, d_value[i]);
285
290135
    Trace(tr) << std::endl;
286
  }
287
183371
}
288
289
892
FullModelChecker::FullModelChecker(Env& env,
290
                                   QuantifiersState& qs,
291
                                   QuantifiersInferenceManager& qim,
292
                                   QuantifiersRegistry& qr,
293
892
                                   TermRegistry& tr)
294
    : QModelBuilder(env, qs, qim, qr, tr),
295
892
      d_fm(new FirstOrderModelFmc(env, qs, qr, tr))
296
{
297
892
  d_true = NodeManager::currentNM()->mkConst(true);
298
892
  d_false = NodeManager::currentNM()->mkConst(false);
299
892
}
300
301
892
void FullModelChecker::finishInit() { d_model = d_fm.get(); }
302
303
1680
bool FullModelChecker::preProcessBuildModel(TheoryModel* m) {
304
  //standard pre-process
305
1680
  if( !preProcessBuildModelStd( m ) ){
306
    return false;
307
  }
308
309
1680
  Trace("fmc") << "---Full Model Check preprocess() " << std::endl;
310
1680
  d_preinitialized_eqc.clear();
311
1680
  d_preinitialized_types.clear();
312
  //traverse equality engine
313
1680
  eq::EqClassesIterator eqcs_i = eq::EqClassesIterator(m->getEqualityEngine());
314
65324
  while( !eqcs_i.isFinished() ){
315
63644
    Node r = *eqcs_i;
316
63644
    TypeNode tr = r.getType();
317
31822
    d_preinitialized_eqc[tr] = r;
318
31822
    ++eqcs_i;
319
  }
320
321
  //must ensure model basis terms exists in model for each relevant type
322
1680
  Trace("fmc") << "preInitialize types..." << std::endl;
323
1680
  d_fm->initialize();
324
7831
  for (std::pair<const Node, Def*>& mp : d_fm->d_models)
325
  {
326
12302
    Node op = mp.first;
327
6151
    Trace("fmc") << "preInitialize types for " << op << std::endl;
328
12302
    TypeNode tno = op.getType();
329
23024
    for( unsigned i=0; i<tno.getNumChildren(); i++) {
330
16873
      Trace("fmc") << "preInitializeType " << tno[i] << std::endl;
331
16873
      preInitializeType(m, tno[i]);
332
16873
      Trace("fmc") << "finished preInitializeType " << tno[i] << std::endl;
333
    }
334
  }
335
1680
  Trace("fmc") << "Finish preInitialize types" << std::endl;
336
  //do not have to introduce terms for sorts of domains of quantified formulas if we are allowed to assume empty sorts
337
9489
  for (unsigned i = 0, nquant = d_fm->getNumAssertedQuantifiers(); i < nquant;
338
       i++)
339
  {
340
15507
    Node q = d_fm->getAssertedQuantifier(i);
341
7809
    registerQuantifiedFormula(q);
342
7809
    if (!isHandled(q))
343
    {
344
111
      continue;
345
    }
346
    // make sure all types are set
347
20075
    for (const Node& v : q[0])
348
    {
349
12377
      preInitializeType(m, v.getType());
350
    }
351
  }
352
1680
  return true;
353
}
354
355
1680
bool FullModelChecker::processBuildModel(TheoryModel* m){
356
1680
  if (!m->areFunctionValuesEnabled())
357
  {
358
    // nothing to do if no functions
359
    return true;
360
  }
361
1680
  FirstOrderModelFmc* fm = d_fm.get();
362
1680
  Trace("fmc") << "---Full Model Check reset() " << std::endl;
363
1680
  d_quant_models.clear();
364
1680
  d_rep_ids.clear();
365
1680
  d_star_insts.clear();
366
  //process representatives
367
1680
  RepSet* rs = m->getRepSetPtr();
368
7056
  for (std::map<TypeNode, std::vector<Node> >::iterator it =
369
1680
           rs->d_type_reps.begin();
370
8736
       it != rs->d_type_reps.end();
371
       ++it)
372
  {
373
7056
    if( it->first.isSort() ){
374
1455
      Trace("fmc") << "Cardinality( " << it->first << " )" << " = " << it->second.size() << std::endl;
375
4028
      for( size_t a=0; a<it->second.size(); a++ ){
376
5146
        Node r = m->getRepresentative(it->second[a]);
377
2573
        if( Trace.isOn("fmc-model-debug") ){
378
          std::vector< Node > eqc;
379
          d_qstate.getEquivalenceClass(r, eqc);
380
          Trace("fmc-model-debug") << "   " << (it->second[a]==r);
381
          Trace("fmc-model-debug") << " : " << it->second[a] << " : " << r << " : ";
382
          //Trace("fmc-model-debug") << r2 << " : " << ir << " : ";
383
          Trace("fmc-model-debug") << " {";
384
          for( size_t i=0; i<eqc.size(); i++ ){
385
            Trace("fmc-model-debug") << eqc[i] << ", ";
386
          }
387
          Trace("fmc-model-debug") << "}" << std::endl;
388
        }
389
2573
        d_rep_ids[it->first][r] = (int)a;
390
      }
391
1455
      Trace("fmc-model-debug") << std::endl;
392
    }
393
  }
394
395
  //now, make models
396
7831
  for (std::pair<const Node, Def*>& fmm : d_fm->d_models)
397
  {
398
12302
    Node op = fmm.first;
399
    //reset the model
400
6151
    d_fm->d_models[op]->reset();
401
402
12302
    std::vector< Node > add_conds;
403
12302
    std::vector< Node > add_values;
404
6151
    bool needsDefault = true;
405
6151
    if (m->hasUfTerms(op))
406
    {
407
4478
      const std::vector<Node>& uft = m->getUfTerms(op);
408
8956
      Trace("fmc-model-debug")
409
4478
          << uft.size() << " model values for " << op << " ... " << std::endl;
410
30382
      for (const Node& n : uft)
411
      {
412
        // only consider unique up to congruence (in model equality engine)?
413
25904
        add_conds.push_back( n );
414
25904
        add_values.push_back( n );
415
51808
        Node r = m->getRepresentative(n);
416
25904
        Trace("fmc-model-debug") << n << " -> " << r << std::endl;
417
      }
418
    }else{
419
1673
      Trace("fmc-model-debug") << "No model values for " << op << " ... " << std::endl;
420
    }
421
6151
    Trace("fmc-model-debug") << std::endl;
422
    //possibly get default
423
6151
    if( needsDefault ){
424
12302
      Node nmb = d_fm->getModelBasisOpTerm(op);
425
      //add default value if necessary
426
6151
      if (m->hasTerm(nmb))
427
      {
428
3287
        Trace("fmc-model-debug") << "Add default " << nmb << std::endl;
429
3287
        add_conds.push_back( nmb );
430
3287
        add_values.push_back( nmb );
431
      }else{
432
5728
        Node vmb = getSomeDomainElement(d_fm.get(), nmb.getType());
433
2864
        Trace("fmc-model-debug") << "Add default to default representative " << nmb << " ";
434
5728
        Trace("fmc-model-debug")
435
5728
            << m->getRepSet()->getNumRepresentatives(nmb.getType())
436
2864
            << std::endl;
437
2864
        add_conds.push_back( nmb );
438
2864
        add_values.push_back( vmb );
439
      }
440
    }
441
442
12302
    std::vector< Node > conds;
443
12302
    std::vector< Node > values;
444
12302
    std::vector< Node > entry_conds;
445
    //get the entries for the model
446
38206
    for( size_t i=0; i<add_conds.size(); i++ ){
447
64110
      Node c = add_conds[i];
448
64110
      Node v = add_values[i];
449
64110
      Trace("fmc-model-debug")
450
32055
          << "Add cond/value : " << c << " -> " << v << std::endl;
451
64110
      std::vector< Node > children;
452
64110
      std::vector< Node > entry_children;
453
32055
      children.push_back(op);
454
32055
      entry_children.push_back(op);
455
32055
      bool hasNonStar = false;
456
99265
      for (const Node& ci : c)
457
      {
458
134420
        Node ri = fm->getRepresentative(ci);
459
67210
        children.push_back(ri);
460
67210
        bool isStar = false;
461
67210
        if (fm->isModelBasisTerm(ri))
462
        {
463
20481
          ri = fm->getStar(ri.getType());
464
20481
          isStar = true;
465
        }
466
        else
467
        {
468
46729
          hasNonStar = true;
469
        }
470
67210
        if( !isStar && !ri.isConst() ){
471
          Trace("fmc-warn") << "Warning : model for " << op
472
                            << " has non-constant argument in model " << ri
473
                            << " (from " << ci << ")" << std::endl;
474
          Assert(false);
475
        }
476
67210
        entry_children.push_back(ri);
477
      }
478
64110
      Node n = NodeManager::currentNM()->mkNode( APPLY_UF, children );
479
64110
      Node nv = fm->getRepresentative( v );
480
64110
      Trace("fmc-model-debug")
481
32055
          << "Representative of " << v << " is " << nv << std::endl;
482
32055
      if( !nv.isConst() ){
483
        Trace("fmc-warn") << "Warning : model for " << op << " has non-constant value in model " << nv << std::endl;
484
        Assert(false);
485
      }
486
64110
      Node en = (useSimpleModels() && hasNonStar) ? n : NodeManager::currentNM()->mkNode( APPLY_UF, entry_children );
487
32055
      if( std::find(conds.begin(), conds.end(), n )==conds.end() ){
488
21260
        Trace("fmc-model-debug") << "- add " << n << " -> " << nv << " (entry is " << en << ")" << std::endl;
489
21260
        conds.push_back(n);
490
21260
        values.push_back(nv);
491
21260
        entry_conds.push_back(en);
492
      }
493
      else {
494
10795
        Trace("fmc-model-debug") << "Already have entry for : " << n << " -> " << nv << " (entry is " << en << ")" << std::endl;
495
      }
496
    }
497
498
499
    //sort based on # default arguments
500
12302
    std::vector< int > indices;
501
12302
    ModelBasisArgSort mbas;
502
27411
    for (int i=0; i<(int)conds.size(); i++) {
503
21260
      mbas.d_terms.push_back(conds[i]);
504
21260
      mbas.d_mba_count[conds[i]] = fm->getModelBasisArg(conds[i]);
505
21260
      indices.push_back(i);
506
    }
507
6151
    std::sort( indices.begin(), indices.end(), mbas );
508
509
27411
    for (int i=0; i<(int)indices.size(); i++) {
510
21260
      fm->d_models[op]->addEntry(fm, entry_conds[indices[i]], values[indices[i]]);
511
    }
512
513
6151
    Trace("fmc-model-simplify") << "Before simplification : " << std::endl;
514
6151
    fm->d_models[op]->debugPrint("fmc-model-simplify", op, this);
515
6151
    Trace("fmc-model-simplify") << std::endl;
516
517
6151
    Trace("fmc-model-simplify") << "Simplifying " << op << "..." << std::endl;
518
6151
    fm->d_models[op]->simplify( this, fm );
519
520
6151
    fm->d_models[op]->debugPrint("fmc-model", op, this);
521
6151
    Trace("fmc-model") << std::endl;
522
523
    //for debugging
524
    /*
525
    for( size_t i=0; i<fm->d_uf_terms[op].size(); i++ ){
526
      std::vector< Node > inst;
527
      for( unsigned j=0; j<fm->d_uf_terms[op][i].getNumChildren(); j++ ){
528
        Node r = fm->getRepresentative( fm->d_uf_terms[op][i][j] );
529
        inst.push_back( r );
530
      }
531
      Node ev = fm->d_models[op]->evaluate( fm, inst );
532
      Trace("fmc-model-debug") << ".....Checking eval( " <<
533
    fm->d_uf_terms[op][i] << " ) = " << ev << std::endl; AlwaysAssert(
534
    fm->areEqual( ev, fm->d_uf_terms[op][i] ) );
535
    }
536
    */
537
  }
538
1680
  Assert(d_addedLemmas == 0);
539
540
  //make function values
541
7831
  for( std::map<Node, Def * >::iterator it = fm->d_models.begin(); it != fm->d_models.end(); ++it ){
542
12302
    Node f_def = getFunctionValue( fm, it->first, "$x" );
543
6151
    m->assignFunctionDefinition( it->first, f_def );
544
  }
545
1680
  return TheoryEngineModelBuilder::processBuildModel( m );
546
}
547
548
29250
void FullModelChecker::preInitializeType(TheoryModel* m, TypeNode tn)
549
{
550
29250
  if( d_preinitialized_types.find( tn )==d_preinitialized_types.end() ){
551
3903
    d_preinitialized_types[tn] = true;
552
3903
    if (tn.isFirstClass())
553
    {
554
3897
      Trace("fmc") << "Get model basis term " << tn << "..." << std::endl;
555
7794
      Node mb = d_fm->getModelBasisTerm(tn);
556
3897
      Trace("fmc") << "...return " << mb << std::endl;
557
      // if the model basis term does not exist in the model,
558
      // either add it directly to the model's equality engine if no other terms
559
      // of this type exist, or otherwise assert that it is equal to the first
560
      // equivalence class of its type.
561
3897
      if (!m->hasTerm(mb) && !mb.isConst())
562
      {
563
478
        std::map<TypeNode, Node>::iterator itpe = d_preinitialized_eqc.find(tn);
564
478
        if (itpe == d_preinitialized_eqc.end())
565
        {
566
436
          Trace("fmc") << "...add model basis term to EE of model " << mb << " "
567
218
                       << tn << std::endl;
568
218
          m->getEqualityEngine()->addTerm(mb);
569
        }
570
        else
571
        {
572
520
          Trace("fmc") << "...add model basis eqc equality to model " << mb
573
260
                       << " == " << itpe->second << " " << tn << std::endl;
574
260
          bool ret = m->assertEquality(mb, itpe->second, true);
575
260
          AlwaysAssert(ret);
576
        }
577
      }
578
    }
579
  }
580
29250
}
581
582
294792
void FullModelChecker::debugPrintCond(const char * tr, Node n, bool dispStar) {
583
294792
  Trace(tr) << "(";
584
794905
  for( unsigned j=0; j<n.getNumChildren(); j++) {
585
500113
    if( j>0 ) Trace(tr) << ", ";
586
500113
    debugPrint(tr, n[j], dispStar);
587
  }
588
294792
  Trace(tr) << ")";
589
294792
}
590
591
1243638
void FullModelChecker::debugPrint(const char * tr, Node n, bool dispStar) {
592
1243638
  if( n.isNull() ){
593
25590
    Trace(tr) << "null";
594
  }
595
1218048
  else if (FirstOrderModelFmc::isStar(n) && dispStar)
596
  {
597
605950
    Trace(tr) << "*";
598
  }
599
  else
600
  {
601
1224196
    TypeNode tn = n.getType();
602
612098
    if( tn.isSort() && d_rep_ids.find(tn)!=d_rep_ids.end() ){
603
294997
      if (d_rep_ids[tn].find(n)!=d_rep_ids[tn].end()) {
604
276200
        Trace(tr) << d_rep_ids[tn][n];
605
      }else{
606
18797
        Trace(tr) << n;
607
      }
608
    }else{
609
317101
      Trace(tr) << n;
610
    }
611
  }
612
1243638
}
613
614
615
9876
int FullModelChecker::doExhaustiveInstantiation( FirstOrderModel * fm, Node f, int effort ) {
616
9876
  Trace("fmc") << "Full model check " << f << ", effort = " << effort << "..." << std::endl;
617
  // register the quantifier
618
9876
  registerQuantifiedFormula(f);
619
9876
  Assert(!d_qstate.isInConflict());
620
  // we do not do model-based quantifier instantiation if the option
621
  // disables it, or if the quantified formula has an unhandled type.
622
9876
  if (!optUseModel() || !isHandled(f))
623
  {
624
    return 0;
625
  }
626
9876
  FirstOrderModelFmc* fmfmc = static_cast<FirstOrderModelFmc*>(fm);
627
9876
  if (effort == 0)
628
  {
629
7520
    if (options::mbqiMode() == options::MbqiMode::NONE)
630
    {
631
      // just exhaustive instantiate
632
4286
      Node c = mkCondDefault(fmfmc, f);
633
2143
      d_quant_models[f].addEntry(fmfmc, c, d_false);
634
2143
      if (!exhaustiveInstantiate(fmfmc, f, c))
635
      {
636
17
        return 0;
637
      }
638
2126
      return 1;
639
    }
640
    // model check the quantifier
641
5377
    doCheck(fmfmc, f, d_quant_models[f], f[1]);
642
5377
    std::vector<Node>& mcond = d_quant_models[f].d_cond;
643
5377
    Trace("fmc") << "Definition for quantifier " << f << " is : " << std::endl;
644
5377
    Assert(!mcond.empty());
645
5377
    d_quant_models[f].debugPrint("fmc", Node::null(), this);
646
5377
    Trace("fmc") << std::endl;
647
648
    // consider all entries going to non-true
649
5377
    Instantiate* instq = d_qim.getInstantiate();
650
12246
    for (unsigned i = 0, msize = mcond.size(); i < msize; i++)
651
    {
652
6869
      if (d_quant_models[f].d_value[i] == d_true)
653
      {
654
        // already satisfied
655
9422
        continue;
656
      }
657
5490
      Trace("fmc-inst") << "Instantiate based on " << mcond[i] << "..."
658
2745
                        << std::endl;
659
2745
      bool hasStar = false;
660
4316
      std::vector<Node> inst;
661
7159
      for (unsigned j = 0, nchild = mcond[i].getNumChildren(); j < nchild; j++)
662
      {
663
4414
        if (fmfmc->isStar(mcond[i][j]))
664
        {
665
3557
          hasStar = true;
666
3557
          inst.push_back(fmfmc->getModelBasisTerm(mcond[i][j].getType()));
667
        }
668
        else
669
        {
670
857
          inst.push_back(mcond[i][j]);
671
        }
672
      }
673
2745
      bool addInst = true;
674
2745
      if (hasStar)
675
      {
676
        // try obvious (specified by inst)
677
4982
        Node ev = d_quant_models[f].evaluate(fmfmc, inst);
678
2491
        if (ev == d_true)
679
        {
680
9
          addInst = false;
681
18
          Trace("fmc-debug")
682
9
              << "...do not instantiate, evaluation was " << ev << std::endl;
683
        }
684
      }
685
      else
686
      {
687
        // for debugging
688
254
        if (Trace.isOn("fmc-test-inst"))
689
        {
690
          Node ev = d_quant_models[f].evaluate(fmfmc, inst);
691
          if (ev == d_true)
692
          {
693
            CVC5Message() << "WARNING: instantiation was true! " << f << " "
694
                          << mcond[i] << std::endl;
695
            AlwaysAssert(false);
696
          }
697
          else
698
          {
699
            Trace("fmc-test-inst")
700
                << "...instantiation evaluated to false." << std::endl;
701
          }
702
        }
703
      }
704
2754
      if (!addInst)
705
      {
706
18
        Trace("fmc-debug-inst")
707
9
            << "** Instantiation was already true." << std::endl;
708
        // might try it next effort level
709
9
        d_star_insts[f].push_back(i);
710
9
        continue;
711
      }
712
2736
      if (options::fmfBound() || options::stringExp())
713
      {
714
2330
        std::vector<Node> cond;
715
1165
        cond.push_back(d_quant_cond[f]);
716
1165
        cond.insert(cond.end(), inst.begin(), inst.end());
717
        // need to do exhaustive instantiate algorithm to set things properly
718
        // (should only add one instance)
719
2330
        Node c = mkCond(cond);
720
1165
        unsigned prevInst = d_addedLemmas;
721
1165
        exhaustiveInstantiate(fmfmc, f, c);
722
1165
        if (d_addedLemmas == prevInst)
723
        {
724
892
          d_star_insts[f].push_back(i);
725
        }
726
1165
        continue;
727
      }
728
      // just add the instance
729
1571
      d_triedLemmas++;
730
3142
      if (instq->addInstantiation(f,
731
                                  inst,
732
                                  InferenceId::QUANTIFIERS_INST_FMF_FMC,
733
3142
                                  Node::null(),
734
                                  true))
735
      {
736
520
        Trace("fmc-debug-inst") << "** Added instantiation." << std::endl;
737
520
        d_addedLemmas++;
738
520
        if (d_qstate.isInConflict() || options::fmfOneInstPerRound())
739
        {
740
          break;
741
        }
742
      }
743
      else
744
      {
745
2102
        Trace("fmc-debug-inst")
746
1051
            << "** Instantiation was duplicate." << std::endl;
747
        // might try it next effort level
748
1051
        d_star_insts[f].push_back(i);
749
      }
750
    }
751
5377
    return 1;
752
  }
753
  // Get the list of instantiation regions (described by "star entries" in the
754
  // definition) that were not tried at the previous effort level. For each
755
  // of these, we add one instantiation.
756
2356
  std::vector<Node>& mcond = d_quant_models[f].d_cond;
757
2356
  if (!d_star_insts[f].empty())
758
  {
759
1307
    if (Trace.isOn("fmc-exh"))
760
    {
761
      Trace("fmc-exh") << "Exhaustive instantiate " << f << std::endl;
762
      Trace("fmc-exh") << "Definition was : " << std::endl;
763
      d_quant_models[f].debugPrint("fmc-exh", Node::null(), this);
764
      Trace("fmc-exh") << std::endl;
765
    }
766
2586
    Def temp;
767
    // simplify the exceptions?
768
2682
    for (int i = (d_star_insts[f].size() - 1); i >= 0; i--)
769
    {
770
      // get witness for d_star_insts[f][i]
771
1403
      int j = d_star_insts[f][i];
772
1403
      if (temp.addEntry(fmfmc, mcond[j], d_quant_models[f].d_value[j]))
773
      {
774
1349
        if (!exhaustiveInstantiate(fmfmc, f, mcond[j]))
775
        {
776
          // something went wrong, resort to exhaustive instantiation
777
28
          return 0;
778
        }
779
      }
780
    }
781
  }
782
2328
  return 1;
783
}
784
785
/** Representative bound fmc entry
786
 *
787
 * This bound information corresponds to one
788
 * entry in a term definition (see terminology in
789
 * Chapter 5 of Finite Model Finding for
790
 * Satisfiability Modulo Theories thesis).
791
 * For example, a term definition for the body
792
 * of a quantified formula:
793
 *   forall xyz. P( x, y, z )
794
 * may be:
795
 *   ( 0, 0, 0 ) -> false
796
 *   ( *, 1, 2 ) -> false
797
 *   ( *, *, * ) -> true
798
 * Indicating that the quantified formula evaluates
799
 * to false in the current model for x=0, y=0, z=0,
800
 * or y=1, z=2 for any x, and evaluates to true
801
 * otherwise.
802
 * This class is used if we wish
803
 * to iterate over all values corresponding to one
804
 * of these entries. For example, for the second entry:
805
 *   (*, 1, 2 )
806
 * we iterate over all values of x, but only {1}
807
 * for y and {2} for z.
808
 */
809
class RepBoundFmcEntry : public QRepBoundExt
810
{
811
 public:
812
4657
  RepBoundFmcEntry(QuantifiersBoundInference& qbi,
813
                   Node e,
814
                   FirstOrderModelFmc* f)
815
4657
      : QRepBoundExt(qbi, f), d_entry(e), d_fm(f)
816
  {
817
4657
  }
818
4657
  ~RepBoundFmcEntry() {}
819
  /** set bound */
820
6415
  virtual RsiEnumType setBound(Node owner,
821
                               unsigned i,
822
                               std::vector<Node>& elements) override
823
  {
824
6415
    if (!d_fm->isStar(d_entry[i]))
825
    {
826
      // only need to consider the single point
827
1462
      elements.push_back(d_entry[i]);
828
1462
      return ENUM_DEFAULT;
829
    }
830
4953
    return QRepBoundExt::setBound(owner, i, elements);
831
  }
832
833
 private:
834
  /** the entry for this bound */
835
  Node d_entry;
836
  /** the model builder associated with this bound */
837
  FirstOrderModelFmc* d_fm;
838
};
839
840
4657
bool FullModelChecker::exhaustiveInstantiate(FirstOrderModelFmc* fm,
841
                                             Node f,
842
                                             Node c)
843
{
844
4657
  Trace("fmc-exh") << "----Exhaustive instantiate based on " << c << " ";
845
4657
  debugPrintCond("fmc-exh", c, true);
846
4657
  Trace("fmc-exh")<< std::endl;
847
4657
  QuantifiersBoundInference& qbi = d_qreg.getQuantifiersBoundInference();
848
9314
  RepBoundFmcEntry rbfe(qbi, c, fm);
849
9314
  RepSetIterator riter(fm->getRepSet(), &rbfe);
850
4657
  Trace("fmc-exh-debug") << "Set quantifier..." << std::endl;
851
  //initialize
852
4657
  if (riter.setQuantifier(f))
853
  {
854
4657
    Trace("fmc-exh-debug") << "Set element domains..." << std::endl;
855
4657
    int addedLemmas = 0;
856
    //now do full iteration
857
4657
    Instantiate* ie = d_qim.getInstantiate();
858
59581
    while( !riter.isFinished() ){
859
27462
      d_triedLemmas++;
860
27462
      Trace("fmc-exh-debug") << "Inst : ";
861
54924
      std::vector< Node > ev_inst;
862
54924
      std::vector< Node > inst;
863
97952
      for (unsigned i = 0; i < riter.getNumTerms(); i++)
864
      {
865
140980
        TypeNode tn = riter.getTypeOf(i);
866
        // if the type is not closed enumerable (see
867
        // TypeNode::isClosedEnumerable), then we must ensure that we are
868
        // using a term and not a value. This ensures that e.g. uninterpreted
869
        // constants do not appear in instantiations.
870
140980
        Node rr = riter.getCurrentTerm(i, !tn.isClosedEnumerable());
871
140980
        Node r = fm->getRepresentative(rr);
872
70490
        debugPrint("fmc-exh-debug", r);
873
70490
        Trace("fmc-exh-debug") << " (term : " << rr << ")";
874
70490
        ev_inst.push_back( r );
875
70490
        inst.push_back( rr );
876
      }
877
27462
      int ev_index = d_quant_models[f].getGeneralizationIndex(fm, ev_inst);
878
27462
      Trace("fmc-exh-debug") << ", index = " << ev_index << " / " << d_quant_models[f].d_value.size();
879
54924
      Node ev = ev_index==-1 ? Node::null() : d_quant_models[f].d_value[ev_index];
880
27462
      if (ev!=d_true) {
881
26015
        Trace("fmc-exh-debug") << ", add!";
882
        //add as instantiation
883
52030
        if (ie->addInstantiation(f,
884
                                 inst,
885
                                 InferenceId::QUANTIFIERS_INST_FMF_FMC_EXH,
886
52030
                                 Node::null(),
887
                                 true))
888
        {
889
3683
          Trace("fmc-exh-debug")  << " ...success.";
890
3683
          addedLemmas++;
891
3683
          if (d_qstate.isInConflict() || options::fmfOneInstPerRound())
892
          {
893
            break;
894
          }
895
        }else{
896
22332
          Trace("fmc-exh-debug") << ", failed.";
897
        }
898
      }else{
899
1447
        Trace("fmc-exh-debug") << ", already true";
900
      }
901
27462
      Trace("fmc-exh-debug") << std::endl;
902
27462
      int index = riter.increment();
903
27462
      Trace("fmc-exh-debug") << "Incremented index " << index << std::endl;
904
27462
      if( !riter.isFinished() ){
905
71016
        if (index >= 0 && riter.d_index[index] > 0 && addedLemmas > 0
906
28151
            && riter.d_enum_type[index] == ENUM_CUSTOM)
907
        {
908
2720
          Trace("fmc-exh-debug")
909
1360
              << "Since this is a custom enumeration, skip to the next..."
910
1360
              << std::endl;
911
1360
          riter.incrementAtIndex(index - 1);
912
        }
913
      }
914
    }
915
4657
    d_addedLemmas += addedLemmas;
916
4657
    Trace("fmc-exh") << "----Finished Exhaustive instantiate, lemmas = " << addedLemmas << ", incomplete=" << riter.isIncomplete() << std::endl;
917
4657
    return addedLemmas>0 || !riter.isIncomplete();
918
  }else{
919
    Trace("fmc-exh") << "----Finished Exhaustive instantiate, failed." << std::endl;
920
    return !riter.isIncomplete();
921
  }
922
}
923
924
93043
void FullModelChecker::doCheck(FirstOrderModelFmc * fm, Node f, Def & d, Node n ) {
925
93043
  Trace("fmc-debug") << "Check " << n << " " << n.getKind() << std::endl;
926
  //first check if it is a bounding literal
927
93043
  if( n.hasAttribute(BoundIntLitAttribute()) ){
928
2352
    Trace("fmc-debug") << "It is a bounding literal, polarity = " << n.getAttribute(BoundIntLitAttribute()) << std::endl;
929
2352
    d.addEntry(fm, mkCondDefault(fm, f), n.getAttribute(BoundIntLitAttribute())==1 ? d_false : d_true );
930
90691
  }else if( n.getKind() == kind::BOUND_VARIABLE ){
931
21900
    Trace("fmc-debug") << "Add default entry..." << std::endl;
932
21900
    d.addEntry(fm, mkCondDefault(fm, f), n);
933
  }
934
68791
  else if( n.getKind() == kind::NOT ){
935
    //just do directly
936
6842
    doCheck( fm, f, d, n[0] );
937
6842
    doNegate( d );
938
  }
939
61949
  else if( n.getKind() == kind::FORALL ){
940
950
    d.addEntry(fm, mkCondDefault(fm, f), Node::null());
941
  }
942
60999
  else if( n.getType().isArray() ){
943
    //Trace("fmc-warn") << "WARNING : ARRAYS : Can't process base array " << r << std::endl;
944
    //Trace("fmc-warn") << "          Default value was : " << odefaultValue << std::endl;
945
    //Trace("fmc-debug") << "Can't process base array " << r << std::endl;
946
    //can't process this array
947
375
    d.reset();
948
375
    d.addEntry(fm, mkCondDefault(fm, f), Node::null());
949
  }
950
60624
  else if( n.getNumChildren()==0 ){
951
35234
    Node r = n;
952
17617
    if( !n.isConst() ){
953
15074
      TypeNode tn = n.getType();
954
7537
      if( !fm->hasTerm(n) && tn.isFirstClass() ){
955
150
        r = getSomeDomainElement(fm, tn );
956
      }
957
7537
      r = fm->getRepresentative( r );
958
    }
959
17617
    Trace("fmc-debug") << "Add constant entry..." << std::endl;
960
17617
    d.addEntry(fm, mkCondDefault(fm, f), r);
961
  }
962
  else{
963
86014
    std::vector< int > var_ch;
964
86014
    std::vector< Def > children;
965
123831
    for( int i=0; i<(int)n.getNumChildren(); i++) {
966
161648
      Def dc;
967
80824
      doCheck(fm, f, dc, n[i]);
968
80824
      children.push_back(dc);
969
80824
      if( n[i].getKind() == kind::BOUND_VARIABLE ){
970
21899
        var_ch.push_back(i);
971
      }
972
    }
973
974
43007
    if( n.getKind()==APPLY_UF ){
975
18990
      Trace("fmc-debug") << "Do uninterpreted compose " << n << std::endl;
976
      //uninterpreted compose
977
18990
      doUninterpretedCompose( fm, f, d, n.getOperator(), children );
978
      /*
979
    } else if( n.getKind()==SELECT ){
980
      Trace("fmc-debug") << "Do select compose " << n << std::endl;
981
      std::vector< Def > children2;
982
      children2.push_back( children[1] );
983
      std::vector< Node > cond;
984
      mkCondDefaultVec(fm, f, cond);
985
      std::vector< Node > val;
986
      doUninterpretedCompose(fm, f, d, children[0], children2, 0, cond, val );
987
      */
988
    } else {
989
24017
      if( !var_ch.empty() ){
990
3616
        if( n.getKind()==EQUAL && !n[0].getType().isBoolean() ){
991
699
          if( var_ch.size()==2 ){
992
293
            Trace("fmc-debug") << "Do variable equality " << n << std::endl;
993
293
            doVariableEquality( fm, f, d, n );
994
          }else{
995
406
            Trace("fmc-debug") << "Do variable relation " << n << std::endl;
996
406
            doVariableRelation( fm, f, d, var_ch[0]==0 ? children[1] : children[0], var_ch[0]==0 ? n[0] : n[1] );
997
          }
998
        }else{
999
2917
          Trace("fmc-warn") << "Don't know how to check " << n << std::endl;
1000
2917
          d.addEntry(fm, mkCondDefault(fm, f), Node::null());
1001
        }
1002
      }else{
1003
20401
        Trace("fmc-debug") << "Do interpreted compose " << n << std::endl;
1004
40802
        std::vector< Node > cond;
1005
20401
        mkCondDefaultVec(fm, f, cond);
1006
40802
        std::vector< Node > val;
1007
        //interpreted compose
1008
20401
        doInterpretedCompose( fm, f, d, n, children, 0, cond, val );
1009
      }
1010
    }
1011
43007
    Trace("fmc-debug") << "Simplify the definition..." << std::endl;
1012
43007
    d.debugPrint("fmc-debug", Node::null(), this);
1013
43007
    d.simplify(this, fm);
1014
43007
    Trace("fmc-debug") << "Done simplifying" << std::endl;
1015
  }
1016
93043
  Trace("fmc-debug") << "Definition for " << n << " is : " << std::endl;
1017
93043
  d.debugPrint("fmc-debug", Node::null(), this);
1018
93043
  Trace("fmc-debug") << std::endl;
1019
93043
}
1020
1021
6842
void FullModelChecker::doNegate( Def & dc ) {
1022
15896
  for (unsigned i=0; i<dc.d_cond.size(); i++) {
1023
18108
    Node v = dc.d_value[i];
1024
9054
    if (!v.isNull())
1025
    {
1026
      // In the case that the value is not-constant, we cannot reason about
1027
      // its value (since the range of this must be a constant or variable).
1028
      // In particular, returning null here is important if we have (not x)
1029
      // where x is a bound variable.
1030
7053
      dc.d_value[i] =
1031
14106
          v == d_true ? d_false : (v == d_false ? d_true : Node::null());
1032
    }
1033
  }
1034
6842
}
1035
1036
293
void FullModelChecker::doVariableEquality( FirstOrderModelFmc * fm, Node f, Def & d, Node eq ) {
1037
586
  std::vector<Node> cond;
1038
293
  mkCondDefaultVec(fm, f, cond);
1039
293
  if (eq[0]==eq[1]){
1040
    d.addEntry(fm, mkCond(cond), d_true);
1041
  }else{
1042
586
    TypeNode tn = eq[0].getType();
1043
293
    if( tn.isSort() ){
1044
293
      int j = fm->getVariableId(f, eq[0]);
1045
293
      int k = fm->getVariableId(f, eq[1]);
1046
293
      const RepSet* rs = fm->getRepSet();
1047
293
      if (!rs->hasType(tn))
1048
      {
1049
        getSomeDomainElement( fm, tn );  //to verify the type is initialized
1050
      }
1051
293
      unsigned nreps = rs->getNumRepresentatives(tn);
1052
754
      for (unsigned i = 0; i < nreps; i++)
1053
      {
1054
922
        Node r = fm->getRepresentative(rs->getRepresentative(tn, i));
1055
461
        cond[j+1] = r;
1056
461
        cond[k+1] = r;
1057
461
        d.addEntry( fm, mkCond(cond), d_true);
1058
      }
1059
293
      d.addEntry( fm, mkCondDefault(fm, f), d_false);
1060
    }else{
1061
      d.addEntry( fm, mkCondDefault(fm, f), Node::null());
1062
    }
1063
  }
1064
293
}
1065
1066
406
void FullModelChecker::doVariableRelation( FirstOrderModelFmc * fm, Node f, Def & d, Def & dc, Node v) {
1067
406
  int j = fm->getVariableId(f, v);
1068
946
  for (unsigned i=0; i<dc.d_cond.size(); i++) {
1069
1080
    Node val = dc.d_value[i];
1070
540
    if( val.isNull() ){
1071
9
      d.addEntry( fm, dc.d_cond[i], val);
1072
    }else{
1073
531
      if( dc.d_cond[i][j]!=val ){
1074
409
        if (fm->isStar(dc.d_cond[i][j])) {
1075
794
          std::vector<Node> cond;
1076
397
          mkCondVec(dc.d_cond[i],cond);
1077
397
          cond[j+1] = val;
1078
397
          d.addEntry(fm, mkCond(cond), d_true);
1079
397
          cond[j+1] = fm->getStar(val.getType());
1080
397
          d.addEntry(fm, mkCond(cond), d_false);
1081
        }else{
1082
12
          d.addEntry( fm, dc.d_cond[i], d_false);
1083
        }
1084
      }else{
1085
122
        d.addEntry( fm, dc.d_cond[i], d_true);
1086
      }
1087
    }
1088
  }
1089
406
}
1090
1091
18990
void FullModelChecker::doUninterpretedCompose( FirstOrderModelFmc * fm, Node f, Def & d, Node op, std::vector< Def > & dc ) {
1092
18990
  Trace("fmc-uf-debug") << "Definition : " << std::endl;
1093
18990
  fm->d_models[op]->debugPrint("fmc-uf-debug", op, this);
1094
18990
  Trace("fmc-uf-debug") << std::endl;
1095
1096
37980
  std::vector< Node > cond;
1097
18990
  mkCondDefaultVec(fm, f, cond);
1098
37980
  std::vector< Node > val;
1099
18990
  doUninterpretedCompose( fm, f, d, *fm->d_models[op], dc, 0, cond, val);
1100
18990
}
1101
1102
54468
void FullModelChecker::doUninterpretedCompose( FirstOrderModelFmc * fm, Node f, Def & d,
1103
                                               Def & df, std::vector< Def > & dc, int index,
1104
                                               std::vector< Node > & cond, std::vector<Node> & val ) {
1105
54468
  Trace("fmc-uf-process") << "process at " << index << std::endl;
1106
156288
  for( unsigned i=1; i<cond.size(); i++) {
1107
101820
    debugPrint("fmc-uf-process", cond[i], true);
1108
101820
    Trace("fmc-uf-process") << " ";
1109
  }
1110
54468
  Trace("fmc-uf-process") << std::endl;
1111
54468
  if (index==(int)dc.size()) {
1112
    //we have an entry, now do actual compose
1113
47252
    std::map< int, Node > entries;
1114
23626
    doUninterpretedCompose2( fm, f, entries, 0, cond, val, df.d_et);
1115
23626
    if( entries.empty() ){
1116
897
      d.addEntry(fm, mkCond(cond), Node::null());
1117
    }else{
1118
      //add them to the definition
1119
68161
      for( unsigned e=0; e<df.d_cond.size(); e++ ){
1120
45432
        if ( entries.find(e)!=entries.end() ){
1121
37899
          Trace("fmf-uf-process-debug") << "Add entry..." << std::endl;
1122
37899
          d.addEntry(fm, entries[e], df.d_value[e] );
1123
37899
          Trace("fmf-uf-process-debug") << "Done add entry." << std::endl;
1124
        }
1125
      }
1126
    }
1127
  }else{
1128
72202
    for (unsigned i=0; i<dc[index].d_cond.size(); i++) {
1129
41360
      if (isCompat(fm, cond, dc[index].d_cond[i])!=0) {
1130
70956
        std::vector< Node > new_cond;
1131
35478
        new_cond.insert(new_cond.end(), cond.begin(), cond.end());
1132
35478
        if( doMeet(fm, new_cond, dc[index].d_cond[i]) ){
1133
35478
          Trace("fmc-uf-process") << "index " << i << " succeeded meet." << std::endl;
1134
35478
          val.push_back(dc[index].d_value[i]);
1135
35478
          doUninterpretedCompose(fm, f, d, df, dc, index+1, new_cond, val);
1136
35478
          val.pop_back();
1137
        }else{
1138
          Trace("fmc-uf-process") << "index " << i << " failed meet." << std::endl;
1139
        }
1140
      }
1141
    }
1142
  }
1143
54468
}
1144
1145
82333
void FullModelChecker::doUninterpretedCompose2( FirstOrderModelFmc * fm, Node f,
1146
                                                std::map< int, Node > & entries, int index,
1147
                                                std::vector< Node > & cond, std::vector< Node > & val,
1148
                                                EntryTrie & curr ) {
1149
82333
  Trace("fmc-uf-process") << "compose " << index << " / " << val.size() << std::endl;
1150
236027
  for( unsigned i=1; i<cond.size(); i++) {
1151
153694
    debugPrint("fmc-uf-process", cond[i], true);
1152
153694
    Trace("fmc-uf-process") << " ";
1153
  }
1154
82333
  Trace("fmc-uf-process") << std::endl;
1155
82333
  if (index==(int)val.size()) {
1156
75798
    Node c = mkCond(cond);
1157
37899
    Trace("fmc-uf-entry") << "Entry : " << c << " -> index[" << curr.d_data << "]" << std::endl;
1158
37899
    entries[curr.d_data] = c;
1159
  }else{
1160
88868
    Node v = val[index];
1161
44434
    Trace("fmc-uf-process") << "Process " << v << std::endl;
1162
44434
    bool bind_var = false;
1163
44434
    if( !v.isNull() && v.getKind()==kind::BOUND_VARIABLE ){
1164
21068
      int j = fm->getVariableId(f, v);
1165
21068
      Trace("fmc-uf-process") << v << " is variable #" << j << std::endl;
1166
21068
      if (!fm->isStar(cond[j + 1]))
1167
      {
1168
38
        v = cond[j+1];
1169
      }else{
1170
21030
        bind_var = true;
1171
      }
1172
    }
1173
44434
    if (bind_var) {
1174
21030
      Trace("fmc-uf-process") << "bind variable..." << std::endl;
1175
21030
      int j = fm->getVariableId(f, v);
1176
21030
      Assert(fm->isStar(cond[j + 1]));
1177
54378
      for (std::map<Node, EntryTrie>::iterator it = curr.d_child.begin();
1178
54378
           it != curr.d_child.end();
1179
           ++it)
1180
      {
1181
33348
        cond[j + 1] = it->first;
1182
33348
        doUninterpretedCompose2(
1183
33348
            fm, f, entries, index + 1, cond, val, it->second);
1184
      }
1185
21030
      cond[j + 1] = fm->getStar(v.getType());
1186
    }else{
1187
23404
      if( !v.isNull() ){
1188
22501
        if (curr.d_child.find(v) != curr.d_child.end())
1189
        {
1190
6511
          Trace("fmc-uf-process") << "follow value..." << std::endl;
1191
6511
          doUninterpretedCompose2(
1192
6511
              fm, f, entries, index + 1, cond, val, curr.d_child[v]);
1193
        }
1194
45002
        Node st = fm->getStar(v.getType());
1195
22501
        if (curr.d_child.find(st) != curr.d_child.end())
1196
        {
1197
18848
          Trace("fmc-uf-process") << "follow star..." << std::endl;
1198
18848
          doUninterpretedCompose2(
1199
18848
              fm, f, entries, index + 1, cond, val, curr.d_child[st]);
1200
        }
1201
      }
1202
    }
1203
  }
1204
82333
}
1205
1206
84960
void FullModelChecker::doInterpretedCompose( FirstOrderModelFmc * fm, Node f, Def & d, Node n,
1207
                                             std::vector< Def > & dc, int index,
1208
                                             std::vector< Node > & cond, std::vector<Node> & val ) {
1209
84960
  Trace("fmc-if-process") << "int compose " << index << " / " << dc.size() << std::endl;
1210
212346
  for( unsigned i=1; i<cond.size(); i++) {
1211
127386
    debugPrint("fmc-if-process", cond[i], true);
1212
127386
    Trace("fmc-if-process") << " ";
1213
  }
1214
84960
  Trace("fmc-if-process") << std::endl;
1215
84960
  if ( index==(int)dc.size() ){
1216
69614
    Node c = mkCond(cond);
1217
69614
    Node v = evaluateInterpreted(n, val);
1218
34807
    d.addEntry(fm, c, v);
1219
  }
1220
  else {
1221
100306
    TypeNode vtn = n.getType();
1222
132076
    for (unsigned i=0; i<dc[index].d_cond.size(); i++) {
1223
81923
      if (isCompat(fm, cond, dc[index].d_cond[i])!=0) {
1224
142324
        std::vector< Node > new_cond;
1225
71162
        new_cond.insert(new_cond.end(), cond.begin(), cond.end());
1226
71162
        if( doMeet(fm, new_cond, dc[index].d_cond[i]) ){
1227
71162
          bool process = true;
1228
71162
          if (vtn.isBoolean()) {
1229
            //short circuit
1230
91251
            if( (n.getKind()==OR && dc[index].d_value[i]==d_true) ||
1231
42093
                (n.getKind()==AND && dc[index].d_value[i]==d_false) ){
1232
13206
              Node c = mkCond(new_cond);
1233
6603
              d.addEntry(fm, c, dc[index].d_value[i]);
1234
6603
              process = false;
1235
            }
1236
          }
1237
71162
          if (process) {
1238
64559
            val.push_back(dc[index].d_value[i]);
1239
64559
            doInterpretedCompose(fm, f, d, n, dc, index+1, new_cond, val);
1240
64559
            val.pop_back();
1241
          }
1242
        }
1243
      }
1244
    }
1245
  }
1246
84960
}
1247
1248
123283
int FullModelChecker::isCompat( FirstOrderModelFmc * fm, std::vector< Node > & cond, Node c ) {
1249
123283
  Trace("fmc-debug3") << "isCompat " << c << std::endl;
1250
123283
  Assert(cond.size() == c.getNumChildren() + 1);
1251
327315
  for (unsigned i=1; i<cond.size(); i++) {
1252
220675
    if (cond[i] != c[i - 1] && !fm->isStar(cond[i]) && !fm->isStar(c[i - 1]))
1253
    {
1254
16643
      return 0;
1255
    }
1256
  }
1257
106640
  return 1;
1258
}
1259
1260
106640
bool FullModelChecker::doMeet( FirstOrderModelFmc * fm, std::vector< Node > & cond, Node c ) {
1261
106640
  Trace("fmc-debug3") << "doMeet " << c << std::endl;
1262
106640
  Assert(cond.size() == c.getNumChildren() + 1);
1263
295347
  for (unsigned i=1; i<cond.size(); i++) {
1264
188707
    if( cond[i]!=c[i-1] ) {
1265
54798
      if (fm->isStar(cond[i]))
1266
      {
1267
29350
        cond[i] = c[i - 1];
1268
      }
1269
25448
      else if (!fm->isStar(c[i - 1]))
1270
      {
1271
        return false;
1272
      }
1273
    }
1274
  }
1275
106640
  return true;
1276
}
1277
1278
131173
Node FullModelChecker::mkCond( std::vector< Node > & cond ) {
1279
131173
  return NodeManager::currentNM()->mkNode(APPLY_UF, cond);
1280
}
1281
1282
48547
Node FullModelChecker::mkCondDefault( FirstOrderModelFmc * fm, Node f) {
1283
97094
  std::vector< Node > cond;
1284
48547
  mkCondDefaultVec(fm, f, cond);
1285
97094
  return mkCond(cond);
1286
}
1287
1288
88231
void FullModelChecker::mkCondDefaultVec( FirstOrderModelFmc * fm, Node f, std::vector< Node > & cond ) {
1289
88231
  Trace("fmc-debug") << "Make default vec" << std::endl;
1290
  //get function symbol for f
1291
88231
  cond.push_back(d_quant_cond[f]);
1292
229588
  for (unsigned i=0; i<f[0].getNumChildren(); i++) {
1293
282714
    Node ts = fm->getStar(f[0][i].getType());
1294
141357
    Assert(ts.getType() == f[0][i].getType());
1295
141357
    cond.push_back(ts);
1296
  }
1297
88231
}
1298
1299
397
void FullModelChecker::mkCondVec( Node n, std::vector< Node > & cond ) {
1300
397
  cond.push_back(n.getOperator());
1301
914
  for( unsigned i=0; i<n.getNumChildren(); i++ ){
1302
517
    cond.push_back( n[i] );
1303
  }
1304
397
}
1305
1306
34807
Node FullModelChecker::evaluateInterpreted( Node n, std::vector< Node > & vals ) {
1307
34807
  if( n.getKind()==EQUAL && !n[0].getType().isBoolean() ){
1308
9233
    if (!vals[0].isNull() && !vals[1].isNull()) {
1309
7733
      return vals[0]==vals[1] ? d_true : d_false;
1310
    }else{
1311
1500
      return Node::null();
1312
    }
1313
25574
  }else if( n.getKind()==ITE ){
1314
2345
    if( vals[0]==d_true ){
1315
1050
      return vals[1];
1316
1295
    }else if( vals[0]==d_false ){
1317
1217
      return vals[2];
1318
    }else{
1319
78
      return vals[1]==vals[2] ? vals[1] : Node::null();
1320
    }
1321
23229
  }else if( n.getKind()==AND || n.getKind()==OR ){
1322
4871
    bool isNull = false;
1323
18860
    for (unsigned i=0; i<vals.size(); i++) {
1324
13989
      if((vals[i]==d_true && n.getKind()==OR) || (vals[i]==d_false && n.getKind()==AND)) {
1325
        return vals[i];
1326
13989
      }else if( vals[i].isNull() ){
1327
2982
        isNull = true;
1328
      }
1329
    }
1330
4871
    return isNull ? Node::null() : vals[0];
1331
  }else{
1332
36716
    std::vector<Node> children;
1333
18358
    if( n.getMetaKind() == kind::metakind::PARAMETERIZED ){
1334
3283
      children.push_back( n.getOperator() );
1335
    }
1336
48452
    for (unsigned i=0; i<vals.size(); i++) {
1337
33120
      if( vals[i].isNull() ){
1338
3026
        return Node::null();
1339
      }else{
1340
30094
        children.push_back( vals[i] );
1341
      }
1342
    }
1343
30664
    Node nc = NodeManager::currentNM()->mkNode(n.getKind(), children);
1344
15332
    Trace("fmc-eval") << "Evaluate " << nc << " to ";
1345
15332
    nc = Rewriter::rewrite(nc);
1346
15332
    Trace("fmc-eval") << nc << std::endl;
1347
15332
    return nc;
1348
  }
1349
}
1350
1351
3014
Node FullModelChecker::getSomeDomainElement( FirstOrderModelFmc * fm, TypeNode tn ) {
1352
3014
  bool addRepId = !fm->getRepSet()->hasType(tn);
1353
3014
  Node de = fm->getSomeDomainElement(tn);
1354
3014
  if( addRepId ){
1355
102
    d_rep_ids[tn][de] = 0;
1356
  }
1357
3014
  return de;
1358
}
1359
1360
6151
Node FullModelChecker::getFunctionValue(FirstOrderModelFmc * fm, Node op, const char* argPrefix ) {
1361
6151
  return fm->getFunctionValue(op, argPrefix);
1362
}
1363
1364
1365
32055
bool FullModelChecker::useSimpleModels() {
1366
32055
  return options::fmfFmcSimple();
1367
}
1368
17685
void FullModelChecker::registerQuantifiedFormula(Node q)
1369
{
1370
17685
  if (d_quant_cond.find(q) != d_quant_cond.end())
1371
  {
1372
16572
    return;
1373
  }
1374
1113
  NodeManager* nm = NodeManager::currentNM();
1375
1113
  SkolemManager* sm = nm->getSkolemManager();
1376
2226
  std::vector<TypeNode> types;
1377
2845
  for (const Node& v : q[0])
1378
  {
1379
3464
    TypeNode tn = v.getType();
1380
1732
    if (tn.isFunction())
1381
    {
1382
      // we will not use model-based quantifier instantiation for q, since
1383
      // the model-based instantiation algorithm does not handle (universally
1384
      // quantified) functions
1385
24
      d_unhandledQuant.insert(q);
1386
    }
1387
1732
    types.push_back(tn);
1388
  }
1389
2226
  TypeNode typ = nm->mkFunctionType(types, nm->booleanType());
1390
2226
  Node op = sm->mkDummySkolem("qfmc", typ, "op for full-model checking");
1391
1113
  d_quant_cond[q] = op;
1392
}
1393
1394
17685
bool FullModelChecker::isHandled(Node q) const
1395
{
1396
17685
  return d_unhandledQuant.find(q) == d_unhandledQuant.end();
1397
}
1398
1399
}  // namespace fmcheck
1400
}  // namespace quantifiers
1401
}  // namespace theory
1402
22755
}  // namespace cvc5