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