GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/fmf/bounded_integers.cpp Lines: 492 526 93.5 %
Date: 2021-09-29 Branches: 1228 2704 45.4 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Andres Noetzli, Mathias Preiner
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
 * Bounded integers module
14
 *
15
 * This class manages integer bounds for quantifiers.
16
 */
17
18
#include "theory/quantifiers/fmf/bounded_integers.h"
19
20
#include "expr/dtype_cons.h"
21
#include "expr/node_algorithm.h"
22
#include "expr/skolem_manager.h"
23
#include "options/quantifiers_options.h"
24
#include "theory/arith/arith_msum.h"
25
#include "theory/datatypes/theory_datatypes_utils.h"
26
#include "theory/decision_manager.h"
27
#include "theory/quantifiers/first_order_model.h"
28
#include "theory/quantifiers/fmf/model_engine.h"
29
#include "theory/quantifiers/term_enumeration.h"
30
#include "theory/quantifiers/term_util.h"
31
#include "theory/rewriter.h"
32
#include "util/rational.h"
33
34
using namespace cvc5;
35
using namespace std;
36
using namespace cvc5::theory;
37
using namespace cvc5::theory::quantifiers;
38
using namespace cvc5::kind;
39
40
431
BoundedIntegers::IntRangeDecisionHeuristic::IntRangeDecisionHeuristic(
41
431
    Env& env, Node r, Valuation valuation, bool isProxy)
42
    : DecisionStrategyFmf(env, valuation),
43
      d_range(r),
44
431
      d_ranges_proxied(userContext())
45
{
46
431
  if( options::fmfBoundLazy() ){
47
1
    SkolemManager* sm = NodeManager::currentNM()->getSkolemManager();
48
1
    d_proxy_range = isProxy ? r : sm->mkDummySkolem("pbir", r.getType());
49
  }else{
50
430
    d_proxy_range = r;
51
  }
52
431
  if( !isProxy ){
53
378
    Trace("bound-int") << "Introduce proxy " << d_proxy_range << " for " << d_range << std::endl;
54
  }
55
431
}
56
1396
Node BoundedIntegers::IntRangeDecisionHeuristic::mkLiteral(unsigned n)
57
{
58
1396
  NodeManager* nm = NodeManager::currentNM();
59
2792
  Node cn = nm->mkConst(Rational(n == 0 ? 0 : n - 1));
60
2792
  return nm->mkNode(n == 0 ? LT : LEQ, d_proxy_range, cn);
61
}
62
63
1611
Node BoundedIntegers::IntRangeDecisionHeuristic::proxyCurrentRangeLemma()
64
{
65
1611
  if (d_range == d_proxy_range)
66
  {
67
1610
    return Node::null();
68
  }
69
1
  unsigned curr = 0;
70
1
  if (!getAssertedLiteralIndex(curr))
71
  {
72
    return Node::null();
73
  }
74
1
  if (d_ranges_proxied.find(curr) != d_ranges_proxied.end())
75
  {
76
    return Node::null();
77
  }
78
1
  d_ranges_proxied[curr] = true;
79
1
  NodeManager* nm = NodeManager::currentNM();
80
2
  Node currLit = getLiteral(curr);
81
  Node lem =
82
      nm->mkNode(EQUAL,
83
                 currLit,
84
2
                 nm->mkNode(curr == 0 ? LT : LEQ,
85
                            d_range,
86
4
                            nm->mkConst(Rational(curr == 0 ? 0 : curr - 1))));
87
1
  return lem;
88
}
89
90
723
BoundedIntegers::BoundedIntegers(Env& env,
91
                                 QuantifiersState& qs,
92
                                 QuantifiersInferenceManager& qim,
93
                                 QuantifiersRegistry& qr,
94
723
                                 TermRegistry& tr)
95
723
    : QuantifiersModule(env, qs, qim, qr, tr)
96
{
97
723
}
98
99
1446
BoundedIntegers::~BoundedIntegers() {}
100
101
806
void BoundedIntegers::presolve() {
102
806
  d_bnd_it.clear();
103
806
}
104
105
8108
bool BoundedIntegers::hasNonBoundVar( Node f, Node b, std::map< Node, bool >& visited ) {
106
8108
  if( visited.find( b )==visited.end() ){
107
7527
    visited[b] = true;
108
7527
    if( b.getKind()==BOUND_VARIABLE ){
109
813
      if( !isBound( f, b ) ){
110
422
        return true;
111
      }
112
    }else{
113
11973
      for( unsigned i=0; i<b.getNumChildren(); i++ ){
114
5820
        if( hasNonBoundVar( f, b[i], visited ) ){
115
561
          return true;
116
        }
117
      }
118
    }
119
  }
120
7125
  return false;
121
}
122
2288
bool BoundedIntegers::hasNonBoundVar( Node f, Node b ) {
123
4576
  std::map< Node, bool > visited;
124
4576
  return hasNonBoundVar( f, b, visited );
125
}
126
127
582
bool BoundedIntegers::processEqDisjunct( Node q, Node n, Node& v, std::vector< Node >& v_cases ) {
128
582
  if( n.getKind()==EQUAL ){
129
1664
    for( unsigned i=0; i<2; i++ ){
130
2218
      Node t = n[i];
131
1136
      if( !hasNonBoundVar( q, n[1-i] ) ){
132
817
        if( t==v ){
133
24
          v_cases.push_back( n[1-i] );
134
24
          return true;
135
793
        }else if( v.isNull() && t.getKind()==BOUND_VARIABLE ){
136
30
          v = t;
137
30
          v_cases.push_back( n[1-i] );
138
30
          return true;
139
        }
140
      }
141
    }
142
  }
143
528
  return false;
144
}
145
146
84
void BoundedIntegers::processMatchBoundVars( Node q, Node n, std::vector< Node >& bvs, std::map< Node, bool >& visited ){
147
84
  if( visited.find( n )==visited.end() ){
148
84
    visited[n] = true;
149
84
    if( n.getKind()==BOUND_VARIABLE && !isBound( q, n ) ){
150
29
      bvs.push_back( n );
151
    //injective operators
152
55
    }else if( n.getKind()==kind::APPLY_CONSTRUCTOR ){
153
60
      for( unsigned i=0; i<n.getNumChildren(); i++ ){
154
36
        processMatchBoundVars( q, n[i], bvs, visited );
155
      }
156
    }
157
  }
158
84
}
159
160
6733
void BoundedIntegers::process( Node q, Node n, bool pol,
161
                               std::map< Node, unsigned >& bound_lit_type_map,
162
                               std::map< int, std::map< Node, Node > >& bound_lit_map,
163
                               std::map< int, std::map< Node, bool > >& bound_lit_pol_map,
164
                               std::map< int, std::map< Node, Node > >& bound_int_range_term,
165
                               std::map< Node, std::vector< Node > >& bound_fixed_set ){
166
6733
  if( n.getKind()==OR || n.getKind()==AND ){
167
1271
    if( (n.getKind()==OR)==pol ){
168
4627
      for( unsigned i=0; i<n.getNumChildren(); i++) {
169
3628
        process( q, n[i], pol, bound_lit_type_map, bound_lit_map, bound_lit_pol_map, bound_int_range_term, bound_fixed_set );
170
      }
171
    }else{
172
      //if we are ( x != t1 ^ ...^ x != tn ), then x can be bound to { t1...tn }
173
544
      Node conj = n;
174
272
      if( !pol ){
175
        conj = TermUtil::simpleNegate( conj );
176
      }
177
272
      Trace("bound-int-debug") << "Process possible finite disequality conjunction : " << conj << std::endl;
178
272
      Assert(conj.getKind() == AND);
179
544
      Node v;
180
544
      std::vector< Node > v_cases;
181
272
      bool success = true;
182
322
      for( unsigned i=0; i<conj.getNumChildren(); i++ ){
183
298
        if( conj[i].getKind()==NOT && processEqDisjunct( q, conj[i][0], v, v_cases ) ){
184
          //continue
185
        }else{
186
248
          Trace("bound-int-debug") << "...failed due to " << conj[i] << std::endl;
187
248
          success = false;
188
248
          break;
189
        }
190
      }
191
272
      if( success && !isBound( q, v ) ){
192
10
        Trace("bound-int-debug") << "Success with variable " << v << std::endl;
193
10
        bound_lit_type_map[v] = BOUND_FIXED_SET;
194
10
        bound_lit_map[3][v] = n;
195
10
        bound_lit_pol_map[3][v] = pol;
196
10
        bound_fixed_set[v].clear();
197
10
        bound_fixed_set[v].insert( bound_fixed_set[v].end(), v_cases.begin(), v_cases.end() );
198
      }
199
    }
200
5462
  }else if( n.getKind()==EQUAL ){
201
829
    if( !pol ){
202
      // non-applied DER on x != t, x can be bound to { t }
203
964
      Node v;
204
964
      std::vector< Node > v_cases;
205
482
      if( processEqDisjunct( q, n, v, v_cases ) ){
206
4
        if( !isBound( q, v ) ){
207
2
          bound_lit_type_map[v] = BOUND_FIXED_SET;
208
2
          bound_lit_map[3][v] = n;
209
2
          bound_lit_pol_map[3][v] = pol;
210
2
          Assert(v_cases.size() == 1);
211
2
          bound_fixed_set[v].clear();
212
2
          bound_fixed_set[v].push_back( v_cases[0] );
213
        }
214
      }
215
    }
216
4633
  }else if( n.getKind()==NOT ){
217
1997
    process( q, n[0], !pol, bound_lit_type_map, bound_lit_map, bound_lit_pol_map, bound_int_range_term, bound_fixed_set );
218
2636
  }else if( n.getKind()==GEQ ){
219
2193
    if( n[0].getType().isInteger() ){
220
4386
      std::map< Node, Node > msum;
221
2193
      if (ArithMSum::getMonomialSumLit(n, msum))
222
      {
223
2193
        Trace("bound-int-debug") << "literal (polarity = " << pol << ") " << n << " is monomial sum : " << std::endl;
224
2193
        ArithMSum::debugPrintMonomialSum(msum, "bound-int-debug");
225
6769
        for( std::map< Node, Node >::iterator it = msum.begin(); it != msum.end(); ++it ){
226
4576
          if ( !it->first.isNull() && it->first.getKind()==BOUND_VARIABLE && !isBound( q, it->first ) ){
227
            //if not bound in another way
228
1106
            if( bound_lit_type_map.find( it->first )==bound_lit_type_map.end() || bound_lit_type_map[it->first] == BOUND_INT_RANGE ){
229
2206
              Node veq;
230
1103
              if (ArithMSum::isolate(it->first, msum, veq, GEQ) != 0)
231
              {
232
2206
                Node n1 = veq[0];
233
2206
                Node n2 = veq[1];
234
1103
                if(pol){
235
                  //flip
236
525
                  n1 = veq[1];
237
525
                  n2 = veq[0];
238
525
                  if( n1.getKind()==BOUND_VARIABLE ){
239
2
                    n2 = ArithMSum::offset(n2, 1);
240
                  }else{
241
523
                    n1 = ArithMSum::offset(n1, -1);
242
                  }
243
525
                  veq = NodeManager::currentNM()->mkNode( GEQ, n1, n2 );
244
                }
245
1103
                Trace("bound-int-debug") << "Isolated for " << it->first << " : (" << n1 << " >= " << n2 << ")" << std::endl;
246
2206
                Node t = n1==it->first ? n2 : n1;
247
1103
                if( !hasNonBoundVar( q, t ) ) {
248
1001
                  Trace("bound-int-debug") << "The bound is relevant." << std::endl;
249
1001
                  int loru = n1==it->first ? 0 : 1;
250
1001
                  bound_lit_type_map[it->first] = BOUND_INT_RANGE;
251
1001
                  bound_int_range_term[loru][it->first] = t;
252
1001
                  bound_lit_map[loru][it->first] = n;
253
1001
                  bound_lit_pol_map[loru][it->first] = pol;
254
                }else{
255
102
                  Trace("bound-int-debug") << "The term " << t << " has non-bound variable." << std::endl;
256
                }
257
              }
258
            }
259
          }
260
        }
261
      }
262
    }
263
443
  }else if( n.getKind()==MEMBER ){
264
49
    if( !pol && !hasNonBoundVar( q, n[1] ) ){
265
96
      std::vector< Node > bound_vars;
266
96
      std::map< Node, bool > visited;
267
48
      processMatchBoundVars( q, n[0], bound_vars, visited );
268
77
      for( unsigned i=0; i<bound_vars.size(); i++ ){
269
58
        Node v = bound_vars[i];
270
29
        Trace("bound-int-debug") << "literal (polarity = " << pol << ") " << n << " is membership." << std::endl;
271
29
        bound_lit_type_map[v] = BOUND_SET_MEMBER;
272
29
        bound_lit_map[2][v] = n;
273
29
        bound_lit_pol_map[2][v] = pol;
274
      }
275
    }
276
  }else{
277
394
    Assert(n.getKind() != LEQ && n.getKind() != LT && n.getKind() != GT);
278
  }
279
6733
}
280
281
10701
bool BoundedIntegers::needsCheck( Theory::Effort e ) {
282
10701
  return e==Theory::EFFORT_LAST_CALL;
283
}
284
285
2916
void BoundedIntegers::check(Theory::Effort e, QEffort quant_e)
286
{
287
2916
  if (quant_e != QEFFORT_STANDARD)
288
  {
289
1958
    return;
290
  }
291
958
  Trace("bint-engine") << "---Bounded Integers---" << std::endl;
292
958
  bool addedLemma = false;
293
  // make sure proxies are up-to-date with range
294
2569
  for (const Node& r : d_ranges)
295
  {
296
3222
    Node prangeLem = d_rms[r]->proxyCurrentRangeLemma();
297
1611
    if (!prangeLem.isNull())
298
    {
299
2
      Trace("bound-int-lemma")
300
1
          << "*** bound int : proxy lemma : " << prangeLem << std::endl;
301
1
      d_qim.addPendingLemma(prangeLem, InferenceId::QUANTIFIERS_BINT_PROXY);
302
1
      addedLemma = true;
303
    }
304
  }
305
958
  Trace("bint-engine") << "   addedLemma = " << addedLemma << std::endl;
306
}
307
606
void BoundedIntegers::setBoundedVar(Node q, Node v, BoundVarType bound_type)
308
{
309
606
  d_bound_type[q][v] = bound_type;
310
606
  d_set_nums[q][v] = d_set[q].size();
311
606
  d_set[q].push_back( v );
312
1212
  Trace("bound-int-var") << "Bound variable #" << d_set_nums[q][v] << " : " << v
313
606
                         << std::endl;
314
606
}
315
316
584
void BoundedIntegers::checkOwnership(Node f)
317
{
318
  //this needs to be done at preregister since it affects e.g. QuantDSplit's preregister
319
584
  Trace("bound-int") << "check ownership quantifier " << f << std::endl;
320
321
  // determine if we should look at the quantified formula at all
322
584
  if (!options::fmfBound())
323
  {
324
    // only applying it to internal quantifiers
325
444
    QuantAttributes& qattr = d_qreg.getQuantAttributes();
326
444
    if (!qattr.isInternal(f))
327
    {
328
69
      Trace("bound-int") << "...not internal, skip" << std::endl;
329
69
      return;
330
    }
331
  }
332
333
515
  NodeManager* nm = NodeManager::currentNM();
334
515
  SkolemManager* sm = nm->getSkolemManager();
335
336
  bool success;
337
1108
  do{
338
2216
    std::map< Node, unsigned > bound_lit_type_map;
339
2216
    std::map< int, std::map< Node, Node > > bound_lit_map;
340
2216
    std::map< int, std::map< Node, bool > > bound_lit_pol_map;
341
2216
    std::map< int, std::map< Node, Node > > bound_int_range_term;
342
2216
    std::map< Node, std::vector< Node > > bound_fixed_set;
343
1108
    success = false;
344
1108
    process( f, f[1], true, bound_lit_type_map, bound_lit_map, bound_lit_pol_map, bound_int_range_term, bound_fixed_set );
345
    //for( std::map< Node, Node >::iterator it = d_bounds[0][f].begin(); it != d_bounds[0][f].end(); ++it ){
346
1659
    for( std::map< Node, unsigned >::iterator it = bound_lit_type_map.begin(); it != bound_lit_type_map.end(); ++it ){
347
1102
      Node v = it->first;
348
551
      if( !isBound( f, v ) ){
349
551
        bool setBoundVar = false;
350
551
        if( it->second==BOUND_INT_RANGE ){
351
          //must have both
352
518
          std::map<Node, Node>& blm0 = bound_lit_map[0];
353
518
          std::map<Node, Node>& blm1 = bound_lit_map[1];
354
518
          if (blm0.find(v) != blm0.end() && blm1.find(v) != blm1.end())
355
          {
356
465
            setBoundedVar( f, v, BOUND_INT_RANGE );
357
465
            setBoundVar = true;
358
1395
            for( unsigned b=0; b<2; b++ ){
359
              //set the bounds
360
930
              Assert(bound_int_range_term[b].find(v)
361
                     != bound_int_range_term[b].end());
362
930
              d_bounds[b][f][v] = bound_int_range_term[b][v];
363
            }
364
930
            Node r = nm->mkNode(MINUS, d_bounds[1][f][v], d_bounds[0][f][v]);
365
465
            d_range[f][v] = Rewriter::rewrite(r);
366
465
            Trace("bound-int") << "Variable " << v << " is bound because of int range literals " << bound_lit_map[0][v] << " and " << bound_lit_map[1][v] << std::endl;
367
          }
368
33
        }else if( it->second==BOUND_SET_MEMBER ){
369
          // only handles infinite element types currently (cardinality is not
370
          // supported for finite element types #1123). Regardless, this is
371
          // typically not a limitation since this variable can be bound in a
372
          // standard way below since its type is finite.
373
21
          if (!d_qstate.isFiniteType(v.getType()))
374
          {
375
21
            setBoundedVar(f, v, BOUND_SET_MEMBER);
376
21
            setBoundVar = true;
377
21
            d_setm_range[f][v] = bound_lit_map[2][v][1];
378
21
            d_setm_range_lit[f][v] = bound_lit_map[2][v];
379
21
            d_range[f][v] = nm->mkNode(CARD, d_setm_range[f][v]);
380
42
            Trace("bound-int") << "Variable " << v
381
21
                               << " is bound because of set membership literal "
382
21
                               << bound_lit_map[2][v] << std::endl;
383
          }
384
12
        }else if( it->second==BOUND_FIXED_SET ){
385
12
          setBoundedVar(f, v, BOUND_FIXED_SET);
386
12
          setBoundVar = true;
387
34
          for (unsigned i = 0; i < bound_fixed_set[v].size(); i++)
388
          {
389
44
            Node t = bound_fixed_set[v][i];
390
22
            if (expr::hasBoundVar(t))
391
            {
392
6
              d_fixed_set_ngr_range[f][v].push_back(t);
393
            }
394
            else
395
            {
396
16
              d_fixed_set_gr_range[f][v].push_back(t);
397
            }
398
          }
399
24
          Trace("bound-int") << "Variable " << v
400
12
                             << " is bound because of disequality conjunction "
401
12
                             << bound_lit_map[3][v] << std::endl;
402
        }
403
551
        if( setBoundVar ){
404
498
          success = true;
405
          //set Attributes on literals
406
1494
          for( unsigned b=0; b<2; b++ ){
407
996
            std::map<Node, Node>& blm = bound_lit_map[b];
408
996
            if (blm.find(v) != blm.end())
409
            {
410
930
              std::map<Node, bool>& blmp = bound_lit_pol_map[b];
411
              // WARNING_CANDIDATE:
412
              // This assertion may fail. We intentionally do not enable this in
413
              // production as it is considered safe for this to fail. We fail
414
              // the assertion in debug mode to have this instance raised to
415
              // our attention.
416
930
              Assert(blmp.find(v) != blmp.end());
417
              BoundIntLitAttribute bila;
418
930
              bound_lit_map[b][v].setAttribute(bila, blmp[v] ? 1 : 0);
419
            }
420
            else
421
            {
422
66
              Assert(it->second != BOUND_INT_RANGE);
423
            }
424
          }
425
        }
426
      }
427
    }
428
1108
    if( !success ){
429
      //resort to setting a finite bound on a variable
430
1261
      for( unsigned i=0; i<f[0].getNumChildren(); i++) {
431
746
        if( d_bound_type[f].find( f[0][i] )==d_bound_type[f].end() ){
432
126
          TypeNode tn = f[0][i].getType();
433
275
          if ((tn.isSort() && d_qstate.isFiniteType(tn))
434
316
              || d_qreg.getQuantifiersBoundInference().mayComplete(tn))
435
          {
436
108
            success = true;
437
108
            setBoundedVar( f, f[0][i], BOUND_FINITE );
438
108
            break;
439
          }
440
        }
441
      }
442
    }
443
  }while( success );
444
445
515
  if( Trace.isOn("bound-int") ){
446
    Trace("bound-int") << "Bounds are : " << std::endl;
447
    for( unsigned i=0; i<f[0].getNumChildren(); i++) {
448
      Node v = f[0][i];
449
      if( std::find( d_set[f].begin(), d_set[f].end(), v )!=d_set[f].end() ){
450
        Assert(d_bound_type[f].find(v) != d_bound_type[f].end());
451
        if( d_bound_type[f][v]==BOUND_INT_RANGE ){
452
          Trace("bound-int") << "  " << d_bounds[0][f][v] << " <= " << v << " <= " << d_bounds[1][f][v] << " (range is " << d_range[f][v] << ")" << std::endl;
453
        }else if( d_bound_type[f][v]==BOUND_SET_MEMBER ){
454
          if( d_setm_range_lit[f][v][0]==v ){
455
            Trace("bound-int") << "  " << v << " in " << d_setm_range[f][v] << std::endl;
456
          }else{
457
            Trace("bound-int") << "  " << v << " unifiable in " << d_setm_range_lit[f][v] << std::endl;
458
          }
459
        }else if( d_bound_type[f][v]==BOUND_FIXED_SET ){
460
          Trace("bound-int") << "  " << v << " in { ";
461
          for (TNode fnr : d_fixed_set_ngr_range[f][v])
462
          {
463
            Trace("bound-int") << fnr << " ";
464
          }
465
          for (TNode fgr : d_fixed_set_gr_range[f][v])
466
          {
467
            Trace("bound-int") << fgr << " ";
468
          }
469
          Trace("bound-int") << "}" << std::endl;
470
        }else if( d_bound_type[f][v]==BOUND_FINITE ){
471
          Trace("bound-int") << "  " << v << " has small finite type." << std::endl;
472
        }else{
473
          Trace("bound-int") << "  " << v << " has unknown bound." << std::endl;
474
          Assert(false);
475
        }
476
      }else{
477
        Trace("bound-int") << "  " << "*** " << v << " is unbounded." << std::endl;
478
      }
479
    }
480
  }
481
482
515
  bool bound_success = true;
483
1119
  for( unsigned i=0; i<f[0].getNumChildren(); i++) {
484
610
    if( d_bound_type[f].find( f[0][i] )==d_bound_type[f].end() ){
485
6
      Trace("bound-int-warn") << "Warning : Bounded Integers : Due to quantification on " << f[0][i] << ", could not find bounds for " << f << std::endl;
486
6
      bound_success = false;
487
6
      break;
488
    }
489
  }
490
491
515
  if( bound_success ){
492
509
    d_bound_quants.push_back( f );
493
509
    DecisionManager* dm = d_qim.getDecisionManager();
494
1113
    for( unsigned i=0; i<d_set[f].size(); i++) {
495
1208
      Node v = d_set[f][i];
496
604
      std::map< Node, Node >::iterator itr = d_range[f].find( v );
497
604
      if( itr != d_range[f].end() ){
498
968
        Node r = itr->second;
499
484
        Assert(!r.isNull());
500
484
        bool isProxy = false;
501
484
        if (expr::hasBoundVar(r))
502
        {
503
          // introduce a new bound
504
          Node new_range =
505
106
              sm->mkDummySkolem("bir", r.getType(), "bound for term");
506
53
          d_nground_range[f][v] = r;
507
53
          d_range[f][v] = new_range;
508
53
          r = new_range;
509
53
          isProxy = true;
510
        }
511
484
        if( !r.isConst() ){
512
457
          if (d_rms.find(r) == d_rms.end())
513
          {
514
431
            Trace("bound-int") << "For " << v << ", bounded Integer Module will try to minimize : " << r << std::endl;
515
431
            d_ranges.push_back( r );
516
1293
            d_rms[r].reset(new IntRangeDecisionHeuristic(
517
862
                d_env, r, d_qstate.getValuation(), isProxy));
518
431
            dm->registerStrategy(DecisionManager::STRAT_QUANT_BOUND_INT_SIZE,
519
431
                                 d_rms[r].get());
520
          }
521
        }
522
      }
523
    }
524
  }
525
}
526
527
4664
bool BoundedIntegers::isBound(Node q, Node v) const
528
{
529
4664
  std::map<Node, std::vector<Node> >::const_iterator its = d_set.find(q);
530
4664
  if (its == d_set.end())
531
  {
532
1867
    return false;
533
  }
534
5594
  return std::find(its->second.begin(), its->second.end(), v)
535
8391
         != its->second.end();
536
}
537
538
6531
BoundVarType BoundedIntegers::getBoundVarType(Node q, Node v) const
539
{
540
  std::map<Node, std::map<Node, BoundVarType> >::const_iterator itb =
541
6531
      d_bound_type.find(q);
542
6531
  if (itb == d_bound_type.end())
543
  {
544
    return BOUND_NONE;
545
  }
546
6531
  std::map<Node, BoundVarType>::const_iterator it = itb->second.find(v);
547
6531
  if (it == itb->second.end())
548
  {
549
14
    return BOUND_NONE;
550
  }
551
6517
  return it->second;
552
}
553
554
4114
void BoundedIntegers::getBoundVarIndices(Node q,
555
                                         std::vector<unsigned>& indices) const
556
{
557
4114
  std::map<Node, std::vector<Node> >::const_iterator it = d_set.find(q);
558
4114
  if (it != d_set.end())
559
  {
560
9384
    for (const Node& v : it->second)
561
    {
562
5328
      indices.push_back(TermUtil::getVariableNum(q, v));
563
    }
564
  }
565
4114
}
566
567
4422
void BoundedIntegers::getBounds( Node f, Node v, RepSetIterator * rsi, Node & l, Node & u ) {
568
4422
  l = d_bounds[0][f][v];
569
4422
  u = d_bounds[1][f][v];
570
4422
  if( d_nground_range[f].find(v)!=d_nground_range[f].end() ){
571
    //get the substitution
572
2712
    std::vector< Node > vars;
573
2712
    std::vector< Node > subs;
574
1356
    if( getRsiSubsitution( f, v, vars, subs, rsi ) ){
575
1290
      u = u.substitute( vars.begin(), vars.end(), subs.begin(), subs.end() );
576
1290
      l = l.substitute( vars.begin(), vars.end(), subs.begin(), subs.end() );
577
    }else{
578
66
      u = Node::null();
579
66
      l = Node::null();
580
    }
581
  }
582
4422
}
583
584
2244
void BoundedIntegers::getBoundValues( Node f, Node v, RepSetIterator * rsi, Node & l, Node & u ) {
585
2244
  getBounds( f, v, rsi, l, u );
586
2244
  Trace("bound-int-rsi") << "Get value in model for..." << l << " and " << u << std::endl;
587
2244
  if( !l.isNull() ){
588
2178
    l = d_treg.getModel()->getValue(l);
589
  }
590
2244
  if( !u.isNull() ){
591
2178
    u = d_treg.getModel()->getValue(u);
592
  }
593
2244
  Trace("bound-int-rsi") << "Value is " << l << " ... " << u << std::endl;
594
2244
  return;
595
}
596
597
791
bool BoundedIntegers::isGroundRange(Node q, Node v)
598
{
599
791
  if (isBound(q, v))
600
  {
601
791
    if (d_bound_type[q][v] == BOUND_INT_RANGE)
602
    {
603
1950
      return !expr::hasBoundVar(getLowerBound(q, v))
604
1950
             && !expr::hasBoundVar(getUpperBound(q, v));
605
    }
606
141
    else if (d_bound_type[q][v] == BOUND_SET_MEMBER)
607
    {
608
9
      return !expr::hasBoundVar(d_setm_range[q][v]);
609
    }
610
132
    else if (d_bound_type[q][v] == BOUND_FIXED_SET)
611
    {
612
132
      return !d_fixed_set_ngr_range[q][v].empty();
613
    }
614
  }
615
  return false;
616
}
617
618
54
Node BoundedIntegers::getSetRange( Node q, Node v, RepSetIterator * rsi ) {
619
54
  Node sr = d_setm_range[q][v];
620
54
  if( d_nground_range[q].find(v)!=d_nground_range[q].end() ){
621
4
    Trace("bound-int-rsi-debug")
622
2
        << sr << " is non-ground, apply substitution..." << std::endl;
623
    //get the substitution
624
4
    std::vector< Node > vars;
625
4
    std::vector< Node > subs;
626
2
    if( getRsiSubsitution( q, v, vars, subs, rsi ) ){
627
2
      Trace("bound-int-rsi-debug")
628
1
          << "  apply " << vars << " -> " << subs << std::endl;
629
1
      sr = sr.substitute( vars.begin(), vars.end(), subs.begin(), subs.end() );
630
    }else{
631
1
      sr = Node::null();
632
    }
633
  }
634
54
  return sr;
635
}
636
637
54
Node BoundedIntegers::getSetRangeValue( Node q, Node v, RepSetIterator * rsi ) {
638
108
  Node sr = getSetRange( q, v, rsi );
639
54
  if (sr.isNull())
640
  {
641
1
    return sr;
642
  }
643
53
  Trace("bound-int-rsi") << "Get value in model for..." << sr << std::endl;
644
53
  Assert(!expr::hasFreeVar(sr));
645
106
  Node sro = sr;
646
53
  sr = d_treg.getModel()->getValue(sr);
647
  // if non-constant, then sr does not occur in the model, we fail
648
53
  if (!sr.isConst())
649
  {
650
    return Node::null();
651
  }
652
53
  Trace("bound-int-rsi") << "Value is " << sr << std::endl;
653
53
  if (sr.getKind() == EMPTYSET)
654
  {
655
2
    return sr;
656
  }
657
51
  NodeManager* nm = NodeManager::currentNM();
658
102
  Node nsr;
659
102
  TypeNode tne = sr.getType().getSetElementType();
660
661
  // we can use choice functions for canonical symbolic instantiations
662
51
  unsigned srCard = 0;
663
95
  while (sr.getKind() == UNION)
664
  {
665
22
    srCard++;
666
22
    sr = sr[0];
667
  }
668
51
  Assert(sr.getKind() == SINGLETON);
669
51
  srCard++;
670
  // choices[i] stores the canonical symbolic representation of the (i+1)^th
671
  // element of sro
672
102
  std::vector<Node> choices;
673
102
  Node srCardN = nm->mkNode(CARD, sro);
674
102
  Node choice_i;
675
124
  for (unsigned i = 0; i < srCard; i++)
676
  {
677
73
    if (i == d_setm_choice[sro].size())
678
    {
679
17
      choice_i = nm->mkBoundVar(tne);
680
17
      choices.push_back(choice_i);
681
34
      Node cBody = nm->mkNode(MEMBER, choice_i, sro);
682
17
      if (choices.size() > 1)
683
      {
684
5
        cBody = nm->mkNode(AND, cBody, nm->mkNode(DISTINCT, choices));
685
      }
686
17
      choices.pop_back();
687
34
      Node bvl = nm->mkNode(BOUND_VAR_LIST, choice_i);
688
34
      Node cMinCard = nm->mkNode(LEQ, srCardN, nm->mkConst(Rational(i)));
689
17
      choice_i = nm->mkNode(WITNESS, bvl, nm->mkNode(OR, cMinCard, cBody));
690
17
      d_setm_choice[sro].push_back(choice_i);
691
    }
692
73
    Assert(i < d_setm_choice[sro].size());
693
73
    choice_i = d_setm_choice[sro][i];
694
73
    choices.push_back(choice_i);
695
146
    Node sChoiceI = nm->mkSingleton(choice_i.getType(), choice_i);
696
73
    if (nsr.isNull())
697
    {
698
51
      nsr = sChoiceI;
699
    }
700
    else
701
    {
702
22
      nsr = nm->mkNode(UNION, nsr, sChoiceI);
703
    }
704
  }
705
  // turns the concrete model value of sro into a canonical representation
706
  //   e.g.
707
  // singleton(0) union singleton(1)
708
  //   becomes
709
  // C1 union ( witness y. card(S)<=1 OR ( y in S AND distinct( y, C1 ) ) )
710
  // where C1 = ( witness x. card(S)<=0 OR x in S ).
711
51
  Trace("bound-int-rsi") << "...reconstructed " << nsr << std::endl;
712
51
  return nsr;
713
}
714
715
1410
bool BoundedIntegers::getRsiSubsitution( Node q, Node v, std::vector< Node >& vars, std::vector< Node >& subs, RepSetIterator * rsi ) {
716
717
1410
  Trace("bound-int-rsi") << "Get bound value in model of variable " << v << std::endl;
718
1410
  Assert(d_set_nums[q].find(v) != d_set_nums[q].end());
719
1410
  int vindex = d_set_nums[q][v];
720
1410
  Assert(d_set_nums[q][v] == vindex);
721
1410
  Trace("bound-int-rsi-debug") << "  index order is " << vindex << std::endl;
722
  //must take substitution for all variables that are iterating at higher level
723
3100
  for( int i=0; i<vindex; i++) {
724
1690
    Assert(d_set_nums[q][d_set[q][i]] == i);
725
1690
    Trace("bound-int-rsi") << "Look up the value for " << d_set[q][i] << " " << i << std::endl;
726
1690
    int vo = rsi->getVariableOrder(i);
727
1690
    Assert(q[0][vo] == d_set[q][i]);
728
3380
    TypeNode tn = d_set[q][i].getType();
729
    // If the type of tn is not closed enumerable, we must map the value back
730
    // to a term that appears in the same equivalence class as the constant.
731
    // Notice that this is to ensure that unhandled values (e.g. uninterpreted
732
    // constants, datatype values) do not enter instantiations/lemmas, which
733
    // can lead to refutation unsoundness. However, it is important that we
734
    // conversely do *not* map terms to values in other cases. In particular,
735
    // replacing a constant c with a term t can lead to solution unsoundness
736
    // if we are instantiating a quantified formula that corresponds to a
737
    // reduction for t, since then the reduction is using circular reasoning:
738
    // the current value of t is being used to reason about the range of
739
    // its axiomatization. This is limited to reductions in the theory of
740
    // strings, which use quantification on integers only. Note this
741
    // impacts only quantified formulas with 2+ dimensions and dependencies
742
    // between dimensions, e.g. str.indexof_re reduction.
743
3380
    Node t = rsi->getCurrentTerm(vo, !tn.isClosedEnumerable());
744
1690
    Trace("bound-int-rsi") << "term : " << t << std::endl;
745
1690
    vars.push_back( d_set[q][i] );
746
1690
    subs.push_back( t );
747
  }
748
749
  //check if it has been instantiated
750
1410
  if( !vars.empty() && !d_bnd_it[q][v].hasInstantiated(subs) ){
751
75
    if( d_bound_type[q][v]==BOUND_INT_RANGE || d_bound_type[q][v]==BOUND_SET_MEMBER ){
752
      //must add the lemma
753
134
      Node nn = d_nground_range[q][v];
754
67
      nn = nn.substitute( vars.begin(), vars.end(), subs.begin(), subs.end() );
755
134
      Node lem = NodeManager::currentNM()->mkNode( LEQ, nn, d_range[q][v] );
756
67
      Trace("bound-int-lemma") << "*** Add lemma to minimize instantiated non-ground term " << lem << std::endl;
757
67
      d_qim.lemma(lem, InferenceId::QUANTIFIERS_BINT_MIN_NG);
758
    }
759
75
    return false;
760
  }else{
761
1335
    return true;
762
  }
763
}
764
765
80
Node BoundedIntegers::matchBoundVar( Node v, Node t, Node e ){
766
80
  if( t==v ){
767
32
    return e;
768
48
  }else if( t.getKind()==kind::APPLY_CONSTRUCTOR ){
769
32
    if( e.getKind()==kind::APPLY_CONSTRUCTOR ){
770
      if( t.getOperator() != e.getOperator() ) {
771
        return Node::null();
772
      }
773
    }
774
32
    NodeManager* nm = NodeManager::currentNM();
775
32
    const DType& dt = datatypes::utils::datatypeOf(t.getOperator());
776
32
    unsigned index = datatypes::utils::indexOf(t.getOperator());
777
48
    for( unsigned i=0; i<t.getNumChildren(); i++ ){
778
64
      Node u;
779
48
      if( e.getKind()==kind::APPLY_CONSTRUCTOR ){
780
        u = matchBoundVar( v, t[i], e[i] );
781
      }else{
782
        Node se = nm->mkNode(APPLY_SELECTOR_TOTAL,
783
96
                             dt[index].getSelectorInternal(e.getType(), i),
784
192
                             e);
785
48
        u = matchBoundVar( v, t[i], se );
786
      }
787
48
      if( !u.isNull() ){
788
32
        return u;
789
      }
790
    }
791
  }
792
16
  return Node::null();
793
}
794
795
2655
bool BoundedIntegers::getBoundElements( RepSetIterator * rsi, bool initial, Node q, Node v, std::vector< Node >& elements ) {
796
2655
  if( initial || !isGroundRange( q, v ) ){
797
2388
    elements.clear();
798
2388
    BoundVarType bvt = getBoundVarType(q, v);
799
2388
    if( bvt==BOUND_INT_RANGE ){
800
4488
      Node l, u;
801
2244
      getBoundValues( q, v, rsi, l, u );
802
2244
      if( l.isNull() || u.isNull() ){
803
66
        Trace("bound-int-warn") << "WARNING: Could not find integer bounds in model for " << v << " in " << q << std::endl;
804
        //failed, abort the iterator
805
66
        return false;
806
      }else{
807
2178
        Trace("bound-int-rsi") << "Can limit bounds of " << v << " to " << l << "..." << u << std::endl;
808
4356
        Node range = Rewriter::rewrite( NodeManager::currentNM()->mkNode( MINUS, u, l ) );
809
4356
        Node ra = Rewriter::rewrite( NodeManager::currentNM()->mkNode( LEQ, range, NodeManager::currentNM()->mkConst( Rational( 9999 ) ) ) );
810
4356
        Node tl = l;
811
4356
        Node tu = u;
812
2178
        getBounds( q, v, rsi, tl, tu );
813
2178
        Assert(!tl.isNull() && !tu.isNull());
814
2178
        if (ra.isConst() && ra.getConst<bool>())
815
        {
816
2178
          long rr = range.getConst<Rational>().getNumerator().getLong()+1;
817
2178
          Trace("bound-int-rsi")  << "Actual bound range is " << rr << std::endl;
818
14945
          for (long k = 0; k < rr; k++)
819
          {
820
25534
            Node t = NodeManager::currentNM()->mkNode(PLUS, tl, NodeManager::currentNM()->mkConst( Rational(k) ) );
821
12767
            t = Rewriter::rewrite( t );
822
12767
            elements.push_back( t );
823
          }
824
2178
          return true;
825
        }else{
826
          Trace("fmf-incomplete") << "Incomplete because of integer quantification, bounds are too big for " << v << "." << std::endl;
827
          return false;
828
        }
829
      }
830
144
    }else if( bvt==BOUND_SET_MEMBER  ){
831
108
      Node srv = getSetRangeValue( q, v, rsi );
832
54
      if( srv.isNull() ){
833
1
        Trace("bound-int-warn") << "WARNING: Could not find set bound in model for " << v << " in " << q << std::endl;
834
1
        return false;
835
      }else{
836
53
        Trace("bound-int-rsi") << "Bounded by set membership : " << srv << std::endl;
837
53
        if( srv.getKind()!=EMPTYSET ){
838
          //collect the elements
839
95
          while( srv.getKind()==UNION ){
840
22
            Assert(srv[1].getKind() == kind::SINGLETON);
841
22
            elements.push_back( srv[1][0] );
842
22
            srv = srv[0];
843
          }
844
51
          Assert(srv.getKind() == kind::SINGLETON);
845
51
          elements.push_back( srv[0] );
846
          //check if we need to do matching, for literals like ( tuple( v ) in S )
847
102
          Node t = d_setm_range_lit[q][v][0];
848
51
          if( t!=v ){
849
32
            std::vector< Node > elements_tmp;
850
16
            elements_tmp.insert( elements_tmp.end(), elements.begin(), elements.end() );
851
16
            elements.clear();
852
48
            for( unsigned i=0; i<elements_tmp.size(); i++ ){
853
              //do matching to determine v -> u
854
64
              Node u = matchBoundVar( v, t, elements_tmp[i] );
855
32
              Trace("bound-int-rsi-debug") << "  unification : " << elements_tmp[i] << " = " << t << " yields " << v << " -> " << u << std::endl;
856
32
              if( !u.isNull() ){
857
32
                elements.push_back( u );
858
              }
859
            }
860
          }
861
        }
862
53
        return true;
863
      }
864
90
    }else if( bvt==BOUND_FIXED_SET ){
865
86
      std::map< Node, std::vector< Node > >::iterator it = d_fixed_set_gr_range[q].find( v );
866
86
      if( it!=d_fixed_set_gr_range[q].end() ){
867
174
        for( unsigned i=0; i<it->second.size(); i++ ){
868
108
          elements.push_back( it->second[i] );
869
        }
870
      }
871
86
      it = d_fixed_set_ngr_range[q].find( v );
872
86
      if( it!=d_fixed_set_ngr_range[q].end() ){
873
104
        std::vector< Node > vars;
874
104
        std::vector< Node > subs;
875
52
        if( getRsiSubsitution( q, v, vars, subs, rsi ) ){
876
98
          for( unsigned i=0; i<it->second.size(); i++ ){
877
108
            Node t = it->second[i].substitute( vars.begin(), vars.end(), subs.begin(), subs.end() );
878
54
            elements.push_back( t );
879
          }
880
44
          return true;
881
        }else{
882
8
          return false;
883
        }
884
      }else{
885
34
        return true;
886
      }
887
    }else{
888
4
      return false;
889
    }
890
  }else{
891
    //no change required
892
267
    return true;
893
  }
894
22746
}