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