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-2017 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 <unordered_set>
17 #include "theory/rep_set.h"
18 #include "theory/type_enumerator.h"
19 #include "theory/quantifiers/bounded_integers.h"
20 #include "theory/quantifiers/term_util.h"
21 #include "theory/quantifiers/first_order_model.h"
25 using namespace CVC4::kind
;
26 using namespace CVC4::context
;
27 using namespace CVC4::theory
;
31 d_type_complete
.clear();
33 d_values_to_terms
.clear();
36 bool RepSet::hasRep(TypeNode tn
, Node n
) const
38 std::map
<TypeNode
, std::vector
<Node
> >::const_iterator it
=
40 if( it
==d_type_reps
.end() ){
43 return std::find( it
->second
.begin(), it
->second
.end(), n
)!=it
->second
.end();
47 unsigned RepSet::getNumRepresentatives(TypeNode tn
) const
49 std::map
< TypeNode
, std::vector
< Node
> >::const_iterator it
= d_type_reps
.find( tn
);
50 if( it
!=d_type_reps
.end() ){
51 return it
->second
.size();
57 Node
RepSet::getRepresentative(TypeNode tn
, unsigned i
) const
59 std::map
<TypeNode
, std::vector
<Node
> >::const_iterator it
=
61 Assert(it
!= d_type_reps
.end());
62 Assert(i
< it
->second
.size());
66 bool containsStoreAll(Node n
, std::unordered_set
<Node
, NodeHashFunction
>& cache
)
68 if( std::find( cache
.begin(), cache
.end(), n
)==cache
.end() ){
70 if( n
.getKind()==STORE_ALL
){
73 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
74 if( containsStoreAll( n
[i
], cache
) ){
83 void RepSet::add( TypeNode tn
, Node n
){
84 //for now, do not add array constants FIXME
86 std::unordered_set
<Node
, NodeHashFunction
> cache
;
87 if( containsStoreAll( n
, cache
) ){
91 Trace("rsi-debug") << "Add rep #" << d_type_reps
[tn
].size() << " for " << tn
<< " : " << n
<< std::endl
;
92 Assert( n
.getType().isSubtypeOf( tn
) );
93 d_tmap
[ n
] = (int)d_type_reps
[tn
].size();
94 d_type_reps
[tn
].push_back( n
);
97 int RepSet::getIndexFor( Node n
) const {
98 std::map
< Node
, int >::const_iterator it
= d_tmap
.find( n
);
99 if( it
!=d_tmap
.end() ){
106 bool RepSet::complete( TypeNode t
){
107 std::map
< TypeNode
, bool >::iterator it
= d_type_complete
.find( t
);
108 if( it
==d_type_complete
.end() ){
109 //remove all previous
110 for( unsigned i
=0; i
<d_type_reps
[t
].size(); i
++ ){
111 d_tmap
.erase( d_type_reps
[t
][i
] );
113 d_type_reps
[t
].clear();
114 //now complete the type
115 d_type_complete
[t
] = true;
116 TypeEnumerator
te(t
);
117 while( !te
.isFinished() ){
119 if( std::find( d_type_reps
[t
].begin(), d_type_reps
[t
].end(), n
)==d_type_reps
[t
].end() ){
124 for( size_t i
=0; i
<d_type_reps
[t
].size(); i
++ ){
125 Trace("reps-complete") << d_type_reps
[t
][i
] << " ";
127 Trace("reps-complete") << std::endl
;
134 Node
RepSet::getTermForRepresentative(Node n
) const
136 std::map
<Node
, Node
>::const_iterator it
= d_values_to_terms
.find(n
);
137 if (it
!= d_values_to_terms
.end())
147 void RepSet::setTermForRepresentative(Node n
, Node t
)
149 d_values_to_terms
[n
] = t
;
152 Node
RepSet::getDomainValue(TypeNode tn
, const std::vector
<Node
>& exclude
) const
154 std::map
<TypeNode
, std::vector
<Node
> >::const_iterator it
=
155 d_type_reps
.find(tn
);
156 if (it
!= d_type_reps
.end())
158 // try to find a pre-existing arbitrary element
159 for (size_t i
= 0; i
< it
->second
.size(); i
++)
161 if (std::find(exclude
.begin(), exclude
.end(), it
->second
[i
])
164 return it
->second
[i
];
171 void RepSet::toStream(std::ostream
& out
){
172 for( std::map
< TypeNode
, std::vector
< Node
> >::iterator it
= d_type_reps
.begin(); it
!= d_type_reps
.end(); ++it
){
173 if( !it
->first
.isFunction() && !it
->first
.isPredicate() ){
174 out
<< "(" << it
->first
<< " " << it
->second
.size();
176 for( unsigned i
=0; i
<it
->second
.size(); i
++ ){
177 if( i
>0 ){ out
<< " "; }
178 out
<< it
->second
[i
];
181 out
<< ")" << std::endl
;
187 RepSetIterator::RepSetIterator( QuantifiersEngine
* qe
, RepSet
* rs
) : d_qe(qe
), d_rep_set( rs
){
188 d_incomplete
= false;
191 int RepSetIterator::domainSize( int i
) {
193 int v
= d_var_order
[i
];
194 return d_domain_elements
[v
].size();
197 bool RepSetIterator::setQuantifier( Node f
, RepBoundExt
* rext
){
198 Trace("rsi") << "Make rsi for " << f
<< std::endl
;
199 Assert( d_types
.empty() );
201 for( size_t i
=0; i
<f
[0].getNumChildren(); i
++ ){
202 d_types
.push_back( f
[0][i
].getType() );
205 return initialize( rext
);
208 bool RepSetIterator::setFunctionDomain( Node op
, RepBoundExt
* rext
){
209 Trace("rsi") << "Make rsi for " << op
<< std::endl
;
210 Assert( d_types
.empty() );
211 TypeNode tn
= op
.getType();
212 for( size_t i
=0; i
<tn
.getNumChildren()-1; i
++ ){
213 d_types
.push_back( tn
[i
] );
216 return initialize( rext
);
219 bool RepSetIterator::initialize( RepBoundExt
* rext
){
220 Trace("rsi") << "Initialize rep set iterator..." << std::endl
;
221 for( unsigned v
=0; v
<d_types
.size(); v
++ ){
222 d_index
.push_back( 0 );
223 //store default index order
224 d_index_order
.push_back( v
);
226 //store default domain
227 //d_domain.push_back( RepDomain() );
228 d_domain_elements
.push_back( std::vector
< Node
>() );
229 TypeNode tn
= d_types
[v
];
230 Trace("rsi") << "Var #" << v
<< " is type " << tn
<< "..." << std::endl
;
232 //must ensure uninterpreted type is non-empty.
233 if( !d_rep_set
->hasType( tn
) ){
235 // terms in rep_set are now constants which mapped to terms through TheoryModel
236 // thus, should introduce a constant and a term. for now, just a term.
238 //Node c = d_qe->getTermUtil()->getEnumerateTerm( tn, 0 );
239 Node var
= d_qe
->getModel()->getSomeDomainElement( tn
);
240 Trace("mkVar") << "RepSetIterator:: Make variable " << var
<< " : " << tn
<< std::endl
;
241 d_rep_set
->add( tn
, var
);
245 //check if it is externally bound
246 if( rext
&& rext
->setBound( d_owner
, v
, tn
, d_domain_elements
[v
] ) ){
247 d_enum_type
.push_back( ENUM_DEFAULT
);
249 //builtin: check if it is bound by bounded integer module
250 }else if( d_owner
.getKind()==FORALL
&& d_qe
&& d_qe
->getBoundedIntegers() ){
251 if( d_qe
->getBoundedIntegers()->isBoundVar( d_owner
, d_owner
[0][v
] ) ){
252 unsigned bvt
= d_qe
->getBoundedIntegers()->getBoundVarType( d_owner
, d_owner
[0][v
] );
253 if( bvt
!=quantifiers::BoundedIntegers::BOUND_FINITE
){
254 d_enum_type
.push_back( ENUM_BOUND_INT
);
257 //will treat in default way
263 if( d_qe
->getTermUtil()->mayComplete( tn
) ){
264 Trace("rsi") << " do complete, since cardinality is small (" << tn
.getCardinality() << ")..." << std::endl
;
265 d_rep_set
->complete( tn
);
266 //must have succeeded
267 Assert( d_rep_set
->hasType( tn
) );
269 Trace("rsi") << " variable cannot be bounded." << std::endl
;
270 Trace("fmf-incomplete") << "Incomplete because of quantification of type " << tn
<< std::endl
;
276 //if we have yet to determine the type of enumeration
277 if( d_enum_type
.size()<=v
){
278 if( d_rep_set
->hasType( tn
) ){
279 d_enum_type
.push_back( ENUM_DEFAULT
);
280 for( unsigned j
=0; j
<d_rep_set
->d_type_reps
[tn
].size(); j
++ ){
281 //d_domain[v].push_back( j );
282 d_domain_elements
[v
].push_back( d_rep_set
->d_type_reps
[tn
][j
] );
285 Assert( d_incomplete
);
290 //must set a variable index order based on bounded integers
291 if( d_owner
.getKind()==FORALL
&& d_qe
&& d_qe
->getBoundedIntegers() ){
292 Trace("bound-int-rsi") << "Calculating variable order..." << std::endl
;
293 std::vector
< int > varOrder
;
294 for( unsigned i
=0; i
<d_qe
->getBoundedIntegers()->getNumBoundVars( d_owner
); i
++ ){
295 Node v
= d_qe
->getBoundedIntegers()->getBoundVar( d_owner
, i
);
296 Trace("bound-int-rsi") << " bound var #" << i
<< " is " << v
<< std::endl
;
297 varOrder
.push_back( d_qe
->getTermUtil()->getVariableNum( d_owner
, v
) );
299 for( unsigned i
=0; i
<d_owner
[0].getNumChildren(); i
++) {
300 if( !d_qe
->getBoundedIntegers()->isBoundVar(d_owner
, d_owner
[0][i
])) {
301 varOrder
.push_back(i
);
304 Trace("bound-int-rsi") << "Variable order : ";
305 for( unsigned i
=0; i
<varOrder
.size(); i
++) {
306 Trace("bound-int-rsi") << varOrder
[i
] << " ";
308 Trace("bound-int-rsi") << std::endl
;
309 std::vector
< int > indexOrder
;
310 indexOrder
.resize(varOrder
.size());
311 for( unsigned i
=0; i
<varOrder
.size(); i
++){
312 indexOrder
[varOrder
[i
]] = i
;
314 Trace("bound-int-rsi") << "Will use index order : ";
315 for( unsigned i
=0; i
<indexOrder
.size(); i
++) {
316 Trace("bound-int-rsi") << indexOrder
[i
] << " ";
318 Trace("bound-int-rsi") << std::endl
;
319 setIndexOrder( indexOrder
);
321 //now reset the indices
322 do_reset_increment( -1, true );
326 void RepSetIterator::setIndexOrder( std::vector
< int >& indexOrder
){
327 d_index_order
.clear();
328 d_index_order
.insert( d_index_order
.begin(), indexOrder
.begin(), indexOrder
.end() );
329 //make the d_var_order mapping
330 for( unsigned i
=0; i
<d_index_order
.size(); i
++ ){
331 d_var_order
[d_index_order
[i
]] = i
;
335 int RepSetIterator::resetIndex( int i
, bool initial
) {
337 int v
= d_var_order
[i
];
338 Trace("bound-int-rsi") << "Reset " << i
<< ", var order = " << v
<< ", initial = " << initial
<< std::endl
;
339 if( d_enum_type
[v
]==ENUM_BOUND_INT
){
340 Assert( d_owner
.getKind()==FORALL
);
341 if( !d_qe
->getBoundedIntegers()->getBoundElements( this, initial
, d_owner
, d_owner
[0][v
], d_domain_elements
[v
] ) ){
345 return d_domain_elements
[v
].empty() ? 0 : 1;
348 int RepSetIterator::increment2( int i
){
349 Assert( !isFinished() );
350 #ifdef DISABLE_EVAL_SKIP_MULTIPLE
351 i
= (int)d_index
.size()-1;
355 Trace("rsi-debug") << "domain size of " << i
<< " is " << domainSize(i
) << std::endl
;
357 while( i
>=0 && d_index
[i
]>=(int)(domainSize(i
)-1) ){
360 Trace("rsi-debug") << "domain size of " << i
<< " is " << domainSize(i
) << std::endl
;
367 Trace("rsi-debug") << "increment " << i
<< std::endl
;
369 return do_reset_increment( i
);
373 int RepSetIterator::do_reset_increment( int i
, bool initial
) {
374 bool emptyDomain
= false;
375 for( unsigned ii
=(i
+1); ii
<d_index
.size(); ii
++ ){
376 int ri_res
= resetIndex( ii
, initial
);
382 }else if( ri_res
==0 ){
385 //force next iteration if currently an empty domain
387 d_index
[ii
] = domainSize(ii
)-1;
391 Trace("rsi-debug") << "This is an empty domain, increment." << std::endl
;
398 int RepSetIterator::increment(){
400 return increment2( (int)d_index
.size()-1 );
406 bool RepSetIterator::isFinished(){
407 return d_index
.empty();
410 Node
RepSetIterator::getCurrentTerm( int v
){
411 int ii
= d_index_order
[v
];
412 int curr
= d_index
[ii
];
413 Trace("rsi-debug") << "rsi : get term " << v
<< ", index order = " << d_index_order
[v
] << std::endl
;
414 Trace("rsi-debug") << "rsi : curr = " << curr
<< " / " << d_domain_elements
[v
].size() << std::endl
;
415 Assert( 0<=curr
&& curr
<(int)d_domain_elements
[v
].size() );
416 return d_domain_elements
[v
][curr
];
419 void RepSetIterator::debugPrint( const char* c
){
420 for( unsigned v
=0; v
<d_index
.size(); v
++ ){
421 Debug( c
) << v
<< " : " << getCurrentTerm( v
) << std::endl
;
425 void RepSetIterator::debugPrintSmall( const char* c
){
426 Debug( c
) << "RI: ";
427 for( unsigned v
=0; v
<d_index
.size(); v
++ ){
428 Debug( c
) << v
<< ": " << getCurrentTerm( v
) << " ";
430 Debug( c
) << std::endl
;