1 /********************* */
2 /*! \file bounded_integers.cpp
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
12 ** \brief Bounded integers module
14 ** This class manages integer bounds for quantifiers
17 #include "theory/quantifiers/fmf/bounded_integers.h"
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"
32 using namespace CVC4::theory
;
33 using namespace CVC4::theory::quantifiers
;
34 using namespace CVC4::kind
;
36 BoundedIntegers::IntRangeDecisionHeuristic::IntRangeDecisionHeuristic(
42 : DecisionStrategyFmf(c
, valuation
), d_range(r
), d_ranges_proxied(u
)
44 if( options::fmfBoundLazy() ){
45 d_proxy_range
= isProxy
? r
: NodeManager::currentNM()->mkSkolem( "pbir", r
.getType() );
50 Trace("bound-int") << "Introduce proxy " << d_proxy_range
<< " for " << d_range
<< std::endl
;
53 Node
BoundedIntegers::IntRangeDecisionHeuristic::mkLiteral(unsigned n
)
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
);
60 Node
BoundedIntegers::IntRangeDecisionHeuristic::proxyCurrentRangeLemma()
62 if (d_range
== d_proxy_range
)
67 if (!getAssertedLiteralIndex(curr
))
71 if (d_ranges_proxied
.find(curr
) != d_ranges_proxied
.end())
75 d_ranges_proxied
[curr
] = true;
76 NodeManager
* nm
= NodeManager::currentNM();
77 Node currLit
= getLiteral(curr
);
81 nm
->mkNode(curr
== 0 ? LT
: LEQ
,
83 nm
->mkConst(Rational(curr
== 0 ? 0 : curr
- 1))));
87 BoundedIntegers::BoundedIntegers(QuantifiersEngine
* qe
,
89 QuantifiersInferenceManager
& qim
)
90 : QuantifiersModule(qs
, qim
, qe
)
94 BoundedIntegers::~BoundedIntegers() {}
96 void BoundedIntegers::presolve() {
100 bool BoundedIntegers::hasNonBoundVar( Node f
, Node b
, std::map
< Node
, bool >& visited
) {
101 if( visited
.find( b
)==visited
.end() ){
103 if( b
.getKind()==BOUND_VARIABLE
){
104 if( !isBound( f
, b
) ){
108 for( unsigned i
=0; i
<b
.getNumChildren(); i
++ ){
109 if( hasNonBoundVar( f
, b
[i
], visited
) ){
117 bool BoundedIntegers::hasNonBoundVar( Node f
, Node b
) {
118 std::map
< Node
, bool > visited
;
119 return hasNonBoundVar( f
, b
, visited
);
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
++ ){
126 if( !hasNonBoundVar( q
, n
[1-i
] ) ){
128 v_cases
.push_back( n
[1-i
] );
130 }else if( v
.isNull() && t
.getKind()==BOUND_VARIABLE
){
132 v_cases
.push_back( n
[1-i
] );
141 void BoundedIntegers::processMatchBoundVars( Node q
, Node n
, std::vector
< Node
>& bvs
, std::map
< Node
, bool >& visited
){
142 if( visited
.find( n
)==visited
.end() ){
144 if( n
.getKind()==BOUND_VARIABLE
&& !isBound( q
, 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
);
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
);
167 //if we are ( x != t1 ^ ...^ x != tn ), then x can be bound to { t1...tn }
170 conj
= TermUtil::simpleNegate( conj
);
172 Trace("bound-int-debug") << "Process possible finite disequality conjunction : " << conj
<< std::endl
;
173 Assert(conj
.getKind() == AND
);
175 std::vector
< Node
> v_cases
;
177 for( unsigned i
=0; i
<conj
.getNumChildren(); i
++ ){
178 if( conj
[i
].getKind()==NOT
&& processEqDisjunct( q
, conj
[i
][0], v
, v_cases
) ){
181 Trace("bound-int-debug") << "...failed due to " << conj
[i
] << std::endl
;
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() );
195 }else if( n
.getKind()==EQUAL
){
197 // non-applied DER on x != t, x can be bound to { t }
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] );
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
))
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
){
225 if (ArithMSum::isolate(it
->first
, msum
, veq
, GEQ
) != 0)
233 if( n1
.getKind()==BOUND_VARIABLE
){
234 n2
= ArithMSum::offset(n2
, 1);
236 n1
= ArithMSum::offset(n1
, -1);
238 veq
= NodeManager::currentNM()->mkNode( GEQ
, n1
, n2
);
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
;
250 Trace("bound-int-debug") << "The term " << t
<< " has non-bound variable." << std::endl
;
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
;
272 Assert(n
.getKind() != LEQ
&& n
.getKind() != LT
&& n
.getKind() != GT
);
276 bool BoundedIntegers::needsCheck( Theory::Effort e
) {
277 return e
==Theory::EFFORT_LAST_CALL
;
280 void BoundedIntegers::check(Theory::Effort e
, QEffort quant_e
)
282 if (quant_e
!= QEFFORT_STANDARD
)
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
)
291 Node prangeLem
= d_rms
[r
]->proxyCurrentRangeLemma();
292 if (!prangeLem
.isNull())
294 Trace("bound-int-lemma")
295 << "*** bound int : proxy lemma : " << prangeLem
<< std::endl
;
296 d_quantEngine
->addLemma(prangeLem
);
300 Trace("bint-engine") << " addedLemma = " << addedLemma
<< std::endl
;
302 void BoundedIntegers::setBoundedVar(Node q
, Node v
, BoundVarType bound_type
)
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
311 void BoundedIntegers::checkOwnership(Node f
)
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();
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
;
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
){
329 if( !isBound( f
, v
) ){
330 bool setBoundVar
= false;
331 if( it
->second
==BOUND_INT_RANGE
){
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())
337 setBoundedVar( f
, v
, BOUND_INT_RANGE
);
339 for( unsigned b
=0; b
<2; b
++ ){
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
];
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
;
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())
356 setBoundedVar(f
, v
, BOUND_SET_MEMBER
);
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
;
365 }else if( it
->second
==BOUND_FIXED_SET
){
366 setBoundedVar(f
, v
, BOUND_FIXED_SET
);
368 for (unsigned i
= 0; i
< bound_fixed_set
[v
].size(); i
++)
370 Node t
= bound_fixed_set
[v
][i
];
371 if (expr::hasBoundVar(t
))
373 d_fixed_set_ngr_range
[f
][v
].push_back(t
);
377 d_fixed_set_gr_range
[f
][v
].push_back(t
);
380 Trace("bound-int") << "Variable " << v
381 << " is bound because of disequality conjunction "
382 << bound_lit_map
[3][v
] << std::endl
;
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())
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
397 Assert(blmp
.find(v
) != blmp
.end());
398 BoundIntLitAttribute bila
;
399 bound_lit_map
[b
][v
].setAttribute(bila
, blmp
[v
] ? 1 : 0);
403 Assert(it
->second
!= BOUND_INT_RANGE
);
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
))
418 setBoundedVar( f
, f
[0][i
], BOUND_FINITE
);
426 if( Trace
.isOn("bound-int") ){
427 Trace("bound-int") << "Bounds are : " << std::endl
;
428 for( unsigned i
=0; i
<f
[0].getNumChildren(); 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
;
438 Trace("bound-int") << " " << v
<< " unifiable in " << d_setm_range_lit
[f
][v
] << std::endl
;
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
])
444 Trace("bound-int") << fnr
<< " ";
446 for (TNode fgr
: d_fixed_set_gr_range
[f
][v
])
448 Trace("bound-int") << fgr
<< " ";
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
;
454 Trace("bound-int") << " " << v
<< " has unknown bound." << std::endl
;
458 Trace("bound-int") << " " << "*** " << v
<< " is unbounded." << std::endl
;
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;
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
;
480 bool isProxy
= false;
481 if (expr::hasBoundVar(r
))
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
;
492 if (d_rms
.find(r
) == d_rms
.end())
494 Trace("bound-int") << "For " << v
<< ", bounded Integer Module will try to minimize : " << r
<< std::endl
;
495 d_ranges
.push_back( r
);
497 new IntRangeDecisionHeuristic(r
,
498 d_qstate
.getSatContext(),
499 d_qstate
.getUserContext(),
500 d_quantEngine
->getValuation(),
502 d_quantEngine
->getTheoryEngine()
503 ->getDecisionManager()
504 ->registerStrategy(DecisionManager::STRAT_QUANT_BOUND_INT_SIZE
,
513 bool BoundedIntegers::isBound(Node q
, Node v
) const
515 std::map
<Node
, std::vector
<Node
> >::const_iterator its
= d_set
.find(q
);
516 if (its
== d_set
.end())
520 return std::find(its
->second
.begin(), its
->second
.end(), v
)
521 != its
->second
.end();
524 BoundVarType
BoundedIntegers::getBoundVarType(Node q
, Node v
) const
526 std::map
<Node
, std::map
<Node
, BoundVarType
> >::const_iterator itb
=
527 d_bound_type
.find(q
);
528 if (itb
== d_bound_type
.end())
532 std::map
<Node
, BoundVarType
>::const_iterator it
= itb
->second
.find(v
);
533 if (it
== itb
->second
.end())
540 void BoundedIntegers::getBoundVarIndices(Node q
,
541 std::vector
<unsigned>& indices
) const
543 std::map
<Node
, std::vector
<Node
> >::const_iterator it
= d_set
.find(q
);
544 if (it
!= d_set
.end())
546 for (const Node
& v
: it
->second
)
548 indices
.push_back(d_quantEngine
->getTermUtil()->getVariableNum(q
, v
));
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() );
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
;
574 l
= d_quantEngine
->getModel()->getValue( l
);
577 u
= d_quantEngine
->getModel()->getValue( u
);
579 Trace("bound-int-rsi") << "Value is " << l
<< " ... " << u
<< std::endl
;
583 bool BoundedIntegers::isGroundRange(Node q
, Node v
)
587 if (d_bound_type
[q
][v
] == BOUND_INT_RANGE
)
589 return !expr::hasBoundVar(getLowerBound(q
, v
))
590 && !expr::hasBoundVar(getUpperBound(q
, v
));
592 else if (d_bound_type
[q
][v
] == BOUND_SET_MEMBER
)
594 return !expr::hasBoundVar(d_setm_range
[q
][v
]);
596 else if (d_bound_type
[q
][v
] == BOUND_FIXED_SET
)
598 return !d_fixed_set_ngr_range
[q
][v
].empty();
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() );
623 Node
BoundedIntegers::getSetRangeValue( Node q
, Node v
, RepSetIterator
* rsi
) {
624 Node sr
= getSetRange( q
, v
, rsi
);
629 Trace("bound-int-rsi") << "Get value in model for..." << sr
<< std::endl
;
630 Assert(!expr::hasFreeVar(sr
));
632 sr
= d_quantEngine
->getModel()->getValue(sr
);
633 // if non-constant, then sr does not occur in the model, we fail
638 Trace("bound-int-rsi") << "Value is " << sr
<< std::endl
;
639 if (sr
.getKind() == EMPTYSET
)
643 NodeManager
* nm
= NodeManager::currentNM();
645 TypeNode tne
= sr
.getType().getSetElementType();
647 // we can use choice functions for canonical symbolic instantiations
649 while (sr
.getKind() == UNION
)
654 Assert(sr
.getKind() == SINGLETON
);
656 // choices[i] stores the canonical symbolic representation of the (i+1)^th
658 std::vector
<Node
> choices
;
659 Node srCardN
= nm
->mkNode(CARD
, sro
);
661 for (unsigned i
= 0; i
< srCard
; i
++)
663 if (i
== d_setm_choice
[sro
].size())
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)
670 cBody
= nm
->mkNode(AND
, cBody
, nm
->mkNode(DISTINCT
, choices
));
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
);
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
);
688 nsr
= nm
->mkNode(UNION
, nsr
, sChoiceI
);
691 // turns the concrete model value of sro into a canonical representation
693 // singleton(0) union singleton(1)
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
;
701 bool BoundedIntegers::getRsiSubsitution( Node q
, Node v
, std::vector
< Node
>& vars
, std::vector
< Node
>& subs
, RepSetIterator
* rsi
) {
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
] );
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
){
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
);
736 Node
BoundedIntegers::matchBoundVar( Node v
, Node t
, Node e
){
739 }else if( t
.getKind()==kind::APPLY_CONSTRUCTOR
){
740 if( e
.getKind()==kind::APPLY_CONSTRUCTOR
){
741 if( t
.getOperator() != e
.getOperator() ) {
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
++ ){
750 if( e
.getKind()==kind::APPLY_CONSTRUCTOR
){
751 u
= matchBoundVar( v
, t
[i
], e
[i
] );
753 Node se
= nm
->mkNode(APPLY_SELECTOR_TOTAL
,
754 dt
[index
].getSelectorInternal(e
.getType(), i
),
756 u
= matchBoundVar( v
, t
[i
], se
);
766 bool BoundedIntegers::getBoundElements( RepSetIterator
* rsi
, bool initial
, Node q
, Node v
, std::vector
< Node
>& elements
) {
767 if( initial
|| !isGroundRange( q
, v
) ){
769 BoundVarType bvt
= getBoundVarType(q
, v
);
770 if( bvt
==BOUND_INT_RANGE
){
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
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 ) ) ) );
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
);
795 Trace("fmf-incomplete") << "Incomplete because of integer quantification, bounds are too big for " << v
<< "." << std::endl
;
799 }else if( bvt
==BOUND_SET_MEMBER
){
800 Node srv
= getSetRangeValue( q
, v
, rsi
);
802 Trace("bound-int-warn") << "WARNING: Could not find set bound in model for " << v
<< " in " << q
<< std::endl
;
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] );
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];
818 std::vector
< Node
> elements_tmp
;
819 elements_tmp
.insert( elements_tmp
.end(), elements
.begin(), elements
.end() );
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
;
826 elements
.push_back( u
);
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
] );
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
);