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-17 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
185248
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
57205
  bool operator() (int i,int j) {
48
57205
    return (d_mba_count[d_terms[i]] < d_mba_count[d_terms[j]]);
49
  }
50
};
51
52
420682
bool EntryTrie::hasGeneralization( FirstOrderModelFmc * m, Node c, int index ) {
53
420682
  if (index==(int)c.getNumChildren()) {
54
16402
    return d_data!=-1;
55
  }else{
56
808560
    TypeNode tn = c[index].getType();
57
808560
    Node st = m->getStar(tn);
58
404280
    if(d_child.find(st)!=d_child.end()) {
59
40498
      if( d_child[st].hasGeneralization(m, c, index+1) ){
60
15196
        return true;
61
      }
62
    }
63
389084
    if( c[index]!=st && d_child.find( c[index] )!=d_child.end() ){
64
56598
      if( d_child[ c[index] ].hasGeneralization(m, c, index+1) ){
65
21642
        return true;
66
      }
67
    }
68
367442
    if( c[index].getType().isSort() ){
69
      //for star: check if all children are defined and have generalizations
70
266724
      if( c[index]==st ){     ///options::fmfFmcCoverSimplify()
71
        //check if all children exist and are complete
72
        unsigned num_child_def =
73
153403
            d_child.size() - (d_child.find(st) != d_child.end() ? 1 : 0);
74
153403
        if (num_child_def == m->getRepSet()->getNumRepresentatives(tn))
75
        {
76
8585
          bool complete = true;
77
14787
          for ( std::map<Node,EntryTrie>::iterator it = d_child.begin(); it != d_child.end(); ++it ){
78
12536
            if( !m->isStar(it->first) ){
79
12034
              if( !it->second.hasGeneralization(m, c, index+1) ){
80
6334
                complete = false;
81
6334
                break;
82
              }
83
            }
84
          }
85
8585
          if( complete ){
86
2251
            return true;
87
          }
88
        }
89
      }
90
    }
91
92
365191
    return false;
93
  }
94
}
95
96
136619
int EntryTrie::getGeneralizationIndex( FirstOrderModelFmc * m, std::vector<Node> & inst, int index ) {
97
136619
  Debug("fmc-entry-trie") << "Get generalization index " << inst.size() << " " << index << std::endl;
98
136619
  if (index==(int)inst.size()) {
99
40124
    return d_data;
100
  }else{
101
96495
    int minIndex = -1;
102
192990
    Node st = m->getStar(inst[index].getType());
103
96495
    if (d_child.find(st) != d_child.end())
104
    {
105
90025
      minIndex = d_child[st].getGeneralizationIndex(m, inst, index + 1);
106
    }
107
192990
    Node cc = inst[index];
108
96495
    if (cc != st && d_child.find(cc) != d_child.end())
109
    {
110
12838
      int gindex = d_child[cc].getGeneralizationIndex(m, inst, index + 1);
111
12838
      if (minIndex == -1 || (gindex != -1 && gindex < minIndex))
112
      {
113
8811
        minIndex = gindex;
114
      }
115
    }
116
96495
    return minIndex;
117
  }
118
}
119
120
778976
void EntryTrie::addEntry( FirstOrderModelFmc * m, Node c, Node v, int data, int index ) {
121
778976
  if (index==(int)c.getNumChildren()) {
122
298599
    if(d_data==-1) {
123
298599
      d_data = data;
124
    }
125
  }
126
  else {
127
480377
    d_child[ c[index] ].addEntry(m,c,v,data,index+1);
128
480377
    if( d_complete==0 ){
129
      d_complete = -1;
130
    }
131
  }
132
778976
}
133
134
336050
void EntryTrie::getEntries( FirstOrderModelFmc * m, Node c, std::vector<int> & compat, std::vector<int> & gen, int index, bool is_gen ) {
135
336050
  if (index==(int)c.getNumChildren()) {
136
70631
    if( d_data!=-1) {
137
70631
      if( is_gen ){
138
64839
        gen.push_back(d_data);
139
      }
140
70631
      compat.push_back(d_data);
141
    }
142
  }else{
143
265419
    if (m->isStar(c[index])) {
144
273391
      for ( std::map<Node,EntryTrie>::iterator it = d_child.begin(); it != d_child.end(); ++it ){
145
111692
        it->second.getEntries(m, c, compat, gen, index+1, is_gen );
146
      }
147
    }else{
148
207440
      Node st = m->getStar(c[index].getType());
149
103720
      if(d_child.find(st)!=d_child.end()) {
150
7748
        d_child[st].getEntries(m, c, compat, gen, index+1, false);
151
      }
152
103720
      if( d_child.find( c[index] )!=d_child.end() ){
153
30190
        d_child[ c[index] ].getEntries(m, c, compat, gen, index+1, is_gen);
154
      }
155
    }
156
157
  }
158
336050
}
159
160
311552
bool Def::addEntry( FirstOrderModelFmc * m, Node c, Node v) {
161
311552
  if (d_et.hasGeneralization(m, c)) {
162
12953
    Trace("fmc-debug") << "Already has generalization, skip." << std::endl;
163
12953
    return false;
164
  }
165
298599
  int newIndex = (int)d_cond.size();
166
298599
  if (!d_has_simplified) {
167
372840
    std::vector<int> compat;
168
372840
    std::vector<int> gen;
169
186420
    d_et.getEntries(m, c, compat, gen);
170
257051
    for( unsigned i=0; i<compat.size(); i++) {
171
70631
      if( d_status[compat[i]]==status_unk ){
172
61719
        if( d_value[compat[i]]!=v ){
173
38785
          d_status[compat[i]] = status_non_redundant;
174
        }
175
      }
176
    }
177
251259
    for( unsigned i=0; i<gen.size(); i++) {
178
64839
      if( d_status[gen[i]]==status_unk ){
179
19270
        if( d_value[gen[i]]==v ){
180
19270
          d_status[gen[i]] = status_redundant;
181
        }
182
      }
183
    }
184
186420
    d_status.push_back( status_unk );
185
  }
186
298599
  d_et.addEntry(m, c, v, newIndex);
187
298599
  d_cond.push_back(c);
188
298599
  d_value.push_back(v);
189
298599
  return true;
190
}
191
192
3237
Node Def::evaluate( FirstOrderModelFmc * m, std::vector<Node>& inst ) {
193
3237
  int gindex = d_et.getGeneralizationIndex(m, inst);
194
3237
  if (gindex!=-1) {
195
3233
    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
30519
int Def::getGeneralizationIndex( FirstOrderModelFmc * m, std::vector<Node>& inst ) {
203
30519
  return d_et.getGeneralizationIndex(m, inst);
204
}
205
206
67899
void Def::basic_simplify( FirstOrderModelFmc * m ) {
207
67899
  d_has_simplified = true;
208
135798
  std::vector< Node > cond;
209
67899
  cond.insert( cond.end(), d_cond.begin(), d_cond.end() );
210
67899
  d_cond.clear();
211
135798
  std::vector< Node > value;
212
67899
  value.insert( value.end(), d_value.begin(), d_value.end() );
213
67899
  d_value.clear();
214
67899
  d_et.reset();
215
199348
  for (unsigned i=0; i<d_status.size(); i++) {
216
131449
    if( d_status[i]!=status_redundant ){
217
112179
      addEntry(m, cond[i], value[i]);
218
    }
219
  }
220
67899
  d_status.clear();
221
67899
}
222
223
61854
void Def::simplify(FullModelChecker * mc, FirstOrderModelFmc * m) {
224
61854
  Trace("fmc-simplify") << "Simplify definition, #cond = " << d_cond.size() << std::endl;
225
61854
  basic_simplify( m );
226
61854
  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
61854
  if( !d_cond.empty() ){
230
61854
    bool last_all_stars = true;
231
123708
    Node cc = d_cond[d_cond.size()-1];
232
144041
    for( unsigned i=0; i<cc.getNumChildren(); i++ ){
233
88232
      if (!m->isStar(cc[i]))
234
      {
235
6045
        last_all_stars = false;
236
6045
        break;
237
      }
238
    }
239
61854
    if( !last_all_stars ){
240
6045
      Trace("fmc-cover-simplify") << "Need to modify last entry to be all stars." << std::endl;
241
6045
      Trace("fmc-cover-simplify") << "Before: " << std::endl;
242
6045
      debugPrint("fmc-cover-simplify",Node::null(), mc);
243
6045
      Trace("fmc-cover-simplify") << std::endl;
244
12090
      std::vector< Node > cond;
245
6045
      cond.insert( cond.end(), d_cond.begin(), d_cond.end() );
246
6045
      d_cond.clear();
247
12090
      std::vector< Node > value;
248
6045
      value.insert( value.end(), d_value.begin(), d_value.end() );
249
6045
      d_value.clear();
250
6045
      d_et.reset();
251
6045
      d_has_simplified = false;
252
      //change the last to all star
253
12090
      std::vector< Node > nc;
254
6045
      nc.push_back( cc.getOperator() );
255
15698
      for( unsigned j=0; j< cc.getNumChildren(); j++){
256
9653
        nc.push_back(m->getStar(cc[j].getType()));
257
      }
258
6045
      cond[cond.size()-1] = NodeManager::currentNM()->mkNode( APPLY_UF, nc );
259
      //re-add the entries
260
17693
      for (unsigned i=0; i<cond.size(); i++) {
261
11648
        addEntry(m, cond[i], value[i]);
262
      }
263
6045
      Trace("fmc-cover-simplify") << "Finished re-adding entries." << std::endl;
264
6045
      basic_simplify( m );
265
6045
      Trace("fmc-cover-simplify") << "After: " << std::endl;
266
6045
      debugPrint("fmc-cover-simplify",Node::null(), mc);
267
6045
      Trace("fmc-cover-simplify") << std::endl;
268
    }
269
  }
270
61854
  Trace("fmc-simplify") << "finish simplify, #cond = " << d_cond.size() << std::endl;
271
61854
}
272
273
226325
void Def::debugPrint(const char * tr, Node op, FullModelChecker * m) {
274
226325
  if (!op.isNull()) {
275
38386
    Trace(tr) << "Model for " << op << " : " << std::endl;
276
  }
277
593004
  for( unsigned i=0; i<d_cond.size(); i++) {
278
    //print the condition
279
366679
    if (!op.isNull()) {
280
96076
      Trace(tr) << op;
281
    }
282
366679
    m->debugPrintCond(tr, d_cond[i], true);
283
366679
    Trace(tr) << " -> ";
284
366679
    m->debugPrint(tr, d_value[i]);
285
366679
    Trace(tr) << std::endl;
286
  }
287
226325
}
288
289
1462
FullModelChecker::FullModelChecker(Env& env,
290
                                   QuantifiersState& qs,
291
                                   QuantifiersInferenceManager& qim,
292
                                   QuantifiersRegistry& qr,
293
1462
                                   TermRegistry& tr)
294
    : QModelBuilder(env, qs, qim, qr, tr),
295
1462
      d_fm(new FirstOrderModelFmc(env, qs, qr, tr))
296
{
297
1462
  d_true = NodeManager::currentNM()->mkConst(true);
298
1462
  d_false = NodeManager::currentNM()->mkConst(false);
299
1462
}
300
301
1462
void FullModelChecker::finishInit() { d_model = d_fm.get(); }
302
303
2038
bool FullModelChecker::preProcessBuildModel(TheoryModel* m) {
304
  //standard pre-process
305
2038
  if( !preProcessBuildModelStd( m ) ){
306
    return false;
307
  }
308
309
2038
  Trace("fmc") << "---Full Model Check preprocess() " << std::endl;
310
2038
  d_preinitialized_eqc.clear();
311
2038
  d_preinitialized_types.clear();
312
  //traverse equality engine
313
2038
  eq::EqClassesIterator eqcs_i = eq::EqClassesIterator(m->getEqualityEngine());
314
85378
  while( !eqcs_i.isFinished() ){
315
83340
    Node r = *eqcs_i;
316
83340
    TypeNode tr = r.getType();
317
41670
    d_preinitialized_eqc[tr] = r;
318
41670
    ++eqcs_i;
319
  }
320
321
  //must ensure model basis terms exists in model for each relevant type
322
2038
  Trace("fmc") << "preInitialize types..." << std::endl;
323
2038
  d_fm->initialize();
324
9245
  for (std::pair<const Node, Def*>& mp : d_fm->d_models)
325
  {
326
14414
    Node op = mp.first;
327
7207
    Trace("fmc") << "preInitialize types for " << op << std::endl;
328
14414
    TypeNode tno = op.getType();
329
26430
    for( unsigned i=0; i<tno.getNumChildren(); i++) {
330
19223
      Trace("fmc") << "preInitializeType " << tno[i] << std::endl;
331
19223
      preInitializeType(m, tno[i]);
332
19223
      Trace("fmc") << "finished preInitializeType " << tno[i] << std::endl;
333
    }
334
  }
335
2038
  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
11159
  for (unsigned i = 0, nquant = d_fm->getNumAssertedQuantifiers(); i < nquant;
338
       i++)
339
  {
340
18131
    Node q = d_fm->getAssertedQuantifier(i);
341
9121
    registerQuantifiedFormula(q);
342
9121
    if (!isHandled(q))
343
    {
344
111
      continue;
345
    }
346
    // make sure all types are set
347
22875
    for (const Node& v : q[0])
348
    {
349
13865
      preInitializeType(m, v.getType());
350
    }
351
  }
352
2038
  return true;
353
}
354
355
2038
bool FullModelChecker::processBuildModel(TheoryModel* m){
356
2038
  if (!m->areFunctionValuesEnabled())
357
  {
358
    // nothing to do if no functions
359
    return true;
360
  }
361
2038
  FirstOrderModelFmc* fm = d_fm.get();
362
2038
  Trace("fmc") << "---Full Model Check reset() " << std::endl;
363
2038
  d_quant_models.clear();
364
2038
  d_rep_ids.clear();
365
2038
  d_star_insts.clear();
366
  //process representatives
367
2038
  RepSet* rs = m->getRepSetPtr();
368
8655
  for (std::map<TypeNode, std::vector<Node> >::iterator it =
369
2038
           rs->d_type_reps.begin();
370
10693
       it != rs->d_type_reps.end();
371
       ++it)
372
  {
373
8655
    if( it->first.isSort() ){
374
1940
      Trace("fmc") << "Cardinality( " << it->first << " )" << " = " << it->second.size() << std::endl;
375
5613
      for( size_t a=0; a<it->second.size(); a++ ){
376
7346
        Node r = m->getRepresentative(it->second[a]);
377
3673
        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
3673
        d_rep_ids[it->first][r] = (int)a;
390
      }
391
1940
      Trace("fmc-model-debug") << std::endl;
392
    }
393
  }
394
395
  //now, make models
396
9245
  for (std::pair<const Node, Def*>& fmm : d_fm->d_models)
397
  {
398
14414
    Node op = fmm.first;
399
    //reset the model
400
7207
    d_fm->d_models[op]->reset();
401
402
14414
    std::vector< Node > add_conds;
403
14414
    std::vector< Node > add_values;
404
7207
    bool needsDefault = true;
405
7207
    if (m->hasUfTerms(op))
406
    {
407
5429
      const std::vector<Node>& uft = m->getUfTerms(op);
408
10858
      Trace("fmc-model-debug")
409
5429
          << uft.size() << " model values for " << op << " ... " << std::endl;
410
36865
      for (const Node& n : uft)
411
      {
412
        // only consider unique up to congruence (in model equality engine)?
413
31436
        add_conds.push_back( n );
414
31436
        add_values.push_back( n );
415
62872
        Node r = m->getRepresentative(n);
416
31436
        Trace("fmc-model-debug") << n << " -> " << r << std::endl;
417
      }
418
    }else{
419
1778
      Trace("fmc-model-debug") << "No model values for " << op << " ... " << std::endl;
420
    }
421
7207
    Trace("fmc-model-debug") << std::endl;
422
    //possibly get default
423
7207
    if( needsDefault ){
424
14414
      Node nmb = d_fm->getModelBasisOpTerm(op);
425
      //add default value if necessary
426
7207
      if (m->hasTerm(nmb))
427
      {
428
3837
        Trace("fmc-model-debug") << "Add default " << nmb << std::endl;
429
3837
        add_conds.push_back( nmb );
430
3837
        add_values.push_back( nmb );
431
      }else{
432
6740
        Node vmb = getSomeDomainElement(d_fm.get(), nmb.getType());
433
3370
        Trace("fmc-model-debug") << "Add default to default representative " << nmb << " ";
434
6740
        Trace("fmc-model-debug")
435
6740
            << m->getRepSet()->getNumRepresentatives(nmb.getType())
436
3370
            << std::endl;
437
3370
        add_conds.push_back( nmb );
438
3370
        add_values.push_back( vmb );
439
      }
440
    }
441
442
14414
    std::vector< Node > conds;
443
14414
    std::vector< Node > values;
444
14414
    std::vector< Node > entry_conds;
445
    //get the entries for the model
446
45850
    for( size_t i=0; i<add_conds.size(); i++ ){
447
77286
      Node c = add_conds[i];
448
77286
      Node v = add_values[i];
449
77286
      Trace("fmc-model-debug")
450
38643
          << "Add cond/value : " << c << " -> " << v << std::endl;
451
77286
      std::vector< Node > children;
452
77286
      std::vector< Node > entry_children;
453
38643
      children.push_back(op);
454
38643
      entry_children.push_back(op);
455
38643
      bool hasNonStar = false;
456
113427
      for (const Node& ci : c)
457
      {
458
149568
        Node ri = fm->getRepresentative(ci);
459
74784
        children.push_back(ri);
460
74784
        bool isStar = false;
461
74784
        if (fm->isModelBasisTerm(ri))
462
        {
463
21486
          ri = fm->getStar(ri.getType());
464
21486
          isStar = true;
465
        }
466
        else
467
        {
468
53298
          hasNonStar = true;
469
        }
470
74784
        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
74784
        entry_children.push_back(ri);
477
      }
478
77286
      Node n = NodeManager::currentNM()->mkNode( APPLY_UF, children );
479
77286
      Node nv = fm->getRepresentative( v );
480
77286
      Trace("fmc-model-debug")
481
38643
          << "Representative of " << v << " is " << nv << std::endl;
482
38643
      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
77286
      Node en = (useSimpleModels() && hasNonStar) ? n : NodeManager::currentNM()->mkNode( APPLY_UF, entry_children );
487
38643
      if( std::find(conds.begin(), conds.end(), n )==conds.end() ){
488
24387
        Trace("fmc-model-debug") << "- add " << n << " -> " << nv << " (entry is " << en << ")" << std::endl;
489
24387
        conds.push_back(n);
490
24387
        values.push_back(nv);
491
24387
        entry_conds.push_back(en);
492
      }
493
      else {
494
14256
        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
14414
    std::vector< int > indices;
501
14414
    ModelBasisArgSort mbas;
502
31594
    for (int i=0; i<(int)conds.size(); i++) {
503
24387
      mbas.d_terms.push_back(conds[i]);
504
24387
      mbas.d_mba_count[conds[i]] = fm->getModelBasisArg(conds[i]);
505
24387
      indices.push_back(i);
506
    }
507
7207
    std::sort( indices.begin(), indices.end(), mbas );
508
509
31594
    for (int i=0; i<(int)indices.size(); i++) {
510
24387
      fm->d_models[op]->addEntry(fm, entry_conds[indices[i]], values[indices[i]]);
511
    }
512
513
7207
    Trace("fmc-model-simplify") << "Before simplification : " << std::endl;
514
7207
    fm->d_models[op]->debugPrint("fmc-model-simplify", op, this);
515
7207
    Trace("fmc-model-simplify") << std::endl;
516
517
7207
    Trace("fmc-model-simplify") << "Simplifying " << op << "..." << std::endl;
518
7207
    fm->d_models[op]->simplify( this, fm );
519
520
7207
    fm->d_models[op]->debugPrint("fmc-model", op, this);
521
7207
    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
2038
  Assert(d_addedLemmas == 0);
539
540
  //make function values
541
9245
  for( std::map<Node, Def * >::iterator it = fm->d_models.begin(); it != fm->d_models.end(); ++it ){
542
14414
    Node f_def = getFunctionValue( fm, it->first, "$x" );
543
7207
    m->assignFunctionDefinition( it->first, f_def );
544
  }
545
2038
  return TheoryEngineModelBuilder::processBuildModel( m );
546
}
547
548
33088
void FullModelChecker::preInitializeType(TheoryModel* m, TypeNode tn)
549
{
550
33088
  if( d_preinitialized_types.find( tn )==d_preinitialized_types.end() ){
551
4853
    d_preinitialized_types[tn] = true;
552
4853
    if (tn.isFirstClass())
553
    {
554
4847
      Trace("fmc") << "Get model basis term " << tn << "..." << std::endl;
555
9694
      Node mb = d_fm->getModelBasisTerm(tn);
556
4847
      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
4847
      if (!m->hasTerm(mb) && !mb.isConst())
562
      {
563
570
        std::map<TypeNode, Node>::iterator itpe = d_preinitialized_eqc.find(tn);
564
570
        if (itpe == d_preinitialized_eqc.end())
565
        {
566
528
          Trace("fmc") << "...add model basis term to EE of model " << mb << " "
567
264
                       << tn << std::endl;
568
264
          m->getEqualityEngine()->addTerm(mb);
569
        }
570
        else
571
        {
572
612
          Trace("fmc") << "...add model basis eqc equality to model " << mb
573
306
                       << " == " << itpe->second << " " << tn << std::endl;
574
306
          bool ret = m->assertEquality(mb, itpe->second, true);
575
306
          AlwaysAssert(ret);
576
        }
577
      }
578
    }
579
  }
580
33088
}
581
582
371894
void FullModelChecker::debugPrintCond(const char * tr, Node n, bool dispStar) {
583
371894
  Trace(tr) << "(";
584
957337
  for( unsigned j=0; j<n.getNumChildren(); j++) {
585
585443
    if( j>0 ) Trace(tr) << ", ";
586
585443
    debugPrint(tr, n[j], dispStar);
587
  }
588
371894
  Trace(tr) << ")";
589
371894
}
590
591
1469137
void FullModelChecker::debugPrint(const char * tr, Node n, bool dispStar) {
592
1469137
  if( n.isNull() ){
593
32555
    Trace(tr) << "null";
594
  }
595
1436582
  else if (FirstOrderModelFmc::isStar(n) && dispStar)
596
  {
597
693956
    Trace(tr) << "*";
598
  }
599
  else
600
  {
601
1485252
    TypeNode tn = n.getType();
602
742626
    if( tn.isSort() && d_rep_ids.find(tn)!=d_rep_ids.end() ){
603
348279
      if (d_rep_ids[tn].find(n)!=d_rep_ids[tn].end()) {
604
326093
        Trace(tr) << d_rep_ids[tn][n];
605
      }else{
606
22186
        Trace(tr) << n;
607
      }
608
    }else{
609
394347
      Trace(tr) << n;
610
    }
611
  }
612
1469137
}
613
614
615
11567
int FullModelChecker::doExhaustiveInstantiation( FirstOrderModel * fm, Node f, int effort ) {
616
11567
  Trace("fmc") << "Full model check " << f << ", effort = " << effort << "..." << std::endl;
617
  // register the quantifier
618
11567
  registerQuantifiedFormula(f);
619
11567
  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
11567
  if (!optUseModel() || !isHandled(f))
623
  {
624
    return 0;
625
  }
626
11567
  FirstOrderModelFmc* fmfmc = static_cast<FirstOrderModelFmc*>(fm);
627
11567
  if (effort == 0)
628
  {
629
8748
    if (options::mbqiMode() == options::MbqiMode::NONE)
630
    {
631
      // just exhaustive instantiate
632
4336
      Node c = mkCondDefault(fmfmc, f);
633
2168
      d_quant_models[f].addEntry(fmfmc, c, d_false);
634
2168
      if (!exhaustiveInstantiate(fmfmc, f, c))
635
      {
636
20
        return 0;
637
      }
638
2148
      return 1;
639
    }
640
    // model check the quantifier
641
6580
    doCheck(fmfmc, f, d_quant_models[f], f[1]);
642
6580
    std::vector<Node>& mcond = d_quant_models[f].d_cond;
643
6580
    Trace("fmc") << "Definition for quantifier " << f << " is : " << std::endl;
644
6580
    Assert(!mcond.empty());
645
6580
    d_quant_models[f].debugPrint("fmc", Node::null(), this);
646
6580
    Trace("fmc") << std::endl;
647
648
    // consider all entries going to non-true
649
6580
    Instantiate* instq = d_qim.getInstantiate();
650
15145
    for (unsigned i = 0, msize = mcond.size(); i < msize; i++)
651
    {
652
8565
      if (d_quant_models[f].d_value[i] == d_true)
653
      {
654
        // already satisfied
655
11244
        continue;
656
      }
657
7238
      Trace("fmc-inst") << "Instantiate based on " << mcond[i] << "..."
658
3619
                        << std::endl;
659
3619
      bool hasStar = false;
660
5886
      std::vector<Node> inst;
661
8997
      for (unsigned j = 0, nchild = mcond[i].getNumChildren(); j < nchild; j++)
662
      {
663
5378
        if (fmfmc->isStar(mcond[i][j]))
664
        {
665
4383
          hasStar = true;
666
4383
          inst.push_back(fmfmc->getModelBasisTerm(mcond[i][j].getType()));
667
        }
668
        else
669
        {
670
995
          inst.push_back(mcond[i][j]);
671
        }
672
      }
673
3619
      bool addInst = true;
674
3619
      if (hasStar)
675
      {
676
        // try obvious (specified by inst)
677
6474
        Node ev = d_quant_models[f].evaluate(fmfmc, inst);
678
3237
        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
382
        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
3628
      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
3610
      if (options::fmfBound() || options::stringExp())
713
      {
714
2686
        std::vector<Node> cond;
715
1343
        cond.push_back(d_quant_cond[f]);
716
1343
        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
2686
        Node c = mkCond(cond);
720
1343
        unsigned prevInst = d_addedLemmas;
721
1343
        exhaustiveInstantiate(fmfmc, f, c);
722
1343
        if (d_addedLemmas == prevInst)
723
        {
724
1026
          d_star_insts[f].push_back(i);
725
        }
726
1343
        continue;
727
      }
728
      // just add the instance
729
2267
      d_triedLemmas++;
730
4534
      if (instq->addInstantiation(f,
731
                                  inst,
732
                                  InferenceId::QUANTIFIERS_INST_FMF_FMC,
733
4534
                                  Node::null(),
734
                                  true))
735
      {
736
734
        Trace("fmc-debug-inst") << "** Added instantiation." << std::endl;
737
734
        d_addedLemmas++;
738
734
        if (d_qstate.isInConflict() || options::fmfOneInstPerRound())
739
        {
740
          break;
741
        }
742
      }
743
      else
744
      {
745
3066
        Trace("fmc-debug-inst")
746
1533
            << "** Instantiation was duplicate." << std::endl;
747
        // might try it next effort level
748
1533
        d_star_insts[f].push_back(i);
749
      }
750
    }
751
6580
    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
2819
  std::vector<Node>& mcond = d_quant_models[f].d_cond;
757
2819
  if (!d_star_insts[f].empty())
758
  {
759
1629
    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
3225
    Def temp;
767
    // simplify the exceptions?
768
3354
    for (int i = (d_star_insts[f].size() - 1); i >= 0; i--)
769
    {
770
      // get witness for d_star_insts[f][i]
771
1758
      int j = d_star_insts[f][i];
772
1758
      if (temp.addEntry(fmfmc, mcond[j], d_quant_models[f].d_value[j]))
773
      {
774
1704
        if (!exhaustiveInstantiate(fmfmc, f, mcond[j]))
775
        {
776
          // something went wrong, resort to exhaustive instantiation
777
33
          return 0;
778
        }
779
      }
780
    }
781
  }
782
2786
  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
5215
  RepBoundFmcEntry(QuantifiersBoundInference& qbi,
813
                   Node e,
814
                   FirstOrderModelFmc* f)
815
5215
      : QRepBoundExt(qbi, f), d_entry(e), d_fm(f)
816
  {
817
5215
  }
818
5215
  ~RepBoundFmcEntry() {}
819
  /** set bound */
820
7084
  virtual RsiEnumType setBound(Node owner,
821
                               unsigned i,
822
                               std::vector<Node>& elements) override
823
  {
824
7084
    if (!d_fm->isStar(d_entry[i]))
825
    {
826
      // only need to consider the single point
827
1756
      elements.push_back(d_entry[i]);
828
1756
      return ENUM_DEFAULT;
829
    }
830
5328
    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
5215
bool FullModelChecker::exhaustiveInstantiate(FirstOrderModelFmc* fm,
841
                                             Node f,
842
                                             Node c)
843
{
844
5215
  Trace("fmc-exh") << "----Exhaustive instantiate based on " << c << " ";
845
5215
  debugPrintCond("fmc-exh", c, true);
846
5215
  Trace("fmc-exh")<< std::endl;
847
5215
  QuantifiersBoundInference& qbi = d_qreg.getQuantifiersBoundInference();
848
10430
  RepBoundFmcEntry rbfe(qbi, c, fm);
849
10430
  RepSetIterator riter(fm->getRepSet(), &rbfe);
850
5215
  Trace("fmc-exh-debug") << "Set quantifier..." << std::endl;
851
  //initialize
852
5215
  if (riter.setQuantifier(f))
853
  {
854
5215
    Trace("fmc-exh-debug") << "Set element domains..." << std::endl;
855
5215
    int addedLemmas = 0;
856
    //now do full iteration
857
5215
    Instantiate* ie = d_qim.getInstantiate();
858
66253
    while( !riter.isFinished() ){
859
30519
      d_triedLemmas++;
860
30519
      Trace("fmc-exh-debug") << "Inst : ";
861
61038
      std::vector< Node > ev_inst;
862
61038
      std::vector< Node > inst;
863
106292
      for (unsigned i = 0; i < riter.getNumTerms(); i++)
864
      {
865
151546
        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
151546
        Node rr = riter.getCurrentTerm(i, !tn.isClosedEnumerable());
871
151546
        Node r = fm->getRepresentative(rr);
872
75773
        debugPrint("fmc-exh-debug", r);
873
75773
        Trace("fmc-exh-debug") << " (term : " << rr << ")";
874
75773
        ev_inst.push_back( r );
875
75773
        inst.push_back( rr );
876
      }
877
30519
      int ev_index = d_quant_models[f].getGeneralizationIndex(fm, ev_inst);
878
30519
      Trace("fmc-exh-debug") << ", index = " << ev_index << " / " << d_quant_models[f].d_value.size();
879
61038
      Node ev = ev_index==-1 ? Node::null() : d_quant_models[f].d_value[ev_index];
880
30519
      if (ev!=d_true) {
881
27789
        Trace("fmc-exh-debug") << ", add!";
882
        //add as instantiation
883
55578
        if (ie->addInstantiation(f,
884
                                 inst,
885
                                 InferenceId::QUANTIFIERS_INST_FMF_FMC_EXH,
886
55578
                                 Node::null(),
887
                                 true))
888
        {
889
4936
          Trace("fmc-exh-debug")  << " ...success.";
890
4936
          addedLemmas++;
891
4936
          if (d_qstate.isInConflict() || options::fmfOneInstPerRound())
892
          {
893
            break;
894
          }
895
        }else{
896
22853
          Trace("fmc-exh-debug") << ", failed.";
897
        }
898
      }else{
899
2730
        Trace("fmc-exh-debug") << ", already true";
900
      }
901
30519
      Trace("fmc-exh-debug") << std::endl;
902
30519
      int index = riter.increment();
903
30519
      Trace("fmc-exh-debug") << "Incremented index " << index << std::endl;
904
30519
      if( !riter.isFinished() ){
905
78789
        if (index >= 0 && riter.d_index[index] > 0 && addedLemmas > 0
906
32770
            && riter.d_enum_type[index] == ENUM_CUSTOM)
907
        {
908
2770
          Trace("fmc-exh-debug")
909
1385
              << "Since this is a custom enumeration, skip to the next..."
910
1385
              << std::endl;
911
1385
          riter.incrementAtIndex(index - 1);
912
        }
913
      }
914
    }
915
5215
    d_addedLemmas += addedLemmas;
916
5215
    Trace("fmc-exh") << "----Finished Exhaustive instantiate, lemmas = " << addedLemmas << ", incomplete=" << riter.isIncomplete() << std::endl;
917
5215
    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
114622
void FullModelChecker::doCheck(FirstOrderModelFmc * fm, Node f, Def & d, Node n ) {
925
114622
  Trace("fmc-debug") << "Check " << n << " " << n.getKind() << std::endl;
926
  //first check if it is a bounding literal
927
114622
  if( n.hasAttribute(BoundIntLitAttribute()) ){
928
2812
    Trace("fmc-debug") << "It is a bounding literal, polarity = " << n.getAttribute(BoundIntLitAttribute()) << std::endl;
929
2812
    d.addEntry(fm, mkCondDefault(fm, f), n.getAttribute(BoundIntLitAttribute())==1 ? d_false : d_true );
930
111810
  }else if( n.getKind() == kind::BOUND_VARIABLE ){
931
25867
    Trace("fmc-debug") << "Add default entry..." << std::endl;
932
25867
    d.addEntry(fm, mkCondDefault(fm, f), n);
933
  }
934
85943
  else if( n.getKind() == kind::NOT ){
935
    //just do directly
936
8876
    doCheck( fm, f, d, n[0] );
937
8876
    doNegate( d );
938
  }
939
77067
  else if( n.getKind() == kind::FORALL ){
940
1998
    d.addEntry(fm, mkCondDefault(fm, f), Node::null());
941
  }
942
75069
  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
432
    d.reset();
948
432
    d.addEntry(fm, mkCondDefault(fm, f), Node::null());
949
  }
950
74637
  else if( n.getNumChildren()==0 ){
951
39980
    Node r = n;
952
19990
    if( !n.isConst() ){
953
17024
      TypeNode tn = n.getType();
954
8512
      if( !fm->hasTerm(n) && tn.isFirstClass() ){
955
185
        r = getSomeDomainElement(fm, tn );
956
      }
957
8512
      r = fm->getRepresentative( r );
958
    }
959
19990
    Trace("fmc-debug") << "Add constant entry..." << std::endl;
960
19990
    d.addEntry(fm, mkCondDefault(fm, f), r);
961
  }
962
  else{
963
109294
    std::vector< int > var_ch;
964
109294
    std::vector< Def > children;
965
153813
    for( int i=0; i<(int)n.getNumChildren(); i++) {
966
198332
      Def dc;
967
99166
      doCheck(fm, f, dc, n[i]);
968
99166
      children.push_back(dc);
969
99166
      if( n[i].getKind() == kind::BOUND_VARIABLE ){
970
25863
        var_ch.push_back(i);
971
      }
972
    }
973
974
54647
    if( n.getKind()==APPLY_UF ){
975
23972
      Trace("fmc-debug") << "Do uninterpreted compose " << n << std::endl;
976
      //uninterpreted compose
977
23972
      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
30675
      if( !var_ch.empty() ){
990
4173
        if( n.getKind()==EQUAL && !n[0].getType().isBoolean() ){
991
788
          if( var_ch.size()==2 ){
992
302
            Trace("fmc-debug") << "Do variable equality " << n << std::endl;
993
302
            doVariableEquality( fm, f, d, n );
994
          }else{
995
486
            Trace("fmc-debug") << "Do variable relation " << n << std::endl;
996
486
            doVariableRelation( fm, f, d, var_ch[0]==0 ? children[1] : children[0], var_ch[0]==0 ? n[0] : n[1] );
997
          }
998
        }else{
999
3385
          Trace("fmc-warn") << "Don't know how to check " << n << std::endl;
1000
3385
          d.addEntry(fm, mkCondDefault(fm, f), Node::null());
1001
        }
1002
      }else{
1003
26502
        Trace("fmc-debug") << "Do interpreted compose " << n << std::endl;
1004
53004
        std::vector< Node > cond;
1005
26502
        mkCondDefaultVec(fm, f, cond);
1006
53004
        std::vector< Node > val;
1007
        //interpreted compose
1008
26502
        doInterpretedCompose( fm, f, d, n, children, 0, cond, val );
1009
      }
1010
    }
1011
54647
    Trace("fmc-debug") << "Simplify the definition..." << std::endl;
1012
54647
    d.debugPrint("fmc-debug", Node::null(), this);
1013
54647
    d.simplify(this, fm);
1014
54647
    Trace("fmc-debug") << "Done simplifying" << std::endl;
1015
  }
1016
114622
  Trace("fmc-debug") << "Definition for " << n << " is : " << std::endl;
1017
114622
  d.debugPrint("fmc-debug", Node::null(), this);
1018
114622
  Trace("fmc-debug") << std::endl;
1019
114622
}
1020
1021
8876
void FullModelChecker::doNegate( Def & dc ) {
1022
20352
  for (unsigned i=0; i<dc.d_cond.size(); i++) {
1023
22952
    Node v = dc.d_value[i];
1024
11476
    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
8267
      dc.d_value[i] =
1031
16534
          v == d_true ? d_false : (v == d_false ? d_true : Node::null());
1032
    }
1033
  }
1034
8876
}
1035
1036
302
void FullModelChecker::doVariableEquality( FirstOrderModelFmc * fm, Node f, Def & d, Node eq ) {
1037
604
  std::vector<Node> cond;
1038
302
  mkCondDefaultVec(fm, f, cond);
1039
302
  if (eq[0]==eq[1]){
1040
    d.addEntry(fm, mkCond(cond), d_true);
1041
  }else{
1042
604
    TypeNode tn = eq[0].getType();
1043
302
    if( tn.isSort() ){
1044
302
      int j = fm->getVariableId(f, eq[0]);
1045
302
      int k = fm->getVariableId(f, eq[1]);
1046
302
      const RepSet* rs = fm->getRepSet();
1047
302
      if (!rs->hasType(tn))
1048
      {
1049
        getSomeDomainElement( fm, tn );  //to verify the type is initialized
1050
      }
1051
302
      unsigned nreps = rs->getNumRepresentatives(tn);
1052
880
      for (unsigned i = 0; i < nreps; i++)
1053
      {
1054
1156
        Node r = fm->getRepresentative(rs->getRepresentative(tn, i));
1055
578
        cond[j+1] = r;
1056
578
        cond[k+1] = r;
1057
578
        d.addEntry( fm, mkCond(cond), d_true);
1058
      }
1059
302
      d.addEntry( fm, mkCondDefault(fm, f), d_false);
1060
    }else{
1061
      d.addEntry( fm, mkCondDefault(fm, f), Node::null());
1062
    }
1063
  }
1064
302
}
1065
1066
486
void FullModelChecker::doVariableRelation( FirstOrderModelFmc * fm, Node f, Def & d, Def & dc, Node v) {
1067
486
  int j = fm->getVariableId(f, v);
1068
1106
  for (unsigned i=0; i<dc.d_cond.size(); i++) {
1069
1240
    Node val = dc.d_value[i];
1070
620
    if( val.isNull() ){
1071
9
      d.addEntry( fm, dc.d_cond[i], val);
1072
    }else{
1073
611
      if( dc.d_cond[i][j]!=val ){
1074
489
        if (fm->isStar(dc.d_cond[i][j])) {
1075
954
          std::vector<Node> cond;
1076
477
          mkCondVec(dc.d_cond[i],cond);
1077
477
          cond[j+1] = val;
1078
477
          d.addEntry(fm, mkCond(cond), d_true);
1079
477
          cond[j+1] = fm->getStar(val.getType());
1080
477
          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
486
}
1090
1091
23972
void FullModelChecker::doUninterpretedCompose( FirstOrderModelFmc * fm, Node f, Def & d, Node op, std::vector< Def > & dc ) {
1092
23972
  Trace("fmc-uf-debug") << "Definition : " << std::endl;
1093
23972
  fm->d_models[op]->debugPrint("fmc-uf-debug", op, this);
1094
23972
  Trace("fmc-uf-debug") << std::endl;
1095
1096
47944
  std::vector< Node > cond;
1097
23972
  mkCondDefaultVec(fm, f, cond);
1098
47944
  std::vector< Node > val;
1099
23972
  doUninterpretedCompose( fm, f, d, *fm->d_models[op], dc, 0, cond, val);
1100
23972
}
1101
1102
66362
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
66362
  Trace("fmc-uf-process") << "process at " << index << std::endl;
1106
180969
  for( unsigned i=1; i<cond.size(); i++) {
1107
114607
    debugPrint("fmc-uf-process", cond[i], true);
1108
114607
    Trace("fmc-uf-process") << " ";
1109
  }
1110
66362
  Trace("fmc-uf-process") << std::endl;
1111
66362
  if (index==(int)dc.size()) {
1112
    //we have an entry, now do actual compose
1113
59976
    std::map< int, Node > entries;
1114
29988
    doUninterpretedCompose2( fm, f, entries, 0, cond, val, df.d_et);
1115
29988
    if( entries.empty() ){
1116
1061
      d.addEntry(fm, mkCond(cond), Node::null());
1117
    }else{
1118
      //add them to the definition
1119
96117
      for( unsigned e=0; e<df.d_cond.size(); e++ ){
1120
67190
        if ( entries.find(e)!=entries.end() ){
1121
48158
          Trace("fmf-uf-process-debug") << "Add entry..." << std::endl;
1122
48158
          d.addEntry(fm, entries[e], df.d_value[e] );
1123
48158
          Trace("fmf-uf-process-debug") << "Done add entry." << std::endl;
1124
        }
1125
      }
1126
    }
1127
  }else{
1128
84652
    for (unsigned i=0; i<dc[index].d_cond.size(); i++) {
1129
48278
      if (isCompat(fm, cond, dc[index].d_cond[i])!=0) {
1130
84780
        std::vector< Node > new_cond;
1131
42390
        new_cond.insert(new_cond.end(), cond.begin(), cond.end());
1132
42390
        if( doMeet(fm, new_cond, dc[index].d_cond[i]) ){
1133
42390
          Trace("fmc-uf-process") << "index " << i << " succeeded meet." << std::endl;
1134
42390
          val.push_back(dc[index].d_value[i]);
1135
42390
          doUninterpretedCompose(fm, f, d, df, dc, index+1, new_cond, val);
1136
42390
          val.pop_back();
1137
        }else{
1138
          Trace("fmc-uf-process") << "index " << i << " failed meet." << std::endl;
1139
        }
1140
      }
1141
    }
1142
  }
1143
66362
}
1144
1145
100271
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
100271
  Trace("fmc-uf-process") << "compose " << index << " / " << val.size() << std::endl;
1150
273120
  for( unsigned i=1; i<cond.size(); i++) {
1151
172849
    debugPrint("fmc-uf-process", cond[i], true);
1152
172849
    Trace("fmc-uf-process") << " ";
1153
  }
1154
100271
  Trace("fmc-uf-process") << std::endl;
1155
100271
  if (index==(int)val.size()) {
1156
96316
    Node c = mkCond(cond);
1157
48158
    Trace("fmc-uf-entry") << "Entry : " << c << " -> index[" << curr.d_data << "]" << std::endl;
1158
48158
    entries[curr.d_data] = c;
1159
  }else{
1160
104226
    Node v = val[index];
1161
52113
    Trace("fmc-uf-process") << "Process " << v << std::endl;
1162
52113
    bool bind_var = false;
1163
52113
    if( !v.isNull() && v.getKind()==kind::BOUND_VARIABLE ){
1164
24471
      int j = fm->getVariableId(f, v);
1165
24471
      Trace("fmc-uf-process") << v << " is variable #" << j << std::endl;
1166
24471
      if (!fm->isStar(cond[j + 1]))
1167
      {
1168
38
        v = cond[j+1];
1169
      }else{
1170
24433
        bind_var = true;
1171
      }
1172
    }
1173
52113
    if (bind_var) {
1174
24433
      Trace("fmc-uf-process") << "bind variable..." << std::endl;
1175
24433
      int j = fm->getVariableId(f, v);
1176
24433
      Assert(fm->isStar(cond[j + 1]));
1177
64302
      for (std::map<Node, EntryTrie>::iterator it = curr.d_child.begin();
1178
64302
           it != curr.d_child.end();
1179
           ++it)
1180
      {
1181
39869
        cond[j + 1] = it->first;
1182
39869
        doUninterpretedCompose2(
1183
39869
            fm, f, entries, index + 1, cond, val, it->second);
1184
      }
1185
24433
      cond[j + 1] = fm->getStar(v.getType());
1186
    }else{
1187
27680
      if( !v.isNull() ){
1188
26610
        if (curr.d_child.find(v) != curr.d_child.end())
1189
        {
1190
7902
          Trace("fmc-uf-process") << "follow value..." << std::endl;
1191
7902
          doUninterpretedCompose2(
1192
7902
              fm, f, entries, index + 1, cond, val, curr.d_child[v]);
1193
        }
1194
53220
        Node st = fm->getStar(v.getType());
1195
26610
        if (curr.d_child.find(st) != curr.d_child.end())
1196
        {
1197
22512
          Trace("fmc-uf-process") << "follow star..." << std::endl;
1198
22512
          doUninterpretedCompose2(
1199
22512
              fm, f, entries, index + 1, cond, val, curr.d_child[st]);
1200
        }
1201
      }
1202
    }
1203
  }
1204
100271
}
1205
1206
109245
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
109245
  Trace("fmc-if-process") << "int compose " << index << " / " << dc.size() << std::endl;
1210
263031
  for( unsigned i=1; i<cond.size(); i++) {
1211
153786
    debugPrint("fmc-if-process", cond[i], true);
1212
153786
    Trace("fmc-if-process") << " ";
1213
  }
1214
109245
  Trace("fmc-if-process") << std::endl;
1215
109245
  if ( index==(int)dc.size() ){
1216
92332
    Node c = mkCond(cond);
1217
92332
    Node v = evaluateInterpreted(n, val);
1218
46166
    d.addEntry(fm, c, v);
1219
  }
1220
  else {
1221
126158
    TypeNode vtn = n.getType();
1222
164864
    for (unsigned i=0; i<dc[index].d_cond.size(); i++) {
1223
101785
      if (isCompat(fm, cond, dc[index].d_cond[i])!=0) {
1224
180618
        std::vector< Node > new_cond;
1225
90309
        new_cond.insert(new_cond.end(), cond.begin(), cond.end());
1226
90309
        if( doMeet(fm, new_cond, dc[index].d_cond[i]) ){
1227
90309
          bool process = true;
1228
90309
          if (vtn.isBoolean()) {
1229
            //short circuit
1230
115985
            if( (n.getKind()==OR && dc[index].d_value[i]==d_true) ||
1231
54603
                (n.getKind()==AND && dc[index].d_value[i]==d_false) ){
1232
15132
              Node c = mkCond(new_cond);
1233
7566
              d.addEntry(fm, c, dc[index].d_value[i]);
1234
7566
              process = false;
1235
            }
1236
          }
1237
90309
          if (process) {
1238
82743
            val.push_back(dc[index].d_value[i]);
1239
82743
            doInterpretedCompose(fm, f, d, n, dc, index+1, new_cond, val);
1240
82743
            val.pop_back();
1241
          }
1242
        }
1243
      }
1244
    }
1245
  }
1246
109245
}
1247
1248
150063
int FullModelChecker::isCompat( FirstOrderModelFmc * fm, std::vector< Node > & cond, Node c ) {
1249
150063
  Trace("fmc-debug3") << "isCompat " << c << std::endl;
1250
150063
  Assert(cond.size() == c.getNumChildren() + 1);
1251
382846
  for (unsigned i=1; i<cond.size(); i++) {
1252
250147
    if (cond[i] != c[i - 1] && !fm->isStar(cond[i]) && !fm->isStar(c[i - 1]))
1253
    {
1254
17364
      return 0;
1255
    }
1256
  }
1257
132699
  return 1;
1258
}
1259
1260
132699
bool FullModelChecker::doMeet( FirstOrderModelFmc * fm, std::vector< Node > & cond, Node c ) {
1261
132699
  Trace("fmc-debug3") << "doMeet " << c << std::endl;
1262
132699
  Assert(cond.size() == c.getNumChildren() + 1);
1263
350150
  for (unsigned i=1; i<cond.size(); i++) {
1264
217451
    if( cond[i]!=c[i-1] ) {
1265
64830
      if (fm->isStar(cond[i]))
1266
      {
1267
36639
        cond[i] = c[i - 1];
1268
      }
1269
28191
      else if (!fm->isStar(c[i - 1]))
1270
      {
1271
        return false;
1272
      }
1273
    }
1274
  }
1275
132699
  return true;
1276
}
1277
1278
162780
Node FullModelChecker::mkCond( std::vector< Node > & cond ) {
1279
162780
  return NodeManager::currentNM()->mkNode(APPLY_UF, cond);
1280
}
1281
1282
56954
Node FullModelChecker::mkCondDefault( FirstOrderModelFmc * fm, Node f) {
1283
113908
  std::vector< Node > cond;
1284
56954
  mkCondDefaultVec(fm, f, cond);
1285
113908
  return mkCond(cond);
1286
}
1287
1288
107730
void FullModelChecker::mkCondDefaultVec( FirstOrderModelFmc * fm, Node f, std::vector< Node > & cond ) {
1289
107730
  Trace("fmc-debug") << "Make default vec" << std::endl;
1290
  //get function symbol for f
1291
107730
  cond.push_back(d_quant_cond[f]);
1292
270354
  for (unsigned i=0; i<f[0].getNumChildren(); i++) {
1293
325248
    Node ts = fm->getStar(f[0][i].getType());
1294
162624
    Assert(ts.getType() == f[0][i].getType());
1295
162624
    cond.push_back(ts);
1296
  }
1297
107730
}
1298
1299
477
void FullModelChecker::mkCondVec( Node n, std::vector< Node > & cond ) {
1300
477
  cond.push_back(n.getOperator());
1301
1074
  for( unsigned i=0; i<n.getNumChildren(); i++ ){
1302
597
    cond.push_back( n[i] );
1303
  }
1304
477
}
1305
1306
46166
Node FullModelChecker::evaluateInterpreted( Node n, std::vector< Node > & vals ) {
1307
46166
  if( n.getKind()==EQUAL && !n[0].getType().isBoolean() ){
1308
11391
    if (!vals[0].isNull() && !vals[1].isNull()) {
1309
9636
      return vals[0]==vals[1] ? d_true : d_false;
1310
    }else{
1311
1755
      return Node::null();
1312
    }
1313
34775
  }else if( n.getKind()==ITE ){
1314
3837
    if( vals[0]==d_true ){
1315
1702
      return vals[1];
1316
2135
    }else if( vals[0]==d_false ){
1317
2051
      return vals[2];
1318
    }else{
1319
84
      return vals[1]==vals[2] ? vals[1] : Node::null();
1320
    }
1321
30938
  }else if( n.getKind()==AND || n.getKind()==OR ){
1322
5810
    bool isNull = false;
1323
22388
    for (unsigned i=0; i<vals.size(); i++) {
1324
16578
      if((vals[i]==d_true && n.getKind()==OR) || (vals[i]==d_false && n.getKind()==AND)) {
1325
        return vals[i];
1326
16578
      }else if( vals[i].isNull() ){
1327
4098
        isNull = true;
1328
      }
1329
    }
1330
5810
    return isNull ? Node::null() : vals[0];
1331
  }else{
1332
50256
    std::vector<Node> children;
1333
25128
    if( n.getMetaKind() == kind::metakind::PARAMETERIZED ){
1334
7863
      children.push_back( n.getOperator() );
1335
    }
1336
63885
    for (unsigned i=0; i<vals.size(); i++) {
1337
42246
      if( vals[i].isNull() ){
1338
3489
        return Node::null();
1339
      }else{
1340
38757
        children.push_back( vals[i] );
1341
      }
1342
    }
1343
43278
    Node nc = NodeManager::currentNM()->mkNode(n.getKind(), children);
1344
21639
    Trace("fmc-eval") << "Evaluate " << nc << " to ";
1345
21639
    nc = Rewriter::rewrite(nc);
1346
21639
    Trace("fmc-eval") << nc << std::endl;
1347
21639
    return nc;
1348
  }
1349
}
1350
1351
3555
Node FullModelChecker::getSomeDomainElement( FirstOrderModelFmc * fm, TypeNode tn ) {
1352
3555
  bool addRepId = !fm->getRepSet()->hasType(tn);
1353
3555
  Node de = fm->getSomeDomainElement(tn);
1354
3555
  if( addRepId ){
1355
116
    d_rep_ids[tn][de] = 0;
1356
  }
1357
3555
  return de;
1358
}
1359
1360
7207
Node FullModelChecker::getFunctionValue(FirstOrderModelFmc * fm, Node op, const char* argPrefix ) {
1361
7207
  return fm->getFunctionValue(op, argPrefix);
1362
}
1363
1364
1365
38643
bool FullModelChecker::useSimpleModels() {
1366
38643
  return options::fmfFmcSimple();
1367
}
1368
20688
void FullModelChecker::registerQuantifiedFormula(Node q)
1369
{
1370
20688
  if (d_quant_cond.find(q) != d_quant_cond.end())
1371
  {
1372
19202
    return;
1373
  }
1374
1486
  NodeManager* nm = NodeManager::currentNM();
1375
1486
  SkolemManager* sm = nm->getSkolemManager();
1376
2972
  std::vector<TypeNode> types;
1377
3677
  for (const Node& v : q[0])
1378
  {
1379
4382
    TypeNode tn = v.getType();
1380
2191
    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
2191
    types.push_back(tn);
1388
  }
1389
2972
  TypeNode typ = nm->mkFunctionType(types, nm->booleanType());
1390
2972
  Node op = sm->mkDummySkolem("qfmc", typ, "op for full-model checking");
1391
1486
  d_quant_cond[q] = op;
1392
}
1393
1394
20688
bool FullModelChecker::isHandled(Node q) const
1395
{
1396
20688
  return d_unhandledQuant.find(q) == d_unhandledQuant.end();
1397
}
1398
1399
}  // namespace fmcheck
1400
}  // namespace quantifiers
1401
}  // namespace theory
1402
29577
}  // namespace cvc5