1 /********************* */
2 /*! \file term_registry.cpp
4 ** Top contributors (to current version):
5 ** Andrew Reynolds, Andres Noetzli, Tianyi Liang
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2021 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 term registry for the theory of strings.
15 #include "theory/strings/term_registry.h"
17 #include "expr/attribute.h"
18 #include "options/smt_options.h"
19 #include "options/strings_options.h"
20 #include "smt/logic_exception.h"
21 #include "theory/rewriter.h"
22 #include "theory/strings/inference_manager.h"
23 #include "theory/strings/theory_strings_utils.h"
24 #include "theory/strings/word.h"
27 using namespace CVC5::context
;
28 using namespace CVC5::kind
;
34 struct StringsProxyVarAttributeId
37 typedef expr::Attribute
<StringsProxyVarAttributeId
, bool>
38 StringsProxyVarAttribute
;
40 TermRegistry::TermRegistry(SolverState
& s
,
41 SequencesStatistics
& statistics
,
42 ProofNodeManager
* pnm
)
45 d_statistics(statistics
),
47 d_functionsTerms(s
.getSatContext()),
48 d_inputVars(s
.getUserContext()),
49 d_preregisteredTerms(s
.getSatContext()),
50 d_registeredTerms(s
.getUserContext()),
51 d_registeredTypes(s
.getUserContext()),
52 d_proxyVar(s
.getUserContext()),
53 d_lengthLemmaTermsCache(s
.getUserContext()),
54 d_epg(pnm
? new EagerProofGenerator(
57 "strings::TermRegistry::EagerProofGenerator")
60 NodeManager
* nm
= NodeManager::currentNM();
61 d_zero
= nm
->mkConst(Rational(0));
62 d_one
= nm
->mkConst(Rational(1));
63 d_negOne
= NodeManager::currentNM()->mkConst(Rational(-1));
64 d_cardSize
= utils::getAlphabetCardinality();
67 TermRegistry::~TermRegistry() {}
69 void TermRegistry::finishInit(InferenceManager
* im
) { d_im
= im
; }
71 Node
TermRegistry::eagerReduce(Node t
, SkolemCache
* sc
)
73 NodeManager
* nm
= NodeManager::currentNM();
75 Kind tk
= t
.getKind();
76 if (tk
== STRING_TO_CODE
)
78 // ite( str.len(s)==1, 0 <= str.code(s) < |A|, str.code(s)=-1 )
79 Node code_len
= utils::mkNLength(t
[0]).eqNode(nm
->mkConst(Rational(1)));
80 Node code_eq_neg1
= t
.eqNode(nm
->mkConst(Rational(-1)));
81 Node code_range
= nm
->mkNode(
83 nm
->mkNode(GEQ
, t
, nm
->mkConst(Rational(0))),
85 LT
, t
, nm
->mkConst(Rational(utils::getAlphabetCardinality()))));
86 lemma
= nm
->mkNode(ITE
, code_len
, code_range
, code_eq_neg1
);
88 else if (tk
== STRING_STRIDOF
)
90 // (and (>= (str.indexof x y n) (- 1)) (<= (str.indexof x y n) (str.len
92 Node l
= utils::mkNLength(t
[0]);
93 lemma
= nm
->mkNode(AND
,
94 nm
->mkNode(GEQ
, t
, nm
->mkConst(Rational(-1))),
95 nm
->mkNode(LEQ
, t
, l
));
97 else if (tk
== STRING_STOI
)
99 // (>= (str.to_int x) (- 1))
100 lemma
= nm
->mkNode(GEQ
, t
, nm
->mkConst(Rational(-1)));
102 else if (tk
== STRING_STRCTN
)
104 // ite( (str.contains s r), (= s (str.++ sk1 r sk2)), (not (= s r)))
106 sc
->mkSkolemCached(t
[0], t
[1], SkolemCache::SK_FIRST_CTN_PRE
, "sc1");
108 sc
->mkSkolemCached(t
[0], t
[1], SkolemCache::SK_FIRST_CTN_POST
, "sc2");
109 lemma
= t
[0].eqNode(utils::mkNConcat(sk1
, t
[1], sk2
));
110 lemma
= nm
->mkNode(ITE
, t
, lemma
, t
[0].eqNode(t
[1]).notNode());
115 Node
TermRegistry::lengthPositive(Node t
)
117 NodeManager
* nm
= NodeManager::currentNM();
118 Node zero
= nm
->mkConst(Rational(0));
119 Node emp
= Word::mkEmptyWord(t
.getType());
120 Node tlen
= nm
->mkNode(STRING_LENGTH
, t
);
121 Node tlenEqZero
= tlen
.eqNode(zero
);
122 Node tEqEmp
= t
.eqNode(emp
);
123 Node caseEmpty
= nm
->mkNode(AND
, tlenEqZero
, tEqEmp
);
124 Node caseNEmpty
= nm
->mkNode(GT
, tlen
, zero
);
125 // (or (and (= (str.len t) 0) (= t "")) (> (str.len t) 0))
126 return nm
->mkNode(OR
, caseEmpty
, caseNEmpty
);
129 void TermRegistry::preRegisterTerm(TNode n
)
131 if (d_preregisteredTerms
.find(n
) != d_preregisteredTerms
.end())
135 eq::EqualityEngine
* ee
= d_state
.getEqualityEngine();
136 d_preregisteredTerms
.insert(n
);
137 Trace("strings-preregister")
138 << "TheoryString::preregister : " << n
<< std::endl
;
139 // check for logic exceptions
140 Kind k
= n
.getKind();
141 if (!options::stringExp())
143 if (k
== STRING_STRIDOF
|| k
== STRING_ITOS
|| k
== STRING_STOI
144 || k
== STRING_STRREPL
|| k
== STRING_SUBSTR
|| k
== STRING_STRREPLALL
145 || k
== SEQ_NTH
|| k
== STRING_REPLACE_RE
|| k
== STRING_REPLACE_RE_ALL
146 || k
== STRING_STRCTN
|| k
== STRING_LEQ
|| k
== STRING_TOLOWER
147 || k
== STRING_TOUPPER
|| k
== STRING_REV
|| k
== STRING_UPDATE
)
149 std::stringstream ss
;
150 ss
<< "Term of kind " << k
151 << " not supported in default mode, try --strings-exp";
152 throw LogicException(ss
.str());
157 if (n
[0].getType().isRegExp())
159 std::stringstream ss
;
160 ss
<< "Equality between regular expressions is not supported";
161 throw LogicException(ss
.str());
163 ee
->addTriggerPredicate(n
);
166 else if (k
== STRING_IN_REGEXP
)
168 d_im
->requirePhase(n
, true);
169 ee
->addTriggerPredicate(n
);
174 else if (k
== STRING_TO_CODE
)
179 TypeNode tn
= n
.getType();
180 if (tn
.isRegExp() && n
.isVar())
182 std::stringstream ss
;
183 ss
<< "Regular expression variables are not supported.";
184 throw LogicException(ss
.str());
186 if (tn
.isString()) // string-only
188 // all characters of constants should fall in the alphabet
191 std::vector
<unsigned> vec
= n
.getConst
<String
>().getVec();
192 for (unsigned u
: vec
)
196 std::stringstream ss
;
197 ss
<< "Characters in string \"" << n
198 << "\" are outside of the given alphabet.";
199 throw LogicException(ss
.str());
205 else if (tn
.isBoolean())
207 // All kinds that we do congruence over that may return a Boolean go here
208 if (k
==STRING_STRCTN
|| k
== STRING_LEQ
|| k
== SEQ_NTH
)
210 // Get triggered for both equal and dis-equal
211 ee
->addTriggerPredicate(n
);
216 // Function applications/predicates
219 // Set d_functionsTerms stores all function applications that are
220 // relevant to theory combination. Notice that this is a subset of
221 // the applications whose kinds are function kinds in the equality
222 // engine. This means it does not include applications of operators
223 // like re.++, which is not a function kind in the equality engine.
224 // Concatenation terms do not need to be considered here because
225 // their arguments have string type and do not introduce any shared
227 if (n
.hasOperator() && ee
->isFunctionKind(k
) && k
!= STRING_CONCAT
)
229 d_functionsTerms
.push_back(n
);
231 if (options::stringFMF())
233 if (tn
.isStringLike())
235 // Our decision strategy will minimize the length of this term if it is a
236 // variable but not an internally generated Skolem, or a term that does
237 // not belong to this theory.
238 if (n
.isVar() ? !d_skCache
.isSkolem(n
)
239 : kindToTheoryId(k
) != THEORY_STRINGS
)
241 d_inputVars
.insert(n
);
242 Trace("strings-preregister") << "input variable: " << n
<< std::endl
;
248 void TermRegistry::registerTerm(Node n
, int effort
)
250 Trace("strings-register") << "TheoryStrings::registerTerm() " << n
251 << ", effort = " << effort
<< std::endl
;
252 if (d_registeredTerms
.find(n
) != d_registeredTerms
.end())
254 Trace("strings-register") << "...already registered" << std::endl
;
257 bool do_register
= true;
258 TypeNode tn
= n
.getType();
259 if (!tn
.isStringLike())
261 if (options::stringEagerLen())
263 do_register
= effort
== 0;
267 do_register
= effort
> 0 || n
.getKind() != STRING_CONCAT
;
272 Trace("strings-register") << "...do not register" << std::endl
;
275 Trace("strings-register") << "...register" << std::endl
;
276 d_registeredTerms
.insert(n
);
277 // ensure the type is registered
279 TrustNode regTermLem
;
280 if (tn
.isStringLike())
282 // register length information:
283 // for variables, split on empty vs positive length
284 // for concat/const/replace, introduce proxy var and state length relation
285 regTermLem
= getRegisterTermLemma(n
);
287 else if (n
.getKind() != STRING_STRCTN
)
289 // we don't send out eager reduction lemma for str.contains currently
290 Node eagerRedLemma
= eagerReduce(n
, &d_skCache
);
291 if (!eagerRedLemma
.isNull())
293 // if there was an eager reduction, we make the trust node for it
294 if (d_epg
!= nullptr)
296 regTermLem
= d_epg
->mkTrustNode(
297 eagerRedLemma
, PfRule::STRING_EAGER_REDUCTION
, {}, {n
});
301 regTermLem
= TrustNode::mkTrustLemma(eagerRedLemma
, nullptr);
305 if (!regTermLem
.isNull())
307 Trace("strings-lemma") << "Strings::Lemma REG-TERM : " << regTermLem
309 Trace("strings-assert")
310 << "(assert " << regTermLem
.getNode() << ")" << std::endl
;
311 d_im
->trustedLemma(regTermLem
, InferenceId::STRINGS_REGISTER_TERM
);
315 void TermRegistry::registerType(TypeNode tn
)
317 if (d_registeredTypes
.find(tn
) != d_registeredTypes
.end())
321 d_registeredTypes
.insert(tn
);
322 if (tn
.isStringLike())
324 // preregister the empty word for the type
325 Node emp
= Word::mkEmptyWord(tn
);
326 if (!d_state
.hasTerm(emp
))
328 preRegisterTerm(emp
);
333 TrustNode
TermRegistry::getRegisterTermLemma(Node n
)
335 Assert(n
.getType().isStringLike());
336 NodeManager
* nm
= NodeManager::currentNM();
337 // register length information:
338 // for variables, split on empty vs positive length
339 // for concat/const/replace, introduce proxy var and state length relation
341 if (n
.getKind() != STRING_CONCAT
&& !n
.isConst())
343 Node lsumb
= nm
->mkNode(STRING_LENGTH
, n
);
344 lsum
= Rewriter::rewrite(lsumb
);
345 // can register length term if it does not rewrite
348 registerTermAtomic(n
, LENGTH_SPLIT
);
349 return TrustNode::null();
352 Node sk
= d_skCache
.mkSkolemCached(n
, SkolemCache::SK_PURIFY
, "lsym");
353 StringsProxyVarAttribute spva
;
354 sk
.setAttribute(spva
, true);
355 Node eq
= Rewriter::rewrite(sk
.eqNode(n
));
357 // If we are introducing a proxy for a constant or concat term, we do not
358 // need to send lemmas about its length, since its length is already
360 if (n
.isConst() || n
.getKind() == STRING_CONCAT
)
362 // do not send length lemma for sk.
363 registerTermAtomic(sk
, LENGTH_IGNORE
);
365 Node skl
= nm
->mkNode(STRING_LENGTH
, sk
);
366 if (n
.getKind() == STRING_CONCAT
)
368 std::vector
<Node
> nodeVec
;
369 for (const Node
& nc
: n
)
371 if (nc
.getAttribute(StringsProxyVarAttribute()))
373 Assert(d_proxyVarToLength
.find(nc
) != d_proxyVarToLength
.end());
374 nodeVec
.push_back(d_proxyVarToLength
[nc
]);
378 Node lni
= nm
->mkNode(STRING_LENGTH
, nc
);
379 nodeVec
.push_back(lni
);
382 lsum
= nm
->mkNode(PLUS
, nodeVec
);
383 lsum
= Rewriter::rewrite(lsum
);
385 else if (n
.isConst())
387 lsum
= nm
->mkConst(Rational(Word::getLength(n
)));
389 Assert(!lsum
.isNull());
390 d_proxyVarToLength
[sk
] = lsum
;
391 Node ceq
= Rewriter::rewrite(skl
.eqNode(lsum
));
393 Node ret
= nm
->mkNode(AND
, eq
, ceq
);
395 // it is a simple rewrite to justify this
396 if (d_epg
!= nullptr)
398 return d_epg
->mkTrustNode(ret
, PfRule::MACRO_SR_PRED_INTRO
, {}, {ret
});
400 return TrustNode::mkTrustLemma(ret
, nullptr);
403 void TermRegistry::registerTermAtomic(Node n
, LengthStatus s
)
405 if (d_lengthLemmaTermsCache
.find(n
) != d_lengthLemmaTermsCache
.end())
409 d_lengthLemmaTermsCache
.insert(n
);
411 if (s
== LENGTH_IGNORE
)
416 std::map
<Node
, bool> reqPhase
;
417 TrustNode lenLem
= getRegisterTermAtomicLemma(n
, s
, reqPhase
);
418 if (!lenLem
.isNull())
420 Trace("strings-lemma") << "Strings::Lemma REGISTER-TERM-ATOMIC : " << lenLem
422 Trace("strings-assert")
423 << "(assert " << lenLem
.getNode() << ")" << std::endl
;
424 d_im
->trustedLemma(lenLem
, InferenceId::STRINGS_REGISTER_TERM_ATOMIC
);
426 for (const std::pair
<const Node
, bool>& rp
: reqPhase
)
428 d_im
->requirePhase(rp
.first
, rp
.second
);
432 SkolemCache
* TermRegistry::getSkolemCache() { return &d_skCache
; }
434 const context::CDList
<TNode
>& TermRegistry::getFunctionTerms() const
436 return d_functionsTerms
;
439 const context::CDHashSet
<Node
, NodeHashFunction
>& TermRegistry::getInputVars()
445 bool TermRegistry::hasStringCode() const { return d_hasStrCode
; }
447 TrustNode
TermRegistry::getRegisterTermAtomicLemma(
448 Node n
, LengthStatus s
, std::map
<Node
, bool>& reqPhase
)
452 // No need to send length for constant terms. This case may be triggered
453 // for cases where the skolem cache automatically replaces a skolem by
455 return TrustNode::null();
457 Assert(n
.getType().isStringLike());
458 NodeManager
* nm
= NodeManager::currentNM();
459 Node n_len
= nm
->mkNode(kind::STRING_LENGTH
, n
);
460 Node emp
= Word::mkEmptyWord(n
.getType());
461 if (s
== LENGTH_GEQ_ONE
)
463 Node neq_empty
= n
.eqNode(emp
).negate();
464 Node len_n_gt_z
= nm
->mkNode(GT
, n_len
, d_zero
);
465 Node len_geq_one
= nm
->mkNode(AND
, neq_empty
, len_n_gt_z
);
466 Trace("strings-lemma") << "Strings::Lemma SK-GEQ-ONE : " << len_geq_one
468 Trace("strings-assert") << "(assert " << len_geq_one
<< ")" << std::endl
;
469 return TrustNode::mkTrustLemma(len_geq_one
, nullptr);
474 Node len_one
= n_len
.eqNode(d_one
);
475 Trace("strings-lemma") << "Strings::Lemma SK-ONE : " << len_one
477 Trace("strings-assert") << "(assert " << len_one
<< ")" << std::endl
;
478 return TrustNode::mkTrustLemma(len_one
, nullptr);
480 Assert(s
== LENGTH_SPLIT
);
482 // get the positive length lemma
483 Node lenLemma
= lengthPositive(n
);
484 // split whether the string is empty
485 Node n_len_eq_z
= n_len
.eqNode(d_zero
);
486 Node n_len_eq_z_2
= n
.eqNode(emp
);
487 Node case_empty
= nm
->mkNode(AND
, n_len_eq_z
, n_len_eq_z_2
);
488 Node case_emptyr
= Rewriter::rewrite(case_empty
);
489 if (!case_emptyr
.isConst())
491 // prefer trying the empty case first
492 // notice that requirePhase must only be called on rewritten literals that
493 // occur in the CNF stream.
494 n_len_eq_z
= Rewriter::rewrite(n_len_eq_z
);
495 Assert(!n_len_eq_z
.isConst());
496 reqPhase
[n_len_eq_z
] = true;
497 n_len_eq_z_2
= Rewriter::rewrite(n_len_eq_z_2
);
498 Assert(!n_len_eq_z_2
.isConst());
499 reqPhase
[n_len_eq_z_2
] = true;
503 // If n = "" ---> true or len( n ) = 0 ----> true, then we expect that
504 // n ---> "". Since this method is only called on non-constants n, it must
505 // be that n = "" ^ len( n ) = 0 does not rewrite to true.
506 Assert(!case_emptyr
.getConst
<bool>());
509 if (d_epg
!= nullptr)
511 return d_epg
->mkTrustNode(lenLemma
, PfRule::STRING_LENGTH_POS
, {}, {n
});
513 return TrustNode::mkTrustLemma(lenLemma
, nullptr);
516 Node
TermRegistry::getSymbolicDefinition(Node n
, std::vector
<Node
>& exp
) const
518 if (n
.getNumChildren() == 0)
520 Node pn
= getProxyVariableFor(n
);
525 Node eq
= n
.eqNode(pn
);
526 eq
= Rewriter::rewrite(eq
);
527 if (std::find(exp
.begin(), exp
.end(), eq
) == exp
.end())
533 std::vector
<Node
> children
;
534 if (n
.getMetaKind() == metakind::PARAMETERIZED
)
536 children
.push_back(n
.getOperator());
538 for (const Node
& nc
: n
)
540 if (n
.getType().isRegExp())
542 children
.push_back(nc
);
546 Node ns
= getSymbolicDefinition(nc
, exp
);
553 children
.push_back(ns
);
557 return NodeManager::currentNM()->mkNode(n
.getKind(), children
);
560 Node
TermRegistry::getProxyVariableFor(Node n
) const
562 NodeNodeMap::const_iterator it
= d_proxyVar
.find(n
);
563 if (it
!= d_proxyVar
.end())
570 Node
TermRegistry::ensureProxyVariableFor(Node n
)
572 Node proxy
= getProxyVariableFor(n
);
576 proxy
= getProxyVariableFor(n
);
578 Assert(!proxy
.isNull());
582 void TermRegistry::removeProxyEqs(Node n
, std::vector
<Node
>& unproc
) const
584 if (n
.getKind() == AND
)
586 for (const Node
& nc
: n
)
588 removeProxyEqs(nc
, unproc
);
592 Trace("strings-subs-proxy") << "Input : " << n
<< std::endl
;
593 Node ns
= Rewriter::rewrite(n
);
594 if (ns
.getKind() == EQUAL
)
596 for (size_t i
= 0; i
< 2; i
++)
598 // determine whether this side has a proxy variable
599 if (ns
[i
].getAttribute(StringsProxyVarAttribute()))
601 if (getProxyVariableFor(ns
[1 - i
]) == ns
[i
])
603 Trace("strings-subs-proxy")
604 << "...trivial definition via " << ns
[i
] << std::endl
;
605 // it is a trivial equality, e.g. between a proxy variable
606 // and its definition
612 if (!n
.isConst() || !n
.getConst
<bool>())
614 Trace("strings-subs-proxy") << "...unprocessed" << std::endl
;
619 } // namespace strings
620 } // namespace theory