1 /********************* */
2 /*! \file theory_strings.cpp
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
12 ** \brief Implementation of the theory of strings.
14 ** Implementation of the theory of strings.
17 #include "theory/strings/theory_strings.h"
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"
33 using namespace CVC4::context
;
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() ){
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
);
53 return d_children
[nir
].add( n
, index
+1, t
, er
, c
);
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
),
65 d_equalityEngine(d_notify
, c
, "theory::strings::TheoryStrings", true),
71 d_length_intro_vars(u
),
72 d_pregistered_terms_cache(u
),
73 d_registered_terms_cache(u
),
76 d_extf_infer_cache(c
),
79 d_proxy_var_to_length(u
),
84 d_regexp_memberships(c
),
91 d_processed_memberships(c
),
95 d_cardinality_lits(u
),
96 d_curr_cardinality(c
, 0)
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
);
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 );
126 TheoryStrings::~TheoryStrings() {
130 Node
TheoryStrings::getRepresentative( Node t
) {
131 if( d_equalityEngine
.hasTerm( t
) ){
132 return d_equalityEngine
.getRepresentative( t
);
138 bool TheoryStrings::hasTerm( Node a
){
139 return d_equalityEngine
.hasTerm( a
);
142 bool TheoryStrings::areEqual( Node a
, Node b
){
145 }else if( hasTerm( a
) && hasTerm( b
) ){
146 return d_equalityEngine
.areEqual( a
, b
);
152 bool TheoryStrings::areDisequal( Node a
, Node b
){
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
);
171 if( hasTerm( a
) && hasTerm( b
) ) {
172 if( d_equalityEngine
.areDisequal( a
, b
, false ) ){
180 Node
TheoryStrings::getLengthExp( Node t
, std::vector
< Node
>& exp
, Node te
){
181 Assert( areEqual( t
, te
) );
182 Node lt
= mkLength( te
);
184 // use own length if it exists, leads to shorter explanation
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
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
) );
199 Node
TheoryStrings::getLength( Node t
, std::vector
< Node
>& exp
) {
200 return getLengthExp( t
, exp
, t
);
203 void TheoryStrings::setMasterEqualityEngine(eq::EqualityEngine
* eq
) {
204 d_equalityEngine
.setMasterEqualityEngine(eq
);
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
;
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
;
220 if (d_equalityEngine
.areDisequal(a
, b
, false)) {
221 // The terms are implied to be dis-equal
222 return EQUALITY_FALSE
;
225 return EQUALITY_UNKNOWN
;
228 void TheoryStrings::propagate(Effort e
) {
229 // direct propagation now
232 bool TheoryStrings::propagate(TNode literal
) {
233 Debug("strings-propagate") << "TheoryStrings::propagate(" << literal
<< ")" << std::endl
;
234 // If already in conflict, no more propagation
236 Debug("strings-propagate") << "TheoryStrings::propagate(" << literal
<< "): already in conflict" << std::endl
;
240 bool ok
= d_out
->propagate(literal
);
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
);
259 d_equalityEngine
.explainPredicate(atom
, polarity
, tassumptions
);
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
] );
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
;
272 Node
TheoryStrings::explain( TNode literal
){
273 std::vector
< TNode
> assumptions
;
274 explain( literal
, assumptions
);
275 if( assumptions
.empty() ){
277 }else if( assumptions
.size()==1 ){
278 return assumptions
[0];
280 return NodeManager::currentNM()->mkNode( kind::AND
, assumptions
);
284 /////////////////////////////////////////////////////////////////////////////
286 /////////////////////////////////////////////////////////////////////////////
289 void TheoryStrings::presolve() {
290 Debug("strings-presolve") << "TheoryStrings::Presolving : get fmf options " << (options::stringFMF() ? "true" : "false") << std::endl
;
292 if(!options::stdASCII()) {
298 /////////////////////////////////////////////////////////////////////////////
300 /////////////////////////////////////////////////////////////////////////////
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
);
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
++ ) {
321 Trace("strings-model") << ", ";
323 Trace("strings-model") << col
[i
][j
];
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;
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;
341 //Trace("strings-model-warn") << "No length for eqc " << col[i][0] << std::endl;
343 lts_values
.push_back( Node::null() );
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();
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
] );
365 processed
[col
[i
][j
]] = cst
;
368 Trace("strings-model") << "have length " << lts_values
[i
] << std::endl
;
370 //assign a new length if necessary
371 if( !pure_eq
.empty() ){
372 if( lts_values
[i
].isNull() ){
374 while( values_used
.find( lvalue
)!=values_used
.end() ){
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;
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
] << " ";
385 Trace("strings-model") << std::endl
;
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() );
394 while( d_equalityEngine
.hasTerm( c
) ){
396 Assert( !sel
.isFinished() );
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 );
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)";
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
]);
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 );
434 //Trace("strings-model") << "String Model : Assigned." << std::endl;
435 Trace("strings-model") << "String Model : Finished." << std::endl
;
438 /////////////////////////////////////////////////////////////////////////////
440 /////////////////////////////////////////////////////////////////////////////
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());
457 switch( n
.getKind() ) {
459 d_equalityEngine
.addTriggerEquality(n
);
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]);
470 if( n
.getType().isString() ) {
471 registerTerm( n
, 0 );
473 if( n
.getKind() == kind::VARIABLE
&& options::stringFMF() ){
474 d_input_vars
.insert(n
);
476 } else if (n
.getType().isBoolean()) {
477 // Get triggered for both equal and dis-equal
478 d_equalityEngine
.addTriggerPredicate(n
);
480 // Function applications/predicates
481 d_equalityEngine
.addTerm(n
);
488 Node
TheoryStrings::expandDefinition(LogicRequest
&logicRequest
, Node node
) {
493 void TheoryStrings::check(Effort e
) {
494 if (done() && !fullEffort(e
)) {
498 TimerStat::CodeTimer
checkTimer(d_checkTime
);
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;
509 if( !done() && !hasTerm( d_emptyString
) ) {
510 preRegisterTerm( d_emptyString
);
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
;
520 Trace("strings-assertion") << "get assertion: " << fact
<< endl
;
521 polarity
= fact
.getKind() != kind::NOT
;
522 atom
= polarity
? fact
: fact
[0];
524 //run preprocess on memberships
525 if( options::stringLazyPreproc() ){
526 checkReduction( atom
, polarity
? 1 : -1, 0 );
529 //assert pending fact
530 assertPendingFact( atom
, polarity
, fact
);
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
;
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() );
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
) << " ";
553 Trace("strings-eqc") << " } " << std::endl
;
554 EqcInfo
* ei
= getOrMakeEqcInfo( eqc
, false );
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
;
563 Trace("strings-eqc") << std::endl
;
565 Trace("strings-eqc") << std::endl
;
568 bool addedLemma
= false;
571 Trace("strings-process") << "----check, next round---" << std::endl
;
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() ){
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
){
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() ){
586 Trace("strings-process") << "Done check lengths, addedFact = " << !d_pending
.empty() << " " << !d_lemma_cache
.empty() << ", d_conflict = " << d_conflict
<< std::endl
;
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() ){
593 Trace("strings-process") << "Done check cardinality, addedFact = " << !d_pending
.empty() << " " << !d_lemma_cache
.empty() << ", d_conflict = " << d_conflict
<< std::endl
;
601 addedFact
= !d_pending
.empty();
602 addedLemma
= !d_lemma_cache
.empty();
605 }while( !d_conflict
&& !addedLemma
&& addedFact
);
607 Trace("strings-check") << "Theory of strings done full effort check " << addedLemma
<< " " << d_conflict
<< std::endl
;
609 Trace("strings-check") << "Theory of strings, done check : " << e
<< std::endl
;
610 Assert( d_pending
.empty() );
611 Assert( d_lemma_cache
.empty() );
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
;
619 Node n
= (*it
).first
;
620 checkReduction( n
, d_extf_pol
[n
], effort
);
621 if( hasProcessed() ){
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
632 if( atom
.getKind()==kind::STRING_IN_REGEXP
){
633 if( pol
==1 && atom
[1].getKind()==kind::REGEXP_RANGE
){
636 }else if( atom
.getKind()==kind::STRING_STRCTN
){
641 if( options::stringLazyPreproc() ){
642 if( atom
.getKind()==kind::STRING_SUBSTR
){
643 r_effort
= options::stringLazyPreproc2() ? 1 : 0;
645 r_effort
= options::stringLazyPreproc2() ? 2 : 0;
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 );
660 }else if( atom
.getKind()==kind::STRING_STRCTN
){
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 );
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
);
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 );
689 TheoryStrings::EqcInfo::EqcInfo( context::Context
* c
) : d_const_term(c
), d_length_term(c
), d_cardinality_lem_k(c
), d_normalized_length(c
) {
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() ){
697 EqcInfo
* ei
= new EqcInfo( getSatContext() );
698 d_eqc_info
[eqc
] = ei
;
704 return (*eqc_i
).second
;
709 /** Conflict when merging two constants */
710 void TheoryStrings::conflict(TNode a
, TNode b
){
712 Debug("strings-conflict") << "Making conflict..." << std::endl
;
715 if (a
.getKind() == kind::CONST_BOOLEAN
) {
716 conflictNode
= explain( a
.iffNode(b
) );
718 conflictNode
= explain( a
.eqNode(b
) );
720 Trace("strings-conflict") << "CONFLICT: Eq engine conflict : " << conflictNode
<< std::endl
;
721 d_out
->conflict( conflictNode
);
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
;
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 );
741 /** called when two equivalance classes will merge */
742 void TheoryStrings::eqNotifyPreMerge(TNode t1
, TNode t2
){
743 EqcInfo
* e2
= getOrMakeEqcInfo(t2
, false);
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
);
750 if( !e2
->d_length_term
.get().isNull() ){
751 e1
->d_length_term
.set( e2
->d_length_term
);
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
);
756 if( !e2
->d_normalized_length
.get().isNull() ){
757 e1
->d_normalized_length
.set( e2
->d_normalized_length
);
761 if( hasTerm( d_zero ) ){
763 if( areEqual(d_zero, t1) ){
765 }else if( areEqual(d_zero, t2) ){
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() ){
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);
792 /** called when two equivalance classes have merged */
793 void TheoryStrings::eqNotifyPostMerge(TNode t1
, TNode t2
) {
797 /** called when two equivalance classes are disequal */
798 void TheoryStrings::eqNotifyDisequal(TNode t1
, TNode t2
, TNode reason
) {
802 void TheoryStrings::computeCareGraph(){
803 Theory::computeCareGraph();
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 );
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
;
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;
826 d_equalityEngine
.assertPredicate( atom
, polarity
, exp
);
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
;
835 void TheoryStrings::doPendingFacts() {
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
);
847 bool polarity
= fact
.getKind() != kind::NOT
;
848 TNode atom
= polarity
? fact
: fact
[0];
849 assertPendingFact(atom
, polarity
, exp
);
854 d_pending_exp
.clear();
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
] );
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
);
868 d_lemma_cache
.clear();
869 d_pending_req_phase
.clear();
872 bool TheoryStrings::hasProcessed() {
873 return d_conflict
|| !d_lemma_cache
.empty() || !d_pending
.empty();
876 void TheoryStrings::addToExplanation( Node a
, Node b
, std::vector
< Node
>& exp
) {
878 Debug("strings-explain") << "Add to explanation : " << a
<< " == " << b
<< std::endl
;
879 Assert( areEqual( a
, b
) );
880 exp
.push_back( a
.eqNode( b
) );
884 void TheoryStrings::addToExplanation( Node lit
, std::vector
< Node
>& exp
) {
886 exp
.push_back( lit
);
890 void TheoryStrings::checkInit() {
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();
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() ){
908 d_strings_eqc
.push_back( eqc
);
911 eq::EqClassIterator eqc_i
= eq::EqClassIterator( eqc
, &d_equalityEngine
);
912 while( !eqc_i
.isFinished() ) {
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];
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
);
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
) );
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]] ) );
957 sendInference( exp
, n
.eqNode( nc
), "I_Norm" );
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
;
965 d_ext_func_terms
[nc
] = d_ext_func_terms
[nc
] && (*it
).second
;
967 d_ext_func_terms
[n
] = false;
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
);
974 }else if( k
==kind::STRING_CONCAT
&& c
.size()==1 ){
975 Trace("strings-process-debug") << " congruent term by singular : " << n
<< " " << c
[0] << std::endl
;
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
) );
987 Assert( !foundNEmpty
);
989 exp
.push_back( n
[i
].eqNode( c
[0] ) );
994 AlwaysAssert( foundNEmpty
);
996 sendInference( exp
, n
.eqNode( c
[0] ), "I_Norm_S" );
998 d_congruent
.insert( n
);
1008 if( d_congruent
.find( n
)==d_congruent
.end() ){
1012 Trace("strings-process-debug") << " congruent variable : " << n
<< std::endl
;
1013 d_congruent
.insert( n
);
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
;
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() ){
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
;
1042 void TheoryStrings::checkExtendedFuncsEval( int effort
) {
1043 Trace("strings-extf-list") << "Active extended functions, effort=" << effort
<< " : " << std::endl
;
1045 d_extf_vars
.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
){
1053 Node n
= (*it
).first
;
1055 if( n
.getType().isBoolean() ){
1056 if( areEqual( n
, d_true
) ){
1058 }else if( areEqual( n
, d_false
) ){
1062 Trace("strings-extf-debug") << "Check extf " << n
<< ", pol = " << d_extf_pol
[n
] << "..." << std::endl
;
1064 std::map
< Node
, bool > visited
;
1065 collectVars( n
, d_extf_vars
[n
], visited
);
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
);
1076 if( itc
!=d_eqc_to_const
.end() ){
1077 b
= d_eqc_to_const_base
[nr
];
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
);
1088 for( unsigned i
=0; i
<itv
->second
.size(); i
++ ){
1089 if( itv
->second
[i
]!=s
){
1090 var
.push_back( itv
->second
[i
] );
1092 addToExplanation( itv
->second
[i
], b
, d_extf_exp
[n
] );
1093 Trace("strings-extf-debug") << " " << itv
->second
[i
] << " --> " << s
<< std::endl
;
1098 addToExplanation( e
, d_extf_exp
[n
] );
1104 Node nr
= n
.substitute( var
.begin(), var
.end(), sub
.begin(), sub
.end() );
1105 Node nrc
= Rewriter::rewrite( nr
);
1106 if( nrc
.isConst() ){
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
;
1122 Trace("strings-extf-debug") << " could not infer symbolic definition." << std::endl
;
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();
1132 conc
= nrs
.eqNode( nrc
);
1134 d_extf_exp
[n
].clear();
1137 if( !areEqual( n
, nrc
) ){
1138 if( n
.getType().isBoolean() ){
1139 d_extf_exp
[n
].push_back( nrc
==d_true
? n
.negate() : n
);
1142 conc
= n
.eqNode( nrc
);
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() );
1150 Trace("strings-extf-debug") << " conflict, return." << std::endl
;
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" );
1169 if( !to_reduce
.isNull() ){
1171 Trace("strings-extf") << " cannot rewrite extf : " << to_reduce
<< std::endl
;
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
];
1180 Trace("strings-extf-list") << ", from " << n
;
1182 Trace("strings-extf-list") << std::endl
;
1186 Trace("strings-extf-debug") << " already reduced " << (*it
).first
<< std::endl
;
1191 void TheoryStrings::checkExtfInference( Node n
, Node nr
, int effort
){
1192 int n_pol
= d_extf_pol
[n
];
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" );
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
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] );
1232 do_infer
= !areEqual( conc
, d_false
);
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" );
1245 Trace("strings-extf-debug") << " redundant." << std::endl
;
1246 d_ext_func_terms
[n
] = false;
1253 void TheoryStrings::collectVars( Node n
, std::map
< Node
, std::vector
< Node
> >& vars
, std::map
< Node
, bool >& visited
) {
1255 if( visited
.find( n
)==visited
.end() ){
1257 if( n
.getNumChildren()>0 ){
1258 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
1259 collectVars( n
[i
], vars
, visited
);
1262 Node nr
= getRepresentative( n
);
1263 vars
[nr
].push_back( n
);
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();
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
);
1280 return (*it
).second
;
1283 std::vector
< Node
> children
;
1284 if (n
.getMetaKind() == kind::metakind::PARAMETERIZED
) {
1285 children
.push_back( n
.getOperator() );
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
] );
1291 Node ns
= getSymbolicDefinition( n
[i
], exp
);
1293 return Node::null();
1295 children
.push_back( ns
);
1299 return NodeManager::currentNM()->mkNode( n
.getKind(), children
);
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
;
1310 Trace( tc
) << "eqc [" << eqc
<< "]";
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
;
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
];
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
);
1327 if( itc
!=d_eqc_to_const
.end() ){
1328 Trace( tc
) << itc
->second
;
1334 Trace( tc
) << ", from " << n
;
1336 Trace( tc
) << std::endl
;
1339 Trace( tc
) << std::endl
;
1342 Trace( tc
) << std::endl
;
1345 void TheoryStrings::checkFlatForms() {
1346 //first check for cycles, while building ordering of equivalence classes
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() ){
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" );
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
];
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?
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
++ ){
1384 std::vector
< Node
> inelig
;
1385 for( unsigned i
=0; i
<=start
; i
++ ){
1386 inelig
.push_back( it
->second
[start
] );
1388 Node a
= it
->second
[start
];
1391 std::vector
< Node
> exp
;
1392 //std::vector< Node > exp_n;
1395 if( count
==d_flat_form
[a
].size() ){
1396 for( unsigned i
=start
+1; i
<it
->second
.size(); i
++ ){
1398 if( std::find( inelig
.begin(), inelig
.end(), b
)==inelig
.end() ){
1399 if( count
<d_flat_form
[b
].size() ){
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
) );
1405 Assert( !conc_c
.empty() );
1406 conc
= mkAnd( conc_c
);
1409 //swap, will enforce is empty past current
1410 a
= it
->second
[i
]; b
= it
->second
[start
];
1414 inelig
.push_back( it
->second
[i
] );
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
++ ){
1425 if( std::find( inelig
.begin(), inelig
.end(), b
)==inelig
.end() ){
1426 if( count
==d_flat_form
[b
].size() ){
1427 inelig
.push_back( b
);
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
) );
1433 Assert( !conc_c
.empty() );
1434 conc
= mkAnd( conc_c
);
1440 Node cc
= d_flat_form
[b
][count
];
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
1449 Node s
= TheoryStringsRewriter::splitConstant( cc_c
, curr_c
, index
, r
==1 );
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
);
1459 }else if( (d_flat_form
[a
].size()-1)==count
&& (d_flat_form
[b
].size()-1)==count
){
1460 conc
= ac
.eqNode( bc
);
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
);
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
);
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
] );
1500 for( int j
=0; j
<jj
; j
++ ){
1501 if( areEqual( c
[j
], d_emptyString
) ){
1502 addToExplanation( c
[j
], d_emptyString
, exp
);
1506 for( int j
=(c
.getNumChildren()-1); j
>jj
; --j
){
1507 if( areEqual( c
[j
], d_emptyString
) ){
1508 addToExplanation( c
[j
], d_emptyString
, exp
);
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" ) ) );
1524 }while( inelig
.size()<it
->second
.size() );
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() );
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
;
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() ){
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() ) {
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
);
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();
1570 if( nr
!=d_emptyString_r
){
1571 d_flat_form
[n
].push_back( nr
);
1572 d_flat_form_index
[n
].push_back( i
);
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
);
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();
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)
1596 if( hasProcessed() ){
1597 return Node::null();
1607 //now we can add it to the list of equivalence classes
1608 d_strings_eqc
.push_back( eqc
);
1612 return Node::null();
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() ) {
1623 if( d_congruent
.find( n
)==d_congruent
.end() ){
1624 registerTerm( n
, 2 );
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() ){
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" );
1655 nf_to_eqc
[nf_term
] = eqc
;
1656 eqc_to_exp
[eqc
] = mkAnd( nf_exp
);
1659 Trace("strings-process-debug") << "Done verifying normal forms are the same for " << eqc
<< std::endl
;
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
;
1667 Trace("strings-nf") << std::endl
;
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() ){
1675 if( hasProcessed() ){
1679 //process disequalities between equivalence classes
1681 Trace("strings-process-debug") << "Done check disequalities, addedFact = " << !d_pending
.empty() << " " << !d_lemma_cache
.empty() << ", d_conflict = " << d_conflict
<< std::endl
;
1684 Trace("strings-solve") << "Finished check normal forms, #lemmas = " << d_lemma_cache
.size() << ", conflict = " << d_conflict
<< std::endl
;
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
) );
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();
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
;
1717 result
= getNormalForms(eqc
, normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
);
1718 if( hasProcessed() ){
1721 if( processNEqc(normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
) ){
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
;
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];
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
);
1743 //just take the first normal form
1744 Trace("strings-solve-debug2") << "take the first normal form" << std::endl
;
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] ) );
1751 Trace("strings-solve-debug2") << "take normal form ... done" << std::endl
;
1752 d_normal_forms_base
[eqc
] = normal_form_src
[nf_index
];
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];
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() );
1766 Trace("strings-process-debug") << "Return process equivalence class " << eqc
<< " : returned, size = " << nf
.size() << std::endl
;
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() );
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() ){
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
);
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
] << " ";
1809 Trace("strings-error") << std::endl
;
1812 Assert( d_normal_forms
[nr
][r
].getKind()!=kind::STRING_CONCAT
);
1814 nf_n
.insert( nf_n
.end(), d_normal_forms
[nr
].begin(), d_normal_forms
[nr
].end() );
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
);
1821 for( unsigned k
=0; k
<2; k
++ ){
1822 int prev_dep
= d_normal_forms_exp_depend
[nr
][exp
][k
==1];
1824 nf_exp_depend_n
[exp
][false] = orig_size
+ prev_dep
;
1826 //store forward index (converted back to reverse index below)
1827 nf_exp_depend_n
[exp
][true] = orig_size
+ ( add_size
- prev_dep
);
1832 Node eq
= n
[i
].eqNode( nr
);
1833 nf_exp_n
.push_back( eq
);
1835 nf_exp_depend_n
[eq
][false] = orig_size
;
1836 nf_exp_depend_n
[eq
][true] = orig_size
+ add_size
;
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 );
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
;
1855 Assert( !areEqual( nf_n
[i
], n
) );
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
);
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 ) );
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" );
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
++ ) {
1890 Trace("strings-solve") << ", ";
1892 Trace("strings-solve") << normal_forms
[i
][j
];
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";
1899 for( unsigned j
=0; j
<normal_forms_exp
[i
].size(); j
++ ) {
1901 Trace("strings-solve") << " AND ";
1903 Trace("strings-solve") << normal_forms_exp
[i
][j
];
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
;
1913 Trace("strings-solve") << std::endl
;
1917 Trace("strings-solve") << "--- Single normal form for equivalence class " << eqc
<< std::endl
;
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() );
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
];
1937 curr_exp
.push_back( exp
);
1938 Trace("strings-explain-prefix-debug") << " include : " << exp
<< std::endl
;
1940 Trace("strings-explain-prefix-debug") << " exclude : " << exp
<< std::endl
;
1944 Trace("strings-explain-prefix") << "Included " << curr_exp
.size() << " / " << ( normal_forms_exp
[i
].size() + normal_forms_exp
[j
].size() ) << std::endl
;
1946 if( normal_form_src
[i
]!=normal_form_src
[j
] ){
1947 curr_exp
.push_back( normal_form_src
[i
].eqNode( normal_form_src
[j
] ) );
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
;
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
) ){
1968 //ensure that normal_forms[i] and normal_forms[j] are the same modulo equality
1973 if( processSimpleNEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, index
, false ) ){
1974 //added a lemma, return
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() );
1983 //addNormalFormPair( normal_form_src[i], normal_form_src[j] );
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;
2000 Trace("strings-solve-debug") << "Non-simple Case 2 : must compare strings" << std::endl
;
2003 if( detectLoop(normal_forms
, i
, j
, index
, loop_in_i
, loop_in_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
;
2012 getExplanationVectorForPrefix( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, -1, false, c_lb_exp
);
2014 if(options::stringLB() == 0) {
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
)) {
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)" );
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
);
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
;
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
;
2061 conc
= Rewriter::rewrite( conc
);
2062 sendInference( antec
, conc
, "S-Split(CST-P)", true );
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
);
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
);
2073 antec_new_lits
.push_back(ldeq
);
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
);
2083 antec_new_lits
.push_back( xgtz
);
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() ){
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
);
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
);
2105 if( conc
.isNull() ){
2106 conc
= Rewriter::rewrite(NodeManager::currentNM()->mkNode( kind::OR
, eq1
, eq2
));
2110 sendInference( antec
, antec_new_lits
, conc
, "S-Split(VAR)", true );
2111 //++(d_statistics.d_eq_splits);
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
)) {
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() );
2141 bool ret
= processSimpleNEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, index
, true );
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() );
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
) {
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() ) {
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" );
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
;
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" );
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
;
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
++ ) {
2213 eqnc
.insert(eqnc
.begin(), normal_forms
[k
][index_l
] );
2215 eqnc
.push_back( normal_forms
[k
][index_l
] );
2218 eqn
.push_back( mkConcat( eqnc
) );
2220 if( !areEqual( eqn
[0], eqn
[1] ) ) {
2221 conc
= eqn
[0].eqNode( eqn
[1] );
2222 sendInference( antec
, conc
, "ENDPOINT", true );
2225 Assert( normal_forms
[i
].size()==normal_forms
[j
].size() );
2226 index
= normal_forms
[i
].size();
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
);
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
;
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
);
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
);
2249 normal_forms
[l
][index
] = normal_forms
[k
][index
];
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 );
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
] ){
2283 if( has_loop
[0]!=-1 || has_loop
[1]!=-1 ) {
2284 loop_in_i
= has_loop
[0];
2285 loop_in_j
= has_loop
[1];
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
;
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
;
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
] );
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
] );
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
] );
2328 Node r
= mkConcat( vec_r
);
2329 Trace("strings-loop") << " (" << r
<< ")" << std::endl
;
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
) {
2336 if(s_zy
.getConst
<String
>().tailcmp( r
.getConst
<String
>(), c
) ) {
2338 s_zy
= NodeManager::currentNM()->mkConst( s_zy
.getConst
<String
>().substr(0, c
) );
2341 Trace("strings-loop") << "Strings::Loop: Refactor S(Z.Y)= " << s_zy
<< ", c=" << c
<< std::endl
;
2346 Trace("strings-loop") << "Strings::Loop: tails are different." << std::endl
;
2347 sendInference( antec
, conc
, "Loop Conflict", true );
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)" );
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() );
2365 Node ant
= mkExplain( antec
);
2366 if(d_loop_antec
.find(ant
) == d_loop_antec
.end()) {
2367 d_loop_antec
.insert(ant
);
2371 r
== d_emptyString
&&
2373 s_zy
.getConst
<String
>().isRepeated()
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
;
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
) ) );
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
));
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
) ));
2400 cc
= Rewriter::rewrite(s_zy
.eqNode( mkConcat( z
, y
) ));
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
);
2414 conc
= vconc
.size()==0 ? Node::null() : vconc
.size()==1 ? vconc
[0] : NodeManager::currentNM()->mkNode(kind::OR
, vconc
);
2416 Trace("strings-loop") << "Strings::Loop: Normal Loop Breaking." << std::endl
;
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
) ) );
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
);
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
;
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
);
2450 d_out
->setIncomplete();
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
] );
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() );
2474 int revRet
= processReverseDeq( nfi
, nfj
, ni
, nj
);
2480 nfi
.insert( nfi
.end(), d_normal_forms
[ni
].begin(), d_normal_forms
[ni
].end() );
2482 nfj
.insert( nfj
.end(), d_normal_forms
[nj
].begin(), d_normal_forms
[nj
].end() );
2485 while( index
<nfi
.size() || index
<nfj
.size() ){
2486 int ret
= processSimpleDeq( nfi
, nfj
, ni
, nj
, index
, false );
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 ){
2502 Trace("strings-solve") << "Non-Simple Case 1 : add lemma " << std::endl
;
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() );
2509 if( areDisequal( ni
, nj
) ){
2510 antec
.push_back( ni
.eqNode( nj
).negate() );
2512 antec_new_lits
.push_back( ni
.eqNode( nj
).negate() );
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
);
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;
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;
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() );
2560 int ret
= processSimpleDeq( nfi
, nfj
, ni
, nj
, index
, true );
2562 //reverse normal form of i, j
2563 std::reverse( nfi
.begin(), nfi
.end() );
2564 std::reverse( nfj
.begin(), nfj
.end() );
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
) );
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);
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
);
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
;
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
;
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
;
2611 if( i
.getConst
<String
>().size() < j
.getConst
<String
>().size() ) {
2612 nfj
.insert( nfj
.begin() + index
+ 1, remainderStr
);
2613 nfj
[index
] = nfi
[index
];
2615 nfi
.insert( nfi
.begin() + index
+ 1, remainderStr
);
2616 nfi
[index
] = nfj
[index
];
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
2640 void TheoryStrings::addNormalFormPair( Node n1
, Node n2
){
2641 if( !isNormalFormPair( n1
, n2
) ){
2642 //Assert( !isNormalFormPair( n1, n2 ) );
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
);
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
;
2655 lst
= (*nf_i
).second
;
2657 Trace("strings-nf") << "Add normal form pair : " << n1
<< " " << n2
<< std::endl
;
2658 lst
->push_back( n2
);
2659 Assert( isNormalFormPair( n1
, n2
) );
2661 Trace("strings-nf-debug") << "Already a normal form pair " << n1
<< " " << n2
<< std::endl
;
2665 bool TheoryStrings::isNormalFormPair( Node n1
, Node n2
) {
2666 //TODO: modulo equality?
2667 return isNormalFormPair2( n1
, n2
) || isNormalFormPair2( n2
, n1
);
2670 bool TheoryStrings::isNormalFormPair2( Node n1
, Node n2
) {
2671 //Trace("strings-debug") << "is normal form pair. " << n1 << " " << n2 << std::endl;
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
) {
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;
2695 do_register
= effort
>0 || n
.getKind()!=kind::STRING_CONCAT
;
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
);
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
;
2719 Node skl
= NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, sk
);
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
]] );
2728 Node lni
= NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, n
[i
] );
2729 node_vec
.push_back(lni
);
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() ) );
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
;
2745 AlwaysAssert(false, "String Terms only in registerTerm.");
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
);
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
;
2759 for( unsigned i
=0; i
<exp_n
.size(); i
++ ){
2760 Trace("strings-infer-debug") << " N:" << exp_n
[i
] << std::endl
;
2762 //Trace("strings-infer-debug") << "as lemma : " << asLemma << std::endl;
2764 bool doSendLemma
= ( asLemma
|| eq
==d_false
|| eq
.getKind()==kind::OR
|| options::stringInferAsLemmas() );
2767 if( options::stringRExplainLemmas() ){
2768 eq_exp
= mkExplain( exp
, exp_n
);
2771 eq_exp
= mkAnd( exp_n
);
2772 }else if( exp_n
.empty() ){
2773 eq_exp
= mkAnd( exp
);
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
);
2781 sendLemma( eq_exp
, eq
, c
);
2783 Assert( exp_n
.empty() );
2784 sendInfer( mkAnd( exp
), eq
, c
);
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
);
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
;
2803 if( ant
== d_true
) {
2806 lem
= NodeManager::currentNM()->mkNode( kind::IMPLIES
, ant
, conc
);
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
);
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
;
2827 sendLemma( d_true
, eqs
, c
);
2830 for( unsigned i
=0; i
<unproc
.size(); i
++ ){
2831 Trace("strings-lemma-debug") << " non-trivial exp : " << unproc
[i
] << std::endl
;
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
);
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
);
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 );
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
);
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
);
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
){
2890 for( unsigned i
=0; i
<2; i
++ ){
2892 if( ns
[i
].getAttribute(StringsProxyVarAttribute()) ){
2894 }else if( ns
[i
].isConst() ){
2895 NodeNodeMap::const_iterator it
= d_proxy_var
.find( ns
[i
] );
2896 if( it
!=d_proxy_var
.end() ){
2902 if( v
.getNumChildren()==0 ){
2906 //both sides involved in proxy var
2917 subs
.push_back( s
);
2918 vars
.push_back( v
);
2926 unproc
.push_back( n
);
2931 Node
TheoryStrings::mkConcat( Node n1
, Node n2
) {
2932 return Rewriter::rewrite( NodeManager::currentNM()->mkNode( kind::STRING_CONCAT
, n1
, n2
) );
2935 Node
TheoryStrings::mkConcat( Node n1
, Node n2
, Node n3
) {
2936 return Rewriter::rewrite( NodeManager::currentNM()->mkNode( kind::STRING_CONCAT
, n1
, n2
, n3
) );
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
) );
2943 Node
TheoryStrings::mkLength( Node t
) {
2944 return Rewriter::rewrite( NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, t
) );
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
;
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
);
2978 Node
TheoryStrings::mkExplain( std::vector
< Node
>& a
) {
2979 std::vector
< Node
> an
;
2980 return mkExplain( a
, an
);
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
) {
2988 Debug("strings-explain") << "Ask for explanation of " << a
[i
] << std::endl
;
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] ){
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
] );
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
;
3014 Debug("strings-explain") << std::endl
;
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
]);
3025 if( antec_exp
.empty() ) {
3027 } else if( antec_exp
.size()==1 ) {
3030 ant
= NodeManager::currentNM()->mkNode( kind::AND
, antec_exp
);
3032 ant
= Rewriter::rewrite( ant
);
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
] );
3045 } else if( au
.size() == 1 ) {
3048 return NodeManager::currentNM()->mkNode( kind::AND
, au
);
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
] );
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 );
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
] ) ){
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
);
3118 ei
->d_normalized_length
.set( eq
);
3119 sendInference( ant
, eq
, "LEN-NORM", true );
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 );
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 );
3142 // Trace("strings-process-debug") << "Do not process length constraints for " << nodes[i] << " " << d_normal_forms[nodes[i]].size() << std::endl;
3148 void TheoryStrings::checkCardinality() {
3149 //int cardinality = options::stringCharCardinality();
3150 //Trace("strings-solve-debug2") << "get cardinality: " << cardinality << endl;
3152 std::vector
< std::vector
< Node
> > cols
;
3153 std::vector
< Node
> lts
;
3154 separateByLength( d_strings_eqc
, cols
, lts
);
3156 for( unsigned i
= 0; i
<cols
.size(); ++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 ) {
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
;
3167 Node cmp
= NodeManager::currentNM()->mkNode( kind::GEQ
, lr
, NodeManager::currentNM()->mkConst( Rational( card_need
) ) );
3168 cmp
= Rewriter::rewrite( cmp
);
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;
3179 sendSplit( *itr1
, *itr2
, "CARD-SP" );
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
);
3197 Node len_eq_lr
= len
.eqNode(lr
);
3198 vec_node
.push_back( len_eq_lr
);
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 );
3206 sendInference( d_empty_vec
, vec_node
, cons
, "CARDINALITY", true );
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
);
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
);
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() ) {
3243 exp
.push_back( NodeManager::currentNM()->mkNode( kind::EQUAL
, n
, nc
) );
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 );
3251 exp
.push_back( NodeManager::currentNM()->mkNode( kind::EQUAL
, n
, nr
) );
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
);
3258 exp
.insert( exp
.end(), d_normal_forms_exp
[nr
].begin(), d_normal_forms_exp
[nr
].end() );
3261 Trace("strings-ind-nf") << "The final normal form of " << n
<< " is " << nf
<< std::endl
;
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
++ ) {
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
;
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
;
3288 eqc_to_strings
[ eqc_to_leqc
[r
] ].push_back( eqc
);
3290 eqc_to_strings
[leqc_counter
].push_back( eqc
);
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
] );
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
) << " ++ ";
3311 void TheoryStrings::updateMpl( Node n, int b ) {
3312 if(d_mpl.find(n) == d_mpl.end()) {
3313 //d_curr_cardinality.get();
3315 } else if(b < d_mpl[n]) {
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
) );
3326 Node n
= d_regexp_ant
[atom
];
3327 return Rewriter::rewrite( NodeManager::currentNM()->mkNode(kind::AND
, ant
, n
) );
3331 Node
TheoryStrings::normalizeRegexp(Node r
) {
3333 if(d_nf_regexps
.find(r
) != d_nf_regexps
.end()) {
3334 nf_r
= d_nf_regexps
[r
];
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
: {
3343 case kind::STRING_TO_REGEXP
: {
3344 if(r
[0].isConst()) {
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
) );
3355 case kind::REGEXP_CONCAT
:
3356 case kind::REGEXP_UNION
:
3357 case kind::REGEXP_INTER
: {
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
);
3368 Node rtmp
= vec_nodes
.size()==1 ? vec_nodes
[0] : NodeManager::currentNM()->mkNode(r
.getKind(), vec_nodes
);
3369 nf_r
= Rewriter::rewrite( rtmp
);
3372 case kind::REGEXP_STAR
: {
3373 Node rtmp
= normalizeRegexp(r
[0]);
3375 rtmp
= NodeManager::currentNM()->mkNode(kind::REGEXP_STAR
, rtmp
);
3376 nf_r
= Rewriter::rewrite( rtmp
);
3384 d_nf_regexps
[r
] = nf_r
;
3385 d_nf_regexps_exp
[r
] = nf_exp
;
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;
3396 Trace("regexp-check") << "Normalizing Positive Memberships ... " << std::endl
;
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
;
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;
3411 Trace("regexp-nf") << "Checking Memberships for N(" << x
<< ") = " << nf_x
<< " :" << std::endl
;
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
) {
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
)) {
3425 Trace("regexp-nf") << "Handling Symbolic Regexp for N(" << r
<< ") = " << nf_r
<< std::endl
;
3426 //TODO: handle symbolic ones
3429 d_processed_memberships
.insert(memb
);
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
;
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());
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];
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
++) {
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
);
3465 bool spflag
= false;
3466 inter_r
= d_regexp_opr
.intersect(inter_r
, inter_r2
, spflag
);
3467 if(inter_r
== d_emptyRegexp
) {
3470 sendInference( d_empty_vec
, exp
, conc
, "INTERSECT CONFLICT", true );
3477 memb
= Rewriter::rewrite( NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP
, nf_x
, inter_r
) );
3478 memb_with_exps
[memb
] = exp
;
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
) );
3491 if( !s
.isEmptyString() ) {
3494 for(unsigned i
=0; i
<s
.size(); ++i
) {
3495 CVC4::String c
= s
.substr(i
, 1);
3497 int rt
= d_regexp_opr
.derivativeS(dc
, c
, dc2
);
3501 } else if(rt
== 2) {
3511 Node
TheoryStrings::applyRSplit(Node s1
, Node s2
, Node r
) {
3512 Assert(d_regexp_opr
.checkConstRegExp(r
));
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
;
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
);
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
) );
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.
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
;
3546 memb
= Rewriter::rewrite( memb
);
3547 if(memb
== d_false
) {
3549 sendInference(d_empty_vec
, itr
->second
, conc
, "MEMBERSHIP CONFLICT", true);
3553 Assert(memb
== d_true
);
3555 } else if(s
.getKind() == kind::VARIABLE
) {
3557 XinR_with_exps
[itr
->first
] = itr
->second
;
3559 Assert(s
.getKind() == kind::STRING_CONCAT
);
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);
3571 //R-Split, see Tianyi's thesis
3572 if(i
== s
.getNumChildren() - 1) {
3574 Node memb2
= NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP
, s
[i
], r
);
3575 XinR_with_exps
[itr
->first
] = itr
->second
;
3578 std::vector
< Node
> vec_s2
;
3579 for( unsigned j
=i
+1; j
<s
.getNumChildren(); j
++ ) {
3580 vec_s2
.push_back(s
[j
]);
3582 Node s2
= mkConcat(vec_s2
);
3583 conc
= applyRSplit(s1
, s2
, r
);
3584 if(conc
== d_true
) {
3586 } else if(conc
.isNull() || conc
== d_false
) {
3587 conc
= Node::null();
3588 sendInference(d_empty_vec
, itr
->second
, conc
, "R-Split Conflict", true);
3592 sendInference(d_empty_vec
, itr
->second
, conc
, "R-Split", true);
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
;
3611 addedLemma
= normalizePosMemberships( memb_with_exps
);
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
;
3624 if(r.getKind() == kind::REGEXP_STAR) {
3626 addedLemma = applyRLen(XinR_with_exps);
3632 Assert(false); //TODO:tmp
3639 void TheoryStrings::checkMemberships() {
3640 bool addedLemma
= false;
3641 bool changed
= false;
3642 std::vector
< Node
> processed
;
3643 std::vector
< Node
> cprocessed
;
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;
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) {
3665 if(d_inter_cache
.find(x
) != d_inter_cache
.end()) {
3666 r
= d_inter_cache
[x
];
3672 NodeList::const_iterator itr_lst
= lst
->begin();
3673 for(int i
=0; i
<cur_inter_idx
; i
++) {
3676 Trace("regexp-debug") << "... staring from : " << cur_inter_idx
<< ", we have " << lst
->size() << std::endl
;
3677 for(;itr_lst
!= lst
->end(); ++itr_lst
) {
3679 r
= d_regexp_opr
.intersect(r
, r2
, spflag
);
3682 } else if(r
== d_emptyRegexp
) {
3683 std::vector
< Node
> vec_nodes
;
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
);
3691 sendInference(vec_nodes
, conc
, "INTERSECT CONFLICT", true);
3700 if(!d_conflict
&& !spflag
) {
3701 d_inter_cache
[x
] = r
;
3702 d_inter_index
[x
] = (int)lst
->size();
3709 Trace("regexp-debug") << "... No Intersect Conflict in Memberships, addedLemma: " << addedLemma
<< std::endl
;
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
;
3722 std::vector
< Node
> rnfexp
;
3724 if(options::stringOpt1()) {
3726 x
= getNormalString( x
, rnfexp
);
3729 if(!d_regexp_opr
.checkConstRegExp(r
)) {
3730 r
= getNormalSymRegExp(r
, rnfexp
);
3733 Trace("strings-regexp-nf") << "Term " << atom
<< " is normalized to " << x
<< " IN " << r
<< std::endl
;
3735 Node tmp
= Rewriter::rewrite( NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP
, x
, r
) );
3740 d_regexp_ccached
.insert(assertion
);
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");
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
]);
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
);
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
) {
3777 cprocessed
.push_back( assertion
);
3779 processed
.push_back( assertion
);
3782 sendLemma(antec
, conc
, "RegExp-CST-SP");
3789 if(! options::stringExp()) {
3790 throw LogicException("Strings Incomplete (due to Negative Membership) by default, try --strings-exp option.");
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() );
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
;
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) {
3808 tmp = tmp==d_true? d_false : d_true;
3810 nvec.push_back( tmp );
3815 d_regexp_opr
.simplify(atom
, nvec
, polarity
);
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
;
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" );
3834 cprocessed
.push_back( assertion
);
3836 processed
.push_back( assertion
);
3838 //d_regexp_ucached[assertion] = true;
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
] << " ";
3844 Trace("strings-regexp") << ", polarity = " << polarity
<< std::endl
;
3845 //otherwise, distribute unrolling over parts
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
);
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() );
3860 antec
.push_back( x
.eqNode( xr
) );
3862 antecn
.push_back( assertion
);
3863 Node ant
= mkExplain( antec
, antecn
);
3864 Trace("strings-regexp-debug") << "Construct conclusion..." << std::endl
;
3867 if( d_normal_forms
[xr
].size()==0 ){
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
);
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;
3895 if( d_normal_forms
[xr
].size()==0 ){
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();
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
);
3926 ant
= mkRegExpAntec(assertion
, ant
);
3927 sendLemma(ant
, conc
, "REGEXP CSTAR");
3929 if( conc
==d_false
){
3930 d_regexp_ccached
.insert( assertion
);
3932 cprocessed
.push_back( assertion
);
3935 d_regexp_ccached
.insert(assertion
);
3947 for( unsigned i
=0; i
<processed
.size(); i
++ ) {
3948 d_regexp_ucached
.insert(processed
[i
]);
3950 for( unsigned i
=0; i
<cprocessed
.size(); i
++ ) {
3951 d_regexp_ccached
.insert(cprocessed
[i
]);
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;
3964 Trace("strings-regexp") << "Membership length is already added." << std::endl;
3967 Node antnf
= mkExplain(nf_exp
);
3969 if(areEqual(x
, d_emptyString
)) {
3971 switch(d_regexp_opr
.delta(r
, exp
)) {
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");
3977 d_regexp_ccached
.insert(atom
);
3981 d_regexp_ccached
.insert(atom
);
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");
3990 d_regexp_ccached
.insert(atom
);
3998 /*Node xr = getRepresentative( x );
4000 Node n = NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP, xr, r);
4001 Node nn = Rewriter::rewrite( n );
4003 d_regexp_ccached.insert(atom);
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");
4010 d_regexp_ccached.insert(atom);
4014 Node sREant
= mkRegExpAntec(atom
, d_true
);
4015 sREant
= Rewriter::rewrite(NodeManager::currentNM()->mkNode(kind::AND
, sREant
, antnf
));
4016 if(deriveRegExp( x
, r
, sREant
)) {
4018 processed
.push_back( atom
);
4025 void TheoryStrings::checkConstantEquivalenceClasses( TermIndex
* ti
, std::vector
< Node
>& vecc
) {
4026 Node n
= ti
->d_data
;
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
] << " ";
4036 Trace("strings-debug") << std::endl
;
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
);
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
] );
4053 addToExplanation( n
[count
], vecc
[countc
], exp
);
4059 //exp contains an explanation of n==c
4060 Assert( countc
==vecc
.size() );
4062 sendInference( exp
, n
.eqNode( c
), "I_CONST_MERGE" );
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
){
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
);
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
);
4083 sendInference( exp
, d_false
, "I_CONST_CONFLICT" );
4086 Trace("strings-debug") << "Duplicate constant." << std::endl
;
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
);
4097 if( hasProcessed() ){
4104 void TheoryStrings::checkExtendedFuncs() {
4105 if( options::stringExp() ){
4106 checkExtfReduction( 2 );
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
){
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
);
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
);
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() );
4139 Trace("strings-process") << "Checking memberships..." << std::endl
;
4141 Trace("strings-process") << "Done check memberships, addedLemma = " << !d_pending
.empty() << " " << !d_lemma_cache
.empty() << ", d_conflict = " << d_conflict
<< std::endl
;
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] ) );
4155 if(options::stringExp()) {
4156 for( unsigned i
=0; i
<negContains
.size(); i
++ ){
4157 Node atom
= negContains
[i
];
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 );
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" );
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
);
4190 Node b2v
= NodeManager::currentNM()->mkNode(kind::BOUND_VAR_LIST
, b2
);//, s1, s3, s4, s6);
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
);
4198 cc
= s2
.eqNode(s5
).negate();
4199 vec_nodes
.push_back(cc
);
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
) );
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;
4217 if( !negContains
.empty() ){
4218 throw LogicException("Strings Incomplete (due to Negative Contain) by default, try --strings-exp option.");
4223 CVC4::String
TheoryStrings::getHeadConst( Node x
) {
4225 return x
.getConst
< String
>();
4226 } else if( x
.getKind() == kind::STRING_CONCAT
) {
4227 if( x
[0].isConst() ) {
4228 return x
[0].getConst
< String
>();
4230 return d_emptyString
.getConst
< String
>();
4233 return d_emptyString
.getConst
< String
>();
4237 bool TheoryStrings::addMembershipLength(Node atom
) {
4241 /*std::vector< int > co;
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;
4248 co.push_back( cols[k].size() );
4252 for(unsigned k=1; k<co.size(); ++k) {
4253 g_co = gcd(g_co, co[k]);
4258 bool TheoryStrings::deriveRegExp( Node x
, Node r
, Node ant
) {
4260 Assert(x
!= d_emptyString
);
4261 Trace("regexp-derive") << "TheoryStrings::deriveRegExp: x=" << x
<< ", r= " << r
<< std::endl
;
4263 // Node n = NodeManager::currentNM()->mkNode( kind::STRING_IN_REGEXP, x, r );
4264 // Node r = Rewriter::rewrite( n );
4266 // sendLemma(ant, r, "REGEXP REWRITE");
4270 CVC4::String s
= getHeadConst( x
);
4271 if( !s
.isEmptyString() && d_regexp_opr
.checkConstRegExp( r
) ) {
4272 Node conc
= Node::null();
4275 for(unsigned i
=0; i
<s
.size(); ++i
) {
4276 CVC4::String c
= s
.substr(i
, 1);
4278 int rt
= d_regexp_opr
.derivativeS(dc
, c
, dc2
);
4282 } else if(rt
== 2) {
4291 Assert(false, "Impossible: TheoryStrings::deriveRegExp: const string in const regular expression.");
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
] );
4299 Node left
= mkConcat( vec_nodes
);
4300 left
= Rewriter::rewrite( left
);
4301 conc
= NodeManager::currentNM()->mkNode( kind::STRING_IN_REGEXP
, left
, dc
);
4303 /*std::vector< Node > sdc;
4304 d_regexp_opr.simplify(conc, sdc, true);
4305 if(sdc.size() == 1) {
4308 conc = Rewriter::rewrite(NodeManager::currentNM()->mkNode(kind::AND, conc));
4312 sendLemma(ant
, conc
, "RegExp-Derive");
4319 void TheoryStrings::addMembership(Node assertion
) {
4320 bool polarity
= assertion
.getKind() != kind::NOT
;
4321 TNode atom
= polarity
? assertion
: assertion
[0];
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
);
4332 lst
= (*itr_xr
).second
;
4335 for( NodeList::const_iterator itr
= lst
->begin(); itr
!= lst
->end(); ++itr
) {
4340 lst
->push_back( r
);
4341 } else if(!options::stringIgnNegMembership()) {
4342 /*if(options::stringEIT() && d_regexp_opr.checkConstRegExp(r)) {
4344 Node r2 = d_regexp_opr.complement(r, rt);
4345 Node a = NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP, x, r2);
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
);
4354 lst
= (*itr_xr
).second
;
4357 for( NodeList::const_iterator itr
= lst
->begin(); itr
!= lst
->end(); ++itr
) {
4362 lst
->push_back( r
);
4365 if(polarity
|| !options::stringIgnNegMembership()) {
4366 d_regexp_memberships
.push_back( assertion
);
4370 Node
TheoryStrings::getNormalString( Node x
, std::vector
<Node
> &nf_exp
){
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
;
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
);
4386 return mkConcat( vec_nodes
);
4393 Node
TheoryStrings::getNormalSymRegExp(Node r
, std::vector
<Node
> &nf_exp
) {
4395 switch( r
.getKind() ) {
4396 case kind::REGEXP_EMPTY
:
4397 case kind::REGEXP_SIGMA
:
4399 case kind::STRING_TO_REGEXP
: {
4400 if(!r
[0].isConst()) {
4401 Node tmp
= getNormalString( r
[0], nf_exp
);
4403 ret
= NodeManager::currentNM()->mkNode(kind::STRING_TO_REGEXP
, tmp
);
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
) );
4413 ret
= mkConcat(vec_nodes
);
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
) );
4421 ret
= Rewriter::rewrite( NodeManager::currentNM()->mkNode(kind::REGEXP_UNION
, vec_nodes
) );
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
) );
4429 ret
= Rewriter::rewrite( NodeManager::currentNM()->mkNode(kind::REGEXP_INTER
, vec_nodes
) );
4432 case kind::REGEXP_STAR
: {
4433 ret
= getNormalSymRegExp( r
[0], nf_exp
);
4434 ret
= Rewriter::rewrite( NodeManager::currentNM()->mkNode(kind::REGEXP_STAR
, ret
) );
4437 //case kind::REGEXP_PLUS:
4438 //case kind::REGEXP_OPT:
4439 //case kind::REGEXP_RANGE:
4441 Trace("strings-error") << "Unsupported term: " << r
<< " in normalization SymRegExp." << std::endl
;
4443 //return Node::null();
4450 //// Finite Model Finding
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
) );
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
);
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() ){
4476 Node cnode
= d_cardinality_lits
[ d_curr_cardinality
.get() ];
4477 if( d_valuation
.hasSatValue( cnode
, 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
;
4484 Trace("strings-fmf-debug") << "Has true SAT value, do not decide." << std::endl
;
4487 Trace("strings-fmf-debug") << "No SAT value, decide." << std::endl
;
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 );
4500 Node lit
= d_cardinality_lits
[ decideCard
];
4501 Trace("strings-fmf") << "Strings::FMF: Decide positive on " << lit
<< std::endl
;
4507 return Node::null();
4510 void TheoryStrings::collectExtendedFuncTerms( Node n
, std::map
< Node
, bool >& visited
) {
4511 if( visited
.find( n
)==visited
.end() ){
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;
4522 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
4523 collectExtendedFuncTerms( n
[i
], visited
);
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)
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
);
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
);
4551 }/* CVC4::theory::strings namespace */
4552 }/* CVC4::theory namespace */
4553 }/* CVC4 namespace */