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"
21 using namespace CVC4::kind
;
28 d_type_complete
.clear();
30 d_values_to_terms
.clear();
33 bool RepSet::hasRep(TypeNode tn
, Node n
) const
35 std::map
<TypeNode
, std::vector
<Node
> >::const_iterator it
=
37 if( it
==d_type_reps
.end() ){
40 return std::find( it
->second
.begin(), it
->second
.end(), n
)!=it
->second
.end();
44 unsigned RepSet::getNumRepresentatives(TypeNode tn
) const
46 std::map
< TypeNode
, std::vector
< Node
> >::const_iterator it
= d_type_reps
.find( tn
);
47 if( it
!=d_type_reps
.end() ){
48 return it
->second
.size();
54 Node
RepSet::getRepresentative(TypeNode tn
, unsigned i
) const
56 std::map
<TypeNode
, std::vector
<Node
> >::const_iterator it
=
58 Assert(it
!= d_type_reps
.end());
59 Assert(i
< it
->second
.size());
63 void RepSet::getRepresentatives(TypeNode tn
, std::vector
<Node
>& reps
) const
65 std::map
<TypeNode
, std::vector
<Node
> >::const_iterator it
=
67 Assert(it
!= d_type_reps
.end());
68 reps
.insert(reps
.end(), it
->second
.begin(), it
->second
.end());
71 bool containsStoreAll(Node n
, std::unordered_set
<Node
, NodeHashFunction
>& cache
)
73 if( std::find( cache
.begin(), cache
.end(), n
)==cache
.end() ){
75 if( n
.getKind()==STORE_ALL
){
78 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
79 if( containsStoreAll( n
[i
], cache
) ){
88 void RepSet::add( TypeNode tn
, Node n
){
89 //for now, do not add array constants FIXME
91 std::unordered_set
<Node
, NodeHashFunction
> cache
;
92 if( containsStoreAll( n
, cache
) ){
96 Trace("rsi-debug") << "Add rep #" << d_type_reps
[tn
].size() << " for " << tn
<< " : " << n
<< std::endl
;
97 Assert( n
.getType().isSubtypeOf( tn
) );
98 d_tmap
[ n
] = (int)d_type_reps
[tn
].size();
99 d_type_reps
[tn
].push_back( n
);
102 int RepSet::getIndexFor( Node n
) const {
103 std::map
< Node
, int >::const_iterator it
= d_tmap
.find( n
);
104 if( it
!=d_tmap
.end() ){
111 bool RepSet::complete( TypeNode t
){
112 std::map
< TypeNode
, bool >::iterator it
= d_type_complete
.find( t
);
113 if( it
==d_type_complete
.end() ){
114 //remove all previous
115 for( unsigned i
=0; i
<d_type_reps
[t
].size(); i
++ ){
116 d_tmap
.erase( d_type_reps
[t
][i
] );
118 d_type_reps
[t
].clear();
119 //now complete the type
120 d_type_complete
[t
] = true;
121 TypeEnumerator
te(t
);
122 while( !te
.isFinished() ){
124 if( std::find( d_type_reps
[t
].begin(), d_type_reps
[t
].end(), n
)==d_type_reps
[t
].end() ){
129 for( size_t i
=0; i
<d_type_reps
[t
].size(); i
++ ){
130 Trace("reps-complete") << d_type_reps
[t
][i
] << " ";
132 Trace("reps-complete") << std::endl
;
139 Node
RepSet::getTermForRepresentative(Node n
) const
141 std::map
<Node
, Node
>::const_iterator it
= d_values_to_terms
.find(n
);
142 if (it
!= d_values_to_terms
.end())
152 void RepSet::setTermForRepresentative(Node n
, Node t
)
154 d_values_to_terms
[n
] = t
;
157 Node
RepSet::getDomainValue(TypeNode tn
, const std::vector
<Node
>& exclude
) const
159 std::map
<TypeNode
, std::vector
<Node
> >::const_iterator it
=
160 d_type_reps
.find(tn
);
161 if (it
!= d_type_reps
.end())
163 // try to find a pre-existing arbitrary element
164 for (size_t i
= 0; i
< it
->second
.size(); i
++)
166 if (std::find(exclude
.begin(), exclude
.end(), it
->second
[i
])
169 return it
->second
[i
];
176 void RepSet::toStream(std::ostream
& out
){
177 for( std::map
< TypeNode
, std::vector
< Node
> >::iterator it
= d_type_reps
.begin(); it
!= d_type_reps
.end(); ++it
){
178 if( !it
->first
.isFunction() && !it
->first
.isPredicate() ){
179 out
<< "(" << it
->first
<< " " << it
->second
.size();
181 for( unsigned i
=0; i
<it
->second
.size(); i
++ ){
182 if( i
>0 ){ out
<< " "; }
183 out
<< it
->second
[i
];
186 out
<< ")" << std::endl
;
191 RepSetIterator::RepSetIterator(const RepSet
* rs
, RepBoundExt
* rext
)
192 : d_rs(rs
), d_rext(rext
), d_incomplete(false)
196 unsigned RepSetIterator::domainSize(unsigned i
)
198 unsigned v
= d_var_order
[i
];
199 return d_domain_elements
[v
].size();
202 bool RepSetIterator::setQuantifier(Node q
)
204 Trace("rsi") << "Make rsi for quantified formula " << q
<< std::endl
;
205 Assert( d_types
.empty() );
207 for (size_t i
= 0; i
< q
[0].getNumChildren(); i
++)
209 d_types
.push_back(q
[0][i
].getType());
215 bool RepSetIterator::setFunctionDomain(Node op
)
217 Trace("rsi") << "Make rsi for function " << op
<< std::endl
;
218 Assert( d_types
.empty() );
219 TypeNode tn
= op
.getType();
220 for( size_t i
=0; i
<tn
.getNumChildren()-1; i
++ ){
221 d_types
.push_back( tn
[i
] );
227 bool RepSetIterator::initialize()
229 Trace("rsi") << "Initialize rep set iterator..." << std::endl
;
230 for( unsigned v
=0; v
<d_types
.size(); v
++ ){
231 d_index
.push_back( 0 );
232 //store default index order
233 d_index_order
.push_back( v
);
235 //store default domain
236 //d_domain.push_back( RepDomain() );
237 d_domain_elements
.push_back( std::vector
< Node
>() );
238 TypeNode tn
= d_types
[v
];
239 Trace("rsi") << "Var #" << v
<< " is type " << tn
<< "..." << std::endl
;
241 bool setEnum
= false;
242 //check if it is externally bound
245 inc
= !d_rext
->initializeRepresentativesForType(tn
);
246 RsiEnumType rsiet
= d_rext
->setBound(d_owner
, v
, d_domain_elements
[v
]);
247 if (rsiet
!= ENUM_INVALID
)
249 d_enum_type
.push_back(rsiet
);
256 Trace("fmf-incomplete") << "Incomplete because of quantification of type "
261 //if we have yet to determine the type of enumeration
264 if (d_rs
->hasType(tn
))
266 d_enum_type
.push_back( ENUM_DEFAULT
);
267 d_rs
->getRepresentatives(tn
, d_domain_elements
[v
]);
269 Assert( d_incomplete
);
277 std::vector
<unsigned> varOrder
;
278 if (d_rext
->getVariableOrder(d_owner
, varOrder
))
280 if (Trace
.isOn("bound-int-rsi"))
282 Trace("bound-int-rsi") << "Variable order : ";
283 for (unsigned i
= 0; i
< varOrder
.size(); i
++)
285 Trace("bound-int-rsi") << varOrder
[i
] << " ";
287 Trace("bound-int-rsi") << std::endl
;
289 std::vector
<unsigned> indexOrder
;
290 indexOrder
.resize(varOrder
.size());
291 for (unsigned i
= 0; i
< varOrder
.size(); i
++)
293 Assert(varOrder
[i
] < indexOrder
.size());
294 indexOrder
[varOrder
[i
]] = i
;
296 if (Trace
.isOn("bound-int-rsi"))
298 Trace("bound-int-rsi") << "Will use index order : ";
299 for (unsigned i
= 0; i
< indexOrder
.size(); i
++)
301 Trace("bound-int-rsi") << indexOrder
[i
] << " ";
303 Trace("bound-int-rsi") << std::endl
;
305 setIndexOrder(indexOrder
);
308 //now reset the indices
309 do_reset_increment( -1, true );
313 void RepSetIterator::setIndexOrder(std::vector
<unsigned>& indexOrder
)
315 d_index_order
.clear();
316 d_index_order
.insert( d_index_order
.begin(), indexOrder
.begin(), indexOrder
.end() );
317 //make the d_var_order mapping
318 for( unsigned i
=0; i
<d_index_order
.size(); i
++ ){
319 d_var_order
[d_index_order
[i
]] = i
;
323 int RepSetIterator::resetIndex(unsigned i
, bool initial
)
326 unsigned v
= d_var_order
[i
];
327 Trace("bound-int-rsi") << "Reset " << i
<< ", var order = " << v
<< ", initial = " << initial
<< std::endl
;
330 if (!d_rext
->resetIndex(this, d_owner
, v
, initial
, d_domain_elements
[v
]))
335 return d_domain_elements
[v
].empty() ? 0 : 1;
338 int RepSetIterator::incrementAtIndex(int i
)
340 Assert( !isFinished() );
341 #ifdef DISABLE_EVAL_SKIP_MULTIPLE
342 i
= (int)d_index
.size()-1;
346 Trace("rsi-debug") << "domain size of " << i
<< " is " << domainSize(i
) << std::endl
;
348 while( i
>=0 && d_index
[i
]>=(int)(domainSize(i
)-1) ){
351 Trace("rsi-debug") << "domain size of " << i
<< " is " << domainSize(i
) << std::endl
;
358 Trace("rsi-debug") << "increment " << i
<< std::endl
;
360 return do_reset_increment( i
);
364 int RepSetIterator::do_reset_increment( int i
, bool initial
) {
365 bool emptyDomain
= false;
366 for( unsigned ii
=(i
+1); ii
<d_index
.size(); ii
++ ){
367 int ri_res
= resetIndex( ii
, initial
);
373 }else if( ri_res
==0 ){
376 //force next iteration if currently an empty domain
378 d_index
[ii
] = domainSize(ii
)-1;
382 Trace("rsi-debug") << "This is an empty domain, increment." << std::endl
;
389 int RepSetIterator::increment(){
391 return incrementAtIndex(d_index
.size() - 1);
397 bool RepSetIterator::isFinished() const { return d_index
.empty(); }
399 Node
RepSetIterator::getCurrentTerm(unsigned v
, bool valTerm
)
401 unsigned ii
= d_index_order
[v
];
402 unsigned curr
= d_index
[ii
];
403 Trace("rsi-debug") << "rsi : get term " << v
<< ", index order = " << d_index_order
[v
] << std::endl
;
404 Trace("rsi-debug") << "rsi : curr = " << curr
<< " / " << d_domain_elements
[v
].size() << std::endl
;
405 Assert(0 <= curr
&& curr
< d_domain_elements
[v
].size());
406 Node t
= d_domain_elements
[v
][curr
];
409 Node tt
= d_rs
->getTermForRepresentative(t
);
418 void RepSetIterator::debugPrint( const char* c
){
419 for( unsigned v
=0; v
<d_index
.size(); v
++ ){
420 Debug( c
) << v
<< " : " << getCurrentTerm( v
) << std::endl
;
424 void RepSetIterator::debugPrintSmall( const char* c
){
425 Debug( c
) << "RI: ";
426 for( unsigned v
=0; v
<d_index
.size(); v
++ ){
427 Debug( c
) << v
<< ": " << getCurrentTerm( v
) << " ";
429 Debug( c
) << std::endl
;
432 } /* CVC4::theory namespace */
433 } /* CVC4 namespace */