}
// (= "" (str.replace x "A" "")) ---> (str.prefix x "A")
- Node one = nm->mkConst(Rational(1));
- Node ylen = nm->mkNode(STRING_LENGTH, ne[1]);
- if (checkEntailArithEq(ylen, one) && ne[2] == empty)
+ if (checkEntailLengthOne(ne[1]) && ne[2] == empty)
{
Node ret = nm->mkNode(STRING_PREFIX, ne[0], ne[1]);
return returnRewrite(node, ret, "str-emp-repl-emp");
// if (str.len y) = 1 and (str.len z) = 1
if (node[1] == zero)
{
- Node one = nm->mkConst(Rational(1));
- Node n1len = nm->mkNode(kind::STRING_LENGTH, node[0][1]);
- Node n2len = nm->mkNode(kind::STRING_LENGTH, node[0][2]);
- if (checkEntailArith(one, n1len) && checkEntailArith(one, n2len)
- && checkEntailNonEmpty(node[0][1]) && checkEntailNonEmpty(node[0][2]))
+ if (checkEntailLengthOne(node[0][1], true)
+ && checkEntailLengthOne(node[0][2], true))
{
Node ret = nm->mkNode(
kind::STRING_STRREPL,
}
// (str.substr s x x) ---> "" if (str.len s) <= 1
- Node one = nm->mkConst(CVC4::Rational(1));
- if (node[1] == node[2] && checkEntailArith(one, tot_len))
+ if (node[1] == node[2] && checkEntailLengthOne(node[0]))
{
Node ret = nm->mkConst(::CVC4::String(""));
return returnRewrite(node, ret, "ss-len-one-z-z");
// (str.contains (str.replace x y x) z) ---> (str.contains x z)
// if (str.len z) <= 1
- Node one = nm->mkConst(Rational(1));
- if (checkEntailArith(one, len_n2))
+ if (checkEntailLengthOne(node[1]))
{
Node ret = nm->mkNode(kind::STRING_STRCTN, node[0][0], node[1]);
return returnRewrite(node, ret, "ctn-repl-len-one-to-ctn");
// (str.replace x y x) ---> (str.replace x (str.++ y1 ... yn) x)
// if 1 >= (str.len x) and (= y "") ---> (= y1 "") ... (= yn "")
- if (checkEntailArith(nm->mkConst(Rational(1)), l0))
+ if (checkEntailLengthOne(node[0]))
{
Node empty = nm->mkConst(String(""));
Node rn1 = Rewriter::rewrite(
// str.replace( x ++ y ++ x ++ y, "A", z ) -->
// str.replace( x ++ y, "A", z ) ++ x ++ y
// since if "A" occurs in x ++ y ++ x ++ y, then it must occur in x ++ y.
- if (node[1].isConst() && node[1].getConst<String>().size() == 1)
+ if (checkEntailLengthOne(node[1]))
{
Node lastLhs;
unsigned lastCheckIndex = 0;
return checkEntailArith(len, true);
}
+bool TheoryStringsRewriter::checkEntailLengthOne(Node s, bool strict)
+{
+ NodeManager* nm = NodeManager::currentNM();
+ Node one = nm->mkConst(Rational(1));
+ Node len = nm->mkNode(STRING_LENGTH, s);
+ len = Rewriter::rewrite(len);
+ return checkEntailArith(one, len) && (!strict || checkEntailArith(len, true));
+}
+
bool TheoryStringsRewriter::checkEntailArithEq(Node a, Node b)
{
if (a == b)
break;
}
- Node strlen = Rewriter::rewrite(nm->mkNode(kind::STRING_LENGTH, n[0]));
- if (strlen == nm->mkConst(Rational(1)) && n[2] == empty)
+ if (checkEntailLengthOne(n[0]) && n[2] == empty)
{
// (str.replace "A" x "") --> "A"
res = n[0];
}
case kind::STRING_SUBSTR:
{
- Node strlen = Rewriter::rewrite(nm->mkNode(kind::STRING_LENGTH, n[0]));
- if (strlen == nm->mkConst(Rational(1)))
+ if (checkEntailLengthOne(n[0]))
{
// (str.substr "A" x y) --> "A"
res = n[0];
TS_ASSERT_DIFFERS(res_t1, res_t2);
}
+ void testCheckEntailLengthOne()
+ {
+ TypeNode intType = d_nm->integerType();
+ TypeNode strType = d_nm->stringType();
+
+ Node a = d_nm->mkConst(::CVC4::String("A"));
+ Node abcd = d_nm->mkConst(::CVC4::String("ABCD"));
+ Node aaad = d_nm->mkConst(::CVC4::String("AAAD"));
+ Node b = d_nm->mkConst(::CVC4::String("B"));
+ Node x = d_nm->mkVar("x", strType);
+ Node y = d_nm->mkVar("y", strType);
+ Node negOne = d_nm->mkConst(Rational(-1));
+ Node zero = d_nm->mkConst(Rational(0));
+ Node one = d_nm->mkConst(Rational(1));
+ Node two = d_nm->mkConst(Rational(2));
+ Node three = d_nm->mkConst(Rational(3));
+ Node i = d_nm->mkVar("i", intType);
+
+ TS_ASSERT(TheoryStringsRewriter::checkEntailLengthOne(a));
+ TS_ASSERT(TheoryStringsRewriter::checkEntailLengthOne(a, true));
+
+ Node substr = d_nm->mkNode(kind::STRING_SUBSTR, x, zero, one);
+ TS_ASSERT(TheoryStringsRewriter::checkEntailLengthOne(substr));
+ TS_ASSERT(!TheoryStringsRewriter::checkEntailLengthOne(substr, true));
+
+ substr = d_nm->mkNode(kind::STRING_SUBSTR,
+ d_nm->mkNode(kind::STRING_CONCAT, a, x),
+ zero,
+ one);
+ TS_ASSERT(TheoryStringsRewriter::checkEntailLengthOne(substr));
+ TS_ASSERT(TheoryStringsRewriter::checkEntailLengthOne(substr, true));
+
+ substr = d_nm->mkNode(kind::STRING_SUBSTR, x, zero, two);
+ TS_ASSERT(!TheoryStringsRewriter::checkEntailLengthOne(substr));
+ TS_ASSERT(!TheoryStringsRewriter::checkEntailLengthOne(substr, true));
+ }
+
void testCheckEntailArith()
{
TypeNode intType = d_nm->integerType();