1 /********************* */
4 ** Top contributors (to current version):
5 ** Andrew Reynolds, Morgan Deters, Tim King
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2016 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 Implementation of representative set
15 #include "theory/rep_set.h"
16 #include "theory/type_enumerator.h"
17 #include "theory/quantifiers/bounded_integers.h"
18 #include "theory/quantifiers/term_database.h"
19 #include "theory/quantifiers/first_order_model.h"
23 using namespace CVC4::kind
;
24 using namespace CVC4::context
;
25 using namespace CVC4::theory
;
29 d_type_complete
.clear();
31 d_values_to_terms
.clear();
34 bool RepSet::hasRep( TypeNode tn
, Node n
) {
35 std::map
< TypeNode
, std::vector
< Node
> >::iterator it
= d_type_reps
.find( tn
);
36 if( it
==d_type_reps
.end() ){
39 return std::find( it
->second
.begin(), it
->second
.end(), n
)!=it
->second
.end();
43 int RepSet::getNumRepresentatives( TypeNode tn
) const{
44 std::map
< TypeNode
, std::vector
< Node
> >::const_iterator it
= d_type_reps
.find( tn
);
45 if( it
!=d_type_reps
.end() ){
46 return (int)it
->second
.size();
52 bool containsStoreAll( Node n
, std::vector
< Node
>& cache
){
53 if( std::find( cache
.begin(), cache
.end(), n
)==cache
.end() ){
55 if( n
.getKind()==STORE_ALL
){
58 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
59 if( containsStoreAll( n
[i
], cache
) ){
68 void RepSet::add( TypeNode tn
, Node n
){
69 //for now, do not add array constants FIXME
71 std::vector
< Node
> cache
;
72 if( containsStoreAll( n
, cache
) ){
76 Trace("rsi-debug") << "Add rep #" << d_type_reps
[tn
].size() << " for " << tn
<< " : " << n
<< std::endl
;
77 Assert( n
.getType().isSubtypeOf( tn
) );
78 d_tmap
[ n
] = (int)d_type_reps
[tn
].size();
79 d_type_reps
[tn
].push_back( n
);
82 int RepSet::getIndexFor( Node n
) const {
83 std::map
< Node
, int >::const_iterator it
= d_tmap
.find( n
);
84 if( it
!=d_tmap
.end() ){
91 bool RepSet::complete( TypeNode t
){
92 std::map
< TypeNode
, bool >::iterator it
= d_type_complete
.find( t
);
93 if( it
==d_type_complete
.end() ){
95 for( unsigned i
=0; i
<d_type_reps
[t
].size(); i
++ ){
96 d_tmap
.erase( d_type_reps
[t
][i
] );
98 d_type_reps
[t
].clear();
99 //now complete the type
100 d_type_complete
[t
] = true;
101 TypeEnumerator
te(t
);
102 while( !te
.isFinished() ){
104 if( std::find( d_type_reps
[t
].begin(), d_type_reps
[t
].end(), n
)==d_type_reps
[t
].end() ){
109 for( size_t i
=0; i
<d_type_reps
[t
].size(); i
++ ){
110 Trace("reps-complete") << d_type_reps
[t
][i
] << " ";
112 Trace("reps-complete") << std::endl
;
119 void RepSet::toStream(std::ostream
& out
){
120 for( std::map
< TypeNode
, std::vector
< Node
> >::iterator it
= d_type_reps
.begin(); it
!= d_type_reps
.end(); ++it
){
121 if( !it
->first
.isFunction() && !it
->first
.isPredicate() ){
122 out
<< "(" << it
->first
<< " " << it
->second
.size();
124 for( unsigned i
=0; i
<it
->second
.size(); i
++ ){
125 if( i
>0 ){ out
<< " "; }
126 out
<< it
->second
[i
];
129 out
<< ")" << std::endl
;
135 RepSetIterator::RepSetIterator( QuantifiersEngine
* qe
, RepSet
* rs
) : d_qe(qe
), d_rep_set( rs
){
136 d_incomplete
= false;
139 int RepSetIterator::domainSize( int i
) {
141 int v
= d_var_order
[i
];
142 if( d_enum_type
[v
]==ENUM_DOMAIN_ELEMENTS
){
143 return d_domain
[v
].size();
145 return d_domain
[v
][0];
149 bool RepSetIterator::setQuantifier( Node f
){
150 Trace("rsi") << "Make rsi for " << f
<< std::endl
;
151 Assert( d_types
.empty() );
153 for( size_t i
=0; i
<f
[0].getNumChildren(); i
++ ){
154 d_types
.push_back( f
[0][i
].getType() );
160 bool RepSetIterator::setFunctionDomain( Node op
){
161 Trace("rsi") << "Make rsi for " << op
<< std::endl
;
162 Assert( d_types
.empty() );
163 TypeNode tn
= op
.getType();
164 for( size_t i
=0; i
<tn
.getNumChildren()-1; i
++ ){
165 d_types
.push_back( tn
[i
] );
171 bool RepSetIterator::initialize(){
172 Trace("rsi") << "Initialize rep set iterator..." << std::endl
;
173 for( unsigned v
=0; v
<d_types
.size(); v
++ ){
174 d_index
.push_back( 0 );
175 //store default index order
176 d_index_order
.push_back( v
);
178 //store default domain
179 d_domain
.push_back( RepDomain() );
180 TypeNode tn
= d_types
[v
];
181 Trace("rsi") << "Var #" << v
<< " is type " << tn
<< "..." << std::endl
;
183 //must ensure uninterpreted type is non-empty.
184 if( !d_rep_set
->hasType( tn
) ){
186 // terms in rep_set are now constants which mapped to terms through TheoryModel
187 // thus, should introduce a constant and a term. for now, just a term.
189 //Node c = d_qe->getTermDatabase()->getEnumerateTerm( tn, 0 );
190 Node var
= d_qe
->getModel()->getSomeDomainElement( tn
);
191 Trace("mkVar") << "RepSetIterator:: Make variable " << var
<< " : " << tn
<< std::endl
;
192 d_rep_set
->add( tn
, var
);
196 //check if it is bound by bounded integer module
197 if( d_owner
.getKind()==FORALL
&& d_qe
&& d_qe
->getBoundedIntegers() ){
198 unsigned bvt
= d_qe
->getBoundedIntegers()->getBoundVarType( d_owner
, d_owner
[0][v
] );
199 if( bvt
==quantifiers::BoundedIntegers::BOUND_INT_RANGE
){
200 Trace("rsi") << " variable is bounded integer." << std::endl
;
201 d_enum_type
.push_back( ENUM_INT_RANGE
);
203 }else if( bvt
==quantifiers::BoundedIntegers::BOUND_SET_MEMBER
){
204 Trace("rsi") << " variable is bounded by set membership." << std::endl
;
205 d_enum_type
.push_back( ENUM_SET_MEMBERS
);
210 //check if it is otherwise bound
211 if( d_bounds
[0].find( v
)!=d_bounds
[0].end() && d_bounds
[1].find( v
)!=d_bounds
[1].end() ){
212 Trace("rsi") << " variable is bounded." << std::endl
;
213 d_enum_type
.push_back( ENUM_INT_RANGE
);
214 }else if( d_qe
->getTermDatabase()->mayComplete( tn
) ){
215 Trace("rsi") << " do complete, since cardinality is small (" << tn
.getCardinality() << ")..." << std::endl
;
216 d_rep_set
->complete( tn
);
217 //must have succeeded
218 Assert( d_rep_set
->hasType( tn
) );
220 Trace("rsi") << " variable cannot be bounded." << std::endl
;
221 Trace("fmf-incomplete") << "Incomplete because of quantification of type " << tn
<< std::endl
;
226 //if we have yet to determine the type of enumeration
227 if( d_enum_type
.size()<=v
){
228 d_enum_type
.push_back( ENUM_DOMAIN_ELEMENTS
);
229 if( d_rep_set
->hasType( tn
) ){
230 for( unsigned j
=0; j
<d_rep_set
->d_type_reps
[tn
].size(); j
++ ){
231 d_domain
[v
].push_back( j
);
234 Assert( d_incomplete
);
239 //must set a variable index order based on bounded integers
240 if( d_owner
.getKind()==FORALL
&& d_qe
&& d_qe
->getBoundedIntegers() ){
241 Trace("bound-int-rsi") << "Calculating variable order..." << std::endl
;
242 std::vector
< int > varOrder
;
243 for( unsigned i
=0; i
<d_qe
->getBoundedIntegers()->getNumBoundVars( d_owner
); i
++ ){
244 Node v
= d_qe
->getBoundedIntegers()->getBoundVar( d_owner
, i
);
245 Trace("bound-int-rsi") << " bound var #" << i
<< " is " << v
<< std::endl
;
246 varOrder
.push_back( d_qe
->getTermDatabase()->getVariableNum( d_owner
, v
) );
248 for( unsigned i
=0; i
<d_owner
[0].getNumChildren(); i
++) {
249 if( !d_qe
->getBoundedIntegers()->isBoundVar(d_owner
, d_owner
[0][i
])) {
250 varOrder
.push_back(i
);
253 Trace("bound-int-rsi") << "Variable order : ";
254 for( unsigned i
=0; i
<varOrder
.size(); i
++) {
255 Trace("bound-int-rsi") << varOrder
[i
] << " ";
257 Trace("bound-int-rsi") << std::endl
;
258 std::vector
< int > indexOrder
;
259 indexOrder
.resize(varOrder
.size());
260 for( unsigned i
=0; i
<varOrder
.size(); i
++){
261 indexOrder
[varOrder
[i
]] = i
;
263 Trace("bound-int-rsi") << "Will use index order : ";
264 for( unsigned i
=0; i
<indexOrder
.size(); i
++) {
265 Trace("bound-int-rsi") << indexOrder
[i
] << " ";
267 Trace("bound-int-rsi") << std::endl
;
268 setIndexOrder( indexOrder
);
270 //now reset the indices
271 do_reset_increment( -1, true );
275 void RepSetIterator::setIndexOrder( std::vector
< int >& indexOrder
){
276 d_index_order
.clear();
277 d_index_order
.insert( d_index_order
.begin(), indexOrder
.begin(), indexOrder
.end() );
278 //make the d_var_order mapping
279 for( unsigned i
=0; i
<d_index_order
.size(); i
++ ){
280 d_var_order
[d_index_order
[i
]] = i
;
284 int RepSetIterator::resetIndex( int i
, bool initial
) {
286 int v
= d_var_order
[i
];
287 Trace("bound-int-rsi") << "Reset " << i
<< ", var order = " << v
<< ", initial = " << initial
<< std::endl
;
288 //determine the current range
289 if( initial
|| ( d_owner
.getKind()==FORALL
&& d_qe
&& d_qe
->getBoundedIntegers() &&
290 d_qe
->getBoundedIntegers()->isBoundVar( d_owner
, d_owner
[0][v
] ) &&
291 !d_qe
->getBoundedIntegers()->isGroundRange( d_owner
, d_owner
[0][v
] ) ) ){
292 Trace("bound-int-rsi") << "Getting range of " << d_owner
[0][v
] << std::endl
;
293 if( d_enum_type
[v
]==ENUM_INT_RANGE
){
296 if( d_qe
->getBoundedIntegers() ){
297 unsigned bvt
= d_qe
->getBoundedIntegers()->getBoundVarType( d_owner
, d_owner
[0][v
] );
298 if( bvt
==quantifiers::BoundedIntegers::BOUND_INT_RANGE
){
299 d_qe
->getBoundedIntegers()->getBoundValues( d_owner
, d_owner
[0][v
], this, l
, u
);
302 for( unsigned b
=0; b
<2; b
++ ){
303 if( d_bounds
[b
].find(v
)!=d_bounds
[b
].end() ){
304 Trace("bound-int-rsi") << "May further limit bound(" << b
<< ") based on " << d_bounds
[b
][v
] << std::endl
;
305 if( b
==0 && (l
.isNull() || d_bounds
[b
][v
].getConst
<Rational
>() > l
.getConst
<Rational
>()) ){
307 //bound was limited externally, record that the value lower bound is not equal to the term lower bound
308 actual_l
= NodeManager::currentNM()->mkNode( MINUS
, d_bounds
[b
][v
], l
);
311 }else if( b
==1 && (u
.isNull() || d_bounds
[b
][v
].getConst
<Rational
>() <= u
.getConst
<Rational
>()) ){
312 u
= NodeManager::currentNM()->mkNode( MINUS
, d_bounds
[b
][v
],
313 NodeManager::currentNM()->mkConst( Rational(1) ) );
314 u
= Rewriter::rewrite( u
);
319 if( l
.isNull() || u
.isNull() ){
320 //failed, abort the iterator
323 Trace("bound-int-rsi") << "Can limit bounds of " << d_owner
[0][v
] << " to " << l
<< "..." << u
<< std::endl
;
324 Node range
= Rewriter::rewrite( NodeManager::currentNM()->mkNode( MINUS
, u
, l
) );
325 Node ra
= Rewriter::rewrite( NodeManager::currentNM()->mkNode( LEQ
, range
, NodeManager::currentNM()->mkConst( Rational( 9999 ) ) ) );
329 if( d_qe
->getBoundedIntegers() && d_qe
->getBoundedIntegers()->isBoundVar( d_owner
, d_owner
[0][v
] ) ){
330 d_qe
->getBoundedIntegers()->getBounds( d_owner
, d_owner
[0][v
], this, tl
, tu
);
332 d_lower_bounds
[v
] = tl
;
333 if( !actual_l
.isNull() ){
334 //if bound was limited externally, must consider the offset
335 d_lower_bounds
[v
] = Rewriter::rewrite( NodeManager::currentNM()->mkNode( PLUS
, tl
, actual_l
) );
337 if( ra
==d_qe
->getTermDatabase()->d_true
){
338 long rr
= range
.getConst
<Rational
>().getNumerator().getLong()+1;
339 Trace("bound-int-rsi") << "Actual bound range is " << rr
<< std::endl
;
340 d_domain
[v
].push_back( (int)rr
);
345 Trace("fmf-incomplete") << "Incomplete because of integer quantification, bounds are too big for " << d_owner
[0][v
] << "." << std::endl
;
349 }else if( d_enum_type
[v
]==ENUM_SET_MEMBERS
){
350 Assert( d_qe
->getBoundedIntegers()->getBoundVarType( d_owner
, d_owner
[0][v
] )==quantifiers::BoundedIntegers::BOUND_SET_MEMBER
);
351 Node srv
= d_qe
->getBoundedIntegers()->getSetRangeValue( d_owner
, d_owner
[0][v
], this );
355 Trace("bound-int-rsi") << "Bounded by set membership : " << srv
<< std::endl
;
357 d_setm_bounds
[v
].clear();
358 if( srv
.getKind()!=EMPTYSET
){
359 while( srv
.getKind()==UNION
){
360 Assert( srv
[1].getKind()==kind::SINGLETON
);
361 d_setm_bounds
[v
].push_back( srv
[1][0] );
364 Assert( srv
.getKind()==kind::SINGLETON
);
365 d_setm_bounds
[v
].push_back( srv
[0] );
366 d_domain
[v
].push_back( d_setm_bounds
[v
].size() );
368 d_domain
[v
].push_back( 0 );
376 int RepSetIterator::increment2( int i
){
377 Assert( !isFinished() );
378 #ifdef DISABLE_EVAL_SKIP_MULTIPLE
379 i
= (int)d_index
.size()-1;
383 Trace("rsi-debug") << "domain size of " << i
<< " is " << domainSize(i
) << std::endl
;
385 while( i
>=0 && d_index
[i
]>=(int)(domainSize(i
)-1) ){
388 Trace("rsi-debug") << "domain size of " << i
<< " is " << domainSize(i
) << std::endl
;
395 Trace("rsi-debug") << "increment " << i
<< std::endl
;
397 return do_reset_increment( i
);
401 int RepSetIterator::do_reset_increment( int i
, bool initial
) {
402 bool emptyDomain
= false;
403 for( unsigned ii
=(i
+1); ii
<d_index
.size(); ii
++ ){
404 int ri_res
= resetIndex( ii
, initial
);
410 }else if( ri_res
==0 ){
413 //force next iteration if currently an empty domain
415 d_index
[ii
] = domainSize(ii
)-1;
419 Trace("rsi-debug") << "This is an empty domain, increment." << std::endl
;
426 int RepSetIterator::increment(){
428 return increment2( (int)d_index
.size()-1 );
434 bool RepSetIterator::isFinished(){
435 return d_index
.empty();
438 Node
RepSetIterator::getCurrentTerm( int v
){
439 Trace("rsi-debug") << "rsi : get term " << v
<< ", index order = " << d_index_order
[v
] << std::endl
;
440 int ii
= d_index_order
[v
];
441 int curr
= d_index
[ii
];
442 if( d_enum_type
[v
]==ENUM_DOMAIN_ELEMENTS
){
443 TypeNode tn
= d_types
[v
];
444 Assert( d_rep_set
->d_type_reps
.find( tn
)!=d_rep_set
->d_type_reps
.end() );
445 Assert( 0<=d_domain
[v
][curr
] && d_domain
[v
][curr
]<(int)d_rep_set
->d_type_reps
[tn
].size() );
446 return d_rep_set
->d_type_reps
[tn
][d_domain
[v
][curr
]];
447 }else if( d_enum_type
[v
]==ENUM_SET_MEMBERS
){
448 Assert( 0<=curr
&& curr
<(int)d_setm_bounds
[v
].size() );
449 return d_setm_bounds
[v
][curr
];
451 Trace("rsi-debug") << "Get, with offset : " << v
<< " " << d_lower_bounds
[v
] << " " << curr
<< std::endl
;
452 Assert( !d_lower_bounds
[v
].isNull() );
453 Node t
= NodeManager::currentNM()->mkNode(PLUS
, d_lower_bounds
[v
], NodeManager::currentNM()->mkConst( Rational(curr
) ) );
454 t
= Rewriter::rewrite( t
);
459 void RepSetIterator::debugPrint( const char* c
){
460 for( unsigned v
=0; v
<d_index
.size(); v
++ ){
461 Debug( c
) << v
<< " : " << getCurrentTerm( v
) << std::endl
;
465 void RepSetIterator::debugPrintSmall( const char* c
){
466 Debug( c
) << "RI: ";
467 for( unsigned v
=0; v
<d_index
.size(); v
++ ){
468 Debug( c
) << v
<< ": " << getCurrentTerm( v
) << " ";
470 Debug( c
) << std::endl
;