More optimization of QCF. Fixed InstMatchTrie for caching instantiations. Use non...
[cvc5.git] / src / theory / quantifiers / quant_conflict_find.h
1 /********************* */
2 /*! \file quant_conflict_find.h
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-2013 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 quantifiers conflict find class
13 **/
14
15 #include "cvc4_private.h"
16
17 #ifndef QUANT_CONFLICT_FIND
18 #define QUANT_CONFLICT_FIND
19
20 #include "context/cdhashmap.h"
21 #include "context/cdchunk_list.h"
22 #include "theory/quantifiers_engine.h"
23
24 namespace CVC4 {
25 namespace theory {
26 namespace quantifiers {
27
28 class QcfNode;
29
30 class QuantConflictFind;
31
32 class QcfNodeIndex {
33 public:
34 std::map< TNode, QcfNodeIndex > d_children;
35 void clear() { d_children.clear(); }
36 void debugPrint( const char * c, int t );
37 Node existsTerm( TNode n, std::vector< TNode >& reps, int index = 0 );
38 Node addTerm( TNode n, std::vector< TNode >& reps, int index = 0 );
39 };
40
41 class QuantInfo;
42
43 //match generator
44 class MatchGen {
45 private:
46 //current children information
47 int d_child_counter;
48 //children of this object
49 //std::vector< int > d_children_order;
50 unsigned getNumChildren() { return d_children.size(); }
51 //MatchGen * getChild( int i ) { return &d_children[d_children_order[i]]; }
52 MatchGen * getChild( int i ) { return &d_children[i]; }
53 //current matching information
54 std::vector< QcfNodeIndex * > d_qn;
55 std::vector< std::map< TNode, QcfNodeIndex >::iterator > d_qni;
56 bool doMatching( QuantConflictFind * p, QuantInfo * qi );
57 //for matching : each index is either a variable or a ground term
58 unsigned d_qni_size;
59 std::map< int, int > d_qni_var_num;
60 std::map< int, TNode > d_qni_gterm;
61 std::map< int, TNode > d_qni_gterm_rep;
62 std::map< int, int > d_qni_bound;
63 std::map< int, TNode > d_qni_bound_cons;
64 std::map< int, int > d_qni_bound_cons_var;
65 std::map< int, int >::iterator d_binding_it;
66 bool d_binding;
67 //int getVarBindingVar();
68 std::map< int, Node > d_ground_eval;
69 public:
70 //type of the match generator
71 enum {
72 typ_invalid,
73 typ_ground,
74 typ_pred,
75 typ_eq,
76 typ_formula,
77 typ_var,
78 typ_top,
79 };
80 void debugPrintType( const char * c, short typ, bool isTrace = false );
81 public:
82 MatchGen() : d_type( typ_invalid ){}
83 MatchGen( QuantConflictFind * p, Node q, Node n, bool isTop = false, bool isVar = false );
84 bool d_tgt;
85 Node d_n;
86 std::vector< MatchGen > d_children;
87 short d_type;
88 bool d_type_not;
89 void reset_round( QuantConflictFind * p );
90 void reset( QuantConflictFind * p, bool tgt, QuantInfo * qi );
91 bool getNextMatch( QuantConflictFind * p, QuantInfo * qi );
92 bool isValid() { return d_type!=typ_invalid; }
93 void setInvalid();
94 };
95
96 //info for quantifiers
97 class QuantInfo {
98 public:
99 QuantInfo() : d_mg( NULL ) {}
100 std::vector< TNode > d_vars;
101 std::map< TNode, int > d_var_num;
102 //std::map< EqRegistry *, bool > d_rel_eqr;
103 std::map< int, std::vector< Node > > d_var_constraint[2];
104 int getVarNum( TNode v ) { return d_var_num.find( v )!=d_var_num.end() ? d_var_num[v] : -1; }
105 bool isVar( TNode v ) { return d_var_num.find( v )!=d_var_num.end(); }
106 int getNumVars() { return (int)d_vars.size(); }
107 TNode getVar( int i ) { return d_vars[i]; }
108 MatchGen * d_mg;
109 Node d_q;
110 std::map< int, MatchGen * > d_var_mg;
111 void reset_round( QuantConflictFind * p );
112 public:
113 //current constraints
114 std::map< int, TNode > d_match;
115 std::map< int, TNode > d_match_term;
116 std::map< int, std::map< TNode, int > > d_curr_var_deq;
117 int getCurrentRepVar( int v );
118 TNode getCurrentValue( TNode n );
119 bool getCurrentCanBeEqual( QuantConflictFind * p, int v, TNode n, bool chDiseq = false );
120 int addConstraint( QuantConflictFind * p, int v, TNode n, bool polarity );
121 int addConstraint( QuantConflictFind * p, int v, TNode n, int vn, bool polarity, bool doRemove );
122 bool setMatch( QuantConflictFind * p, int v, TNode n );
123 bool isMatchSpurious( QuantConflictFind * p );
124 bool completeMatch( QuantConflictFind * p, std::vector< int >& assigned );
125 void debugPrintMatch( const char * c );
126 bool isConstrainedVar( int v );
127 };
128
129 class QuantConflictFind : public QuantifiersModule
130 {
131 friend class QcfNodeIndex;
132 friend class MatchGen;
133 friend class QuantInfo;
134 typedef context::CDChunkList<Node> NodeList;
135 typedef context::CDHashMap<Node, bool, NodeHashFunction> NodeBoolMap;
136 private:
137 context::Context* d_c;
138 context::CDO< bool > d_conflict;
139 bool d_performCheck;
140 //void registerAssertion( Node n );
141 void registerNode( Node q, Node n, bool hasPol, bool pol );
142 void flatten( Node q, Node n );
143 private:
144 std::map< Node, Node > d_op_node;
145 Node getFunction( Node n, int& index, bool isQuant = false );
146 int d_fid_count;
147 std::map< Node, int > d_fid;
148 Node mkEqNode( Node a, Node b );
149 public: //for ground terms
150 Node d_true;
151 Node d_false;
152 private:
153 Node evaluateTerm( Node n );
154 int evaluate( Node n, bool pref = false, bool hasPref = false );
155 private:
156 //currently asserted quantifiers
157 NodeList d_qassert;
158 std::map< Node, QuantInfo > d_qinfo;
159 private: //for equivalence classes
160 eq::EqualityEngine * getEqualityEngine();
161 bool areDisequal( Node n1, Node n2 );
162 bool areEqual( Node n1, Node n2 );
163 Node getRepresentative( Node n );
164
165 /*
166 class EqcInfo {
167 public:
168 EqcInfo( context::Context* c ) : d_diseq( c ) {}
169 NodeBoolMap d_diseq;
170 bool isDisequal( Node n ) { return d_diseq.find( n )!=d_diseq.end() && d_diseq[n]; }
171 void setDisequal( Node n, bool val = true ) { d_diseq[n] = val; }
172 //NodeBoolMap& getRelEqr( int index ) { return index==0 ? d_rel_eqr_e : d_rel_eqr_d; }
173 };
174 std::map< Node, EqcInfo * > d_eqc_info;
175 EqcInfo * getEqcInfo( Node n, bool doCreate = true );
176 */
177 // operator -> index(terms)
178 std::map< TNode, QcfNodeIndex > d_uf_terms;
179 // operator -> index(eqc -> terms)
180 std::map< TNode, QcfNodeIndex > d_eqc_uf_terms;
181 //get qcf node index
182 QcfNodeIndex * getQcfNodeIndex( Node eqc, Node f );
183 QcfNodeIndex * getQcfNodeIndex( Node f );
184 // type -> list(eqc)
185 std::map< TypeNode, std::vector< TNode > > d_eqcs;
186 //mapping from UF terms to representatives of their arguments
187 std::map< TNode, std::vector< TNode > > d_arg_reps;
188 //compute arg reps
189 void computeArgReps( TNode n );
190 // is this term treated as UF application?
191 bool isHandledUfTerm( TNode n );
192 public:
193 enum {
194 effort_conflict,
195 effort_prop_eq,
196 effort_prop_deq,
197 };
198 short d_effort;
199 //for effort_prop
200 TNode d_prop_eq[2];
201 bool d_prop_pol;
202 bool isPropagationSet();
203 bool areMatchEqual( TNode n1, TNode n2 );
204 bool areMatchDisequal( TNode n1, TNode n2 );
205 public:
206 QuantConflictFind( QuantifiersEngine * qe, context::Context* c );
207
208 /** register assertions */
209 //void registerAssertions( std::vector< Node >& assertions );
210 /** register quantifier */
211 void registerQuantifier( Node q );
212
213 public:
214 /** assert quantifier */
215 void assertNode( Node q );
216 /** new node */
217 void newEqClass( Node n );
218 /** merge */
219 void merge( Node a, Node b );
220 /** assert disequal */
221 void assertDisequal( Node a, Node b );
222 /** check */
223 void check( Theory::Effort level );
224 /** needs check */
225 bool needsCheck( Theory::Effort level );
226 private:
227 void computeRelevantEqr();
228 private:
229 void debugPrint( const char * c );
230 //for debugging
231 std::vector< Node > d_quants;
232 std::map< Node, int > d_quant_id;
233 void debugPrintQuant( const char * c, Node q );
234 void debugPrintQuantBody( const char * c, Node q, Node n, bool doVarNum = true );
235 public:
236 /** statistics class */
237 class Statistics {
238 public:
239 IntStat d_inst_rounds;
240 IntStat d_conflict_inst;
241 IntStat d_prop_inst;
242 Statistics();
243 ~Statistics();
244 };
245 Statistics d_statistics;
246 };
247
248 }
249 }
250 }
251
252 #endif