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