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-09 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
185496
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
57185
  bool operator() (int i,int j) {
48
57185
    return (d_mba_count[d_terms[i]] < d_mba_count[d_terms[j]]);
49
  }
50
};
51
52
421758
bool EntryTrie::hasGeneralization( FirstOrderModelFmc * m, Node c, int index ) {
53
421758
  if (index==(int)c.getNumChildren()) {
54
16404
    return d_data!=-1;
55
  }else{
56
810708
    TypeNode tn = c[index].getType();
57
810708
    Node st = m->getStar(tn);
58
405354
    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
390158
    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
368516
    if( c[index].getType().isSort() ){
69
      //for star: check if all children are defined and have generalizations
70
267760
      if( c[index]==st ){     ///options::fmfFmcCoverSimplify()
71
        //check if all children exist and are complete
72
        unsigned num_child_def =
73
154357
            d_child.size() - (d_child.find(st) != d_child.end() ? 1 : 0);
74
154357
        if (num_child_def == m->getRepSet()->getNumRepresentatives(tn))
75
        {
76
8587
          bool complete = true;
77
14791
          for ( std::map<Node,EntryTrie>::iterator it = d_child.begin(); it != d_child.end(); ++it ){
78
12538
            if( !m->isStar(it->first) ){
79
12036
              if( !it->second.hasGeneralization(m, c, index+1) ){
80
6334
                complete = false;
81
6334
                break;
82
              }
83
            }
84
          }
85
8587
          if( complete ){
86
2253
            return true;
87
          }
88
        }
89
      }
90
    }
91
92
366263
    return false;
93
  }
94
}
95
96
136475
int EntryTrie::getGeneralizationIndex( FirstOrderModelFmc * m, std::vector<Node> & inst, int index ) {
97
136475
  Debug("fmc-entry-trie") << "Get generalization index " << inst.size() << " " << index << std::endl;
98
136475
  if (index==(int)inst.size()) {
99
40094
    return d_data;
100
  }else{
101
96381
    int minIndex = -1;
102
192762
    Node st = m->getStar(inst[index].getType());
103
96381
    if (d_child.find(st) != d_child.end())
104
    {
105
89911
      minIndex = d_child[st].getGeneralizationIndex(m, inst, index + 1);
106
    }
107
192762
    Node cc = inst[index];
108
96381
    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
96381
    return minIndex;
117
  }
118
}
119
120
781144
void EntryTrie::addEntry( FirstOrderModelFmc * m, Node c, Node v, int data, int index ) {
121
781144
  if (index==(int)c.getNumChildren()) {
122
299671
    if(d_data==-1) {
123
299671
      d_data = data;
124
    }
125
  }
126
  else {
127
481473
    d_child[ c[index] ].addEntry(m,c,v,data,index+1);
128
481473
    if( d_complete==0 ){
129
      d_complete = -1;
130
    }
131
  }
132
781144
}
133
134
336720
void EntryTrie::getEntries( FirstOrderModelFmc * m, Node c, std::vector<int> & compat, std::vector<int> & gen, int index, bool is_gen ) {
135
336720
  if (index==(int)c.getNumChildren()) {
136
70665
    if( d_data!=-1) {
137
70665
      if( is_gen ){
138
64873
        gen.push_back(d_data);
139
      }
140
70665
      compat.push_back(d_data);
141
    }
142
  }else{
143
266055
    if (m->isStar(c[index])) {
144
274011
      for ( std::map<Node,EntryTrie>::iterator it = d_child.begin(); it != d_child.end(); ++it ){
145
111730
        it->second.getEntries(m, c, compat, gen, index+1, is_gen );
146
      }
147
    }else{
148
207548
      Node st = m->getStar(c[index].getType());
149
103774
      if(d_child.find(st)!=d_child.end()) {
150
7748
        d_child[st].getEntries(m, c, compat, gen, index+1, false);
151
      }
152
103774
      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
336720
}
159
160
312626
bool Def::addEntry( FirstOrderModelFmc * m, Node c, Node v) {
161
312626
  if (d_et.hasGeneralization(m, c)) {
162
12955
    Trace("fmc-debug") << "Already has generalization, skip." << std::endl;
163
12955
    return false;
164
  }
165
299671
  int newIndex = (int)d_cond.size();
166
299671
  if (!d_has_simplified) {
167
374104
    std::vector<int> compat;
168
374104
    std::vector<int> gen;
169
187052
    d_et.getEntries(m, c, compat, gen);
170
257717
    for( unsigned i=0; i<compat.size(); i++) {
171
70665
      if( d_status[compat[i]]==status_unk ){
172
61753
        if( d_value[compat[i]]!=v ){
173
38821
          d_status[compat[i]] = status_non_redundant;
174
        }
175
      }
176
    }
177
251925
    for( unsigned i=0; i<gen.size(); i++) {
178
64873
      if( d_status[gen[i]]==status_unk ){
179
19268
        if( d_value[gen[i]]==v ){
180
19268
          d_status[gen[i]] = status_redundant;
181
        }
182
      }
183
    }
184
187052
    d_status.push_back( status_unk );
185
  }
186
299671
  d_et.addEntry(m, c, v, newIndex);
187
299671
  d_cond.push_back(c);
188
299671
  d_value.push_back(v);
189
299671
  return true;
190
}
191
192
3259
Node Def::evaluate( FirstOrderModelFmc * m, std::vector<Node>& inst ) {
193
3259
  int gindex = d_et.getGeneralizationIndex(m, inst);
194
3259
  if (gindex!=-1) {
195
3255
    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
30467
int Def::getGeneralizationIndex( FirstOrderModelFmc * m, std::vector<Node>& inst ) {
203
30467
  return d_et.getGeneralizationIndex(m, inst);
204
}
205
206
68299
void Def::basic_simplify( FirstOrderModelFmc * m ) {
207
68299
  d_has_simplified = true;
208
136598
  std::vector< Node > cond;
209
68299
  cond.insert( cond.end(), d_cond.begin(), d_cond.end() );
210
68299
  d_cond.clear();
211
136598
  std::vector< Node > value;
212
68299
  value.insert( value.end(), d_value.begin(), d_value.end() );
213
68299
  d_value.clear();
214
68299
  d_et.reset();
215
200186
  for (unsigned i=0; i<d_status.size(); i++) {
216
131887
    if( d_status[i]!=status_redundant ){
217
112619
      addEntry(m, cond[i], value[i]);
218
    }
219
  }
220
68299
  d_status.clear();
221
68299
}
222
223
62238
void Def::simplify(FullModelChecker * mc, FirstOrderModelFmc * m) {
224
62238
  Trace("fmc-simplify") << "Simplify definition, #cond = " << d_cond.size() << std::endl;
225
62238
  basic_simplify( m );
226
62238
  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
62238
  if( !d_cond.empty() ){
230
62238
    bool last_all_stars = true;
231
124476
    Node cc = d_cond[d_cond.size()-1];
232
144793
    for( unsigned i=0; i<cc.getNumChildren(); i++ ){
233
88616
      if (!m->isStar(cc[i]))
234
      {
235
6061
        last_all_stars = false;
236
6061
        break;
237
      }
238
    }
239
62238
    if( !last_all_stars ){
240
6061
      Trace("fmc-cover-simplify") << "Need to modify last entry to be all stars." << std::endl;
241
6061
      Trace("fmc-cover-simplify") << "Before: " << std::endl;
242
6061
      debugPrint("fmc-cover-simplify",Node::null(), mc);
243
6061
      Trace("fmc-cover-simplify") << std::endl;
244
12122
      std::vector< Node > cond;
245
6061
      cond.insert( cond.end(), d_cond.begin(), d_cond.end() );
246
6061
      d_cond.clear();
247
12122
      std::vector< Node > value;
248
6061
      value.insert( value.end(), d_value.begin(), d_value.end() );
249
6061
      d_value.clear();
250
6061
      d_et.reset();
251
6061
      d_has_simplified = false;
252
      //change the last to all star
253
12122
      std::vector< Node > nc;
254
6061
      nc.push_back( cc.getOperator() );
255
15732
      for( unsigned j=0; j< cc.getNumChildren(); j++){
256
9671
        nc.push_back(m->getStar(cc[j].getType()));
257
      }
258
6061
      cond[cond.size()-1] = NodeManager::currentNM()->mkNode( APPLY_UF, nc );
259
      //re-add the entries
260
17729
      for (unsigned i=0; i<cond.size(); i++) {
261
11668
        addEntry(m, cond[i], value[i]);
262
      }
263
6061
      Trace("fmc-cover-simplify") << "Finished re-adding entries." << std::endl;
264
6061
      basic_simplify( m );
265
6061
      Trace("fmc-cover-simplify") << "After: " << std::endl;
266
6061
      debugPrint("fmc-cover-simplify",Node::null(), mc);
267
6061
      Trace("fmc-cover-simplify") << std::endl;
268
    }
269
  }
270
62238
  Trace("fmc-simplify") << "finish simplify, #cond = " << d_cond.size() << std::endl;
271
62238
}
272
273
227601
void Def::debugPrint(const char * tr, Node op, FullModelChecker * m) {
274
227601
  if (!op.isNull()) {
275
38582
    Trace(tr) << "Model for " << op << " : " << std::endl;
276
  }
277
595690
  for( unsigned i=0; i<d_cond.size(); i++) {
278
    //print the condition
279
368089
    if (!op.isNull()) {
280
96312
      Trace(tr) << op;
281
    }
282
368089
    m->debugPrintCond(tr, d_cond[i], true);
283
368089
    Trace(tr) << " -> ";
284
368089
    m->debugPrint(tr, d_value[i]);
285
368089
    Trace(tr) << std::endl;
286
  }
287
227601
}
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
2046
bool FullModelChecker::preProcessBuildModel(TheoryModel* m) {
304
  //standard pre-process
305
2046
  if( !preProcessBuildModelStd( m ) ){
306
    return false;
307
  }
308
309
2046
  Trace("fmc") << "---Full Model Check preprocess() " << std::endl;
310
2046
  d_preinitialized_eqc.clear();
311
2046
  d_preinitialized_types.clear();
312
  //traverse equality engine
313
2046
  eq::EqClassesIterator eqcs_i = eq::EqClassesIterator(m->getEqualityEngine());
314
85596
  while( !eqcs_i.isFinished() ){
315
83550
    Node r = *eqcs_i;
316
83550
    TypeNode tr = r.getType();
317
41775
    d_preinitialized_eqc[tr] = r;
318
41775
    ++eqcs_i;
319
  }
320
321
  //must ensure model basis terms exists in model for each relevant type
322
2046
  Trace("fmc") << "preInitialize types..." << std::endl;
323
2046
  d_fm->initialize();
324
9267
  for (std::pair<const Node, Def*>& mp : d_fm->d_models)
325
  {
326
14442
    Node op = mp.first;
327
7221
    Trace("fmc") << "preInitialize types for " << op << std::endl;
328
14442
    TypeNode tno = op.getType();
329
26474
    for( unsigned i=0; i<tno.getNumChildren(); i++) {
330
19253
      Trace("fmc") << "preInitializeType " << tno[i] << std::endl;
331
19253
      preInitializeType(m, tno[i]);
332
19253
      Trace("fmc") << "finished preInitializeType " << tno[i] << std::endl;
333
    }
334
  }
335
2046
  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
11211
  for (unsigned i = 0, nquant = d_fm->getNumAssertedQuantifiers(); i < nquant;
338
       i++)
339
  {
340
18219
    Node q = d_fm->getAssertedQuantifier(i);
341
9165
    registerQuantifiedFormula(q);
342
9165
    if (!isHandled(q))
343
    {
344
111
      continue;
345
    }
346
    // make sure all types are set
347
22963
    for (const Node& v : q[0])
348
    {
349
13909
      preInitializeType(m, v.getType());
350
    }
351
  }
352
2046
  return true;
353
}
354
355
2046
bool FullModelChecker::processBuildModel(TheoryModel* m){
356
2046
  if (!m->areFunctionValuesEnabled())
357
  {
358
    // nothing to do if no functions
359
    return true;
360
  }
361
2046
  FirstOrderModelFmc* fm = d_fm.get();
362
2046
  Trace("fmc") << "---Full Model Check reset() " << std::endl;
363
2046
  d_quant_models.clear();
364
2046
  d_rep_ids.clear();
365
2046
  d_star_insts.clear();
366
  //process representatives
367
2046
  RepSet* rs = m->getRepSetPtr();
368
8707
  for (std::map<TypeNode, std::vector<Node> >::iterator it =
369
2046
           rs->d_type_reps.begin();
370
10753
       it != rs->d_type_reps.end();
371
       ++it)
372
  {
373
8707
    if( it->first.isSort() ){
374
1958
      Trace("fmc") << "Cardinality( " << it->first << " )" << " = " << it->second.size() << std::endl;
375
5649
      for( size_t a=0; a<it->second.size(); a++ ){
376
7382
        Node r = m->getRepresentative(it->second[a]);
377
3691
        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
3691
        d_rep_ids[it->first][r] = (int)a;
390
      }
391
1958
      Trace("fmc-model-debug") << std::endl;
392
    }
393
  }
394
395
  //now, make models
396
9267
  for (std::pair<const Node, Def*>& fmm : d_fm->d_models)
397
  {
398
14442
    Node op = fmm.first;
399
    //reset the model
400
7221
    d_fm->d_models[op]->reset();
401
402
14442
    std::vector< Node > add_conds;
403
14442
    std::vector< Node > add_values;
404
7221
    bool needsDefault = true;
405
7221
    if (m->hasUfTerms(op))
406
    {
407
5443
      const std::vector<Node>& uft = m->getUfTerms(op);
408
10886
      Trace("fmc-model-debug")
409
5443
          << uft.size() << " model values for " << op << " ... " << std::endl;
410
36991
      for (const Node& n : uft)
411
      {
412
        // only consider unique up to congruence (in model equality engine)?
413
31548
        add_conds.push_back( n );
414
31548
        add_values.push_back( n );
415
63096
        Node r = m->getRepresentative(n);
416
31548
        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
7221
    Trace("fmc-model-debug") << std::endl;
422
    //possibly get default
423
7221
    if( needsDefault ){
424
14442
      Node nmb = d_fm->getModelBasisOpTerm(op);
425
      //add default value if necessary
426
7221
      if (m->hasTerm(nmb))
427
      {
428
3879
        Trace("fmc-model-debug") << "Add default " << nmb << std::endl;
429
3879
        add_conds.push_back( nmb );
430
3879
        add_values.push_back( nmb );
431
      }else{
432
6684
        Node vmb = getSomeDomainElement(d_fm.get(), nmb.getType());
433
3342
        Trace("fmc-model-debug") << "Add default to default representative " << nmb << " ";
434
6684
        Trace("fmc-model-debug")
435
6684
            << m->getRepSet()->getNumRepresentatives(nmb.getType())
436
3342
            << std::endl;
437
3342
        add_conds.push_back( nmb );
438
3342
        add_values.push_back( vmb );
439
      }
440
    }
441
442
14442
    std::vector< Node > conds;
443
14442
    std::vector< Node > values;
444
14442
    std::vector< Node > entry_conds;
445
    //get the entries for the model
446
45990
    for( size_t i=0; i<add_conds.size(); i++ ){
447
77538
      Node c = add_conds[i];
448
77538
      Node v = add_values[i];
449
77538
      Trace("fmc-model-debug")
450
38769
          << "Add cond/value : " << c << " -> " << v << std::endl;
451
77538
      std::vector< Node > children;
452
77538
      std::vector< Node > entry_children;
453
38769
      children.push_back(op);
454
38769
      entry_children.push_back(op);
455
38769
      bool hasNonStar = false;
456
113799
      for (const Node& ci : c)
457
      {
458
150060
        Node ri = fm->getRepresentative(ci);
459
75030
        children.push_back(ri);
460
75030
        bool isStar = false;
461
75030
        if (fm->isModelBasisTerm(ri))
462
        {
463
21596
          ri = fm->getStar(ri.getType());
464
21596
          isStar = true;
465
        }
466
        else
467
        {
468
53434
          hasNonStar = true;
469
        }
470
75030
        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
75030
        entry_children.push_back(ri);
477
      }
478
77538
      Node n = NodeManager::currentNM()->mkNode( APPLY_UF, children );
479
77538
      Node nv = fm->getRepresentative( v );
480
77538
      Trace("fmc-model-debug")
481
38769
          << "Representative of " << v << " is " << nv << std::endl;
482
38769
      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
77538
      Node en = (useSimpleModels() && hasNonStar) ? n : NodeManager::currentNM()->mkNode( APPLY_UF, entry_children );
487
38769
      if( std::find(conds.begin(), conds.end(), n )==conds.end() ){
488
24405
        Trace("fmc-model-debug") << "- add " << n << " -> " << nv << " (entry is " << en << ")" << std::endl;
489
24405
        conds.push_back(n);
490
24405
        values.push_back(nv);
491
24405
        entry_conds.push_back(en);
492
      }
493
      else {
494
14364
        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
14442
    std::vector< int > indices;
501
14442
    ModelBasisArgSort mbas;
502
31626
    for (int i=0; i<(int)conds.size(); i++) {
503
24405
      mbas.d_terms.push_back(conds[i]);
504
24405
      mbas.d_mba_count[conds[i]] = fm->getModelBasisArg(conds[i]);
505
24405
      indices.push_back(i);
506
    }
507
7221
    std::sort( indices.begin(), indices.end(), mbas );
508
509
31626
    for (int i=0; i<(int)indices.size(); i++) {
510
24405
      fm->d_models[op]->addEntry(fm, entry_conds[indices[i]], values[indices[i]]);
511
    }
512
513
7221
    Trace("fmc-model-simplify") << "Before simplification : " << std::endl;
514
7221
    fm->d_models[op]->debugPrint("fmc-model-simplify", op, this);
515
7221
    Trace("fmc-model-simplify") << std::endl;
516
517
7221
    Trace("fmc-model-simplify") << "Simplifying " << op << "..." << std::endl;
518
7221
    fm->d_models[op]->simplify( this, fm );
519
520
7221
    fm->d_models[op]->debugPrint("fmc-model", op, this);
521
7221
    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
2046
  Assert(d_addedLemmas == 0);
539
540
  //make function values
541
9267
  for( std::map<Node, Def * >::iterator it = fm->d_models.begin(); it != fm->d_models.end(); ++it ){
542
14442
    Node f_def = getFunctionValue( fm, it->first, "$x" );
543
7221
    m->assignFunctionDefinition( it->first, f_def );
544
  }
545
2046
  return TheoryEngineModelBuilder::processBuildModel( m );
546
}
547
548
33162
void FullModelChecker::preInitializeType(TheoryModel* m, TypeNode tn)
549
{
550
33162
  if( d_preinitialized_types.find( tn )==d_preinitialized_types.end() ){
551
4871
    d_preinitialized_types[tn] = true;
552
4871
    if (tn.isFirstClass())
553
    {
554
4865
      Trace("fmc") << "Get model basis term " << tn << "..." << std::endl;
555
9730
      Node mb = d_fm->getModelBasisTerm(tn);
556
4865
      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
4865
      if (!m->hasTerm(mb) && !mb.isConst())
562
      {
563
558
        std::map<TypeNode, Node>::iterator itpe = d_preinitialized_eqc.find(tn);
564
558
        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
588
          Trace("fmc") << "...add model basis eqc equality to model " << mb
573
294
                       << " == " << itpe->second << " " << tn << std::endl;
574
294
          bool ret = m->assertEquality(mb, itpe->second, true);
575
294
          AlwaysAssert(ret);
576
        }
577
      }
578
    }
579
  }
580
33162
}
581
582
373302
void FullModelChecker::debugPrintCond(const char * tr, Node n, bool dispStar) {
583
373302
  Trace(tr) << "(";
584
960205
  for( unsigned j=0; j<n.getNumChildren(); j++) {
585
586903
    if( j>0 ) Trace(tr) << ", ";
586
586903
    debugPrint(tr, n[j], dispStar);
587
  }
588
373302
  Trace(tr) << ")";
589
373302
}
590
591
1473063
void FullModelChecker::debugPrint(const char * tr, Node n, bool dispStar) {
592
1473063
  if( n.isNull() ){
593
32717
    Trace(tr) << "null";
594
  }
595
1440346
  else if (FirstOrderModelFmc::isStar(n) && dispStar)
596
  {
597
696384
    Trace(tr) << "*";
598
  }
599
  else
600
  {
601
1487924
    TypeNode tn = n.getType();
602
743962
    if( tn.isSort() && d_rep_ids.find(tn)!=d_rep_ids.end() ){
603
348651
      if (d_rep_ids[tn].find(n)!=d_rep_ids[tn].end()) {
604
326381
        Trace(tr) << d_rep_ids[tn][n];
605
      }else{
606
22270
        Trace(tr) << n;
607
      }
608
    }else{
609
395311
      Trace(tr) << n;
610
    }
611
  }
612
1473063
}
613
614
615
11605
int FullModelChecker::doExhaustiveInstantiation( FirstOrderModel * fm, Node f, int effort ) {
616
11605
  Trace("fmc") << "Full model check " << f << ", effort = " << effort << "..." << std::endl;
617
  // register the quantifier
618
11605
  registerQuantifiedFormula(f);
619
11605
  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
11605
  if (!optUseModel() || !isHandled(f))
623
  {
624
    return 0;
625
  }
626
11605
  FirstOrderModelFmc* fmfmc = static_cast<FirstOrderModelFmc*>(fm);
627
11605
  if (effort == 0)
628
  {
629
8786
    if (options::mbqiMode() == options::MbqiMode::NONE)
630
    {
631
      // just exhaustive instantiate
632
4332
      Node c = mkCondDefault(fmfmc, f);
633
2166
      d_quant_models[f].addEntry(fmfmc, c, d_false);
634
2166
      if (!exhaustiveInstantiate(fmfmc, f, c))
635
      {
636
20
        return 0;
637
      }
638
2146
      return 1;
639
    }
640
    // model check the quantifier
641
6620
    doCheck(fmfmc, f, d_quant_models[f], f[1]);
642
6620
    std::vector<Node>& mcond = d_quant_models[f].d_cond;
643
6620
    Trace("fmc") << "Definition for quantifier " << f << " is : " << std::endl;
644
6620
    Assert(!mcond.empty());
645
6620
    d_quant_models[f].debugPrint("fmc", Node::null(), this);
646
6620
    Trace("fmc") << std::endl;
647
648
    // consider all entries going to non-true
649
6620
    Instantiate* instq = d_qim.getInstantiate();
650
15237
    for (unsigned i = 0, msize = mcond.size(); i < msize; i++)
651
    {
652
8617
      if (d_quant_models[f].d_value[i] == d_true)
653
      {
654
        // already satisfied
655
11296
        continue;
656
      }
657
7290
      Trace("fmc-inst") << "Instantiate based on " << mcond[i] << "..."
658
3645
                        << std::endl;
659
3645
      bool hasStar = false;
660
5938
      std::vector<Node> inst;
661
9049
      for (unsigned j = 0, nchild = mcond[i].getNumChildren(); j < nchild; j++)
662
      {
663
5404
        if (fmfmc->isStar(mcond[i][j]))
664
        {
665
4405
          hasStar = true;
666
4405
          inst.push_back(fmfmc->getModelBasisTerm(mcond[i][j].getType()));
667
        }
668
        else
669
        {
670
999
          inst.push_back(mcond[i][j]);
671
        }
672
      }
673
3645
      bool addInst = true;
674
3645
      if (hasStar)
675
      {
676
        // try obvious (specified by inst)
677
6518
        Node ev = d_quant_models[f].evaluate(fmfmc, inst);
678
3259
        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
386
        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
3654
      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
3636
      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
2293
      d_triedLemmas++;
730
4586
      if (instq->addInstantiation(f,
731
                                  inst,
732
                                  InferenceId::QUANTIFIERS_INST_FMF_FMC,
733
4586
                                  Node::null(),
734
                                  true))
735
      {
736
758
        Trace("fmc-debug-inst") << "** Added instantiation." << std::endl;
737
758
        d_addedLemmas++;
738
758
        if (d_qstate.isInConflict() || options::fmfOneInstPerRound())
739
        {
740
          break;
741
        }
742
      }
743
      else
744
      {
745
3070
        Trace("fmc-debug-inst")
746
1535
            << "** Instantiation was duplicate." << std::endl;
747
        // might try it next effort level
748
1535
        d_star_insts[f].push_back(i);
749
      }
750
    }
751
6620
    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
5213
  RepBoundFmcEntry(QuantifiersBoundInference& qbi,
813
                   Node e,
814
                   FirstOrderModelFmc* f)
815
5213
      : QRepBoundExt(qbi, f), d_entry(e), d_fm(f)
816
  {
817
5213
  }
818
5213
  ~RepBoundFmcEntry() {}
819
  /** set bound */
820
7082
  virtual RsiEnumType setBound(Node owner,
821
                               unsigned i,
822
                               std::vector<Node>& elements) override
823
  {
824
7082
    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
5326
    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
5213
bool FullModelChecker::exhaustiveInstantiate(FirstOrderModelFmc* fm,
841
                                             Node f,
842
                                             Node c)
843
{
844
5213
  Trace("fmc-exh") << "----Exhaustive instantiate based on " << c << " ";
845
5213
  debugPrintCond("fmc-exh", c, true);
846
5213
  Trace("fmc-exh")<< std::endl;
847
5213
  QuantifiersBoundInference& qbi = d_qreg.getQuantifiersBoundInference();
848
10426
  RepBoundFmcEntry rbfe(qbi, c, fm);
849
10426
  RepSetIterator riter(fm->getRepSet(), &rbfe);
850
5213
  Trace("fmc-exh-debug") << "Set quantifier..." << std::endl;
851
  //initialize
852
5213
  if (riter.setQuantifier(f))
853
  {
854
5213
    Trace("fmc-exh-debug") << "Set element domains..." << std::endl;
855
5213
    int addedLemmas = 0;
856
    //now do full iteration
857
5213
    Instantiate* ie = d_qim.getInstantiate();
858
66147
    while( !riter.isFinished() ){
859
30467
      d_triedLemmas++;
860
30467
      Trace("fmc-exh-debug") << "Inst : ";
861
60934
      std::vector< Node > ev_inst;
862
60934
      std::vector< Node > inst;
863
106104
      for (unsigned i = 0; i < riter.getNumTerms(); i++)
864
      {
865
151274
        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
151274
        Node rr = riter.getCurrentTerm(i, !tn.isClosedEnumerable());
871
151274
        Node r = fm->getRepresentative(rr);
872
75637
        debugPrint("fmc-exh-debug", r);
873
75637
        Trace("fmc-exh-debug") << " (term : " << rr << ")";
874
75637
        ev_inst.push_back( r );
875
75637
        inst.push_back( rr );
876
      }
877
30467
      int ev_index = d_quant_models[f].getGeneralizationIndex(fm, ev_inst);
878
30467
      Trace("fmc-exh-debug") << ", index = " << ev_index << " / " << d_quant_models[f].d_value.size();
879
60934
      Node ev = ev_index==-1 ? Node::null() : d_quant_models[f].d_value[ev_index];
880
30467
      if (ev!=d_true) {
881
27737
        Trace("fmc-exh-debug") << ", add!";
882
        //add as instantiation
883
55474
        if (ie->addInstantiation(f,
884
                                 inst,
885
                                 InferenceId::QUANTIFIERS_INST_FMF_FMC_EXH,
886
55474
                                 Node::null(),
887
                                 true))
888
        {
889
5002
          Trace("fmc-exh-debug")  << " ...success.";
890
5002
          addedLemmas++;
891
5002
          if (d_qstate.isInConflict() || options::fmfOneInstPerRound())
892
          {
893
            break;
894
          }
895
        }else{
896
22735
          Trace("fmc-exh-debug") << ", failed.";
897
        }
898
      }else{
899
2730
        Trace("fmc-exh-debug") << ", already true";
900
      }
901
30467
      Trace("fmc-exh-debug") << std::endl;
902
30467
      int index = riter.increment();
903
30467
      Trace("fmc-exh-debug") << "Incremented index " << index << std::endl;
904
30467
      if( !riter.isFinished() ){
905
78657
        if (index >= 0 && riter.d_index[index] > 0 && addedLemmas > 0
906
32772
            && riter.d_enum_type[index] == ENUM_CUSTOM)
907
        {
908
2794
          Trace("fmc-exh-debug")
909
1397
              << "Since this is a custom enumeration, skip to the next..."
910
1397
              << std::endl;
911
1397
          riter.incrementAtIndex(index - 1);
912
        }
913
      }
914
    }
915
5213
    d_addedLemmas += addedLemmas;
916
5213
    Trace("fmc-exh") << "----Finished Exhaustive instantiate, lemmas = " << addedLemmas << ", incomplete=" << riter.isIncomplete() << std::endl;
917
5213
    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
115260
void FullModelChecker::doCheck(FirstOrderModelFmc * fm, Node f, Def & d, Node n ) {
925
115260
  Trace("fmc-debug") << "Check " << n << " " << n.getKind() << std::endl;
926
  //first check if it is a bounding literal
927
115260
  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
112448
  }else if( n.getKind() == kind::BOUND_VARIABLE ){
931
25951
    Trace("fmc-debug") << "Add default entry..." << std::endl;
932
25951
    d.addEntry(fm, mkCondDefault(fm, f), n);
933
  }
934
86497
  else if( n.getKind() == kind::NOT ){
935
    //just do directly
936
8948
    doCheck( fm, f, d, n[0] );
937
8948
    doNegate( d );
938
  }
939
77549
  else if( n.getKind() == kind::FORALL ){
940
1998
    d.addEntry(fm, mkCondDefault(fm, f), Node::null());
941
  }
942
75551
  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
452
    d.reset();
948
452
    d.addEntry(fm, mkCondDefault(fm, f), Node::null());
949
  }
950
75099
  else if( n.getNumChildren()==0 ){
951
40164
    Node r = n;
952
20082
    if( !n.isConst() ){
953
17156
      TypeNode tn = n.getType();
954
8578
      if( !fm->hasTerm(n) && tn.isFirstClass() ){
955
185
        r = getSomeDomainElement(fm, tn );
956
      }
957
8578
      r = fm->getRepresentative( r );
958
    }
959
20082
    Trace("fmc-debug") << "Add constant entry..." << std::endl;
960
20082
    d.addEntry(fm, mkCondDefault(fm, f), r);
961
  }
962
  else{
963
110034
    std::vector< int > var_ch;
964
110034
    std::vector< Def > children;
965
154709
    for( int i=0; i<(int)n.getNumChildren(); i++) {
966
199384
      Def dc;
967
99692
      doCheck(fm, f, dc, n[i]);
968
99692
      children.push_back(dc);
969
99692
      if( n[i].getKind() == kind::BOUND_VARIABLE ){
970
25947
        var_ch.push_back(i);
971
      }
972
    }
973
974
55017
    if( n.getKind()==APPLY_UF ){
975
24140
      Trace("fmc-debug") << "Do uninterpreted compose " << n << std::endl;
976
      //uninterpreted compose
977
24140
      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
30877
      if( !var_ch.empty() ){
990
4193
        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
3405
          Trace("fmc-warn") << "Don't know how to check " << n << std::endl;
1000
3405
          d.addEntry(fm, mkCondDefault(fm, f), Node::null());
1001
        }
1002
      }else{
1003
26684
        Trace("fmc-debug") << "Do interpreted compose " << n << std::endl;
1004
53368
        std::vector< Node > cond;
1005
26684
        mkCondDefaultVec(fm, f, cond);
1006
53368
        std::vector< Node > val;
1007
        //interpreted compose
1008
26684
        doInterpretedCompose( fm, f, d, n, children, 0, cond, val );
1009
      }
1010
    }
1011
55017
    Trace("fmc-debug") << "Simplify the definition..." << std::endl;
1012
55017
    d.debugPrint("fmc-debug", Node::null(), this);
1013
55017
    d.simplify(this, fm);
1014
55017
    Trace("fmc-debug") << "Done simplifying" << std::endl;
1015
  }
1016
115260
  Trace("fmc-debug") << "Definition for " << n << " is : " << std::endl;
1017
115260
  d.debugPrint("fmc-debug", Node::null(), this);
1018
115260
  Trace("fmc-debug") << std::endl;
1019
115260
}
1020
1021
8948
void FullModelChecker::doNegate( Def & dc ) {
1022
20508
  for (unsigned i=0; i<dc.d_cond.size(); i++) {
1023
23120
    Node v = dc.d_value[i];
1024
11560
    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
8335
      dc.d_value[i] =
1031
16670
          v == d_true ? d_false : (v == d_false ? d_true : Node::null());
1032
    }
1033
  }
1034
8948
}
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
24140
void FullModelChecker::doUninterpretedCompose( FirstOrderModelFmc * fm, Node f, Def & d, Node op, std::vector< Def > & dc ) {
1092
24140
  Trace("fmc-uf-debug") << "Definition : " << std::endl;
1093
24140
  fm->d_models[op]->debugPrint("fmc-uf-debug", op, this);
1094
24140
  Trace("fmc-uf-debug") << std::endl;
1095
1096
48280
  std::vector< Node > cond;
1097
24140
  mkCondDefaultVec(fm, f, cond);
1098
48280
  std::vector< Node > val;
1099
24140
  doUninterpretedCompose( fm, f, d, *fm->d_models[op], dc, 0, cond, val);
1100
24140
}
1101
1102
66714
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
66714
  Trace("fmc-uf-process") << "process at " << index << std::endl;
1106
181673
  for( unsigned i=1; i<cond.size(); i++) {
1107
114959
    debugPrint("fmc-uf-process", cond[i], true);
1108
114959
    Trace("fmc-uf-process") << " ";
1109
  }
1110
66714
  Trace("fmc-uf-process") << std::endl;
1111
66714
  if (index==(int)dc.size()) {
1112
    //we have an entry, now do actual compose
1113
60312
    std::map< int, Node > entries;
1114
30156
    doUninterpretedCompose2( fm, f, entries, 0, cond, val, df.d_et);
1115
30156
    if( entries.empty() ){
1116
1061
      d.addEntry(fm, mkCond(cond), Node::null());
1117
    }else{
1118
      //add them to the definition
1119
96485
      for( unsigned e=0; e<df.d_cond.size(); e++ ){
1120
67390
        if ( entries.find(e)!=entries.end() ){
1121
48338
          Trace("fmf-uf-process-debug") << "Add entry..." << std::endl;
1122
48338
          d.addEntry(fm, entries[e], df.d_value[e] );
1123
48338
          Trace("fmf-uf-process-debug") << "Done add entry." << std::endl;
1124
        }
1125
      }
1126
    }
1127
  }else{
1128
85020
    for (unsigned i=0; i<dc[index].d_cond.size(); i++) {
1129
48462
      if (isCompat(fm, cond, dc[index].d_cond[i])!=0) {
1130
85148
        std::vector< Node > new_cond;
1131
42574
        new_cond.insert(new_cond.end(), cond.begin(), cond.end());
1132
42574
        if( doMeet(fm, new_cond, dc[index].d_cond[i]) ){
1133
42574
          Trace("fmc-uf-process") << "index " << i << " succeeded meet." << std::endl;
1134
42574
          val.push_back(dc[index].d_value[i]);
1135
42574
          doUninterpretedCompose(fm, f, d, df, dc, index+1, new_cond, val);
1136
42574
          val.pop_back();
1137
        }else{
1138
          Trace("fmc-uf-process") << "index " << i << " failed meet." << std::endl;
1139
        }
1140
      }
1141
    }
1142
  }
1143
66714
}
1144
1145
100625
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
100625
  Trace("fmc-uf-process") << "compose " << index << " / " << val.size() << std::endl;
1150
273828
  for( unsigned i=1; i<cond.size(); i++) {
1151
173203
    debugPrint("fmc-uf-process", cond[i], true);
1152
173203
    Trace("fmc-uf-process") << " ";
1153
  }
1154
100625
  Trace("fmc-uf-process") << std::endl;
1155
100625
  if (index==(int)val.size()) {
1156
96676
    Node c = mkCond(cond);
1157
48338
    Trace("fmc-uf-entry") << "Entry : " << c << " -> index[" << curr.d_data << "]" << std::endl;
1158
48338
    entries[curr.d_data] = c;
1159
  }else{
1160
104574
    Node v = val[index];
1161
52287
    Trace("fmc-uf-process") << "Process " << v << std::endl;
1162
52287
    bool bind_var = false;
1163
52287
    if( !v.isNull() && v.getKind()==kind::BOUND_VARIABLE ){
1164
24535
      int j = fm->getVariableId(f, v);
1165
24535
      Trace("fmc-uf-process") << v << " is variable #" << j << std::endl;
1166
24535
      if (!fm->isStar(cond[j + 1]))
1167
      {
1168
38
        v = cond[j+1];
1169
      }else{
1170
24497
        bind_var = true;
1171
      }
1172
    }
1173
52287
    if (bind_var) {
1174
24497
      Trace("fmc-uf-process") << "bind variable..." << std::endl;
1175
24497
      int j = fm->getVariableId(f, v);
1176
24497
      Assert(fm->isStar(cond[j + 1]));
1177
64442
      for (std::map<Node, EntryTrie>::iterator it = curr.d_child.begin();
1178
64442
           it != curr.d_child.end();
1179
           ++it)
1180
      {
1181
39945
        cond[j + 1] = it->first;
1182
39945
        doUninterpretedCompose2(
1183
39945
            fm, f, entries, index + 1, cond, val, it->second);
1184
      }
1185
24497
      cond[j + 1] = fm->getStar(v.getType());
1186
    }else{
1187
27790
      if( !v.isNull() ){
1188
26720
        if (curr.d_child.find(v) != curr.d_child.end())
1189
        {
1190
7892
          Trace("fmc-uf-process") << "follow value..." << std::endl;
1191
7892
          doUninterpretedCompose2(
1192
7892
              fm, f, entries, index + 1, cond, val, curr.d_child[v]);
1193
        }
1194
53440
        Node st = fm->getStar(v.getType());
1195
26720
        if (curr.d_child.find(st) != curr.d_child.end())
1196
        {
1197
22632
          Trace("fmc-uf-process") << "follow star..." << std::endl;
1198
22632
          doUninterpretedCompose2(
1199
22632
              fm, f, entries, index + 1, cond, val, curr.d_child[st]);
1200
        }
1201
      }
1202
    }
1203
  }
1204
100625
}
1205
1206
109731
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
109731
  Trace("fmc-if-process") << "int compose " << index << " / " << dc.size() << std::endl;
1210
264003
  for( unsigned i=1; i<cond.size(); i++) {
1211
154272
    debugPrint("fmc-if-process", cond[i], true);
1212
154272
    Trace("fmc-if-process") << " ";
1213
  }
1214
109731
  Trace("fmc-if-process") << std::endl;
1215
109731
  if ( index==(int)dc.size() ){
1216
92684
    Node c = mkCond(cond);
1217
92684
    Node v = evaluateInterpreted(n, val);
1218
46342
    d.addEntry(fm, c, v);
1219
  }
1220
  else {
1221
126778
    TypeNode vtn = n.getType();
1222
165504
    for (unsigned i=0; i<dc[index].d_cond.size(); i++) {
1223
102115
      if (isCompat(fm, cond, dc[index].d_cond[i])!=0) {
1224
181278
        std::vector< Node > new_cond;
1225
90639
        new_cond.insert(new_cond.end(), cond.begin(), cond.end());
1226
90639
        if( doMeet(fm, new_cond, dc[index].d_cond[i]) ){
1227
90639
          bool process = true;
1228
90639
          if (vtn.isBoolean()) {
1229
            //short circuit
1230
116533
            if( (n.getKind()==OR && dc[index].d_value[i]==d_true) ||
1231
54859
                (n.getKind()==AND && dc[index].d_value[i]==d_false) ){
1232
15184
              Node c = mkCond(new_cond);
1233
7592
              d.addEntry(fm, c, dc[index].d_value[i]);
1234
7592
              process = false;
1235
            }
1236
          }
1237
90639
          if (process) {
1238
83047
            val.push_back(dc[index].d_value[i]);
1239
83047
            doInterpretedCompose(fm, f, d, n, dc, index+1, new_cond, val);
1240
83047
            val.pop_back();
1241
          }
1242
        }
1243
      }
1244
    }
1245
  }
1246
109731
}
1247
1248
150577
int FullModelChecker::isCompat( FirstOrderModelFmc * fm, std::vector< Node > & cond, Node c ) {
1249
150577
  Trace("fmc-debug3") << "isCompat " << c << std::endl;
1250
150577
  Assert(cond.size() == c.getNumChildren() + 1);
1251
383874
  for (unsigned i=1; i<cond.size(); i++) {
1252
250661
    if (cond[i] != c[i - 1] && !fm->isStar(cond[i]) && !fm->isStar(c[i - 1]))
1253
    {
1254
17364
      return 0;
1255
    }
1256
  }
1257
133213
  return 1;
1258
}
1259
1260
133213
bool FullModelChecker::doMeet( FirstOrderModelFmc * fm, std::vector< Node > & cond, Node c ) {
1261
133213
  Trace("fmc-debug3") << "doMeet " << c << std::endl;
1262
133213
  Assert(cond.size() == c.getNumChildren() + 1);
1263
351178
  for (unsigned i=1; i<cond.size(); i++) {
1264
217965
    if( cond[i]!=c[i-1] ) {
1265
64858
      if (fm->isStar(cond[i]))
1266
      {
1267
36659
        cond[i] = c[i - 1];
1268
      }
1269
28199
      else if (!fm->isStar(c[i - 1]))
1270
      {
1271
        return false;
1272
      }
1273
    }
1274
  }
1275
133213
  return true;
1276
}
1277
1278
163376
Node FullModelChecker::mkCond( std::vector< Node > & cond ) {
1279
163376
  return NodeManager::currentNM()->mkNode(APPLY_UF, cond);
1280
}
1281
1282
57168
Node FullModelChecker::mkCondDefault( FirstOrderModelFmc * fm, Node f) {
1283
114336
  std::vector< Node > cond;
1284
57168
  mkCondDefaultVec(fm, f, cond);
1285
114336
  return mkCond(cond);
1286
}
1287
1288
108294
void FullModelChecker::mkCondDefaultVec( FirstOrderModelFmc * fm, Node f, std::vector< Node > & cond ) {
1289
108294
  Trace("fmc-debug") << "Make default vec" << std::endl;
1290
  //get function symbol for f
1291
108294
  cond.push_back(d_quant_cond[f]);
1292
271482
  for (unsigned i=0; i<f[0].getNumChildren(); i++) {
1293
326376
    Node ts = fm->getStar(f[0][i].getType());
1294
163188
    Assert(ts.getType() == f[0][i].getType());
1295
163188
    cond.push_back(ts);
1296
  }
1297
108294
}
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
46342
Node FullModelChecker::evaluateInterpreted( Node n, std::vector< Node > & vals ) {
1307
46342
  if( n.getKind()==EQUAL && !n[0].getType().isBoolean() ){
1308
11481
    if (!vals[0].isNull() && !vals[1].isNull()) {
1309
9708
      return vals[0]==vals[1] ? d_true : d_false;
1310
    }else{
1311
1773
      return Node::null();
1312
    }
1313
34861
  }else if( n.getKind()==ITE ){
1314
3835
    if( vals[0]==d_true ){
1315
1702
      return vals[1];
1316
2133
    }else if( vals[0]==d_false ){
1317
2049
      return vals[2];
1318
    }else{
1319
84
      return vals[1]==vals[2] ? vals[1] : Node::null();
1320
    }
1321
31026
  }else if( n.getKind()==AND || n.getKind()==OR ){
1322
5836
    bool isNull = false;
1323
22466
    for (unsigned i=0; i<vals.size(); i++) {
1324
16630
      if((vals[i]==d_true && n.getKind()==OR) || (vals[i]==d_false && n.getKind()==AND)) {
1325
        return vals[i];
1326
16630
      }else if( vals[i].isNull() ){
1327
4122
        isNull = true;
1328
      }
1329
    }
1330
5836
    return isNull ? Node::null() : vals[0];
1331
  }else{
1332
50380
    std::vector<Node> children;
1333
25190
    if( n.getMetaKind() == kind::metakind::PARAMETERIZED ){
1334
7925
      children.push_back( n.getOperator() );
1335
    }
1336
64001
    for (unsigned i=0; i<vals.size(); i++) {
1337
42308
      if( vals[i].isNull() ){
1338
3497
        return Node::null();
1339
      }else{
1340
38811
        children.push_back( vals[i] );
1341
      }
1342
    }
1343
43386
    Node nc = NodeManager::currentNM()->mkNode(n.getKind(), children);
1344
21693
    Trace("fmc-eval") << "Evaluate " << nc << " to ";
1345
21693
    nc = Rewriter::rewrite(nc);
1346
21693
    Trace("fmc-eval") << nc << std::endl;
1347
21693
    return nc;
1348
  }
1349
}
1350
1351
3527
Node FullModelChecker::getSomeDomainElement( FirstOrderModelFmc * fm, TypeNode tn ) {
1352
3527
  bool addRepId = !fm->getRepSet()->hasType(tn);
1353
3527
  Node de = fm->getSomeDomainElement(tn);
1354
3527
  if( addRepId ){
1355
116
    d_rep_ids[tn][de] = 0;
1356
  }
1357
3527
  return de;
1358
}
1359
1360
7221
Node FullModelChecker::getFunctionValue(FirstOrderModelFmc * fm, Node op, const char* argPrefix ) {
1361
7221
  return fm->getFunctionValue(op, argPrefix);
1362
}
1363
1364
1365
38769
bool FullModelChecker::useSimpleModels() {
1366
38769
  return options::fmfFmcSimple();
1367
}
1368
20770
void FullModelChecker::registerQuantifiedFormula(Node q)
1369
{
1370
20770
  if (d_quant_cond.find(q) != d_quant_cond.end())
1371
  {
1372
19270
    return;
1373
  }
1374
1500
  NodeManager* nm = NodeManager::currentNM();
1375
1500
  SkolemManager* sm = nm->getSkolemManager();
1376
3000
  std::vector<TypeNode> types;
1377
3705
  for (const Node& v : q[0])
1378
  {
1379
4410
    TypeNode tn = v.getType();
1380
2205
    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
2205
    types.push_back(tn);
1388
  }
1389
3000
  TypeNode typ = nm->mkFunctionType(types, nm->booleanType());
1390
3000
  Node op = sm->mkDummySkolem("qfmc", typ, "op for full-model checking");
1391
1500
  d_quant_cond[q] = op;
1392
}
1393
1394
20770
bool FullModelChecker::isHandled(Node q) const
1395
{
1396
20770
  return d_unhandledQuant.find(q) == d_unhandledQuant.end();
1397
}
1398
1399
}  // namespace fmcheck
1400
}  // namespace quantifiers
1401
}  // namespace theory
1402
29505
}  // namespace cvc5