Split regular expression solver (#2891)
[cvc5.git] / src / theory / strings / theory_strings.cpp
1 /********************* */
2 /*! \file theory_strings.cpp
3 ** \verbatim
4 ** Top contributors (to current version):
5 ** Andrew Reynolds, Tianyi Liang, Morgan Deters
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2018 by the authors listed in the file AUTHORS
8 ** in the top-level source directory) and their institutional affiliations.
9 ** All rights reserved. See the file COPYING in the top-level source
10 ** directory for licensing information.\endverbatim
11 **
12 ** \brief Implementation of the theory of strings.
13 **
14 ** Implementation of the theory of strings.
15 **/
16
17 #include "theory/strings/theory_strings.h"
18
19 #include <cmath>
20
21 #include "expr/kind.h"
22 #include "options/strings_options.h"
23 #include "smt/command.h"
24 #include "smt/logic_exception.h"
25 #include "smt/smt_statistics_registry.h"
26 #include "theory/ext_theory.h"
27 #include "theory/rewriter.h"
28 #include "theory/strings/theory_strings_rewriter.h"
29 #include "theory/strings/type_enumerator.h"
30 #include "theory/theory_model.h"
31 #include "theory/valuation.h"
32
33 using namespace std;
34 using namespace CVC4::context;
35 using namespace CVC4::kind;
36
37 namespace CVC4 {
38 namespace theory {
39 namespace strings {
40
41 std::ostream& operator<<(std::ostream& out, Inference i)
42 {
43 switch (i)
44 {
45 case INFER_SSPLIT_CST_PROP: out << "S-Split(CST-P)-prop"; break;
46 case INFER_SSPLIT_VAR_PROP: out << "S-Split(VAR)-prop"; break;
47 case INFER_LEN_SPLIT: out << "Len-Split(Len)"; break;
48 case INFER_LEN_SPLIT_EMP: out << "Len-Split(Emp)"; break;
49 case INFER_SSPLIT_CST_BINARY: out << "S-Split(CST-P)-binary"; break;
50 case INFER_SSPLIT_CST: out << "S-Split(CST-P)"; break;
51 case INFER_SSPLIT_VAR: out << "S-Split(VAR)"; break;
52 case INFER_FLOOP: out << "F-Loop"; break;
53 default: out << "?"; break;
54 }
55 return out;
56 }
57
58 std::ostream& operator<<(std::ostream& out, InferStep s)
59 {
60 switch (s)
61 {
62 case BREAK: out << "break"; break;
63 case CHECK_INIT: out << "check_init"; break;
64 case CHECK_CONST_EQC: out << "check_const_eqc"; break;
65 case CHECK_EXTF_EVAL: out << "check_extf_eval"; break;
66 case CHECK_CYCLES: out << "check_cycles"; break;
67 case CHECK_FLAT_FORMS: out << "check_flat_forms"; break;
68 case CHECK_NORMAL_FORMS_EQ: out << "check_normal_forms_eq"; break;
69 case CHECK_NORMAL_FORMS_DEQ: out << "check_normal_forms_deq"; break;
70 case CHECK_CODES: out << "check_codes"; break;
71 case CHECK_LENGTH_EQC: out << "check_length_eqc"; break;
72 case CHECK_EXTF_REDUCTION: out << "check_extf_reduction"; break;
73 case CHECK_MEMBERSHIP: out << "check_membership"; break;
74 case CHECK_CARDINALITY: out << "check_cardinality"; break;
75 default: out << "?"; break;
76 }
77 return out;
78 }
79
80 Node TheoryStrings::TermIndex::add( TNode n, unsigned index, TheoryStrings* t, Node er, std::vector< Node >& c ) {
81 if( index==n.getNumChildren() ){
82 if( d_data.isNull() ){
83 d_data = n;
84 }
85 return d_data;
86 }else{
87 Assert( index<n.getNumChildren() );
88 TNode nir = t->getRepresentative( n[index] );
89 //if it is empty, and doing CONCAT, ignore
90 if( nir==er && n.getKind()==kind::STRING_CONCAT ){
91 return add( n, index+1, t, er, c );
92 }else{
93 c.push_back( nir );
94 return d_children[nir].add( n, index+1, t, er, c );
95 }
96 }
97 }
98
99 TheoryStrings::TheoryStrings(context::Context* c,
100 context::UserContext* u,
101 OutputChannel& out,
102 Valuation valuation,
103 const LogicInfo& logicInfo)
104 : Theory(THEORY_STRINGS, c, u, out, valuation, logicInfo),
105 d_notify(*this),
106 d_equalityEngine(d_notify, c, "theory::strings", true),
107 d_conflict(c, false),
108 d_infer(c),
109 d_infer_exp(c),
110 d_nf_pairs(c),
111 d_pregistered_terms_cache(u),
112 d_registered_terms_cache(u),
113 d_length_lemma_terms_cache(u),
114 d_preproc(&d_sk_cache, u),
115 d_extf_infer_cache(c),
116 d_extf_infer_cache_u(u),
117 d_ee_disequalities(c),
118 d_congruent(c),
119 d_proxy_var(u),
120 d_proxy_var_to_length(u),
121 d_functionsTerms(c),
122 d_has_extf(c, false),
123 d_has_str_code(false),
124 d_regexp_solver(*this, c, u),
125 d_input_vars(u),
126 d_input_var_lsum(u),
127 d_cardinality_lits(u),
128 d_curr_cardinality(c, 0),
129 d_sslds(nullptr),
130 d_strategy_init(false)
131 {
132 setupExtTheory();
133 getExtTheory()->addFunctionKind(kind::STRING_SUBSTR);
134 getExtTheory()->addFunctionKind(kind::STRING_STRIDOF);
135 getExtTheory()->addFunctionKind(kind::STRING_ITOS);
136 getExtTheory()->addFunctionKind(kind::STRING_STOI);
137 getExtTheory()->addFunctionKind(kind::STRING_STRREPL);
138 getExtTheory()->addFunctionKind(kind::STRING_STRREPLALL);
139 getExtTheory()->addFunctionKind(kind::STRING_STRCTN);
140 getExtTheory()->addFunctionKind(kind::STRING_IN_REGEXP);
141 getExtTheory()->addFunctionKind(kind::STRING_LEQ);
142 getExtTheory()->addFunctionKind(kind::STRING_CODE);
143
144 // The kinds we are treating as function application in congruence
145 d_equalityEngine.addFunctionKind(kind::STRING_LENGTH);
146 d_equalityEngine.addFunctionKind(kind::STRING_CONCAT);
147 d_equalityEngine.addFunctionKind(kind::STRING_IN_REGEXP);
148 d_equalityEngine.addFunctionKind(kind::STRING_CODE);
149 if( options::stringLazyPreproc() ){
150 d_equalityEngine.addFunctionKind(kind::STRING_STRCTN);
151 d_equalityEngine.addFunctionKind(kind::STRING_LEQ);
152 d_equalityEngine.addFunctionKind(kind::STRING_SUBSTR);
153 d_equalityEngine.addFunctionKind(kind::STRING_ITOS);
154 d_equalityEngine.addFunctionKind(kind::STRING_STOI);
155 d_equalityEngine.addFunctionKind(kind::STRING_STRIDOF);
156 d_equalityEngine.addFunctionKind(kind::STRING_STRREPL);
157 d_equalityEngine.addFunctionKind(kind::STRING_STRREPLALL);
158 }
159
160 d_zero = NodeManager::currentNM()->mkConst( Rational( 0 ) );
161 d_one = NodeManager::currentNM()->mkConst( Rational( 1 ) );
162 d_neg_one = NodeManager::currentNM()->mkConst(Rational(-1));
163 d_emptyString = NodeManager::currentNM()->mkConst( ::CVC4::String("") );
164 d_true = NodeManager::currentNM()->mkConst( true );
165 d_false = NodeManager::currentNM()->mkConst( false );
166
167 d_card_size = TheoryStringsRewriter::getAlphabetCardinality();
168 }
169
170 TheoryStrings::~TheoryStrings() {
171 for( std::map< Node, EqcInfo* >::iterator it = d_eqc_info.begin(); it != d_eqc_info.end(); ++it ){
172 delete it->second;
173 }
174 }
175
176 Node TheoryStrings::getRepresentative( Node t ) {
177 if( d_equalityEngine.hasTerm( t ) ){
178 return d_equalityEngine.getRepresentative( t );
179 }else{
180 return t;
181 }
182 }
183
184 bool TheoryStrings::hasTerm( Node a ){
185 return d_equalityEngine.hasTerm( a );
186 }
187
188 bool TheoryStrings::areEqual( Node a, Node b ){
189 if( a==b ){
190 return true;
191 }else if( hasTerm( a ) && hasTerm( b ) ){
192 return d_equalityEngine.areEqual( a, b );
193 }else{
194 return false;
195 }
196 }
197
198 bool TheoryStrings::areDisequal( Node a, Node b ){
199 if( a==b ){
200 return false;
201 }else{
202 if( hasTerm( a ) && hasTerm( b ) ) {
203 Node ar = d_equalityEngine.getRepresentative( a );
204 Node br = d_equalityEngine.getRepresentative( b );
205 return ( ar!=br && ar.isConst() && br.isConst() ) || d_equalityEngine.areDisequal( ar, br, false );
206 }else{
207 Node ar = getRepresentative( a );
208 Node br = getRepresentative( b );
209 return ar!=br && ar.isConst() && br.isConst();
210 }
211 }
212 }
213
214 bool TheoryStrings::areCareDisequal( TNode x, TNode y ) {
215 Assert( d_equalityEngine.hasTerm(x) );
216 Assert( d_equalityEngine.hasTerm(y) );
217 if( d_equalityEngine.isTriggerTerm(x, THEORY_STRINGS) && d_equalityEngine.isTriggerTerm(y, THEORY_STRINGS) ){
218 TNode x_shared = d_equalityEngine.getTriggerTermRepresentative(x, THEORY_STRINGS);
219 TNode y_shared = d_equalityEngine.getTriggerTermRepresentative(y, THEORY_STRINGS);
220 EqualityStatus eqStatus = d_valuation.getEqualityStatus(x_shared, y_shared);
221 if( eqStatus==EQUALITY_FALSE_AND_PROPAGATED || eqStatus==EQUALITY_FALSE || eqStatus==EQUALITY_FALSE_IN_MODEL ){
222 return true;
223 }
224 }
225 return false;
226 }
227
228 Node TheoryStrings::getLengthExp( Node t, std::vector< Node >& exp, Node te ){
229 Assert( areEqual( t, te ) );
230 Node lt = mkLength( te );
231 if( hasTerm( lt ) ){
232 // use own length if it exists, leads to shorter explanation
233 return lt;
234 }else{
235 EqcInfo * ei = getOrMakeEqcInfo( t, false );
236 Node length_term = ei ? ei->d_length_term : Node::null();
237 if( length_term.isNull() ){
238 //typically shouldnt be necessary
239 length_term = t;
240 }
241 Debug("strings") << "TheoryStrings::getLengthTerm " << t << " is " << length_term << std::endl;
242 addToExplanation( length_term, te, exp );
243 return Rewriter::rewrite( NodeManager::currentNM()->mkNode( kind::STRING_LENGTH, length_term ) );
244 }
245 }
246
247 Node TheoryStrings::getLength( Node t, std::vector< Node >& exp ) {
248 return getLengthExp( t, exp, t );
249 }
250
251 Node TheoryStrings::getNormalString(Node x, std::vector<Node>& nf_exp)
252 {
253 if (!x.isConst())
254 {
255 Node xr = getRepresentative(x);
256 if (d_normal_forms.find(xr) != d_normal_forms.end())
257 {
258 Node ret = mkConcat(d_normal_forms[xr]);
259 nf_exp.insert(nf_exp.end(),
260 d_normal_forms_exp[xr].begin(),
261 d_normal_forms_exp[xr].end());
262 addToExplanation(x, d_normal_forms_base[xr], nf_exp);
263 Trace("strings-debug")
264 << "Term: " << x << " has a normal form " << ret << std::endl;
265 return ret;
266 }
267 // if x does not have a normal form, then it should not occur in the
268 // equality engine and hence should be its own representative.
269 Assert(xr == x);
270 if (x.getKind() == kind::STRING_CONCAT)
271 {
272 std::vector<Node> vec_nodes;
273 for (unsigned i = 0; i < x.getNumChildren(); i++)
274 {
275 Node nc = getNormalString(x[i], nf_exp);
276 vec_nodes.push_back(nc);
277 }
278 return mkConcat(vec_nodes);
279 }
280 }
281 return x;
282 }
283
284 void TheoryStrings::setMasterEqualityEngine(eq::EqualityEngine* eq) {
285 d_equalityEngine.setMasterEqualityEngine(eq);
286 }
287
288 void TheoryStrings::addSharedTerm(TNode t) {
289 Debug("strings") << "TheoryStrings::addSharedTerm(): "
290 << t << " " << t.getType().isBoolean() << endl;
291 d_equalityEngine.addTriggerTerm(t, THEORY_STRINGS);
292 if (options::stringExp())
293 {
294 getExtTheory()->registerTermRec(t);
295 }
296 Debug("strings") << "TheoryStrings::addSharedTerm() finished" << std::endl;
297 }
298
299 EqualityStatus TheoryStrings::getEqualityStatus(TNode a, TNode b) {
300 if( d_equalityEngine.hasTerm(a) && d_equalityEngine.hasTerm(b) ){
301 if (d_equalityEngine.areEqual(a, b)) {
302 // The terms are implied to be equal
303 return EQUALITY_TRUE;
304 }
305 if (d_equalityEngine.areDisequal(a, b, false)) {
306 // The terms are implied to be dis-equal
307 return EQUALITY_FALSE;
308 }
309 }
310 return EQUALITY_UNKNOWN;
311 }
312
313 void TheoryStrings::propagate(Effort e) {
314 // direct propagation now
315 }
316
317 bool TheoryStrings::propagate(TNode literal) {
318 Debug("strings-propagate") << "TheoryStrings::propagate(" << literal << ")" << std::endl;
319 // If already in conflict, no more propagation
320 if (d_conflict) {
321 Debug("strings-propagate") << "TheoryStrings::propagate(" << literal << "): already in conflict" << std::endl;
322 return false;
323 }
324 // Propagate out
325 bool ok = d_out->propagate(literal);
326 if (!ok) {
327 d_conflict = true;
328 }
329 return ok;
330 }
331
332 /** explain */
333 void TheoryStrings::explain(TNode literal, std::vector<TNode>& assumptions) {
334 Debug("strings-explain") << "Explain " << literal << " " << d_conflict << std::endl;
335 bool polarity = literal.getKind() != kind::NOT;
336 TNode atom = polarity ? literal : literal[0];
337 unsigned ps = assumptions.size();
338 std::vector< TNode > tassumptions;
339 if (atom.getKind() == kind::EQUAL) {
340 if( atom[0]!=atom[1] ){
341 Assert( hasTerm( atom[0] ) );
342 Assert( hasTerm( atom[1] ) );
343 d_equalityEngine.explainEquality(atom[0], atom[1], polarity, tassumptions);
344 }
345 } else {
346 d_equalityEngine.explainPredicate(atom, polarity, tassumptions);
347 }
348 for( unsigned i=0; i<tassumptions.size(); i++ ){
349 if( std::find( assumptions.begin(), assumptions.end(), tassumptions[i] )==assumptions.end() ){
350 assumptions.push_back( tassumptions[i] );
351 }
352 }
353 if (Debug.isOn("strings-explain-debug"))
354 {
355 Debug("strings-explain-debug") << "Explanation for " << literal << " was "
356 << std::endl;
357 for (unsigned i = ps; i < assumptions.size(); i++)
358 {
359 Debug("strings-explain-debug") << " " << assumptions[i] << std::endl;
360 }
361 }
362 }
363
364 Node TheoryStrings::explain( TNode literal ){
365 Debug("strings-explain") << "explain called on " << literal << std::endl;
366 std::vector< TNode > assumptions;
367 explain( literal, assumptions );
368 if( assumptions.empty() ){
369 return d_true;
370 }else if( assumptions.size()==1 ){
371 return assumptions[0];
372 }else{
373 return NodeManager::currentNM()->mkNode( kind::AND, assumptions );
374 }
375 }
376
377 bool TheoryStrings::getCurrentSubstitution( int effort, std::vector< Node >& vars,
378 std::vector< Node >& subs, std::map< Node, std::vector< Node > >& exp ) {
379 Trace("strings-subs") << "getCurrentSubstitution, effort = " << effort << std::endl;
380 for( unsigned i=0; i<vars.size(); i++ ){
381 Node n = vars[i];
382 Trace("strings-subs") << " get subs for " << n << "..." << std::endl;
383 if( effort>=3 ){
384 //model values
385 Node mv = d_valuation.getModel()->getRepresentative( n );
386 Trace("strings-subs") << " model val : " << mv << std::endl;
387 subs.push_back( mv );
388 }else{
389 Node nr = getRepresentative( n );
390 std::map< Node, Node >::iterator itc = d_eqc_to_const.find( nr );
391 if( itc!=d_eqc_to_const.end() ){
392 //constant equivalence classes
393 Trace("strings-subs") << " constant eqc : " << d_eqc_to_const_exp[nr] << " " << d_eqc_to_const_base[nr] << " " << nr << std::endl;
394 subs.push_back( itc->second );
395 if( !d_eqc_to_const_exp[nr].isNull() ){
396 exp[n].push_back( d_eqc_to_const_exp[nr] );
397 }
398 if( !d_eqc_to_const_base[nr].isNull() ){
399 addToExplanation( n, d_eqc_to_const_base[nr], exp[n] );
400 }
401 }else if( effort>=1 && effort<3 && n.getType().isString() ){
402 //normal forms
403 Node ns = getNormalString( d_normal_forms_base[nr], exp[n] );
404 subs.push_back( ns );
405 Trace("strings-subs") << " normal eqc : " << ns << " " << d_normal_forms_base[nr] << " " << nr << std::endl;
406 if( !d_normal_forms_base[nr].isNull() ) {
407 addToExplanation( n, d_normal_forms_base[nr], exp[n] );
408 }
409 }else{
410 //representative?
411 //Trace("strings-subs") << " representative : " << nr << std::endl;
412 //addToExplanation( n, nr, exp[n] );
413 //subs.push_back( nr );
414 subs.push_back( n );
415 }
416 }
417 }
418 return true;
419 }
420
421 bool TheoryStrings::doReduction(int effort, Node n, bool& isCd)
422 {
423 Assert(d_extf_info_tmp.find(n) != d_extf_info_tmp.end());
424 if (!d_extf_info_tmp[n].d_model_active)
425 {
426 // n is not active in the model, no need to reduce
427 return false;
428 }
429 //determine the effort level to process the extf at
430 // 0 - at assertion time, 1+ - after no other reduction is applicable
431 int r_effort = -1;
432 // polarity : 1 true, -1 false, 0 neither
433 int pol = 0;
434 Kind k = n.getKind();
435 if (n.getType().isBoolean() && !d_extf_info_tmp[n].d_const.isNull())
436 {
437 pol = d_extf_info_tmp[n].d_const.getConst<bool>() ? 1 : -1;
438 }
439 if (k == STRING_STRCTN)
440 {
441 if (pol == 1)
442 {
443 r_effort = 1;
444 }
445 else if (pol == -1)
446 {
447 if (effort == 2)
448 {
449 Node x = n[0];
450 Node s = n[1];
451 std::vector<Node> lexp;
452 Node lenx = getLength(x, lexp);
453 Node lens = getLength(s, lexp);
454 if (areEqual(lenx, lens))
455 {
456 Trace("strings-extf-debug")
457 << " resolve extf : " << n
458 << " based on equal lengths disequality." << std::endl;
459 // We can reduce negative contains to a disequality when lengths are
460 // equal. In other words, len( x ) = len( s ) implies
461 // ~contains( x, s ) reduces to x != s.
462 if (!areDisequal(x, s))
463 {
464 // len( x ) = len( s ) ^ ~contains( x, s ) => x != s
465 lexp.push_back(lenx.eqNode(lens));
466 lexp.push_back(n.negate());
467 Node xneqs = x.eqNode(s).negate();
468 sendInference(lexp, xneqs, "NEG-CTN-EQL", true);
469 }
470 // this depends on the current assertions, so we set that this
471 // inference is context-dependent.
472 isCd = true;
473 return true;
474 }
475 else
476 {
477 r_effort = 2;
478 }
479 }
480 }
481 }
482 else
483 {
484 if (options::stringLazyPreproc())
485 {
486 if (k == STRING_SUBSTR)
487 {
488 r_effort = 1;
489 }
490 else if (k != STRING_IN_REGEXP)
491 {
492 r_effort = 2;
493 }
494 }
495 }
496 if (effort != r_effort)
497 {
498 // not the right effort level to reduce
499 return false;
500 }
501 Node c_n = pol == -1 ? n.negate() : n;
502 Trace("strings-process-debug")
503 << "Process reduction for " << n << ", pol = " << pol << std::endl;
504 if (k == STRING_STRCTN && pol == 1)
505 {
506 Node x = n[0];
507 Node s = n[1];
508 // positive contains reduces to a equality
509 Node sk1 =
510 d_sk_cache.mkSkolemCached(x, s, SkolemCache::SK_FIRST_CTN_PRE, "sc1");
511 Node sk2 =
512 d_sk_cache.mkSkolemCached(x, s, SkolemCache::SK_FIRST_CTN_POST, "sc2");
513 Node eq = Rewriter::rewrite(x.eqNode(mkConcat(sk1, s, sk2)));
514 std::vector<Node> exp_vec;
515 exp_vec.push_back(n);
516 sendInference(d_empty_vec, exp_vec, eq, "POS-CTN", true);
517 Trace("strings-extf-debug")
518 << " resolve extf : " << n << " based on positive contain reduction."
519 << std::endl;
520 Trace("strings-red-lemma") << "Reduction (positive contains) lemma : " << n
521 << " => " << eq << std::endl;
522 // context-dependent because it depends on the polarity of n itself
523 isCd = true;
524 }
525 else if (k != kind::STRING_CODE)
526 {
527 NodeManager* nm = NodeManager::currentNM();
528 Assert(k == STRING_SUBSTR || k == STRING_STRCTN || k == STRING_STRIDOF
529 || k == STRING_ITOS || k == STRING_STOI || k == STRING_STRREPL
530 || k == STRING_STRREPLALL || k == STRING_LEQ);
531 std::vector<Node> new_nodes;
532 Node res = d_preproc.simplify(n, new_nodes);
533 Assert(res != n);
534 new_nodes.push_back(res.eqNode(n));
535 Node nnlem =
536 new_nodes.size() == 1 ? new_nodes[0] : nm->mkNode(AND, new_nodes);
537 nnlem = Rewriter::rewrite(nnlem);
538 Trace("strings-red-lemma")
539 << "Reduction_" << effort << " lemma : " << nnlem << std::endl;
540 Trace("strings-red-lemma") << "...from " << n << std::endl;
541 sendInference(d_empty_vec, nnlem, "Reduction", true);
542 Trace("strings-extf-debug")
543 << " resolve extf : " << n << " based on reduction." << std::endl;
544 isCd = false;
545 }
546 return true;
547 }
548
549 /////////////////////////////////////////////////////////////////////////////
550 // NOTIFICATIONS
551 /////////////////////////////////////////////////////////////////////////////
552
553
554 void TheoryStrings::presolve() {
555 Debug("strings-presolve") << "TheoryStrings::Presolving : get fmf options " << (options::stringFMF() ? "true" : "false") << std::endl;
556 initializeStrategy();
557
558 // if strings fmf is enabled, register the strategy
559 if (options::stringFMF())
560 {
561 d_sslds.reset(new StringSumLengthDecisionStrategy(
562 getSatContext(), getUserContext(), d_valuation));
563 Trace("strings-dstrat-reg")
564 << "presolve: register decision strategy." << std::endl;
565 std::vector<Node> inputVars;
566 for (NodeSet::const_iterator itr = d_input_vars.begin();
567 itr != d_input_vars.end();
568 ++itr)
569 {
570 inputVars.push_back(*itr);
571 }
572 d_sslds->initialize(inputVars);
573 getDecisionManager()->registerStrategy(
574 DecisionManager::STRAT_STRINGS_SUM_LENGTHS, d_sslds.get());
575 }
576 }
577
578
579 /////////////////////////////////////////////////////////////////////////////
580 // MODEL GENERATION
581 /////////////////////////////////////////////////////////////////////////////
582
583 bool TheoryStrings::collectModelInfo(TheoryModel* m)
584 {
585 Trace("strings-model") << "TheoryStrings : Collect model info" << std::endl;
586 Trace("strings-model") << "TheoryStrings : assertEqualityEngine." << std::endl;
587
588 std::set<Node> termSet;
589
590 // Compute terms appearing in assertions and shared terms
591 computeRelevantTerms(termSet);
592 // assert the (relevant) portion of the equality engine to the model
593 if (!m->assertEqualityEngine(&d_equalityEngine, &termSet))
594 {
595 return false;
596 }
597
598 std::unordered_set<Node, NodeHashFunction> repSet;
599 NodeManager* nm = NodeManager::currentNM();
600 // Generate model
601 // get the relevant string equivalence classes
602 for (const Node& s : termSet)
603 {
604 if (s.getType().isString())
605 {
606 Node r = getRepresentative(s);
607 repSet.insert(r);
608 }
609 }
610 std::vector<Node> nodes(repSet.begin(), repSet.end());
611 std::map< Node, Node > processed;
612 std::vector< std::vector< Node > > col;
613 std::vector< Node > lts;
614 separateByLength( nodes, col, lts );
615 //step 1 : get all values for known lengths
616 std::vector< Node > lts_values;
617 std::map<unsigned, Node> values_used;
618 std::vector<Node> len_splits;
619 for( unsigned i=0; i<col.size(); i++ ) {
620 Trace("strings-model") << "Checking length for {";
621 for( unsigned j=0; j<col[i].size(); j++ ) {
622 if( j>0 ) {
623 Trace("strings-model") << ", ";
624 }
625 Trace("strings-model") << col[i][j];
626 }
627 Trace("strings-model") << " } (length is " << lts[i] << ")" << std::endl;
628 Node len_value;
629 if( lts[i].isConst() ) {
630 len_value = lts[i];
631 }
632 else if (!lts[i].isNull())
633 {
634 // get the model value for lts[i]
635 len_value = d_valuation.getModelValue(lts[i]);
636 }
637 if (len_value.isNull())
638 {
639 lts_values.push_back(Node::null());
640 }
641 else
642 {
643 Assert(len_value.getConst<Rational>() <= Rational(String::maxSize()),
644 "Exceeded UINT32_MAX in string model");
645 unsigned lvalue =
646 len_value.getConst<Rational>().getNumerator().toUnsignedInt();
647 std::map<unsigned, Node>::iterator itvu = values_used.find(lvalue);
648 if (itvu == values_used.end())
649 {
650 values_used[lvalue] = lts[i];
651 }
652 else
653 {
654 len_splits.push_back(lts[i].eqNode(itvu->second));
655 }
656 lts_values.push_back(len_value);
657 }
658 }
659 ////step 2 : assign arbitrary values for unknown lengths?
660 // confirmed by calculus invariant, see paper
661 Trace("strings-model") << "Assign to equivalence classes..." << std::endl;
662 std::map<Node, Node> pure_eq_assign;
663 //step 3 : assign values to equivalence classes that are pure variables
664 for( unsigned i=0; i<col.size(); i++ ){
665 std::vector< Node > pure_eq;
666 Trace("strings-model") << "The (" << col[i].size()
667 << ") equivalence classes ";
668 for (const Node& eqc : col[i])
669 {
670 Trace("strings-model") << eqc << " ";
671 //check if col[i][j] has only variables
672 if (!eqc.isConst())
673 {
674 Assert(d_normal_forms.find(eqc) != d_normal_forms.end());
675 if (d_normal_forms[eqc].size() == 1)
676 {
677 // does it have a code and the length of these equivalence classes are
678 // one?
679 if (d_has_str_code && lts_values[i] == d_one)
680 {
681 EqcInfo* eip = getOrMakeEqcInfo(eqc, false);
682 if (eip && !eip->d_code_term.get().isNull())
683 {
684 // its value must be equal to its code
685 Node ct = nm->mkNode(kind::STRING_CODE, eip->d_code_term.get());
686 Node ctv = d_valuation.getModelValue(ct);
687 unsigned cvalue =
688 ctv.getConst<Rational>().getNumerator().toUnsignedInt();
689 Trace("strings-model") << "(code: " << cvalue << ") ";
690 std::vector<unsigned> vec;
691 vec.push_back(String::convertCodeToUnsignedInt(cvalue));
692 Node mv = nm->mkConst(String(vec));
693 pure_eq_assign[eqc] = mv;
694 m->getEqualityEngine()->addTerm(mv);
695 }
696 }
697 pure_eq.push_back(eqc);
698 }
699 }
700 else
701 {
702 processed[eqc] = eqc;
703 }
704 }
705 Trace("strings-model") << "have length " << lts_values[i] << std::endl;
706
707 //assign a new length if necessary
708 if( !pure_eq.empty() ){
709 if( lts_values[i].isNull() ){
710 // start with length two (other lengths have special precendence)
711 unsigned lvalue = 2;
712 while( values_used.find( lvalue )!=values_used.end() ){
713 lvalue++;
714 }
715 Trace("strings-model") << "*** Decide to make length of " << lvalue << std::endl;
716 lts_values[i] = nm->mkConst(Rational(lvalue));
717 values_used[lvalue] = Node::null();
718 }
719 Trace("strings-model") << "Need to assign values of length " << lts_values[i] << " to equivalence classes ";
720 for( unsigned j=0; j<pure_eq.size(); j++ ){
721 Trace("strings-model") << pure_eq[j] << " ";
722 }
723 Trace("strings-model") << std::endl;
724
725 //use type enumerator
726 Assert(lts_values[i].getConst<Rational>() <= Rational(String::maxSize()),
727 "Exceeded UINT32_MAX in string model");
728 StringEnumeratorLength sel(lts_values[i].getConst<Rational>().getNumerator().toUnsignedInt());
729 for (const Node& eqc : pure_eq)
730 {
731 Node c;
732 std::map<Node, Node>::iterator itp = pure_eq_assign.find(eqc);
733 if (itp == pure_eq_assign.end())
734 {
735 Assert( !sel.isFinished() );
736 c = *sel;
737 while (m->hasTerm(c))
738 {
739 ++sel;
740 if (sel.isFinished())
741 {
742 // We are in a case where model construction is impossible due to
743 // an insufficient number of constants of a given length.
744
745 // Consider an integer equivalence class E whose value is assigned
746 // n in the model. Let { S_1, ..., S_m } be the set of string
747 // equivalence classes such that len( x ) is a member of E for
748 // some member x of each class S1, ...,Sm. Since our calculus is
749 // saturated with respect to cardinality inference (see Liang
750 // et al, Figure 6, CAV 2014), we have that m <= A^n, where A is
751 // the cardinality of our alphabet.
752
753 // Now, consider the case where there exists two integer
754 // equivalence classes E1 and E2 that are assigned n, and moreover
755 // we did not received notification from arithmetic that E1 = E2.
756 // This typically should never happen, but assume in the following
757 // that it does.
758
759 // Now, it may be the case that there are string equivalence
760 // classes { S_1, ..., S_m1 } whose lengths are in E1,
761 // and classes { S'_1, ..., S'_m2 } whose lengths are in E2, where
762 // m1 + m2 > A^n. In this case, we have insufficient strings to
763 // assign to { S_1, ..., S_m1, S'_1, ..., S'_m2 }. If this
764 // happens, we add a split on len( u1 ) = len( u2 ) for some
765 // len( u1 ) in E1, len( u2 ) in E2. We do this for each pair of
766 // integer equivalence classes that are assigned to the same value
767 // in the model.
768 AlwaysAssert(!len_splits.empty());
769 for (const Node& sl : len_splits)
770 {
771 Node spl = nm->mkNode(OR, sl, sl.negate());
772 d_out->lemma(spl);
773 }
774 return false;
775 }
776 c = *sel;
777 }
778 ++sel;
779 }
780 else
781 {
782 c = itp->second;
783 }
784 Trace("strings-model") << "*** Assigned constant " << c << " for "
785 << eqc << std::endl;
786 processed[eqc] = c;
787 if (!m->assertEquality(eqc, c, true))
788 {
789 return false;
790 }
791 }
792 }
793 }
794 Trace("strings-model") << "String Model : Pure Assigned." << std::endl;
795 //step 4 : assign constants to all other equivalence classes
796 for( unsigned i=0; i<nodes.size(); i++ ){
797 if( processed.find( nodes[i] )==processed.end() ){
798 Assert( d_normal_forms.find( nodes[i] )!=d_normal_forms.end() );
799 Trace("strings-model") << "Construct model for " << nodes[i] << " based on normal form ";
800 for( unsigned j=0; j<d_normal_forms[nodes[i]].size(); j++ ) {
801 if( j>0 ) Trace("strings-model") << " ++ ";
802 Trace("strings-model") << d_normal_forms[nodes[i]][j];
803 Node r = getRepresentative( d_normal_forms[nodes[i]][j] );
804 if( !r.isConst() && processed.find( r )==processed.end() ){
805 Trace("strings-model") << "(UNPROCESSED)";
806 }
807 }
808 Trace("strings-model") << std::endl;
809 std::vector< Node > nc;
810 for( unsigned j=0; j<d_normal_forms[nodes[i]].size(); j++ ) {
811 Node r = getRepresentative( d_normal_forms[nodes[i]][j] );
812 Assert( r.isConst() || processed.find( r )!=processed.end() );
813 nc.push_back(r.isConst() ? r : processed[r]);
814 }
815 Node cc = mkConcat( nc );
816 Assert( cc.getKind()==kind::CONST_STRING );
817 Trace("strings-model") << "*** Determined constant " << cc << " for " << nodes[i] << std::endl;
818 processed[nodes[i]] = cc;
819 if (!m->assertEquality(nodes[i], cc, true))
820 {
821 return false;
822 }
823 }
824 }
825 //Trace("strings-model") << "String Model : Assigned." << std::endl;
826 Trace("strings-model") << "String Model : Finished." << std::endl;
827 return true;
828 }
829
830 /////////////////////////////////////////////////////////////////////////////
831 // MAIN SOLVER
832 /////////////////////////////////////////////////////////////////////////////
833
834
835 void TheoryStrings::preRegisterTerm(TNode n) {
836 if( d_pregistered_terms_cache.find(n) == d_pregistered_terms_cache.end() ) {
837 d_pregistered_terms_cache.insert(n);
838 Trace("strings-preregister")
839 << "TheoryString::preregister : " << n << std::endl;
840 //check for logic exceptions
841 Kind k = n.getKind();
842 if( !options::stringExp() ){
843 if (k == kind::STRING_STRIDOF || k == kind::STRING_ITOS
844 || k == kind::STRING_STOI || k == kind::STRING_STRREPL
845 || k == kind::STRING_STRREPLALL || k == kind::STRING_STRCTN
846 || k == STRING_LEQ)
847 {
848 std::stringstream ss;
849 ss << "Term of kind " << k
850 << " not supported in default mode, try --strings-exp";
851 throw LogicException(ss.str());
852 }
853 }
854 switch (k)
855 {
856 case kind::EQUAL: {
857 d_equalityEngine.addTriggerEquality(n);
858 break;
859 }
860 case kind::STRING_IN_REGEXP: {
861 d_out->requirePhase(n, true);
862 d_equalityEngine.addTriggerPredicate(n);
863 d_equalityEngine.addTerm(n[0]);
864 d_equalityEngine.addTerm(n[1]);
865 break;
866 }
867 default: {
868 registerTerm(n, 0);
869 TypeNode tn = n.getType();
870 if (tn.isRegExp() && n.isVar())
871 {
872 std::stringstream ss;
873 ss << "Regular expression variables are not supported.";
874 throw LogicException(ss.str());
875 }
876 if( tn.isString() ) {
877 // all characters of constants should fall in the alphabet
878 if (n.isConst())
879 {
880 std::vector<unsigned> vec = n.getConst<String>().getVec();
881 for (unsigned u : vec)
882 {
883 if (u >= d_card_size)
884 {
885 std::stringstream ss;
886 ss << "Characters in string \"" << n
887 << "\" are outside of the given alphabet.";
888 throw LogicException(ss.str());
889 }
890 }
891 }
892 // if finite model finding is enabled,
893 // then we minimize the length of this term if it is a variable
894 // but not an internally generated Skolem, or a term that does
895 // not belong to this theory.
896 if (options::stringFMF()
897 && (n.isVar() ? !d_sk_cache.isSkolem(n)
898 : kindToTheoryId(k) != THEORY_STRINGS))
899 {
900 d_input_vars.insert(n);
901 Trace("strings-dstrat-reg") << "input variable: " << n << std::endl;
902 }
903 d_equalityEngine.addTerm(n);
904 } else if (tn.isBoolean()) {
905 // Get triggered for both equal and dis-equal
906 d_equalityEngine.addTriggerPredicate(n);
907 } else {
908 // Function applications/predicates
909 d_equalityEngine.addTerm(n);
910 }
911 // Set d_functionsTerms stores all function applications that are
912 // relevant to theory combination. Notice that this is a subset of
913 // the applications whose kinds are function kinds in the equality
914 // engine. This means it does not include applications of operators
915 // like re.++, which is not a function kind in the equality engine.
916 // Concatenation terms do not need to be considered here because
917 // their arguments have string type and do not introduce any shared
918 // terms.
919 if (n.hasOperator() && d_equalityEngine.isFunctionKind(k)
920 && k != kind::STRING_CONCAT)
921 {
922 d_functionsTerms.push_back( n );
923 }
924 }
925 }
926 }
927 }
928
929 Node TheoryStrings::expandDefinition(LogicRequest &logicRequest, Node node) {
930 Trace("strings-exp-def") << "TheoryStrings::expandDefinition : " << node << std::endl;
931 return node;
932 }
933
934 void TheoryStrings::check(Effort e) {
935 if (done() && e<EFFORT_FULL) {
936 return;
937 }
938
939 TimerStat::CodeTimer checkTimer(d_checkTime);
940
941 bool polarity;
942 TNode atom;
943
944 if( !done() && !hasTerm( d_emptyString ) ) {
945 preRegisterTerm( d_emptyString );
946 }
947
948 // Trace("strings-process") << "Theory of strings, check : " << e << std::endl;
949 Trace("strings-check") << "Theory of strings, check : " << e << std::endl;
950 while ( !done() && !d_conflict ) {
951 // Get all the assertions
952 Assertion assertion = get();
953 TNode fact = assertion.assertion;
954
955 Trace("strings-assertion") << "get assertion: " << fact << endl;
956 polarity = fact.getKind() != kind::NOT;
957 atom = polarity ? fact : fact[0];
958
959 //assert pending fact
960 assertPendingFact( atom, polarity, fact );
961 }
962 doPendingFacts();
963
964 Assert(d_strategy_init);
965 std::map<Effort, std::pair<unsigned, unsigned> >::iterator itsr =
966 d_strat_steps.find(e);
967 if (!d_conflict && !d_valuation.needCheck() && itsr != d_strat_steps.end())
968 {
969 Trace("strings-check") << "Theory of strings " << e << " effort check "
970 << std::endl;
971 if(Trace.isOn("strings-eqc")) {
972 for( unsigned t=0; t<2; t++ ) {
973 eq::EqClassesIterator eqcs2_i = eq::EqClassesIterator( &d_equalityEngine );
974 Trace("strings-eqc") << (t==0 ? "STRINGS:" : "OTHER:") << std::endl;
975 while( !eqcs2_i.isFinished() ){
976 Node eqc = (*eqcs2_i);
977 bool print = (t==0 && eqc.getType().isString() ) || (t==1 && !eqc.getType().isString() );
978 if (print) {
979 eq::EqClassIterator eqc2_i = eq::EqClassIterator( eqc, &d_equalityEngine );
980 Trace("strings-eqc") << "Eqc( " << eqc << " ) : { ";
981 while( !eqc2_i.isFinished() ) {
982 if( (*eqc2_i)!=eqc && (*eqc2_i).getKind()!=kind::EQUAL ){
983 Trace("strings-eqc") << (*eqc2_i) << " ";
984 }
985 ++eqc2_i;
986 }
987 Trace("strings-eqc") << " } " << std::endl;
988 EqcInfo * ei = getOrMakeEqcInfo( eqc, false );
989 if( ei ){
990 Trace("strings-eqc-debug") << "* Length term : " << ei->d_length_term.get() << std::endl;
991 Trace("strings-eqc-debug") << "* Cardinality lemma k : " << ei->d_cardinality_lem_k.get() << std::endl;
992 Trace("strings-eqc-debug") << "* Normalization length lemma : " << ei->d_normalized_length.get() << std::endl;
993 }
994 }
995 ++eqcs2_i;
996 }
997 Trace("strings-eqc") << std::endl;
998 }
999 Trace("strings-eqc") << std::endl;
1000 }
1001 unsigned sbegin = itsr->second.first;
1002 unsigned send = itsr->second.second;
1003 bool addedLemma = false;
1004 bool addedFact;
1005 do{
1006 runStrategy(sbegin, send);
1007 // flush the facts
1008 addedFact = !d_pending.empty();
1009 addedLemma = !d_lemma_cache.empty();
1010 doPendingFacts();
1011 doPendingLemmas();
1012 // repeat if we did not add a lemma or conflict
1013 }while( !d_conflict && !addedLemma && addedFact );
1014
1015 Trace("strings-check") << "Theory of strings done full effort check " << addedLemma << " " << d_conflict << std::endl;
1016 }
1017 Trace("strings-check") << "Theory of strings, done check : " << e << std::endl;
1018 Assert( d_pending.empty() );
1019 Assert( d_lemma_cache.empty() );
1020 }
1021
1022 bool TheoryStrings::needsCheckLastEffort() {
1023 if( options::stringGuessModel() ){
1024 return d_has_extf.get();
1025 }else{
1026 return false;
1027 }
1028 }
1029
1030 void TheoryStrings::checkExtfReductions( int effort ) {
1031 // Notice we don't make a standard call to ExtTheory::doReductions here,
1032 // since certain optimizations like context-dependent reductions and
1033 // stratifying effort levels are done in doReduction below.
1034 std::vector< Node > extf = getExtTheory()->getActive();
1035 Trace("strings-process") << " checking " << extf.size() << " active extf"
1036 << std::endl;
1037 for( unsigned i=0; i<extf.size(); i++ ){
1038 Assert(!d_conflict);
1039 Node n = extf[i];
1040 Trace("strings-process") << " check " << n << ", active in model="
1041 << d_extf_info_tmp[n].d_model_active << std::endl;
1042 // whether the reduction was context-dependent
1043 bool isCd = false;
1044 bool ret = doReduction(effort, n, isCd);
1045 if (ret)
1046 {
1047 getExtTheory()->markReduced(extf[i], isCd);
1048 if (hasProcessed())
1049 {
1050 return;
1051 }
1052 }
1053 }
1054 }
1055
1056 void TheoryStrings::checkMemberships()
1057 {
1058 // add the memberships
1059 std::vector<Node> mems = getExtTheory()->getActive(kind::STRING_IN_REGEXP);
1060 for (unsigned i = 0; i < mems.size(); i++)
1061 {
1062 Node n = mems[i];
1063 Assert(d_extf_info_tmp.find(n) != d_extf_info_tmp.end());
1064 if (!d_extf_info_tmp[n].d_const.isNull())
1065 {
1066 bool pol = d_extf_info_tmp[n].d_const.getConst<bool>();
1067 Trace("strings-process-debug")
1068 << " add membership : " << n << ", pol = " << pol << std::endl;
1069 d_regexp_solver.addMembership(pol ? n : n.negate());
1070 }
1071 else
1072 {
1073 Trace("strings-process-debug")
1074 << " irrelevant (non-asserted) membership : " << n << std::endl;
1075 }
1076 }
1077 d_regexp_solver.check();
1078 }
1079
1080 TheoryStrings::EqcInfo::EqcInfo(context::Context* c)
1081 : d_length_term(c),
1082 d_code_term(c),
1083 d_cardinality_lem_k(c),
1084 d_normalized_length(c)
1085 {
1086 }
1087
1088 TheoryStrings::EqcInfo * TheoryStrings::getOrMakeEqcInfo( Node eqc, bool doMake ) {
1089 std::map< Node, EqcInfo* >::iterator eqc_i = d_eqc_info.find( eqc );
1090 if( eqc_i==d_eqc_info.end() ){
1091 if( doMake ){
1092 EqcInfo* ei = new EqcInfo( getSatContext() );
1093 d_eqc_info[eqc] = ei;
1094 return ei;
1095 }else{
1096 return NULL;
1097 }
1098 }else{
1099 return (*eqc_i).second;
1100 }
1101 }
1102
1103
1104 /** Conflict when merging two constants */
1105 void TheoryStrings::conflict(TNode a, TNode b){
1106 if( !d_conflict ){
1107 Debug("strings-conflict") << "Making conflict..." << std::endl;
1108 d_conflict = true;
1109 Node conflictNode;
1110 conflictNode = explain( a.eqNode(b) );
1111 Trace("strings-conflict") << "CONFLICT: Eq engine conflict : " << conflictNode << std::endl;
1112 d_out->conflict( conflictNode );
1113 }
1114 }
1115
1116 /** called when a new equivalance class is created */
1117 void TheoryStrings::eqNotifyNewClass(TNode t){
1118 Kind k = t.getKind();
1119 if (k == kind::STRING_LENGTH || k == kind::STRING_CODE)
1120 {
1121 Trace("strings-debug") << "New length eqc : " << t << std::endl;
1122 Node r = d_equalityEngine.getRepresentative(t[0]);
1123 EqcInfo * ei = getOrMakeEqcInfo( r, true );
1124 if (k == kind::STRING_LENGTH)
1125 {
1126 ei->d_length_term = t[0];
1127 }
1128 else
1129 {
1130 ei->d_code_term = t[0];
1131 }
1132 //we care about the length of this string
1133 registerTerm( t[0], 1 );
1134 }else{
1135 //getExtTheory()->registerTerm( t );
1136 }
1137 }
1138
1139 /** called when two equivalance classes will merge */
1140 void TheoryStrings::eqNotifyPreMerge(TNode t1, TNode t2){
1141 EqcInfo * e2 = getOrMakeEqcInfo(t2, false);
1142 if( e2 ){
1143 EqcInfo * e1 = getOrMakeEqcInfo( t1 );
1144 //add information from e2 to e1
1145 if( !e2->d_length_term.get().isNull() ){
1146 e1->d_length_term.set( e2->d_length_term );
1147 }
1148 if (!e2->d_code_term.get().isNull())
1149 {
1150 e1->d_code_term.set(e2->d_code_term);
1151 }
1152 if( e2->d_cardinality_lem_k.get()>e1->d_cardinality_lem_k.get() ) {
1153 e1->d_cardinality_lem_k.set( e2->d_cardinality_lem_k );
1154 }
1155 if( !e2->d_normalized_length.get().isNull() ){
1156 e1->d_normalized_length.set( e2->d_normalized_length );
1157 }
1158 }
1159 }
1160
1161 /** called when two equivalance classes have merged */
1162 void TheoryStrings::eqNotifyPostMerge(TNode t1, TNode t2) {
1163
1164 }
1165
1166 /** called when two equivalance classes are disequal */
1167 void TheoryStrings::eqNotifyDisequal(TNode t1, TNode t2, TNode reason) {
1168 if( t1.getType().isString() ){
1169 //store disequalities between strings, may need to check if their lengths are equal/disequal
1170 d_ee_disequalities.push_back( t1.eqNode( t2 ) );
1171 }
1172 }
1173
1174 void TheoryStrings::addCarePairs(TNodeTrie* t1,
1175 TNodeTrie* t2,
1176 unsigned arity,
1177 unsigned depth)
1178 {
1179 if( depth==arity ){
1180 if( t2!=NULL ){
1181 Node f1 = t1->getData();
1182 Node f2 = t2->getData();
1183 if( !d_equalityEngine.areEqual( f1, f2 ) ){
1184 Trace("strings-cg-debug") << "TheoryStrings::computeCareGraph(): checking function " << f1 << " and " << f2 << std::endl;
1185 vector< pair<TNode, TNode> > currentPairs;
1186 for (unsigned k = 0; k < f1.getNumChildren(); ++ k) {
1187 TNode x = f1[k];
1188 TNode y = f2[k];
1189 Assert( d_equalityEngine.hasTerm(x) );
1190 Assert( d_equalityEngine.hasTerm(y) );
1191 Assert( !d_equalityEngine.areDisequal( x, y, false ) );
1192 Assert( !areCareDisequal( x, y ) );
1193 if( !d_equalityEngine.areEqual( x, y ) ){
1194 if( d_equalityEngine.isTriggerTerm(x, THEORY_STRINGS) && d_equalityEngine.isTriggerTerm(y, THEORY_STRINGS) ){
1195 TNode x_shared = d_equalityEngine.getTriggerTermRepresentative(x, THEORY_STRINGS);
1196 TNode y_shared = d_equalityEngine.getTriggerTermRepresentative(y, THEORY_STRINGS);
1197 currentPairs.push_back(make_pair(x_shared, y_shared));
1198 }
1199 }
1200 }
1201 for (unsigned c = 0; c < currentPairs.size(); ++ c) {
1202 Trace("strings-cg-pair") << "TheoryStrings::computeCareGraph(): pair : " << currentPairs[c].first << " " << currentPairs[c].second << std::endl;
1203 addCarePair(currentPairs[c].first, currentPairs[c].second);
1204 }
1205 }
1206 }
1207 }else{
1208 if( t2==NULL ){
1209 if( depth<(arity-1) ){
1210 //add care pairs internal to each child
1211 for (std::pair<const TNode, TNodeTrie>& tt : t1->d_data)
1212 {
1213 addCarePairs(&tt.second, nullptr, arity, depth + 1);
1214 }
1215 }
1216 //add care pairs based on each pair of non-disequal arguments
1217 for (std::map<TNode, TNodeTrie>::iterator it = t1->d_data.begin();
1218 it != t1->d_data.end();
1219 ++it)
1220 {
1221 std::map<TNode, TNodeTrie>::iterator it2 = it;
1222 ++it2;
1223 for( ; it2 != t1->d_data.end(); ++it2 ){
1224 if( !d_equalityEngine.areDisequal(it->first, it2->first, false) ){
1225 if( !areCareDisequal(it->first, it2->first) ){
1226 addCarePairs( &it->second, &it2->second, arity, depth+1 );
1227 }
1228 }
1229 }
1230 }
1231 }else{
1232 //add care pairs based on product of indices, non-disequal arguments
1233 for (std::pair<const TNode, TNodeTrie>& tt1 : t1->d_data)
1234 {
1235 for (std::pair<const TNode, TNodeTrie>& tt2 : t2->d_data)
1236 {
1237 if (!d_equalityEngine.areDisequal(tt1.first, tt2.first, false))
1238 {
1239 if (!areCareDisequal(tt1.first, tt2.first))
1240 {
1241 addCarePairs(&tt1.second, &tt2.second, arity, depth + 1);
1242 }
1243 }
1244 }
1245 }
1246 }
1247 }
1248 }
1249
1250 void TheoryStrings::computeCareGraph(){
1251 //computing the care graph here is probably still necessary, due to operators that take non-string arguments TODO: verify
1252 Trace("strings-cg") << "TheoryStrings::computeCareGraph(): Build term indices..." << std::endl;
1253 std::map<Node, TNodeTrie> index;
1254 std::map< Node, unsigned > arity;
1255 unsigned functionTerms = d_functionsTerms.size();
1256 for (unsigned i = 0; i < functionTerms; ++ i) {
1257 TNode f1 = d_functionsTerms[i];
1258 Trace("strings-cg") << "...build for " << f1 << std::endl;
1259 Node op = f1.getOperator();
1260 std::vector< TNode > reps;
1261 bool has_trigger_arg = false;
1262 for( unsigned j=0; j<f1.getNumChildren(); j++ ){
1263 reps.push_back( d_equalityEngine.getRepresentative( f1[j] ) );
1264 if( d_equalityEngine.isTriggerTerm( f1[j], THEORY_STRINGS ) ){
1265 has_trigger_arg = true;
1266 }
1267 }
1268 if( has_trigger_arg ){
1269 index[op].addTerm( f1, reps );
1270 arity[op] = reps.size();
1271 }
1272 }
1273 //for each index
1274 for (std::pair<const Node, TNodeTrie>& tt : index)
1275 {
1276 Trace("strings-cg") << "TheoryStrings::computeCareGraph(): Process index "
1277 << tt.first << "..." << std::endl;
1278 addCarePairs(&tt.second, nullptr, arity[tt.first], 0);
1279 }
1280 }
1281
1282 void TheoryStrings::assertPendingFact(Node atom, bool polarity, Node exp) {
1283 Trace("strings-pending") << "Assert pending fact : " << atom << " " << polarity << " from " << exp << std::endl;
1284 Assert(atom.getKind() != kind::OR, "Infer error: a split.");
1285 if( atom.getKind()==kind::EQUAL ){
1286 Trace("strings-pending-debug") << " Register term" << std::endl;
1287 for( unsigned j=0; j<2; j++ ) {
1288 if( !d_equalityEngine.hasTerm( atom[j] ) && atom[j].getType().isString() ) {
1289 registerTerm( atom[j], 0 );
1290 }
1291 }
1292 Trace("strings-pending-debug") << " Now assert equality" << std::endl;
1293 d_equalityEngine.assertEquality( atom, polarity, exp );
1294 Trace("strings-pending-debug") << " Finished assert equality" << std::endl;
1295 } else {
1296 d_equalityEngine.assertPredicate( atom, polarity, exp );
1297 //process extf
1298 if( atom.getKind()==kind::STRING_IN_REGEXP ){
1299 if( polarity && atom[1].getKind()==kind::REGEXP_RANGE ){
1300 if( d_extf_infer_cache_u.find( atom )==d_extf_infer_cache_u.end() ){
1301 d_extf_infer_cache_u.insert( atom );
1302 //length of first argument is one
1303 Node conc = d_one.eqNode( NodeManager::currentNM()->mkNode( kind::STRING_LENGTH, atom[0] ) );
1304 Node lem = NodeManager::currentNM()->mkNode( kind::OR, atom.negate(), conc );
1305 Trace("strings-lemma") << "Strings::Lemma RE-Range-Len : " << lem << std::endl;
1306 d_out->lemma( lem );
1307 }
1308 }
1309 }
1310 //register the atom here, since it may not create a new equivalence class
1311 //getExtTheory()->registerTerm( atom );
1312 }
1313 Trace("strings-pending-debug") << " Now collect terms" << std::endl;
1314 // Collect extended function terms in the atom. Notice that we must register
1315 // all extended functions occurring in assertions and shared terms. We
1316 // make a similar call to registerTermRec in addSharedTerm.
1317 getExtTheory()->registerTermRec( atom );
1318 Trace("strings-pending-debug") << " Finished collect terms" << std::endl;
1319 }
1320
1321 void TheoryStrings::doPendingFacts() {
1322 size_t i=0;
1323 while( !d_conflict && i<d_pending.size() ) {
1324 Node fact = d_pending[i];
1325 Node exp = d_pending_exp[ fact ];
1326 if(fact.getKind() == kind::AND) {
1327 for(size_t j=0; j<fact.getNumChildren(); j++) {
1328 bool polarity = fact[j].getKind() != kind::NOT;
1329 TNode atom = polarity ? fact[j] : fact[j][0];
1330 assertPendingFact(atom, polarity, exp);
1331 }
1332 } else {
1333 bool polarity = fact.getKind() != kind::NOT;
1334 TNode atom = polarity ? fact : fact[0];
1335 assertPendingFact(atom, polarity, exp);
1336 }
1337 i++;
1338 }
1339 d_pending.clear();
1340 d_pending_exp.clear();
1341 }
1342
1343 void TheoryStrings::doPendingLemmas() {
1344 if( !d_conflict && !d_lemma_cache.empty() ){
1345 for( unsigned i=0; i<d_lemma_cache.size(); i++ ){
1346 Trace("strings-pending") << "Process pending lemma : " << d_lemma_cache[i] << std::endl;
1347 d_out->lemma( d_lemma_cache[i] );
1348 }
1349 for( std::map< Node, bool >::iterator it = d_pending_req_phase.begin(); it != d_pending_req_phase.end(); ++it ){
1350 Trace("strings-pending") << "Require phase : " << it->first << ", polarity = " << it->second << std::endl;
1351 d_out->requirePhase( it->first, it->second );
1352 }
1353 }
1354 d_lemma_cache.clear();
1355 d_pending_req_phase.clear();
1356 }
1357
1358 bool TheoryStrings::hasProcessed() {
1359 return d_conflict || !d_lemma_cache.empty() || !d_pending.empty();
1360 }
1361
1362 void TheoryStrings::addToExplanation( Node a, Node b, std::vector< Node >& exp ) {
1363 if( a!=b ){
1364 Debug("strings-explain") << "Add to explanation : " << a << " == " << b << std::endl;
1365 Assert( areEqual( a, b ) );
1366 exp.push_back( a.eqNode( b ) );
1367 }
1368 }
1369
1370 void TheoryStrings::addToExplanation( Node lit, std::vector< Node >& exp ) {
1371 if( !lit.isNull() ){
1372 exp.push_back( lit );
1373 }
1374 }
1375
1376 void TheoryStrings::checkInit() {
1377 //build term index
1378 d_eqc_to_const.clear();
1379 d_eqc_to_const_base.clear();
1380 d_eqc_to_const_exp.clear();
1381 d_eqc_to_len_term.clear();
1382 d_term_index.clear();
1383 d_strings_eqc.clear();
1384
1385 std::map< Kind, unsigned > ncongruent;
1386 std::map< Kind, unsigned > congruent;
1387 d_emptyString_r = getRepresentative( d_emptyString );
1388 eq::EqClassesIterator eqcs_i = eq::EqClassesIterator( &d_equalityEngine );
1389 while( !eqcs_i.isFinished() ){
1390 Node eqc = (*eqcs_i);
1391 TypeNode tn = eqc.getType();
1392 if( !tn.isRegExp() ){
1393 if( tn.isString() ){
1394 d_strings_eqc.push_back( eqc );
1395 }
1396 Node var;
1397 eq::EqClassIterator eqc_i = eq::EqClassIterator( eqc, &d_equalityEngine );
1398 while( !eqc_i.isFinished() ) {
1399 Node n = *eqc_i;
1400 if( n.isConst() ){
1401 d_eqc_to_const[eqc] = n;
1402 d_eqc_to_const_base[eqc] = n;
1403 d_eqc_to_const_exp[eqc] = Node::null();
1404 }else if( tn.isInteger() ){
1405 if( n.getKind()==kind::STRING_LENGTH ){
1406 Node nr = getRepresentative( n[0] );
1407 d_eqc_to_len_term[nr] = n[0];
1408 }
1409 }else if( n.getNumChildren()>0 ){
1410 Kind k = n.getKind();
1411 if( k!=kind::EQUAL ){
1412 if( d_congruent.find( n )==d_congruent.end() ){
1413 std::vector< Node > c;
1414 Node nc = d_term_index[k].add( n, 0, this, d_emptyString_r, c );
1415 if( nc!=n ){
1416 //check if we have inferred a new equality by removal of empty components
1417 if( n.getKind()==kind::STRING_CONCAT && !areEqual( nc, n ) ){
1418 std::vector< Node > exp;
1419 unsigned count[2] = { 0, 0 };
1420 while( count[0]<nc.getNumChildren() || count[1]<n.getNumChildren() ){
1421 //explain empty prefixes
1422 for( unsigned t=0; t<2; t++ ){
1423 Node nn = t==0 ? nc : n;
1424 while( count[t]<nn.getNumChildren() &&
1425 ( nn[count[t]]==d_emptyString || areEqual( nn[count[t]], d_emptyString ) ) ){
1426 if( nn[count[t]]!=d_emptyString ){
1427 exp.push_back( nn[count[t]].eqNode( d_emptyString ) );
1428 }
1429 count[t]++;
1430 }
1431 }
1432 //explain equal components
1433 if( count[0]<nc.getNumChildren() ){
1434 Assert( count[1]<n.getNumChildren() );
1435 if( nc[count[0]]!=n[count[1]] ){
1436 exp.push_back( nc[count[0]].eqNode( n[count[1]] ) );
1437 }
1438 count[0]++;
1439 count[1]++;
1440 }
1441 }
1442 //infer the equality
1443 sendInference( exp, n.eqNode( nc ), "I_Norm" );
1444 }else if( getExtTheory()->hasFunctionKind( n.getKind() ) ){
1445 //mark as congruent : only process if neither has been reduced
1446 getExtTheory()->markCongruent( nc, n );
1447 }
1448 //this node is congruent to another one, we can ignore it
1449 Trace("strings-process-debug") << " congruent term : " << n << std::endl;
1450 d_congruent.insert( n );
1451 congruent[k]++;
1452 }else if( k==kind::STRING_CONCAT && c.size()==1 ){
1453 Trace("strings-process-debug") << " congruent term by singular : " << n << " " << c[0] << std::endl;
1454 //singular case
1455 if( !areEqual( c[0], n ) ){
1456 std::vector< Node > exp;
1457 //explain empty components
1458 bool foundNEmpty = false;
1459 for( unsigned i=0; i<n.getNumChildren(); i++ ){
1460 if( areEqual( n[i], d_emptyString ) ){
1461 if( n[i]!=d_emptyString ){
1462 exp.push_back( n[i].eqNode( d_emptyString ) );
1463 }
1464 }else{
1465 Assert( !foundNEmpty );
1466 if( n[i]!=c[0] ){
1467 exp.push_back( n[i].eqNode( c[0] ) );
1468 }
1469 foundNEmpty = true;
1470 }
1471 }
1472 AlwaysAssert( foundNEmpty );
1473 //infer the equality
1474 sendInference( exp, n.eqNode( c[0] ), "I_Norm_S" );
1475 }
1476 d_congruent.insert( n );
1477 congruent[k]++;
1478 }else{
1479 ncongruent[k]++;
1480 }
1481 }else{
1482 congruent[k]++;
1483 }
1484 }
1485 }else{
1486 if( d_congruent.find( n )==d_congruent.end() ){
1487 if( var.isNull() ){
1488 var = n;
1489 }else{
1490 Trace("strings-process-debug") << " congruent variable : " << n << std::endl;
1491 d_congruent.insert( n );
1492 }
1493 }
1494 }
1495 ++eqc_i;
1496 }
1497 }
1498 ++eqcs_i;
1499 }
1500 if( Trace.isOn("strings-process") ){
1501 for( std::map< Kind, TermIndex >::iterator it = d_term_index.begin(); it != d_term_index.end(); ++it ){
1502 Trace("strings-process") << " Terms[" << it->first << "] = " << ncongruent[it->first] << "/" << (congruent[it->first]+ncongruent[it->first]) << std::endl;
1503 }
1504 }
1505 }
1506
1507 void TheoryStrings::checkConstantEquivalenceClasses()
1508 {
1509 // do fixed point
1510 unsigned prevSize;
1511 std::vector<Node> vecc;
1512 do
1513 {
1514 vecc.clear();
1515 Trace("strings-process-debug") << "Check constant equivalence classes..."
1516 << std::endl;
1517 prevSize = d_eqc_to_const.size();
1518 checkConstantEquivalenceClasses(&d_term_index[kind::STRING_CONCAT], vecc);
1519 } while (!hasProcessed() && d_eqc_to_const.size() > prevSize);
1520 }
1521
1522 void TheoryStrings::checkConstantEquivalenceClasses( TermIndex* ti, std::vector< Node >& vecc ) {
1523 Node n = ti->d_data;
1524 if( !n.isNull() ){
1525 //construct the constant
1526 Node c = mkConcat( vecc );
1527 if( !areEqual( n, c ) ){
1528 Trace("strings-debug") << "Constant eqc : " << c << " for " << n << std::endl;
1529 Trace("strings-debug") << " ";
1530 for( unsigned i=0; i<vecc.size(); i++ ){
1531 Trace("strings-debug") << vecc[i] << " ";
1532 }
1533 Trace("strings-debug") << std::endl;
1534 unsigned count = 0;
1535 unsigned countc = 0;
1536 std::vector< Node > exp;
1537 while( count<n.getNumChildren() ){
1538 while( count<n.getNumChildren() && areEqual( n[count], d_emptyString ) ){
1539 addToExplanation( n[count], d_emptyString, exp );
1540 count++;
1541 }
1542 if( count<n.getNumChildren() ){
1543 Trace("strings-debug") << "...explain " << n[count] << " " << vecc[countc] << std::endl;
1544 if( !areEqual( n[count], vecc[countc] ) ){
1545 Node nrr = getRepresentative( n[count] );
1546 Assert( !d_eqc_to_const_exp[nrr].isNull() );
1547 addToExplanation( n[count], d_eqc_to_const_base[nrr], exp );
1548 exp.push_back( d_eqc_to_const_exp[nrr] );
1549 }else{
1550 addToExplanation( n[count], vecc[countc], exp );
1551 }
1552 countc++;
1553 count++;
1554 }
1555 }
1556 //exp contains an explanation of n==c
1557 Assert( countc==vecc.size() );
1558 if( hasTerm( c ) ){
1559 sendInference( exp, n.eqNode( c ), "I_CONST_MERGE" );
1560 return;
1561 }else if( !hasProcessed() ){
1562 Node nr = getRepresentative( n );
1563 std::map< Node, Node >::iterator it = d_eqc_to_const.find( nr );
1564 if( it==d_eqc_to_const.end() ){
1565 Trace("strings-debug") << "Set eqc const " << n << " to " << c << std::endl;
1566 d_eqc_to_const[nr] = c;
1567 d_eqc_to_const_base[nr] = n;
1568 d_eqc_to_const_exp[nr] = mkAnd( exp );
1569 }else if( c!=it->second ){
1570 //conflict
1571 Trace("strings-debug") << "Conflict, other constant was " << it->second << ", this constant was " << c << std::endl;
1572 if( d_eqc_to_const_exp[nr].isNull() ){
1573 // n==c ^ n == c' => false
1574 addToExplanation( n, it->second, exp );
1575 }else{
1576 // n==c ^ n == d_eqc_to_const_base[nr] == c' => false
1577 exp.push_back( d_eqc_to_const_exp[nr] );
1578 addToExplanation( n, d_eqc_to_const_base[nr], exp );
1579 }
1580 sendInference( exp, d_false, "I_CONST_CONFLICT" );
1581 return;
1582 }else{
1583 Trace("strings-debug") << "Duplicate constant." << std::endl;
1584 }
1585 }
1586 }
1587 }
1588 for( std::map< TNode, TermIndex >::iterator it = ti->d_children.begin(); it != ti->d_children.end(); ++it ){
1589 std::map< Node, Node >::iterator itc = d_eqc_to_const.find( it->first );
1590 if( itc!=d_eqc_to_const.end() ){
1591 vecc.push_back( itc->second );
1592 checkConstantEquivalenceClasses( &it->second, vecc );
1593 vecc.pop_back();
1594 if( hasProcessed() ){
1595 break;
1596 }
1597 }
1598 }
1599 }
1600
1601 void TheoryStrings::checkExtfEval( int effort ) {
1602 Trace("strings-extf-list") << "Active extended functions, effort=" << effort << " : " << std::endl;
1603 d_extf_info_tmp.clear();
1604 bool has_nreduce = false;
1605 std::vector< Node > terms = getExtTheory()->getActive();
1606 std::vector< Node > sterms;
1607 std::vector< std::vector< Node > > exp;
1608 getExtTheory()->getSubstitutedTerms( effort, terms, sterms, exp );
1609 for( unsigned i=0; i<terms.size(); i++ ){
1610 Node n = terms[i];
1611 Node sn = sterms[i];
1612 //setup information about extf
1613 ExtfInfoTmp& einfo = d_extf_info_tmp[n];
1614 Node r = getRepresentative(n);
1615 std::map<Node, Node>::iterator itcit = d_eqc_to_const.find(r);
1616 if (itcit != d_eqc_to_const.end())
1617 {
1618 einfo.d_const = itcit->second;
1619 }
1620 Trace("strings-extf-debug") << "Check extf " << n << " == " << sn
1621 << ", constant = " << einfo.d_const
1622 << ", effort=" << effort << "..." << std::endl;
1623 //do the inference
1624 Node to_reduce;
1625 if( n!=sn ){
1626 einfo.d_exp.insert(einfo.d_exp.end(), exp[i].begin(), exp[i].end());
1627 // inference is rewriting the substituted node
1628 Node nrc = Rewriter::rewrite( sn );
1629 Kind nrck = nrc.getKind();
1630 //if rewrites to a constant, then do the inference and mark as reduced
1631 if( nrc.isConst() ){
1632 if( effort<3 ){
1633 getExtTheory()->markReduced( n );
1634 Trace("strings-extf-debug") << " resolvable by evaluation..." << std::endl;
1635 std::vector< Node > exps;
1636 // The following optimization gets the "symbolic definition" of
1637 // an extended term. The symbolic definition of a term t is a term
1638 // t' where constants are replaced by their corresponding proxy
1639 // variables.
1640 // For example, if lsym is a proxy variable for "", then
1641 // str.replace( lsym, lsym, lsym ) is the symbolic definition for
1642 // str.replace( "", "", "" ). It is generally better to use symbolic
1643 // definitions when doing cd-rewriting for the purpose of minimizing
1644 // clauses, e.g. we infer the unit equality:
1645 // str.replace( lsym, lsym, lsym ) == ""
1646 // instead of making this inference multiple times:
1647 // x = "" => str.replace( x, x, x ) == ""
1648 // y = "" => str.replace( y, y, y ) == ""
1649 Trace("strings-extf-debug") << " get symbolic definition..." << std::endl;
1650 Node nrs = getSymbolicDefinition( sn, exps );
1651 if( !nrs.isNull() ){
1652 Trace("strings-extf-debug") << " rewrite " << nrs << "..." << std::endl;
1653 Node nrsr = Rewriter::rewrite(nrs);
1654 // ensure the symbolic form is not rewritable
1655 if (nrsr != nrs)
1656 {
1657 // we cannot use the symbolic definition if it rewrites
1658 Trace("strings-extf-debug") << " symbolic definition is trivial..." << std::endl;
1659 nrs = Node::null();
1660 }
1661 }else{
1662 Trace("strings-extf-debug") << " could not infer symbolic definition." << std::endl;
1663 }
1664 Node conc;
1665 if( !nrs.isNull() ){
1666 Trace("strings-extf-debug") << " symbolic def : " << nrs << std::endl;
1667 if( !areEqual( nrs, nrc ) ){
1668 //infer symbolic unit
1669 if( n.getType().isBoolean() ){
1670 conc = nrc==d_true ? nrs : nrs.negate();
1671 }else{
1672 conc = nrs.eqNode( nrc );
1673 }
1674 einfo.d_exp.clear();
1675 }
1676 }else{
1677 if( !areEqual( n, nrc ) ){
1678 if( n.getType().isBoolean() ){
1679 if( areEqual( n, nrc==d_true ? d_false : d_true ) ){
1680 einfo.d_exp.push_back(nrc == d_true ? n.negate() : n);
1681 conc = d_false;
1682 }else{
1683 conc = nrc==d_true ? n : n.negate();
1684 }
1685 }else{
1686 conc = n.eqNode( nrc );
1687 }
1688 }
1689 }
1690 if( !conc.isNull() ){
1691 Trace("strings-extf") << " resolve extf : " << sn << " -> " << nrc << std::endl;
1692 sendInference(
1693 einfo.d_exp, conc, effort == 0 ? "EXTF" : "EXTF-N", true);
1694 if( d_conflict ){
1695 Trace("strings-extf-debug") << " conflict, return." << std::endl;
1696 return;
1697 }
1698 }
1699 }else{
1700 //check if it is already equal, if so, mark as reduced. Otherwise, do nothing.
1701 if( areEqual( n, nrc ) ){
1702 Trace("strings-extf") << " resolved extf, since satisfied by model: " << n << std::endl;
1703 einfo.d_model_active = false;
1704 }
1705 }
1706 }
1707 else
1708 {
1709 bool reduced = false;
1710 if (!einfo.d_const.isNull() && nrc.getType().isBoolean())
1711 {
1712 bool pol = einfo.d_const == d_true;
1713 Node nrcAssert = pol ? nrc : nrc.negate();
1714 Node nAssert = pol ? n : n.negate();
1715 Assert(effort < 3);
1716 einfo.d_exp.push_back(nAssert);
1717 Trace("strings-extf-debug") << " decomposable..." << std::endl;
1718 Trace("strings-extf") << " resolve extf : " << sn << " -> " << nrc
1719 << ", const = " << einfo.d_const << std::endl;
1720 reduced = sendInternalInference(
1721 einfo.d_exp, nrcAssert, effort == 0 ? "EXTF_d" : "EXTF_d-N");
1722 if (!reduced)
1723 {
1724 Trace("strings-extf") << "EXT: could not fully reduce ";
1725 Trace("strings-extf")
1726 << nAssert << " via " << nrcAssert << std::endl;
1727 }
1728 }
1729 if (reduced)
1730 {
1731 getExtTheory()->markReduced(n);
1732 }
1733 else
1734 {
1735 to_reduce = nrc;
1736 }
1737 }
1738 }else{
1739 to_reduce = sterms[i];
1740 }
1741 //if not reduced
1742 if( !to_reduce.isNull() ){
1743 Assert( effort<3 );
1744 if( effort==1 ){
1745 Trace("strings-extf") << " cannot rewrite extf : " << to_reduce << std::endl;
1746 }
1747 checkExtfInference(n, to_reduce, einfo, effort);
1748 if( Trace.isOn("strings-extf-list") ){
1749 Trace("strings-extf-list") << " * " << to_reduce;
1750 if (!einfo.d_const.isNull())
1751 {
1752 Trace("strings-extf-list") << ", const = " << einfo.d_const;
1753 }
1754 if( n!=to_reduce ){
1755 Trace("strings-extf-list") << ", from " << n;
1756 }
1757 Trace("strings-extf-list") << std::endl;
1758 }
1759 if (getExtTheory()->isActive(n) && einfo.d_model_active)
1760 {
1761 has_nreduce = true;
1762 }
1763 }
1764 }
1765 d_has_extf = has_nreduce;
1766 }
1767
1768 void TheoryStrings::checkExtfInference( Node n, Node nr, ExtfInfoTmp& in, int effort ){
1769 if (in.d_const.isNull())
1770 {
1771 return;
1772 }
1773 NodeManager* nm = NodeManager::currentNM();
1774 Trace("strings-extf-infer") << "checkExtfInference: " << n << " : " << nr
1775 << " == " << in.d_const << std::endl;
1776
1777 // add original to explanation
1778 if (n.getType().isBoolean())
1779 {
1780 // if Boolean, it's easy
1781 in.d_exp.push_back(in.d_const.getConst<bool>() ? n : n.negate());
1782 }
1783 else
1784 {
1785 // otherwise, must explain via base node
1786 Node r = getRepresentative(n);
1787 // we have that:
1788 // d_eqc_to_const_exp[r] => d_eqc_to_const_base[r] = in.d_const
1789 // thus:
1790 // n = d_eqc_to_const_base[r] ^ d_eqc_to_const_exp[r] => n = in.d_const
1791 Assert(d_eqc_to_const_base.find(r) != d_eqc_to_const_base.end());
1792 addToExplanation(n, d_eqc_to_const_base[r], in.d_exp);
1793 Assert(d_eqc_to_const_exp.find(r) != d_eqc_to_const_exp.end());
1794 in.d_exp.insert(in.d_exp.end(),
1795 d_eqc_to_const_exp[r].begin(),
1796 d_eqc_to_const_exp[r].end());
1797 }
1798
1799 // d_extf_infer_cache stores whether we have made the inferences associated
1800 // with a node n,
1801 // this may need to be generalized if multiple inferences apply
1802
1803 if (nr.getKind() == STRING_STRCTN)
1804 {
1805 Assert(in.d_const.isConst());
1806 bool pol = in.d_const.getConst<bool>();
1807 if ((pol && nr[1].getKind() == STRING_CONCAT)
1808 || (!pol && nr[0].getKind() == STRING_CONCAT))
1809 {
1810 // If str.contains( x, str.++( y1, ..., yn ) ),
1811 // we may infer str.contains( x, y1 ), ..., str.contains( x, yn )
1812 // The following recognizes two situations related to the above reasoning:
1813 // (1) If ~str.contains( x, yi ) holds for some i, we are in conflict,
1814 // (2) If str.contains( x, yj ) already holds for some j, then the term
1815 // str.contains( x, yj ) is irrelevant since it is satisfied by all models
1816 // for str.contains( x, str.++( y1, ..., yn ) ).
1817
1818 // Notice that the dual of the above reasoning also holds, i.e.
1819 // If ~str.contains( str.++( x1, ..., xn ), y ),
1820 // we may infer ~str.contains( x1, y ), ..., ~str.contains( xn, y )
1821 // This is also handled here.
1822 if (d_extf_infer_cache.find(nr) == d_extf_infer_cache.end())
1823 {
1824 d_extf_infer_cache.insert(nr);
1825
1826 int index = pol ? 1 : 0;
1827 std::vector<Node> children;
1828 children.push_back(nr[0]);
1829 children.push_back(nr[1]);
1830 for (const Node& nrc : nr[index])
1831 {
1832 children[index] = nrc;
1833 Node conc = nm->mkNode(STRING_STRCTN, children);
1834 conc = Rewriter::rewrite(pol ? conc : conc.negate());
1835 // check if it already (does not) hold
1836 if (hasTerm(conc))
1837 {
1838 if (areEqual(conc, d_false))
1839 {
1840 // we are in conflict
1841 sendInference(in.d_exp, conc, "CTN_Decompose");
1842 }
1843 else if (getExtTheory()->hasFunctionKind(conc.getKind()))
1844 {
1845 // can mark as reduced, since model for n implies model for conc
1846 getExtTheory()->markReduced(conc);
1847 }
1848 }
1849 }
1850 }
1851 }
1852 else
1853 {
1854 if (std::find(d_extf_info_tmp[nr[0]].d_ctn[pol].begin(),
1855 d_extf_info_tmp[nr[0]].d_ctn[pol].end(),
1856 nr[1])
1857 == d_extf_info_tmp[nr[0]].d_ctn[pol].end())
1858 {
1859 Trace("strings-extf-debug") << " store contains info : " << nr[0]
1860 << " " << pol << " " << nr[1] << std::endl;
1861 // Store s (does not) contains t, since nr = (~)contains( s, t ) holds.
1862 d_extf_info_tmp[nr[0]].d_ctn[pol].push_back(nr[1]);
1863 d_extf_info_tmp[nr[0]].d_ctn_from[pol].push_back(n);
1864 // Do transistive closure on contains, e.g.
1865 // if contains( s, t ) and ~contains( s, r ), then ~contains( t, r ).
1866
1867 // The following infers new (negative) contains based on the above
1868 // reasoning, provided that ~contains( t, r ) does not
1869 // already hold in the current context. We test this by checking that
1870 // contains( t, r ) is not already asserted false in the current
1871 // context. We also handle the case where contains( t, r ) is equivalent
1872 // to t = r, in which case we check that t != r does not already hold
1873 // in the current context.
1874
1875 // Notice that form of the above inference is enough to find
1876 // conflicts purely due to contains predicates. For example, if we
1877 // have only positive occurrences of contains, then no conflicts due to
1878 // contains predicates are possible and this schema does nothing. For
1879 // example, note that contains( s, t ) and contains( t, r ) implies
1880 // contains( s, r ), which we could but choose not to infer. Instead,
1881 // we prefer being lazy: only if ~contains( s, r ) appears later do we
1882 // infer ~contains( t, r ), which suffices to show a conflict.
1883 bool opol = !pol;
1884 for (unsigned i = 0, size = d_extf_info_tmp[nr[0]].d_ctn[opol].size();
1885 i < size;
1886 i++)
1887 {
1888 Node onr = d_extf_info_tmp[nr[0]].d_ctn[opol][i];
1889 Node conc =
1890 nm->mkNode(STRING_STRCTN, pol ? nr[1] : onr, pol ? onr : nr[1]);
1891 conc = Rewriter::rewrite(conc);
1892 conc = conc.negate();
1893 bool do_infer = false;
1894 bool pol = conc.getKind() != NOT;
1895 Node lit = pol ? conc : conc[0];
1896 if (lit.getKind() == EQUAL)
1897 {
1898 do_infer = pol ? !areEqual(lit[0], lit[1])
1899 : !areDisequal(lit[0], lit[1]);
1900 }
1901 else
1902 {
1903 do_infer = !areEqual(lit, pol ? d_true : d_false);
1904 }
1905 if (do_infer)
1906 {
1907 std::vector<Node> exp_c;
1908 exp_c.insert(exp_c.end(), in.d_exp.begin(), in.d_exp.end());
1909 Node ofrom = d_extf_info_tmp[nr[0]].d_ctn_from[opol][i];
1910 Assert(d_extf_info_tmp.find(ofrom) != d_extf_info_tmp.end());
1911 exp_c.insert(exp_c.end(),
1912 d_extf_info_tmp[ofrom].d_exp.begin(),
1913 d_extf_info_tmp[ofrom].d_exp.end());
1914 sendInference(exp_c, conc, "CTN_Trans");
1915 }
1916 }
1917 }
1918 else
1919 {
1920 // If we already know that s (does not) contain t, then n is redundant.
1921 // For example, if str.contains( x, y ), str.contains( z, y ), and x=z
1922 // are asserted in the current context, then str.contains( z, y ) is
1923 // satisfied by all models of str.contains( x, y ) ^ x=z and thus can
1924 // be ignored.
1925 Trace("strings-extf-debug") << " redundant." << std::endl;
1926 getExtTheory()->markReduced(n);
1927 }
1928 }
1929 return;
1930 }
1931
1932 // If it's not a predicate, see if we can solve the equality n = c, where c
1933 // is the constant that extended term n is equal to.
1934 Node inferEq = nr.eqNode(in.d_const);
1935 Node inferEqr = Rewriter::rewrite(inferEq);
1936 Node inferEqrr = inferEqr;
1937 if (inferEqr.getKind() == EQUAL)
1938 {
1939 // try to use the extended rewriter for equalities
1940 inferEqrr = TheoryStringsRewriter::rewriteEqualityExt(inferEqr);
1941 }
1942 if (inferEqrr != inferEqr)
1943 {
1944 inferEqrr = Rewriter::rewrite(inferEqrr);
1945 Trace("strings-extf-infer") << "checkExtfInference: " << inferEq
1946 << " ...reduces to " << inferEqrr << std::endl;
1947 sendInternalInference(in.d_exp, inferEqrr, "EXTF_equality_rew");
1948 }
1949 }
1950
1951 Node TheoryStrings::getSymbolicDefinition( Node n, std::vector< Node >& exp ) {
1952 if( n.getNumChildren()==0 ){
1953 NodeNodeMap::const_iterator it = d_proxy_var.find( n );
1954 if( it==d_proxy_var.end() ){
1955 return Node::null();
1956 }else{
1957 Node eq = n.eqNode( (*it).second );
1958 eq = Rewriter::rewrite( eq );
1959 if( std::find( exp.begin(), exp.end(), eq )==exp.end() ){
1960 exp.push_back( eq );
1961 }
1962 return (*it).second;
1963 }
1964 }else{
1965 std::vector< Node > children;
1966 if (n.getMetaKind() == kind::metakind::PARAMETERIZED) {
1967 children.push_back( n.getOperator() );
1968 }
1969 for( unsigned i=0; i<n.getNumChildren(); i++ ){
1970 if( n.getKind()==kind::STRING_IN_REGEXP && i==1 ){
1971 children.push_back( n[i] );
1972 }else{
1973 Node ns = getSymbolicDefinition( n[i], exp );
1974 if( ns.isNull() ){
1975 return Node::null();
1976 }else{
1977 children.push_back( ns );
1978 }
1979 }
1980 }
1981 return NodeManager::currentNM()->mkNode( n.getKind(), children );
1982 }
1983 }
1984
1985 Node TheoryStrings::getConstantEqc( Node eqc ) {
1986 std::map< Node, Node >::iterator it = d_eqc_to_const.find( eqc );
1987 if( it!=d_eqc_to_const.end() ){
1988 return it->second;
1989 }else{
1990 return Node::null();
1991 }
1992 }
1993
1994 void TheoryStrings::debugPrintFlatForms( const char * tc ){
1995 for( unsigned k=0; k<d_strings_eqc.size(); k++ ){
1996 Node eqc = d_strings_eqc[k];
1997 if( d_eqc[eqc].size()>1 ){
1998 Trace( tc ) << "EQC [" << eqc << "]" << std::endl;
1999 }else{
2000 Trace( tc ) << "eqc [" << eqc << "]";
2001 }
2002 std::map< Node, Node >::iterator itc = d_eqc_to_const.find( eqc );
2003 if( itc!=d_eqc_to_const.end() ){
2004 Trace( tc ) << " C: " << itc->second;
2005 if( d_eqc[eqc].size()>1 ){
2006 Trace( tc ) << std::endl;
2007 }
2008 }
2009 if( d_eqc[eqc].size()>1 ){
2010 for( unsigned i=0; i<d_eqc[eqc].size(); i++ ){
2011 Node n = d_eqc[eqc][i];
2012 Trace( tc ) << " ";
2013 for( unsigned j=0; j<d_flat_form[n].size(); j++ ){
2014 Node fc = d_flat_form[n][j];
2015 itc = d_eqc_to_const.find( fc );
2016 Trace( tc ) << " ";
2017 if( itc!=d_eqc_to_const.end() ){
2018 Trace( tc ) << itc->second;
2019 }else{
2020 Trace( tc ) << fc;
2021 }
2022 }
2023 if( n!=eqc ){
2024 Trace( tc ) << ", from " << n;
2025 }
2026 Trace( tc ) << std::endl;
2027 }
2028 }else{
2029 Trace( tc ) << std::endl;
2030 }
2031 }
2032 Trace( tc ) << std::endl;
2033 }
2034
2035 void TheoryStrings::debugPrintNormalForms( const char * tc ) {
2036 }
2037
2038 struct sortConstLength {
2039 std::map< Node, unsigned > d_const_length;
2040 bool operator() (Node i, Node j) {
2041 std::map< Node, unsigned >::iterator it_i = d_const_length.find( i );
2042 std::map< Node, unsigned >::iterator it_j = d_const_length.find( j );
2043 if( it_i==d_const_length.end() ){
2044 if( it_j==d_const_length.end() ){
2045 return i<j;
2046 }else{
2047 return false;
2048 }
2049 }else{
2050 if( it_j==d_const_length.end() ){
2051 return true;
2052 }else{
2053 return it_i->second<it_j->second;
2054 }
2055 }
2056 }
2057 };
2058
2059 void TheoryStrings::checkCycles()
2060 {
2061 // first check for cycles, while building ordering of equivalence classes
2062 d_flat_form.clear();
2063 d_flat_form_index.clear();
2064 d_eqc.clear();
2065 //rebuild strings eqc based on acyclic ordering
2066 std::vector< Node > eqc;
2067 eqc.insert( eqc.end(), d_strings_eqc.begin(), d_strings_eqc.end() );
2068 d_strings_eqc.clear();
2069 if( options::stringBinaryCsp() ){
2070 //sort: process smallest constants first (necessary if doing binary splits)
2071 sortConstLength scl;
2072 for( unsigned i=0; i<eqc.size(); i++ ){
2073 std::map< Node, Node >::iterator itc = d_eqc_to_const.find( eqc[i] );
2074 if( itc!=d_eqc_to_const.end() ){
2075 scl.d_const_length[eqc[i]] = itc->second.getConst<String>().size();
2076 }
2077 }
2078 std::sort( eqc.begin(), eqc.end(), scl );
2079 }
2080 for( unsigned i=0; i<eqc.size(); i++ ){
2081 std::vector< Node > curr;
2082 std::vector< Node > exp;
2083 checkCycles( eqc[i], curr, exp );
2084 if( hasProcessed() ){
2085 return;
2086 }
2087 }
2088 }
2089
2090 void TheoryStrings::checkFlatForms()
2091 {
2092 // debug print flat forms
2093 if (Trace.isOn("strings-ff"))
2094 {
2095 Trace("strings-ff") << "Flat forms : " << std::endl;
2096 debugPrintFlatForms("strings-ff");
2097 }
2098
2099 // inferences without recursively expanding flat forms
2100
2101 //(1) approximate equality by containment, infer conflicts
2102 for (const Node& eqc : d_strings_eqc)
2103 {
2104 Node c = getConstantEqc(eqc);
2105 if (!c.isNull())
2106 {
2107 // if equivalence class is constant, all component constants in flat forms
2108 // must be contained in it, in order
2109 std::map<Node, std::vector<Node> >::iterator it = d_eqc.find(eqc);
2110 if (it != d_eqc.end())
2111 {
2112 for (const Node& n : it->second)
2113 {
2114 int firstc, lastc;
2115 if (!TheoryStringsRewriter::canConstantContainList(
2116 c, d_flat_form[n], firstc, lastc))
2117 {
2118 Trace("strings-ff-debug") << "Flat form for " << n
2119 << " cannot be contained in constant "
2120 << c << std::endl;
2121 Trace("strings-ff-debug") << " indices = " << firstc << "/"
2122 << lastc << std::endl;
2123 // conflict, explanation is n = base ^ base = c ^ relevant portion
2124 // of ( n = f[n] )
2125 std::vector<Node> exp;
2126 Assert(d_eqc_to_const_base.find(eqc) != d_eqc_to_const_base.end());
2127 addToExplanation(n, d_eqc_to_const_base[eqc], exp);
2128 Assert(d_eqc_to_const_exp.find(eqc) != d_eqc_to_const_exp.end());
2129 if (!d_eqc_to_const_exp[eqc].isNull())
2130 {
2131 exp.push_back(d_eqc_to_const_exp[eqc]);
2132 }
2133 for (int e = firstc; e <= lastc; e++)
2134 {
2135 if (d_flat_form[n][e].isConst())
2136 {
2137 Assert(e >= 0 && e < (int)d_flat_form_index[n].size());
2138 Assert(d_flat_form_index[n][e] >= 0
2139 && d_flat_form_index[n][e] < (int)n.getNumChildren());
2140 addToExplanation(
2141 d_flat_form[n][e], n[d_flat_form_index[n][e]], exp);
2142 }
2143 }
2144 Node conc = d_false;
2145 sendInference(exp, conc, "F_NCTN");
2146 return;
2147 }
2148 }
2149 }
2150 }
2151 }
2152
2153 //(2) scan lists, unification to infer conflicts and equalities
2154 for (const Node& eqc : d_strings_eqc)
2155 {
2156 std::map<Node, std::vector<Node> >::iterator it = d_eqc.find(eqc);
2157 if (it == d_eqc.end() || it->second.size() <= 1)
2158 {
2159 continue;
2160 }
2161 // iterate over start index
2162 for (unsigned start = 0; start < it->second.size() - 1; start++)
2163 {
2164 for (unsigned r = 0; r < 2; r++)
2165 {
2166 bool isRev = r == 1;
2167 checkFlatForm(it->second, start, isRev);
2168 if (d_conflict)
2169 {
2170 return;
2171 }
2172 }
2173 }
2174 }
2175 }
2176
2177 void TheoryStrings::checkFlatForm(std::vector<Node>& eqc,
2178 unsigned start,
2179 bool isRev)
2180 {
2181 unsigned count = 0;
2182 std::vector<Node> inelig;
2183 for (unsigned i = 0; i <= start; i++)
2184 {
2185 inelig.push_back(eqc[start]);
2186 }
2187 Node a = eqc[start];
2188 Node b;
2189 do
2190 {
2191 std::vector<Node> exp;
2192 Node conc;
2193 int inf_type = -1;
2194 unsigned eqc_size = eqc.size();
2195 unsigned asize = d_flat_form[a].size();
2196 if (count == asize)
2197 {
2198 for (unsigned i = start + 1; i < eqc_size; i++)
2199 {
2200 b = eqc[i];
2201 if (std::find(inelig.begin(), inelig.end(), b) == inelig.end())
2202 {
2203 unsigned bsize = d_flat_form[b].size();
2204 if (count < bsize)
2205 {
2206 // endpoint
2207 std::vector<Node> conc_c;
2208 for (unsigned j = count; j < bsize; j++)
2209 {
2210 conc_c.push_back(
2211 b[d_flat_form_index[b][j]].eqNode(d_emptyString));
2212 }
2213 Assert(!conc_c.empty());
2214 conc = mkAnd(conc_c);
2215 inf_type = 2;
2216 Assert(count > 0);
2217 // swap, will enforce is empty past current
2218 a = eqc[i];
2219 b = eqc[start];
2220 count--;
2221 break;
2222 }
2223 inelig.push_back(eqc[i]);
2224 }
2225 }
2226 }
2227 else
2228 {
2229 Node curr = d_flat_form[a][count];
2230 Node curr_c = getConstantEqc(curr);
2231 Node ac = a[d_flat_form_index[a][count]];
2232 std::vector<Node> lexp;
2233 Node lcurr = getLength(ac, lexp);
2234 for (unsigned i = 1; i < eqc_size; i++)
2235 {
2236 b = eqc[i];
2237 if (std::find(inelig.begin(), inelig.end(), b) == inelig.end())
2238 {
2239 if (count == d_flat_form[b].size())
2240 {
2241 inelig.push_back(b);
2242 // endpoint
2243 std::vector<Node> conc_c;
2244 for (unsigned j = count; j < asize; j++)
2245 {
2246 conc_c.push_back(
2247 a[d_flat_form_index[a][j]].eqNode(d_emptyString));
2248 }
2249 Assert(!conc_c.empty());
2250 conc = mkAnd(conc_c);
2251 inf_type = 2;
2252 Assert(count > 0);
2253 count--;
2254 break;
2255 }
2256 else
2257 {
2258 Node cc = d_flat_form[b][count];
2259 if (cc != curr)
2260 {
2261 Node bc = b[d_flat_form_index[b][count]];
2262 inelig.push_back(b);
2263 Assert(!areEqual(curr, cc));
2264 Node cc_c = getConstantEqc(cc);
2265 if (!curr_c.isNull() && !cc_c.isNull())
2266 {
2267 // check for constant conflict
2268 int index;
2269 Node s = TheoryStringsRewriter::splitConstant(
2270 cc_c, curr_c, index, isRev);
2271 if (s.isNull())
2272 {
2273 addToExplanation(ac, d_eqc_to_const_base[curr], exp);
2274 addToExplanation(d_eqc_to_const_exp[curr], exp);
2275 addToExplanation(bc, d_eqc_to_const_base[cc], exp);
2276 addToExplanation(d_eqc_to_const_exp[cc], exp);
2277 conc = d_false;
2278 inf_type = 0;
2279 break;
2280 }
2281 }
2282 else if ((d_flat_form[a].size() - 1) == count
2283 && (d_flat_form[b].size() - 1) == count)
2284 {
2285 conc = ac.eqNode(bc);
2286 inf_type = 3;
2287 break;
2288 }
2289 else
2290 {
2291 // if lengths are the same, apply LengthEq
2292 std::vector<Node> lexp2;
2293 Node lcc = getLength(bc, lexp2);
2294 if (areEqual(lcurr, lcc))
2295 {
2296 Trace("strings-ff-debug") << "Infer " << ac << " == " << bc
2297 << " since " << lcurr
2298 << " == " << lcc << std::endl;
2299 // exp_n.push_back( getLength( curr, true ).eqNode(
2300 // getLength( cc, true ) ) );
2301 Trace("strings-ff-debug") << "Explanation for " << lcurr
2302 << " is ";
2303 for (unsigned j = 0; j < lexp.size(); j++)
2304 {
2305 Trace("strings-ff-debug") << lexp[j] << std::endl;
2306 }
2307 Trace("strings-ff-debug") << "Explanation for " << lcc
2308 << " is ";
2309 for (unsigned j = 0; j < lexp2.size(); j++)
2310 {
2311 Trace("strings-ff-debug") << lexp2[j] << std::endl;
2312 }
2313 exp.insert(exp.end(), lexp.begin(), lexp.end());
2314 exp.insert(exp.end(), lexp2.begin(), lexp2.end());
2315 addToExplanation(lcurr, lcc, exp);
2316 conc = ac.eqNode(bc);
2317 inf_type = 1;
2318 break;
2319 }
2320 }
2321 }
2322 }
2323 }
2324 }
2325 }
2326 if (!conc.isNull())
2327 {
2328 Trace("strings-ff-debug")
2329 << "Found inference : " << conc << " based on equality " << a
2330 << " == " << b << ", " << isRev << " " << inf_type << std::endl;
2331 addToExplanation(a, b, exp);
2332 // explain why prefixes up to now were the same
2333 for (unsigned j = 0; j < count; j++)
2334 {
2335 Trace("strings-ff-debug") << "Add at " << d_flat_form_index[a][j] << " "
2336 << d_flat_form_index[b][j] << std::endl;
2337 addToExplanation(
2338 a[d_flat_form_index[a][j]], b[d_flat_form_index[b][j]], exp);
2339 }
2340 // explain why other components up to now are empty
2341 for (unsigned t = 0; t < 2; t++)
2342 {
2343 Node c = t == 0 ? a : b;
2344 int jj;
2345 if (inf_type == 3 || (t == 1 && inf_type == 2))
2346 {
2347 // explain all the empty components for F_EndpointEq, all for
2348 // the short end for F_EndpointEmp
2349 jj = isRev ? -1 : c.getNumChildren();
2350 }
2351 else
2352 {
2353 jj = t == 0 ? d_flat_form_index[a][count]
2354 : d_flat_form_index[b][count];
2355 }
2356 int startj = isRev ? jj + 1 : 0;
2357 int endj = isRev ? c.getNumChildren() : jj;
2358 for (int j = startj; j < endj; j++)
2359 {
2360 if (areEqual(c[j], d_emptyString))
2361 {
2362 addToExplanation(c[j], d_emptyString, exp);
2363 }
2364 }
2365 }
2366 // notice that F_EndpointEmp is not typically applied, since
2367 // strict prefix equality ( a.b = a ) where a,b non-empty
2368 // is conflicting by arithmetic len(a.b)=len(a)+len(b)!=len(a)
2369 // when len(b)!=0.
2370 sendInference(
2371 exp,
2372 conc,
2373 inf_type == 0
2374 ? "F_Const"
2375 : (inf_type == 1 ? "F_Unify" : (inf_type == 2 ? "F_EndpointEmp"
2376 : "F_EndpointEq")));
2377 if (d_conflict)
2378 {
2379 return;
2380 }
2381 break;
2382 }
2383 count++;
2384 } while (inelig.size() < eqc.size());
2385
2386 for (const Node& n : eqc)
2387 {
2388 std::reverse(d_flat_form[n].begin(), d_flat_form[n].end());
2389 std::reverse(d_flat_form_index[n].begin(), d_flat_form_index[n].end());
2390 }
2391 }
2392
2393 Node TheoryStrings::checkCycles( Node eqc, std::vector< Node >& curr, std::vector< Node >& exp ){
2394 if( std::find( curr.begin(), curr.end(), eqc )!=curr.end() ){
2395 // a loop
2396 return eqc;
2397 }else if( std::find( d_strings_eqc.begin(), d_strings_eqc.end(), eqc )==d_strings_eqc.end() ){
2398 curr.push_back( eqc );
2399 //look at all terms in this equivalence class
2400 eq::EqClassIterator eqc_i = eq::EqClassIterator( eqc, &d_equalityEngine );
2401 while( !eqc_i.isFinished() ) {
2402 Node n = (*eqc_i);
2403 if( d_congruent.find( n )==d_congruent.end() ){
2404 if( n.getKind() == kind::STRING_CONCAT ){
2405 Trace("strings-cycle") << eqc << " check term : " << n << " in " << eqc << std::endl;
2406 if( eqc!=d_emptyString_r ){
2407 d_eqc[eqc].push_back( n );
2408 }
2409 for( unsigned i=0; i<n.getNumChildren(); i++ ){
2410 Node nr = getRepresentative( n[i] );
2411 if( eqc==d_emptyString_r ){
2412 //for empty eqc, ensure all components are empty
2413 if( nr!=d_emptyString_r ){
2414 std::vector< Node > exp;
2415 exp.push_back( n.eqNode( d_emptyString ) );
2416 sendInference( exp, n[i].eqNode( d_emptyString ), "I_CYCLE_E" );
2417 return Node::null();
2418 }
2419 }else{
2420 if( nr!=d_emptyString_r ){
2421 d_flat_form[n].push_back( nr );
2422 d_flat_form_index[n].push_back( i );
2423 }
2424 //for non-empty eqc, recurse and see if we find a loop
2425 Node ncy = checkCycles( nr, curr, exp );
2426 if( !ncy.isNull() ){
2427 Trace("strings-cycle") << eqc << " cycle: " << ncy << " at " << n << "[" << i << "] : " << n[i] << std::endl;
2428 addToExplanation( n, eqc, exp );
2429 addToExplanation( nr, n[i], exp );
2430 if( ncy==eqc ){
2431 //can infer all other components must be empty
2432 for( unsigned j=0; j<n.getNumChildren(); j++ ){
2433 //take first non-empty
2434 if( j!=i && !areEqual( n[j], d_emptyString ) ){
2435 sendInference( exp, n[j].eqNode( d_emptyString ), "I_CYCLE" );
2436 return Node::null();
2437 }
2438 }
2439 Trace("strings-error") << "Looping term should be congruent : " << n << " " << eqc << " " << ncy << std::endl;
2440 //should find a non-empty component, otherwise would have been singular congruent (I_Norm_S)
2441 Assert( false );
2442 }else{
2443 return ncy;
2444 }
2445 }else{
2446 if( hasProcessed() ){
2447 return Node::null();
2448 }
2449 }
2450 }
2451 }
2452 }
2453 }
2454 ++eqc_i;
2455 }
2456 curr.pop_back();
2457 //now we can add it to the list of equivalence classes
2458 d_strings_eqc.push_back( eqc );
2459 }else{
2460 //already processed
2461 }
2462 return Node::null();
2463 }
2464
2465 void TheoryStrings::checkNormalFormsEq()
2466 {
2467 if( !options::stringEagerLen() ){
2468 for( unsigned i=0; i<d_strings_eqc.size(); i++ ) {
2469 Node eqc = d_strings_eqc[i];
2470 eq::EqClassIterator eqc_i = eq::EqClassIterator( eqc, &d_equalityEngine );
2471 while( !eqc_i.isFinished() ) {
2472 Node n = (*eqc_i);
2473 if( d_congruent.find( n )==d_congruent.end() ){
2474 registerTerm( n, 2 );
2475 }
2476 ++eqc_i;
2477 }
2478 }
2479 }
2480
2481 if (hasProcessed())
2482 {
2483 return;
2484 }
2485 // calculate normal forms for each equivalence class, possibly adding
2486 // splitting lemmas
2487 d_normal_forms.clear();
2488 d_normal_forms_exp.clear();
2489 std::map<Node, Node> nf_to_eqc;
2490 std::map<Node, Node> eqc_to_nf;
2491 std::map<Node, Node> eqc_to_exp;
2492 for (const Node& eqc : d_strings_eqc)
2493 {
2494 Trace("strings-process-debug") << "- Verify normal forms are the same for "
2495 << eqc << std::endl;
2496 normalizeEquivalenceClass(eqc);
2497 Trace("strings-debug") << "Finished normalizing eqc..." << std::endl;
2498 if (hasProcessed())
2499 {
2500 return;
2501 }
2502 Node nf_term = mkConcat(d_normal_forms[eqc]);
2503 std::map<Node, Node>::iterator itn = nf_to_eqc.find(nf_term);
2504 if (itn != nf_to_eqc.end())
2505 {
2506 // two equivalence classes have same normal form, merge
2507 std::vector<Node> nf_exp;
2508 nf_exp.push_back(mkAnd(d_normal_forms_exp[eqc]));
2509 nf_exp.push_back(eqc_to_exp[itn->second]);
2510 Node eq =
2511 d_normal_forms_base[eqc].eqNode(d_normal_forms_base[itn->second]);
2512 sendInference(nf_exp, eq, "Normal_Form");
2513 if( hasProcessed() ){
2514 return;
2515 }
2516 }
2517 else
2518 {
2519 nf_to_eqc[nf_term] = eqc;
2520 eqc_to_nf[eqc] = nf_term;
2521 eqc_to_exp[eqc] = mkAnd(d_normal_forms_exp[eqc]);
2522 }
2523 Trace("strings-process-debug")
2524 << "Done verifying normal forms are the same for " << eqc << std::endl;
2525 }
2526 if (Trace.isOn("strings-nf"))
2527 {
2528 Trace("strings-nf") << "**** Normal forms are : " << std::endl;
2529 for (std::map<Node, Node>::iterator it = eqc_to_exp.begin();
2530 it != eqc_to_exp.end();
2531 ++it)
2532 {
2533 Trace("strings-nf") << " N[" << it->first << "] (base "
2534 << d_normal_forms_base[it->first]
2535 << ") = " << eqc_to_nf[it->first] << std::endl;
2536 Trace("strings-nf") << " exp: " << it->second << std::endl;
2537 }
2538 Trace("strings-nf") << std::endl;
2539 }
2540 }
2541
2542 void TheoryStrings::checkCodes()
2543 {
2544 // ensure that lemmas regarding str.code been added for each constant string
2545 // of length one
2546 if (d_has_str_code)
2547 {
2548 NodeManager* nm = NodeManager::currentNM();
2549 // str.code applied to the code term for each equivalence class that has a
2550 // code term but is not a constant
2551 std::vector<Node> nconst_codes;
2552 // str.code applied to the proxy variables for each equivalence classes that
2553 // are constants of size one
2554 std::vector<Node> const_codes;
2555 for (const Node& eqc : d_strings_eqc)
2556 {
2557 if (d_normal_forms[eqc].size() == 1 && d_normal_forms[eqc][0].isConst())
2558 {
2559 Node c = d_normal_forms[eqc][0];
2560 Trace("strings-code-debug") << "Get proxy variable for " << c
2561 << std::endl;
2562 Node cc = nm->mkNode(kind::STRING_CODE, c);
2563 cc = Rewriter::rewrite(cc);
2564 Assert(cc.isConst());
2565 NodeNodeMap::const_iterator it = d_proxy_var.find(c);
2566 AlwaysAssert(it != d_proxy_var.end());
2567 Node vc = nm->mkNode(kind::STRING_CODE, (*it).second);
2568 if (!areEqual(cc, vc))
2569 {
2570 sendInference(d_empty_vec, cc.eqNode(vc), "Code_Proxy");
2571 }
2572 const_codes.push_back(vc);
2573 }
2574 else
2575 {
2576 EqcInfo* ei = getOrMakeEqcInfo(eqc, false);
2577 if (ei && !ei->d_code_term.get().isNull())
2578 {
2579 Node vc = nm->mkNode(kind::STRING_CODE, ei->d_code_term.get());
2580 nconst_codes.push_back(vc);
2581 }
2582 }
2583 }
2584 if (hasProcessed())
2585 {
2586 return;
2587 }
2588 // now, ensure that str.code is injective
2589 std::vector<Node> cmps;
2590 cmps.insert(cmps.end(), const_codes.rbegin(), const_codes.rend());
2591 cmps.insert(cmps.end(), nconst_codes.rbegin(), nconst_codes.rend());
2592 for (unsigned i = 0, num_ncc = nconst_codes.size(); i < num_ncc; i++)
2593 {
2594 Node c1 = nconst_codes[i];
2595 cmps.pop_back();
2596 for (const Node& c2 : cmps)
2597 {
2598 Trace("strings-code-debug")
2599 << "Compare codes : " << c1 << " " << c2 << std::endl;
2600 if (!areDisequal(c1, c2) && !areEqual(c1, d_neg_one))
2601 {
2602 Node eq_no = c1.eqNode(d_neg_one);
2603 Node deq = c1.eqNode(c2).negate();
2604 Node eqn = c1[0].eqNode(c2[0]);
2605 // str.code(x)==-1 V str.code(x)!=str.code(y) V x==y
2606 Node inj_lem = nm->mkNode(kind::OR, eq_no, deq, eqn);
2607 sendInference(d_empty_vec, inj_lem, "Code_Inj");
2608 }
2609 }
2610 }
2611 }
2612 }
2613
2614 //compute d_normal_forms_(base,exp,exp_depend)[eqc]
2615 void TheoryStrings::normalizeEquivalenceClass( Node eqc ) {
2616 Trace("strings-process-debug") << "Process equivalence class " << eqc << std::endl;
2617 if( areEqual( eqc, d_emptyString ) ) {
2618 #ifdef CVC4_ASSERTIONS
2619 for( unsigned j=0; j<d_eqc[eqc].size(); j++ ){
2620 Node n = d_eqc[eqc][j];
2621 for( unsigned i=0; i<n.getNumChildren(); i++ ){
2622 Assert( areEqual( n[i], d_emptyString ) );
2623 }
2624 }
2625 #endif
2626 //do nothing
2627 Trace("strings-process-debug") << "Return process equivalence class " << eqc << " : empty." << std::endl;
2628 d_normal_forms_base[eqc] = d_emptyString;
2629 d_normal_forms[eqc].clear();
2630 d_normal_forms_exp[eqc].clear();
2631 } else {
2632 Assert( d_normal_forms.find(eqc)==d_normal_forms.end() );
2633 //phi => t = s1 * ... * sn
2634 // normal form for each non-variable term in this eqc (s1...sn)
2635 std::vector< std::vector< Node > > normal_forms;
2636 // explanation for each normal form (phi)
2637 std::vector< std::vector< Node > > normal_forms_exp;
2638 // dependency information
2639 std::vector< std::map< Node, std::map< bool, int > > > normal_forms_exp_depend;
2640 // record terms for each normal form (t)
2641 std::vector< Node > normal_form_src;
2642 // get normal forms
2643 getNormalForms(eqc, normal_forms, normal_form_src, normal_forms_exp, normal_forms_exp_depend);
2644 if( hasProcessed() ){
2645 return;
2646 }
2647 // process the normal forms
2648 processNEqc( normal_forms, normal_form_src, normal_forms_exp, normal_forms_exp_depend );
2649 if( hasProcessed() ){
2650 return;
2651 }
2652 //debugPrintNormalForms( "strings-solve", eqc, normal_forms, normal_form_src, normal_forms_exp, normal_forms_exp_depend );
2653
2654 //construct the normal form
2655 Assert( !normal_forms.empty() );
2656
2657 int nf_index = 0;
2658 std::vector< Node >::iterator itn = std::find( normal_form_src.begin(), normal_form_src.end(), eqc );
2659 if( itn!=normal_form_src.end() ){
2660 nf_index = itn - normal_form_src.begin();
2661 Trace("strings-solve-debug2") << "take normal form " << nf_index << std::endl;
2662 Assert( normal_form_src[nf_index]==eqc );
2663 }else{
2664 //just take the first normal form
2665 Trace("strings-solve-debug2") << "take the first normal form" << std::endl;
2666 }
2667 d_normal_forms[eqc].insert( d_normal_forms[eqc].end(), normal_forms[nf_index].begin(), normal_forms[nf_index].end() );
2668 d_normal_forms_exp[eqc].insert( d_normal_forms_exp[eqc].end(), normal_forms_exp[nf_index].begin(), normal_forms_exp[nf_index].end() );
2669 Trace("strings-solve-debug2") << "take normal form ... done" << std::endl;
2670 d_normal_forms_base[eqc] = normal_form_src[nf_index];
2671 //track dependencies
2672 for( unsigned i=0; i<normal_forms_exp[nf_index].size(); i++ ){
2673 Node exp = normal_forms_exp[nf_index][i];
2674 for( unsigned r=0; r<2; r++ ){
2675 d_normal_forms_exp_depend[eqc][exp][r==0] = normal_forms_exp_depend[nf_index][exp][r==0];
2676 }
2677 }
2678 Trace("strings-process-debug") << "Return process equivalence class " << eqc << " : returned, size = " << d_normal_forms[eqc].size() << std::endl;
2679 }
2680 }
2681
2682 void trackNfExpDependency( std::vector< Node >& nf_exp_n, std::map< Node, std::map< bool, int > >& nf_exp_depend_n, Node exp, int new_val, int new_rev_val ){
2683 if( std::find( nf_exp_n.begin(), nf_exp_n.end(), exp )==nf_exp_n.end() ){
2684 nf_exp_n.push_back( exp );
2685 }
2686 for( unsigned k=0; k<2; k++ ){
2687 int val = k==0 ? new_val : new_rev_val;
2688 std::map< bool, int >::iterator itned = nf_exp_depend_n[exp].find( k==1 );
2689 if( itned==nf_exp_depend_n[exp].end() ){
2690 Trace("strings-process-debug") << "Deps : set dependency on " << exp << " to " << val << " isRev=" << (k==0) << std::endl;
2691 nf_exp_depend_n[exp][k==1] = val;
2692 }else{
2693 Trace("strings-process-debug") << "Deps : Multiple dependencies on " << exp << " : " << itned->second << " " << val << " isRev=" << (k==0) << std::endl;
2694 //if we already have a dependency (in the case of non-linear string equalities), it is min/max
2695 bool cmp = val > itned->second;
2696 if( cmp==(k==1) ){
2697 nf_exp_depend_n[exp][k==1] = val;
2698 }
2699 }
2700 }
2701 }
2702
2703 void TheoryStrings::getNormalForms( Node &eqc, std::vector< std::vector< Node > > &normal_forms, std::vector< Node > &normal_form_src,
2704 std::vector< std::vector< Node > > &normal_forms_exp, std::vector< std::map< Node, std::map< bool, int > > >& normal_forms_exp_depend ) {
2705 //constant for equivalence class
2706 Node eqc_non_c = eqc;
2707 Trace("strings-process-debug") << "Get normal forms " << eqc << std::endl;
2708 eq::EqClassIterator eqc_i = eq::EqClassIterator( eqc, &d_equalityEngine );
2709 while( !eqc_i.isFinished() ){
2710 Node n = (*eqc_i);
2711 if( d_congruent.find( n )==d_congruent.end() ){
2712 if( n.getKind() == kind::CONST_STRING || n.getKind() == kind::STRING_CONCAT ){
2713 Trace("strings-process-debug") << "Get Normal Form : Process term " << n << " in eqc " << eqc << std::endl;
2714 std::vector< Node > nf_n;
2715 std::vector< Node > nf_exp_n;
2716 std::map< Node, std::map< bool, int > > nf_exp_depend_n;
2717 if( n.getKind()==kind::CONST_STRING ){
2718 if( n!=d_emptyString ) {
2719 nf_n.push_back( n );
2720 }
2721 }else if( n.getKind()==kind::STRING_CONCAT ){
2722 for( unsigned i=0; i<n.getNumChildren(); i++ ) {
2723 Node nr = d_equalityEngine.getRepresentative( n[i] );
2724 Trace("strings-process-debug") << "Normalizing subterm " << n[i] << " = " << nr << std::endl;
2725 Assert( d_normal_forms.find( nr )!=d_normal_forms.end() );
2726 unsigned orig_size = nf_n.size();
2727 unsigned add_size = d_normal_forms[nr].size();
2728 //if not the empty string, add to current normal form
2729 if( !d_normal_forms[nr].empty() ){
2730 for( unsigned r=0; r<d_normal_forms[nr].size(); r++ ) {
2731 if( Trace.isOn("strings-error") ) {
2732 if( d_normal_forms[nr][r].getKind()==kind::STRING_CONCAT ){
2733 Trace("strings-error") << "Strings::Error: From eqc = " << eqc << ", " << n << " index " << i << ", bad normal form : ";
2734 for( unsigned rr=0; rr<d_normal_forms[nr].size(); rr++ ) {
2735 Trace("strings-error") << d_normal_forms[nr][rr] << " ";
2736 }
2737 Trace("strings-error") << std::endl;
2738 }
2739 }
2740 Assert( d_normal_forms[nr][r].getKind()!=kind::STRING_CONCAT );
2741 }
2742 nf_n.insert( nf_n.end(), d_normal_forms[nr].begin(), d_normal_forms[nr].end() );
2743 }
2744
2745 for( unsigned j=0; j<d_normal_forms_exp[nr].size(); j++ ){
2746 Node exp = d_normal_forms_exp[nr][j];
2747 //track depends
2748 trackNfExpDependency( nf_exp_n, nf_exp_depend_n, exp,
2749 orig_size + d_normal_forms_exp_depend[nr][exp][false],
2750 orig_size + ( add_size - d_normal_forms_exp_depend[nr][exp][true] ) );
2751 }
2752 if( d_normal_forms_base[nr]!=n[i] ){
2753 Assert( d_normal_forms_base.find( nr )!=d_normal_forms_base.end() );
2754 Node eq = n[i].eqNode( d_normal_forms_base[nr] );
2755 //track depends : entire current segment is dependent upon base equality
2756 trackNfExpDependency( nf_exp_n, nf_exp_depend_n, eq, orig_size, orig_size + add_size );
2757 }
2758 }
2759 //convert forward indices to reverse indices
2760 int total_size = nf_n.size();
2761 for( std::map< Node, std::map< bool, int > >::iterator it = nf_exp_depend_n.begin(); it != nf_exp_depend_n.end(); ++it ){
2762 it->second[true] = total_size - it->second[true];
2763 Assert( it->second[true]>=0 );
2764 }
2765 }
2766 //if not equal to self
2767 if( nf_n.size()>1 || ( nf_n.size()==1 && nf_n[0].getKind()==kind::CONST_STRING ) ){
2768 if( nf_n.size()>1 ) {
2769 for( unsigned i=0; i<nf_n.size(); i++ ){
2770 if( Trace.isOn("strings-error") ){
2771 Trace("strings-error") << "Cycle for normal form ";
2772 printConcat(nf_n,"strings-error");
2773 Trace("strings-error") << "..." << nf_n[i] << std::endl;
2774 }
2775 Assert( !areEqual( nf_n[i], n ) );
2776 }
2777 }
2778 normal_forms.push_back(nf_n);
2779 normal_form_src.push_back(n);
2780 normal_forms_exp.push_back(nf_exp_n);
2781 normal_forms_exp_depend.push_back(nf_exp_depend_n);
2782 }else{
2783 //this was redundant: combination of self + empty string(s)
2784 Node nn = nf_n.size()==0 ? d_emptyString : nf_n[0];
2785 Assert( areEqual( nn, eqc ) );
2786 }
2787 }else{
2788 eqc_non_c = n;
2789 }
2790 }
2791 ++eqc_i;
2792 }
2793
2794 if( normal_forms.empty() ) {
2795 Trace("strings-solve-debug2") << "construct the normal form" << std::endl;
2796 //do not choose a concat here use "eqc_non_c" (in this case they have non-trivial explanation why they normalize to self)
2797 std::vector< Node > eqc_non_c_nf;
2798 getConcatVec( eqc_non_c, eqc_non_c_nf );
2799 normal_forms.push_back( eqc_non_c_nf );
2800 normal_form_src.push_back( eqc_non_c );
2801 normal_forms_exp.push_back( std::vector< Node >() );
2802 normal_forms_exp_depend.push_back( std::map< Node, std::map< bool, int > >() );
2803 }else{
2804 if(Trace.isOn("strings-solve")) {
2805 Trace("strings-solve") << "--- Normal forms for equivalance class " << eqc << " : " << std::endl;
2806 for( unsigned i=0; i<normal_forms.size(); i++ ) {
2807 Trace("strings-solve") << "#" << i << " (from " << normal_form_src[i] << ") : ";
2808 for( unsigned j=0; j<normal_forms[i].size(); j++ ) {
2809 if(j>0) {
2810 Trace("strings-solve") << ", ";
2811 }
2812 Trace("strings-solve") << normal_forms[i][j];
2813 }
2814 Trace("strings-solve") << std::endl;
2815 Trace("strings-solve") << " Explanation is : ";
2816 if(normal_forms_exp[i].size() == 0) {
2817 Trace("strings-solve") << "NONE";
2818 } else {
2819 for( unsigned j=0; j<normal_forms_exp[i].size(); j++ ) {
2820 if(j>0) {
2821 Trace("strings-solve") << " AND ";
2822 }
2823 Trace("strings-solve") << normal_forms_exp[i][j];
2824 }
2825 Trace("strings-solve") << std::endl;
2826 Trace("strings-solve") << "WITH DEPENDENCIES : " << std::endl;
2827 for( unsigned j=0; j<normal_forms_exp[i].size(); j++ ) {
2828 Trace("strings-solve") << " " << normal_forms_exp[i][j] << " -> ";
2829 Trace("strings-solve") << normal_forms_exp_depend[i][normal_forms_exp[i][j]][false] << ",";
2830 Trace("strings-solve") << normal_forms_exp_depend[i][normal_forms_exp[i][j]][true] << std::endl;
2831 }
2832 }
2833 Trace("strings-solve") << std::endl;
2834
2835 }
2836 } else {
2837 Trace("strings-solve") << "--- Single normal form for equivalence class " << eqc << std::endl;
2838 }
2839
2840 //if equivalence class is constant, approximate as containment, infer conflicts
2841 Node c = getConstantEqc( eqc );
2842 if( !c.isNull() ){
2843 Trace("strings-solve") << "Eqc is constant " << c << std::endl;
2844 for( unsigned i=0; i<normal_forms.size(); i++ ) {
2845 int firstc, lastc;
2846 if( !TheoryStringsRewriter::canConstantContainList( c, normal_forms[i], firstc, lastc ) ){
2847 Node n = normal_form_src[i];
2848 //conflict
2849 Trace("strings-solve") << "Normal form for " << n << " cannot be contained in constant " << c << std::endl;
2850 //conflict, explanation is n = base ^ base = c ^ relevant porition of ( n = N[n] )
2851 std::vector< Node > exp;
2852 Assert( d_eqc_to_const_base.find( eqc )!=d_eqc_to_const_base.end() );
2853 addToExplanation( n, d_eqc_to_const_base[eqc], exp );
2854 Assert( d_eqc_to_const_exp.find( eqc )!=d_eqc_to_const_exp.end() );
2855 if( !d_eqc_to_const_exp[eqc].isNull() ){
2856 exp.push_back( d_eqc_to_const_exp[eqc] );
2857 }
2858 //TODO: this can be minimized based on firstc/lastc, normal_forms_exp_depend
2859 exp.insert( exp.end(), normal_forms_exp[i].begin(), normal_forms_exp[i].end() );
2860 Node conc = d_false;
2861 sendInference( exp, conc, "N_NCTN" );
2862 }
2863 }
2864 }
2865 }
2866 }
2867
2868 void TheoryStrings::getExplanationVectorForPrefix( std::vector< std::vector< Node > > &normal_forms_exp, std::vector< std::map< Node, std::map< bool, int > > >& normal_forms_exp_depend,
2869 unsigned i, int index, bool isRev, std::vector< Node >& curr_exp ) {
2870 if( index==-1 || !options::stringMinPrefixExplain() ){
2871 curr_exp.insert(curr_exp.end(), normal_forms_exp[i].begin(), normal_forms_exp[i].end() );
2872 }else{
2873 for( unsigned k=0; k<normal_forms_exp[i].size(); k++ ){
2874 Node exp = normal_forms_exp[i][k];
2875 int dep = normal_forms_exp_depend[i][exp][isRev];
2876 if( dep<=index ){
2877 curr_exp.push_back( exp );
2878 Trace("strings-explain-prefix-debug") << " include : " << exp << std::endl;
2879 }else{
2880 Trace("strings-explain-prefix-debug") << " exclude : " << exp << std::endl;
2881 }
2882 }
2883 }
2884 }
2885
2886 void TheoryStrings::getExplanationVectorForPrefixEq( std::vector< std::vector< Node > > &normal_forms, std::vector< Node > &normal_form_src,
2887 std::vector< std::vector< Node > > &normal_forms_exp, std::vector< std::map< Node, std::map< bool, int > > >& normal_forms_exp_depend,
2888 unsigned i, unsigned j, int index_i, int index_j, bool isRev, std::vector< Node >& curr_exp ) {
2889 Trace("strings-explain-prefix") << "Get explanation for prefix " << index_i << ", " << index_j << " of normal forms " << i << " and " << j << ", reverse = " << isRev << std::endl;
2890 for( unsigned r=0; r<2; r++ ){
2891 getExplanationVectorForPrefix( normal_forms_exp, normal_forms_exp_depend, r==0 ? i : j, r==0 ? index_i : index_j, isRev, curr_exp );
2892 }
2893 Trace("strings-explain-prefix") << "Included " << curr_exp.size() << " / " << ( normal_forms_exp[i].size() + normal_forms_exp[j].size() ) << std::endl;
2894 addToExplanation( normal_form_src[i], normal_form_src[j], curr_exp );
2895 }
2896
2897
2898 void TheoryStrings::processNEqc( std::vector< std::vector< Node > > &normal_forms, std::vector< Node > &normal_form_src,
2899 std::vector< std::vector< Node > > &normal_forms_exp, std::vector< std::map< Node, std::map< bool, int > > >& normal_forms_exp_depend ){
2900 //the possible inferences
2901 std::vector< InferInfo > pinfer;
2902 // loop over all pairs
2903 for(unsigned i=0; i<normal_forms.size()-1; i++) {
2904 //unify each normalform[j] with normal_forms[i]
2905 for(unsigned j=i+1; j<normal_forms.size(); j++ ) {
2906 //ensure that normal_forms[i] and normal_forms[j] are the same modulo equality, add to pinfer if not
2907 Trace("strings-solve") << "Strings: Process normal form #" << i << " against #" << j << "..." << std::endl;
2908 if( isNormalFormPair( normal_form_src[i], normal_form_src[j] ) ) {
2909 Trace("strings-solve") << "Strings: Already cached." << std::endl;
2910 }else{
2911 //process the reverse direction first (check for easy conflicts and inferences)
2912 unsigned rindex = 0;
2913 processReverseNEq( normal_forms, normal_form_src, normal_forms_exp, normal_forms_exp_depend, i, j, rindex, 0, pinfer );
2914 if( hasProcessed() ){
2915 return;
2916 }else if( !pinfer.empty() && pinfer.back().d_id==1 ){
2917 break;
2918 }
2919 //AJR: for less aggressive endpoint inference
2920 //rindex = 0;
2921
2922 unsigned index = 0;
2923 processSimpleNEq( normal_forms, normal_form_src, normal_forms_exp, normal_forms_exp_depend, i, j, index, false, rindex, pinfer );
2924 if( hasProcessed() ){
2925 return;
2926 }else if( !pinfer.empty() && pinfer.back().d_id==1 ){
2927 break;
2928 }
2929 }
2930 }
2931 }
2932 if (pinfer.empty())
2933 {
2934 return;
2935 }
2936 // now, determine which of the possible inferences we want to add
2937 unsigned use_index = 0;
2938 bool set_use_index = false;
2939 Trace("strings-solve") << "Possible inferences (" << pinfer.size()
2940 << ") : " << std::endl;
2941 unsigned min_id = 9;
2942 unsigned max_index = 0;
2943 for (unsigned i = 0, size = pinfer.size(); i < size; i++)
2944 {
2945 Trace("strings-solve") << "From " << pinfer[i].d_i << " / " << pinfer[i].d_j
2946 << " (rev=" << pinfer[i].d_rev << ") : ";
2947 Trace("strings-solve") << pinfer[i].d_conc << " by " << pinfer[i].d_id
2948 << std::endl;
2949 if (!set_use_index || pinfer[i].d_id < min_id
2950 || (pinfer[i].d_id == min_id && pinfer[i].d_index > max_index))
2951 {
2952 min_id = pinfer[i].d_id;
2953 max_index = pinfer[i].d_index;
2954 use_index = i;
2955 set_use_index = true;
2956 }
2957 }
2958 // send the inference
2959 if (!pinfer[use_index].d_nf_pair[0].isNull())
2960 {
2961 Assert(!pinfer[use_index].d_nf_pair[1].isNull());
2962 addNormalFormPair(pinfer[use_index].d_nf_pair[0],
2963 pinfer[use_index].d_nf_pair[1]);
2964 }
2965 std::stringstream ssi;
2966 ssi << pinfer[use_index].d_id;
2967 sendInference(pinfer[use_index].d_ant,
2968 pinfer[use_index].d_antn,
2969 pinfer[use_index].d_conc,
2970 ssi.str().c_str(),
2971 pinfer[use_index].sendAsLemma());
2972 // Register the new skolems from this inference. We register them here
2973 // (lazily), since the code above has now decided to use the inference
2974 // at use_index that involves them.
2975 for (const std::pair<const LengthStatus, std::vector<Node> >& sks :
2976 pinfer[use_index].d_new_skolem)
2977 {
2978 for (const Node& n : sks.second)
2979 {
2980 registerLength(n, sks.first);
2981 }
2982 }
2983 }
2984
2985 bool TheoryStrings::InferInfo::sendAsLemma() {
2986 return true;
2987 }
2988
2989 void TheoryStrings::processReverseNEq( std::vector< std::vector< Node > > &normal_forms, std::vector< Node > &normal_form_src,
2990 std::vector< std::vector< Node > > &normal_forms_exp, std::vector< std::map< Node, std::map< bool, int > > >& normal_forms_exp_depend,
2991 unsigned i, unsigned j, unsigned& index, unsigned rproc, std::vector< InferInfo >& pinfer ) {
2992 //reverse normal form of i, j
2993 std::reverse( normal_forms[i].begin(), normal_forms[i].end() );
2994 std::reverse( normal_forms[j].begin(), normal_forms[j].end() );
2995
2996 processSimpleNEq( normal_forms, normal_form_src, normal_forms_exp, normal_forms_exp_depend, i, j, index, true, rproc, pinfer );
2997
2998 //reverse normal form of i, j
2999 std::reverse( normal_forms[i].begin(), normal_forms[i].end() );
3000 std::reverse( normal_forms[j].begin(), normal_forms[j].end() );
3001 }
3002
3003 //rproc is the # is the size of suffix that is identical
3004 void TheoryStrings::processSimpleNEq( std::vector< std::vector< Node > > &normal_forms, std::vector< Node > &normal_form_src,
3005 std::vector< std::vector< Node > > &normal_forms_exp, std::vector< std::map< Node, std::map< bool, int > > >& normal_forms_exp_depend,
3006 unsigned i, unsigned j, unsigned& index, bool isRev, unsigned rproc, std::vector< InferInfo >& pinfer ) {
3007 Assert( rproc<=normal_forms[i].size() && rproc<=normal_forms[j].size() );
3008 bool success;
3009 do {
3010 success = false;
3011 //if we are at the end
3012 if( index==(normal_forms[i].size()-rproc) || index==(normal_forms[j].size()-rproc) ){
3013 if( index==(normal_forms[i].size()-rproc) && index==(normal_forms[j].size()-rproc) ){
3014 //we're done
3015 }else{
3016 //the remainder must be empty
3017 unsigned k = index==(normal_forms[i].size()-rproc) ? j : i;
3018 unsigned index_k = index;
3019 //Node eq_exp = mkAnd( curr_exp );
3020 std::vector< Node > curr_exp;
3021 getExplanationVectorForPrefixEq( normal_forms, normal_form_src, normal_forms_exp, normal_forms_exp_depend, i, j, -1, -1, isRev, curr_exp );
3022 while( !d_conflict && index_k<(normal_forms[k].size()-rproc) ){
3023 //can infer that this string must be empty
3024 Node eq = normal_forms[k][index_k].eqNode( d_emptyString );
3025 //Trace("strings-lemma") << "Strings: Infer " << eq << " from " << eq_exp << std::endl;
3026 Assert( !areEqual( d_emptyString, normal_forms[k][index_k] ) );
3027 sendInference( curr_exp, eq, "N_EndpointEmp" );
3028 index_k++;
3029 }
3030 }
3031 }else{
3032 Trace("strings-solve-debug") << "Process " << normal_forms[i][index] << " ... " << normal_forms[j][index] << std::endl;
3033 if( normal_forms[i][index]==normal_forms[j][index] ){
3034 Trace("strings-solve-debug") << "Simple Case 1 : strings are equal" << std::endl;
3035 index++;
3036 success = true;
3037 }else{
3038 Assert( !areEqual(normal_forms[i][index], normal_forms[j][index]) );
3039 std::vector< Node > temp_exp;
3040 Node length_term_i = getLength( normal_forms[i][index], temp_exp );
3041 Node length_term_j = getLength( normal_forms[j][index], temp_exp );
3042 //check length(normal_forms[i][index]) == length(normal_forms[j][index])
3043 if( areEqual( length_term_i, length_term_j ) ){
3044 Trace("strings-solve-debug") << "Simple Case 2 : string lengths are equal" << std::endl;
3045 Node eq = normal_forms[i][index].eqNode( normal_forms[j][index] );
3046 //eq = Rewriter::rewrite( eq );
3047 Node length_eq = length_term_i.eqNode( length_term_j );
3048 //temp_exp.insert(temp_exp.end(), curr_exp.begin(), curr_exp.end() );
3049 getExplanationVectorForPrefixEq( normal_forms, normal_form_src, normal_forms_exp, normal_forms_exp_depend, i, j, index, index, isRev, temp_exp );
3050 temp_exp.push_back(length_eq);
3051 sendInference( temp_exp, eq, "N_Unify" );
3052 return;
3053 }else if( ( normal_forms[i][index].getKind()!=kind::CONST_STRING && index==normal_forms[i].size()-rproc-1 ) ||
3054 ( normal_forms[j][index].getKind()!=kind::CONST_STRING && index==normal_forms[j].size()-rproc-1 ) ){
3055 Trace("strings-solve-debug") << "Simple Case 3 : at endpoint" << std::endl;
3056 std::vector< Node > antec;
3057 //antec.insert(antec.end(), curr_exp.begin(), curr_exp.end() );
3058 getExplanationVectorForPrefixEq( normal_forms, normal_form_src, normal_forms_exp, normal_forms_exp_depend, i, j, -1, -1, isRev, antec );
3059 std::vector< Node > eqn;
3060 for( unsigned r=0; r<2; r++ ) {
3061 int index_k = index;
3062 int k = r==0 ? i : j;
3063 std::vector< Node > eqnc;
3064 for( unsigned index_l=index_k; index_l<(normal_forms[k].size()-rproc); index_l++ ) {
3065 if(isRev) {
3066 eqnc.insert(eqnc.begin(), normal_forms[k][index_l] );
3067 } else {
3068 eqnc.push_back( normal_forms[k][index_l] );
3069 }
3070 }
3071 eqn.push_back( mkConcat( eqnc ) );
3072 }
3073 if( !areEqual( eqn[0], eqn[1] ) ){
3074 sendInference( antec, eqn[0].eqNode( eqn[1] ), "N_EndpointEq", true );
3075 return;
3076 }else{
3077 Assert( normal_forms[i].size()==normal_forms[j].size() );
3078 index = normal_forms[i].size()-rproc;
3079 }
3080 }else if( normal_forms[i][index].isConst() && normal_forms[j][index].isConst() ){
3081 Node const_str = normal_forms[i][index];
3082 Node other_str = normal_forms[j][index];
3083 Trace("strings-solve-debug") << "Simple Case 3 : Const Split : " << const_str << " vs " << other_str << " at index " << index << ", isRev = " << isRev << std::endl;
3084 unsigned len_short = const_str.getConst<String>().size() <= other_str.getConst<String>().size() ? const_str.getConst<String>().size() : other_str.getConst<String>().size();
3085 bool isSameFix = isRev ? const_str.getConst<String>().rstrncmp(other_str.getConst<String>(), len_short): const_str.getConst<String>().strncmp(other_str.getConst<String>(), len_short);
3086 if( isSameFix ) {
3087 //same prefix/suffix
3088 //k is the index of the string that is shorter
3089 int k = const_str.getConst<String>().size()<other_str.getConst<String>().size() ? i : j;
3090 int l = const_str.getConst<String>().size()<other_str.getConst<String>().size() ? j : i;
3091 //update the nf exp dependencies
3092 //notice this is not critical for soundness: not doing the below incrementing will only lead to overapproximating when antecedants are required in explanations
3093 for( std::map< Node, std::map< bool, int > >::iterator itnd = normal_forms_exp_depend[l].begin(); itnd != normal_forms_exp_depend[l].end(); ++itnd ){
3094 for( std::map< bool, int >::iterator itnd2 = itnd->second.begin(); itnd2 != itnd->second.end(); ++itnd2 ){
3095 //see if this can be incremented: it can if it is not relevant to the current index
3096 Assert( itnd2->second>=0 && itnd2->second<=(int)normal_forms[l].size() );
3097 bool increment = (itnd2->first==isRev) ? itnd2->second>(int)index : ( (int)normal_forms[l].size()-1-itnd2->second )<(int)index;
3098 if( increment ){
3099 normal_forms_exp_depend[l][itnd->first][itnd2->first] = itnd2->second + 1;
3100 }
3101 }
3102 }
3103 if( isRev ){
3104 int new_len = normal_forms[l][index].getConst<String>().size() - len_short;
3105 Node remainderStr = NodeManager::currentNM()->mkConst( normal_forms[l][index].getConst<String>().substr(0, new_len) );
3106 Trace("strings-solve-debug-test") << "Break normal form of " << normal_forms[l][index] << " into " << normal_forms[k][index] << ", " << remainderStr << std::endl;
3107 normal_forms[l].insert( normal_forms[l].begin()+index + 1, remainderStr );
3108 }else{
3109 Node remainderStr = NodeManager::currentNM()->mkConst(normal_forms[l][index].getConst<String>().substr(len_short));
3110 Trace("strings-solve-debug-test") << "Break normal form of " << normal_forms[l][index] << " into " << normal_forms[k][index] << ", " << remainderStr << std::endl;
3111 normal_forms[l].insert( normal_forms[l].begin()+index + 1, remainderStr );
3112 }
3113 normal_forms[l][index] = normal_forms[k][index];
3114 index++;
3115 success = true;
3116 }else{
3117 //conflict
3118 std::vector< Node > antec;
3119 getExplanationVectorForPrefixEq( normal_forms, normal_form_src, normal_forms_exp, normal_forms_exp_depend, i, j, index, index, isRev, antec );
3120 sendInference( antec, d_false, "N_Const", true );
3121 return;
3122 }
3123 }else{
3124 //construct the candidate inference "info"
3125 InferInfo info;
3126 info.d_index = index;
3127 //for debugging
3128 info.d_i = i;
3129 info.d_j = j;
3130 info.d_rev = isRev;
3131 bool info_valid = false;
3132 Assert( index<normal_forms[i].size()-rproc && index<normal_forms[j].size()-rproc );
3133 std::vector< Node > lexp;
3134 Node length_term_i = getLength( normal_forms[i][index], lexp );
3135 Node length_term_j = getLength( normal_forms[j][index], lexp );
3136 //split on equality between string lengths (note that splitting on equality between strings is worse since it is harder to process)
3137 if( !areDisequal( length_term_i, length_term_j ) && !areEqual( length_term_i, length_term_j ) &&
3138 normal_forms[i][index].getKind()!=kind::CONST_STRING && normal_forms[j][index].getKind()!=kind::CONST_STRING ){ //AJR: remove the latter 2 conditions?
3139 Trace("strings-solve-debug") << "Non-simple Case 1 : string lengths neither equal nor disequal" << std::endl;
3140 //try to make the lengths equal via splitting on demand
3141 Node length_eq = NodeManager::currentNM()->mkNode( kind::EQUAL, length_term_i, length_term_j );
3142 length_eq = Rewriter::rewrite( length_eq );
3143 //set info
3144 info.d_conc = NodeManager::currentNM()->mkNode( kind::OR, length_eq, length_eq.negate() );
3145 info.d_pending_phase[ length_eq ] = true;
3146 info.d_id = INFER_LEN_SPLIT;
3147 info_valid = true;
3148 }else{
3149 Trace("strings-solve-debug") << "Non-simple Case 2 : must compare strings" << std::endl;
3150 int loop_in_i = -1;
3151 int loop_in_j = -1;
3152 ProcessLoopResult plr = ProcessLoopResult::SKIPPED;
3153 if( detectLoop( normal_forms, i, j, index, loop_in_i, loop_in_j, rproc ) ){
3154 if( !isRev ){ //FIXME
3155 getExplanationVectorForPrefixEq( normal_forms, normal_form_src, normal_forms_exp, normal_forms_exp_depend, i, j, -1, -1, isRev, info.d_ant );
3156 //set info
3157 plr = processLoop(normal_forms,
3158 normal_form_src,
3159 i,
3160 j,
3161 loop_in_i != -1 ? i : j,
3162 loop_in_i != -1 ? j : i,
3163 loop_in_i != -1 ? loop_in_i : loop_in_j,
3164 index,
3165 info);
3166 if (plr == ProcessLoopResult::INFERENCE)
3167 {
3168 info_valid = true;
3169 }
3170 }
3171 }
3172
3173 if (plr == ProcessLoopResult::SKIPPED)
3174 {
3175 //AJR: length entailment here?
3176 if( normal_forms[i][index].getKind() == kind::CONST_STRING || normal_forms[j][index].getKind() == kind::CONST_STRING ){
3177 unsigned const_k = normal_forms[i][index].getKind() == kind::CONST_STRING ? i : j;
3178 unsigned nconst_k = normal_forms[i][index].getKind() == kind::CONST_STRING ? j : i;
3179 Node other_str = normal_forms[nconst_k][index];
3180 Assert( other_str.getKind()!=kind::CONST_STRING, "Other string is not constant." );
3181 Assert( other_str.getKind()!=kind::STRING_CONCAT, "Other string is not CONCAT." );
3182 if( !d_equalityEngine.areDisequal( other_str, d_emptyString, true ) ){
3183 Node eq = other_str.eqNode( d_emptyString );
3184 //set info
3185 info.d_conc = NodeManager::currentNM()->mkNode( kind::OR, eq, eq.negate() );
3186 info.d_id = INFER_LEN_SPLIT_EMP;
3187 info_valid = true;
3188 }else{
3189 if( !isRev ){ //FIXME
3190 Node xnz = other_str.eqNode( d_emptyString ).negate();
3191 unsigned index_nc_k = index+1;
3192 //Node next_const_str = TheoryStringsRewriter::collectConstantStringAt( normal_forms[nconst_k], index_nc_k, false );
3193 unsigned start_index_nc_k = index+1;
3194 Node next_const_str = TheoryStringsRewriter::getNextConstantAt( normal_forms[nconst_k], start_index_nc_k, index_nc_k, false );
3195 if( !next_const_str.isNull() ) {
3196 unsigned index_c_k = index;
3197 Node const_str = TheoryStringsRewriter::collectConstantStringAt( normal_forms[const_k], index_c_k, false );
3198 Assert( !const_str.isNull() );
3199 CVC4::String stra = const_str.getConst<String>();
3200 CVC4::String strb = next_const_str.getConst<String>();
3201 //since non-empty, we start with charecter #1
3202 size_t p;
3203 if( isRev ){
3204 CVC4::String stra1 = stra.prefix( stra.size()-1 );
3205 p = stra.size() - stra1.roverlap(strb);
3206 Trace("strings-csp-debug") << "Compute roverlap : " << const_str << " " << next_const_str << std::endl;
3207 size_t p2 = stra1.rfind(strb);
3208 p = p2==std::string::npos ? p : ( p>p2+1? p2+1 : p );
3209 Trace("strings-csp-debug") << "overlap : " << stra1 << " " << strb << " returned " << p << " " << p2 << " " << (p2==std::string::npos) << std::endl;
3210 }else{
3211 CVC4::String stra1 = stra.substr( 1 );
3212 p = stra.size() - stra1.overlap(strb);
3213 Trace("strings-csp-debug") << "Compute overlap : " << const_str << " " << next_const_str << std::endl;
3214 size_t p2 = stra1.find(strb);
3215 p = p2==std::string::npos ? p : ( p>p2+1? p2+1 : p );
3216 Trace("strings-csp-debug") << "overlap : " << stra1 << " " << strb << " returned " << p << " " << p2 << " " << (p2==std::string::npos) << std::endl;
3217 }
3218 if( p>1 ){
3219 if( start_index_nc_k==index+1 ){
3220 info.d_ant.push_back( xnz );
3221 getExplanationVectorForPrefixEq( normal_forms, normal_form_src, normal_forms_exp, normal_forms_exp_depend,
3222 const_k, nconst_k, index_c_k, index_nc_k, isRev, info.d_ant );
3223 Node prea = p==stra.size() ? const_str : NodeManager::currentNM()->mkConst( isRev ? stra.suffix( p ) : stra.prefix( p ) );
3224 Node sk = d_sk_cache.mkSkolemCached(
3225 other_str,
3226 prea,
3227 isRev ? SkolemCache::SK_ID_C_SPT_REV
3228 : SkolemCache::SK_ID_C_SPT,
3229 "c_spt");
3230 Trace("strings-csp") << "Const Split: " << prea << " is removed from " << stra << " due to " << strb << ", p=" << p << std::endl;
3231 //set info
3232 info.d_conc = other_str.eqNode( isRev ? mkConcat( sk, prea ) : mkConcat(prea, sk) );
3233 info.d_new_skolem[LENGTH_SPLIT].push_back(sk);
3234 info.d_id = INFER_SSPLIT_CST_PROP;
3235 info_valid = true;
3236 }
3237 /* FIXME for isRev, speculative
3238 else if( options::stringLenPropCsp() ){
3239 //propagate length constraint
3240 std::vector< Node > cc;
3241 for( unsigned i=index; i<start_index_nc_k; i++ ){
3242 cc.push_back( normal_forms[nconst_k][i] );
3243 }
3244 Node lt = NodeManager::currentNM()->mkNode( kind::STRING_LENGTH, mkConcat( cc ) );
3245 conc = NodeManager::currentNM()->mkNode( kind::GEQ, lt, NodeManager::currentNM()->mkConst( Rational(p) ) );
3246 sendInference( ant, conc, "S-Split(CSP-P)-lprop", true );
3247 }
3248 */
3249 }
3250 }
3251 if( !info_valid ){
3252 info.d_ant.push_back( xnz );
3253 Node const_str = normal_forms[const_k][index];
3254 getExplanationVectorForPrefixEq( normal_forms, normal_form_src, normal_forms_exp, normal_forms_exp_depend, i, j, index, index, isRev, info.d_ant );
3255 CVC4::String stra = const_str.getConst<String>();
3256 if( options::stringBinaryCsp() && stra.size()>3 ){
3257 //split string in half
3258 Node c_firstHalf = NodeManager::currentNM()->mkConst( isRev ? stra.substr( stra.size()/2 ) : stra.substr(0, stra.size()/2 ) );
3259 Node sk = d_sk_cache.mkSkolemCached(
3260 other_str,
3261 c_firstHalf,
3262 isRev ? SkolemCache::SK_ID_VC_BIN_SPT_REV
3263 : SkolemCache::SK_ID_VC_BIN_SPT,
3264 "cb_spt");
3265 Trace("strings-csp") << "Const Split: " << c_firstHalf << " is removed from " << const_str << " (binary) " << std::endl;
3266 info.d_conc = NodeManager::currentNM()->mkNode( kind::OR, other_str.eqNode( isRev ? mkConcat( sk, c_firstHalf ) : mkConcat( c_firstHalf, sk ) ),
3267 NodeManager::currentNM()->mkNode( kind::AND,
3268 sk.eqNode( d_emptyString ).negate(),
3269 c_firstHalf.eqNode( isRev ? mkConcat( sk, other_str ) : mkConcat( other_str, sk ) ) ) );
3270 info.d_new_skolem[LENGTH_SPLIT].push_back(sk);
3271 info.d_id = INFER_SSPLIT_CST_BINARY;
3272 info_valid = true;
3273 }else{
3274 // normal v/c split
3275 Node firstChar = stra.size() == 1 ? const_str : NodeManager::currentNM()->mkConst( isRev ? stra.suffix( 1 ) : stra.prefix( 1 ) );
3276 Node sk = d_sk_cache.mkSkolemCached(
3277 other_str,
3278 firstChar,
3279 isRev ? SkolemCache::SK_ID_VC_SPT_REV
3280 : SkolemCache::SK_ID_VC_SPT,
3281 "c_spt");
3282 Trace("strings-csp") << "Const Split: " << firstChar << " is removed from " << const_str << " (serial) " << std::endl;
3283 info.d_conc = other_str.eqNode( isRev ? mkConcat( sk, firstChar ) : mkConcat(firstChar, sk) );
3284 info.d_new_skolem[LENGTH_SPLIT].push_back(sk);
3285 info.d_id = INFER_SSPLIT_CST;
3286 info_valid = true;
3287 }
3288 }
3289 }
3290 }
3291 }else{
3292 int lentTestSuccess = -1;
3293 Node lentTestExp;
3294 if( options::stringCheckEntailLen() ){
3295 //check entailment
3296 for( unsigned e=0; e<2; e++ ){
3297 Node t = e==0 ? normal_forms[i][index] : normal_forms[j][index];
3298 //do not infer constants are larger than variables
3299 if( t.getKind()!=kind::CONST_STRING ){
3300 Node lt1 = e==0 ? length_term_i : length_term_j;
3301 Node lt2 = e==0 ? length_term_j : length_term_i;
3302 Node ent_lit = Rewriter::rewrite( NodeManager::currentNM()->mkNode( kind::GT, lt1, lt2 ) );
3303 std::pair<bool, Node> et = d_valuation.entailmentCheck( THEORY_OF_TYPE_BASED, ent_lit );
3304 if( et.first ){
3305 Trace("strings-entail") << "Strings entailment : " << ent_lit << " is entailed in the current context." << std::endl;
3306 Trace("strings-entail") << " explanation was : " << et.second << std::endl;
3307 lentTestSuccess = e;
3308 lentTestExp = et.second;
3309 break;
3310 }
3311 }
3312 }
3313 }
3314
3315 getExplanationVectorForPrefixEq( normal_forms, normal_form_src, normal_forms_exp, normal_forms_exp_depend, i, j, index, index, isRev, info.d_ant );
3316 //x!=e /\ y!=e
3317 for(unsigned xory=0; xory<2; xory++) {
3318 Node x = xory==0 ? normal_forms[i][index] : normal_forms[j][index];
3319 Node xgtz = x.eqNode( d_emptyString ).negate();
3320 if( d_equalityEngine.areDisequal( x, d_emptyString, true ) ) {
3321 info.d_ant.push_back( xgtz );
3322 } else {
3323 info.d_antn.push_back( xgtz );
3324 }
3325 }
3326 Node sk = d_sk_cache.mkSkolemCached(
3327 normal_forms[i][index],
3328 normal_forms[j][index],
3329 isRev ? SkolemCache::SK_ID_V_SPT_REV
3330 : SkolemCache::SK_ID_V_SPT,
3331 "v_spt");
3332 // must add length requirement
3333 info.d_new_skolem[LENGTH_GEQ_ONE].push_back(sk);
3334 Node eq1 = normal_forms[i][index].eqNode( isRev ? mkConcat(sk, normal_forms[j][index]) : mkConcat(normal_forms[j][index], sk) );
3335 Node eq2 = normal_forms[j][index].eqNode( isRev ? mkConcat(sk, normal_forms[i][index]) : mkConcat(normal_forms[i][index], sk) );
3336
3337 if( lentTestSuccess!=-1 ){
3338 info.d_antn.push_back( lentTestExp );
3339 info.d_conc = lentTestSuccess==0 ? eq1 : eq2;
3340 info.d_id = INFER_SSPLIT_VAR_PROP;
3341 info_valid = true;
3342 }else{
3343 Node ldeq = NodeManager::currentNM()->mkNode( kind::EQUAL, length_term_i, length_term_j ).negate();
3344 if( d_equalityEngine.areDisequal( length_term_i, length_term_j, true ) ){
3345 info.d_ant.push_back( ldeq );
3346 }else{
3347 info.d_antn.push_back(ldeq);
3348 }
3349 //set info
3350 info.d_conc = NodeManager::currentNM()->mkNode( kind::OR, eq1, eq2 );
3351 info.d_id = INFER_SSPLIT_VAR;
3352 info_valid = true;
3353 }
3354 }
3355 }
3356 }
3357 if( info_valid ){
3358 pinfer.push_back( info );
3359 Assert( !success );
3360 }
3361 }
3362 }
3363 }
3364 }while( success );
3365 }
3366
3367 bool TheoryStrings::detectLoop( std::vector< std::vector< Node > > &normal_forms, int i, int j, int index, int &loop_in_i, int &loop_in_j, unsigned rproc ){
3368 int has_loop[2] = { -1, -1 };
3369 if( options::stringLB() != 2 ) {
3370 for( unsigned r=0; r<2; r++ ) {
3371 int n_index = (r==0 ? i : j);
3372 int other_n_index = (r==0 ? j : i);
3373 if( normal_forms[other_n_index][index].getKind() != kind::CONST_STRING ) {
3374 for( unsigned lp = index+1; lp<normal_forms[n_index].size()-rproc; lp++ ){
3375 if( normal_forms[n_index][lp]==normal_forms[other_n_index][index] ){
3376 has_loop[r] = lp;
3377 break;
3378 }
3379 }
3380 }
3381 }
3382 }
3383 if( has_loop[0]!=-1 || has_loop[1]!=-1 ) {
3384 loop_in_i = has_loop[0];
3385 loop_in_j = has_loop[1];
3386 return true;
3387 } else {
3388 Trace("strings-solve-debug") << "No loops detected." << std::endl;
3389 return false;
3390 }
3391 }
3392
3393 //xs(zy)=t(yz)xr
3394 TheoryStrings::ProcessLoopResult TheoryStrings::processLoop(
3395 const std::vector<std::vector<Node> >& normal_forms,
3396 const std::vector<Node>& normal_form_src,
3397 int i,
3398 int j,
3399 int loop_n_index,
3400 int other_n_index,
3401 int loop_index,
3402 int index,
3403 InferInfo& info)
3404 {
3405 if (options::stringProcessLoopMode() == ProcessLoopMode::ABORT)
3406 {
3407 throw LogicException("Looping word equation encountered.");
3408 }
3409 else if (options::stringProcessLoopMode() == ProcessLoopMode::NONE)
3410 {
3411 d_out->setIncomplete();
3412 return ProcessLoopResult::SKIPPED;
3413 }
3414
3415 NodeManager* nm = NodeManager::currentNM();
3416 Node conc;
3417 Trace("strings-loop") << "Detected possible loop for "
3418 << normal_forms[loop_n_index][loop_index] << std::endl;
3419 Trace("strings-loop") << " ... (X)= " << normal_forms[other_n_index][index]
3420 << std::endl;
3421
3422 Trace("strings-loop") << " ... T(Y.Z)= ";
3423 const std::vector<Node>& veci = normal_forms[loop_n_index];
3424 std::vector<Node> vec_t(veci.begin() + index, veci.begin() + loop_index);
3425 Node t_yz = mkConcat(vec_t);
3426 Trace("strings-loop") << " (" << t_yz << ")" << std::endl;
3427 Trace("strings-loop") << " ... S(Z.Y)= ";
3428 const std::vector<Node>& vecoi = normal_forms[other_n_index];
3429 std::vector<Node> vec_s(vecoi.begin() + index + 1, vecoi.end());
3430 Node s_zy = mkConcat(vec_s);
3431 Trace("strings-loop") << s_zy << std::endl;
3432 Trace("strings-loop") << " ... R= ";
3433 std::vector<Node> vec_r(veci.begin() + loop_index + 1, veci.end());
3434 Node r = mkConcat(vec_r);
3435 Trace("strings-loop") << r << std::endl;
3436
3437 if (s_zy.isConst() && r.isConst() && r != d_emptyString)
3438 {
3439 int c;
3440 bool flag = true;
3441 if (s_zy.getConst<String>().tailcmp(r.getConst<String>(), c))
3442 {
3443 if (c >= 0)
3444 {
3445 s_zy = nm->mkConst(s_zy.getConst<String>().substr(0, c));
3446 r = d_emptyString;
3447 vec_r.clear();
3448 Trace("strings-loop") << "Strings::Loop: Refactor S(Z.Y)= " << s_zy
3449 << ", c=" << c << std::endl;
3450 flag = false;
3451 }
3452 }
3453 if (flag)
3454 {
3455 Trace("strings-loop") << "Strings::Loop: tails are different."
3456 << std::endl;
3457 sendInference(info.d_ant, conc, "Loop Conflict", true);
3458 return ProcessLoopResult::CONFLICT;
3459 }
3460 }
3461
3462 Node split_eq;
3463 for (unsigned r = 0; r < 2; r++)
3464 {
3465 Node t = r == 0 ? normal_forms[loop_n_index][loop_index] : t_yz;
3466 split_eq = t.eqNode(d_emptyString);
3467 Node split_eqr = Rewriter::rewrite(split_eq);
3468 // the equality could rewrite to false
3469 if (!split_eqr.isConst())
3470 {
3471 if (!areDisequal(t, d_emptyString))
3472 {
3473 // try to make t equal to empty to avoid loop
3474 info.d_conc = nm->mkNode(kind::OR, split_eq, split_eq.negate());
3475 info.d_id = INFER_LEN_SPLIT_EMP;
3476 return ProcessLoopResult::INFERENCE;
3477 }
3478 else
3479 {
3480 info.d_ant.push_back(split_eq.negate());
3481 }
3482 }
3483 else
3484 {
3485 Assert(!split_eqr.getConst<bool>());
3486 }
3487 }
3488
3489 Node ant = mkExplain(info.d_ant);
3490 info.d_ant.clear();
3491 info.d_antn.push_back(ant);
3492
3493 Node str_in_re;
3494 if (s_zy == t_yz && r == d_emptyString && s_zy.isConst()
3495 && s_zy.getConst<String>().isRepeated())
3496 {
3497 Node rep_c = nm->mkConst(s_zy.getConst<String>().substr(0, 1));
3498 Trace("strings-loop") << "Special case (X)="
3499 << normal_forms[other_n_index][index] << " "
3500 << std::endl;
3501 Trace("strings-loop") << "... (C)=" << rep_c << " " << std::endl;
3502 // special case
3503 str_in_re =
3504 nm->mkNode(kind::STRING_IN_REGEXP,
3505 normal_forms[other_n_index][index],
3506 nm->mkNode(kind::REGEXP_STAR,
3507 nm->mkNode(kind::STRING_TO_REGEXP, rep_c)));
3508 conc = str_in_re;
3509 }
3510 else if (t_yz.isConst())
3511 {
3512 Trace("strings-loop") << "Strings::Loop: Const Normal Breaking."
3513 << std::endl;
3514 CVC4::String s = t_yz.getConst<CVC4::String>();
3515 unsigned size = s.size();
3516 std::vector<Node> vconc;
3517 for (unsigned len = 1; len <= size; len++)
3518 {
3519 Node y = nm->mkConst(s.substr(0, len));
3520 Node z = nm->mkConst(s.substr(len, size - len));
3521 Node restr = s_zy;
3522 Node cc;
3523 if (r != d_emptyString)
3524 {
3525 std::vector<Node> v2(vec_r);
3526 v2.insert(v2.begin(), y);
3527 v2.insert(v2.begin(), z);
3528 restr = mkConcat(z, y);
3529 cc = Rewriter::rewrite(s_zy.eqNode(mkConcat(v2)));
3530 }
3531 else
3532 {
3533 cc = Rewriter::rewrite(s_zy.eqNode(mkConcat(z, y)));
3534 }
3535 if (cc == d_false)
3536 {
3537 continue;
3538 }
3539 Node conc2 = nm->mkNode(
3540 kind::STRING_IN_REGEXP,
3541 normal_forms[other_n_index][index],
3542 nm->mkNode(kind::REGEXP_CONCAT,
3543 nm->mkNode(kind::STRING_TO_REGEXP, y),
3544 nm->mkNode(kind::REGEXP_STAR,
3545 nm->mkNode(kind::STRING_TO_REGEXP, restr))));
3546 cc = cc == d_true ? conc2 : nm->mkNode(kind::AND, cc, conc2);
3547 vconc.push_back(cc);
3548 }
3549 conc = vconc.size() == 0 ? Node::null() : vconc.size() == 1
3550 ? vconc[0]
3551 : nm->mkNode(kind::OR, vconc);
3552 }
3553 else
3554 {
3555 if (options::stringProcessLoopMode() == ProcessLoopMode::SIMPLE_ABORT)
3556 {
3557 throw LogicException("Normal looping word equation encountered.");
3558 }
3559 else if (options::stringProcessLoopMode() == ProcessLoopMode::SIMPLE)
3560 {
3561 d_out->setIncomplete();
3562 return ProcessLoopResult::SKIPPED;
3563 }
3564
3565 Trace("strings-loop") << "Strings::Loop: Normal Loop Breaking."
3566 << std::endl;
3567 // right
3568 Node sk_w = d_sk_cache.mkSkolem("w_loop");
3569 Node sk_y = d_sk_cache.mkSkolem("y_loop");
3570 registerLength(sk_y, LENGTH_GEQ_ONE);
3571 Node sk_z = d_sk_cache.mkSkolem("z_loop");
3572 // t1 * ... * tn = y * z
3573 Node conc1 = t_yz.eqNode(mkConcat(sk_y, sk_z));
3574 // s1 * ... * sk = z * y * r
3575 vec_r.insert(vec_r.begin(), sk_y);
3576 vec_r.insert(vec_r.begin(), sk_z);
3577 Node conc2 = s_zy.eqNode(mkConcat(vec_r));
3578 Node conc3 =
3579 normal_forms[other_n_index][index].eqNode(mkConcat(sk_y, sk_w));
3580 Node restr = r == d_emptyString ? s_zy : mkConcat(sk_z, sk_y);
3581 str_in_re =
3582 nm->mkNode(kind::STRING_IN_REGEXP,
3583 sk_w,
3584 nm->mkNode(kind::REGEXP_STAR,
3585 nm->mkNode(kind::STRING_TO_REGEXP, restr)));
3586
3587 std::vector<Node> vec_conc;
3588 vec_conc.push_back(conc1);
3589 vec_conc.push_back(conc2);
3590 vec_conc.push_back(conc3);
3591 vec_conc.push_back(str_in_re);
3592 // vec_conc.push_back(sk_y.eqNode(d_emptyString).negate());//by mkskolems
3593 conc = nm->mkNode(kind::AND, vec_conc);
3594 } // normal case
3595
3596 // we will be done
3597 info.d_conc = conc;
3598 info.d_id = INFER_FLOOP;
3599 info.d_nf_pair[0] = normal_form_src[i];
3600 info.d_nf_pair[1] = normal_form_src[j];
3601 return ProcessLoopResult::INFERENCE;
3602 }
3603
3604 //return true for lemma, false if we succeed
3605 void TheoryStrings::processDeq( Node ni, Node nj ) {
3606 //Assert( areDisequal( ni, nj ) );
3607 if( d_normal_forms[ni].size()>1 || d_normal_forms[nj].size()>1 ){
3608 std::vector< Node > nfi;
3609 nfi.insert( nfi.end(), d_normal_forms[ni].begin(), d_normal_forms[ni].end() );
3610 std::vector< Node > nfj;
3611 nfj.insert( nfj.end(), d_normal_forms[nj].begin(), d_normal_forms[nj].end() );
3612
3613 int revRet = processReverseDeq( nfi, nfj, ni, nj );
3614 if( revRet!=0 ){
3615 return;
3616 }
3617
3618 nfi.clear();
3619 nfi.insert( nfi.end(), d_normal_forms[ni].begin(), d_normal_forms[ni].end() );
3620 nfj.clear();
3621 nfj.insert( nfj.end(), d_normal_forms[nj].begin(), d_normal_forms[nj].end() );
3622
3623 unsigned index = 0;
3624 while( index<nfi.size() || index<nfj.size() ){
3625 int ret = processSimpleDeq( nfi, nfj, ni, nj, index, false );
3626 if( ret!=0 ) {
3627 return;
3628 }else{
3629 Assert( index<nfi.size() && index<nfj.size() );
3630 Node i = nfi[index];
3631 Node j = nfj[index];
3632 Trace("strings-solve-debug") << "...Processing(DEQ) " << i << " " << j << std::endl;
3633 if( !areEqual( i, j ) ){
3634 Assert( i.getKind()!=kind::CONST_STRING || j.getKind()!=kind::CONST_STRING );
3635 std::vector< Node > lexp;
3636 Node li = getLength( i, lexp );
3637 Node lj = getLength( j, lexp );
3638 if( areDisequal( li, lj ) ){
3639 if( i.getKind()==kind::CONST_STRING || j.getKind()==kind::CONST_STRING ){
3640 //check if empty
3641 Node const_k = i.getKind() == kind::CONST_STRING ? i : j;
3642 Node nconst_k = i.getKind() == kind::CONST_STRING ? j : i;
3643 Node lnck = i.getKind() == kind::CONST_STRING ? lj : li;
3644 if( !d_equalityEngine.areDisequal( nconst_k, d_emptyString, true ) ){
3645 Node eq = nconst_k.eqNode( d_emptyString );
3646 Node conc = NodeManager::currentNM()->mkNode( kind::OR, eq, eq.negate() );
3647 sendInference( d_empty_vec, conc, "D-DISL-Emp-Split" );
3648 return;
3649 }else{
3650 //split on first character
3651 CVC4::String str = const_k.getConst<String>();
3652 Node firstChar = str.size() == 1 ? const_k : NodeManager::currentNM()->mkConst( str.prefix( 1 ) );
3653 if( areEqual( lnck, d_one ) ){
3654 if( areDisequal( firstChar, nconst_k ) ){
3655 return;
3656 }else if( !areEqual( firstChar, nconst_k ) ){
3657 //splitting on demand : try to make them disequal
3658 if (sendSplit(
3659 firstChar, nconst_k, "S-Split(DEQL-Const)", false))
3660 {
3661 return;
3662 }
3663 }
3664 }else{
3665 Node sk = d_sk_cache.mkSkolemCached(
3666 nconst_k, firstChar, SkolemCache::SK_ID_DC_SPT, "dc_spt");
3667 registerLength(sk, LENGTH_ONE);
3668 Node skr =
3669 d_sk_cache.mkSkolemCached(nconst_k,
3670 firstChar,
3671 SkolemCache::SK_ID_DC_SPT_REM,
3672 "dc_spt_rem");
3673 Node eq1 = nconst_k.eqNode( NodeManager::currentNM()->mkNode( kind::STRING_CONCAT, sk, skr ) );
3674 eq1 = Rewriter::rewrite( eq1 );
3675 Node eq2 = nconst_k.eqNode( NodeManager::currentNM()->mkNode( kind::STRING_CONCAT, firstChar, skr ) );
3676 std::vector< Node > antec;
3677 antec.insert( antec.end(), d_normal_forms_exp[ni].begin(), d_normal_forms_exp[ni].end() );
3678 antec.insert( antec.end(), d_normal_forms_exp[nj].begin(), d_normal_forms_exp[nj].end() );
3679 antec.push_back( nconst_k.eqNode( d_emptyString ).negate() );
3680 sendInference( antec, NodeManager::currentNM()->mkNode( kind::OR,
3681 NodeManager::currentNM()->mkNode( kind::AND, eq1, sk.eqNode( firstChar ).negate() ), eq2 ), "D-DISL-CSplit" );
3682 d_pending_req_phase[ eq1 ] = true;
3683 return;
3684 }
3685 }
3686 }else{
3687 Trace("strings-solve") << "Non-Simple Case 1 : add lemma " << std::endl;
3688 //must add lemma
3689 std::vector< Node > antec;
3690 std::vector< Node > antec_new_lits;
3691 antec.insert( antec.end(), d_normal_forms_exp[ni].begin(), d_normal_forms_exp[ni].end() );
3692 antec.insert( antec.end(), d_normal_forms_exp[nj].begin(), d_normal_forms_exp[nj].end() );
3693 //check disequal
3694 if( areDisequal( ni, nj ) ){
3695 antec.push_back( ni.eqNode( nj ).negate() );
3696 }else{
3697 antec_new_lits.push_back( ni.eqNode( nj ).negate() );
3698 }
3699 antec_new_lits.push_back( li.eqNode( lj ).negate() );
3700 std::vector< Node > conc;
3701 Node sk1 = d_sk_cache.mkSkolemCached(
3702 i, j, SkolemCache::SK_ID_DEQ_X, "x_dsplit");
3703 Node sk2 = d_sk_cache.mkSkolemCached(
3704 i, j, SkolemCache::SK_ID_DEQ_Y, "y_dsplit");
3705 Node sk3 = d_sk_cache.mkSkolemCached(
3706 i, j, SkolemCache::SK_ID_DEQ_Z, "z_dsplit");
3707 registerLength(sk3, LENGTH_GEQ_ONE);
3708 //Node nemp = sk3.eqNode(d_emptyString).negate();
3709 //conc.push_back(nemp);
3710 Node lsk1 = mkLength( sk1 );
3711 conc.push_back( lsk1.eqNode( li ) );
3712 Node lsk2 = mkLength( sk2 );
3713 conc.push_back( lsk2.eqNode( lj ) );
3714 conc.push_back( NodeManager::currentNM()->mkNode( kind::OR, j.eqNode( mkConcat( sk1, sk3 ) ), i.eqNode( mkConcat( sk2, sk3 ) ) ) );
3715 sendInference( antec, antec_new_lits, NodeManager::currentNM()->mkNode( kind::AND, conc ), "D-DISL-Split" );
3716 ++(d_statistics.d_deq_splits);
3717 return;
3718 }
3719 }else if( areEqual( li, lj ) ){
3720 Assert( !areDisequal( i, j ) );
3721 //splitting on demand : try to make them disequal
3722 if (sendSplit(i, j, "S-Split(DEQL)", false))
3723 {
3724 return;
3725 }
3726 }else{
3727 //splitting on demand : try to make lengths equal
3728 if (sendSplit(li, lj, "D-Split"))
3729 {
3730 return;
3731 }
3732 }
3733 }
3734 index++;
3735 }
3736 }
3737 Assert( false );
3738 }
3739 }
3740
3741 int TheoryStrings::processReverseDeq( std::vector< Node >& nfi, std::vector< Node >& nfj, Node ni, Node nj ) {
3742 //reverse normal form of i, j
3743 std::reverse( nfi.begin(), nfi.end() );
3744 std::reverse( nfj.begin(), nfj.end() );
3745
3746 unsigned index = 0;
3747 int ret = processSimpleDeq( nfi, nfj, ni, nj, index, true );
3748
3749 //reverse normal form of i, j
3750 std::reverse( nfi.begin(), nfi.end() );
3751 std::reverse( nfj.begin(), nfj.end() );
3752
3753 return ret;
3754 }
3755
3756 int TheoryStrings::processSimpleDeq( std::vector< Node >& nfi, std::vector< Node >& nfj, Node ni, Node nj, unsigned& index, bool isRev ){
3757 // See if one side is constant, if so, the disequality ni != nj is satisfied
3758 // since ni does not contain nj or vice versa.
3759 // This is only valid when isRev is false, since when isRev=true, the contents
3760 // of normal form vectors nfi and nfj are reversed.
3761 if (!isRev)
3762 {
3763 for (unsigned i = 0; i < 2; i++)
3764 {
3765 Node c = getConstantEqc(i == 0 ? ni : nj);
3766 if (!c.isNull())
3767 {
3768 int findex, lindex;
3769 if (!TheoryStringsRewriter::canConstantContainList(
3770 c, i == 0 ? nfj : nfi, findex, lindex))
3771 {
3772 Trace("strings-solve-debug")
3773 << "Disequality: constant cannot contain list" << std::endl;
3774 return 1;
3775 }
3776 }
3777 }
3778 }
3779 while( index<nfi.size() || index<nfj.size() ) {
3780 if( index>=nfi.size() || index>=nfj.size() ){
3781 Trace("strings-solve-debug") << "Disequality normalize empty" << std::endl;
3782 std::vector< Node > ant;
3783 //we have a conflict : because the lengths are equal, the remainder needs to be empty, which will lead to a conflict
3784 Node lni = getLengthExp( ni, ant, d_normal_forms_base[ni] );
3785 Node lnj = getLengthExp( nj, ant, d_normal_forms_base[nj] );
3786 ant.push_back( lni.eqNode( lnj ) );
3787 ant.insert( ant.end(), d_normal_forms_exp[ni].begin(), d_normal_forms_exp[ni].end() );
3788 ant.insert( ant.end(), d_normal_forms_exp[nj].begin(), d_normal_forms_exp[nj].end() );
3789 std::vector< Node > cc;
3790 std::vector< Node >& nfk = index>=nfi.size() ? nfj : nfi;
3791 for( unsigned index_k=index; index_k<nfk.size(); index_k++ ){
3792 cc.push_back( nfk[index_k].eqNode( d_emptyString ) );
3793 }
3794 Node conc = cc.size()==1 ? cc[0] : NodeManager::currentNM()->mkNode( kind::AND, cc );
3795 conc = Rewriter::rewrite( conc );
3796 sendInference( ant, conc, "Disequality Normalize Empty", true);
3797 return -1;
3798 }else{
3799 Node i = nfi[index];
3800 Node j = nfj[index];
3801 Trace("strings-solve-debug") << "...Processing(QED) " << i << " " << j << std::endl;
3802 if( !areEqual( i, j ) ) {
3803 if( i.getKind()==kind::CONST_STRING && j.getKind()==kind::CONST_STRING ) {
3804 unsigned int len_short = i.getConst<String>().size() < j.getConst<String>().size() ? i.getConst<String>().size() : j.getConst<String>().size();
3805 bool isSameFix = isRev ? i.getConst<String>().rstrncmp(j.getConst<String>(), len_short): i.getConst<String>().strncmp(j.getConst<String>(), len_short);
3806 if( isSameFix ) {
3807 //same prefix/suffix
3808 //k is the index of the string that is shorter
3809 Node nk = i.getConst<String>().size() < j.getConst<String>().size() ? i : j;
3810 Node nl = i.getConst<String>().size() < j.getConst<String>().size() ? j : i;
3811 Node remainderStr;
3812 if( isRev ){
3813 int new_len = nl.getConst<String>().size() - len_short;
3814 remainderStr = NodeManager::currentNM()->mkConst( nl.getConst<String>().substr(0, new_len) );
3815 Trace("strings-solve-debug-test") << "Rev. Break normal form of " << nl << " into " << nk << ", " << remainderStr << std::endl;
3816 } else {
3817 remainderStr = NodeManager::currentNM()->mkConst( nl.getConst<String>().substr( len_short ) );
3818 Trace("strings-solve-debug-test") << "Break normal form of " << nl << " into " << nk << ", " << remainderStr << std::endl;
3819 }
3820 if( i.getConst<String>().size() < j.getConst<String>().size() ) {
3821 nfj.insert( nfj.begin() + index + 1, remainderStr );
3822 nfj[index] = nfi[index];
3823 } else {
3824 nfi.insert( nfi.begin() + index + 1, remainderStr );
3825 nfi[index] = nfj[index];
3826 }
3827 }else{
3828 return 1;
3829 }
3830 }else{
3831 std::vector< Node > lexp;
3832 Node li = getLength( i, lexp );
3833 Node lj = getLength( j, lexp );
3834 if( areEqual( li, lj ) && areDisequal( i, j ) ){
3835 Trace("strings-solve") << "Simple Case 2 : found equal length disequal sub strings " << i << " " << j << std::endl;
3836 //we are done: D-Remove
3837 return 1;
3838 }else{
3839 return 0;
3840 }
3841 }
3842 }
3843 index++;
3844 }
3845 }
3846 return 0;
3847 }
3848
3849 void TheoryStrings::addNormalFormPair( Node n1, Node n2 ){
3850 if( !isNormalFormPair( n1, n2 ) ){
3851 int index = 0;
3852 NodeIntMap::const_iterator it = d_nf_pairs.find( n1 );
3853 if( it!=d_nf_pairs.end() ){
3854 index = (*it).second;
3855 }
3856 d_nf_pairs[n1] = index + 1;
3857 if( index<(int)d_nf_pairs_data[n1].size() ){
3858 d_nf_pairs_data[n1][index] = n2;
3859 }else{
3860 d_nf_pairs_data[n1].push_back( n2 );
3861 }
3862 Assert( isNormalFormPair( n1, n2 ) );
3863 } else {
3864 Trace("strings-nf-debug") << "Already a normal form pair " << n1 << " " << n2 << std::endl;
3865 }
3866 }
3867
3868 bool TheoryStrings::isNormalFormPair( Node n1, Node n2 ) {
3869 //TODO: modulo equality?
3870 return isNormalFormPair2( n1, n2 ) || isNormalFormPair2( n2, n1 );
3871 }
3872
3873 bool TheoryStrings::isNormalFormPair2( Node n1, Node n2 ) {
3874 //Trace("strings-debug") << "is normal form pair. " << n1 << " " << n2 << std::endl;
3875 NodeIntMap::const_iterator it = d_nf_pairs.find( n1 );
3876 if( it!=d_nf_pairs.end() ){
3877 Assert( d_nf_pairs_data.find( n1 )!=d_nf_pairs_data.end() );
3878 for( int i=0; i<(*it).second; i++ ){
3879 Assert( i<(int)d_nf_pairs_data[n1].size() );
3880 if( d_nf_pairs_data[n1][i]==n2 ){
3881 return true;
3882 }
3883 }
3884 }
3885 return false;
3886 }
3887
3888 void TheoryStrings::registerTerm( Node n, int effort ) {
3889 TypeNode tn = n.getType();
3890 bool do_register = true;
3891 if (!tn.isString())
3892 {
3893 if (options::stringEagerLen())
3894 {
3895 do_register = effort == 0;
3896 }
3897 else
3898 {
3899 do_register = effort > 0 || n.getKind() != STRING_CONCAT;
3900 }
3901 }
3902 if (!do_register)
3903 {
3904 return;
3905 }
3906 if (d_registered_terms_cache.find(n) != d_registered_terms_cache.end())
3907 {
3908 return;
3909 }
3910 d_registered_terms_cache.insert(n);
3911 NodeManager* nm = NodeManager::currentNM();
3912 Debug("strings-register") << "TheoryStrings::registerTerm() " << n
3913 << ", effort = " << effort << std::endl;
3914 if (tn.isString())
3915 {
3916 // register length information:
3917 // for variables, split on empty vs positive length
3918 // for concat/const/replace, introduce proxy var and state length relation
3919 Node lsum;
3920 if (n.getKind() != STRING_CONCAT && n.getKind() != CONST_STRING)
3921 {
3922 Node lsumb = nm->mkNode(STRING_LENGTH, n);
3923 lsum = Rewriter::rewrite(lsumb);
3924 // can register length term if it does not rewrite
3925 if (lsum == lsumb)
3926 {
3927 registerLength(n, LENGTH_SPLIT);
3928 return;
3929 }
3930 }
3931 Node sk = d_sk_cache.mkSkolemCached(n, SkolemCache::SK_PURIFY, "lsym");
3932 StringsProxyVarAttribute spva;
3933 sk.setAttribute(spva, true);
3934 Node eq = Rewriter::rewrite(sk.eqNode(n));
3935 Trace("strings-lemma") << "Strings::Lemma LENGTH Term : " << eq
3936 << std::endl;
3937 d_proxy_var[n] = sk;
3938 Trace("strings-assert") << "(assert " << eq << ")" << std::endl;
3939 d_out->lemma(eq);
3940 Node skl = nm->mkNode(STRING_LENGTH, sk);
3941 if (n.getKind() == STRING_CONCAT)
3942 {
3943 std::vector<Node> node_vec;
3944 for (unsigned i = 0; i < n.getNumChildren(); i++)
3945 {
3946 if (n[i].getAttribute(StringsProxyVarAttribute()))
3947 {
3948 Assert(d_proxy_var_to_length.find(n[i])
3949 != d_proxy_var_to_length.end());
3950 node_vec.push_back(d_proxy_var_to_length[n[i]]);
3951 }
3952 else
3953 {
3954 Node lni = nm->mkNode(STRING_LENGTH, n[i]);
3955 node_vec.push_back(lni);
3956 }
3957 }
3958 lsum = nm->mkNode(PLUS, node_vec);
3959 lsum = Rewriter::rewrite(lsum);
3960 }
3961 else if (n.getKind() == CONST_STRING)
3962 {
3963 lsum = nm->mkConst(Rational(n.getConst<String>().size()));
3964 }
3965 Assert(!lsum.isNull());
3966 d_proxy_var_to_length[sk] = lsum;
3967 Node ceq = Rewriter::rewrite(skl.eqNode(lsum));
3968 Trace("strings-lemma") << "Strings::Lemma LENGTH : " << ceq << std::endl;
3969 Trace("strings-lemma-debug")
3970 << " prerewrite : " << skl.eqNode(lsum) << std::endl;
3971 Trace("strings-assert") << "(assert " << ceq << ")" << std::endl;
3972 d_out->lemma(ceq);
3973 }
3974 else if (n.getKind() == STRING_CODE)
3975 {
3976 d_has_str_code = true;
3977 // ite( str.len(s)==1, 0 <= str.code(s) < num_codes, str.code(s)=-1 )
3978 Node code_len = mkLength(n[0]).eqNode(d_one);
3979 Node code_eq_neg1 = n.eqNode(d_neg_one);
3980 Node code_range = nm->mkNode(
3981 AND,
3982 nm->mkNode(GEQ, n, d_zero),
3983 nm->mkNode(LT, n, nm->mkConst(Rational(CVC4::String::num_codes()))));
3984 Node lem = nm->mkNode(ITE, code_len, code_range, code_eq_neg1);
3985 Trace("strings-lemma") << "Strings::Lemma CODE : " << lem << std::endl;
3986 Trace("strings-assert") << "(assert " << lem << ")" << std::endl;
3987 d_out->lemma(lem);
3988 }
3989 }
3990
3991 bool TheoryStrings::sendInternalInference(std::vector<Node>& exp,
3992 Node conc,
3993 const char* c)
3994 {
3995 if (conc.getKind() == AND
3996 || (conc.getKind() == NOT && conc[0].getKind() == OR))
3997 {
3998 Node conj = conc.getKind() == AND ? conc : conc[0];
3999 bool pol = conc.getKind() == AND;
4000 bool ret = true;
4001 for (const Node& cc : conj)
4002 {
4003 bool retc = sendInternalInference(exp, pol ? cc : cc.negate(), c);
4004 ret = ret && retc;
4005 }
4006 return ret;
4007 }
4008 bool pol = conc.getKind() != NOT;
4009 Node lit = pol ? conc : conc[0];
4010 if (lit.getKind() == EQUAL)
4011 {
4012 for (unsigned i = 0; i < 2; i++)
4013 {
4014 if (!lit[i].isConst() && !hasTerm(lit[i]))
4015 {
4016 // introduces a new non-constant term, do not infer
4017 return false;
4018 }
4019 }
4020 // does it already hold?
4021 if (pol ? areEqual(lit[0], lit[1]) : areDisequal(lit[0], lit[1]))
4022 {
4023 return true;
4024 }
4025 }
4026 else if (lit.isConst())
4027 {
4028 if (lit.getConst<bool>())
4029 {
4030 Assert(pol);
4031 // trivially holds
4032 return true;
4033 }
4034 }
4035 else if (!hasTerm(lit))
4036 {
4037 // introduces a new non-constant term, do not infer
4038 return false;
4039 }
4040 else if (areEqual(lit, pol ? d_true : d_false))
4041 {
4042 // already holds
4043 return true;
4044 }
4045 sendInference(exp, conc, c);
4046 return true;
4047 }
4048
4049 void TheoryStrings::sendInference( std::vector< Node >& exp, std::vector< Node >& exp_n, Node eq, const char * c, bool asLemma ) {
4050 eq = eq.isNull() ? d_false : Rewriter::rewrite( eq );
4051 if( eq!=d_true ){
4052 if( Trace.isOn("strings-infer-debug") ){
4053 Trace("strings-infer-debug") << "By " << c << ", infer : " << eq << " from: " << std::endl;
4054 for( unsigned i=0; i<exp.size(); i++ ){
4055 Trace("strings-infer-debug") << " " << exp[i] << std::endl;
4056 }
4057 for( unsigned i=0; i<exp_n.size(); i++ ){
4058 Trace("strings-infer-debug") << " N:" << exp_n[i] << std::endl;
4059 }
4060 //Trace("strings-infer-debug") << "as lemma : " << asLemma << std::endl;
4061 }
4062 //check if we should send a lemma or an inference
4063 if( asLemma || eq==d_false || eq.getKind()==kind::OR || !exp_n.empty() || options::stringInferAsLemmas() ){
4064 Node eq_exp;
4065 if( options::stringRExplainLemmas() ){
4066 eq_exp = mkExplain( exp, exp_n );
4067 }else{
4068 if( exp.empty() ){
4069 eq_exp = mkAnd( exp_n );
4070 }else if( exp_n.empty() ){
4071 eq_exp = mkAnd( exp );
4072 }else{
4073 std::vector< Node > ev;
4074 ev.insert( ev.end(), exp.begin(), exp.end() );
4075 ev.insert( ev.end(), exp_n.begin(), exp_n.end() );
4076 eq_exp = NodeManager::currentNM()->mkNode( kind::AND, ev );
4077 }
4078 }
4079 // if we have unexplained literals, this lemma is not a conflict
4080 if (eq == d_false && !exp_n.empty())
4081 {
4082 eq = eq_exp.negate();
4083 eq_exp = d_true;
4084 }
4085 sendLemma( eq_exp, eq, c );
4086 }else{
4087 sendInfer( mkAnd( exp ), eq, c );
4088 }
4089 }
4090 }
4091
4092 void TheoryStrings::sendInference( std::vector< Node >& exp, Node eq, const char * c, bool asLemma ) {
4093 std::vector< Node > exp_n;
4094 sendInference( exp, exp_n, eq, c, asLemma );
4095 }
4096
4097 void TheoryStrings::sendLemma( Node ant, Node conc, const char * c ) {
4098 if( conc.isNull() || conc == d_false ) {
4099 Trace("strings-conflict") << "Strings::Conflict : " << c << " : " << ant << std::endl;
4100 Trace("strings-lemma") << "Strings::Conflict : " << c << " : " << ant << std::endl;
4101 Trace("strings-assert") << "(assert (not " << ant << ")) ; conflict " << c << std::endl;
4102 d_out->conflict(ant);
4103 d_conflict = true;
4104 } else {
4105 Node lem;
4106 if( ant == d_true ) {
4107 lem = conc;
4108 }else{
4109 lem = NodeManager::currentNM()->mkNode( kind::IMPLIES, ant, conc );
4110 }
4111 Trace("strings-lemma") << "Strings::Lemma " << c << " : " << lem << std::endl;
4112 Trace("strings-assert") << "(assert " << lem << ") ; lemma " << c << std::endl;
4113 d_lemma_cache.push_back( lem );
4114 }
4115 }
4116
4117 void TheoryStrings::sendInfer( Node eq_exp, Node eq, const char * c ) {
4118 if( options::stringInferSym() ){
4119 std::vector< Node > vars;
4120 std::vector< Node > subs;
4121 std::vector< Node > unproc;
4122 inferSubstitutionProxyVars( eq_exp, vars, subs, unproc );
4123 if( unproc.empty() ){
4124 Trace("strings-lemma-debug") << "Strings::Infer " << eq << " from " << eq_exp << " by " << c << std::endl;
4125 Node eqs = eq.substitute( vars.begin(), vars.end(), subs.begin(), subs.end() );
4126 Trace("strings-lemma-debug") << "Strings::Infer Alternate : " << eqs << std::endl;
4127 for( unsigned i=0; i<vars.size(); i++ ){
4128 Trace("strings-lemma-debug") << " " << vars[i] << " -> " << subs[i] << std::endl;
4129 }
4130 sendLemma( d_true, eqs, c );
4131 return;
4132 }else{
4133 for( unsigned i=0; i<unproc.size(); i++ ){
4134 Trace("strings-lemma-debug") << " non-trivial exp : " << unproc[i] << std::endl;
4135 }
4136 }
4137 }
4138 Trace("strings-lemma") << "Strings::Infer " << eq << " from " << eq_exp << " by " << c << std::endl;
4139 Trace("strings-assert") << "(assert (=> " << eq_exp << " " << eq << ")) ; infer " << c << std::endl;
4140 d_pending.push_back( eq );
4141 d_pending_exp[eq] = eq_exp;
4142 d_infer.push_back( eq );
4143 d_infer_exp.push_back( eq_exp );
4144 }
4145
4146 bool TheoryStrings::sendSplit(Node a, Node b, const char* c, bool preq)
4147 {
4148 Node eq = a.eqNode( b );
4149 eq = Rewriter::rewrite( eq );
4150 if (!eq.isConst())
4151 {
4152 Node neq = NodeManager::currentNM()->mkNode(kind::NOT, eq);
4153 Node lemma_or = NodeManager::currentNM()->mkNode(kind::OR, eq, neq);
4154 Trace("strings-lemma") << "Strings::Lemma " << c << " SPLIT : " << lemma_or
4155 << std::endl;
4156 d_lemma_cache.push_back(lemma_or);
4157 d_pending_req_phase[eq] = preq;
4158 ++(d_statistics.d_splits);
4159 return true;
4160 }
4161 return false;
4162 }
4163
4164 void TheoryStrings::registerLength(Node n, LengthStatus s)
4165 {
4166 if (d_length_lemma_terms_cache.find(n) != d_length_lemma_terms_cache.end())
4167 {
4168 return;
4169 }
4170 d_length_lemma_terms_cache.insert(n);
4171
4172 NodeManager* nm = NodeManager::currentNM();
4173 Node n_len = nm->mkNode(kind::STRING_LENGTH, n);
4174
4175 if (s == LENGTH_GEQ_ONE)
4176 {
4177 Node neq_empty = n.eqNode(d_emptyString).negate();
4178 Node len_n_gt_z = nm->mkNode(GT, n_len, d_zero);
4179 Node len_geq_one = nm->mkNode(AND, neq_empty, len_n_gt_z);
4180 Trace("strings-lemma") << "Strings::Lemma SK-GEQ-ONE : " << len_geq_one
4181 << std::endl;
4182 Trace("strings-assert") << "(assert " << len_geq_one << ")" << std::endl;
4183 d_out->lemma(len_geq_one);
4184 return;
4185 }
4186
4187 if (s == LENGTH_ONE)
4188 {
4189 Node len_one = n_len.eqNode(d_one);
4190 Trace("strings-lemma") << "Strings::Lemma SK-ONE : " << len_one
4191 << std::endl;
4192 Trace("strings-assert") << "(assert " << len_one << ")" << std::endl;
4193 d_out->lemma(len_one);
4194 return;
4195 }
4196 Assert(s == LENGTH_SPLIT);
4197
4198 if( options::stringSplitEmp() || !options::stringLenGeqZ() ){
4199 Node n_len_eq_z = n_len.eqNode( d_zero );
4200 Node n_len_eq_z_2 = n.eqNode( d_emptyString );
4201 Node case_empty = nm->mkNode(AND, n_len_eq_z, n_len_eq_z_2);
4202 case_empty = Rewriter::rewrite(case_empty);
4203 Node case_nempty = nm->mkNode(GT, n_len, d_zero);
4204 if (!case_empty.isConst())
4205 {
4206 Node lem = nm->mkNode(OR, case_empty, case_nempty);
4207 d_out->lemma(lem);
4208 Trace("strings-lemma") << "Strings::Lemma LENGTH >= 0 : " << lem
4209 << std::endl;
4210 // prefer trying the empty case first
4211 // notice that requirePhase must only be called on rewritten literals that
4212 // occur in the CNF stream.
4213 n_len_eq_z = Rewriter::rewrite(n_len_eq_z);
4214 Assert(!n_len_eq_z.isConst());
4215 d_out->requirePhase(n_len_eq_z, true);
4216 n_len_eq_z_2 = Rewriter::rewrite(n_len_eq_z_2);
4217 Assert(!n_len_eq_z_2.isConst());
4218 d_out->requirePhase(n_len_eq_z_2, true);
4219 }
4220 else if (!case_empty.getConst<bool>())
4221 {
4222 // the rewriter knows that n is non-empty
4223 Trace("strings-lemma")
4224 << "Strings::Lemma LENGTH > 0 (non-empty): " << case_nempty
4225 << std::endl;
4226 d_out->lemma(case_nempty);
4227 }
4228 else
4229 {
4230 // If n = "" ---> true or len( n ) = 0 ----> true, then we expect that
4231 // n ---> "". Since this method is only called on non-constants n, it must
4232 // be that n = "" ^ len( n ) = 0 does not rewrite to true.
4233 Assert(false);
4234 }
4235 }
4236
4237 // additionally add len( x ) >= 0 ?
4238 if( options::stringLenGeqZ() ){
4239 Node n_len_geq = nm->mkNode(kind::GEQ, n_len, d_zero);
4240 n_len_geq = Rewriter::rewrite( n_len_geq );
4241 d_out->lemma( n_len_geq );
4242 }
4243 }
4244
4245 void TheoryStrings::inferSubstitutionProxyVars( Node n, std::vector< Node >& vars, std::vector< Node >& subs, std::vector< Node >& unproc ) {
4246 if( n.getKind()==kind::AND ){
4247 for( unsigned i=0; i<n.getNumChildren(); i++ ){
4248 inferSubstitutionProxyVars( n[i], vars, subs, unproc );
4249 }
4250 return;
4251 }else if( n.getKind()==kind::EQUAL ){
4252 Node ns = n.substitute( vars.begin(), vars.end(), subs.begin(), subs.end() );
4253 ns = Rewriter::rewrite( ns );
4254 if( ns.getKind()==kind::EQUAL ){
4255 Node s;
4256 Node v;
4257 for( unsigned i=0; i<2; i++ ){
4258 Node ss;
4259 if( ns[i].getAttribute(StringsProxyVarAttribute()) ){
4260 ss = ns[i];
4261 }else if( ns[i].isConst() ){
4262 NodeNodeMap::const_iterator it = d_proxy_var.find( ns[i] );
4263 if( it!=d_proxy_var.end() ){
4264 ss = (*it).second;
4265 }
4266 }
4267 if( !ss.isNull() ){
4268 v = ns[1-i];
4269 if( v.getNumChildren()==0 ){
4270 if( s.isNull() ){
4271 s = ss;
4272 }else{
4273 //both sides involved in proxy var
4274 if( ss==s ){
4275 return;
4276 }else{
4277 s = Node::null();
4278 }
4279 }
4280 }
4281 }
4282 }
4283 if( !s.isNull() ){
4284 subs.push_back( s );
4285 vars.push_back( v );
4286 return;
4287 }
4288 }else{
4289 n = ns;
4290 }
4291 }
4292 if( n!=d_true ){
4293 unproc.push_back( n );
4294 }
4295 }
4296
4297
4298 Node TheoryStrings::mkConcat( Node n1, Node n2 ) {
4299 return Rewriter::rewrite( NodeManager::currentNM()->mkNode( kind::STRING_CONCAT, n1, n2 ) );
4300 }
4301
4302 Node TheoryStrings::mkConcat( Node n1, Node n2, Node n3 ) {
4303 return Rewriter::rewrite( NodeManager::currentNM()->mkNode( kind::STRING_CONCAT, n1, n2, n3 ) );
4304 }
4305
4306 Node TheoryStrings::mkConcat( const std::vector< Node >& c ) {
4307 return Rewriter::rewrite( c.size()>1 ? NodeManager::currentNM()->mkNode( kind::STRING_CONCAT, c ) : ( c.size()==1 ? c[0] : d_emptyString ) );
4308 }
4309
4310 Node TheoryStrings::mkLength( Node t ) {
4311 return Rewriter::rewrite( NodeManager::currentNM()->mkNode( kind::STRING_LENGTH, t ) );
4312 }
4313
4314 Node TheoryStrings::mkExplain( std::vector< Node >& a ) {
4315 std::vector< Node > an;
4316 return mkExplain( a, an );
4317 }
4318
4319 Node TheoryStrings::mkExplain( std::vector< Node >& a, std::vector< Node >& an ) {
4320 std::vector< TNode > antec_exp;
4321 for( unsigned i=0; i<a.size(); i++ ) {
4322 if( std::find( a.begin(), a.begin() + i, a[i] )==a.begin() + i ) {
4323 bool exp = true;
4324 Debug("strings-explain") << "Ask for explanation of " << a[i] << std::endl;
4325 //assert
4326 if(a[i].getKind() == kind::EQUAL) {
4327 //Assert( hasTerm(a[i][0]) );
4328 //Assert( hasTerm(a[i][1]) );
4329 Assert( areEqual(a[i][0], a[i][1]) );
4330 if( a[i][0]==a[i][1] ){
4331 exp = false;
4332 }
4333 } else if( a[i].getKind()==kind::NOT && a[i][0].getKind()==kind::EQUAL ) {
4334 Assert( hasTerm(a[i][0][0]) );
4335 Assert( hasTerm(a[i][0][1]) );
4336 AlwaysAssert( d_equalityEngine.areDisequal(a[i][0][0], a[i][0][1], true) );
4337 }else if( a[i].getKind() == kind::AND ){
4338 for( unsigned j=0; j<a[i].getNumChildren(); j++ ){
4339 a.push_back( a[i][j] );
4340 }
4341 exp = false;
4342 }
4343 if( exp ) {
4344 unsigned ps = antec_exp.size();
4345 explain(a[i], antec_exp);
4346 Debug("strings-explain") << "Done, explanation was : " << std::endl;
4347 for( unsigned j=ps; j<antec_exp.size(); j++ ) {
4348 Debug("strings-explain") << " " << antec_exp[j] << std::endl;
4349 }
4350 Debug("strings-explain") << std::endl;
4351 }
4352 }
4353 }
4354 for( unsigned i=0; i<an.size(); i++ ) {
4355 if( std::find( an.begin(), an.begin() + i, an[i] )==an.begin() + i ){
4356 Debug("strings-explain") << "Add to explanation (new literal) " << an[i] << std::endl;
4357 antec_exp.push_back(an[i]);
4358 }
4359 }
4360 Node ant;
4361 if( antec_exp.empty() ) {
4362 ant = d_true;
4363 } else if( antec_exp.size()==1 ) {
4364 ant = antec_exp[0];
4365 } else {
4366 ant = NodeManager::currentNM()->mkNode( kind::AND, antec_exp );
4367 }
4368 //ant = Rewriter::rewrite( ant );
4369 return ant;
4370 }
4371
4372 Node TheoryStrings::mkAnd( std::vector< Node >& a ) {
4373 std::vector< Node > au;
4374 for( unsigned i=0; i<a.size(); i++ ){
4375 if( std::find( au.begin(), au.end(), a[i] )==au.end() ){
4376 au.push_back( a[i] );
4377 }
4378 }
4379 if( au.empty() ) {
4380 return d_true;
4381 } else if( au.size() == 1 ) {
4382 return au[0];
4383 } else {
4384 return NodeManager::currentNM()->mkNode( kind::AND, au );
4385 }
4386 }
4387
4388 void TheoryStrings::getConcatVec( Node n, std::vector< Node >& c ) {
4389 if( n.getKind()==kind::STRING_CONCAT ) {
4390 for( unsigned i=0; i<n.getNumChildren(); i++ ) {
4391 if( !areEqual( n[i], d_emptyString ) ) {
4392 c.push_back( n[i] );
4393 }
4394 }
4395 }else{
4396 c.push_back( n );
4397 }
4398 }
4399
4400 void TheoryStrings::checkNormalFormsDeq()
4401 {
4402 std::vector< std::vector< Node > > cols;
4403 std::vector< Node > lts;
4404 std::map< Node, std::map< Node, bool > > processed;
4405
4406 //for each pair of disequal strings, must determine whether their lengths are equal or disequal
4407 for( NodeList::const_iterator id = d_ee_disequalities.begin(); id != d_ee_disequalities.end(); ++id ) {
4408 Node eq = *id;
4409 Node n[2];
4410 for( unsigned i=0; i<2; i++ ){
4411 n[i] = d_equalityEngine.getRepresentative( eq[i] );
4412 }
4413 if( processed[n[0]].find( n[1] )==processed[n[0]].end() ){
4414 processed[n[0]][n[1]] = true;
4415 Node lt[2];
4416 for( unsigned i=0; i<2; i++ ){
4417 EqcInfo* ei = getOrMakeEqcInfo( n[i], false );
4418 lt[i] = ei ? ei->d_length_term : Node::null();
4419 if( lt[i].isNull() ){
4420 lt[i] = eq[i];
4421 }
4422 lt[i] = NodeManager::currentNM()->mkNode( kind::STRING_LENGTH, lt[i] );
4423 }
4424 if( !areEqual( lt[0], lt[1] ) && !areDisequal( lt[0], lt[1] ) ){
4425 sendSplit( lt[0], lt[1], "DEQ-LENGTH-SP" );
4426 }
4427 }
4428 }
4429
4430 if( !hasProcessed() ){
4431 separateByLength( d_strings_eqc, cols, lts );
4432 for( unsigned i=0; i<cols.size(); i++ ){
4433 if( cols[i].size()>1 && d_lemma_cache.empty() ){
4434 Trace("strings-solve") << "- Verify disequalities are processed for " << cols[i][0] << ", normal form : ";
4435 printConcat( d_normal_forms[cols[i][0]], "strings-solve" );
4436 Trace("strings-solve") << "... #eql = " << cols[i].size() << std::endl;
4437 //must ensure that normal forms are disequal
4438 for( unsigned j=0; j<cols[i].size(); j++ ){
4439 for( unsigned k=(j+1); k<cols[i].size(); k++ ){
4440 //for strings that are disequal, but have the same length
4441 if( areDisequal( cols[i][j], cols[i][k] ) ){
4442 Assert( !d_conflict );
4443 Trace("strings-solve") << "- Compare " << cols[i][j] << " ";
4444 printConcat( d_normal_forms[cols[i][j]], "strings-solve" );
4445 Trace("strings-solve") << " against " << cols[i][k] << " ";
4446 printConcat( d_normal_forms[cols[i][k]], "strings-solve" );
4447 Trace("strings-solve") << "..." << std::endl;
4448 processDeq( cols[i][j], cols[i][k] );
4449 if( hasProcessed() ){
4450 return;
4451 }
4452 }
4453 }
4454 }
4455 }
4456 }
4457 }
4458 }
4459
4460 void TheoryStrings::checkLengthsEqc() {
4461 if( options::stringLenNorm() ){
4462 for( unsigned i=0; i<d_strings_eqc.size(); i++ ){
4463 //if( d_normal_forms[nodes[i]].size()>1 ) {
4464 Trace("strings-process-debug") << "Process length constraints for " << d_strings_eqc[i] << std::endl;
4465 //check if there is a length term for this equivalence class
4466 EqcInfo* ei = getOrMakeEqcInfo( d_strings_eqc[i], false );
4467 Node lt = ei ? ei->d_length_term : Node::null();
4468 if( !lt.isNull() ) {
4469 Node llt = NodeManager::currentNM()->mkNode( kind::STRING_LENGTH, lt );
4470 //now, check if length normalization has occurred
4471 if( ei->d_normalized_length.get().isNull() ) {
4472 Node nf = mkConcat( d_normal_forms[d_strings_eqc[i]] );
4473 if( Trace.isOn("strings-process-debug") ){
4474 Trace("strings-process-debug") << " normal form is " << nf << " from base " << d_normal_forms_base[d_strings_eqc[i]] << std::endl;
4475 Trace("strings-process-debug") << " normal form exp is: " << std::endl;
4476 for( unsigned j=0; j<d_normal_forms_exp[d_strings_eqc[i]].size(); j++ ){
4477 Trace("strings-process-debug") << " " << d_normal_forms_exp[d_strings_eqc[i]][j] << std::endl;
4478 }
4479 }
4480
4481 //if not, add the lemma
4482 std::vector< Node > ant;
4483 ant.insert( ant.end(), d_normal_forms_exp[d_strings_eqc[i]].begin(), d_normal_forms_exp[d_strings_eqc[i]].end() );
4484 ant.push_back( d_normal_forms_base[d_strings_eqc[i]].eqNode( lt ) );
4485 Node lc = NodeManager::currentNM()->mkNode( kind::STRING_LENGTH, nf );
4486 Node lcr = Rewriter::rewrite( lc );
4487 Trace("strings-process-debug") << "Rewrote length " << lc << " to " << lcr << std::endl;
4488 Node eq = llt.eqNode( lcr );
4489 if( llt!=lcr ){
4490 ei->d_normalized_length.set( eq );
4491 sendInference( ant, eq, "LEN-NORM", true );
4492 }
4493 }
4494 }else{
4495 Trace("strings-process-debug") << "No length term for eqc " << d_strings_eqc[i] << " " << d_eqc_to_len_term[d_strings_eqc[i]] << std::endl;
4496 if( !options::stringEagerLen() ){
4497 Node c = mkConcat( d_normal_forms[d_strings_eqc[i]] );
4498 registerTerm( c, 3 );
4499 /*
4500 if( !c.isConst() ){
4501 NodeNodeMap::const_iterator it = d_proxy_var.find( c );
4502 if( it!=d_proxy_var.end() ){
4503 Node pv = (*it).second;
4504 Assert( d_proxy_var_to_length.find( pv )!=d_proxy_var_to_length.end() );
4505 Node pvl = d_proxy_var_to_length[pv];
4506 Node ceq = Rewriter::rewrite( mkLength( pv ).eqNode( pvl ) );
4507 sendInference( d_empty_vec, ceq, "LEN-NORM-I", true );
4508 }
4509 }
4510 */
4511 }
4512 }
4513 //} else {
4514 // Trace("strings-process-debug") << "Do not process length constraints for " << nodes[i] << " " << d_normal_forms[nodes[i]].size() << std::endl;
4515 //}
4516 }
4517 }
4518 }
4519
4520 void TheoryStrings::checkCardinality() {
4521 //int cardinality = options::stringCharCardinality();
4522 //Trace("strings-solve-debug2") << "get cardinality: " << cardinality << endl;
4523
4524 //AJR: this will create a partition of eqc, where each collection has length that are pairwise propagated to be equal.
4525 // we do not require disequalities between the lengths of each collection, since we split on disequalities between lengths of string terms that are disequal (DEQ-LENGTH-SP).
4526 // TODO: revisit this?
4527 std::vector< std::vector< Node > > cols;
4528 std::vector< Node > lts;
4529 separateByLength( d_strings_eqc, cols, lts );
4530
4531 Trace("strings-card") << "Check cardinality...." << std::endl;
4532 for( unsigned i = 0; i<cols.size(); ++i ) {
4533 Node lr = lts[i];
4534 Trace("strings-card") << "Number of strings with length equal to " << lr << " is " << cols[i].size() << std::endl;
4535 if( cols[i].size() > 1 ) {
4536 // size > c^k
4537 unsigned card_need = 1;
4538 double curr = (double)cols[i].size();
4539 while( curr>d_card_size ){
4540 curr = curr/(double)d_card_size;
4541 card_need++;
4542 }
4543 Trace("strings-card") << "Need length " << card_need << " for this number of strings (where alphabet size is " << d_card_size << ")." << std::endl;
4544 //check if we need to split
4545 bool needsSplit = true;
4546 if( lr.isConst() ){
4547 // if constant, compare
4548 Node cmp = NodeManager::currentNM()->mkNode( kind::GEQ, lr, NodeManager::currentNM()->mkConst( Rational( card_need ) ) );
4549 cmp = Rewriter::rewrite( cmp );
4550 needsSplit = cmp!=d_true;
4551 }else{
4552 // find the minimimum constant that we are unknown to be disequal from, or otherwise stop if we increment such that cardinality does not apply
4553 unsigned r=0;
4554 bool success = true;
4555 while( r<card_need && success ){
4556 Node rr = NodeManager::currentNM()->mkConst<Rational>( Rational(r) );
4557 if( areDisequal( rr, lr ) ){
4558 r++;
4559 }else{
4560 success = false;
4561 }
4562 }
4563 if( r>0 ){
4564 Trace("strings-card") << "Symbolic length " << lr << " must be at least " << r << " due to constant disequalities." << std::endl;
4565 }
4566 needsSplit = r<card_need;
4567 }
4568
4569 if( needsSplit ){
4570 unsigned int int_k = (unsigned int)card_need;
4571 for( std::vector< Node >::iterator itr1 = cols[i].begin();
4572 itr1 != cols[i].end(); ++itr1) {
4573 for( std::vector< Node >::iterator itr2 = itr1 + 1;
4574 itr2 != cols[i].end(); ++itr2) {
4575 if(!areDisequal( *itr1, *itr2 )) {
4576 // add split lemma
4577 if (sendSplit(*itr1, *itr2, "CARD-SP"))
4578 {
4579 return;
4580 }
4581 }
4582 }
4583 }
4584 EqcInfo* ei = getOrMakeEqcInfo( lr, true );
4585 Trace("strings-card") << "Previous cardinality used for " << lr << " is " << ((int)ei->d_cardinality_lem_k.get()-1) << std::endl;
4586 if( int_k+1 > ei->d_cardinality_lem_k.get() ){
4587 Node k_node = NodeManager::currentNM()->mkConst( ::CVC4::Rational( int_k ) );
4588 //add cardinality lemma
4589 Node dist = NodeManager::currentNM()->mkNode( kind::DISTINCT, cols[i] );
4590 std::vector< Node > vec_node;
4591 vec_node.push_back( dist );
4592 for( std::vector< Node >::iterator itr1 = cols[i].begin();
4593 itr1 != cols[i].end(); ++itr1) {
4594 Node len = NodeManager::currentNM()->mkNode( kind::STRING_LENGTH, *itr1 );
4595 if( len!=lr ) {
4596 Node len_eq_lr = len.eqNode(lr);
4597 vec_node.push_back( len_eq_lr );
4598 }
4599 }
4600 Node len = NodeManager::currentNM()->mkNode( kind::STRING_LENGTH, cols[i][0] );
4601 Node cons = NodeManager::currentNM()->mkNode( kind::GEQ, len, k_node );
4602 cons = Rewriter::rewrite( cons );
4603 ei->d_cardinality_lem_k.set( int_k+1 );
4604 if( cons!=d_true ){
4605 sendInference( d_empty_vec, vec_node, cons, "CARDINALITY", true );
4606 return;
4607 }
4608 }
4609 }
4610 }
4611 }
4612 Trace("strings-card") << "...end check cardinality" << std::endl;
4613 }
4614
4615 void TheoryStrings::getEquivalenceClasses( std::vector< Node >& eqcs ) {
4616 eq::EqClassesIterator eqcs_i = eq::EqClassesIterator( &d_equalityEngine );
4617 while( !eqcs_i.isFinished() ) {
4618 Node eqc = (*eqcs_i);
4619 //if eqc.getType is string
4620 if (eqc.getType().isString()) {
4621 eqcs.push_back( eqc );
4622 }
4623 ++eqcs_i;
4624 }
4625 }
4626
4627 void TheoryStrings::separateByLength(std::vector< Node >& n,
4628 std::vector< std::vector< Node > >& cols,
4629 std::vector< Node >& lts ) {
4630 unsigned leqc_counter = 0;
4631 std::map< Node, unsigned > eqc_to_leqc;
4632 std::map< unsigned, Node > leqc_to_eqc;
4633 std::map< unsigned, std::vector< Node > > eqc_to_strings;
4634 for( unsigned i=0; i<n.size(); i++ ) {
4635 Node eqc = n[i];
4636 Assert( d_equalityEngine.getRepresentative(eqc)==eqc );
4637 EqcInfo* ei = getOrMakeEqcInfo( eqc, false );
4638 Node lt = ei ? ei->d_length_term : Node::null();
4639 if( !lt.isNull() ){
4640 lt = NodeManager::currentNM()->mkNode( kind::STRING_LENGTH, lt );
4641 Node r = d_equalityEngine.getRepresentative( lt );
4642 if( eqc_to_leqc.find( r )==eqc_to_leqc.end() ){
4643 eqc_to_leqc[r] = leqc_counter;
4644 leqc_to_eqc[leqc_counter] = r;
4645 leqc_counter++;
4646 }
4647 eqc_to_strings[ eqc_to_leqc[r] ].push_back( eqc );
4648 }else{
4649 eqc_to_strings[leqc_counter].push_back( eqc );
4650 leqc_counter++;
4651 }
4652 }
4653 for( std::map< unsigned, std::vector< Node > >::iterator it = eqc_to_strings.begin(); it != eqc_to_strings.end(); ++it ){
4654 cols.push_back( std::vector< Node >() );
4655 cols.back().insert( cols.back().end(), it->second.begin(), it->second.end() );
4656 lts.push_back( leqc_to_eqc[it->first] );
4657 }
4658 }
4659
4660 void TheoryStrings::printConcat( std::vector< Node >& n, const char * c ) {
4661 for( unsigned i=0; i<n.size(); i++ ){
4662 if( i>0 ) Trace(c) << " ++ ";
4663 Trace(c) << n[i];
4664 }
4665 }
4666
4667
4668 //// Finite Model Finding
4669
4670 TheoryStrings::StringSumLengthDecisionStrategy::StringSumLengthDecisionStrategy(
4671 context::Context* c, context::UserContext* u, Valuation valuation)
4672 : DecisionStrategyFmf(c, valuation), d_input_var_lsum(u)
4673 {
4674 }
4675
4676 bool TheoryStrings::StringSumLengthDecisionStrategy::isInitialized()
4677 {
4678 return !d_input_var_lsum.get().isNull();
4679 }
4680
4681 void TheoryStrings::StringSumLengthDecisionStrategy::initialize(
4682 const std::vector<Node>& vars)
4683 {
4684 if (d_input_var_lsum.get().isNull() && !vars.empty())
4685 {
4686 NodeManager* nm = NodeManager::currentNM();
4687 std::vector<Node> sum;
4688 for (const Node& v : vars)
4689 {
4690 sum.push_back(nm->mkNode(STRING_LENGTH, v));
4691 }
4692 Node sumn = sum.size() == 1 ? sum[0] : nm->mkNode(PLUS, sum);
4693 d_input_var_lsum.set(sumn);
4694 }
4695 }
4696
4697 Node TheoryStrings::StringSumLengthDecisionStrategy::mkLiteral(unsigned i)
4698 {
4699 if (d_input_var_lsum.get().isNull())
4700 {
4701 return Node::null();
4702 }
4703 NodeManager* nm = NodeManager::currentNM();
4704 Node lit = nm->mkNode(LEQ, d_input_var_lsum.get(), nm->mkConst(Rational(i)));
4705 Trace("strings-fmf") << "StringsFMF::mkLiteral: " << lit << std::endl;
4706 return lit;
4707 }
4708 std::string TheoryStrings::StringSumLengthDecisionStrategy::identify() const
4709 {
4710 return std::string("string_sum_len");
4711 }
4712
4713 Node TheoryStrings::ppRewrite(TNode atom) {
4714 Trace("strings-ppr") << "TheoryStrings::ppRewrite " << atom << std::endl;
4715 Node atomElim;
4716 if (options::regExpElim() && atom.getKind() == STRING_IN_REGEXP)
4717 {
4718 // aggressive elimination of regular expression membership
4719 atomElim = d_regexp_elim.eliminate(atom);
4720 if (!atomElim.isNull())
4721 {
4722 Trace("strings-ppr") << " rewrote " << atom << " -> " << atomElim
4723 << " via regular expression elimination."
4724 << std::endl;
4725 atom = atomElim;
4726 }
4727 }
4728 if( !options::stringLazyPreproc() ){
4729 //eager preprocess here
4730 std::vector< Node > new_nodes;
4731 Node ret = d_preproc.processAssertion( atom, new_nodes );
4732 if( ret!=atom ){
4733 Trace("strings-ppr") << " rewrote " << atom << " -> " << ret << ", with " << new_nodes.size() << " lemmas." << std::endl;
4734 for( unsigned i=0; i<new_nodes.size(); i++ ){
4735 Trace("strings-ppr") << " lemma : " << new_nodes[i] << std::endl;
4736 d_out->lemma( new_nodes[i] );
4737 }
4738 return ret;
4739 }else{
4740 Assert( new_nodes.empty() );
4741 }
4742 }
4743 return atom;
4744 }
4745
4746 // Stats
4747 TheoryStrings::Statistics::Statistics()
4748 : d_splits("theory::strings::NumOfSplitOnDemands", 0),
4749 d_eq_splits("theory::strings::NumOfEqSplits", 0),
4750 d_deq_splits("theory::strings::NumOfDiseqSplits", 0),
4751 d_loop_lemmas("theory::strings::NumOfLoops", 0)
4752 {
4753 smtStatisticsRegistry()->registerStat(&d_splits);
4754 smtStatisticsRegistry()->registerStat(&d_eq_splits);
4755 smtStatisticsRegistry()->registerStat(&d_deq_splits);
4756 smtStatisticsRegistry()->registerStat(&d_loop_lemmas);
4757 }
4758
4759 TheoryStrings::Statistics::~Statistics(){
4760 smtStatisticsRegistry()->unregisterStat(&d_splits);
4761 smtStatisticsRegistry()->unregisterStat(&d_eq_splits);
4762 smtStatisticsRegistry()->unregisterStat(&d_deq_splits);
4763 smtStatisticsRegistry()->unregisterStat(&d_loop_lemmas);
4764 }
4765
4766 /** run the given inference step */
4767 void TheoryStrings::runInferStep(InferStep s, int effort)
4768 {
4769 Trace("strings-process") << "Run " << s;
4770 if (effort > 0)
4771 {
4772 Trace("strings-process") << ", effort = " << effort;
4773 }
4774 Trace("strings-process") << "..." << std::endl;
4775 switch (s)
4776 {
4777 case CHECK_INIT: checkInit(); break;
4778 case CHECK_CONST_EQC: checkConstantEquivalenceClasses(); break;
4779 case CHECK_EXTF_EVAL: checkExtfEval(effort); break;
4780 case CHECK_CYCLES: checkCycles(); break;
4781 case CHECK_FLAT_FORMS: checkFlatForms(); break;
4782 case CHECK_NORMAL_FORMS_EQ: checkNormalFormsEq(); break;
4783 case CHECK_NORMAL_FORMS_DEQ: checkNormalFormsDeq(); break;
4784 case CHECK_CODES: checkCodes(); break;
4785 case CHECK_LENGTH_EQC: checkLengthsEqc(); break;
4786 case CHECK_EXTF_REDUCTION: checkExtfReductions(effort); break;
4787 case CHECK_MEMBERSHIP: checkMemberships(); break;
4788 case CHECK_CARDINALITY: checkCardinality(); break;
4789 default: Unreachable(); break;
4790 }
4791 Trace("strings-process") << "Done " << s
4792 << ", addedFact = " << !d_pending.empty() << " "
4793 << !d_lemma_cache.empty()
4794 << ", d_conflict = " << d_conflict << std::endl;
4795 }
4796
4797 bool TheoryStrings::hasStrategyEffort(Effort e) const
4798 {
4799 return d_strat_steps.find(e) != d_strat_steps.end();
4800 }
4801
4802 void TheoryStrings::addStrategyStep(InferStep s, int effort, bool addBreak)
4803 {
4804 // must run check init first
4805 Assert((s == CHECK_INIT)==d_infer_steps.empty());
4806 // must use check cycles when using flat forms
4807 Assert(s != CHECK_FLAT_FORMS
4808 || std::find(d_infer_steps.begin(), d_infer_steps.end(), CHECK_CYCLES)
4809 != d_infer_steps.end());
4810 d_infer_steps.push_back(s);
4811 d_infer_step_effort.push_back(effort);
4812 if (addBreak)
4813 {
4814 d_infer_steps.push_back(BREAK);
4815 d_infer_step_effort.push_back(0);
4816 }
4817 }
4818
4819 void TheoryStrings::initializeStrategy()
4820 {
4821 // initialize the strategy if not already done so
4822 if (!d_strategy_init)
4823 {
4824 std::map<Effort, unsigned> step_begin;
4825 std::map<Effort, unsigned> step_end;
4826 d_strategy_init = true;
4827 // beginning indices
4828 step_begin[EFFORT_FULL] = 0;
4829 if (options::stringEager())
4830 {
4831 step_begin[EFFORT_STANDARD] = 0;
4832 }
4833 // add the inference steps
4834 addStrategyStep(CHECK_INIT);
4835 addStrategyStep(CHECK_CONST_EQC);
4836 addStrategyStep(CHECK_EXTF_EVAL, 0);
4837 addStrategyStep(CHECK_CYCLES);
4838 if (options::stringFlatForms())
4839 {
4840 addStrategyStep(CHECK_FLAT_FORMS);
4841 }
4842 addStrategyStep(CHECK_EXTF_REDUCTION, 1);
4843 if (options::stringEager())
4844 {
4845 // do only the above inferences at standard effort, if applicable
4846 step_end[EFFORT_STANDARD] = d_infer_steps.size() - 1;
4847 }
4848 addStrategyStep(CHECK_NORMAL_FORMS_EQ);
4849 addStrategyStep(CHECK_EXTF_EVAL, 1);
4850 if (!options::stringEagerLen())
4851 {
4852 addStrategyStep(CHECK_LENGTH_EQC);
4853 }
4854 addStrategyStep(CHECK_NORMAL_FORMS_DEQ);
4855 addStrategyStep(CHECK_CODES);
4856 if (options::stringEagerLen())
4857 {
4858 addStrategyStep(CHECK_LENGTH_EQC);
4859 }
4860 if (options::stringExp() && !options::stringGuessModel())
4861 {
4862 addStrategyStep(CHECK_EXTF_REDUCTION, 2);
4863 }
4864 addStrategyStep(CHECK_MEMBERSHIP);
4865 addStrategyStep(CHECK_CARDINALITY);
4866 step_end[EFFORT_FULL] = d_infer_steps.size() - 1;
4867 if (options::stringExp() && options::stringGuessModel())
4868 {
4869 step_begin[EFFORT_LAST_CALL] = d_infer_steps.size();
4870 // these two steps are run in parallel
4871 addStrategyStep(CHECK_EXTF_REDUCTION, 2, false);
4872 addStrategyStep(CHECK_EXTF_EVAL, 3);
4873 step_end[EFFORT_LAST_CALL] = d_infer_steps.size() - 1;
4874 }
4875 // set the beginning/ending ranges
4876 for (const std::pair<const Effort, unsigned>& it_begin : step_begin)
4877 {
4878 Effort e = it_begin.first;
4879 std::map<Effort, unsigned>::iterator it_end = step_end.find(e);
4880 Assert(it_end != step_end.end());
4881 d_strat_steps[e] =
4882 std::pair<unsigned, unsigned>(it_begin.second, it_end->second);
4883 }
4884 }
4885 }
4886
4887 void TheoryStrings::runStrategy(unsigned sbegin, unsigned send)
4888 {
4889 Trace("strings-process") << "----check, next round---" << std::endl;
4890 for (unsigned i = sbegin; i <= send; i++)
4891 {
4892 InferStep curr = d_infer_steps[i];
4893 if (curr == BREAK)
4894 {
4895 if (hasProcessed())
4896 {
4897 break;
4898 }
4899 }
4900 else
4901 {
4902 runInferStep(curr, d_infer_step_effort[i]);
4903 if (d_conflict)
4904 {
4905 break;
4906 }
4907 }
4908 }
4909 Trace("strings-process") << "----finished round---" << std::endl;
4910 }
4911
4912 }/* CVC4::theory::strings namespace */
4913 }/* CVC4::theory namespace */
4914 }/* CVC4 namespace */