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