Fix for fmf with large finite cardinalities.
[cvc5.git] / src / theory / rep_set.cpp
1 /********************* */
2 /*! \file rep_set.cpp
3 ** \verbatim
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
11 **
12 ** \brief Implementation of representative set
13 **/
14
15 #include "theory/rep_set.h"
16 #include "theory/type_enumerator.h"
17 #include "theory/quantifiers/bounded_integers.h"
18
19 using namespace std;
20 using namespace CVC4;
21 using namespace CVC4::kind;
22 using namespace CVC4::context;
23 using namespace CVC4::theory;
24
25 void RepSet::clear(){
26 d_type_reps.clear();
27 d_type_complete.clear();
28 d_tmap.clear();
29 }
30
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();
35 }else{
36 return 0;
37 }
38 }
39
40 void RepSet::add( TypeNode tn, Node n ){
41 d_tmap[ n ] = (int)d_type_reps[tn].size();
42 Trace("rsi-debug") << "Add rep #" << d_type_reps[tn].size() << " for " << tn << " : " << n << std::endl;
43 d_type_reps[tn].push_back( n );
44 }
45
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() ){
49 return it->second;
50 }else{
51 return -1;
52 }
53 }
54
55 void RepSet::complete( TypeNode t ){
56 if( d_type_complete.find( t )==d_type_complete.end() ){
57 d_type_complete[t] = true;
58 TypeEnumerator te(t);
59 while( !te.isFinished() ){
60 Node n = *te;
61 if( std::find( d_type_reps[t].begin(), d_type_reps[t].end(), n )==d_type_reps[t].end() ){
62 add( t, n );
63 }
64 ++te;
65 }
66 for( size_t i=0; i<d_type_reps[t].size(); i++ ){
67 Trace("reps-complete") << d_type_reps[t][i] << " ";
68 }
69 Trace("reps-complete") << std::endl;
70 }
71 }
72
73 void RepSet::toStream(std::ostream& out){
74 #if 0
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;
79 }
80 }
81 #else
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();
85 out << " (";
86 for( int i=0; i<(int)it->second.size(); i++ ){
87 if( i>0 ){ out << " "; }
88 out << it->second[i];
89 }
90 out << ")";
91 out << ")" << std::endl;
92 }
93 }
94 #endif
95 }
96
97
98 RepSetIterator::RepSetIterator( QuantifiersEngine * qe, RepSet* rs ) : d_qe(qe), d_rep_set( rs ){
99 d_incomplete = false;
100 }
101
102 int RepSetIterator::domainSize( int i ) {
103 Assert(i>=0);
104 if( d_enum_type[i]==ENUM_DOMAIN_ELEMENTS ){
105 return d_domain[i].size();
106 }else{
107 return d_domain[i][0];
108 }
109 }
110
111 bool RepSetIterator::setQuantifier( Node f ){
112 Trace("rsi") << "Make rsi for " << f << std::endl;
113 Assert( d_types.empty() );
114 //store indicies
115 for( size_t i=0; i<f[0].getNumChildren(); i++ ){
116 d_types.push_back( f[0][i].getType() );
117 }
118 d_owner = f;
119 return initialize();
120 }
121
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] );
128 }
129 d_owner = op;
130 return initialize();
131 }
132
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 );
138 d_var_order[i] = i;
139 //store default domain
140 d_domain.push_back( RepDomain() );
141 TypeNode tn = d_types[i];
142 if( tn.isSort() ){
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( tn, var );
147 }
148 }else if( tn.isInteger() ){
149 bool inc = false;
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 );
155 }else{
156 inc = true;
157 }
158 }else{
159 inc = true;
160 }
161 if( inc ){
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 );
166 }else{
167 Trace("fmf-incomplete") << "Incomplete because of integer quantification of " << d_owner[0][i] << "." << std::endl;
168 d_incomplete = true;
169 }
170 }
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().isLargeFinite() &&
173 tn.getCardinality().getFiniteCardinality().toUnsignedInt()<=1000 ){
174 d_rep_set->complete( tn );
175 }else{
176 Trace("fmf-incomplete") << "Incomplete because of quantification of type " << tn << std::endl;
177 d_incomplete = true;
178 }
179 if( d_enum_type.size()<=i ){
180 d_enum_type.push_back( ENUM_DOMAIN_ELEMENTS );
181 if( d_rep_set->hasType( tn ) ){
182 for( size_t j=0; j<d_rep_set->d_type_reps[tn].size(); j++ ){
183 d_domain[i].push_back( j );
184 }
185 }else{
186 return false;
187 }
188 }
189 }
190 //must set a variable index order based on bounded integers
191 if (d_owner.getKind()==FORALL && d_qe && d_qe->getBoundedIntegers()) {
192 Trace("bound-int-rsi") << "Calculating variable order..." << std::endl;
193 std::vector< int > varOrder;
194 for( unsigned i=0; i<d_qe->getBoundedIntegers()->getNumBoundVars(d_owner); i++ ){
195 varOrder.push_back(d_qe->getBoundedIntegers()->getBoundVarNum(d_owner,i));
196 }
197 for( unsigned i=0; i<d_owner[0].getNumChildren(); i++) {
198 if( !d_qe->getBoundedIntegers()->isBoundVar(d_owner, d_owner[0][i])) {
199 varOrder.push_back(i);
200 }
201 }
202 Trace("bound-int-rsi") << "Variable order : ";
203 for( unsigned i=0; i<varOrder.size(); i++) {
204 Trace("bound-int-rsi") << varOrder[i] << " ";
205 }
206 Trace("bound-int-rsi") << std::endl;
207 std::vector< int > indexOrder;
208 indexOrder.resize(varOrder.size());
209 for( unsigned i=0; i<varOrder.size(); i++){
210 indexOrder[varOrder[i]] = i;
211 }
212 Trace("bound-int-rsi") << "Will use index order : ";
213 for( unsigned i=0; i<indexOrder.size(); i++) {
214 Trace("bound-int-rsi") << indexOrder[i] << " ";
215 }
216 Trace("bound-int-rsi") << std::endl;
217 setIndexOrder(indexOrder);
218 }
219 //now reset the indices
220 for (unsigned i=0; i<d_index.size(); i++) {
221 if (!resetIndex(i, true)){
222 break;
223 }
224 }
225 return true;
226 }
227
228 void RepSetIterator::setIndexOrder( std::vector< int >& indexOrder ){
229 d_index_order.clear();
230 d_index_order.insert( d_index_order.begin(), indexOrder.begin(), indexOrder.end() );
231 //make the d_var_order mapping
232 for( int i=0; i<(int)d_index_order.size(); i++ ){
233 d_var_order[d_index_order[i]] = i;
234 }
235 }
236 /*
237 void RepSetIterator::setDomain( std::vector< RepDomain >& domain ){
238 d_domain.clear();
239 d_domain.insert( d_domain.begin(), domain.begin(), domain.end() );
240 //we are done if a domain is empty
241 for( int i=0; i<(int)d_domain.size(); i++ ){
242 if( d_domain[i].empty() ){
243 d_index.clear();
244 }
245 }
246 }
247 */
248 bool RepSetIterator::resetIndex( int i, bool initial ) {
249 d_index[i] = 0;
250 int ii = d_index_order[i];
251 Trace("bound-int-rsi") << "Reset " << i << " " << ii << " " << initial << std::endl;
252 //determine the current range
253 if( d_enum_type[ii]==ENUM_RANGE ){
254 if( initial || ( d_qe->getBoundedIntegers() && !d_qe->getBoundedIntegers()->isGroundRange( d_owner, d_owner[0][ii] ) ) ){
255 Trace("bound-int-rsi") << "Getting range of " << d_owner[0][ii] << std::endl;
256 Node actual_l;
257 Node l, u;
258 if( d_qe->getBoundedIntegers() && d_qe->getBoundedIntegers()->isBoundVar( d_owner, d_owner[0][ii] ) ){
259 d_qe->getBoundedIntegers()->getBoundValues( d_owner, d_owner[0][ii], this, l, u );
260 }
261 for( unsigned b=0; b<2; b++ ){
262 if( d_bounds[b].find(ii)!=d_bounds[b].end() ){
263 Trace("bound-int-rsi") << "May further limit bound(" << b << ") based on " << d_bounds[b][ii] << std::endl;
264 if( b==0 && (l.isNull() || d_bounds[b][ii].getConst<Rational>() > l.getConst<Rational>()) ){
265 if( !l.isNull() ){
266 //bound was limited externally, record that the value lower bound is not equal to the term lower bound
267 actual_l = NodeManager::currentNM()->mkNode( MINUS, d_bounds[b][ii], l );
268 }
269 l = d_bounds[b][ii];
270 }else if( b==1 && (u.isNull() || d_bounds[b][ii].getConst<Rational>() <= u.getConst<Rational>()) ){
271 u = NodeManager::currentNM()->mkNode( MINUS, d_bounds[b][ii],
272 NodeManager::currentNM()->mkConst( Rational(1) ) );
273 u = Rewriter::rewrite( u );
274 }
275 }
276 }
277
278 if( l.isNull() || u.isNull() ){
279 //failed, abort the iterator
280 d_index.clear();
281 return false;
282 }else{
283 Trace("bound-int-rsi") << "Can limit bounds of " << d_owner[0][ii] << " to " << l << "..." << u << std::endl;
284 Node range = Rewriter::rewrite( NodeManager::currentNM()->mkNode( MINUS, u, l ) );
285 Node ra = Rewriter::rewrite( NodeManager::currentNM()->mkNode( LEQ, range, NodeManager::currentNM()->mkConst( Rational( 9999 ) ) ) );
286 d_domain[ii].clear();
287 Node tl = l;
288 Node tu = u;
289 if( d_qe->getBoundedIntegers() && d_qe->getBoundedIntegers()->isBoundVar( d_owner, d_owner[0][ii] ) ){
290 d_qe->getBoundedIntegers()->getBounds( d_owner, d_owner[0][ii], this, tl, tu );
291 }
292 d_lower_bounds[ii] = tl;
293 if( !actual_l.isNull() ){
294 //if bound was limited externally, must consider the offset
295 d_lower_bounds[ii] = Rewriter::rewrite( NodeManager::currentNM()->mkNode( PLUS, tl, actual_l ) );
296 }
297 if( ra==NodeManager::currentNM()->mkConst(true) ){
298 long rr = range.getConst<Rational>().getNumerator().getLong()+1;
299 Trace("bound-int-rsi") << "Actual bound range is " << rr << std::endl;
300 d_domain[ii].push_back( (int)rr );
301 }else{
302 Trace("fmf-incomplete") << "Incomplete because of integer quantification, bounds are too big for " << d_owner[0][ii] << "." << std::endl;
303 d_incomplete = true;
304 d_domain[ii].push_back( 0 );
305 }
306 }
307 }else{
308 Trace("bound-int-rsi") << d_owner[0][ii] << " has ground range, skip." << std::endl;
309 }
310 }
311 return true;
312 }
313
314 int RepSetIterator::increment2( int counter ){
315 Assert( !isFinished() );
316 #ifdef DISABLE_EVAL_SKIP_MULTIPLE
317 counter = (int)d_index.size()-1;
318 #endif
319 //increment d_index
320 if( counter>=0){
321 Trace("rsi-debug") << "domain size of " << counter << " is " << domainSize(counter) << std::endl;
322 }
323 while( counter>=0 && d_index[counter]>=(int)(domainSize(counter)-1) ){
324 counter--;
325 if( counter>=0){
326 Trace("rsi-debug") << "domain size of " << counter << " is " << domainSize(counter) << std::endl;
327 }
328 }
329 if( counter==-1 ){
330 d_index.clear();
331 }else{
332 d_index[counter]++;
333 bool emptyDomain = false;
334 for( int i=(int)d_index.size()-1; i>counter; i-- ){
335 if (!resetIndex(i)){
336 break;
337 }else if( domainSize(i)<=0 ){
338 emptyDomain = true;
339 }
340 }
341 if( emptyDomain ){
342 Trace("rsi-debug") << "This is an empty domain, increment again." << std::endl;
343 return increment();
344 }
345 }
346 return counter;
347 }
348
349 int RepSetIterator::increment(){
350 if( !isFinished() ){
351 return increment2( (int)d_index.size()-1 );
352 }else{
353 return -1;
354 }
355 }
356
357 bool RepSetIterator::isFinished(){
358 return d_index.empty();
359 }
360
361 Node RepSetIterator::getTerm( int i ){
362 int index = d_index_order[i];
363 if( d_enum_type[index]==ENUM_DOMAIN_ELEMENTS ){
364 TypeNode tn = d_types[index];
365 Assert( d_rep_set->d_type_reps.find( tn )!=d_rep_set->d_type_reps.end() );
366 return d_rep_set->d_type_reps[tn][d_domain[index][d_index[index]]];
367 }else{
368 Trace("rsi-debug") << "Get, with offset : " << index << " " << d_lower_bounds[index] << " " << d_index[index] << std::endl;
369 Node t = NodeManager::currentNM()->mkNode(PLUS, d_lower_bounds[index],
370 NodeManager::currentNM()->mkConst( Rational(d_index[index]) ) );
371 t = Rewriter::rewrite( t );
372 return t;
373 }
374 }
375
376 void RepSetIterator::debugPrint( const char* c ){
377 for( int i=0; i<(int)d_index.size(); i++ ){
378 Debug( c ) << i << " : " << d_index[i] << " : " << getTerm( i ) << std::endl;
379 }
380 }
381
382 void RepSetIterator::debugPrintSmall( const char* c ){
383 Debug( c ) << "RI: ";
384 for( int i=0; i<(int)d_index.size(); i++ ){
385 Debug( c ) << d_index[i] << ": " << getTerm( i ) << " ";
386 }
387 Debug( c ) << std::endl;
388 }