00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef _cvc3__include__theory_quant_h_
00025 #define _cvc3__include__theory_quant_h_
00026
00027 #include "theory.h"
00028 #include "cdmap.h"
00029 #include "statistics.h"
00030 #include<queue>
00031
00032 namespace CVC3 {
00033
00034 class QuantProofRules;
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 typedef enum{ Ukn, Pos, Neg, PosNeg} Polarity;
00049
00050 class Trigger {
00051 public:
00052 Expr trig;
00053 Polarity polarity;
00054
00055 std::vector<Expr> bvs;
00056 Expr head;
00057 bool hasRWOp;
00058 bool hasTrans;
00059 bool hasT2;
00060 bool isSimple;
00061 bool isSuperSimple;
00062 bool isMulti;
00063 size_t multiIndex;
00064 size_t multiId;
00065
00066 Trigger(TheoryCore* core, Expr e, Polarity pol, std::set<Expr>);
00067 bool isPos();
00068 bool isNeg();
00069 Expr getEx();
00070 std::vector<Expr> getBVs();
00071 void setHead(Expr h);
00072 Expr getHead();
00073 void setRWOp(bool b);
00074 bool hasRW();
00075 void setTrans(bool b);
00076 bool hasTr();
00077 void setTrans2(bool b);
00078 bool hasTr2();
00079 void setSimp();
00080 bool isSimp();
00081 void setSuperSimp();
00082 bool isSuperSimp();
00083 void setMultiTrig();
00084 bool isMultiTrig();
00085
00086
00087 };
00088
00089 typedef struct dynTrig{
00090 Trigger trig;
00091 size_t univ_id;
00092 ExprMap<Expr> binds;
00093 dynTrig(Trigger t, ExprMap<Expr> b, size_t id);
00094 } dynTrig;
00095
00096
00097 class CompleteInstPreProcessor {
00098
00099 TheoryCore* d_theoryCore;
00100 QuantProofRules* d_quant_rules;
00101
00102 std::set<Expr> d_allIndex;
00103
00104 ExprMap<Polarity> d_expr_pol ;
00105
00106 ExprMap<Expr> d_quant_equiv_map ;
00107
00108 std::vector<Expr> d_gnd_cache;
00109
00110 ExprMap<bool> d_is_macro_def;
00111
00112 ExprMap<Expr> d_macro_quant;
00113
00114 ExprMap<Expr> d_macro_def;
00115
00116 ExprMap<Expr> d_macro_lhs;
00117
00118
00119 bool d_all_good ;
00120
00121
00122 bool isShield(const Expr& e);
00123
00124
00125 void addIndex(const Expr& e);
00126
00127 void collect_shield_index(const Expr& e);
00128
00129 void collect_forall_index(const Expr& forall_quant);
00130
00131
00132 bool isGoodQuant(const Expr& e);
00133
00134
00135 Expr plusOne(const Expr& e);
00136
00137
00138 Expr minusOne(const Expr& e);
00139
00140 void collectHeads(const Expr& assert, std::set<Expr>& heads);
00141
00142
00143 bool isMacro(const Expr& assert);
00144
00145 Expr recInstMacros(const Expr& assert);
00146
00147 Expr substMacro(const Expr&);
00148
00149 Expr recRewriteNot(const Expr&, ExprMap<Polarity>&);
00150
00151
00152 Expr rewriteNot(const Expr &);
00153
00154 Expr recSkolemize(const Expr &, ExprMap<Polarity>&);
00155
00156 Expr pullVarOut(const Expr&);
00157
00158 public :
00159
00160 CompleteInstPreProcessor(TheoryCore * , QuantProofRules*);
00161
00162
00163 bool isGood(const Expr& e);
00164
00165
00166 void collectIndex(const Expr & e);
00167
00168
00169 Expr inst(const Expr & e);
00170
00171
00172 bool hasMacros(const std::vector<Expr>& asserts);
00173
00174
00175 Expr instMacros(const Expr& , const Expr );
00176
00177
00178 Expr simplifyEq(const Expr &);
00179
00180
00181 Expr simplifyQuant(const Expr &);
00182 };
00183
00184 class TheoryQuant :public Theory {
00185
00186 Theorem rewrite(const Expr& e);
00187
00188 Theorem theoryPreprocess(const Expr& e);
00189
00190 class TypeComp {
00191 public:
00192 bool operator() (const Type t1, const Type t2) const
00193 {return (t1.getExpr() < t2.getExpr()); }
00194 };
00195
00196
00197 typedef std::map<Type, std::vector<size_t>, TypeComp > typeMap;
00198
00199
00200 CDList<Theorem> d_univs;
00201
00202 CDList<Theorem> d_rawUnivs;
00203
00204 CDList<dynTrig> d_arrayTrigs;
00205 CDO<size_t> d_lastArrayPos;
00206
00207
00208 std::queue<Theorem> d_univsQueue;
00209
00210 std::queue<Theorem> d_simplifiedThmQueue;
00211
00212 std::queue<Theorem> d_gUnivQueue;
00213
00214 std::queue<Expr> d_gBindQueue;
00215
00216
00217 ExprMap<std::set<std::vector<Expr> > > d_tempBinds;
00218
00219
00220 CDO<size_t> d_lastPredsPos;
00221
00222 CDO<size_t> d_lastTermsPos;
00223
00224
00225 CDO<size_t> d_lastPartPredsPos;
00226
00227 CDO<size_t> d_lastPartTermsPos;
00228
00229 CDO<size_t> d_univsPartSavedPos;
00230
00231
00232 CDO<size_t> d_lastPartLevel;
00233
00234 CDO<bool> d_partCalled;
00235
00236
00237 CDO<bool> d_maxILReached;
00238
00239
00240
00241
00242 CDList<Expr> d_usefulGterms;
00243
00244
00245 CDO<size_t> d_lastUsefulGtermsPos;
00246
00247
00248 CDO<size_t> d_savedTermsPos;
00249
00250 CDO<size_t> d_univsSavedPos;
00251 CDO<size_t> d_rawUnivsSavedPos;
00252
00253 CDO<size_t> d_univsPosFull;
00254
00255
00256 CDO<size_t> d_univsContextPos;
00257
00258
00259 CDO<int> d_instCount;
00260
00261
00262 int d_inEnd;
00263
00264 int d_allout;
00265
00266
00267 std::map<Type, CDList<size_t>* ,TypeComp> d_contextMap;
00268
00269 CDList<Expr> d_contextTerms;
00270
00271 CDMap<Expr, bool> d_contextCache;
00272
00273
00274 typeMap d_savedMap;
00275 ExprMap<bool> d_savedCache;
00276
00277 std::vector<Expr> d_savedTerms;
00278
00279
00280 ExprMap<std::vector<Expr> > d_insts;
00281
00282
00283 QuantProofRules* d_rules;
00284
00285 const int* d_maxQuantInst;
00286
00287
00288
00289
00290
00291
00292
00293
00294 bool recursiveMap(const Expr& term);
00295
00296
00297
00298
00299
00300
00301 void mapTermsByType(const CDList<Expr>& terms);
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311 void instantiate(Theorem univ, bool all, bool savedMap,
00312 size_t newIndex);
00313
00314 void recInstantiate(Theorem& univ , bool all, bool savedMap,size_t newIndex,
00315 std::vector<Expr>& varReplacements);
00316
00317
00318
00319
00320 void findInstAssumptions(const Theorem& thm);
00321
00322
00323 const bool* d_useNew;
00324 const bool* d_useLazyInst;
00325 const bool* d_useSemMatch;
00326 const bool* d_useCompleteInst;
00327 const bool* d_translate;
00328
00329 const bool* d_usePart;
00330 const bool* d_useMult;
00331
00332 const bool* d_useInstLCache;
00333 const bool* d_useInstGCache;
00334 const bool* d_useInstThmCache;
00335 const bool* d_useInstTrue;
00336 const bool* d_usePullVar;
00337 const bool* d_useExprScore;
00338 const int* d_useTrigLoop;
00339 const int* d_maxInst;
00340
00341 const int* d_maxIL;
00342 const bool* d_useTrans;
00343 const bool* d_useTrans2;
00344 const bool* d_useManTrig;
00345 const bool* d_useGFact;
00346 const int* d_gfactLimit;
00347 const bool* d_useInstAll;
00348 const bool* d_usePolarity;
00349 const bool* d_useEqu;
00350 const bool* d_useNewEqu;
00351 const int* d_maxNaiveCall;
00352 const bool* d_useNaiveInst;
00353
00354
00355 CDO<int> d_curMaxExprScore;
00356
00357 bool d_useFullTrig;
00358 bool d_usePartTrig;
00359 bool d_useMultTrig;
00360
00361
00362 CDMap<Expr, std::vector<Expr> > d_arrayIndic;
00363 void arrayIndexName(const Expr& e);
00364
00365 std::vector<Expr> d_allInsts;
00366
00367 int d_initMaxScore;
00368 int d_offset_multi_trig ;
00369
00370 int d_instThisRound;
00371 int d_callThisRound;
00372
00373 int partial_called;
00374
00375
00376
00377
00378
00379 ExprMap<std::vector<Expr> > d_multTriggers;
00380 ExprMap<std::vector<Expr> > d_partTriggers;
00381
00382 ExprMap<std::vector<Trigger> > d_fullTrigs;
00383
00384 ExprMap<std::vector<Trigger> > d_multTrigs;
00385 ExprMap<std::vector<Trigger> > d_partTrigs;
00386
00387
00388 CDO<size_t> d_exprLastUpdatedPos ;
00389
00390 std::map<ExprIndex, int> d_indexScore;
00391
00392 std::map<ExprIndex, Expr> d_indexExpr;
00393
00394 int getExprScore(const Expr& e);
00395
00396
00397
00398 ExprMap<bool> d_hasTriggers;
00399 ExprMap<bool> d_hasMoreBVs;
00400
00401 int d_trans_num;
00402 int d_trans2_num;
00403
00404 typedef struct{
00405 std::vector<std::vector<size_t> > common_pos;
00406 std::vector<std::vector<size_t> > var_pos;
00407
00408 std::vector<CDMap<Expr, bool>* > var_binds_found;
00409
00410 std::vector<ExprMap<CDList<Expr>* >* > uncomm_list;
00411 Theorem univThm;
00412 size_t univ_id;
00413 } multTrigsInfo ;
00414
00415 ExprMap<multTrigsInfo> d_multitrigs_maps;
00416 std::vector<multTrigsInfo> d_all_multTrigsInfo;
00417
00418 ExprMap<CDList<Expr>* > d_trans_back;
00419 ExprMap<CDList<Expr>* > d_trans_forw;
00420 CDMap<Expr,bool > d_trans_found;
00421 CDMap<Expr,bool > d_trans2_found;
00422
00423
00424 inline bool transFound(const Expr& comb);
00425
00426 inline void setTransFound(const Expr& comb);
00427
00428 inline bool trans2Found(const Expr& comb);
00429
00430 inline void setTrans2Found(const Expr& comb);
00431
00432
00433 inline CDList<Expr> & backList(const Expr& ex);
00434
00435 inline CDList<Expr> & forwList(const Expr& ex);
00436
00437 void inline iterFWList(const Expr& sr, const Expr& dt, size_t univ_id, const Expr& gterm);
00438 void inline iterBKList(const Expr& sr, const Expr& dt, size_t univ_id, const Expr& gterm);
00439
00440 Expr defaultWriteExpr;
00441 Expr defaultReadExpr;
00442 Expr defaultPlusExpr;
00443 Expr defaultMinusExpr ;
00444 Expr defaultMultExpr ;
00445 Expr defaultDivideExpr;
00446 Expr defaultPowExpr ;
00447
00448 Expr getHead(const Expr& e) ;
00449 Expr getHeadExpr(const Expr& e) ;
00450
00451
00452
00453 CDList<Expr> null_cdlist;
00454
00455 Theorem d_transThm;
00456
00457 inline void pushBackList(const Expr& node, Expr ex);
00458 inline void pushForwList(const Expr& node, Expr ex);
00459
00460
00461 ExprMap<CDList<std::vector<Expr> >* > d_mtrigs_inst;
00462
00463 ExprMap<CDList<Expr>* > d_same_head_expr;
00464 ExprMap<CDList<Expr>* > d_eq_list;
00465
00466 CDList<Theorem> d_eqsUpdate;
00467 CDO<size_t> d_lastEqsUpdatePos;
00468
00469 CDList<Expr > d_eqs;
00470 CDO<size_t > d_eqs_pos;
00471
00472 ExprMap<CDO<size_t>* > d_eq_pos;
00473
00474 ExprMap<CDList<Expr>* > d_parent_list;
00475 void collectChangedTerms(CDList<Expr>& changed_terms);
00476 ExprMap<std::vector<Expr> > d_mtrigs_bvorder;
00477
00478 int loc_gterm(const std::vector<Expr>& border,
00479 const Expr& gterm,
00480 int pos);
00481
00482 void recSearchCover(const std::vector<Expr>& border,
00483 const std::vector<Expr>& mtrigs,
00484 int cur_depth,
00485 std::vector<std::vector<Expr> >& instSet,
00486 std::vector<Expr>& cur_inst
00487 );
00488
00489 void searchCover(const Expr& thm,
00490 const std::vector<Expr>& border,
00491 std::vector<std::vector<Expr> >& instSet
00492 );
00493
00494
00495 std::map<Type, std::vector<Expr>,TypeComp > d_typeExprMap;
00496 std::set<std::string> cacheHead;
00497
00498 StatCounter d_allInstCount ;
00499 StatCounter d_allInstCount2 ;
00500 StatCounter d_totalInstCount ;
00501 StatCounter d_trueInstCount;
00502 StatCounter d_abInstCount;
00503
00504
00505
00506
00507
00508
00509
00510 std::vector<Theorem> d_cacheTheorem;
00511 size_t d_cacheThmPos;
00512
00513 void addNotify(const Expr& e);
00514
00515 int sendInstNew();
00516
00517 CDMap<Expr, std::set<std::vector<Expr> > > d_instHistory;
00518
00519
00520 ExprMap<int> d_thmCount;
00521 ExprMap<size_t> d_totalThmCount;
00522
00523 ExprMap<CDMap<Expr, bool>* > d_bindHistory;
00524 ExprMap<std::hash_map<Expr, bool>* > d_bindGlobalHistory;
00525
00526 ExprMap<std::hash_map<Expr, Theorem>* > d_bindGlobalThmHistory;
00527
00528 ExprMap<std::set<std::vector<Expr> > > d_instHistoryGlobal;
00529
00530
00531 ExprMap<std::vector<Expr> > d_subTermsMap;
00532
00533 const std::vector<Expr>& getSubTerms(const Expr& e);
00534
00535
00536 void simplifyExprMap(ExprMap<Expr>& orgExprMap);
00537 void simplifyVectorExprMap(std::vector<ExprMap<Expr> >& orgVectorExprMap);
00538 std::string exprMap2string(const ExprMap<Expr>& vec);
00539 std::string exprMap2stringSimplify(const ExprMap<Expr>& vec);
00540 std::string exprMap2stringSig(const ExprMap<Expr>& vec);
00541
00542
00543 void enqueueInst(const Theorem, const Theorem);
00544 void enqueueInst(const Theorem& univ, const std::vector<Expr>& bind, const Expr& gterm);
00545 void enqueueInst(size_t univ_id , const std::vector<Expr>& bind, const Expr& gterm);
00546 void enqueueInst(const Theorem& univ,
00547 Trigger& trig,
00548 const std::vector<Expr>& binds,
00549 const Expr& gterm
00550 );
00551
00552 void synCheckSat(ExprMap<ExprMap<std::vector<dynTrig>* >* >& , bool);
00553 void synCheckSat(bool);
00554 void semCheckSat(bool);
00555 void naiveCheckSat(bool);
00556
00557 bool insted(const Theorem & univ, const std::vector<Expr>& binds);
00558 void synInst(const Theorem & univ, const CDList<Expr>& allterms, size_t tBegin);
00559
00560 void synFullInst(const Theorem & univ, const CDList<Expr>& allterms, size_t tBegin);
00561
00562 void arrayHeuristic(const Trigger& trig, size_t univid);
00563
00564 Expr simpRAWList(const Expr& org);
00565
00566 void synNewInst(size_t univ_id, const std::vector<Expr>& binds, const Expr& gterm, const Trigger& trig );
00567 void synMultInst(const Theorem & univ, const CDList<Expr>& allterms, size_t tBegin);
00568
00569 void synPartInst(const Theorem & univ, const CDList<Expr>& allterms, size_t tBegin);
00570
00571 void semInst(const Theorem & univ, size_t tBegin);
00572
00573
00574 void goodSynMatch(const Expr& e,
00575 const std::vector<Expr> & boundVars,
00576 std::vector<std::vector<Expr> >& instBindsTerm,
00577 std::vector<Expr>& instGterm,
00578 const CDList<Expr>& allterms,
00579 size_t tBegin);
00580 void goodSynMatchNewTrig(const Trigger& trig,
00581 const std::vector<Expr> & boundVars,
00582 std::vector<std::vector<Expr> >& instBinds,
00583 std::vector<Expr>& instGterms,
00584 const CDList<Expr>& allterms,
00585 size_t tBegin);
00586
00587 bool goodSynMatchNewTrig(const Trigger& trig,
00588 const std::vector<Expr> & boundVars,
00589 std::vector<std::vector<Expr> >& instBinds,
00590 std::vector<Expr>& instGterms,
00591 const Expr& gterm);
00592
00593 void matchListOld(const CDList<Expr>& list, size_t gbegin, size_t gend);
00594
00595 void matchListNew(ExprMap<ExprMap<std::vector<dynTrig>*>*>& new_trigs,
00596 const CDList<Expr>& list,
00597 size_t gbegin,
00598 size_t gend);
00599
00600 void delNewTrigs(ExprMap<ExprMap<std::vector<dynTrig>*>*>& new_trigs);
00601 void combineOldNewTrigs(ExprMap<ExprMap<std::vector<dynTrig>*>*>& new_trigs);
00602
00603 inline void add_parent(const Expr& parent);
00604
00605 void newTopMatch(const Expr& gterm,
00606 const Expr& vterm,
00607 std::vector<ExprMap<Expr> >& binds,
00608 const Trigger& trig);
00609
00610 void newTopMatchSig(const Expr& gterm,
00611 const Expr& vterm,
00612 std::vector<ExprMap<Expr> >& binds,
00613 const Trigger& trig);
00614
00615 void newTopMatchNoSig(const Expr& gterm,
00616 const Expr& vterm,
00617 std::vector<ExprMap<Expr> >& binds,
00618 const Trigger& trig);
00619
00620
00621
00622 void newTopMatchBackupOnly(const Expr& gterm,
00623 const Expr& vterm,
00624 std::vector<ExprMap<Expr> >& binds,
00625 const Trigger& trig);
00626
00627
00628 bool synMatchTopPred(const Expr& gterm, const Trigger trig, ExprMap<Expr>& env);
00629
00630
00631
00632
00633 bool recSynMatch(const Expr& gterm, const Expr& vterm, ExprMap<Expr>& env);
00634
00635 bool recMultMatch(const Expr& gterm,const Expr& vterm, std::vector<ExprMap<Expr> >& binds);
00636 bool recMultMatchDebug(const Expr& gterm,const Expr& vterm, std::vector<ExprMap<Expr> >& binds);
00637 bool recMultMatchNewWay(const Expr& gterm,const Expr& vterm, std::vector<ExprMap<Expr> >& binds);
00638 bool recMultMatchOldWay(const Expr& gterm,const Expr& vterm, std::vector<ExprMap<Expr> >& binds);
00639
00640 inline bool multMatchChild(const Expr& gterm, const Expr& vterm, std::vector<ExprMap<Expr> >& binds, bool top=false);
00641 inline bool multMatchTop(const Expr& gterm, const Expr& vterm, std::vector<ExprMap<Expr> >& binds);
00642
00643
00644 bool recSynMatchBackupOnly(const Expr& gterm, const Expr& vterm, ExprMap<Expr>& env);
00645
00646 bool hasGoodSynInstNewTrigOld(Trigger& trig,
00647 std::vector<Expr> & boundVars,
00648 std::vector<std::vector<Expr> >& instBinds,
00649 std::vector<Expr>& instGterms,
00650 const CDList<Expr>& allterms,
00651 size_t tBegin);
00652
00653 bool hasGoodSynInstNewTrig(Trigger& trig,
00654 const std::vector<Expr> & boundVars,
00655 std::vector<std::vector<Expr> >& instBinds,
00656 std::vector<Expr>& instGterms,
00657 const CDList<Expr>& allterms,
00658 size_t tBegin);
00659
00660
00661 bool hasGoodSynMultiInst(const Expr& e,
00662 std::vector<Expr>& bVars,
00663 std::vector<std::vector<Expr> >& instSet,
00664 const CDList<Expr>& allterms,
00665 size_t tBegin);
00666
00667 void recGoodSemMatch(const Expr& e,
00668 const std::vector<Expr>& bVars,
00669 std::vector<Expr>& newInst,
00670 std::set<std::vector<Expr> >& instSet);
00671
00672 bool hasGoodSemInst(const Expr& e,
00673 std::vector<Expr>& bVars,
00674 std::set<std::vector<Expr> >& instSet,
00675 size_t tBegin);
00676
00677 bool isTransLike (const std::vector<Expr>& cur_trig);
00678 bool isTrans2Like (const std::vector<Expr>& all_terms, const Expr& tr2);
00679
00680
00681 static const size_t MAX_TRIG_BVS=15;
00682 Expr d_mybvs[MAX_TRIG_BVS];
00683
00684 Expr recGeneralTrig(const Expr& trig, ExprMap<Expr>& bvs, size_t& mybvs_count);
00685 Expr generalTrig(const Expr& trig, ExprMap<Expr>& bvs);
00686
00687 ExprMap<CDMap<Expr, CDList<dynTrig>* >* > d_allmap_trigs;
00688
00689 CDList<Trigger> d_alltrig_list;
00690
00691 void registerTrig(ExprMap<ExprMap<std::vector<dynTrig>* >* >& cur_trig_map,
00692 Trigger trig,
00693 const std::vector<Expr> thmBVs,
00694 size_t univ_id);
00695
00696 void registerTrigReal(Trigger trig, const std::vector<Expr>, size_t univ_id);
00697
00698 bool canMatch(const Expr& t1, const Expr& t2, ExprMap<Expr>& env);
00699 void setGround(const Expr& gterm, const Expr& trig, const Theorem& univ, const std::vector<Expr>& subTerms) ;
00700
00701
00702 void setupTriggers(ExprMap<ExprMap<std::vector<dynTrig>* >*>& trig_maps,
00703 const Theorem& thm,
00704 size_t univs_id);
00705
00706 void saveContext();
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718 public:
00719 TheoryQuant(TheoryCore* core);
00720 ~TheoryQuant();
00721
00722 QuantProofRules* createProofRules();
00723
00724
00725
00726 void addSharedTerm(const Expr& e) {}
00727
00728
00729
00730
00731
00732
00733
00734 void assertFact(const Theorem& e);
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749 void checkSat(bool fullEffort);
00750 void setup(const Expr& e);
00751
00752 int help(int i);
00753
00754 void update(const Theorem& e, const Expr& d);
00755
00756
00757
00758 void debug(int i);
00759 void notifyInconsistent(const Theorem& thm);
00760
00761 void computeType(const Expr& e);
00762 Expr computeTCC(const Expr& e);
00763
00764 virtual Expr parseExprOp(const Expr& e);
00765
00766 ExprStream& print(ExprStream& os, const Expr& e);
00767 };
00768
00769 }
00770
00771 #endif