1 /********************* */
4 ** Original author: Andrew Reynolds
5 ** Major contributors: Morgan Deters
6 ** Minor contributors (to current version): none
7 ** This file is part of the CVC4 project.
8 ** Copyright (c) 2009-2013 New York University and The University of Iowa
9 ** See the file COPYING in the top-level source directory for licensing
10 ** 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"
21 using namespace CVC4::kind
;
22 using namespace CVC4::context
;
23 using namespace CVC4::theory
;
27 d_type_complete
.clear();
31 int RepSet::getNumRepresentatives( TypeNode tn
) const{
32 std::map
< TypeNode
, std::vector
< Node
> >::const_iterator it
= d_type_reps
.find( tn
);
33 if( it
!=d_type_reps
.end() ){
34 return (int)it
->second
.size();
40 void RepSet::add( Node n
){
41 TypeNode t
= n
.getType();
42 d_tmap
[ n
] = (int)d_type_reps
[t
].size();
43 d_type_reps
[t
].push_back( n
);
46 int RepSet::getIndexFor( Node n
) const {
47 std::map
< Node
, int >::const_iterator it
= d_tmap
.find( n
);
48 if( it
!=d_tmap
.end() ){
55 void RepSet::complete( TypeNode t
){
56 if( d_type_complete
.find( t
)==d_type_complete
.end() ){
57 d_type_complete
[t
] = true;
59 while( !te
.isFinished() ){
61 if( std::find( d_type_reps
[t
].begin(), d_type_reps
[t
].end(), n
)==d_type_reps
[t
].end() ){
66 for( size_t i
=0; i
<d_type_reps
[t
].size(); i
++ ){
67 Trace("reps-complete") << d_type_reps
[t
][i
] << " ";
69 Trace("reps-complete") << std::endl
;
73 void RepSet::toStream(std::ostream
& out
){
75 for( std::map
< TypeNode
, std::vector
< Node
> >::iterator it
= d_type_reps
.begin(); it
!= d_type_reps
.end(); ++it
){
76 out
<< it
->first
<< " : " << std::endl
;
77 for( int i
=0; i
<(int)it
->second
.size(); i
++ ){
78 out
<< " " << i
<< ": " << it
->second
[i
] << std::endl
;
82 for( std::map
< TypeNode
, std::vector
< Node
> >::iterator it
= d_type_reps
.begin(); it
!= d_type_reps
.end(); ++it
){
83 if( !it
->first
.isFunction() && !it
->first
.isPredicate() ){
84 out
<< "(" << it
->first
<< " " << it
->second
.size();
86 for( int i
=0; i
<(int)it
->second
.size(); i
++ ){
87 if( i
>0 ){ out
<< " "; }
91 out
<< ")" << std::endl
;
98 RepSetIterator::RepSetIterator( QuantifiersEngine
* qe
, RepSet
* rs
) : d_qe(qe
), d_rep_set( rs
){
102 int RepSetIterator::domainSize( int i
) {
104 if( d_enum_type
[i
]==ENUM_DOMAIN_ELEMENTS
){
105 return d_domain
[i
].size();
107 return d_domain
[i
][0];
111 bool RepSetIterator::setQuantifier( Node f
){
112 Trace("rsi") << "Make rsi for " << f
<< std::endl
;
113 Assert( d_types
.empty() );
115 for( size_t i
=0; i
<f
[0].getNumChildren(); i
++ ){
116 d_types
.push_back( f
[0][i
].getType() );
122 bool RepSetIterator::setFunctionDomain( Node op
){
123 Trace("rsi") << "Make rsi for " << op
<< std::endl
;
124 Assert( d_types
.empty() );
125 TypeNode tn
= op
.getType();
126 for( size_t i
=0; i
<tn
.getNumChildren()-1; i
++ ){
127 d_types
.push_back( tn
[i
] );
133 bool RepSetIterator::initialize(){
134 for( size_t i
=0; i
<d_types
.size(); i
++ ){
135 d_index
.push_back( 0 );
136 //store default index order
137 d_index_order
.push_back( i
);
139 //store default domain
140 d_domain
.push_back( RepDomain() );
141 TypeNode tn
= d_types
[i
];
143 if( !d_rep_set
->hasType( tn
) ){
144 Node var
= NodeManager::currentNM()->mkSkolem( "repSet_$$", tn
, "is a variable created by the RepSetIterator" );
145 Trace("mkVar") << "RepSetIterator:: Make variable " << var
<< " : " << tn
<< std::endl
;
146 d_rep_set
->add( var
);
148 }else if( tn
.isInteger() ){
150 //check if it is bound
151 if( d_owner
.getKind()==FORALL
&& d_qe
&& d_qe
->getBoundedIntegers() ){
152 if( d_qe
->getBoundedIntegers()->isBoundVar( d_owner
, d_owner
[0][i
] ) ){
153 Trace("bound-int-rsi") << "Rep set iterator: variable #" << i
<< " is bounded integer." << std::endl
;
154 d_enum_type
.push_back( ENUM_RANGE
);
162 //check if it is otherwise bound
163 if( d_bounds
[0].find(i
)!=d_bounds
[0].end() && d_bounds
[1].find(i
)!=d_bounds
[1].end() ){
164 Trace("bound-int-rsi") << "Rep set iterator: variable #" << i
<< " is bounded." << std::endl
;
165 d_enum_type
.push_back( ENUM_RANGE
);
167 Trace("fmf-incomplete") << "Incomplete because of integer quantification of " << d_owner
[0][i
] << "." << std::endl
;
171 //enumerate if the sort is reasonably small, the upper bound of 1000 is chosen arbitrarily for now
172 }else if( tn
.getCardinality().isFinite() && tn
.getCardinality().getFiniteCardinality().toUnsignedInt()<=1000 ){
173 d_rep_set
->complete( tn
);
175 Trace("fmf-incomplete") << "Incomplete because of quantification of type " << tn
<< std::endl
;
178 if( d_enum_type
.size()<=i
){
179 d_enum_type
.push_back( ENUM_DOMAIN_ELEMENTS
);
180 if( d_rep_set
->hasType( tn
) ){
181 for( size_t j
=0; j
<d_rep_set
->d_type_reps
[tn
].size(); j
++ ){
182 d_domain
[i
].push_back( j
);
189 //must set a variable index order based on bounded integers
190 if (d_owner
.getKind()==FORALL
&& d_qe
&& d_qe
->getBoundedIntegers()) {
191 Trace("bound-int-rsi") << "Calculating variable order..." << std::endl
;
192 std::vector
< int > varOrder
;
193 for( unsigned i
=0; i
<d_qe
->getBoundedIntegers()->getNumBoundVars(d_owner
); i
++ ){
194 varOrder
.push_back(d_qe
->getBoundedIntegers()->getBoundVarNum(d_owner
,i
));
196 for( unsigned i
=0; i
<d_owner
[0].getNumChildren(); i
++) {
197 if( !d_qe
->getBoundedIntegers()->isBoundVar(d_owner
, d_owner
[0][i
])) {
198 varOrder
.push_back(i
);
201 Trace("bound-int-rsi") << "Variable order : ";
202 for( unsigned i
=0; i
<varOrder
.size(); i
++) {
203 Trace("bound-int-rsi") << varOrder
[i
] << " ";
205 Trace("bound-int-rsi") << std::endl
;
206 std::vector
< int > indexOrder
;
207 indexOrder
.resize(varOrder
.size());
208 for( unsigned i
=0; i
<varOrder
.size(); i
++){
209 indexOrder
[varOrder
[i
]] = i
;
211 Trace("bound-int-rsi") << "Will use index order : ";
212 for( unsigned i
=0; i
<indexOrder
.size(); i
++) {
213 Trace("bound-int-rsi") << indexOrder
[i
] << " ";
215 Trace("bound-int-rsi") << std::endl
;
216 setIndexOrder(indexOrder
);
218 //now reset the indices
219 for (unsigned i
=0; i
<d_index
.size(); i
++) {
220 if (!resetIndex(i
, true)){
227 void RepSetIterator::setIndexOrder( std::vector
< int >& indexOrder
){
228 d_index_order
.clear();
229 d_index_order
.insert( d_index_order
.begin(), indexOrder
.begin(), indexOrder
.end() );
230 //make the d_var_order mapping
231 for( int i
=0; i
<(int)d_index_order
.size(); i
++ ){
232 d_var_order
[d_index_order
[i
]] = i
;
236 void RepSetIterator::setDomain( std::vector< RepDomain >& domain ){
238 d_domain.insert( d_domain.begin(), domain.begin(), domain.end() );
239 //we are done if a domain is empty
240 for( int i=0; i<(int)d_domain.size(); i++ ){
241 if( d_domain[i].empty() ){
247 bool RepSetIterator::resetIndex( int i
, bool initial
) {
249 int ii
= d_index_order
[i
];
250 Trace("bound-int-rsi") << "Reset " << i
<< " " << ii
<< " " << initial
<< std::endl
;
251 //determine the current range
252 if( d_enum_type
[ii
]==ENUM_RANGE
){
253 if( initial
|| ( d_qe
->getBoundedIntegers() && !d_qe
->getBoundedIntegers()->isGroundRange( d_owner
, d_owner
[0][ii
] ) ) ){
254 Trace("bound-int-rsi") << "Getting range of " << d_owner
[0][ii
] << std::endl
;
257 if( d_qe
->getBoundedIntegers() && d_qe
->getBoundedIntegers()->isBoundVar( d_owner
, d_owner
[0][ii
] ) ){
258 d_qe
->getBoundedIntegers()->getBoundValues( d_owner
, d_owner
[0][ii
], this, l
, u
);
260 for( unsigned b
=0; b
<2; b
++ ){
261 if( d_bounds
[b
].find(ii
)!=d_bounds
[b
].end() ){
262 Trace("bound-int-rsi") << "May further limit bound(" << b
<< ") based on " << d_bounds
[b
][ii
] << std::endl
;
263 if( b
==0 && (l
.isNull() || d_bounds
[b
][ii
].getConst
<Rational
>() > l
.getConst
<Rational
>()) ){
265 //bound was limited externally, record that the value lower bound is not equal to the term lower bound
266 actual_l
= NodeManager::currentNM()->mkNode( MINUS
, d_bounds
[b
][ii
], l
);
269 }else if( b
==1 && (u
.isNull() || d_bounds
[b
][ii
].getConst
<Rational
>() <= u
.getConst
<Rational
>()) ){
270 u
= NodeManager::currentNM()->mkNode( MINUS
, d_bounds
[b
][ii
],
271 NodeManager::currentNM()->mkConst( Rational(1) ) );
272 u
= Rewriter::rewrite( u
);
277 if( l
.isNull() || u
.isNull() ){
278 //failed, abort the iterator
282 Trace("bound-int-rsi") << "Can limit bounds of " << d_owner
[0][ii
] << " to " << l
<< "..." << u
<< std::endl
;
283 Node range
= Rewriter::rewrite( NodeManager::currentNM()->mkNode( MINUS
, u
, l
) );
284 Node ra
= Rewriter::rewrite( NodeManager::currentNM()->mkNode( LEQ
, range
, NodeManager::currentNM()->mkConst( Rational( 9999 ) ) ) );
285 d_domain
[ii
].clear();
288 if( d_qe
->getBoundedIntegers() && d_qe
->getBoundedIntegers()->isBoundVar( d_owner
, d_owner
[0][ii
] ) ){
289 d_qe
->getBoundedIntegers()->getBounds( d_owner
, d_owner
[0][ii
], this, tl
, tu
);
291 d_lower_bounds
[ii
] = tl
;
292 if( !actual_l
.isNull() ){
293 //if bound was limited externally, must consider the offset
294 d_lower_bounds
[ii
] = Rewriter::rewrite( NodeManager::currentNM()->mkNode( PLUS
, tl
, actual_l
) );
296 if( ra
==NodeManager::currentNM()->mkConst(true) ){
297 long rr
= range
.getConst
<Rational
>().getNumerator().getLong()+1;
298 Trace("bound-int-rsi") << "Actual bound range is " << rr
<< std::endl
;
299 d_domain
[ii
].push_back( (int)rr
);
301 Trace("fmf-incomplete") << "Incomplete because of integer quantification, bounds are too big for " << d_owner
[0][ii
] << "." << std::endl
;
303 d_domain
[ii
].push_back( 0 );
307 Trace("bound-int-rsi") << d_owner
[0][ii
] << " has ground range, skip." << std::endl
;
313 int RepSetIterator::increment2( int counter
){
314 Assert( !isFinished() );
315 #ifdef DISABLE_EVAL_SKIP_MULTIPLE
316 counter
= (int)d_index
.size()-1;
320 Trace("rsi-debug") << "domain size of " << counter
<< " is " << domainSize(counter
) << std::endl
;
322 while( counter
>=0 && d_index
[counter
]>=(int)(domainSize(counter
)-1) ){
325 Trace("rsi-debug") << "domain size of " << counter
<< " is " << domainSize(counter
) << std::endl
;
332 bool emptyDomain
= false;
333 for( int i
=(int)d_index
.size()-1; i
>counter
; i
-- ){
336 }else if( domainSize(i
)<=0 ){
341 Trace("rsi-debug") << "This is an empty domain, increment again." << std::endl
;
348 int RepSetIterator::increment(){
350 return increment2( (int)d_index
.size()-1 );
356 bool RepSetIterator::isFinished(){
357 return d_index
.empty();
360 Node
RepSetIterator::getTerm( int i
){
361 int index
= d_index_order
[i
];
362 if( d_enum_type
[index
]==ENUM_DOMAIN_ELEMENTS
){
363 TypeNode tn
= d_types
[index
];
364 Assert( d_rep_set
->d_type_reps
.find( tn
)!=d_rep_set
->d_type_reps
.end() );
365 return d_rep_set
->d_type_reps
[tn
][d_domain
[index
][d_index
[index
]]];
367 Trace("rsi-debug") << "Get, with offset : " << index
<< " " << d_lower_bounds
[index
] << " " << d_index
[index
] << std::endl
;
368 Node t
= NodeManager::currentNM()->mkNode(PLUS
, d_lower_bounds
[index
],
369 NodeManager::currentNM()->mkConst( Rational(d_index
[index
]) ) );
370 t
= Rewriter::rewrite( t
);
375 void RepSetIterator::debugPrint( const char* c
){
376 for( int i
=0; i
<(int)d_index
.size(); i
++ ){
377 Debug( c
) << i
<< " : " << d_index
[i
] << " : " << getTerm( i
) << std::endl
;
381 void RepSetIterator::debugPrintSmall( const char* c
){
382 Debug( c
) << "RI: ";
383 for( int i
=0; i
<(int)d_index
.size(); i
++ ){
384 Debug( c
) << d_index
[i
] << ": " << getTerm( i
) << " ";
386 Debug( c
) << std::endl
;