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