Work on conjecture generator : do not generalize subterms with concrete values, filte...
[cvc5.git] / src / theory / quantifiers / conjecture_generator.cpp
1 /********************* */
2 /*! \file conjecture_generator.cpp
3 ** \verbatim
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
11 **
12 ** \brief conjecture generator class
13 **
14 **/
15
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"
22
23 using namespace CVC4;
24 using namespace CVC4::kind;
25 using namespace CVC4::theory;
26 using namespace CVC4::theory::quantifiers;
27 using namespace std;
28
29
30 namespace CVC4 {
31
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 );
38 }
39 }else{
40 d_child[s->getTermDatabase()->d_arg_reps[n][index]].addTerm( s, n, index+1 );
41 }
42 }
43
44 Node OpArgIndex::getGroundTerm( ConjectureGenerator * s, std::vector< TNode >& args ) {
45 if( d_ops.empty() ){
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 );
51 args.pop_back();
52 if( !n.isNull() ){
53 return n;
54 }
55 }
56 }
57 return Node::null();
58 }else{
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 );
63 }
64 }
65
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 );
71 }
72 }
73 }
74
75
76
77 ConjectureGenerator::ConjectureGenerator( QuantifiersEngine * qe, context::Context* c ) : QuantifiersModule( qe ),
78 d_notify( *this ),
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 );
84
85 }
86
87 void ConjectureGenerator::eqNotifyNewClass( TNode t ){
88 Trace("thm-ee-debug") << "UEE : new equivalence class " << t << std::endl;
89 d_upendingAdds.push_back( t );
90 }
91
92 void ConjectureGenerator::eqNotifyPreMerge(TNode t1, TNode t2) {
93 //get maintained representatives
94 TNode rt1 = t1;
95 TNode rt2 = t2;
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();
99 }
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();
103 }
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;
108
109 if( isUniversalLessThan( rt2, rt1 ) ){
110 EqcInfo * ei;
111 if( it1==d_eqc_info.end() ){
112 ei = getOrMakeEqcInfo( t1, true );
113 }else{
114 ei = it1->second;
115 }
116 ei->d_rep = t2;
117 }
118 }
119
120 void ConjectureGenerator::eqNotifyPostMerge(TNode t1, TNode t2) {
121
122 }
123
124 void ConjectureGenerator::eqNotifyDisequal(TNode t1, TNode t2, TNode reason) {
125 Trace("thm-ee-debug") << "UEE : disequality holds : " << t1 << " != " << t2 << std::endl;
126
127 }
128
129
130 ConjectureGenerator::EqcInfo::EqcInfo( context::Context* c ) : d_rep( c, Node::null() ){
131
132 }
133
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;
139 }else if( doMake ){
140 EqcInfo* ei = new EqcInfo( d_quantEngine->getSatContext() );
141 d_eqc_info[n] = ei;
142 return ei;
143 }else{
144 return NULL;
145 }
146 }
147
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++ ){
156 Node t = pending[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() ){
170 assertEq = true;
171 }else{
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] );
176 assertEq = true;
177 }
178 }
179 if( assertEq ){
180 Node exp;
181 d_uequalityEngine.assertEquality( t.eqNode( eq_terms[i] ), true, exp );
182 }
183 }
184 }else{
185 Trace("thm-ee-add") << "UEE : No equivalent terms." << std::endl;
186 }
187 //if occurs at ground level, merge with representative of ground equality engine
188 /*
189 eq::EqualityEngine * ee = getEqualityEngine();
190 if( ee->hasTerm( t ) ){
191 TNode grt = ee->getRepresentative( t );
192 if( t!=grt ){
193 Node exp;
194 d_uequalityEngine.assertEquality( t.eqNode( grt ), true, exp );
195 }
196 }
197 */
198 }
199 }
200 }
201
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] );
208 }
209 }
210
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() );
217
218 if( d_pattern_is_normal[rt1] && !d_pattern_is_normal[rt2] ){
219 Trace("thm-ee-debug") << "UEE : LT due to normal." << std::endl;
220 return true;
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
225 return true;
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 );
229 }
230 }
231 return false;
232 }
233
234
235 bool ConjectureGenerator::isReportedCanon( TNode n ) {
236 return std::find( d_ue_canon.begin(), d_ue_canon.end(), n )==d_ue_canon.end();
237 }
238
239 void ConjectureGenerator::markReportedCanon( TNode n ) {
240 if( !isReportedCanon( n ) ){
241 d_ue_canon.push_back( n );
242 }
243 }
244
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 ) );
247 }
248
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 );
251 }
252
253 TNode ConjectureGenerator::getUniversalRepresentative( TNode n, bool add ) {
254 if( 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();
261 }
262 }
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();
268 }else{
269 return r;
270 }
271 }else{
272 return n;
273 }
274 }
275
276 eq::EqualityEngine * ConjectureGenerator::getEqualityEngine() {
277 return d_quantEngine->getTheoryEngine()->getMasterEqualityEngine();
278 }
279
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 ) );
283 }
284
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 );
288 }
289
290 TNode ConjectureGenerator::getRepresentative( TNode n ) {
291 eq::EqualityEngine * ee = getEqualityEngine();
292 if( ee->hasTerm( n ) ){
293 return ee->getRepresentative( n );
294 }else{
295 return n;
296 }
297 }
298
299 TermDb * ConjectureGenerator::getTermDatabase() {
300 return d_quantEngine->getTermDatabase();
301 }
302
303 Node ConjectureGenerator::getFreeVar( TypeNode tn, unsigned i ) {
304 Assert( !tn.isNull() );
305 while( d_free_var[tn].size()<=i ){
306 std::stringstream oss;
307 oss << tn;
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 );
313 }
314 return d_free_var[tn][i];
315 }
316
317
318
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();
325 //allocate variable
326 unsigned vn = var_count[tn];
327 var_count[tn]++;
328 subs[n] = getFreeVar( tn, vn );
329 return subs[n];
330 }else{
331 return it->second;
332 }
333 }else{
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() ){
339 return Node::null();
340 }
341 children.push_back( op );
342 }else{
343 return Node::null();
344 }
345 }
346 for( unsigned i=0; i<n.getNumChildren(); i++ ){
347 Node cn = getCanonicalTerm( n[i], var_count, subs );
348 if( cn.isNull() ){
349 return Node::null();
350 }else{
351 children.push_back( cn );
352 }
353 }
354 return NodeManager::currentNM()->mkNode( n.getKind(), children );
355 }
356 }
357
358 bool ConjectureGenerator::isHandledTerm( TNode n ){
359 return !n.getAttribute(NoMatchAttribute()) && inst::Trigger::isAtomicTrigger( n ) && ( n.getKind()!=APPLY_UF || n.getOperator().getKind()!=SKOLEM );
360 }
361
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();
365 }
366
367 bool ConjectureGenerator::isGroundEqc( TNode r ) {
368 return d_ground_eqc_map.find( r )!=d_ground_eqc_map.end();
369 }
370
371 bool ConjectureGenerator::isGroundTerm( TNode n ) {
372 return std::find( d_ground_terms.begin(), d_ground_terms.end(), n )!=d_ground_terms.end();
373 }
374
375 bool ConjectureGenerator::needsCheck( Theory::Effort e ) {
376 return e==Theory::EFFORT_FULL;
377 }
378
379 void ConjectureGenerator::reset_round( Theory::Effort e ) {
380
381 }
382
383 void ConjectureGenerator::check( Theory::Effort e, unsigned quant_e ) {
384 if( quant_e==QuantifiersEngine::QEFFORT_STANDARD ){
385 d_fullEffortCount++;
386 if( d_fullEffortCount%optFullCheckFrequency()==0 ){
387 Trace("sg-engine") << "---Conjecture generator, effort = " << e << "--- " << std::endl;
388 eq::EqualityEngine * ee = getEqualityEngine();
389
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;
396 d_em.clear();
397 eq::EqClassesIterator eqcs_i = eq::EqClassesIterator( ee );
398 while( !eqcs_i.isFinished() ){
399 TNode r = (*eqcs_i);
400 eqcs.push_back( r );
401 if( r.getType().isBoolean() ){
402 if( areEqual( r, getTermDatabase()->d_true ) ){
403 d_ground_eqc_map[r] = getTermDatabase()->d_true;
404 d_bool_eqc[0] = r;
405 }else if( areEqual( r, getTermDatabase()->d_false ) ){
406 d_ground_eqc_map[r] = getTermDatabase()->d_false;
407 d_bool_eqc[1] = r;
408 }
409 }
410 d_em[r] = eqcs.size();
411 eq::EqClassIterator ieqc_i = eq::EqClassIterator( r, ee );
412 while( !ieqc_i.isFinished() ){
413 TNode n = (*ieqc_i);
414 if( isHandledTerm( n ) ){
415 d_op_arg_index[r].addTerm( this, n );
416 }
417 ++ieqc_i;
418 }
419 ++eqcs_i;
420 }
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;
425
426 Trace("sg-proc") << "Determine ground EQC..." << std::endl;
427 bool success;
428 do{
429 success = false;
430 for( unsigned i=0; i<eqcs.size(); i++ ){
431 TNode r = eqcs[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;
435 Node n;
436 if( getTermDatabase()->isInductionTerm( r ) ){
437 n = d_op_arg_index[r].getGroundTerm( this, args );
438 }else{
439 n = r;
440 }
441 if( !n.isNull() ){
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;
445 success = true;
446 }else{
447 Trace("sg-pat-debug") << "...could not find ground term." << std::endl;
448 }
449 }
450 }
451 }while( success );
452 //also get ground terms
453 d_ground_terms.clear();
454 for( unsigned i=0; i<eqcs.size(); i++ ){
455 TNode r = eqcs[i];
456 d_op_arg_index[r].getGroundTerms( this, d_ground_terms );
457 }
458 Trace("sg-proc") << "...done determine ground EQC" << std::endl;
459
460 //debug printing
461 if( Trace.isOn("sg-gen-eqc") ){
462 for( unsigned i=0; i<eqcs.size(); i++ ){
463 TNode r = eqcs[i];
464 //print out members
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() ){
469 TNode n = (*eqc_i);
470 if( !n.getAttribute(NoMatchAttribute()) && ( n.getKind()!=EQUAL || isFalse ) ){
471 if( firstTime ){
472 Trace("sg-gen-eqc") << "e" << d_em[r] << " : { " << std::endl;
473 firstTime = false;
474 }
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]];
480 }
481 Trace("sg-gen-eqc") << ") :: " << n << std::endl;
482 }else{
483 Trace("sg-gen-eqc") << " " << n << std::endl;
484 }
485 }
486 ++eqc_i;
487 }
488 if( !firstTime ){
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;
494 }
495 }
496 }
497 }
498
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++ ){
503 TNode r = eqcs[i];
504 std::map< TNode, Node >::iterator it = d_ground_eqc_map.find( r );
505 unsigned index = 1;
506 if( it==d_ground_eqc_map.end() ){
507 index = 0;
508 }
509 //based on unproven conjectures? TODO
510 d_relevant_eqc[index].push_back( r );
511 }
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]] << " ";
515 }
516 Trace("sg-gen-tg-debug") << std::endl;
517 Trace("sg-proc") << "...done compute relevant eqc" << std::endl;
518
519
520 Trace("sg-proc") << "Collect signature information..." << std::endl;
521 d_funcs.clear();
522 d_typ_funcs.clear();
523 d_func_kind.clear();
524 d_func_args.clear();
525 TypeNode tnull;
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() );
535 }
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 );
539 }
540 }
541 }
542 //shuffle functions
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] << " ";
549 }
550 Trace("sg-gen-tg-debug") << std::endl;
551 }
552 }
553 Trace("sg-proc") << "...done collect signature information" << std::endl;
554
555
556 Trace("sg-proc") << "Build theorem index..." << std::endl;
557 d_ue_canon.clear();
558 d_thm_index.clear();
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;
564 Node conjEq;
565 if( q[1].getKind()==EQUAL ){
566 bool isSubsume = false;
567 bool inEe = 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 );
578 }
579 if( !eq.isNull() ){
580 if( r==0 ){
581 inEe = d_ee_conjectures.find( q[1] )!=d_ee_conjectures.end();
582 if( !inEe ){
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 ) ){
587 isSubsume = true;
588 //set inactive (will be ignored by other modules)
589 d_quantEngine->getModel()->setQuantifierActive( q, false );
590 }else{
591 Node exp;
592 d_ee_conjectures[q[1]] = true;
593 d_uequalityEngine.assertEquality( nl.eqNode( nr ), true, exp );
594 }
595 }
596 Trace("sg-conjecture") << "*** CONJECTURE : currently proven" << (isSubsume ? " and subsumed" : "");
597 Trace("sg-conjecture") << " : " << q[1] << std::endl;
598 provenConj.push_back( q );
599 }
600 if( !isSubsume ){
601 Trace("thm-db-debug") << "Adding theorem to database " << eq[0] << " == " << eq[1] << std::endl;
602 d_thm_index.addTheorem( eq[0], eq[1] );
603 }else{
604 break;
605 }
606 }else{
607 break;
608 }
609 }
610 }
611 }
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") << "}";
633 }
634 Trace("sg-conjecture") << std::endl;
635 disproven = false;
636 break;
637 }else{
638 ce.push_back( git->second );
639 }
640 }
641 if( disproven ){
642 Trace("sg-conjecture") << "disproven : " << q << " : ";
643 for( unsigned i=0; i<ce.size(); i++ ){
644 Trace("sg-conjecture") << q[0][i] << " -> " << ce[i] << " ";
645 }
646 Trace("sg-conjecture") << std::endl;
647 }
648 }
649 }
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;
654
655 //clear patterns
656 d_patterns.clear();
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();
670
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] );
676 }
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() );
682 }
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;
693 //set up environment
694 d_var_id.clear();
695 d_var_limit.clear();
696 d_tg_id = 0;
697 d_tg_gdepth = 0;
698 d_tg_gdepth_limit = depth;
699 //consider all types
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 ){
704 //construct term
705 Node nn = d_tg_alloc[0].getTerm( this );
706 if( getUniversalRepresentative( nn )==nn ){
707 d_rel_term_count++;
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;
711
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]] << " ";
717 }
718 Trace("sg-gen-tg-eqc") << std::endl;
719 }
720 TypeNode tnn = nn.getType();
721 Trace("sg-gen-tg-debug") << "...term is " << nn << std::endl;
722 conj_lhs[depth].push_back( nn );
723
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
729 unsigned sum = 0;
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 ){
733 if( it->second>0 ){
734 typ_to_subs_index[it->first] = sum;
735 sum += it->second;
736 for( unsigned i=0; i<it->second; i++ ){
737 gsubs_vars.push_back( getFreeVar( it->first, i ) );
738 }
739 }
740 }
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;
746
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];
752 //record maximum
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;
755 }
756 }
757 if( std::find( rt_types.begin(), rt_types.end(), tnn )==rt_types.end() ){
758 rt_types.push_back( tnn );
759 }
760 pti->d_terms.push_back( nn );
761 Trace("sg-gen-tg-debug") << "...done collect type information" << std::endl;
762
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 ){
781 if( !firstTime ){
782 Trace("sg-gen-tg-eqc") << ", ";
783 }else{
784 firstTime = false;
785 Trace("sg-gen-tg-eqc") << " ";
786 }
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;
790 }
791 }
792 Trace("sg-gen-tg-eqc") << std::endl;
793 d_rel_pattern_subs_index[nn].addSubstitution( r, gsubs_vars, gsubs_terms );
794 }
795 }
796 Trace("sg-gen-tg-debug") << "...done build substitutions for ground EQC" << std::endl;
797 }else{
798 Trace("sg-gen-tg-debug") << "> not canonical : " << nn << std::endl;
799 }
800 }else{
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;
804 }
805 }
806 Trace("sg-proc") << "...done generate terms at depth " << depth << std::endl;
807
808 //now generate right hand sides
809
810
811 }
812 Trace("sg-stats") << "--------> Total relevant patterns : " << d_rel_term_count << std::endl;
813
814 Trace("sg-proc") << "Generate properties..." << std::endl;
815 //set up environment
816 d_use_ccand_eqc = false;
817 d_var_id.clear();
818 d_var_limit.clear();
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;
822 }
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();
827 //}
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;
836 d_tg_id = 0;
837 d_tg_gdepth = 0;
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 );
845 //register pattern
846 Assert( rhs.getType()==rt_types[i] );
847 registerPattern( rhs, rt_types[i] );
848 totalCount++;
849 }
850 }
851 //could have been partial, we must clear
852 d_tg_alloc.clear();
853 }
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 );
860 }
861 }
862 }
863 Trace("sg-proc") << "...done process candidate conjectures at RHS term depth " << depth << std::endl;
864 }
865 Trace("sg-proc") << "...done generate properties" << std::endl;
866
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 );
875 }
876 d_waiting_conjectures.clear();
877 }
878
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() ){
890 TNode n = (*ueqc_i);
891 if( r!=n ){
892 if( firstTime ){
893 Trace("thm-ee") << std::endl;
894 firstTime = false;
895 }
896 Trace("thm-ee") << " " << n << std::endl;
897 }
898 ++ueqc_i;
899 }
900 if( !firstTime ){ Trace("thm-ee") << " "; }
901 Trace("thm-ee") << "}" << std::endl;
902 ++ueqcs_i;
903 }
904 Trace("thm-ee") << std::endl;
905 }
906 }
907 }
908
909 void ConjectureGenerator::registerQuantifier( Node q ) {
910
911 }
912
913 void ConjectureGenerator::assertNode( Node n ) {
914
915 }
916
917
918 unsigned ConjectureGenerator::getNumTgVars( TypeNode tn ) {
919 //return d_var_tg.size();
920 return d_var_id[tn];
921 }
922
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() ){
926 return true;
927 }else{
928 return d_var_id[tn]<it->second;
929 }
930 }
931
932 void ConjectureGenerator::addVar( TypeNode tn ) {
933 //d_var_tg.push_back( v );
934 d_var_id[tn]++;
935 //d_var_eq_tg.push_back( std::vector< unsigned >() );
936 }
937
938 void ConjectureGenerator::removeVar( TypeNode tn ) {
939 d_var_id[tn]--;
940 //d_var_eq_tg.pop_back();
941 //d_var_tg.pop_back();
942 }
943
944 unsigned ConjectureGenerator::getNumTgFuncs( TypeNode tn ) {
945 return d_typ_tg_funcs[tn].size();
946 }
947
948 TNode ConjectureGenerator::getTgFunc( TypeNode tn, unsigned i ) {
949 return d_typ_tg_funcs[tn][i];
950 }
951
952 bool ConjectureGenerator::considerCurrentTerm() {
953 Assert( !d_tg_alloc.empty() );
954
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;
961
962 Assert( d_tg_alloc[0].getGeneralizationDepth( this )==d_tg_gdepth );
963
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;
968 return false;
969 }
970
971 //----optimizations
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 ){
975 }else{
976 Trace("sg-gen-tg-debug") << "Bad tg: " << &d_tg_alloc[i-1] << std::endl;
977 Assert( false );
978 }
979 //if equated two variables, first check if context-independent TODO
980 //----end optimizations
981
982
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;
987
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();
994 }
995
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;
1001 unsigned mode;
1002 if( r==0 ){
1003 mode = optReqDistinctVarPatterns() ? 1 : 0;
1004 }else{
1005 mode = (optFilterConfirmation() && optFilterConfirmationOnlyGround() ) ? 2 : 0;
1006 }
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] );
1010 }
1011 }
1012 }
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]] << " ";
1017 }
1018 Trace("sg-gen-tg-debug") << std::endl;
1019 }
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;
1024 return false;
1025 }
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;
1030 return false;
1031 }
1032 }
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;
1037 return true;
1038 }
1039
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;
1046
1047 Node ln = d_tg_alloc[tg_id].getTerm( this );
1048 Trace("sg-gen-tg-debug") << "Term is " << ln << std::endl;
1049 if( !ln.isNull() ){
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 );
1054 if( lnr==ln ){
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;
1060 return false;
1061 }
1062 }
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;
1067 return true;
1068 }
1069
1070 void ConjectureGenerator::changeContext( bool add ) {
1071 if( add ){
1072 for( unsigned r=0; r<2; r++ ){
1073 d_ccand_eqc[r].push_back( std::vector< TNode >() );
1074 }
1075 d_tg_id++;
1076 }else{
1077 for( unsigned r=0; r<2; r++ ){
1078 d_ccand_eqc[r].pop_back();
1079 }
1080 d_tg_id--;
1081 Assert( d_tg_alloc.find( d_tg_id )!=d_tg_alloc.end() );
1082 d_tg_alloc.erase( d_tg_id );
1083 }
1084 }
1085
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()]++;
1090 unsigned sum = 1;
1091 for( unsigned i=0; i<pat.getNumChildren(); i++ ){
1092 sum += collectFunctions( opat, pat[i], funcs, mnvn, mxvn );
1093 }
1094 return sum;
1095 }else{
1096 Assert( pat.getNumChildren()==0 );
1097 funcs[pat]++;
1098 //for variables
1099 if( pat.getKind()==BOUND_VARIABLE ){
1100 if( funcs[pat]>1 ){
1101 //duplicate variable
1102 d_pattern_var_duplicate[opat]++;
1103 }else{
1104 //check for max/min
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;
1111 mnvn[tn] = vn;
1112 }else if( vn>mxvn[tn] ){
1113 if( vn!=mxvn[tn]+1 ){
1114 d_pattern_is_normal[opat] = false;
1115 }
1116 mxvn[tn] = vn;
1117 }
1118 }else{
1119 //first variable of this type
1120 mnvn[tn] = vn;
1121 mxvn[tn] = vn;
1122 }
1123 }
1124 }
1125 return 1;
1126 }
1127 }
1128
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 );
1133
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() );
1136
1137 //collect functions
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;
1142 }
1143 }
1144 }
1145
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;
1151 }else{
1152 subs[patg] = pat;
1153 return true;
1154 }
1155 }else{
1156 Assert( patg.hasOperator() );
1157 if( !pat.hasOperator() || patg.getOperator()!=pat.getOperator() ){
1158 return false;
1159 }else{
1160 Assert( patg.getNumChildren()==pat.getNumChildren() );
1161 for( unsigned i=0; i<patg.getNumChildren(); i++ ){
1162 if( !isGeneralization( patg[i], pat[i], subs ) ){
1163 return false;
1164 }
1165 }
1166 return true;
1167 }
1168 }
1169 }
1170
1171 void ConjectureGenerator::processCandidateConjecture( TNode lhs, TNode rhs, unsigned lhs_depth, unsigned rhs_depth ) {
1172 if( d_waiting_conjectures.size()>=optFullCheckConjectures() ){
1173 return;
1174 }
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 ){
1183 if( !firstTime ){
1184 Trace("sg-conjecture-debug") << ", ";
1185 }
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] << ")";
1189 }
1190 firstTime = false;
1191 }
1192 Trace("sg-conjecture-debug") << std::endl;
1193 Trace("sg-conjecture-debug") << " unknown = " << d_subs_unkCount << std::endl;
1194 }
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 ) );
1205 }
1206 }
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 );
1211 }
1212 }
1213
1214 bool ConjectureGenerator::considerCandidateConjecture( TNode lhs, TNode rhs ) {
1215 Trace("sg-cconj-debug") << "Consider candidate conjecture : " << lhs << " == " << rhs << "?" << std::endl;
1216 if( lhs==rhs ){
1217 Trace("sg-cconj-debug") << " -> trivial." << std::endl;
1218 return false;
1219 }else{
1220 if( lhs.getKind()==APPLY_CONSTRUCTOR && rhs.getKind()==APPLY_CONSTRUCTOR ){
1221 Trace("sg-cconj-debug") << " -> irrelevant by syntactic analysis." << std::endl;
1222 return false;
1223 }
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;
1230 return false;
1231 }else{
1232 Trace("sg-cconj-debug2") << " variables of sort " << it->first << " are : " << itl->second << " vs " << it->second << std::endl;
1233 }
1234 }else{
1235 Trace("sg-cconj-debug") << " -> has no variables of sort " << it->first << "." << std::endl;
1236 return false;
1237 }
1238 }
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;
1244 return false;
1245 }
1246 }
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;
1259 return false;
1260 }
1261 if( optFilterConfirmation() ){
1262 if( d_subs_confirmCount==0 ){
1263 Trace("sg-cconj") << " -> not confirmed by a ground substitutions." << std::endl;
1264 return false;
1265 }
1266 }
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;
1276 }
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;
1279 return false;
1280 }
1281 if( it->second.size()==1 ){
1282 vars.push_back( it->first );
1283 subs.push_back( it->second[0] );
1284 }
1285 }
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;
1293 return false;
1294 }else{
1295 Trace("sg-cconj-debug") << "Checked if " << slhsr << " and " << srhsr << " were equal." << std::endl;
1296 }
1297 }
1298 }
1299 }
1300
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;
1306 }
1307 }
1308
1309 /*
1310 if( getUniversalRepresentative( lhs )!=lhs ){
1311 std::cout << "bad universal representative LHS : " << lhs << " " << getUniversalRepresentative( lhs ) << std::endl;
1312 exit(0);
1313 }
1314 if( getUniversalRepresentative( rhs )!=rhs ){
1315 std::cout << "bad universal representative RHS : " << rhs << " " << getUniversalRepresentative( rhs ) << std::endl;
1316 exit(0);
1317 }
1318 */
1319
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;
1323 }
1324
1325 return true;
1326 }
1327 }
1328
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;
1335 }
1336 }
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() ){
1342 if( glhs!=grhs ){
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;
1356 }
1357 return false;
1358 }else{
1359 d_subs_unkCount++;
1360 if( optFilterUnknown() ){
1361 return false;
1362 }
1363 }
1364 }
1365 }
1366 }
1367 /*
1368 if( getEqualityEngine()->areDisequal( glhs, grhs, false ) ){
1369 Trace("sg-cconj-debug") << "..." << glhs << " and " << grhs << " are disequal." << std::endl;
1370 return false;
1371 }else{
1372 Trace("sg-cconj-debug") << "..." << glhs << " and " << grhs << " are not disequal." << std::endl;
1373 }
1374 */
1375 }else{
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 );
1381 }
1382 }
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 );
1386 }
1387 Trace("sg-cconj-debug") << "RHS is identical." << std::endl;
1388 }
1389 }else{
1390 Trace("sg-cconj-debug") << "(could not ground eqc for RHS)." << std::endl;
1391 }
1392 return true;
1393 }
1394
1395
1396 void TermGenerator::reset( ConjectureGenerator * s, TypeNode tn ) {
1397 Assert( d_children.empty() );
1398 d_typ = tn;
1399 d_status = 0;
1400 d_status_num = 0;
1401 d_children.clear();
1402 Trace("sg-gen-tg-debug2") << "...add to context " << this << std::endl;
1403 d_id = s->d_tg_id;
1404 s->changeContext( true );
1405 }
1406
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;
1410 if( d_status==5 ){
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();
1416 }
1417 Trace("sg-gen-tg-debug2") << std::endl;
1418 }
1419
1420 if( d_status==0 ){
1421 d_status++;
1422 if( !d_typ.isNull() ){
1423 if( s->allowVar( d_typ ) ){
1424 //allocate variable
1425 d_status_num = s->d_var_id[d_typ];
1426 s->addVar( 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 );
1429 }else{
1430 //check allocating new variable
1431 d_status++;
1432 d_status_num = -1;
1433 s->d_tg_gdepth++;
1434 return getNextTerm( s, depth );
1435 }
1436 }else{
1437 d_status = 4;
1438 d_status_num = -1;
1439 return getNextTerm( s, depth );
1440 }
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();
1445 //}
1446 //check if there is another variable
1447 if( (d_status_num+1)<(int)s->getNumTgVars( d_typ ) ){
1448 d_status_num++;
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 );
1453 }else{
1454 s->d_tg_gdepth--;
1455 d_status++;
1456 return getNextTerm( s, depth );
1457 }
1458 }else if( d_status==4 ){
1459 d_status++;
1460 if( depth>0 && (d_status_num+1)<(int)s->getNumTgFuncs( d_typ ) ){
1461 d_status_num++;
1462 d_status_child_num = 0;
1463 Trace("sg-gen-tg-debug2") << this << "...consider function " << s->getTgFunc( d_typ, d_status_num ) << std::endl;
1464 s->d_tg_gdepth++;
1465 if( !s->considerCurrentTerm() ){
1466 s->d_tg_gdepth--;
1467 //don't consider this function
1468 d_status--;
1469 }else{
1470 //we have decided on a function application
1471 }
1472 return getNextTerm( s, depth );
1473 }else{
1474 //do not choose function applications at depth 0
1475 d_status++;
1476 return getNextTerm( s, depth );
1477 }
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 ){
1482 //no more arguments
1483 s->d_tg_gdepth--;
1484 d_status--;
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 );
1489 //return true;
1490 }else{
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 );
1497 }else{
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 );
1502 }else{
1503 d_children.pop_back();
1504 d_status_child_num--;
1505 return getNextTerm( s, depth );
1506 }
1507 }
1508 }
1509 }else if( d_status==1 || d_status==3 ){
1510 if( d_status==1 ){
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;
1517 // d_status = 6;
1518 // return getNextTerm( s, depth );
1519 //}
1520 s->d_tg_gdepth++;
1521 }
1522 d_status++;
1523 d_status_num = -1;
1524 return getNextTerm( s, depth );
1525 }else{
1526 //clean up
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 );
1531 return false;
1532 }
1533 }
1534
1535 void TermGenerator::resetMatching( ConjectureGenerator * s, TNode eqc, unsigned mode ) {
1536 d_match_status = 0;
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;
1544 }
1545 }
1546
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 ){
1549 return false;
1550 }
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;
1556 if( d_status==5 ){
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();
1562 }
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];
1567 }
1568 }
1569 Trace("sg-gen-tg-debug2") << " } " << std::endl;
1570 }
1571 if( d_status==1 ){
1572 //a variable
1573 if( d_match_status==0 ){
1574 d_match_status++;
1575 if( d_match_mode>=2 ){
1576 //only ground terms
1577 if( !s->isGroundEqc( eqc ) ){
1578 return false;
1579 }
1580 }
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;
1585 }else{
1586 return false;
1587 }
1588 }
1589 Assert( subs[d_typ].find( d_status_num )==subs[d_typ].end() );
1590 subs[d_typ][d_status_num] = eqc;
1591 return true;
1592 }else{
1593 //clean up
1594 subs[d_typ].erase( d_status_num );
1595 if( d_match_mode%2==1 ){
1596 rev_subs.erase( eqc );
1597 }
1598 return false;
1599 }
1600 }else if( d_status==2 ){
1601 if( d_match_status==0 ){
1602 d_match_status++;
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;
1607 }else{
1608 return false;
1609 }
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 ){
1614 return false;
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 ){
1619 //initial binding
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() );
1625 }else{
1626 d_match_status++;
1627 d_match_status_child_num--;
1628 return getNextMatch( s, eqc, subs, rev_subs );
1629 }
1630 }else{
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() );
1633 }
1634 }
1635 d_match_status++;
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 );
1643 }else{
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];
1650 }else{
1651 //do next binding
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 );
1654 }
1655 }
1656 }else{
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] );
1660 d_match_status--;
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 );
1664 }else{
1665 //iterate
1666 d_match_children[d_match_status_child_num]++;
1667 return getNextMatch( s, eqc, subs, rev_subs );
1668 }
1669 }
1670 }
1671 Assert( false );
1672 return false;
1673 }
1674
1675 unsigned TermGenerator::getDepth( ConjectureGenerator * s ) {
1676 if( d_status==5 ){
1677 unsigned maxd = 0;
1678 for( unsigned i=0; i<d_children.size(); i++ ){
1679 unsigned d = s->d_tg_alloc[d_children[i]].getDepth( s );
1680 if( d>maxd ){
1681 maxd = d;
1682 }
1683 }
1684 return 1+maxd;
1685 }else{
1686 return 0;
1687 }
1688 }
1689
1690 unsigned TermGenerator::getGeneralizationDepth( ConjectureGenerator * s ) {
1691 if( d_status==5 ){
1692 unsigned sum = 1;
1693 for( unsigned i=0; i<d_children.size(); i++ ){
1694 sum += s->d_tg_alloc[d_children[i]].getGeneralizationDepth( s );
1695 }
1696 return sum;
1697 }else if( d_status==2 ){
1698 return 1;
1699 }else{
1700 Assert( d_status==1 );
1701 return 0;
1702 }
1703 }
1704
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 );
1716 if( nc.isNull() ){
1717 return Node::null();
1718 }else{
1719 //Assert( nc.getType()==s->d_func_args[f][i] );
1720 children.push_back( nc );
1721 }
1722 }
1723 return NodeManager::currentNM()->mkNode( s->d_func_kind[f], children );
1724 }
1725 }else{
1726 Assert( false );
1727 }
1728 return Node::null();
1729 }
1730
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++ ){
1739 Trace(c) << " ";
1740 s->d_tg_alloc[d_children[i]].debugPrint( s, c, cd );
1741 }
1742 if( d_children.size()<s->d_func_args[f].size() ){
1743 Trace(c) << " ...";
1744 }
1745 Trace(c) << ")";
1746 }else{
1747 Trace(c) << "???";
1748 }
1749 }
1750
1751
1752 void SubstitutionIndex::addSubstitution( TNode eqc, std::vector< TNode >& vars, std::vector< TNode >& terms, unsigned i ) {
1753 if( i==vars.size() ){
1754 d_var = eqc;
1755 }else{
1756 Assert( d_var.isNull() || d_var==vars[i] );
1757 d_var = vars[i];
1758 d_children[terms[i]].addSubstitution( eqc, vars, terms, i+1 );
1759 }
1760 }
1761
1762 bool SubstitutionIndex::notifySubstitutions( ConjectureGenerator * s, std::map< TNode, TNode >& subs, TNode rhs, unsigned numVars, unsigned i ) {
1763 if( i==numVars ){
1764 Assert( d_children.empty() );
1765 return s->notifySubstitution( d_var, subs, rhs );
1766 }else{
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 ) ){
1772 return false;
1773 }
1774 }
1775 return true;
1776 }
1777 }
1778
1779
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 );
1784 }
1785 }else{
1786 unsigned index = lhs_v.size()-1;
1787 if( lhs_arg[index]==lhs_v[index].getNumChildren() ){
1788 lhs_v.pop_back();
1789 lhs_arg.pop_back();
1790 addTheorem( lhs_v, lhs_arg, rhs );
1791 }else{
1792 lhs_arg[index]++;
1793 addTheoremNode( lhs_v[index][lhs_arg[index]-1], lhs_v, lhs_arg, rhs );
1794 }
1795 }
1796 }
1797
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 );
1804 }else{
1805 Assert( curr.getKind()==kind::BOUND_VARIABLE );
1806 Assert( d_var.isNull() || d_var==curr );
1807 d_var = curr;
1808 d_children[curr].addTheorem( lhs_v, lhs_arg, rhs );
1809 }
1810 }
1811
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;
1816 if( n_v.empty() ){
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 );
1822 }
1823 }else{
1824 unsigned index = n_v.size()-1;
1825 if( n_arg[index]==n_v[index].getNumChildren() ){
1826 n_v.pop_back();
1827 n_arg.pop_back();
1828 getEquivalentTerms( n_v, n_arg, smap, vars, subs, terms );
1829 }else{
1830 n_arg[index]++;
1831 getEquivalentTermsNode( n_v[index][n_arg[index]-1], n_v, n_arg, smap, vars, subs, terms );
1832 }
1833 }
1834 }
1835
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 );
1847 }
1848 Trace("thm-db-debug") << "...done check based on operator" << std::endl;
1849 }
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() ){
1857 smap[d_var] = curr;
1858 vars.push_back( d_var );
1859 subs.push_back( curr );
1860 success = true;
1861 }else if( it->second==curr ){
1862 success = true;
1863 }else{
1864 //also check modulo equality (in universal equality engine)
1865 }
1866 Trace("thm-db-debug") << "...check for substitution with " << d_var << ", success = " << success << "." << std::endl;
1867 if( success ){
1868 d_children[d_var].getEquivalentTerms( n_v, n_arg, smap, vars, subs, terms );
1869 }
1870 }
1871 }
1872 }
1873
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 );
1879 }
1880 if( !d_terms.empty() ){
1881 for( unsigned i=0; i<ind; i++ ){ Trace(c) << " "; }
1882 Trace(c) << "{";
1883 for( unsigned i=0; i<d_terms.size(); i++ ){
1884 Trace(c) << " " << d_terms[i];
1885 }
1886 Trace(c) << " }" << std::endl;
1887 }
1888 //if( !d_var.isNull() ){
1889 // for( unsigned i=0; i<ind; i++ ){ Trace(c) << " "; }
1890 // Trace(c) << "var:" << d_var << std::endl;
1891 //}
1892 }
1893
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(); }
1903
1904 }
1905
1906