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/rewriter.h"
28 #include "theory/strings/theory_strings_rewriter.h"
29 #include "theory/strings/type_enumerator.h"
30 #include "theory/theory_model.h"
31 #include "theory/valuation.h"
34 using namespace CVC4::context
;
35 using namespace CVC4::kind
;
41 std::ostream
& operator<<(std::ostream
& out
, Inference i
)
45 case INFER_SSPLIT_CST_PROP
: out
<< "S-Split(CST-P)-prop"; break;
46 case INFER_SSPLIT_VAR_PROP
: out
<< "S-Split(VAR)-prop"; break;
47 case INFER_LEN_SPLIT
: out
<< "Len-Split(Len)"; break;
48 case INFER_LEN_SPLIT_EMP
: out
<< "Len-Split(Emp)"; break;
49 case INFER_SSPLIT_CST_BINARY
: out
<< "S-Split(CST-P)-binary"; break;
50 case INFER_SSPLIT_CST
: out
<< "S-Split(CST-P)"; break;
51 case INFER_SSPLIT_VAR
: out
<< "S-Split(VAR)"; break;
52 case INFER_FLOOP
: out
<< "F-Loop"; break;
53 default: out
<< "?"; break;
58 std::ostream
& operator<<(std::ostream
& out
, InferStep s
)
62 case BREAK
: out
<< "break"; break;
63 case CHECK_INIT
: out
<< "check_init"; break;
64 case CHECK_CONST_EQC
: out
<< "check_const_eqc"; break;
65 case CHECK_EXTF_EVAL
: out
<< "check_extf_eval"; break;
66 case CHECK_CYCLES
: out
<< "check_cycles"; break;
67 case CHECK_FLAT_FORMS
: out
<< "check_flat_forms"; break;
68 case CHECK_NORMAL_FORMS_EQ
: out
<< "check_normal_forms_eq"; break;
69 case CHECK_NORMAL_FORMS_DEQ
: out
<< "check_normal_forms_deq"; break;
70 case CHECK_CODES
: out
<< "check_codes"; break;
71 case CHECK_LENGTH_EQC
: out
<< "check_length_eqc"; break;
72 case CHECK_EXTF_REDUCTION
: out
<< "check_extf_reduction"; break;
73 case CHECK_MEMBERSHIP
: out
<< "check_membership"; break;
74 case CHECK_CARDINALITY
: out
<< "check_cardinality"; break;
75 default: out
<< "?"; break;
80 Node
TheoryStrings::TermIndex::add( TNode n
, unsigned index
, TheoryStrings
* t
, Node er
, std::vector
< Node
>& c
) {
81 if( index
==n
.getNumChildren() ){
82 if( d_data
.isNull() ){
87 Assert( index
<n
.getNumChildren() );
88 TNode nir
= t
->getRepresentative( n
[index
] );
89 //if it is empty, and doing CONCAT, ignore
90 if( nir
==er
&& n
.getKind()==kind::STRING_CONCAT
){
91 return add( n
, index
+1, t
, er
, c
);
94 return d_children
[nir
].add( n
, index
+1, t
, er
, c
);
99 TheoryStrings::TheoryStrings(context::Context
* c
,
100 context::UserContext
* u
,
103 const LogicInfo
& logicInfo
)
104 : Theory(THEORY_STRINGS
, c
, u
, out
, valuation
, logicInfo
),
106 d_equalityEngine(d_notify
, c
, "theory::strings", true),
107 d_conflict(c
, false),
111 d_pregistered_terms_cache(u
),
112 d_registered_terms_cache(u
),
113 d_length_lemma_terms_cache(u
),
114 d_preproc(&d_sk_cache
, u
),
115 d_extf_infer_cache(c
),
116 d_extf_infer_cache_u(u
),
117 d_ee_disequalities(c
),
120 d_proxy_var_to_length(u
),
122 d_has_extf(c
, false),
123 d_has_str_code(false),
124 d_regexp_memberships(c
),
127 d_pos_memberships(c
),
128 d_neg_memberships(c
),
131 d_processed_memberships(c
),
135 d_cardinality_lits(u
),
136 d_curr_cardinality(c
, 0),
138 d_strategy_init(false)
141 getExtTheory()->addFunctionKind(kind::STRING_SUBSTR
);
142 getExtTheory()->addFunctionKind(kind::STRING_STRIDOF
);
143 getExtTheory()->addFunctionKind(kind::STRING_ITOS
);
144 getExtTheory()->addFunctionKind(kind::STRING_STOI
);
145 getExtTheory()->addFunctionKind(kind::STRING_STRREPL
);
146 getExtTheory()->addFunctionKind(kind::STRING_STRREPLALL
);
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
);
165 d_equalityEngine
.addFunctionKind(kind::STRING_STRREPLALL
);
168 d_zero
= NodeManager::currentNM()->mkConst( Rational( 0 ) );
169 d_one
= NodeManager::currentNM()->mkConst( Rational( 1 ) );
170 d_neg_one
= NodeManager::currentNM()->mkConst(Rational(-1));
171 d_emptyString
= NodeManager::currentNM()->mkConst( ::CVC4::String("") );
172 std::vector
< Node
> nvec
;
173 d_emptyRegexp
= NodeManager::currentNM()->mkNode( kind::REGEXP_EMPTY
, nvec
);
174 d_true
= NodeManager::currentNM()->mkConst( true );
175 d_false
= NodeManager::currentNM()->mkConst( false );
177 d_card_size
= TheoryStringsRewriter::getAlphabetCardinality();
180 TheoryStrings::~TheoryStrings() {
181 for( std::map
< Node
, EqcInfo
* >::iterator it
= d_eqc_info
.begin(); it
!= d_eqc_info
.end(); ++it
){
186 Node
TheoryStrings::getRepresentative( Node t
) {
187 if( d_equalityEngine
.hasTerm( t
) ){
188 return d_equalityEngine
.getRepresentative( t
);
194 bool TheoryStrings::hasTerm( Node a
){
195 return d_equalityEngine
.hasTerm( a
);
198 bool TheoryStrings::areEqual( Node a
, Node b
){
201 }else if( hasTerm( a
) && hasTerm( b
) ){
202 return d_equalityEngine
.areEqual( a
, b
);
208 bool TheoryStrings::areDisequal( Node a
, Node b
){
212 if( hasTerm( a
) && hasTerm( b
) ) {
213 Node ar
= d_equalityEngine
.getRepresentative( a
);
214 Node br
= d_equalityEngine
.getRepresentative( b
);
215 return ( ar
!=br
&& ar
.isConst() && br
.isConst() ) || d_equalityEngine
.areDisequal( ar
, br
, false );
217 Node ar
= getRepresentative( a
);
218 Node br
= getRepresentative( b
);
219 return ar
!=br
&& ar
.isConst() && br
.isConst();
224 bool TheoryStrings::areCareDisequal( TNode x
, TNode y
) {
225 Assert( d_equalityEngine
.hasTerm(x
) );
226 Assert( d_equalityEngine
.hasTerm(y
) );
227 if( d_equalityEngine
.isTriggerTerm(x
, THEORY_STRINGS
) && d_equalityEngine
.isTriggerTerm(y
, THEORY_STRINGS
) ){
228 TNode x_shared
= d_equalityEngine
.getTriggerTermRepresentative(x
, THEORY_STRINGS
);
229 TNode y_shared
= d_equalityEngine
.getTriggerTermRepresentative(y
, THEORY_STRINGS
);
230 EqualityStatus eqStatus
= d_valuation
.getEqualityStatus(x_shared
, y_shared
);
231 if( eqStatus
==EQUALITY_FALSE_AND_PROPAGATED
|| eqStatus
==EQUALITY_FALSE
|| eqStatus
==EQUALITY_FALSE_IN_MODEL
){
238 Node
TheoryStrings::getLengthExp( Node t
, std::vector
< Node
>& exp
, Node te
){
239 Assert( areEqual( t
, te
) );
240 Node lt
= mkLength( te
);
242 // use own length if it exists, leads to shorter explanation
245 EqcInfo
* ei
= getOrMakeEqcInfo( t
, false );
246 Node length_term
= ei
? ei
->d_length_term
: Node::null();
247 if( length_term
.isNull() ){
248 //typically shouldnt be necessary
251 Debug("strings") << "TheoryStrings::getLengthTerm " << t
<< " is " << length_term
<< std::endl
;
252 addToExplanation( length_term
, te
, exp
);
253 return Rewriter::rewrite( NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, length_term
) );
257 Node
TheoryStrings::getLength( Node t
, std::vector
< Node
>& exp
) {
258 return getLengthExp( t
, exp
, t
);
261 void TheoryStrings::setMasterEqualityEngine(eq::EqualityEngine
* eq
) {
262 d_equalityEngine
.setMasterEqualityEngine(eq
);
265 void TheoryStrings::addSharedTerm(TNode t
) {
266 Debug("strings") << "TheoryStrings::addSharedTerm(): "
267 << t
<< " " << t
.getType().isBoolean() << endl
;
268 d_equalityEngine
.addTriggerTerm(t
, THEORY_STRINGS
);
269 if (options::stringExp())
271 getExtTheory()->registerTermRec(t
);
273 Debug("strings") << "TheoryStrings::addSharedTerm() finished" << std::endl
;
276 EqualityStatus
TheoryStrings::getEqualityStatus(TNode a
, TNode b
) {
277 if( d_equalityEngine
.hasTerm(a
) && d_equalityEngine
.hasTerm(b
) ){
278 if (d_equalityEngine
.areEqual(a
, b
)) {
279 // The terms are implied to be equal
280 return EQUALITY_TRUE
;
282 if (d_equalityEngine
.areDisequal(a
, b
, false)) {
283 // The terms are implied to be dis-equal
284 return EQUALITY_FALSE
;
287 return EQUALITY_UNKNOWN
;
290 void TheoryStrings::propagate(Effort e
) {
291 // direct propagation now
294 bool TheoryStrings::propagate(TNode literal
) {
295 Debug("strings-propagate") << "TheoryStrings::propagate(" << literal
<< ")" << std::endl
;
296 // If already in conflict, no more propagation
298 Debug("strings-propagate") << "TheoryStrings::propagate(" << literal
<< "): already in conflict" << std::endl
;
302 bool ok
= d_out
->propagate(literal
);
310 void TheoryStrings::explain(TNode literal
, std::vector
<TNode
>& assumptions
) {
311 Debug("strings-explain") << "Explain " << literal
<< " " << d_conflict
<< std::endl
;
312 bool polarity
= literal
.getKind() != kind::NOT
;
313 TNode atom
= polarity
? literal
: literal
[0];
314 unsigned ps
= assumptions
.size();
315 std::vector
< TNode
> tassumptions
;
316 if (atom
.getKind() == kind::EQUAL
) {
317 if( atom
[0]!=atom
[1] ){
318 Assert( hasTerm( atom
[0] ) );
319 Assert( hasTerm( atom
[1] ) );
320 d_equalityEngine
.explainEquality(atom
[0], atom
[1], polarity
, tassumptions
);
323 d_equalityEngine
.explainPredicate(atom
, polarity
, tassumptions
);
325 for( unsigned i
=0; i
<tassumptions
.size(); i
++ ){
326 if( std::find( assumptions
.begin(), assumptions
.end(), tassumptions
[i
] )==assumptions
.end() ){
327 assumptions
.push_back( tassumptions
[i
] );
330 if (Debug
.isOn("strings-explain-debug"))
332 Debug("strings-explain-debug") << "Explanation for " << literal
<< " was "
334 for (unsigned i
= ps
; i
< assumptions
.size(); i
++)
336 Debug("strings-explain-debug") << " " << assumptions
[i
] << std::endl
;
341 Node
TheoryStrings::explain( TNode literal
){
342 Debug("strings-explain") << "explain called on " << literal
<< std::endl
;
343 std::vector
< TNode
> assumptions
;
344 explain( literal
, assumptions
);
345 if( assumptions
.empty() ){
347 }else if( assumptions
.size()==1 ){
348 return assumptions
[0];
350 return NodeManager::currentNM()->mkNode( kind::AND
, assumptions
);
354 bool TheoryStrings::getCurrentSubstitution( int effort
, std::vector
< Node
>& vars
,
355 std::vector
< Node
>& subs
, std::map
< Node
, std::vector
< Node
> >& exp
) {
356 Trace("strings-subs") << "getCurrentSubstitution, effort = " << effort
<< std::endl
;
357 for( unsigned i
=0; i
<vars
.size(); i
++ ){
359 Trace("strings-subs") << " get subs for " << n
<< "..." << std::endl
;
362 Node mv
= d_valuation
.getModel()->getRepresentative( n
);
363 Trace("strings-subs") << " model val : " << mv
<< std::endl
;
364 subs
.push_back( mv
);
366 Node nr
= getRepresentative( n
);
367 std::map
< Node
, Node
>::iterator itc
= d_eqc_to_const
.find( nr
);
368 if( itc
!=d_eqc_to_const
.end() ){
369 //constant equivalence classes
370 Trace("strings-subs") << " constant eqc : " << d_eqc_to_const_exp
[nr
] << " " << d_eqc_to_const_base
[nr
] << " " << nr
<< std::endl
;
371 subs
.push_back( itc
->second
);
372 if( !d_eqc_to_const_exp
[nr
].isNull() ){
373 exp
[n
].push_back( d_eqc_to_const_exp
[nr
] );
375 if( !d_eqc_to_const_base
[nr
].isNull() ){
376 addToExplanation( n
, d_eqc_to_const_base
[nr
], exp
[n
] );
378 }else if( effort
>=1 && effort
<3 && n
.getType().isString() ){
380 Node ns
= getNormalString( d_normal_forms_base
[nr
], exp
[n
] );
381 subs
.push_back( ns
);
382 Trace("strings-subs") << " normal eqc : " << ns
<< " " << d_normal_forms_base
[nr
] << " " << nr
<< std::endl
;
383 if( !d_normal_forms_base
[nr
].isNull() ) {
384 addToExplanation( n
, d_normal_forms_base
[nr
], exp
[n
] );
388 //Trace("strings-subs") << " representative : " << nr << std::endl;
389 //addToExplanation( n, nr, exp[n] );
390 //subs.push_back( nr );
398 bool TheoryStrings::doReduction(int effort
, Node n
, bool& isCd
)
400 Assert(d_extf_info_tmp
.find(n
) != d_extf_info_tmp
.end());
401 if (!d_extf_info_tmp
[n
].d_model_active
)
403 // n is not active in the model, no need to reduce
406 //determine the effort level to process the extf at
407 // 0 - at assertion time, 1+ - after no other reduction is applicable
409 // polarity : 1 true, -1 false, 0 neither
411 Kind k
= n
.getKind();
412 if (n
.getType().isBoolean() && !d_extf_info_tmp
[n
].d_const
.isNull())
414 pol
= d_extf_info_tmp
[n
].d_const
.getConst
<bool>() ? 1 : -1;
416 if (k
== STRING_STRCTN
)
428 std::vector
<Node
> lexp
;
429 Node lenx
= getLength(x
, lexp
);
430 Node lens
= getLength(s
, lexp
);
431 if (areEqual(lenx
, lens
))
433 Trace("strings-extf-debug")
434 << " resolve extf : " << n
435 << " based on equal lengths disequality." << std::endl
;
436 // We can reduce negative contains to a disequality when lengths are
437 // equal. In other words, len( x ) = len( s ) implies
438 // ~contains( x, s ) reduces to x != s.
439 if (!areDisequal(x
, s
))
441 // len( x ) = len( s ) ^ ~contains( x, s ) => x != s
442 lexp
.push_back(lenx
.eqNode(lens
));
443 lexp
.push_back(n
.negate());
444 Node xneqs
= x
.eqNode(s
).negate();
445 sendInference(lexp
, xneqs
, "NEG-CTN-EQL", true);
447 // this depends on the current assertions, so we set that this
448 // inference is context-dependent.
461 if (options::stringLazyPreproc())
463 if (k
== STRING_SUBSTR
)
467 else if (k
!= STRING_IN_REGEXP
)
473 if (effort
!= r_effort
)
475 // not the right effort level to reduce
478 Node c_n
= pol
== -1 ? n
.negate() : n
;
479 Trace("strings-process-debug")
480 << "Process reduction for " << n
<< ", pol = " << pol
<< std::endl
;
481 if (k
== STRING_STRCTN
&& pol
== 1)
485 // positive contains reduces to a equality
487 d_sk_cache
.mkSkolemCached(x
, s
, SkolemCache::SK_FIRST_CTN_PRE
, "sc1");
489 d_sk_cache
.mkSkolemCached(x
, s
, SkolemCache::SK_FIRST_CTN_POST
, "sc2");
490 Node eq
= Rewriter::rewrite(x
.eqNode(mkConcat(sk1
, s
, sk2
)));
491 std::vector
<Node
> exp_vec
;
492 exp_vec
.push_back(n
);
493 sendInference(d_empty_vec
, exp_vec
, eq
, "POS-CTN", true);
494 Trace("strings-extf-debug")
495 << " resolve extf : " << n
<< " based on positive contain reduction."
497 Trace("strings-red-lemma") << "Reduction (positive contains) lemma : " << n
498 << " => " << eq
<< std::endl
;
499 // context-dependent because it depends on the polarity of n itself
502 else if (k
!= kind::STRING_CODE
)
504 NodeManager
* nm
= NodeManager::currentNM();
505 Assert(k
== STRING_SUBSTR
|| k
== STRING_STRCTN
|| k
== STRING_STRIDOF
506 || k
== STRING_ITOS
|| k
== STRING_STOI
|| k
== STRING_STRREPL
507 || k
== STRING_STRREPLALL
|| k
== STRING_LEQ
);
508 std::vector
<Node
> new_nodes
;
509 Node res
= d_preproc
.simplify(n
, new_nodes
);
511 new_nodes
.push_back(res
.eqNode(n
));
513 new_nodes
.size() == 1 ? new_nodes
[0] : nm
->mkNode(AND
, new_nodes
);
514 nnlem
= Rewriter::rewrite(nnlem
);
515 Trace("strings-red-lemma")
516 << "Reduction_" << effort
<< " lemma : " << nnlem
<< std::endl
;
517 Trace("strings-red-lemma") << "...from " << n
<< std::endl
;
518 sendInference(d_empty_vec
, nnlem
, "Reduction", true);
519 Trace("strings-extf-debug")
520 << " resolve extf : " << n
<< " based on reduction." << std::endl
;
526 /////////////////////////////////////////////////////////////////////////////
528 /////////////////////////////////////////////////////////////////////////////
531 void TheoryStrings::presolve() {
532 Debug("strings-presolve") << "TheoryStrings::Presolving : get fmf options " << (options::stringFMF() ? "true" : "false") << std::endl
;
533 initializeStrategy();
535 // if strings fmf is enabled, register the strategy
536 if (options::stringFMF())
538 d_sslds
.reset(new StringSumLengthDecisionStrategy(
539 getSatContext(), getUserContext(), d_valuation
));
540 Trace("strings-dstrat-reg")
541 << "presolve: register decision strategy." << std::endl
;
542 std::vector
<Node
> inputVars
;
543 for (NodeSet::const_iterator itr
= d_input_vars
.begin();
544 itr
!= d_input_vars
.end();
547 inputVars
.push_back(*itr
);
549 d_sslds
->initialize(inputVars
);
550 getDecisionManager()->registerStrategy(
551 DecisionManager::STRAT_STRINGS_SUM_LENGTHS
, d_sslds
.get());
556 /////////////////////////////////////////////////////////////////////////////
558 /////////////////////////////////////////////////////////////////////////////
560 bool TheoryStrings::collectModelInfo(TheoryModel
* m
)
562 Trace("strings-model") << "TheoryStrings : Collect model info" << std::endl
;
563 Trace("strings-model") << "TheoryStrings : assertEqualityEngine." << std::endl
;
565 std::set
<Node
> termSet
;
567 // Compute terms appearing in assertions and shared terms
568 computeRelevantTerms(termSet
);
569 // assert the (relevant) portion of the equality engine to the model
570 if (!m
->assertEqualityEngine(&d_equalityEngine
, &termSet
))
575 std::unordered_set
<Node
, NodeHashFunction
> repSet
;
576 NodeManager
* nm
= NodeManager::currentNM();
578 // get the relevant string equivalence classes
579 for (const Node
& s
: termSet
)
581 if (s
.getType().isString())
583 Node r
= getRepresentative(s
);
587 std::vector
<Node
> nodes(repSet
.begin(), repSet
.end());
588 std::map
< Node
, Node
> processed
;
589 std::vector
< std::vector
< Node
> > col
;
590 std::vector
< Node
> lts
;
591 separateByLength( nodes
, col
, lts
);
592 //step 1 : get all values for known lengths
593 std::vector
< Node
> lts_values
;
594 std::map
<unsigned, Node
> values_used
;
595 std::vector
<Node
> len_splits
;
596 for( unsigned i
=0; i
<col
.size(); i
++ ) {
597 Trace("strings-model") << "Checking length for {";
598 for( unsigned j
=0; j
<col
[i
].size(); j
++ ) {
600 Trace("strings-model") << ", ";
602 Trace("strings-model") << col
[i
][j
];
604 Trace("strings-model") << " } (length is " << lts
[i
] << ")" << std::endl
;
606 if( lts
[i
].isConst() ) {
609 else if (!lts
[i
].isNull())
611 // get the model value for lts[i]
612 len_value
= d_valuation
.getModelValue(lts
[i
]);
614 if (len_value
.isNull())
616 lts_values
.push_back(Node::null());
620 Assert(len_value
.getConst
<Rational
>() <= Rational(String::maxSize()),
621 "Exceeded UINT32_MAX in string model");
623 len_value
.getConst
<Rational
>().getNumerator().toUnsignedInt();
624 std::map
<unsigned, Node
>::iterator itvu
= values_used
.find(lvalue
);
625 if (itvu
== values_used
.end())
627 values_used
[lvalue
] = lts
[i
];
631 len_splits
.push_back(lts
[i
].eqNode(itvu
->second
));
633 lts_values
.push_back(len_value
);
636 ////step 2 : assign arbitrary values for unknown lengths?
637 // confirmed by calculus invariant, see paper
638 Trace("strings-model") << "Assign to equivalence classes..." << std::endl
;
639 std::map
<Node
, Node
> pure_eq_assign
;
640 //step 3 : assign values to equivalence classes that are pure variables
641 for( unsigned i
=0; i
<col
.size(); i
++ ){
642 std::vector
< Node
> pure_eq
;
643 Trace("strings-model") << "The (" << col
[i
].size()
644 << ") equivalence classes ";
645 for (const Node
& eqc
: col
[i
])
647 Trace("strings-model") << eqc
<< " ";
648 //check if col[i][j] has only variables
651 Assert(d_normal_forms
.find(eqc
) != d_normal_forms
.end());
652 if (d_normal_forms
[eqc
].size() == 1)
654 // does it have a code and the length of these equivalence classes are
656 if (d_has_str_code
&& lts_values
[i
] == d_one
)
658 EqcInfo
* eip
= getOrMakeEqcInfo(eqc
, false);
659 if (eip
&& !eip
->d_code_term
.get().isNull())
661 // its value must be equal to its code
662 Node ct
= nm
->mkNode(kind::STRING_CODE
, eip
->d_code_term
.get());
663 Node ctv
= d_valuation
.getModelValue(ct
);
665 ctv
.getConst
<Rational
>().getNumerator().toUnsignedInt();
666 Trace("strings-model") << "(code: " << cvalue
<< ") ";
667 std::vector
<unsigned> vec
;
668 vec
.push_back(String::convertCodeToUnsignedInt(cvalue
));
669 Node mv
= nm
->mkConst(String(vec
));
670 pure_eq_assign
[eqc
] = mv
;
671 m
->getEqualityEngine()->addTerm(mv
);
674 pure_eq
.push_back(eqc
);
679 processed
[eqc
] = eqc
;
682 Trace("strings-model") << "have length " << lts_values
[i
] << std::endl
;
684 //assign a new length if necessary
685 if( !pure_eq
.empty() ){
686 if( lts_values
[i
].isNull() ){
687 // start with length two (other lengths have special precendence)
689 while( values_used
.find( lvalue
)!=values_used
.end() ){
692 Trace("strings-model") << "*** Decide to make length of " << lvalue
<< std::endl
;
693 lts_values
[i
] = nm
->mkConst(Rational(lvalue
));
694 values_used
[lvalue
] = Node::null();
696 Trace("strings-model") << "Need to assign values of length " << lts_values
[i
] << " to equivalence classes ";
697 for( unsigned j
=0; j
<pure_eq
.size(); j
++ ){
698 Trace("strings-model") << pure_eq
[j
] << " ";
700 Trace("strings-model") << std::endl
;
702 //use type enumerator
703 Assert(lts_values
[i
].getConst
<Rational
>() <= Rational(String::maxSize()),
704 "Exceeded UINT32_MAX in string model");
705 StringEnumeratorLength
sel(lts_values
[i
].getConst
<Rational
>().getNumerator().toUnsignedInt());
706 for (const Node
& eqc
: pure_eq
)
709 std::map
<Node
, Node
>::iterator itp
= pure_eq_assign
.find(eqc
);
710 if (itp
== pure_eq_assign
.end())
712 Assert( !sel
.isFinished() );
714 while (m
->hasTerm(c
))
717 if (sel
.isFinished())
719 // We are in a case where model construction is impossible due to
720 // an insufficient number of constants of a given length.
722 // Consider an integer equivalence class E whose value is assigned
723 // n in the model. Let { S_1, ..., S_m } be the set of string
724 // equivalence classes such that len( x ) is a member of E for
725 // some member x of each class S1, ...,Sm. Since our calculus is
726 // saturated with respect to cardinality inference (see Liang
727 // et al, Figure 6, CAV 2014), we have that m <= A^n, where A is
728 // the cardinality of our alphabet.
730 // Now, consider the case where there exists two integer
731 // equivalence classes E1 and E2 that are assigned n, and moreover
732 // we did not received notification from arithmetic that E1 = E2.
733 // This typically should never happen, but assume in the following
736 // Now, it may be the case that there are string equivalence
737 // classes { S_1, ..., S_m1 } whose lengths are in E1,
738 // and classes { S'_1, ..., S'_m2 } whose lengths are in E2, where
739 // m1 + m2 > A^n. In this case, we have insufficient strings to
740 // assign to { S_1, ..., S_m1, S'_1, ..., S'_m2 }. If this
741 // happens, we add a split on len( u1 ) = len( u2 ) for some
742 // len( u1 ) in E1, len( u2 ) in E2. We do this for each pair of
743 // integer equivalence classes that are assigned to the same value
745 AlwaysAssert(!len_splits
.empty());
746 for (const Node
& sl
: len_splits
)
748 Node spl
= nm
->mkNode(OR
, sl
, sl
.negate());
761 Trace("strings-model") << "*** Assigned constant " << c
<< " for "
764 if (!m
->assertEquality(eqc
, c
, true))
771 Trace("strings-model") << "String Model : Pure Assigned." << std::endl
;
772 //step 4 : assign constants to all other equivalence classes
773 for( unsigned i
=0; i
<nodes
.size(); i
++ ){
774 if( processed
.find( nodes
[i
] )==processed
.end() ){
775 Assert( d_normal_forms
.find( nodes
[i
] )!=d_normal_forms
.end() );
776 Trace("strings-model") << "Construct model for " << nodes
[i
] << " based on normal form ";
777 for( unsigned j
=0; j
<d_normal_forms
[nodes
[i
]].size(); j
++ ) {
778 if( j
>0 ) Trace("strings-model") << " ++ ";
779 Trace("strings-model") << d_normal_forms
[nodes
[i
]][j
];
780 Node r
= getRepresentative( d_normal_forms
[nodes
[i
]][j
] );
781 if( !r
.isConst() && processed
.find( r
)==processed
.end() ){
782 Trace("strings-model") << "(UNPROCESSED)";
785 Trace("strings-model") << std::endl
;
786 std::vector
< Node
> nc
;
787 for( unsigned j
=0; j
<d_normal_forms
[nodes
[i
]].size(); j
++ ) {
788 Node r
= getRepresentative( d_normal_forms
[nodes
[i
]][j
] );
789 Assert( r
.isConst() || processed
.find( r
)!=processed
.end() );
790 nc
.push_back(r
.isConst() ? r
: processed
[r
]);
792 Node cc
= mkConcat( nc
);
793 Assert( cc
.getKind()==kind::CONST_STRING
);
794 Trace("strings-model") << "*** Determined constant " << cc
<< " for " << nodes
[i
] << std::endl
;
795 processed
[nodes
[i
]] = cc
;
796 if (!m
->assertEquality(nodes
[i
], cc
, true))
802 //Trace("strings-model") << "String Model : Assigned." << std::endl;
803 Trace("strings-model") << "String Model : Finished." << std::endl
;
807 /////////////////////////////////////////////////////////////////////////////
809 /////////////////////////////////////////////////////////////////////////////
812 void TheoryStrings::preRegisterTerm(TNode n
) {
813 if( d_pregistered_terms_cache
.find(n
) == d_pregistered_terms_cache
.end() ) {
814 d_pregistered_terms_cache
.insert(n
);
815 Trace("strings-preregister")
816 << "TheoryString::preregister : " << n
<< std::endl
;
817 //check for logic exceptions
818 Kind k
= n
.getKind();
819 if( !options::stringExp() ){
820 if (k
== kind::STRING_STRIDOF
|| k
== kind::STRING_ITOS
821 || k
== kind::STRING_STOI
|| k
== kind::STRING_STRREPL
822 || k
== kind::STRING_STRREPLALL
|| k
== kind::STRING_STRCTN
825 std::stringstream ss
;
826 ss
<< "Term of kind " << k
827 << " not supported in default mode, try --strings-exp";
828 throw LogicException(ss
.str());
834 d_equalityEngine
.addTriggerEquality(n
);
837 case kind::STRING_IN_REGEXP
: {
838 d_out
->requirePhase(n
, true);
839 d_equalityEngine
.addTriggerPredicate(n
);
840 d_equalityEngine
.addTerm(n
[0]);
841 d_equalityEngine
.addTerm(n
[1]);
846 TypeNode tn
= n
.getType();
847 if (tn
.isRegExp() && n
.isVar())
849 std::stringstream ss
;
850 ss
<< "Regular expression variables are not supported.";
851 throw LogicException(ss
.str());
853 if( tn
.isString() ) {
854 // all characters of constants should fall in the alphabet
857 std::vector
<unsigned> vec
= n
.getConst
<String
>().getVec();
858 for (unsigned u
: vec
)
860 if (u
>= d_card_size
)
862 std::stringstream ss
;
863 ss
<< "Characters in string \"" << n
864 << "\" are outside of the given alphabet.";
865 throw LogicException(ss
.str());
869 // if finite model finding is enabled,
870 // then we minimize the length of this term if it is a variable
871 // but not an internally generated Skolem, or a term that does
872 // not belong to this theory.
873 if (options::stringFMF()
874 && (n
.isVar() ? !d_sk_cache
.isSkolem(n
)
875 : kindToTheoryId(k
) != THEORY_STRINGS
))
877 d_input_vars
.insert(n
);
878 Trace("strings-dstrat-reg") << "input variable: " << n
<< std::endl
;
880 d_equalityEngine
.addTerm(n
);
881 } else if (tn
.isBoolean()) {
882 // Get triggered for both equal and dis-equal
883 d_equalityEngine
.addTriggerPredicate(n
);
885 // Function applications/predicates
886 d_equalityEngine
.addTerm(n
);
888 // Set d_functionsTerms stores all function applications that are
889 // relevant to theory combination. Notice that this is a subset of
890 // the applications whose kinds are function kinds in the equality
891 // engine. This means it does not include applications of operators
892 // like re.++, which is not a function kind in the equality engine.
893 // Concatenation terms do not need to be considered here because
894 // their arguments have string type and do not introduce any shared
896 if (n
.hasOperator() && d_equalityEngine
.isFunctionKind(k
)
897 && k
!= kind::STRING_CONCAT
)
899 d_functionsTerms
.push_back( n
);
906 Node
TheoryStrings::expandDefinition(LogicRequest
&logicRequest
, Node node
) {
907 Trace("strings-exp-def") << "TheoryStrings::expandDefinition : " << node
<< std::endl
;
911 void TheoryStrings::check(Effort e
) {
912 if (done() && e
<EFFORT_FULL
) {
916 TimerStat::CodeTimer
checkTimer(d_checkTime
);
921 if( !done() && !hasTerm( d_emptyString
) ) {
922 preRegisterTerm( d_emptyString
);
925 // Trace("strings-process") << "Theory of strings, check : " << e << std::endl;
926 Trace("strings-check") << "Theory of strings, check : " << e
<< std::endl
;
927 while ( !done() && !d_conflict
) {
928 // Get all the assertions
929 Assertion assertion
= get();
930 TNode fact
= assertion
.assertion
;
932 Trace("strings-assertion") << "get assertion: " << fact
<< endl
;
933 polarity
= fact
.getKind() != kind::NOT
;
934 atom
= polarity
? fact
: fact
[0];
936 //assert pending fact
937 assertPendingFact( atom
, polarity
, fact
);
941 Assert(d_strategy_init
);
942 std::map
<Effort
, std::pair
<unsigned, unsigned> >::iterator itsr
=
943 d_strat_steps
.find(e
);
944 if (!d_conflict
&& !d_valuation
.needCheck() && itsr
!= d_strat_steps
.end())
946 Trace("strings-check") << "Theory of strings " << e
<< " effort check "
948 if(Trace
.isOn("strings-eqc")) {
949 for( unsigned t
=0; t
<2; t
++ ) {
950 eq::EqClassesIterator eqcs2_i
= eq::EqClassesIterator( &d_equalityEngine
);
951 Trace("strings-eqc") << (t
==0 ? "STRINGS:" : "OTHER:") << std::endl
;
952 while( !eqcs2_i
.isFinished() ){
953 Node eqc
= (*eqcs2_i
);
954 bool print
= (t
==0 && eqc
.getType().isString() ) || (t
==1 && !eqc
.getType().isString() );
956 eq::EqClassIterator eqc2_i
= eq::EqClassIterator( eqc
, &d_equalityEngine
);
957 Trace("strings-eqc") << "Eqc( " << eqc
<< " ) : { ";
958 while( !eqc2_i
.isFinished() ) {
959 if( (*eqc2_i
)!=eqc
&& (*eqc2_i
).getKind()!=kind::EQUAL
){
960 Trace("strings-eqc") << (*eqc2_i
) << " ";
964 Trace("strings-eqc") << " } " << std::endl
;
965 EqcInfo
* ei
= getOrMakeEqcInfo( eqc
, false );
967 Trace("strings-eqc-debug") << "* Length term : " << ei
->d_length_term
.get() << std::endl
;
968 Trace("strings-eqc-debug") << "* Cardinality lemma k : " << ei
->d_cardinality_lem_k
.get() << std::endl
;
969 Trace("strings-eqc-debug") << "* Normalization length lemma : " << ei
->d_normalized_length
.get() << std::endl
;
974 Trace("strings-eqc") << std::endl
;
976 Trace("strings-eqc") << std::endl
;
978 unsigned sbegin
= itsr
->second
.first
;
979 unsigned send
= itsr
->second
.second
;
980 bool addedLemma
= false;
983 runStrategy(sbegin
, send
);
985 addedFact
= !d_pending
.empty();
986 addedLemma
= !d_lemma_cache
.empty();
989 // repeat if we did not add a lemma or conflict
990 }while( !d_conflict
&& !addedLemma
&& addedFact
);
992 Trace("strings-check") << "Theory of strings done full effort check " << addedLemma
<< " " << d_conflict
<< std::endl
;
994 Trace("strings-check") << "Theory of strings, done check : " << e
<< std::endl
;
995 Assert( d_pending
.empty() );
996 Assert( d_lemma_cache
.empty() );
999 bool TheoryStrings::needsCheckLastEffort() {
1000 if( options::stringGuessModel() ){
1001 return d_has_extf
.get();
1007 void TheoryStrings::checkExtfReductions( int effort
) {
1008 // Notice we don't make a standard call to ExtTheory::doReductions here,
1009 // since certain optimizations like context-dependent reductions and
1010 // stratifying effort levels are done in doReduction below.
1011 std::vector
< Node
> extf
= getExtTheory()->getActive();
1012 Trace("strings-process") << " checking " << extf
.size() << " active extf"
1014 for( unsigned i
=0; i
<extf
.size(); i
++ ){
1015 Assert(!d_conflict
);
1017 Trace("strings-process") << " check " << n
<< ", active in model="
1018 << d_extf_info_tmp
[n
].d_model_active
<< std::endl
;
1019 // whether the reduction was context-dependent
1021 bool ret
= doReduction(effort
, n
, isCd
);
1024 getExtTheory()->markReduced(extf
[i
], isCd
);
1033 TheoryStrings::EqcInfo::EqcInfo(context::Context
* c
)
1036 d_cardinality_lem_k(c
),
1037 d_normalized_length(c
)
1041 TheoryStrings::EqcInfo
* TheoryStrings::getOrMakeEqcInfo( Node eqc
, bool doMake
) {
1042 std::map
< Node
, EqcInfo
* >::iterator eqc_i
= d_eqc_info
.find( eqc
);
1043 if( eqc_i
==d_eqc_info
.end() ){
1045 EqcInfo
* ei
= new EqcInfo( getSatContext() );
1046 d_eqc_info
[eqc
] = ei
;
1052 return (*eqc_i
).second
;
1057 /** Conflict when merging two constants */
1058 void TheoryStrings::conflict(TNode a
, TNode b
){
1060 Debug("strings-conflict") << "Making conflict..." << std::endl
;
1063 conflictNode
= explain( a
.eqNode(b
) );
1064 Trace("strings-conflict") << "CONFLICT: Eq engine conflict : " << conflictNode
<< std::endl
;
1065 d_out
->conflict( conflictNode
);
1069 /** called when a new equivalance class is created */
1070 void TheoryStrings::eqNotifyNewClass(TNode t
){
1071 Kind k
= t
.getKind();
1072 if (k
== kind::STRING_LENGTH
|| k
== kind::STRING_CODE
)
1074 Trace("strings-debug") << "New length eqc : " << t
<< std::endl
;
1075 Node r
= d_equalityEngine
.getRepresentative(t
[0]);
1076 EqcInfo
* ei
= getOrMakeEqcInfo( r
, true );
1077 if (k
== kind::STRING_LENGTH
)
1079 ei
->d_length_term
= t
[0];
1083 ei
->d_code_term
= t
[0];
1085 //we care about the length of this string
1086 registerTerm( t
[0], 1 );
1088 //getExtTheory()->registerTerm( t );
1092 /** called when two equivalance classes will merge */
1093 void TheoryStrings::eqNotifyPreMerge(TNode t1
, TNode t2
){
1094 EqcInfo
* e2
= getOrMakeEqcInfo(t2
, false);
1096 EqcInfo
* e1
= getOrMakeEqcInfo( t1
);
1097 //add information from e2 to e1
1098 if( !e2
->d_length_term
.get().isNull() ){
1099 e1
->d_length_term
.set( e2
->d_length_term
);
1101 if (!e2
->d_code_term
.get().isNull())
1103 e1
->d_code_term
.set(e2
->d_code_term
);
1105 if( e2
->d_cardinality_lem_k
.get()>e1
->d_cardinality_lem_k
.get() ) {
1106 e1
->d_cardinality_lem_k
.set( e2
->d_cardinality_lem_k
);
1108 if( !e2
->d_normalized_length
.get().isNull() ){
1109 e1
->d_normalized_length
.set( e2
->d_normalized_length
);
1114 /** called when two equivalance classes have merged */
1115 void TheoryStrings::eqNotifyPostMerge(TNode t1
, TNode t2
) {
1119 /** called when two equivalance classes are disequal */
1120 void TheoryStrings::eqNotifyDisequal(TNode t1
, TNode t2
, TNode reason
) {
1121 if( t1
.getType().isString() ){
1122 //store disequalities between strings, may need to check if their lengths are equal/disequal
1123 d_ee_disequalities
.push_back( t1
.eqNode( t2
) );
1127 void TheoryStrings::addCarePairs(TNodeTrie
* t1
,
1134 Node f1
= t1
->getData();
1135 Node f2
= t2
->getData();
1136 if( !d_equalityEngine
.areEqual( f1
, f2
) ){
1137 Trace("strings-cg-debug") << "TheoryStrings::computeCareGraph(): checking function " << f1
<< " and " << f2
<< std::endl
;
1138 vector
< pair
<TNode
, TNode
> > currentPairs
;
1139 for (unsigned k
= 0; k
< f1
.getNumChildren(); ++ k
) {
1142 Assert( d_equalityEngine
.hasTerm(x
) );
1143 Assert( d_equalityEngine
.hasTerm(y
) );
1144 Assert( !d_equalityEngine
.areDisequal( x
, y
, false ) );
1145 Assert( !areCareDisequal( x
, y
) );
1146 if( !d_equalityEngine
.areEqual( x
, y
) ){
1147 if( d_equalityEngine
.isTriggerTerm(x
, THEORY_STRINGS
) && d_equalityEngine
.isTriggerTerm(y
, THEORY_STRINGS
) ){
1148 TNode x_shared
= d_equalityEngine
.getTriggerTermRepresentative(x
, THEORY_STRINGS
);
1149 TNode y_shared
= d_equalityEngine
.getTriggerTermRepresentative(y
, THEORY_STRINGS
);
1150 currentPairs
.push_back(make_pair(x_shared
, y_shared
));
1154 for (unsigned c
= 0; c
< currentPairs
.size(); ++ c
) {
1155 Trace("strings-cg-pair") << "TheoryStrings::computeCareGraph(): pair : " << currentPairs
[c
].first
<< " " << currentPairs
[c
].second
<< std::endl
;
1156 addCarePair(currentPairs
[c
].first
, currentPairs
[c
].second
);
1162 if( depth
<(arity
-1) ){
1163 //add care pairs internal to each child
1164 for (std::pair
<const TNode
, TNodeTrie
>& tt
: t1
->d_data
)
1166 addCarePairs(&tt
.second
, nullptr, arity
, depth
+ 1);
1169 //add care pairs based on each pair of non-disequal arguments
1170 for (std::map
<TNode
, TNodeTrie
>::iterator it
= t1
->d_data
.begin();
1171 it
!= t1
->d_data
.end();
1174 std::map
<TNode
, TNodeTrie
>::iterator it2
= it
;
1176 for( ; it2
!= t1
->d_data
.end(); ++it2
){
1177 if( !d_equalityEngine
.areDisequal(it
->first
, it2
->first
, false) ){
1178 if( !areCareDisequal(it
->first
, it2
->first
) ){
1179 addCarePairs( &it
->second
, &it2
->second
, arity
, depth
+1 );
1185 //add care pairs based on product of indices, non-disequal arguments
1186 for (std::pair
<const TNode
, TNodeTrie
>& tt1
: t1
->d_data
)
1188 for (std::pair
<const TNode
, TNodeTrie
>& tt2
: t2
->d_data
)
1190 if (!d_equalityEngine
.areDisequal(tt1
.first
, tt2
.first
, false))
1192 if (!areCareDisequal(tt1
.first
, tt2
.first
))
1194 addCarePairs(&tt1
.second
, &tt2
.second
, arity
, depth
+ 1);
1203 void TheoryStrings::computeCareGraph(){
1204 //computing the care graph here is probably still necessary, due to operators that take non-string arguments TODO: verify
1205 Trace("strings-cg") << "TheoryStrings::computeCareGraph(): Build term indices..." << std::endl
;
1206 std::map
<Node
, TNodeTrie
> index
;
1207 std::map
< Node
, unsigned > arity
;
1208 unsigned functionTerms
= d_functionsTerms
.size();
1209 for (unsigned i
= 0; i
< functionTerms
; ++ i
) {
1210 TNode f1
= d_functionsTerms
[i
];
1211 Trace("strings-cg") << "...build for " << f1
<< std::endl
;
1212 Node op
= f1
.getOperator();
1213 std::vector
< TNode
> reps
;
1214 bool has_trigger_arg
= false;
1215 for( unsigned j
=0; j
<f1
.getNumChildren(); j
++ ){
1216 reps
.push_back( d_equalityEngine
.getRepresentative( f1
[j
] ) );
1217 if( d_equalityEngine
.isTriggerTerm( f1
[j
], THEORY_STRINGS
) ){
1218 has_trigger_arg
= true;
1221 if( has_trigger_arg
){
1222 index
[op
].addTerm( f1
, reps
);
1223 arity
[op
] = reps
.size();
1227 for (std::pair
<const Node
, TNodeTrie
>& tt
: index
)
1229 Trace("strings-cg") << "TheoryStrings::computeCareGraph(): Process index "
1230 << tt
.first
<< "..." << std::endl
;
1231 addCarePairs(&tt
.second
, nullptr, arity
[tt
.first
], 0);
1235 void TheoryStrings::assertPendingFact(Node atom
, bool polarity
, Node exp
) {
1236 Trace("strings-pending") << "Assert pending fact : " << atom
<< " " << polarity
<< " from " << exp
<< std::endl
;
1237 Assert(atom
.getKind() != kind::OR
, "Infer error: a split.");
1238 if( atom
.getKind()==kind::EQUAL
){
1239 Trace("strings-pending-debug") << " Register term" << std::endl
;
1240 for( unsigned j
=0; j
<2; j
++ ) {
1241 if( !d_equalityEngine
.hasTerm( atom
[j
] ) && atom
[j
].getType().isString() ) {
1242 registerTerm( atom
[j
], 0 );
1245 Trace("strings-pending-debug") << " Now assert equality" << std::endl
;
1246 d_equalityEngine
.assertEquality( atom
, polarity
, exp
);
1247 Trace("strings-pending-debug") << " Finished assert equality" << std::endl
;
1249 d_equalityEngine
.assertPredicate( atom
, polarity
, exp
);
1251 if( atom
.getKind()==kind::STRING_IN_REGEXP
){
1252 if( polarity
&& atom
[1].getKind()==kind::REGEXP_RANGE
){
1253 if( d_extf_infer_cache_u
.find( atom
)==d_extf_infer_cache_u
.end() ){
1254 d_extf_infer_cache_u
.insert( atom
);
1255 //length of first argument is one
1256 Node conc
= d_one
.eqNode( NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, atom
[0] ) );
1257 Node lem
= NodeManager::currentNM()->mkNode( kind::OR
, atom
.negate(), conc
);
1258 Trace("strings-lemma") << "Strings::Lemma RE-Range-Len : " << lem
<< std::endl
;
1259 d_out
->lemma( lem
);
1263 //register the atom here, since it may not create a new equivalence class
1264 //getExtTheory()->registerTerm( atom );
1266 Trace("strings-pending-debug") << " Now collect terms" << std::endl
;
1267 // Collect extended function terms in the atom. Notice that we must register
1268 // all extended functions occurring in assertions and shared terms. We
1269 // make a similar call to registerTermRec in addSharedTerm.
1270 getExtTheory()->registerTermRec( atom
);
1271 Trace("strings-pending-debug") << " Finished collect terms" << std::endl
;
1274 void TheoryStrings::doPendingFacts() {
1276 while( !d_conflict
&& i
<d_pending
.size() ) {
1277 Node fact
= d_pending
[i
];
1278 Node exp
= d_pending_exp
[ fact
];
1279 if(fact
.getKind() == kind::AND
) {
1280 for(size_t j
=0; j
<fact
.getNumChildren(); j
++) {
1281 bool polarity
= fact
[j
].getKind() != kind::NOT
;
1282 TNode atom
= polarity
? fact
[j
] : fact
[j
][0];
1283 assertPendingFact(atom
, polarity
, exp
);
1286 bool polarity
= fact
.getKind() != kind::NOT
;
1287 TNode atom
= polarity
? fact
: fact
[0];
1288 assertPendingFact(atom
, polarity
, exp
);
1293 d_pending_exp
.clear();
1296 void TheoryStrings::doPendingLemmas() {
1297 if( !d_conflict
&& !d_lemma_cache
.empty() ){
1298 for( unsigned i
=0; i
<d_lemma_cache
.size(); i
++ ){
1299 Trace("strings-pending") << "Process pending lemma : " << d_lemma_cache
[i
] << std::endl
;
1300 d_out
->lemma( d_lemma_cache
[i
] );
1302 for( std::map
< Node
, bool >::iterator it
= d_pending_req_phase
.begin(); it
!= d_pending_req_phase
.end(); ++it
){
1303 Trace("strings-pending") << "Require phase : " << it
->first
<< ", polarity = " << it
->second
<< std::endl
;
1304 d_out
->requirePhase( it
->first
, it
->second
);
1307 d_lemma_cache
.clear();
1308 d_pending_req_phase
.clear();
1311 bool TheoryStrings::hasProcessed() {
1312 return d_conflict
|| !d_lemma_cache
.empty() || !d_pending
.empty();
1315 void TheoryStrings::addToExplanation( Node a
, Node b
, std::vector
< Node
>& exp
) {
1317 Debug("strings-explain") << "Add to explanation : " << a
<< " == " << b
<< std::endl
;
1318 Assert( areEqual( a
, b
) );
1319 exp
.push_back( a
.eqNode( b
) );
1323 void TheoryStrings::addToExplanation( Node lit
, std::vector
< Node
>& exp
) {
1324 if( !lit
.isNull() ){
1325 exp
.push_back( lit
);
1329 void TheoryStrings::checkInit() {
1331 d_eqc_to_const
.clear();
1332 d_eqc_to_const_base
.clear();
1333 d_eqc_to_const_exp
.clear();
1334 d_eqc_to_len_term
.clear();
1335 d_term_index
.clear();
1336 d_strings_eqc
.clear();
1338 std::map
< Kind
, unsigned > ncongruent
;
1339 std::map
< Kind
, unsigned > congruent
;
1340 d_emptyString_r
= getRepresentative( d_emptyString
);
1341 eq::EqClassesIterator eqcs_i
= eq::EqClassesIterator( &d_equalityEngine
);
1342 while( !eqcs_i
.isFinished() ){
1343 Node eqc
= (*eqcs_i
);
1344 TypeNode tn
= eqc
.getType();
1345 if( !tn
.isRegExp() ){
1346 if( tn
.isString() ){
1347 d_strings_eqc
.push_back( eqc
);
1350 eq::EqClassIterator eqc_i
= eq::EqClassIterator( eqc
, &d_equalityEngine
);
1351 while( !eqc_i
.isFinished() ) {
1354 d_eqc_to_const
[eqc
] = n
;
1355 d_eqc_to_const_base
[eqc
] = n
;
1356 d_eqc_to_const_exp
[eqc
] = Node::null();
1357 }else if( tn
.isInteger() ){
1358 if( n
.getKind()==kind::STRING_LENGTH
){
1359 Node nr
= getRepresentative( n
[0] );
1360 d_eqc_to_len_term
[nr
] = n
[0];
1362 }else if( n
.getNumChildren()>0 ){
1363 Kind k
= n
.getKind();
1364 if( k
!=kind::EQUAL
){
1365 if( d_congruent
.find( n
)==d_congruent
.end() ){
1366 std::vector
< Node
> c
;
1367 Node nc
= d_term_index
[k
].add( n
, 0, this, d_emptyString_r
, c
);
1369 //check if we have inferred a new equality by removal of empty components
1370 if( n
.getKind()==kind::STRING_CONCAT
&& !areEqual( nc
, n
) ){
1371 std::vector
< Node
> exp
;
1372 unsigned count
[2] = { 0, 0 };
1373 while( count
[0]<nc
.getNumChildren() || count
[1]<n
.getNumChildren() ){
1374 //explain empty prefixes
1375 for( unsigned t
=0; t
<2; t
++ ){
1376 Node nn
= t
==0 ? nc
: n
;
1377 while( count
[t
]<nn
.getNumChildren() &&
1378 ( nn
[count
[t
]]==d_emptyString
|| areEqual( nn
[count
[t
]], d_emptyString
) ) ){
1379 if( nn
[count
[t
]]!=d_emptyString
){
1380 exp
.push_back( nn
[count
[t
]].eqNode( d_emptyString
) );
1385 //explain equal components
1386 if( count
[0]<nc
.getNumChildren() ){
1387 Assert( count
[1]<n
.getNumChildren() );
1388 if( nc
[count
[0]]!=n
[count
[1]] ){
1389 exp
.push_back( nc
[count
[0]].eqNode( n
[count
[1]] ) );
1395 //infer the equality
1396 sendInference( exp
, n
.eqNode( nc
), "I_Norm" );
1397 }else if( getExtTheory()->hasFunctionKind( n
.getKind() ) ){
1398 //mark as congruent : only process if neither has been reduced
1399 getExtTheory()->markCongruent( nc
, n
);
1401 //this node is congruent to another one, we can ignore it
1402 Trace("strings-process-debug") << " congruent term : " << n
<< std::endl
;
1403 d_congruent
.insert( n
);
1405 }else if( k
==kind::STRING_CONCAT
&& c
.size()==1 ){
1406 Trace("strings-process-debug") << " congruent term by singular : " << n
<< " " << c
[0] << std::endl
;
1408 if( !areEqual( c
[0], n
) ){
1409 std::vector
< Node
> exp
;
1410 //explain empty components
1411 bool foundNEmpty
= false;
1412 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
1413 if( areEqual( n
[i
], d_emptyString
) ){
1414 if( n
[i
]!=d_emptyString
){
1415 exp
.push_back( n
[i
].eqNode( d_emptyString
) );
1418 Assert( !foundNEmpty
);
1420 exp
.push_back( n
[i
].eqNode( c
[0] ) );
1425 AlwaysAssert( foundNEmpty
);
1426 //infer the equality
1427 sendInference( exp
, n
.eqNode( c
[0] ), "I_Norm_S" );
1429 d_congruent
.insert( n
);
1439 if( d_congruent
.find( n
)==d_congruent
.end() ){
1443 Trace("strings-process-debug") << " congruent variable : " << n
<< std::endl
;
1444 d_congruent
.insert( n
);
1453 if( Trace
.isOn("strings-process") ){
1454 for( std::map
< Kind
, TermIndex
>::iterator it
= d_term_index
.begin(); it
!= d_term_index
.end(); ++it
){
1455 Trace("strings-process") << " Terms[" << it
->first
<< "] = " << ncongruent
[it
->first
] << "/" << (congruent
[it
->first
]+ncongruent
[it
->first
]) << std::endl
;
1460 void TheoryStrings::checkConstantEquivalenceClasses()
1464 std::vector
<Node
> vecc
;
1468 Trace("strings-process-debug") << "Check constant equivalence classes..."
1470 prevSize
= d_eqc_to_const
.size();
1471 checkConstantEquivalenceClasses(&d_term_index
[kind::STRING_CONCAT
], vecc
);
1472 } while (!hasProcessed() && d_eqc_to_const
.size() > prevSize
);
1475 void TheoryStrings::checkConstantEquivalenceClasses( TermIndex
* ti
, std::vector
< Node
>& vecc
) {
1476 Node n
= ti
->d_data
;
1478 //construct the constant
1479 Node c
= mkConcat( vecc
);
1480 if( !areEqual( n
, c
) ){
1481 Trace("strings-debug") << "Constant eqc : " << c
<< " for " << n
<< std::endl
;
1482 Trace("strings-debug") << " ";
1483 for( unsigned i
=0; i
<vecc
.size(); i
++ ){
1484 Trace("strings-debug") << vecc
[i
] << " ";
1486 Trace("strings-debug") << std::endl
;
1488 unsigned countc
= 0;
1489 std::vector
< Node
> exp
;
1490 while( count
<n
.getNumChildren() ){
1491 while( count
<n
.getNumChildren() && areEqual( n
[count
], d_emptyString
) ){
1492 addToExplanation( n
[count
], d_emptyString
, exp
);
1495 if( count
<n
.getNumChildren() ){
1496 Trace("strings-debug") << "...explain " << n
[count
] << " " << vecc
[countc
] << std::endl
;
1497 if( !areEqual( n
[count
], vecc
[countc
] ) ){
1498 Node nrr
= getRepresentative( n
[count
] );
1499 Assert( !d_eqc_to_const_exp
[nrr
].isNull() );
1500 addToExplanation( n
[count
], d_eqc_to_const_base
[nrr
], exp
);
1501 exp
.push_back( d_eqc_to_const_exp
[nrr
] );
1503 addToExplanation( n
[count
], vecc
[countc
], exp
);
1509 //exp contains an explanation of n==c
1510 Assert( countc
==vecc
.size() );
1512 sendInference( exp
, n
.eqNode( c
), "I_CONST_MERGE" );
1514 }else if( !hasProcessed() ){
1515 Node nr
= getRepresentative( n
);
1516 std::map
< Node
, Node
>::iterator it
= d_eqc_to_const
.find( nr
);
1517 if( it
==d_eqc_to_const
.end() ){
1518 Trace("strings-debug") << "Set eqc const " << n
<< " to " << c
<< std::endl
;
1519 d_eqc_to_const
[nr
] = c
;
1520 d_eqc_to_const_base
[nr
] = n
;
1521 d_eqc_to_const_exp
[nr
] = mkAnd( exp
);
1522 }else if( c
!=it
->second
){
1524 Trace("strings-debug") << "Conflict, other constant was " << it
->second
<< ", this constant was " << c
<< std::endl
;
1525 if( d_eqc_to_const_exp
[nr
].isNull() ){
1526 // n==c ^ n == c' => false
1527 addToExplanation( n
, it
->second
, exp
);
1529 // n==c ^ n == d_eqc_to_const_base[nr] == c' => false
1530 exp
.push_back( d_eqc_to_const_exp
[nr
] );
1531 addToExplanation( n
, d_eqc_to_const_base
[nr
], exp
);
1533 sendInference( exp
, d_false
, "I_CONST_CONFLICT" );
1536 Trace("strings-debug") << "Duplicate constant." << std::endl
;
1541 for( std::map
< TNode
, TermIndex
>::iterator it
= ti
->d_children
.begin(); it
!= ti
->d_children
.end(); ++it
){
1542 std::map
< Node
, Node
>::iterator itc
= d_eqc_to_const
.find( it
->first
);
1543 if( itc
!=d_eqc_to_const
.end() ){
1544 vecc
.push_back( itc
->second
);
1545 checkConstantEquivalenceClasses( &it
->second
, vecc
);
1547 if( hasProcessed() ){
1554 void TheoryStrings::checkExtfEval( int effort
) {
1555 Trace("strings-extf-list") << "Active extended functions, effort=" << effort
<< " : " << std::endl
;
1556 d_extf_info_tmp
.clear();
1557 bool has_nreduce
= false;
1558 std::vector
< Node
> terms
= getExtTheory()->getActive();
1559 std::vector
< Node
> sterms
;
1560 std::vector
< std::vector
< Node
> > exp
;
1561 getExtTheory()->getSubstitutedTerms( effort
, terms
, sterms
, exp
);
1562 for( unsigned i
=0; i
<terms
.size(); i
++ ){
1564 Node sn
= sterms
[i
];
1565 //setup information about extf
1566 ExtfInfoTmp
& einfo
= d_extf_info_tmp
[n
];
1567 Node r
= getRepresentative(n
);
1568 std::map
<Node
, Node
>::iterator itcit
= d_eqc_to_const
.find(r
);
1569 if (itcit
!= d_eqc_to_const
.end())
1571 einfo
.d_const
= itcit
->second
;
1573 Trace("strings-extf-debug") << "Check extf " << n
<< " == " << sn
1574 << ", constant = " << einfo
.d_const
1575 << ", effort=" << effort
<< "..." << std::endl
;
1579 einfo
.d_exp
.insert(einfo
.d_exp
.end(), exp
[i
].begin(), exp
[i
].end());
1580 // inference is rewriting the substituted node
1581 Node nrc
= Rewriter::rewrite( sn
);
1582 Kind nrck
= nrc
.getKind();
1583 //if rewrites to a constant, then do the inference and mark as reduced
1584 if( nrc
.isConst() ){
1586 getExtTheory()->markReduced( n
);
1587 Trace("strings-extf-debug") << " resolvable by evaluation..." << std::endl
;
1588 std::vector
< Node
> exps
;
1589 // The following optimization gets the "symbolic definition" of
1590 // an extended term. The symbolic definition of a term t is a term
1591 // t' where constants are replaced by their corresponding proxy
1593 // For example, if lsym is a proxy variable for "", then
1594 // str.replace( lsym, lsym, lsym ) is the symbolic definition for
1595 // str.replace( "", "", "" ). It is generally better to use symbolic
1596 // definitions when doing cd-rewriting for the purpose of minimizing
1597 // clauses, e.g. we infer the unit equality:
1598 // str.replace( lsym, lsym, lsym ) == ""
1599 // instead of making this inference multiple times:
1600 // x = "" => str.replace( x, x, x ) == ""
1601 // y = "" => str.replace( y, y, y ) == ""
1602 Trace("strings-extf-debug") << " get symbolic definition..." << std::endl
;
1603 Node nrs
= getSymbolicDefinition( sn
, exps
);
1604 if( !nrs
.isNull() ){
1605 Trace("strings-extf-debug") << " rewrite " << nrs
<< "..." << std::endl
;
1606 Node nrsr
= Rewriter::rewrite(nrs
);
1607 // ensure the symbolic form is not rewritable
1610 // we cannot use the symbolic definition if it rewrites
1611 Trace("strings-extf-debug") << " symbolic definition is trivial..." << std::endl
;
1615 Trace("strings-extf-debug") << " could not infer symbolic definition." << std::endl
;
1618 if( !nrs
.isNull() ){
1619 Trace("strings-extf-debug") << " symbolic def : " << nrs
<< std::endl
;
1620 if( !areEqual( nrs
, nrc
) ){
1621 //infer symbolic unit
1622 if( n
.getType().isBoolean() ){
1623 conc
= nrc
==d_true
? nrs
: nrs
.negate();
1625 conc
= nrs
.eqNode( nrc
);
1627 einfo
.d_exp
.clear();
1630 if( !areEqual( n
, nrc
) ){
1631 if( n
.getType().isBoolean() ){
1632 if( areEqual( n
, nrc
==d_true
? d_false
: d_true
) ){
1633 einfo
.d_exp
.push_back(nrc
== d_true
? n
.negate() : n
);
1636 conc
= nrc
==d_true
? n
: n
.negate();
1639 conc
= n
.eqNode( nrc
);
1643 if( !conc
.isNull() ){
1644 Trace("strings-extf") << " resolve extf : " << sn
<< " -> " << nrc
<< std::endl
;
1646 einfo
.d_exp
, conc
, effort
== 0 ? "EXTF" : "EXTF-N", true);
1648 Trace("strings-extf-debug") << " conflict, return." << std::endl
;
1653 //check if it is already equal, if so, mark as reduced. Otherwise, do nothing.
1654 if( areEqual( n
, nrc
) ){
1655 Trace("strings-extf") << " resolved extf, since satisfied by model: " << n
<< std::endl
;
1656 einfo
.d_model_active
= false;
1662 bool reduced
= false;
1663 if (!einfo
.d_const
.isNull() && nrc
.getType().isBoolean())
1665 bool pol
= einfo
.d_const
== d_true
;
1666 Node nrcAssert
= pol
? nrc
: nrc
.negate();
1667 Node nAssert
= pol
? n
: n
.negate();
1669 einfo
.d_exp
.push_back(nAssert
);
1670 Trace("strings-extf-debug") << " decomposable..." << std::endl
;
1671 Trace("strings-extf") << " resolve extf : " << sn
<< " -> " << nrc
1672 << ", const = " << einfo
.d_const
<< std::endl
;
1673 reduced
= sendInternalInference(
1674 einfo
.d_exp
, nrcAssert
, effort
== 0 ? "EXTF_d" : "EXTF_d-N");
1677 Trace("strings-extf") << "EXT: could not fully reduce ";
1678 Trace("strings-extf")
1679 << nAssert
<< " via " << nrcAssert
<< std::endl
;
1684 getExtTheory()->markReduced(n
);
1692 to_reduce
= sterms
[i
];
1695 if( !to_reduce
.isNull() ){
1698 Trace("strings-extf") << " cannot rewrite extf : " << to_reduce
<< std::endl
;
1700 checkExtfInference(n
, to_reduce
, einfo
, effort
);
1701 if( Trace
.isOn("strings-extf-list") ){
1702 Trace("strings-extf-list") << " * " << to_reduce
;
1703 if (!einfo
.d_const
.isNull())
1705 Trace("strings-extf-list") << ", const = " << einfo
.d_const
;
1708 Trace("strings-extf-list") << ", from " << n
;
1710 Trace("strings-extf-list") << std::endl
;
1712 if (getExtTheory()->isActive(n
) && einfo
.d_model_active
)
1718 d_has_extf
= has_nreduce
;
1721 void TheoryStrings::checkExtfInference( Node n
, Node nr
, ExtfInfoTmp
& in
, int effort
){
1722 if (in
.d_const
.isNull())
1726 NodeManager
* nm
= NodeManager::currentNM();
1727 Trace("strings-extf-infer") << "checkExtfInference: " << n
<< " : " << nr
1728 << " == " << in
.d_const
<< std::endl
;
1730 // add original to explanation
1731 if (n
.getType().isBoolean())
1733 // if Boolean, it's easy
1734 in
.d_exp
.push_back(in
.d_const
.getConst
<bool>() ? n
: n
.negate());
1738 // otherwise, must explain via base node
1739 Node r
= getRepresentative(n
);
1741 // d_eqc_to_const_exp[r] => d_eqc_to_const_base[r] = in.d_const
1743 // n = d_eqc_to_const_base[r] ^ d_eqc_to_const_exp[r] => n = in.d_const
1744 Assert(d_eqc_to_const_base
.find(r
) != d_eqc_to_const_base
.end());
1745 addToExplanation(n
, d_eqc_to_const_base
[r
], in
.d_exp
);
1746 Assert(d_eqc_to_const_exp
.find(r
) != d_eqc_to_const_exp
.end());
1747 in
.d_exp
.insert(in
.d_exp
.end(),
1748 d_eqc_to_const_exp
[r
].begin(),
1749 d_eqc_to_const_exp
[r
].end());
1752 // d_extf_infer_cache stores whether we have made the inferences associated
1754 // this may need to be generalized if multiple inferences apply
1756 if (nr
.getKind() == STRING_STRCTN
)
1758 Assert(in
.d_const
.isConst());
1759 bool pol
= in
.d_const
.getConst
<bool>();
1760 if ((pol
&& nr
[1].getKind() == STRING_CONCAT
)
1761 || (!pol
&& nr
[0].getKind() == STRING_CONCAT
))
1763 // If str.contains( x, str.++( y1, ..., yn ) ),
1764 // we may infer str.contains( x, y1 ), ..., str.contains( x, yn )
1765 // The following recognizes two situations related to the above reasoning:
1766 // (1) If ~str.contains( x, yi ) holds for some i, we are in conflict,
1767 // (2) If str.contains( x, yj ) already holds for some j, then the term
1768 // str.contains( x, yj ) is irrelevant since it is satisfied by all models
1769 // for str.contains( x, str.++( y1, ..., yn ) ).
1771 // Notice that the dual of the above reasoning also holds, i.e.
1772 // If ~str.contains( str.++( x1, ..., xn ), y ),
1773 // we may infer ~str.contains( x1, y ), ..., ~str.contains( xn, y )
1774 // This is also handled here.
1775 if (d_extf_infer_cache
.find(nr
) == d_extf_infer_cache
.end())
1777 d_extf_infer_cache
.insert(nr
);
1779 int index
= pol
? 1 : 0;
1780 std::vector
<Node
> children
;
1781 children
.push_back(nr
[0]);
1782 children
.push_back(nr
[1]);
1783 for (const Node
& nrc
: nr
[index
])
1785 children
[index
] = nrc
;
1786 Node conc
= nm
->mkNode(STRING_STRCTN
, children
);
1787 conc
= Rewriter::rewrite(pol
? conc
: conc
.negate());
1788 // check if it already (does not) hold
1791 if (areEqual(conc
, d_false
))
1793 // we are in conflict
1794 sendInference(in
.d_exp
, conc
, "CTN_Decompose");
1796 else if (getExtTheory()->hasFunctionKind(conc
.getKind()))
1798 // can mark as reduced, since model for n implies model for conc
1799 getExtTheory()->markReduced(conc
);
1807 if (std::find(d_extf_info_tmp
[nr
[0]].d_ctn
[pol
].begin(),
1808 d_extf_info_tmp
[nr
[0]].d_ctn
[pol
].end(),
1810 == d_extf_info_tmp
[nr
[0]].d_ctn
[pol
].end())
1812 Trace("strings-extf-debug") << " store contains info : " << nr
[0]
1813 << " " << pol
<< " " << nr
[1] << std::endl
;
1814 // Store s (does not) contains t, since nr = (~)contains( s, t ) holds.
1815 d_extf_info_tmp
[nr
[0]].d_ctn
[pol
].push_back(nr
[1]);
1816 d_extf_info_tmp
[nr
[0]].d_ctn_from
[pol
].push_back(n
);
1817 // Do transistive closure on contains, e.g.
1818 // if contains( s, t ) and ~contains( s, r ), then ~contains( t, r ).
1820 // The following infers new (negative) contains based on the above
1821 // reasoning, provided that ~contains( t, r ) does not
1822 // already hold in the current context. We test this by checking that
1823 // contains( t, r ) is not already asserted false in the current
1824 // context. We also handle the case where contains( t, r ) is equivalent
1825 // to t = r, in which case we check that t != r does not already hold
1826 // in the current context.
1828 // Notice that form of the above inference is enough to find
1829 // conflicts purely due to contains predicates. For example, if we
1830 // have only positive occurrences of contains, then no conflicts due to
1831 // contains predicates are possible and this schema does nothing. For
1832 // example, note that contains( s, t ) and contains( t, r ) implies
1833 // contains( s, r ), which we could but choose not to infer. Instead,
1834 // we prefer being lazy: only if ~contains( s, r ) appears later do we
1835 // infer ~contains( t, r ), which suffices to show a conflict.
1837 for (unsigned i
= 0, size
= d_extf_info_tmp
[nr
[0]].d_ctn
[opol
].size();
1841 Node onr
= d_extf_info_tmp
[nr
[0]].d_ctn
[opol
][i
];
1843 nm
->mkNode(STRING_STRCTN
, pol
? nr
[1] : onr
, pol
? onr
: nr
[1]);
1844 conc
= Rewriter::rewrite(conc
);
1845 conc
= conc
.negate();
1846 bool do_infer
= false;
1847 bool pol
= conc
.getKind() != NOT
;
1848 Node lit
= pol
? conc
: conc
[0];
1849 if (lit
.getKind() == EQUAL
)
1851 do_infer
= pol
? !areEqual(lit
[0], lit
[1])
1852 : !areDisequal(lit
[0], lit
[1]);
1856 do_infer
= !areEqual(lit
, pol
? d_true
: d_false
);
1860 std::vector
<Node
> exp_c
;
1861 exp_c
.insert(exp_c
.end(), in
.d_exp
.begin(), in
.d_exp
.end());
1862 Node ofrom
= d_extf_info_tmp
[nr
[0]].d_ctn_from
[opol
][i
];
1863 Assert(d_extf_info_tmp
.find(ofrom
) != d_extf_info_tmp
.end());
1864 exp_c
.insert(exp_c
.end(),
1865 d_extf_info_tmp
[ofrom
].d_exp
.begin(),
1866 d_extf_info_tmp
[ofrom
].d_exp
.end());
1867 sendInference(exp_c
, conc
, "CTN_Trans");
1873 // If we already know that s (does not) contain t, then n is redundant.
1874 // For example, if str.contains( x, y ), str.contains( z, y ), and x=z
1875 // are asserted in the current context, then str.contains( z, y ) is
1876 // satisfied by all models of str.contains( x, y ) ^ x=z and thus can
1878 Trace("strings-extf-debug") << " redundant." << std::endl
;
1879 getExtTheory()->markReduced(n
);
1885 // If it's not a predicate, see if we can solve the equality n = c, where c
1886 // is the constant that extended term n is equal to.
1887 Node inferEq
= nr
.eqNode(in
.d_const
);
1888 Node inferEqr
= Rewriter::rewrite(inferEq
);
1889 Node inferEqrr
= inferEqr
;
1890 if (inferEqr
.getKind() == EQUAL
)
1892 // try to use the extended rewriter for equalities
1893 inferEqrr
= TheoryStringsRewriter::rewriteEqualityExt(inferEqr
);
1895 if (inferEqrr
!= inferEqr
)
1897 inferEqrr
= Rewriter::rewrite(inferEqrr
);
1898 Trace("strings-extf-infer") << "checkExtfInference: " << inferEq
1899 << " ...reduces to " << inferEqrr
<< std::endl
;
1900 sendInternalInference(in
.d_exp
, inferEqrr
, "EXTF_equality_rew");
1904 Node
TheoryStrings::getSymbolicDefinition( Node n
, std::vector
< Node
>& exp
) {
1905 if( n
.getNumChildren()==0 ){
1906 NodeNodeMap::const_iterator it
= d_proxy_var
.find( n
);
1907 if( it
==d_proxy_var
.end() ){
1908 return Node::null();
1910 Node eq
= n
.eqNode( (*it
).second
);
1911 eq
= Rewriter::rewrite( eq
);
1912 if( std::find( exp
.begin(), exp
.end(), eq
)==exp
.end() ){
1913 exp
.push_back( eq
);
1915 return (*it
).second
;
1918 std::vector
< Node
> children
;
1919 if (n
.getMetaKind() == kind::metakind::PARAMETERIZED
) {
1920 children
.push_back( n
.getOperator() );
1922 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
1923 if( n
.getKind()==kind::STRING_IN_REGEXP
&& i
==1 ){
1924 children
.push_back( n
[i
] );
1926 Node ns
= getSymbolicDefinition( n
[i
], exp
);
1928 return Node::null();
1930 children
.push_back( ns
);
1934 return NodeManager::currentNM()->mkNode( n
.getKind(), children
);
1938 Node
TheoryStrings::getConstantEqc( Node eqc
) {
1939 std::map
< Node
, Node
>::iterator it
= d_eqc_to_const
.find( eqc
);
1940 if( it
!=d_eqc_to_const
.end() ){
1943 return Node::null();
1947 void TheoryStrings::debugPrintFlatForms( const char * tc
){
1948 for( unsigned k
=0; k
<d_strings_eqc
.size(); k
++ ){
1949 Node eqc
= d_strings_eqc
[k
];
1950 if( d_eqc
[eqc
].size()>1 ){
1951 Trace( tc
) << "EQC [" << eqc
<< "]" << std::endl
;
1953 Trace( tc
) << "eqc [" << eqc
<< "]";
1955 std::map
< Node
, Node
>::iterator itc
= d_eqc_to_const
.find( eqc
);
1956 if( itc
!=d_eqc_to_const
.end() ){
1957 Trace( tc
) << " C: " << itc
->second
;
1958 if( d_eqc
[eqc
].size()>1 ){
1959 Trace( tc
) << std::endl
;
1962 if( d_eqc
[eqc
].size()>1 ){
1963 for( unsigned i
=0; i
<d_eqc
[eqc
].size(); i
++ ){
1964 Node n
= d_eqc
[eqc
][i
];
1966 for( unsigned j
=0; j
<d_flat_form
[n
].size(); j
++ ){
1967 Node fc
= d_flat_form
[n
][j
];
1968 itc
= d_eqc_to_const
.find( fc
);
1970 if( itc
!=d_eqc_to_const
.end() ){
1971 Trace( tc
) << itc
->second
;
1977 Trace( tc
) << ", from " << n
;
1979 Trace( tc
) << std::endl
;
1982 Trace( tc
) << std::endl
;
1985 Trace( tc
) << std::endl
;
1988 void TheoryStrings::debugPrintNormalForms( const char * tc
) {
1991 struct sortConstLength
{
1992 std::map
< Node
, unsigned > d_const_length
;
1993 bool operator() (Node i
, Node j
) {
1994 std::map
< Node
, unsigned >::iterator it_i
= d_const_length
.find( i
);
1995 std::map
< Node
, unsigned >::iterator it_j
= d_const_length
.find( j
);
1996 if( it_i
==d_const_length
.end() ){
1997 if( it_j
==d_const_length
.end() ){
2003 if( it_j
==d_const_length
.end() ){
2006 return it_i
->second
<it_j
->second
;
2012 void TheoryStrings::checkCycles()
2014 // first check for cycles, while building ordering of equivalence classes
2015 d_flat_form
.clear();
2016 d_flat_form_index
.clear();
2018 //rebuild strings eqc based on acyclic ordering
2019 std::vector
< Node
> eqc
;
2020 eqc
.insert( eqc
.end(), d_strings_eqc
.begin(), d_strings_eqc
.end() );
2021 d_strings_eqc
.clear();
2022 if( options::stringBinaryCsp() ){
2023 //sort: process smallest constants first (necessary if doing binary splits)
2024 sortConstLength scl
;
2025 for( unsigned i
=0; i
<eqc
.size(); i
++ ){
2026 std::map
< Node
, Node
>::iterator itc
= d_eqc_to_const
.find( eqc
[i
] );
2027 if( itc
!=d_eqc_to_const
.end() ){
2028 scl
.d_const_length
[eqc
[i
]] = itc
->second
.getConst
<String
>().size();
2031 std::sort( eqc
.begin(), eqc
.end(), scl
);
2033 for( unsigned i
=0; i
<eqc
.size(); i
++ ){
2034 std::vector
< Node
> curr
;
2035 std::vector
< Node
> exp
;
2036 checkCycles( eqc
[i
], curr
, exp
);
2037 if( hasProcessed() ){
2043 void TheoryStrings::checkFlatForms()
2045 // debug print flat forms
2046 if (Trace
.isOn("strings-ff"))
2048 Trace("strings-ff") << "Flat forms : " << std::endl
;
2049 debugPrintFlatForms("strings-ff");
2052 // inferences without recursively expanding flat forms
2054 //(1) approximate equality by containment, infer conflicts
2055 for (const Node
& eqc
: d_strings_eqc
)
2057 Node c
= getConstantEqc(eqc
);
2060 // if equivalence class is constant, all component constants in flat forms
2061 // must be contained in it, in order
2062 std::map
<Node
, std::vector
<Node
> >::iterator it
= d_eqc
.find(eqc
);
2063 if (it
!= d_eqc
.end())
2065 for (const Node
& n
: it
->second
)
2068 if (!TheoryStringsRewriter::canConstantContainList(
2069 c
, d_flat_form
[n
], firstc
, lastc
))
2071 Trace("strings-ff-debug") << "Flat form for " << n
2072 << " cannot be contained in constant "
2074 Trace("strings-ff-debug") << " indices = " << firstc
<< "/"
2075 << lastc
<< std::endl
;
2076 // conflict, explanation is n = base ^ base = c ^ relevant portion
2078 std::vector
<Node
> exp
;
2079 Assert(d_eqc_to_const_base
.find(eqc
) != d_eqc_to_const_base
.end());
2080 addToExplanation(n
, d_eqc_to_const_base
[eqc
], exp
);
2081 Assert(d_eqc_to_const_exp
.find(eqc
) != d_eqc_to_const_exp
.end());
2082 if (!d_eqc_to_const_exp
[eqc
].isNull())
2084 exp
.push_back(d_eqc_to_const_exp
[eqc
]);
2086 for (int e
= firstc
; e
<= lastc
; e
++)
2088 if (d_flat_form
[n
][e
].isConst())
2090 Assert(e
>= 0 && e
< (int)d_flat_form_index
[n
].size());
2091 Assert(d_flat_form_index
[n
][e
] >= 0
2092 && d_flat_form_index
[n
][e
] < (int)n
.getNumChildren());
2094 d_flat_form
[n
][e
], n
[d_flat_form_index
[n
][e
]], exp
);
2097 Node conc
= d_false
;
2098 sendInference(exp
, conc
, "F_NCTN");
2106 //(2) scan lists, unification to infer conflicts and equalities
2107 for (const Node
& eqc
: d_strings_eqc
)
2109 std::map
<Node
, std::vector
<Node
> >::iterator it
= d_eqc
.find(eqc
);
2110 if (it
== d_eqc
.end() || it
->second
.size() <= 1)
2114 // iterate over start index
2115 for (unsigned start
= 0; start
< it
->second
.size() - 1; start
++)
2117 for (unsigned r
= 0; r
< 2; r
++)
2119 bool isRev
= r
== 1;
2120 checkFlatForm(it
->second
, start
, isRev
);
2130 void TheoryStrings::checkFlatForm(std::vector
<Node
>& eqc
,
2135 std::vector
<Node
> inelig
;
2136 for (unsigned i
= 0; i
<= start
; i
++)
2138 inelig
.push_back(eqc
[start
]);
2140 Node a
= eqc
[start
];
2144 std::vector
<Node
> exp
;
2147 unsigned eqc_size
= eqc
.size();
2148 unsigned asize
= d_flat_form
[a
].size();
2151 for (unsigned i
= start
+ 1; i
< eqc_size
; i
++)
2154 if (std::find(inelig
.begin(), inelig
.end(), b
) == inelig
.end())
2156 unsigned bsize
= d_flat_form
[b
].size();
2160 std::vector
<Node
> conc_c
;
2161 for (unsigned j
= count
; j
< bsize
; j
++)
2164 b
[d_flat_form_index
[b
][j
]].eqNode(d_emptyString
));
2166 Assert(!conc_c
.empty());
2167 conc
= mkAnd(conc_c
);
2170 // swap, will enforce is empty past current
2176 inelig
.push_back(eqc
[i
]);
2182 Node curr
= d_flat_form
[a
][count
];
2183 Node curr_c
= getConstantEqc(curr
);
2184 Node ac
= a
[d_flat_form_index
[a
][count
]];
2185 std::vector
<Node
> lexp
;
2186 Node lcurr
= getLength(ac
, lexp
);
2187 for (unsigned i
= 1; i
< eqc_size
; i
++)
2190 if (std::find(inelig
.begin(), inelig
.end(), b
) == inelig
.end())
2192 if (count
== d_flat_form
[b
].size())
2194 inelig
.push_back(b
);
2196 std::vector
<Node
> conc_c
;
2197 for (unsigned j
= count
; j
< asize
; j
++)
2200 a
[d_flat_form_index
[a
][j
]].eqNode(d_emptyString
));
2202 Assert(!conc_c
.empty());
2203 conc
= mkAnd(conc_c
);
2211 Node cc
= d_flat_form
[b
][count
];
2214 Node bc
= b
[d_flat_form_index
[b
][count
]];
2215 inelig
.push_back(b
);
2216 Assert(!areEqual(curr
, cc
));
2217 Node cc_c
= getConstantEqc(cc
);
2218 if (!curr_c
.isNull() && !cc_c
.isNull())
2220 // check for constant conflict
2222 Node s
= TheoryStringsRewriter::splitConstant(
2223 cc_c
, curr_c
, index
, isRev
);
2226 addToExplanation(ac
, d_eqc_to_const_base
[curr
], exp
);
2227 addToExplanation(d_eqc_to_const_exp
[curr
], exp
);
2228 addToExplanation(bc
, d_eqc_to_const_base
[cc
], exp
);
2229 addToExplanation(d_eqc_to_const_exp
[cc
], exp
);
2235 else if ((d_flat_form
[a
].size() - 1) == count
2236 && (d_flat_form
[b
].size() - 1) == count
)
2238 conc
= ac
.eqNode(bc
);
2244 // if lengths are the same, apply LengthEq
2245 std::vector
<Node
> lexp2
;
2246 Node lcc
= getLength(bc
, lexp2
);
2247 if (areEqual(lcurr
, lcc
))
2249 Trace("strings-ff-debug") << "Infer " << ac
<< " == " << bc
2250 << " since " << lcurr
2251 << " == " << lcc
<< std::endl
;
2252 // exp_n.push_back( getLength( curr, true ).eqNode(
2253 // getLength( cc, true ) ) );
2254 Trace("strings-ff-debug") << "Explanation for " << lcurr
2256 for (unsigned j
= 0; j
< lexp
.size(); j
++)
2258 Trace("strings-ff-debug") << lexp
[j
] << std::endl
;
2260 Trace("strings-ff-debug") << "Explanation for " << lcc
2262 for (unsigned j
= 0; j
< lexp2
.size(); j
++)
2264 Trace("strings-ff-debug") << lexp2
[j
] << std::endl
;
2266 exp
.insert(exp
.end(), lexp
.begin(), lexp
.end());
2267 exp
.insert(exp
.end(), lexp2
.begin(), lexp2
.end());
2268 addToExplanation(lcurr
, lcc
, exp
);
2269 conc
= ac
.eqNode(bc
);
2281 Trace("strings-ff-debug")
2282 << "Found inference : " << conc
<< " based on equality " << a
2283 << " == " << b
<< ", " << isRev
<< " " << inf_type
<< std::endl
;
2284 addToExplanation(a
, b
, exp
);
2285 // explain why prefixes up to now were the same
2286 for (unsigned j
= 0; j
< count
; j
++)
2288 Trace("strings-ff-debug") << "Add at " << d_flat_form_index
[a
][j
] << " "
2289 << d_flat_form_index
[b
][j
] << std::endl
;
2291 a
[d_flat_form_index
[a
][j
]], b
[d_flat_form_index
[b
][j
]], exp
);
2293 // explain why other components up to now are empty
2294 for (unsigned t
= 0; t
< 2; t
++)
2296 Node c
= t
== 0 ? a
: b
;
2298 if (inf_type
== 3 || (t
== 1 && inf_type
== 2))
2300 // explain all the empty components for F_EndpointEq, all for
2301 // the short end for F_EndpointEmp
2302 jj
= isRev
? -1 : c
.getNumChildren();
2306 jj
= t
== 0 ? d_flat_form_index
[a
][count
]
2307 : d_flat_form_index
[b
][count
];
2309 int startj
= isRev
? jj
+ 1 : 0;
2310 int endj
= isRev
? c
.getNumChildren() : jj
;
2311 for (int j
= startj
; j
< endj
; j
++)
2313 if (areEqual(c
[j
], d_emptyString
))
2315 addToExplanation(c
[j
], d_emptyString
, exp
);
2319 // notice that F_EndpointEmp is not typically applied, since
2320 // strict prefix equality ( a.b = a ) where a,b non-empty
2321 // is conflicting by arithmetic len(a.b)=len(a)+len(b)!=len(a)
2328 : (inf_type
== 1 ? "F_Unify" : (inf_type
== 2 ? "F_EndpointEmp"
2329 : "F_EndpointEq")));
2337 } while (inelig
.size() < eqc
.size());
2339 for (const Node
& n
: eqc
)
2341 std::reverse(d_flat_form
[n
].begin(), d_flat_form
[n
].end());
2342 std::reverse(d_flat_form_index
[n
].begin(), d_flat_form_index
[n
].end());
2346 Node
TheoryStrings::checkCycles( Node eqc
, std::vector
< Node
>& curr
, std::vector
< Node
>& exp
){
2347 if( std::find( curr
.begin(), curr
.end(), eqc
)!=curr
.end() ){
2350 }else if( std::find( d_strings_eqc
.begin(), d_strings_eqc
.end(), eqc
)==d_strings_eqc
.end() ){
2351 curr
.push_back( eqc
);
2352 //look at all terms in this equivalence class
2353 eq::EqClassIterator eqc_i
= eq::EqClassIterator( eqc
, &d_equalityEngine
);
2354 while( !eqc_i
.isFinished() ) {
2356 if( d_congruent
.find( n
)==d_congruent
.end() ){
2357 if( n
.getKind() == kind::STRING_CONCAT
){
2358 Trace("strings-cycle") << eqc
<< " check term : " << n
<< " in " << eqc
<< std::endl
;
2359 if( eqc
!=d_emptyString_r
){
2360 d_eqc
[eqc
].push_back( n
);
2362 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
2363 Node nr
= getRepresentative( n
[i
] );
2364 if( eqc
==d_emptyString_r
){
2365 //for empty eqc, ensure all components are empty
2366 if( nr
!=d_emptyString_r
){
2367 std::vector
< Node
> exp
;
2368 exp
.push_back( n
.eqNode( d_emptyString
) );
2369 sendInference( exp
, n
[i
].eqNode( d_emptyString
), "I_CYCLE_E" );
2370 return Node::null();
2373 if( nr
!=d_emptyString_r
){
2374 d_flat_form
[n
].push_back( nr
);
2375 d_flat_form_index
[n
].push_back( i
);
2377 //for non-empty eqc, recurse and see if we find a loop
2378 Node ncy
= checkCycles( nr
, curr
, exp
);
2379 if( !ncy
.isNull() ){
2380 Trace("strings-cycle") << eqc
<< " cycle: " << ncy
<< " at " << n
<< "[" << i
<< "] : " << n
[i
] << std::endl
;
2381 addToExplanation( n
, eqc
, exp
);
2382 addToExplanation( nr
, n
[i
], exp
);
2384 //can infer all other components must be empty
2385 for( unsigned j
=0; j
<n
.getNumChildren(); j
++ ){
2386 //take first non-empty
2387 if( j
!=i
&& !areEqual( n
[j
], d_emptyString
) ){
2388 sendInference( exp
, n
[j
].eqNode( d_emptyString
), "I_CYCLE" );
2389 return Node::null();
2392 Trace("strings-error") << "Looping term should be congruent : " << n
<< " " << eqc
<< " " << ncy
<< std::endl
;
2393 //should find a non-empty component, otherwise would have been singular congruent (I_Norm_S)
2399 if( hasProcessed() ){
2400 return Node::null();
2410 //now we can add it to the list of equivalence classes
2411 d_strings_eqc
.push_back( eqc
);
2415 return Node::null();
2418 void TheoryStrings::checkNormalFormsEq()
2420 if( !options::stringEagerLen() ){
2421 for( unsigned i
=0; i
<d_strings_eqc
.size(); i
++ ) {
2422 Node eqc
= d_strings_eqc
[i
];
2423 eq::EqClassIterator eqc_i
= eq::EqClassIterator( eqc
, &d_equalityEngine
);
2424 while( !eqc_i
.isFinished() ) {
2426 if( d_congruent
.find( n
)==d_congruent
.end() ){
2427 registerTerm( n
, 2 );
2438 // calculate normal forms for each equivalence class, possibly adding
2440 d_normal_forms
.clear();
2441 d_normal_forms_exp
.clear();
2442 std::map
<Node
, Node
> nf_to_eqc
;
2443 std::map
<Node
, Node
> eqc_to_nf
;
2444 std::map
<Node
, Node
> eqc_to_exp
;
2445 for (const Node
& eqc
: d_strings_eqc
)
2447 Trace("strings-process-debug") << "- Verify normal forms are the same for "
2448 << eqc
<< std::endl
;
2449 normalizeEquivalenceClass(eqc
);
2450 Trace("strings-debug") << "Finished normalizing eqc..." << std::endl
;
2455 Node nf_term
= mkConcat(d_normal_forms
[eqc
]);
2456 std::map
<Node
, Node
>::iterator itn
= nf_to_eqc
.find(nf_term
);
2457 if (itn
!= nf_to_eqc
.end())
2459 // two equivalence classes have same normal form, merge
2460 std::vector
<Node
> nf_exp
;
2461 nf_exp
.push_back(mkAnd(d_normal_forms_exp
[eqc
]));
2462 nf_exp
.push_back(eqc_to_exp
[itn
->second
]);
2464 d_normal_forms_base
[eqc
].eqNode(d_normal_forms_base
[itn
->second
]);
2465 sendInference(nf_exp
, eq
, "Normal_Form");
2466 if( hasProcessed() ){
2472 nf_to_eqc
[nf_term
] = eqc
;
2473 eqc_to_nf
[eqc
] = nf_term
;
2474 eqc_to_exp
[eqc
] = mkAnd(d_normal_forms_exp
[eqc
]);
2476 Trace("strings-process-debug")
2477 << "Done verifying normal forms are the same for " << eqc
<< std::endl
;
2479 if (Trace
.isOn("strings-nf"))
2481 Trace("strings-nf") << "**** Normal forms are : " << std::endl
;
2482 for (std::map
<Node
, Node
>::iterator it
= eqc_to_exp
.begin();
2483 it
!= eqc_to_exp
.end();
2486 Trace("strings-nf") << " N[" << it
->first
<< "] (base "
2487 << d_normal_forms_base
[it
->first
]
2488 << ") = " << eqc_to_nf
[it
->first
] << std::endl
;
2489 Trace("strings-nf") << " exp: " << it
->second
<< std::endl
;
2491 Trace("strings-nf") << std::endl
;
2495 void TheoryStrings::checkCodes()
2497 // ensure that lemmas regarding str.code been added for each constant string
2501 NodeManager
* nm
= NodeManager::currentNM();
2502 // str.code applied to the code term for each equivalence class that has a
2503 // code term but is not a constant
2504 std::vector
<Node
> nconst_codes
;
2505 // str.code applied to the proxy variables for each equivalence classes that
2506 // are constants of size one
2507 std::vector
<Node
> const_codes
;
2508 for (const Node
& eqc
: d_strings_eqc
)
2510 if (d_normal_forms
[eqc
].size() == 1 && d_normal_forms
[eqc
][0].isConst())
2512 Node c
= d_normal_forms
[eqc
][0];
2513 Trace("strings-code-debug") << "Get proxy variable for " << c
2515 Node cc
= nm
->mkNode(kind::STRING_CODE
, c
);
2516 cc
= Rewriter::rewrite(cc
);
2517 Assert(cc
.isConst());
2518 NodeNodeMap::const_iterator it
= d_proxy_var
.find(c
);
2519 AlwaysAssert(it
!= d_proxy_var
.end());
2520 Node vc
= nm
->mkNode(kind::STRING_CODE
, (*it
).second
);
2521 if (!areEqual(cc
, vc
))
2523 sendInference(d_empty_vec
, cc
.eqNode(vc
), "Code_Proxy");
2525 const_codes
.push_back(vc
);
2529 EqcInfo
* ei
= getOrMakeEqcInfo(eqc
, false);
2530 if (ei
&& !ei
->d_code_term
.get().isNull())
2532 Node vc
= nm
->mkNode(kind::STRING_CODE
, ei
->d_code_term
.get());
2533 nconst_codes
.push_back(vc
);
2541 // now, ensure that str.code is injective
2542 std::vector
<Node
> cmps
;
2543 cmps
.insert(cmps
.end(), const_codes
.rbegin(), const_codes
.rend());
2544 cmps
.insert(cmps
.end(), nconst_codes
.rbegin(), nconst_codes
.rend());
2545 for (unsigned i
= 0, num_ncc
= nconst_codes
.size(); i
< num_ncc
; i
++)
2547 Node c1
= nconst_codes
[i
];
2549 for (const Node
& c2
: cmps
)
2551 Trace("strings-code-debug")
2552 << "Compare codes : " << c1
<< " " << c2
<< std::endl
;
2553 if (!areDisequal(c1
, c2
) && !areEqual(c1
, d_neg_one
))
2555 Node eq_no
= c1
.eqNode(d_neg_one
);
2556 Node deq
= c1
.eqNode(c2
).negate();
2557 Node eqn
= c1
[0].eqNode(c2
[0]);
2558 // str.code(x)==-1 V str.code(x)!=str.code(y) V x==y
2559 Node inj_lem
= nm
->mkNode(kind::OR
, eq_no
, deq
, eqn
);
2560 sendInference(d_empty_vec
, inj_lem
, "Code_Inj");
2567 //compute d_normal_forms_(base,exp,exp_depend)[eqc]
2568 void TheoryStrings::normalizeEquivalenceClass( Node eqc
) {
2569 Trace("strings-process-debug") << "Process equivalence class " << eqc
<< std::endl
;
2570 if( areEqual( eqc
, d_emptyString
) ) {
2571 #ifdef CVC4_ASSERTIONS
2572 for( unsigned j
=0; j
<d_eqc
[eqc
].size(); j
++ ){
2573 Node n
= d_eqc
[eqc
][j
];
2574 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
2575 Assert( areEqual( n
[i
], d_emptyString
) );
2580 Trace("strings-process-debug") << "Return process equivalence class " << eqc
<< " : empty." << std::endl
;
2581 d_normal_forms_base
[eqc
] = d_emptyString
;
2582 d_normal_forms
[eqc
].clear();
2583 d_normal_forms_exp
[eqc
].clear();
2585 Assert( d_normal_forms
.find(eqc
)==d_normal_forms
.end() );
2586 //phi => t = s1 * ... * sn
2587 // normal form for each non-variable term in this eqc (s1...sn)
2588 std::vector
< std::vector
< Node
> > normal_forms
;
2589 // explanation for each normal form (phi)
2590 std::vector
< std::vector
< Node
> > normal_forms_exp
;
2591 // dependency information
2592 std::vector
< std::map
< Node
, std::map
< bool, int > > > normal_forms_exp_depend
;
2593 // record terms for each normal form (t)
2594 std::vector
< Node
> normal_form_src
;
2596 getNormalForms(eqc
, normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
);
2597 if( hasProcessed() ){
2600 // process the normal forms
2601 processNEqc( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
);
2602 if( hasProcessed() ){
2605 //debugPrintNormalForms( "strings-solve", eqc, normal_forms, normal_form_src, normal_forms_exp, normal_forms_exp_depend );
2607 //construct the normal form
2608 Assert( !normal_forms
.empty() );
2611 std::vector
< Node
>::iterator itn
= std::find( normal_form_src
.begin(), normal_form_src
.end(), eqc
);
2612 if( itn
!=normal_form_src
.end() ){
2613 nf_index
= itn
- normal_form_src
.begin();
2614 Trace("strings-solve-debug2") << "take normal form " << nf_index
<< std::endl
;
2615 Assert( normal_form_src
[nf_index
]==eqc
);
2617 //just take the first normal form
2618 Trace("strings-solve-debug2") << "take the first normal form" << std::endl
;
2620 d_normal_forms
[eqc
].insert( d_normal_forms
[eqc
].end(), normal_forms
[nf_index
].begin(), normal_forms
[nf_index
].end() );
2621 d_normal_forms_exp
[eqc
].insert( d_normal_forms_exp
[eqc
].end(), normal_forms_exp
[nf_index
].begin(), normal_forms_exp
[nf_index
].end() );
2622 Trace("strings-solve-debug2") << "take normal form ... done" << std::endl
;
2623 d_normal_forms_base
[eqc
] = normal_form_src
[nf_index
];
2624 //track dependencies
2625 for( unsigned i
=0; i
<normal_forms_exp
[nf_index
].size(); i
++ ){
2626 Node exp
= normal_forms_exp
[nf_index
][i
];
2627 for( unsigned r
=0; r
<2; r
++ ){
2628 d_normal_forms_exp_depend
[eqc
][exp
][r
==0] = normal_forms_exp_depend
[nf_index
][exp
][r
==0];
2631 Trace("strings-process-debug") << "Return process equivalence class " << eqc
<< " : returned, size = " << d_normal_forms
[eqc
].size() << std::endl
;
2635 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
){
2636 if( std::find( nf_exp_n
.begin(), nf_exp_n
.end(), exp
)==nf_exp_n
.end() ){
2637 nf_exp_n
.push_back( exp
);
2639 for( unsigned k
=0; k
<2; k
++ ){
2640 int val
= k
==0 ? new_val
: new_rev_val
;
2641 std::map
< bool, int >::iterator itned
= nf_exp_depend_n
[exp
].find( k
==1 );
2642 if( itned
==nf_exp_depend_n
[exp
].end() ){
2643 Trace("strings-process-debug") << "Deps : set dependency on " << exp
<< " to " << val
<< " isRev=" << (k
==0) << std::endl
;
2644 nf_exp_depend_n
[exp
][k
==1] = val
;
2646 Trace("strings-process-debug") << "Deps : Multiple dependencies on " << exp
<< " : " << itned
->second
<< " " << val
<< " isRev=" << (k
==0) << std::endl
;
2647 //if we already have a dependency (in the case of non-linear string equalities), it is min/max
2648 bool cmp
= val
> itned
->second
;
2650 nf_exp_depend_n
[exp
][k
==1] = val
;
2656 void TheoryStrings::getNormalForms( Node
&eqc
, std::vector
< std::vector
< Node
> > &normal_forms
, std::vector
< Node
> &normal_form_src
,
2657 std::vector
< std::vector
< Node
> > &normal_forms_exp
, std::vector
< std::map
< Node
, std::map
< bool, int > > >& normal_forms_exp_depend
) {
2658 //constant for equivalence class
2659 Node eqc_non_c
= eqc
;
2660 Trace("strings-process-debug") << "Get normal forms " << eqc
<< std::endl
;
2661 eq::EqClassIterator eqc_i
= eq::EqClassIterator( eqc
, &d_equalityEngine
);
2662 while( !eqc_i
.isFinished() ){
2664 if( d_congruent
.find( n
)==d_congruent
.end() ){
2665 if( n
.getKind() == kind::CONST_STRING
|| n
.getKind() == kind::STRING_CONCAT
){
2666 Trace("strings-process-debug") << "Get Normal Form : Process term " << n
<< " in eqc " << eqc
<< std::endl
;
2667 std::vector
< Node
> nf_n
;
2668 std::vector
< Node
> nf_exp_n
;
2669 std::map
< Node
, std::map
< bool, int > > nf_exp_depend_n
;
2670 if( n
.getKind()==kind::CONST_STRING
){
2671 if( n
!=d_emptyString
) {
2672 nf_n
.push_back( n
);
2674 }else if( n
.getKind()==kind::STRING_CONCAT
){
2675 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ) {
2676 Node nr
= d_equalityEngine
.getRepresentative( n
[i
] );
2677 Trace("strings-process-debug") << "Normalizing subterm " << n
[i
] << " = " << nr
<< std::endl
;
2678 Assert( d_normal_forms
.find( nr
)!=d_normal_forms
.end() );
2679 unsigned orig_size
= nf_n
.size();
2680 unsigned add_size
= d_normal_forms
[nr
].size();
2681 //if not the empty string, add to current normal form
2682 if( !d_normal_forms
[nr
].empty() ){
2683 for( unsigned r
=0; r
<d_normal_forms
[nr
].size(); r
++ ) {
2684 if( Trace
.isOn("strings-error") ) {
2685 if( d_normal_forms
[nr
][r
].getKind()==kind::STRING_CONCAT
){
2686 Trace("strings-error") << "Strings::Error: From eqc = " << eqc
<< ", " << n
<< " index " << i
<< ", bad normal form : ";
2687 for( unsigned rr
=0; rr
<d_normal_forms
[nr
].size(); rr
++ ) {
2688 Trace("strings-error") << d_normal_forms
[nr
][rr
] << " ";
2690 Trace("strings-error") << std::endl
;
2693 Assert( d_normal_forms
[nr
][r
].getKind()!=kind::STRING_CONCAT
);
2695 nf_n
.insert( nf_n
.end(), d_normal_forms
[nr
].begin(), d_normal_forms
[nr
].end() );
2698 for( unsigned j
=0; j
<d_normal_forms_exp
[nr
].size(); j
++ ){
2699 Node exp
= d_normal_forms_exp
[nr
][j
];
2701 trackNfExpDependency( nf_exp_n
, nf_exp_depend_n
, exp
,
2702 orig_size
+ d_normal_forms_exp_depend
[nr
][exp
][false],
2703 orig_size
+ ( add_size
- d_normal_forms_exp_depend
[nr
][exp
][true] ) );
2705 if( d_normal_forms_base
[nr
]!=n
[i
] ){
2706 Assert( d_normal_forms_base
.find( nr
)!=d_normal_forms_base
.end() );
2707 Node eq
= n
[i
].eqNode( d_normal_forms_base
[nr
] );
2708 //track depends : entire current segment is dependent upon base equality
2709 trackNfExpDependency( nf_exp_n
, nf_exp_depend_n
, eq
, orig_size
, orig_size
+ add_size
);
2712 //convert forward indices to reverse indices
2713 int total_size
= nf_n
.size();
2714 for( std::map
< Node
, std::map
< bool, int > >::iterator it
= nf_exp_depend_n
.begin(); it
!= nf_exp_depend_n
.end(); ++it
){
2715 it
->second
[true] = total_size
- it
->second
[true];
2716 Assert( it
->second
[true]>=0 );
2719 //if not equal to self
2720 if( nf_n
.size()>1 || ( nf_n
.size()==1 && nf_n
[0].getKind()==kind::CONST_STRING
) ){
2721 if( nf_n
.size()>1 ) {
2722 for( unsigned i
=0; i
<nf_n
.size(); i
++ ){
2723 if( Trace
.isOn("strings-error") ){
2724 Trace("strings-error") << "Cycle for normal form ";
2725 printConcat(nf_n
,"strings-error");
2726 Trace("strings-error") << "..." << nf_n
[i
] << std::endl
;
2728 Assert( !areEqual( nf_n
[i
], n
) );
2731 normal_forms
.push_back(nf_n
);
2732 normal_form_src
.push_back(n
);
2733 normal_forms_exp
.push_back(nf_exp_n
);
2734 normal_forms_exp_depend
.push_back(nf_exp_depend_n
);
2736 //this was redundant: combination of self + empty string(s)
2737 Node nn
= nf_n
.size()==0 ? d_emptyString
: nf_n
[0];
2738 Assert( areEqual( nn
, eqc
) );
2747 if( normal_forms
.empty() ) {
2748 Trace("strings-solve-debug2") << "construct the normal form" << std::endl
;
2749 //do not choose a concat here use "eqc_non_c" (in this case they have non-trivial explanation why they normalize to self)
2750 std::vector
< Node
> eqc_non_c_nf
;
2751 getConcatVec( eqc_non_c
, eqc_non_c_nf
);
2752 normal_forms
.push_back( eqc_non_c_nf
);
2753 normal_form_src
.push_back( eqc_non_c
);
2754 normal_forms_exp
.push_back( std::vector
< Node
>() );
2755 normal_forms_exp_depend
.push_back( std::map
< Node
, std::map
< bool, int > >() );
2757 if(Trace
.isOn("strings-solve")) {
2758 Trace("strings-solve") << "--- Normal forms for equivalance class " << eqc
<< " : " << std::endl
;
2759 for( unsigned i
=0; i
<normal_forms
.size(); i
++ ) {
2760 Trace("strings-solve") << "#" << i
<< " (from " << normal_form_src
[i
] << ") : ";
2761 for( unsigned j
=0; j
<normal_forms
[i
].size(); j
++ ) {
2763 Trace("strings-solve") << ", ";
2765 Trace("strings-solve") << normal_forms
[i
][j
];
2767 Trace("strings-solve") << std::endl
;
2768 Trace("strings-solve") << " Explanation is : ";
2769 if(normal_forms_exp
[i
].size() == 0) {
2770 Trace("strings-solve") << "NONE";
2772 for( unsigned j
=0; j
<normal_forms_exp
[i
].size(); j
++ ) {
2774 Trace("strings-solve") << " AND ";
2776 Trace("strings-solve") << normal_forms_exp
[i
][j
];
2778 Trace("strings-solve") << std::endl
;
2779 Trace("strings-solve") << "WITH DEPENDENCIES : " << std::endl
;
2780 for( unsigned j
=0; j
<normal_forms_exp
[i
].size(); j
++ ) {
2781 Trace("strings-solve") << " " << normal_forms_exp
[i
][j
] << " -> ";
2782 Trace("strings-solve") << normal_forms_exp_depend
[i
][normal_forms_exp
[i
][j
]][false] << ",";
2783 Trace("strings-solve") << normal_forms_exp_depend
[i
][normal_forms_exp
[i
][j
]][true] << std::endl
;
2786 Trace("strings-solve") << std::endl
;
2790 Trace("strings-solve") << "--- Single normal form for equivalence class " << eqc
<< std::endl
;
2793 //if equivalence class is constant, approximate as containment, infer conflicts
2794 Node c
= getConstantEqc( eqc
);
2796 Trace("strings-solve") << "Eqc is constant " << c
<< std::endl
;
2797 for( unsigned i
=0; i
<normal_forms
.size(); i
++ ) {
2799 if( !TheoryStringsRewriter::canConstantContainList( c
, normal_forms
[i
], firstc
, lastc
) ){
2800 Node n
= normal_form_src
[i
];
2802 Trace("strings-solve") << "Normal form for " << n
<< " cannot be contained in constant " << c
<< std::endl
;
2803 //conflict, explanation is n = base ^ base = c ^ relevant porition of ( n = N[n] )
2804 std::vector
< Node
> exp
;
2805 Assert( d_eqc_to_const_base
.find( eqc
)!=d_eqc_to_const_base
.end() );
2806 addToExplanation( n
, d_eqc_to_const_base
[eqc
], exp
);
2807 Assert( d_eqc_to_const_exp
.find( eqc
)!=d_eqc_to_const_exp
.end() );
2808 if( !d_eqc_to_const_exp
[eqc
].isNull() ){
2809 exp
.push_back( d_eqc_to_const_exp
[eqc
] );
2811 //TODO: this can be minimized based on firstc/lastc, normal_forms_exp_depend
2812 exp
.insert( exp
.end(), normal_forms_exp
[i
].begin(), normal_forms_exp
[i
].end() );
2813 Node conc
= d_false
;
2814 sendInference( exp
, conc
, "N_NCTN" );
2821 void TheoryStrings::getExplanationVectorForPrefix( std::vector
< std::vector
< Node
> > &normal_forms_exp
, std::vector
< std::map
< Node
, std::map
< bool, int > > >& normal_forms_exp_depend
,
2822 unsigned i
, int index
, bool isRev
, std::vector
< Node
>& curr_exp
) {
2823 if( index
==-1 || !options::stringMinPrefixExplain() ){
2824 curr_exp
.insert(curr_exp
.end(), normal_forms_exp
[i
].begin(), normal_forms_exp
[i
].end() );
2826 for( unsigned k
=0; k
<normal_forms_exp
[i
].size(); k
++ ){
2827 Node exp
= normal_forms_exp
[i
][k
];
2828 int dep
= normal_forms_exp_depend
[i
][exp
][isRev
];
2830 curr_exp
.push_back( exp
);
2831 Trace("strings-explain-prefix-debug") << " include : " << exp
<< std::endl
;
2833 Trace("strings-explain-prefix-debug") << " exclude : " << exp
<< std::endl
;
2839 void TheoryStrings::getExplanationVectorForPrefixEq( std::vector
< std::vector
< Node
> > &normal_forms
, std::vector
< Node
> &normal_form_src
,
2840 std::vector
< std::vector
< Node
> > &normal_forms_exp
, std::vector
< std::map
< Node
, std::map
< bool, int > > >& normal_forms_exp_depend
,
2841 unsigned i
, unsigned j
, int index_i
, int index_j
, bool isRev
, std::vector
< Node
>& curr_exp
) {
2842 Trace("strings-explain-prefix") << "Get explanation for prefix " << index_i
<< ", " << index_j
<< " of normal forms " << i
<< " and " << j
<< ", reverse = " << isRev
<< std::endl
;
2843 for( unsigned r
=0; r
<2; r
++ ){
2844 getExplanationVectorForPrefix( normal_forms_exp
, normal_forms_exp_depend
, r
==0 ? i
: j
, r
==0 ? index_i
: index_j
, isRev
, curr_exp
);
2846 Trace("strings-explain-prefix") << "Included " << curr_exp
.size() << " / " << ( normal_forms_exp
[i
].size() + normal_forms_exp
[j
].size() ) << std::endl
;
2847 addToExplanation( normal_form_src
[i
], normal_form_src
[j
], curr_exp
);
2851 void TheoryStrings::processNEqc( std::vector
< std::vector
< Node
> > &normal_forms
, std::vector
< Node
> &normal_form_src
,
2852 std::vector
< std::vector
< Node
> > &normal_forms_exp
, std::vector
< std::map
< Node
, std::map
< bool, int > > >& normal_forms_exp_depend
){
2853 //the possible inferences
2854 std::vector
< InferInfo
> pinfer
;
2855 // loop over all pairs
2856 for(unsigned i
=0; i
<normal_forms
.size()-1; i
++) {
2857 //unify each normalform[j] with normal_forms[i]
2858 for(unsigned j
=i
+1; j
<normal_forms
.size(); j
++ ) {
2859 //ensure that normal_forms[i] and normal_forms[j] are the same modulo equality, add to pinfer if not
2860 Trace("strings-solve") << "Strings: Process normal form #" << i
<< " against #" << j
<< "..." << std::endl
;
2861 if( isNormalFormPair( normal_form_src
[i
], normal_form_src
[j
] ) ) {
2862 Trace("strings-solve") << "Strings: Already cached." << std::endl
;
2864 //process the reverse direction first (check for easy conflicts and inferences)
2865 unsigned rindex
= 0;
2866 processReverseNEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, rindex
, 0, pinfer
);
2867 if( hasProcessed() ){
2869 }else if( !pinfer
.empty() && pinfer
.back().d_id
==1 ){
2872 //AJR: for less aggressive endpoint inference
2876 processSimpleNEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, index
, false, rindex
, pinfer
);
2877 if( hasProcessed() ){
2879 }else if( !pinfer
.empty() && pinfer
.back().d_id
==1 ){
2889 // now, determine which of the possible inferences we want to add
2890 unsigned use_index
= 0;
2891 bool set_use_index
= false;
2892 Trace("strings-solve") << "Possible inferences (" << pinfer
.size()
2893 << ") : " << std::endl
;
2894 unsigned min_id
= 9;
2895 unsigned max_index
= 0;
2896 for (unsigned i
= 0, size
= pinfer
.size(); i
< size
; i
++)
2898 Trace("strings-solve") << "From " << pinfer
[i
].d_i
<< " / " << pinfer
[i
].d_j
2899 << " (rev=" << pinfer
[i
].d_rev
<< ") : ";
2900 Trace("strings-solve") << pinfer
[i
].d_conc
<< " by " << pinfer
[i
].d_id
2902 if (!set_use_index
|| pinfer
[i
].d_id
< min_id
2903 || (pinfer
[i
].d_id
== min_id
&& pinfer
[i
].d_index
> max_index
))
2905 min_id
= pinfer
[i
].d_id
;
2906 max_index
= pinfer
[i
].d_index
;
2908 set_use_index
= true;
2911 // send the inference
2912 if (!pinfer
[use_index
].d_nf_pair
[0].isNull())
2914 Assert(!pinfer
[use_index
].d_nf_pair
[1].isNull());
2915 addNormalFormPair(pinfer
[use_index
].d_nf_pair
[0],
2916 pinfer
[use_index
].d_nf_pair
[1]);
2918 std::stringstream ssi
;
2919 ssi
<< pinfer
[use_index
].d_id
;
2920 sendInference(pinfer
[use_index
].d_ant
,
2921 pinfer
[use_index
].d_antn
,
2922 pinfer
[use_index
].d_conc
,
2924 pinfer
[use_index
].sendAsLemma());
2925 // Register the new skolems from this inference. We register them here
2926 // (lazily), since the code above has now decided to use the inference
2927 // at use_index that involves them.
2928 for (const std::pair
<const LengthStatus
, std::vector
<Node
> >& sks
:
2929 pinfer
[use_index
].d_new_skolem
)
2931 for (const Node
& n
: sks
.second
)
2933 registerLength(n
, sks
.first
);
2938 bool TheoryStrings::InferInfo::sendAsLemma() {
2942 void TheoryStrings::processReverseNEq( std::vector
< std::vector
< Node
> > &normal_forms
, std::vector
< Node
> &normal_form_src
,
2943 std::vector
< std::vector
< Node
> > &normal_forms_exp
, std::vector
< std::map
< Node
, std::map
< bool, int > > >& normal_forms_exp_depend
,
2944 unsigned i
, unsigned j
, unsigned& index
, unsigned rproc
, std::vector
< InferInfo
>& pinfer
) {
2945 //reverse normal form of i, j
2946 std::reverse( normal_forms
[i
].begin(), normal_forms
[i
].end() );
2947 std::reverse( normal_forms
[j
].begin(), normal_forms
[j
].end() );
2949 processSimpleNEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, index
, true, rproc
, pinfer
);
2951 //reverse normal form of i, j
2952 std::reverse( normal_forms
[i
].begin(), normal_forms
[i
].end() );
2953 std::reverse( normal_forms
[j
].begin(), normal_forms
[j
].end() );
2956 //rproc is the # is the size of suffix that is identical
2957 void TheoryStrings::processSimpleNEq( std::vector
< std::vector
< Node
> > &normal_forms
, std::vector
< Node
> &normal_form_src
,
2958 std::vector
< std::vector
< Node
> > &normal_forms_exp
, std::vector
< std::map
< Node
, std::map
< bool, int > > >& normal_forms_exp_depend
,
2959 unsigned i
, unsigned j
, unsigned& index
, bool isRev
, unsigned rproc
, std::vector
< InferInfo
>& pinfer
) {
2960 Assert( rproc
<=normal_forms
[i
].size() && rproc
<=normal_forms
[j
].size() );
2964 //if we are at the end
2965 if( index
==(normal_forms
[i
].size()-rproc
) || index
==(normal_forms
[j
].size()-rproc
) ){
2966 if( index
==(normal_forms
[i
].size()-rproc
) && index
==(normal_forms
[j
].size()-rproc
) ){
2969 //the remainder must be empty
2970 unsigned k
= index
==(normal_forms
[i
].size()-rproc
) ? j
: i
;
2971 unsigned index_k
= index
;
2972 //Node eq_exp = mkAnd( curr_exp );
2973 std::vector
< Node
> curr_exp
;
2974 getExplanationVectorForPrefixEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, -1, -1, isRev
, curr_exp
);
2975 while( !d_conflict
&& index_k
<(normal_forms
[k
].size()-rproc
) ){
2976 //can infer that this string must be empty
2977 Node eq
= normal_forms
[k
][index_k
].eqNode( d_emptyString
);
2978 //Trace("strings-lemma") << "Strings: Infer " << eq << " from " << eq_exp << std::endl;
2979 Assert( !areEqual( d_emptyString
, normal_forms
[k
][index_k
] ) );
2980 sendInference( curr_exp
, eq
, "N_EndpointEmp" );
2985 Trace("strings-solve-debug") << "Process " << normal_forms
[i
][index
] << " ... " << normal_forms
[j
][index
] << std::endl
;
2986 if( normal_forms
[i
][index
]==normal_forms
[j
][index
] ){
2987 Trace("strings-solve-debug") << "Simple Case 1 : strings are equal" << std::endl
;
2991 Assert( !areEqual(normal_forms
[i
][index
], normal_forms
[j
][index
]) );
2992 std::vector
< Node
> temp_exp
;
2993 Node length_term_i
= getLength( normal_forms
[i
][index
], temp_exp
);
2994 Node length_term_j
= getLength( normal_forms
[j
][index
], temp_exp
);
2995 //check length(normal_forms[i][index]) == length(normal_forms[j][index])
2996 if( areEqual( length_term_i
, length_term_j
) ){
2997 Trace("strings-solve-debug") << "Simple Case 2 : string lengths are equal" << std::endl
;
2998 Node eq
= normal_forms
[i
][index
].eqNode( normal_forms
[j
][index
] );
2999 //eq = Rewriter::rewrite( eq );
3000 Node length_eq
= length_term_i
.eqNode( length_term_j
);
3001 //temp_exp.insert(temp_exp.end(), curr_exp.begin(), curr_exp.end() );
3002 getExplanationVectorForPrefixEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, index
, index
, isRev
, temp_exp
);
3003 temp_exp
.push_back(length_eq
);
3004 sendInference( temp_exp
, eq
, "N_Unify" );
3006 }else if( ( normal_forms
[i
][index
].getKind()!=kind::CONST_STRING
&& index
==normal_forms
[i
].size()-rproc
-1 ) ||
3007 ( normal_forms
[j
][index
].getKind()!=kind::CONST_STRING
&& index
==normal_forms
[j
].size()-rproc
-1 ) ){
3008 Trace("strings-solve-debug") << "Simple Case 3 : at endpoint" << std::endl
;
3009 std::vector
< Node
> antec
;
3010 //antec.insert(antec.end(), curr_exp.begin(), curr_exp.end() );
3011 getExplanationVectorForPrefixEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, -1, -1, isRev
, antec
);
3012 std::vector
< Node
> eqn
;
3013 for( unsigned r
=0; r
<2; r
++ ) {
3014 int index_k
= index
;
3015 int k
= r
==0 ? i
: j
;
3016 std::vector
< Node
> eqnc
;
3017 for( unsigned index_l
=index_k
; index_l
<(normal_forms
[k
].size()-rproc
); index_l
++ ) {
3019 eqnc
.insert(eqnc
.begin(), normal_forms
[k
][index_l
] );
3021 eqnc
.push_back( normal_forms
[k
][index_l
] );
3024 eqn
.push_back( mkConcat( eqnc
) );
3026 if( !areEqual( eqn
[0], eqn
[1] ) ){
3027 sendInference( antec
, eqn
[0].eqNode( eqn
[1] ), "N_EndpointEq", true );
3030 Assert( normal_forms
[i
].size()==normal_forms
[j
].size() );
3031 index
= normal_forms
[i
].size()-rproc
;
3033 }else if( normal_forms
[i
][index
].isConst() && normal_forms
[j
][index
].isConst() ){
3034 Node const_str
= normal_forms
[i
][index
];
3035 Node other_str
= normal_forms
[j
][index
];
3036 Trace("strings-solve-debug") << "Simple Case 3 : Const Split : " << const_str
<< " vs " << other_str
<< " at index " << index
<< ", isRev = " << isRev
<< std::endl
;
3037 unsigned len_short
= const_str
.getConst
<String
>().size() <= other_str
.getConst
<String
>().size() ? const_str
.getConst
<String
>().size() : other_str
.getConst
<String
>().size();
3038 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
);
3040 //same prefix/suffix
3041 //k is the index of the string that is shorter
3042 int k
= const_str
.getConst
<String
>().size()<other_str
.getConst
<String
>().size() ? i
: j
;
3043 int l
= const_str
.getConst
<String
>().size()<other_str
.getConst
<String
>().size() ? j
: i
;
3044 //update the nf exp dependencies
3045 //notice this is not critical for soundness: not doing the below incrementing will only lead to overapproximating when antecedants are required in explanations
3046 for( std::map
< Node
, std::map
< bool, int > >::iterator itnd
= normal_forms_exp_depend
[l
].begin(); itnd
!= normal_forms_exp_depend
[l
].end(); ++itnd
){
3047 for( std::map
< bool, int >::iterator itnd2
= itnd
->second
.begin(); itnd2
!= itnd
->second
.end(); ++itnd2
){
3048 //see if this can be incremented: it can if it is not relevant to the current index
3049 Assert( itnd2
->second
>=0 && itnd2
->second
<=(int)normal_forms
[l
].size() );
3050 bool increment
= (itnd2
->first
==isRev
) ? itnd2
->second
>(int)index
: ( (int)normal_forms
[l
].size()-1-itnd2
->second
)<(int)index
;
3052 normal_forms_exp_depend
[l
][itnd
->first
][itnd2
->first
] = itnd2
->second
+ 1;
3057 int new_len
= normal_forms
[l
][index
].getConst
<String
>().size() - len_short
;
3058 Node remainderStr
= NodeManager::currentNM()->mkConst( normal_forms
[l
][index
].getConst
<String
>().substr(0, new_len
) );
3059 Trace("strings-solve-debug-test") << "Break normal form of " << normal_forms
[l
][index
] << " into " << normal_forms
[k
][index
] << ", " << remainderStr
<< std::endl
;
3060 normal_forms
[l
].insert( normal_forms
[l
].begin()+index
+ 1, remainderStr
);
3062 Node remainderStr
= NodeManager::currentNM()->mkConst(normal_forms
[l
][index
].getConst
<String
>().substr(len_short
));
3063 Trace("strings-solve-debug-test") << "Break normal form of " << normal_forms
[l
][index
] << " into " << normal_forms
[k
][index
] << ", " << remainderStr
<< std::endl
;
3064 normal_forms
[l
].insert( normal_forms
[l
].begin()+index
+ 1, remainderStr
);
3066 normal_forms
[l
][index
] = normal_forms
[k
][index
];
3071 std::vector
< Node
> antec
;
3072 getExplanationVectorForPrefixEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, index
, index
, isRev
, antec
);
3073 sendInference( antec
, d_false
, "N_Const", true );
3077 //construct the candidate inference "info"
3079 info
.d_index
= index
;
3084 bool info_valid
= false;
3085 Assert( index
<normal_forms
[i
].size()-rproc
&& index
<normal_forms
[j
].size()-rproc
);
3086 std::vector
< Node
> lexp
;
3087 Node length_term_i
= getLength( normal_forms
[i
][index
], lexp
);
3088 Node length_term_j
= getLength( normal_forms
[j
][index
], lexp
);
3089 //split on equality between string lengths (note that splitting on equality between strings is worse since it is harder to process)
3090 if( !areDisequal( length_term_i
, length_term_j
) && !areEqual( length_term_i
, length_term_j
) &&
3091 normal_forms
[i
][index
].getKind()!=kind::CONST_STRING
&& normal_forms
[j
][index
].getKind()!=kind::CONST_STRING
){ //AJR: remove the latter 2 conditions?
3092 Trace("strings-solve-debug") << "Non-simple Case 1 : string lengths neither equal nor disequal" << std::endl
;
3093 //try to make the lengths equal via splitting on demand
3094 Node length_eq
= NodeManager::currentNM()->mkNode( kind::EQUAL
, length_term_i
, length_term_j
);
3095 length_eq
= Rewriter::rewrite( length_eq
);
3097 info
.d_conc
= NodeManager::currentNM()->mkNode( kind::OR
, length_eq
, length_eq
.negate() );
3098 info
.d_pending_phase
[ length_eq
] = true;
3099 info
.d_id
= INFER_LEN_SPLIT
;
3102 Trace("strings-solve-debug") << "Non-simple Case 2 : must compare strings" << std::endl
;
3105 ProcessLoopResult plr
= ProcessLoopResult::SKIPPED
;
3106 if( detectLoop( normal_forms
, i
, j
, index
, loop_in_i
, loop_in_j
, rproc
) ){
3107 if( !isRev
){ //FIXME
3108 getExplanationVectorForPrefixEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, -1, -1, isRev
, info
.d_ant
);
3110 plr
= processLoop(normal_forms
,
3114 loop_in_i
!= -1 ? i
: j
,
3115 loop_in_i
!= -1 ? j
: i
,
3116 loop_in_i
!= -1 ? loop_in_i
: loop_in_j
,
3119 if (plr
== ProcessLoopResult::INFERENCE
)
3126 if (plr
== ProcessLoopResult::SKIPPED
)
3128 //AJR: length entailment here?
3129 if( normal_forms
[i
][index
].getKind() == kind::CONST_STRING
|| normal_forms
[j
][index
].getKind() == kind::CONST_STRING
){
3130 unsigned const_k
= normal_forms
[i
][index
].getKind() == kind::CONST_STRING
? i
: j
;
3131 unsigned nconst_k
= normal_forms
[i
][index
].getKind() == kind::CONST_STRING
? j
: i
;
3132 Node other_str
= normal_forms
[nconst_k
][index
];
3133 Assert( other_str
.getKind()!=kind::CONST_STRING
, "Other string is not constant." );
3134 Assert( other_str
.getKind()!=kind::STRING_CONCAT
, "Other string is not CONCAT." );
3135 if( !d_equalityEngine
.areDisequal( other_str
, d_emptyString
, true ) ){
3136 Node eq
= other_str
.eqNode( d_emptyString
);
3138 info
.d_conc
= NodeManager::currentNM()->mkNode( kind::OR
, eq
, eq
.negate() );
3139 info
.d_id
= INFER_LEN_SPLIT_EMP
;
3142 if( !isRev
){ //FIXME
3143 Node xnz
= other_str
.eqNode( d_emptyString
).negate();
3144 unsigned index_nc_k
= index
+1;
3145 //Node next_const_str = TheoryStringsRewriter::collectConstantStringAt( normal_forms[nconst_k], index_nc_k, false );
3146 unsigned start_index_nc_k
= index
+1;
3147 Node next_const_str
= TheoryStringsRewriter::getNextConstantAt( normal_forms
[nconst_k
], start_index_nc_k
, index_nc_k
, false );
3148 if( !next_const_str
.isNull() ) {
3149 unsigned index_c_k
= index
;
3150 Node const_str
= TheoryStringsRewriter::collectConstantStringAt( normal_forms
[const_k
], index_c_k
, false );
3151 Assert( !const_str
.isNull() );
3152 CVC4::String stra
= const_str
.getConst
<String
>();
3153 CVC4::String strb
= next_const_str
.getConst
<String
>();
3154 //since non-empty, we start with charecter #1
3157 CVC4::String stra1
= stra
.prefix( stra
.size()-1 );
3158 p
= stra
.size() - stra1
.roverlap(strb
);
3159 Trace("strings-csp-debug") << "Compute roverlap : " << const_str
<< " " << next_const_str
<< std::endl
;
3160 size_t p2
= stra1
.rfind(strb
);
3161 p
= p2
==std::string::npos
? p
: ( p
>p2
+1? p2
+1 : p
);
3162 Trace("strings-csp-debug") << "overlap : " << stra1
<< " " << strb
<< " returned " << p
<< " " << p2
<< " " << (p2
==std::string::npos
) << std::endl
;
3164 CVC4::String stra1
= stra
.substr( 1 );
3165 p
= stra
.size() - stra1
.overlap(strb
);
3166 Trace("strings-csp-debug") << "Compute overlap : " << const_str
<< " " << next_const_str
<< std::endl
;
3167 size_t p2
= stra1
.find(strb
);
3168 p
= p2
==std::string::npos
? p
: ( p
>p2
+1? p2
+1 : p
);
3169 Trace("strings-csp-debug") << "overlap : " << stra1
<< " " << strb
<< " returned " << p
<< " " << p2
<< " " << (p2
==std::string::npos
) << std::endl
;
3172 if( start_index_nc_k
==index
+1 ){
3173 info
.d_ant
.push_back( xnz
);
3174 getExplanationVectorForPrefixEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
,
3175 const_k
, nconst_k
, index_c_k
, index_nc_k
, isRev
, info
.d_ant
);
3176 Node prea
= p
==stra
.size() ? const_str
: NodeManager::currentNM()->mkConst( isRev
? stra
.suffix( p
) : stra
.prefix( p
) );
3177 Node sk
= d_sk_cache
.mkSkolemCached(
3180 isRev
? SkolemCache::SK_ID_C_SPT_REV
3181 : SkolemCache::SK_ID_C_SPT
,
3183 Trace("strings-csp") << "Const Split: " << prea
<< " is removed from " << stra
<< " due to " << strb
<< ", p=" << p
<< std::endl
;
3185 info
.d_conc
= other_str
.eqNode( isRev
? mkConcat( sk
, prea
) : mkConcat(prea
, sk
) );
3186 info
.d_new_skolem
[LENGTH_SPLIT
].push_back(sk
);
3187 info
.d_id
= INFER_SSPLIT_CST_PROP
;
3190 /* FIXME for isRev, speculative
3191 else if( options::stringLenPropCsp() ){
3192 //propagate length constraint
3193 std::vector< Node > cc;
3194 for( unsigned i=index; i<start_index_nc_k; i++ ){
3195 cc.push_back( normal_forms[nconst_k][i] );
3197 Node lt = NodeManager::currentNM()->mkNode( kind::STRING_LENGTH, mkConcat( cc ) );
3198 conc = NodeManager::currentNM()->mkNode( kind::GEQ, lt, NodeManager::currentNM()->mkConst( Rational(p) ) );
3199 sendInference( ant, conc, "S-Split(CSP-P)-lprop", true );
3205 info
.d_ant
.push_back( xnz
);
3206 Node const_str
= normal_forms
[const_k
][index
];
3207 getExplanationVectorForPrefixEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, index
, index
, isRev
, info
.d_ant
);
3208 CVC4::String stra
= const_str
.getConst
<String
>();
3209 if( options::stringBinaryCsp() && stra
.size()>3 ){
3210 //split string in half
3211 Node c_firstHalf
= NodeManager::currentNM()->mkConst( isRev
? stra
.substr( stra
.size()/2 ) : stra
.substr(0, stra
.size()/2 ) );
3212 Node sk
= d_sk_cache
.mkSkolemCached(
3215 isRev
? SkolemCache::SK_ID_VC_BIN_SPT_REV
3216 : SkolemCache::SK_ID_VC_BIN_SPT
,
3218 Trace("strings-csp") << "Const Split: " << c_firstHalf
<< " is removed from " << const_str
<< " (binary) " << std::endl
;
3219 info
.d_conc
= NodeManager::currentNM()->mkNode( kind::OR
, other_str
.eqNode( isRev
? mkConcat( sk
, c_firstHalf
) : mkConcat( c_firstHalf
, sk
) ),
3220 NodeManager::currentNM()->mkNode( kind::AND
,
3221 sk
.eqNode( d_emptyString
).negate(),
3222 c_firstHalf
.eqNode( isRev
? mkConcat( sk
, other_str
) : mkConcat( other_str
, sk
) ) ) );
3223 info
.d_new_skolem
[LENGTH_SPLIT
].push_back(sk
);
3224 info
.d_id
= INFER_SSPLIT_CST_BINARY
;
3228 Node firstChar
= stra
.size() == 1 ? const_str
: NodeManager::currentNM()->mkConst( isRev
? stra
.suffix( 1 ) : stra
.prefix( 1 ) );
3229 Node sk
= d_sk_cache
.mkSkolemCached(
3232 isRev
? SkolemCache::SK_ID_VC_SPT_REV
3233 : SkolemCache::SK_ID_VC_SPT
,
3235 Trace("strings-csp") << "Const Split: " << firstChar
<< " is removed from " << const_str
<< " (serial) " << std::endl
;
3236 info
.d_conc
= other_str
.eqNode( isRev
? mkConcat( sk
, firstChar
) : mkConcat(firstChar
, sk
) );
3237 info
.d_new_skolem
[LENGTH_SPLIT
].push_back(sk
);
3238 info
.d_id
= INFER_SSPLIT_CST
;
3245 int lentTestSuccess
= -1;
3247 if( options::stringCheckEntailLen() ){
3249 for( unsigned e
=0; e
<2; e
++ ){
3250 Node t
= e
==0 ? normal_forms
[i
][index
] : normal_forms
[j
][index
];
3251 //do not infer constants are larger than variables
3252 if( t
.getKind()!=kind::CONST_STRING
){
3253 Node lt1
= e
==0 ? length_term_i
: length_term_j
;
3254 Node lt2
= e
==0 ? length_term_j
: length_term_i
;
3255 Node ent_lit
= Rewriter::rewrite( NodeManager::currentNM()->mkNode( kind::GT
, lt1
, lt2
) );
3256 std::pair
<bool, Node
> et
= d_valuation
.entailmentCheck( THEORY_OF_TYPE_BASED
, ent_lit
);
3258 Trace("strings-entail") << "Strings entailment : " << ent_lit
<< " is entailed in the current context." << std::endl
;
3259 Trace("strings-entail") << " explanation was : " << et
.second
<< std::endl
;
3260 lentTestSuccess
= e
;
3261 lentTestExp
= et
.second
;
3268 getExplanationVectorForPrefixEq( normal_forms
, normal_form_src
, normal_forms_exp
, normal_forms_exp_depend
, i
, j
, index
, index
, isRev
, info
.d_ant
);
3270 for(unsigned xory
=0; xory
<2; xory
++) {
3271 Node x
= xory
==0 ? normal_forms
[i
][index
] : normal_forms
[j
][index
];
3272 Node xgtz
= x
.eqNode( d_emptyString
).negate();
3273 if( d_equalityEngine
.areDisequal( x
, d_emptyString
, true ) ) {
3274 info
.d_ant
.push_back( xgtz
);
3276 info
.d_antn
.push_back( xgtz
);
3279 Node sk
= d_sk_cache
.mkSkolemCached(
3280 normal_forms
[i
][index
],
3281 normal_forms
[j
][index
],
3282 isRev
? SkolemCache::SK_ID_V_SPT_REV
3283 : SkolemCache::SK_ID_V_SPT
,
3285 // must add length requirement
3286 info
.d_new_skolem
[LENGTH_GEQ_ONE
].push_back(sk
);
3287 Node eq1
= normal_forms
[i
][index
].eqNode( isRev
? mkConcat(sk
, normal_forms
[j
][index
]) : mkConcat(normal_forms
[j
][index
], sk
) );
3288 Node eq2
= normal_forms
[j
][index
].eqNode( isRev
? mkConcat(sk
, normal_forms
[i
][index
]) : mkConcat(normal_forms
[i
][index
], sk
) );
3290 if( lentTestSuccess
!=-1 ){
3291 info
.d_antn
.push_back( lentTestExp
);
3292 info
.d_conc
= lentTestSuccess
==0 ? eq1
: eq2
;
3293 info
.d_id
= INFER_SSPLIT_VAR_PROP
;
3296 Node ldeq
= NodeManager::currentNM()->mkNode( kind::EQUAL
, length_term_i
, length_term_j
).negate();
3297 if( d_equalityEngine
.areDisequal( length_term_i
, length_term_j
, true ) ){
3298 info
.d_ant
.push_back( ldeq
);
3300 info
.d_antn
.push_back(ldeq
);
3303 info
.d_conc
= NodeManager::currentNM()->mkNode( kind::OR
, eq1
, eq2
);
3304 info
.d_id
= INFER_SSPLIT_VAR
;
3311 pinfer
.push_back( info
);
3320 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
){
3321 int has_loop
[2] = { -1, -1 };
3322 if( options::stringLB() != 2 ) {
3323 for( unsigned r
=0; r
<2; r
++ ) {
3324 int n_index
= (r
==0 ? i
: j
);
3325 int other_n_index
= (r
==0 ? j
: i
);
3326 if( normal_forms
[other_n_index
][index
].getKind() != kind::CONST_STRING
) {
3327 for( unsigned lp
= index
+1; lp
<normal_forms
[n_index
].size()-rproc
; lp
++ ){
3328 if( normal_forms
[n_index
][lp
]==normal_forms
[other_n_index
][index
] ){
3336 if( has_loop
[0]!=-1 || has_loop
[1]!=-1 ) {
3337 loop_in_i
= has_loop
[0];
3338 loop_in_j
= has_loop
[1];
3341 Trace("strings-solve-debug") << "No loops detected." << std::endl
;
3347 TheoryStrings::ProcessLoopResult
TheoryStrings::processLoop(
3348 const std::vector
<std::vector
<Node
> >& normal_forms
,
3349 const std::vector
<Node
>& normal_form_src
,
3358 if (options::stringProcessLoopMode() == ProcessLoopMode::ABORT
)
3360 throw LogicException("Looping word equation encountered.");
3362 else if (options::stringProcessLoopMode() == ProcessLoopMode::NONE
)
3364 d_out
->setIncomplete();
3365 return ProcessLoopResult::SKIPPED
;
3368 NodeManager
* nm
= NodeManager::currentNM();
3370 Trace("strings-loop") << "Detected possible loop for "
3371 << normal_forms
[loop_n_index
][loop_index
] << std::endl
;
3372 Trace("strings-loop") << " ... (X)= " << normal_forms
[other_n_index
][index
]
3375 Trace("strings-loop") << " ... T(Y.Z)= ";
3376 const std::vector
<Node
>& veci
= normal_forms
[loop_n_index
];
3377 std::vector
<Node
> vec_t(veci
.begin() + index
, veci
.begin() + loop_index
);
3378 Node t_yz
= mkConcat(vec_t
);
3379 Trace("strings-loop") << " (" << t_yz
<< ")" << std::endl
;
3380 Trace("strings-loop") << " ... S(Z.Y)= ";
3381 const std::vector
<Node
>& vecoi
= normal_forms
[other_n_index
];
3382 std::vector
<Node
> vec_s(vecoi
.begin() + index
+ 1, vecoi
.end());
3383 Node s_zy
= mkConcat(vec_s
);
3384 Trace("strings-loop") << s_zy
<< std::endl
;
3385 Trace("strings-loop") << " ... R= ";
3386 std::vector
<Node
> vec_r(veci
.begin() + loop_index
+ 1, veci
.end());
3387 Node r
= mkConcat(vec_r
);
3388 Trace("strings-loop") << r
<< std::endl
;
3390 if (s_zy
.isConst() && r
.isConst() && r
!= d_emptyString
)
3394 if (s_zy
.getConst
<String
>().tailcmp(r
.getConst
<String
>(), c
))
3398 s_zy
= nm
->mkConst(s_zy
.getConst
<String
>().substr(0, c
));
3401 Trace("strings-loop") << "Strings::Loop: Refactor S(Z.Y)= " << s_zy
3402 << ", c=" << c
<< std::endl
;
3408 Trace("strings-loop") << "Strings::Loop: tails are different."
3410 sendInference(info
.d_ant
, conc
, "Loop Conflict", true);
3411 return ProcessLoopResult::CONFLICT
;
3416 for (unsigned r
= 0; r
< 2; r
++)
3418 Node t
= r
== 0 ? normal_forms
[loop_n_index
][loop_index
] : t_yz
;
3419 split_eq
= t
.eqNode(d_emptyString
);
3420 Node split_eqr
= Rewriter::rewrite(split_eq
);
3421 // the equality could rewrite to false
3422 if (!split_eqr
.isConst())
3424 if (!areDisequal(t
, d_emptyString
))
3426 // try to make t equal to empty to avoid loop
3427 info
.d_conc
= nm
->mkNode(kind::OR
, split_eq
, split_eq
.negate());
3428 info
.d_id
= INFER_LEN_SPLIT_EMP
;
3429 return ProcessLoopResult::INFERENCE
;
3433 info
.d_ant
.push_back(split_eq
.negate());
3438 Assert(!split_eqr
.getConst
<bool>());
3442 Node ant
= mkExplain(info
.d_ant
);
3444 info
.d_antn
.push_back(ant
);
3447 if (s_zy
== t_yz
&& r
== d_emptyString
&& s_zy
.isConst()
3448 && s_zy
.getConst
<String
>().isRepeated())
3450 Node rep_c
= nm
->mkConst(s_zy
.getConst
<String
>().substr(0, 1));
3451 Trace("strings-loop") << "Special case (X)="
3452 << normal_forms
[other_n_index
][index
] << " "
3454 Trace("strings-loop") << "... (C)=" << rep_c
<< " " << std::endl
;
3457 nm
->mkNode(kind::STRING_IN_REGEXP
,
3458 normal_forms
[other_n_index
][index
],
3459 nm
->mkNode(kind::REGEXP_STAR
,
3460 nm
->mkNode(kind::STRING_TO_REGEXP
, rep_c
)));
3463 else if (t_yz
.isConst())
3465 Trace("strings-loop") << "Strings::Loop: Const Normal Breaking."
3467 CVC4::String s
= t_yz
.getConst
<CVC4::String
>();
3468 unsigned size
= s
.size();
3469 std::vector
<Node
> vconc
;
3470 for (unsigned len
= 1; len
<= size
; len
++)
3472 Node y
= nm
->mkConst(s
.substr(0, len
));
3473 Node z
= nm
->mkConst(s
.substr(len
, size
- len
));
3476 if (r
!= d_emptyString
)
3478 std::vector
<Node
> v2(vec_r
);
3479 v2
.insert(v2
.begin(), y
);
3480 v2
.insert(v2
.begin(), z
);
3481 restr
= mkConcat(z
, y
);
3482 cc
= Rewriter::rewrite(s_zy
.eqNode(mkConcat(v2
)));
3486 cc
= Rewriter::rewrite(s_zy
.eqNode(mkConcat(z
, y
)));
3492 Node conc2
= nm
->mkNode(
3493 kind::STRING_IN_REGEXP
,
3494 normal_forms
[other_n_index
][index
],
3495 nm
->mkNode(kind::REGEXP_CONCAT
,
3496 nm
->mkNode(kind::STRING_TO_REGEXP
, y
),
3497 nm
->mkNode(kind::REGEXP_STAR
,
3498 nm
->mkNode(kind::STRING_TO_REGEXP
, restr
))));
3499 cc
= cc
== d_true
? conc2
: nm
->mkNode(kind::AND
, cc
, conc2
);
3500 d_regexp_ant
[conc2
] = ant
;
3501 vconc
.push_back(cc
);
3503 conc
= vconc
.size() == 0 ? Node::null() : vconc
.size() == 1
3505 : nm
->mkNode(kind::OR
, vconc
);
3509 if (options::stringProcessLoopMode() == ProcessLoopMode::SIMPLE_ABORT
)
3511 throw LogicException("Normal looping word equation encountered.");
3513 else if (options::stringProcessLoopMode() == ProcessLoopMode::SIMPLE
)
3515 d_out
->setIncomplete();
3516 return ProcessLoopResult::SKIPPED
;
3519 Trace("strings-loop") << "Strings::Loop: Normal Loop Breaking."
3522 Node sk_w
= d_sk_cache
.mkSkolem("w_loop");
3523 Node sk_y
= d_sk_cache
.mkSkolem("y_loop");
3524 registerLength(sk_y
, LENGTH_GEQ_ONE
);
3525 Node sk_z
= d_sk_cache
.mkSkolem("z_loop");
3526 // t1 * ... * tn = y * z
3527 Node conc1
= t_yz
.eqNode(mkConcat(sk_y
, sk_z
));
3528 // s1 * ... * sk = z * y * r
3529 vec_r
.insert(vec_r
.begin(), sk_y
);
3530 vec_r
.insert(vec_r
.begin(), sk_z
);
3531 Node conc2
= s_zy
.eqNode(mkConcat(vec_r
));
3533 normal_forms
[other_n_index
][index
].eqNode(mkConcat(sk_y
, sk_w
));
3534 Node restr
= r
== d_emptyString
? s_zy
: mkConcat(sk_z
, sk_y
);
3536 nm
->mkNode(kind::STRING_IN_REGEXP
,
3538 nm
->mkNode(kind::REGEXP_STAR
,
3539 nm
->mkNode(kind::STRING_TO_REGEXP
, restr
)));
3541 std::vector
<Node
> vec_conc
;
3542 vec_conc
.push_back(conc1
);
3543 vec_conc
.push_back(conc2
);
3544 vec_conc
.push_back(conc3
);
3545 vec_conc
.push_back(str_in_re
);
3546 // vec_conc.push_back(sk_y.eqNode(d_emptyString).negate());//by mkskolems
3547 conc
= nm
->mkNode(kind::AND
, vec_conc
);
3550 // set its antecedant to ant, to say when it is relevant
3551 if (!str_in_re
.isNull())
3553 d_regexp_ant
[str_in_re
] = ant
;
3557 info
.d_id
= INFER_FLOOP
;
3558 info
.d_nf_pair
[0] = normal_form_src
[i
];
3559 info
.d_nf_pair
[1] = normal_form_src
[j
];
3560 return ProcessLoopResult::INFERENCE
;
3563 //return true for lemma, false if we succeed
3564 void TheoryStrings::processDeq( Node ni
, Node nj
) {
3565 //Assert( areDisequal( ni, nj ) );
3566 if( d_normal_forms
[ni
].size()>1 || d_normal_forms
[nj
].size()>1 ){
3567 std::vector
< Node
> nfi
;
3568 nfi
.insert( nfi
.end(), d_normal_forms
[ni
].begin(), d_normal_forms
[ni
].end() );
3569 std::vector
< Node
> nfj
;
3570 nfj
.insert( nfj
.end(), d_normal_forms
[nj
].begin(), d_normal_forms
[nj
].end() );
3572 int revRet
= processReverseDeq( nfi
, nfj
, ni
, nj
);
3578 nfi
.insert( nfi
.end(), d_normal_forms
[ni
].begin(), d_normal_forms
[ni
].end() );
3580 nfj
.insert( nfj
.end(), d_normal_forms
[nj
].begin(), d_normal_forms
[nj
].end() );
3583 while( index
<nfi
.size() || index
<nfj
.size() ){
3584 int ret
= processSimpleDeq( nfi
, nfj
, ni
, nj
, index
, false );
3588 Assert( index
<nfi
.size() && index
<nfj
.size() );
3589 Node i
= nfi
[index
];
3590 Node j
= nfj
[index
];
3591 Trace("strings-solve-debug") << "...Processing(DEQ) " << i
<< " " << j
<< std::endl
;
3592 if( !areEqual( i
, j
) ){
3593 Assert( i
.getKind()!=kind::CONST_STRING
|| j
.getKind()!=kind::CONST_STRING
);
3594 std::vector
< Node
> lexp
;
3595 Node li
= getLength( i
, lexp
);
3596 Node lj
= getLength( j
, lexp
);
3597 if( areDisequal( li
, lj
) ){
3598 if( i
.getKind()==kind::CONST_STRING
|| j
.getKind()==kind::CONST_STRING
){
3600 Node const_k
= i
.getKind() == kind::CONST_STRING
? i
: j
;
3601 Node nconst_k
= i
.getKind() == kind::CONST_STRING
? j
: i
;
3602 Node lnck
= i
.getKind() == kind::CONST_STRING
? lj
: li
;
3603 if( !d_equalityEngine
.areDisequal( nconst_k
, d_emptyString
, true ) ){
3604 Node eq
= nconst_k
.eqNode( d_emptyString
);
3605 Node conc
= NodeManager::currentNM()->mkNode( kind::OR
, eq
, eq
.negate() );
3606 sendInference( d_empty_vec
, conc
, "D-DISL-Emp-Split" );
3609 //split on first character
3610 CVC4::String str
= const_k
.getConst
<String
>();
3611 Node firstChar
= str
.size() == 1 ? const_k
: NodeManager::currentNM()->mkConst( str
.prefix( 1 ) );
3612 if( areEqual( lnck
, d_one
) ){
3613 if( areDisequal( firstChar
, nconst_k
) ){
3615 }else if( !areEqual( firstChar
, nconst_k
) ){
3616 //splitting on demand : try to make them disequal
3618 firstChar
, nconst_k
, "S-Split(DEQL-Const)", false))
3624 Node sk
= d_sk_cache
.mkSkolemCached(
3625 nconst_k
, firstChar
, SkolemCache::SK_ID_DC_SPT
, "dc_spt");
3626 registerLength(sk
, LENGTH_ONE
);
3628 d_sk_cache
.mkSkolemCached(nconst_k
,
3630 SkolemCache::SK_ID_DC_SPT_REM
,
3632 Node eq1
= nconst_k
.eqNode( NodeManager::currentNM()->mkNode( kind::STRING_CONCAT
, sk
, skr
) );
3633 eq1
= Rewriter::rewrite( eq1
);
3634 Node eq2
= nconst_k
.eqNode( NodeManager::currentNM()->mkNode( kind::STRING_CONCAT
, firstChar
, skr
) );
3635 std::vector
< Node
> antec
;
3636 antec
.insert( antec
.end(), d_normal_forms_exp
[ni
].begin(), d_normal_forms_exp
[ni
].end() );
3637 antec
.insert( antec
.end(), d_normal_forms_exp
[nj
].begin(), d_normal_forms_exp
[nj
].end() );
3638 antec
.push_back( nconst_k
.eqNode( d_emptyString
).negate() );
3639 sendInference( antec
, NodeManager::currentNM()->mkNode( kind::OR
,
3640 NodeManager::currentNM()->mkNode( kind::AND
, eq1
, sk
.eqNode( firstChar
).negate() ), eq2
), "D-DISL-CSplit" );
3641 d_pending_req_phase
[ eq1
] = true;
3646 Trace("strings-solve") << "Non-Simple Case 1 : add lemma " << std::endl
;
3648 std::vector
< Node
> antec
;
3649 std::vector
< Node
> antec_new_lits
;
3650 antec
.insert( antec
.end(), d_normal_forms_exp
[ni
].begin(), d_normal_forms_exp
[ni
].end() );
3651 antec
.insert( antec
.end(), d_normal_forms_exp
[nj
].begin(), d_normal_forms_exp
[nj
].end() );
3653 if( areDisequal( ni
, nj
) ){
3654 antec
.push_back( ni
.eqNode( nj
).negate() );
3656 antec_new_lits
.push_back( ni
.eqNode( nj
).negate() );
3658 antec_new_lits
.push_back( li
.eqNode( lj
).negate() );
3659 std::vector
< Node
> conc
;
3660 Node sk1
= d_sk_cache
.mkSkolemCached(
3661 i
, j
, SkolemCache::SK_ID_DEQ_X
, "x_dsplit");
3662 Node sk2
= d_sk_cache
.mkSkolemCached(
3663 i
, j
, SkolemCache::SK_ID_DEQ_Y
, "y_dsplit");
3664 Node sk3
= d_sk_cache
.mkSkolemCached(
3665 i
, j
, SkolemCache::SK_ID_DEQ_Z
, "z_dsplit");
3666 registerLength(sk3
, LENGTH_GEQ_ONE
);
3667 //Node nemp = sk3.eqNode(d_emptyString).negate();
3668 //conc.push_back(nemp);
3669 Node lsk1
= mkLength( sk1
);
3670 conc
.push_back( lsk1
.eqNode( li
) );
3671 Node lsk2
= mkLength( sk2
);
3672 conc
.push_back( lsk2
.eqNode( lj
) );
3673 conc
.push_back( NodeManager::currentNM()->mkNode( kind::OR
, j
.eqNode( mkConcat( sk1
, sk3
) ), i
.eqNode( mkConcat( sk2
, sk3
) ) ) );
3674 sendInference( antec
, antec_new_lits
, NodeManager::currentNM()->mkNode( kind::AND
, conc
), "D-DISL-Split" );
3675 ++(d_statistics
.d_deq_splits
);
3678 }else if( areEqual( li
, lj
) ){
3679 Assert( !areDisequal( i
, j
) );
3680 //splitting on demand : try to make them disequal
3681 if (sendSplit(i
, j
, "S-Split(DEQL)", false))
3686 //splitting on demand : try to make lengths equal
3687 if (sendSplit(li
, lj
, "D-Split"))
3700 int TheoryStrings::processReverseDeq( std::vector
< Node
>& nfi
, std::vector
< Node
>& nfj
, Node ni
, Node nj
) {
3701 //reverse normal form of i, j
3702 std::reverse( nfi
.begin(), nfi
.end() );
3703 std::reverse( nfj
.begin(), nfj
.end() );
3706 int ret
= processSimpleDeq( nfi
, nfj
, ni
, nj
, index
, true );
3708 //reverse normal form of i, j
3709 std::reverse( nfi
.begin(), nfi
.end() );
3710 std::reverse( nfj
.begin(), nfj
.end() );
3715 int TheoryStrings::processSimpleDeq( std::vector
< Node
>& nfi
, std::vector
< Node
>& nfj
, Node ni
, Node nj
, unsigned& index
, bool isRev
){
3716 // See if one side is constant, if so, the disequality ni != nj is satisfied
3717 // since ni does not contain nj or vice versa.
3718 // This is only valid when isRev is false, since when isRev=true, the contents
3719 // of normal form vectors nfi and nfj are reversed.
3722 for (unsigned i
= 0; i
< 2; i
++)
3724 Node c
= getConstantEqc(i
== 0 ? ni
: nj
);
3728 if (!TheoryStringsRewriter::canConstantContainList(
3729 c
, i
== 0 ? nfj
: nfi
, findex
, lindex
))
3731 Trace("strings-solve-debug")
3732 << "Disequality: constant cannot contain list" << std::endl
;
3738 while( index
<nfi
.size() || index
<nfj
.size() ) {
3739 if( index
>=nfi
.size() || index
>=nfj
.size() ){
3740 Trace("strings-solve-debug") << "Disequality normalize empty" << std::endl
;
3741 std::vector
< Node
> ant
;
3742 //we have a conflict : because the lengths are equal, the remainder needs to be empty, which will lead to a conflict
3743 Node lni
= getLengthExp( ni
, ant
, d_normal_forms_base
[ni
] );
3744 Node lnj
= getLengthExp( nj
, ant
, d_normal_forms_base
[nj
] );
3745 ant
.push_back( lni
.eqNode( lnj
) );
3746 ant
.insert( ant
.end(), d_normal_forms_exp
[ni
].begin(), d_normal_forms_exp
[ni
].end() );
3747 ant
.insert( ant
.end(), d_normal_forms_exp
[nj
].begin(), d_normal_forms_exp
[nj
].end() );
3748 std::vector
< Node
> cc
;
3749 std::vector
< Node
>& nfk
= index
>=nfi
.size() ? nfj
: nfi
;
3750 for( unsigned index_k
=index
; index_k
<nfk
.size(); index_k
++ ){
3751 cc
.push_back( nfk
[index_k
].eqNode( d_emptyString
) );
3753 Node conc
= cc
.size()==1 ? cc
[0] : NodeManager::currentNM()->mkNode( kind::AND
, cc
);
3754 conc
= Rewriter::rewrite( conc
);
3755 sendInference( ant
, conc
, "Disequality Normalize Empty", true);
3758 Node i
= nfi
[index
];
3759 Node j
= nfj
[index
];
3760 Trace("strings-solve-debug") << "...Processing(QED) " << i
<< " " << j
<< std::endl
;
3761 if( !areEqual( i
, j
) ) {
3762 if( i
.getKind()==kind::CONST_STRING
&& j
.getKind()==kind::CONST_STRING
) {
3763 unsigned int len_short
= i
.getConst
<String
>().size() < j
.getConst
<String
>().size() ? i
.getConst
<String
>().size() : j
.getConst
<String
>().size();
3764 bool isSameFix
= isRev
? i
.getConst
<String
>().rstrncmp(j
.getConst
<String
>(), len_short
): i
.getConst
<String
>().strncmp(j
.getConst
<String
>(), len_short
);
3766 //same prefix/suffix
3767 //k is the index of the string that is shorter
3768 Node nk
= i
.getConst
<String
>().size() < j
.getConst
<String
>().size() ? i
: j
;
3769 Node nl
= i
.getConst
<String
>().size() < j
.getConst
<String
>().size() ? j
: i
;
3772 int new_len
= nl
.getConst
<String
>().size() - len_short
;
3773 remainderStr
= NodeManager::currentNM()->mkConst( nl
.getConst
<String
>().substr(0, new_len
) );
3774 Trace("strings-solve-debug-test") << "Rev. Break normal form of " << nl
<< " into " << nk
<< ", " << remainderStr
<< std::endl
;
3776 remainderStr
= NodeManager::currentNM()->mkConst( nl
.getConst
<String
>().substr( len_short
) );
3777 Trace("strings-solve-debug-test") << "Break normal form of " << nl
<< " into " << nk
<< ", " << remainderStr
<< std::endl
;
3779 if( i
.getConst
<String
>().size() < j
.getConst
<String
>().size() ) {
3780 nfj
.insert( nfj
.begin() + index
+ 1, remainderStr
);
3781 nfj
[index
] = nfi
[index
];
3783 nfi
.insert( nfi
.begin() + index
+ 1, remainderStr
);
3784 nfi
[index
] = nfj
[index
];
3790 std::vector
< Node
> lexp
;
3791 Node li
= getLength( i
, lexp
);
3792 Node lj
= getLength( j
, lexp
);
3793 if( areEqual( li
, lj
) && areDisequal( i
, j
) ){
3794 Trace("strings-solve") << "Simple Case 2 : found equal length disequal sub strings " << i
<< " " << j
<< std::endl
;
3795 //we are done: D-Remove
3808 void TheoryStrings::addNormalFormPair( Node n1
, Node n2
){
3809 if( !isNormalFormPair( n1
, n2
) ){
3811 NodeIntMap::const_iterator it
= d_nf_pairs
.find( n1
);
3812 if( it
!=d_nf_pairs
.end() ){
3813 index
= (*it
).second
;
3815 d_nf_pairs
[n1
] = index
+ 1;
3816 if( index
<(int)d_nf_pairs_data
[n1
].size() ){
3817 d_nf_pairs_data
[n1
][index
] = n2
;
3819 d_nf_pairs_data
[n1
].push_back( n2
);
3821 Assert( isNormalFormPair( n1
, n2
) );
3823 Trace("strings-nf-debug") << "Already a normal form pair " << n1
<< " " << n2
<< std::endl
;
3827 bool TheoryStrings::isNormalFormPair( Node n1
, Node n2
) {
3828 //TODO: modulo equality?
3829 return isNormalFormPair2( n1
, n2
) || isNormalFormPair2( n2
, n1
);
3832 bool TheoryStrings::isNormalFormPair2( Node n1
, Node n2
) {
3833 //Trace("strings-debug") << "is normal form pair. " << n1 << " " << n2 << std::endl;
3834 NodeIntMap::const_iterator it
= d_nf_pairs
.find( n1
);
3835 if( it
!=d_nf_pairs
.end() ){
3836 Assert( d_nf_pairs_data
.find( n1
)!=d_nf_pairs_data
.end() );
3837 for( int i
=0; i
<(*it
).second
; i
++ ){
3838 Assert( i
<(int)d_nf_pairs_data
[n1
].size() );
3839 if( d_nf_pairs_data
[n1
][i
]==n2
){
3847 void TheoryStrings::registerTerm( Node n
, int effort
) {
3848 TypeNode tn
= n
.getType();
3849 bool do_register
= true;
3852 if (options::stringEagerLen())
3854 do_register
= effort
== 0;
3858 do_register
= effort
> 0 || n
.getKind() != STRING_CONCAT
;
3865 if (d_registered_terms_cache
.find(n
) != d_registered_terms_cache
.end())
3869 d_registered_terms_cache
.insert(n
);
3870 NodeManager
* nm
= NodeManager::currentNM();
3871 Debug("strings-register") << "TheoryStrings::registerTerm() " << n
3872 << ", effort = " << effort
<< std::endl
;
3875 // register length information:
3876 // for variables, split on empty vs positive length
3877 // for concat/const/replace, introduce proxy var and state length relation
3879 if (n
.getKind() != STRING_CONCAT
&& n
.getKind() != CONST_STRING
)
3881 Node lsumb
= nm
->mkNode(STRING_LENGTH
, n
);
3882 lsum
= Rewriter::rewrite(lsumb
);
3883 // can register length term if it does not rewrite
3886 registerLength(n
, LENGTH_SPLIT
);
3890 Node sk
= d_sk_cache
.mkSkolemCached(n
, SkolemCache::SK_PURIFY
, "lsym");
3891 StringsProxyVarAttribute spva
;
3892 sk
.setAttribute(spva
, true);
3893 Node eq
= Rewriter::rewrite(sk
.eqNode(n
));
3894 Trace("strings-lemma") << "Strings::Lemma LENGTH Term : " << eq
3896 d_proxy_var
[n
] = sk
;
3897 Trace("strings-assert") << "(assert " << eq
<< ")" << std::endl
;
3899 Node skl
= nm
->mkNode(STRING_LENGTH
, sk
);
3900 if (n
.getKind() == STRING_CONCAT
)
3902 std::vector
<Node
> node_vec
;
3903 for (unsigned i
= 0; i
< n
.getNumChildren(); i
++)
3905 if (n
[i
].getAttribute(StringsProxyVarAttribute()))
3907 Assert(d_proxy_var_to_length
.find(n
[i
])
3908 != d_proxy_var_to_length
.end());
3909 node_vec
.push_back(d_proxy_var_to_length
[n
[i
]]);
3913 Node lni
= nm
->mkNode(STRING_LENGTH
, n
[i
]);
3914 node_vec
.push_back(lni
);
3917 lsum
= nm
->mkNode(PLUS
, node_vec
);
3918 lsum
= Rewriter::rewrite(lsum
);
3920 else if (n
.getKind() == CONST_STRING
)
3922 lsum
= nm
->mkConst(Rational(n
.getConst
<String
>().size()));
3924 Assert(!lsum
.isNull());
3925 d_proxy_var_to_length
[sk
] = lsum
;
3926 Node ceq
= Rewriter::rewrite(skl
.eqNode(lsum
));
3927 Trace("strings-lemma") << "Strings::Lemma LENGTH : " << ceq
<< std::endl
;
3928 Trace("strings-lemma-debug")
3929 << " prerewrite : " << skl
.eqNode(lsum
) << std::endl
;
3930 Trace("strings-assert") << "(assert " << ceq
<< ")" << std::endl
;
3933 else if (n
.getKind() == STRING_CODE
)
3935 d_has_str_code
= true;
3936 // ite( str.len(s)==1, 0 <= str.code(s) < num_codes, str.code(s)=-1 )
3937 Node code_len
= mkLength(n
[0]).eqNode(d_one
);
3938 Node code_eq_neg1
= n
.eqNode(d_neg_one
);
3939 Node code_range
= nm
->mkNode(
3941 nm
->mkNode(GEQ
, n
, d_zero
),
3942 nm
->mkNode(LT
, n
, nm
->mkConst(Rational(CVC4::String::num_codes()))));
3943 Node lem
= nm
->mkNode(ITE
, code_len
, code_range
, code_eq_neg1
);
3944 Trace("strings-lemma") << "Strings::Lemma CODE : " << lem
<< std::endl
;
3945 Trace("strings-assert") << "(assert " << lem
<< ")" << std::endl
;
3950 bool TheoryStrings::sendInternalInference(std::vector
<Node
>& exp
,
3954 if (conc
.getKind() == AND
3955 || (conc
.getKind() == NOT
&& conc
[0].getKind() == OR
))
3957 Node conj
= conc
.getKind() == AND
? conc
: conc
[0];
3958 bool pol
= conc
.getKind() == AND
;
3960 for (const Node
& cc
: conj
)
3962 bool retc
= sendInternalInference(exp
, pol
? cc
: cc
.negate(), c
);
3967 bool pol
= conc
.getKind() != NOT
;
3968 Node lit
= pol
? conc
: conc
[0];
3969 if (lit
.getKind() == EQUAL
)
3971 for (unsigned i
= 0; i
< 2; i
++)
3973 if (!lit
[i
].isConst() && !hasTerm(lit
[i
]))
3975 // introduces a new non-constant term, do not infer
3979 // does it already hold?
3980 if (pol
? areEqual(lit
[0], lit
[1]) : areDisequal(lit
[0], lit
[1]))
3985 else if (lit
.isConst())
3987 if (lit
.getConst
<bool>())
3994 else if (!hasTerm(lit
))
3996 // introduces a new non-constant term, do not infer
3999 else if (areEqual(lit
, pol
? d_true
: d_false
))
4004 sendInference(exp
, conc
, c
);
4008 void TheoryStrings::sendInference( std::vector
< Node
>& exp
, std::vector
< Node
>& exp_n
, Node eq
, const char * c
, bool asLemma
) {
4009 eq
= eq
.isNull() ? d_false
: Rewriter::rewrite( eq
);
4011 if( Trace
.isOn("strings-infer-debug") ){
4012 Trace("strings-infer-debug") << "By " << c
<< ", infer : " << eq
<< " from: " << std::endl
;
4013 for( unsigned i
=0; i
<exp
.size(); i
++ ){
4014 Trace("strings-infer-debug") << " " << exp
[i
] << std::endl
;
4016 for( unsigned i
=0; i
<exp_n
.size(); i
++ ){
4017 Trace("strings-infer-debug") << " N:" << exp_n
[i
] << std::endl
;
4019 //Trace("strings-infer-debug") << "as lemma : " << asLemma << std::endl;
4021 //check if we should send a lemma or an inference
4022 if( asLemma
|| eq
==d_false
|| eq
.getKind()==kind::OR
|| !exp_n
.empty() || options::stringInferAsLemmas() ){
4024 if( options::stringRExplainLemmas() ){
4025 eq_exp
= mkExplain( exp
, exp_n
);
4028 eq_exp
= mkAnd( exp_n
);
4029 }else if( exp_n
.empty() ){
4030 eq_exp
= mkAnd( exp
);
4032 std::vector
< Node
> ev
;
4033 ev
.insert( ev
.end(), exp
.begin(), exp
.end() );
4034 ev
.insert( ev
.end(), exp_n
.begin(), exp_n
.end() );
4035 eq_exp
= NodeManager::currentNM()->mkNode( kind::AND
, ev
);
4038 // if we have unexplained literals, this lemma is not a conflict
4039 if (eq
== d_false
&& !exp_n
.empty())
4041 eq
= eq_exp
.negate();
4044 sendLemma( eq_exp
, eq
, c
);
4046 sendInfer( mkAnd( exp
), eq
, c
);
4051 void TheoryStrings::sendInference( std::vector
< Node
>& exp
, Node eq
, const char * c
, bool asLemma
) {
4052 std::vector
< Node
> exp_n
;
4053 sendInference( exp
, exp_n
, eq
, c
, asLemma
);
4056 void TheoryStrings::sendLemma( Node ant
, Node conc
, const char * c
) {
4057 if( conc
.isNull() || conc
== d_false
) {
4058 Trace("strings-conflict") << "Strings::Conflict : " << c
<< " : " << ant
<< std::endl
;
4059 Trace("strings-lemma") << "Strings::Conflict : " << c
<< " : " << ant
<< std::endl
;
4060 Trace("strings-assert") << "(assert (not " << ant
<< ")) ; conflict " << c
<< std::endl
;
4061 d_out
->conflict(ant
);
4065 if( ant
== d_true
) {
4068 lem
= NodeManager::currentNM()->mkNode( kind::IMPLIES
, ant
, conc
);
4070 Trace("strings-lemma") << "Strings::Lemma " << c
<< " : " << lem
<< std::endl
;
4071 Trace("strings-assert") << "(assert " << lem
<< ") ; lemma " << c
<< std::endl
;
4072 d_lemma_cache
.push_back( lem
);
4076 void TheoryStrings::sendInfer( Node eq_exp
, Node eq
, const char * c
) {
4077 if( options::stringInferSym() ){
4078 std::vector
< Node
> vars
;
4079 std::vector
< Node
> subs
;
4080 std::vector
< Node
> unproc
;
4081 inferSubstitutionProxyVars( eq_exp
, vars
, subs
, unproc
);
4082 if( unproc
.empty() ){
4083 Trace("strings-lemma-debug") << "Strings::Infer " << eq
<< " from " << eq_exp
<< " by " << c
<< std::endl
;
4084 Node eqs
= eq
.substitute( vars
.begin(), vars
.end(), subs
.begin(), subs
.end() );
4085 Trace("strings-lemma-debug") << "Strings::Infer Alternate : " << eqs
<< std::endl
;
4086 for( unsigned i
=0; i
<vars
.size(); i
++ ){
4087 Trace("strings-lemma-debug") << " " << vars
[i
] << " -> " << subs
[i
] << std::endl
;
4089 sendLemma( d_true
, eqs
, c
);
4092 for( unsigned i
=0; i
<unproc
.size(); i
++ ){
4093 Trace("strings-lemma-debug") << " non-trivial exp : " << unproc
[i
] << std::endl
;
4097 Trace("strings-lemma") << "Strings::Infer " << eq
<< " from " << eq_exp
<< " by " << c
<< std::endl
;
4098 Trace("strings-assert") << "(assert (=> " << eq_exp
<< " " << eq
<< ")) ; infer " << c
<< std::endl
;
4099 d_pending
.push_back( eq
);
4100 d_pending_exp
[eq
] = eq_exp
;
4101 d_infer
.push_back( eq
);
4102 d_infer_exp
.push_back( eq_exp
);
4105 bool TheoryStrings::sendSplit(Node a
, Node b
, const char* c
, bool preq
)
4107 Node eq
= a
.eqNode( b
);
4108 eq
= Rewriter::rewrite( eq
);
4111 Node neq
= NodeManager::currentNM()->mkNode(kind::NOT
, eq
);
4112 Node lemma_or
= NodeManager::currentNM()->mkNode(kind::OR
, eq
, neq
);
4113 Trace("strings-lemma") << "Strings::Lemma " << c
<< " SPLIT : " << lemma_or
4115 d_lemma_cache
.push_back(lemma_or
);
4116 d_pending_req_phase
[eq
] = preq
;
4117 ++(d_statistics
.d_splits
);
4123 void TheoryStrings::registerLength(Node n
, LengthStatus s
)
4125 if (d_length_lemma_terms_cache
.find(n
) != d_length_lemma_terms_cache
.end())
4129 d_length_lemma_terms_cache
.insert(n
);
4131 NodeManager
* nm
= NodeManager::currentNM();
4132 Node n_len
= nm
->mkNode(kind::STRING_LENGTH
, n
);
4134 if (s
== LENGTH_GEQ_ONE
)
4136 Node neq_empty
= n
.eqNode(d_emptyString
).negate();
4137 Node len_n_gt_z
= nm
->mkNode(GT
, n_len
, d_zero
);
4138 Node len_geq_one
= nm
->mkNode(AND
, neq_empty
, len_n_gt_z
);
4139 Trace("strings-lemma") << "Strings::Lemma SK-GEQ-ONE : " << len_geq_one
4141 Trace("strings-assert") << "(assert " << len_geq_one
<< ")" << std::endl
;
4142 d_out
->lemma(len_geq_one
);
4146 if (s
== LENGTH_ONE
)
4148 Node len_one
= n_len
.eqNode(d_one
);
4149 Trace("strings-lemma") << "Strings::Lemma SK-ONE : " << len_one
4151 Trace("strings-assert") << "(assert " << len_one
<< ")" << std::endl
;
4152 d_out
->lemma(len_one
);
4155 Assert(s
== LENGTH_SPLIT
);
4157 if( options::stringSplitEmp() || !options::stringLenGeqZ() ){
4158 Node n_len_eq_z
= n_len
.eqNode( d_zero
);
4159 Node n_len_eq_z_2
= n
.eqNode( d_emptyString
);
4160 Node case_empty
= nm
->mkNode(AND
, n_len_eq_z
, n_len_eq_z_2
);
4161 case_empty
= Rewriter::rewrite(case_empty
);
4162 Node case_nempty
= nm
->mkNode(GT
, n_len
, d_zero
);
4163 if (!case_empty
.isConst())
4165 Node lem
= nm
->mkNode(OR
, case_empty
, case_nempty
);
4167 Trace("strings-lemma") << "Strings::Lemma LENGTH >= 0 : " << lem
4169 // prefer trying the empty case first
4170 // notice that requirePhase must only be called on rewritten literals that
4171 // occur in the CNF stream.
4172 n_len_eq_z
= Rewriter::rewrite(n_len_eq_z
);
4173 Assert(!n_len_eq_z
.isConst());
4174 d_out
->requirePhase(n_len_eq_z
, true);
4175 n_len_eq_z_2
= Rewriter::rewrite(n_len_eq_z_2
);
4176 Assert(!n_len_eq_z_2
.isConst());
4177 d_out
->requirePhase(n_len_eq_z_2
, true);
4179 else if (!case_empty
.getConst
<bool>())
4181 // the rewriter knows that n is non-empty
4182 Trace("strings-lemma")
4183 << "Strings::Lemma LENGTH > 0 (non-empty): " << case_nempty
4185 d_out
->lemma(case_nempty
);
4189 // If n = "" ---> true or len( n ) = 0 ----> true, then we expect that
4190 // n ---> "". Since this method is only called on non-constants n, it must
4191 // be that n = "" ^ len( n ) = 0 does not rewrite to true.
4196 // additionally add len( x ) >= 0 ?
4197 if( options::stringLenGeqZ() ){
4198 Node n_len_geq
= nm
->mkNode(kind::GEQ
, n_len
, d_zero
);
4199 n_len_geq
= Rewriter::rewrite( n_len_geq
);
4200 d_out
->lemma( n_len_geq
);
4204 void TheoryStrings::inferSubstitutionProxyVars( Node n
, std::vector
< Node
>& vars
, std::vector
< Node
>& subs
, std::vector
< Node
>& unproc
) {
4205 if( n
.getKind()==kind::AND
){
4206 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
4207 inferSubstitutionProxyVars( n
[i
], vars
, subs
, unproc
);
4210 }else if( n
.getKind()==kind::EQUAL
){
4211 Node ns
= n
.substitute( vars
.begin(), vars
.end(), subs
.begin(), subs
.end() );
4212 ns
= Rewriter::rewrite( ns
);
4213 if( ns
.getKind()==kind::EQUAL
){
4216 for( unsigned i
=0; i
<2; i
++ ){
4218 if( ns
[i
].getAttribute(StringsProxyVarAttribute()) ){
4220 }else if( ns
[i
].isConst() ){
4221 NodeNodeMap::const_iterator it
= d_proxy_var
.find( ns
[i
] );
4222 if( it
!=d_proxy_var
.end() ){
4228 if( v
.getNumChildren()==0 ){
4232 //both sides involved in proxy var
4243 subs
.push_back( s
);
4244 vars
.push_back( v
);
4252 unproc
.push_back( n
);
4257 Node
TheoryStrings::mkConcat( Node n1
, Node n2
) {
4258 return Rewriter::rewrite( NodeManager::currentNM()->mkNode( kind::STRING_CONCAT
, n1
, n2
) );
4261 Node
TheoryStrings::mkConcat( Node n1
, Node n2
, Node n3
) {
4262 return Rewriter::rewrite( NodeManager::currentNM()->mkNode( kind::STRING_CONCAT
, n1
, n2
, n3
) );
4265 Node
TheoryStrings::mkConcat( const std::vector
< Node
>& c
) {
4266 return Rewriter::rewrite( c
.size()>1 ? NodeManager::currentNM()->mkNode( kind::STRING_CONCAT
, c
) : ( c
.size()==1 ? c
[0] : d_emptyString
) );
4269 Node
TheoryStrings::mkLength( Node t
) {
4270 return Rewriter::rewrite( NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, t
) );
4273 Node
TheoryStrings::mkExplain( std::vector
< Node
>& a
) {
4274 std::vector
< Node
> an
;
4275 return mkExplain( a
, an
);
4278 Node
TheoryStrings::mkExplain( std::vector
< Node
>& a
, std::vector
< Node
>& an
) {
4279 std::vector
< TNode
> antec_exp
;
4280 for( unsigned i
=0; i
<a
.size(); i
++ ) {
4281 if( std::find( a
.begin(), a
.begin() + i
, a
[i
] )==a
.begin() + i
) {
4283 Debug("strings-explain") << "Ask for explanation of " << a
[i
] << std::endl
;
4285 if(a
[i
].getKind() == kind::EQUAL
) {
4286 //Assert( hasTerm(a[i][0]) );
4287 //Assert( hasTerm(a[i][1]) );
4288 Assert( areEqual(a
[i
][0], a
[i
][1]) );
4289 if( a
[i
][0]==a
[i
][1] ){
4292 } else if( a
[i
].getKind()==kind::NOT
&& a
[i
][0].getKind()==kind::EQUAL
) {
4293 Assert( hasTerm(a
[i
][0][0]) );
4294 Assert( hasTerm(a
[i
][0][1]) );
4295 AlwaysAssert( d_equalityEngine
.areDisequal(a
[i
][0][0], a
[i
][0][1], true) );
4296 }else if( a
[i
].getKind() == kind::AND
){
4297 for( unsigned j
=0; j
<a
[i
].getNumChildren(); j
++ ){
4298 a
.push_back( a
[i
][j
] );
4303 unsigned ps
= antec_exp
.size();
4304 explain(a
[i
], antec_exp
);
4305 Debug("strings-explain") << "Done, explanation was : " << std::endl
;
4306 for( unsigned j
=ps
; j
<antec_exp
.size(); j
++ ) {
4307 Debug("strings-explain") << " " << antec_exp
[j
] << std::endl
;
4309 Debug("strings-explain") << std::endl
;
4313 for( unsigned i
=0; i
<an
.size(); i
++ ) {
4314 if( std::find( an
.begin(), an
.begin() + i
, an
[i
] )==an
.begin() + i
){
4315 Debug("strings-explain") << "Add to explanation (new literal) " << an
[i
] << std::endl
;
4316 antec_exp
.push_back(an
[i
]);
4320 if( antec_exp
.empty() ) {
4322 } else if( antec_exp
.size()==1 ) {
4325 ant
= NodeManager::currentNM()->mkNode( kind::AND
, antec_exp
);
4327 //ant = Rewriter::rewrite( ant );
4331 Node
TheoryStrings::mkAnd( std::vector
< Node
>& a
) {
4332 std::vector
< Node
> au
;
4333 for( unsigned i
=0; i
<a
.size(); i
++ ){
4334 if( std::find( au
.begin(), au
.end(), a
[i
] )==au
.end() ){
4335 au
.push_back( a
[i
] );
4340 } else if( au
.size() == 1 ) {
4343 return NodeManager::currentNM()->mkNode( kind::AND
, au
);
4347 void TheoryStrings::getConcatVec( Node n
, std::vector
< Node
>& c
) {
4348 if( n
.getKind()==kind::STRING_CONCAT
) {
4349 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ) {
4350 if( !areEqual( n
[i
], d_emptyString
) ) {
4351 c
.push_back( n
[i
] );
4359 void TheoryStrings::checkNormalFormsDeq()
4361 std::vector
< std::vector
< Node
> > cols
;
4362 std::vector
< Node
> lts
;
4363 std::map
< Node
, std::map
< Node
, bool > > processed
;
4365 //for each pair of disequal strings, must determine whether their lengths are equal or disequal
4366 for( NodeList::const_iterator id
= d_ee_disequalities
.begin(); id
!= d_ee_disequalities
.end(); ++id
) {
4369 for( unsigned i
=0; i
<2; i
++ ){
4370 n
[i
] = d_equalityEngine
.getRepresentative( eq
[i
] );
4372 if( processed
[n
[0]].find( n
[1] )==processed
[n
[0]].end() ){
4373 processed
[n
[0]][n
[1]] = true;
4375 for( unsigned i
=0; i
<2; i
++ ){
4376 EqcInfo
* ei
= getOrMakeEqcInfo( n
[i
], false );
4377 lt
[i
] = ei
? ei
->d_length_term
: Node::null();
4378 if( lt
[i
].isNull() ){
4381 lt
[i
] = NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, lt
[i
] );
4383 if( !areEqual( lt
[0], lt
[1] ) && !areDisequal( lt
[0], lt
[1] ) ){
4384 sendSplit( lt
[0], lt
[1], "DEQ-LENGTH-SP" );
4389 if( !hasProcessed() ){
4390 separateByLength( d_strings_eqc
, cols
, lts
);
4391 for( unsigned i
=0; i
<cols
.size(); i
++ ){
4392 if( cols
[i
].size()>1 && d_lemma_cache
.empty() ){
4393 Trace("strings-solve") << "- Verify disequalities are processed for " << cols
[i
][0] << ", normal form : ";
4394 printConcat( d_normal_forms
[cols
[i
][0]], "strings-solve" );
4395 Trace("strings-solve") << "... #eql = " << cols
[i
].size() << std::endl
;
4396 //must ensure that normal forms are disequal
4397 for( unsigned j
=0; j
<cols
[i
].size(); j
++ ){
4398 for( unsigned k
=(j
+1); k
<cols
[i
].size(); k
++ ){
4399 //for strings that are disequal, but have the same length
4400 if( areDisequal( cols
[i
][j
], cols
[i
][k
] ) ){
4401 Assert( !d_conflict
);
4402 Trace("strings-solve") << "- Compare " << cols
[i
][j
] << " ";
4403 printConcat( d_normal_forms
[cols
[i
][j
]], "strings-solve" );
4404 Trace("strings-solve") << " against " << cols
[i
][k
] << " ";
4405 printConcat( d_normal_forms
[cols
[i
][k
]], "strings-solve" );
4406 Trace("strings-solve") << "..." << std::endl
;
4407 processDeq( cols
[i
][j
], cols
[i
][k
] );
4408 if( hasProcessed() ){
4419 void TheoryStrings::checkLengthsEqc() {
4420 if( options::stringLenNorm() ){
4421 for( unsigned i
=0; i
<d_strings_eqc
.size(); i
++ ){
4422 //if( d_normal_forms[nodes[i]].size()>1 ) {
4423 Trace("strings-process-debug") << "Process length constraints for " << d_strings_eqc
[i
] << std::endl
;
4424 //check if there is a length term for this equivalence class
4425 EqcInfo
* ei
= getOrMakeEqcInfo( d_strings_eqc
[i
], false );
4426 Node lt
= ei
? ei
->d_length_term
: Node::null();
4427 if( !lt
.isNull() ) {
4428 Node llt
= NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, lt
);
4429 //now, check if length normalization has occurred
4430 if( ei
->d_normalized_length
.get().isNull() ) {
4431 Node nf
= mkConcat( d_normal_forms
[d_strings_eqc
[i
]] );
4432 if( Trace
.isOn("strings-process-debug") ){
4433 Trace("strings-process-debug") << " normal form is " << nf
<< " from base " << d_normal_forms_base
[d_strings_eqc
[i
]] << std::endl
;
4434 Trace("strings-process-debug") << " normal form exp is: " << std::endl
;
4435 for( unsigned j
=0; j
<d_normal_forms_exp
[d_strings_eqc
[i
]].size(); j
++ ){
4436 Trace("strings-process-debug") << " " << d_normal_forms_exp
[d_strings_eqc
[i
]][j
] << std::endl
;
4440 //if not, add the lemma
4441 std::vector
< Node
> ant
;
4442 ant
.insert( ant
.end(), d_normal_forms_exp
[d_strings_eqc
[i
]].begin(), d_normal_forms_exp
[d_strings_eqc
[i
]].end() );
4443 ant
.push_back( d_normal_forms_base
[d_strings_eqc
[i
]].eqNode( lt
) );
4444 Node lc
= NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, nf
);
4445 Node lcr
= Rewriter::rewrite( lc
);
4446 Trace("strings-process-debug") << "Rewrote length " << lc
<< " to " << lcr
<< std::endl
;
4447 Node eq
= llt
.eqNode( lcr
);
4449 ei
->d_normalized_length
.set( eq
);
4450 sendInference( ant
, eq
, "LEN-NORM", true );
4454 Trace("strings-process-debug") << "No length term for eqc " << d_strings_eqc
[i
] << " " << d_eqc_to_len_term
[d_strings_eqc
[i
]] << std::endl
;
4455 if( !options::stringEagerLen() ){
4456 Node c
= mkConcat( d_normal_forms
[d_strings_eqc
[i
]] );
4457 registerTerm( c
, 3 );
4460 NodeNodeMap::const_iterator it = d_proxy_var.find( c );
4461 if( it!=d_proxy_var.end() ){
4462 Node pv = (*it).second;
4463 Assert( d_proxy_var_to_length.find( pv )!=d_proxy_var_to_length.end() );
4464 Node pvl = d_proxy_var_to_length[pv];
4465 Node ceq = Rewriter::rewrite( mkLength( pv ).eqNode( pvl ) );
4466 sendInference( d_empty_vec, ceq, "LEN-NORM-I", true );
4473 // Trace("strings-process-debug") << "Do not process length constraints for " << nodes[i] << " " << d_normal_forms[nodes[i]].size() << std::endl;
4479 void TheoryStrings::checkCardinality() {
4480 //int cardinality = options::stringCharCardinality();
4481 //Trace("strings-solve-debug2") << "get cardinality: " << cardinality << endl;
4483 //AJR: this will create a partition of eqc, where each collection has length that are pairwise propagated to be equal.
4484 // 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).
4485 // TODO: revisit this?
4486 std::vector
< std::vector
< Node
> > cols
;
4487 std::vector
< Node
> lts
;
4488 separateByLength( d_strings_eqc
, cols
, lts
);
4490 Trace("strings-card") << "Check cardinality...." << std::endl
;
4491 for( unsigned i
= 0; i
<cols
.size(); ++i
) {
4493 Trace("strings-card") << "Number of strings with length equal to " << lr
<< " is " << cols
[i
].size() << std::endl
;
4494 if( cols
[i
].size() > 1 ) {
4496 unsigned card_need
= 1;
4497 double curr
= (double)cols
[i
].size();
4498 while( curr
>d_card_size
){
4499 curr
= curr
/(double)d_card_size
;
4502 Trace("strings-card") << "Need length " << card_need
<< " for this number of strings (where alphabet size is " << d_card_size
<< ")." << std::endl
;
4503 //check if we need to split
4504 bool needsSplit
= true;
4506 // if constant, compare
4507 Node cmp
= NodeManager::currentNM()->mkNode( kind::GEQ
, lr
, NodeManager::currentNM()->mkConst( Rational( card_need
) ) );
4508 cmp
= Rewriter::rewrite( cmp
);
4509 needsSplit
= cmp
!=d_true
;
4511 // find the minimimum constant that we are unknown to be disequal from, or otherwise stop if we increment such that cardinality does not apply
4513 bool success
= true;
4514 while( r
<card_need
&& success
){
4515 Node rr
= NodeManager::currentNM()->mkConst
<Rational
>( Rational(r
) );
4516 if( areDisequal( rr
, lr
) ){
4523 Trace("strings-card") << "Symbolic length " << lr
<< " must be at least " << r
<< " due to constant disequalities." << std::endl
;
4525 needsSplit
= r
<card_need
;
4529 unsigned int int_k
= (unsigned int)card_need
;
4530 for( std::vector
< Node
>::iterator itr1
= cols
[i
].begin();
4531 itr1
!= cols
[i
].end(); ++itr1
) {
4532 for( std::vector
< Node
>::iterator itr2
= itr1
+ 1;
4533 itr2
!= cols
[i
].end(); ++itr2
) {
4534 if(!areDisequal( *itr1
, *itr2
)) {
4536 if (sendSplit(*itr1
, *itr2
, "CARD-SP"))
4543 EqcInfo
* ei
= getOrMakeEqcInfo( lr
, true );
4544 Trace("strings-card") << "Previous cardinality used for " << lr
<< " is " << ((int)ei
->d_cardinality_lem_k
.get()-1) << std::endl
;
4545 if( int_k
+1 > ei
->d_cardinality_lem_k
.get() ){
4546 Node k_node
= NodeManager::currentNM()->mkConst( ::CVC4::Rational( int_k
) );
4547 //add cardinality lemma
4548 Node dist
= NodeManager::currentNM()->mkNode( kind::DISTINCT
, cols
[i
] );
4549 std::vector
< Node
> vec_node
;
4550 vec_node
.push_back( dist
);
4551 for( std::vector
< Node
>::iterator itr1
= cols
[i
].begin();
4552 itr1
!= cols
[i
].end(); ++itr1
) {
4553 Node len
= NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, *itr1
);
4555 Node len_eq_lr
= len
.eqNode(lr
);
4556 vec_node
.push_back( len_eq_lr
);
4559 Node len
= NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, cols
[i
][0] );
4560 Node cons
= NodeManager::currentNM()->mkNode( kind::GEQ
, len
, k_node
);
4561 cons
= Rewriter::rewrite( cons
);
4562 ei
->d_cardinality_lem_k
.set( int_k
+1 );
4564 sendInference( d_empty_vec
, vec_node
, cons
, "CARDINALITY", true );
4571 Trace("strings-card") << "...end check cardinality" << std::endl
;
4574 void TheoryStrings::getEquivalenceClasses( std::vector
< Node
>& eqcs
) {
4575 eq::EqClassesIterator eqcs_i
= eq::EqClassesIterator( &d_equalityEngine
);
4576 while( !eqcs_i
.isFinished() ) {
4577 Node eqc
= (*eqcs_i
);
4578 //if eqc.getType is string
4579 if (eqc
.getType().isString()) {
4580 eqcs
.push_back( eqc
);
4586 void TheoryStrings::separateByLength(std::vector
< Node
>& n
,
4587 std::vector
< std::vector
< Node
> >& cols
,
4588 std::vector
< Node
>& lts
) {
4589 unsigned leqc_counter
= 0;
4590 std::map
< Node
, unsigned > eqc_to_leqc
;
4591 std::map
< unsigned, Node
> leqc_to_eqc
;
4592 std::map
< unsigned, std::vector
< Node
> > eqc_to_strings
;
4593 for( unsigned i
=0; i
<n
.size(); i
++ ) {
4595 Assert( d_equalityEngine
.getRepresentative(eqc
)==eqc
);
4596 EqcInfo
* ei
= getOrMakeEqcInfo( eqc
, false );
4597 Node lt
= ei
? ei
->d_length_term
: Node::null();
4599 lt
= NodeManager::currentNM()->mkNode( kind::STRING_LENGTH
, lt
);
4600 Node r
= d_equalityEngine
.getRepresentative( lt
);
4601 if( eqc_to_leqc
.find( r
)==eqc_to_leqc
.end() ){
4602 eqc_to_leqc
[r
] = leqc_counter
;
4603 leqc_to_eqc
[leqc_counter
] = r
;
4606 eqc_to_strings
[ eqc_to_leqc
[r
] ].push_back( eqc
);
4608 eqc_to_strings
[leqc_counter
].push_back( eqc
);
4612 for( std::map
< unsigned, std::vector
< Node
> >::iterator it
= eqc_to_strings
.begin(); it
!= eqc_to_strings
.end(); ++it
){
4613 cols
.push_back( std::vector
< Node
>() );
4614 cols
.back().insert( cols
.back().end(), it
->second
.begin(), it
->second
.end() );
4615 lts
.push_back( leqc_to_eqc
[it
->first
] );
4619 void TheoryStrings::printConcat( std::vector
< Node
>& n
, const char * c
) {
4620 for( unsigned i
=0; i
<n
.size(); i
++ ){
4621 if( i
>0 ) Trace(c
) << " ++ ";
4627 //// Finite Model Finding
4629 TheoryStrings::StringSumLengthDecisionStrategy::StringSumLengthDecisionStrategy(
4630 context::Context
* c
, context::UserContext
* u
, Valuation valuation
)
4631 : DecisionStrategyFmf(c
, valuation
), d_input_var_lsum(u
)
4635 bool TheoryStrings::StringSumLengthDecisionStrategy::isInitialized()
4637 return !d_input_var_lsum
.get().isNull();
4640 void TheoryStrings::StringSumLengthDecisionStrategy::initialize(
4641 const std::vector
<Node
>& vars
)
4643 if (d_input_var_lsum
.get().isNull() && !vars
.empty())
4645 NodeManager
* nm
= NodeManager::currentNM();
4646 std::vector
<Node
> sum
;
4647 for (const Node
& v
: vars
)
4649 sum
.push_back(nm
->mkNode(STRING_LENGTH
, v
));
4651 Node sumn
= sum
.size() == 1 ? sum
[0] : nm
->mkNode(PLUS
, sum
);
4652 d_input_var_lsum
.set(sumn
);
4656 Node
TheoryStrings::StringSumLengthDecisionStrategy::mkLiteral(unsigned i
)
4658 if (d_input_var_lsum
.get().isNull())
4660 return Node::null();
4662 NodeManager
* nm
= NodeManager::currentNM();
4663 Node lit
= nm
->mkNode(LEQ
, d_input_var_lsum
.get(), nm
->mkConst(Rational(i
)));
4664 Trace("strings-fmf") << "StringsFMF::mkLiteral: " << lit
<< std::endl
;
4667 std::string
TheoryStrings::StringSumLengthDecisionStrategy::identify() const
4669 return std::string("string_sum_len");
4672 Node
TheoryStrings::ppRewrite(TNode atom
) {
4673 Trace("strings-ppr") << "TheoryStrings::ppRewrite " << atom
<< std::endl
;
4675 if (options::regExpElim() && atom
.getKind() == STRING_IN_REGEXP
)
4677 // aggressive elimination of regular expression membership
4678 atomElim
= d_regexp_elim
.eliminate(atom
);
4679 if (!atomElim
.isNull())
4681 Trace("strings-ppr") << " rewrote " << atom
<< " -> " << atomElim
4682 << " via regular expression elimination."
4687 if( !options::stringLazyPreproc() ){
4688 //eager preprocess here
4689 std::vector
< Node
> new_nodes
;
4690 Node ret
= d_preproc
.processAssertion( atom
, new_nodes
);
4692 Trace("strings-ppr") << " rewrote " << atom
<< " -> " << ret
<< ", with " << new_nodes
.size() << " lemmas." << std::endl
;
4693 for( unsigned i
=0; i
<new_nodes
.size(); i
++ ){
4694 Trace("strings-ppr") << " lemma : " << new_nodes
[i
] << std::endl
;
4695 d_out
->lemma( new_nodes
[i
] );
4699 Assert( new_nodes
.empty() );
4706 TheoryStrings::Statistics::Statistics()
4707 : d_splits("theory::strings::NumOfSplitOnDemands", 0),
4708 d_eq_splits("theory::strings::NumOfEqSplits", 0),
4709 d_deq_splits("theory::strings::NumOfDiseqSplits", 0),
4710 d_loop_lemmas("theory::strings::NumOfLoops", 0)
4712 smtStatisticsRegistry()->registerStat(&d_splits
);
4713 smtStatisticsRegistry()->registerStat(&d_eq_splits
);
4714 smtStatisticsRegistry()->registerStat(&d_deq_splits
);
4715 smtStatisticsRegistry()->registerStat(&d_loop_lemmas
);
4718 TheoryStrings::Statistics::~Statistics(){
4719 smtStatisticsRegistry()->unregisterStat(&d_splits
);
4720 smtStatisticsRegistry()->unregisterStat(&d_eq_splits
);
4721 smtStatisticsRegistry()->unregisterStat(&d_deq_splits
);
4722 smtStatisticsRegistry()->unregisterStat(&d_loop_lemmas
);
4744 //// Regular Expressions
4747 unsigned TheoryStrings::getNumMemberships( Node n
, bool isPos
) {
4749 NodeIntMap::const_iterator it
= d_pos_memberships
.find( n
);
4750 if( it
!=d_pos_memberships
.end() ){
4751 return (*it
).second
;
4754 NodeIntMap::const_iterator it
= d_neg_memberships
.find( n
);
4755 if( it
!=d_neg_memberships
.end() ){
4756 return (*it
).second
;
4762 Node
TheoryStrings::getMembership( Node n
, bool isPos
, unsigned i
) {
4763 return isPos
? d_pos_memberships_data
[n
][i
] : d_neg_memberships_data
[n
][i
];
4766 Node
TheoryStrings::mkRegExpAntec(Node atom
, Node ant
) {
4767 if(d_regexp_ant
.find(atom
) == d_regexp_ant
.end()) {
4768 return NodeManager::currentNM()->mkNode(kind::AND
, ant
, atom
);
4770 Node n
= d_regexp_ant
[atom
];
4771 return NodeManager::currentNM()->mkNode(kind::AND
, ant
, n
);
4775 void TheoryStrings::checkMemberships() {
4776 //add the memberships
4777 std::vector
<Node
> mems
= getExtTheory()->getActive(kind::STRING_IN_REGEXP
);
4778 for (unsigned i
= 0; i
< mems
.size(); i
++) {
4780 Assert( d_extf_info_tmp
.find( n
)!=d_extf_info_tmp
.end() );
4781 if (!d_extf_info_tmp
[n
].d_const
.isNull())
4783 bool pol
= d_extf_info_tmp
[n
].d_const
.getConst
<bool>();
4784 Trace("strings-process-debug") << " add membership : " << n
<< ", pol = " << pol
<< std::endl
;
4785 addMembership( pol
? n
: n
.negate() );
4787 Trace("strings-process-debug") << " irrelevant (non-asserted) membership : " << n
<< std::endl
;
4791 bool addedLemma
= false;
4792 bool changed
= false;
4793 std::vector
< Node
> processed
;
4794 std::vector
< Node
> cprocessed
;
4796 Trace("regexp-debug") << "Checking Memberships ... " << std::endl
;
4797 //if(options::stringEIT()) {
4798 //TODO: Opt for normal forms
4799 for( NodeIntMap::const_iterator itr_xr
= d_pos_memberships
.begin(); itr_xr
!= d_pos_memberships
.end(); ++itr_xr
){
4800 bool spflag
= false;
4801 Node x
= (*itr_xr
).first
;
4802 Trace("regexp-debug") << "Checking Memberships for " << x
<< std::endl
;
4803 if(d_inter_index
.find(x
) == d_inter_index
.end()) {
4804 d_inter_index
[x
] = 0;
4806 int cur_inter_idx
= d_inter_index
[x
];
4807 unsigned n_pmem
= (*itr_xr
).second
;
4808 Assert( getNumMemberships( x
, true )==n_pmem
);
4809 if( cur_inter_idx
!= (int)n_pmem
) {
4811 d_inter_cache
[x
] = getMembership( x
, true, 0 );
4812 d_inter_index
[x
] = 1;
4813 Trace("regexp-debug") << "... only one choice " << std::endl
;
4814 } else if(n_pmem
> 1) {
4816 if(d_inter_cache
.find(x
) != d_inter_cache
.end()) {
4817 r
= d_inter_cache
[x
];
4820 r
= getMembership( x
, true, 0 );
4824 unsigned k_start
= cur_inter_idx
;
4825 Trace("regexp-debug") << "... staring from : " << cur_inter_idx
<< ", we have " << n_pmem
<< std::endl
;
4826 for(unsigned k
= k_start
; k
<n_pmem
; k
++) {
4827 Node r2
= getMembership( x
, true, k
);
4828 r
= d_regexp_opr
.intersect(r
, r2
, spflag
);
4831 } else if(r
== d_emptyRegexp
) {
4832 std::vector
< Node
> vec_nodes
;
4833 for( unsigned kk
=0; kk
<=k
; kk
++ ){
4834 Node rr
= getMembership( x
, true, kk
);
4835 Node n
= NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP
, x
, rr
);
4836 vec_nodes
.push_back( n
);
4839 sendInference(vec_nodes
, conc
, "INTERSECT CONFLICT", true);
4848 if(!d_conflict
&& !spflag
) {
4849 d_inter_cache
[x
] = r
;
4850 d_inter_index
[x
] = (int)n_pmem
;
4857 Trace("regexp-debug") << "... No Intersect Conflict in Memberships, addedLemma: " << addedLemma
<< std::endl
;
4859 NodeManager
* nm
= NodeManager::currentNM();
4860 for( unsigned i
=0; i
<d_regexp_memberships
.size(); i
++ ) {
4861 //check regular expression membership
4862 Node assertion
= d_regexp_memberships
[i
];
4863 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
;
4864 if( d_regexp_ucached
.find(assertion
) == d_regexp_ucached
.end()
4865 && d_regexp_ccached
.find(assertion
) == d_regexp_ccached
.end() ) {
4866 Trace("strings-regexp") << "We have regular expression assertion : " << assertion
<< std::endl
;
4867 Node atom
= assertion
.getKind()==kind::NOT
? assertion
[0] : assertion
;
4868 bool polarity
= assertion
.getKind()!=kind::NOT
;
4872 std::vector
< Node
> rnfexp
;
4876 x
= getNormalString(x
, rnfexp
);
4879 if (!d_regexp_opr
.checkConstRegExp(r
))
4881 r
= getNormalSymRegExp(r
, rnfexp
);
4884 Trace("strings-regexp-nf") << "Term " << atom
<< " is normalized to "
4885 << x
<< " IN " << r
<< std::endl
;
4889 Rewriter::rewrite(nm
->mkNode(kind::STRING_IN_REGEXP
, x
, r
));
4896 d_regexp_ccached
.insert(assertion
);
4899 else if (tmp
== d_false
)
4901 Node antec
= mkRegExpAntec(assertion
, mkExplain(rnfexp
));
4902 Node conc
= Node::null();
4903 sendLemma(antec
, conc
, "REGEXP NF Conflict");
4910 flag
= checkPDerivative(x
, r
, atom
, addedLemma
, rnfexp
);
4912 if(! options::stringExp()) {
4913 throw LogicException("Strings Incomplete (due to Negative Membership) by default, try --strings-exp option.");
4917 //check if the term is atomic
4918 Node xr
= getRepresentative( x
);
4919 //Trace("strings-regexp") << xr << " is rep of " << x << std::endl;
4920 //Assert( d_normal_forms.find( xr )!=d_normal_forms.end() );
4921 Trace("strings-regexp")
4922 << "Unroll/simplify membership of atomic term " << xr
4924 // if so, do simple unrolling
4925 std::vector
<Node
> nvec
;
4929 d_regexp_opr
.simplify(atom
, nvec
, polarity
);
4931 Node antec
= assertion
;
4932 if (d_regexp_ant
.find(assertion
) != d_regexp_ant
.end())
4934 antec
= d_regexp_ant
[assertion
];
4935 for (std::vector
<Node
>::const_iterator itr
= nvec
.begin();
4939 if (itr
->getKind() == kind::STRING_IN_REGEXP
)
4941 if (d_regexp_ant
.find(*itr
) == d_regexp_ant
.end())
4943 d_regexp_ant
[*itr
] = antec
;
4948 antec
= NodeManager::currentNM()->mkNode(
4949 kind::AND
, antec
, mkExplain(rnfexp
));
4950 Node conc
= nvec
.size() == 1
4952 : NodeManager::currentNM()->mkNode(kind::AND
, nvec
);
4953 conc
= Rewriter::rewrite(conc
);
4954 sendLemma(antec
, conc
, "REGEXP_Unfold");
4958 cprocessed
.push_back(assertion
);
4962 processed
.push_back(assertion
);
4964 // d_regexp_ucached[assertion] = true;
4974 for( unsigned i
=0; i
<processed
.size(); i
++ ) {
4975 Trace("strings-regexp") << "...add " << processed
[i
] << " to u-cache." << std::endl
;
4976 d_regexp_ucached
.insert(processed
[i
]);
4978 for( unsigned i
=0; i
<cprocessed
.size(); i
++ ) {
4979 Trace("strings-regexp") << "...add " << cprocessed
[i
] << " to c-cache." << std::endl
;
4980 d_regexp_ccached
.insert(cprocessed
[i
]);
4986 bool TheoryStrings::checkPDerivative( Node x
, Node r
, Node atom
, bool &addedLemma
, std::vector
< Node
> &nf_exp
) {
4988 Node antnf
= mkExplain(nf_exp
);
4990 if(areEqual(x
, d_emptyString
)) {
4992 switch(d_regexp_opr
.delta(r
, exp
)) {
4994 Node antec
= mkRegExpAntec(atom
, x
.eqNode(d_emptyString
));
4995 antec
= NodeManager::currentNM()->mkNode(kind::AND
, antec
, antnf
);
4996 sendLemma(antec
, exp
, "RegExp Delta");
4998 d_regexp_ccached
.insert(atom
);
5002 d_regexp_ccached
.insert(atom
);
5006 Node antec
= mkRegExpAntec(atom
, x
.eqNode(d_emptyString
));
5007 antec
= NodeManager::currentNM()->mkNode(kind::AND
, antec
, antnf
);
5008 Node conc
= Node::null();
5009 sendLemma(antec
, conc
, "RegExp Delta CONFLICT");
5011 d_regexp_ccached
.insert(atom
);
5019 /*Node xr = getRepresentative( x );
5021 Node n = NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP, xr, r);
5022 Node nn = Rewriter::rewrite( n );
5024 d_regexp_ccached.insert(atom);
5026 } else if(nn == d_false) {
5027 Node antec = mkRegExpAntec(atom, x.eqNode(xr));
5028 Node conc = Node::null();
5029 sendLemma(antec, conc, "RegExp Delta CONFLICT");
5031 d_regexp_ccached.insert(atom);
5035 Node sREant
= mkRegExpAntec(atom
, d_true
);
5036 sREant
= NodeManager::currentNM()->mkNode(kind::AND
, sREant
, antnf
);
5037 if(deriveRegExp( x
, r
, sREant
)) {
5039 d_regexp_ccached
.insert(atom
);
5046 CVC4::String
TheoryStrings::getHeadConst( Node x
) {
5048 return x
.getConst
< String
>();
5049 } else if( x
.getKind() == kind::STRING_CONCAT
) {
5050 if( x
[0].isConst() ) {
5051 return x
[0].getConst
< String
>();
5053 return d_emptyString
.getConst
< String
>();
5056 return d_emptyString
.getConst
< String
>();
5060 bool TheoryStrings::deriveRegExp( Node x
, Node r
, Node ant
) {
5062 Assert(x
!= d_emptyString
);
5063 Trace("regexp-derive") << "TheoryStrings::deriveRegExp: x=" << x
<< ", r= " << r
<< std::endl
;
5065 // Node n = NodeManager::currentNM()->mkNode( kind::STRING_IN_REGEXP, x, r );
5066 // Node r = Rewriter::rewrite( n );
5068 // sendLemma(ant, r, "REGEXP REWRITE");
5072 CVC4::String s
= getHeadConst( x
);
5073 if( !s
.isEmptyString() && d_regexp_opr
.checkConstRegExp( r
) ) {
5074 Node conc
= Node::null();
5077 for(unsigned i
=0; i
<s
.size(); ++i
) {
5078 CVC4::String c
= s
.substr(i
, 1);
5080 int rt
= d_regexp_opr
.derivativeS(dc
, c
, dc2
);
5084 } else if(rt
== 2) {
5093 Assert(false, "Impossible: TheoryStrings::deriveRegExp: const string in const regular expression.");
5096 Assert( x
.getKind() == kind::STRING_CONCAT
);
5097 std::vector
< Node
> vec_nodes
;
5098 for(unsigned int i
=1; i
<x
.getNumChildren(); ++i
) {
5099 vec_nodes
.push_back( x
[i
] );
5101 Node left
= mkConcat( vec_nodes
);
5102 left
= Rewriter::rewrite( left
);
5103 conc
= NodeManager::currentNM()->mkNode( kind::STRING_IN_REGEXP
, left
, dc
);
5105 /*std::vector< Node > sdc;
5106 d_regexp_opr.simplify(conc, sdc, true);
5107 if(sdc.size() == 1) {
5110 conc = Rewriter::rewrite(NodeManager::currentNM()->mkNode(kind::AND, conc));
5114 sendLemma(ant
, conc
, "RegExp-Derive");
5121 void TheoryStrings::addMembership(Node assertion
) {
5122 bool polarity
= assertion
.getKind() != kind::NOT
;
5123 TNode atom
= polarity
? assertion
: assertion
[0];
5128 NodeIntMap::const_iterator it
= d_pos_memberships
.find( x
);
5129 if (it
!= d_pos_memberships
.end())
5131 index
= (*it
).second
;
5132 for( int k
=0; k
<index
; k
++ ){
5133 if( k
<(int)d_pos_memberships_data
[x
].size() ){
5134 if( d_pos_memberships_data
[x
][k
]==r
){
5142 d_pos_memberships
[x
] = index
+ 1;
5143 if( index
<(int)d_pos_memberships_data
[x
].size() ){
5144 d_pos_memberships_data
[x
][index
] = r
;
5146 d_pos_memberships_data
[x
].push_back( r
);
5148 } else if(!options::stringIgnNegMembership()) {
5149 /*if(options::stringEIT() && d_regexp_opr.checkConstRegExp(r)) {
5151 Node r2 = d_regexp_opr.complement(r, rt);
5152 Node a = NodeManager::currentNM()->mkNode(kind::STRING_IN_REGEXP, x, r2);
5155 NodeIntMap::const_iterator it
= d_neg_memberships
.find( x
);
5156 if (it
!= d_neg_memberships
.end())
5158 index
= (*it
).second
;
5159 for( int k
=0; k
<index
; k
++ ){
5160 if( k
<(int)d_neg_memberships_data
[x
].size() ){
5161 if( d_neg_memberships_data
[x
][k
]==r
){
5169 d_neg_memberships
[x
] = index
+ 1;
5170 if( index
<(int)d_neg_memberships_data
[x
].size() ){
5171 d_neg_memberships_data
[x
][index
] = r
;
5173 d_neg_memberships_data
[x
].push_back( r
);
5177 if(polarity
|| !options::stringIgnNegMembership()) {
5178 d_regexp_memberships
.push_back( assertion
);
5182 Node
TheoryStrings::getNormalString( Node x
, std::vector
< Node
>& nf_exp
){
5184 Node xr
= getRepresentative( x
);
5185 if( d_normal_forms
.find( xr
) != d_normal_forms
.end() ){
5186 Node ret
= mkConcat( d_normal_forms
[xr
] );
5187 nf_exp
.insert( nf_exp
.end(), d_normal_forms_exp
[xr
].begin(), d_normal_forms_exp
[xr
].end() );
5188 addToExplanation( x
, d_normal_forms_base
[xr
], nf_exp
);
5189 Trace("strings-debug") << "Term: " << x
<< " has a normal form " << ret
<< std::endl
;
5192 if(x
.getKind() == kind::STRING_CONCAT
) {
5193 std::vector
< Node
> vec_nodes
;
5194 for(unsigned i
=0; i
<x
.getNumChildren(); i
++) {
5195 Node nc
= getNormalString( x
[i
], nf_exp
);
5196 vec_nodes
.push_back( nc
);
5198 return mkConcat( vec_nodes
);
5205 Node
TheoryStrings::getNormalSymRegExp(Node r
, std::vector
<Node
> &nf_exp
) {
5207 switch( r
.getKind() ) {
5208 case kind::REGEXP_EMPTY
:
5209 case kind::REGEXP_SIGMA
:
5211 case kind::STRING_TO_REGEXP
: {
5212 if(!r
[0].isConst()) {
5213 Node tmp
= getNormalString( r
[0], nf_exp
);
5215 ret
= NodeManager::currentNM()->mkNode(kind::STRING_TO_REGEXP
, tmp
);
5220 case kind::REGEXP_CONCAT
:
5221 case kind::REGEXP_UNION
:
5222 case kind::REGEXP_INTER
:
5223 case kind::REGEXP_STAR
:
5225 std::vector
< Node
> vec_nodes
;
5226 for (const Node
& cr
: r
)
5228 vec_nodes
.push_back(getNormalSymRegExp(cr
, nf_exp
));
5230 ret
= Rewriter::rewrite(
5231 NodeManager::currentNM()->mkNode(r
.getKind(), vec_nodes
));
5234 //case kind::REGEXP_PLUS:
5235 //case kind::REGEXP_OPT:
5236 //case kind::REGEXP_RANGE:
5238 Trace("strings-error") << "Unsupported term: " << r
<< " in normalization SymRegExp." << std::endl
;
5240 //return Node::null();
5246 /** run the given inference step */
5247 void TheoryStrings::runInferStep(InferStep s
, int effort
)
5249 Trace("strings-process") << "Run " << s
;
5252 Trace("strings-process") << ", effort = " << effort
;
5254 Trace("strings-process") << "..." << std::endl
;
5257 case CHECK_INIT
: checkInit(); break;
5258 case CHECK_CONST_EQC
: checkConstantEquivalenceClasses(); break;
5259 case CHECK_EXTF_EVAL
: checkExtfEval(effort
); break;
5260 case CHECK_CYCLES
: checkCycles(); break;
5261 case CHECK_FLAT_FORMS
: checkFlatForms(); break;
5262 case CHECK_NORMAL_FORMS_EQ
: checkNormalFormsEq(); break;
5263 case CHECK_NORMAL_FORMS_DEQ
: checkNormalFormsDeq(); break;
5264 case CHECK_CODES
: checkCodes(); break;
5265 case CHECK_LENGTH_EQC
: checkLengthsEqc(); break;
5266 case CHECK_EXTF_REDUCTION
: checkExtfReductions(effort
); break;
5267 case CHECK_MEMBERSHIP
: checkMemberships(); break;
5268 case CHECK_CARDINALITY
: checkCardinality(); break;
5269 default: Unreachable(); break;
5271 Trace("strings-process") << "Done " << s
5272 << ", addedFact = " << !d_pending
.empty() << " "
5273 << !d_lemma_cache
.empty()
5274 << ", d_conflict = " << d_conflict
<< std::endl
;
5277 bool TheoryStrings::hasStrategyEffort(Effort e
) const
5279 return d_strat_steps
.find(e
) != d_strat_steps
.end();
5282 void TheoryStrings::addStrategyStep(InferStep s
, int effort
, bool addBreak
)
5284 // must run check init first
5285 Assert((s
== CHECK_INIT
)==d_infer_steps
.empty());
5286 // must use check cycles when using flat forms
5287 Assert(s
!= CHECK_FLAT_FORMS
5288 || std::find(d_infer_steps
.begin(), d_infer_steps
.end(), CHECK_CYCLES
)
5289 != d_infer_steps
.end());
5290 d_infer_steps
.push_back(s
);
5291 d_infer_step_effort
.push_back(effort
);
5294 d_infer_steps
.push_back(BREAK
);
5295 d_infer_step_effort
.push_back(0);
5299 void TheoryStrings::initializeStrategy()
5301 // initialize the strategy if not already done so
5302 if (!d_strategy_init
)
5304 std::map
<Effort
, unsigned> step_begin
;
5305 std::map
<Effort
, unsigned> step_end
;
5306 d_strategy_init
= true;
5307 // beginning indices
5308 step_begin
[EFFORT_FULL
] = 0;
5309 if (options::stringEager())
5311 step_begin
[EFFORT_STANDARD
] = 0;
5313 // add the inference steps
5314 addStrategyStep(CHECK_INIT
);
5315 addStrategyStep(CHECK_CONST_EQC
);
5316 addStrategyStep(CHECK_EXTF_EVAL
, 0);
5317 addStrategyStep(CHECK_CYCLES
);
5318 if (options::stringFlatForms())
5320 addStrategyStep(CHECK_FLAT_FORMS
);
5322 addStrategyStep(CHECK_EXTF_REDUCTION
, 1);
5323 if (options::stringEager())
5325 // do only the above inferences at standard effort, if applicable
5326 step_end
[EFFORT_STANDARD
] = d_infer_steps
.size() - 1;
5328 addStrategyStep(CHECK_NORMAL_FORMS_EQ
);
5329 addStrategyStep(CHECK_EXTF_EVAL
, 1);
5330 if (!options::stringEagerLen())
5332 addStrategyStep(CHECK_LENGTH_EQC
);
5334 addStrategyStep(CHECK_NORMAL_FORMS_DEQ
);
5335 addStrategyStep(CHECK_CODES
);
5336 if (options::stringEagerLen())
5338 addStrategyStep(CHECK_LENGTH_EQC
);
5340 if (options::stringExp() && !options::stringGuessModel())
5342 addStrategyStep(CHECK_EXTF_REDUCTION
, 2);
5344 addStrategyStep(CHECK_MEMBERSHIP
);
5345 addStrategyStep(CHECK_CARDINALITY
);
5346 step_end
[EFFORT_FULL
] = d_infer_steps
.size() - 1;
5347 if (options::stringExp() && options::stringGuessModel())
5349 step_begin
[EFFORT_LAST_CALL
] = d_infer_steps
.size();
5350 // these two steps are run in parallel
5351 addStrategyStep(CHECK_EXTF_REDUCTION
, 2, false);
5352 addStrategyStep(CHECK_EXTF_EVAL
, 3);
5353 step_end
[EFFORT_LAST_CALL
] = d_infer_steps
.size() - 1;
5355 // set the beginning/ending ranges
5356 for (const std::pair
<const Effort
, unsigned>& it_begin
: step_begin
)
5358 Effort e
= it_begin
.first
;
5359 std::map
<Effort
, unsigned>::iterator it_end
= step_end
.find(e
);
5360 Assert(it_end
!= step_end
.end());
5362 std::pair
<unsigned, unsigned>(it_begin
.second
, it_end
->second
);
5367 void TheoryStrings::runStrategy(unsigned sbegin
, unsigned send
)
5369 Trace("strings-process") << "----check, next round---" << std::endl
;
5370 for (unsigned i
= sbegin
; i
<= send
; i
++)
5372 InferStep curr
= d_infer_steps
[i
];
5382 runInferStep(curr
, d_infer_step_effort
[i
]);
5389 Trace("strings-process") << "----finished round---" << std::endl
;
5392 }/* CVC4::theory::strings namespace */
5393 }/* CVC4::theory namespace */
5394 }/* CVC4 namespace */