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-29 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
159490
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
52255
  bool operator() (int i,int j) {
48
52255
    return (d_mba_count[d_terms[i]] < d_mba_count[d_terms[j]]);
49
  }
50
};
51
52
355241
bool EntryTrie::hasGeneralization( FirstOrderModelFmc * m, Node c, int index ) {
53
355241
  if (index==(int)c.getNumChildren()) {
54
14096
    return d_data!=-1;
55
  }else{
56
682290
    TypeNode tn = c[index].getType();
57
682290
    Node st = m->getStar(tn);
58
341145
    if(d_child.find(st)!=d_child.end()) {
59
39475
      if( d_child[st].hasGeneralization(m, c, index+1) ){
60
14507
        return true;
61
      }
62
    }
63
326638
    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
306686
    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
304455
    return false;
93
  }
94
}
95
96
122094
int EntryTrie::getGeneralizationIndex( FirstOrderModelFmc * m, std::vector<Node> & inst, int index ) {
97
122094
  Debug("fmc-entry-trie") << "Get generalization index " << inst.size() << " " << index << std::endl;
98
122094
  if (index==(int)inst.size()) {
99
34018
    return d_data;
100
  }else{
101
88076
    int minIndex = -1;
102
176152
    Node st = m->getStar(inst[index].getType());
103
88076
    if (d_child.find(st) != d_child.end())
104
    {
105
82803
      minIndex = d_child[st].getGeneralizationIndex(m, inst, index + 1);
106
    }
107
176152
    Node cc = inst[index];
108
88076
    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
88076
    return minIndex;
117
  }
118
}
119
120
654501
void EntryTrie::addEntry( FirstOrderModelFmc * m, Node c, Node v, int data, int index ) {
121
654501
  if (index==(int)c.getNumChildren()) {
122
239613
    if(d_data==-1) {
123
239613
      d_data = data;
124
    }
125
  }
126
  else {
127
414888
    d_child[ c[index] ].addEntry(m,c,v,data,index+1);
128
414888
    if( d_complete==0 ){
129
      d_complete = -1;
130
    }
131
  }
132
654501
}
133
134
281067
void EntryTrie::getEntries( FirstOrderModelFmc * m, Node c, std::vector<int> & compat, std::vector<int> & gen, int index, bool is_gen ) {
135
281067
  if (index==(int)c.getNumChildren()) {
136
54963
    if( d_data!=-1) {
137
54963
      if( is_gen ){
138
50911
        gen.push_back(d_data);
139
      }
140
54963
      compat.push_back(d_data);
141
    }
142
  }else{
143
226104
    if (m->isStar(c[index])) {
144
234804
      for ( std::map<Node,EntryTrie>::iterator it = d_child.begin(); it != d_child.end(); ++it ){
145
96050
        it->second.getEntries(m, c, compat, gen, index+1, is_gen );
146
      }
147
    }else{
148
174700
      Node st = m->getStar(c[index].getType());
149
87350
      if(d_child.find(st)!=d_child.end()) {
150
6008
        d_child[st].getEntries(m, c, compat, gen, index+1, false);
151
      }
152
87350
      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
281067
}
159
160
250596
bool Def::addEntry( FirstOrderModelFmc * m, Node c, Node v) {
161
250596
  if (d_et.hasGeneralization(m, c)) {
162
10983
    Trace("fmc-debug") << "Already has generalization, skip." << std::endl;
163
10983
    return false;
164
  }
165
239613
  int newIndex = (int)d_cond.size();
166
239613
  if (!d_has_simplified) {
167
301664
    std::vector<int> compat;
168
301664
    std::vector<int> gen;
169
150832
    d_et.getEntries(m, c, compat, gen);
170
205795
    for( unsigned i=0; i<compat.size(); i++) {
171
54963
      if( d_status[compat[i]]==status_unk ){
172
47900
        if( d_value[compat[i]]!=v ){
173
30065
          d_status[compat[i]] = status_non_redundant;
174
        }
175
      }
176
    }
177
201743
    for( unsigned i=0; i<gen.size(); i++) {
178
50911
      if( d_status[gen[i]]==status_unk ){
179
15911
        if( d_value[gen[i]]==v ){
180
15911
          d_status[gen[i]] = status_redundant;
181
        }
182
      }
183
    }
184
150832
    d_status.push_back( status_unk );
185
  }
186
239613
  d_et.addEntry(m, c, v, newIndex);
187
239613
  d_cond.push_back(c);
188
239613
  d_value.push_back(v);
189
239613
  return true;
190
}
191
192
2471
Node Def::evaluate( FirstOrderModelFmc * m, std::vector<Node>& inst ) {
193
2471
  int gindex = d_et.getGeneralizationIndex(m, inst);
194
2471
  if (gindex!=-1) {
195
2467
    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
27393
int Def::getGeneralizationIndex( FirstOrderModelFmc * m, std::vector<Node>& inst ) {
203
27393
  return d_et.getGeneralizationIndex(m, inst);
204
}
205
206
53931
void Def::basic_simplify( FirstOrderModelFmc * m ) {
207
53931
  d_has_simplified = true;
208
107862
  std::vector< Node > cond;
209
53931
  cond.insert( cond.end(), d_cond.begin(), d_cond.end() );
210
53931
  d_cond.clear();
211
107862
  std::vector< Node > value;
212
53931
  value.insert( value.end(), d_value.begin(), d_value.end() );
213
53931
  d_value.clear();
214
53931
  d_et.reset();
215
158623
  for (unsigned i=0; i<d_status.size(); i++) {
216
104692
    if( d_status[i]!=status_redundant ){
217
88781
      addEntry(m, cond[i], value[i]);
218
    }
219
  }
220
53931
  d_status.clear();
221
53931
}
222
223
48605
void Def::simplify(FullModelChecker * mc, FirstOrderModelFmc * m) {
224
48605
  Trace("fmc-simplify") << "Simplify definition, #cond = " << d_cond.size() << std::endl;
225
48605
  basic_simplify( m );
226
48605
  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
48605
  if( !d_cond.empty() ){
230
48605
    bool last_all_stars = true;
231
97210
    Node cc = d_cond[d_cond.size()-1];
232
117313
    for( unsigned i=0; i<cc.getNumChildren(); i++ ){
233
74034
      if (!m->isStar(cc[i]))
234
      {
235
5326
        last_all_stars = false;
236
5326
        break;
237
      }
238
    }
239
48605
    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
48605
  Trace("fmc-simplify") << "finish simplify, #cond = " << d_cond.size() << std::endl;
271
48605
}
272
273
181554
void Def::debugPrint(const char * tr, Node op, FullModelChecker * m) {
274
181554
  if (!op.isNull()) {
275
31126
    Trace(tr) << "Model for " << op << " : " << std::endl;
276
  }
277
469564
  for( unsigned i=0; i<d_cond.size(); i++) {
278
    //print the condition
279
288010
    if (!op.isNull()) {
280
73965
      Trace(tr) << op;
281
    }
282
288010
    m->debugPrintCond(tr, d_cond[i], true);
283
288010
    Trace(tr) << " -> ";
284
288010
    m->debugPrint(tr, d_value[i]);
285
288010
    Trace(tr) << std::endl;
286
  }
287
181554
}
288
289
891
FullModelChecker::FullModelChecker(Env& env,
290
                                   QuantifiersState& qs,
291
                                   QuantifiersInferenceManager& qim,
292
                                   QuantifiersRegistry& qr,
293
891
                                   TermRegistry& tr)
294
    : QModelBuilder(env, qs, qim, qr, tr),
295
891
      d_fm(new FirstOrderModelFmc(env, qs, qr, tr))
296
{
297
891
  d_true = NodeManager::currentNM()->mkConst(true);
298
891
  d_false = NodeManager::currentNM()->mkConst(false);
299
891
}
300
301
891
void FullModelChecker::finishInit() { d_model = d_fm.get(); }
302
303
1673
bool FullModelChecker::preProcessBuildModel(TheoryModel* m) {
304
  //standard pre-process
305
1673
  if( !preProcessBuildModelStd( m ) ){
306
    return false;
307
  }
308
309
1673
  Trace("fmc") << "---Full Model Check preprocess() " << std::endl;
310
1673
  d_preinitialized_eqc.clear();
311
1673
  d_preinitialized_types.clear();
312
  //traverse equality engine
313
1673
  eq::EqClassesIterator eqcs_i = eq::EqClassesIterator(m->getEqualityEngine());
314
64587
  while( !eqcs_i.isFinished() ){
315
62914
    Node r = *eqcs_i;
316
62914
    TypeNode tr = r.getType();
317
31457
    d_preinitialized_eqc[tr] = r;
318
31457
    ++eqcs_i;
319
  }
320
321
  //must ensure model basis terms exists in model for each relevant type
322
1673
  Trace("fmc") << "preInitialize types..." << std::endl;
323
1673
  d_fm->initialize();
324
7794
  for (std::pair<const Node, Def*>& mp : d_fm->d_models)
325
  {
326
12242
    Node op = mp.first;
327
6121
    Trace("fmc") << "preInitialize types for " << op << std::endl;
328
12242
    TypeNode tno = op.getType();
329
22934
    for( unsigned i=0; i<tno.getNumChildren(); i++) {
330
16813
      Trace("fmc") << "preInitializeType " << tno[i] << std::endl;
331
16813
      preInitializeType(m, tno[i]);
332
16813
      Trace("fmc") << "finished preInitializeType " << tno[i] << std::endl;
333
    }
334
  }
335
1673
  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
9462
  for (unsigned i = 0, nquant = d_fm->getNumAssertedQuantifiers(); i < nquant;
338
       i++)
339
  {
340
15467
    Node q = d_fm->getAssertedQuantifier(i);
341
7789
    registerQuantifiedFormula(q);
342
7789
    if (!isHandled(q))
343
    {
344
111
      continue;
345
    }
346
    // make sure all types are set
347
20026
    for (const Node& v : q[0])
348
    {
349
12348
      preInitializeType(m, v.getType());
350
    }
351
  }
352
1673
  return true;
353
}
354
355
1673
bool FullModelChecker::processBuildModel(TheoryModel* m){
356
1673
  if (!m->areFunctionValuesEnabled())
357
  {
358
    // nothing to do if no functions
359
    return true;
360
  }
361
1673
  FirstOrderModelFmc* fm = d_fm.get();
362
1673
  Trace("fmc") << "---Full Model Check reset() " << std::endl;
363
1673
  d_quant_models.clear();
364
1673
  d_rep_ids.clear();
365
1673
  d_star_insts.clear();
366
  //process representatives
367
1673
  RepSet* rs = m->getRepSetPtr();
368
7035
  for (std::map<TypeNode, std::vector<Node> >::iterator it =
369
1673
           rs->d_type_reps.begin();
370
8708
       it != rs->d_type_reps.end();
371
       ++it)
372
  {
373
7035
    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
7794
  for (std::pair<const Node, Def*>& fmm : d_fm->d_models)
397
  {
398
12242
    Node op = fmm.first;
399
    //reset the model
400
6121
    d_fm->d_models[op]->reset();
401
402
12242
    std::vector< Node > add_conds;
403
12242
    std::vector< Node > add_values;
404
6121
    bool needsDefault = true;
405
6121
    if (m->hasUfTerms(op))
406
    {
407
4462
      const std::vector<Node>& uft = m->getUfTerms(op);
408
8924
      Trace("fmc-model-debug")
409
4462
          << uft.size() << " model values for " << op << " ... " << std::endl;
410
30327
      for (const Node& n : uft)
411
      {
412
        // only consider unique up to congruence (in model equality engine)?
413
25865
        add_conds.push_back( n );
414
25865
        add_values.push_back( n );
415
51730
        Node r = m->getRepresentative(n);
416
25865
        Trace("fmc-model-debug") << n << " -> " << r << std::endl;
417
      }
418
    }else{
419
1659
      Trace("fmc-model-debug") << "No model values for " << op << " ... " << std::endl;
420
    }
421
6121
    Trace("fmc-model-debug") << std::endl;
422
    //possibly get default
423
6121
    if( needsDefault ){
424
12242
      Node nmb = d_fm->getModelBasisOpTerm(op);
425
      //add default value if necessary
426
6121
      if (m->hasTerm(nmb))
427
      {
428
3275
        Trace("fmc-model-debug") << "Add default " << nmb << std::endl;
429
3275
        add_conds.push_back( nmb );
430
3275
        add_values.push_back( nmb );
431
      }else{
432
5692
        Node vmb = getSomeDomainElement(d_fm.get(), nmb.getType());
433
2846
        Trace("fmc-model-debug") << "Add default to default representative " << nmb << " ";
434
5692
        Trace("fmc-model-debug")
435
5692
            << m->getRepSet()->getNumRepresentatives(nmb.getType())
436
2846
            << std::endl;
437
2846
        add_conds.push_back( nmb );
438
2846
        add_values.push_back( vmb );
439
      }
440
    }
441
442
12242
    std::vector< Node > conds;
443
12242
    std::vector< Node > values;
444
12242
    std::vector< Node > entry_conds;
445
    //get the entries for the model
446
38107
    for( size_t i=0; i<add_conds.size(); i++ ){
447
63972
      Node c = add_conds[i];
448
63972
      Node v = add_values[i];
449
63972
      Trace("fmc-model-debug")
450
31986
          << "Add cond/value : " << c << " -> " << v << std::endl;
451
63972
      std::vector< Node > children;
452
63972
      std::vector< Node > entry_children;
453
31986
      children.push_back(op);
454
31986
      entry_children.push_back(op);
455
31986
      bool hasNonStar = false;
456
99127
      for (const Node& ci : c)
457
      {
458
134282
        Node ri = fm->getRepresentative(ci);
459
67141
        children.push_back(ri);
460
67141
        bool isStar = false;
461
67141
        if (fm->isModelBasisTerm(ri))
462
        {
463
20439
          ri = fm->getStar(ri.getType());
464
20439
          isStar = true;
465
        }
466
        else
467
        {
468
46702
          hasNonStar = true;
469
        }
470
67141
        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
67141
        entry_children.push_back(ri);
477
      }
478
63972
      Node n = NodeManager::currentNM()->mkNode( APPLY_UF, children );
479
63972
      Node nv = fm->getRepresentative( v );
480
63972
      Trace("fmc-model-debug")
481
31986
          << "Representative of " << v << " is " << nv << std::endl;
482
31986
      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
63972
      Node en = (useSimpleModels() && hasNonStar) ? n : NodeManager::currentNM()->mkNode( APPLY_UF, entry_children );
487
31986
      if( std::find(conds.begin(), conds.end(), n )==conds.end() ){
488
21209
        Trace("fmc-model-debug") << "- add " << n << " -> " << nv << " (entry is " << en << ")" << std::endl;
489
21209
        conds.push_back(n);
490
21209
        values.push_back(nv);
491
21209
        entry_conds.push_back(en);
492
      }
493
      else {
494
10777
        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
12242
    std::vector< int > indices;
501
12242
    ModelBasisArgSort mbas;
502
27330
    for (int i=0; i<(int)conds.size(); i++) {
503
21209
      mbas.d_terms.push_back(conds[i]);
504
21209
      mbas.d_mba_count[conds[i]] = fm->getModelBasisArg(conds[i]);
505
21209
      indices.push_back(i);
506
    }
507
6121
    std::sort( indices.begin(), indices.end(), mbas );
508
509
27330
    for (int i=0; i<(int)indices.size(); i++) {
510
21209
      fm->d_models[op]->addEntry(fm, entry_conds[indices[i]], values[indices[i]]);
511
    }
512
513
6121
    Trace("fmc-model-simplify") << "Before simplification : " << std::endl;
514
6121
    fm->d_models[op]->debugPrint("fmc-model-simplify", op, this);
515
6121
    Trace("fmc-model-simplify") << std::endl;
516
517
6121
    Trace("fmc-model-simplify") << "Simplifying " << op << "..." << std::endl;
518
6121
    fm->d_models[op]->simplify( this, fm );
519
520
6121
    fm->d_models[op]->debugPrint("fmc-model", op, this);
521
6121
    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
1673
  Assert(d_addedLemmas == 0);
539
540
  //make function values
541
7794
  for( std::map<Node, Def * >::iterator it = fm->d_models.begin(); it != fm->d_models.end(); ++it ){
542
12242
    Node f_def = getFunctionValue( fm, it->first, "$x" );
543
6121
    m->assignFunctionDefinition( it->first, f_def );
544
  }
545
1673
  return TheoryEngineModelBuilder::processBuildModel( m );
546
}
547
548
29161
void FullModelChecker::preInitializeType(TheoryModel* m, TypeNode tn)
549
{
550
29161
  if( d_preinitialized_types.find( tn )==d_preinitialized_types.end() ){
551
3889
    d_preinitialized_types[tn] = true;
552
3889
    if (tn.isFirstClass())
553
    {
554
3883
      Trace("fmc") << "Get model basis term " << tn << "..." << std::endl;
555
7766
      Node mb = d_fm->getModelBasisTerm(tn);
556
3883
      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
3883
      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
29161
}
581
582
292631
void FullModelChecker::debugPrintCond(const char * tr, Node n, bool dispStar) {
583
292631
  Trace(tr) << "(";
584
790195
  for( unsigned j=0; j<n.getNumChildren(); j++) {
585
497564
    if( j>0 ) Trace(tr) << ", ";
586
497564
    debugPrint(tr, n[j], dispStar);
587
  }
588
292631
  Trace(tr) << ")";
589
292631
}
590
591
1237076
void FullModelChecker::debugPrint(const char * tr, Node n, bool dispStar) {
592
1237076
  if( n.isNull() ){
593
24888
    Trace(tr) << "null";
594
  }
595
1212188
  else if (FirstOrderModelFmc::isStar(n) && dispStar)
596
  {
597
602085
    Trace(tr) << "*";
598
  }
599
  else
600
  {
601
1220206
    TypeNode tn = n.getType();
602
610103
    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
315106
      Trace(tr) << n;
610
    }
611
  }
612
1237076
}
613
614
615
9840
int FullModelChecker::doExhaustiveInstantiation( FirstOrderModel * fm, Node f, int effort ) {
616
9840
  Trace("fmc") << "Full model check " << f << ", effort = " << effort << "..." << std::endl;
617
  // register the quantifier
618
9840
  registerQuantifiedFormula(f);
619
9840
  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
9840
  if (!optUseModel() || !isHandled(f))
623
  {
624
    return 0;
625
  }
626
9840
  FirstOrderModelFmc* fmfmc = static_cast<FirstOrderModelFmc*>(fm);
627
9840
  if (effort == 0)
628
  {
629
7500
    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
5357
    doCheck(fmfmc, f, d_quant_models[f], f[1]);
642
5357
    std::vector<Node>& mcond = d_quant_models[f].d_cond;
643
5357
    Trace("fmc") << "Definition for quantifier " << f << " is : " << std::endl;
644
5357
    Assert(!mcond.empty());
645
5357
    d_quant_models[f].debugPrint("fmc", Node::null(), this);
646
5357
    Trace("fmc") << std::endl;
647
648
    // consider all entries going to non-true
649
5357
    Instantiate* instq = d_qim.getInstantiate();
650
12206
    for (unsigned i = 0, msize = mcond.size(); i < msize; i++)
651
    {
652
6849
      if (d_quant_models[f].d_value[i] == d_true)
653
      {
654
        // already satisfied
655
9402
        continue;
656
      }
657
5450
      Trace("fmc-inst") << "Instantiate based on " << mcond[i] << "..."
658
2725
                        << std::endl;
659
2725
      bool hasStar = false;
660
4296
      std::vector<Node> inst;
661
7110
      for (unsigned j = 0, nchild = mcond[i].getNumChildren(); j < nchild; j++)
662
      {
663
4385
        if (fmfmc->isStar(mcond[i][j]))
664
        {
665
3528
          hasStar = true;
666
3528
          inst.push_back(fmfmc->getModelBasisTerm(mcond[i][j].getType()));
667
        }
668
        else
669
        {
670
857
          inst.push_back(mcond[i][j]);
671
        }
672
      }
673
2725
      bool addInst = true;
674
2725
      if (hasStar)
675
      {
676
        // try obvious (specified by inst)
677
4942
        Node ev = d_quant_models[f].evaluate(fmfmc, inst);
678
2471
        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
2734
      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
2716
      if (options::fmfBound() || options::stringExp())
713
      {
714
2290
        std::vector<Node> cond;
715
1145
        cond.push_back(d_quant_cond[f]);
716
1145
        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
2290
        Node c = mkCond(cond);
720
1145
        unsigned prevInst = d_addedLemmas;
721
1145
        exhaustiveInstantiate(fmfmc, f, c);
722
1145
        if (d_addedLemmas == prevInst)
723
        {
724
874
          d_star_insts[f].push_back(i);
725
        }
726
1145
        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
5357
    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
2340
  std::vector<Node>& mcond = d_quant_models[f].d_cond;
757
2340
  if (!d_star_insts[f].empty())
758
  {
759
1291
    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
2558
    Def temp;
767
    // simplify the exceptions?
768
2654
    for (int i = (d_star_insts[f].size() - 1); i >= 0; i--)
769
    {
770
      // get witness for d_star_insts[f][i]
771
1387
      int j = d_star_insts[f][i];
772
1387
      if (temp.addEntry(fmfmc, mcond[j], d_quant_models[f].d_value[j]))
773
      {
774
1333
        if (!exhaustiveInstantiate(fmfmc, f, mcond[j]))
775
        {
776
          // something went wrong, resort to exhaustive instantiation
777
24
          return 0;
778
        }
779
      }
780
    }
781
  }
782
2316
  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
4621
  RepBoundFmcEntry(QuantifiersBoundInference& qbi,
813
                   Node e,
814
                   FirstOrderModelFmc* f)
815
4621
      : QRepBoundExt(qbi, f), d_entry(e), d_fm(f)
816
  {
817
4621
  }
818
4621
  ~RepBoundFmcEntry() {}
819
  /** set bound */
820
6363
  virtual RsiEnumType setBound(Node owner,
821
                               unsigned i,
822
                               std::vector<Node>& elements) override
823
  {
824
6363
    if (!d_fm->isStar(d_entry[i]))
825
    {
826
      // only need to consider the single point
827
1433
      elements.push_back(d_entry[i]);
828
1433
      return ENUM_DEFAULT;
829
    }
830
4930
    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
4621
bool FullModelChecker::exhaustiveInstantiate(FirstOrderModelFmc* fm,
841
                                             Node f,
842
                                             Node c)
843
{
844
4621
  Trace("fmc-exh") << "----Exhaustive instantiate based on " << c << " ";
845
4621
  debugPrintCond("fmc-exh", c, true);
846
4621
  Trace("fmc-exh")<< std::endl;
847
4621
  QuantifiersBoundInference& qbi = d_qreg.getQuantifiersBoundInference();
848
9242
  RepBoundFmcEntry rbfe(qbi, c, fm);
849
9242
  RepSetIterator riter(fm->getRepSet(), &rbfe);
850
4621
  Trace("fmc-exh-debug") << "Set quantifier..." << std::endl;
851
  //initialize
852
4621
  if (riter.setQuantifier(f))
853
  {
854
4621
    Trace("fmc-exh-debug") << "Set element domains..." << std::endl;
855
4621
    int addedLemmas = 0;
856
    //now do full iteration
857
4621
    Instantiate* ie = d_qim.getInstantiate();
858
59407
    while( !riter.isFinished() ){
859
27393
      d_triedLemmas++;
860
27393
      Trace("fmc-exh-debug") << "Inst : ";
861
54786
      std::vector< Node > ev_inst;
862
54786
      std::vector< Node > inst;
863
97768
      for (unsigned i = 0; i < riter.getNumTerms(); i++)
864
      {
865
140750
        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
140750
        Node rr = riter.getCurrentTerm(i, !tn.isClosedEnumerable());
871
140750
        Node r = fm->getRepresentative(rr);
872
70375
        debugPrint("fmc-exh-debug", r);
873
70375
        Trace("fmc-exh-debug") << " (term : " << rr << ")";
874
70375
        ev_inst.push_back( r );
875
70375
        inst.push_back( rr );
876
      }
877
27393
      int ev_index = d_quant_models[f].getGeneralizationIndex(fm, ev_inst);
878
27393
      Trace("fmc-exh-debug") << ", index = " << ev_index << " / " << d_quant_models[f].d_value.size();
879
54786
      Node ev = ev_index==-1 ? Node::null() : d_quant_models[f].d_value[ev_index];
880
27393
      if (ev!=d_true) {
881
25946
        Trace("fmc-exh-debug") << ", add!";
882
        //add as instantiation
883
51892
        if (ie->addInstantiation(f,
884
                                 inst,
885
                                 InferenceId::QUANTIFIERS_INST_FMF_FMC_EXH,
886
51892
                                 Node::null(),
887
                                 true))
888
        {
889
3676
          Trace("fmc-exh-debug")  << " ...success.";
890
3676
          addedLemmas++;
891
3676
          if (d_qstate.isInConflict() || options::fmfOneInstPerRound())
892
          {
893
            break;
894
          }
895
        }else{
896
22270
          Trace("fmc-exh-debug") << ", failed.";
897
        }
898
      }else{
899
1447
        Trace("fmc-exh-debug") << ", already true";
900
      }
901
27393
      Trace("fmc-exh-debug") << std::endl;
902
27393
      int index = riter.increment();
903
27393
      Trace("fmc-exh-debug") << "Incremented index " << index << std::endl;
904
27393
      if( !riter.isFinished() ){
905
70899
        if (index >= 0 && riter.d_index[index] > 0 && addedLemmas > 0
906
28110
            && riter.d_enum_type[index] == ENUM_CUSTOM)
907
        {
908
2716
          Trace("fmc-exh-debug")
909
1358
              << "Since this is a custom enumeration, skip to the next..."
910
1358
              << std::endl;
911
1358
          riter.incrementAtIndex(index - 1);
912
        }
913
      }
914
    }
915
4621
    d_addedLemmas += addedLemmas;
916
4621
    Trace("fmc-exh") << "----Finished Exhaustive instantiate, lemmas = " << addedLemmas << ", incomplete=" << riter.isIncomplete() << std::endl;
917
4621
    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
91935
void FullModelChecker::doCheck(FirstOrderModelFmc * fm, Node f, Def & d, Node n ) {
925
91935
  Trace("fmc-debug") << "Check " << n << " " << n.getKind() << std::endl;
926
  //first check if it is a bounding literal
927
91935
  if( n.hasAttribute(BoundIntLitAttribute()) ){
928
2294
    Trace("fmc-debug") << "It is a bounding literal, polarity = " << n.getAttribute(BoundIntLitAttribute()) << std::endl;
929
2294
    d.addEntry(fm, mkCondDefault(fm, f), n.getAttribute(BoundIntLitAttribute())==1 ? d_false : d_true );
930
89641
  }else if( n.getKind() == kind::BOUND_VARIABLE ){
931
21763
    Trace("fmc-debug") << "Add default entry..." << std::endl;
932
21763
    d.addEntry(fm, mkCondDefault(fm, f), n);
933
  }
934
67878
  else if( n.getKind() == kind::NOT ){
935
    //just do directly
936
6787
    doCheck( fm, f, d, n[0] );
937
6787
    doNegate( d );
938
  }
939
61091
  else if( n.getKind() == kind::FORALL ){
940
944
    d.addEntry(fm, mkCondDefault(fm, f), Node::null());
941
  }
942
60147
  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
59772
  else if( n.getNumChildren()==0 ){
951
34576
    Node r = n;
952
17288
    if( !n.isConst() ){
953
14928
      TypeNode tn = n.getType();
954
7464
      if( !fm->hasTerm(n) && tn.isFirstClass() ){
955
150
        r = getSomeDomainElement(fm, tn );
956
      }
957
7464
      r = fm->getRepresentative( r );
958
    }
959
17288
    Trace("fmc-debug") << "Add constant entry..." << std::endl;
960
17288
    d.addEntry(fm, mkCondDefault(fm, f), r);
961
  }
962
  else{
963
84968
    std::vector< int > var_ch;
964
84968
    std::vector< Def > children;
965
122275
    for( int i=0; i<(int)n.getNumChildren(); i++) {
966
159582
      Def dc;
967
79791
      doCheck(fm, f, dc, n[i]);
968
79791
      children.push_back(dc);
969
79791
      if( n[i].getKind() == kind::BOUND_VARIABLE ){
970
21762
        var_ch.push_back(i);
971
      }
972
    }
973
974
42484
    if( n.getKind()==APPLY_UF ){
975
18884
      Trace("fmc-debug") << "Do uninterpreted compose " << n << std::endl;
976
      //uninterpreted compose
977
18884
      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
23600
      if( !var_ch.empty() ){
990
3521
        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
2822
          Trace("fmc-warn") << "Don't know how to check " << n << std::endl;
1000
2822
          d.addEntry(fm, mkCondDefault(fm, f), Node::null());
1001
        }
1002
      }else{
1003
20079
        Trace("fmc-debug") << "Do interpreted compose " << n << std::endl;
1004
40158
        std::vector< Node > cond;
1005
20079
        mkCondDefaultVec(fm, f, cond);
1006
40158
        std::vector< Node > val;
1007
        //interpreted compose
1008
20079
        doInterpretedCompose( fm, f, d, n, children, 0, cond, val );
1009
      }
1010
    }
1011
42484
    Trace("fmc-debug") << "Simplify the definition..." << std::endl;
1012
42484
    d.debugPrint("fmc-debug", Node::null(), this);
1013
42484
    d.simplify(this, fm);
1014
42484
    Trace("fmc-debug") << "Done simplifying" << std::endl;
1015
  }
1016
91935
  Trace("fmc-debug") << "Definition for " << n << " is : " << std::endl;
1017
91935
  d.debugPrint("fmc-debug", Node::null(), this);
1018
91935
  Trace("fmc-debug") << std::endl;
1019
91935
}
1020
1021
6787
void FullModelChecker::doNegate( Def & dc ) {
1022
15786
  for (unsigned i=0; i<dc.d_cond.size(); i++) {
1023
17998
    Node v = dc.d_value[i];
1024
8999
    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
7018
      dc.d_value[i] =
1031
14036
          v == d_true ? d_false : (v == d_false ? d_true : Node::null());
1032
    }
1033
  }
1034
6787
}
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
18884
void FullModelChecker::doUninterpretedCompose( FirstOrderModelFmc * fm, Node f, Def & d, Node op, std::vector< Def > & dc ) {
1092
18884
  Trace("fmc-uf-debug") << "Definition : " << std::endl;
1093
18884
  fm->d_models[op]->debugPrint("fmc-uf-debug", op, this);
1094
18884
  Trace("fmc-uf-debug") << std::endl;
1095
1096
37768
  std::vector< Node > cond;
1097
18884
  mkCondDefaultVec(fm, f, cond);
1098
37768
  std::vector< Node > val;
1099
18884
  doUninterpretedCompose( fm, f, d, *fm->d_models[op], dc, 0, cond, val);
1100
18884
}
1101
1102
54256
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
54256
  Trace("fmc-uf-process") << "process at " << index << std::endl;
1106
155864
  for( unsigned i=1; i<cond.size(); i++) {
1107
101608
    debugPrint("fmc-uf-process", cond[i], true);
1108
101608
    Trace("fmc-uf-process") << " ";
1109
  }
1110
54256
  Trace("fmc-uf-process") << std::endl;
1111
54256
  if (index==(int)dc.size()) {
1112
    //we have an entry, now do actual compose
1113
47040
    std::map< int, Node > entries;
1114
23520
    doUninterpretedCompose2( fm, f, entries, 0, cond, val, df.d_et);
1115
23520
    if( entries.empty() ){
1116
825
      d.addEntry(fm, mkCond(cond), Node::null());
1117
    }else{
1118
      //add them to the definition
1119
68046
      for( unsigned e=0; e<df.d_cond.size(); e++ ){
1120
45351
        if ( entries.find(e)!=entries.end() ){
1121
37823
          Trace("fmf-uf-process-debug") << "Add entry..." << std::endl;
1122
37823
          d.addEntry(fm, entries[e], df.d_value[e] );
1123
37823
          Trace("fmf-uf-process-debug") << "Done add entry." << std::endl;
1124
        }
1125
      }
1126
    }
1127
  }else{
1128
71990
    for (unsigned i=0; i<dc[index].d_cond.size(); i++) {
1129
41254
      if (isCompat(fm, cond, dc[index].d_cond[i])!=0) {
1130
70744
        std::vector< Node > new_cond;
1131
35372
        new_cond.insert(new_cond.end(), cond.begin(), cond.end());
1132
35372
        if( doMeet(fm, new_cond, dc[index].d_cond[i]) ){
1133
35372
          Trace("fmc-uf-process") << "index " << i << " succeeded meet." << std::endl;
1134
35372
          val.push_back(dc[index].d_value[i]);
1135
35372
          doUninterpretedCompose(fm, f, d, df, dc, index+1, new_cond, val);
1136
35372
          val.pop_back();
1137
        }else{
1138
          Trace("fmc-uf-process") << "index " << i << " failed meet." << std::endl;
1139
        }
1140
      }
1141
    }
1142
  }
1143
54256
}
1144
1145
82151
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
82151
  Trace("fmc-uf-process") << "compose " << index << " / " << val.size() << std::endl;
1150
235663
  for( unsigned i=1; i<cond.size(); i++) {
1151
153512
    debugPrint("fmc-uf-process", cond[i], true);
1152
153512
    Trace("fmc-uf-process") << " ";
1153
  }
1154
82151
  Trace("fmc-uf-process") << std::endl;
1155
82151
  if (index==(int)val.size()) {
1156
75646
    Node c = mkCond(cond);
1157
37823
    Trace("fmc-uf-entry") << "Entry : " << c << " -> index[" << curr.d_data << "]" << std::endl;
1158
37823
    entries[curr.d_data] = c;
1159
  }else{
1160
88656
    Node v = val[index];
1161
44328
    Trace("fmc-uf-process") << "Process " << v << std::endl;
1162
44328
    bool bind_var = false;
1163
44328
    if( !v.isNull() && v.getKind()==kind::BOUND_VARIABLE ){
1164
21044
      int j = fm->getVariableId(f, v);
1165
21044
      Trace("fmc-uf-process") << v << " is variable #" << j << std::endl;
1166
21044
      if (!fm->isStar(cond[j + 1]))
1167
      {
1168
38
        v = cond[j+1];
1169
      }else{
1170
21006
        bind_var = true;
1171
      }
1172
    }
1173
44328
    if (bind_var) {
1174
21006
      Trace("fmc-uf-process") << "bind variable..." << std::endl;
1175
21006
      int j = fm->getVariableId(f, v);
1176
21006
      Assert(fm->isStar(cond[j + 1]));
1177
54298
      for (std::map<Node, EntryTrie>::iterator it = curr.d_child.begin();
1178
54298
           it != curr.d_child.end();
1179
           ++it)
1180
      {
1181
33292
        cond[j + 1] = it->first;
1182
33292
        doUninterpretedCompose2(
1183
33292
            fm, f, entries, index + 1, cond, val, it->second);
1184
      }
1185
21006
      cond[j + 1] = fm->getStar(v.getType());
1186
    }else{
1187
23322
      if( !v.isNull() ){
1188
22491
        if (curr.d_child.find(v) != curr.d_child.end())
1189
        {
1190
6501
          Trace("fmc-uf-process") << "follow value..." << std::endl;
1191
6501
          doUninterpretedCompose2(
1192
6501
              fm, f, entries, index + 1, cond, val, curr.d_child[v]);
1193
        }
1194
44982
        Node st = fm->getStar(v.getType());
1195
22491
        if (curr.d_child.find(st) != curr.d_child.end())
1196
        {
1197
18838
          Trace("fmc-uf-process") << "follow star..." << std::endl;
1198
18838
          doUninterpretedCompose2(
1199
18838
              fm, f, entries, index + 1, cond, val, curr.d_child[st]);
1200
        }
1201
      }
1202
    }
1203
  }
1204
82151
}
1205
1206
83836
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
83836
  Trace("fmc-if-process") << "int compose " << index << " / " << dc.size() << std::endl;
1210
209843
  for( unsigned i=1; i<cond.size(); i++) {
1211
126007
    debugPrint("fmc-if-process", cond[i], true);
1212
126007
    Trace("fmc-if-process") << " ";
1213
  }
1214
83836
  Trace("fmc-if-process") << std::endl;
1215
83836
  if ( index==(int)dc.size() ){
1216
68858
    Node c = mkCond(cond);
1217
68858
    Node v = evaluateInterpreted(n, val);
1218
34429
    d.addEntry(fm, c, v);
1219
  }
1220
  else {
1221
98814
    TypeNode vtn = n.getType();
1222
130528
    for (unsigned i=0; i<dc[index].d_cond.size(); i++) {
1223
81121
      if (isCompat(fm, cond, dc[index].d_cond[i])!=0) {
1224
140720
        std::vector< Node > new_cond;
1225
70360
        new_cond.insert(new_cond.end(), cond.begin(), cond.end());
1226
70360
        if( doMeet(fm, new_cond, dc[index].d_cond[i]) ){
1227
70360
          bool process = true;
1228
70360
          if (vtn.isBoolean()) {
1229
            //short circuit
1230
90761
            if( (n.getKind()==OR && dc[index].d_value[i]==d_true) ||
1231
41812
                (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
70360
          if (process) {
1238
63757
            val.push_back(dc[index].d_value[i]);
1239
63757
            doInterpretedCompose(fm, f, d, n, dc, index+1, new_cond, val);
1240
63757
            val.pop_back();
1241
          }
1242
        }
1243
      }
1244
    }
1245
  }
1246
83836
}
1247
1248
122375
int FullModelChecker::isCompat( FirstOrderModelFmc * fm, std::vector< Node > & cond, Node c ) {
1249
122375
  Trace("fmc-debug3") << "isCompat " << c << std::endl;
1250
122375
  Assert(cond.size() == c.getNumChildren() + 1);
1251
325319
  for (unsigned i=1; i<cond.size(); i++) {
1252
219587
    if (cond[i] != c[i - 1] && !fm->isStar(cond[i]) && !fm->isStar(c[i - 1]))
1253
    {
1254
16643
      return 0;
1255
    }
1256
  }
1257
105732
  return 1;
1258
}
1259
1260
105732
bool FullModelChecker::doMeet( FirstOrderModelFmc * fm, std::vector< Node > & cond, Node c ) {
1261
105732
  Trace("fmc-debug3") << "doMeet " << c << std::endl;
1262
105732
  Assert(cond.size() == c.getNumChildren() + 1);
1263
293351
  for (unsigned i=1; i<cond.size(); i++) {
1264
187619
    if( cond[i]!=c[i-1] ) {
1265
54710
      if (fm->isStar(cond[i]))
1266
      {
1267
29294
        cond[i] = c[i - 1];
1268
      }
1269
25416
      else if (!fm->isStar(c[i - 1]))
1270
      {
1271
        return false;
1272
      }
1273
    }
1274
  }
1275
105732
  return true;
1276
}
1277
1278
130002
Node FullModelChecker::mkCond( std::vector< Node > & cond ) {
1279
130002
  return NodeManager::currentNM()->mkNode(APPLY_UF, cond);
1280
}
1281
1282
47922
Node FullModelChecker::mkCondDefault( FirstOrderModelFmc * fm, Node f) {
1283
95844
  std::vector< Node > cond;
1284
47922
  mkCondDefaultVec(fm, f, cond);
1285
95844
  return mkCond(cond);
1286
}
1287
1288
87178
void FullModelChecker::mkCondDefaultVec( FirstOrderModelFmc * fm, Node f, std::vector< Node > & cond ) {
1289
87178
  Trace("fmc-debug") << "Make default vec" << std::endl;
1290
  //get function symbol for f
1291
87178
  cond.push_back(d_quant_cond[f]);
1292
227239
  for (unsigned i=0; i<f[0].getNumChildren(); i++) {
1293
280122
    Node ts = fm->getStar(f[0][i].getType());
1294
140061
    Assert(ts.getType() == f[0][i].getType());
1295
140061
    cond.push_back(ts);
1296
  }
1297
87178
}
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
34429
Node FullModelChecker::evaluateInterpreted( Node n, std::vector< Node > & vals ) {
1307
34429
  if( n.getKind()==EQUAL && !n[0].getType().isBoolean() ){
1308
9198
    if (!vals[0].isNull() && !vals[1].isNull()) {
1309
7733
      return vals[0]==vals[1] ? d_true : d_false;
1310
    }else{
1311
1465
      return Node::null();
1312
    }
1313
25231
  }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
22886
  }else if( n.getKind()==AND || n.getKind()==OR ){
1322
4845
    bool isNull = false;
1323
18711
    for (unsigned i=0; i<vals.size(); i++) {
1324
13866
      if((vals[i]==d_true && n.getKind()==OR) || (vals[i]==d_false && n.getKind()==AND)) {
1325
        return vals[i];
1326
13866
      }else if( vals[i].isNull() ){
1327
2917
        isNull = true;
1328
      }
1329
    }
1330
4845
    return isNull ? Node::null() : vals[0];
1331
  }else{
1332
36082
    std::vector<Node> children;
1333
18041
    if( n.getMetaKind() == kind::metakind::PARAMETERIZED ){
1334
3283
      children.push_back( n.getOperator() );
1335
    }
1336
47684
    for (unsigned i=0; i<vals.size(); i++) {
1337
32553
      if( vals[i].isNull() ){
1338
2910
        return Node::null();
1339
      }else{
1340
29643
        children.push_back( vals[i] );
1341
      }
1342
    }
1343
30262
    Node nc = NodeManager::currentNM()->mkNode(n.getKind(), children);
1344
15131
    Trace("fmc-eval") << "Evaluate " << nc << " to ";
1345
15131
    nc = Rewriter::rewrite(nc);
1346
15131
    Trace("fmc-eval") << nc << std::endl;
1347
15131
    return nc;
1348
  }
1349
}
1350
1351
2996
Node FullModelChecker::getSomeDomainElement( FirstOrderModelFmc * fm, TypeNode tn ) {
1352
2996
  bool addRepId = !fm->getRepSet()->hasType(tn);
1353
2996
  Node de = fm->getSomeDomainElement(tn);
1354
2996
  if( addRepId ){
1355
102
    d_rep_ids[tn][de] = 0;
1356
  }
1357
2996
  return de;
1358
}
1359
1360
6121
Node FullModelChecker::getFunctionValue(FirstOrderModelFmc * fm, Node op, const char* argPrefix ) {
1361
6121
  return fm->getFunctionValue(op, argPrefix);
1362
}
1363
1364
1365
31986
bool FullModelChecker::useSimpleModels() {
1366
31986
  return options::fmfFmcSimple();
1367
}
1368
17629
void FullModelChecker::registerQuantifiedFormula(Node q)
1369
{
1370
17629
  if (d_quant_cond.find(q) != d_quant_cond.end())
1371
  {
1372
16525
    return;
1373
  }
1374
1104
  NodeManager* nm = NodeManager::currentNM();
1375
1104
  SkolemManager* sm = nm->getSkolemManager();
1376
2208
  std::vector<TypeNode> types;
1377
2823
  for (const Node& v : q[0])
1378
  {
1379
3438
    TypeNode tn = v.getType();
1380
1719
    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
1719
    types.push_back(tn);
1388
  }
1389
2208
  TypeNode typ = nm->mkFunctionType(types, nm->booleanType());
1390
2208
  Node op = sm->mkDummySkolem("qfmc", typ, "op for full-model checking");
1391
1104
  d_quant_cond[q] = op;
1392
}
1393
1394
17629
bool FullModelChecker::isHandled(Node q) const
1395
{
1396
17629
  return d_unhandledQuant.find(q) == d_unhandledQuant.end();
1397
}
1398
1399
}  // namespace fmcheck
1400
}  // namespace quantifiers
1401
}  // namespace theory
1402
22746
}  // namespace cvc5