hot fix for pre-reg term caching in strings
[cvc5.git] / src / theory / strings / theory_strings.h
1 /********************* */
2 /*! \file theory_strings.h
3 ** \verbatim
4 ** Original author: Tianyi Liang
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 Theory of strings
13 **
14 ** Theory of strings.
15 **/
16
17 #include "cvc4_private.h"
18
19 #ifndef __CVC4__THEORY__STRINGS__THEORY_STRINGS_H
20 #define __CVC4__THEORY__STRINGS__THEORY_STRINGS_H
21
22 #include "theory/theory.h"
23 #include "theory/uf/equality_engine.h"
24 #include "theory/strings/theory_strings_preprocess.h"
25 #include "theory/strings/regexp_operation.h"
26
27 #include "context/cdchunk_list.h"
28 #include "context/cdhashset.h"
29
30 namespace CVC4 {
31 namespace theory {
32 namespace strings {
33
34 /**
35 * Decision procedure for strings.
36 *
37 */
38
39 class TheoryStrings : public Theory {
40 typedef context::CDChunkList<Node> NodeList;
41 typedef context::CDHashMap<Node, NodeList*, NodeHashFunction> NodeListMap;
42 typedef context::CDHashMap<Node, bool, NodeHashFunction> NodeBoolMap;
43 typedef context::CDHashMap<Node, int, NodeHashFunction> NodeIntMap;
44 typedef context::CDHashMap<Node, Node, NodeHashFunction> NodeNodeMap;
45 typedef context::CDHashSet<Node, NodeHashFunction> NodeSet;
46
47 public:
48 TheoryStrings(context::Context* c, context::UserContext* u, OutputChannel& out, Valuation valuation, const LogicInfo& logicInfo);
49 ~TheoryStrings();
50
51 void setMasterEqualityEngine(eq::EqualityEngine* eq);
52
53 std::string identify() const { return std::string("TheoryStrings"); }
54
55 public:
56 void propagate(Effort e);
57 bool propagate(TNode literal);
58 void explain( TNode literal, std::vector<TNode>& assumptions );
59 Node explain( TNode literal );
60
61
62 // NotifyClass for equality engine
63 class NotifyClass : public eq::EqualityEngineNotify {
64 TheoryStrings& d_str;
65 public:
66 NotifyClass(TheoryStrings& t_str): d_str(t_str) {}
67 bool eqNotifyTriggerEquality(TNode equality, bool value) {
68 Debug("strings") << "NotifyClass::eqNotifyTriggerEquality(" << equality << ", " << (value ? "true" : "false" )<< ")" << std::endl;
69 if (value) {
70 return d_str.propagate(equality);
71 } else {
72 // We use only literal triggers so taking not is safe
73 return d_str.propagate(equality.notNode());
74 }
75 }
76 bool eqNotifyTriggerPredicate(TNode predicate, bool value) {
77 Debug("strings") << "NotifyClass::eqNotifyTriggerPredicate(" << predicate << ", " << (value ? "true" : "false") << ")" << std::endl;
78 if (value) {
79 return d_str.propagate(predicate);
80 } else {
81 return d_str.propagate(predicate.notNode());
82 }
83 }
84 bool eqNotifyTriggerTermEquality(TheoryId tag, TNode t1, TNode t2, bool value) {
85 Debug("strings") << "NotifyClass::eqNotifyTriggerTermMerge(" << tag << ", " << t1 << ", " << t2 << ")" << std::endl;
86 if (value) {
87 return d_str.propagate(t1.eqNode(t2));
88 } else {
89 return d_str.propagate(t1.eqNode(t2).notNode());
90 }
91 }
92 void eqNotifyConstantTermMerge(TNode t1, TNode t2) {
93 Debug("strings") << "NotifyClass::eqNotifyConstantTermMerge(" << t1 << ", " << t2 << ")" << std::endl;
94 d_str.conflict(t1, t2);
95 }
96 void eqNotifyNewClass(TNode t) {
97 Debug("strings") << "NotifyClass::eqNotifyNewClass(" << t << std::endl;
98 d_str.eqNotifyNewClass(t);
99 }
100 void eqNotifyPreMerge(TNode t1, TNode t2) {
101 Debug("strings") << "NotifyClass::eqNotifyPreMerge(" << t1 << ", " << t2 << std::endl;
102 d_str.eqNotifyPreMerge(t1, t2);
103 }
104 void eqNotifyPostMerge(TNode t1, TNode t2) {
105 Debug("strings") << "NotifyClass::eqNotifyPostMerge(" << t1 << ", " << t2 << std::endl;
106 d_str.eqNotifyPostMerge(t1, t2);
107 }
108 void eqNotifyDisequal(TNode t1, TNode t2, TNode reason) {
109 Debug("strings") << "NotifyClass::eqNotifyDisequal(" << t1 << ", " << t2 << ", " << reason << std::endl;
110 d_str.eqNotifyDisequal(t1, t2, reason);
111 }
112 };/* class TheoryStrings::NotifyClass */
113
114 private:
115 // Constants
116 Node d_emptyString;
117 Node d_emptyRegexp;
118 Node d_true;
119 Node d_false;
120 Node d_zero;
121 Node d_one;
122 // Options
123 bool d_opt_fmf;
124 bool d_opt_regexp_gcd;
125 // Helper functions
126 Node getRepresentative( Node t );
127 bool hasTerm( Node a );
128 bool areEqual( Node a, Node b );
129 bool areDisequal( Node a, Node b );
130 Node getLengthTerm( Node t );
131 Node getLength( Node t );
132
133 private:
134 /** The notify class */
135 NotifyClass d_notify;
136 /** Equaltity engine */
137 eq::EqualityEngine d_equalityEngine;
138 /** Are we in conflict */
139 context::CDO<bool> d_conflict;
140 //list of pairs of nodes to merge
141 std::map< Node, Node > d_pending_exp;
142 std::vector< Node > d_pending;
143 std::vector< Node > d_lemma_cache;
144 std::map< Node, bool > d_pending_req_phase;
145 /** inferences */
146 NodeList d_infer;
147 NodeList d_infer_exp;
148 /** normal forms */
149 std::map< Node, Node > d_normal_forms_base;
150 std::map< Node, std::vector< Node > > d_normal_forms;
151 std::map< Node, std::vector< Node > > d_normal_forms_exp;
152 //map of pairs of terms that have the same normal form
153 NodeListMap d_nf_pairs;
154 void addNormalFormPair( Node n1, Node n2 );
155 bool isNormalFormPair( Node n1, Node n2 );
156 bool isNormalFormPair2( Node n1, Node n2 );
157 // loop ant
158 NodeSet d_loop_antec;
159 NodeSet d_length_intro_vars;
160 // preReg cache
161 NodeSet d_prereg_cached;
162
163 /////////////////////////////////////////////////////////////////////////////
164 // MODEL GENERATION
165 /////////////////////////////////////////////////////////////////////////////
166 public:
167 void collectModelInfo(TheoryModel* m, bool fullModel);
168
169 /////////////////////////////////////////////////////////////////////////////
170 // NOTIFICATIONS
171 /////////////////////////////////////////////////////////////////////////////
172 public:
173 void presolve();
174 void shutdown() { }
175
176 /////////////////////////////////////////////////////////////////////////////
177 // MAIN SOLVER
178 /////////////////////////////////////////////////////////////////////////////
179 private:
180 void addSharedTerm(TNode n);
181 EqualityStatus getEqualityStatus(TNode a, TNode b);
182
183 private:
184 class EqcInfo {
185 public:
186 EqcInfo( context::Context* c );
187 ~EqcInfo(){}
188 //constant in this eqc
189 context::CDO< Node > d_const_term;
190 context::CDO< Node > d_length_term;
191 context::CDO< unsigned > d_cardinality_lem_k;
192 // 1 = added length lemma
193 context::CDO< Node > d_normalized_length;
194 };
195 /** map from representatives to information necessary for equivalence classes */
196 std::map< Node, EqcInfo* > d_eqc_info;
197 EqcInfo * getOrMakeEqcInfo( Node eqc, bool doMake = true );
198 //maintain which concat terms have the length lemma instantiated
199 NodeSet d_length_nodes;
200 NodeNodeMap d_length_inst;
201 private:
202 void mergeCstVec(std::vector< Node > &vec_strings);
203 bool getNormalForms(Node &eqc, std::vector< Node > & visited, std::vector< Node > & nf,
204 std::vector< std::vector< Node > > &normal_forms,
205 std::vector< std::vector< Node > > &normal_forms_exp,
206 std::vector< Node > &normal_form_src);
207 bool detectLoop(std::vector< std::vector< Node > > &normal_forms,
208 int i, int j, int index_i, int index_j,
209 int &loop_in_i, int &loop_in_j);
210 bool processLoop(std::vector< Node > &antec,
211 std::vector< std::vector< Node > > &normal_forms,
212 std::vector< Node > &normal_form_src,
213 int i, int j, int loop_n_index, int other_n_index,
214 int loop_index, int index, int other_index);
215 bool processNEqc(std::vector< std::vector< Node > > &normal_forms,
216 std::vector< std::vector< Node > > &normal_forms_exp,
217 std::vector< Node > &normal_form_src);
218 bool processReverseNEq(std::vector< std::vector< Node > > &normal_forms,
219 std::vector< Node > &normal_form_src, std::vector< Node > &curr_exp, unsigned i, unsigned j );
220 bool processSimpleNEq( std::vector< std::vector< Node > > &normal_forms,
221 std::vector< Node > &normal_form_src, std::vector< Node > &curr_exp, unsigned i, unsigned j,
222 unsigned& index_i, unsigned& index_j, bool isRev );
223 bool normalizeEquivalenceClass( Node n, std::vector< Node > & visited, std::vector< Node > & nf, std::vector< Node > & nf_exp );
224 bool processDeq( Node n1, Node n2 );
225 int processReverseDeq( std::vector< Node >& nfi, std::vector< Node >& nfj, Node ni, Node nj );
226 int processSimpleDeq( std::vector< Node >& nfi, std::vector< Node >& nfj, Node ni, Node nj, unsigned& index, bool isRev );
227 //bool unrollStar( Node atom );
228 Node mkRegExpAntec(Node atom, Node ant);
229
230 bool checkSimple();
231 bool checkNormalForms();
232 void checkDeqNF();
233 bool checkLengthsEqc();
234 bool checkCardinality();
235 bool checkInductiveEquations();
236 bool checkMemberships();
237 bool checkPDerivative(Node x, Node r, Node atom, bool &addedLemma, std::vector< Node > &processed, std::vector< Node > &cprocessed);
238 bool checkContains();
239 bool checkPosContains();
240 bool checkNegContains();
241
242 public:
243 void preRegisterTerm(TNode n);
244 void check(Effort e);
245
246 /** Conflict when merging two constants */
247 void conflict(TNode a, TNode b);
248 /** called when a new equivalence class is created */
249 void eqNotifyNewClass(TNode t);
250 /** called when two equivalence classes will merge */
251 void eqNotifyPreMerge(TNode t1, TNode t2);
252 /** called when two equivalence classes have merged */
253 void eqNotifyPostMerge(TNode t1, TNode t2);
254 /** called when two equivalence classes are made disequal */
255 void eqNotifyDisequal(TNode t1, TNode t2, TNode reason);
256 protected:
257 /** compute care graph */
258 void computeCareGraph();
259
260 //do pending merges
261 void doPendingFacts();
262 void doPendingLemmas();
263
264 void sendLemma( Node ant, Node conc, const char * c );
265 void sendInfer( Node eq_exp, Node eq, const char * c );
266 void sendSplit( Node a, Node b, const char * c, bool preq = true );
267 /** mkConcat **/
268 Node mkConcat( Node n1, Node n2 );
269 Node mkConcat( std::vector< Node >& c );
270 /** mkExplain **/
271 Node mkExplain( std::vector< Node >& a );
272 Node mkExplain( std::vector< Node >& a, std::vector< Node >& an );
273 /** mkAnd **/
274 Node mkAnd( std::vector< Node >& a );
275 /** get concat vector */
276 void getConcatVec( Node n, std::vector< Node >& c );
277
278 //get equivalence classes
279 void getEquivalenceClasses( std::vector< Node >& eqcs );
280 //get final normal form
281 void getFinalNormalForm( Node n, std::vector< Node >& nf, std::vector< Node >& exp );
282
283 //separate into collections with equal length
284 void separateByLength( std::vector< Node >& n, std::vector< std::vector< Node > >& col, std::vector< Node >& lts );
285 void printConcat( std::vector< Node >& n, const char * c );
286
287 private:
288 Node mkSplitEq( const char * c, const char * info, Node lhs, Node rhs, bool lgtZero );
289
290 // Special String Functions
291 NodeList d_str_pos_ctn;
292 NodeList d_str_neg_ctn;
293 NodeSet d_neg_ctn_eqlen;
294 NodeSet d_neg_ctn_ulen;
295 NodeSet d_pos_ctn_cached;
296 NodeSet d_neg_ctn_cached;
297
298 // Regular Expression
299 private:
300 // regular expression memberships
301 NodeList d_regexp_memberships;
302 NodeSet d_regexp_ucached;
303 NodeSet d_regexp_ccached;
304 // antecedant for why regexp membership must be true
305 NodeNodeMap d_regexp_ant;
306 // membership length
307 //std::map< Node, bool > d_membership_length;
308 // regular expression operations
309 RegExpOpr d_regexp_opr;
310
311 CVC4::String getHeadConst( Node x );
312 bool splitRegExp( Node x, Node r, Node ant );
313 bool addMembershipLength(Node atom);
314
315
316 // Finite Model Finding
317 private:
318 NodeSet d_input_vars;
319 context::CDO< Node > d_input_var_lsum;
320 context::CDHashMap< int, Node > d_cardinality_lits;
321 context::CDO< int > d_curr_cardinality;
322 public:
323 //for finite model finding
324 Node getNextDecisionRequest();
325 void assertNode( Node lit );
326
327 public:
328 /** statistics class */
329 class Statistics {
330 public:
331 IntStat d_splits;
332 IntStat d_eq_splits;
333 IntStat d_deq_splits;
334 IntStat d_loop_lemmas;
335 IntStat d_new_skolems;
336 Statistics();
337 ~Statistics();
338 };/* class TheoryStrings::Statistics */
339 Statistics d_statistics;
340 };/* class TheoryStrings */
341
342 }/* CVC4::theory::strings namespace */
343 }/* CVC4::theory namespace */
344 }/* CVC4 namespace */
345
346 #endif /* __CVC4__THEORY__STRINGS__THEORY_STRINGS_H */