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