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"
31 #include "theory/quantifiers/term_database.h"
34 using namespace CVC4::context
;
40 Node
TheoryStrings::TermIndex::add( TNode n
, unsigned index
, TheoryStrings
* t
, Node er
, std::vector
< Node
>& c
) {
41 if( index
==n
.getNumChildren() ){
42 if( d_data
.isNull() ){
47 Assert( index
<n
.getNumChildren() );
48 TNode nir
= t
->getRepresentative( n
[index
] );
49 //if it is empty, and doing CONCAT, ignore
50 if( nir
==er
&& n
.getKind()==kind::STRING_CONCAT
){
51 return add( n
, index
+1, t
, er
, c
);
54 return d_children
[nir
].add( n
, index
+1, t
, er
, c
);
60 TheoryStrings::TheoryStrings(context::Context
* c
, context::UserContext
* u
,
61 OutputChannel
& out
, Valuation valuation
,
62 const LogicInfo
& logicInfo
)
63 : Theory(THEORY_STRINGS
, c
, u
, out
, valuation
, logicInfo
),
66 d_equalityEngine(d_notify
, c
, "theory::strings::TheoryStrings", true),
72 d_pregistered_terms_cache(u
),
73 d_registered_terms_cache(u
),
74 d_length_lemma_terms_cache(u
),
75 d_skolem_ne_reg_cache(u
),
78 d_extf_infer_cache(c
),
79 d_extf_infer_cache_u(u
),
80 d_ee_disequalities(c
),
83 d_proxy_var_to_length(u
),
85 d_has_extf(c
, false ),
86 d_regexp_memberships(c
),
93 d_processed_memberships(c
),
97 d_cardinality_lits(u
),
98 d_curr_cardinality(c
, 0)
100 d_extt
= new ExtTheory( this );
101 d_extt
->addFunctionKind( kind::STRING_SUBSTR
);
102 d_extt
->addFunctionKind( kind::STRING_STRIDOF
);
103 d_extt
->addFunctionKind( kind::STRING_ITOS
);
104 d_extt
->addFunctionKind( kind::STRING_U16TOS
);
105 d_extt
->addFunctionKind( kind::STRING_U32TOS
);
106 d_extt
->addFunctionKind( kind::STRING_STOI
);
107 d_extt
->addFunctionKind( kind::STRING_STOU16
);
108 d_extt
->addFunctionKind( kind::STRING_STOU32
);
109 d_extt
->addFunctionKind( kind::STRING_STRREPL
);
110 d_extt
->addFunctionKind( kind::STRING_STRCTN
);
111 d_extt
->addFunctionKind( kind::STRING_IN_REGEXP
);
113 // The kinds we are treating as function application in congruence
114 d_equalityEngine
.addFunctionKind(kind::STRING_IN_REGEXP
);
115 d_equalityEngine
.addFunctionKind(kind::STRING_LENGTH
);
116 d_equalityEngine
.addFunctionKind(kind::STRING_CONCAT
);
117 if( options::stringLazyPreproc() ){
118 d_equalityEngine
.addFunctionKind(kind::STRING_STRCTN
);
119 d_equalityEngine
.addFunctionKind(kind::STRING_SUBSTR
);
120 d_equalityEngine
.addFunctionKind(kind::STRING_ITOS
);
121 d_equalityEngine
.addFunctionKind(kind::STRING_STOI
);
122 d_equalityEngine
.addFunctionKind(kind::STRING_U16TOS
);
123 d_equalityEngine
.addFunctionKind(kind::STRING_STOU16
);
124 d_equalityEngine
.addFunctionKind(kind::STRING_U32TOS
);
125 d_equalityEngine
.addFunctionKind(kind::STRING_STOU32
);
126 d_equalityEngine
.addFunctionKind(kind::STRING_STRIDOF
);
127 d_equalityEngine
.addFunctionKind(kind::STRING_STRREPL
);
130 d_zero
= NodeManager::currentNM()->mkConst( Rational( 0 ) );
131 d_one
= NodeManager::currentNM()->mkConst( Rational( 1 ) );
132 d_emptyString
= NodeManager::currentNM()->mkConst( ::CVC4::String("") );
133 std::vector
< Node
> nvec
;
134 d_emptyRegexp
= NodeManager::currentNM()->mkNode( kind::REGEXP_EMPTY
, nvec
);
135 d_true
= NodeManager::currentNM()->mkConst( true );
136 d_false
= NodeManager::currentNM()->mkConst( false );
141 TheoryStrings::~TheoryStrings() {
142 for( std::map
< Node
, EqcInfo
* >::iterator it
= d_eqc_info
.begin(); it
!= d_eqc_info
.end(); ++it
){
148 Node
TheoryStrings::getRepresentative( Node t
) {
149 if( d_equalityEngine
.hasTerm( t
) ){
150 return d_equalityEngine
.getRepresentative( t
);
156 bool TheoryStrings::hasTerm( Node a
){
157 return d_equalityEngine
.hasTerm( a
);
160 bool TheoryStrings::areEqual( Node a
, Node b
){
163 }else if( hasTerm( a
) && hasTerm( b
) ){
164 return d_equalityEngine
.areEqual( a
, b
);
170 bool TheoryStrings::areDisequal( Node a
, Node b
){
174 if( hasTerm( a
) && hasTerm( b
) ) {
175 Node ar
= d_equalityEngine
.getRepresentative( a
);
176 Node br
= d_equalityEngine
.getRepresentative( b
);
177 return ( ar
!=br
&& ar
.isConst() && br
.isConst() ) || d_equalityEngine
.areDisequal( ar
, br
, false );
179 Node ar
= getRepresentative( a
);
180 Node br
= getRepresentative( b
);
181 return ar
!=br
&& ar
.isConst() && br
.isConst();
186 Node
TheoryStrings::getLengthExp( Node t
, std::vector
< Node
>& exp
, Node te
){
187 Assert( areEqual( t
, te
) );
188 Node lt
= mkLength( te
);
190 // use own length if it exists, leads to shorter explanation
193 EqcInfo
* ei
= getOrMakeEqcInfo( t
, false );
194 Node length_term
= ei
? ei
->d_length_term
: Node::null();
195 if( length_term
.isNull() ){
196 //typically shouldnt be necessary
199 Debug("strings") << "TheoryStrings::getLengthTerm " << t
<< " is " << length_term
<< std::endl
;
200 addToExplanation( length_term
, te
, exp
);
201 return Rewriter::rewrite( NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, length_term
) );
205 Node
TheoryStrings::getLength( Node t
, std::vector
< Node
>& exp
) {
206 return getLengthExp( t
, exp
, t
);
209 void TheoryStrings::setMasterEqualityEngine(eq::EqualityEngine
* eq
) {
210 d_equalityEngine
.setMasterEqualityEngine(eq
);
213 void TheoryStrings::addSharedTerm(TNode t
) {
214 Debug("strings") << "TheoryStrings::addSharedTerm(): "
215 << t
<< " " << t
.getType().isBoolean() << endl
;
216 d_equalityEngine
.addTriggerTerm(t
, THEORY_STRINGS
);
217 Debug("strings") << "TheoryStrings::addSharedTerm() finished" << std::endl
;
220 EqualityStatus
TheoryStrings::getEqualityStatus(TNode a
, TNode b
) {
221 if( d_equalityEngine
.hasTerm(a
) && d_equalityEngine
.hasTerm(b
) ){
222 if (d_equalityEngine
.areEqual(a
, b
)) {
223 // The terms are implied to be equal
224 return EQUALITY_TRUE
;
226 if (d_equalityEngine
.areDisequal(a
, b
, false)) {
227 // The terms are implied to be dis-equal
228 return EQUALITY_FALSE
;
231 return EQUALITY_UNKNOWN
;
234 void TheoryStrings::propagate(Effort e
) {
235 // direct propagation now
238 bool TheoryStrings::propagate(TNode literal
) {
239 Debug("strings-propagate") << "TheoryStrings::propagate(" << literal
<< ")" << std::endl
;
240 // If already in conflict, no more propagation
242 Debug("strings-propagate") << "TheoryStrings::propagate(" << literal
<< "): already in conflict" << std::endl
;
246 bool ok
= d_out
->propagate(literal
);
254 void TheoryStrings::explain(TNode literal
, std::vector
<TNode
>& assumptions
) {
255 Debug("strings-explain") << "Explain " << literal
<< " " << d_conflict
<< std::endl
;
256 bool polarity
= literal
.getKind() != kind::NOT
;
257 TNode atom
= polarity
? literal
: literal
[0];
258 unsigned ps
= assumptions
.size();
259 std::vector
< TNode
> tassumptions
;
260 if (atom
.getKind() == kind::EQUAL
|| atom
.getKind() == kind::IFF
) {
261 if( atom
[0]!=atom
[1] ){
262 d_equalityEngine
.explainEquality(atom
[0], atom
[1], polarity
, tassumptions
);
265 d_equalityEngine
.explainPredicate(atom
, polarity
, tassumptions
);
267 for( unsigned i
=0; i
<tassumptions
.size(); i
++ ){
268 if( std::find( assumptions
.begin(), assumptions
.end(), tassumptions
[i
] )==assumptions
.end() ){
269 assumptions
.push_back( tassumptions
[i
] );
272 Debug("strings-explain-debug") << "Explanation for " << literal
<< " was " << std::endl
;
273 for( unsigned i
=ps
; i
<assumptions
.size(); i
++ ){
274 Debug("strings-explain-debug") << " " << assumptions
[i
] << std::endl
;
278 Node
TheoryStrings::explain( TNode literal
){
279 std::vector
< TNode
> assumptions
;
280 explain( literal
, assumptions
);
281 if( assumptions
.empty() ){
283 }else if( assumptions
.size()==1 ){
284 return assumptions
[0];
286 return NodeManager::currentNM()->mkNode( kind::AND
, assumptions
);
290 bool TheoryStrings::getCurrentSubstitution( int effort
, std::vector
< Node
>& vars
,
291 std::vector
< Node
>& subs
, std::map
< Node
, std::vector
< Node
> >& exp
) {
292 Trace("strings-subs") << "getCurrentSubstitution, effort = " << effort
<< std::endl
;
293 for( unsigned i
=0; i
<vars
.size(); i
++ ){
295 Trace("strings-subs") << " get subs for " << n
<< "..." << std::endl
;
298 Node mv
= d_valuation
.getModel()->getRepresentative( n
);
299 Trace("strings-subs") << " model val : " << mv
<< std::endl
;
300 subs
.push_back( mv
);
302 Node nr
= getRepresentative( n
);
303 std::map
< Node
, Node
>::iterator itc
= d_eqc_to_const
.find( nr
);
304 if( itc
!=d_eqc_to_const
.end() ){
305 //constant equivalence classes
306 Trace("strings-subs") << " constant eqc : " << d_eqc_to_const_exp
[nr
] << " " << d_eqc_to_const_base
[nr
] << " " << nr
<< std::endl
;
307 subs
.push_back( itc
->second
);
308 if( !d_eqc_to_const_exp
[nr
].isNull() ){
309 exp
[n
].push_back( d_eqc_to_const_exp
[nr
] );
311 if( !d_eqc_to_const_base
[nr
].isNull() ){
312 addToExplanation( n
, d_eqc_to_const_base
[nr
], exp
[n
] );
314 }else if( effort
>=1 && effort
<3 && n
.getType().isString() ){
316 Node ns
= getNormalString( d_normal_forms_base
[nr
], exp
[n
] );
317 subs
.push_back( ns
);
318 Trace("strings-subs") << " normal eqc : " << ns
<< " " << d_normal_forms_base
[nr
] << " " << nr
<< std::endl
;
319 if( !d_normal_forms_base
[nr
].isNull() ) {
320 addToExplanation( n
, d_normal_forms_base
[nr
], exp
[n
] );
324 //Trace("strings-subs") << " representative : " << nr << std::endl;
325 //addToExplanation( n, nr, exp[n] );
326 //subs.push_back( nr );
334 int TheoryStrings::getReduction( int effort
, Node n
, Node
& nr
) {
335 //determine the effort level to process the extf at
336 // 0 - at assertion time, 1+ - after no other reduction is applicable
337 Assert( d_extf_info_tmp
.find( n
)!=d_extf_info_tmp
.end() );
338 if( d_extf_info_tmp
[n
].d_model_active
){
340 int pol
= d_extf_info_tmp
[n
].d_pol
;
341 if( n
.getKind()==kind::STRING_STRCTN
){
348 std::vector
< Node
> lexp
;
349 Node lenx
= getLength( x
, lexp
);
350 Node lens
= getLength( s
, lexp
);
351 if( areEqual( lenx
, lens
) ){
352 Trace("strings-extf-debug") << " resolve extf : " << n
<< " based on equal lengths disequality." << std::endl
;
353 //we can reduce to disequality when lengths are equal
354 if( !areDisequal( x
, s
) ){
355 lexp
.push_back( lenx
.eqNode(lens
) );
356 lexp
.push_back( n
.negate() );
357 Node xneqs
= x
.eqNode(s
).negate();
358 sendInference( lexp
, xneqs
, "NEG-CTN-EQL", true );
361 }else if( !areDisequal( lenx
, lens
) ){
362 //split on their lenths
363 sendSplit( lenx
, lens
, "NEG-CTN-SP" );
370 if( options::stringLazyPreproc() ){
371 if( n
.getKind()==kind::STRING_SUBSTR
){
373 }else if( n
.getKind()!=kind::STRING_IN_REGEXP
){
378 if( effort
==r_effort
){
379 Node c_n
= pol
==-1 ? n
.negate() : n
;
380 if( d_preproc_cache
.find( c_n
)==d_preproc_cache
.end() ){
381 d_preproc_cache
[ c_n
] = true;
382 Trace("strings-process-debug") << "Process reduction for " << n
<< ", pol = " << pol
<< std::endl
;
383 if( n
.getKind()==kind::STRING_STRCTN
&& pol
==1 ){
386 //positive contains reduces to a equality
387 Node sk1
= mkSkolemCached( x
, s
, sk_id_ctn_pre
, "sc1" );
388 Node sk2
= mkSkolemCached( x
, s
, sk_id_ctn_post
, "sc2" );
389 Node eq
= Rewriter::rewrite( x
.eqNode( mkConcat( sk1
, s
, sk2
) ) );
390 std::vector
< Node
> exp_vec
;
391 exp_vec
.push_back( n
);
392 sendInference( d_empty_vec
, exp_vec
, eq
, "POS-CTN", true );
393 //we've reduced this n
394 Trace("strings-extf-debug") << " resolve extf : " << n
<< " based on positive contain reduction." << std::endl
;
397 // for STRING_SUBSTR, STRING_STRCTN with pol=-1,
398 // STRING_STRIDOF, STRING_ITOS, STRING_U16TOS, STRING_U32TOS, STRING_STOI, STRING_STOU16, STRING_STOU32, STRING_STRREPL
399 std::vector
< Node
> new_nodes
;
400 Node res
= d_preproc
.simplify( n
, new_nodes
);
402 new_nodes
.push_back( NodeManager::currentNM()->mkNode( res
.getType().isBoolean() ? kind::IFF
: kind::EQUAL
, res
, n
) );
403 Node nnlem
= new_nodes
.size()==1 ? new_nodes
[0] : NodeManager::currentNM()->mkNode( kind::AND
, new_nodes
);
404 nnlem
= Rewriter::rewrite( nnlem
);
405 Trace("strings-red-lemma") << "Reduction_" << effort
<< " lemma : " << nnlem
<< std::endl
;
406 Trace("strings-red-lemma") << "...from " << n
<< std::endl
;
407 sendInference( d_empty_vec
, nnlem
, "Reduction", true );
408 //we've reduced this n
409 Trace("strings-extf-debug") << " resolve extf : " << n
<< " based on reduction." << std::endl
;
420 /////////////////////////////////////////////////////////////////////////////
422 /////////////////////////////////////////////////////////////////////////////
425 void TheoryStrings::presolve() {
426 Debug("strings-presolve") << "TheoryStrings::Presolving : get fmf options " << (options::stringFMF() ? "true" : "false") << std::endl
;
428 if(!options::stdASCII()) {
434 /////////////////////////////////////////////////////////////////////////////
436 /////////////////////////////////////////////////////////////////////////////
439 void TheoryStrings::collectModelInfo( TheoryModel
* m
, bool fullModel
) {
440 Trace("strings-model") << "TheoryStrings : Collect model info, fullModel = " << fullModel
<< std::endl
;
441 Trace("strings-model") << "TheoryStrings : assertEqualityEngine." << std::endl
;
442 m
->assertEqualityEngine( &d_equalityEngine
);
444 std::vector
< Node
> nodes
;
445 getEquivalenceClasses( nodes
);
446 std::map
< Node
, Node
> processed
;
447 std::vector
< std::vector
< Node
> > col
;
448 std::vector
< Node
> lts
;
449 separateByLength( nodes
, col
, lts
);
450 //step 1 : get all values for known lengths
451 std::vector
< Node
> lts_values
;
452 std::map
< unsigned, bool > values_used
;
453 for( unsigned i
=0; i
<col
.size(); i
++ ) {
454 Trace("strings-model") << "Checking length for {";
455 for( unsigned j
=0; j
<col
[i
].size(); j
++ ) {
457 Trace("strings-model") << ", ";
459 Trace("strings-model") << col
[i
][j
];
461 Trace("strings-model") << " } (length is " << lts
[i
] << ")" << std::endl
;
462 if( lts
[i
].isConst() ) {
463 lts_values
.push_back( lts
[i
] );
464 Assert(lts
[i
].getConst
<Rational
>() <= RMAXINT
, "Exceeded LONG_MAX in string model");
465 unsigned lvalue
= lts
[i
].getConst
<Rational
>().getNumerator().toUnsignedInt();
466 values_used
[ lvalue
] = true;
468 //get value for lts[i];
469 if( !lts
[i
].isNull() ){
470 Node v
= d_valuation
.getModelValue(lts
[i
]);
471 Trace("strings-model") << "Model value for " << lts
[i
] << " is " << v
<< std::endl
;
472 lts_values
.push_back( v
);
473 Assert(v
.getConst
<Rational
>() <= RMAXINT
, "Exceeded LONG_MAX in string model");
474 unsigned lvalue
= v
.getConst
<Rational
>().getNumerator().toUnsignedInt();
475 values_used
[ lvalue
] = true;
477 //Trace("strings-model-warn") << "No length for eqc " << col[i][0] << std::endl;
479 lts_values
.push_back( Node::null() );
483 ////step 2 : assign arbitrary values for unknown lengths?
484 // confirmed by calculus invariant, see paper
485 Trace("strings-model") << "Assign to equivalence classes..." << std::endl
;
486 //step 3 : assign values to equivalence classes that are pure variables
487 for( unsigned i
=0; i
<col
.size(); i
++ ){
488 std::vector
< Node
> pure_eq
;
489 Trace("strings-model") << "The equivalence classes ";
490 for( unsigned j
=0; j
<col
[i
].size(); j
++ ) {
491 Trace("strings-model") << col
[i
][j
] << " ";
492 //check if col[i][j] has only variables
493 if( !col
[i
][j
].isConst() ){
494 Assert( d_normal_forms
.find( col
[i
][j
] )!=d_normal_forms
.end() );
495 if( d_normal_forms
[col
[i
][j
]].size()==1 ){//&& d_normal_forms[col[i][j]][0]==col[i][j] ){
496 pure_eq
.push_back( col
[i
][j
] );
499 processed
[col
[i
][j
]] = col
[i
][j
];
502 Trace("strings-model") << "have length " << lts_values
[i
] << std::endl
;
504 //assign a new length if necessary
505 if( !pure_eq
.empty() ){
506 if( lts_values
[i
].isNull() ){
508 while( values_used
.find( lvalue
)!=values_used
.end() ){
511 Trace("strings-model") << "*** Decide to make length of " << lvalue
<< std::endl
;
512 lts_values
[i
] = NodeManager::currentNM()->mkConst( Rational( lvalue
) );
513 values_used
[ lvalue
] = true;
515 Trace("strings-model") << "Need to assign values of length " << lts_values
[i
] << " to equivalence classes ";
516 for( unsigned j
=0; j
<pure_eq
.size(); j
++ ){
517 Trace("strings-model") << pure_eq
[j
] << " ";
519 Trace("strings-model") << std::endl
;
522 //use type enumerator
523 Assert(lts_values
[i
].getConst
<Rational
>() <= RMAXINT
, "Exceeded LONG_MAX in string model");
524 StringEnumeratorLength
sel(lts_values
[i
].getConst
<Rational
>().getNumerator().toUnsignedInt());
525 for( unsigned j
=0; j
<pure_eq
.size(); j
++ ){
526 Assert( !sel
.isFinished() );
528 while( d_equalityEngine
.hasTerm( c
) ){
530 Assert( !sel
.isFinished() );
534 Trace("strings-model") << "*** Assigned constant " << c
<< " for " << pure_eq
[j
] << std::endl
;
535 processed
[pure_eq
[j
]] = c
;
536 m
->assertEquality( pure_eq
[j
], c
, true );
540 Trace("strings-model") << "String Model : Pure Assigned." << std::endl
;
541 //step 4 : assign constants to all other equivalence classes
542 for( unsigned i
=0; i
<nodes
.size(); i
++ ){
543 if( processed
.find( nodes
[i
] )==processed
.end() ){
544 Assert( d_normal_forms
.find( nodes
[i
] )!=d_normal_forms
.end() );
545 Trace("strings-model") << "Construct model for " << nodes
[i
] << " based on normal form ";
546 for( unsigned j
=0; j
<d_normal_forms
[nodes
[i
]].size(); j
++ ) {
547 if( j
>0 ) Trace("strings-model") << " ++ ";
548 Trace("strings-model") << d_normal_forms
[nodes
[i
]][j
];
549 Node r
= getRepresentative( d_normal_forms
[nodes
[i
]][j
] );
550 if( !r
.isConst() && processed
.find( r
)==processed
.end() ){
551 Trace("strings-model") << "(UNPROCESSED)";
554 Trace("strings-model") << std::endl
;
555 std::vector
< Node
> nc
;
556 for( unsigned j
=0; j
<d_normal_forms
[nodes
[i
]].size(); j
++ ) {
557 Node r
= getRepresentative( d_normal_forms
[nodes
[i
]][j
] );
558 Assert( r
.isConst() || processed
.find( r
)!=processed
.end() );
559 nc
.push_back(r
.isConst() ? r
: processed
[r
]);
561 Node cc
= mkConcat( nc
);
562 Assert( cc
.getKind()==kind::CONST_STRING
);
563 Trace("strings-model") << "*** Determined constant " << cc
<< " for " << nodes
[i
] << std::endl
;
564 processed
[nodes
[i
]] = cc
;
565 m
->assertEquality( nodes
[i
], cc
, true );
568 //Trace("strings-model") << "String Model : Assigned." << std::endl;
569 Trace("strings-model") << "String Model : Finished." << std::endl
;
572 /////////////////////////////////////////////////////////////////////////////
574 /////////////////////////////////////////////////////////////////////////////
577 void TheoryStrings::preRegisterTerm(TNode n
) {
578 if( d_pregistered_terms_cache
.find(n
) == d_pregistered_terms_cache
.end() ) {
579 d_pregistered_terms_cache
.insert(n
);
580 //check for logic exceptions
581 if( !options::stringExp() ){
582 if( n
.getKind()==kind::STRING_STRIDOF
||
583 n
.getKind() == kind::STRING_ITOS
|| n
.getKind() == kind::STRING_U16TOS
|| n
.getKind() == kind::STRING_U32TOS
||
584 n
.getKind() == kind::STRING_STOI
|| n
.getKind() == kind::STRING_STOU16
|| n
.getKind() == kind::STRING_STOU32
||
585 n
.getKind() == kind::STRING_STRREPL
|| n
.getKind() == kind::STRING_STRCTN
){
586 std::stringstream ss
;
587 ss
<< "Term of kind " << n
.getKind() << " not supported in default mode, try --strings-exp";
588 throw LogicException(ss
.str());
591 switch( n
.getKind() ) {
593 d_equalityEngine
.addTriggerEquality(n
);
596 case kind::STRING_IN_REGEXP
: {
597 d_out
->requirePhase(n
, true);
598 d_equalityEngine
.addTriggerPredicate(n
);
599 d_equalityEngine
.addTerm(n
[0]);
600 d_equalityEngine
.addTerm(n
[1]);
604 TypeNode tn
= n
.getType();
605 if( tn
.isString() ) {
606 registerTerm( n
, 0 );
608 if( n
.getKind() == kind::VARIABLE
&& options::stringFMF() ){
609 d_input_vars
.insert(n
);
611 d_equalityEngine
.addTerm(n
);
612 } else if (tn
.isBoolean()) {
613 // Get triggered for both equal and dis-equal
614 d_equalityEngine
.addTriggerPredicate(n
);
616 // Function applications/predicates
617 d_equalityEngine
.addTerm(n
);
618 if( options::stringExp() ){
619 //collect extended functions here: some may not be asserted to strings (such as those with return type Int),
620 // but we need to record them so they are treated properly
621 d_extt
->registerTermRec( n
);
624 //concat terms do not contribute to theory combination? TODO: verify
625 if( n
.hasOperator() && kindToTheoryId( n
.getKind() )==THEORY_STRINGS
&& n
.getKind()!=kind::STRING_CONCAT
){
626 d_functionsTerms
.push_back( n
);
633 Node
TheoryStrings::expandDefinition(LogicRequest
&logicRequest
, Node node
) {
634 Trace("strings-exp-def") << "TheoryStrings::expandDefinition : " << node
<< std::endl
;
639 void TheoryStrings::check(Effort e
) {
640 if (done() && e
<EFFORT_FULL
) {
644 TimerStat::CodeTimer
checkTimer(d_checkTime
);
649 /*if(getLogicInfo().hasEverything()) {
650 WarningOnce() << "WARNING: strings not supported in default configuration (ALL_SUPPORTED).\n"
651 << "To suppress this warning in the future use proper logic symbol, e.g. (set-logic QF_S)." << std::endl;
655 if( !done() && !hasTerm( d_emptyString
) ) {
656 preRegisterTerm( d_emptyString
);
659 // Trace("strings-process") << "Theory of strings, check : " << e << std::endl;
660 Trace("strings-check") << "Theory of strings, check : " << e
<< std::endl
;
661 while ( !done() && !d_conflict
) {
662 // Get all the assertions
663 Assertion assertion
= get();
664 TNode fact
= assertion
.assertion
;
666 Trace("strings-assertion") << "get assertion: " << fact
<< endl
;
667 polarity
= fact
.getKind() != kind::NOT
;
668 atom
= polarity
? fact
: fact
[0];
670 //assert pending fact
671 assertPendingFact( atom
, polarity
, fact
);
675 if( !d_conflict
&& ( ( e
== EFFORT_FULL
&& !d_valuation
.needCheck() ) || ( e
==EFFORT_STANDARD
&& options::stringEager() ) ) ) {
676 Trace("strings-check") << "Theory of strings full effort check " << std::endl
;
678 if(Trace
.isOn("strings-eqc")) {
679 for( unsigned t
=0; t
<2; t
++ ) {
680 eq::EqClassesIterator eqcs2_i
= eq::EqClassesIterator( &d_equalityEngine
);
681 Trace("strings-eqc") << (t
==0 ? "STRINGS:" : "OTHER:") << std::endl
;
682 while( !eqcs2_i
.isFinished() ){
683 Node eqc
= (*eqcs2_i
);
684 bool print
= (t
==0 && eqc
.getType().isString() ) || (t
==1 && !eqc
.getType().isString() );
686 eq::EqClassIterator eqc2_i
= eq::EqClassIterator( eqc
, &d_equalityEngine
);
687 Trace("strings-eqc") << "Eqc( " << eqc
<< " ) : { ";
688 while( !eqc2_i
.isFinished() ) {
689 if( (*eqc2_i
)!=eqc
&& (*eqc2_i
).getKind()!=kind::EQUAL
){
690 Trace("strings-eqc") << (*eqc2_i
) << " ";
694 Trace("strings-eqc") << " } " << std::endl
;
695 EqcInfo
* ei
= getOrMakeEqcInfo( eqc
, false );
697 Trace("strings-eqc-debug") << "* Length term : " << ei
->d_length_term
.get() << std::endl
;
698 Trace("strings-eqc-debug") << "* Cardinality lemma k : " << ei
->d_cardinality_lem_k
.get() << std::endl
;
699 Trace("strings-eqc-debug") << "* Normalization length lemma : " << ei
->d_normalized_length
.get() << std::endl
;
704 Trace("strings-eqc") << std::endl
;
706 Trace("strings-eqc") << std::endl
;
709 bool addedLemma
= false;
712 Trace("strings-process") << "----check, next round---" << std::endl
;
714 Trace("strings-process") << "Done check init, addedFact = " << !d_pending
.empty() << " " << !d_lemma_cache
.empty() << ", d_conflict = " << d_conflict
<< std::endl
;
715 if( !hasProcessed() ){
717 Trace("strings-process") << "Done check extended functions eval, addedFact = " << !d_pending
.empty() << " " << !d_lemma_cache
.empty() << ", d_conflict = " << d_conflict
<< std::endl
;
718 if( !hasProcessed() ){
720 Trace("strings-process") << "Done check flat forms, addedFact = " << !d_pending
.empty() << " " << !d_lemma_cache
.empty() << ", d_conflict = " << d_conflict
<< std::endl
;
721 if( !hasProcessed() && e
==EFFORT_FULL
){
723 Trace("strings-process") << "Done check normal forms, addedFact = " << !d_pending
.empty() << " " << !d_lemma_cache
.empty() << ", d_conflict = " << d_conflict
<< std::endl
;
724 if( !hasProcessed() ){
725 if( options::stringEagerLen() ){
727 Trace("strings-process") << "Done check lengths, addedFact = " << !d_pending
.empty() << " " << !d_lemma_cache
.empty() << ", d_conflict = " << d_conflict
<< std::endl
;
729 if( !hasProcessed() ){
730 if( options::stringExp() && !options::stringGuessModel() ){
731 checkExtfReductions( 2 );
732 Trace("strings-process") << "Done check extended functions reduction 2, addedFact = " << !d_pending
.empty() << " " << !d_lemma_cache
.empty() << ", d_conflict = " << d_conflict
<< std::endl
;
734 if( !hasProcessed() ){
736 Trace("strings-process") << "Done check memberships, addedFact = " << !d_pending
.empty() << " " << !d_lemma_cache
.empty() << ", d_conflict = " << d_conflict
<< std::endl
;
737 if( !hasProcessed() ){
739 Trace("strings-process") << "Done check cardinality, addedFact = " << !d_pending
.empty() << " " << !d_lemma_cache
.empty() << ", d_conflict = " << d_conflict
<< std::endl
;
748 addedFact
= !d_pending
.empty();
749 addedLemma
= !d_lemma_cache
.empty();
752 }while( !d_conflict
&& !addedLemma
&& addedFact
);
754 Trace("strings-check") << "Theory of strings done full effort check " << addedLemma
<< " " << d_conflict
<< std::endl
;
755 }else if( e
==EFFORT_LAST_CALL
){
756 Assert( !hasProcessed() );
757 Trace("strings-check") << "Theory of strings last call effort check " << std::endl
;
759 checkExtfReductions( 2 );
762 Trace("strings-process") << "Done check extended functions reduction 2, addedFact = " << !d_pending
.empty() << " " << !d_lemma_cache
.empty() << ", d_conflict = " << d_conflict
<< std::endl
;
764 Trace("strings-check") << "Theory of strings, done check : " << e
<< std::endl
;
765 Assert( d_pending
.empty() );
766 Assert( d_lemma_cache
.empty() );
769 bool TheoryStrings::needsCheckLastEffort() {
770 if( options::stringGuessModel() ){
771 return d_has_extf
.get();
777 void TheoryStrings::checkExtfReductions( int effort
) {
779 //std::vector< Node > nred;
780 //d_extt->doReductions( effort, nred, false );
782 std::vector
< Node
> extf
;
783 d_extt
->getActive( extf
);
784 Trace("strings-process") << "checking " << extf
.size() << " active extf" << std::endl
;
785 for( unsigned i
=0; i
<extf
.size(); i
++ ){
787 Trace("strings-process") << "Check " << n
<< ", active in model=" << d_extf_info_tmp
[n
].d_model_active
<< std::endl
;
789 int ret
= getReduction( effort
, n
, nr
);
790 Assert( nr
.isNull() );
792 d_extt
->markReduced( extf
[i
] );
793 if( options::stringOpt1() && hasProcessed() ){
800 TheoryStrings::EqcInfo::EqcInfo( context::Context
* c
) : d_length_term(c
), d_cardinality_lem_k(c
), d_normalized_length(c
) {
804 TheoryStrings::EqcInfo
* TheoryStrings::getOrMakeEqcInfo( Node eqc
, bool doMake
) {
805 std::map
< Node
, EqcInfo
* >::iterator eqc_i
= d_eqc_info
.find( eqc
);
806 if( eqc_i
==d_eqc_info
.end() ){
808 EqcInfo
* ei
= new EqcInfo( getSatContext() );
809 d_eqc_info
[eqc
] = ei
;
815 return (*eqc_i
).second
;
820 /** Conflict when merging two constants */
821 void TheoryStrings::conflict(TNode a
, TNode b
){
823 Debug("strings-conflict") << "Making conflict..." << std::endl
;
826 if (a
.getKind() == kind::CONST_BOOLEAN
) {
827 conflictNode
= explain( a
.iffNode(b
) );
829 conflictNode
= explain( a
.eqNode(b
) );
831 Trace("strings-conflict") << "CONFLICT: Eq engine conflict : " << conflictNode
<< std::endl
;
832 d_out
->conflict( conflictNode
);
836 /** called when a new equivalance class is created */
837 void TheoryStrings::eqNotifyNewClass(TNode t
){
838 if( t
.getKind() == kind::STRING_LENGTH
){
839 Trace("strings-debug") << "New length eqc : " << t
<< std::endl
;
840 Node r
= d_equalityEngine
.getRepresentative(t
[0]);
841 EqcInfo
* ei
= getOrMakeEqcInfo( r
, true );
842 ei
->d_length_term
= t
[0];
843 //we care about the length of this string
844 registerTerm( t
[0], 1 );
846 //d_extt->registerTerm( t );
850 /** called when two equivalance classes will merge */
851 void TheoryStrings::eqNotifyPreMerge(TNode t1
, TNode t2
){
852 EqcInfo
* e2
= getOrMakeEqcInfo(t2
, false);
854 EqcInfo
* e1
= getOrMakeEqcInfo( t1
);
855 //add information from e2 to e1
856 if( !e2
->d_length_term
.get().isNull() ){
857 e1
->d_length_term
.set( e2
->d_length_term
);
859 if( e2
->d_cardinality_lem_k
.get()>e1
->d_cardinality_lem_k
.get() ) {
860 e1
->d_cardinality_lem_k
.set( e2
->d_cardinality_lem_k
);
862 if( !e2
->d_normalized_length
.get().isNull() ){
863 e1
->d_normalized_length
.set( e2
->d_normalized_length
);
868 /** called when two equivalance classes have merged */
869 void TheoryStrings::eqNotifyPostMerge(TNode t1
, TNode t2
) {
873 /** called when two equivalance classes are disequal */
874 void TheoryStrings::eqNotifyDisequal(TNode t1
, TNode t2
, TNode reason
) {
875 if( t1
.getType().isString() ){
876 //store disequalities between strings, may need to check if their lengths are equal/disequal
877 d_ee_disequalities
.push_back( t1
.eqNode( t2
) );
881 void TheoryStrings::addCarePairs( quantifiers::TermArgTrie
* t1
, quantifiers::TermArgTrie
* t2
, unsigned arity
, unsigned depth
) {
884 Node f1
= t1
->getNodeData();
885 Node f2
= t2
->getNodeData();
886 if( !d_equalityEngine
.areEqual( f1
, f2
) ){
887 Trace("strings-cg-debug") << "TheoryStrings::computeCareGraph(): checking function " << f1
<< " and " << f2
<< std::endl
;
888 vector
< pair
<TNode
, TNode
> > currentPairs
;
889 for (unsigned k
= 0; k
< f1
.getNumChildren(); ++ k
) {
892 Assert( d_equalityEngine
.hasTerm(x
) );
893 Assert( d_equalityEngine
.hasTerm(y
) );
894 Assert( !d_equalityEngine
.areDisequal( x
, y
, false ) );
895 if( !d_equalityEngine
.areEqual( x
, y
) ){
896 if( d_equalityEngine
.isTriggerTerm(x
, THEORY_STRINGS
) && d_equalityEngine
.isTriggerTerm(y
, THEORY_STRINGS
) ){
897 TNode x_shared
= d_equalityEngine
.getTriggerTermRepresentative(x
, THEORY_STRINGS
);
898 TNode y_shared
= d_equalityEngine
.getTriggerTermRepresentative(y
, THEORY_STRINGS
);
899 EqualityStatus eqStatus
= d_valuation
.getEqualityStatus(x_shared
, y_shared
);
900 if( eqStatus
==EQUALITY_FALSE_AND_PROPAGATED
|| eqStatus
==EQUALITY_FALSE
|| eqStatus
==EQUALITY_FALSE_IN_MODEL
){
901 //an argument is disequal, we are done
904 currentPairs
.push_back(make_pair(x_shared
, y_shared
));
909 for (unsigned c
= 0; c
< currentPairs
.size(); ++ c
) {
910 Trace("strings-cg-pair") << "TheoryStrings::computeCareGraph(): pair : " << currentPairs
[c
].first
<< " " << currentPairs
[c
].second
<< std::endl
;
911 addCarePair(currentPairs
[c
].first
, currentPairs
[c
].second
);
917 if( depth
<(arity
-1) ){
918 //add care pairs internal to each child
919 for( std::map
< TNode
, quantifiers::TermArgTrie
>::iterator it
= t1
->d_data
.begin(); it
!= t1
->d_data
.end(); ++it
){
920 addCarePairs( &it
->second
, NULL
, arity
, depth
+1 );
923 //add care pairs based on each pair of non-disequal arguments
924 for( std::map
< TNode
, quantifiers::TermArgTrie
>::iterator it
= t1
->d_data
.begin(); it
!= t1
->d_data
.end(); ++it
){
925 std::map
< TNode
, quantifiers::TermArgTrie
>::iterator it2
= it
;
927 for( ; it2
!= t1
->d_data
.end(); ++it2
){
928 if( !d_equalityEngine
.areDisequal(it
->first
, it2
->first
, false) ){
929 addCarePairs( &it
->second
, &it2
->second
, arity
, depth
+1 );
934 //add care pairs based on product of indices, non-disequal arguments
935 for( std::map
< TNode
, quantifiers::TermArgTrie
>::iterator it
= t1
->d_data
.begin(); it
!= t1
->d_data
.end(); ++it
){
936 for( std::map
< TNode
, quantifiers::TermArgTrie
>::iterator it2
= t2
->d_data
.begin(); it2
!= t2
->d_data
.end(); ++it2
){
937 if( !d_equalityEngine
.areDisequal(it
->first
, it2
->first
, false) ){
938 addCarePairs( &it
->second
, &it2
->second
, arity
, depth
+1 );
946 void TheoryStrings::computeCareGraph(){
947 //computing the care graph here is probably still necessary, due to operators that take non-string arguments TODO: verify
948 Trace("strings-cg") << "TheoryStrings::computeCareGraph(): Build term indices..." << std::endl
;
949 std::map
< Node
, quantifiers::TermArgTrie
> index
;
950 std::map
< Node
, unsigned > arity
;
951 unsigned functionTerms
= d_functionsTerms
.size();
952 for (unsigned i
= 0; i
< functionTerms
; ++ i
) {
953 TNode f1
= d_functionsTerms
[i
];
954 Trace("strings-cg") << "...build for " << f1
<< std::endl
;
955 Node op
= f1
.getOperator();
956 std::vector
< TNode
> reps
;
957 bool has_trigger_arg
= false;
958 for( unsigned j
=0; j
<f1
.getNumChildren(); j
++ ){
959 reps
.push_back( d_equalityEngine
.getRepresentative( f1
[j
] ) );
960 if( d_equalityEngine
.isTriggerTerm( f1
[j
], THEORY_STRINGS
) ){
961 has_trigger_arg
= true;
964 if( has_trigger_arg
){
965 index
[op
].addTerm( f1
, reps
);
966 arity
[op
] = reps
.size();
970 for( std::map
< Node
, quantifiers::TermArgTrie
>::iterator itii
= index
.begin(); itii
!= index
.end(); ++itii
){
971 Trace("strings-cg") << "TheoryStrings::computeCareGraph(): Process index " << itii
->first
<< "..." << std::endl
;
972 addCarePairs( &itii
->second
, NULL
, arity
[ itii
->first
], 0 );
976 void TheoryStrings::assertPendingFact(Node atom
, bool polarity
, Node exp
) {
977 Trace("strings-pending") << "Assert pending fact : " << atom
<< " " << polarity
<< " from " << exp
<< std::endl
;
978 Assert(atom
.getKind() != kind::OR
, "Infer error: a split.");
979 if( atom
.getKind()==kind::EQUAL
){
980 Trace("strings-pending-debug") << " Register term" << std::endl
;
981 for( unsigned j
=0; j
<2; j
++ ) {
982 if( !d_equalityEngine
.hasTerm( atom
[j
] ) && atom
[j
].getType().isString() ) {
983 registerTerm( atom
[j
], 0 );
986 Trace("strings-pending-debug") << " Now assert equality" << std::endl
;
987 d_equalityEngine
.assertEquality( atom
, polarity
, exp
);
988 Trace("strings-pending-debug") << " Finished assert equality" << std::endl
;
990 d_equalityEngine
.assertPredicate( atom
, polarity
, exp
);
992 if( atom
.getKind()==kind::STRING_IN_REGEXP
){
993 if( polarity
&& atom
[1].getKind()==kind::REGEXP_RANGE
){
994 if( d_extf_infer_cache_u
.find( atom
)==d_extf_infer_cache_u
.end() ){
995 d_extf_infer_cache_u
.insert( atom
);
996 //length of first argument is one
997 Node conc
= d_one
.eqNode( NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, atom
[0] ) );
998 Node lem
= NodeManager::currentNM()->mkNode( kind::OR
, atom
.negate(), conc
);
999 Trace("strings-lemma") << "Strings::Lemma RE-Range-Len : " << lem
<< std::endl
;
1000 d_out
->lemma( lem
);
1004 //register the atom here, since it may not create a new equivalence class
1005 //d_extt->registerTerm( atom );
1007 Trace("strings-pending-debug") << " Now collect terms" << std::endl
;
1008 //collect extended function terms in the atom
1009 d_extt
->registerTermRec( atom
);
1010 Trace("strings-pending-debug") << " Finished collect terms" << std::endl
;
1013 void TheoryStrings::doPendingFacts() {
1015 while( !d_conflict
&& i
<d_pending
.size() ) {
1016 Node fact
= d_pending
[i
];
1017 Node exp
= d_pending_exp
[ fact
];
1018 if(fact
.getKind() == kind::AND
) {
1019 for(size_t j
=0; j
<fact
.getNumChildren(); j
++) {
1020 bool polarity
= fact
[j
].getKind() != kind::NOT
;
1021 TNode atom
= polarity
? fact
[j
] : fact
[j
][0];
1022 assertPendingFact(atom
, polarity
, exp
);
1025 bool polarity
= fact
.getKind() != kind::NOT
;
1026 TNode atom
= polarity
? fact
: fact
[0];
1027 assertPendingFact(atom
, polarity
, exp
);
1032 d_pending_exp
.clear();
1035 void TheoryStrings::doPendingLemmas() {
1036 if( !d_conflict
&& !d_lemma_cache
.empty() ){
1037 for( unsigned i
=0; i
<d_lemma_cache
.size(); i
++ ){
1038 Trace("strings-pending") << "Process pending lemma : " << d_lemma_cache
[i
] << std::endl
;
1039 d_out
->lemma( d_lemma_cache
[i
] );
1041 for( std::map
< Node
, bool >::iterator it
= d_pending_req_phase
.begin(); it
!= d_pending_req_phase
.end(); ++it
){
1042 Trace("strings-pending") << "Require phase : " << it
->first
<< ", polarity = " << it
->second
<< std::endl
;
1043 d_out
->requirePhase( it
->first
, it
->second
);
1046 d_lemma_cache
.clear();
1047 d_pending_req_phase
.clear();
1050 bool TheoryStrings::hasProcessed() {
1051 return d_conflict
|| !d_lemma_cache
.empty() || !d_pending
.empty();
1054 void TheoryStrings::addToExplanation( Node a
, Node b
, std::vector
< Node
>& exp
) {
1056 Debug("strings-explain") << "Add to explanation : " << a
<< " == " << b
<< std::endl
;
1057 Assert( areEqual( a
, b
) );
1058 exp
.push_back( a
.eqNode( b
) );
1062 void TheoryStrings::addToExplanation( Node lit
, std::vector
< Node
>& exp
) {
1063 if( !lit
.isNull() ){
1064 exp
.push_back( lit
);
1068 void TheoryStrings::checkInit() {
1070 d_eqc_to_const
.clear();
1071 d_eqc_to_const_base
.clear();
1072 d_eqc_to_const_exp
.clear();
1073 d_eqc_to_len_term
.clear();
1074 d_term_index
.clear();
1075 d_strings_eqc
.clear();
1077 std::map
< Kind
, unsigned > ncongruent
;
1078 std::map
< Kind
, unsigned > congruent
;
1079 d_emptyString_r
= getRepresentative( d_emptyString
);
1080 eq::EqClassesIterator eqcs_i
= eq::EqClassesIterator( &d_equalityEngine
);
1081 while( !eqcs_i
.isFinished() ){
1082 Node eqc
= (*eqcs_i
);
1083 TypeNode tn
= eqc
.getType();
1084 if( !tn
.isRegExp() ){
1085 if( tn
.isString() ){
1086 d_strings_eqc
.push_back( eqc
);
1089 eq::EqClassIterator eqc_i
= eq::EqClassIterator( eqc
, &d_equalityEngine
);
1090 while( !eqc_i
.isFinished() ) {
1093 d_eqc_to_const
[eqc
] = n
;
1094 d_eqc_to_const_base
[eqc
] = n
;
1095 d_eqc_to_const_exp
[eqc
] = Node::null();
1096 }else if( tn
.isInteger() ){
1097 if( n
.getKind()==kind::STRING_LENGTH
){
1098 Node nr
= getRepresentative( n
[0] );
1099 d_eqc_to_len_term
[nr
] = n
[0];
1101 }else if( n
.getNumChildren()>0 ){
1102 Kind k
= n
.getKind();
1103 if( k
!=kind::EQUAL
){
1104 if( d_congruent
.find( n
)==d_congruent
.end() ){
1105 std::vector
< Node
> c
;
1106 Node nc
= d_term_index
[k
].add( n
, 0, this, d_emptyString_r
, c
);
1108 //check if we have inferred a new equality by removal of empty components
1109 if( n
.getKind()==kind::STRING_CONCAT
&& !areEqual( nc
, n
) ){
1110 std::vector
< Node
> exp
;
1111 unsigned count
[2] = { 0, 0 };
1112 while( count
[0]<nc
.getNumChildren() || count
[1]<n
.getNumChildren() ){
1113 //explain empty prefixes
1114 for( unsigned t
=0; t
<2; t
++ ){
1115 Node nn
= t
==0 ? nc
: n
;
1116 while( count
[t
]<nn
.getNumChildren() &&
1117 ( nn
[count
[t
]]==d_emptyString
|| areEqual( nn
[count
[t
]], d_emptyString
) ) ){
1118 if( nn
[count
[t
]]!=d_emptyString
){
1119 exp
.push_back( nn
[count
[t
]].eqNode( d_emptyString
) );
1124 //explain equal components
1125 if( count
[0]<nc
.getNumChildren() ){
1126 Assert( count
[1]<n
.getNumChildren() );
1127 if( nc
[count
[0]]!=n
[count
[1]] ){
1128 exp
.push_back( nc
[count
[0]].eqNode( n
[count
[1]] ) );
1134 //infer the equality
1135 sendInference( exp
, n
.eqNode( nc
), "I_Norm" );
1136 }else if( d_extt
->hasFunctionKind( n
.getKind() ) ){
1137 //mark as congruent : only process if neither has been reduced
1138 d_extt
->markCongruent( nc
, n
);
1140 //this node is congruent to another one, we can ignore it
1141 Trace("strings-process-debug") << " congruent term : " << n
<< std::endl
;
1142 d_congruent
.insert( n
);
1144 }else if( k
==kind::STRING_CONCAT
&& c
.size()==1 ){
1145 Trace("strings-process-debug") << " congruent term by singular : " << n
<< " " << c
[0] << std::endl
;
1147 if( !areEqual( c
[0], n
) ){
1148 std::vector
< Node
> exp
;
1149 //explain empty components
1150 bool foundNEmpty
= false;
1151 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
1152 if( areEqual( n
[i
], d_emptyString
) ){
1153 if( n
[i
]!=d_emptyString
){
1154 exp
.push_back( n
[i
].eqNode( d_emptyString
) );
1157 Assert( !foundNEmpty
);
1159 exp
.push_back( n
[i
].eqNode( c
[0] ) );
1164 AlwaysAssert( foundNEmpty
);
1165 //infer the equality
1166 sendInference( exp
, n
.eqNode( c
[0] ), "I_Norm_S" );
1168 d_congruent
.insert( n
);
1178 if( d_congruent
.find( n
)==d_congruent
.end() ){
1182 Trace("strings-process-debug") << " congruent variable : " << n
<< std::endl
;
1183 d_congruent
.insert( n
);
1192 if( Trace
.isOn("strings-process") ){
1193 for( std::map
< Kind
, TermIndex
>::iterator it
= d_term_index
.begin(); it
!= d_term_index
.end(); ++it
){
1194 Trace("strings-process") << " Terms[" << it
->first
<< "] = " << ncongruent
[it
->first
] << "/" << (congruent
[it
->first
]+ncongruent
[it
->first
]) << std::endl
;
1197 Trace("strings-process") << "Done check init, addedLemma = " << !d_pending
.empty() << " " << !d_lemma_cache
.empty() << ", d_conflict = " << d_conflict
<< std::endl
;
1198 //now, infer constants for equivalence classes
1199 if( !hasProcessed() ){
1203 Trace("strings-process-debug") << "Check constant equivalence classes..." << std::endl
;
1204 prevSize
= d_eqc_to_const
.size();
1205 std::vector
< Node
> vecc
;
1206 checkConstantEquivalenceClasses( &d_term_index
[kind::STRING_CONCAT
], vecc
);
1207 }while( !hasProcessed() && d_eqc_to_const
.size()>prevSize
);
1208 Trace("strings-process") << "Done check constant equivalence classes, addedLemma = " << !d_pending
.empty() << " " << !d_lemma_cache
.empty() << ", d_conflict = " << d_conflict
<< std::endl
;
1212 void TheoryStrings::checkConstantEquivalenceClasses( TermIndex
* ti
, std::vector
< Node
>& vecc
) {
1213 Node n
= ti
->d_data
;
1215 //construct the constant
1216 Node c
= mkConcat( vecc
);
1217 if( !areEqual( n
, c
) ){
1218 Trace("strings-debug") << "Constant eqc : " << c
<< " for " << n
<< std::endl
;
1219 Trace("strings-debug") << " ";
1220 for( unsigned i
=0; i
<vecc
.size(); i
++ ){
1221 Trace("strings-debug") << vecc
[i
] << " ";
1223 Trace("strings-debug") << std::endl
;
1225 unsigned countc
= 0;
1226 std::vector
< Node
> exp
;
1227 while( count
<n
.getNumChildren() ){
1228 while( count
<n
.getNumChildren() && areEqual( n
[count
], d_emptyString
) ){
1229 addToExplanation( n
[count
], d_emptyString
, exp
);
1232 if( count
<n
.getNumChildren() ){
1233 Trace("strings-debug") << "...explain " << n
[count
] << " " << vecc
[countc
] << std::endl
;
1234 if( !areEqual( n
[count
], vecc
[countc
] ) ){
1235 Node nrr
= getRepresentative( n
[count
] );
1236 Assert( !d_eqc_to_const_exp
[nrr
].isNull() );
1237 addToExplanation( n
[count
], d_eqc_to_const_base
[nrr
], exp
);
1238 exp
.push_back( d_eqc_to_const_exp
[nrr
] );
1240 addToExplanation( n
[count
], vecc
[countc
], exp
);
1246 //exp contains an explanation of n==c
1247 Assert( countc
==vecc
.size() );
1249 sendInference( exp
, n
.eqNode( c
), "I_CONST_MERGE" );
1251 }else if( !hasProcessed() ){
1252 Node nr
= getRepresentative( n
);
1253 std::map
< Node
, Node
>::iterator it
= d_eqc_to_const
.find( nr
);
1254 if( it
==d_eqc_to_const
.end() ){
1255 Trace("strings-debug") << "Set eqc const " << n
<< " to " << c
<< std::endl
;
1256 d_eqc_to_const
[nr
] = c
;
1257 d_eqc_to_const_base
[nr
] = n
;
1258 d_eqc_to_const_exp
[nr
] = mkAnd( exp
);
1259 }else if( c
!=it
->second
){
1261 Trace("strings-debug") << "Conflict, other constant was " << it
->second
<< ", this constant was " << c
<< std::endl
;
1262 if( d_eqc_to_const_exp
[nr
].isNull() ){
1263 // n==c ^ n == c' => false
1264 addToExplanation( n
, it
->second
, exp
);
1266 // n==c ^ n == d_eqc_to_const_base[nr] == c' => false
1267 exp
.push_back( d_eqc_to_const_exp
[nr
] );
1268 addToExplanation( n
, d_eqc_to_const_base
[nr
], exp
);
1270 sendInference( exp
, d_false
, "I_CONST_CONFLICT" );
1273 Trace("strings-debug") << "Duplicate constant." << std::endl
;
1278 for( std::map
< TNode
, TermIndex
>::iterator it
= ti
->d_children
.begin(); it
!= ti
->d_children
.end(); ++it
){
1279 std::map
< Node
, Node
>::iterator itc
= d_eqc_to_const
.find( it
->first
);
1280 if( itc
!=d_eqc_to_const
.end() ){
1281 vecc
.push_back( itc
->second
);
1282 checkConstantEquivalenceClasses( &it
->second
, vecc
);
1284 if( hasProcessed() ){
1291 void TheoryStrings::checkExtfEval( int effort
) {
1292 Trace("strings-extf-list") << "Active extended functions, effort=" << effort
<< " : " << std::endl
;
1293 d_extf_info_tmp
.clear();
1294 bool has_nreduce
= false;
1295 std::vector
< Node
> terms
;
1296 std::vector
< Node
> sterms
;
1297 std::vector
< std::vector
< Node
> > exp
;
1298 d_extt
->getActive( terms
);
1299 d_extt
->getSubstitutedTerms( effort
, terms
, sterms
, exp
);
1300 for( unsigned i
=0; i
<terms
.size(); i
++ ){
1302 Node sn
= sterms
[i
];
1303 //setup information about extf
1304 d_extf_info_tmp
[n
].init();
1305 std::map
< Node
, ExtfInfoTmp
>::iterator itit
= d_extf_info_tmp
.find( n
);
1306 if( n
.getType().isBoolean() ){
1307 if( areEqual( n
, d_true
) ){
1308 itit
->second
.d_pol
= 1;
1309 }else if( areEqual( n
, d_false
) ){
1310 itit
->second
.d_pol
= -1;
1313 Trace("strings-extf-debug") << "Check extf " << n
<< " == " << sn
<< ", pol = " << itit
->second
.d_pol
<< ", effort=" << effort
<< "..." << std::endl
;
1317 itit
->second
.d_exp
.insert( itit
->second
.d_exp
.end(), exp
[i
].begin(), exp
[i
].end() );
1318 // inference is rewriting the substituted node
1319 Node nrc
= Rewriter::rewrite( sn
);
1320 //if rewrites to a constant, then do the inference and mark as reduced
1321 if( nrc
.isConst() ){
1323 d_extt
->markReduced( n
);
1324 Trace("strings-extf-debug") << " resolvable by evaluation..." << std::endl
;
1325 std::vector
< Node
> exps
;
1326 Trace("strings-extf-debug") << " get symbolic definition..." << std::endl
;
1327 Node nrs
= getSymbolicDefinition( sn
, exps
);
1328 if( !nrs
.isNull() ){
1329 Trace("strings-extf-debug") << " rewrite " << nrs
<< "..." << std::endl
;
1330 nrs
= Rewriter::rewrite( nrs
);
1331 //ensure the symbolic form is non-trivial
1332 if( nrs
.isConst() ){
1333 Trace("strings-extf-debug") << " symbolic definition is trivial..." << std::endl
;
1337 Trace("strings-extf-debug") << " could not infer symbolic definition." << std::endl
;
1340 if( !nrs
.isNull() ){
1341 Trace("strings-extf-debug") << " symbolic def : " << nrs
<< std::endl
;
1342 if( !areEqual( nrs
, nrc
) ){
1343 //infer symbolic unit
1344 if( n
.getType().isBoolean() ){
1345 conc
= nrc
==d_true
? nrs
: nrs
.negate();
1347 conc
= nrs
.eqNode( nrc
);
1349 itit
->second
.d_exp
.clear();
1352 if( !areEqual( n
, nrc
) ){
1353 if( n
.getType().isBoolean() ){
1354 if( areEqual( n
, nrc
==d_true
? d_false
: d_true
) ){
1355 itit
->second
.d_exp
.push_back( nrc
==d_true
? n
.negate() : n
);
1358 conc
= nrc
==d_true
? n
: n
.negate();
1361 conc
= n
.eqNode( nrc
);
1365 if( !conc
.isNull() ){
1366 Trace("strings-extf") << " resolve extf : " << sn
<< " -> " << nrc
<< std::endl
;
1367 sendInference( itit
->second
.d_exp
, conc
, effort
==0 ? "EXTF" : "EXTF-N", true );
1369 Trace("strings-extf-debug") << " conflict, return." << std::endl
;
1374 //check if it is already equal, if so, mark as reduced. Otherwise, do nothing.
1375 if( areEqual( n
, nrc
) ){
1376 Trace("strings-extf") << " resolved extf, since satisfied by model: " << n
<< std::endl
;
1377 itit
->second
.d_model_active
= false;
1380 //if it reduces to a conjunction, infer each and reduce
1381 }else if( ( nrc
.getKind()==kind::OR
&& itit
->second
.d_pol
==-1 ) || ( nrc
.getKind()==kind::AND
&& itit
->second
.d_pol
==1 ) ){
1383 d_extt
->markReduced( n
);
1384 itit
->second
.d_exp
.push_back( itit
->second
.d_pol
==-1 ? n
.negate() : n
);
1385 Trace("strings-extf-debug") << " decomposable..." << std::endl
;
1386 Trace("strings-extf") << " resolve extf : " << sn
<< " -> " << nrc
<< ", pol = " << itit
->second
.d_pol
<< std::endl
;
1387 for( unsigned i
=0; i
<nrc
.getNumChildren(); i
++ ){
1388 sendInference( itit
->second
.d_exp
, itit
->second
.d_pol
==-1 ? nrc
[i
].negate() : nrc
[i
], effort
==0 ? "EXTF_d" : "EXTF_d-N" );
1394 to_reduce
= sterms
[i
];
1397 if( !to_reduce
.isNull() ){
1400 Trace("strings-extf") << " cannot rewrite extf : " << to_reduce
<< std::endl
;
1402 checkExtfInference( n
, to_reduce
, itit
->second
, effort
);
1403 if( Trace
.isOn("strings-extf-list") ){
1404 Trace("strings-extf-list") << " * " << to_reduce
;
1405 if( itit
->second
.d_pol
!=0 ){
1406 Trace("strings-extf-list") << ", pol = " << itit
->second
.d_pol
;
1409 Trace("strings-extf-list") << ", from " << n
;
1411 Trace("strings-extf-list") << std::endl
;
1413 if( d_extt
->isActive( n
) && itit
->second
.d_model_active
){
1418 d_has_extf
= has_nreduce
;
1421 void TheoryStrings::checkExtfInference( Node n
, Node nr
, ExtfInfoTmp
& in
, int effort
){
1422 //make additional inferences that do not contribute to the reduction of n, but may help show a refutation
1424 //add original to explanation
1425 in
.d_exp
.push_back( in
.d_pol
==1 ? n
: n
.negate() );
1427 //d_extf_infer_cache stores whether we have made the inferences associated with a node n,
1428 // this may need to be generalized if multiple inferences apply
1430 if( nr
.getKind()==kind::STRING_STRCTN
){
1431 if( ( in
.d_pol
==1 && nr
[1].getKind()==kind::STRING_CONCAT
) || ( in
.d_pol
==-1 && nr
[0].getKind()==kind::STRING_CONCAT
) ){
1432 if( d_extf_infer_cache
.find( nr
)==d_extf_infer_cache
.end() ){
1433 d_extf_infer_cache
.insert( nr
);
1435 //one argument does (not) contain each of the components of the other argument
1436 int index
= in
.d_pol
==1 ? 1 : 0;
1437 std::vector
< Node
> children
;
1438 children
.push_back( nr
[0] );
1439 children
.push_back( nr
[1] );
1440 //Node exp_n = mkAnd( exp );
1441 for( unsigned i
=0; i
<nr
[index
].getNumChildren(); i
++ ){
1442 children
[index
] = nr
[index
][i
];
1443 Node conc
= NodeManager::currentNM()->mkNode( kind::STRING_STRCTN
, children
);
1444 //can mark as reduced, since model for n => model for conc
1445 d_extt
->markReduced( conc
);
1446 sendInference( in
.d_exp
, in
.d_pol
==1 ? conc
: conc
.negate(), "CTN_Decompose" );
1451 //store this (reduced) assertion
1452 //Assert( effort==0 || nr[0]==getRepresentative( nr[0] ) );
1453 bool pol
= in
.d_pol
==1;
1454 if( std::find( d_extf_info_tmp
[nr
[0]].d_ctn
[pol
].begin(), d_extf_info_tmp
[nr
[0]].d_ctn
[pol
].end(), nr
[1] )==d_extf_info_tmp
[nr
[0]].d_ctn
[pol
].end() ){
1455 Trace("strings-extf-debug") << " store contains info : " << nr
[0] << " " << pol
<< " " << nr
[1] << std::endl
;
1456 d_extf_info_tmp
[nr
[0]].d_ctn
[pol
].push_back( nr
[1] );
1457 d_extf_info_tmp
[nr
[0]].d_ctn_from
[pol
].push_back( n
);
1458 //transitive closure for contains
1460 for( unsigned i
=0; i
<d_extf_info_tmp
[nr
[0]].d_ctn
[opol
].size(); i
++ ){
1461 Node onr
= d_extf_info_tmp
[nr
[0]].d_ctn
[opol
][i
];
1462 Node conc
= NodeManager::currentNM()->mkNode( kind::STRING_STRCTN
, pol
? nr
[1] : onr
, pol
? onr
: nr
[1] );
1463 conc
= Rewriter::rewrite( conc
);
1464 bool do_infer
= false;
1465 if( conc
.getKind()==kind::EQUAL
){
1466 do_infer
= !areDisequal( conc
[0], conc
[1] );
1468 do_infer
= !areEqual( conc
, d_false
);
1471 conc
= conc
.negate();
1472 std::vector
< Node
> exp_c
;
1473 exp_c
.insert( exp_c
.end(), in
.d_exp
.begin(), in
.d_exp
.end() );
1474 Node ofrom
= d_extf_info_tmp
[nr
[0]].d_ctn_from
[opol
][i
];
1475 Assert( d_extf_info_tmp
.find( ofrom
)!=d_extf_info_tmp
.end() );
1476 exp_c
.insert( exp_c
.end(), d_extf_info_tmp
[ofrom
].d_exp
.begin(), d_extf_info_tmp
[ofrom
].d_exp
.end() );
1477 sendInference( exp_c
, conc
, "CTN_Trans" );
1481 Trace("strings-extf-debug") << " redundant." << std::endl
;
1482 d_extt
->markReduced( n
);
1489 void TheoryStrings::collectVars( Node n
, std::vector
< Node
>& vars
, std::map
< Node
, bool >& visited
) {
1491 if( visited
.find( n
)==visited
.end() ){
1493 if( n
.getNumChildren()>0 ){
1494 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
1495 collectVars( n
[i
], vars
, visited
);
1498 //Node nr = getRepresentative( n );
1499 //vars[nr].push_back( n );
1500 vars
.push_back( n
);
1506 Node
TheoryStrings::getSymbolicDefinition( Node n
, std::vector
< Node
>& exp
) {
1507 if( n
.getNumChildren()==0 ){
1508 NodeNodeMap::const_iterator it
= d_proxy_var
.find( n
);
1509 if( it
==d_proxy_var
.end() ){
1510 return Node::null();
1512 Node eq
= n
.eqNode( (*it
).second
);
1513 eq
= Rewriter::rewrite( eq
);
1514 if( std::find( exp
.begin(), exp
.end(), eq
)==exp
.end() ){
1515 exp
.push_back( eq
);
1517 return (*it
).second
;
1520 std::vector
< Node
> children
;
1521 if (n
.getMetaKind() == kind::metakind::PARAMETERIZED
) {
1522 children
.push_back( n
.getOperator() );
1524 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
1525 if( n
.getKind()==kind::STRING_IN_REGEXP
&& i
==1 ){
1526 children
.push_back( n
[i
] );
1528 Node ns
= getSymbolicDefinition( n
[i
], exp
);
1530 return Node::null();
1532 children
.push_back( ns
);
1536 return NodeManager::currentNM()->mkNode( n
.getKind(), children
);
1540 Node
TheoryStrings::getConstantEqc( Node eqc
) {
1541 std::map
< Node
, Node
>::iterator it
= d_eqc_to_const
.find( eqc
);
1542 if( it
!=d_eqc_to_const
.end() ){
1545 return Node::null();
1549 void TheoryStrings::debugPrintFlatForms( const char * tc
){
1550 for( unsigned k
=0; k
<d_strings_eqc
.size(); k
++ ){
1551 Node eqc
= d_strings_eqc
[k
];
1552 if( d_eqc
[eqc
].size()>1 ){
1553 Trace( tc
) << "EQC [" << eqc
<< "]" << std::endl
;
1555 Trace( tc
) << "eqc [" << eqc
<< "]";
1557 std::map
< Node
, Node
>::iterator itc
= d_eqc_to_const
.find( eqc
);
1558 if( itc
!=d_eqc_to_const
.end() ){
1559 Trace( tc
) << " C: " << itc
->second
;
1560 if( d_eqc
[eqc
].size()>1 ){
1561 Trace( tc
) << std::endl
;
1564 if( d_eqc
[eqc
].size()>1 ){
1565 for( unsigned i
=0; i
<d_eqc
[eqc
].size(); i
++ ){
1566 Node n
= d_eqc
[eqc
][i
];
1568 for( unsigned j
=0; j
<d_flat_form
[n
].size(); j
++ ){
1569 Node fc
= d_flat_form
[n
][j
];
1570 itc
= d_eqc_to_const
.find( fc
);
1572 if( itc
!=d_eqc_to_const
.end() ){
1573 Trace( tc
) << itc
->second
;
1579 Trace( tc
) << ", from " << n
;
1581 Trace( tc
) << std::endl
;
1584 Trace( tc
) << std::endl
;
1587 Trace( tc
) << std::endl
;
1590 void TheoryStrings::debugPrintNormalForms( const char * tc
) {
1593 struct sortConstLength
{
1594 std::map
< Node
, unsigned > d_const_length
;
1595 bool operator() (Node i
, Node j
) {
1596 std::map
< Node
, unsigned >::iterator it_i
= d_const_length
.find( i
);
1597 std::map
< Node
, unsigned >::iterator it_j
= d_const_length
.find( j
);
1598 if( it_i
==d_const_length
.end() ){
1599 if( it_j
==d_const_length
.end() ){
1605 if( it_j
==d_const_length
.end() ){
1608 return it_i
->second
<it_j
->second
;
1615 void TheoryStrings::checkFlatForms() {
1616 //first check for cycles, while building ordering of equivalence classes
1618 d_flat_form
.clear();
1619 d_flat_form_index
.clear();
1620 Trace("strings-process") << "Check equivalence classes cycles...." << std::endl
;
1621 //rebuild strings eqc based on acyclic ordering
1622 std::vector
< Node
> eqc
;
1623 eqc
.insert( eqc
.end(), d_strings_eqc
.begin(), d_strings_eqc
.end() );
1624 d_strings_eqc
.clear();
1625 if( options::stringBinaryCsp() ){
1626 //sort: process smallest constants first (necessary if doing binary splits)
1627 sortConstLength scl
;
1628 for( unsigned i
=0; i
<eqc
.size(); i
++ ){
1629 std::map
< Node
, Node
>::iterator itc
= d_eqc_to_const
.find( eqc
[i
] );
1630 if( itc
!=d_eqc_to_const
.end() ){
1631 scl
.d_const_length
[eqc
[i
]] = itc
->second
.getConst
<String
>().size();
1634 std::sort( eqc
.begin(), eqc
.end(), scl
);
1636 for( unsigned i
=0; i
<eqc
.size(); i
++ ){
1637 std::vector
< Node
> curr
;
1638 std::vector
< Node
> exp
;
1639 checkCycles( eqc
[i
], curr
, exp
);
1640 if( hasProcessed() ){
1644 Trace("strings-process-debug") << "Done check cycles, lemmas = " << !d_pending
.empty() << " " << !d_lemma_cache
.empty() << std::endl
;
1645 if( !hasProcessed() ){
1646 //debug print flat forms
1647 if( Trace
.isOn("strings-ff") ){
1648 Trace("strings-ff") << "Flat forms : " << std::endl
;
1649 debugPrintFlatForms( "strings-ff" );
1652 //inferences without recursively expanding flat forms
1654 //(1) approximate equality by containment, infer conflicts
1655 for( unsigned k
=0; k
<d_strings_eqc
.size(); k
++ ){
1656 Node eqc
= d_strings_eqc
[k
];
1657 Node c
= getConstantEqc( eqc
);
1659 //if equivalence class is constant, all component constants in flat forms must be contained in it, in order
1660 std::map
< Node
, std::vector
< Node
> >::iterator it
= d_eqc
.find( eqc
);
1661 if( it
!=d_eqc
.end() ){
1662 for( unsigned i
=0; i
<it
->second
.size(); i
++ ){
1663 Node n
= it
->second
[i
];
1665 if( !TheoryStringsRewriter::canConstantContainList( c
, d_flat_form
[n
], firstc
, lastc
) ){
1666 Trace("strings-ff-debug") << "Flat form for " << n
<< " cannot be contained in constant " << c
<< std::endl
;
1667 Trace("strings-ff-debug") << " indices = " << firstc
<< "/" << lastc
<< std::endl
;
1668 //conflict, explanation is n = base ^ base = c ^ relevant porition of ( n = f[n] )
1669 std::vector
< Node
> exp
;
1670 Assert( d_eqc_to_const_base
.find( eqc
)!=d_eqc_to_const_base
.end() );
1671 addToExplanation( n
, d_eqc_to_const_base
[eqc
], exp
);
1672 Assert( d_eqc_to_const_exp
.find( eqc
)!=d_eqc_to_const_exp
.end() );
1673 if( !d_eqc_to_const_exp
[eqc
].isNull() ){
1674 exp
.push_back( d_eqc_to_const_exp
[eqc
] );
1676 for( int e
=firstc
; e
<=lastc
; e
++ ){
1677 if( d_flat_form
[n
][e
].isConst() ){
1678 Assert( e
>=0 && e
<(int)d_flat_form_index
[n
].size() );
1679 Assert( d_flat_form_index
[n
][e
]>=0 && d_flat_form_index
[n
][e
]<(int)n
.getNumChildren() );
1680 addToExplanation( d_flat_form
[n
][e
], n
[d_flat_form_index
[n
][e
]], exp
);
1683 Node conc
= d_false
;
1684 sendInference( exp
, conc
, "F_NCTN" );
1692 //(2) scan lists, unification to infer conflicts and equalities
1693 for( unsigned k
=0; k
<d_strings_eqc
.size(); k
++ ){
1694 Node eqc
= d_strings_eqc
[k
];
1695 std::map
< Node
, std::vector
< Node
> >::iterator it
= d_eqc
.find( eqc
);
1696 if( it
!=d_eqc
.end() && it
->second
.size()>1 ){
1697 //iterate over start index
1698 for( unsigned start
=0; start
<it
->second
.size()-1; start
++ ){
1699 for( unsigned r
=0; r
<2; r
++ ){
1701 std::vector
< Node
> inelig
;
1702 for( unsigned i
=0; i
<=start
; i
++ ){
1703 inelig
.push_back( it
->second
[start
] );
1705 Node a
= it
->second
[start
];
1708 std::vector
< Node
> exp
;
1709 //std::vector< Node > exp_n;
1712 if( count
==d_flat_form
[a
].size() ){
1713 for( unsigned i
=start
+1; i
<it
->second
.size(); i
++ ){
1715 if( std::find( inelig
.begin(), inelig
.end(), b
)==inelig
.end() ){
1716 if( count
<d_flat_form
[b
].size() ){
1718 std::vector
< Node
> conc_c
;
1719 for( unsigned j
=count
; j
<d_flat_form
[b
].size(); j
++ ){
1720 conc_c
.push_back( b
[d_flat_form_index
[b
][j
]].eqNode( d_emptyString
) );
1722 Assert( !conc_c
.empty() );
1723 conc
= mkAnd( conc_c
);
1726 //swap, will enforce is empty past current
1727 a
= it
->second
[i
]; b
= it
->second
[start
];
1731 inelig
.push_back( it
->second
[i
] );
1735 Node curr
= d_flat_form
[a
][count
];
1736 Node curr_c
= getConstantEqc( curr
);
1737 Node ac
= a
[d_flat_form_index
[a
][count
]];
1738 std::vector
< Node
> lexp
;
1739 Node lcurr
= getLength( ac
, lexp
);
1740 for( unsigned i
=1; i
<it
->second
.size(); i
++ ){
1742 if( std::find( inelig
.begin(), inelig
.end(), b
)==inelig
.end() ){
1743 if( count
==d_flat_form
[b
].size() ){
1744 inelig
.push_back( b
);
1746 std::vector
< Node
> conc_c
;
1747 for( unsigned j
=count
; j
<d_flat_form
[a
].size(); j
++ ){
1748 conc_c
.push_back( a
[d_flat_form_index
[a
][j
]].eqNode( d_emptyString
) );
1750 Assert( !conc_c
.empty() );
1751 conc
= mkAnd( conc_c
);
1757 Node cc
= d_flat_form
[b
][count
];
1759 Node bc
= b
[d_flat_form_index
[b
][count
]];
1760 inelig
.push_back( b
);
1761 Assert( !areEqual( curr
, cc
) );
1762 Node cc_c
= getConstantEqc( cc
);
1763 if( !curr_c
.isNull() && !cc_c
.isNull() ){
1764 //check for constant conflict
1766 Node s
= TheoryStringsRewriter::splitConstant( cc_c
, curr_c
, index
, r
==1 );
1768 addToExplanation( ac
, d_eqc_to_const_base
[curr
], exp
);
1769 addToExplanation( d_eqc_to_const_exp
[curr
], exp
);
1770 addToExplanation( bc
, d_eqc_to_const_base
[cc
], exp
);
1771 addToExplanation( d_eqc_to_const_exp
[cc
], exp
);
1776 }else if( (d_flat_form
[a
].size()-1)==count
&& (d_flat_form
[b
].size()-1)==count
){
1777 conc
= ac
.eqNode( bc
);
1781 //if lengths are the same, apply LengthEq
1782 std::vector
< Node
> lexp2
;
1783 Node lcc
= getLength( bc
, lexp2
);
1784 if( areEqual( lcurr
, lcc
) ){
1785 Trace("strings-ff-debug") << "Infer " << ac
<< " == " << bc
<< " since " << lcurr
<< " == " << lcc
<< std::endl
;
1786 //exp_n.push_back( getLength( curr, true ).eqNode( getLength( cc, true ) ) );
1787 Trace("strings-ff-debug") << "Explanation for " << lcurr
<< " is ";
1788 for( unsigned j
=0; j
<lexp
.size(); j
++ ) { Trace("strings-ff-debug") << lexp
[j
] << std::endl
; }
1789 Trace("strings-ff-debug") << "Explanation for " << lcc
<< " is ";
1790 for( unsigned j
=0; j
<lexp2
.size(); j
++ ) { Trace("strings-ff-debug") << lexp2
[j
] << std::endl
; }
1791 exp
.insert( exp
.end(), lexp
.begin(), lexp
.end() );
1792 exp
.insert( exp
.end(), lexp2
.begin(), lexp2
.end() );
1793 addToExplanation( lcurr
, lcc
, exp
);
1794 conc
= ac
.eqNode( bc
);
1804 if( !conc
.isNull() ){
1805 Trace("strings-ff-debug") << "Found inference : " << conc
<< " based on equality " << a
<< " == " << b
<< " " << r
<< " " << inf_type
<< std::endl
;
1806 addToExplanation( a
, b
, exp
);
1807 //explain why prefixes up to now were the same
1808 for( unsigned j
=0; j
<count
; j
++ ){
1809 Trace("strings-ff-debug") << "Add at " << d_flat_form_index
[a
][j
] << " " << d_flat_form_index
[b
][j
] << std::endl
;
1810 addToExplanation( a
[d_flat_form_index
[a
][j
]], b
[d_flat_form_index
[b
][j
]], exp
);
1812 //explain why other components up to now are empty
1813 for( unsigned t
=0; t
<2; t
++ ){
1814 Node c
= t
==0 ? a
: b
;
1816 if( inf_type
==3 || ( t
==1 && inf_type
==2 ) ){
1817 //explain all the empty components for F_EndpointEq, all for the short end for F_EndpointEmp
1818 jj
= r
==0 ? c
.getNumChildren() : -1;
1820 jj
= t
==0 ? d_flat_form_index
[a
][count
] : d_flat_form_index
[b
][count
];
1823 for( int j
=0; j
<jj
; j
++ ){
1824 if( areEqual( c
[j
], d_emptyString
) ){
1825 addToExplanation( c
[j
], d_emptyString
, exp
);
1829 for( int j
=(c
.getNumChildren()-1); j
>jj
; --j
){
1830 if( areEqual( c
[j
], d_emptyString
) ){
1831 addToExplanation( c
[j
], d_emptyString
, exp
);
1836 //notice that F_EndpointEmp is not typically applied, since strict prefix equality ( a.b = a ) where a,b non-empty
1837 // is conflicting by arithmetic len(a.b)=len(a)+len(b)!=len(a) when len(b)!=0.
1838 sendInference( exp
, conc
, inf_type
==0 ? "F_Const" : ( inf_type
==1 ? "F_Unify" : ( inf_type
==2 ? "F_EndpointEmp" : "F_EndpointEq" ) ) );
1846 }while( inelig
.size()<it
->second
.size() );
1848 for( unsigned i
=0; i
<it
->second
.size(); i
++ ){
1849 std::reverse( d_flat_form
[it
->second
[i
]].begin(), d_flat_form
[it
->second
[i
]].end() );
1850 std::reverse( d_flat_form_index
[it
->second
[i
]].begin(), d_flat_form_index
[it
->second
[i
]].end() );
1856 if( !hasProcessed() ){
1857 // simple extended func reduction
1858 Trace("strings-process") << "Check extended function reduction effort=1..." << std::endl
;
1859 checkExtfReductions( 1 );
1860 Trace("strings-process") << "Done check extended function reduction" << std::endl
;
1865 Node
TheoryStrings::checkCycles( Node eqc
, std::vector
< Node
>& curr
, std::vector
< Node
>& exp
){
1866 if( std::find( curr
.begin(), curr
.end(), eqc
)!=curr
.end() ){
1869 }else if( std::find( d_strings_eqc
.begin(), d_strings_eqc
.end(), eqc
)==d_strings_eqc
.end() ){
1870 curr
.push_back( eqc
);
1871 //look at all terms in this equivalence class
1872 eq::EqClassIterator eqc_i
= eq::EqClassIterator( eqc
, &d_equalityEngine
);
1873 while( !eqc_i
.isFinished() ) {
1875 if( d_congruent
.find( n
)==d_congruent
.end() ){
1876 if( n
.getKind() == kind::STRING_CONCAT
){
1877 Trace("strings-cycle") << eqc
<< " check term : " << n
<< " in " << eqc
<< std::endl
;
1878 if( eqc
!=d_emptyString_r
){
1879 d_eqc
[eqc
].push_back( n
);
1881 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
1882 Node nr
= getRepresentative( n
[i
] );
1883 if( eqc
==d_emptyString_r
){
1884 //for empty eqc, ensure all components are empty
1885 if( nr
!=d_emptyString_r
){
1886 std::vector
< Node
> exp
;
1887 exp
.push_back( n
.eqNode( d_emptyString
) );
1888 sendInference( exp
, n
[i
].eqNode( d_emptyString
), "I_CYCLE_E" );
1889 return Node::null();
1892 if( nr
!=d_emptyString_r
){
1893 d_flat_form
[n
].push_back( nr
);
1894 d_flat_form_index
[n
].push_back( i
);
1896 //for non-empty eqc, recurse and see if we find a loop
1897 Node ncy
= checkCycles( nr
, curr
, exp
);
1898 if( !ncy
.isNull() ){
1899 Trace("strings-cycle") << eqc
<< " cycle: " << ncy
<< " at " << n
<< "[" << i
<< "] : " << n
[i
] << std::endl
;
1900 addToExplanation( n
, eqc
, exp
);
1901 addToExplanation( nr
, n
[i
], exp
);
1903 //can infer all other components must be empty
1904 for( unsigned j
=0; j
<n
.getNumChildren(); j
++ ){
1905 //take first non-empty
1906 if( j
!=i
&& !areEqual( n
[j
], d_emptyString
) ){
1907 sendInference( exp
, n
[j
].eqNode( d_emptyString
), "I_CYCLE" );
1908 return Node::null();
1911 Trace("strings-error") << "Looping term should be congruent : " << n
<< " " << eqc
<< " " << ncy
<< std::endl
;
1912 //should find a non-empty component, otherwise would have been singular congruent (I_Norm_S)
1918 if( hasProcessed() ){
1919 return Node::null();
1929 //now we can add it to the list of equivalence classes
1930 d_strings_eqc
.push_back( eqc
);
1934 return Node::null();
1938 void TheoryStrings::checkNormalForms(){
1939 if( !options::stringEagerLen() ){
1940 for( unsigned i
=0; i
<d_strings_eqc
.size(); i
++ ) {
1941 Node eqc
= d_strings_eqc
[i
];
1942 eq::EqClassIterator eqc_i
= eq::EqClassIterator( eqc
, &d_equalityEngine
);
1943 while( !eqc_i
.isFinished() ) {
1945 if( d_congruent
.find( n
)==d_congruent
.end() ){
1946 registerTerm( n
, 2 );
1952 if( !hasProcessed() ){
1953 Trace("strings-process") << "Normalize equivalence classes...." << std::endl
;
1954 //calculate normal forms for each equivalence class, possibly adding splitting lemmas
1955 d_normal_forms
.clear();
1956 d_normal_forms_exp
.clear();
1957 std::map
< Node
, Node
> nf_to_eqc
;
1958 std::map
< Node
, Node
> eqc_to_nf
;
1959 std::map
< Node
, Node
> eqc_to_exp
;
1960 for( unsigned i
=0; i
<d_strings_eqc
.size(); i
++ ) {
1961 Node eqc
= d_strings_eqc
[i
];
1962 Trace("strings-process-debug") << "- Verify normal forms are the same for " << eqc
<< std::endl
;
1963 normalizeEquivalenceClass( eqc
);
1964 Trace("strings-debug") << "Finished normalizing eqc..." << std::endl
;
1965 if( hasProcessed() ){
1968 Node nf_term
= mkConcat( d_normal_forms
[eqc
] );
1969 std::map
< Node
, Node
>::iterator itn
= nf_to_eqc
.find( nf_term
);
1970 if( itn
!=nf_to_eqc
.end() ){
1971 //two equivalence classes have same normal form, merge
1972 std::vector
< Node
> nf_exp
;
1973 nf_exp
.push_back( mkAnd( d_normal_forms_exp
[eqc
] ) );
1974 nf_exp
.push_back( eqc_to_exp
[itn
->second
] );
1975 Node eq
= d_normal_forms_base
[eqc
].eqNode( d_normal_forms_base
[itn
->second
] );
1976 sendInference( nf_exp
, eq
, "Normal_Form" );
1978 nf_to_eqc
[nf_term
] = eqc
;
1979 eqc_to_nf
[eqc
] = nf_term
;
1980 eqc_to_exp
[eqc
] = mkAnd( d_normal_forms_exp
[eqc
] );
1983 Trace("strings-process-debug") << "Done verifying normal forms are the same for " << eqc
<< std::endl
;
1985 if( !hasProcessed() ){
1986 if(Trace
.isOn("strings-nf")) {
1987 Trace("strings-nf") << "**** Normal forms are : " << std::endl
;
1988 for( std::map
< Node
, Node
>::iterator it
= eqc_to_exp
.begin(); it
!= eqc_to_exp
.end(); ++it
){
1989 Trace("strings-nf") << " N[" << it
->first
<< "] (base " << d_normal_forms_base
[it
->first
] << ") = " << eqc_to_nf
[it
->first
] << std::endl
;
1990 Trace("strings-nf") << " exp: " << it
->second
<< std::endl
;
1992 Trace("strings-nf") << std::endl
;
1995 Trace("strings-process-debug") << "Done check extended functions re-eval, addedFact = " << !d_pending
.empty() << " " << !d_lemma_cache
.empty() << ", d_conflict = " << d_conflict
<< std::endl
;
1996 if( !hasProcessed() ){
1997 if( !options::stringEagerLen() ){
1999 if( hasProcessed() ){
2003 //process disequalities between equivalence classes
2005 Trace("strings-process-debug") << "Done check disequalities, addedFact = " << !d_pending
.empty() << " " << !d_lemma_cache
.empty() << ", d_conflict = " << d_conflict
<< std::endl
;
2008 Trace("strings-solve") << "Finished check normal forms, #lemmas = " << d_lemma_cache
.size() << ", conflict = " << d_conflict
<< std::endl
;
2012 //compute d_normal_forms_(base,exp,exp_depend)[eqc]
2013 void TheoryStrings::normalizeEquivalenceClass( Node eqc
) {
2014 Trace("strings-process-debug") << "Process equivalence class " << eqc
<< std::endl
;
2015 if( areEqual( eqc
, d_emptyString
) ) {
2016 #ifdef CVC4_ASSERTIONS
2017 for( unsigned j
=0; j
<d_eqc
[eqc
].size(); j
++ ){
2018 Node n
= d_eqc
[eqc
][j
];
2019 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
2020 Assert( areEqual( n
[i
], d_emptyString
) );
2025 Trace("strings-process-debug") << "Return process equivalence class " << eqc
<< " : empty." << std::endl
;
2026 d_normal_forms_base
[eqc
] = d_emptyString
;
2027 d_normal_forms
[eqc
].clear();
2028 d_normal_forms_exp
[eqc
].clear();
2030 Assert( d_normal_forms
.find(eqc
)==d_normal_forms
.end() );
2031 //phi => t = s1 * ... * sn
2032 // normal form for each non-variable term in this eqc (s1...sn)
2033 std::vector
< std::vector
< Node
> > normal_forms
;
2034 // explanation for each normal form (phi)
2035 std::vector
< std::vector
< Node
> > normal_forms_exp
;
2036 // dependency information
2037 std::vector
< std::map
< Node
, std::map
< bool, int > > > normal_forms_exp_depend
;
2038 // record terms for each normal form (t)
2039 std::vector
< Node
> normal_form_src
;
2041 getNormalForms(eqc
, normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
);
2042 if( hasProcessed() ){
2045 // process the normal forms
2046 processNEqc( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
);
2047 if( hasProcessed() ){
2050 //debugPrintNormalForms( "strings-solve", eqc, normal_forms, normal_form_src, normal_forms_exp, normal_forms_exp_depend );
2052 //construct the normal form
2053 Assert( !normal_forms
.empty() );
2056 std::vector
< Node
>::iterator itn
= std::find( normal_form_src
.begin(), normal_form_src
.end(), eqc
);
2057 if( itn
!=normal_form_src
.end() ){
2058 nf_index
= itn
- normal_form_src
.begin();
2059 Trace("strings-solve-debug2") << "take normal form " << nf_index
<< std::endl
;
2060 Assert( normal_form_src
[nf_index
]==eqc
);
2062 //just take the first normal form
2063 Trace("strings-solve-debug2") << "take the first normal form" << std::endl
;
2065 d_normal_forms
[eqc
].insert( d_normal_forms
[eqc
].end(), normal_forms
[nf_index
].begin(), normal_forms
[nf_index
].end() );
2066 d_normal_forms_exp
[eqc
].insert( d_normal_forms_exp
[eqc
].end(), normal_forms_exp
[nf_index
].begin(), normal_forms_exp
[nf_index
].end() );
2067 Trace("strings-solve-debug2") << "take normal form ... done" << std::endl
;
2068 d_normal_forms_base
[eqc
] = normal_form_src
[nf_index
];
2069 //track dependencies
2070 for( unsigned i
=0; i
<normal_forms_exp
[nf_index
].size(); i
++ ){
2071 Node exp
= normal_forms_exp
[nf_index
][i
];
2072 for( unsigned r
=0; r
<2; r
++ ){
2073 d_normal_forms_exp_depend
[eqc
][exp
][r
==0] = normal_forms_exp_depend
[nf_index
][exp
][r
==0];
2076 Trace("strings-process-debug") << "Return process equivalence class " << eqc
<< " : returned, size = " << d_normal_forms
[eqc
].size() << std::endl
;
2080 void TheoryStrings::getNormalForms( Node
&eqc
, std::vector
< std::vector
< Node
> > &normal_forms
, std::vector
< Node
> &normal_form_src
,
2081 std::vector
< std::vector
< Node
> > &normal_forms_exp
, std::vector
< std::map
< Node
, std::map
< bool, int > > >& normal_forms_exp_depend
) {
2082 //constant for equivalence class
2083 Node eqc_non_c
= eqc
;
2084 Trace("strings-process-debug") << "Get normal forms " << eqc
<< std::endl
;
2085 eq::EqClassIterator eqc_i
= eq::EqClassIterator( eqc
, &d_equalityEngine
);
2086 while( !eqc_i
.isFinished() ){
2088 if( d_congruent
.find( n
)==d_congruent
.end() ){
2089 if( n
.getKind() == kind::CONST_STRING
|| n
.getKind() == kind::STRING_CONCAT
){
2090 Trace("strings-process-debug") << "Get Normal Form : Process term " << n
<< " in eqc " << eqc
<< std::endl
;
2091 std::vector
< Node
> nf_n
;
2092 std::vector
< Node
> nf_exp_n
;
2093 std::map
< Node
, std::map
< bool, int > > nf_exp_depend_n
;
2094 if( n
.getKind()==kind::CONST_STRING
){
2095 if( n
!=d_emptyString
) {
2096 nf_n
.push_back( n
);
2098 }else if( n
.getKind()==kind::STRING_CONCAT
){
2099 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ) {
2100 Node nr
= d_equalityEngine
.getRepresentative( n
[i
] );
2101 Trace("strings-process-debug") << "Normalizing subterm " << n
[i
] << " = " << nr
<< std::endl
;
2102 Assert( d_normal_forms
.find( nr
)!=d_normal_forms
.end() );
2103 unsigned orig_size
= nf_n
.size();
2104 unsigned add_size
= d_normal_forms
[nr
].size();
2105 //if not the empty string, add to current normal form
2106 if( !d_normal_forms
[nr
].empty() ){
2107 for( unsigned r
=0; r
<d_normal_forms
[nr
].size(); r
++ ) {
2108 if( Trace
.isOn("strings-error") ) {
2109 if( d_normal_forms
[nr
][r
].getKind()==kind::STRING_CONCAT
){
2110 Trace("strings-error") << "Strings::Error: From eqc = " << eqc
<< ", " << n
<< " index " << i
<< ", bad normal form : ";
2111 for( unsigned rr
=0; rr
<d_normal_forms
[nr
].size(); rr
++ ) {
2112 Trace("strings-error") << d_normal_forms
[nr
][rr
] << " ";
2114 Trace("strings-error") << std::endl
;
2117 Assert( d_normal_forms
[nr
][r
].getKind()!=kind::STRING_CONCAT
);
2119 nf_n
.insert( nf_n
.end(), d_normal_forms
[nr
].begin(), d_normal_forms
[nr
].end() );
2122 for( unsigned j
=0; j
<d_normal_forms_exp
[nr
].size(); j
++ ){
2123 Node exp
= d_normal_forms_exp
[nr
][j
];
2124 nf_exp_n
.push_back( exp
);
2126 for( unsigned k
=0; k
<2; k
++ ){
2127 int prev_dep
= d_normal_forms_exp_depend
[nr
][exp
][k
==1];
2129 nf_exp_depend_n
[exp
][false] = orig_size
+ prev_dep
;
2131 //store forward index (converted back to reverse index below)
2132 nf_exp_depend_n
[exp
][true] = orig_size
+ ( add_size
- prev_dep
);
2136 if( d_normal_forms_base
[nr
]!=n
[i
] ){
2137 Assert( d_normal_forms_base
.find( nr
)!=d_normal_forms_base
.end() );
2138 Node eq
= n
[i
].eqNode( d_normal_forms_base
[nr
] );
2139 nf_exp_n
.push_back( eq
);
2141 nf_exp_depend_n
[eq
][false] = orig_size
;
2142 nf_exp_depend_n
[eq
][true] = orig_size
+ add_size
;
2145 //convert forward indices to reverse indices
2146 int total_size
= nf_n
.size();
2147 for( std::map
< Node
, std::map
< bool, int > >::iterator it
= nf_exp_depend_n
.begin(); it
!= nf_exp_depend_n
.end(); ++it
){
2148 it
->second
[true] = total_size
- it
->second
[true];
2149 Assert( it
->second
[true]>=0 );
2152 //if not equal to self
2153 if( nf_n
.size()>1 || ( nf_n
.size()==1 && nf_n
[0].getKind()==kind::CONST_STRING
) ){
2154 if( nf_n
.size()>1 ) {
2155 for( unsigned i
=0; i
<nf_n
.size(); i
++ ){
2156 if( Trace
.isOn("strings-error") ){
2157 Trace("strings-error") << "Cycle for normal form ";
2158 printConcat(nf_n
,"strings-error");
2159 Trace("strings-error") << "..." << nf_n
[i
] << std::endl
;
2161 Assert( !areEqual( nf_n
[i
], n
) );
2164 normal_forms
.push_back(nf_n
);
2165 normal_form_src
.push_back(n
);
2166 normal_forms_exp
.push_back(nf_exp_n
);
2167 normal_forms_exp_depend
.push_back(nf_exp_depend_n
);
2169 //this was redundant: combination of self + empty string(s)
2170 Node nn
= nf_n
.size()==0 ? d_emptyString
: nf_n
[0];
2171 Assert( areEqual( nn
, eqc
) );
2180 if( normal_forms
.empty() ) {
2181 Trace("strings-solve-debug2") << "construct the normal form" << std::endl
;
2182 //do not choose a concat here use "eqc_non_c" (in this case they have non-trivial explanation why they normalize to self)
2183 std::vector
< Node
> eqc_non_c_nf
;
2184 getConcatVec( eqc_non_c
, eqc_non_c_nf
);
2185 normal_forms
.push_back( eqc_non_c_nf
);
2186 normal_form_src
.push_back( eqc_non_c
);
2187 normal_forms_exp
.push_back( std::vector
< Node
>() );
2188 normal_forms_exp_depend
.push_back( std::map
< Node
, std::map
< bool, int > >() );
2190 if(Trace
.isOn("strings-solve")) {
2191 Trace("strings-solve") << "--- Normal forms for equivlance class " << eqc
<< " : " << std::endl
;
2192 for( unsigned i
=0; i
<normal_forms
.size(); i
++ ) {
2193 Trace("strings-solve") << "#" << i
<< " (from " << normal_form_src
[i
] << ") : ";
2194 for( unsigned j
=0; j
<normal_forms
[i
].size(); j
++ ) {
2196 Trace("strings-solve") << ", ";
2198 Trace("strings-solve") << normal_forms
[i
][j
];
2200 Trace("strings-solve") << std::endl
;
2201 Trace("strings-solve") << " Explanation is : ";
2202 if(normal_forms_exp
[i
].size() == 0) {
2203 Trace("strings-solve") << "NONE";
2205 for( unsigned j
=0; j
<normal_forms_exp
[i
].size(); j
++ ) {
2207 Trace("strings-solve") << " AND ";
2209 Trace("strings-solve") << normal_forms_exp
[i
][j
];
2211 Trace("strings-solve") << std::endl
;
2212 Trace("strings-solve") << "WITH DEPENDENCIES : " << std::endl
;
2213 for( unsigned j
=0; j
<normal_forms_exp
[i
].size(); j
++ ) {
2214 Trace("strings-solve") << " " << normal_forms_exp
[i
][j
] << " -> ";
2215 Trace("strings-solve") << normal_forms_exp_depend
[i
][normal_forms_exp
[i
][j
]][false] << ",";
2216 Trace("strings-solve") << normal_forms_exp_depend
[i
][normal_forms_exp
[i
][j
]][true] << std::endl
;
2219 Trace("strings-solve") << std::endl
;
2223 Trace("strings-solve") << "--- Single normal form for equivalence class " << eqc
<< std::endl
;
2226 //if equivalence class is constant, approximate as containment, infer conflicts
2227 Node c
= getConstantEqc( eqc
);
2229 Trace("strings-solve") << "Eqc is constant " << c
<< std::endl
;
2230 for( unsigned i
=0; i
<normal_forms
.size(); i
++ ) {
2232 if( !TheoryStringsRewriter::canConstantContainList( c
, normal_forms
[i
], firstc
, lastc
) ){
2233 Node n
= normal_form_src
[i
];
2235 Trace("strings-solve") << "Normal form for " << n
<< " cannot be contained in constant " << c
<< std::endl
;
2236 //conflict, explanation is n = base ^ base = c ^ relevant porition of ( n = N[n] )
2237 std::vector
< Node
> exp
;
2238 Assert( d_eqc_to_const_base
.find( eqc
)!=d_eqc_to_const_base
.end() );
2239 addToExplanation( n
, d_eqc_to_const_base
[eqc
], exp
);
2240 Assert( d_eqc_to_const_exp
.find( eqc
)!=d_eqc_to_const_exp
.end() );
2241 if( !d_eqc_to_const_exp
[eqc
].isNull() ){
2242 exp
.push_back( d_eqc_to_const_exp
[eqc
] );
2244 //TODO: this can be minimized based on firstc/lastc, normal_forms_exp_depend
2245 exp
.insert( exp
.end(), normal_forms_exp
[i
].begin(), normal_forms_exp
[i
].end() );
2246 Node conc
= d_false
;
2247 sendInference( exp
, conc
, "N_NCTN" );
2254 void TheoryStrings::getExplanationVectorForPrefix( std::vector
< std::vector
< Node
> > &normal_forms_exp
, std::vector
< std::map
< Node
, std::map
< bool, int > > >& normal_forms_exp_depend
,
2255 unsigned i
, int index
, bool isRev
, std::vector
< Node
>& curr_exp
) {
2256 if( index
==-1 || !options::stringMinPrefixExplain() ){
2257 curr_exp
.insert(curr_exp
.end(), normal_forms_exp
[i
].begin(), normal_forms_exp
[i
].end() );
2259 for( unsigned k
=0; k
<normal_forms_exp
[i
].size(); k
++ ){
2260 Node exp
= normal_forms_exp
[i
][k
];
2261 int dep
= normal_forms_exp_depend
[i
][exp
][isRev
];
2263 curr_exp
.push_back( exp
);
2264 Trace("strings-explain-prefix-debug") << " include : " << exp
<< std::endl
;
2266 Trace("strings-explain-prefix-debug") << " exclude : " << exp
<< std::endl
;
2272 void TheoryStrings::getExplanationVectorForPrefixEq( std::vector
< std::vector
< Node
> > &normal_forms
, std::vector
< Node
> &normal_form_src
,
2273 std::vector
< std::vector
< Node
> > &normal_forms_exp
, std::vector
< std::map
< Node
, std::map
< bool, int > > >& normal_forms_exp_depend
,
2274 unsigned i
, unsigned j
, int index_i
, int index_j
, bool isRev
, std::vector
< Node
>& curr_exp
) {
2275 Trace("strings-explain-prefix") << "Get explanation for prefix " << index_i
<< ", " << index_j
<< " of normal forms " << i
<< " and " << j
<< ", reverse = " << isRev
<< std::endl
;
2276 for( unsigned r
=0; r
<2; r
++ ){
2277 getExplanationVectorForPrefix( normal_forms_exp
, normal_forms_exp_depend
, r
==0 ? i
: j
, r
==0 ? index_i
: index_j
, isRev
, curr_exp
);
2279 Trace("strings-explain-prefix") << "Included " << curr_exp
.size() << " / " << ( normal_forms_exp
[i
].size() + normal_forms_exp
[j
].size() ) << std::endl
;
2280 addToExplanation( normal_form_src
[i
], normal_form_src
[j
], curr_exp
);
2284 void TheoryStrings::processNEqc( std::vector
< std::vector
< Node
> > &normal_forms
, std::vector
< Node
> &normal_form_src
,
2285 std::vector
< std::vector
< Node
> > &normal_forms_exp
, std::vector
< std::map
< Node
, std::map
< bool, int > > >& normal_forms_exp_depend
){
2286 //the possible inferences
2287 std::vector
< InferInfo
> pinfer
;
2288 // loop over all pairs
2289 for(unsigned i
=0; i
<normal_forms
.size()-1; i
++) {
2290 //unify each normalform[j] with normal_forms[i]
2291 for(unsigned j
=i
+1; j
<normal_forms
.size(); j
++ ) {
2292 //ensure that normal_forms[i] and normal_forms[j] are the same modulo equality, add to pinfer if not
2293 Trace("strings-solve") << "Strings: Process normal form #" << i
<< " against #" << j
<< "..." << std::endl
;
2294 if( isNormalFormPair( normal_form_src
[i
], normal_form_src
[j
] ) ) {
2295 Trace("strings-solve") << "Strings: Already cached." << std::endl
;
2297 //process the reverse direction first (check for easy conflicts and inferences)
2298 unsigned rindex
= 0;
2299 processReverseNEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, rindex
, 0, pinfer
);
2300 if( hasProcessed() ){
2302 }else if( !pinfer
.empty() && pinfer
.back().d_id
==1 ){
2305 //AJR: for less aggressive endpoint inference
2309 processSimpleNEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, index
, false, rindex
, pinfer
);
2310 if( hasProcessed() ){
2312 }else if( !pinfer
.empty() && pinfer
.back().d_id
==1 ){
2318 if( !pinfer
.empty() ){
2319 //now, determine which of the possible inferences we want to add
2321 Trace("strings-solve") << "Possible inferences (" << pinfer
.size() << ") : " << std::endl
;
2322 unsigned min_id
= 9;
2323 unsigned max_index
= 0;
2324 for( unsigned i
=0; i
<pinfer
.size(); i
++ ){
2325 Trace("strings-solve") << "From " << pinfer
[i
].d_i
<< " / " << pinfer
[i
].d_j
<< " (rev=" << pinfer
[i
].d_rev
<< ") : ";
2326 Trace("strings-solve") << pinfer
[i
].d_conc
<< " by " << pinfer
[i
].getId() << std::endl
;
2327 if( use_index
==-1 || pinfer
[i
].d_id
<min_id
|| ( pinfer
[i
].d_id
==min_id
&& pinfer
[i
].d_index
>max_index
) ){
2328 min_id
= pinfer
[i
].d_id
;
2329 max_index
= pinfer
[i
].d_index
;
2333 //send the inference
2334 sendInference( pinfer
[use_index
].d_ant
, pinfer
[use_index
].d_antn
, pinfer
[use_index
].d_conc
, pinfer
[use_index
].getId(), pinfer
[use_index
].sendAsLemma() );
2335 for( std::map
< int, std::vector
< Node
> >::iterator it
= pinfer
[use_index
].d_new_skolem
.begin(); it
!= pinfer
[use_index
].d_new_skolem
.end(); ++it
){
2336 for( unsigned i
=0; i
<it
->second
.size(); i
++ ){
2338 sendLengthLemma( it
->second
[i
] );
2339 }else if( it
->first
==1 ){
2340 registerNonEmptySkolem( it
->second
[i
] );
2347 bool TheoryStrings::InferInfo::sendAsLemma() {
2351 void TheoryStrings::processReverseNEq( std::vector
< std::vector
< Node
> > &normal_forms
, std::vector
< Node
> &normal_form_src
,
2352 std::vector
< std::vector
< Node
> > &normal_forms_exp
, std::vector
< std::map
< Node
, std::map
< bool, int > > >& normal_forms_exp_depend
,
2353 unsigned i
, unsigned j
, unsigned& index
, unsigned rproc
, std::vector
< InferInfo
>& pinfer
) {
2354 //reverse normal form of i, j
2355 std::reverse( normal_forms
[i
].begin(), normal_forms
[i
].end() );
2356 std::reverse( normal_forms
[j
].begin(), normal_forms
[j
].end() );
2358 processSimpleNEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, index
, true, rproc
, pinfer
);
2360 //reverse normal form of i, j
2361 std::reverse( normal_forms
[i
].begin(), normal_forms
[i
].end() );
2362 std::reverse( normal_forms
[j
].begin(), normal_forms
[j
].end() );
2365 //rproc is the # is the size of suffix that is identical
2366 void TheoryStrings::processSimpleNEq( std::vector
< std::vector
< Node
> > &normal_forms
, std::vector
< Node
> &normal_form_src
,
2367 std::vector
< std::vector
< Node
> > &normal_forms_exp
, std::vector
< std::map
< Node
, std::map
< bool, int > > >& normal_forms_exp_depend
,
2368 unsigned i
, unsigned j
, unsigned& index
, bool isRev
, unsigned rproc
, std::vector
< InferInfo
>& pinfer
) {
2369 Assert( rproc
<=normal_forms
[i
].size() && rproc
<=normal_forms
[j
].size() );
2373 //if we are at the end
2374 if( index
==(normal_forms
[i
].size()-rproc
) || index
==(normal_forms
[j
].size()-rproc
) ){
2375 if( index
==(normal_forms
[i
].size()-rproc
) && index
==(normal_forms
[j
].size()-rproc
) ){
2378 //the remainder must be empty
2379 unsigned k
= index
==(normal_forms
[i
].size()-rproc
) ? j
: i
;
2380 unsigned index_k
= index
;
2381 //Node eq_exp = mkAnd( curr_exp );
2382 std::vector
< Node
> curr_exp
;
2383 getExplanationVectorForPrefixEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, -1, -1, isRev
, curr_exp
);
2384 while( !d_conflict
&& index_k
<(normal_forms
[k
].size()-rproc
) ){
2385 //can infer that this string must be empty
2386 Node eq
= normal_forms
[k
][index_k
].eqNode( d_emptyString
);
2387 //Trace("strings-lemma") << "Strings: Infer " << eq << " from " << eq_exp << std::endl;
2388 Assert( !areEqual( d_emptyString
, normal_forms
[k
][index_k
] ) );
2389 sendInference( curr_exp
, eq
, "N_EndpointEmp" );
2394 Trace("strings-solve-debug") << "Process " << normal_forms
[i
][index
] << " ... " << normal_forms
[j
][index
] << std::endl
;
2395 if( normal_forms
[i
][index
]==normal_forms
[j
][index
] ){
2396 Trace("strings-solve-debug") << "Simple Case 1 : strings are equal" << std::endl
;
2400 Assert( !areEqual(normal_forms
[i
][index
], normal_forms
[j
][index
]) );
2401 std::vector
< Node
> temp_exp
;
2402 Node length_term_i
= getLength( normal_forms
[i
][index
], temp_exp
);
2403 Node length_term_j
= getLength( normal_forms
[j
][index
], temp_exp
);
2404 //check length(normal_forms[i][index]) == length(normal_forms[j][index])
2405 if( areEqual( length_term_i
, length_term_j
) ){
2406 Trace("strings-solve-debug") << "Simple Case 2 : string lengths are equal" << std::endl
;
2407 Node eq
= normal_forms
[i
][index
].eqNode( normal_forms
[j
][index
] );
2408 //eq = Rewriter::rewrite( eq );
2409 Node length_eq
= length_term_i
.eqNode( length_term_j
);
2410 //temp_exp.insert(temp_exp.end(), curr_exp.begin(), curr_exp.end() );
2411 getExplanationVectorForPrefixEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, index
, index
, isRev
, temp_exp
);
2412 temp_exp
.push_back(length_eq
);
2413 sendInference( temp_exp
, eq
, "N_Unify" );
2415 }else if( ( normal_forms
[i
][index
].getKind()!=kind::CONST_STRING
&& index
==normal_forms
[i
].size()-rproc
-1 ) ||
2416 ( normal_forms
[j
][index
].getKind()!=kind::CONST_STRING
&& index
==normal_forms
[j
].size()-rproc
-1 ) ){
2417 Trace("strings-solve-debug") << "Simple Case 3 : at endpoint" << std::endl
;
2418 std::vector
< Node
> antec
;
2419 //antec.insert(antec.end(), curr_exp.begin(), curr_exp.end() );
2420 getExplanationVectorForPrefixEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, -1, -1, isRev
, antec
);
2421 std::vector
< Node
> eqn
;
2422 for( unsigned r
=0; r
<2; r
++ ) {
2423 int index_k
= index
;
2424 int k
= r
==0 ? i
: j
;
2425 std::vector
< Node
> eqnc
;
2426 for( unsigned index_l
=index_k
; index_l
<(normal_forms
[k
].size()-rproc
); index_l
++ ) {
2428 eqnc
.insert(eqnc
.begin(), normal_forms
[k
][index_l
] );
2430 eqnc
.push_back( normal_forms
[k
][index_l
] );
2433 eqn
.push_back( mkConcat( eqnc
) );
2435 if( !areEqual( eqn
[0], eqn
[1] ) ){
2436 sendInference( antec
, eqn
[0].eqNode( eqn
[1] ), "N_EndpointEq", true );
2439 Assert( normal_forms
[i
].size()==normal_forms
[j
].size() );
2440 index
= normal_forms
[i
].size()-rproc
;
2442 }else if( normal_forms
[i
][index
].isConst() && normal_forms
[j
][index
].isConst() ){
2443 Node const_str
= normal_forms
[i
][index
];
2444 Node other_str
= normal_forms
[j
][index
];
2445 Trace("strings-solve-debug") << "Simple Case 3 : Const Split : " << const_str
<< " vs " << other_str
<< " at index " << index
<< ", isRev = " << isRev
<< std::endl
;
2446 unsigned len_short
= const_str
.getConst
<String
>().size() <= other_str
.getConst
<String
>().size() ? const_str
.getConst
<String
>().size() : other_str
.getConst
<String
>().size();
2447 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
);
2449 //same prefix/suffix
2450 //k is the index of the string that is shorter
2451 int k
= const_str
.getConst
<String
>().size()<other_str
.getConst
<String
>().size() ? i
: j
;
2452 int l
= const_str
.getConst
<String
>().size()<other_str
.getConst
<String
>().size() ? j
: i
;
2453 //update the nf exp dependencies
2454 //notice this is not critical for soundness: not doing the below incrementing will only lead to overapproximating when antecedants are required in explanations
2455 for( std::map
< Node
, std::map
< bool, int > >::iterator itnd
= normal_forms_exp_depend
[l
].begin(); itnd
!= normal_forms_exp_depend
[l
].end(); ++itnd
){
2456 for( std::map
< bool, int >::iterator itnd2
= itnd
->second
.begin(); itnd2
!= itnd
->second
.end(); ++itnd2
){
2457 //see if this can be incremented: it can if it is not relevant to the current index
2458 Assert( itnd2
->second
>=0 && itnd2
->second
<=(int)normal_forms
[l
].size() );
2459 bool increment
= (itnd2
->first
==isRev
) ? itnd2
->second
>(int)index
: ( (int)normal_forms
[l
].size()-1-itnd2
->second
)<(int)index
;
2461 normal_forms_exp_depend
[l
][itnd
->first
][itnd2
->first
] = itnd2
->second
+ 1;
2466 int new_len
= normal_forms
[l
][index
].getConst
<String
>().size() - len_short
;
2467 Node remainderStr
= NodeManager::currentNM()->mkConst( normal_forms
[l
][index
].getConst
<String
>().substr(0, new_len
) );
2468 Trace("strings-solve-debug-test") << "Break normal form of " << normal_forms
[l
][index
] << " into " << normal_forms
[k
][index
] << ", " << remainderStr
<< std::endl
;
2469 normal_forms
[l
].insert( normal_forms
[l
].begin()+index
+ 1, remainderStr
);
2471 Node remainderStr
= NodeManager::currentNM()->mkConst(normal_forms
[l
][index
].getConst
<String
>().substr(len_short
));
2472 Trace("strings-solve-debug-test") << "Break normal form of " << normal_forms
[l
][index
] << " into " << normal_forms
[k
][index
] << ", " << remainderStr
<< std::endl
;
2473 normal_forms
[l
].insert( normal_forms
[l
].begin()+index
+ 1, remainderStr
);
2475 normal_forms
[l
][index
] = normal_forms
[k
][index
];
2480 std::vector
< Node
> antec
;
2481 getExplanationVectorForPrefixEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, index
, index
, isRev
, antec
);
2482 sendInference( antec
, d_false
, "N_Const", true );
2486 //construct the candidate inference "info"
2488 info
.d_index
= index
;
2493 bool info_valid
= false;
2494 Assert( index
<normal_forms
[i
].size()-rproc
&& index
<normal_forms
[j
].size()-rproc
);
2495 std::vector
< Node
> lexp
;
2496 Node length_term_i
= getLength( normal_forms
[i
][index
], lexp
);
2497 Node length_term_j
= getLength( normal_forms
[j
][index
], lexp
);
2498 //split on equality between string lengths (note that splitting on equality between strings is worse since it is harder to process)
2499 if( !areDisequal( length_term_i
, length_term_j
) && !areEqual( length_term_i
, length_term_j
) &&
2500 normal_forms
[i
][index
].getKind()!=kind::CONST_STRING
&& normal_forms
[j
][index
].getKind()!=kind::CONST_STRING
){ //AJR: remove the latter 2 conditions?
2501 Trace("strings-solve-debug") << "Non-simple Case 1 : string lengths neither equal nor disequal" << std::endl
;
2502 //try to make the lengths equal via splitting on demand
2503 Node length_eq
= NodeManager::currentNM()->mkNode( kind::EQUAL
, length_term_i
, length_term_j
);
2504 length_eq
= Rewriter::rewrite( length_eq
);
2506 info
.d_conc
= NodeManager::currentNM()->mkNode( kind::OR
, length_eq
, length_eq
.negate() );
2507 info
.d_pending_phase
[ length_eq
] = true;
2511 Trace("strings-solve-debug") << "Non-simple Case 2 : must compare strings" << std::endl
;
2514 if( detectLoop( normal_forms
, i
, j
, index
, loop_in_i
, loop_in_j
, rproc
) ){
2515 if( !isRev
){ //FIXME
2516 getExplanationVectorForPrefixEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, -1, -1, isRev
, info
.d_ant
);
2518 if( processLoop( normal_forms
, normal_form_src
, i
, j
, loop_in_i
!=-1 ? i
: j
, loop_in_i
!=-1 ? j
: i
, loop_in_i
!=-1 ? loop_in_i
: loop_in_j
, index
, info
) ){
2523 //AJR: length entailment here?
2524 if( normal_forms
[i
][index
].getKind() == kind::CONST_STRING
|| normal_forms
[j
][index
].getKind() == kind::CONST_STRING
){
2525 unsigned const_k
= normal_forms
[i
][index
].getKind() == kind::CONST_STRING
? i
: j
;
2526 unsigned nconst_k
= normal_forms
[i
][index
].getKind() == kind::CONST_STRING
? j
: i
;
2527 Node other_str
= normal_forms
[nconst_k
][index
];
2528 Assert( other_str
.getKind()!=kind::CONST_STRING
, "Other string is not constant." );
2529 Assert( other_str
.getKind()!=kind::STRING_CONCAT
, "Other string is not CONCAT." );
2530 if( !d_equalityEngine
.areDisequal( other_str
, d_emptyString
, true ) ){
2531 Node eq
= other_str
.eqNode( d_emptyString
);
2533 info
.d_conc
= NodeManager::currentNM()->mkNode( kind::OR
, eq
, eq
.negate() );
2537 if( !isRev
){ //FIXME
2538 Node xnz
= other_str
.eqNode( d_emptyString
).negate();
2539 unsigned index_nc_k
= index
+1;
2540 //Node next_const_str = TheoryStringsRewriter::collectConstantStringAt( normal_forms[nconst_k], index_nc_k, false );
2541 unsigned start_index_nc_k
= index
+1;
2542 Node next_const_str
= TheoryStringsRewriter::getNextConstantAt( normal_forms
[nconst_k
], start_index_nc_k
, index_nc_k
, false );
2543 if( !next_const_str
.isNull() ) {
2544 unsigned index_c_k
= index
;
2545 Node const_str
= TheoryStringsRewriter::collectConstantStringAt( normal_forms
[const_k
], index_c_k
, false );
2546 Assert( !const_str
.isNull() );
2547 CVC4::String stra
= const_str
.getConst
<String
>();
2548 CVC4::String strb
= next_const_str
.getConst
<String
>();
2549 //since non-empty, we start with charecter #1
2552 CVC4::String stra1
= stra
.prefix( stra
.size()-1 );
2553 p
= stra
.size() - stra1
.roverlap(strb
);
2554 Trace("strings-csp-debug") << "Compute roverlap : " << const_str
<< " " << next_const_str
<< std::endl
;
2555 size_t p2
= stra1
.rfind(strb
);
2556 p
= p2
==std::string::npos
? p
: ( p
>p2
+1? p2
+1 : p
);
2557 Trace("strings-csp-debug") << "overlap : " << stra1
<< " " << strb
<< " returned " << p
<< " " << p2
<< " " << (p2
==std::string::npos
) << std::endl
;
2559 CVC4::String stra1
= stra
.substr( 1 );
2560 p
= stra
.size() - stra1
.overlap(strb
);
2561 Trace("strings-csp-debug") << "Compute overlap : " << const_str
<< " " << next_const_str
<< std::endl
;
2562 size_t p2
= stra1
.find(strb
);
2563 p
= p2
==std::string::npos
? p
: ( p
>p2
+1? p2
+1 : p
);
2564 Trace("strings-csp-debug") << "overlap : " << stra1
<< " " << strb
<< " returned " << p
<< " " << p2
<< " " << (p2
==std::string::npos
) << std::endl
;
2567 if( start_index_nc_k
==index
+1 ){
2568 info
.d_ant
.push_back( xnz
);
2569 getExplanationVectorForPrefixEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
,
2570 const_k
, nconst_k
, index_c_k
, index_nc_k
, isRev
, info
.d_ant
);
2571 Node prea
= p
==stra
.size() ? const_str
: NodeManager::currentNM()->mkConst( isRev
? stra
.suffix( p
) : stra
.prefix( p
) );
2572 Node sk
= mkSkolemCached( other_str
, prea
, isRev
? sk_id_c_spt_rev
: sk_id_c_spt
, "c_spt", -1 );
2573 Trace("strings-csp") << "Const Split: " << prea
<< " is removed from " << stra
<< " due to " << strb
<< ", p=" << p
<< std::endl
;
2575 info
.d_conc
= other_str
.eqNode( isRev
? mkConcat( sk
, prea
) : mkConcat(prea
, sk
) );
2576 info
.d_new_skolem
[0].push_back( sk
);
2580 /* FIXME for isRev, speculative
2581 else if( options::stringLenPropCsp() ){
2582 //propagate length constraint
2583 std::vector< Node > cc;
2584 for( unsigned i=index; i<start_index_nc_k; i++ ){
2585 cc.push_back( normal_forms[nconst_k][i] );
2587 Node lt = NodeManager::currentNM()->mkNode( kind::STRING_LENGTH, mkConcat( cc ) );
2588 conc = NodeManager::currentNM()->mkNode( kind::GEQ, lt, NodeManager::currentNM()->mkConst( Rational(p) ) );
2589 sendInference( ant, conc, "S-Split(CSP-P)-lprop", true );
2595 info
.d_ant
.push_back( xnz
);
2596 Node const_str
= normal_forms
[const_k
][index
];
2597 getExplanationVectorForPrefixEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, index
, index
, isRev
, info
.d_ant
);
2598 CVC4::String stra
= const_str
.getConst
<String
>();
2599 if( options::stringBinaryCsp() && stra
.size()>3 ){
2600 //split string in half
2601 Node c_firstHalf
= NodeManager::currentNM()->mkConst( isRev
? stra
.substr( stra
.size()/2 ) : stra
.substr(0, stra
.size()/2 ) );
2602 Node sk
= mkSkolemCached( other_str
, c_firstHalf
, isRev
? sk_id_vc_bin_spt_rev
: sk_id_vc_bin_spt
, "cb_spt", -1 );
2603 Trace("strings-csp") << "Const Split: " << c_firstHalf
<< " is removed from " << const_str
<< " (binary) " << std::endl
;
2604 info
.d_conc
= NodeManager::currentNM()->mkNode( kind::OR
, other_str
.eqNode( isRev
? mkConcat( sk
, c_firstHalf
) : mkConcat( c_firstHalf
, sk
) ),
2605 NodeManager::currentNM()->mkNode( kind::AND
,
2606 sk
.eqNode( d_emptyString
).negate(),
2607 c_firstHalf
.eqNode( isRev
? mkConcat( sk
, other_str
) : mkConcat( other_str
, sk
) ) ) );
2608 info
.d_new_skolem
[0].push_back( sk
);
2613 Node firstChar
= stra
.size() == 1 ? const_str
: NodeManager::currentNM()->mkConst( isRev
? stra
.suffix( 1 ) : stra
.prefix( 1 ) );
2614 Node sk
= mkSkolemCached( other_str
, firstChar
, isRev
? sk_id_vc_spt_rev
: sk_id_vc_spt
, "c_spt", -1 );
2615 Trace("strings-csp") << "Const Split: " << firstChar
<< " is removed from " << const_str
<< " (serial) " << std::endl
;
2616 info
.d_conc
= other_str
.eqNode( isRev
? mkConcat( sk
, firstChar
) : mkConcat(firstChar
, sk
) );
2617 info
.d_new_skolem
[0].push_back( sk
);
2625 int lentTestSuccess
= -1;
2627 if( options::stringCheckEntailLen() ){
2629 for( unsigned e
=0; e
<2; e
++ ){
2630 Node t
= e
==0 ? normal_forms
[i
][index
] : normal_forms
[j
][index
];
2631 //do not infer constants are larger than variables
2632 if( t
.getKind()!=kind::CONST_STRING
){
2633 Node lt1
= e
==0 ? length_term_i
: length_term_j
;
2634 Node lt2
= e
==0 ? length_term_j
: length_term_i
;
2635 Node ent_lit
= Rewriter::rewrite( NodeManager::currentNM()->mkNode( kind::GT
, lt1
, lt2
) );
2636 std::pair
<bool, Node
> et
= d_valuation
.entailmentCheck( THEORY_OF_TYPE_BASED
, ent_lit
);
2638 Trace("strings-entail") << "Strings entailment : " << ent_lit
<< " is entailed in the current context." << std::endl
;
2639 Trace("strings-entail") << " explanation was : " << et
.second
<< std::endl
;
2640 lentTestSuccess
= e
;
2641 lentTestExp
= et
.second
;
2648 getExplanationVectorForPrefixEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, index
, index
, isRev
, info
.d_ant
);
2650 for(unsigned xory
=0; xory
<2; xory
++) {
2651 Node x
= xory
==0 ? normal_forms
[i
][index
] : normal_forms
[j
][index
];
2652 Node xgtz
= x
.eqNode( d_emptyString
).negate();
2653 if( d_equalityEngine
.areDisequal( x
, d_emptyString
, true ) ) {
2654 info
.d_ant
.push_back( xgtz
);
2656 info
.d_antn
.push_back( xgtz
);
2659 Node sk
= mkSkolemCached( normal_forms
[i
][index
], normal_forms
[j
][index
], isRev
? sk_id_v_spt_rev
: sk_id_v_spt
, "v_spt", -1 );
2660 //must add length requirement
2661 info
.d_new_skolem
[1].push_back( sk
);
2662 Node eq1
= normal_forms
[i
][index
].eqNode( isRev
? mkConcat(sk
, normal_forms
[j
][index
]) : mkConcat(normal_forms
[j
][index
], sk
) );
2663 Node eq2
= normal_forms
[j
][index
].eqNode( isRev
? mkConcat(sk
, normal_forms
[i
][index
]) : mkConcat(normal_forms
[i
][index
], sk
) );
2665 if( lentTestSuccess
!=-1 ){
2666 info
.d_antn
.push_back( lentTestExp
);
2667 info
.d_conc
= lentTestSuccess
==0 ? eq1
: eq2
;
2671 Node ldeq
= NodeManager::currentNM()->mkNode( kind::EQUAL
, length_term_i
, length_term_j
).negate();
2672 if( d_equalityEngine
.areDisequal( length_term_i
, length_term_j
, true ) ){
2673 info
.d_ant
.push_back( ldeq
);
2675 info
.d_antn
.push_back(ldeq
);
2678 info
.d_conc
= NodeManager::currentNM()->mkNode( kind::OR
, eq1
, eq2
);
2686 pinfer
.push_back( info
);
2695 bool TheoryStrings::detectLoop( std::vector
< std::vector
< Node
> > &normal_forms
, int i
, int j
, int index
, int &loop_in_i
, int &loop_in_j
, unsigned rproc
){
2696 int has_loop
[2] = { -1, -1 };
2697 if( options::stringLB() != 2 ) {
2698 for( unsigned r
=0; r
<2; r
++ ) {
2699 int n_index
= (r
==0 ? i
: j
);
2700 int other_n_index
= (r
==0 ? j
: i
);
2701 if( normal_forms
[other_n_index
][index
].getKind() != kind::CONST_STRING
) {
2702 for( unsigned lp
= index
+1; lp
<normal_forms
[n_index
].size()-rproc
; lp
++ ){
2703 if( normal_forms
[n_index
][lp
]==normal_forms
[other_n_index
][index
] ){
2711 if( has_loop
[0]!=-1 || has_loop
[1]!=-1 ) {
2712 loop_in_i
= has_loop
[0];
2713 loop_in_j
= has_loop
[1];
2716 Trace("strings-solve-debug") << "No loops detected." << std::endl
;
2722 bool TheoryStrings::processLoop( std::vector
< std::vector
< Node
> > &normal_forms
, std::vector
< Node
> &normal_form_src
,
2723 int i
, int j
, int loop_n_index
, int other_n_index
, int loop_index
, int index
, InferInfo
& info
){
2724 if( options::stringAbortLoop() ){
2725 Message() << "Looping word equation encountered." << std::endl
;
2729 Trace("strings-loop") << "Detected possible loop for " << normal_forms
[loop_n_index
][loop_index
] << std::endl
;
2730 Trace("strings-loop") << " ... (X)= " << normal_forms
[other_n_index
][index
] << std::endl
;
2732 Trace("strings-loop") << " ... T(Y.Z)= ";
2733 std::vector
< Node
> vec_t
;
2734 for(int lp
=index
; lp
<loop_index
; ++lp
) {
2735 if(lp
!= index
) Trace("strings-loop") << " ++ ";
2736 Trace("strings-loop") << normal_forms
[loop_n_index
][lp
];
2737 vec_t
.push_back( normal_forms
[loop_n_index
][lp
] );
2739 Node t_yz
= mkConcat( vec_t
);
2740 Trace("strings-loop") << " (" << t_yz
<< ")" << std::endl
;
2741 Trace("strings-loop") << " ... S(Z.Y)= ";
2742 std::vector
< Node
> vec_s
;
2743 for(int lp
=index
+1; lp
<(int)normal_forms
[other_n_index
].size(); ++lp
) {
2744 if(lp
!= index
+1) Trace("strings-loop") << " ++ ";
2745 Trace("strings-loop") << normal_forms
[other_n_index
][lp
];
2746 vec_s
.push_back( normal_forms
[other_n_index
][lp
] );
2748 Node s_zy
= mkConcat( vec_s
);
2749 Trace("strings-loop") << " (" << s_zy
<< ")" << std::endl
;
2750 Trace("strings-loop") << " ... R= ";
2751 std::vector
< Node
> vec_r
;
2752 for(int lp
=loop_index
+1; lp
<(int)normal_forms
[loop_n_index
].size(); ++lp
) {
2753 if(lp
!= loop_index
+1) Trace("strings-loop") << " ++ ";
2754 Trace("strings-loop") << normal_forms
[loop_n_index
][lp
];
2755 vec_r
.push_back( normal_forms
[loop_n_index
][lp
] );
2757 Node r
= mkConcat( vec_r
);
2758 Trace("strings-loop") << " (" << r
<< ")" << std::endl
;
2760 //Trace("strings-loop") << "Lemma Cache: " << normal_form_src[i] << " vs " << normal_form_src[j] << std::endl;
2761 //TODO: can be more general
2762 if( s_zy
.isConst() && r
.isConst() && r
!=d_emptyString
) {
2765 if(s_zy
.getConst
<String
>().tailcmp( r
.getConst
<String
>(), c
) ) {
2767 s_zy
= NodeManager::currentNM()->mkConst( s_zy
.getConst
<String
>().substr(0, c
) );
2770 Trace("strings-loop") << "Strings::Loop: Refactor S(Z.Y)= " << s_zy
<< ", c=" << c
<< std::endl
;
2775 Trace("strings-loop") << "Strings::Loop: tails are different." << std::endl
;
2776 sendInference( info
.d_ant
, conc
, "Loop Conflict", true );
2781 //require that x is non-empty
2783 if( !areDisequal( normal_forms
[loop_n_index
][loop_index
], d_emptyString
) ){
2784 //try to make normal_forms[loop_n_index][loop_index] equal to empty to avoid loop
2785 split_eq
= normal_forms
[loop_n_index
][loop_index
].eqNode( d_emptyString
);
2786 }else if( !areDisequal( t_yz
, d_emptyString
) && t_yz
.getKind()!=kind::CONST_STRING
) {
2787 //try to make normal_forms[loop_n_index][loop_index] equal to empty to avoid loop
2788 split_eq
= t_yz
.eqNode( d_emptyString
);
2790 if( !split_eq
.isNull() ){
2791 info
.d_conc
= NodeManager::currentNM()->mkNode( kind::OR
, split_eq
, split_eq
.negate() );
2796 info
.d_ant
.push_back( normal_forms
[loop_n_index
][loop_index
].eqNode( d_emptyString
).negate() );
2797 if( t_yz
.getKind()!=kind::CONST_STRING
) {
2798 info
.d_ant
.push_back( t_yz
.eqNode( d_emptyString
).negate() );
2800 Node ant
= mkExplain( info
.d_ant
);
2801 if( d_loop_antec
.find( ant
) == d_loop_antec
.end() ){
2802 d_loop_antec
.insert( ant
);
2804 info
.d_antn
.push_back( ant
);
2808 r
== d_emptyString
&&
2810 s_zy
.getConst
<String
>().isRepeated()
2812 Node rep_c
= NodeManager::currentNM()->mkConst( s_zy
.getConst
<String
>().substr(0, 1) );
2813 Trace("strings-loop") << "Special case (X)=" << normal_forms
[other_n_index
][index
] << " " << std::endl
;
2814 Trace("strings-loop") << "... (C)=" << rep_c
<< " " << std::endl
;
2816 str_in_re
= NodeManager::currentNM()->mkNode( kind::STRING_IN_REGEXP
, normal_forms
[other_n_index
][index
],
2817 NodeManager::currentNM()->mkNode( kind::REGEXP_STAR
,
2818 NodeManager::currentNM()->mkNode( kind::STRING_TO_REGEXP
, rep_c
) ) );
2820 } else if(t_yz
.isConst()) {
2821 Trace("strings-loop") << "Strings::Loop: Const Normal Breaking." << std::endl
;
2822 CVC4::String s
= t_yz
.getConst
< CVC4::String
>();
2823 unsigned size
= s
.size();
2824 std::vector
< Node
> vconc
;
2825 for(unsigned len
=1; len
<=size
; len
++) {
2826 Node y
= NodeManager::currentNM()->mkConst(s
.substr(0, len
));
2827 Node z
= NodeManager::currentNM()->mkConst(s
.substr(len
, size
- len
));
2830 if(r
!= d_emptyString
) {
2831 std::vector
< Node
> v2(vec_r
);
2832 v2
.insert(v2
.begin(), y
);
2833 v2
.insert(v2
.begin(), z
);
2834 restr
= mkConcat( z
, y
);
2835 cc
= Rewriter::rewrite(s_zy
.eqNode( mkConcat( v2
) ));
2837 cc
= Rewriter::rewrite(s_zy
.eqNode( mkConcat( z
, y
) ));
2842 Node conc2
= NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP
, normal_forms
[other_n_index
][index
],
2843 NodeManager::currentNM()->mkNode(kind::REGEXP_CONCAT
,
2844 NodeManager::currentNM()->mkNode(kind::STRING_TO_REGEXP
, y
),
2845 NodeManager::currentNM()->mkNode(kind::REGEXP_STAR
,
2846 NodeManager::currentNM()->mkNode(kind::STRING_TO_REGEXP
, restr
))));
2847 cc
= cc
==d_true
? conc2
: NodeManager::currentNM()->mkNode( kind::AND
, cc
, conc2
);
2848 d_regexp_ant
[conc2
] = ant
;
2849 vconc
.push_back(cc
);
2851 conc
= vconc
.size()==0 ? Node::null() : vconc
.size()==1 ? vconc
[0] : NodeManager::currentNM()->mkNode(kind::OR
, vconc
);
2853 Trace("strings-loop") << "Strings::Loop: Normal Loop Breaking." << std::endl
;
2855 Node sk_w
= mkSkolemS( "w_loop" );
2856 Node sk_y
= mkSkolemS( "y_loop", 1 );
2857 Node sk_z
= mkSkolemS( "z_loop" );
2858 //t1 * ... * tn = y * z
2859 Node conc1
= t_yz
.eqNode( mkConcat( sk_y
, sk_z
) );
2860 // s1 * ... * sk = z * y * r
2861 vec_r
.insert(vec_r
.begin(), sk_y
);
2862 vec_r
.insert(vec_r
.begin(), sk_z
);
2863 Node conc2
= s_zy
.eqNode( mkConcat( vec_r
) );
2864 Node conc3
= normal_forms
[other_n_index
][index
].eqNode( mkConcat( sk_y
, sk_w
) );
2865 Node restr
= r
== d_emptyString
? s_zy
: mkConcat( sk_z
, sk_y
);
2866 str_in_re
= NodeManager::currentNM()->mkNode( kind::STRING_IN_REGEXP
, sk_w
,
2867 NodeManager::currentNM()->mkNode( kind::REGEXP_STAR
,
2868 NodeManager::currentNM()->mkNode( kind::STRING_TO_REGEXP
, restr
) ) );
2870 std::vector
< Node
> vec_conc
;
2871 vec_conc
.push_back(conc1
); vec_conc
.push_back(conc2
); vec_conc
.push_back(conc3
);
2872 vec_conc
.push_back(str_in_re
);
2873 //vec_conc.push_back(sk_y.eqNode(d_emptyString).negate());//by mkskolems
2874 conc
= NodeManager::currentNM()->mkNode( kind::AND
, vec_conc
);
2877 //set its antecedant to ant, to say when it is relevant
2878 if(!str_in_re
.isNull()) {
2879 d_regexp_ant
[str_in_re
] = ant
;
2882 addNormalFormPair( normal_form_src
[i
], normal_form_src
[j
] );
2883 if( options::stringProcessLoop() ){
2888 d_out
->setIncomplete();
2891 Trace("strings-loop") << "Strings::Loop: loop lemma for " << ant
<< " has already added." << std::endl
;
2892 addNormalFormPair( normal_form_src
[i
], normal_form_src
[j
] );
2899 //return true for lemma, false if we succeed
2900 void TheoryStrings::processDeq( Node ni
, Node nj
) {
2901 //Assert( areDisequal( ni, nj ) );
2902 if( d_normal_forms
[ni
].size()>1 || d_normal_forms
[nj
].size()>1 ){
2903 std::vector
< Node
> nfi
;
2904 nfi
.insert( nfi
.end(), d_normal_forms
[ni
].begin(), d_normal_forms
[ni
].end() );
2905 std::vector
< Node
> nfj
;
2906 nfj
.insert( nfj
.end(), d_normal_forms
[nj
].begin(), d_normal_forms
[nj
].end() );
2908 int revRet
= processReverseDeq( nfi
, nfj
, ni
, nj
);
2914 nfi
.insert( nfi
.end(), d_normal_forms
[ni
].begin(), d_normal_forms
[ni
].end() );
2916 nfj
.insert( nfj
.end(), d_normal_forms
[nj
].begin(), d_normal_forms
[nj
].end() );
2919 while( index
<nfi
.size() || index
<nfj
.size() ){
2920 int ret
= processSimpleDeq( nfi
, nfj
, ni
, nj
, index
, false );
2924 Assert( index
<nfi
.size() && index
<nfj
.size() );
2925 Node i
= nfi
[index
];
2926 Node j
= nfj
[index
];
2927 Trace("strings-solve-debug") << "...Processing(DEQ) " << i
<< " " << j
<< std::endl
;
2928 if( !areEqual( i
, j
) ){
2929 Assert( i
.getKind()!=kind::CONST_STRING
|| j
.getKind()!=kind::CONST_STRING
);
2930 std::vector
< Node
> lexp
;
2931 Node li
= getLength( i
, lexp
);
2932 Node lj
= getLength( j
, lexp
);
2933 if( areDisequal( li
, lj
) ){
2934 if( i
.getKind()==kind::CONST_STRING
|| j
.getKind()==kind::CONST_STRING
){
2936 Node const_k
= i
.getKind() == kind::CONST_STRING
? i
: j
;
2937 Node nconst_k
= i
.getKind() == kind::CONST_STRING
? j
: i
;
2938 Node lnck
= i
.getKind() == kind::CONST_STRING
? lj
: li
;
2939 if( !d_equalityEngine
.areDisequal( nconst_k
, d_emptyString
, true ) ){
2940 Node eq
= nconst_k
.eqNode( d_emptyString
);
2941 Node conc
= NodeManager::currentNM()->mkNode( kind::OR
, eq
, eq
.negate() );
2942 sendInference( d_empty_vec
, conc
, "D-DISL-Emp-Split" );
2945 //split on first character
2946 CVC4::String str
= const_k
.getConst
<String
>();
2947 Node firstChar
= str
.size() == 1 ? const_k
: NodeManager::currentNM()->mkConst( str
.prefix( 1 ) );
2948 if( areEqual( lnck
, d_one
) ){
2949 if( areDisequal( firstChar
, nconst_k
) ){
2951 }else if( !areEqual( firstChar
, nconst_k
) ){
2952 //splitting on demand : try to make them disequal
2953 Node eq
= firstChar
.eqNode( nconst_k
);
2954 sendSplit( firstChar
, nconst_k
, "S-Split(DEQL-Const)" );
2955 eq
= Rewriter::rewrite( eq
);
2956 d_pending_req_phase
[ eq
] = false;
2960 Node sk
= mkSkolemCached( nconst_k
, firstChar
, sk_id_dc_spt
, "dc_spt", 2 );
2961 Node skr
= mkSkolemCached( nconst_k
, firstChar
, sk_id_dc_spt_rem
, "dc_spt_rem" );
2962 Node eq1
= nconst_k
.eqNode( NodeManager::currentNM()->mkNode( kind::STRING_CONCAT
, sk
, skr
) );
2963 eq1
= Rewriter::rewrite( eq1
);
2964 Node eq2
= nconst_k
.eqNode( NodeManager::currentNM()->mkNode( kind::STRING_CONCAT
, firstChar
, skr
) );
2965 std::vector
< Node
> antec
;
2966 antec
.insert( antec
.end(), d_normal_forms_exp
[ni
].begin(), d_normal_forms_exp
[ni
].end() );
2967 antec
.insert( antec
.end(), d_normal_forms_exp
[nj
].begin(), d_normal_forms_exp
[nj
].end() );
2968 antec
.push_back( nconst_k
.eqNode( d_emptyString
).negate() );
2969 sendInference( antec
, NodeManager::currentNM()->mkNode( kind::OR
,
2970 NodeManager::currentNM()->mkNode( kind::AND
, eq1
, sk
.eqNode( firstChar
).negate() ), eq2
), "D-DISL-CSplit" );
2971 d_pending_req_phase
[ eq1
] = true;
2976 Trace("strings-solve") << "Non-Simple Case 1 : add lemma " << std::endl
;
2978 std::vector
< Node
> antec
;
2979 std::vector
< Node
> antec_new_lits
;
2980 antec
.insert( antec
.end(), d_normal_forms_exp
[ni
].begin(), d_normal_forms_exp
[ni
].end() );
2981 antec
.insert( antec
.end(), d_normal_forms_exp
[nj
].begin(), d_normal_forms_exp
[nj
].end() );
2983 if( areDisequal( ni
, nj
) ){
2984 antec
.push_back( ni
.eqNode( nj
).negate() );
2986 antec_new_lits
.push_back( ni
.eqNode( nj
).negate() );
2988 antec_new_lits
.push_back( li
.eqNode( lj
).negate() );
2989 std::vector
< Node
> conc
;
2990 Node sk1
= mkSkolemCached( i
, j
, sk_id_deq_x
, "x_dsplit" );
2991 Node sk2
= mkSkolemCached( i
, j
, sk_id_deq_y
, "y_dsplit" );
2992 Node sk3
= mkSkolemCached( i
, j
, sk_id_deq_z
, "z_dsplit", 1 );
2993 //Node nemp = sk3.eqNode(d_emptyString).negate();
2994 //conc.push_back(nemp);
2995 Node lsk1
= mkLength( sk1
);
2996 conc
.push_back( lsk1
.eqNode( li
) );
2997 Node lsk2
= mkLength( sk2
);
2998 conc
.push_back( lsk2
.eqNode( lj
) );
2999 conc
.push_back( NodeManager::currentNM()->mkNode( kind::OR
, j
.eqNode( mkConcat( sk1
, sk3
) ), i
.eqNode( mkConcat( sk2
, sk3
) ) ) );
3000 sendInference( antec
, antec_new_lits
, NodeManager::currentNM()->mkNode( kind::AND
, conc
), "D-DISL-Split" );
3001 ++(d_statistics
.d_deq_splits
);
3004 }else if( areEqual( li
, lj
) ){
3005 Assert( !areDisequal( i
, j
) );
3006 //splitting on demand : try to make them disequal
3007 Node eq
= i
.eqNode( j
);
3008 sendSplit( i
, j
, "S-Split(DEQL)" );
3009 eq
= Rewriter::rewrite( eq
);
3010 d_pending_req_phase
[ eq
] = false;
3013 //splitting on demand : try to make lengths equal
3014 Node eq
= li
.eqNode( lj
);
3015 sendSplit( li
, lj
, "D-Split" );
3016 eq
= Rewriter::rewrite( eq
);
3017 d_pending_req_phase
[ eq
] = true;
3028 int TheoryStrings::processReverseDeq( std::vector
< Node
>& nfi
, std::vector
< Node
>& nfj
, Node ni
, Node nj
) {
3029 //reverse normal form of i, j
3030 std::reverse( nfi
.begin(), nfi
.end() );
3031 std::reverse( nfj
.begin(), nfj
.end() );
3034 int ret
= processSimpleDeq( nfi
, nfj
, ni
, nj
, index
, true );
3036 //reverse normal form of i, j
3037 std::reverse( nfi
.begin(), nfi
.end() );
3038 std::reverse( nfj
.begin(), nfj
.end() );
3043 int TheoryStrings::processSimpleDeq( std::vector
< Node
>& nfi
, std::vector
< Node
>& nfj
, Node ni
, Node nj
, unsigned& index
, bool isRev
){
3044 //see if one side is constant, if so, we can approximate as containment
3045 for( unsigned i
=0; i
<2; i
++ ){
3046 Node c
= getConstantEqc( i
==0 ? ni
: nj
);
3049 if( !TheoryStringsRewriter::canConstantContainList( c
, i
==0 ? nfj
: nfi
, findex
, lindex
) ){
3054 while( index
<nfi
.size() || index
<nfj
.size() ) {
3055 if( index
>=nfi
.size() || index
>=nfj
.size() ){
3056 Trace("strings-solve-debug") << "Disequality normalize empty" << std::endl
;
3057 std::vector
< Node
> ant
;
3058 //we have a conflict : because the lengths are equal, the remainder needs to be empty, which will lead to a conflict
3059 Node lni
= getLengthExp( ni
, ant
, d_normal_forms_base
[ni
] );
3060 Node lnj
= getLengthExp( nj
, ant
, d_normal_forms_base
[nj
] );
3061 ant
.push_back( lni
.eqNode( lnj
) );
3062 ant
.insert( ant
.end(), d_normal_forms_exp
[ni
].begin(), d_normal_forms_exp
[ni
].end() );
3063 ant
.insert( ant
.end(), d_normal_forms_exp
[nj
].begin(), d_normal_forms_exp
[nj
].end() );
3064 std::vector
< Node
> cc
;
3065 std::vector
< Node
>& nfk
= index
>=nfi
.size() ? nfj
: nfi
;
3066 for( unsigned index_k
=index
; index_k
<nfk
.size(); index_k
++ ){
3067 cc
.push_back( nfk
[index_k
].eqNode( d_emptyString
) );
3069 Node conc
= cc
.size()==1 ? cc
[0] : NodeManager::currentNM()->mkNode( kind::AND
, cc
);
3070 conc
= Rewriter::rewrite( conc
);
3071 sendInference( ant
, conc
, "Disequality Normalize Empty", true);
3074 Node i
= nfi
[index
];
3075 Node j
= nfj
[index
];
3076 Trace("strings-solve-debug") << "...Processing(QED) " << i
<< " " << j
<< std::endl
;
3077 if( !areEqual( i
, j
) ) {
3078 if( i
.getKind()==kind::CONST_STRING
&& j
.getKind()==kind::CONST_STRING
) {
3079 unsigned int len_short
= i
.getConst
<String
>().size() < j
.getConst
<String
>().size() ? i
.getConst
<String
>().size() : j
.getConst
<String
>().size();
3080 bool isSameFix
= isRev
? i
.getConst
<String
>().rstrncmp(j
.getConst
<String
>(), len_short
): i
.getConst
<String
>().strncmp(j
.getConst
<String
>(), len_short
);
3082 //same prefix/suffix
3083 //k is the index of the string that is shorter
3084 Node nk
= i
.getConst
<String
>().size() < j
.getConst
<String
>().size() ? i
: j
;
3085 Node nl
= i
.getConst
<String
>().size() < j
.getConst
<String
>().size() ? j
: i
;
3088 int new_len
= nl
.getConst
<String
>().size() - len_short
;
3089 remainderStr
= NodeManager::currentNM()->mkConst( nl
.getConst
<String
>().substr(0, new_len
) );
3090 Trace("strings-solve-debug-test") << "Rev. Break normal form of " << nl
<< " into " << nk
<< ", " << remainderStr
<< std::endl
;
3092 remainderStr
= NodeManager::currentNM()->mkConst( nl
.getConst
<String
>().substr( len_short
) );
3093 Trace("strings-solve-debug-test") << "Break normal form of " << nl
<< " into " << nk
<< ", " << remainderStr
<< std::endl
;
3095 if( i
.getConst
<String
>().size() < j
.getConst
<String
>().size() ) {
3096 nfj
.insert( nfj
.begin() + index
+ 1, remainderStr
);
3097 nfj
[index
] = nfi
[index
];
3099 nfi
.insert( nfi
.begin() + index
+ 1, remainderStr
);
3100 nfi
[index
] = nfj
[index
];
3106 std::vector
< Node
> lexp
;
3107 Node li
= getLength( i
, lexp
);
3108 Node lj
= getLength( j
, lexp
);
3109 if( areEqual( li
, lj
) && areDisequal( i
, j
) ){
3110 Trace("strings-solve") << "Simple Case 2 : found equal length disequal sub strings " << i
<< " " << j
<< std::endl
;
3111 //we are done: D-Remove
3124 void TheoryStrings::addNormalFormPair( Node n1
, Node n2
){
3125 if( !isNormalFormPair( n1
, n2
) ){
3127 NodeIntMap::const_iterator it
= d_nf_pairs
.find( n1
);
3128 if( it
!=d_nf_pairs
.end() ){
3129 index
= (*it
).second
;
3131 d_nf_pairs
[n1
] = index
+ 1;
3132 if( index
<(int)d_nf_pairs_data
[n1
].size() ){
3133 d_nf_pairs_data
[n1
][index
] = n2
;
3135 d_nf_pairs_data
[n1
].push_back( n2
);
3137 Assert( isNormalFormPair( n1
, n2
) );
3139 Trace("strings-nf-debug") << "Already a normal form pair " << n1
<< " " << n2
<< std::endl
;
3143 bool TheoryStrings::isNormalFormPair( Node n1
, Node n2
) {
3144 //TODO: modulo equality?
3145 return isNormalFormPair2( n1
, n2
) || isNormalFormPair2( n2
, n1
);
3148 bool TheoryStrings::isNormalFormPair2( Node n1
, Node n2
) {
3149 //Trace("strings-debug") << "is normal form pair. " << n1 << " " << n2 << std::endl;
3150 NodeIntMap::const_iterator it
= d_nf_pairs
.find( n1
);
3151 if( it
!=d_nf_pairs
.end() ){
3152 Assert( d_nf_pairs_data
.find( n1
)!=d_nf_pairs_data
.end() );
3153 for( int i
=0; i
<(*it
).second
; i
++ ){
3154 Assert( i
<(int)d_nf_pairs_data
[n1
].size() );
3155 if( d_nf_pairs_data
[n1
][i
]==n2
){
3163 void TheoryStrings::registerTerm( Node n
, int effort
) {
3164 // 0 : upon preregistration or internal assertion
3165 // 1 : upon occurrence in length term
3166 // 2 : before normal form computation
3167 // 3 : called on normal form terms
3168 bool do_register
= false;
3169 if( options::stringEagerLen() ){
3170 do_register
= effort
==0;
3172 do_register
= effort
>0 || n
.getKind()!=kind::STRING_CONCAT
;
3175 if(d_registered_terms_cache
.find(n
) == d_registered_terms_cache
.end()) {
3176 d_registered_terms_cache
.insert(n
);
3177 Debug("strings-register") << "TheoryStrings::registerTerm() " << n
<< ", effort = " << effort
<< std::endl
;
3178 if(n
.getType().isString()) {
3179 //register length information:
3180 // for variables, split on empty vs positive length
3181 // for concat/const/replace, introduce proxy var and state length relation
3183 bool processed
= false;
3184 if( n
.getKind()!=kind::STRING_CONCAT
&& n
.getKind()!=kind::CONST_STRING
) {
3185 if( d_length_lemma_terms_cache
.find( n
)==d_length_lemma_terms_cache
.end() ){
3186 Node lsumb
= NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, n
);
3187 lsum
= Rewriter::rewrite( lsumb
);
3188 // can register length term if it does not rewrite
3190 sendLengthLemma( n
);
3198 Node sk
= mkSkolemS( "lsym", -1 );
3199 StringsProxyVarAttribute spva
;
3200 sk
.setAttribute(spva
,true);
3201 Node eq
= Rewriter::rewrite( sk
.eqNode(n
) );
3202 Trace("strings-lemma") << "Strings::Lemma LENGTH Term : " << eq
<< std::endl
;
3203 d_proxy_var
[n
] = sk
;
3204 Trace("strings-assert") << "(assert " << eq
<< ")" << std::endl
;
3206 Node skl
= NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, sk
);
3207 if( n
.getKind()==kind::STRING_CONCAT
){
3208 std::vector
<Node
> node_vec
;
3209 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ) {
3210 if( n
[i
].getAttribute(StringsProxyVarAttribute()) ){
3211 Assert( d_proxy_var_to_length
.find( n
[i
] )!=d_proxy_var_to_length
.end() );
3212 node_vec
.push_back( d_proxy_var_to_length
[n
[i
]] );
3214 Node lni
= NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, n
[i
] );
3215 node_vec
.push_back(lni
);
3218 lsum
= NodeManager::currentNM()->mkNode( kind::PLUS
, node_vec
);
3219 lsum
= Rewriter::rewrite( lsum
);
3220 }else if( n
.getKind()==kind::CONST_STRING
){
3221 lsum
= NodeManager::currentNM()->mkConst( ::CVC4::Rational( n
.getConst
<String
>().size() ) );
3223 Assert( !lsum
.isNull() );
3224 d_proxy_var_to_length
[sk
] = lsum
;
3225 Node ceq
= Rewriter::rewrite( skl
.eqNode( lsum
) );
3226 Trace("strings-lemma") << "Strings::Lemma LENGTH : " << ceq
<< std::endl
;
3227 Trace("strings-lemma-debug") << " prerewrite : " << skl
.eqNode( lsum
) << std::endl
;
3228 Trace("strings-assert") << "(assert " << ceq
<< ")" << std::endl
;
3233 AlwaysAssert(false, "String Terms only in registerTerm.");
3239 void TheoryStrings::sendInference( std::vector
< Node
>& exp
, std::vector
< Node
>& exp_n
, Node eq
, const char * c
, bool asLemma
) {
3240 eq
= eq
.isNull() ? d_false
: Rewriter::rewrite( eq
);
3242 if( Trace
.isOn("strings-infer-debug") ){
3243 Trace("strings-infer-debug") << "By " << c
<< ", infer : " << eq
<< " from: " << std::endl
;
3244 for( unsigned i
=0; i
<exp
.size(); i
++ ){
3245 Trace("strings-infer-debug") << " " << exp
[i
] << std::endl
;
3247 for( unsigned i
=0; i
<exp_n
.size(); i
++ ){
3248 Trace("strings-infer-debug") << " N:" << exp_n
[i
] << std::endl
;
3250 //Trace("strings-infer-debug") << "as lemma : " << asLemma << std::endl;
3252 //check if we should send a lemma or an inference
3253 if( asLemma
|| eq
==d_false
|| eq
.getKind()==kind::OR
|| !exp_n
.empty() || options::stringInferAsLemmas() ){
3255 if( options::stringRExplainLemmas() ){
3256 eq_exp
= mkExplain( exp
, exp_n
);
3259 eq_exp
= mkAnd( exp_n
);
3260 }else if( exp_n
.empty() ){
3261 eq_exp
= mkAnd( exp
);
3263 std::vector
< Node
> ev
;
3264 ev
.insert( ev
.end(), exp
.begin(), exp
.end() );
3265 ev
.insert( ev
.end(), exp_n
.begin(), exp_n
.end() );
3266 eq_exp
= NodeManager::currentNM()->mkNode( kind::AND
, ev
);
3269 sendLemma( eq_exp
, eq
, c
);
3271 sendInfer( mkAnd( exp
), eq
, c
);
3276 void TheoryStrings::sendInference( std::vector
< Node
>& exp
, Node eq
, const char * c
, bool asLemma
) {
3277 std::vector
< Node
> exp_n
;
3278 sendInference( exp
, exp_n
, eq
, c
, asLemma
);
3281 void TheoryStrings::sendLemma( Node ant
, Node conc
, const char * c
) {
3282 if( conc
.isNull() || conc
== d_false
) {
3283 d_out
->conflict(ant
);
3284 Trace("strings-conflict") << "Strings::Conflict : " << c
<< " : " << ant
<< std::endl
;
3285 Trace("strings-lemma") << "Strings::Conflict : " << c
<< " : " << ant
<< std::endl
;
3286 Trace("strings-assert") << "(assert (not " << ant
<< ")) ; conflict " << c
<< std::endl
;
3290 if( ant
== d_true
) {
3293 lem
= NodeManager::currentNM()->mkNode( kind::IMPLIES
, ant
, conc
);
3295 Trace("strings-lemma") << "Strings::Lemma " << c
<< " : " << lem
<< std::endl
;
3296 Trace("strings-assert") << "(assert " << lem
<< ") ; lemma " << c
<< std::endl
;
3297 d_lemma_cache
.push_back( lem
);
3301 void TheoryStrings::sendInfer( Node eq_exp
, Node eq
, const char * c
) {
3302 if( options::stringInferSym() ){
3303 std::vector
< Node
> vars
;
3304 std::vector
< Node
> subs
;
3305 std::vector
< Node
> unproc
;
3306 inferSubstitutionProxyVars( eq_exp
, vars
, subs
, unproc
);
3307 if( unproc
.empty() ){
3308 Trace("strings-lemma-debug") << "Strings::Infer " << eq
<< " from " << eq_exp
<< " by " << c
<< std::endl
;
3309 Node eqs
= eq
.substitute( vars
.begin(), vars
.end(), subs
.begin(), subs
.end() );
3310 Trace("strings-lemma-debug") << "Strings::Infer Alternate : " << eqs
<< std::endl
;
3311 for( unsigned i
=0; i
<vars
.size(); i
++ ){
3312 Trace("strings-lemma-debug") << " " << vars
[i
] << " -> " << subs
[i
] << std::endl
;
3314 sendLemma( d_true
, eqs
, c
);
3317 for( unsigned i
=0; i
<unproc
.size(); i
++ ){
3318 Trace("strings-lemma-debug") << " non-trivial exp : " << unproc
[i
] << std::endl
;
3322 Trace("strings-lemma") << "Strings::Infer " << eq
<< " from " << eq_exp
<< " by " << c
<< std::endl
;
3323 Trace("strings-assert") << "(assert (=> " << eq_exp
<< " " << eq
<< ")) ; infer " << c
<< std::endl
;
3324 d_pending
.push_back( eq
);
3325 d_pending_exp
[eq
] = eq_exp
;
3326 d_infer
.push_back( eq
);
3327 d_infer_exp
.push_back( eq_exp
);
3331 void TheoryStrings::sendSplit( Node a
, Node b
, const char * c
, bool preq
) {
3332 Node eq
= a
.eqNode( b
);
3333 eq
= Rewriter::rewrite( eq
);
3334 Node neq
= NodeManager::currentNM()->mkNode( kind::NOT
, eq
);
3335 Node lemma_or
= NodeManager::currentNM()->mkNode( kind::OR
, eq
, neq
);
3336 Trace("strings-lemma") << "Strings::Lemma " << c
<< " SPLIT : " << lemma_or
<< std::endl
;
3337 d_lemma_cache
.push_back(lemma_or
);
3338 d_pending_req_phase
[eq
] = preq
;
3339 ++(d_statistics
.d_splits
);
3343 void TheoryStrings::sendLengthLemma( Node n
){
3344 Node n_len
= NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, n
);
3345 if( options::stringSplitEmp() || !options::stringLenGeqZ() ){
3346 Node n_len_eq_z
= n_len
.eqNode( d_zero
);
3347 Node n_len_eq_z_2
= n
.eqNode( d_emptyString
);
3348 n_len_eq_z
= Rewriter::rewrite( n_len_eq_z
);
3349 n_len_eq_z_2
= Rewriter::rewrite( n_len_eq_z_2
);
3350 Node n_len_geq_zero
= NodeManager::currentNM()->mkNode( kind::OR
, NodeManager::currentNM()->mkNode( kind::AND
, n_len_eq_z
, n_len_eq_z_2
),
3351 NodeManager::currentNM()->mkNode( kind::GT
, n_len
, d_zero
) );
3352 Trace("strings-lemma") << "Strings::Lemma LENGTH >= 0 : " << n_len_geq_zero
<< std::endl
;
3353 d_out
->lemma(n_len_geq_zero
);
3354 d_out
->requirePhase( n_len_eq_z
, true );
3355 d_out
->requirePhase( n_len_eq_z_2
, true );
3357 //AJR: probably a good idea
3358 if( options::stringLenGeqZ() ){
3359 Node n_len_geq
= NodeManager::currentNM()->mkNode( kind::GEQ
, n_len
, d_zero
);
3360 n_len_geq
= Rewriter::rewrite( n_len_geq
);
3361 d_out
->lemma( n_len_geq
);
3365 void TheoryStrings::inferSubstitutionProxyVars( Node n
, std::vector
< Node
>& vars
, std::vector
< Node
>& subs
, std::vector
< Node
>& unproc
) {
3366 if( n
.getKind()==kind::AND
){
3367 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
3368 inferSubstitutionProxyVars( n
[i
], vars
, subs
, unproc
);
3371 }else if( n
.getKind()==kind::EQUAL
){
3372 Node ns
= n
.substitute( vars
.begin(), vars
.end(), subs
.begin(), subs
.end() );
3373 ns
= Rewriter::rewrite( ns
);
3374 if( ns
.getKind()==kind::EQUAL
){
3377 for( unsigned i
=0; i
<2; i
++ ){
3379 if( ns
[i
].getAttribute(StringsProxyVarAttribute()) ){
3381 }else if( ns
[i
].isConst() ){
3382 NodeNodeMap::const_iterator it
= d_proxy_var
.find( ns
[i
] );
3383 if( it
!=d_proxy_var
.end() ){
3389 if( v
.getNumChildren()==0 ){
3393 //both sides involved in proxy var
3404 subs
.push_back( s
);
3405 vars
.push_back( v
);
3413 unproc
.push_back( n
);
3418 Node
TheoryStrings::mkConcat( Node n1
, Node n2
) {
3419 return Rewriter::rewrite( NodeManager::currentNM()->mkNode( kind::STRING_CONCAT
, n1
, n2
) );
3422 Node
TheoryStrings::mkConcat( Node n1
, Node n2
, Node n3
) {
3423 return Rewriter::rewrite( NodeManager::currentNM()->mkNode( kind::STRING_CONCAT
, n1
, n2
, n3
) );
3426 Node
TheoryStrings::mkConcat( const std::vector
< Node
>& c
) {
3427 return Rewriter::rewrite( c
.size()>1 ? NodeManager::currentNM()->mkNode( kind::STRING_CONCAT
, c
) : ( c
.size()==1 ? c
[0] : d_emptyString
) );
3430 Node
TheoryStrings::mkLength( Node t
) {
3431 return Rewriter::rewrite( NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, t
) );
3434 Node
TheoryStrings::mkSkolemCached( Node a
, Node b
, int id
, const char * c
, int isLenSplit
){
3435 //return mkSkolemS( c, isLenSplit );
3436 std::map
< int, Node
>::iterator it
= d_skolem_cache
[a
][b
].find( id
);
3437 if( it
==d_skolem_cache
[a
][b
].end() ){
3438 Node sk
= mkSkolemS( c
, isLenSplit
);
3439 d_skolem_cache
[a
][b
][id
] = sk
;
3446 //isLenSplit: -1-ignore, 0-no restriction, 1-greater than one, 2-one
3447 Node
TheoryStrings::mkSkolemS( const char *c
, int isLenSplit
) {
3448 Node n
= NodeManager::currentNM()->mkSkolem( c
, NodeManager::currentNM()->stringType(), "string sko" );
3449 d_length_lemma_terms_cache
.insert( n
);
3450 ++(d_statistics
.d_new_skolems
);
3451 if( isLenSplit
==0 ){
3452 sendLengthLemma( n
);
3453 } else if( isLenSplit
== 1 ){
3454 registerNonEmptySkolem( n
);
3455 }else if( isLenSplit
==2 ){
3456 Node len_one
= NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, n
).eqNode( d_one
);
3457 Trace("strings-lemma") << "Strings::Lemma SK-ONE : " << len_one
<< std::endl
;
3458 Trace("strings-assert") << "(assert " << len_one
<< ")" << std::endl
;
3459 d_out
->lemma( len_one
);
3464 void TheoryStrings::registerNonEmptySkolem( Node n
) {
3465 if( d_skolem_ne_reg_cache
.find( n
)==d_skolem_ne_reg_cache
.end() ){
3466 d_skolem_ne_reg_cache
.insert( n
);
3467 d_equalityEngine
.assertEquality(n
.eqNode(d_emptyString
), false, d_true
);
3468 Node len_n_gt_z
= NodeManager::currentNM()->mkNode(kind::GT
,
3469 NodeManager::currentNM()->mkNode(kind::STRING_LENGTH
, n
), d_zero
);
3470 Trace("strings-lemma") << "Strings::Lemma SK-NON-ZERO : " << len_n_gt_z
<< std::endl
;
3471 Trace("strings-assert") << "(assert " << len_n_gt_z
<< ")" << std::endl
;
3472 d_out
->lemma(len_n_gt_z
);
3476 Node
TheoryStrings::mkExplain( std::vector
< Node
>& a
) {
3477 std::vector
< Node
> an
;
3478 return mkExplain( a
, an
);
3481 Node
TheoryStrings::mkExplain( std::vector
< Node
>& a
, std::vector
< Node
>& an
) {
3482 std::vector
< TNode
> antec_exp
;
3483 for( unsigned i
=0; i
<a
.size(); i
++ ) {
3484 if( std::find( a
.begin(), a
.begin() + i
, a
[i
] )==a
.begin() + i
) {
3486 Debug("strings-explain") << "Ask for explanation of " << a
[i
] << std::endl
;
3488 if(a
[i
].getKind() == kind::EQUAL
) {
3489 //assert( hasTerm(a[i][0]) );
3490 //assert( hasTerm(a[i][1]) );
3491 Assert( areEqual(a
[i
][0], a
[i
][1]) );
3492 if( a
[i
][0]==a
[i
][1] ){
3495 } else if( a
[i
].getKind()==kind::NOT
&& a
[i
][0].getKind()==kind::EQUAL
) {
3496 Assert( hasTerm(a
[i
][0][0]) );
3497 Assert( hasTerm(a
[i
][0][1]) );
3498 AlwaysAssert( d_equalityEngine
.areDisequal(a
[i
][0][0], a
[i
][0][1], true) );
3499 }else if( a
[i
].getKind() == kind::AND
){
3500 for( unsigned j
=0; j
<a
[i
].getNumChildren(); j
++ ){
3501 a
.push_back( a
[i
][j
] );
3506 unsigned ps
= antec_exp
.size();
3507 explain(a
[i
], antec_exp
);
3508 Debug("strings-explain") << "Done, explanation was : " << std::endl
;
3509 for( unsigned j
=ps
; j
<antec_exp
.size(); j
++ ) {
3510 Debug("strings-explain") << " " << antec_exp
[j
] << std::endl
;
3512 Debug("strings-explain") << std::endl
;
3516 for( unsigned i
=0; i
<an
.size(); i
++ ) {
3517 if( std::find( an
.begin(), an
.begin() + i
, an
[i
] )==an
.begin() + i
){
3518 Debug("strings-explain") << "Add to explanation (new literal) " << an
[i
] << std::endl
;
3519 antec_exp
.push_back(an
[i
]);
3523 if( antec_exp
.empty() ) {
3525 } else if( antec_exp
.size()==1 ) {
3528 ant
= NodeManager::currentNM()->mkNode( kind::AND
, antec_exp
);
3530 ant
= Rewriter::rewrite( ant
);
3534 Node
TheoryStrings::mkAnd( std::vector
< Node
>& a
) {
3535 std::vector
< Node
> au
;
3536 for( unsigned i
=0; i
<a
.size(); i
++ ){
3537 if( std::find( au
.begin(), au
.end(), a
[i
] )==au
.end() ){
3538 au
.push_back( a
[i
] );
3543 } else if( au
.size() == 1 ) {
3546 return NodeManager::currentNM()->mkNode( kind::AND
, au
);
3550 void TheoryStrings::getConcatVec( Node n
, std::vector
< Node
>& c
) {
3551 if( n
.getKind()==kind::STRING_CONCAT
) {
3552 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ) {
3553 if( !areEqual( n
[i
], d_emptyString
) ) {
3554 c
.push_back( n
[i
] );
3562 void TheoryStrings::checkDeqNF() {
3563 std::vector
< std::vector
< Node
> > cols
;
3564 std::vector
< Node
> lts
;
3565 std::map
< Node
, std::map
< Node
, bool > > processed
;
3567 //for each pair of disequal strings, must determine whether their lengths are equal or disequal
3568 for( NodeList::const_iterator id
= d_ee_disequalities
.begin(); id
!= d_ee_disequalities
.end(); ++id
) {
3571 for( unsigned i
=0; i
<2; i
++ ){
3572 n
[i
] = d_equalityEngine
.getRepresentative( eq
[i
] );
3574 if( processed
[n
[0]].find( n
[1] )==processed
[n
[0]].end() ){
3575 processed
[n
[0]][n
[1]] = true;
3577 for( unsigned i
=0; i
<2; i
++ ){
3578 EqcInfo
* ei
= getOrMakeEqcInfo( n
[i
], false );
3579 lt
[i
] = ei
? ei
->d_length_term
: Node::null();
3580 if( lt
[i
].isNull() ){
3583 lt
[i
] = NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, lt
[i
] );
3585 if( !areEqual( lt
[0], lt
[1] ) && !areDisequal( lt
[0], lt
[1] ) ){
3586 sendSplit( lt
[0], lt
[1], "DEQ-LENGTH-SP" );
3591 if( !hasProcessed() ){
3592 separateByLength( d_strings_eqc
, cols
, lts
);
3593 for( unsigned i
=0; i
<cols
.size(); i
++ ){
3594 if( cols
[i
].size()>1 && d_lemma_cache
.empty() ){
3595 Trace("strings-solve") << "- Verify disequalities are processed for " << cols
[i
][0] << ", normal form : ";
3596 printConcat( d_normal_forms
[cols
[i
][0]], "strings-solve" );
3597 Trace("strings-solve") << "... #eql = " << cols
[i
].size() << std::endl
;
3598 //must ensure that normal forms are disequal
3599 for( unsigned j
=0; j
<cols
[i
].size(); j
++ ){
3600 for( unsigned k
=(j
+1); k
<cols
[i
].size(); k
++ ){
3601 //for strings that are disequal, but have the same length
3602 if( areDisequal( cols
[i
][j
], cols
[i
][k
] ) ){
3603 Assert( !d_conflict
);
3604 Trace("strings-solve") << "- Compare " << cols
[i
][j
] << " ";
3605 printConcat( d_normal_forms
[cols
[i
][j
]], "strings-solve" );
3606 Trace("strings-solve") << " against " << cols
[i
][k
] << " ";
3607 printConcat( d_normal_forms
[cols
[i
][k
]], "strings-solve" );
3608 Trace("strings-solve") << "..." << std::endl
;
3609 processDeq( cols
[i
][j
], cols
[i
][k
] );
3610 if( hasProcessed() ){
3621 void TheoryStrings::checkLengthsEqc() {
3622 if( options::stringLenNorm() ){
3623 for( unsigned i
=0; i
<d_strings_eqc
.size(); i
++ ){
3624 //if( d_normal_forms[nodes[i]].size()>1 ) {
3625 Trace("strings-process-debug") << "Process length constraints for " << d_strings_eqc
[i
] << std::endl
;
3626 //check if there is a length term for this equivalence class
3627 EqcInfo
* ei
= getOrMakeEqcInfo( d_strings_eqc
[i
], false );
3628 Node lt
= ei
? ei
->d_length_term
: Node::null();
3629 if( !lt
.isNull() ) {
3630 Node llt
= NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, lt
);
3631 //now, check if length normalization has occurred
3632 if( ei
->d_normalized_length
.get().isNull() ) {
3633 Node nf
= mkConcat( d_normal_forms
[d_strings_eqc
[i
]] );
3634 if( Trace
.isOn("strings-process-debug") ){
3635 Trace("strings-process-debug") << " normal form is " << nf
<< " from base " << d_normal_forms_base
[d_strings_eqc
[i
]] << std::endl
;
3636 Trace("strings-process-debug") << " normal form exp is: " << std::endl
;
3637 for( unsigned j
=0; j
<d_normal_forms_exp
[d_strings_eqc
[i
]].size(); j
++ ){
3638 Trace("strings-process-debug") << " " << d_normal_forms_exp
[d_strings_eqc
[i
]][j
] << std::endl
;
3642 //if not, add the lemma
3643 std::vector
< Node
> ant
;
3644 ant
.insert( ant
.end(), d_normal_forms_exp
[d_strings_eqc
[i
]].begin(), d_normal_forms_exp
[d_strings_eqc
[i
]].end() );
3645 ant
.push_back( d_normal_forms_base
[d_strings_eqc
[i
]].eqNode( lt
) );
3646 Node lc
= NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, nf
);
3647 Node lcr
= Rewriter::rewrite( lc
);
3648 Trace("strings-process-debug") << "Rewrote length " << lc
<< " to " << lcr
<< std::endl
;
3649 Node eq
= llt
.eqNode( lcr
);
3651 ei
->d_normalized_length
.set( eq
);
3652 sendInference( ant
, eq
, "LEN-NORM", true );
3656 Trace("strings-process-debug") << "No length term for eqc " << d_strings_eqc
[i
] << " " << d_eqc_to_len_term
[d_strings_eqc
[i
]] << std::endl
;
3657 if( !options::stringEagerLen() ){
3658 Node c
= mkConcat( d_normal_forms
[d_strings_eqc
[i
]] );
3659 registerTerm( c
, 3 );
3662 NodeNodeMap::const_iterator it = d_proxy_var.find( c );
3663 if( it!=d_proxy_var.end() ){
3664 Node pv = (*it).second;
3665 Assert( d_proxy_var_to_length.find( pv )!=d_proxy_var_to_length.end() );
3666 Node pvl = d_proxy_var_to_length[pv];
3667 Node ceq = Rewriter::rewrite( mkLength( pv ).eqNode( pvl ) );
3668 sendInference( d_empty_vec, ceq, "LEN-NORM-I", true );
3675 // Trace("strings-process-debug") << "Do not process length constraints for " << nodes[i] << " " << d_normal_forms[nodes[i]].size() << std::endl;
3681 void TheoryStrings::checkCardinality() {
3682 //int cardinality = options::stringCharCardinality();
3683 //Trace("strings-solve-debug2") << "get cardinality: " << cardinality << endl;
3685 //AJR: this will create a partition of eqc, where each collection has length that are pairwise propagated to be equal.
3686 // we do not require disequalities between the lengths of each collection, since we split on disequalities between lengths of string terms that are disequal (DEQ-LENGTH-SP).
3687 // TODO: revisit this?
3688 std::vector
< std::vector
< Node
> > cols
;
3689 std::vector
< Node
> lts
;
3690 separateByLength( d_strings_eqc
, cols
, lts
);
3692 for( unsigned i
= 0; i
<cols
.size(); ++i
) {
3694 Trace("strings-card") << "Number of strings with length equal to " << lr
<< " is " << cols
[i
].size() << std::endl
;
3695 if( cols
[i
].size() > 1 ) {
3697 unsigned card_need
= 1;
3698 double curr
= (double)cols
[i
].size();
3699 while( curr
>d_card_size
){
3700 curr
= curr
/(double)d_card_size
;
3703 Trace("strings-card") << "Need length " << card_need
<< " for this number of strings (where alphabet size is " << d_card_size
<< ")." << std::endl
;
3704 Node cmp
= NodeManager::currentNM()->mkNode( kind::GEQ
, lr
, NodeManager::currentNM()->mkConst( Rational( card_need
) ) );
3705 cmp
= Rewriter::rewrite( cmp
);
3707 unsigned int int_k
= (unsigned int)card_need
;
3708 for( std::vector
< Node
>::iterator itr1
= cols
[i
].begin();
3709 itr1
!= cols
[i
].end(); ++itr1
) {
3710 for( std::vector
< Node
>::iterator itr2
= itr1
+ 1;
3711 itr2
!= cols
[i
].end(); ++itr2
) {
3712 if(!areDisequal( *itr1
, *itr2
)) {
3714 sendSplit( *itr1
, *itr2
, "CARD-SP" );
3719 EqcInfo
* ei
= getOrMakeEqcInfo( lr
, true );
3720 Trace("strings-card") << "Previous cardinality used for " << lr
<< " is " << ((int)ei
->d_cardinality_lem_k
.get()-1) << std::endl
;
3721 if( int_k
+1 > ei
->d_cardinality_lem_k
.get() ){
3722 Node k_node
= NodeManager::currentNM()->mkConst( ::CVC4::Rational( int_k
) );
3723 //add cardinality lemma
3724 Node dist
= NodeManager::currentNM()->mkNode( kind::DISTINCT
, cols
[i
] );
3725 std::vector
< Node
> vec_node
;
3726 vec_node
.push_back( dist
);
3727 for( std::vector
< Node
>::iterator itr1
= cols
[i
].begin();
3728 itr1
!= cols
[i
].end(); ++itr1
) {
3729 Node len
= NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, *itr1
);
3731 Node len_eq_lr
= len
.eqNode(lr
);
3732 vec_node
.push_back( len_eq_lr
);
3735 Node len
= NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, cols
[i
][0] );
3736 Node cons
= NodeManager::currentNM()->mkNode( kind::GEQ
, len
, k_node
);
3737 cons
= Rewriter::rewrite( cons
);
3738 ei
->d_cardinality_lem_k
.set( int_k
+1 );
3740 sendInference( d_empty_vec
, vec_node
, cons
, "CARDINALITY", true );
3749 void TheoryStrings::getEquivalenceClasses( std::vector
< Node
>& eqcs
) {
3750 eq::EqClassesIterator eqcs_i
= eq::EqClassesIterator( &d_equalityEngine
);
3751 while( !eqcs_i
.isFinished() ) {
3752 Node eqc
= (*eqcs_i
);
3753 //if eqc.getType is string
3754 if (eqc
.getType().isString()) {
3755 eqcs
.push_back( eqc
);
3761 void TheoryStrings::separateByLength(std::vector
< Node
>& n
,
3762 std::vector
< std::vector
< Node
> >& cols
,
3763 std::vector
< Node
>& lts
) {
3764 unsigned leqc_counter
= 0;
3765 std::map
< Node
, unsigned > eqc_to_leqc
;
3766 std::map
< unsigned, Node
> leqc_to_eqc
;
3767 std::map
< unsigned, std::vector
< Node
> > eqc_to_strings
;
3768 for( unsigned i
=0; i
<n
.size(); i
++ ) {
3770 Assert( d_equalityEngine
.getRepresentative(eqc
)==eqc
);
3771 EqcInfo
* ei
= getOrMakeEqcInfo( eqc
, false );
3772 Node lt
= ei
? ei
->d_length_term
: Node::null();
3774 lt
= NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, lt
);
3775 Node r
= d_equalityEngine
.getRepresentative( lt
);
3776 if( eqc_to_leqc
.find( r
)==eqc_to_leqc
.end() ){
3777 eqc_to_leqc
[r
] = leqc_counter
;
3778 leqc_to_eqc
[leqc_counter
] = r
;
3781 eqc_to_strings
[ eqc_to_leqc
[r
] ].push_back( eqc
);
3783 eqc_to_strings
[leqc_counter
].push_back( eqc
);
3787 for( std::map
< unsigned, std::vector
< Node
> >::iterator it
= eqc_to_strings
.begin(); it
!= eqc_to_strings
.end(); ++it
){
3788 cols
.push_back( std::vector
< Node
>() );
3789 cols
.back().insert( cols
.back().end(), it
->second
.begin(), it
->second
.end() );
3790 lts
.push_back( leqc_to_eqc
[it
->first
] );
3794 void TheoryStrings::printConcat( std::vector
< Node
>& n
, const char * c
) {
3795 for( unsigned i
=0; i
<n
.size(); i
++ ){
3796 if( i
>0 ) Trace(c
) << " ++ ";
3803 //// Finite Model Finding
3805 Node
TheoryStrings::getNextDecisionRequest( unsigned& priority
) {
3806 if( options::stringFMF() && !d_conflict
){
3807 Node in_var_lsum
= d_input_var_lsum
.get();
3808 //Trace("strings-fmf-debug") << "Strings::FMF: Assertion Level = " << d_valuation.getAssertionLevel() << std::endl;
3809 //initialize the term we will minimize
3810 if( in_var_lsum
.isNull() && !d_input_vars
.empty() ){
3811 Trace("strings-fmf-debug") << "Input variables: ";
3812 std::vector
< Node
> ll
;
3813 for(NodeSet::key_iterator itr
= d_input_vars
.key_begin();
3814 itr
!= d_input_vars
.key_end(); ++itr
) {
3815 Trace("strings-fmf-debug") << " " << (*itr
) ;
3816 ll
.push_back( NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, *itr
) );
3818 Trace("strings-fmf-debug") << std::endl
;
3819 in_var_lsum
= ll
.size()==1 ? ll
[0] : NodeManager::currentNM()->mkNode( kind::PLUS
, ll
);
3820 in_var_lsum
= Rewriter::rewrite( in_var_lsum
);
3821 d_input_var_lsum
.set( in_var_lsum
);
3823 if( !in_var_lsum
.isNull() ){
3824 //Trace("strings-fmf") << "Get next decision request." << std::endl;
3825 //check if we need to decide on something
3826 int decideCard
= d_curr_cardinality
.get();
3827 if( d_cardinality_lits
.find( decideCard
)!=d_cardinality_lits
.end() ){
3829 Node cnode
= d_cardinality_lits
[ d_curr_cardinality
.get() ];
3830 if( d_valuation
.hasSatValue( cnode
, value
) ) {
3832 d_curr_cardinality
.set( d_curr_cardinality
.get() + 1 );
3833 decideCard
= d_curr_cardinality
.get();
3834 Trace("strings-fmf-debug") << "Has false SAT value, increment and decide." << std::endl
;
3837 Trace("strings-fmf-debug") << "Has true SAT value, do not decide." << std::endl
;
3840 Trace("strings-fmf-debug") << "No SAT value, decide." << std::endl
;
3843 if( decideCard
!=-1 ){
3844 if( d_cardinality_lits
.find( decideCard
)==d_cardinality_lits
.end() ){
3845 Node lit
= NodeManager::currentNM()->mkNode( kind::LEQ
, in_var_lsum
, NodeManager::currentNM()->mkConst( Rational( decideCard
) ) );
3846 lit
= Rewriter::rewrite( lit
);
3847 d_cardinality_lits
[decideCard
] = lit
;
3848 Node lem
= NodeManager::currentNM()->mkNode( kind::OR
, lit
, lit
.negate() );
3849 Trace("strings-fmf") << "Strings::FMF: Add decision lemma " << lem
<< ", decideCard = " << decideCard
<< std::endl
;
3850 d_out
->lemma( lem
);
3851 d_out
->requirePhase( lit
, true );
3853 Node lit
= d_cardinality_lits
[ decideCard
];
3854 Trace("strings-fmf") << "Strings::FMF: Decide positive on " << lit
<< std::endl
;
3860 return Node::null();
3863 Node
TheoryStrings::ppRewrite(TNode atom
) {
3864 Trace("strings-ppr") << "TheoryStrings::ppRewrite " << atom
<< std::endl
;
3865 if( !options::stringLazyPreproc() ){
3866 //eager preprocess here
3867 std::vector
< Node
> new_nodes
;
3868 Node ret
= d_preproc
.processAssertion( atom
, new_nodes
);
3870 Trace("strings-ppr") << " rewrote " << atom
<< " -> " << ret
<< ", with " << new_nodes
.size() << " lemmas." << std::endl
;
3871 for( unsigned i
=0; i
<new_nodes
.size(); i
++ ){
3872 Trace("strings-ppr") << " lemma : " << new_nodes
[i
] << std::endl
;
3873 d_out
->lemma( new_nodes
[i
] );
3877 Assert( new_nodes
.empty() );
3884 TheoryStrings::Statistics::Statistics():
3885 d_splits("TheoryStrings::NumOfSplitOnDemands", 0),
3886 d_eq_splits("TheoryStrings::NumOfEqSplits", 0),
3887 d_deq_splits("TheoryStrings::NumOfDiseqSplits", 0),
3888 d_loop_lemmas("TheoryStrings::NumOfLoops", 0),
3889 d_new_skolems("TheoryStrings::NumOfNewSkolems", 0)
3891 smtStatisticsRegistry()->registerStat(&d_splits
);
3892 smtStatisticsRegistry()->registerStat(&d_eq_splits
);
3893 smtStatisticsRegistry()->registerStat(&d_deq_splits
);
3894 smtStatisticsRegistry()->registerStat(&d_loop_lemmas
);
3895 smtStatisticsRegistry()->registerStat(&d_new_skolems
);
3898 TheoryStrings::Statistics::~Statistics(){
3899 smtStatisticsRegistry()->unregisterStat(&d_splits
);
3900 smtStatisticsRegistry()->unregisterStat(&d_eq_splits
);
3901 smtStatisticsRegistry()->unregisterStat(&d_deq_splits
);
3902 smtStatisticsRegistry()->unregisterStat(&d_loop_lemmas
);
3903 smtStatisticsRegistry()->unregisterStat(&d_new_skolems
);
3925 //// Regular Expressions
3928 unsigned TheoryStrings::getNumMemberships( Node n
, bool isPos
) {
3930 NodeIntMap::const_iterator it
= d_pos_memberships
.find( n
);
3931 if( it
!=d_pos_memberships
.end() ){
3932 return (*it
).second
;
3935 NodeIntMap::const_iterator it
= d_neg_memberships
.find( n
);
3936 if( it
!=d_neg_memberships
.end() ){
3937 return (*it
).second
;
3943 Node
TheoryStrings::getMembership( Node n
, bool isPos
, unsigned i
) {
3944 return isPos
? d_pos_memberships_data
[n
][i
] : d_neg_memberships_data
[n
][i
];
3947 Node
TheoryStrings::mkRegExpAntec(Node atom
, Node ant
) {
3948 if(d_regexp_ant
.find(atom
) == d_regexp_ant
.end()) {
3949 return Rewriter::rewrite( NodeManager::currentNM()->mkNode(kind::AND
, ant
, atom
) );
3951 Node n
= d_regexp_ant
[atom
];
3952 return Rewriter::rewrite( NodeManager::currentNM()->mkNode(kind::AND
, ant
, n
) );
3956 Node
TheoryStrings::normalizeRegexp(Node r
) {
3958 if(d_nf_regexps
.find(r
) != d_nf_regexps
.end()) {
3959 nf_r
= d_nf_regexps
[r
];
3961 std::vector
< Node
> nf_exp
;
3962 if(!d_regexp_opr
.checkConstRegExp(r
)) {
3963 switch( r
.getKind() ) {
3964 case kind::REGEXP_EMPTY
:
3965 case kind::REGEXP_SIGMA
: {
3968 case kind::STRING_TO_REGEXP
: {
3969 if(r
[0].isConst()) {
3972 if(d_normal_forms
.find( r
[0] ) != d_normal_forms
.end()) {
3973 nf_r
= mkConcat( d_normal_forms
[r
[0]] );
3974 Debug("regexp-nf") << "Term: " << r
[0] << " has a normal form " << nf_r
<< std::endl
;
3975 nf_exp
.insert(nf_exp
.end(), d_normal_forms_exp
[r
[0]].begin(), d_normal_forms_exp
[r
[0]].end());
3976 nf_r
= Rewriter::rewrite( NodeManager::currentNM()->mkNode(kind::STRING_TO_REGEXP
, nf_r
) );
3980 case kind::REGEXP_CONCAT
:
3981 case kind::REGEXP_UNION
:
3982 case kind::REGEXP_INTER
: {
3984 std::vector
< Node
> vec_nodes
;
3985 for(unsigned i
=0; i
<r
.getNumChildren(); ++i
) {
3986 Node rtmp
= normalizeRegexp(r
[i
]);
3987 vec_nodes
.push_back(rtmp
);
3993 Node rtmp
= vec_nodes
.size()==1 ? vec_nodes
[0] : NodeManager::currentNM()->mkNode(r
.getKind(), vec_nodes
);
3994 nf_r
= Rewriter::rewrite( rtmp
);
3997 case kind::REGEXP_STAR
: {
3998 Node rtmp
= normalizeRegexp(r
[0]);
4000 rtmp
= NodeManager::currentNM()->mkNode(kind::REGEXP_STAR
, rtmp
);
4001 nf_r
= Rewriter::rewrite( rtmp
);
4009 d_nf_regexps
[r
] = nf_r
;
4010 d_nf_regexps_exp
[r
] = nf_exp
;
4015 bool TheoryStrings::normalizePosMemberships(std::map
< Node
, std::vector
< Node
> > &memb_with_exps
) {
4016 std::map
< Node
, std::vector
< Node
> > unprocessed_x_exps
;
4017 std::map
< Node
, std::vector
< Node
> > unprocessed_memberships
;
4018 std::map
< Node
, std::vector
< Node
> > unprocessed_memberships_bases
;
4019 bool addLemma
= false;
4021 Trace("regexp-check") << "Normalizing Positive Memberships ... " << std::endl
;
4023 for( NodeIntMap::const_iterator itr_xr
= d_pos_memberships
.begin(); itr_xr
!= d_pos_memberships
.end(); ++itr_xr
){
4024 Node x
= (*itr_xr
).first
;
4026 std::vector
< Node
> nf_x_exp
;
4027 if(d_normal_forms
.find( x
) != d_normal_forms
.end()) {
4028 //nf_x = mkConcat( d_normal_forms[x] );
4029 nf_x_exp
.insert(nf_x_exp
.end(), d_normal_forms_exp
[x
].begin(), d_normal_forms_exp
[x
].end());
4030 //Debug("regexp-nf") << "Term: " << x << " has a normal form " << ret << std::endl;
4034 Trace("regexp-nf") << "Checking Memberships for N(" << x
<< ") = " << nf_x
<< " :" << std::endl
;
4036 std::vector
< Node
> vec_x
;
4037 std::vector
< Node
> vec_r
;
4038 unsigned n_pmem
= (*itr_xr
).second
;
4039 Assert( getNumMemberships( x
, true )==n_pmem
);
4040 for( unsigned k
=0; k
<n_pmem
; k
++ ){
4041 Node r
= getMembership( x
, true, k
);
4042 Node nf_r
= normalizeRegexp( r
); //AJR: fixed (was normalizing mem #0 always)
4043 Node memb
= NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP
, nf_x
, nf_r
);
4044 if(d_processed_memberships
.find(memb
) == d_processed_memberships
.end()) {
4045 if(d_regexp_opr
.checkConstRegExp(nf_r
)) {
4049 Trace("regexp-nf") << "Handling Symbolic Regexp for N(" << r
<< ") = " << nf_r
<< std::endl
;
4050 //TODO: handle symbolic ones
4053 d_processed_memberships
.insert(memb
);
4056 if(!vec_x
.empty()) {
4057 if(unprocessed_x_exps
.find(nf_x
) == unprocessed_x_exps
.end()) {
4058 unprocessed_x_exps
[nf_x
] = nf_x_exp
;
4059 unprocessed_memberships
[nf_x
] = vec_r
;
4060 unprocessed_memberships_bases
[nf_x
] = vec_x
;
4062 unprocessed_x_exps
[nf_x
].insert(unprocessed_x_exps
[nf_x
].end(), nf_x_exp
.begin(), nf_x_exp
.end());
4063 unprocessed_memberships
[nf_x
].insert(unprocessed_memberships
[nf_x
].end(), vec_r
.begin(), vec_r
.end());
4064 unprocessed_memberships_bases
[nf_x
].insert(unprocessed_memberships_bases
[nf_x
].end(), vec_x
.begin(), vec_x
.end());
4069 for(std::map
< Node
, std::vector
< Node
> >::const_iterator itr
= unprocessed_memberships
.begin();
4070 itr
!= unprocessed_memberships
.end(); ++itr
) {
4071 Node nf_x
= itr
->first
;
4072 std::vector
< Node
> exp( unprocessed_x_exps
[nf_x
] );
4073 Node r
= itr
->second
[0];
4075 Node inter_r
= d_nf_regexps
[r
];
4076 exp
.insert(exp
.end(), d_nf_regexps_exp
[r
].begin(), d_nf_regexps_exp
[r
].end());
4077 Node x
= unprocessed_memberships_bases
[itr
->first
][0];
4078 Node memb
= NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP
, x
, r
);
4079 exp
.push_back(memb
);
4080 for(std::size_t i
=1; i
< itr
->second
.size(); i
++) {
4082 Node r2
= itr
->second
[i
];
4083 Node inter_r2
= d_nf_regexps
[r2
];
4084 exp
.insert(exp
.end(), d_nf_regexps_exp
[r2
].begin(), d_nf_regexps_exp
[r2
].end());
4085 Node x2
= unprocessed_memberships_bases
[itr
->first
][i
];
4086 memb
= NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP
, x2
, r2
);
4087 exp
.push_back(memb
);
4089 bool spflag
= false;
4090 inter_r
= d_regexp_opr
.intersect(inter_r
, inter_r2
, spflag
);
4091 if(inter_r
== d_emptyRegexp
) {
4094 sendInference( d_empty_vec
, exp
, conc
, "INTERSECT CONFLICT", true );
4101 memb
= Rewriter::rewrite( NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP
, nf_x
, inter_r
) );
4102 memb_with_exps
[memb
] = exp
;
4111 bool TheoryStrings::applyRConsume( CVC4::String
&s
, Node
&r
) {
4112 Trace("regexp-derivative") << "TheoryStrings::derivative: s=" << s
<< ", r= " << r
<< std::endl
;
4113 Assert( d_regexp_opr
.checkConstRegExp(r
) );
4115 if( !s
.isEmptyString() ) {
4118 for(unsigned i
=0; i
<s
.size(); ++i
) {
4119 CVC4::String c
= s
.substr(i
, 1);
4121 int rt
= d_regexp_opr
.derivativeS(dc
, c
, dc2
);
4125 } else if(rt
== 2) {
4135 Node
TheoryStrings::applyRSplit(Node s1
, Node s2
, Node r
) {
4136 Assert(d_regexp_opr
.checkConstRegExp(r
));
4138 std::vector
< std::pair
< Node
, Node
> > vec_can
;
4139 d_regexp_opr
.splitRegExp(r
, vec_can
);
4140 //TODO: lazy cache or eager?
4141 std::vector
< Node
> vec_or
;
4143 for(unsigned int i
=0; i
<vec_can
.size(); i
++) {
4144 Node m1
= NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP
, s1
, vec_can
[i
].first
);
4145 Node m2
= NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP
, s2
, vec_can
[i
].second
);
4146 Node c
= Rewriter::rewrite( NodeManager::currentNM()->mkNode(kind::AND
, m1
, m2
) );
4147 vec_or
.push_back( c
);
4149 Node conc
= vec_or
.size()==0? Node::null() : vec_or
.size()==1 ? vec_or
[0] : Rewriter::rewrite( NodeManager::currentNM()->mkNode(kind::OR
, vec_or
) );
4153 bool TheoryStrings::applyRLen(std::map
< Node
, std::vector
< Node
> > &XinR_with_exps
) {
4154 if(XinR_with_exps
.size() > 0) {
4155 //TODO: get vector, var, store.
4162 bool TheoryStrings::checkMembershipsWithoutLength(
4163 std::map
< Node
, std::vector
< Node
> > &memb_with_exps
,
4164 std::map
< Node
, std::vector
< Node
> > &XinR_with_exps
) {
4165 for(std::map
< Node
, std::vector
< Node
> >::iterator itr
= memb_with_exps
.begin(); itr
!= memb_with_exps
.end(); ++itr
) {
4166 Node memb
= itr
->first
;
4170 memb
= Rewriter::rewrite( memb
);
4171 if(memb
== d_false
) {
4173 sendInference(d_empty_vec
, itr
->second
, conc
, "MEMBERSHIP CONFLICT", true);
4177 Assert(memb
== d_true
);
4179 } else if(s
.getKind() == kind::VARIABLE
) {
4181 XinR_with_exps
[itr
->first
] = itr
->second
;
4183 Assert(s
.getKind() == kind::STRING_CONCAT
);
4185 for( unsigned i
=0; i
<s
.getNumChildren(); i
++ ) {
4186 if(s
[i
].isConst()) {
4187 CVC4::String
str( s
[0].getConst
< String
>() );
4188 //R-Consume, see Tianyi's thesis
4189 if(!applyRConsume(str
, r
)) {
4190 sendInference(d_empty_vec
, itr
->second
, conc
, "R-Consume CONFLICT", true);
4195 //R-Split, see Tianyi's thesis
4196 if(i
== s
.getNumChildren() - 1) {
4198 Node memb2
= NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP
, s
[i
], r
);
4199 XinR_with_exps
[itr
->first
] = itr
->second
;
4202 std::vector
< Node
> vec_s2
;
4203 for( unsigned j
=i
+1; j
<s
.getNumChildren(); j
++ ) {
4204 vec_s2
.push_back(s
[j
]);
4206 Node s2
= mkConcat(vec_s2
);
4207 conc
= applyRSplit(s1
, s2
, r
);
4208 if(conc
== d_true
) {
4210 } else if(conc
.isNull() || conc
== d_false
) {
4211 conc
= Node::null();
4212 sendInference(d_empty_vec
, itr
->second
, conc
, "R-Split Conflict", true);
4216 sendInference(d_empty_vec
, itr
->second
, conc
, "R-Split", true);
4228 bool TheoryStrings::checkMemberships2() {
4229 bool addedLemma
= false;
4230 d_nf_regexps
.clear();
4231 d_nf_regexps_exp
.clear();
4232 std::map
< Node
, std::vector
< Node
> > memb_with_exps
;
4233 std::map
< Node
, std::vector
< Node
> > XinR_with_exps
;
4235 addedLemma
= normalizePosMemberships( memb_with_exps
);
4238 addedLemma
|= checkMembershipsWithoutLength( memb_with_exps
, XinR_with_exps
);
4239 //TODO: check addlemma
4240 if (!addedLemma
&& !d_conflict
) {
4241 for(std::map
< Node
, std::vector
< Node
> >::const_iterator itr
= XinR_with_exps
.begin();
4242 itr
!= XinR_with_exps
.end(); ++itr
) {
4243 std::vector
<Node
> vec_or
;
4244 d_regexp_opr
.disjunctRegExp( itr
->first
, vec_or
);
4245 Node tmp
= NodeManager::currentNM()->mkNode(kind::REGEXP_UNION
, vec_or
);
4246 Trace("regexp-process") << "Got r: " << itr
->first
<< " to " << tmp
<< std::endl
;
4248 if(r.getKind() == kind::REGEXP_STAR) {
4250 addedLemma = applyRLen(XinR_with_exps);
4256 Assert(false); //TODO:tmp
4263 void TheoryStrings::checkMemberships() {
4264 //add the memberships
4265 std::vector
< Node
> mems
;
4266 d_extt
->getActive( mems
, kind::STRING_IN_REGEXP
);
4267 for( unsigned i
=0; i
<mems
.size(); i
++ ){
4269 Assert( d_extf_info_tmp
.find( n
)!=d_extf_info_tmp
.end() );
4270 Assert( d_extf_info_tmp
[n
].d_pol
==1 || d_extf_info_tmp
[n
].d_pol
==-1 );
4271 bool pol
= d_extf_info_tmp
[n
].d_pol
==1;
4272 Trace("strings-process-debug") << " add membership : " << n
<< ", pol = " << pol
<< std::endl
;
4273 addMembership( pol
? n
: n
.negate() );
4276 bool addedLemma
= false;
4277 bool changed
= false;
4278 std::vector
< Node
> processed
;
4279 std::vector
< Node
> cprocessed
;
4281 Trace("regexp-debug") << "Checking Memberships ... " << std::endl
;
4282 //if(options::stringEIT()) {
4283 //TODO: Opt for normal forms
4284 for( NodeIntMap::const_iterator itr_xr
= d_pos_memberships
.begin(); itr_xr
!= d_pos_memberships
.end(); ++itr_xr
){
4285 bool spflag
= false;
4286 Node x
= (*itr_xr
).first
;
4287 Trace("regexp-debug") << "Checking Memberships for " << x
<< std::endl
;
4288 if(d_inter_index
.find(x
) == d_inter_index
.end()) {
4289 d_inter_index
[x
] = 0;
4291 int cur_inter_idx
= d_inter_index
[x
];
4292 unsigned n_pmem
= (*itr_xr
).second
;
4293 Assert( getNumMemberships( x
, true )==n_pmem
);
4294 if( cur_inter_idx
!= (int)n_pmem
) {
4296 d_inter_cache
[x
] = getMembership( x
, true, 0 );
4297 d_inter_index
[x
] = 1;
4298 Trace("regexp-debug") << "... only one choice " << std::endl
;
4299 } else if(n_pmem
> 1) {
4301 if(d_inter_cache
.find(x
) != d_inter_cache
.end()) {
4302 r
= d_inter_cache
[x
];
4305 r
= getMembership( x
, true, 0 );
4309 unsigned k_start
= cur_inter_idx
;
4310 Trace("regexp-debug") << "... staring from : " << cur_inter_idx
<< ", we have " << n_pmem
<< std::endl
;
4311 for(unsigned k
= k_start
; k
<n_pmem
; k
++) {
4312 Node r2
= getMembership( x
, true, k
);
4313 r
= d_regexp_opr
.intersect(r
, r2
, spflag
);
4316 } else if(r
== d_emptyRegexp
) {
4317 std::vector
< Node
> vec_nodes
;
4318 for( unsigned kk
=0; kk
<=k
; kk
++ ){
4319 Node rr
= getMembership( x
, true, kk
);
4320 Node n
= NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP
, x
, rr
);
4321 vec_nodes
.push_back( n
);
4324 sendInference(vec_nodes
, conc
, "INTERSECT CONFLICT", true);
4333 if(!d_conflict
&& !spflag
) {
4334 d_inter_cache
[x
] = r
;
4335 d_inter_index
[x
] = (int)n_pmem
;
4342 Trace("regexp-debug") << "... No Intersect Conflict in Memberships, addedLemma: " << addedLemma
<< std::endl
;
4344 for( unsigned i
=0; i
<d_regexp_memberships
.size(); i
++ ) {
4345 //check regular expression membership
4346 Node assertion
= d_regexp_memberships
[i
];
4347 if( d_regexp_ucached
.find(assertion
) == d_regexp_ucached
.end()
4348 && d_regexp_ccached
.find(assertion
) == d_regexp_ccached
.end() ) {
4349 Trace("strings-regexp") << "We have regular expression assertion : " << assertion
<< std::endl
;
4350 Node atom
= assertion
.getKind()==kind::NOT
? assertion
[0] : assertion
;
4351 bool polarity
= assertion
.getKind()!=kind::NOT
;
4355 std::vector
< Node
> rnfexp
;
4357 //if(options::stringOpt1()) {
4360 x
= getNormalString( x
, rnfexp
);
4363 if(!d_regexp_opr
.checkConstRegExp(r
)) {
4364 r
= getNormalSymRegExp(r
, rnfexp
);
4367 Trace("strings-regexp-nf") << "Term " << atom
<< " is normalized to " << x
<< " IN " << r
<< std::endl
;
4369 Node tmp
= Rewriter::rewrite( NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP
, x
, r
) );
4374 d_regexp_ccached
.insert(assertion
);
4376 } else if(tmp
== d_false
) {
4377 Node antec
= mkRegExpAntec(assertion
, mkExplain(rnfexp
));
4378 Node conc
= Node::null();
4379 sendLemma(antec
, conc
, "REGEXP NF Conflict");
4387 flag
= checkPDerivative(x
, r
, atom
, addedLemma
, processed
, cprocessed
, rnfexp
);
4388 if(options::stringOpt2() && flag
) {
4389 if(d_regexp_opr
.checkConstRegExp(r
) && x
.getKind()==kind::STRING_CONCAT
) {
4390 std::vector
< std::pair
< Node
, Node
> > vec_can
;
4391 d_regexp_opr
.splitRegExp(r
, vec_can
);
4392 //TODO: lazy cache or eager?
4393 std::vector
< Node
> vec_or
;
4394 std::vector
< Node
> vec_s2
;
4395 for(unsigned int s2i
=1; s2i
<x
.getNumChildren(); s2i
++) {
4396 vec_s2
.push_back(x
[s2i
]);
4399 Node s2
= mkConcat(vec_s2
);
4400 for(unsigned int i
=0; i
<vec_can
.size(); i
++) {
4401 Node m1
= NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP
, s1
, vec_can
[i
].first
);
4402 Node m2
= NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP
, s2
, vec_can
[i
].second
);
4403 Node c
= Rewriter::rewrite( NodeManager::currentNM()->mkNode(kind::AND
, m1
, m2
) );
4404 vec_or
.push_back( c
);
4406 Node conc
= vec_or
.size()==1 ? vec_or
[0] : Rewriter::rewrite( NodeManager::currentNM()->mkNode(kind::OR
, vec_or
) );
4407 //Trace("regexp-split") << "R " << r << " to " << conc << std::endl;
4408 Node antec
= mkRegExpAntec(atom
, mkExplain(rnfexp
));
4409 if(conc
== d_true
) {
4411 cprocessed
.push_back( assertion
);
4413 processed
.push_back( assertion
);
4416 sendLemma(antec
, conc
, "RegExp-CST-SP");
4423 if(! options::stringExp()) {
4424 throw LogicException("Strings Incomplete (due to Negative Membership) by default, try --strings-exp option.");
4428 //check if the term is atomic
4429 Node xr
= getRepresentative( x
);
4430 //Trace("strings-regexp") << xr << " is rep of " << x << std::endl;
4431 //Assert( d_normal_forms.find( xr )!=d_normal_forms.end() );
4433 if( true || r
.getKind()!=kind::REGEXP_STAR
|| ( d_normal_forms
[xr
].size()==1 && x
.getKind()!=kind::STRING_CONCAT
) ){
4434 Trace("strings-regexp") << "Unroll/simplify membership of atomic term " << xr
<< std::endl
;
4435 //if so, do simple unrolling
4436 std::vector
< Node
> nvec
;
4438 /*if(xr.isConst()) {
4439 Node tmp = Rewriter::rewrite( NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP, xr, r) );
4440 if(tmp==d_true || tmp==d_false) {
4442 tmp = tmp==d_true? d_false : d_true;
4444 nvec.push_back( tmp );
4449 d_regexp_opr
.simplify(atom
, nvec
, polarity
);
4451 Node antec
= assertion
;
4452 if(d_regexp_ant
.find(assertion
) != d_regexp_ant
.end()) {
4453 antec
= d_regexp_ant
[assertion
];
4454 for(std::vector
< Node
>::const_iterator itr
=nvec
.begin(); itr
<nvec
.end(); itr
++) {
4455 if(itr
->getKind() == kind::STRING_IN_REGEXP
) {
4456 if(d_regexp_ant
.find( *itr
) == d_regexp_ant
.end()) {
4457 d_regexp_ant
[ *itr
] = antec
;
4462 antec
= Rewriter::rewrite( NodeManager::currentNM()->mkNode(kind::AND
, antec
, mkExplain(rnfexp
)) );
4463 Node conc
= nvec
.size()==1 ? nvec
[0] : NodeManager::currentNM()->mkNode(kind::AND
, nvec
);
4464 conc
= Rewriter::rewrite(conc
);
4465 sendLemma( antec
, conc
, "REGEXP_Unfold" );
4468 cprocessed
.push_back( assertion
);
4470 processed
.push_back( assertion
);
4472 //d_regexp_ucached[assertion] = true;
4474 Trace("strings-regexp") << "Unroll/simplify membership of non-atomic term " << xr
<< " = ";
4475 for( unsigned j
=0; j
<d_normal_forms
[xr
].size(); j
++ ){
4476 Trace("strings-regexp") << d_normal_forms
[xr
][j
] << " ";
4478 Trace("strings-regexp") << ", polarity = " << polarity
<< std::endl
;
4479 //otherwise, distribute unrolling over parts
4482 if( d_normal_forms
[xr
].size()>1 ){
4483 p1
= d_normal_forms
[xr
][0];
4484 std::vector
< Node
> cc
;
4485 cc
.insert( cc
.begin(), d_normal_forms
[xr
].begin() + 1, d_normal_forms
[xr
].end() );
4486 p2
= mkConcat( cc
);
4489 Trace("strings-regexp-debug") << "Construct antecedant..." << std::endl
;
4490 std::vector
< Node
> antec
;
4491 std::vector
< Node
> antecn
;
4492 antec
.insert( antec
.begin(), d_normal_forms_exp
[xr
].begin(), d_normal_forms_exp
[xr
].end() );
4494 antec
.push_back( x
.eqNode( xr
) );
4496 antecn
.push_back( assertion
);
4497 Node ant
= mkExplain( antec
, antecn
);
4498 Trace("strings-regexp-debug") << "Construct conclusion..." << std::endl
;
4501 if( d_normal_forms
[xr
].size()==0 ){
4503 }else if( d_normal_forms
[xr
].size()==1 ){
4504 Trace("strings-regexp-debug") << "Case 1\n";
4505 conc
= NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP
, d_normal_forms
[xr
][0], r
);
4507 Trace("strings-regexp-debug") << "Case 2\n";
4508 std::vector
< Node
> conc_c
;
4509 Node s11
= mkSkolemS( "s11" );
4510 Node s12
= mkSkolemS( "s12" );
4511 Node s21
= mkSkolemS( "s21" );
4512 Node s22
= mkSkolemS( "s22" );
4513 conc
= p1
.eqNode( mkConcat(s11
, s12
) );
4514 conc_c
.push_back(conc
);
4515 conc
= p2
.eqNode( mkConcat(s21
, s22
) );
4516 conc_c
.push_back(conc
);
4517 conc
= NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP
, s11
, r
);
4518 conc_c
.push_back(conc
);
4519 conc
= NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP
, mkConcat(s12
, s21
), r
[0]);
4520 conc_c
.push_back(conc
);
4521 conc
= NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP
, s22
, r
);
4522 conc_c
.push_back(conc
);
4523 conc
= Rewriter::rewrite(NodeManager::currentNM()->mkNode(kind::AND
, conc_c
));
4524 Node eqz
= Rewriter::rewrite(x
.eqNode(d_emptyString
));
4525 conc
= NodeManager::currentNM()->mkNode(kind::OR
, eqz
, conc
);
4526 d_pending_req_phase
[eqz
] = true;
4529 if( d_normal_forms
[xr
].size()==0 ){
4531 }else if( d_normal_forms
[xr
].size()==1 ){
4532 Trace("strings-regexp-debug") << "Case 3\n";
4533 conc
= NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP
, d_normal_forms
[xr
][0], r
).negate();
4535 Trace("strings-regexp-debug") << "Case 4\n";
4536 Node len1
= NodeManager::currentNM()->mkNode(kind::STRING_LENGTH
, p1
);
4537 Node len2
= NodeManager::currentNM()->mkNode(kind::STRING_LENGTH
, p2
);
4538 Node bi
= NodeManager::currentNM()->mkBoundVar(NodeManager::currentNM()->integerType());
4539 Node bj
= NodeManager::currentNM()->mkBoundVar(NodeManager::currentNM()->integerType());
4540 Node b1v
= NodeManager::currentNM()->mkNode(kind::BOUND_VAR_LIST
, bi
, bj
);
4541 Node g1
= NodeManager::currentNM()->mkNode(kind::AND
,
4542 NodeManager::currentNM()->mkNode(kind::GEQ
, bi
, d_zero
),
4543 NodeManager::currentNM()->mkNode(kind::GEQ
, len1
, bi
),
4544 NodeManager::currentNM()->mkNode(kind::GEQ
, bj
, d_zero
),
4545 NodeManager::currentNM()->mkNode(kind::GEQ
, len2
, bj
));
4546 Node s11
= NodeManager::currentNM()->mkNode(kind::STRING_SUBSTR
, p1
, d_zero
, bi
);
4547 Node s12
= NodeManager::currentNM()->mkNode(kind::STRING_SUBSTR
, p1
, bi
, NodeManager::currentNM()->mkNode(kind::MINUS
, len1
, bi
));
4548 Node s21
= NodeManager::currentNM()->mkNode(kind::STRING_SUBSTR
, p2
, d_zero
, bj
);
4549 Node s22
= NodeManager::currentNM()->mkNode(kind::STRING_SUBSTR
, p2
, bj
, NodeManager::currentNM()->mkNode(kind::MINUS
, len2
, bj
));
4550 Node cc1
= NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP
, s11
, r
).negate();
4551 Node cc2
= NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP
, mkConcat(s12
, s21
), r
[0]).negate();
4552 Node cc3
= NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP
, s22
, r
).negate();
4553 conc
= NodeManager::currentNM()->mkNode(kind::OR
, cc1
, cc2
, cc3
);
4554 conc
= NodeManager::currentNM()->mkNode(kind::IMPLIES
, g1
, conc
);
4555 conc
= NodeManager::currentNM()->mkNode(kind::FORALL
, b1v
, conc
);
4556 conc
= NodeManager::currentNM()->mkNode(kind::AND
, x
.eqNode(d_emptyString
).negate(), conc
);
4560 ant
= mkRegExpAntec(assertion
, ant
);
4561 sendLemma(ant
, conc
, "REGEXP CSTAR");
4563 if( conc
==d_false
){
4564 d_regexp_ccached
.insert( assertion
);
4566 cprocessed
.push_back( assertion
);
4569 d_regexp_ccached
.insert(assertion
);
4581 for( unsigned i
=0; i
<processed
.size(); i
++ ) {
4582 d_regexp_ucached
.insert(processed
[i
]);
4584 for( unsigned i
=0; i
<cprocessed
.size(); i
++ ) {
4585 d_regexp_ccached
.insert(cprocessed
[i
]);
4591 bool TheoryStrings::checkPDerivative(Node x
, Node r
, Node atom
, bool &addedLemma
,
4592 std::vector
< Node
> &processed
, std::vector
< Node
> &cprocessed
, std::vector
< Node
> &nf_exp
) {
4594 Node antnf
= mkExplain(nf_exp
);
4596 if(areEqual(x
, d_emptyString
)) {
4598 switch(d_regexp_opr
.delta(r
, exp
)) {
4600 Node antec
= mkRegExpAntec(atom
, x
.eqNode(d_emptyString
));
4601 antec
= Rewriter::rewrite(NodeManager::currentNM()->mkNode(kind::AND
, antec
, antnf
));
4602 sendLemma(antec
, exp
, "RegExp Delta");
4604 d_regexp_ccached
.insert(atom
);
4608 d_regexp_ccached
.insert(atom
);
4612 Node antec
= mkRegExpAntec(atom
, x
.eqNode(d_emptyString
));
4613 antec
= Rewriter::rewrite(NodeManager::currentNM()->mkNode(kind::AND
, antec
, antnf
));
4614 Node conc
= Node::null();
4615 sendLemma(antec
, conc
, "RegExp Delta CONFLICT");
4617 d_regexp_ccached
.insert(atom
);
4625 /*Node xr = getRepresentative( x );
4627 Node n = NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP, xr, r);
4628 Node nn = Rewriter::rewrite( n );
4630 d_regexp_ccached.insert(atom);
4632 } else if(nn == d_false) {
4633 Node antec = mkRegExpAntec(atom, x.eqNode(xr));
4634 Node conc = Node::null();
4635 sendLemma(antec, conc, "RegExp Delta CONFLICT");
4637 d_regexp_ccached.insert(atom);
4641 Node sREant
= mkRegExpAntec(atom
, d_true
);
4642 sREant
= Rewriter::rewrite(NodeManager::currentNM()->mkNode(kind::AND
, sREant
, antnf
));
4643 if(deriveRegExp( x
, r
, sREant
)) {
4645 processed
.push_back( atom
);
4652 CVC4::String
TheoryStrings::getHeadConst( Node x
) {
4654 return x
.getConst
< String
>();
4655 } else if( x
.getKind() == kind::STRING_CONCAT
) {
4656 if( x
[0].isConst() ) {
4657 return x
[0].getConst
< String
>();
4659 return d_emptyString
.getConst
< String
>();
4662 return d_emptyString
.getConst
< String
>();
4666 bool TheoryStrings::deriveRegExp( Node x
, Node r
, Node ant
) {
4668 Assert(x
!= d_emptyString
);
4669 Trace("regexp-derive") << "TheoryStrings::deriveRegExp: x=" << x
<< ", r= " << r
<< std::endl
;
4671 // Node n = NodeManager::currentNM()->mkNode( kind::STRING_IN_REGEXP, x, r );
4672 // Node r = Rewriter::rewrite( n );
4674 // sendLemma(ant, r, "REGEXP REWRITE");
4678 CVC4::String s
= getHeadConst( x
);
4679 if( !s
.isEmptyString() && d_regexp_opr
.checkConstRegExp( r
) ) {
4680 Node conc
= Node::null();
4683 for(unsigned i
=0; i
<s
.size(); ++i
) {
4684 CVC4::String c
= s
.substr(i
, 1);
4686 int rt
= d_regexp_opr
.derivativeS(dc
, c
, dc2
);
4690 } else if(rt
== 2) {
4699 Assert(false, "Impossible: TheoryStrings::deriveRegExp: const string in const regular expression.");
4702 Assert( x
.getKind() == kind::STRING_CONCAT
);
4703 std::vector
< Node
> vec_nodes
;
4704 for(unsigned int i
=1; i
<x
.getNumChildren(); ++i
) {
4705 vec_nodes
.push_back( x
[i
] );
4707 Node left
= mkConcat( vec_nodes
);
4708 left
= Rewriter::rewrite( left
);
4709 conc
= NodeManager::currentNM()->mkNode( kind::STRING_IN_REGEXP
, left
, dc
);
4711 /*std::vector< Node > sdc;
4712 d_regexp_opr.simplify(conc, sdc, true);
4713 if(sdc.size() == 1) {
4716 conc = Rewriter::rewrite(NodeManager::currentNM()->mkNode(kind::AND, conc));
4720 sendLemma(ant
, conc
, "RegExp-Derive");
4727 void TheoryStrings::addMembership(Node assertion
) {
4728 bool polarity
= assertion
.getKind() != kind::NOT
;
4729 TNode atom
= polarity
? assertion
: assertion
[0];
4734 NodeIntMap::const_iterator it
= d_pos_memberships
.find( x
);
4735 if( it
!=d_nf_pairs
.end() ){
4736 index
= (*it
).second
;
4737 for( int k
=0; k
<index
; k
++ ){
4738 if( k
<(int)d_pos_memberships_data
[x
].size() ){
4739 if( d_pos_memberships_data
[x
][k
]==r
){
4747 d_pos_memberships
[x
] = index
+ 1;
4748 if( index
<(int)d_pos_memberships_data
[x
].size() ){
4749 d_pos_memberships_data
[x
][index
] = r
;
4751 d_pos_memberships_data
[x
].push_back( r
);
4753 } else if(!options::stringIgnNegMembership()) {
4754 /*if(options::stringEIT() && d_regexp_opr.checkConstRegExp(r)) {
4756 Node r2 = d_regexp_opr.complement(r, rt);
4757 Node a = NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP, x, r2);
4760 NodeIntMap::const_iterator it
= d_neg_memberships
.find( x
);
4761 if( it
!=d_nf_pairs
.end() ){
4762 index
= (*it
).second
;
4763 for( int k
=0; k
<index
; k
++ ){
4764 if( k
<(int)d_neg_memberships_data
[x
].size() ){
4765 if( d_neg_memberships_data
[x
][k
]==r
){
4773 d_neg_memberships
[x
] = index
+ 1;
4774 if( index
<(int)d_neg_memberships_data
[x
].size() ){
4775 d_neg_memberships_data
[x
][index
] = r
;
4777 d_neg_memberships_data
[x
].push_back( r
);
4781 if(polarity
|| !options::stringIgnNegMembership()) {
4782 d_regexp_memberships
.push_back( assertion
);
4786 Node
TheoryStrings::getNormalString( Node x
, std::vector
< Node
>& nf_exp
){
4788 Node xr
= getRepresentative( x
);
4789 if( d_normal_forms
.find( xr
) != d_normal_forms
.end() ){
4790 Node ret
= mkConcat( d_normal_forms
[xr
] );
4791 nf_exp
.insert( nf_exp
.end(), d_normal_forms_exp
[xr
].begin(), d_normal_forms_exp
[xr
].end() );
4792 addToExplanation( x
, d_normal_forms_base
[xr
], nf_exp
);
4793 Trace("strings-debug") << "Term: " << x
<< " has a normal form " << ret
<< std::endl
;
4796 if(x
.getKind() == kind::STRING_CONCAT
) {
4797 std::vector
< Node
> vec_nodes
;
4798 for(unsigned i
=0; i
<x
.getNumChildren(); i
++) {
4799 Node nc
= getNormalString( x
[i
], nf_exp
);
4800 vec_nodes
.push_back( nc
);
4802 return mkConcat( vec_nodes
);
4809 Node
TheoryStrings::getNormalSymRegExp(Node r
, std::vector
<Node
> &nf_exp
) {
4811 switch( r
.getKind() ) {
4812 case kind::REGEXP_EMPTY
:
4813 case kind::REGEXP_SIGMA
:
4815 case kind::STRING_TO_REGEXP
: {
4816 if(!r
[0].isConst()) {
4817 Node tmp
= getNormalString( r
[0], nf_exp
);
4819 ret
= NodeManager::currentNM()->mkNode(kind::STRING_TO_REGEXP
, tmp
);
4824 case kind::REGEXP_CONCAT
: {
4825 std::vector
< Node
> vec_nodes
;
4826 for(unsigned i
=0; i
<r
.getNumChildren(); ++i
) {
4827 vec_nodes
.push_back( getNormalSymRegExp(r
[i
], nf_exp
) );
4829 ret
= mkConcat(vec_nodes
);
4832 case kind::REGEXP_UNION
: {
4833 std::vector
< Node
> vec_nodes
;
4834 for(unsigned i
=0; i
<r
.getNumChildren(); ++i
) {
4835 vec_nodes
.push_back( getNormalSymRegExp(r
[i
], nf_exp
) );
4837 ret
= Rewriter::rewrite( NodeManager::currentNM()->mkNode(kind::REGEXP_UNION
, vec_nodes
) );
4840 case kind::REGEXP_INTER
: {
4841 std::vector
< Node
> vec_nodes
;
4842 for(unsigned i
=0; i
<r
.getNumChildren(); ++i
) {
4843 vec_nodes
.push_back( getNormalSymRegExp(r
[i
], nf_exp
) );
4845 ret
= Rewriter::rewrite( NodeManager::currentNM()->mkNode(kind::REGEXP_INTER
, vec_nodes
) );
4848 case kind::REGEXP_STAR
: {
4849 ret
= getNormalSymRegExp( r
[0], nf_exp
);
4850 ret
= Rewriter::rewrite( NodeManager::currentNM()->mkNode(kind::REGEXP_STAR
, ret
) );
4853 //case kind::REGEXP_PLUS:
4854 //case kind::REGEXP_OPT:
4855 //case kind::REGEXP_RANGE:
4857 Trace("strings-error") << "Unsupported term: " << r
<< " in normalization SymRegExp." << std::endl
;
4859 //return Node::null();
4865 }/* CVC4::theory::strings namespace */
4866 }/* CVC4::theory namespace */
4867 }/* CVC4 namespace */