1 /********************* */
2 /*! \file conjecture_generator.cpp
4 ** Original author: Andrew Reynolds
5 ** Major contributors: none
6 ** Minor contributors (to current version): none
7 ** This file is part of the CVC4 project.
8 ** Copyright (c) 2009-2014 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 conjecture generator class
16 #include "theory/quantifiers/conjecture_generator.h"
17 #include "theory/theory_engine.h"
18 #include "theory/quantifiers/options.h"
19 #include "theory/quantifiers/term_database.h"
20 #include "theory/quantifiers/trigger.h"
21 #include "theory/quantifiers/first_order_model.h"
24 using namespace CVC4::kind
;
25 using namespace CVC4::theory
;
26 using namespace CVC4::theory::quantifiers
;
32 void OpArgIndex::addTerm( ConjectureGenerator
* s
, TNode n
, unsigned index
){
33 if( index
==n
.getNumChildren() ){
34 Assert( n
.hasOperator() );
35 if( std::find( d_ops
.begin(), d_ops
.end(), n
.getOperator() )==d_ops
.end() ){
36 d_ops
.push_back( n
.getOperator() );
37 d_op_terms
.push_back( n
);
40 d_child
[s
->getTermDatabase()->d_arg_reps
[n
][index
]].addTerm( s
, n
, index
+1 );
44 Node
OpArgIndex::getGroundTerm( ConjectureGenerator
* s
, std::vector
< TNode
>& args
) {
46 for( std::map
< TNode
, OpArgIndex
>::iterator it
= d_child
.begin(); it
!= d_child
.end(); ++it
){
47 std::map
< TNode
, Node
>::iterator itf
= s
->d_ground_eqc_map
.find( it
->first
);
48 if( itf
!=s
->d_ground_eqc_map
.end() ){
49 args
.push_back( itf
->second
);
50 Node n
= it
->second
.getGroundTerm( s
, args
);
59 std::vector
< TNode
> args2
;
60 args2
.push_back( d_ops
[0] );
61 args2
.insert( args2
.end(), args
.begin(), args
.end() );
62 return NodeManager::currentNM()->mkNode( d_op_terms
[0].getKind(), args2
);
66 void OpArgIndex::getGroundTerms( ConjectureGenerator
* s
, std::vector
< TNode
>& terms
) {
67 terms
.insert( terms
.end(), d_op_terms
.begin(), d_op_terms
.end() );
68 for( std::map
< TNode
, OpArgIndex
>::iterator it
= d_child
.begin(); it
!= d_child
.end(); ++it
){
69 if( s
->isGroundEqc( it
->first
) ){
70 it
->second
.getGroundTerms( s
, terms
);
77 ConjectureGenerator::ConjectureGenerator( QuantifiersEngine
* qe
, context::Context
* c
) : QuantifiersModule( qe
),
79 d_uequalityEngine(d_notify
, c
, "ConjectureGenerator::ee"),
80 d_ee_conjectures( c
){
81 d_fullEffortCount
= 0;
82 d_uequalityEngine
.addFunctionKind( kind::APPLY_UF
);
83 d_uequalityEngine
.addFunctionKind( kind::APPLY_CONSTRUCTOR
);
87 void ConjectureGenerator::eqNotifyNewClass( TNode t
){
88 Trace("thm-ee-debug") << "UEE : new equivalence class " << t
<< std::endl
;
89 d_upendingAdds
.push_back( t
);
92 void ConjectureGenerator::eqNotifyPreMerge(TNode t1
, TNode t2
) {
93 //get maintained representatives
96 std::map
< Node
, EqcInfo
* >::iterator it1
= d_eqc_info
.find( t1
);
97 if( it1
!=d_eqc_info
.end() && !it1
->second
->d_rep
.get().isNull() ){
98 rt1
= it1
->second
->d_rep
.get();
100 std::map
< Node
, EqcInfo
* >::iterator it2
= d_eqc_info
.find( t2
);
101 if( it2
!=d_eqc_info
.end() && !it2
->second
->d_rep
.get().isNull() ){
102 rt2
= it2
->second
->d_rep
.get();
104 Trace("thm-ee-debug") << "UEE : equality holds : " << t1
<< " == " << t2
<< std::endl
;
105 Trace("thm-ee-debug") << " ureps : " << rt1
<< " == " << rt2
<< std::endl
;
106 Trace("thm-ee-debug") << " normal : " << d_pattern_is_normal
[rt1
] << " " << d_pattern_is_normal
[rt2
] << std::endl
;
107 Trace("thm-ee-debug") << " size : " << d_pattern_fun_sum
[rt1
] << " " << d_pattern_fun_sum
[rt2
] << std::endl
;
109 if( isUniversalLessThan( rt2
, rt1
) ){
111 if( it1
==d_eqc_info
.end() ){
112 ei
= getOrMakeEqcInfo( t1
, true );
120 void ConjectureGenerator::eqNotifyPostMerge(TNode t1
, TNode t2
) {
124 void ConjectureGenerator::eqNotifyDisequal(TNode t1
, TNode t2
, TNode reason
) {
125 Trace("thm-ee-debug") << "UEE : disequality holds : " << t1
<< " != " << t2
<< std::endl
;
130 ConjectureGenerator::EqcInfo::EqcInfo( context::Context
* c
) : d_rep( c
, Node::null() ){
134 ConjectureGenerator::EqcInfo
* ConjectureGenerator::getOrMakeEqcInfo( TNode n
, bool doMake
) {
135 //Assert( getUniversalRepresentative( n )==n );
136 std::map
< Node
, EqcInfo
* >::iterator eqc_i
= d_eqc_info
.find( n
);
137 if( eqc_i
!=d_eqc_info
.end() ){
138 return eqc_i
->second
;
140 EqcInfo
* ei
= new EqcInfo( d_quantEngine
->getSatContext() );
148 void ConjectureGenerator::doPendingAddUniversalTerms() {
149 //merge all pending equalities
150 while( !d_upendingAdds
.empty() ){
151 Trace("sg-pending") << "Add " << d_upendingAdds
.size() << " pending terms..." << std::endl
;
152 std::vector
< Node
> pending
;
153 pending
.insert( pending
.end(), d_upendingAdds
.begin(), d_upendingAdds
.end() );
154 d_upendingAdds
.clear();
155 for( unsigned i
=0; i
<pending
.size(); i
++ ){
157 TypeNode tn
= t
.getType();
158 Trace("thm-ee-add") << "UEE : Add universal term " << t
<< std::endl
;
159 //get all equivalent terms based on theorem database
160 std::vector
< Node
> eq_terms
;
161 d_thm_index
.getEquivalentTerms( t
, eq_terms
);
162 if( !eq_terms
.empty() ){
163 Trace("thm-ee-add") << "UEE : Based on theorem database, it is equivalent to " << eq_terms
.size() << " terms : " << std::endl
;
164 //add equivalent terms as equalities to universal engine
165 for( unsigned i
=0; i
<eq_terms
.size(); i
++ ){
166 Trace("thm-ee-add") << " " << eq_terms
[i
] << std::endl
;
167 //if( d_urelevant_terms.find( eq_terms[i] )!=d_urelevant_terms.end() ){
168 bool assertEq
= false;
169 if( d_urelevant_terms
.find( eq_terms
[i
] )!=d_urelevant_terms
.end() ){
172 Assert( eq_terms
[i
].getType()==tn
);
173 registerPattern( eq_terms
[i
], tn
);
174 if( isUniversalLessThan( eq_terms
[i
], t
) ){
175 setUniversalRelevant( eq_terms
[i
] );
181 d_uequalityEngine
.assertEquality( t
.eqNode( eq_terms
[i
] ), true, exp
);
185 Trace("thm-ee-add") << "UEE : No equivalent terms." << std::endl
;
187 //if occurs at ground level, merge with representative of ground equality engine
189 eq::EqualityEngine * ee = getEqualityEngine();
190 if( ee->hasTerm( t ) ){
191 TNode grt = ee->getRepresentative( t );
194 d_uequalityEngine.assertEquality( t.eqNode( grt ), true, exp );
202 void ConjectureGenerator::setUniversalRelevant( TNode n
) {
203 //add pattern information
204 registerPattern( n
, n
.getType() );
205 d_urelevant_terms
[n
] = true;
206 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
207 setUniversalRelevant( n
[i
] );
211 bool ConjectureGenerator::isUniversalLessThan( TNode rt1
, TNode rt2
) {
212 //prefer the one that is (normal, smaller) lexographically
213 Assert( d_pattern_is_normal
.find( rt1
)!=d_pattern_is_normal
.end() );
214 Assert( d_pattern_is_normal
.find( rt2
)!=d_pattern_is_normal
.end() );
215 Assert( d_pattern_fun_sum
.find( rt1
)!=d_pattern_fun_sum
.end() );
216 Assert( d_pattern_fun_sum
.find( rt2
)!=d_pattern_fun_sum
.end() );
218 if( d_pattern_is_normal
[rt1
] && !d_pattern_is_normal
[rt2
] ){
219 Trace("thm-ee-debug") << "UEE : LT due to normal." << std::endl
;
221 }else if( d_pattern_is_normal
[rt1
]==d_pattern_is_normal
[rt2
] ){
222 if( d_pattern_fun_sum
[rt1
]<d_pattern_fun_sum
[rt2
] ){
223 Trace("thm-ee-debug") << "UEE : LT due to size." << std::endl
;
224 //decide which representative to use : based on size of the term
226 }else if( d_pattern_fun_sum
[rt1
]==d_pattern_fun_sum
[rt2
] ){
227 //same size : tie goes to term that has already been reported
228 return isReportedCanon( rt1
) && !isReportedCanon( rt2
);
235 bool ConjectureGenerator::isReportedCanon( TNode n
) {
236 return std::find( d_ue_canon
.begin(), d_ue_canon
.end(), n
)==d_ue_canon
.end();
239 void ConjectureGenerator::markReportedCanon( TNode n
) {
240 if( !isReportedCanon( n
) ){
241 d_ue_canon
.push_back( n
);
245 bool ConjectureGenerator::areUniversalEqual( TNode n1
, TNode n2
) {
246 return n1
==n2
|| ( d_uequalityEngine
.hasTerm( n1
) && d_uequalityEngine
.hasTerm( n2
) && d_uequalityEngine
.areEqual( n1
, n2
) );
249 bool ConjectureGenerator::areUniversalDisequal( TNode n1
, TNode n2
) {
250 return n1
!=n2
&& d_uequalityEngine
.hasTerm( n1
) && d_uequalityEngine
.hasTerm( n2
) && d_uequalityEngine
.areDisequal( n1
, n2
, false );
253 TNode
ConjectureGenerator::getUniversalRepresentative( TNode n
, bool add
) {
255 if( d_urelevant_terms
.find( n
)==d_urelevant_terms
.end() ){
256 setUniversalRelevant( n
);
257 //add term to universal equality engine
258 d_uequalityEngine
.addTerm( n
);
259 Trace("thm-ee-debug") << "Merge equivalence classes based on terms..." << std::endl
;
260 doPendingAddUniversalTerms();
263 if( d_uequalityEngine
.hasTerm( n
) ){
264 Node r
= d_uequalityEngine
.getRepresentative( n
);
265 EqcInfo
* ei
= getOrMakeEqcInfo( r
);
266 if( ei
&& !ei
->d_rep
.get().isNull() ){
267 return ei
->d_rep
.get();
276 eq::EqualityEngine
* ConjectureGenerator::getEqualityEngine() {
277 return d_quantEngine
->getTheoryEngine()->getMasterEqualityEngine();
280 bool ConjectureGenerator::areEqual( TNode n1
, TNode n2
) {
281 eq::EqualityEngine
* ee
= getEqualityEngine();
282 return n1
==n2
|| ( ee
->hasTerm( n1
) && ee
->hasTerm( n2
) && ee
->areEqual( n1
, n2
) );
285 bool ConjectureGenerator::areDisequal( TNode n1
, TNode n2
) {
286 eq::EqualityEngine
* ee
= getEqualityEngine();
287 return n1
!=n2
&& ee
->hasTerm( n1
) && ee
->hasTerm( n2
) && ee
->areDisequal( n1
, n2
, false );
290 TNode
ConjectureGenerator::getRepresentative( TNode n
) {
291 eq::EqualityEngine
* ee
= getEqualityEngine();
292 if( ee
->hasTerm( n
) ){
293 return ee
->getRepresentative( n
);
299 TermDb
* ConjectureGenerator::getTermDatabase() {
300 return d_quantEngine
->getTermDatabase();
303 Node
ConjectureGenerator::getFreeVar( TypeNode tn
, unsigned i
) {
304 Assert( !tn
.isNull() );
305 while( d_free_var
[tn
].size()<=i
){
306 std::stringstream oss
;
308 std::stringstream os
;
309 os
<< oss
.str()[0] << i
;
310 Node x
= NodeManager::currentNM()->mkBoundVar( os
.str().c_str(), tn
);
311 d_free_var_num
[x
] = d_free_var
[tn
].size();
312 d_free_var
[tn
].push_back( x
);
314 return d_free_var
[tn
][i
];
319 Node
ConjectureGenerator::getCanonicalTerm( TNode n
, std::map
< TypeNode
, unsigned >& var_count
, std::map
< TNode
, TNode
>& subs
) {
320 Trace("ajr-temp") << "get canonical term " << n
<< " " << n
.getKind() << " " << n
.hasOperator() << std::endl
;
321 if( n
.getKind()==BOUND_VARIABLE
){
322 std::map
< TNode
, TNode
>::iterator it
= subs
.find( n
);
323 if( it
==subs
.end() ){
324 TypeNode tn
= n
.getType();
326 unsigned vn
= var_count
[tn
];
328 subs
[n
] = getFreeVar( tn
, vn
);
334 std::vector
< Node
> children
;
335 if( n
.getKind()!=EQUAL
){
336 if( n
.hasOperator() ){
337 TNode op
= n
.getOperator();
338 if( std::find( d_funcs
.begin(), d_funcs
.end(), op
)==d_funcs
.end() ){
341 children
.push_back( op
);
346 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
347 Node cn
= getCanonicalTerm( n
[i
], var_count
, subs
);
351 children
.push_back( cn
);
354 return NodeManager::currentNM()->mkNode( n
.getKind(), children
);
358 bool ConjectureGenerator::isHandledTerm( TNode n
){
359 return !n
.getAttribute(NoMatchAttribute()) && inst::Trigger::isAtomicTrigger( n
) && ( n
.getKind()!=APPLY_UF
|| n
.getOperator().getKind()!=SKOLEM
);
362 Node
ConjectureGenerator::getGroundEqc( TNode r
) {
363 std::map
< TNode
, Node
>::iterator it
= d_ground_eqc_map
.find( r
);
364 return it
!=d_ground_eqc_map
.end() ? it
->second
: Node::null();
367 bool ConjectureGenerator::isGroundEqc( TNode r
) {
368 return d_ground_eqc_map
.find( r
)!=d_ground_eqc_map
.end();
371 bool ConjectureGenerator::isGroundTerm( TNode n
) {
372 return std::find( d_ground_terms
.begin(), d_ground_terms
.end(), n
)!=d_ground_terms
.end();
375 bool ConjectureGenerator::needsCheck( Theory::Effort e
) {
376 return e
==Theory::EFFORT_FULL
;
379 void ConjectureGenerator::reset_round( Theory::Effort e
) {
383 void ConjectureGenerator::check( Theory::Effort e
, unsigned quant_e
) {
384 if( quant_e
==QuantifiersEngine::QEFFORT_STANDARD
){
386 if( d_fullEffortCount
%optFullCheckFrequency()==0 ){
387 Trace("sg-engine") << "---Conjecture generator, effort = " << e
<< "--- " << std::endl
;
388 eq::EqualityEngine
* ee
= getEqualityEngine();
390 Trace("sg-proc") << "Get eq classes..." << std::endl
;
391 d_op_arg_index
.clear();
392 d_ground_eqc_map
.clear();
393 d_bool_eqc
[0] = Node::null();
394 d_bool_eqc
[1] = Node::null();
395 std::vector
< TNode
> eqcs
;
397 eq::EqClassesIterator eqcs_i
= eq::EqClassesIterator( ee
);
398 while( !eqcs_i
.isFinished() ){
401 if( r
.getType().isBoolean() ){
402 if( areEqual( r
, getTermDatabase()->d_true
) ){
403 d_ground_eqc_map
[r
] = getTermDatabase()->d_true
;
405 }else if( areEqual( r
, getTermDatabase()->d_false
) ){
406 d_ground_eqc_map
[r
] = getTermDatabase()->d_false
;
410 d_em
[r
] = eqcs
.size();
411 eq::EqClassIterator ieqc_i
= eq::EqClassIterator( r
, ee
);
412 while( !ieqc_i
.isFinished() ){
414 if( isHandledTerm( n
) ){
415 d_op_arg_index
[r
].addTerm( this, n
);
421 Assert( !d_bool_eqc
[0].isNull() );
422 Assert( !d_bool_eqc
[1].isNull() );
423 d_urelevant_terms
.clear();
424 Trace("sg-proc") << "...done get eq classes" << std::endl
;
426 Trace("sg-proc") << "Determine ground EQC..." << std::endl
;
430 for( unsigned i
=0; i
<eqcs
.size(); i
++ ){
432 if( d_ground_eqc_map
.find( r
)==d_ground_eqc_map
.end() ){
433 std::vector
< TNode
> args
;
434 Trace("sg-pat-debug") << "******* Get ground term for " << r
<< std::endl
;
436 if( getTermDatabase()->isInductionTerm( r
) ){
437 n
= d_op_arg_index
[r
].getGroundTerm( this, args
);
442 Trace("sg-pat") << "Ground term for eqc " << r
<< " : " << std::endl
;
443 Trace("sg-pat") << " " << n
<< std::endl
;
444 d_ground_eqc_map
[r
] = n
;
447 Trace("sg-pat-debug") << "...could not find ground term." << std::endl
;
452 //also get ground terms
453 d_ground_terms
.clear();
454 for( unsigned i
=0; i
<eqcs
.size(); i
++ ){
456 d_op_arg_index
[r
].getGroundTerms( this, d_ground_terms
);
458 Trace("sg-proc") << "...done determine ground EQC" << std::endl
;
461 if( Trace
.isOn("sg-gen-eqc") ){
462 for( unsigned i
=0; i
<eqcs
.size(); i
++ ){
465 bool firstTime
= true;
466 bool isFalse
= areEqual( r
, getTermDatabase()->d_false
);
467 eq::EqClassIterator eqc_i
= eq::EqClassIterator( r
, ee
);
468 while( !eqc_i
.isFinished() ){
470 if( !n
.getAttribute(NoMatchAttribute()) && ( n
.getKind()!=EQUAL
|| isFalse
) ){
472 Trace("sg-gen-eqc") << "e" << d_em
[r
] << " : { " << std::endl
;
475 if( n
.hasOperator() ){
476 Trace("sg-gen-eqc") << " (" << n
.getOperator();
477 getTermDatabase()->computeArgReps( n
);
478 for( unsigned i
=0; i
<getTermDatabase()->d_arg_reps
[n
].size(); i
++ ){
479 Trace("sg-gen-eqc") << " e" << d_em
[getTermDatabase()->d_arg_reps
[n
][i
]];
481 Trace("sg-gen-eqc") << ") :: " << n
<< std::endl
;
483 Trace("sg-gen-eqc") << " " << n
<< std::endl
;
489 Trace("sg-gen-eqc") << "}" << std::endl
;
490 //print out ground term
491 std::map
< TNode
, Node
>::iterator it
= d_ground_eqc_map
.find( r
);
492 if( it
!=d_ground_eqc_map
.end() ){
493 Trace("sg-gen-eqc") << "- Ground term : " << it
->second
<< std::endl
;
499 Trace("sg-proc") << "Compute relevant eqc..." << std::endl
;
500 d_relevant_eqc
[0].clear();
501 d_relevant_eqc
[1].clear();
502 for( unsigned i
=0; i
<eqcs
.size(); i
++ ){
504 std::map
< TNode
, Node
>::iterator it
= d_ground_eqc_map
.find( r
);
506 if( it
==d_ground_eqc_map
.end() ){
509 //based on unproven conjectures? TODO
510 d_relevant_eqc
[index
].push_back( r
);
512 Trace("sg-gen-tg-debug") << "Initial relevant eqc : ";
513 for( unsigned i
=0; i
<d_relevant_eqc
[0].size(); i
++ ){
514 Trace("sg-gen-tg-debug") << "e" << d_em
[d_relevant_eqc
[0][i
]] << " ";
516 Trace("sg-gen-tg-debug") << std::endl
;
517 Trace("sg-proc") << "...done compute relevant eqc" << std::endl
;
520 Trace("sg-proc") << "Collect signature information..." << std::endl
;
526 for( std::map
< Node
, TermArgTrie
>::iterator it
= getTermDatabase()->d_func_map_trie
.begin(); it
!= getTermDatabase()->d_func_map_trie
.end(); ++it
){
527 if( !getTermDatabase()->d_op_map
[it
->first
].empty() ){
528 Node nn
= getTermDatabase()->d_op_map
[it
->first
][0];
529 if( isHandledTerm( nn
) && nn
.getKind()!=APPLY_SELECTOR_TOTAL
&& !nn
.getType().isBoolean() ){
530 d_funcs
.push_back( it
->first
);
531 d_typ_funcs
[tnull
].push_back( it
->first
);
532 d_typ_funcs
[nn
.getType()].push_back( it
->first
);
533 for( unsigned i
=0; i
<nn
.getNumChildren(); i
++ ){
534 d_func_args
[it
->first
].push_back( nn
[i
].getType() );
536 d_func_kind
[it
->first
] = nn
.getKind();
537 Trace("sg-rel-sig") << "Will enumerate function applications of : " << it
->first
<< ", #args = " << d_func_args
[it
->first
].size() << ", kind = " << nn
.getKind() << std::endl
;
538 getTermDatabase()->computeUfEqcTerms( it
->first
);
543 for( std::map
< TypeNode
, std::vector
< TNode
> >::iterator it
= d_typ_funcs
.begin(); it
!=d_typ_funcs
.end(); ++it
){
544 std::random_shuffle( it
->second
.begin(), it
->second
.end() );
545 if( it
->first
.isNull() ){
546 Trace("sg-gen-tg-debug") << "In this order : ";
547 for( unsigned i
=0; i
<it
->second
.size(); i
++ ){
548 Trace("sg-gen-tg-debug") << it
->second
[i
] << " ";
550 Trace("sg-gen-tg-debug") << std::endl
;
553 Trace("sg-proc") << "...done collect signature information" << std::endl
;
556 Trace("sg-proc") << "Build theorem index..." << std::endl
;
559 std::vector
< Node
> provenConj
;
560 quantifiers::FirstOrderModel
* m
= d_quantEngine
->getModel();
561 for( int i
=0; i
<m
->getNumAssertedQuantifiers(); i
++ ){
562 Node q
= m
->getAssertedQuantifier( i
);
563 Trace("thm-db-debug") << "Is " << q
<< " a relevant theorem?" << std::endl
;
565 if( q
[1].getKind()==EQUAL
){
566 bool isSubsume
= false;
568 for( unsigned r
=0; r
<2; r
++ ){
569 TNode nl
= q
[1][r
==0 ? 0 : 1];
570 TNode nr
= q
[1][r
==0 ? 1 : 0];
571 Node eq
= nl
.eqNode( nr
);
572 if( r
==1 || std::find( d_conjectures
.begin(), d_conjectures
.end(), q
)==d_conjectures
.end() ){
573 //must make it canonical
574 std::map
< TypeNode
, unsigned > var_count
;
575 std::map
< TNode
, TNode
> subs
;
576 Trace("sg-proc-debug") << "get canonical " << eq
<< std::endl
;
577 eq
= getCanonicalTerm( eq
, var_count
, subs
);
581 inEe
= d_ee_conjectures
.find( q
[1] )!=d_ee_conjectures
.end();
583 //add to universal equality engine
584 Node nl
= getUniversalRepresentative( eq
[0], true );
585 Node nr
= getUniversalRepresentative( eq
[1], true );
586 if( areUniversalEqual( nl
, nr
) ){
588 //set inactive (will be ignored by other modules)
589 d_quantEngine
->getModel()->setQuantifierActive( q
, false );
592 d_ee_conjectures
[q
[1]] = true;
593 d_uequalityEngine
.assertEquality( nl
.eqNode( nr
), true, exp
);
596 Trace("sg-conjecture") << "*** CONJECTURE : currently proven" << (isSubsume
? " and subsumed" : "");
597 Trace("sg-conjecture") << " : " << q
[1] << std::endl
;
598 provenConj
.push_back( q
);
601 Trace("thm-db-debug") << "Adding theorem to database " << eq
[0] << " == " << eq
[1] << std::endl
;
602 d_thm_index
.addTheorem( eq
[0], eq
[1] );
612 //examine status of other conjectures
613 for( unsigned i
=0; i
<d_conjectures
.size(); i
++ ){
614 Node q
= d_conjectures
[i
];
615 if( std::find( provenConj
.begin(), provenConj
.end(), q
)==provenConj
.end() ){
616 //check each skolem variable
617 bool disproven
= true;
618 //std::vector< Node > sk;
619 //getTermDatabase()->getSkolemConstants( q, sk, true );
620 Trace("sg-conjecture") << " CONJECTURE : ";
621 std::vector
< Node
> ce
;
622 for( unsigned j
=0; j
<getTermDatabase()->d_skolem_constants
[q
].size(); j
++ ){
623 TNode k
= getTermDatabase()->d_skolem_constants
[q
][j
];
624 TNode rk
= getRepresentative( k
);
625 std::map
< TNode
, Node
>::iterator git
= d_ground_eqc_map
.find( rk
);
626 //check if it is a ground term
627 if( git
==d_ground_eqc_map
.end() ){
628 Trace("sg-conjecture") << "ACTIVE : " << q
;
629 if( Trace
.isOn("sg-gen-eqc") ){
630 Trace("sg-conjecture") << " { ";
631 for( unsigned k
=0; k
<getTermDatabase()->d_skolem_constants
[q
].size(); k
++ ){ Trace("sg-conjecture") << getTermDatabase()->d_skolem_constants
[q
][k
] << ( j
==k
? "*" : "" ) << " "; }
632 Trace("sg-conjecture") << "}";
634 Trace("sg-conjecture") << std::endl
;
638 ce
.push_back( git
->second
);
642 Trace("sg-conjecture") << "disproven : " << q
<< " : ";
643 for( unsigned i
=0; i
<ce
.size(); i
++ ){
644 Trace("sg-conjecture") << q
[0][i
] << " -> " << ce
[i
] << " ";
646 Trace("sg-conjecture") << std::endl
;
650 Trace("thm-db") << "Theorem database is : " << std::endl
;
651 d_thm_index
.debugPrint( "thm-db" );
652 Trace("thm-db") << std::endl
;
653 Trace("sg-proc") << "...done build theorem index" << std::endl
;
657 d_pattern_var_id
.clear();
658 d_pattern_var_duplicate
.clear();
659 d_pattern_is_normal
.clear();
660 d_pattern_fun_id
.clear();
661 d_pattern_fun_sum
.clear();
662 d_rel_patterns
.clear();
663 d_rel_pattern_var_sum
.clear();
664 d_rel_pattern_typ_index
.clear();
665 d_rel_pattern_subs_index
.clear();
666 //d_gen_lat_maximal.clear();
667 //d_gen_lat_child.clear();
668 //d_gen_lat_parent.clear();
669 //d_gen_depth.clear();
671 //the following generates a set of relevant terms
672 d_use_ccand_eqc
= true;
673 for( unsigned i
=0; i
<2; i
++ ){
674 d_ccand_eqc
[i
].clear();
675 d_ccand_eqc
[i
].push_back( d_relevant_eqc
[i
] );
677 d_rel_term_count
= 0;
678 //consider all functions
679 d_typ_tg_funcs
.clear();
680 for( std::map
< TypeNode
, std::vector
< TNode
> >::iterator it
= d_typ_funcs
.begin(); it
!= d_typ_funcs
.end(); ++it
){
681 d_typ_tg_funcs
[it
->first
].insert( d_typ_tg_funcs
[it
->first
].end(), it
->second
.begin(), it
->second
.end() );
683 std::map
< TypeNode
, unsigned > rt_var_max
;
684 std::vector
< TypeNode
> rt_types
;
685 std::map
< int, std::vector
< Node
> > conj_lhs
;
686 std::map
< int, std::vector
< Node
> > conj_rhs
;
687 //map from generalization depth to maximum depth
688 //std::map< unsigned, unsigned > gdepth_to_tdepth;
689 for( unsigned depth
=1; depth
<3; depth
++ ){
690 Assert( d_tg_alloc
.empty() );
691 Trace("sg-proc") << "Generate terms at depth " << depth
<< "..." << std::endl
;
692 Trace("sg-rel-term") << "Relevant terms of depth " << depth
<< " : " << std::endl
;
698 d_tg_gdepth_limit
= depth
;
700 d_tg_alloc
[0].reset( this, TypeNode::null() );
701 while( d_tg_alloc
[0].getNextTerm( this, depth
) ){
702 Assert( d_tg_alloc
[0].getGeneralizationDepth( this )==d_tg_gdepth
);
703 if( d_tg_alloc
[0].getDepth( this )==depth
){
705 Node nn
= d_tg_alloc
[0].getTerm( this );
706 if( getUniversalRepresentative( nn
)==nn
){
708 Trace("sg-rel-term") << "*** Relevant term : ";
709 d_tg_alloc
[0].debugPrint( this, "sg-rel-term", "sg-rel-term-debug2" );
710 Trace("sg-rel-term") << std::endl
;
712 for( unsigned r
=0; r
<2; r
++ ){
713 Trace("sg-gen-tg-eqc") << "...from equivalence classes (" << r
<< ") : ";
714 int index
= d_ccand_eqc
[r
].size()-1;
715 for( unsigned j
=0; j
<d_ccand_eqc
[r
][index
].size(); j
++ ){
716 Trace("sg-gen-tg-eqc") << "e" << d_em
[d_ccand_eqc
[r
][index
][j
]] << " ";
718 Trace("sg-gen-tg-eqc") << std::endl
;
720 TypeNode tnn
= nn
.getType();
721 Trace("sg-gen-tg-debug") << "...term is " << nn
<< std::endl
;
722 conj_lhs
[depth
].push_back( nn
);
724 //add information about pattern
725 Trace("sg-gen-tg-debug") << "Collect pattern information..." << std::endl
;
726 Assert( std::find( d_rel_patterns
[tnn
].begin(), d_rel_patterns
[tnn
].end(), nn
)==d_rel_patterns
[tnn
].end() );
727 d_rel_patterns
[tnn
].push_back( nn
);
728 //build information concerning the variables in this pattern
730 std::map
< TypeNode
, unsigned > typ_to_subs_index
;
731 std::vector
< TNode
> gsubs_vars
;
732 for( std::map
< TypeNode
, unsigned >::iterator it
= d_var_id
.begin(); it
!= d_var_id
.end(); ++it
){
734 typ_to_subs_index
[it
->first
] = sum
;
736 for( unsigned i
=0; i
<it
->second
; i
++ ){
737 gsubs_vars
.push_back( getFreeVar( it
->first
, i
) );
741 d_rel_pattern_var_sum
[nn
] = sum
;
742 //register the pattern
743 registerPattern( nn
, tnn
);
744 Assert( d_pattern_is_normal
[nn
] );
745 Trace("sg-gen-tg-debug") << "...done collect pattern information" << std::endl
;
747 //record information about types
748 Trace("sg-gen-tg-debug") << "Collect type information..." << std::endl
;
749 PatternTypIndex
* pti
= &d_rel_pattern_typ_index
;
750 for( std::map
< TypeNode
, unsigned >::iterator it
= d_var_id
.begin(); it
!= d_var_id
.end(); ++it
){
751 pti
= &pti
->d_children
[it
->first
][it
->second
];
753 if( rt_var_max
.find( it
->first
)==rt_var_max
.end() || it
->second
>rt_var_max
[it
->first
] ){
754 rt_var_max
[it
->first
] = it
->second
;
757 if( std::find( rt_types
.begin(), rt_types
.end(), tnn
)==rt_types
.end() ){
758 rt_types
.push_back( tnn
);
760 pti
->d_terms
.push_back( nn
);
761 Trace("sg-gen-tg-debug") << "...done collect type information" << std::endl
;
763 Trace("sg-gen-tg-debug") << "Build substitutions for ground EQC..." << std::endl
;
764 std::vector
< TNode
> gsubs_terms
;
765 gsubs_terms
.resize( gsubs_vars
.size() );
766 int index
= d_ccand_eqc
[1].size()-1;
767 for( unsigned j
=0; j
<d_ccand_eqc
[1][index
].size(); j
++ ){
768 TNode r
= d_ccand_eqc
[1][index
][j
];
769 Trace("sg-gen-tg-eqc") << " Matches for e" << d_em
[r
] << ", which is ground term " << d_ground_eqc_map
[r
] << ":" << std::endl
;
770 std::map
< TypeNode
, std::map
< unsigned, TNode
> > subs
;
771 std::map
< TNode
, bool > rev_subs
;
772 //only get ground terms
773 unsigned mode
= optFilterConfirmationOnlyGround() ? 2 : 0;
774 d_tg_alloc
[0].resetMatching( this, r
, mode
);
775 while( d_tg_alloc
[0].getNextMatch( this, r
, subs
, rev_subs
) ){
776 //we will be building substitutions
777 bool firstTime
= true;
778 for( std::map
< TypeNode
, std::map
< unsigned, TNode
> >::iterator it
= subs
.begin(); it
!= subs
.end(); ++it
){
779 unsigned tindex
= typ_to_subs_index
[it
->first
];
780 for( std::map
< unsigned, TNode
>::iterator it2
= it
->second
.begin(); it2
!= it
->second
.end(); ++it2
){
782 Trace("sg-gen-tg-eqc") << ", ";
785 Trace("sg-gen-tg-eqc") << " ";
787 Trace("sg-gen-tg-eqc") << it
->first
<< ":x" << it2
->first
<< " -> " << it2
->second
;
788 Assert( tindex
+it2
->first
<gsubs_terms
.size() );
789 gsubs_terms
[tindex
+it2
->first
] = it2
->second
;
792 Trace("sg-gen-tg-eqc") << std::endl
;
793 d_rel_pattern_subs_index
[nn
].addSubstitution( r
, gsubs_vars
, gsubs_terms
);
796 Trace("sg-gen-tg-debug") << "...done build substitutions for ground EQC" << std::endl
;
798 Trace("sg-gen-tg-debug") << "> not canonical : " << nn
<< std::endl
;
801 Trace("sg-gen-tg-debug") << "> produced term at previous depth : ";
802 d_tg_alloc
[0].debugPrint( this, "sg-gen-tg-debug", "sg-gen-tg-debug" );
803 Trace("sg-gen-tg-debug") << std::endl
;
806 Trace("sg-proc") << "...done generate terms at depth " << depth
<< std::endl
;
808 //now generate right hand sides
812 Trace("sg-stats") << "--------> Total relevant patterns : " << d_rel_term_count
<< std::endl
;
814 Trace("sg-proc") << "Generate properties..." << std::endl
;
816 d_use_ccand_eqc
= false;
819 for( std::map
< TypeNode
, unsigned >::iterator it
= rt_var_max
.begin(); it
!= rt_var_max
.end(); ++it
){
820 d_var_id
[ it
->first
] = it
->second
;
821 d_var_limit
[ it
->first
] = it
->second
;
823 //set up environment for candidate conjectures
824 //d_cconj_at_depth.clear();
825 //for( unsigned i=0; i<2; i++ ){
826 // d_cconj[i].clear();
828 //d_cconj_rhs_paired.clear();
829 unsigned totalCount
= 0;
830 for( unsigned depth
=0; depth
<5; depth
++ ){
831 //consider types from relevant terms
832 std::random_shuffle( rt_types
.begin(), rt_types
.end() );
833 for( unsigned i
=0; i
<rt_types
.size(); i
++ ){
834 Assert( d_tg_alloc
.empty() );
835 Trace("sg-proc") << "Generate relevant RHS terms of type " << rt_types
[i
] << " at depth " << depth
<< "..." << std::endl
;
838 d_tg_gdepth_limit
= -1;
839 d_tg_alloc
[0].reset( this, rt_types
[i
] );
840 while( d_tg_alloc
[0].getNextTerm( this, depth
) && totalCount
<100 ){
841 if( d_tg_alloc
[0].getDepth( this )==depth
){
842 Node rhs
= d_tg_alloc
[0].getTerm( this );
843 Trace("sg-rel-prop") << "Relevant RHS : " << rhs
<< std::endl
;
844 conj_rhs
[depth
].push_back( rhs
);
846 Assert( rhs
.getType()==rt_types
[i
] );
847 registerPattern( rhs
, rt_types
[i
] );
851 //could have been partial, we must clear
854 Trace("sg-proc") << "Process candidate conjectures up to RHS term depth " << depth
<< "..." << std::endl
;
855 for( int rhs_d
=depth
; rhs_d
>=0; rhs_d
-- ){
856 int lhs_d
= (depth
-rhs_d
)+1;
857 for( unsigned i
=0; i
<conj_rhs
[rhs_d
].size(); i
++ ){
858 for( unsigned j
=0; j
<conj_lhs
[lhs_d
].size(); j
++ ){
859 processCandidateConjecture( conj_lhs
[lhs_d
][j
], conj_rhs
[rhs_d
][i
], lhs_d
, rhs_d
);
863 Trace("sg-proc") << "...done process candidate conjectures at RHS term depth " << depth
<< std::endl
;
865 Trace("sg-proc") << "...done generate properties" << std::endl
;
867 if( !d_waiting_conjectures
.empty() ){
868 Trace("sg-proc") << "Generated " << d_waiting_conjectures
.size() << " conjectures." << std::endl
;
869 d_conjectures
.insert( d_conjectures
.end(), d_waiting_conjectures
.begin(), d_waiting_conjectures
.end() );
870 for( unsigned i
=0; i
<d_waiting_conjectures
.size(); i
++ ){
871 Assert( d_waiting_conjectures
[i
].getKind()==FORALL
);
872 Node lem
= NodeManager::currentNM()->mkNode( OR
, d_waiting_conjectures
[i
].negate(), d_waiting_conjectures
[i
] );
873 d_quantEngine
->addLemma( lem
, false );
874 d_quantEngine
->addRequirePhase( d_waiting_conjectures
[i
], false );
876 d_waiting_conjectures
.clear();
879 Trace("thm-ee") << "Universal equality engine is : " << std::endl
;
880 eq::EqClassesIterator ueqcs_i
= eq::EqClassesIterator( &d_uequalityEngine
);
881 while( !ueqcs_i
.isFinished() ){
882 TNode r
= (*ueqcs_i
);
883 bool firstTime
= true;
884 TNode rr
= getUniversalRepresentative( r
);
885 Trace("thm-ee") << " " << r
;
886 if( rr
!=r
){ Trace("thm-ee") << " [" << rr
<< "]"; }
887 Trace("thm-ee") << " : { ";
888 eq::EqClassIterator ueqc_i
= eq::EqClassIterator( r
, &d_uequalityEngine
);
889 while( !ueqc_i
.isFinished() ){
893 Trace("thm-ee") << std::endl
;
896 Trace("thm-ee") << " " << n
<< std::endl
;
900 if( !firstTime
){ Trace("thm-ee") << " "; }
901 Trace("thm-ee") << "}" << std::endl
;
904 Trace("thm-ee") << std::endl
;
909 void ConjectureGenerator::registerQuantifier( Node q
) {
913 void ConjectureGenerator::assertNode( Node n
) {
918 unsigned ConjectureGenerator::getNumTgVars( TypeNode tn
) {
919 //return d_var_tg.size();
923 bool ConjectureGenerator::allowVar( TypeNode tn
) {
924 std::map
< TypeNode
, unsigned >::iterator it
= d_var_limit
.find( tn
);
925 if( it
==d_var_limit
.end() ){
928 return d_var_id
[tn
]<it
->second
;
932 void ConjectureGenerator::addVar( TypeNode tn
) {
933 //d_var_tg.push_back( v );
935 //d_var_eq_tg.push_back( std::vector< unsigned >() );
938 void ConjectureGenerator::removeVar( TypeNode tn
) {
940 //d_var_eq_tg.pop_back();
941 //d_var_tg.pop_back();
944 unsigned ConjectureGenerator::getNumTgFuncs( TypeNode tn
) {
945 return d_typ_tg_funcs
[tn
].size();
948 TNode
ConjectureGenerator::getTgFunc( TypeNode tn
, unsigned i
) {
949 return d_typ_tg_funcs
[tn
][i
];
952 bool ConjectureGenerator::considerCurrentTerm() {
953 Assert( !d_tg_alloc
.empty() );
955 //if generalization depth is too large, don't consider it
956 unsigned i
= d_tg_alloc
.size();
957 Trace("sg-gen-tg-debug") << "Consider term ";
958 d_tg_alloc
[0].debugPrint( this, "sg-gen-tg-debug", "sg-gen-tg-debug" );
959 Trace("sg-gen-tg-debug") << "? curr term size = " << d_tg_alloc
.size() << ", last status = " << d_tg_alloc
[i
-1].d_status
;
960 Trace("sg-gen-tg-debug") << std::endl
;
962 Assert( d_tg_alloc
[0].getGeneralizationDepth( this )==d_tg_gdepth
);
964 if( d_tg_gdepth_limit
>=0 && d_tg_gdepth
>(unsigned)d_tg_gdepth_limit
){
965 Trace("sg-gen-consider-term") << "-> generalization depth of ";
966 d_tg_alloc
[0].debugPrint( this, "sg-gen-consider-term", "sg-gen-tg-debug" );
967 Trace("sg-gen-consider-term") << " is too high " << d_tg_gdepth
<< " " << d_tg_alloc
[0].getGeneralizationDepth( this ) << ", do not consider." << std::endl
;
972 if( d_tg_alloc
[i
-1].d_status
==1 ){
973 }else if( d_tg_alloc
[i
-1].d_status
==2 ){
974 }else if( d_tg_alloc
[i
-1].d_status
==5 ){
976 Trace("sg-gen-tg-debug") << "Bad tg: " << &d_tg_alloc
[i
-1] << std::endl
;
979 //if equated two variables, first check if context-independent TODO
980 //----end optimizations
983 //check based on which candidate equivalence classes match
984 if( d_use_ccand_eqc
){
985 Trace("sg-gen-tg-debug") << "Filter based on relevant ground EQC";
986 Trace("sg-gen-tg-debug") << ", #eqc to try = " << d_ccand_eqc
[0][i
-1].size() << "/" << d_ccand_eqc
[1][i
-1].size() << std::endl
;
988 Assert( d_ccand_eqc
[0].size()>=2 );
989 Assert( d_ccand_eqc
[0].size()==d_ccand_eqc
[1].size() );
990 Assert( d_ccand_eqc
[0].size()==d_tg_id
+1 );
991 Assert( d_tg_id
==d_tg_alloc
.size() );
992 for( unsigned r
=0; r
<2; r
++ ){
993 d_ccand_eqc
[r
][i
].clear();
996 //re-check feasibility of EQC
997 for( unsigned r
=0; r
<2; r
++ ){
998 for( unsigned j
=0; j
<d_ccand_eqc
[r
][i
-1].size(); j
++ ){
999 std::map
< TypeNode
, std::map
< unsigned, TNode
> > subs
;
1000 std::map
< TNode
, bool > rev_subs
;
1003 mode
= optReqDistinctVarPatterns() ? 1 : 0;
1005 mode
= (optFilterConfirmation() && optFilterConfirmationOnlyGround() ) ? 2 : 0;
1007 d_tg_alloc
[0].resetMatching( this, d_ccand_eqc
[r
][i
-1][j
], mode
);
1008 if( d_tg_alloc
[0].getNextMatch( this, d_ccand_eqc
[r
][i
-1][j
], subs
, rev_subs
) ){
1009 d_ccand_eqc
[r
][i
].push_back( d_ccand_eqc
[r
][i
-1][j
] );
1013 for( unsigned r
=0; r
<2; r
++ ){
1014 Trace("sg-gen-tg-debug") << "Current eqc of type " << r
<< " : ";
1015 for( unsigned j
=0; j
<d_ccand_eqc
[r
][i
].size(); j
++ ){
1016 Trace("sg-gen-tg-debug") << "e" << d_em
[d_ccand_eqc
[r
][i
][j
]] << " ";
1018 Trace("sg-gen-tg-debug") << std::endl
;
1020 if( d_ccand_eqc
[0][i
].empty() ){
1021 Trace("sg-gen-consider-term") << "Do not consider term of form ";
1022 d_tg_alloc
[0].debugPrint( this, "sg-gen-consider-term", "sg-gen-consider-term-debug" );
1023 Trace("sg-gen-consider-term") << " since no relevant EQC matches it." << std::endl
;
1026 if( d_ccand_eqc
[1][i
].empty() && optFilterConfirmation() ){
1027 Trace("sg-gen-consider-term") << "Do not consider term of form ";
1028 d_tg_alloc
[0].debugPrint( this, "sg-gen-consider-term", "sg-gen-consider-term-debug" );
1029 Trace("sg-gen-consider-term") << " since no ground EQC matches it." << std::endl
;
1033 Trace("sg-gen-tg-debug") << "Will consider term ";
1034 d_tg_alloc
[0].debugPrint( this, "sg-gen-tg-debug", "sg-gen-tg-debug" );
1035 Trace("sg-gen-tg-debug") << std::endl
;
1036 Trace("sg-gen-consider-term-debug") << std::endl
;
1040 bool ConjectureGenerator::considerTermCanon( unsigned tg_id
){
1041 Assert( tg_id
<d_tg_alloc
.size() );
1042 //check based on a canonicity of the term (if there is one)
1043 Trace("sg-gen-tg-debug") << "Consider term canon ";
1044 d_tg_alloc
[0].debugPrint( this, "sg-gen-tg-debug", "sg-gen-tg-debug" );
1045 Trace("sg-gen-tg-debug") << ", tg is [" << tg_id
<< "]..." << std::endl
;
1047 Node ln
= d_tg_alloc
[tg_id
].getTerm( this );
1048 Trace("sg-gen-tg-debug") << "Term is " << ln
<< std::endl
;
1050 //do not consider if it is non-canonical, and either:
1051 // (1) we are not filtering based on matching candidate eqc, or
1052 // (2) its canonical form is a generalization.
1053 TNode lnr
= getUniversalRepresentative( ln
, true );
1055 markReportedCanon( ln
);
1056 }else if( !d_use_ccand_eqc
|| isGeneralization( lnr
, ln
) ){
1057 Trace("sg-gen-consider-term") << "Do not consider term of form ";
1058 d_tg_alloc
[0].debugPrint( this, "sg-gen-consider-term", "sg-gen-consider-term-debug" );
1059 Trace("sg-gen-consider-term") << " since sub-term " << ln
<< " is not canonical representation (which is " << lnr
<< ")." << std::endl
;
1063 Trace("sg-gen-tg-debug") << "Will consider term canon ";
1064 d_tg_alloc
[0].debugPrint( this, "sg-gen-tg-debug", "sg-gen-tg-debug" );
1065 Trace("sg-gen-tg-debug") << std::endl
;
1066 Trace("sg-gen-consider-term-debug") << std::endl
;
1070 void ConjectureGenerator::changeContext( bool add
) {
1072 for( unsigned r
=0; r
<2; r
++ ){
1073 d_ccand_eqc
[r
].push_back( std::vector
< TNode
>() );
1077 for( unsigned r
=0; r
<2; r
++ ){
1078 d_ccand_eqc
[r
].pop_back();
1081 Assert( d_tg_alloc
.find( d_tg_id
)!=d_tg_alloc
.end() );
1082 d_tg_alloc
.erase( d_tg_id
);
1086 unsigned ConjectureGenerator::collectFunctions( TNode opat
, TNode pat
, std::map
< TNode
, unsigned >& funcs
,
1087 std::map
< TypeNode
, unsigned >& mnvn
, std::map
< TypeNode
, unsigned >& mxvn
){
1088 if( pat
.hasOperator() ){
1089 funcs
[pat
.getOperator()]++;
1091 for( unsigned i
=0; i
<pat
.getNumChildren(); i
++ ){
1092 sum
+= collectFunctions( opat
, pat
[i
], funcs
, mnvn
, mxvn
);
1096 Assert( pat
.getNumChildren()==0 );
1099 if( pat
.getKind()==BOUND_VARIABLE
){
1101 //duplicate variable
1102 d_pattern_var_duplicate
[opat
]++;
1105 TypeNode tn
= pat
.getType();
1106 unsigned vn
= d_free_var_num
[pat
];
1107 std::map
< TypeNode
, unsigned >::iterator it
= mnvn
.find( tn
);
1108 if( it
!=mnvn
.end() ){
1109 if( vn
<it
->second
){
1110 d_pattern_is_normal
[opat
] = false;
1112 }else if( vn
>mxvn
[tn
] ){
1113 if( vn
!=mxvn
[tn
]+1 ){
1114 d_pattern_is_normal
[opat
] = false;
1119 //first variable of this type
1129 void ConjectureGenerator::registerPattern( Node pat
, TypeNode tpat
) {
1130 if( std::find( d_patterns
[tpat
].begin(), d_patterns
[tpat
].end(), pat
)==d_patterns
[tpat
].end() ){
1131 d_patterns
[TypeNode::null()].push_back( pat
);
1132 d_patterns
[tpat
].push_back( pat
);
1134 Assert( d_pattern_fun_id
.find( pat
)==d_pattern_fun_id
.end() );
1135 Assert( d_pattern_var_id
.find( pat
)==d_pattern_var_id
.end() );
1138 std::map
< TypeNode
, unsigned > mnvn
;
1139 d_pattern_fun_sum
[pat
] = collectFunctions( pat
, pat
, d_pattern_fun_id
[pat
], mnvn
, d_pattern_var_id
[pat
] );
1140 if( d_pattern_is_normal
.find( pat
)==d_pattern_is_normal
.end() ){
1141 d_pattern_is_normal
[pat
] = true;
1146 bool ConjectureGenerator::isGeneralization( TNode patg
, TNode pat
, std::map
< TNode
, TNode
>& subs
) {
1147 if( patg
.getKind()==BOUND_VARIABLE
){
1148 std::map
< TNode
, TNode
>::iterator it
= subs
.find( patg
);
1149 if( it
!=subs
.end() ){
1150 return it
->second
==pat
;
1156 Assert( patg
.hasOperator() );
1157 if( !pat
.hasOperator() || patg
.getOperator()!=pat
.getOperator() ){
1160 Assert( patg
.getNumChildren()==pat
.getNumChildren() );
1161 for( unsigned i
=0; i
<patg
.getNumChildren(); i
++ ){
1162 if( !isGeneralization( patg
[i
], pat
[i
], subs
) ){
1171 void ConjectureGenerator::processCandidateConjecture( TNode lhs
, TNode rhs
, unsigned lhs_depth
, unsigned rhs_depth
) {
1172 if( d_waiting_conjectures
.size()>=optFullCheckConjectures() ){
1175 if( considerCandidateConjecture( lhs
, rhs
) ){
1176 Trace("sg-conjecture") << "* Candidate conjecture : " << lhs
<< " == " << rhs
<< std::endl
;
1177 Trace("sg-conjecture-debug") << " LHS, RHS generalization depth : " << lhs_depth
<< ", " << rhs_depth
<< std::endl
;
1178 if( optFilterConfirmation() || optFilterFalsification() ){
1179 Trace("sg-conjecture-debug") << " confirmed = " << d_subs_confirmCount
<< ", #witnesses range = " << d_subs_confirmWitnessRange
.size() << "." << std::endl
;
1180 Trace("sg-conjecture-debug") << " #witnesses for ";
1181 bool firstTime
= true;
1182 for( std::map
< TNode
, std::vector
< TNode
> >::iterator it
= d_subs_confirmWitnessDomain
.begin(); it
!= d_subs_confirmWitnessDomain
.end(); ++it
){
1184 Trace("sg-conjecture-debug") << ", ";
1186 Trace("sg-conjecture-debug") << it
->first
<< " : " << it
->second
.size() << "/" << d_pattern_fun_id
[lhs
][it
->first
];
1187 if( it
->second
.size()==1 ){
1188 Trace("sg-conjecture-debug") << " (" << it
->second
[0] << ")";
1192 Trace("sg-conjecture-debug") << std::endl
;
1193 Trace("sg-conjecture-debug") << " unknown = " << d_subs_unkCount
<< std::endl
;
1195 Assert( getUniversalRepresentative( rhs
)==rhs
);
1196 Assert( getUniversalRepresentative( lhs
)==lhs
);
1197 //make universal quantified formula
1198 Assert( std::find( d_eq_conjectures
[lhs
].begin(), d_eq_conjectures
[lhs
].end(), rhs
)==d_eq_conjectures
[lhs
].end() );
1199 d_eq_conjectures
[lhs
].push_back( rhs
);
1200 d_eq_conjectures
[rhs
].push_back( lhs
);
1201 std::vector
< Node
> bvs
;
1202 for( std::map
< TypeNode
, unsigned >::iterator it
= d_pattern_var_id
[lhs
].begin(); it
!= d_pattern_var_id
[lhs
].end(); ++it
){
1203 for( unsigned i
=0; i
<=it
->second
; i
++ ){
1204 bvs
.push_back( getFreeVar( it
->first
, i
) );
1207 Node bvl
= NodeManager::currentNM()->mkNode( BOUND_VAR_LIST
, bvs
);
1208 Node conj
= NodeManager::currentNM()->mkNode( FORALL
, bvl
, lhs
.eqNode( rhs
) );
1209 conj
= Rewriter::rewrite( conj
);
1210 d_waiting_conjectures
.push_back( conj
);
1214 bool ConjectureGenerator::considerCandidateConjecture( TNode lhs
, TNode rhs
) {
1215 Trace("sg-cconj-debug") << "Consider candidate conjecture : " << lhs
<< " == " << rhs
<< "?" << std::endl
;
1217 Trace("sg-cconj-debug") << " -> trivial." << std::endl
;
1220 if( lhs
.getKind()==APPLY_CONSTRUCTOR
&& rhs
.getKind()==APPLY_CONSTRUCTOR
){
1221 Trace("sg-cconj-debug") << " -> irrelevant by syntactic analysis." << std::endl
;
1224 //variables of LHS must subsume variables of RHS
1225 for( std::map
< TypeNode
, unsigned >::iterator it
= d_pattern_var_id
[rhs
].begin(); it
!= d_pattern_var_id
[rhs
].end(); ++it
){
1226 std::map
< TypeNode
, unsigned >::iterator itl
= d_pattern_var_id
[lhs
].find( it
->first
);
1227 if( itl
!=d_pattern_var_id
[lhs
].end() ){
1228 if( itl
->second
<it
->second
){
1229 Trace("sg-cconj-debug") << " -> variables of sort " << it
->first
<< " are not subsumed." << std::endl
;
1232 Trace("sg-cconj-debug2") << " variables of sort " << it
->first
<< " are : " << itl
->second
<< " vs " << it
->second
<< std::endl
;
1235 Trace("sg-cconj-debug") << " -> has no variables of sort " << it
->first
<< "." << std::endl
;
1239 //currently active conjecture?
1240 std::map
< Node
, std::vector
< Node
> >::iterator iteq
= d_eq_conjectures
.find( lhs
);
1241 if( iteq
!=d_eq_conjectures
.end() ){
1242 if( std::find( iteq
->second
.begin(), iteq
->second
.end(), rhs
)!=iteq
->second
.end() ){
1243 Trace("sg-cconj-debug") << " -> already are considering this conjecture." << std::endl
;
1247 Trace("sg-cconj") << "Consider possible candidate conjecture : " << lhs
<< " == " << rhs
<< "?" << std::endl
;
1248 //find witness for counterexample, if possible
1249 if( optFilterConfirmation() || optFilterFalsification() ){
1250 Assert( d_rel_pattern_var_sum
.find( lhs
)!=d_rel_pattern_var_sum
.end() );
1251 Trace("sg-cconj-debug") << "Notify substitutions over " << d_rel_pattern_var_sum
[lhs
] << " variables." << std::endl
;
1252 std::map
< TNode
, TNode
> subs
;
1253 d_subs_confirmCount
= 0;
1254 d_subs_confirmWitnessRange
.clear();
1255 d_subs_confirmWitnessDomain
.clear();
1256 d_subs_unkCount
= 0;
1257 if( !d_rel_pattern_subs_index
[lhs
].notifySubstitutions( this, subs
, rhs
, d_rel_pattern_var_sum
[lhs
] ) ){
1258 Trace("sg-cconj") << " -> found witness that falsifies the conjecture." << std::endl
;
1261 if( optFilterConfirmation() ){
1262 if( d_subs_confirmCount
==0 ){
1263 Trace("sg-cconj") << " -> not confirmed by a ground substitutions." << std::endl
;
1267 if( optFilterConfirmationDomain() ){
1268 std::vector
< TNode
> vars
;
1269 std::vector
< TNode
> subs
;
1270 for( std::map
< TNode
, std::vector
< TNode
> >::iterator it
= d_subs_confirmWitnessDomain
.begin(); it
!= d_subs_confirmWitnessDomain
.end(); ++it
){
1271 Assert( d_pattern_fun_id
[lhs
].find( it
->first
)!=d_pattern_fun_id
[lhs
].end() );
1272 unsigned req
= d_pattern_fun_id
[lhs
][it
->first
];
1273 std::map
< TNode
, unsigned >::iterator itrf
= d_pattern_fun_id
[rhs
].find( it
->first
);
1274 if( itrf
!=d_pattern_fun_id
[rhs
].end() ){
1275 req
= itrf
->second
>req
? itrf
->second
: req
;
1277 if( it
->second
.size()<req
){
1278 Trace("sg-cconj") << " -> did not find at least " << d_pattern_fun_id
[lhs
][it
->first
] << " different values in ground substitutions for variable " << it
->first
<< "." << std::endl
;
1281 if( it
->second
.size()==1 ){
1282 vars
.push_back( it
->first
);
1283 subs
.push_back( it
->second
[0] );
1286 if( optFilterConfirmationNonCanonical() && !vars
.empty() ){
1287 Node slhs
= lhs
.substitute( vars
.begin(), vars
.end(), subs
.begin(), subs
.end() );
1288 Node srhs
= rhs
.substitute( vars
.begin(), vars
.end(), subs
.begin(), subs
.end() );
1289 TNode slhsr
= getUniversalRepresentative( slhs
, true );
1290 TNode srhsr
= getUniversalRepresentative( srhs
, true );
1291 if( areUniversalEqual( slhsr
, srhsr
) ){
1292 Trace("sg-cconj") << " -> all ground witnesses can be proven by other theorems." << std::endl
;
1295 Trace("sg-cconj-debug") << "Checked if " << slhsr
<< " and " << srhsr
<< " were equal." << std::endl
;
1301 Trace("sg-cconj") << " -> SUCCESS." << std::endl
;
1302 if( optFilterConfirmation() || optFilterFalsification() ){
1303 Trace("sg-cconj") << " confirmed = " << d_subs_confirmCount
<< ", #witnesses range = " << d_subs_confirmWitnessRange
.size() << "." << std::endl
;
1304 for( std::map
< TNode
, std::vector
< TNode
> >::iterator it
= d_subs_confirmWitnessDomain
.begin(); it
!= d_subs_confirmWitnessDomain
.end(); ++it
){
1305 Trace("sg-cconj") << " #witnesses for " << it
->first
<< " : " << it
->second
.size() << std::endl
;
1310 if( getUniversalRepresentative( lhs )!=lhs ){
1311 std::cout << "bad universal representative LHS : " << lhs << " " << getUniversalRepresentative( lhs ) << std::endl;
1314 if( getUniversalRepresentative( rhs )!=rhs ){
1315 std::cout << "bad universal representative RHS : " << rhs << " " << getUniversalRepresentative( rhs ) << std::endl;
1320 //check if still canonical representation (should be, but for efficiency this is not guarenteed)
1321 if( getUniversalRepresentative( lhs
)!=lhs
|| getUniversalRepresentative( rhs
)!=rhs
){
1322 Trace("sg-cconj") << " -> after processing, not canonical." << std::endl
;
1329 bool ConjectureGenerator::notifySubstitution( TNode glhs
, std::map
< TNode
, TNode
>& subs
, TNode rhs
) {
1330 if( Trace
.isOn("sg-cconj-debug") ){
1331 Trace("sg-cconj-debug") << "Ground eqc for LHS : " << glhs
<< ", based on substituion: " << std::endl
;
1332 for( std::map
< TNode
, TNode
>::iterator it
= subs
.begin(); it
!= subs
.end(); ++it
){
1333 Assert( getRepresentative( it
->second
)==it
->second
);
1334 Trace("sg-cconj-debug") << " " << it
->first
<< " -> " << it
->second
<< std::endl
;
1337 Trace("sg-cconj-debug") << "Evaluate RHS : : " << rhs
<< std::endl
;
1338 //get the representative of rhs with substitution subs
1339 TNode grhs
= getTermDatabase()->evaluateTerm( rhs
, subs
, true );
1340 Trace("sg-cconj-debug") << "...done evaluating term, got : " << grhs
<< std::endl
;
1341 if( !grhs
.isNull() ){
1343 if( optFilterFalsification() ){
1344 Trace("sg-cconj-debug") << "Ground eqc for RHS : " << grhs
<< std::endl
;
1345 //check based on ground terms
1346 std::map
< TNode
, Node
>::iterator itl
= d_ground_eqc_map
.find( glhs
);
1347 if( itl
!=d_ground_eqc_map
.end() ){
1348 std::map
< TNode
, Node
>::iterator itr
= d_ground_eqc_map
.find( grhs
);
1349 if( itr
!=d_ground_eqc_map
.end() ){
1350 Trace("sg-cconj-debug") << "We have ground terms " << itl
->second
<< " and " << itr
->second
<< "." << std::endl
;
1351 if( itl
->second
.isConst() && itr
->second
.isConst() ){
1352 Trace("sg-cconj-debug") << "...disequal constants." << std::endl
;
1353 Trace("sg-cconj-witness") << " Witness of falsification : " << itl
->second
<< " != " << itr
->second
<< ", substutition is : " << std::endl
;
1354 for( std::map
< TNode
, TNode
>::iterator it
= subs
.begin(); it
!= subs
.end(); ++it
){
1355 Trace("sg-cconj-witness") << " " << it
->first
<< " -> " << it
->second
<< std::endl
;
1360 if( optFilterUnknown() ){
1368 if( getEqualityEngine()->areDisequal( glhs, grhs, false ) ){
1369 Trace("sg-cconj-debug") << "..." << glhs << " and " << grhs << " are disequal." << std::endl;
1372 Trace("sg-cconj-debug") << "..." << glhs << " and " << grhs << " are not disequal." << std::endl;
1376 Trace("sg-cconj-witness") << " Witnessed " << glhs
<< " == " << grhs
<< ", substutition is : " << std::endl
;
1377 for( std::map
< TNode
, TNode
>::iterator it
= subs
.begin(); it
!= subs
.end(); ++it
){
1378 Trace("sg-cconj-witness") << " " << it
->first
<< " -> " << it
->second
<< std::endl
;
1379 if( std::find( d_subs_confirmWitnessDomain
[it
->first
].begin(), d_subs_confirmWitnessDomain
[it
->first
].end(), it
->second
)==d_subs_confirmWitnessDomain
[it
->first
].end() ){
1380 d_subs_confirmWitnessDomain
[it
->first
].push_back( it
->second
);
1383 d_subs_confirmCount
++;
1384 if( std::find( d_subs_confirmWitnessRange
.begin(), d_subs_confirmWitnessRange
.end(), glhs
)==d_subs_confirmWitnessRange
.end() ){
1385 d_subs_confirmWitnessRange
.push_back( glhs
);
1387 Trace("sg-cconj-debug") << "RHS is identical." << std::endl
;
1390 Trace("sg-cconj-debug") << "(could not ground eqc for RHS)." << std::endl
;
1396 void TermGenerator::reset( ConjectureGenerator
* s
, TypeNode tn
) {
1397 Assert( d_children
.empty() );
1402 Trace("sg-gen-tg-debug2") << "...add to context " << this << std::endl
;
1404 s
->changeContext( true );
1407 bool TermGenerator::getNextTerm( ConjectureGenerator
* s
, unsigned depth
) {
1408 if( Trace
.isOn("sg-gen-tg-debug2") ){
1409 Trace("sg-gen-tg-debug2") << this << " getNextTerm depth " << depth
<< " : status = " << d_status
<< ", num = " << d_status_num
;
1411 TNode f
= s
->getTgFunc( d_typ
, d_status_num
);
1412 Trace("sg-gen-tg-debug2") << ", f = " << f
;
1413 Trace("sg-gen-tg-debug2") << ", #args = " << s
->d_func_args
[f
].size();
1414 Trace("sg-gen-tg-debug2") << ", childNum = " << d_status_child_num
;
1415 Trace("sg-gen-tg-debug2") << ", #children = " << d_children
.size();
1417 Trace("sg-gen-tg-debug2") << std::endl
;
1422 if( !d_typ
.isNull() ){
1423 if( s
->allowVar( d_typ
) ){
1425 d_status_num
= s
->d_var_id
[d_typ
];
1427 Trace("sg-gen-tg-debug2") << this << " ...return unique var #" << d_status_num
<< std::endl
;
1428 return s
->considerCurrentTerm() ? true : getNextTerm( s
, depth
);
1430 //check allocating new variable
1434 return getNextTerm( s
, depth
);
1439 return getNextTerm( s
, depth
);
1441 }else if( d_status
==2 ){
1442 //cleanup previous information
1443 //if( d_status_num>=0 ){
1444 // s->d_var_eq_tg[d_status_num].pop_back();
1446 //check if there is another variable
1447 if( (d_status_num
+1)<(int)s
->getNumTgVars( d_typ
) ){
1449 //we have equated two variables
1450 //s->d_var_eq_tg[d_status_num].push_back( d_id );
1451 Trace("sg-gen-tg-debug2") << this << "...consider other var #" << d_status_num
<< std::endl
;
1452 return s
->considerCurrentTerm() ? true : getNextTerm( s
, depth
);
1456 return getNextTerm( s
, depth
);
1458 }else if( d_status
==4 ){
1460 if( depth
>0 && (d_status_num
+1)<(int)s
->getNumTgFuncs( d_typ
) ){
1462 d_status_child_num
= 0;
1463 Trace("sg-gen-tg-debug2") << this << "...consider function " << s
->getTgFunc( d_typ
, d_status_num
) << std::endl
;
1465 if( !s
->considerCurrentTerm() ){
1467 //don't consider this function
1470 //we have decided on a function application
1472 return getNextTerm( s
, depth
);
1474 //do not choose function applications at depth 0
1476 return getNextTerm( s
, depth
);
1478 }else if( d_status
==5 ){
1479 //iterating over arguments
1480 TNode f
= s
->getTgFunc( d_typ
, d_status_num
);
1481 if( d_status_child_num
<0 ){
1485 return getNextTerm( s
, depth
);
1486 }else if( d_status_child_num
==(int)s
->d_func_args
[f
].size() ){
1487 d_status_child_num
--;
1488 return s
->considerTermCanon( d_id
) ? true : getNextTerm( s
, depth
);
1491 Assert( d_status_child_num
<(int)s
->d_func_args
[f
].size() );
1492 if( d_status_child_num
==(int)d_children
.size() ){
1493 d_children
.push_back( s
->d_tg_id
);
1494 Assert( s
->d_tg_alloc
.find( s
->d_tg_id
)==s
->d_tg_alloc
.end() );
1495 s
->d_tg_alloc
[d_children
[d_status_child_num
]].reset( s
, s
->d_func_args
[f
][d_status_child_num
] );
1496 return getNextTerm( s
, depth
);
1498 Assert( d_status_child_num
+1==(int)d_children
.size() );
1499 if( s
->d_tg_alloc
[d_children
[d_status_child_num
]].getNextTerm( s
, depth
-1 ) ){
1500 d_status_child_num
++;
1501 return getNextTerm( s
, depth
);
1503 d_children
.pop_back();
1504 d_status_child_num
--;
1505 return getNextTerm( s
, depth
);
1509 }else if( d_status
==1 || d_status
==3 ){
1511 s
->removeVar( d_typ
);
1512 Assert( d_status_num
==(int)s
->d_var_id
[d_typ
] );
1513 //check if there is only one feasible equivalence class. if so, don't make pattern any more specific.
1514 //unsigned i = s->d_ccand_eqc[0].size()-1;
1515 //if( s->d_ccand_eqc[0][i].size()==1 && s->d_ccand_eqc[1][i].empty() ){
1516 // Trace("ajr-temp") << "Apply this!" << std::endl;
1518 // return getNextTerm( s, depth );
1524 return getNextTerm( s
, depth
);
1527 Assert( d_children
.empty() );
1528 Trace("sg-gen-tg-debug2") << "...remove from context " << this << std::endl
;
1529 s
->changeContext( false );
1530 Assert( d_id
==s
->d_tg_id
);
1535 void TermGenerator::resetMatching( ConjectureGenerator
* s
, TNode eqc
, unsigned mode
) {
1537 d_match_status_child_num
= 0;
1538 d_match_children
.clear();
1539 d_match_children_end
.clear();
1540 d_match_mode
= mode
;
1541 //if this term generalizes, it must generalize a non-ground term
1542 if( mode
<2 && s
->isGroundEqc( eqc
) && d_status
==5 ){
1543 d_match_status
= -1;
1547 bool TermGenerator::getNextMatch( ConjectureGenerator
* s
, TNode eqc
, std::map
< TypeNode
, std::map
< unsigned, TNode
> >& subs
, std::map
< TNode
, bool >& rev_subs
) {
1548 if( d_match_status
<0 ){
1551 if( Trace
.isOn("sg-gen-tg-match") ){
1552 Trace("sg-gen-tg-match") << "Matching ";
1553 debugPrint( s
, "sg-gen-tg-match", "sg-gen-tg-match" );
1554 Trace("sg-gen-tg-match") << " with eqc e" << s
->d_em
[eqc
] << "..." << std::endl
;
1555 Trace("sg-gen-tg-match") << " mstatus = " << d_match_status
;
1557 TNode f
= s
->getTgFunc( d_typ
, d_status_num
);
1558 Trace("sg-gen-tg-debug2") << ", f = " << f
;
1559 Trace("sg-gen-tg-debug2") << ", #args = " << s
->d_func_args
[f
].size();
1560 Trace("sg-gen-tg-debug2") << ", mchildNum = " << d_match_status_child_num
;
1561 Trace("sg-gen-tg-debug2") << ", #mchildren = " << d_match_children
.size();
1563 Trace("sg-gen-tg-debug2") << ", current substitution : {";
1564 for( std::map
< TypeNode
, std::map
< unsigned, TNode
> >::iterator itt
= subs
.begin(); itt
!= subs
.end(); ++itt
){
1565 for( std::map
< unsigned, TNode
>::iterator it
= itt
->second
.begin(); it
!= itt
->second
.end(); ++it
){
1566 Trace("sg-gen-tg-debug2") << " " << it
->first
<< " -> e" << s
->d_em
[it
->second
];
1569 Trace("sg-gen-tg-debug2") << " } " << std::endl
;
1573 if( d_match_status
==0 ){
1575 if( d_match_mode
>=2 ){
1577 if( !s
->isGroundEqc( eqc
) ){
1581 if( d_match_mode
%2==1 ){
1582 std::map
< TNode
, bool >::iterator it
= rev_subs
.find( eqc
);
1583 if( it
==rev_subs
.end() ){
1584 rev_subs
[eqc
] = true;
1589 Assert( subs
[d_typ
].find( d_status_num
)==subs
[d_typ
].end() );
1590 subs
[d_typ
][d_status_num
] = eqc
;
1594 subs
[d_typ
].erase( d_status_num
);
1595 if( d_match_mode
%2==1 ){
1596 rev_subs
.erase( eqc
);
1600 }else if( d_status
==2 ){
1601 if( d_match_status
==0 ){
1603 Assert( d_status_num
<(int)s
->getNumTgVars( d_typ
) );
1604 std::map
< unsigned, TNode
>::iterator it
= subs
[d_typ
].find( d_status_num
);
1605 Assert( it
!=subs
[d_typ
].end() );
1606 return it
->second
==eqc
;
1610 }else if( d_status
==5 ){
1611 //Assert( d_match_children.size()<=d_children.size() );
1612 //enumerating over f-applications in eqc
1613 if( d_match_status_child_num
<0 ){
1615 }else if( d_match_status
==0 ){
1616 //set up next binding
1617 if( d_match_status_child_num
==(int)d_match_children
.size() ){
1618 if( d_match_status_child_num
==0 ){
1620 TNode f
= s
->getTgFunc( d_typ
, d_status_num
);
1621 std::map
< TNode
, TermArgTrie
>::iterator it
= s
->getTermDatabase()->d_func_map_eqc_trie
[f
].d_data
.find( eqc
);
1622 if( it
!=s
->getTermDatabase()->d_func_map_eqc_trie
[f
].d_data
.end() ){
1623 d_match_children
.push_back( it
->second
.d_data
.begin() );
1624 d_match_children_end
.push_back( it
->second
.d_data
.end() );
1627 d_match_status_child_num
--;
1628 return getNextMatch( s
, eqc
, subs
, rev_subs
);
1631 d_match_children
.push_back( d_match_children
[d_match_status_child_num
-1]->second
.d_data
.begin() );
1632 d_match_children_end
.push_back( d_match_children
[d_match_status_child_num
-1]->second
.d_data
.end() );
1636 Assert( d_match_status_child_num
+1==(int)d_match_children
.size() );
1637 if( d_match_children
[d_match_status_child_num
]==d_match_children_end
[d_match_status_child_num
] ){
1638 //no more arguments to bind
1639 d_match_children
.pop_back();
1640 d_match_children_end
.pop_back();
1641 d_match_status_child_num
--;
1642 return getNextMatch( s
, eqc
, subs
, rev_subs
);
1644 if( d_match_status_child_num
==(int)d_children
.size() ){
1645 //successfully matched all children
1646 d_match_children
.pop_back();
1647 d_match_children_end
.pop_back();
1648 d_match_status_child_num
--;
1649 return true;//return d_match_children[d_match_status]!=d_match_children_end[d_match_status];
1652 s
->d_tg_alloc
[d_children
[d_match_status_child_num
]].resetMatching( s
, d_match_children
[d_match_status_child_num
]->first
, d_match_mode
);
1653 return getNextMatch( s
, eqc
, subs
, rev_subs
);
1657 Assert( d_match_status
==1 );
1658 Assert( d_match_status_child_num
+1==(int)d_match_children
.size() );
1659 Assert( d_match_children
[d_match_status_child_num
]!=d_match_children_end
[d_match_status_child_num
] );
1661 if( s
->d_tg_alloc
[d_children
[d_match_status_child_num
]].getNextMatch( s
, d_match_children
[d_match_status_child_num
]->first
, subs
, rev_subs
) ){
1662 d_match_status_child_num
++;
1663 return getNextMatch( s
, eqc
, subs
, rev_subs
);
1666 d_match_children
[d_match_status_child_num
]++;
1667 return getNextMatch( s
, eqc
, subs
, rev_subs
);
1675 unsigned TermGenerator::getDepth( ConjectureGenerator
* s
) {
1678 for( unsigned i
=0; i
<d_children
.size(); i
++ ){
1679 unsigned d
= s
->d_tg_alloc
[d_children
[i
]].getDepth( s
);
1690 unsigned TermGenerator::getGeneralizationDepth( ConjectureGenerator
* s
) {
1693 for( unsigned i
=0; i
<d_children
.size(); i
++ ){
1694 sum
+= s
->d_tg_alloc
[d_children
[i
]].getGeneralizationDepth( s
);
1697 }else if( d_status
==2 ){
1700 Assert( d_status
==1 );
1705 Node
TermGenerator::getTerm( ConjectureGenerator
* s
) {
1706 if( d_status
==1 || d_status
==2 ){
1707 Assert( !d_typ
.isNull() );
1708 return s
->getFreeVar( d_typ
, d_status_num
);
1709 }else if( d_status
==5 ){
1710 Node f
= s
->getTgFunc( d_typ
, d_status_num
);
1711 if( d_children
.size()==s
->d_func_args
[f
].size() ){
1712 std::vector
< Node
> children
;
1713 children
.push_back( f
);
1714 for( unsigned i
=0; i
<d_children
.size(); i
++ ){
1715 Node nc
= s
->d_tg_alloc
[d_children
[i
]].getTerm( s
);
1717 return Node::null();
1719 //Assert( nc.getType()==s->d_func_args[f][i] );
1720 children
.push_back( nc
);
1723 return NodeManager::currentNM()->mkNode( s
->d_func_kind
[f
], children
);
1728 return Node::null();
1731 void TermGenerator::debugPrint( ConjectureGenerator
* s
, const char * c
, const char * cd
) {
1732 Trace(cd
) << "[*" << d_id
<< "," << d_status
<< "]:";
1733 if( d_status
==1 || d_status
==2 ){
1734 Trace(c
) << s
->getFreeVar( d_typ
, d_status_num
);
1735 }else if( d_status
==5 ){
1736 TNode f
= s
->getTgFunc( d_typ
, d_status_num
);
1737 Trace(c
) << "(" << f
;
1738 for( unsigned i
=0; i
<d_children
.size(); i
++ ){
1740 s
->d_tg_alloc
[d_children
[i
]].debugPrint( s
, c
, cd
);
1742 if( d_children
.size()<s
->d_func_args
[f
].size() ){
1752 void SubstitutionIndex::addSubstitution( TNode eqc
, std::vector
< TNode
>& vars
, std::vector
< TNode
>& terms
, unsigned i
) {
1753 if( i
==vars
.size() ){
1756 Assert( d_var
.isNull() || d_var
==vars
[i
] );
1758 d_children
[terms
[i
]].addSubstitution( eqc
, vars
, terms
, i
+1 );
1762 bool SubstitutionIndex::notifySubstitutions( ConjectureGenerator
* s
, std::map
< TNode
, TNode
>& subs
, TNode rhs
, unsigned numVars
, unsigned i
) {
1764 Assert( d_children
.empty() );
1765 return s
->notifySubstitution( d_var
, subs
, rhs
);
1767 Assert( i
==0 || !d_children
.empty() );
1768 for( std::map
< TNode
, SubstitutionIndex
>::iterator it
= d_children
.begin(); it
!= d_children
.end(); ++it
){
1769 Trace("sg-cconj-debug2") << "Try " << d_var
<< " -> " << it
->first
<< " (" << i
<< "/" << numVars
<< ")" << std::endl
;
1770 subs
[d_var
] = it
->first
;
1771 if( !it
->second
.notifySubstitutions( s
, subs
, rhs
, numVars
, i
+1 ) ){
1780 void TheoremIndex::addTheorem( std::vector
< TNode
>& lhs_v
, std::vector
< unsigned >& lhs_arg
, TNode rhs
){
1781 if( lhs_v
.empty() ){
1782 if( std::find( d_terms
.begin(), d_terms
.end(), rhs
)==d_terms
.end() ){
1783 d_terms
.push_back( rhs
);
1786 unsigned index
= lhs_v
.size()-1;
1787 if( lhs_arg
[index
]==lhs_v
[index
].getNumChildren() ){
1790 addTheorem( lhs_v
, lhs_arg
, rhs
);
1793 addTheoremNode( lhs_v
[index
][lhs_arg
[index
]-1], lhs_v
, lhs_arg
, rhs
);
1798 void TheoremIndex::addTheoremNode( TNode curr
, std::vector
< TNode
>& lhs_v
, std::vector
< unsigned >& lhs_arg
, TNode rhs
){
1799 Trace("thm-db-debug") << "Adding conjecture for subterm " << curr
<< "..." << std::endl
;
1800 if( curr
.hasOperator() ){
1801 lhs_v
.push_back( curr
);
1802 lhs_arg
.push_back( 0 );
1803 d_children
[curr
.getOperator()].addTheorem( lhs_v
, lhs_arg
, rhs
);
1805 Assert( curr
.getKind()==kind::BOUND_VARIABLE
);
1806 Assert( d_var
.isNull() || d_var
==curr
);
1808 d_children
[curr
].addTheorem( lhs_v
, lhs_arg
, rhs
);
1812 void TheoremIndex::getEquivalentTerms( std::vector
< TNode
>& n_v
, std::vector
< unsigned >& n_arg
,
1813 std::map
< TNode
, TNode
>& smap
, std::vector
< TNode
>& vars
, std::vector
< TNode
>& subs
,
1814 std::vector
< Node
>& terms
) {
1815 Trace("thm-db-debug") << "Get equivalent terms " << n_v
.size() << " " << n_arg
.size() << std::endl
;
1817 Trace("thm-db-debug") << "Number of terms : " << d_terms
.size() << std::endl
;
1818 //apply substutitions to RHS's
1819 for( unsigned i
=0; i
<d_terms
.size(); i
++ ){
1820 Node n
= d_terms
[i
].substitute( vars
.begin(), vars
.end(), subs
.begin(), subs
.end() );
1821 terms
.push_back( n
);
1824 unsigned index
= n_v
.size()-1;
1825 if( n_arg
[index
]==n_v
[index
].getNumChildren() ){
1828 getEquivalentTerms( n_v
, n_arg
, smap
, vars
, subs
, terms
);
1831 getEquivalentTermsNode( n_v
[index
][n_arg
[index
]-1], n_v
, n_arg
, smap
, vars
, subs
, terms
);
1836 void TheoremIndex::getEquivalentTermsNode( Node curr
, std::vector
< TNode
>& n_v
, std::vector
< unsigned >& n_arg
,
1837 std::map
< TNode
, TNode
>& smap
, std::vector
< TNode
>& vars
, std::vector
< TNode
>& subs
,
1838 std::vector
< Node
>& terms
) {
1839 Trace("thm-db-debug") << "Get equivalent based on subterm " << curr
<< "..." << std::endl
;
1840 if( curr
.hasOperator() ){
1841 Trace("thm-db-debug") << "Check based on operator..." << std::endl
;
1842 std::map
< TNode
, TheoremIndex
>::iterator it
= d_children
.find( curr
.getOperator() );
1843 if( it
!=d_children
.end() ){
1844 n_v
.push_back( curr
);
1845 n_arg
.push_back( 0 );
1846 it
->second
.getEquivalentTerms( n_v
, n_arg
, smap
, vars
, subs
, terms
);
1848 Trace("thm-db-debug") << "...done check based on operator" << std::endl
;
1850 if( !d_var
.isNull() ){
1851 Trace("thm-db-debug") << "Check for substitution with " << d_var
<< "..." << std::endl
;
1852 if( curr
.getType()==d_var
.getType() ){
1853 //add to substitution if possible
1854 bool success
= false;
1855 std::map
< TNode
, TNode
>::iterator it
= smap
.find( d_var
);
1856 if( it
==smap
.end() ){
1858 vars
.push_back( d_var
);
1859 subs
.push_back( curr
);
1861 }else if( it
->second
==curr
){
1864 //also check modulo equality (in universal equality engine)
1866 Trace("thm-db-debug") << "...check for substitution with " << d_var
<< ", success = " << success
<< "." << std::endl
;
1868 d_children
[d_var
].getEquivalentTerms( n_v
, n_arg
, smap
, vars
, subs
, terms
);
1874 void TheoremIndex::debugPrint( const char * c
, unsigned ind
) {
1875 for( std::map
< TNode
, TheoremIndex
>::iterator it
= d_children
.begin(); it
!= d_children
.end(); ++it
){
1876 for( unsigned i
=0; i
<ind
; i
++ ){ Trace(c
) << " "; }
1877 Trace(c
) << it
->first
<< std::endl
;
1878 it
->second
.debugPrint( c
, ind
+1 );
1880 if( !d_terms
.empty() ){
1881 for( unsigned i
=0; i
<ind
; i
++ ){ Trace(c
) << " "; }
1883 for( unsigned i
=0; i
<d_terms
.size(); i
++ ){
1884 Trace(c
) << " " << d_terms
[i
];
1886 Trace(c
) << " }" << std::endl
;
1888 //if( !d_var.isNull() ){
1889 // for( unsigned i=0; i<ind; i++ ){ Trace(c) << " "; }
1890 // Trace(c) << "var:" << d_var << std::endl;
1894 bool ConjectureGenerator::optReqDistinctVarPatterns() { return false; }
1895 bool ConjectureGenerator::optFilterFalsification() { return true; }
1896 bool ConjectureGenerator::optFilterUnknown() { return true; } //may change
1897 bool ConjectureGenerator::optFilterConfirmation() { return true; }
1898 bool ConjectureGenerator::optFilterConfirmationDomain() { return true; }
1899 bool ConjectureGenerator::optFilterConfirmationOnlyGround() { return true; }//false; }
1900 bool ConjectureGenerator::optFilterConfirmationNonCanonical() { return false; }//true; }
1901 unsigned ConjectureGenerator::optFullCheckFrequency() { return 1; }
1902 unsigned ConjectureGenerator::optFullCheckConjectures() { return options::conjectureGenPerRound(); }