1 /********************* */
2 /*! \file theory_strings.cpp
4 ** Top contributors (to current version):
5 ** Andrew Reynolds, Tianyi Liang, Morgan Deters
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2018 by the authors listed in the file AUTHORS
8 ** in the top-level source directory) and their institutional affiliations.
9 ** All rights reserved. See the file COPYING in the top-level source
10 ** directory for licensing information.\endverbatim
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/command.h"
24 #include "smt/logic_exception.h"
25 #include "smt/smt_statistics_registry.h"
26 #include "theory/ext_theory.h"
27 #include "theory/quantifiers/term_database.h"
28 #include "theory/rewriter.h"
29 #include "theory/strings/theory_strings_rewriter.h"
30 #include "theory/strings/type_enumerator.h"
31 #include "theory/theory_model.h"
32 #include "theory/valuation.h"
35 using namespace CVC4::context
;
36 using namespace CVC4::kind
;
42 std::ostream
& operator<<(std::ostream
& out
, Inference i
)
46 case INFER_SSPLIT_CST_PROP
: out
<< "S-Split(CST-P)-prop"; break;
47 case INFER_SSPLIT_VAR_PROP
: out
<< "S-Split(VAR)-prop"; break;
48 case INFER_LEN_SPLIT
: out
<< "Len-Split(Len)"; break;
49 case INFER_LEN_SPLIT_EMP
: out
<< "Len-Split(Emp)"; break;
50 case INFER_SSPLIT_CST_BINARY
: out
<< "S-Split(CST-P)-binary"; break;
51 case INFER_SSPLIT_CST
: out
<< "S-Split(CST-P)"; break;
52 case INFER_SSPLIT_VAR
: out
<< "S-Split(VAR)"; break;
53 case INFER_FLOOP
: out
<< "F-Loop"; break;
54 default: out
<< "?"; break;
59 std::ostream
& operator<<(std::ostream
& out
, InferStep s
)
63 case BREAK
: out
<< "break"; break;
64 case CHECK_INIT
: out
<< "check_init"; break;
65 case CHECK_CONST_EQC
: out
<< "check_const_eqc"; break;
66 case CHECK_EXTF_EVAL
: out
<< "check_extf_eval"; break;
67 case CHECK_CYCLES
: out
<< "check_cycles"; break;
68 case CHECK_FLAT_FORMS
: out
<< "check_flat_forms"; break;
69 case CHECK_NORMAL_FORMS_EQ
: out
<< "check_normal_forms_eq"; break;
70 case CHECK_NORMAL_FORMS_DEQ
: out
<< "check_normal_forms_deq"; break;
71 case CHECK_CODES
: out
<< "check_codes"; break;
72 case CHECK_LENGTH_EQC
: out
<< "check_length_eqc"; break;
73 case CHECK_EXTF_REDUCTION
: out
<< "check_extf_reduction"; break;
74 case CHECK_MEMBERSHIP
: out
<< "check_membership"; break;
75 case CHECK_CARDINALITY
: out
<< "check_cardinality"; break;
76 default: out
<< "?"; break;
81 Node
TheoryStrings::TermIndex::add( TNode n
, unsigned index
, TheoryStrings
* t
, Node er
, std::vector
< Node
>& c
) {
82 if( index
==n
.getNumChildren() ){
83 if( d_data
.isNull() ){
88 Assert( index
<n
.getNumChildren() );
89 TNode nir
= t
->getRepresentative( n
[index
] );
90 //if it is empty, and doing CONCAT, ignore
91 if( nir
==er
&& n
.getKind()==kind::STRING_CONCAT
){
92 return add( n
, index
+1, t
, er
, c
);
95 return d_children
[nir
].add( n
, index
+1, t
, er
, c
);
100 TheoryStrings::TheoryStrings(context::Context
* c
,
101 context::UserContext
* u
,
104 const LogicInfo
& logicInfo
)
105 : Theory(THEORY_STRINGS
, c
, u
, out
, valuation
, logicInfo
),
107 d_equalityEngine(d_notify
, c
, "theory::strings", true),
108 d_conflict(c
, false),
112 d_pregistered_terms_cache(u
),
113 d_registered_terms_cache(u
),
114 d_length_lemma_terms_cache(u
),
117 d_extf_infer_cache(c
),
118 d_extf_infer_cache_u(u
),
119 d_ee_disequalities(c
),
122 d_proxy_var_to_length(u
),
124 d_has_extf(c
, false),
125 d_has_str_code(false),
126 d_regexp_memberships(c
),
129 d_pos_memberships(c
),
130 d_neg_memberships(c
),
133 d_processed_memberships(c
),
137 d_cardinality_lits(u
),
138 d_curr_cardinality(c
, 0),
139 d_strategy_init(false)
142 getExtTheory()->addFunctionKind(kind::STRING_SUBSTR
);
143 getExtTheory()->addFunctionKind(kind::STRING_STRIDOF
);
144 getExtTheory()->addFunctionKind(kind::STRING_ITOS
);
145 getExtTheory()->addFunctionKind(kind::STRING_STOI
);
146 getExtTheory()->addFunctionKind(kind::STRING_STRREPL
);
147 getExtTheory()->addFunctionKind(kind::STRING_STRCTN
);
148 getExtTheory()->addFunctionKind(kind::STRING_IN_REGEXP
);
149 getExtTheory()->addFunctionKind(kind::STRING_LEQ
);
150 getExtTheory()->addFunctionKind(kind::STRING_CODE
);
152 // The kinds we are treating as function application in congruence
153 d_equalityEngine
.addFunctionKind(kind::STRING_LENGTH
);
154 d_equalityEngine
.addFunctionKind(kind::STRING_CONCAT
);
155 d_equalityEngine
.addFunctionKind(kind::STRING_IN_REGEXP
);
156 d_equalityEngine
.addFunctionKind(kind::STRING_CODE
);
157 if( options::stringLazyPreproc() ){
158 d_equalityEngine
.addFunctionKind(kind::STRING_STRCTN
);
159 d_equalityEngine
.addFunctionKind(kind::STRING_LEQ
);
160 d_equalityEngine
.addFunctionKind(kind::STRING_SUBSTR
);
161 d_equalityEngine
.addFunctionKind(kind::STRING_ITOS
);
162 d_equalityEngine
.addFunctionKind(kind::STRING_STOI
);
163 d_equalityEngine
.addFunctionKind(kind::STRING_STRIDOF
);
164 d_equalityEngine
.addFunctionKind(kind::STRING_STRREPL
);
167 d_zero
= NodeManager::currentNM()->mkConst( Rational( 0 ) );
168 d_one
= NodeManager::currentNM()->mkConst( Rational( 1 ) );
169 d_neg_one
= NodeManager::currentNM()->mkConst(Rational(-1));
170 d_emptyString
= NodeManager::currentNM()->mkConst( ::CVC4::String("") );
171 std::vector
< Node
> nvec
;
172 d_emptyRegexp
= NodeManager::currentNM()->mkNode( kind::REGEXP_EMPTY
, nvec
);
173 d_true
= NodeManager::currentNM()->mkConst( true );
174 d_false
= NodeManager::currentNM()->mkConst( false );
176 d_card_size
= TheoryStringsRewriter::getAlphabetCardinality();
179 TheoryStrings::~TheoryStrings() {
180 for( std::map
< Node
, EqcInfo
* >::iterator it
= d_eqc_info
.begin(); it
!= d_eqc_info
.end(); ++it
){
185 Node
TheoryStrings::getRepresentative( Node t
) {
186 if( d_equalityEngine
.hasTerm( t
) ){
187 return d_equalityEngine
.getRepresentative( t
);
193 bool TheoryStrings::hasTerm( Node a
){
194 return d_equalityEngine
.hasTerm( a
);
197 bool TheoryStrings::areEqual( Node a
, Node b
){
200 }else if( hasTerm( a
) && hasTerm( b
) ){
201 return d_equalityEngine
.areEqual( a
, b
);
207 bool TheoryStrings::areDisequal( Node a
, Node b
){
211 if( hasTerm( a
) && hasTerm( b
) ) {
212 Node ar
= d_equalityEngine
.getRepresentative( a
);
213 Node br
= d_equalityEngine
.getRepresentative( b
);
214 return ( ar
!=br
&& ar
.isConst() && br
.isConst() ) || d_equalityEngine
.areDisequal( ar
, br
, false );
216 Node ar
= getRepresentative( a
);
217 Node br
= getRepresentative( b
);
218 return ar
!=br
&& ar
.isConst() && br
.isConst();
223 bool TheoryStrings::areCareDisequal( TNode x
, TNode y
) {
224 Assert( d_equalityEngine
.hasTerm(x
) );
225 Assert( d_equalityEngine
.hasTerm(y
) );
226 if( d_equalityEngine
.isTriggerTerm(x
, THEORY_STRINGS
) && d_equalityEngine
.isTriggerTerm(y
, THEORY_STRINGS
) ){
227 TNode x_shared
= d_equalityEngine
.getTriggerTermRepresentative(x
, THEORY_STRINGS
);
228 TNode y_shared
= d_equalityEngine
.getTriggerTermRepresentative(y
, THEORY_STRINGS
);
229 EqualityStatus eqStatus
= d_valuation
.getEqualityStatus(x_shared
, y_shared
);
230 if( eqStatus
==EQUALITY_FALSE_AND_PROPAGATED
|| eqStatus
==EQUALITY_FALSE
|| eqStatus
==EQUALITY_FALSE_IN_MODEL
){
237 Node
TheoryStrings::getLengthExp( Node t
, std::vector
< Node
>& exp
, Node te
){
238 Assert( areEqual( t
, te
) );
239 Node lt
= mkLength( te
);
241 // use own length if it exists, leads to shorter explanation
244 EqcInfo
* ei
= getOrMakeEqcInfo( t
, false );
245 Node length_term
= ei
? ei
->d_length_term
: Node::null();
246 if( length_term
.isNull() ){
247 //typically shouldnt be necessary
250 Debug("strings") << "TheoryStrings::getLengthTerm " << t
<< " is " << length_term
<< std::endl
;
251 addToExplanation( length_term
, te
, exp
);
252 return Rewriter::rewrite( NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, length_term
) );
256 Node
TheoryStrings::getLength( Node t
, std::vector
< Node
>& exp
) {
257 return getLengthExp( t
, exp
, t
);
260 void TheoryStrings::setMasterEqualityEngine(eq::EqualityEngine
* eq
) {
261 d_equalityEngine
.setMasterEqualityEngine(eq
);
264 void TheoryStrings::addSharedTerm(TNode t
) {
265 Debug("strings") << "TheoryStrings::addSharedTerm(): "
266 << t
<< " " << t
.getType().isBoolean() << endl
;
267 d_equalityEngine
.addTriggerTerm(t
, THEORY_STRINGS
);
268 Debug("strings") << "TheoryStrings::addSharedTerm() finished" << std::endl
;
271 EqualityStatus
TheoryStrings::getEqualityStatus(TNode a
, TNode b
) {
272 if( d_equalityEngine
.hasTerm(a
) && d_equalityEngine
.hasTerm(b
) ){
273 if (d_equalityEngine
.areEqual(a
, b
)) {
274 // The terms are implied to be equal
275 return EQUALITY_TRUE
;
277 if (d_equalityEngine
.areDisequal(a
, b
, false)) {
278 // The terms are implied to be dis-equal
279 return EQUALITY_FALSE
;
282 return EQUALITY_UNKNOWN
;
285 void TheoryStrings::propagate(Effort e
) {
286 // direct propagation now
289 bool TheoryStrings::propagate(TNode literal
) {
290 Debug("strings-propagate") << "TheoryStrings::propagate(" << literal
<< ")" << std::endl
;
291 // If already in conflict, no more propagation
293 Debug("strings-propagate") << "TheoryStrings::propagate(" << literal
<< "): already in conflict" << std::endl
;
297 bool ok
= d_out
->propagate(literal
);
305 void TheoryStrings::explain(TNode literal
, std::vector
<TNode
>& assumptions
) {
306 Debug("strings-explain") << "Explain " << literal
<< " " << d_conflict
<< std::endl
;
307 bool polarity
= literal
.getKind() != kind::NOT
;
308 TNode atom
= polarity
? literal
: literal
[0];
309 unsigned ps
= assumptions
.size();
310 std::vector
< TNode
> tassumptions
;
311 if (atom
.getKind() == kind::EQUAL
) {
312 if( atom
[0]!=atom
[1] ){
313 Assert( hasTerm( atom
[0] ) );
314 Assert( hasTerm( atom
[1] ) );
315 d_equalityEngine
.explainEquality(atom
[0], atom
[1], polarity
, tassumptions
);
318 d_equalityEngine
.explainPredicate(atom
, polarity
, tassumptions
);
320 for( unsigned i
=0; i
<tassumptions
.size(); i
++ ){
321 if( std::find( assumptions
.begin(), assumptions
.end(), tassumptions
[i
] )==assumptions
.end() ){
322 assumptions
.push_back( tassumptions
[i
] );
325 if (Debug
.isOn("strings-explain-debug"))
327 Debug("strings-explain-debug") << "Explanation for " << literal
<< " was "
329 for (unsigned i
= ps
; i
< assumptions
.size(); i
++)
331 Debug("strings-explain-debug") << " " << assumptions
[i
] << std::endl
;
336 Node
TheoryStrings::explain( TNode literal
){
337 Debug("strings-explain") << "explain called on " << literal
<< std::endl
;
338 std::vector
< TNode
> assumptions
;
339 explain( literal
, assumptions
);
340 if( assumptions
.empty() ){
342 }else if( assumptions
.size()==1 ){
343 return assumptions
[0];
345 return NodeManager::currentNM()->mkNode( kind::AND
, assumptions
);
349 bool TheoryStrings::getCurrentSubstitution( int effort
, std::vector
< Node
>& vars
,
350 std::vector
< Node
>& subs
, std::map
< Node
, std::vector
< Node
> >& exp
) {
351 Trace("strings-subs") << "getCurrentSubstitution, effort = " << effort
<< std::endl
;
352 for( unsigned i
=0; i
<vars
.size(); i
++ ){
354 Trace("strings-subs") << " get subs for " << n
<< "..." << std::endl
;
357 Node mv
= d_valuation
.getModel()->getRepresentative( n
);
358 Trace("strings-subs") << " model val : " << mv
<< std::endl
;
359 subs
.push_back( mv
);
361 Node nr
= getRepresentative( n
);
362 std::map
< Node
, Node
>::iterator itc
= d_eqc_to_const
.find( nr
);
363 if( itc
!=d_eqc_to_const
.end() ){
364 //constant equivalence classes
365 Trace("strings-subs") << " constant eqc : " << d_eqc_to_const_exp
[nr
] << " " << d_eqc_to_const_base
[nr
] << " " << nr
<< std::endl
;
366 subs
.push_back( itc
->second
);
367 if( !d_eqc_to_const_exp
[nr
].isNull() ){
368 exp
[n
].push_back( d_eqc_to_const_exp
[nr
] );
370 if( !d_eqc_to_const_base
[nr
].isNull() ){
371 addToExplanation( n
, d_eqc_to_const_base
[nr
], exp
[n
] );
373 }else if( effort
>=1 && effort
<3 && n
.getType().isString() ){
375 Node ns
= getNormalString( d_normal_forms_base
[nr
], exp
[n
] );
376 subs
.push_back( ns
);
377 Trace("strings-subs") << " normal eqc : " << ns
<< " " << d_normal_forms_base
[nr
] << " " << nr
<< std::endl
;
378 if( !d_normal_forms_base
[nr
].isNull() ) {
379 addToExplanation( n
, d_normal_forms_base
[nr
], exp
[n
] );
383 //Trace("strings-subs") << " representative : " << nr << std::endl;
384 //addToExplanation( n, nr, exp[n] );
385 //subs.push_back( nr );
393 int TheoryStrings::getReduction( int effort
, Node n
, Node
& nr
) {
394 //determine the effort level to process the extf at
395 // 0 - at assertion time, 1+ - after no other reduction is applicable
396 Assert( d_extf_info_tmp
.find( n
)!=d_extf_info_tmp
.end() );
397 if( d_extf_info_tmp
[n
].d_model_active
){
399 int pol
= d_extf_info_tmp
[n
].d_pol
;
400 if( n
.getKind()==kind::STRING_STRCTN
){
407 std::vector
< Node
> lexp
;
408 Node lenx
= getLength( x
, lexp
);
409 Node lens
= getLength( s
, lexp
);
410 if( areEqual( lenx
, lens
) ){
411 Trace("strings-extf-debug") << " resolve extf : " << n
<< " based on equal lengths disequality." << std::endl
;
412 //we can reduce to disequality when lengths are equal
413 if( !areDisequal( x
, s
) ){
414 lexp
.push_back( lenx
.eqNode(lens
) );
415 lexp
.push_back( n
.negate() );
416 Node xneqs
= x
.eqNode(s
).negate();
417 sendInference( lexp
, xneqs
, "NEG-CTN-EQL", true );
426 if( options::stringLazyPreproc() ){
427 if( n
.getKind()==kind::STRING_SUBSTR
){
429 }else if( n
.getKind()!=kind::STRING_IN_REGEXP
){
434 if( effort
==r_effort
){
435 Node c_n
= pol
==-1 ? n
.negate() : n
;
436 if( d_preproc_cache
.find( c_n
)==d_preproc_cache
.end() ){
437 d_preproc_cache
[ c_n
] = true;
438 Trace("strings-process-debug") << "Process reduction for " << n
<< ", pol = " << pol
<< std::endl
;
439 Kind k
= n
.getKind();
440 if (k
== kind::STRING_STRCTN
&& pol
== 1)
444 //positive contains reduces to a equality
445 Node sk1
= d_sk_cache
.mkSkolemCached(
446 x
, s
, SkolemCache::SK_ID_CTN_PRE
, "sc1");
447 Node sk2
= d_sk_cache
.mkSkolemCached(
448 x
, s
, SkolemCache::SK_ID_CTN_POST
, "sc2");
449 Node eq
= Rewriter::rewrite( x
.eqNode( mkConcat( sk1
, s
, sk2
) ) );
450 std::vector
< Node
> exp_vec
;
451 exp_vec
.push_back( n
);
452 sendInference( d_empty_vec
, exp_vec
, eq
, "POS-CTN", true );
453 //we've reduced this n
454 Trace("strings-extf-debug") << " resolve extf : " << n
<< " based on positive contain reduction." << std::endl
;
457 else if (k
!= kind::STRING_CODE
)
459 Assert(k
== STRING_SUBSTR
|| k
== STRING_STRCTN
|| k
== STRING_STRIDOF
462 || k
== STRING_STRREPL
464 std::vector
< Node
> new_nodes
;
465 Node res
= d_preproc
.simplify( n
, new_nodes
);
467 new_nodes
.push_back( NodeManager::currentNM()->mkNode( kind::EQUAL
, res
, n
) );
468 Node nnlem
= new_nodes
.size()==1 ? new_nodes
[0] : NodeManager::currentNM()->mkNode( kind::AND
, new_nodes
);
469 nnlem
= Rewriter::rewrite( nnlem
);
470 Trace("strings-red-lemma") << "Reduction_" << effort
<< " lemma : " << nnlem
<< std::endl
;
471 Trace("strings-red-lemma") << "...from " << n
<< std::endl
;
472 sendInference( d_empty_vec
, nnlem
, "Reduction", true );
473 //we've reduced this n
474 Trace("strings-extf-debug") << " resolve extf : " << n
<< " based on reduction." << std::endl
;
485 /////////////////////////////////////////////////////////////////////////////
487 /////////////////////////////////////////////////////////////////////////////
490 void TheoryStrings::presolve() {
491 Debug("strings-presolve") << "TheoryStrings::Presolving : get fmf options " << (options::stringFMF() ? "true" : "false") << std::endl
;
492 initializeStrategy();
496 /////////////////////////////////////////////////////////////////////////////
498 /////////////////////////////////////////////////////////////////////////////
500 bool TheoryStrings::collectModelInfo(TheoryModel
* m
)
502 Trace("strings-model") << "TheoryStrings : Collect model info" << std::endl
;
503 Trace("strings-model") << "TheoryStrings : assertEqualityEngine." << std::endl
;
505 std::set
<Node
> termSet
;
507 // Compute terms appearing in assertions and shared terms
508 computeRelevantTerms(termSet
);
509 // assert the (relevant) portion of the equality engine to the model
510 if (!m
->assertEqualityEngine(&d_equalityEngine
, &termSet
))
515 std::unordered_set
<Node
, NodeHashFunction
> repSet
;
516 NodeManager
* nm
= NodeManager::currentNM();
518 // get the relevant string equivalence classes
519 for (const Node
& s
: termSet
)
521 if (s
.getType().isString())
523 Node r
= getRepresentative(s
);
527 std::vector
<Node
> nodes(repSet
.begin(), repSet
.end());
528 std::map
< Node
, Node
> processed
;
529 std::vector
< std::vector
< Node
> > col
;
530 std::vector
< Node
> lts
;
531 separateByLength( nodes
, col
, lts
);
532 //step 1 : get all values for known lengths
533 std::vector
< Node
> lts_values
;
534 std::map
<unsigned, Node
> values_used
;
535 std::vector
<Node
> len_splits
;
536 for( unsigned i
=0; i
<col
.size(); i
++ ) {
537 Trace("strings-model") << "Checking length for {";
538 for( unsigned j
=0; j
<col
[i
].size(); j
++ ) {
540 Trace("strings-model") << ", ";
542 Trace("strings-model") << col
[i
][j
];
544 Trace("strings-model") << " } (length is " << lts
[i
] << ")" << std::endl
;
546 if( lts
[i
].isConst() ) {
549 else if (!lts
[i
].isNull())
551 // get the model value for lts[i]
552 len_value
= d_valuation
.getModelValue(lts
[i
]);
554 if (len_value
.isNull())
556 lts_values
.push_back(Node::null());
560 Assert(len_value
.getConst
<Rational
>() <= Rational(String::maxSize()),
561 "Exceeded UINT32_MAX in string model");
563 len_value
.getConst
<Rational
>().getNumerator().toUnsignedInt();
564 std::map
<unsigned, Node
>::iterator itvu
= values_used
.find(lvalue
);
565 if (itvu
== values_used
.end())
567 values_used
[lvalue
] = lts
[i
];
571 len_splits
.push_back(lts
[i
].eqNode(itvu
->second
));
573 lts_values
.push_back(len_value
);
576 ////step 2 : assign arbitrary values for unknown lengths?
577 // confirmed by calculus invariant, see paper
578 Trace("strings-model") << "Assign to equivalence classes..." << std::endl
;
579 std::map
<Node
, Node
> pure_eq_assign
;
580 //step 3 : assign values to equivalence classes that are pure variables
581 for( unsigned i
=0; i
<col
.size(); i
++ ){
582 std::vector
< Node
> pure_eq
;
583 Trace("strings-model") << "The (" << col
[i
].size()
584 << ") equivalence classes ";
585 for (const Node
& eqc
: col
[i
])
587 Trace("strings-model") << eqc
<< " ";
588 //check if col[i][j] has only variables
591 Assert(d_normal_forms
.find(eqc
) != d_normal_forms
.end());
592 if (d_normal_forms
[eqc
].size() == 1)
594 // does it have a code and the length of these equivalence classes are
596 if (d_has_str_code
&& lts_values
[i
] == d_one
)
598 EqcInfo
* eip
= getOrMakeEqcInfo(eqc
, false);
599 if (eip
&& !eip
->d_code_term
.get().isNull())
601 // its value must be equal to its code
602 Node ct
= nm
->mkNode(kind::STRING_CODE
, eip
->d_code_term
.get());
603 Node ctv
= d_valuation
.getModelValue(ct
);
605 ctv
.getConst
<Rational
>().getNumerator().toUnsignedInt();
606 Trace("strings-model") << "(code: " << cvalue
<< ") ";
607 std::vector
<unsigned> vec
;
608 vec
.push_back(String::convertCodeToUnsignedInt(cvalue
));
609 Node mv
= nm
->mkConst(String(vec
));
610 pure_eq_assign
[eqc
] = mv
;
611 m
->getEqualityEngine()->addTerm(mv
);
614 pure_eq
.push_back(eqc
);
619 processed
[eqc
] = eqc
;
622 Trace("strings-model") << "have length " << lts_values
[i
] << std::endl
;
624 //assign a new length if necessary
625 if( !pure_eq
.empty() ){
626 if( lts_values
[i
].isNull() ){
627 // start with length two (other lengths have special precendence)
629 while( values_used
.find( lvalue
)!=values_used
.end() ){
632 Trace("strings-model") << "*** Decide to make length of " << lvalue
<< std::endl
;
633 lts_values
[i
] = nm
->mkConst(Rational(lvalue
));
634 values_used
[lvalue
] = Node::null();
636 Trace("strings-model") << "Need to assign values of length " << lts_values
[i
] << " to equivalence classes ";
637 for( unsigned j
=0; j
<pure_eq
.size(); j
++ ){
638 Trace("strings-model") << pure_eq
[j
] << " ";
640 Trace("strings-model") << std::endl
;
642 //use type enumerator
643 Assert(lts_values
[i
].getConst
<Rational
>() <= Rational(String::maxSize()),
644 "Exceeded UINT32_MAX in string model");
645 StringEnumeratorLength
sel(lts_values
[i
].getConst
<Rational
>().getNumerator().toUnsignedInt());
646 for (const Node
& eqc
: pure_eq
)
649 std::map
<Node
, Node
>::iterator itp
= pure_eq_assign
.find(eqc
);
650 if (itp
== pure_eq_assign
.end())
652 Assert( !sel
.isFinished() );
654 while (m
->hasTerm(c
))
657 if (sel
.isFinished())
659 // We are in a case where model construction is impossible due to
660 // an insufficient number of constants of a given length.
662 // Consider an integer equivalence class E whose value is assigned
663 // n in the model. Let { S_1, ..., S_m } be the set of string
664 // equivalence classes such that len( x ) is a member of E for
665 // some member x of each class S1, ...,Sm. Since our calculus is
666 // saturated with respect to cardinality inference (see Liang
667 // et al, Figure 6, CAV 2014), we have that m <= A^n, where A is
668 // the cardinality of our alphabet.
670 // Now, consider the case where there exists two integer
671 // equivalence classes E1 and E2 that are assigned n, and moreover
672 // we did not received notification from arithmetic that E1 = E2.
673 // This typically should never happen, but assume in the following
676 // Now, it may be the case that there are string equivalence
677 // classes { S_1, ..., S_m1 } whose lengths are in E1,
678 // and classes { S'_1, ..., S'_m2 } whose lengths are in E2, where
679 // m1 + m2 > A^n. In this case, we have insufficient strings to
680 // assign to { S_1, ..., S_m1, S'_1, ..., S'_m2 }. If this
681 // happens, we add a split on len( u1 ) = len( u2 ) for some
682 // len( u1 ) in E1, len( u2 ) in E2. We do this for each pair of
683 // integer equivalence classes that are assigned to the same value
685 AlwaysAssert(!len_splits
.empty());
686 for (const Node
& sl
: len_splits
)
688 Node spl
= nm
->mkNode(OR
, sl
, sl
.negate());
701 Trace("strings-model") << "*** Assigned constant " << c
<< " for "
704 if (!m
->assertEquality(eqc
, c
, true))
711 Trace("strings-model") << "String Model : Pure Assigned." << std::endl
;
712 //step 4 : assign constants to all other equivalence classes
713 for( unsigned i
=0; i
<nodes
.size(); i
++ ){
714 if( processed
.find( nodes
[i
] )==processed
.end() ){
715 Assert( d_normal_forms
.find( nodes
[i
] )!=d_normal_forms
.end() );
716 Trace("strings-model") << "Construct model for " << nodes
[i
] << " based on normal form ";
717 for( unsigned j
=0; j
<d_normal_forms
[nodes
[i
]].size(); j
++ ) {
718 if( j
>0 ) Trace("strings-model") << " ++ ";
719 Trace("strings-model") << d_normal_forms
[nodes
[i
]][j
];
720 Node r
= getRepresentative( d_normal_forms
[nodes
[i
]][j
] );
721 if( !r
.isConst() && processed
.find( r
)==processed
.end() ){
722 Trace("strings-model") << "(UNPROCESSED)";
725 Trace("strings-model") << std::endl
;
726 std::vector
< Node
> nc
;
727 for( unsigned j
=0; j
<d_normal_forms
[nodes
[i
]].size(); j
++ ) {
728 Node r
= getRepresentative( d_normal_forms
[nodes
[i
]][j
] );
729 Assert( r
.isConst() || processed
.find( r
)!=processed
.end() );
730 nc
.push_back(r
.isConst() ? r
: processed
[r
]);
732 Node cc
= mkConcat( nc
);
733 Assert( cc
.getKind()==kind::CONST_STRING
);
734 Trace("strings-model") << "*** Determined constant " << cc
<< " for " << nodes
[i
] << std::endl
;
735 processed
[nodes
[i
]] = cc
;
736 if (!m
->assertEquality(nodes
[i
], cc
, true))
742 //Trace("strings-model") << "String Model : Assigned." << std::endl;
743 Trace("strings-model") << "String Model : Finished." << std::endl
;
747 /////////////////////////////////////////////////////////////////////////////
749 /////////////////////////////////////////////////////////////////////////////
752 void TheoryStrings::preRegisterTerm(TNode n
) {
753 if( d_pregistered_terms_cache
.find(n
) == d_pregistered_terms_cache
.end() ) {
754 d_pregistered_terms_cache
.insert(n
);
755 Trace("strings-preregister")
756 << "TheoryString::preregister : " << n
<< std::endl
;
757 //check for logic exceptions
758 Kind k
= n
.getKind();
759 if( !options::stringExp() ){
760 if (k
== kind::STRING_STRIDOF
|| k
== kind::STRING_ITOS
761 || k
== kind::STRING_STOI
762 || k
== kind::STRING_STRREPL
763 || k
== kind::STRING_STRCTN
766 std::stringstream ss
;
767 ss
<< "Term of kind " << k
768 << " not supported in default mode, try --strings-exp";
769 throw LogicException(ss
.str());
775 d_equalityEngine
.addTriggerEquality(n
);
778 case kind::STRING_IN_REGEXP
: {
779 d_out
->requirePhase(n
, true);
780 d_equalityEngine
.addTriggerPredicate(n
);
781 d_equalityEngine
.addTerm(n
[0]);
782 d_equalityEngine
.addTerm(n
[1]);
787 TypeNode tn
= n
.getType();
788 if (tn
.isRegExp() && n
.isVar())
790 std::stringstream ss
;
791 ss
<< "Regular expression variables are not supported.";
792 throw LogicException(ss
.str());
794 if( tn
.isString() ) {
795 // all characters of constants should fall in the alphabet
798 std::vector
<unsigned> vec
= n
.getConst
<String
>().getVec();
799 for (unsigned u
: vec
)
801 if (u
>= d_card_size
)
803 std::stringstream ss
;
804 ss
<< "Characters in string \"" << n
805 << "\" are outside of the given alphabet.";
806 throw LogicException(ss
.str());
810 // if finite model finding is enabled,
811 // then we minimize the length of this term if it is a variable
812 // but not an internally generated Skolem, or a term that does
813 // not belong to this theory.
814 if (options::stringFMF()
815 && (n
.isVar() ? !d_sk_cache
.isSkolem(n
)
816 : kindToTheoryId(k
) != THEORY_STRINGS
))
818 d_input_vars
.insert(n
);
820 d_equalityEngine
.addTerm(n
);
821 } else if (tn
.isBoolean()) {
822 // Get triggered for both equal and dis-equal
823 d_equalityEngine
.addTriggerPredicate(n
);
825 // Function applications/predicates
826 d_equalityEngine
.addTerm(n
);
827 if( options::stringExp() ){
828 //collect extended functions here: some may not be asserted to strings (such as those with return type Int),
829 // but we need to record them so they are treated properly
830 getExtTheory()->registerTermRec( n
);
833 //concat terms do not contribute to theory combination? TODO: verify
834 if (n
.hasOperator() && kindToTheoryId(k
) == THEORY_STRINGS
835 && k
!= kind::STRING_CONCAT
)
837 d_functionsTerms
.push_back( n
);
844 Node
TheoryStrings::expandDefinition(LogicRequest
&logicRequest
, Node node
) {
845 Trace("strings-exp-def") << "TheoryStrings::expandDefinition : " << node
<< std::endl
;
849 void TheoryStrings::check(Effort e
) {
850 if (done() && e
<EFFORT_FULL
) {
854 TimerStat::CodeTimer
checkTimer(d_checkTime
);
859 if( !done() && !hasTerm( d_emptyString
) ) {
860 preRegisterTerm( d_emptyString
);
863 // Trace("strings-process") << "Theory of strings, check : " << e << std::endl;
864 Trace("strings-check") << "Theory of strings, check : " << e
<< std::endl
;
865 while ( !done() && !d_conflict
) {
866 // Get all the assertions
867 Assertion assertion
= get();
868 TNode fact
= assertion
.assertion
;
870 Trace("strings-assertion") << "get assertion: " << fact
<< endl
;
871 polarity
= fact
.getKind() != kind::NOT
;
872 atom
= polarity
? fact
: fact
[0];
874 //assert pending fact
875 assertPendingFact( atom
, polarity
, fact
);
879 Assert(d_strategy_init
);
880 std::map
<Effort
, std::pair
<unsigned, unsigned> >::iterator itsr
=
881 d_strat_steps
.find(e
);
882 if (!d_conflict
&& !d_valuation
.needCheck() && itsr
!= d_strat_steps
.end())
884 Trace("strings-check") << "Theory of strings " << e
<< " effort check "
886 if(Trace
.isOn("strings-eqc")) {
887 for( unsigned t
=0; t
<2; t
++ ) {
888 eq::EqClassesIterator eqcs2_i
= eq::EqClassesIterator( &d_equalityEngine
);
889 Trace("strings-eqc") << (t
==0 ? "STRINGS:" : "OTHER:") << std::endl
;
890 while( !eqcs2_i
.isFinished() ){
891 Node eqc
= (*eqcs2_i
);
892 bool print
= (t
==0 && eqc
.getType().isString() ) || (t
==1 && !eqc
.getType().isString() );
894 eq::EqClassIterator eqc2_i
= eq::EqClassIterator( eqc
, &d_equalityEngine
);
895 Trace("strings-eqc") << "Eqc( " << eqc
<< " ) : { ";
896 while( !eqc2_i
.isFinished() ) {
897 if( (*eqc2_i
)!=eqc
&& (*eqc2_i
).getKind()!=kind::EQUAL
){
898 Trace("strings-eqc") << (*eqc2_i
) << " ";
902 Trace("strings-eqc") << " } " << std::endl
;
903 EqcInfo
* ei
= getOrMakeEqcInfo( eqc
, false );
905 Trace("strings-eqc-debug") << "* Length term : " << ei
->d_length_term
.get() << std::endl
;
906 Trace("strings-eqc-debug") << "* Cardinality lemma k : " << ei
->d_cardinality_lem_k
.get() << std::endl
;
907 Trace("strings-eqc-debug") << "* Normalization length lemma : " << ei
->d_normalized_length
.get() << std::endl
;
912 Trace("strings-eqc") << std::endl
;
914 Trace("strings-eqc") << std::endl
;
916 unsigned sbegin
= itsr
->second
.first
;
917 unsigned send
= itsr
->second
.second
;
918 bool addedLemma
= false;
921 runStrategy(sbegin
, send
);
923 addedFact
= !d_pending
.empty();
924 addedLemma
= !d_lemma_cache
.empty();
927 // repeat if we did not add a lemma or conflict
928 }while( !d_conflict
&& !addedLemma
&& addedFact
);
930 Trace("strings-check") << "Theory of strings done full effort check " << addedLemma
<< " " << d_conflict
<< std::endl
;
932 Trace("strings-check") << "Theory of strings, done check : " << e
<< std::endl
;
933 Assert( d_pending
.empty() );
934 Assert( d_lemma_cache
.empty() );
937 bool TheoryStrings::needsCheckLastEffort() {
938 if( options::stringGuessModel() ){
939 return d_has_extf
.get();
945 void TheoryStrings::checkExtfReductions( int effort
) {
947 //std::vector< Node > nred;
948 //getExtTheory()->doReductions( effort, nred, false );
950 std::vector
< Node
> extf
= getExtTheory()->getActive();
951 Trace("strings-process") << " checking " << extf
.size() << " active extf"
953 for( unsigned i
=0; i
<extf
.size(); i
++ ){
955 Trace("strings-process") << " check " << n
<< ", active in model="
956 << d_extf_info_tmp
[n
].d_model_active
<< std::endl
;
958 int ret
= getReduction( effort
, n
, nr
);
959 Assert( nr
.isNull() );
961 getExtTheory()->markReduced( extf
[i
] );
970 TheoryStrings::EqcInfo::EqcInfo(context::Context
* c
)
973 d_cardinality_lem_k(c
),
974 d_normalized_length(c
)
978 TheoryStrings::EqcInfo
* TheoryStrings::getOrMakeEqcInfo( Node eqc
, bool doMake
) {
979 std::map
< Node
, EqcInfo
* >::iterator eqc_i
= d_eqc_info
.find( eqc
);
980 if( eqc_i
==d_eqc_info
.end() ){
982 EqcInfo
* ei
= new EqcInfo( getSatContext() );
983 d_eqc_info
[eqc
] = ei
;
989 return (*eqc_i
).second
;
994 /** Conflict when merging two constants */
995 void TheoryStrings::conflict(TNode a
, TNode b
){
997 Debug("strings-conflict") << "Making conflict..." << std::endl
;
1000 conflictNode
= explain( a
.eqNode(b
) );
1001 Trace("strings-conflict") << "CONFLICT: Eq engine conflict : " << conflictNode
<< std::endl
;
1002 d_out
->conflict( conflictNode
);
1006 /** called when a new equivalance class is created */
1007 void TheoryStrings::eqNotifyNewClass(TNode t
){
1008 Kind k
= t
.getKind();
1009 if (k
== kind::STRING_LENGTH
|| k
== kind::STRING_CODE
)
1011 Trace("strings-debug") << "New length eqc : " << t
<< std::endl
;
1012 Node r
= d_equalityEngine
.getRepresentative(t
[0]);
1013 EqcInfo
* ei
= getOrMakeEqcInfo( r
, true );
1014 if (k
== kind::STRING_LENGTH
)
1016 ei
->d_length_term
= t
[0];
1020 ei
->d_code_term
= t
[0];
1022 //we care about the length of this string
1023 registerTerm( t
[0], 1 );
1025 //getExtTheory()->registerTerm( t );
1029 /** called when two equivalance classes will merge */
1030 void TheoryStrings::eqNotifyPreMerge(TNode t1
, TNode t2
){
1031 EqcInfo
* e2
= getOrMakeEqcInfo(t2
, false);
1033 EqcInfo
* e1
= getOrMakeEqcInfo( t1
);
1034 //add information from e2 to e1
1035 if( !e2
->d_length_term
.get().isNull() ){
1036 e1
->d_length_term
.set( e2
->d_length_term
);
1038 if (!e2
->d_code_term
.get().isNull())
1040 e1
->d_code_term
.set(e2
->d_code_term
);
1042 if( e2
->d_cardinality_lem_k
.get()>e1
->d_cardinality_lem_k
.get() ) {
1043 e1
->d_cardinality_lem_k
.set( e2
->d_cardinality_lem_k
);
1045 if( !e2
->d_normalized_length
.get().isNull() ){
1046 e1
->d_normalized_length
.set( e2
->d_normalized_length
);
1051 /** called when two equivalance classes have merged */
1052 void TheoryStrings::eqNotifyPostMerge(TNode t1
, TNode t2
) {
1056 /** called when two equivalance classes are disequal */
1057 void TheoryStrings::eqNotifyDisequal(TNode t1
, TNode t2
, TNode reason
) {
1058 if( t1
.getType().isString() ){
1059 //store disequalities between strings, may need to check if their lengths are equal/disequal
1060 d_ee_disequalities
.push_back( t1
.eqNode( t2
) );
1064 void TheoryStrings::addCarePairs( quantifiers::TermArgTrie
* t1
, quantifiers::TermArgTrie
* t2
, unsigned arity
, unsigned depth
) {
1067 Node f1
= t1
->getNodeData();
1068 Node f2
= t2
->getNodeData();
1069 if( !d_equalityEngine
.areEqual( f1
, f2
) ){
1070 Trace("strings-cg-debug") << "TheoryStrings::computeCareGraph(): checking function " << f1
<< " and " << f2
<< std::endl
;
1071 vector
< pair
<TNode
, TNode
> > currentPairs
;
1072 for (unsigned k
= 0; k
< f1
.getNumChildren(); ++ k
) {
1075 Assert( d_equalityEngine
.hasTerm(x
) );
1076 Assert( d_equalityEngine
.hasTerm(y
) );
1077 Assert( !d_equalityEngine
.areDisequal( x
, y
, false ) );
1078 Assert( !areCareDisequal( x
, y
) );
1079 if( !d_equalityEngine
.areEqual( x
, y
) ){
1080 if( d_equalityEngine
.isTriggerTerm(x
, THEORY_STRINGS
) && d_equalityEngine
.isTriggerTerm(y
, THEORY_STRINGS
) ){
1081 TNode x_shared
= d_equalityEngine
.getTriggerTermRepresentative(x
, THEORY_STRINGS
);
1082 TNode y_shared
= d_equalityEngine
.getTriggerTermRepresentative(y
, THEORY_STRINGS
);
1083 currentPairs
.push_back(make_pair(x_shared
, y_shared
));
1087 for (unsigned c
= 0; c
< currentPairs
.size(); ++ c
) {
1088 Trace("strings-cg-pair") << "TheoryStrings::computeCareGraph(): pair : " << currentPairs
[c
].first
<< " " << currentPairs
[c
].second
<< std::endl
;
1089 addCarePair(currentPairs
[c
].first
, currentPairs
[c
].second
);
1095 if( depth
<(arity
-1) ){
1096 //add care pairs internal to each child
1097 for( std::map
< TNode
, quantifiers::TermArgTrie
>::iterator it
= t1
->d_data
.begin(); it
!= t1
->d_data
.end(); ++it
){
1098 addCarePairs( &it
->second
, NULL
, arity
, depth
+1 );
1101 //add care pairs based on each pair of non-disequal arguments
1102 for( std::map
< TNode
, quantifiers::TermArgTrie
>::iterator it
= t1
->d_data
.begin(); it
!= t1
->d_data
.end(); ++it
){
1103 std::map
< TNode
, quantifiers::TermArgTrie
>::iterator it2
= it
;
1105 for( ; it2
!= t1
->d_data
.end(); ++it2
){
1106 if( !d_equalityEngine
.areDisequal(it
->first
, it2
->first
, false) ){
1107 if( !areCareDisequal(it
->first
, it2
->first
) ){
1108 addCarePairs( &it
->second
, &it2
->second
, arity
, depth
+1 );
1114 //add care pairs based on product of indices, non-disequal arguments
1115 for( std::map
< TNode
, quantifiers::TermArgTrie
>::iterator it
= t1
->d_data
.begin(); it
!= t1
->d_data
.end(); ++it
){
1116 for( std::map
< TNode
, quantifiers::TermArgTrie
>::iterator it2
= t2
->d_data
.begin(); it2
!= t2
->d_data
.end(); ++it2
){
1117 if( !d_equalityEngine
.areDisequal(it
->first
, it2
->first
, false) ){
1118 if( !areCareDisequal(it
->first
, it2
->first
) ){
1119 addCarePairs( &it
->second
, &it2
->second
, arity
, depth
+1 );
1128 void TheoryStrings::computeCareGraph(){
1129 //computing the care graph here is probably still necessary, due to operators that take non-string arguments TODO: verify
1130 Trace("strings-cg") << "TheoryStrings::computeCareGraph(): Build term indices..." << std::endl
;
1131 std::map
< Node
, quantifiers::TermArgTrie
> index
;
1132 std::map
< Node
, unsigned > arity
;
1133 unsigned functionTerms
= d_functionsTerms
.size();
1134 for (unsigned i
= 0; i
< functionTerms
; ++ i
) {
1135 TNode f1
= d_functionsTerms
[i
];
1136 Trace("strings-cg") << "...build for " << f1
<< std::endl
;
1137 Node op
= f1
.getOperator();
1138 std::vector
< TNode
> reps
;
1139 bool has_trigger_arg
= false;
1140 for( unsigned j
=0; j
<f1
.getNumChildren(); j
++ ){
1141 reps
.push_back( d_equalityEngine
.getRepresentative( f1
[j
] ) );
1142 if( d_equalityEngine
.isTriggerTerm( f1
[j
], THEORY_STRINGS
) ){
1143 has_trigger_arg
= true;
1146 if( has_trigger_arg
){
1147 index
[op
].addTerm( f1
, reps
);
1148 arity
[op
] = reps
.size();
1152 for( std::map
< Node
, quantifiers::TermArgTrie
>::iterator itii
= index
.begin(); itii
!= index
.end(); ++itii
){
1153 Trace("strings-cg") << "TheoryStrings::computeCareGraph(): Process index " << itii
->first
<< "..." << std::endl
;
1154 addCarePairs( &itii
->second
, NULL
, arity
[ itii
->first
], 0 );
1158 void TheoryStrings::assertPendingFact(Node atom
, bool polarity
, Node exp
) {
1159 Trace("strings-pending") << "Assert pending fact : " << atom
<< " " << polarity
<< " from " << exp
<< std::endl
;
1160 Assert(atom
.getKind() != kind::OR
, "Infer error: a split.");
1161 if( atom
.getKind()==kind::EQUAL
){
1162 Trace("strings-pending-debug") << " Register term" << std::endl
;
1163 for( unsigned j
=0; j
<2; j
++ ) {
1164 if( !d_equalityEngine
.hasTerm( atom
[j
] ) && atom
[j
].getType().isString() ) {
1165 registerTerm( atom
[j
], 0 );
1168 Trace("strings-pending-debug") << " Now assert equality" << std::endl
;
1169 d_equalityEngine
.assertEquality( atom
, polarity
, exp
);
1170 Trace("strings-pending-debug") << " Finished assert equality" << std::endl
;
1172 d_equalityEngine
.assertPredicate( atom
, polarity
, exp
);
1174 if( atom
.getKind()==kind::STRING_IN_REGEXP
){
1175 if( polarity
&& atom
[1].getKind()==kind::REGEXP_RANGE
){
1176 if( d_extf_infer_cache_u
.find( atom
)==d_extf_infer_cache_u
.end() ){
1177 d_extf_infer_cache_u
.insert( atom
);
1178 //length of first argument is one
1179 Node conc
= d_one
.eqNode( NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, atom
[0] ) );
1180 Node lem
= NodeManager::currentNM()->mkNode( kind::OR
, atom
.negate(), conc
);
1181 Trace("strings-lemma") << "Strings::Lemma RE-Range-Len : " << lem
<< std::endl
;
1182 d_out
->lemma( lem
);
1186 //register the atom here, since it may not create a new equivalence class
1187 //getExtTheory()->registerTerm( atom );
1189 Trace("strings-pending-debug") << " Now collect terms" << std::endl
;
1190 //collect extended function terms in the atom
1191 getExtTheory()->registerTermRec( atom
);
1192 Trace("strings-pending-debug") << " Finished collect terms" << std::endl
;
1195 void TheoryStrings::doPendingFacts() {
1197 while( !d_conflict
&& i
<d_pending
.size() ) {
1198 Node fact
= d_pending
[i
];
1199 Node exp
= d_pending_exp
[ fact
];
1200 if(fact
.getKind() == kind::AND
) {
1201 for(size_t j
=0; j
<fact
.getNumChildren(); j
++) {
1202 bool polarity
= fact
[j
].getKind() != kind::NOT
;
1203 TNode atom
= polarity
? fact
[j
] : fact
[j
][0];
1204 assertPendingFact(atom
, polarity
, exp
);
1207 bool polarity
= fact
.getKind() != kind::NOT
;
1208 TNode atom
= polarity
? fact
: fact
[0];
1209 assertPendingFact(atom
, polarity
, exp
);
1214 d_pending_exp
.clear();
1217 void TheoryStrings::doPendingLemmas() {
1218 if( !d_conflict
&& !d_lemma_cache
.empty() ){
1219 for( unsigned i
=0; i
<d_lemma_cache
.size(); i
++ ){
1220 Trace("strings-pending") << "Process pending lemma : " << d_lemma_cache
[i
] << std::endl
;
1221 d_out
->lemma( d_lemma_cache
[i
] );
1223 for( std::map
< Node
, bool >::iterator it
= d_pending_req_phase
.begin(); it
!= d_pending_req_phase
.end(); ++it
){
1224 Trace("strings-pending") << "Require phase : " << it
->first
<< ", polarity = " << it
->second
<< std::endl
;
1225 d_out
->requirePhase( it
->first
, it
->second
);
1228 d_lemma_cache
.clear();
1229 d_pending_req_phase
.clear();
1232 bool TheoryStrings::hasProcessed() {
1233 return d_conflict
|| !d_lemma_cache
.empty() || !d_pending
.empty();
1236 void TheoryStrings::addToExplanation( Node a
, Node b
, std::vector
< Node
>& exp
) {
1238 Debug("strings-explain") << "Add to explanation : " << a
<< " == " << b
<< std::endl
;
1239 Assert( areEqual( a
, b
) );
1240 exp
.push_back( a
.eqNode( b
) );
1244 void TheoryStrings::addToExplanation( Node lit
, std::vector
< Node
>& exp
) {
1245 if( !lit
.isNull() ){
1246 exp
.push_back( lit
);
1250 void TheoryStrings::checkInit() {
1252 d_eqc_to_const
.clear();
1253 d_eqc_to_const_base
.clear();
1254 d_eqc_to_const_exp
.clear();
1255 d_eqc_to_len_term
.clear();
1256 d_term_index
.clear();
1257 d_strings_eqc
.clear();
1259 std::map
< Kind
, unsigned > ncongruent
;
1260 std::map
< Kind
, unsigned > congruent
;
1261 d_emptyString_r
= getRepresentative( d_emptyString
);
1262 eq::EqClassesIterator eqcs_i
= eq::EqClassesIterator( &d_equalityEngine
);
1263 while( !eqcs_i
.isFinished() ){
1264 Node eqc
= (*eqcs_i
);
1265 TypeNode tn
= eqc
.getType();
1266 if( !tn
.isRegExp() ){
1267 if( tn
.isString() ){
1268 d_strings_eqc
.push_back( eqc
);
1271 eq::EqClassIterator eqc_i
= eq::EqClassIterator( eqc
, &d_equalityEngine
);
1272 while( !eqc_i
.isFinished() ) {
1275 d_eqc_to_const
[eqc
] = n
;
1276 d_eqc_to_const_base
[eqc
] = n
;
1277 d_eqc_to_const_exp
[eqc
] = Node::null();
1278 }else if( tn
.isInteger() ){
1279 if( n
.getKind()==kind::STRING_LENGTH
){
1280 Node nr
= getRepresentative( n
[0] );
1281 d_eqc_to_len_term
[nr
] = n
[0];
1283 }else if( n
.getNumChildren()>0 ){
1284 Kind k
= n
.getKind();
1285 if( k
!=kind::EQUAL
){
1286 if( d_congruent
.find( n
)==d_congruent
.end() ){
1287 std::vector
< Node
> c
;
1288 Node nc
= d_term_index
[k
].add( n
, 0, this, d_emptyString_r
, c
);
1290 //check if we have inferred a new equality by removal of empty components
1291 if( n
.getKind()==kind::STRING_CONCAT
&& !areEqual( nc
, n
) ){
1292 std::vector
< Node
> exp
;
1293 unsigned count
[2] = { 0, 0 };
1294 while( count
[0]<nc
.getNumChildren() || count
[1]<n
.getNumChildren() ){
1295 //explain empty prefixes
1296 for( unsigned t
=0; t
<2; t
++ ){
1297 Node nn
= t
==0 ? nc
: n
;
1298 while( count
[t
]<nn
.getNumChildren() &&
1299 ( nn
[count
[t
]]==d_emptyString
|| areEqual( nn
[count
[t
]], d_emptyString
) ) ){
1300 if( nn
[count
[t
]]!=d_emptyString
){
1301 exp
.push_back( nn
[count
[t
]].eqNode( d_emptyString
) );
1306 //explain equal components
1307 if( count
[0]<nc
.getNumChildren() ){
1308 Assert( count
[1]<n
.getNumChildren() );
1309 if( nc
[count
[0]]!=n
[count
[1]] ){
1310 exp
.push_back( nc
[count
[0]].eqNode( n
[count
[1]] ) );
1316 //infer the equality
1317 sendInference( exp
, n
.eqNode( nc
), "I_Norm" );
1318 }else if( getExtTheory()->hasFunctionKind( n
.getKind() ) ){
1319 //mark as congruent : only process if neither has been reduced
1320 getExtTheory()->markCongruent( nc
, n
);
1322 //this node is congruent to another one, we can ignore it
1323 Trace("strings-process-debug") << " congruent term : " << n
<< std::endl
;
1324 d_congruent
.insert( n
);
1326 }else if( k
==kind::STRING_CONCAT
&& c
.size()==1 ){
1327 Trace("strings-process-debug") << " congruent term by singular : " << n
<< " " << c
[0] << std::endl
;
1329 if( !areEqual( c
[0], n
) ){
1330 std::vector
< Node
> exp
;
1331 //explain empty components
1332 bool foundNEmpty
= false;
1333 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
1334 if( areEqual( n
[i
], d_emptyString
) ){
1335 if( n
[i
]!=d_emptyString
){
1336 exp
.push_back( n
[i
].eqNode( d_emptyString
) );
1339 Assert( !foundNEmpty
);
1341 exp
.push_back( n
[i
].eqNode( c
[0] ) );
1346 AlwaysAssert( foundNEmpty
);
1347 //infer the equality
1348 sendInference( exp
, n
.eqNode( c
[0] ), "I_Norm_S" );
1350 d_congruent
.insert( n
);
1360 if( d_congruent
.find( n
)==d_congruent
.end() ){
1364 Trace("strings-process-debug") << " congruent variable : " << n
<< std::endl
;
1365 d_congruent
.insert( n
);
1374 if( Trace
.isOn("strings-process") ){
1375 for( std::map
< Kind
, TermIndex
>::iterator it
= d_term_index
.begin(); it
!= d_term_index
.end(); ++it
){
1376 Trace("strings-process") << " Terms[" << it
->first
<< "] = " << ncongruent
[it
->first
] << "/" << (congruent
[it
->first
]+ncongruent
[it
->first
]) << std::endl
;
1381 void TheoryStrings::checkConstantEquivalenceClasses()
1385 std::vector
<Node
> vecc
;
1389 Trace("strings-process-debug") << "Check constant equivalence classes..."
1391 prevSize
= d_eqc_to_const
.size();
1392 checkConstantEquivalenceClasses(&d_term_index
[kind::STRING_CONCAT
], vecc
);
1393 } while (!hasProcessed() && d_eqc_to_const
.size() > prevSize
);
1396 void TheoryStrings::checkConstantEquivalenceClasses( TermIndex
* ti
, std::vector
< Node
>& vecc
) {
1397 Node n
= ti
->d_data
;
1399 //construct the constant
1400 Node c
= mkConcat( vecc
);
1401 if( !areEqual( n
, c
) ){
1402 Trace("strings-debug") << "Constant eqc : " << c
<< " for " << n
<< std::endl
;
1403 Trace("strings-debug") << " ";
1404 for( unsigned i
=0; i
<vecc
.size(); i
++ ){
1405 Trace("strings-debug") << vecc
[i
] << " ";
1407 Trace("strings-debug") << std::endl
;
1409 unsigned countc
= 0;
1410 std::vector
< Node
> exp
;
1411 while( count
<n
.getNumChildren() ){
1412 while( count
<n
.getNumChildren() && areEqual( n
[count
], d_emptyString
) ){
1413 addToExplanation( n
[count
], d_emptyString
, exp
);
1416 if( count
<n
.getNumChildren() ){
1417 Trace("strings-debug") << "...explain " << n
[count
] << " " << vecc
[countc
] << std::endl
;
1418 if( !areEqual( n
[count
], vecc
[countc
] ) ){
1419 Node nrr
= getRepresentative( n
[count
] );
1420 Assert( !d_eqc_to_const_exp
[nrr
].isNull() );
1421 addToExplanation( n
[count
], d_eqc_to_const_base
[nrr
], exp
);
1422 exp
.push_back( d_eqc_to_const_exp
[nrr
] );
1424 addToExplanation( n
[count
], vecc
[countc
], exp
);
1430 //exp contains an explanation of n==c
1431 Assert( countc
==vecc
.size() );
1433 sendInference( exp
, n
.eqNode( c
), "I_CONST_MERGE" );
1435 }else if( !hasProcessed() ){
1436 Node nr
= getRepresentative( n
);
1437 std::map
< Node
, Node
>::iterator it
= d_eqc_to_const
.find( nr
);
1438 if( it
==d_eqc_to_const
.end() ){
1439 Trace("strings-debug") << "Set eqc const " << n
<< " to " << c
<< std::endl
;
1440 d_eqc_to_const
[nr
] = c
;
1441 d_eqc_to_const_base
[nr
] = n
;
1442 d_eqc_to_const_exp
[nr
] = mkAnd( exp
);
1443 }else if( c
!=it
->second
){
1445 Trace("strings-debug") << "Conflict, other constant was " << it
->second
<< ", this constant was " << c
<< std::endl
;
1446 if( d_eqc_to_const_exp
[nr
].isNull() ){
1447 // n==c ^ n == c' => false
1448 addToExplanation( n
, it
->second
, exp
);
1450 // n==c ^ n == d_eqc_to_const_base[nr] == c' => false
1451 exp
.push_back( d_eqc_to_const_exp
[nr
] );
1452 addToExplanation( n
, d_eqc_to_const_base
[nr
], exp
);
1454 sendInference( exp
, d_false
, "I_CONST_CONFLICT" );
1457 Trace("strings-debug") << "Duplicate constant." << std::endl
;
1462 for( std::map
< TNode
, TermIndex
>::iterator it
= ti
->d_children
.begin(); it
!= ti
->d_children
.end(); ++it
){
1463 std::map
< Node
, Node
>::iterator itc
= d_eqc_to_const
.find( it
->first
);
1464 if( itc
!=d_eqc_to_const
.end() ){
1465 vecc
.push_back( itc
->second
);
1466 checkConstantEquivalenceClasses( &it
->second
, vecc
);
1468 if( hasProcessed() ){
1475 void TheoryStrings::checkExtfEval( int effort
) {
1476 Trace("strings-extf-list") << "Active extended functions, effort=" << effort
<< " : " << std::endl
;
1477 d_extf_info_tmp
.clear();
1478 bool has_nreduce
= false;
1479 std::vector
< Node
> terms
= getExtTheory()->getActive();
1480 std::vector
< Node
> sterms
;
1481 std::vector
< std::vector
< Node
> > exp
;
1482 getExtTheory()->getSubstitutedTerms( effort
, terms
, sterms
, exp
);
1483 for( unsigned i
=0; i
<terms
.size(); i
++ ){
1485 Node sn
= sterms
[i
];
1486 //setup information about extf
1487 d_extf_info_tmp
[n
].init();
1488 std::map
< Node
, ExtfInfoTmp
>::iterator itit
= d_extf_info_tmp
.find( n
);
1489 if( n
.getType().isBoolean() ){
1490 if( areEqual( n
, d_true
) ){
1491 itit
->second
.d_pol
= 1;
1492 }else if( areEqual( n
, d_false
) ){
1493 itit
->second
.d_pol
= -1;
1496 Trace("strings-extf-debug") << "Check extf " << n
<< " == " << sn
<< ", pol = " << itit
->second
.d_pol
<< ", effort=" << effort
<< "..." << std::endl
;
1500 itit
->second
.d_exp
.insert( itit
->second
.d_exp
.end(), exp
[i
].begin(), exp
[i
].end() );
1501 // inference is rewriting the substituted node
1502 Node nrc
= Rewriter::rewrite( sn
);
1503 //if rewrites to a constant, then do the inference and mark as reduced
1504 if( nrc
.isConst() ){
1506 getExtTheory()->markReduced( n
);
1507 Trace("strings-extf-debug") << " resolvable by evaluation..." << std::endl
;
1508 std::vector
< Node
> exps
;
1509 // The following optimization gets the "symbolic definition" of
1510 // an extended term. The symbolic definition of a term t is a term
1511 // t' where constants are replaced by their corresponding proxy
1513 // For example, if lsym is a proxy variable for "", then
1514 // str.replace( lsym, lsym, lsym ) is the symbolic definition for
1515 // str.replace( "", "", "" ). It is generally better to use symbolic
1516 // definitions when doing cd-rewriting for the purpose of minimizing
1517 // clauses, e.g. we infer the unit equality:
1518 // str.replace( lsym, lsym, lsym ) == ""
1519 // instead of making this inference multiple times:
1520 // x = "" => str.replace( x, x, x ) == ""
1521 // y = "" => str.replace( y, y, y ) == ""
1522 Trace("strings-extf-debug") << " get symbolic definition..." << std::endl
;
1523 Node nrs
= getSymbolicDefinition( sn
, exps
);
1524 if( !nrs
.isNull() ){
1525 Trace("strings-extf-debug") << " rewrite " << nrs
<< "..." << std::endl
;
1526 Node nrsr
= Rewriter::rewrite(nrs
);
1527 // ensure the symbolic form is not rewritable
1530 // we cannot use the symbolic definition if it rewrites
1531 Trace("strings-extf-debug") << " symbolic definition is trivial..." << std::endl
;
1535 Trace("strings-extf-debug") << " could not infer symbolic definition." << std::endl
;
1538 if( !nrs
.isNull() ){
1539 Trace("strings-extf-debug") << " symbolic def : " << nrs
<< std::endl
;
1540 if( !areEqual( nrs
, nrc
) ){
1541 //infer symbolic unit
1542 if( n
.getType().isBoolean() ){
1543 conc
= nrc
==d_true
? nrs
: nrs
.negate();
1545 conc
= nrs
.eqNode( nrc
);
1547 itit
->second
.d_exp
.clear();
1550 if( !areEqual( n
, nrc
) ){
1551 if( n
.getType().isBoolean() ){
1552 if( areEqual( n
, nrc
==d_true
? d_false
: d_true
) ){
1553 itit
->second
.d_exp
.push_back( nrc
==d_true
? n
.negate() : n
);
1556 conc
= nrc
==d_true
? n
: n
.negate();
1559 conc
= n
.eqNode( nrc
);
1563 if( !conc
.isNull() ){
1564 Trace("strings-extf") << " resolve extf : " << sn
<< " -> " << nrc
<< std::endl
;
1565 sendInference( itit
->second
.d_exp
, conc
, effort
==0 ? "EXTF" : "EXTF-N", true );
1567 Trace("strings-extf-debug") << " conflict, return." << std::endl
;
1572 //check if it is already equal, if so, mark as reduced. Otherwise, do nothing.
1573 if( areEqual( n
, nrc
) ){
1574 Trace("strings-extf") << " resolved extf, since satisfied by model: " << n
<< std::endl
;
1575 itit
->second
.d_model_active
= false;
1578 //if it reduces to a conjunction, infer each and reduce
1579 }else if( ( nrc
.getKind()==kind::OR
&& itit
->second
.d_pol
==-1 ) || ( nrc
.getKind()==kind::AND
&& itit
->second
.d_pol
==1 ) ){
1581 getExtTheory()->markReduced( n
);
1582 itit
->second
.d_exp
.push_back( itit
->second
.d_pol
==-1 ? n
.negate() : n
);
1583 Trace("strings-extf-debug") << " decomposable..." << std::endl
;
1584 Trace("strings-extf") << " resolve extf : " << sn
<< " -> " << nrc
<< ", pol = " << itit
->second
.d_pol
<< std::endl
;
1585 for( unsigned i
=0; i
<nrc
.getNumChildren(); i
++ ){
1586 sendInference( itit
->second
.d_exp
, itit
->second
.d_pol
==-1 ? nrc
[i
].negate() : nrc
[i
], effort
==0 ? "EXTF_d" : "EXTF_d-N" );
1592 to_reduce
= sterms
[i
];
1595 if( !to_reduce
.isNull() ){
1598 Trace("strings-extf") << " cannot rewrite extf : " << to_reduce
<< std::endl
;
1600 checkExtfInference( n
, to_reduce
, itit
->second
, effort
);
1601 if( Trace
.isOn("strings-extf-list") ){
1602 Trace("strings-extf-list") << " * " << to_reduce
;
1603 if( itit
->second
.d_pol
!=0 ){
1604 Trace("strings-extf-list") << ", pol = " << itit
->second
.d_pol
;
1607 Trace("strings-extf-list") << ", from " << n
;
1609 Trace("strings-extf-list") << std::endl
;
1611 if( getExtTheory()->isActive( n
) && itit
->second
.d_model_active
){
1616 d_has_extf
= has_nreduce
;
1619 void TheoryStrings::checkExtfInference( Node n
, Node nr
, ExtfInfoTmp
& in
, int effort
){
1620 //make additional inferences that do not contribute to the reduction of n, but may help show a refutation
1622 //add original to explanation
1623 in
.d_exp
.push_back( in
.d_pol
==1 ? n
: n
.negate() );
1625 //d_extf_infer_cache stores whether we have made the inferences associated with a node n,
1626 // this may need to be generalized if multiple inferences apply
1628 if( nr
.getKind()==kind::STRING_STRCTN
){
1629 if( ( in
.d_pol
==1 && nr
[1].getKind()==kind::STRING_CONCAT
) || ( in
.d_pol
==-1 && nr
[0].getKind()==kind::STRING_CONCAT
) ){
1630 if( d_extf_infer_cache
.find( nr
)==d_extf_infer_cache
.end() ){
1631 d_extf_infer_cache
.insert( nr
);
1633 //one argument does (not) contain each of the components of the other argument
1634 int index
= in
.d_pol
==1 ? 1 : 0;
1635 std::vector
< Node
> children
;
1636 children
.push_back( nr
[0] );
1637 children
.push_back( nr
[1] );
1638 //Node exp_n = mkAnd( exp );
1639 for( unsigned i
=0; i
<nr
[index
].getNumChildren(); i
++ ){
1640 children
[index
] = nr
[index
][i
];
1641 Node conc
= NodeManager::currentNM()->mkNode( kind::STRING_STRCTN
, children
);
1642 conc
= Rewriter::rewrite(in
.d_pol
== 1 ? conc
: conc
.negate());
1643 // check if it already (does not) hold
1646 if (areEqual(conc
, d_false
))
1648 // should be a conflict
1649 sendInference(in
.d_exp
, conc
, "CTN_Decompose");
1651 else if (getExtTheory()->hasFunctionKind(conc
.getKind()))
1653 // can mark as reduced, since model for n => model for conc
1654 getExtTheory()->markReduced(conc
);
1661 //store this (reduced) assertion
1662 //Assert( effort==0 || nr[0]==getRepresentative( nr[0] ) );
1663 bool pol
= in
.d_pol
==1;
1664 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() ){
1665 Trace("strings-extf-debug") << " store contains info : " << nr
[0] << " " << pol
<< " " << nr
[1] << std::endl
;
1666 d_extf_info_tmp
[nr
[0]].d_ctn
[pol
].push_back( nr
[1] );
1667 d_extf_info_tmp
[nr
[0]].d_ctn_from
[pol
].push_back( n
);
1668 //transitive closure for contains
1670 for( unsigned i
=0; i
<d_extf_info_tmp
[nr
[0]].d_ctn
[opol
].size(); i
++ ){
1671 Node onr
= d_extf_info_tmp
[nr
[0]].d_ctn
[opol
][i
];
1672 Node conc
= NodeManager::currentNM()->mkNode( kind::STRING_STRCTN
, pol
? nr
[1] : onr
, pol
? onr
: nr
[1] );
1673 conc
= Rewriter::rewrite( conc
);
1674 bool do_infer
= false;
1675 if( conc
.getKind()==kind::EQUAL
){
1676 do_infer
= !areDisequal( conc
[0], conc
[1] );
1678 do_infer
= !areEqual( conc
, d_false
);
1681 conc
= conc
.negate();
1682 std::vector
< Node
> exp_c
;
1683 exp_c
.insert( exp_c
.end(), in
.d_exp
.begin(), in
.d_exp
.end() );
1684 Node ofrom
= d_extf_info_tmp
[nr
[0]].d_ctn_from
[opol
][i
];
1685 Assert( d_extf_info_tmp
.find( ofrom
)!=d_extf_info_tmp
.end() );
1686 exp_c
.insert( exp_c
.end(), d_extf_info_tmp
[ofrom
].d_exp
.begin(), d_extf_info_tmp
[ofrom
].d_exp
.end() );
1687 sendInference( exp_c
, conc
, "CTN_Trans" );
1691 Trace("strings-extf-debug") << " redundant." << std::endl
;
1692 getExtTheory()->markReduced( n
);
1699 Node
TheoryStrings::getSymbolicDefinition( Node n
, std::vector
< Node
>& exp
) {
1700 if( n
.getNumChildren()==0 ){
1701 NodeNodeMap::const_iterator it
= d_proxy_var
.find( n
);
1702 if( it
==d_proxy_var
.end() ){
1703 return Node::null();
1705 Node eq
= n
.eqNode( (*it
).second
);
1706 eq
= Rewriter::rewrite( eq
);
1707 if( std::find( exp
.begin(), exp
.end(), eq
)==exp
.end() ){
1708 exp
.push_back( eq
);
1710 return (*it
).second
;
1713 std::vector
< Node
> children
;
1714 if (n
.getMetaKind() == kind::metakind::PARAMETERIZED
) {
1715 children
.push_back( n
.getOperator() );
1717 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
1718 if( n
.getKind()==kind::STRING_IN_REGEXP
&& i
==1 ){
1719 children
.push_back( n
[i
] );
1721 Node ns
= getSymbolicDefinition( n
[i
], exp
);
1723 return Node::null();
1725 children
.push_back( ns
);
1729 return NodeManager::currentNM()->mkNode( n
.getKind(), children
);
1733 Node
TheoryStrings::getConstantEqc( Node eqc
) {
1734 std::map
< Node
, Node
>::iterator it
= d_eqc_to_const
.find( eqc
);
1735 if( it
!=d_eqc_to_const
.end() ){
1738 return Node::null();
1742 void TheoryStrings::debugPrintFlatForms( const char * tc
){
1743 for( unsigned k
=0; k
<d_strings_eqc
.size(); k
++ ){
1744 Node eqc
= d_strings_eqc
[k
];
1745 if( d_eqc
[eqc
].size()>1 ){
1746 Trace( tc
) << "EQC [" << eqc
<< "]" << std::endl
;
1748 Trace( tc
) << "eqc [" << eqc
<< "]";
1750 std::map
< Node
, Node
>::iterator itc
= d_eqc_to_const
.find( eqc
);
1751 if( itc
!=d_eqc_to_const
.end() ){
1752 Trace( tc
) << " C: " << itc
->second
;
1753 if( d_eqc
[eqc
].size()>1 ){
1754 Trace( tc
) << std::endl
;
1757 if( d_eqc
[eqc
].size()>1 ){
1758 for( unsigned i
=0; i
<d_eqc
[eqc
].size(); i
++ ){
1759 Node n
= d_eqc
[eqc
][i
];
1761 for( unsigned j
=0; j
<d_flat_form
[n
].size(); j
++ ){
1762 Node fc
= d_flat_form
[n
][j
];
1763 itc
= d_eqc_to_const
.find( fc
);
1765 if( itc
!=d_eqc_to_const
.end() ){
1766 Trace( tc
) << itc
->second
;
1772 Trace( tc
) << ", from " << n
;
1774 Trace( tc
) << std::endl
;
1777 Trace( tc
) << std::endl
;
1780 Trace( tc
) << std::endl
;
1783 void TheoryStrings::debugPrintNormalForms( const char * tc
) {
1786 struct sortConstLength
{
1787 std::map
< Node
, unsigned > d_const_length
;
1788 bool operator() (Node i
, Node j
) {
1789 std::map
< Node
, unsigned >::iterator it_i
= d_const_length
.find( i
);
1790 std::map
< Node
, unsigned >::iterator it_j
= d_const_length
.find( j
);
1791 if( it_i
==d_const_length
.end() ){
1792 if( it_j
==d_const_length
.end() ){
1798 if( it_j
==d_const_length
.end() ){
1801 return it_i
->second
<it_j
->second
;
1807 void TheoryStrings::checkCycles()
1809 // first check for cycles, while building ordering of equivalence classes
1810 d_flat_form
.clear();
1811 d_flat_form_index
.clear();
1813 //rebuild strings eqc based on acyclic ordering
1814 std::vector
< Node
> eqc
;
1815 eqc
.insert( eqc
.end(), d_strings_eqc
.begin(), d_strings_eqc
.end() );
1816 d_strings_eqc
.clear();
1817 if( options::stringBinaryCsp() ){
1818 //sort: process smallest constants first (necessary if doing binary splits)
1819 sortConstLength scl
;
1820 for( unsigned i
=0; i
<eqc
.size(); i
++ ){
1821 std::map
< Node
, Node
>::iterator itc
= d_eqc_to_const
.find( eqc
[i
] );
1822 if( itc
!=d_eqc_to_const
.end() ){
1823 scl
.d_const_length
[eqc
[i
]] = itc
->second
.getConst
<String
>().size();
1826 std::sort( eqc
.begin(), eqc
.end(), scl
);
1828 for( unsigned i
=0; i
<eqc
.size(); i
++ ){
1829 std::vector
< Node
> curr
;
1830 std::vector
< Node
> exp
;
1831 checkCycles( eqc
[i
], curr
, exp
);
1832 if( hasProcessed() ){
1838 void TheoryStrings::checkFlatForms()
1840 // debug print flat forms
1841 if (Trace
.isOn("strings-ff"))
1843 Trace("strings-ff") << "Flat forms : " << std::endl
;
1844 debugPrintFlatForms("strings-ff");
1847 // inferences without recursively expanding flat forms
1849 //(1) approximate equality by containment, infer conflicts
1850 for (const Node
& eqc
: d_strings_eqc
)
1852 Node c
= getConstantEqc(eqc
);
1855 // if equivalence class is constant, all component constants in flat forms
1856 // must be contained in it, in order
1857 std::map
<Node
, std::vector
<Node
> >::iterator it
= d_eqc
.find(eqc
);
1858 if (it
!= d_eqc
.end())
1860 for (const Node
& n
: it
->second
)
1863 if (!TheoryStringsRewriter::canConstantContainList(
1864 c
, d_flat_form
[n
], firstc
, lastc
))
1866 Trace("strings-ff-debug") << "Flat form for " << n
1867 << " cannot be contained in constant "
1869 Trace("strings-ff-debug") << " indices = " << firstc
<< "/"
1870 << lastc
<< std::endl
;
1871 // conflict, explanation is n = base ^ base = c ^ relevant portion
1873 std::vector
<Node
> exp
;
1874 Assert(d_eqc_to_const_base
.find(eqc
) != d_eqc_to_const_base
.end());
1875 addToExplanation(n
, d_eqc_to_const_base
[eqc
], exp
);
1876 Assert(d_eqc_to_const_exp
.find(eqc
) != d_eqc_to_const_exp
.end());
1877 if (!d_eqc_to_const_exp
[eqc
].isNull())
1879 exp
.push_back(d_eqc_to_const_exp
[eqc
]);
1881 for (int e
= firstc
; e
<= lastc
; e
++)
1883 if (d_flat_form
[n
][e
].isConst())
1885 Assert(e
>= 0 && e
< (int)d_flat_form_index
[n
].size());
1886 Assert(d_flat_form_index
[n
][e
] >= 0
1887 && d_flat_form_index
[n
][e
] < (int)n
.getNumChildren());
1889 d_flat_form
[n
][e
], n
[d_flat_form_index
[n
][e
]], exp
);
1892 Node conc
= d_false
;
1893 sendInference(exp
, conc
, "F_NCTN");
1901 //(2) scan lists, unification to infer conflicts and equalities
1902 for (const Node
& eqc
: d_strings_eqc
)
1904 std::map
<Node
, std::vector
<Node
> >::iterator it
= d_eqc
.find(eqc
);
1905 if (it
== d_eqc
.end() || it
->second
.size() <= 1)
1909 // iterate over start index
1910 for (unsigned start
= 0; start
< it
->second
.size() - 1; start
++)
1912 for (unsigned r
= 0; r
< 2; r
++)
1914 bool isRev
= r
== 1;
1915 checkFlatForm(it
->second
, start
, isRev
);
1925 void TheoryStrings::checkFlatForm(std::vector
<Node
>& eqc
,
1930 std::vector
<Node
> inelig
;
1931 for (unsigned i
= 0; i
<= start
; i
++)
1933 inelig
.push_back(eqc
[start
]);
1935 Node a
= eqc
[start
];
1939 std::vector
<Node
> exp
;
1942 unsigned eqc_size
= eqc
.size();
1943 unsigned asize
= d_flat_form
[a
].size();
1946 for (unsigned i
= start
+ 1; i
< eqc_size
; i
++)
1949 if (std::find(inelig
.begin(), inelig
.end(), b
) == inelig
.end())
1951 unsigned bsize
= d_flat_form
[b
].size();
1955 std::vector
<Node
> conc_c
;
1956 for (unsigned j
= count
; j
< bsize
; j
++)
1959 b
[d_flat_form_index
[b
][j
]].eqNode(d_emptyString
));
1961 Assert(!conc_c
.empty());
1962 conc
= mkAnd(conc_c
);
1965 // swap, will enforce is empty past current
1971 inelig
.push_back(eqc
[i
]);
1977 Node curr
= d_flat_form
[a
][count
];
1978 Node curr_c
= getConstantEqc(curr
);
1979 Node ac
= a
[d_flat_form_index
[a
][count
]];
1980 std::vector
<Node
> lexp
;
1981 Node lcurr
= getLength(ac
, lexp
);
1982 for (unsigned i
= 1; i
< eqc_size
; i
++)
1985 if (std::find(inelig
.begin(), inelig
.end(), b
) == inelig
.end())
1987 if (count
== d_flat_form
[b
].size())
1989 inelig
.push_back(b
);
1991 std::vector
<Node
> conc_c
;
1992 for (unsigned j
= count
; j
< asize
; j
++)
1995 a
[d_flat_form_index
[a
][j
]].eqNode(d_emptyString
));
1997 Assert(!conc_c
.empty());
1998 conc
= mkAnd(conc_c
);
2006 Node cc
= d_flat_form
[b
][count
];
2009 Node bc
= b
[d_flat_form_index
[b
][count
]];
2010 inelig
.push_back(b
);
2011 Assert(!areEqual(curr
, cc
));
2012 Node cc_c
= getConstantEqc(cc
);
2013 if (!curr_c
.isNull() && !cc_c
.isNull())
2015 // check for constant conflict
2017 Node s
= TheoryStringsRewriter::splitConstant(
2018 cc_c
, curr_c
, index
, isRev
);
2021 addToExplanation(ac
, d_eqc_to_const_base
[curr
], exp
);
2022 addToExplanation(d_eqc_to_const_exp
[curr
], exp
);
2023 addToExplanation(bc
, d_eqc_to_const_base
[cc
], exp
);
2024 addToExplanation(d_eqc_to_const_exp
[cc
], exp
);
2030 else if ((d_flat_form
[a
].size() - 1) == count
2031 && (d_flat_form
[b
].size() - 1) == count
)
2033 conc
= ac
.eqNode(bc
);
2039 // if lengths are the same, apply LengthEq
2040 std::vector
<Node
> lexp2
;
2041 Node lcc
= getLength(bc
, lexp2
);
2042 if (areEqual(lcurr
, lcc
))
2044 Trace("strings-ff-debug") << "Infer " << ac
<< " == " << bc
2045 << " since " << lcurr
2046 << " == " << lcc
<< std::endl
;
2047 // exp_n.push_back( getLength( curr, true ).eqNode(
2048 // getLength( cc, true ) ) );
2049 Trace("strings-ff-debug") << "Explanation for " << lcurr
2051 for (unsigned j
= 0; j
< lexp
.size(); j
++)
2053 Trace("strings-ff-debug") << lexp
[j
] << std::endl
;
2055 Trace("strings-ff-debug") << "Explanation for " << lcc
2057 for (unsigned j
= 0; j
< lexp2
.size(); j
++)
2059 Trace("strings-ff-debug") << lexp2
[j
] << std::endl
;
2061 exp
.insert(exp
.end(), lexp
.begin(), lexp
.end());
2062 exp
.insert(exp
.end(), lexp2
.begin(), lexp2
.end());
2063 addToExplanation(lcurr
, lcc
, exp
);
2064 conc
= ac
.eqNode(bc
);
2076 Trace("strings-ff-debug")
2077 << "Found inference : " << conc
<< " based on equality " << a
2078 << " == " << b
<< ", " << isRev
<< " " << inf_type
<< std::endl
;
2079 addToExplanation(a
, b
, exp
);
2080 // explain why prefixes up to now were the same
2081 for (unsigned j
= 0; j
< count
; j
++)
2083 Trace("strings-ff-debug") << "Add at " << d_flat_form_index
[a
][j
] << " "
2084 << d_flat_form_index
[b
][j
] << std::endl
;
2086 a
[d_flat_form_index
[a
][j
]], b
[d_flat_form_index
[b
][j
]], exp
);
2088 // explain why other components up to now are empty
2089 for (unsigned t
= 0; t
< 2; t
++)
2091 Node c
= t
== 0 ? a
: b
;
2093 if (inf_type
== 3 || (t
== 1 && inf_type
== 2))
2095 // explain all the empty components for F_EndpointEq, all for
2096 // the short end for F_EndpointEmp
2097 jj
= isRev
? -1 : c
.getNumChildren();
2101 jj
= t
== 0 ? d_flat_form_index
[a
][count
]
2102 : d_flat_form_index
[b
][count
];
2104 int startj
= isRev
? jj
+ 1 : 0;
2105 int endj
= isRev
? c
.getNumChildren() : jj
;
2106 for (int j
= startj
; j
< endj
; j
++)
2108 if (areEqual(c
[j
], d_emptyString
))
2110 addToExplanation(c
[j
], d_emptyString
, exp
);
2114 // notice that F_EndpointEmp is not typically applied, since
2115 // strict prefix equality ( a.b = a ) where a,b non-empty
2116 // is conflicting by arithmetic len(a.b)=len(a)+len(b)!=len(a)
2123 : (inf_type
== 1 ? "F_Unify" : (inf_type
== 2 ? "F_EndpointEmp"
2124 : "F_EndpointEq")));
2132 } while (inelig
.size() < eqc
.size());
2134 for (const Node
& n
: eqc
)
2136 std::reverse(d_flat_form
[n
].begin(), d_flat_form
[n
].end());
2137 std::reverse(d_flat_form_index
[n
].begin(), d_flat_form_index
[n
].end());
2141 Node
TheoryStrings::checkCycles( Node eqc
, std::vector
< Node
>& curr
, std::vector
< Node
>& exp
){
2142 if( std::find( curr
.begin(), curr
.end(), eqc
)!=curr
.end() ){
2145 }else if( std::find( d_strings_eqc
.begin(), d_strings_eqc
.end(), eqc
)==d_strings_eqc
.end() ){
2146 curr
.push_back( eqc
);
2147 //look at all terms in this equivalence class
2148 eq::EqClassIterator eqc_i
= eq::EqClassIterator( eqc
, &d_equalityEngine
);
2149 while( !eqc_i
.isFinished() ) {
2151 if( d_congruent
.find( n
)==d_congruent
.end() ){
2152 if( n
.getKind() == kind::STRING_CONCAT
){
2153 Trace("strings-cycle") << eqc
<< " check term : " << n
<< " in " << eqc
<< std::endl
;
2154 if( eqc
!=d_emptyString_r
){
2155 d_eqc
[eqc
].push_back( n
);
2157 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
2158 Node nr
= getRepresentative( n
[i
] );
2159 if( eqc
==d_emptyString_r
){
2160 //for empty eqc, ensure all components are empty
2161 if( nr
!=d_emptyString_r
){
2162 std::vector
< Node
> exp
;
2163 exp
.push_back( n
.eqNode( d_emptyString
) );
2164 sendInference( exp
, n
[i
].eqNode( d_emptyString
), "I_CYCLE_E" );
2165 return Node::null();
2168 if( nr
!=d_emptyString_r
){
2169 d_flat_form
[n
].push_back( nr
);
2170 d_flat_form_index
[n
].push_back( i
);
2172 //for non-empty eqc, recurse and see if we find a loop
2173 Node ncy
= checkCycles( nr
, curr
, exp
);
2174 if( !ncy
.isNull() ){
2175 Trace("strings-cycle") << eqc
<< " cycle: " << ncy
<< " at " << n
<< "[" << i
<< "] : " << n
[i
] << std::endl
;
2176 addToExplanation( n
, eqc
, exp
);
2177 addToExplanation( nr
, n
[i
], exp
);
2179 //can infer all other components must be empty
2180 for( unsigned j
=0; j
<n
.getNumChildren(); j
++ ){
2181 //take first non-empty
2182 if( j
!=i
&& !areEqual( n
[j
], d_emptyString
) ){
2183 sendInference( exp
, n
[j
].eqNode( d_emptyString
), "I_CYCLE" );
2184 return Node::null();
2187 Trace("strings-error") << "Looping term should be congruent : " << n
<< " " << eqc
<< " " << ncy
<< std::endl
;
2188 //should find a non-empty component, otherwise would have been singular congruent (I_Norm_S)
2194 if( hasProcessed() ){
2195 return Node::null();
2205 //now we can add it to the list of equivalence classes
2206 d_strings_eqc
.push_back( eqc
);
2210 return Node::null();
2213 void TheoryStrings::checkNormalFormsEq()
2215 if( !options::stringEagerLen() ){
2216 for( unsigned i
=0; i
<d_strings_eqc
.size(); i
++ ) {
2217 Node eqc
= d_strings_eqc
[i
];
2218 eq::EqClassIterator eqc_i
= eq::EqClassIterator( eqc
, &d_equalityEngine
);
2219 while( !eqc_i
.isFinished() ) {
2221 if( d_congruent
.find( n
)==d_congruent
.end() ){
2222 registerTerm( n
, 2 );
2233 // calculate normal forms for each equivalence class, possibly adding
2235 d_normal_forms
.clear();
2236 d_normal_forms_exp
.clear();
2237 std::map
<Node
, Node
> nf_to_eqc
;
2238 std::map
<Node
, Node
> eqc_to_nf
;
2239 std::map
<Node
, Node
> eqc_to_exp
;
2240 for (const Node
& eqc
: d_strings_eqc
)
2242 Trace("strings-process-debug") << "- Verify normal forms are the same for "
2243 << eqc
<< std::endl
;
2244 normalizeEquivalenceClass(eqc
);
2245 Trace("strings-debug") << "Finished normalizing eqc..." << std::endl
;
2250 Node nf_term
= mkConcat(d_normal_forms
[eqc
]);
2251 std::map
<Node
, Node
>::iterator itn
= nf_to_eqc
.find(nf_term
);
2252 if (itn
!= nf_to_eqc
.end())
2254 // two equivalence classes have same normal form, merge
2255 std::vector
<Node
> nf_exp
;
2256 nf_exp
.push_back(mkAnd(d_normal_forms_exp
[eqc
]));
2257 nf_exp
.push_back(eqc_to_exp
[itn
->second
]);
2259 d_normal_forms_base
[eqc
].eqNode(d_normal_forms_base
[itn
->second
]);
2260 sendInference(nf_exp
, eq
, "Normal_Form");
2261 if( hasProcessed() ){
2267 nf_to_eqc
[nf_term
] = eqc
;
2268 eqc_to_nf
[eqc
] = nf_term
;
2269 eqc_to_exp
[eqc
] = mkAnd(d_normal_forms_exp
[eqc
]);
2271 Trace("strings-process-debug")
2272 << "Done verifying normal forms are the same for " << eqc
<< std::endl
;
2274 if (Trace
.isOn("strings-nf"))
2276 Trace("strings-nf") << "**** Normal forms are : " << std::endl
;
2277 for (std::map
<Node
, Node
>::iterator it
= eqc_to_exp
.begin();
2278 it
!= eqc_to_exp
.end();
2281 Trace("strings-nf") << " N[" << it
->first
<< "] (base "
2282 << d_normal_forms_base
[it
->first
]
2283 << ") = " << eqc_to_nf
[it
->first
] << std::endl
;
2284 Trace("strings-nf") << " exp: " << it
->second
<< std::endl
;
2286 Trace("strings-nf") << std::endl
;
2290 void TheoryStrings::checkCodes()
2292 // ensure that lemmas regarding str.code been added for each constant string
2296 NodeManager
* nm
= NodeManager::currentNM();
2297 // str.code applied to the code term for each equivalence class that has a
2298 // code term but is not a constant
2299 std::vector
<Node
> nconst_codes
;
2300 // str.code applied to the proxy variables for each equivalence classes that
2301 // are constants of size one
2302 std::vector
<Node
> const_codes
;
2303 for (const Node
& eqc
: d_strings_eqc
)
2305 if (d_normal_forms
[eqc
].size() == 1 && d_normal_forms
[eqc
][0].isConst())
2307 Node c
= d_normal_forms
[eqc
][0];
2308 Trace("strings-code-debug") << "Get proxy variable for " << c
2310 Node cc
= nm
->mkNode(kind::STRING_CODE
, c
);
2311 cc
= Rewriter::rewrite(cc
);
2312 Assert(cc
.isConst());
2313 NodeNodeMap::const_iterator it
= d_proxy_var
.find(c
);
2314 AlwaysAssert(it
!= d_proxy_var
.end());
2315 Node vc
= nm
->mkNode(kind::STRING_CODE
, (*it
).second
);
2316 if (!areEqual(cc
, vc
))
2318 sendInference(d_empty_vec
, cc
.eqNode(vc
), "Code_Proxy");
2320 const_codes
.push_back(vc
);
2324 EqcInfo
* ei
= getOrMakeEqcInfo(eqc
, false);
2325 if (ei
&& !ei
->d_code_term
.get().isNull())
2327 Node vc
= nm
->mkNode(kind::STRING_CODE
, ei
->d_code_term
.get());
2328 nconst_codes
.push_back(vc
);
2336 // now, ensure that str.code is injective
2337 std::vector
<Node
> cmps
;
2338 cmps
.insert(cmps
.end(), const_codes
.rbegin(), const_codes
.rend());
2339 cmps
.insert(cmps
.end(), nconst_codes
.rbegin(), nconst_codes
.rend());
2340 for (unsigned i
= 0, num_ncc
= nconst_codes
.size(); i
< num_ncc
; i
++)
2342 Node c1
= nconst_codes
[i
];
2344 for (const Node
& c2
: cmps
)
2346 Trace("strings-code-debug")
2347 << "Compare codes : " << c1
<< " " << c2
<< std::endl
;
2348 if (!areDisequal(c1
, c2
) && !areEqual(c1
, d_neg_one
))
2350 Node eq_no
= c1
.eqNode(d_neg_one
);
2351 Node deq
= c1
.eqNode(c2
).negate();
2352 Node eqn
= c1
[0].eqNode(c2
[0]);
2353 // str.code(x)==-1 V str.code(x)!=str.code(y) V x==y
2354 Node inj_lem
= nm
->mkNode(kind::OR
, eq_no
, deq
, eqn
);
2355 sendInference(d_empty_vec
, inj_lem
, "Code_Inj");
2362 //compute d_normal_forms_(base,exp,exp_depend)[eqc]
2363 void TheoryStrings::normalizeEquivalenceClass( Node eqc
) {
2364 Trace("strings-process-debug") << "Process equivalence class " << eqc
<< std::endl
;
2365 if( areEqual( eqc
, d_emptyString
) ) {
2366 #ifdef CVC4_ASSERTIONS
2367 for( unsigned j
=0; j
<d_eqc
[eqc
].size(); j
++ ){
2368 Node n
= d_eqc
[eqc
][j
];
2369 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
2370 Assert( areEqual( n
[i
], d_emptyString
) );
2375 Trace("strings-process-debug") << "Return process equivalence class " << eqc
<< " : empty." << std::endl
;
2376 d_normal_forms_base
[eqc
] = d_emptyString
;
2377 d_normal_forms
[eqc
].clear();
2378 d_normal_forms_exp
[eqc
].clear();
2380 Assert( d_normal_forms
.find(eqc
)==d_normal_forms
.end() );
2381 //phi => t = s1 * ... * sn
2382 // normal form for each non-variable term in this eqc (s1...sn)
2383 std::vector
< std::vector
< Node
> > normal_forms
;
2384 // explanation for each normal form (phi)
2385 std::vector
< std::vector
< Node
> > normal_forms_exp
;
2386 // dependency information
2387 std::vector
< std::map
< Node
, std::map
< bool, int > > > normal_forms_exp_depend
;
2388 // record terms for each normal form (t)
2389 std::vector
< Node
> normal_form_src
;
2391 getNormalForms(eqc
, normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
);
2392 if( hasProcessed() ){
2395 // process the normal forms
2396 processNEqc( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
);
2397 if( hasProcessed() ){
2400 //debugPrintNormalForms( "strings-solve", eqc, normal_forms, normal_form_src, normal_forms_exp, normal_forms_exp_depend );
2402 //construct the normal form
2403 Assert( !normal_forms
.empty() );
2406 std::vector
< Node
>::iterator itn
= std::find( normal_form_src
.begin(), normal_form_src
.end(), eqc
);
2407 if( itn
!=normal_form_src
.end() ){
2408 nf_index
= itn
- normal_form_src
.begin();
2409 Trace("strings-solve-debug2") << "take normal form " << nf_index
<< std::endl
;
2410 Assert( normal_form_src
[nf_index
]==eqc
);
2412 //just take the first normal form
2413 Trace("strings-solve-debug2") << "take the first normal form" << std::endl
;
2415 d_normal_forms
[eqc
].insert( d_normal_forms
[eqc
].end(), normal_forms
[nf_index
].begin(), normal_forms
[nf_index
].end() );
2416 d_normal_forms_exp
[eqc
].insert( d_normal_forms_exp
[eqc
].end(), normal_forms_exp
[nf_index
].begin(), normal_forms_exp
[nf_index
].end() );
2417 Trace("strings-solve-debug2") << "take normal form ... done" << std::endl
;
2418 d_normal_forms_base
[eqc
] = normal_form_src
[nf_index
];
2419 //track dependencies
2420 for( unsigned i
=0; i
<normal_forms_exp
[nf_index
].size(); i
++ ){
2421 Node exp
= normal_forms_exp
[nf_index
][i
];
2422 for( unsigned r
=0; r
<2; r
++ ){
2423 d_normal_forms_exp_depend
[eqc
][exp
][r
==0] = normal_forms_exp_depend
[nf_index
][exp
][r
==0];
2426 Trace("strings-process-debug") << "Return process equivalence class " << eqc
<< " : returned, size = " << d_normal_forms
[eqc
].size() << std::endl
;
2430 void trackNfExpDependency( std::vector
< Node
>& nf_exp_n
, std::map
< Node
, std::map
< bool, int > >& nf_exp_depend_n
, Node exp
, int new_val
, int new_rev_val
){
2431 if( std::find( nf_exp_n
.begin(), nf_exp_n
.end(), exp
)==nf_exp_n
.end() ){
2432 nf_exp_n
.push_back( exp
);
2434 for( unsigned k
=0; k
<2; k
++ ){
2435 int val
= k
==0 ? new_val
: new_rev_val
;
2436 std::map
< bool, int >::iterator itned
= nf_exp_depend_n
[exp
].find( k
==1 );
2437 if( itned
==nf_exp_depend_n
[exp
].end() ){
2438 Trace("strings-process-debug") << "Deps : set dependency on " << exp
<< " to " << val
<< " isRev=" << (k
==0) << std::endl
;
2439 nf_exp_depend_n
[exp
][k
==1] = val
;
2441 Trace("strings-process-debug") << "Deps : Multiple dependencies on " << exp
<< " : " << itned
->second
<< " " << val
<< " isRev=" << (k
==0) << std::endl
;
2442 //if we already have a dependency (in the case of non-linear string equalities), it is min/max
2443 bool cmp
= val
> itned
->second
;
2445 nf_exp_depend_n
[exp
][k
==1] = val
;
2451 void TheoryStrings::getNormalForms( Node
&eqc
, std::vector
< std::vector
< Node
> > &normal_forms
, std::vector
< Node
> &normal_form_src
,
2452 std::vector
< std::vector
< Node
> > &normal_forms_exp
, std::vector
< std::map
< Node
, std::map
< bool, int > > >& normal_forms_exp_depend
) {
2453 //constant for equivalence class
2454 Node eqc_non_c
= eqc
;
2455 Trace("strings-process-debug") << "Get normal forms " << eqc
<< std::endl
;
2456 eq::EqClassIterator eqc_i
= eq::EqClassIterator( eqc
, &d_equalityEngine
);
2457 while( !eqc_i
.isFinished() ){
2459 if( d_congruent
.find( n
)==d_congruent
.end() ){
2460 if( n
.getKind() == kind::CONST_STRING
|| n
.getKind() == kind::STRING_CONCAT
){
2461 Trace("strings-process-debug") << "Get Normal Form : Process term " << n
<< " in eqc " << eqc
<< std::endl
;
2462 std::vector
< Node
> nf_n
;
2463 std::vector
< Node
> nf_exp_n
;
2464 std::map
< Node
, std::map
< bool, int > > nf_exp_depend_n
;
2465 if( n
.getKind()==kind::CONST_STRING
){
2466 if( n
!=d_emptyString
) {
2467 nf_n
.push_back( n
);
2469 }else if( n
.getKind()==kind::STRING_CONCAT
){
2470 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ) {
2471 Node nr
= d_equalityEngine
.getRepresentative( n
[i
] );
2472 Trace("strings-process-debug") << "Normalizing subterm " << n
[i
] << " = " << nr
<< std::endl
;
2473 Assert( d_normal_forms
.find( nr
)!=d_normal_forms
.end() );
2474 unsigned orig_size
= nf_n
.size();
2475 unsigned add_size
= d_normal_forms
[nr
].size();
2476 //if not the empty string, add to current normal form
2477 if( !d_normal_forms
[nr
].empty() ){
2478 for( unsigned r
=0; r
<d_normal_forms
[nr
].size(); r
++ ) {
2479 if( Trace
.isOn("strings-error") ) {
2480 if( d_normal_forms
[nr
][r
].getKind()==kind::STRING_CONCAT
){
2481 Trace("strings-error") << "Strings::Error: From eqc = " << eqc
<< ", " << n
<< " index " << i
<< ", bad normal form : ";
2482 for( unsigned rr
=0; rr
<d_normal_forms
[nr
].size(); rr
++ ) {
2483 Trace("strings-error") << d_normal_forms
[nr
][rr
] << " ";
2485 Trace("strings-error") << std::endl
;
2488 Assert( d_normal_forms
[nr
][r
].getKind()!=kind::STRING_CONCAT
);
2490 nf_n
.insert( nf_n
.end(), d_normal_forms
[nr
].begin(), d_normal_forms
[nr
].end() );
2493 for( unsigned j
=0; j
<d_normal_forms_exp
[nr
].size(); j
++ ){
2494 Node exp
= d_normal_forms_exp
[nr
][j
];
2496 trackNfExpDependency( nf_exp_n
, nf_exp_depend_n
, exp
,
2497 orig_size
+ d_normal_forms_exp_depend
[nr
][exp
][false],
2498 orig_size
+ ( add_size
- d_normal_forms_exp_depend
[nr
][exp
][true] ) );
2500 if( d_normal_forms_base
[nr
]!=n
[i
] ){
2501 Assert( d_normal_forms_base
.find( nr
)!=d_normal_forms_base
.end() );
2502 Node eq
= n
[i
].eqNode( d_normal_forms_base
[nr
] );
2503 //track depends : entire current segment is dependent upon base equality
2504 trackNfExpDependency( nf_exp_n
, nf_exp_depend_n
, eq
, orig_size
, orig_size
+ add_size
);
2507 //convert forward indices to reverse indices
2508 int total_size
= nf_n
.size();
2509 for( std::map
< Node
, std::map
< bool, int > >::iterator it
= nf_exp_depend_n
.begin(); it
!= nf_exp_depend_n
.end(); ++it
){
2510 it
->second
[true] = total_size
- it
->second
[true];
2511 Assert( it
->second
[true]>=0 );
2514 //if not equal to self
2515 if( nf_n
.size()>1 || ( nf_n
.size()==1 && nf_n
[0].getKind()==kind::CONST_STRING
) ){
2516 if( nf_n
.size()>1 ) {
2517 for( unsigned i
=0; i
<nf_n
.size(); i
++ ){
2518 if( Trace
.isOn("strings-error") ){
2519 Trace("strings-error") << "Cycle for normal form ";
2520 printConcat(nf_n
,"strings-error");
2521 Trace("strings-error") << "..." << nf_n
[i
] << std::endl
;
2523 Assert( !areEqual( nf_n
[i
], n
) );
2526 normal_forms
.push_back(nf_n
);
2527 normal_form_src
.push_back(n
);
2528 normal_forms_exp
.push_back(nf_exp_n
);
2529 normal_forms_exp_depend
.push_back(nf_exp_depend_n
);
2531 //this was redundant: combination of self + empty string(s)
2532 Node nn
= nf_n
.size()==0 ? d_emptyString
: nf_n
[0];
2533 Assert( areEqual( nn
, eqc
) );
2542 if( normal_forms
.empty() ) {
2543 Trace("strings-solve-debug2") << "construct the normal form" << std::endl
;
2544 //do not choose a concat here use "eqc_non_c" (in this case they have non-trivial explanation why they normalize to self)
2545 std::vector
< Node
> eqc_non_c_nf
;
2546 getConcatVec( eqc_non_c
, eqc_non_c_nf
);
2547 normal_forms
.push_back( eqc_non_c_nf
);
2548 normal_form_src
.push_back( eqc_non_c
);
2549 normal_forms_exp
.push_back( std::vector
< Node
>() );
2550 normal_forms_exp_depend
.push_back( std::map
< Node
, std::map
< bool, int > >() );
2552 if(Trace
.isOn("strings-solve")) {
2553 Trace("strings-solve") << "--- Normal forms for equivalance class " << eqc
<< " : " << std::endl
;
2554 for( unsigned i
=0; i
<normal_forms
.size(); i
++ ) {
2555 Trace("strings-solve") << "#" << i
<< " (from " << normal_form_src
[i
] << ") : ";
2556 for( unsigned j
=0; j
<normal_forms
[i
].size(); j
++ ) {
2558 Trace("strings-solve") << ", ";
2560 Trace("strings-solve") << normal_forms
[i
][j
];
2562 Trace("strings-solve") << std::endl
;
2563 Trace("strings-solve") << " Explanation is : ";
2564 if(normal_forms_exp
[i
].size() == 0) {
2565 Trace("strings-solve") << "NONE";
2567 for( unsigned j
=0; j
<normal_forms_exp
[i
].size(); j
++ ) {
2569 Trace("strings-solve") << " AND ";
2571 Trace("strings-solve") << normal_forms_exp
[i
][j
];
2573 Trace("strings-solve") << std::endl
;
2574 Trace("strings-solve") << "WITH DEPENDENCIES : " << std::endl
;
2575 for( unsigned j
=0; j
<normal_forms_exp
[i
].size(); j
++ ) {
2576 Trace("strings-solve") << " " << normal_forms_exp
[i
][j
] << " -> ";
2577 Trace("strings-solve") << normal_forms_exp_depend
[i
][normal_forms_exp
[i
][j
]][false] << ",";
2578 Trace("strings-solve") << normal_forms_exp_depend
[i
][normal_forms_exp
[i
][j
]][true] << std::endl
;
2581 Trace("strings-solve") << std::endl
;
2585 Trace("strings-solve") << "--- Single normal form for equivalence class " << eqc
<< std::endl
;
2588 //if equivalence class is constant, approximate as containment, infer conflicts
2589 Node c
= getConstantEqc( eqc
);
2591 Trace("strings-solve") << "Eqc is constant " << c
<< std::endl
;
2592 for( unsigned i
=0; i
<normal_forms
.size(); i
++ ) {
2594 if( !TheoryStringsRewriter::canConstantContainList( c
, normal_forms
[i
], firstc
, lastc
) ){
2595 Node n
= normal_form_src
[i
];
2597 Trace("strings-solve") << "Normal form for " << n
<< " cannot be contained in constant " << c
<< std::endl
;
2598 //conflict, explanation is n = base ^ base = c ^ relevant porition of ( n = N[n] )
2599 std::vector
< Node
> exp
;
2600 Assert( d_eqc_to_const_base
.find( eqc
)!=d_eqc_to_const_base
.end() );
2601 addToExplanation( n
, d_eqc_to_const_base
[eqc
], exp
);
2602 Assert( d_eqc_to_const_exp
.find( eqc
)!=d_eqc_to_const_exp
.end() );
2603 if( !d_eqc_to_const_exp
[eqc
].isNull() ){
2604 exp
.push_back( d_eqc_to_const_exp
[eqc
] );
2606 //TODO: this can be minimized based on firstc/lastc, normal_forms_exp_depend
2607 exp
.insert( exp
.end(), normal_forms_exp
[i
].begin(), normal_forms_exp
[i
].end() );
2608 Node conc
= d_false
;
2609 sendInference( exp
, conc
, "N_NCTN" );
2616 void TheoryStrings::getExplanationVectorForPrefix( std::vector
< std::vector
< Node
> > &normal_forms_exp
, std::vector
< std::map
< Node
, std::map
< bool, int > > >& normal_forms_exp_depend
,
2617 unsigned i
, int index
, bool isRev
, std::vector
< Node
>& curr_exp
) {
2618 if( index
==-1 || !options::stringMinPrefixExplain() ){
2619 curr_exp
.insert(curr_exp
.end(), normal_forms_exp
[i
].begin(), normal_forms_exp
[i
].end() );
2621 for( unsigned k
=0; k
<normal_forms_exp
[i
].size(); k
++ ){
2622 Node exp
= normal_forms_exp
[i
][k
];
2623 int dep
= normal_forms_exp_depend
[i
][exp
][isRev
];
2625 curr_exp
.push_back( exp
);
2626 Trace("strings-explain-prefix-debug") << " include : " << exp
<< std::endl
;
2628 Trace("strings-explain-prefix-debug") << " exclude : " << exp
<< std::endl
;
2634 void TheoryStrings::getExplanationVectorForPrefixEq( std::vector
< std::vector
< Node
> > &normal_forms
, std::vector
< Node
> &normal_form_src
,
2635 std::vector
< std::vector
< Node
> > &normal_forms_exp
, std::vector
< std::map
< Node
, std::map
< bool, int > > >& normal_forms_exp_depend
,
2636 unsigned i
, unsigned j
, int index_i
, int index_j
, bool isRev
, std::vector
< Node
>& curr_exp
) {
2637 Trace("strings-explain-prefix") << "Get explanation for prefix " << index_i
<< ", " << index_j
<< " of normal forms " << i
<< " and " << j
<< ", reverse = " << isRev
<< std::endl
;
2638 for( unsigned r
=0; r
<2; r
++ ){
2639 getExplanationVectorForPrefix( normal_forms_exp
, normal_forms_exp_depend
, r
==0 ? i
: j
, r
==0 ? index_i
: index_j
, isRev
, curr_exp
);
2641 Trace("strings-explain-prefix") << "Included " << curr_exp
.size() << " / " << ( normal_forms_exp
[i
].size() + normal_forms_exp
[j
].size() ) << std::endl
;
2642 addToExplanation( normal_form_src
[i
], normal_form_src
[j
], curr_exp
);
2646 void TheoryStrings::processNEqc( std::vector
< std::vector
< Node
> > &normal_forms
, std::vector
< Node
> &normal_form_src
,
2647 std::vector
< std::vector
< Node
> > &normal_forms_exp
, std::vector
< std::map
< Node
, std::map
< bool, int > > >& normal_forms_exp_depend
){
2648 //the possible inferences
2649 std::vector
< InferInfo
> pinfer
;
2650 // loop over all pairs
2651 for(unsigned i
=0; i
<normal_forms
.size()-1; i
++) {
2652 //unify each normalform[j] with normal_forms[i]
2653 for(unsigned j
=i
+1; j
<normal_forms
.size(); j
++ ) {
2654 //ensure that normal_forms[i] and normal_forms[j] are the same modulo equality, add to pinfer if not
2655 Trace("strings-solve") << "Strings: Process normal form #" << i
<< " against #" << j
<< "..." << std::endl
;
2656 if( isNormalFormPair( normal_form_src
[i
], normal_form_src
[j
] ) ) {
2657 Trace("strings-solve") << "Strings: Already cached." << std::endl
;
2659 //process the reverse direction first (check for easy conflicts and inferences)
2660 unsigned rindex
= 0;
2661 processReverseNEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, rindex
, 0, pinfer
);
2662 if( hasProcessed() ){
2664 }else if( !pinfer
.empty() && pinfer
.back().d_id
==1 ){
2667 //AJR: for less aggressive endpoint inference
2671 processSimpleNEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, index
, false, rindex
, pinfer
);
2672 if( hasProcessed() ){
2674 }else if( !pinfer
.empty() && pinfer
.back().d_id
==1 ){
2684 // now, determine which of the possible inferences we want to add
2685 unsigned use_index
= 0;
2686 bool set_use_index
= false;
2687 Trace("strings-solve") << "Possible inferences (" << pinfer
.size()
2688 << ") : " << std::endl
;
2689 unsigned min_id
= 9;
2690 unsigned max_index
= 0;
2691 for (unsigned i
= 0, size
= pinfer
.size(); i
< size
; i
++)
2693 Trace("strings-solve") << "From " << pinfer
[i
].d_i
<< " / " << pinfer
[i
].d_j
2694 << " (rev=" << pinfer
[i
].d_rev
<< ") : ";
2695 Trace("strings-solve") << pinfer
[i
].d_conc
<< " by " << pinfer
[i
].d_id
2697 if (!set_use_index
|| pinfer
[i
].d_id
< min_id
2698 || (pinfer
[i
].d_id
== min_id
&& pinfer
[i
].d_index
> max_index
))
2700 min_id
= pinfer
[i
].d_id
;
2701 max_index
= pinfer
[i
].d_index
;
2703 set_use_index
= true;
2706 // send the inference
2707 if (!pinfer
[use_index
].d_nf_pair
[0].isNull())
2709 Assert(!pinfer
[use_index
].d_nf_pair
[1].isNull());
2710 addNormalFormPair(pinfer
[use_index
].d_nf_pair
[0],
2711 pinfer
[use_index
].d_nf_pair
[1]);
2713 std::stringstream ssi
;
2714 ssi
<< pinfer
[use_index
].d_id
;
2715 sendInference(pinfer
[use_index
].d_ant
,
2716 pinfer
[use_index
].d_antn
,
2717 pinfer
[use_index
].d_conc
,
2719 pinfer
[use_index
].sendAsLemma());
2720 // Register the new skolems from this inference. We register them here
2721 // (lazily), since the code above has now decided to use the inference
2722 // at use_index that involves them.
2723 for (const std::pair
<const LengthStatus
, std::vector
<Node
> >& sks
:
2724 pinfer
[use_index
].d_new_skolem
)
2726 for (const Node
& n
: sks
.second
)
2728 registerLength(n
, sks
.first
);
2733 bool TheoryStrings::InferInfo::sendAsLemma() {
2737 void TheoryStrings::processReverseNEq( std::vector
< std::vector
< Node
> > &normal_forms
, std::vector
< Node
> &normal_form_src
,
2738 std::vector
< std::vector
< Node
> > &normal_forms_exp
, std::vector
< std::map
< Node
, std::map
< bool, int > > >& normal_forms_exp_depend
,
2739 unsigned i
, unsigned j
, unsigned& index
, unsigned rproc
, std::vector
< InferInfo
>& pinfer
) {
2740 //reverse normal form of i, j
2741 std::reverse( normal_forms
[i
].begin(), normal_forms
[i
].end() );
2742 std::reverse( normal_forms
[j
].begin(), normal_forms
[j
].end() );
2744 processSimpleNEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, index
, true, rproc
, pinfer
);
2746 //reverse normal form of i, j
2747 std::reverse( normal_forms
[i
].begin(), normal_forms
[i
].end() );
2748 std::reverse( normal_forms
[j
].begin(), normal_forms
[j
].end() );
2751 //rproc is the # is the size of suffix that is identical
2752 void TheoryStrings::processSimpleNEq( std::vector
< std::vector
< Node
> > &normal_forms
, std::vector
< Node
> &normal_form_src
,
2753 std::vector
< std::vector
< Node
> > &normal_forms_exp
, std::vector
< std::map
< Node
, std::map
< bool, int > > >& normal_forms_exp_depend
,
2754 unsigned i
, unsigned j
, unsigned& index
, bool isRev
, unsigned rproc
, std::vector
< InferInfo
>& pinfer
) {
2755 Assert( rproc
<=normal_forms
[i
].size() && rproc
<=normal_forms
[j
].size() );
2759 //if we are at the end
2760 if( index
==(normal_forms
[i
].size()-rproc
) || index
==(normal_forms
[j
].size()-rproc
) ){
2761 if( index
==(normal_forms
[i
].size()-rproc
) && index
==(normal_forms
[j
].size()-rproc
) ){
2764 //the remainder must be empty
2765 unsigned k
= index
==(normal_forms
[i
].size()-rproc
) ? j
: i
;
2766 unsigned index_k
= index
;
2767 //Node eq_exp = mkAnd( curr_exp );
2768 std::vector
< Node
> curr_exp
;
2769 getExplanationVectorForPrefixEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, -1, -1, isRev
, curr_exp
);
2770 while( !d_conflict
&& index_k
<(normal_forms
[k
].size()-rproc
) ){
2771 //can infer that this string must be empty
2772 Node eq
= normal_forms
[k
][index_k
].eqNode( d_emptyString
);
2773 //Trace("strings-lemma") << "Strings: Infer " << eq << " from " << eq_exp << std::endl;
2774 Assert( !areEqual( d_emptyString
, normal_forms
[k
][index_k
] ) );
2775 sendInference( curr_exp
, eq
, "N_EndpointEmp" );
2780 Trace("strings-solve-debug") << "Process " << normal_forms
[i
][index
] << " ... " << normal_forms
[j
][index
] << std::endl
;
2781 if( normal_forms
[i
][index
]==normal_forms
[j
][index
] ){
2782 Trace("strings-solve-debug") << "Simple Case 1 : strings are equal" << std::endl
;
2786 Assert( !areEqual(normal_forms
[i
][index
], normal_forms
[j
][index
]) );
2787 std::vector
< Node
> temp_exp
;
2788 Node length_term_i
= getLength( normal_forms
[i
][index
], temp_exp
);
2789 Node length_term_j
= getLength( normal_forms
[j
][index
], temp_exp
);
2790 //check length(normal_forms[i][index]) == length(normal_forms[j][index])
2791 if( areEqual( length_term_i
, length_term_j
) ){
2792 Trace("strings-solve-debug") << "Simple Case 2 : string lengths are equal" << std::endl
;
2793 Node eq
= normal_forms
[i
][index
].eqNode( normal_forms
[j
][index
] );
2794 //eq = Rewriter::rewrite( eq );
2795 Node length_eq
= length_term_i
.eqNode( length_term_j
);
2796 //temp_exp.insert(temp_exp.end(), curr_exp.begin(), curr_exp.end() );
2797 getExplanationVectorForPrefixEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, index
, index
, isRev
, temp_exp
);
2798 temp_exp
.push_back(length_eq
);
2799 sendInference( temp_exp
, eq
, "N_Unify" );
2801 }else if( ( normal_forms
[i
][index
].getKind()!=kind::CONST_STRING
&& index
==normal_forms
[i
].size()-rproc
-1 ) ||
2802 ( normal_forms
[j
][index
].getKind()!=kind::CONST_STRING
&& index
==normal_forms
[j
].size()-rproc
-1 ) ){
2803 Trace("strings-solve-debug") << "Simple Case 3 : at endpoint" << std::endl
;
2804 std::vector
< Node
> antec
;
2805 //antec.insert(antec.end(), curr_exp.begin(), curr_exp.end() );
2806 getExplanationVectorForPrefixEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, -1, -1, isRev
, antec
);
2807 std::vector
< Node
> eqn
;
2808 for( unsigned r
=0; r
<2; r
++ ) {
2809 int index_k
= index
;
2810 int k
= r
==0 ? i
: j
;
2811 std::vector
< Node
> eqnc
;
2812 for( unsigned index_l
=index_k
; index_l
<(normal_forms
[k
].size()-rproc
); index_l
++ ) {
2814 eqnc
.insert(eqnc
.begin(), normal_forms
[k
][index_l
] );
2816 eqnc
.push_back( normal_forms
[k
][index_l
] );
2819 eqn
.push_back( mkConcat( eqnc
) );
2821 if( !areEqual( eqn
[0], eqn
[1] ) ){
2822 sendInference( antec
, eqn
[0].eqNode( eqn
[1] ), "N_EndpointEq", true );
2825 Assert( normal_forms
[i
].size()==normal_forms
[j
].size() );
2826 index
= normal_forms
[i
].size()-rproc
;
2828 }else if( normal_forms
[i
][index
].isConst() && normal_forms
[j
][index
].isConst() ){
2829 Node const_str
= normal_forms
[i
][index
];
2830 Node other_str
= normal_forms
[j
][index
];
2831 Trace("strings-solve-debug") << "Simple Case 3 : Const Split : " << const_str
<< " vs " << other_str
<< " at index " << index
<< ", isRev = " << isRev
<< std::endl
;
2832 unsigned len_short
= const_str
.getConst
<String
>().size() <= other_str
.getConst
<String
>().size() ? const_str
.getConst
<String
>().size() : other_str
.getConst
<String
>().size();
2833 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
);
2835 //same prefix/suffix
2836 //k is the index of the string that is shorter
2837 int k
= const_str
.getConst
<String
>().size()<other_str
.getConst
<String
>().size() ? i
: j
;
2838 int l
= const_str
.getConst
<String
>().size()<other_str
.getConst
<String
>().size() ? j
: i
;
2839 //update the nf exp dependencies
2840 //notice this is not critical for soundness: not doing the below incrementing will only lead to overapproximating when antecedants are required in explanations
2841 for( std::map
< Node
, std::map
< bool, int > >::iterator itnd
= normal_forms_exp_depend
[l
].begin(); itnd
!= normal_forms_exp_depend
[l
].end(); ++itnd
){
2842 for( std::map
< bool, int >::iterator itnd2
= itnd
->second
.begin(); itnd2
!= itnd
->second
.end(); ++itnd2
){
2843 //see if this can be incremented: it can if it is not relevant to the current index
2844 Assert( itnd2
->second
>=0 && itnd2
->second
<=(int)normal_forms
[l
].size() );
2845 bool increment
= (itnd2
->first
==isRev
) ? itnd2
->second
>(int)index
: ( (int)normal_forms
[l
].size()-1-itnd2
->second
)<(int)index
;
2847 normal_forms_exp_depend
[l
][itnd
->first
][itnd2
->first
] = itnd2
->second
+ 1;
2852 int new_len
= normal_forms
[l
][index
].getConst
<String
>().size() - len_short
;
2853 Node remainderStr
= NodeManager::currentNM()->mkConst( normal_forms
[l
][index
].getConst
<String
>().substr(0, new_len
) );
2854 Trace("strings-solve-debug-test") << "Break normal form of " << normal_forms
[l
][index
] << " into " << normal_forms
[k
][index
] << ", " << remainderStr
<< std::endl
;
2855 normal_forms
[l
].insert( normal_forms
[l
].begin()+index
+ 1, remainderStr
);
2857 Node remainderStr
= NodeManager::currentNM()->mkConst(normal_forms
[l
][index
].getConst
<String
>().substr(len_short
));
2858 Trace("strings-solve-debug-test") << "Break normal form of " << normal_forms
[l
][index
] << " into " << normal_forms
[k
][index
] << ", " << remainderStr
<< std::endl
;
2859 normal_forms
[l
].insert( normal_forms
[l
].begin()+index
+ 1, remainderStr
);
2861 normal_forms
[l
][index
] = normal_forms
[k
][index
];
2866 std::vector
< Node
> antec
;
2867 getExplanationVectorForPrefixEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, index
, index
, isRev
, antec
);
2868 sendInference( antec
, d_false
, "N_Const", true );
2872 //construct the candidate inference "info"
2874 info
.d_index
= index
;
2879 bool info_valid
= false;
2880 Assert( index
<normal_forms
[i
].size()-rproc
&& index
<normal_forms
[j
].size()-rproc
);
2881 std::vector
< Node
> lexp
;
2882 Node length_term_i
= getLength( normal_forms
[i
][index
], lexp
);
2883 Node length_term_j
= getLength( normal_forms
[j
][index
], lexp
);
2884 //split on equality between string lengths (note that splitting on equality between strings is worse since it is harder to process)
2885 if( !areDisequal( length_term_i
, length_term_j
) && !areEqual( length_term_i
, length_term_j
) &&
2886 normal_forms
[i
][index
].getKind()!=kind::CONST_STRING
&& normal_forms
[j
][index
].getKind()!=kind::CONST_STRING
){ //AJR: remove the latter 2 conditions?
2887 Trace("strings-solve-debug") << "Non-simple Case 1 : string lengths neither equal nor disequal" << std::endl
;
2888 //try to make the lengths equal via splitting on demand
2889 Node length_eq
= NodeManager::currentNM()->mkNode( kind::EQUAL
, length_term_i
, length_term_j
);
2890 length_eq
= Rewriter::rewrite( length_eq
);
2892 info
.d_conc
= NodeManager::currentNM()->mkNode( kind::OR
, length_eq
, length_eq
.negate() );
2893 info
.d_pending_phase
[ length_eq
] = true;
2894 info
.d_id
= INFER_LEN_SPLIT
;
2897 Trace("strings-solve-debug") << "Non-simple Case 2 : must compare strings" << std::endl
;
2900 if( detectLoop( normal_forms
, i
, j
, index
, loop_in_i
, loop_in_j
, rproc
) ){
2901 if( !isRev
){ //FIXME
2902 getExplanationVectorForPrefixEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, -1, -1, isRev
, info
.d_ant
);
2904 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
) ){
2909 //AJR: length entailment here?
2910 if( normal_forms
[i
][index
].getKind() == kind::CONST_STRING
|| normal_forms
[j
][index
].getKind() == kind::CONST_STRING
){
2911 unsigned const_k
= normal_forms
[i
][index
].getKind() == kind::CONST_STRING
? i
: j
;
2912 unsigned nconst_k
= normal_forms
[i
][index
].getKind() == kind::CONST_STRING
? j
: i
;
2913 Node other_str
= normal_forms
[nconst_k
][index
];
2914 Assert( other_str
.getKind()!=kind::CONST_STRING
, "Other string is not constant." );
2915 Assert( other_str
.getKind()!=kind::STRING_CONCAT
, "Other string is not CONCAT." );
2916 if( !d_equalityEngine
.areDisequal( other_str
, d_emptyString
, true ) ){
2917 Node eq
= other_str
.eqNode( d_emptyString
);
2919 info
.d_conc
= NodeManager::currentNM()->mkNode( kind::OR
, eq
, eq
.negate() );
2920 info
.d_id
= INFER_LEN_SPLIT_EMP
;
2923 if( !isRev
){ //FIXME
2924 Node xnz
= other_str
.eqNode( d_emptyString
).negate();
2925 unsigned index_nc_k
= index
+1;
2926 //Node next_const_str = TheoryStringsRewriter::collectConstantStringAt( normal_forms[nconst_k], index_nc_k, false );
2927 unsigned start_index_nc_k
= index
+1;
2928 Node next_const_str
= TheoryStringsRewriter::getNextConstantAt( normal_forms
[nconst_k
], start_index_nc_k
, index_nc_k
, false );
2929 if( !next_const_str
.isNull() ) {
2930 unsigned index_c_k
= index
;
2931 Node const_str
= TheoryStringsRewriter::collectConstantStringAt( normal_forms
[const_k
], index_c_k
, false );
2932 Assert( !const_str
.isNull() );
2933 CVC4::String stra
= const_str
.getConst
<String
>();
2934 CVC4::String strb
= next_const_str
.getConst
<String
>();
2935 //since non-empty, we start with charecter #1
2938 CVC4::String stra1
= stra
.prefix( stra
.size()-1 );
2939 p
= stra
.size() - stra1
.roverlap(strb
);
2940 Trace("strings-csp-debug") << "Compute roverlap : " << const_str
<< " " << next_const_str
<< std::endl
;
2941 size_t p2
= stra1
.rfind(strb
);
2942 p
= p2
==std::string::npos
? p
: ( p
>p2
+1? p2
+1 : p
);
2943 Trace("strings-csp-debug") << "overlap : " << stra1
<< " " << strb
<< " returned " << p
<< " " << p2
<< " " << (p2
==std::string::npos
) << std::endl
;
2945 CVC4::String stra1
= stra
.substr( 1 );
2946 p
= stra
.size() - stra1
.overlap(strb
);
2947 Trace("strings-csp-debug") << "Compute overlap : " << const_str
<< " " << next_const_str
<< std::endl
;
2948 size_t p2
= stra1
.find(strb
);
2949 p
= p2
==std::string::npos
? p
: ( p
>p2
+1? p2
+1 : p
);
2950 Trace("strings-csp-debug") << "overlap : " << stra1
<< " " << strb
<< " returned " << p
<< " " << p2
<< " " << (p2
==std::string::npos
) << std::endl
;
2953 if( start_index_nc_k
==index
+1 ){
2954 info
.d_ant
.push_back( xnz
);
2955 getExplanationVectorForPrefixEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
,
2956 const_k
, nconst_k
, index_c_k
, index_nc_k
, isRev
, info
.d_ant
);
2957 Node prea
= p
==stra
.size() ? const_str
: NodeManager::currentNM()->mkConst( isRev
? stra
.suffix( p
) : stra
.prefix( p
) );
2958 Node sk
= d_sk_cache
.mkSkolemCached(
2961 isRev
? SkolemCache::SK_ID_C_SPT_REV
2962 : SkolemCache::SK_ID_C_SPT
,
2964 Trace("strings-csp") << "Const Split: " << prea
<< " is removed from " << stra
<< " due to " << strb
<< ", p=" << p
<< std::endl
;
2966 info
.d_conc
= other_str
.eqNode( isRev
? mkConcat( sk
, prea
) : mkConcat(prea
, sk
) );
2967 info
.d_new_skolem
[LENGTH_SPLIT
].push_back(sk
);
2968 info
.d_id
= INFER_SSPLIT_CST_PROP
;
2971 /* FIXME for isRev, speculative
2972 else if( options::stringLenPropCsp() ){
2973 //propagate length constraint
2974 std::vector< Node > cc;
2975 for( unsigned i=index; i<start_index_nc_k; i++ ){
2976 cc.push_back( normal_forms[nconst_k][i] );
2978 Node lt = NodeManager::currentNM()->mkNode( kind::STRING_LENGTH, mkConcat( cc ) );
2979 conc = NodeManager::currentNM()->mkNode( kind::GEQ, lt, NodeManager::currentNM()->mkConst( Rational(p) ) );
2980 sendInference( ant, conc, "S-Split(CSP-P)-lprop", true );
2986 info
.d_ant
.push_back( xnz
);
2987 Node const_str
= normal_forms
[const_k
][index
];
2988 getExplanationVectorForPrefixEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, index
, index
, isRev
, info
.d_ant
);
2989 CVC4::String stra
= const_str
.getConst
<String
>();
2990 if( options::stringBinaryCsp() && stra
.size()>3 ){
2991 //split string in half
2992 Node c_firstHalf
= NodeManager::currentNM()->mkConst( isRev
? stra
.substr( stra
.size()/2 ) : stra
.substr(0, stra
.size()/2 ) );
2993 Node sk
= d_sk_cache
.mkSkolemCached(
2996 isRev
? SkolemCache::SK_ID_VC_BIN_SPT_REV
2997 : SkolemCache::SK_ID_VC_BIN_SPT
,
2999 Trace("strings-csp") << "Const Split: " << c_firstHalf
<< " is removed from " << const_str
<< " (binary) " << std::endl
;
3000 info
.d_conc
= NodeManager::currentNM()->mkNode( kind::OR
, other_str
.eqNode( isRev
? mkConcat( sk
, c_firstHalf
) : mkConcat( c_firstHalf
, sk
) ),
3001 NodeManager::currentNM()->mkNode( kind::AND
,
3002 sk
.eqNode( d_emptyString
).negate(),
3003 c_firstHalf
.eqNode( isRev
? mkConcat( sk
, other_str
) : mkConcat( other_str
, sk
) ) ) );
3004 info
.d_new_skolem
[LENGTH_SPLIT
].push_back(sk
);
3005 info
.d_id
= INFER_SSPLIT_CST_BINARY
;
3009 Node firstChar
= stra
.size() == 1 ? const_str
: NodeManager::currentNM()->mkConst( isRev
? stra
.suffix( 1 ) : stra
.prefix( 1 ) );
3010 Node sk
= d_sk_cache
.mkSkolemCached(
3013 isRev
? SkolemCache::SK_ID_VC_SPT_REV
3014 : SkolemCache::SK_ID_VC_SPT
,
3016 Trace("strings-csp") << "Const Split: " << firstChar
<< " is removed from " << const_str
<< " (serial) " << std::endl
;
3017 info
.d_conc
= other_str
.eqNode( isRev
? mkConcat( sk
, firstChar
) : mkConcat(firstChar
, sk
) );
3018 info
.d_new_skolem
[LENGTH_SPLIT
].push_back(sk
);
3019 info
.d_id
= INFER_SSPLIT_CST
;
3026 int lentTestSuccess
= -1;
3028 if( options::stringCheckEntailLen() ){
3030 for( unsigned e
=0; e
<2; e
++ ){
3031 Node t
= e
==0 ? normal_forms
[i
][index
] : normal_forms
[j
][index
];
3032 //do not infer constants are larger than variables
3033 if( t
.getKind()!=kind::CONST_STRING
){
3034 Node lt1
= e
==0 ? length_term_i
: length_term_j
;
3035 Node lt2
= e
==0 ? length_term_j
: length_term_i
;
3036 Node ent_lit
= Rewriter::rewrite( NodeManager::currentNM()->mkNode( kind::GT
, lt1
, lt2
) );
3037 std::pair
<bool, Node
> et
= d_valuation
.entailmentCheck( THEORY_OF_TYPE_BASED
, ent_lit
);
3039 Trace("strings-entail") << "Strings entailment : " << ent_lit
<< " is entailed in the current context." << std::endl
;
3040 Trace("strings-entail") << " explanation was : " << et
.second
<< std::endl
;
3041 lentTestSuccess
= e
;
3042 lentTestExp
= et
.second
;
3049 getExplanationVectorForPrefixEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, index
, index
, isRev
, info
.d_ant
);
3051 for(unsigned xory
=0; xory
<2; xory
++) {
3052 Node x
= xory
==0 ? normal_forms
[i
][index
] : normal_forms
[j
][index
];
3053 Node xgtz
= x
.eqNode( d_emptyString
).negate();
3054 if( d_equalityEngine
.areDisequal( x
, d_emptyString
, true ) ) {
3055 info
.d_ant
.push_back( xgtz
);
3057 info
.d_antn
.push_back( xgtz
);
3060 Node sk
= d_sk_cache
.mkSkolemCached(
3061 normal_forms
[i
][index
],
3062 normal_forms
[j
][index
],
3063 isRev
? SkolemCache::SK_ID_V_SPT_REV
3064 : SkolemCache::SK_ID_V_SPT
,
3066 // must add length requirement
3067 info
.d_new_skolem
[LENGTH_GEQ_ONE
].push_back(sk
);
3068 Node eq1
= normal_forms
[i
][index
].eqNode( isRev
? mkConcat(sk
, normal_forms
[j
][index
]) : mkConcat(normal_forms
[j
][index
], sk
) );
3069 Node eq2
= normal_forms
[j
][index
].eqNode( isRev
? mkConcat(sk
, normal_forms
[i
][index
]) : mkConcat(normal_forms
[i
][index
], sk
) );
3071 if( lentTestSuccess
!=-1 ){
3072 info
.d_antn
.push_back( lentTestExp
);
3073 info
.d_conc
= lentTestSuccess
==0 ? eq1
: eq2
;
3074 info
.d_id
= INFER_SSPLIT_VAR_PROP
;
3077 Node ldeq
= NodeManager::currentNM()->mkNode( kind::EQUAL
, length_term_i
, length_term_j
).negate();
3078 if( d_equalityEngine
.areDisequal( length_term_i
, length_term_j
, true ) ){
3079 info
.d_ant
.push_back( ldeq
);
3081 info
.d_antn
.push_back(ldeq
);
3084 info
.d_conc
= NodeManager::currentNM()->mkNode( kind::OR
, eq1
, eq2
);
3085 info
.d_id
= INFER_SSPLIT_VAR
;
3092 pinfer
.push_back( info
);
3101 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
){
3102 int has_loop
[2] = { -1, -1 };
3103 if( options::stringLB() != 2 ) {
3104 for( unsigned r
=0; r
<2; r
++ ) {
3105 int n_index
= (r
==0 ? i
: j
);
3106 int other_n_index
= (r
==0 ? j
: i
);
3107 if( normal_forms
[other_n_index
][index
].getKind() != kind::CONST_STRING
) {
3108 for( unsigned lp
= index
+1; lp
<normal_forms
[n_index
].size()-rproc
; lp
++ ){
3109 if( normal_forms
[n_index
][lp
]==normal_forms
[other_n_index
][index
] ){
3117 if( has_loop
[0]!=-1 || has_loop
[1]!=-1 ) {
3118 loop_in_i
= has_loop
[0];
3119 loop_in_j
= has_loop
[1];
3122 Trace("strings-solve-debug") << "No loops detected." << std::endl
;
3128 bool TheoryStrings::processLoop( std::vector
< std::vector
< Node
> > &normal_forms
, std::vector
< Node
> &normal_form_src
,
3129 int i
, int j
, int loop_n_index
, int other_n_index
, int loop_index
, int index
, InferInfo
& info
){
3130 if( options::stringAbortLoop() ){
3131 std::stringstream ss
;
3132 ss
<< "Looping word equation encountered." << std::endl
;
3133 throw LogicException(ss
.str());
3135 if (!options::stringProcessLoop())
3137 d_out
->setIncomplete();
3140 NodeManager
* nm
= NodeManager::currentNM();
3142 Trace("strings-loop") << "Detected possible loop for "
3143 << normal_forms
[loop_n_index
][loop_index
] << std::endl
;
3144 Trace("strings-loop") << " ... (X)= " << normal_forms
[other_n_index
][index
]
3147 Trace("strings-loop") << " ... T(Y.Z)= ";
3148 std::vector
<Node
>& veci
= normal_forms
[loop_n_index
];
3149 std::vector
<Node
> vec_t(veci
.begin() + index
, veci
.begin() + loop_index
);
3150 Node t_yz
= mkConcat(vec_t
);
3151 Trace("strings-loop") << " (" << t_yz
<< ")" << std::endl
;
3152 Trace("strings-loop") << " ... S(Z.Y)= ";
3153 std::vector
<Node
>& vecoi
= normal_forms
[other_n_index
];
3154 std::vector
<Node
> vec_s(vecoi
.begin() + index
+ 1, vecoi
.end());
3155 Node s_zy
= mkConcat(vec_s
);
3156 Trace("strings-loop") << s_zy
<< std::endl
;
3157 Trace("strings-loop") << " ... R= ";
3158 std::vector
<Node
> vec_r(veci
.begin() + loop_index
+ 1, veci
.end());
3159 Node r
= mkConcat(vec_r
);
3160 Trace("strings-loop") << r
<< std::endl
;
3162 if (s_zy
.isConst() && r
.isConst() && r
!= d_emptyString
)
3166 if (s_zy
.getConst
<String
>().tailcmp(r
.getConst
<String
>(), c
))
3170 s_zy
= nm
->mkConst(s_zy
.getConst
<String
>().substr(0, c
));
3173 Trace("strings-loop") << "Strings::Loop: Refactor S(Z.Y)= " << s_zy
3174 << ", c=" << c
<< std::endl
;
3180 Trace("strings-loop") << "Strings::Loop: tails are different."
3182 sendInference(info
.d_ant
, conc
, "Loop Conflict", true);
3188 for (unsigned r
= 0; r
< 2; r
++)
3190 Node t
= r
== 0 ? normal_forms
[loop_n_index
][loop_index
] : t_yz
;
3191 split_eq
= t
.eqNode(d_emptyString
);
3192 Node split_eqr
= Rewriter::rewrite(split_eq
);
3193 // the equality could rewrite to false
3194 if (!split_eqr
.isConst())
3196 if (!areDisequal(t
, d_emptyString
))
3198 // try to make t equal to empty to avoid loop
3199 info
.d_conc
= nm
->mkNode(kind::OR
, split_eq
, split_eq
.negate());
3200 info
.d_id
= INFER_LEN_SPLIT_EMP
;
3205 info
.d_ant
.push_back(split_eq
.negate());
3210 Assert(!split_eqr
.getConst
<bool>());
3214 Node ant
= mkExplain(info
.d_ant
);
3216 info
.d_antn
.push_back(ant
);
3219 if (s_zy
== t_yz
&& r
== d_emptyString
&& s_zy
.isConst()
3220 && s_zy
.getConst
<String
>().isRepeated())
3222 Node rep_c
= nm
->mkConst(s_zy
.getConst
<String
>().substr(0, 1));
3223 Trace("strings-loop") << "Special case (X)="
3224 << normal_forms
[other_n_index
][index
] << " "
3226 Trace("strings-loop") << "... (C)=" << rep_c
<< " " << std::endl
;
3229 nm
->mkNode(kind::STRING_IN_REGEXP
,
3230 normal_forms
[other_n_index
][index
],
3231 nm
->mkNode(kind::REGEXP_STAR
,
3232 nm
->mkNode(kind::STRING_TO_REGEXP
, rep_c
)));
3235 else if (t_yz
.isConst())
3237 Trace("strings-loop") << "Strings::Loop: Const Normal Breaking."
3239 CVC4::String s
= t_yz
.getConst
<CVC4::String
>();
3240 unsigned size
= s
.size();
3241 std::vector
<Node
> vconc
;
3242 for (unsigned len
= 1; len
<= size
; len
++)
3244 Node y
= nm
->mkConst(s
.substr(0, len
));
3245 Node z
= nm
->mkConst(s
.substr(len
, size
- len
));
3248 if (r
!= d_emptyString
)
3250 std::vector
<Node
> v2(vec_r
);
3251 v2
.insert(v2
.begin(), y
);
3252 v2
.insert(v2
.begin(), z
);
3253 restr
= mkConcat(z
, y
);
3254 cc
= Rewriter::rewrite(s_zy
.eqNode(mkConcat(v2
)));
3258 cc
= Rewriter::rewrite(s_zy
.eqNode(mkConcat(z
, y
)));
3264 Node conc2
= nm
->mkNode(
3265 kind::STRING_IN_REGEXP
,
3266 normal_forms
[other_n_index
][index
],
3267 nm
->mkNode(kind::REGEXP_CONCAT
,
3268 nm
->mkNode(kind::STRING_TO_REGEXP
, y
),
3269 nm
->mkNode(kind::REGEXP_STAR
,
3270 nm
->mkNode(kind::STRING_TO_REGEXP
, restr
))));
3271 cc
= cc
== d_true
? conc2
: nm
->mkNode(kind::AND
, cc
, conc2
);
3272 d_regexp_ant
[conc2
] = ant
;
3273 vconc
.push_back(cc
);
3275 conc
= vconc
.size() == 0 ? Node::null() : vconc
.size() == 1
3277 : nm
->mkNode(kind::OR
, vconc
);
3281 Trace("strings-loop") << "Strings::Loop: Normal Loop Breaking."
3284 Node sk_w
= d_sk_cache
.mkSkolem("w_loop");
3285 Node sk_y
= d_sk_cache
.mkSkolem("y_loop");
3286 registerLength(sk_y
, LENGTH_GEQ_ONE
);
3287 Node sk_z
= d_sk_cache
.mkSkolem("z_loop");
3288 // t1 * ... * tn = y * z
3289 Node conc1
= t_yz
.eqNode(mkConcat(sk_y
, sk_z
));
3290 // s1 * ... * sk = z * y * r
3291 vec_r
.insert(vec_r
.begin(), sk_y
);
3292 vec_r
.insert(vec_r
.begin(), sk_z
);
3293 Node conc2
= s_zy
.eqNode(mkConcat(vec_r
));
3295 normal_forms
[other_n_index
][index
].eqNode(mkConcat(sk_y
, sk_w
));
3296 Node restr
= r
== d_emptyString
? s_zy
: mkConcat(sk_z
, sk_y
);
3298 nm
->mkNode(kind::STRING_IN_REGEXP
,
3300 nm
->mkNode(kind::REGEXP_STAR
,
3301 nm
->mkNode(kind::STRING_TO_REGEXP
, restr
)));
3303 std::vector
<Node
> vec_conc
;
3304 vec_conc
.push_back(conc1
);
3305 vec_conc
.push_back(conc2
);
3306 vec_conc
.push_back(conc3
);
3307 vec_conc
.push_back(str_in_re
);
3308 // vec_conc.push_back(sk_y.eqNode(d_emptyString).negate());//by mkskolems
3309 conc
= nm
->mkNode(kind::AND
, vec_conc
);
3312 // set its antecedant to ant, to say when it is relevant
3313 if (!str_in_re
.isNull())
3315 d_regexp_ant
[str_in_re
] = ant
;
3319 info
.d_id
= INFER_FLOOP
;
3320 info
.d_nf_pair
[0] = normal_form_src
[i
];
3321 info
.d_nf_pair
[1] = normal_form_src
[j
];
3325 //return true for lemma, false if we succeed
3326 void TheoryStrings::processDeq( Node ni
, Node nj
) {
3327 //Assert( areDisequal( ni, nj ) );
3328 if( d_normal_forms
[ni
].size()>1 || d_normal_forms
[nj
].size()>1 ){
3329 std::vector
< Node
> nfi
;
3330 nfi
.insert( nfi
.end(), d_normal_forms
[ni
].begin(), d_normal_forms
[ni
].end() );
3331 std::vector
< Node
> nfj
;
3332 nfj
.insert( nfj
.end(), d_normal_forms
[nj
].begin(), d_normal_forms
[nj
].end() );
3334 int revRet
= processReverseDeq( nfi
, nfj
, ni
, nj
);
3340 nfi
.insert( nfi
.end(), d_normal_forms
[ni
].begin(), d_normal_forms
[ni
].end() );
3342 nfj
.insert( nfj
.end(), d_normal_forms
[nj
].begin(), d_normal_forms
[nj
].end() );
3345 while( index
<nfi
.size() || index
<nfj
.size() ){
3346 int ret
= processSimpleDeq( nfi
, nfj
, ni
, nj
, index
, false );
3350 Assert( index
<nfi
.size() && index
<nfj
.size() );
3351 Node i
= nfi
[index
];
3352 Node j
= nfj
[index
];
3353 Trace("strings-solve-debug") << "...Processing(DEQ) " << i
<< " " << j
<< std::endl
;
3354 if( !areEqual( i
, j
) ){
3355 Assert( i
.getKind()!=kind::CONST_STRING
|| j
.getKind()!=kind::CONST_STRING
);
3356 std::vector
< Node
> lexp
;
3357 Node li
= getLength( i
, lexp
);
3358 Node lj
= getLength( j
, lexp
);
3359 if( areDisequal( li
, lj
) ){
3360 if( i
.getKind()==kind::CONST_STRING
|| j
.getKind()==kind::CONST_STRING
){
3362 Node const_k
= i
.getKind() == kind::CONST_STRING
? i
: j
;
3363 Node nconst_k
= i
.getKind() == kind::CONST_STRING
? j
: i
;
3364 Node lnck
= i
.getKind() == kind::CONST_STRING
? lj
: li
;
3365 if( !d_equalityEngine
.areDisequal( nconst_k
, d_emptyString
, true ) ){
3366 Node eq
= nconst_k
.eqNode( d_emptyString
);
3367 Node conc
= NodeManager::currentNM()->mkNode( kind::OR
, eq
, eq
.negate() );
3368 sendInference( d_empty_vec
, conc
, "D-DISL-Emp-Split" );
3371 //split on first character
3372 CVC4::String str
= const_k
.getConst
<String
>();
3373 Node firstChar
= str
.size() == 1 ? const_k
: NodeManager::currentNM()->mkConst( str
.prefix( 1 ) );
3374 if( areEqual( lnck
, d_one
) ){
3375 if( areDisequal( firstChar
, nconst_k
) ){
3377 }else if( !areEqual( firstChar
, nconst_k
) ){
3378 //splitting on demand : try to make them disequal
3380 firstChar
, nconst_k
, "S-Split(DEQL-Const)", false))
3386 Node sk
= d_sk_cache
.mkSkolemCached(
3387 nconst_k
, firstChar
, SkolemCache::SK_ID_DC_SPT
, "dc_spt");
3388 registerLength(sk
, LENGTH_ONE
);
3390 d_sk_cache
.mkSkolemCached(nconst_k
,
3392 SkolemCache::SK_ID_DC_SPT_REM
,
3394 Node eq1
= nconst_k
.eqNode( NodeManager::currentNM()->mkNode( kind::STRING_CONCAT
, sk
, skr
) );
3395 eq1
= Rewriter::rewrite( eq1
);
3396 Node eq2
= nconst_k
.eqNode( NodeManager::currentNM()->mkNode( kind::STRING_CONCAT
, firstChar
, skr
) );
3397 std::vector
< Node
> antec
;
3398 antec
.insert( antec
.end(), d_normal_forms_exp
[ni
].begin(), d_normal_forms_exp
[ni
].end() );
3399 antec
.insert( antec
.end(), d_normal_forms_exp
[nj
].begin(), d_normal_forms_exp
[nj
].end() );
3400 antec
.push_back( nconst_k
.eqNode( d_emptyString
).negate() );
3401 sendInference( antec
, NodeManager::currentNM()->mkNode( kind::OR
,
3402 NodeManager::currentNM()->mkNode( kind::AND
, eq1
, sk
.eqNode( firstChar
).negate() ), eq2
), "D-DISL-CSplit" );
3403 d_pending_req_phase
[ eq1
] = true;
3408 Trace("strings-solve") << "Non-Simple Case 1 : add lemma " << std::endl
;
3410 std::vector
< Node
> antec
;
3411 std::vector
< Node
> antec_new_lits
;
3412 antec
.insert( antec
.end(), d_normal_forms_exp
[ni
].begin(), d_normal_forms_exp
[ni
].end() );
3413 antec
.insert( antec
.end(), d_normal_forms_exp
[nj
].begin(), d_normal_forms_exp
[nj
].end() );
3415 if( areDisequal( ni
, nj
) ){
3416 antec
.push_back( ni
.eqNode( nj
).negate() );
3418 antec_new_lits
.push_back( ni
.eqNode( nj
).negate() );
3420 antec_new_lits
.push_back( li
.eqNode( lj
).negate() );
3421 std::vector
< Node
> conc
;
3422 Node sk1
= d_sk_cache
.mkSkolemCached(
3423 i
, j
, SkolemCache::SK_ID_DEQ_X
, "x_dsplit");
3424 Node sk2
= d_sk_cache
.mkSkolemCached(
3425 i
, j
, SkolemCache::SK_ID_DEQ_Y
, "y_dsplit");
3426 Node sk3
= d_sk_cache
.mkSkolemCached(
3427 i
, j
, SkolemCache::SK_ID_DEQ_Z
, "z_dsplit");
3428 registerLength(sk3
, LENGTH_GEQ_ONE
);
3429 //Node nemp = sk3.eqNode(d_emptyString).negate();
3430 //conc.push_back(nemp);
3431 Node lsk1
= mkLength( sk1
);
3432 conc
.push_back( lsk1
.eqNode( li
) );
3433 Node lsk2
= mkLength( sk2
);
3434 conc
.push_back( lsk2
.eqNode( lj
) );
3435 conc
.push_back( NodeManager::currentNM()->mkNode( kind::OR
, j
.eqNode( mkConcat( sk1
, sk3
) ), i
.eqNode( mkConcat( sk2
, sk3
) ) ) );
3436 sendInference( antec
, antec_new_lits
, NodeManager::currentNM()->mkNode( kind::AND
, conc
), "D-DISL-Split" );
3437 ++(d_statistics
.d_deq_splits
);
3440 }else if( areEqual( li
, lj
) ){
3441 Assert( !areDisequal( i
, j
) );
3442 //splitting on demand : try to make them disequal
3443 if (sendSplit(i
, j
, "S-Split(DEQL)", false))
3448 //splitting on demand : try to make lengths equal
3449 if (sendSplit(li
, lj
, "D-Split"))
3462 int TheoryStrings::processReverseDeq( std::vector
< Node
>& nfi
, std::vector
< Node
>& nfj
, Node ni
, Node nj
) {
3463 //reverse normal form of i, j
3464 std::reverse( nfi
.begin(), nfi
.end() );
3465 std::reverse( nfj
.begin(), nfj
.end() );
3468 int ret
= processSimpleDeq( nfi
, nfj
, ni
, nj
, index
, true );
3470 //reverse normal form of i, j
3471 std::reverse( nfi
.begin(), nfi
.end() );
3472 std::reverse( nfj
.begin(), nfj
.end() );
3477 int TheoryStrings::processSimpleDeq( std::vector
< Node
>& nfi
, std::vector
< Node
>& nfj
, Node ni
, Node nj
, unsigned& index
, bool isRev
){
3478 // See if one side is constant, if so, the disequality ni != nj is satisfied
3479 // since ni does not contain nj or vice versa.
3480 // This is only valid when isRev is false, since when isRev=true, the contents
3481 // of normal form vectors nfi and nfj are reversed.
3484 for (unsigned i
= 0; i
< 2; i
++)
3486 Node c
= getConstantEqc(i
== 0 ? ni
: nj
);
3490 if (!TheoryStringsRewriter::canConstantContainList(
3491 c
, i
== 0 ? nfj
: nfi
, findex
, lindex
))
3493 Trace("strings-solve-debug")
3494 << "Disequality: constant cannot contain list" << std::endl
;
3500 while( index
<nfi
.size() || index
<nfj
.size() ) {
3501 if( index
>=nfi
.size() || index
>=nfj
.size() ){
3502 Trace("strings-solve-debug") << "Disequality normalize empty" << std::endl
;
3503 std::vector
< Node
> ant
;
3504 //we have a conflict : because the lengths are equal, the remainder needs to be empty, which will lead to a conflict
3505 Node lni
= getLengthExp( ni
, ant
, d_normal_forms_base
[ni
] );
3506 Node lnj
= getLengthExp( nj
, ant
, d_normal_forms_base
[nj
] );
3507 ant
.push_back( lni
.eqNode( lnj
) );
3508 ant
.insert( ant
.end(), d_normal_forms_exp
[ni
].begin(), d_normal_forms_exp
[ni
].end() );
3509 ant
.insert( ant
.end(), d_normal_forms_exp
[nj
].begin(), d_normal_forms_exp
[nj
].end() );
3510 std::vector
< Node
> cc
;
3511 std::vector
< Node
>& nfk
= index
>=nfi
.size() ? nfj
: nfi
;
3512 for( unsigned index_k
=index
; index_k
<nfk
.size(); index_k
++ ){
3513 cc
.push_back( nfk
[index_k
].eqNode( d_emptyString
) );
3515 Node conc
= cc
.size()==1 ? cc
[0] : NodeManager::currentNM()->mkNode( kind::AND
, cc
);
3516 conc
= Rewriter::rewrite( conc
);
3517 sendInference( ant
, conc
, "Disequality Normalize Empty", true);
3520 Node i
= nfi
[index
];
3521 Node j
= nfj
[index
];
3522 Trace("strings-solve-debug") << "...Processing(QED) " << i
<< " " << j
<< std::endl
;
3523 if( !areEqual( i
, j
) ) {
3524 if( i
.getKind()==kind::CONST_STRING
&& j
.getKind()==kind::CONST_STRING
) {
3525 unsigned int len_short
= i
.getConst
<String
>().size() < j
.getConst
<String
>().size() ? i
.getConst
<String
>().size() : j
.getConst
<String
>().size();
3526 bool isSameFix
= isRev
? i
.getConst
<String
>().rstrncmp(j
.getConst
<String
>(), len_short
): i
.getConst
<String
>().strncmp(j
.getConst
<String
>(), len_short
);
3528 //same prefix/suffix
3529 //k is the index of the string that is shorter
3530 Node nk
= i
.getConst
<String
>().size() < j
.getConst
<String
>().size() ? i
: j
;
3531 Node nl
= i
.getConst
<String
>().size() < j
.getConst
<String
>().size() ? j
: i
;
3534 int new_len
= nl
.getConst
<String
>().size() - len_short
;
3535 remainderStr
= NodeManager::currentNM()->mkConst( nl
.getConst
<String
>().substr(0, new_len
) );
3536 Trace("strings-solve-debug-test") << "Rev. Break normal form of " << nl
<< " into " << nk
<< ", " << remainderStr
<< std::endl
;
3538 remainderStr
= NodeManager::currentNM()->mkConst( nl
.getConst
<String
>().substr( len_short
) );
3539 Trace("strings-solve-debug-test") << "Break normal form of " << nl
<< " into " << nk
<< ", " << remainderStr
<< std::endl
;
3541 if( i
.getConst
<String
>().size() < j
.getConst
<String
>().size() ) {
3542 nfj
.insert( nfj
.begin() + index
+ 1, remainderStr
);
3543 nfj
[index
] = nfi
[index
];
3545 nfi
.insert( nfi
.begin() + index
+ 1, remainderStr
);
3546 nfi
[index
] = nfj
[index
];
3552 std::vector
< Node
> lexp
;
3553 Node li
= getLength( i
, lexp
);
3554 Node lj
= getLength( j
, lexp
);
3555 if( areEqual( li
, lj
) && areDisequal( i
, j
) ){
3556 Trace("strings-solve") << "Simple Case 2 : found equal length disequal sub strings " << i
<< " " << j
<< std::endl
;
3557 //we are done: D-Remove
3570 void TheoryStrings::addNormalFormPair( Node n1
, Node n2
){
3571 if( !isNormalFormPair( n1
, n2
) ){
3573 NodeIntMap::const_iterator it
= d_nf_pairs
.find( n1
);
3574 if( it
!=d_nf_pairs
.end() ){
3575 index
= (*it
).second
;
3577 d_nf_pairs
[n1
] = index
+ 1;
3578 if( index
<(int)d_nf_pairs_data
[n1
].size() ){
3579 d_nf_pairs_data
[n1
][index
] = n2
;
3581 d_nf_pairs_data
[n1
].push_back( n2
);
3583 Assert( isNormalFormPair( n1
, n2
) );
3585 Trace("strings-nf-debug") << "Already a normal form pair " << n1
<< " " << n2
<< std::endl
;
3589 bool TheoryStrings::isNormalFormPair( Node n1
, Node n2
) {
3590 //TODO: modulo equality?
3591 return isNormalFormPair2( n1
, n2
) || isNormalFormPair2( n2
, n1
);
3594 bool TheoryStrings::isNormalFormPair2( Node n1
, Node n2
) {
3595 //Trace("strings-debug") << "is normal form pair. " << n1 << " " << n2 << std::endl;
3596 NodeIntMap::const_iterator it
= d_nf_pairs
.find( n1
);
3597 if( it
!=d_nf_pairs
.end() ){
3598 Assert( d_nf_pairs_data
.find( n1
)!=d_nf_pairs_data
.end() );
3599 for( int i
=0; i
<(*it
).second
; i
++ ){
3600 Assert( i
<(int)d_nf_pairs_data
[n1
].size() );
3601 if( d_nf_pairs_data
[n1
][i
]==n2
){
3609 void TheoryStrings::registerTerm( Node n
, int effort
) {
3610 TypeNode tn
= n
.getType();
3611 bool do_register
= true;
3614 if (options::stringEagerLen())
3616 do_register
= effort
== 0;
3620 do_register
= effort
> 0 || n
.getKind() != STRING_CONCAT
;
3627 if (d_registered_terms_cache
.find(n
) != d_registered_terms_cache
.end())
3631 d_registered_terms_cache
.insert(n
);
3632 NodeManager
* nm
= NodeManager::currentNM();
3633 Debug("strings-register") << "TheoryStrings::registerTerm() " << n
3634 << ", effort = " << effort
<< std::endl
;
3637 // register length information:
3638 // for variables, split on empty vs positive length
3639 // for concat/const/replace, introduce proxy var and state length relation
3641 if (n
.getKind() != STRING_CONCAT
&& n
.getKind() != CONST_STRING
)
3643 Node lsumb
= nm
->mkNode(STRING_LENGTH
, n
);
3644 lsum
= Rewriter::rewrite(lsumb
);
3645 // can register length term if it does not rewrite
3648 registerLength(n
, LENGTH_SPLIT
);
3652 Node sk
= d_sk_cache
.mkSkolem("lsym");
3653 StringsProxyVarAttribute spva
;
3654 sk
.setAttribute(spva
, true);
3655 Node eq
= Rewriter::rewrite(sk
.eqNode(n
));
3656 Trace("strings-lemma") << "Strings::Lemma LENGTH Term : " << eq
3658 d_proxy_var
[n
] = sk
;
3659 Trace("strings-assert") << "(assert " << eq
<< ")" << std::endl
;
3661 Node skl
= nm
->mkNode(STRING_LENGTH
, sk
);
3662 if (n
.getKind() == STRING_CONCAT
)
3664 std::vector
<Node
> node_vec
;
3665 for (unsigned i
= 0; i
< n
.getNumChildren(); i
++)
3667 if (n
[i
].getAttribute(StringsProxyVarAttribute()))
3669 Assert(d_proxy_var_to_length
.find(n
[i
])
3670 != d_proxy_var_to_length
.end());
3671 node_vec
.push_back(d_proxy_var_to_length
[n
[i
]]);
3675 Node lni
= nm
->mkNode(STRING_LENGTH
, n
[i
]);
3676 node_vec
.push_back(lni
);
3679 lsum
= nm
->mkNode(PLUS
, node_vec
);
3680 lsum
= Rewriter::rewrite(lsum
);
3682 else if (n
.getKind() == CONST_STRING
)
3684 lsum
= nm
->mkConst(Rational(n
.getConst
<String
>().size()));
3686 Assert(!lsum
.isNull());
3687 d_proxy_var_to_length
[sk
] = lsum
;
3688 Node ceq
= Rewriter::rewrite(skl
.eqNode(lsum
));
3689 Trace("strings-lemma") << "Strings::Lemma LENGTH : " << ceq
<< std::endl
;
3690 Trace("strings-lemma-debug")
3691 << " prerewrite : " << skl
.eqNode(lsum
) << std::endl
;
3692 Trace("strings-assert") << "(assert " << ceq
<< ")" << std::endl
;
3695 else if (n
.getKind() == STRING_CODE
)
3697 d_has_str_code
= true;
3698 // ite( str.len(s)==1, 0 <= str.code(s) < num_codes, str.code(s)=-1 )
3699 Node code_len
= mkLength(n
[0]).eqNode(d_one
);
3700 Node code_eq_neg1
= n
.eqNode(d_neg_one
);
3701 Node code_range
= nm
->mkNode(
3703 nm
->mkNode(GEQ
, n
, d_zero
),
3704 nm
->mkNode(LT
, n
, nm
->mkConst(Rational(CVC4::String::num_codes()))));
3705 Node lem
= nm
->mkNode(ITE
, code_len
, code_range
, code_eq_neg1
);
3706 Trace("strings-lemma") << "Strings::Lemma CODE : " << lem
<< std::endl
;
3707 Trace("strings-assert") << "(assert " << lem
<< ")" << std::endl
;
3712 void TheoryStrings::sendInference( std::vector
< Node
>& exp
, std::vector
< Node
>& exp_n
, Node eq
, const char * c
, bool asLemma
) {
3713 eq
= eq
.isNull() ? d_false
: Rewriter::rewrite( eq
);
3715 if( Trace
.isOn("strings-infer-debug") ){
3716 Trace("strings-infer-debug") << "By " << c
<< ", infer : " << eq
<< " from: " << std::endl
;
3717 for( unsigned i
=0; i
<exp
.size(); i
++ ){
3718 Trace("strings-infer-debug") << " " << exp
[i
] << std::endl
;
3720 for( unsigned i
=0; i
<exp_n
.size(); i
++ ){
3721 Trace("strings-infer-debug") << " N:" << exp_n
[i
] << std::endl
;
3723 //Trace("strings-infer-debug") << "as lemma : " << asLemma << std::endl;
3725 //check if we should send a lemma or an inference
3726 if( asLemma
|| eq
==d_false
|| eq
.getKind()==kind::OR
|| !exp_n
.empty() || options::stringInferAsLemmas() ){
3728 if( options::stringRExplainLemmas() ){
3729 eq_exp
= mkExplain( exp
, exp_n
);
3732 eq_exp
= mkAnd( exp_n
);
3733 }else if( exp_n
.empty() ){
3734 eq_exp
= mkAnd( exp
);
3736 std::vector
< Node
> ev
;
3737 ev
.insert( ev
.end(), exp
.begin(), exp
.end() );
3738 ev
.insert( ev
.end(), exp_n
.begin(), exp_n
.end() );
3739 eq_exp
= NodeManager::currentNM()->mkNode( kind::AND
, ev
);
3742 // if we have unexplained literals, this lemma is not a conflict
3743 if (eq
== d_false
&& !exp_n
.empty())
3745 eq
= eq_exp
.negate();
3748 sendLemma( eq_exp
, eq
, c
);
3750 sendInfer( mkAnd( exp
), eq
, c
);
3755 void TheoryStrings::sendInference( std::vector
< Node
>& exp
, Node eq
, const char * c
, bool asLemma
) {
3756 std::vector
< Node
> exp_n
;
3757 sendInference( exp
, exp_n
, eq
, c
, asLemma
);
3760 void TheoryStrings::sendLemma( Node ant
, Node conc
, const char * c
) {
3761 if( conc
.isNull() || conc
== d_false
) {
3762 Trace("strings-conflict") << "Strings::Conflict : " << c
<< " : " << ant
<< std::endl
;
3763 Trace("strings-lemma") << "Strings::Conflict : " << c
<< " : " << ant
<< std::endl
;
3764 Trace("strings-assert") << "(assert (not " << ant
<< ")) ; conflict " << c
<< std::endl
;
3765 d_out
->conflict(ant
);
3769 if( ant
== d_true
) {
3772 lem
= NodeManager::currentNM()->mkNode( kind::IMPLIES
, ant
, conc
);
3774 Trace("strings-lemma") << "Strings::Lemma " << c
<< " : " << lem
<< std::endl
;
3775 Trace("strings-assert") << "(assert " << lem
<< ") ; lemma " << c
<< std::endl
;
3776 d_lemma_cache
.push_back( lem
);
3780 void TheoryStrings::sendInfer( Node eq_exp
, Node eq
, const char * c
) {
3781 if( options::stringInferSym() ){
3782 std::vector
< Node
> vars
;
3783 std::vector
< Node
> subs
;
3784 std::vector
< Node
> unproc
;
3785 inferSubstitutionProxyVars( eq_exp
, vars
, subs
, unproc
);
3786 if( unproc
.empty() ){
3787 Trace("strings-lemma-debug") << "Strings::Infer " << eq
<< " from " << eq_exp
<< " by " << c
<< std::endl
;
3788 Node eqs
= eq
.substitute( vars
.begin(), vars
.end(), subs
.begin(), subs
.end() );
3789 Trace("strings-lemma-debug") << "Strings::Infer Alternate : " << eqs
<< std::endl
;
3790 for( unsigned i
=0; i
<vars
.size(); i
++ ){
3791 Trace("strings-lemma-debug") << " " << vars
[i
] << " -> " << subs
[i
] << std::endl
;
3793 sendLemma( d_true
, eqs
, c
);
3796 for( unsigned i
=0; i
<unproc
.size(); i
++ ){
3797 Trace("strings-lemma-debug") << " non-trivial exp : " << unproc
[i
] << std::endl
;
3801 Trace("strings-lemma") << "Strings::Infer " << eq
<< " from " << eq_exp
<< " by " << c
<< std::endl
;
3802 Trace("strings-assert") << "(assert (=> " << eq_exp
<< " " << eq
<< ")) ; infer " << c
<< std::endl
;
3803 d_pending
.push_back( eq
);
3804 d_pending_exp
[eq
] = eq_exp
;
3805 d_infer
.push_back( eq
);
3806 d_infer_exp
.push_back( eq_exp
);
3809 bool TheoryStrings::sendSplit(Node a
, Node b
, const char* c
, bool preq
)
3811 Node eq
= a
.eqNode( b
);
3812 eq
= Rewriter::rewrite( eq
);
3815 Node neq
= NodeManager::currentNM()->mkNode(kind::NOT
, eq
);
3816 Node lemma_or
= NodeManager::currentNM()->mkNode(kind::OR
, eq
, neq
);
3817 Trace("strings-lemma") << "Strings::Lemma " << c
<< " SPLIT : " << lemma_or
3819 d_lemma_cache
.push_back(lemma_or
);
3820 d_pending_req_phase
[eq
] = preq
;
3821 ++(d_statistics
.d_splits
);
3827 void TheoryStrings::registerLength(Node n
, LengthStatus s
)
3829 if (d_length_lemma_terms_cache
.find(n
) != d_length_lemma_terms_cache
.end())
3833 d_length_lemma_terms_cache
.insert(n
);
3835 NodeManager
* nm
= NodeManager::currentNM();
3836 Node n_len
= nm
->mkNode(kind::STRING_LENGTH
, n
);
3838 if (s
== LENGTH_GEQ_ONE
)
3840 Node neq_empty
= n
.eqNode(d_emptyString
).negate();
3841 Node len_n_gt_z
= nm
->mkNode(GT
, n_len
, d_zero
);
3842 Node len_geq_one
= nm
->mkNode(AND
, neq_empty
, len_n_gt_z
);
3843 Trace("strings-lemma") << "Strings::Lemma SK-GEQ-ONE : " << len_geq_one
3845 Trace("strings-assert") << "(assert " << len_geq_one
<< ")" << std::endl
;
3846 d_out
->lemma(len_geq_one
);
3850 if (s
== LENGTH_ONE
)
3852 Node len_one
= n_len
.eqNode(d_one
);
3853 Trace("strings-lemma") << "Strings::Lemma SK-ONE : " << len_one
3855 Trace("strings-assert") << "(assert " << len_one
<< ")" << std::endl
;
3856 d_out
->lemma(len_one
);
3859 Assert(s
== LENGTH_SPLIT
);
3861 if( options::stringSplitEmp() || !options::stringLenGeqZ() ){
3862 Node n_len_eq_z
= n_len
.eqNode( d_zero
);
3863 Node n_len_eq_z_2
= n
.eqNode( d_emptyString
);
3864 Node case_empty
= nm
->mkNode(AND
, n_len_eq_z
, n_len_eq_z_2
);
3865 case_empty
= Rewriter::rewrite(case_empty
);
3866 Node case_nempty
= nm
->mkNode(GT
, n_len
, d_zero
);
3867 if (!case_empty
.isConst())
3869 Node lem
= nm
->mkNode(OR
, case_empty
, case_nempty
);
3871 Trace("strings-lemma") << "Strings::Lemma LENGTH >= 0 : " << lem
3873 // prefer trying the empty case first
3874 // notice that requirePhase must only be called on rewritten literals that
3875 // occur in the CNF stream.
3876 n_len_eq_z
= Rewriter::rewrite(n_len_eq_z
);
3877 Assert(!n_len_eq_z
.isConst());
3878 d_out
->requirePhase(n_len_eq_z
, true);
3879 n_len_eq_z_2
= Rewriter::rewrite(n_len_eq_z_2
);
3880 Assert(!n_len_eq_z_2
.isConst());
3881 d_out
->requirePhase(n_len_eq_z_2
, true);
3883 else if (!case_empty
.getConst
<bool>())
3885 // the rewriter knows that n is non-empty
3886 Trace("strings-lemma")
3887 << "Strings::Lemma LENGTH > 0 (non-empty): " << case_nempty
3889 d_out
->lemma(case_nempty
);
3893 // If n = "" ---> true or len( n ) = 0 ----> true, then we expect that
3894 // n ---> "". Since this method is only called on non-constants n, it must
3895 // be that n = "" ^ len( n ) = 0 does not rewrite to true.
3900 // additionally add len( x ) >= 0 ?
3901 if( options::stringLenGeqZ() ){
3902 Node n_len_geq
= nm
->mkNode(kind::GEQ
, n_len
, d_zero
);
3903 n_len_geq
= Rewriter::rewrite( n_len_geq
);
3904 d_out
->lemma( n_len_geq
);
3908 void TheoryStrings::inferSubstitutionProxyVars( Node n
, std::vector
< Node
>& vars
, std::vector
< Node
>& subs
, std::vector
< Node
>& unproc
) {
3909 if( n
.getKind()==kind::AND
){
3910 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
3911 inferSubstitutionProxyVars( n
[i
], vars
, subs
, unproc
);
3914 }else if( n
.getKind()==kind::EQUAL
){
3915 Node ns
= n
.substitute( vars
.begin(), vars
.end(), subs
.begin(), subs
.end() );
3916 ns
= Rewriter::rewrite( ns
);
3917 if( ns
.getKind()==kind::EQUAL
){
3920 for( unsigned i
=0; i
<2; i
++ ){
3922 if( ns
[i
].getAttribute(StringsProxyVarAttribute()) ){
3924 }else if( ns
[i
].isConst() ){
3925 NodeNodeMap::const_iterator it
= d_proxy_var
.find( ns
[i
] );
3926 if( it
!=d_proxy_var
.end() ){
3932 if( v
.getNumChildren()==0 ){
3936 //both sides involved in proxy var
3947 subs
.push_back( s
);
3948 vars
.push_back( v
);
3956 unproc
.push_back( n
);
3961 Node
TheoryStrings::mkConcat( Node n1
, Node n2
) {
3962 return Rewriter::rewrite( NodeManager::currentNM()->mkNode( kind::STRING_CONCAT
, n1
, n2
) );
3965 Node
TheoryStrings::mkConcat( Node n1
, Node n2
, Node n3
) {
3966 return Rewriter::rewrite( NodeManager::currentNM()->mkNode( kind::STRING_CONCAT
, n1
, n2
, n3
) );
3969 Node
TheoryStrings::mkConcat( const std::vector
< Node
>& c
) {
3970 return Rewriter::rewrite( c
.size()>1 ? NodeManager::currentNM()->mkNode( kind::STRING_CONCAT
, c
) : ( c
.size()==1 ? c
[0] : d_emptyString
) );
3973 Node
TheoryStrings::mkLength( Node t
) {
3974 return Rewriter::rewrite( NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, t
) );
3977 Node
TheoryStrings::mkExplain( std::vector
< Node
>& a
) {
3978 std::vector
< Node
> an
;
3979 return mkExplain( a
, an
);
3982 Node
TheoryStrings::mkExplain( std::vector
< Node
>& a
, std::vector
< Node
>& an
) {
3983 std::vector
< TNode
> antec_exp
;
3984 for( unsigned i
=0; i
<a
.size(); i
++ ) {
3985 if( std::find( a
.begin(), a
.begin() + i
, a
[i
] )==a
.begin() + i
) {
3987 Debug("strings-explain") << "Ask for explanation of " << a
[i
] << std::endl
;
3989 if(a
[i
].getKind() == kind::EQUAL
) {
3990 //Assert( hasTerm(a[i][0]) );
3991 //Assert( hasTerm(a[i][1]) );
3992 Assert( areEqual(a
[i
][0], a
[i
][1]) );
3993 if( a
[i
][0]==a
[i
][1] ){
3996 } else if( a
[i
].getKind()==kind::NOT
&& a
[i
][0].getKind()==kind::EQUAL
) {
3997 Assert( hasTerm(a
[i
][0][0]) );
3998 Assert( hasTerm(a
[i
][0][1]) );
3999 AlwaysAssert( d_equalityEngine
.areDisequal(a
[i
][0][0], a
[i
][0][1], true) );
4000 }else if( a
[i
].getKind() == kind::AND
){
4001 for( unsigned j
=0; j
<a
[i
].getNumChildren(); j
++ ){
4002 a
.push_back( a
[i
][j
] );
4007 unsigned ps
= antec_exp
.size();
4008 explain(a
[i
], antec_exp
);
4009 Debug("strings-explain") << "Done, explanation was : " << std::endl
;
4010 for( unsigned j
=ps
; j
<antec_exp
.size(); j
++ ) {
4011 Debug("strings-explain") << " " << antec_exp
[j
] << std::endl
;
4013 Debug("strings-explain") << std::endl
;
4017 for( unsigned i
=0; i
<an
.size(); i
++ ) {
4018 if( std::find( an
.begin(), an
.begin() + i
, an
[i
] )==an
.begin() + i
){
4019 Debug("strings-explain") << "Add to explanation (new literal) " << an
[i
] << std::endl
;
4020 antec_exp
.push_back(an
[i
]);
4024 if( antec_exp
.empty() ) {
4026 } else if( antec_exp
.size()==1 ) {
4029 ant
= NodeManager::currentNM()->mkNode( kind::AND
, antec_exp
);
4031 //ant = Rewriter::rewrite( ant );
4035 Node
TheoryStrings::mkAnd( std::vector
< Node
>& a
) {
4036 std::vector
< Node
> au
;
4037 for( unsigned i
=0; i
<a
.size(); i
++ ){
4038 if( std::find( au
.begin(), au
.end(), a
[i
] )==au
.end() ){
4039 au
.push_back( a
[i
] );
4044 } else if( au
.size() == 1 ) {
4047 return NodeManager::currentNM()->mkNode( kind::AND
, au
);
4051 void TheoryStrings::getConcatVec( Node n
, std::vector
< Node
>& c
) {
4052 if( n
.getKind()==kind::STRING_CONCAT
) {
4053 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ) {
4054 if( !areEqual( n
[i
], d_emptyString
) ) {
4055 c
.push_back( n
[i
] );
4063 void TheoryStrings::checkNormalFormsDeq()
4065 std::vector
< std::vector
< Node
> > cols
;
4066 std::vector
< Node
> lts
;
4067 std::map
< Node
, std::map
< Node
, bool > > processed
;
4069 //for each pair of disequal strings, must determine whether their lengths are equal or disequal
4070 for( NodeList::const_iterator id
= d_ee_disequalities
.begin(); id
!= d_ee_disequalities
.end(); ++id
) {
4073 for( unsigned i
=0; i
<2; i
++ ){
4074 n
[i
] = d_equalityEngine
.getRepresentative( eq
[i
] );
4076 if( processed
[n
[0]].find( n
[1] )==processed
[n
[0]].end() ){
4077 processed
[n
[0]][n
[1]] = true;
4079 for( unsigned i
=0; i
<2; i
++ ){
4080 EqcInfo
* ei
= getOrMakeEqcInfo( n
[i
], false );
4081 lt
[i
] = ei
? ei
->d_length_term
: Node::null();
4082 if( lt
[i
].isNull() ){
4085 lt
[i
] = NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, lt
[i
] );
4087 if( !areEqual( lt
[0], lt
[1] ) && !areDisequal( lt
[0], lt
[1] ) ){
4088 sendSplit( lt
[0], lt
[1], "DEQ-LENGTH-SP" );
4093 if( !hasProcessed() ){
4094 separateByLength( d_strings_eqc
, cols
, lts
);
4095 for( unsigned i
=0; i
<cols
.size(); i
++ ){
4096 if( cols
[i
].size()>1 && d_lemma_cache
.empty() ){
4097 Trace("strings-solve") << "- Verify disequalities are processed for " << cols
[i
][0] << ", normal form : ";
4098 printConcat( d_normal_forms
[cols
[i
][0]], "strings-solve" );
4099 Trace("strings-solve") << "... #eql = " << cols
[i
].size() << std::endl
;
4100 //must ensure that normal forms are disequal
4101 for( unsigned j
=0; j
<cols
[i
].size(); j
++ ){
4102 for( unsigned k
=(j
+1); k
<cols
[i
].size(); k
++ ){
4103 //for strings that are disequal, but have the same length
4104 if( areDisequal( cols
[i
][j
], cols
[i
][k
] ) ){
4105 Assert( !d_conflict
);
4106 Trace("strings-solve") << "- Compare " << cols
[i
][j
] << " ";
4107 printConcat( d_normal_forms
[cols
[i
][j
]], "strings-solve" );
4108 Trace("strings-solve") << " against " << cols
[i
][k
] << " ";
4109 printConcat( d_normal_forms
[cols
[i
][k
]], "strings-solve" );
4110 Trace("strings-solve") << "..." << std::endl
;
4111 processDeq( cols
[i
][j
], cols
[i
][k
] );
4112 if( hasProcessed() ){
4123 void TheoryStrings::checkLengthsEqc() {
4124 if( options::stringLenNorm() ){
4125 for( unsigned i
=0; i
<d_strings_eqc
.size(); i
++ ){
4126 //if( d_normal_forms[nodes[i]].size()>1 ) {
4127 Trace("strings-process-debug") << "Process length constraints for " << d_strings_eqc
[i
] << std::endl
;
4128 //check if there is a length term for this equivalence class
4129 EqcInfo
* ei
= getOrMakeEqcInfo( d_strings_eqc
[i
], false );
4130 Node lt
= ei
? ei
->d_length_term
: Node::null();
4131 if( !lt
.isNull() ) {
4132 Node llt
= NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, lt
);
4133 //now, check if length normalization has occurred
4134 if( ei
->d_normalized_length
.get().isNull() ) {
4135 Node nf
= mkConcat( d_normal_forms
[d_strings_eqc
[i
]] );
4136 if( Trace
.isOn("strings-process-debug") ){
4137 Trace("strings-process-debug") << " normal form is " << nf
<< " from base " << d_normal_forms_base
[d_strings_eqc
[i
]] << std::endl
;
4138 Trace("strings-process-debug") << " normal form exp is: " << std::endl
;
4139 for( unsigned j
=0; j
<d_normal_forms_exp
[d_strings_eqc
[i
]].size(); j
++ ){
4140 Trace("strings-process-debug") << " " << d_normal_forms_exp
[d_strings_eqc
[i
]][j
] << std::endl
;
4144 //if not, add the lemma
4145 std::vector
< Node
> ant
;
4146 ant
.insert( ant
.end(), d_normal_forms_exp
[d_strings_eqc
[i
]].begin(), d_normal_forms_exp
[d_strings_eqc
[i
]].end() );
4147 ant
.push_back( d_normal_forms_base
[d_strings_eqc
[i
]].eqNode( lt
) );
4148 Node lc
= NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, nf
);
4149 Node lcr
= Rewriter::rewrite( lc
);
4150 Trace("strings-process-debug") << "Rewrote length " << lc
<< " to " << lcr
<< std::endl
;
4151 Node eq
= llt
.eqNode( lcr
);
4153 ei
->d_normalized_length
.set( eq
);
4154 sendInference( ant
, eq
, "LEN-NORM", true );
4158 Trace("strings-process-debug") << "No length term for eqc " << d_strings_eqc
[i
] << " " << d_eqc_to_len_term
[d_strings_eqc
[i
]] << std::endl
;
4159 if( !options::stringEagerLen() ){
4160 Node c
= mkConcat( d_normal_forms
[d_strings_eqc
[i
]] );
4161 registerTerm( c
, 3 );
4164 NodeNodeMap::const_iterator it = d_proxy_var.find( c );
4165 if( it!=d_proxy_var.end() ){
4166 Node pv = (*it).second;
4167 Assert( d_proxy_var_to_length.find( pv )!=d_proxy_var_to_length.end() );
4168 Node pvl = d_proxy_var_to_length[pv];
4169 Node ceq = Rewriter::rewrite( mkLength( pv ).eqNode( pvl ) );
4170 sendInference( d_empty_vec, ceq, "LEN-NORM-I", true );
4177 // Trace("strings-process-debug") << "Do not process length constraints for " << nodes[i] << " " << d_normal_forms[nodes[i]].size() << std::endl;
4183 void TheoryStrings::checkCardinality() {
4184 //int cardinality = options::stringCharCardinality();
4185 //Trace("strings-solve-debug2") << "get cardinality: " << cardinality << endl;
4187 //AJR: this will create a partition of eqc, where each collection has length that are pairwise propagated to be equal.
4188 // 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).
4189 // TODO: revisit this?
4190 std::vector
< std::vector
< Node
> > cols
;
4191 std::vector
< Node
> lts
;
4192 separateByLength( d_strings_eqc
, cols
, lts
);
4194 Trace("strings-card") << "Check cardinality...." << std::endl
;
4195 for( unsigned i
= 0; i
<cols
.size(); ++i
) {
4197 Trace("strings-card") << "Number of strings with length equal to " << lr
<< " is " << cols
[i
].size() << std::endl
;
4198 if( cols
[i
].size() > 1 ) {
4200 unsigned card_need
= 1;
4201 double curr
= (double)cols
[i
].size();
4202 while( curr
>d_card_size
){
4203 curr
= curr
/(double)d_card_size
;
4206 Trace("strings-card") << "Need length " << card_need
<< " for this number of strings (where alphabet size is " << d_card_size
<< ")." << std::endl
;
4207 //check if we need to split
4208 bool needsSplit
= true;
4210 // if constant, compare
4211 Node cmp
= NodeManager::currentNM()->mkNode( kind::GEQ
, lr
, NodeManager::currentNM()->mkConst( Rational( card_need
) ) );
4212 cmp
= Rewriter::rewrite( cmp
);
4213 needsSplit
= cmp
!=d_true
;
4215 // find the minimimum constant that we are unknown to be disequal from, or otherwise stop if we increment such that cardinality does not apply
4217 bool success
= true;
4218 while( r
<card_need
&& success
){
4219 Node rr
= NodeManager::currentNM()->mkConst
<Rational
>( Rational(r
) );
4220 if( areDisequal( rr
, lr
) ){
4227 Trace("strings-card") << "Symbolic length " << lr
<< " must be at least " << r
<< " due to constant disequalities." << std::endl
;
4229 needsSplit
= r
<card_need
;
4233 unsigned int int_k
= (unsigned int)card_need
;
4234 for( std::vector
< Node
>::iterator itr1
= cols
[i
].begin();
4235 itr1
!= cols
[i
].end(); ++itr1
) {
4236 for( std::vector
< Node
>::iterator itr2
= itr1
+ 1;
4237 itr2
!= cols
[i
].end(); ++itr2
) {
4238 if(!areDisequal( *itr1
, *itr2
)) {
4240 if (sendSplit(*itr1
, *itr2
, "CARD-SP"))
4247 EqcInfo
* ei
= getOrMakeEqcInfo( lr
, true );
4248 Trace("strings-card") << "Previous cardinality used for " << lr
<< " is " << ((int)ei
->d_cardinality_lem_k
.get()-1) << std::endl
;
4249 if( int_k
+1 > ei
->d_cardinality_lem_k
.get() ){
4250 Node k_node
= NodeManager::currentNM()->mkConst( ::CVC4::Rational( int_k
) );
4251 //add cardinality lemma
4252 Node dist
= NodeManager::currentNM()->mkNode( kind::DISTINCT
, cols
[i
] );
4253 std::vector
< Node
> vec_node
;
4254 vec_node
.push_back( dist
);
4255 for( std::vector
< Node
>::iterator itr1
= cols
[i
].begin();
4256 itr1
!= cols
[i
].end(); ++itr1
) {
4257 Node len
= NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, *itr1
);
4259 Node len_eq_lr
= len
.eqNode(lr
);
4260 vec_node
.push_back( len_eq_lr
);
4263 Node len
= NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, cols
[i
][0] );
4264 Node cons
= NodeManager::currentNM()->mkNode( kind::GEQ
, len
, k_node
);
4265 cons
= Rewriter::rewrite( cons
);
4266 ei
->d_cardinality_lem_k
.set( int_k
+1 );
4268 sendInference( d_empty_vec
, vec_node
, cons
, "CARDINALITY", true );
4275 Trace("strings-card") << "...end check cardinality" << std::endl
;
4278 void TheoryStrings::getEquivalenceClasses( std::vector
< Node
>& eqcs
) {
4279 eq::EqClassesIterator eqcs_i
= eq::EqClassesIterator( &d_equalityEngine
);
4280 while( !eqcs_i
.isFinished() ) {
4281 Node eqc
= (*eqcs_i
);
4282 //if eqc.getType is string
4283 if (eqc
.getType().isString()) {
4284 eqcs
.push_back( eqc
);
4290 void TheoryStrings::separateByLength(std::vector
< Node
>& n
,
4291 std::vector
< std::vector
< Node
> >& cols
,
4292 std::vector
< Node
>& lts
) {
4293 unsigned leqc_counter
= 0;
4294 std::map
< Node
, unsigned > eqc_to_leqc
;
4295 std::map
< unsigned, Node
> leqc_to_eqc
;
4296 std::map
< unsigned, std::vector
< Node
> > eqc_to_strings
;
4297 for( unsigned i
=0; i
<n
.size(); i
++ ) {
4299 Assert( d_equalityEngine
.getRepresentative(eqc
)==eqc
);
4300 EqcInfo
* ei
= getOrMakeEqcInfo( eqc
, false );
4301 Node lt
= ei
? ei
->d_length_term
: Node::null();
4303 lt
= NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, lt
);
4304 Node r
= d_equalityEngine
.getRepresentative( lt
);
4305 if( eqc_to_leqc
.find( r
)==eqc_to_leqc
.end() ){
4306 eqc_to_leqc
[r
] = leqc_counter
;
4307 leqc_to_eqc
[leqc_counter
] = r
;
4310 eqc_to_strings
[ eqc_to_leqc
[r
] ].push_back( eqc
);
4312 eqc_to_strings
[leqc_counter
].push_back( eqc
);
4316 for( std::map
< unsigned, std::vector
< Node
> >::iterator it
= eqc_to_strings
.begin(); it
!= eqc_to_strings
.end(); ++it
){
4317 cols
.push_back( std::vector
< Node
>() );
4318 cols
.back().insert( cols
.back().end(), it
->second
.begin(), it
->second
.end() );
4319 lts
.push_back( leqc_to_eqc
[it
->first
] );
4323 void TheoryStrings::printConcat( std::vector
< Node
>& n
, const char * c
) {
4324 for( unsigned i
=0; i
<n
.size(); i
++ ){
4325 if( i
>0 ) Trace(c
) << " ++ ";
4332 //// Finite Model Finding
4334 Node
TheoryStrings::getNextDecisionRequest( unsigned& priority
) {
4335 if( options::stringFMF() && !d_conflict
){
4336 Node in_var_lsum
= d_input_var_lsum
.get();
4337 //Trace("strings-fmf-debug") << "Strings::FMF: Assertion Level = " << d_valuation.getAssertionLevel() << std::endl;
4338 //initialize the term we will minimize
4339 if( in_var_lsum
.isNull() && !d_input_vars
.empty() ){
4340 Trace("strings-fmf-debug") << "Input variables: ";
4341 std::vector
< Node
> ll
;
4342 for(NodeSet::key_iterator itr
= d_input_vars
.key_begin();
4343 itr
!= d_input_vars
.key_end(); ++itr
) {
4344 Trace("strings-fmf-debug") << " " << (*itr
) ;
4345 ll
.push_back( NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, *itr
) );
4347 Trace("strings-fmf-debug") << std::endl
;
4348 in_var_lsum
= ll
.size()==1 ? ll
[0] : NodeManager::currentNM()->mkNode( kind::PLUS
, ll
);
4349 in_var_lsum
= Rewriter::rewrite( in_var_lsum
);
4350 d_input_var_lsum
.set( in_var_lsum
);
4352 if( !in_var_lsum
.isNull() ){
4353 //Trace("strings-fmf") << "Get next decision request." << std::endl;
4354 //check if we need to decide on something
4355 int decideCard
= d_curr_cardinality
.get();
4356 if( d_cardinality_lits
.find( decideCard
)!=d_cardinality_lits
.end() ){
4358 Node cnode
= d_cardinality_lits
[ d_curr_cardinality
.get() ];
4359 if( d_valuation
.hasSatValue( cnode
, value
) ) {
4361 d_curr_cardinality
.set( d_curr_cardinality
.get() + 1 );
4362 decideCard
= d_curr_cardinality
.get();
4363 Trace("strings-fmf-debug") << "Has false SAT value, increment and decide." << std::endl
;
4366 Trace("strings-fmf-debug") << "Has true SAT value, do not decide." << std::endl
;
4369 Trace("strings-fmf-debug") << "No SAT value, decide." << std::endl
;
4372 if( decideCard
!=-1 ){
4373 if( d_cardinality_lits
.find( decideCard
)==d_cardinality_lits
.end() ){
4374 Node lit
= NodeManager::currentNM()->mkNode( kind::LEQ
, in_var_lsum
, NodeManager::currentNM()->mkConst( Rational( decideCard
) ) );
4375 lit
= Rewriter::rewrite( lit
);
4376 d_cardinality_lits
[decideCard
] = lit
;
4377 Node lem
= NodeManager::currentNM()->mkNode( kind::OR
, lit
, lit
.negate() );
4378 Trace("strings-fmf") << "Strings::FMF: Add decision lemma " << lem
<< ", decideCard = " << decideCard
<< std::endl
;
4379 d_out
->lemma( lem
);
4380 d_out
->requirePhase( lit
, true );
4382 Node lit
= d_cardinality_lits
[ decideCard
];
4383 Trace("strings-fmf") << "Strings::FMF: Decide positive on " << lit
<< std::endl
;
4389 return Node::null();
4392 Node
TheoryStrings::ppRewrite(TNode atom
) {
4393 Trace("strings-ppr") << "TheoryStrings::ppRewrite " << atom
<< std::endl
;
4395 if (options::regExpElim() && atom
.getKind() == STRING_IN_REGEXP
)
4397 // aggressive elimination of regular expression membership
4398 atomElim
= d_regexp_elim
.eliminate(atom
);
4399 if (!atomElim
.isNull())
4401 Trace("strings-ppr") << " rewrote " << atom
<< " -> " << atomElim
4402 << " via regular expression elimination."
4407 if( !options::stringLazyPreproc() ){
4408 //eager preprocess here
4409 std::vector
< Node
> new_nodes
;
4410 Node ret
= d_preproc
.processAssertion( atom
, new_nodes
);
4412 Trace("strings-ppr") << " rewrote " << atom
<< " -> " << ret
<< ", with " << new_nodes
.size() << " lemmas." << std::endl
;
4413 for( unsigned i
=0; i
<new_nodes
.size(); i
++ ){
4414 Trace("strings-ppr") << " lemma : " << new_nodes
[i
] << std::endl
;
4415 d_out
->lemma( new_nodes
[i
] );
4419 Assert( new_nodes
.empty() );
4426 TheoryStrings::Statistics::Statistics()
4427 : d_splits("theory::strings::NumOfSplitOnDemands", 0),
4428 d_eq_splits("theory::strings::NumOfEqSplits", 0),
4429 d_deq_splits("theory::strings::NumOfDiseqSplits", 0),
4430 d_loop_lemmas("theory::strings::NumOfLoops", 0)
4432 smtStatisticsRegistry()->registerStat(&d_splits
);
4433 smtStatisticsRegistry()->registerStat(&d_eq_splits
);
4434 smtStatisticsRegistry()->registerStat(&d_deq_splits
);
4435 smtStatisticsRegistry()->registerStat(&d_loop_lemmas
);
4438 TheoryStrings::Statistics::~Statistics(){
4439 smtStatisticsRegistry()->unregisterStat(&d_splits
);
4440 smtStatisticsRegistry()->unregisterStat(&d_eq_splits
);
4441 smtStatisticsRegistry()->unregisterStat(&d_deq_splits
);
4442 smtStatisticsRegistry()->unregisterStat(&d_loop_lemmas
);
4464 //// Regular Expressions
4467 unsigned TheoryStrings::getNumMemberships( Node n
, bool isPos
) {
4469 NodeIntMap::const_iterator it
= d_pos_memberships
.find( n
);
4470 if( it
!=d_pos_memberships
.end() ){
4471 return (*it
).second
;
4474 NodeIntMap::const_iterator it
= d_neg_memberships
.find( n
);
4475 if( it
!=d_neg_memberships
.end() ){
4476 return (*it
).second
;
4482 Node
TheoryStrings::getMembership( Node n
, bool isPos
, unsigned i
) {
4483 return isPos
? d_pos_memberships_data
[n
][i
] : d_neg_memberships_data
[n
][i
];
4486 Node
TheoryStrings::mkRegExpAntec(Node atom
, Node ant
) {
4487 if(d_regexp_ant
.find(atom
) == d_regexp_ant
.end()) {
4488 return NodeManager::currentNM()->mkNode(kind::AND
, ant
, atom
);
4490 Node n
= d_regexp_ant
[atom
];
4491 return NodeManager::currentNM()->mkNode(kind::AND
, ant
, n
);
4495 void TheoryStrings::checkMemberships() {
4496 //add the memberships
4497 std::vector
<Node
> mems
= getExtTheory()->getActive(kind::STRING_IN_REGEXP
);
4498 for (unsigned i
= 0; i
< mems
.size(); i
++) {
4500 Assert( d_extf_info_tmp
.find( n
)!=d_extf_info_tmp
.end() );
4501 if( d_extf_info_tmp
[n
].d_pol
==1 || d_extf_info_tmp
[n
].d_pol
==-1 ){
4502 bool pol
= d_extf_info_tmp
[n
].d_pol
==1;
4503 Trace("strings-process-debug") << " add membership : " << n
<< ", pol = " << pol
<< std::endl
;
4504 addMembership( pol
? n
: n
.negate() );
4506 Trace("strings-process-debug") << " irrelevant (non-asserted) membership : " << n
<< std::endl
;
4510 bool addedLemma
= false;
4511 bool changed
= false;
4512 std::vector
< Node
> processed
;
4513 std::vector
< Node
> cprocessed
;
4515 Trace("regexp-debug") << "Checking Memberships ... " << std::endl
;
4516 //if(options::stringEIT()) {
4517 //TODO: Opt for normal forms
4518 for( NodeIntMap::const_iterator itr_xr
= d_pos_memberships
.begin(); itr_xr
!= d_pos_memberships
.end(); ++itr_xr
){
4519 bool spflag
= false;
4520 Node x
= (*itr_xr
).first
;
4521 Trace("regexp-debug") << "Checking Memberships for " << x
<< std::endl
;
4522 if(d_inter_index
.find(x
) == d_inter_index
.end()) {
4523 d_inter_index
[x
] = 0;
4525 int cur_inter_idx
= d_inter_index
[x
];
4526 unsigned n_pmem
= (*itr_xr
).second
;
4527 Assert( getNumMemberships( x
, true )==n_pmem
);
4528 if( cur_inter_idx
!= (int)n_pmem
) {
4530 d_inter_cache
[x
] = getMembership( x
, true, 0 );
4531 d_inter_index
[x
] = 1;
4532 Trace("regexp-debug") << "... only one choice " << std::endl
;
4533 } else if(n_pmem
> 1) {
4535 if(d_inter_cache
.find(x
) != d_inter_cache
.end()) {
4536 r
= d_inter_cache
[x
];
4539 r
= getMembership( x
, true, 0 );
4543 unsigned k_start
= cur_inter_idx
;
4544 Trace("regexp-debug") << "... staring from : " << cur_inter_idx
<< ", we have " << n_pmem
<< std::endl
;
4545 for(unsigned k
= k_start
; k
<n_pmem
; k
++) {
4546 Node r2
= getMembership( x
, true, k
);
4547 r
= d_regexp_opr
.intersect(r
, r2
, spflag
);
4550 } else if(r
== d_emptyRegexp
) {
4551 std::vector
< Node
> vec_nodes
;
4552 for( unsigned kk
=0; kk
<=k
; kk
++ ){
4553 Node rr
= getMembership( x
, true, kk
);
4554 Node n
= NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP
, x
, rr
);
4555 vec_nodes
.push_back( n
);
4558 sendInference(vec_nodes
, conc
, "INTERSECT CONFLICT", true);
4567 if(!d_conflict
&& !spflag
) {
4568 d_inter_cache
[x
] = r
;
4569 d_inter_index
[x
] = (int)n_pmem
;
4576 Trace("regexp-debug") << "... No Intersect Conflict in Memberships, addedLemma: " << addedLemma
<< std::endl
;
4578 NodeManager
* nm
= NodeManager::currentNM();
4579 for( unsigned i
=0; i
<d_regexp_memberships
.size(); i
++ ) {
4580 //check regular expression membership
4581 Node assertion
= d_regexp_memberships
[i
];
4582 Trace("regexp-debug") << "Check : " << assertion
<< " " << (d_regexp_ucached
.find(assertion
) == d_regexp_ucached
.end()) << " " << (d_regexp_ccached
.find(assertion
) == d_regexp_ccached
.end()) << std::endl
;
4583 if( d_regexp_ucached
.find(assertion
) == d_regexp_ucached
.end()
4584 && d_regexp_ccached
.find(assertion
) == d_regexp_ccached
.end() ) {
4585 Trace("strings-regexp") << "We have regular expression assertion : " << assertion
<< std::endl
;
4586 Node atom
= assertion
.getKind()==kind::NOT
? assertion
[0] : assertion
;
4587 bool polarity
= assertion
.getKind()!=kind::NOT
;
4591 std::vector
< Node
> rnfexp
;
4595 x
= getNormalString(x
, rnfexp
);
4598 if (!d_regexp_opr
.checkConstRegExp(r
))
4600 r
= getNormalSymRegExp(r
, rnfexp
);
4603 Trace("strings-regexp-nf") << "Term " << atom
<< " is normalized to "
4604 << x
<< " IN " << r
<< std::endl
;
4608 Rewriter::rewrite(nm
->mkNode(kind::STRING_IN_REGEXP
, x
, r
));
4615 d_regexp_ccached
.insert(assertion
);
4618 else if (tmp
== d_false
)
4620 Node antec
= mkRegExpAntec(assertion
, mkExplain(rnfexp
));
4621 Node conc
= Node::null();
4622 sendLemma(antec
, conc
, "REGEXP NF Conflict");
4629 flag
= checkPDerivative(x
, r
, atom
, addedLemma
, rnfexp
);
4631 if(! options::stringExp()) {
4632 throw LogicException("Strings Incomplete (due to Negative Membership) by default, try --strings-exp option.");
4636 //check if the term is atomic
4637 Node xr
= getRepresentative( x
);
4638 //Trace("strings-regexp") << xr << " is rep of " << x << std::endl;
4639 //Assert( d_normal_forms.find( xr )!=d_normal_forms.end() );
4640 Trace("strings-regexp")
4641 << "Unroll/simplify membership of atomic term " << xr
4643 // if so, do simple unrolling
4644 std::vector
<Node
> nvec
;
4648 d_regexp_opr
.simplify(atom
, nvec
, polarity
);
4650 Node antec
= assertion
;
4651 if (d_regexp_ant
.find(assertion
) != d_regexp_ant
.end())
4653 antec
= d_regexp_ant
[assertion
];
4654 for (std::vector
<Node
>::const_iterator itr
= nvec
.begin();
4658 if (itr
->getKind() == kind::STRING_IN_REGEXP
)
4660 if (d_regexp_ant
.find(*itr
) == d_regexp_ant
.end())
4662 d_regexp_ant
[*itr
] = antec
;
4667 antec
= NodeManager::currentNM()->mkNode(
4668 kind::AND
, antec
, mkExplain(rnfexp
));
4669 Node conc
= nvec
.size() == 1
4671 : NodeManager::currentNM()->mkNode(kind::AND
, nvec
);
4672 conc
= Rewriter::rewrite(conc
);
4673 sendLemma(antec
, conc
, "REGEXP_Unfold");
4677 cprocessed
.push_back(assertion
);
4681 processed
.push_back(assertion
);
4683 // d_regexp_ucached[assertion] = true;
4693 for( unsigned i
=0; i
<processed
.size(); i
++ ) {
4694 Trace("strings-regexp") << "...add " << processed
[i
] << " to u-cache." << std::endl
;
4695 d_regexp_ucached
.insert(processed
[i
]);
4697 for( unsigned i
=0; i
<cprocessed
.size(); i
++ ) {
4698 Trace("strings-regexp") << "...add " << cprocessed
[i
] << " to c-cache." << std::endl
;
4699 d_regexp_ccached
.insert(cprocessed
[i
]);
4705 bool TheoryStrings::checkPDerivative( Node x
, Node r
, Node atom
, bool &addedLemma
, std::vector
< Node
> &nf_exp
) {
4707 Node antnf
= mkExplain(nf_exp
);
4709 if(areEqual(x
, d_emptyString
)) {
4711 switch(d_regexp_opr
.delta(r
, exp
)) {
4713 Node antec
= mkRegExpAntec(atom
, x
.eqNode(d_emptyString
));
4714 antec
= NodeManager::currentNM()->mkNode(kind::AND
, antec
, antnf
);
4715 sendLemma(antec
, exp
, "RegExp Delta");
4717 d_regexp_ccached
.insert(atom
);
4721 d_regexp_ccached
.insert(atom
);
4725 Node antec
= mkRegExpAntec(atom
, x
.eqNode(d_emptyString
));
4726 antec
= NodeManager::currentNM()->mkNode(kind::AND
, antec
, antnf
);
4727 Node conc
= Node::null();
4728 sendLemma(antec
, conc
, "RegExp Delta CONFLICT");
4730 d_regexp_ccached
.insert(atom
);
4738 /*Node xr = getRepresentative( x );
4740 Node n = NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP, xr, r);
4741 Node nn = Rewriter::rewrite( n );
4743 d_regexp_ccached.insert(atom);
4745 } else if(nn == d_false) {
4746 Node antec = mkRegExpAntec(atom, x.eqNode(xr));
4747 Node conc = Node::null();
4748 sendLemma(antec, conc, "RegExp Delta CONFLICT");
4750 d_regexp_ccached.insert(atom);
4754 Node sREant
= mkRegExpAntec(atom
, d_true
);
4755 sREant
= NodeManager::currentNM()->mkNode(kind::AND
, sREant
, antnf
);
4756 if(deriveRegExp( x
, r
, sREant
)) {
4758 d_regexp_ccached
.insert(atom
);
4765 CVC4::String
TheoryStrings::getHeadConst( Node x
) {
4767 return x
.getConst
< String
>();
4768 } else if( x
.getKind() == kind::STRING_CONCAT
) {
4769 if( x
[0].isConst() ) {
4770 return x
[0].getConst
< String
>();
4772 return d_emptyString
.getConst
< String
>();
4775 return d_emptyString
.getConst
< String
>();
4779 bool TheoryStrings::deriveRegExp( Node x
, Node r
, Node ant
) {
4781 Assert(x
!= d_emptyString
);
4782 Trace("regexp-derive") << "TheoryStrings::deriveRegExp: x=" << x
<< ", r= " << r
<< std::endl
;
4784 // Node n = NodeManager::currentNM()->mkNode( kind::STRING_IN_REGEXP, x, r );
4785 // Node r = Rewriter::rewrite( n );
4787 // sendLemma(ant, r, "REGEXP REWRITE");
4791 CVC4::String s
= getHeadConst( x
);
4792 if( !s
.isEmptyString() && d_regexp_opr
.checkConstRegExp( r
) ) {
4793 Node conc
= Node::null();
4796 for(unsigned i
=0; i
<s
.size(); ++i
) {
4797 CVC4::String c
= s
.substr(i
, 1);
4799 int rt
= d_regexp_opr
.derivativeS(dc
, c
, dc2
);
4803 } else if(rt
== 2) {
4812 Assert(false, "Impossible: TheoryStrings::deriveRegExp: const string in const regular expression.");
4815 Assert( x
.getKind() == kind::STRING_CONCAT
);
4816 std::vector
< Node
> vec_nodes
;
4817 for(unsigned int i
=1; i
<x
.getNumChildren(); ++i
) {
4818 vec_nodes
.push_back( x
[i
] );
4820 Node left
= mkConcat( vec_nodes
);
4821 left
= Rewriter::rewrite( left
);
4822 conc
= NodeManager::currentNM()->mkNode( kind::STRING_IN_REGEXP
, left
, dc
);
4824 /*std::vector< Node > sdc;
4825 d_regexp_opr.simplify(conc, sdc, true);
4826 if(sdc.size() == 1) {
4829 conc = Rewriter::rewrite(NodeManager::currentNM()->mkNode(kind::AND, conc));
4833 sendLemma(ant
, conc
, "RegExp-Derive");
4840 void TheoryStrings::addMembership(Node assertion
) {
4841 bool polarity
= assertion
.getKind() != kind::NOT
;
4842 TNode atom
= polarity
? assertion
: assertion
[0];
4847 NodeIntMap::const_iterator it
= d_pos_memberships
.find( x
);
4848 if( it
!=d_nf_pairs
.end() ){
4849 index
= (*it
).second
;
4850 for( int k
=0; k
<index
; k
++ ){
4851 if( k
<(int)d_pos_memberships_data
[x
].size() ){
4852 if( d_pos_memberships_data
[x
][k
]==r
){
4860 d_pos_memberships
[x
] = index
+ 1;
4861 if( index
<(int)d_pos_memberships_data
[x
].size() ){
4862 d_pos_memberships_data
[x
][index
] = r
;
4864 d_pos_memberships_data
[x
].push_back( r
);
4866 } else if(!options::stringIgnNegMembership()) {
4867 /*if(options::stringEIT() && d_regexp_opr.checkConstRegExp(r)) {
4869 Node r2 = d_regexp_opr.complement(r, rt);
4870 Node a = NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP, x, r2);
4873 NodeIntMap::const_iterator it
= d_neg_memberships
.find( x
);
4874 if( it
!=d_nf_pairs
.end() ){
4875 index
= (*it
).second
;
4876 for( int k
=0; k
<index
; k
++ ){
4877 if( k
<(int)d_neg_memberships_data
[x
].size() ){
4878 if( d_neg_memberships_data
[x
][k
]==r
){
4886 d_neg_memberships
[x
] = index
+ 1;
4887 if( index
<(int)d_neg_memberships_data
[x
].size() ){
4888 d_neg_memberships_data
[x
][index
] = r
;
4890 d_neg_memberships_data
[x
].push_back( r
);
4894 if(polarity
|| !options::stringIgnNegMembership()) {
4895 d_regexp_memberships
.push_back( assertion
);
4899 Node
TheoryStrings::getNormalString( Node x
, std::vector
< Node
>& nf_exp
){
4901 Node xr
= getRepresentative( x
);
4902 if( d_normal_forms
.find( xr
) != d_normal_forms
.end() ){
4903 Node ret
= mkConcat( d_normal_forms
[xr
] );
4904 nf_exp
.insert( nf_exp
.end(), d_normal_forms_exp
[xr
].begin(), d_normal_forms_exp
[xr
].end() );
4905 addToExplanation( x
, d_normal_forms_base
[xr
], nf_exp
);
4906 Trace("strings-debug") << "Term: " << x
<< " has a normal form " << ret
<< std::endl
;
4909 if(x
.getKind() == kind::STRING_CONCAT
) {
4910 std::vector
< Node
> vec_nodes
;
4911 for(unsigned i
=0; i
<x
.getNumChildren(); i
++) {
4912 Node nc
= getNormalString( x
[i
], nf_exp
);
4913 vec_nodes
.push_back( nc
);
4915 return mkConcat( vec_nodes
);
4922 Node
TheoryStrings::getNormalSymRegExp(Node r
, std::vector
<Node
> &nf_exp
) {
4924 switch( r
.getKind() ) {
4925 case kind::REGEXP_EMPTY
:
4926 case kind::REGEXP_SIGMA
:
4928 case kind::STRING_TO_REGEXP
: {
4929 if(!r
[0].isConst()) {
4930 Node tmp
= getNormalString( r
[0], nf_exp
);
4932 ret
= NodeManager::currentNM()->mkNode(kind::STRING_TO_REGEXP
, tmp
);
4937 case kind::REGEXP_CONCAT
:
4938 case kind::REGEXP_UNION
:
4939 case kind::REGEXP_INTER
:
4940 case kind::REGEXP_STAR
:
4942 std::vector
< Node
> vec_nodes
;
4943 for (const Node
& cr
: r
)
4945 vec_nodes
.push_back(getNormalSymRegExp(cr
, nf_exp
));
4947 ret
= Rewriter::rewrite(
4948 NodeManager::currentNM()->mkNode(r
.getKind(), vec_nodes
));
4951 //case kind::REGEXP_PLUS:
4952 //case kind::REGEXP_OPT:
4953 //case kind::REGEXP_RANGE:
4955 Trace("strings-error") << "Unsupported term: " << r
<< " in normalization SymRegExp." << std::endl
;
4957 //return Node::null();
4963 /** run the given inference step */
4964 void TheoryStrings::runInferStep(InferStep s
, int effort
)
4966 Trace("strings-process") << "Run " << s
;
4969 Trace("strings-process") << ", effort = " << effort
;
4971 Trace("strings-process") << "..." << std::endl
;
4974 case CHECK_INIT
: checkInit(); break;
4975 case CHECK_CONST_EQC
: checkConstantEquivalenceClasses(); break;
4976 case CHECK_EXTF_EVAL
: checkExtfEval(effort
); break;
4977 case CHECK_CYCLES
: checkCycles(); break;
4978 case CHECK_FLAT_FORMS
: checkFlatForms(); break;
4979 case CHECK_NORMAL_FORMS_EQ
: checkNormalFormsEq(); break;
4980 case CHECK_NORMAL_FORMS_DEQ
: checkNormalFormsDeq(); break;
4981 case CHECK_CODES
: checkCodes(); break;
4982 case CHECK_LENGTH_EQC
: checkLengthsEqc(); break;
4983 case CHECK_EXTF_REDUCTION
: checkExtfReductions(effort
); break;
4984 case CHECK_MEMBERSHIP
: checkMemberships(); break;
4985 case CHECK_CARDINALITY
: checkCardinality(); break;
4986 default: Unreachable(); break;
4988 Trace("strings-process") << "Done " << s
4989 << ", addedFact = " << !d_pending
.empty() << " "
4990 << !d_lemma_cache
.empty()
4991 << ", d_conflict = " << d_conflict
<< std::endl
;
4994 bool TheoryStrings::hasStrategyEffort(Effort e
) const
4996 return d_strat_steps
.find(e
) != d_strat_steps
.end();
4999 void TheoryStrings::addStrategyStep(InferStep s
, int effort
, bool addBreak
)
5001 // must run check init first
5002 Assert((s
== CHECK_INIT
)==d_infer_steps
.empty());
5003 // must use check cycles when using flat forms
5004 Assert(s
!= CHECK_FLAT_FORMS
5005 || std::find(d_infer_steps
.begin(), d_infer_steps
.end(), CHECK_CYCLES
)
5006 != d_infer_steps
.end());
5007 d_infer_steps
.push_back(s
);
5008 d_infer_step_effort
.push_back(effort
);
5011 d_infer_steps
.push_back(BREAK
);
5012 d_infer_step_effort
.push_back(0);
5016 void TheoryStrings::initializeStrategy()
5018 // initialize the strategy if not already done so
5019 if (!d_strategy_init
)
5021 std::map
<Effort
, unsigned> step_begin
;
5022 std::map
<Effort
, unsigned> step_end
;
5023 d_strategy_init
= true;
5024 // beginning indices
5025 step_begin
[EFFORT_FULL
] = 0;
5026 if (options::stringEager())
5028 step_begin
[EFFORT_STANDARD
] = 0;
5030 // add the inference steps
5031 addStrategyStep(CHECK_INIT
);
5032 addStrategyStep(CHECK_CONST_EQC
);
5033 addStrategyStep(CHECK_EXTF_EVAL
, 0);
5034 addStrategyStep(CHECK_CYCLES
);
5035 if (options::stringFlatForms())
5037 addStrategyStep(CHECK_FLAT_FORMS
);
5039 addStrategyStep(CHECK_EXTF_REDUCTION
, 1);
5040 if (options::stringEager())
5042 // do only the above inferences at standard effort, if applicable
5043 step_end
[EFFORT_STANDARD
] = d_infer_steps
.size() - 1;
5045 addStrategyStep(CHECK_NORMAL_FORMS_EQ
);
5046 addStrategyStep(CHECK_EXTF_EVAL
, 1);
5047 if (!options::stringEagerLen())
5049 addStrategyStep(CHECK_LENGTH_EQC
);
5051 addStrategyStep(CHECK_NORMAL_FORMS_DEQ
);
5052 addStrategyStep(CHECK_CODES
);
5053 if (options::stringEagerLen())
5055 addStrategyStep(CHECK_LENGTH_EQC
);
5057 if (options::stringExp() && !options::stringGuessModel())
5059 addStrategyStep(CHECK_EXTF_REDUCTION
, 2);
5061 addStrategyStep(CHECK_MEMBERSHIP
);
5062 addStrategyStep(CHECK_CARDINALITY
);
5063 step_end
[EFFORT_FULL
] = d_infer_steps
.size() - 1;
5064 if (options::stringExp() && options::stringGuessModel())
5066 step_begin
[EFFORT_LAST_CALL
] = d_infer_steps
.size();
5067 // these two steps are run in parallel
5068 addStrategyStep(CHECK_EXTF_REDUCTION
, 2, false);
5069 addStrategyStep(CHECK_EXTF_EVAL
, 3);
5070 step_end
[EFFORT_LAST_CALL
] = d_infer_steps
.size() - 1;
5072 // set the beginning/ending ranges
5073 for (const std::pair
<const Effort
, unsigned>& it_begin
: step_begin
)
5075 Effort e
= it_begin
.first
;
5076 std::map
<Effort
, unsigned>::iterator it_end
= step_end
.find(e
);
5077 Assert(it_end
!= step_end
.end());
5079 std::pair
<unsigned, unsigned>(it_begin
.second
, it_end
->second
);
5084 void TheoryStrings::runStrategy(unsigned sbegin
, unsigned send
)
5086 Trace("strings-process") << "----check, next round---" << std::endl
;
5087 for (unsigned i
= sbegin
; i
<= send
; i
++)
5089 InferStep curr
= d_infer_steps
[i
];
5099 runInferStep(curr
, d_infer_step_effort
[i
]);
5106 Trace("strings-process") << "----finished round---" << std::endl
;
5109 }/* CVC4::theory::strings namespace */
5110 }/* CVC4::theory namespace */
5111 }/* CVC4 namespace */