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