mirror of
https://github.com/Z3Prover/z3
synced 2025-04-24 01:25:31 +00:00
seq + API
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
parent
68a532d066
commit
c1ebf6b4fc
9 changed files with 484 additions and 77 deletions
|
@ -125,6 +125,7 @@ namespace Microsoft.Z3
|
|||
private BoolSort m_boolSort = null;
|
||||
private IntSort m_intSort = null;
|
||||
private RealSort m_realSort = null;
|
||||
private SeqSort m_stringSort = null;
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the Boolean sort of the context.
|
||||
|
@ -163,6 +164,20 @@ namespace Microsoft.Z3
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the String sort of the context.
|
||||
/// </summary>
|
||||
public SeqSort StringSort
|
||||
{
|
||||
get
|
||||
{
|
||||
Contract.Ensures(Contract.Result<SeqSort>() != null);
|
||||
if (m_stringSort == null) m_stringSort = new SeqSort(this, Native.Z3_mk_string_sort(nCtx));
|
||||
return m_stringSort;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Create a new Boolean sort.
|
||||
/// </summary>
|
||||
|
@ -223,6 +238,27 @@ namespace Microsoft.Z3
|
|||
return new BitVecSort(this, Native.Z3_mk_bv_sort(nCtx, size));
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Create a new sequence sort.
|
||||
/// </summary>
|
||||
public SeqSort MkSeqSort(Sort s)
|
||||
{
|
||||
Contract.Requires(s != null);
|
||||
Contract.Ensures(Contract.Result<SeqSort>() != null);
|
||||
return new SeqSort(this, Native.Z3_mk_seq_sort(nCtx, s.NativeObject));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new regular expression sort.
|
||||
/// </summary>
|
||||
public ReSort MkReSort(SeqSort s)
|
||||
{
|
||||
Contract.Requires(s != null);
|
||||
Contract.Ensures(Contract.Result<ReSort>() != null);
|
||||
return new ReSort(this, Native.Z3_mk_re_sort(nCtx, s.NativeObject));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new array sort.
|
||||
/// </summary>
|
||||
|
@ -4872,6 +4908,7 @@ namespace Microsoft.Z3
|
|||
m_boolSort = null;
|
||||
m_intSort = null;
|
||||
m_realSort = null;
|
||||
m_stringSort = null;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
|
|
@ -1849,6 +1849,184 @@ public class Context extends IDisposable
|
|||
arg2.getNativeObject()));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sequences, Strings and regular expressions.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Create the empty sequence.
|
||||
*/
|
||||
public SeqExpr MkEmptySeq(Sort s)
|
||||
{
|
||||
checkContextMatch(s);
|
||||
return new SeqExpr(this, Native.mkSeqEmpty(nCtx, s.NativeObject));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the singleton sequence.
|
||||
*/
|
||||
public SeqExpr MkUnit(Expr elem)
|
||||
{
|
||||
checkContextMatch(elem);
|
||||
return new SeqExpr(this, Native.mkSeqUnit(nCtx, elem.NativeObject));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a string constant.
|
||||
*/
|
||||
public SeqExpr MkString(string s)
|
||||
{
|
||||
return new SeqExpr(this, Native.mkString(nCtx, s));
|
||||
}
|
||||
|
||||
/**
|
||||
* Concatentate sequences.
|
||||
*/
|
||||
public SeqExpr MkConcat(params SeqExpr[] t)
|
||||
{
|
||||
checkContextMatch(t);
|
||||
return new SeqExpr(this, Native.mkSeqConcat(nCtx, (uint)t.Length, AST.ArrayToNative(t)));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve the length of a given sequence.
|
||||
*/
|
||||
public IntExpr MkLength(SeqExpr s)
|
||||
{
|
||||
checkContextMatch(s);
|
||||
return (IntExpr) Expr.Create(this, Native.mkSeqLength(nCtx, s.NativeObject));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for sequence prefix.
|
||||
*/
|
||||
public BoolExpr MkPrefixOf(SeqExpr s1, SeqExpr s2)
|
||||
{
|
||||
checkContextMatch(s1, s2);
|
||||
return new BoolExpr(this, Native.mkSeqPrefix(nCtx, s1.NativeObject, s2.NativeObject));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for sequence suffix.
|
||||
*/
|
||||
public BoolExpr MkSuffixOf(SeqExpr s1, SeqExpr s2)
|
||||
{
|
||||
checkContextMatch(s1, s2);
|
||||
return new BoolExpr(this, Native.mkSeqSuffix(nCtx, s1.NativeObject, s2.NativeObject));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for sequence containment of s2 in s1.
|
||||
*/
|
||||
public BoolExpr MkContains(SeqExpr s1, SeqExpr s2)
|
||||
{
|
||||
checkContextMatch(s1, s2);
|
||||
return new BoolExpr(this, Native.mkSeqContains(nCtx, s1.NativeObject, s2.NativeObject));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve sequence of length one at index.
|
||||
*/
|
||||
public SeqExpr MkAt(SeqExpr s, IntExpr index)
|
||||
{
|
||||
checkContextMatch(s, index);
|
||||
return new SeqExpr(this, Native.mkSeqAt(nCtx, s.NativeObject, index.NativeObject));
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract subsequence.
|
||||
*/
|
||||
public SeqExpr MkExtract(SeqExpr s, IntExpr offset, IntExpr length)
|
||||
{
|
||||
checkContextMatch(s, offset, length);
|
||||
return new SeqExpr(this, Native.mkSeqExtract(nCtx, s.NativeObject, offset.NativeObject, length.NativeObject));
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract index of sub-string starting at offset.
|
||||
*/
|
||||
public IntExpr MkIndexOf(SeqExpr s, SeqExpr substr, ArithExpr offset)
|
||||
{
|
||||
checkContextMatch(s, substr, offset);
|
||||
return new IntExpr(this, Native.mkSeqIndex(nCtx, s.NativeObject, substr.NativeObject, offset.NativeObject));
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace the first occurrence of src by dst in s.
|
||||
*/
|
||||
public SeqExpr MkReplace(SeqExpr s, SeqExpr src, SeqExpr dst)
|
||||
{
|
||||
checkContextMatch(s, src, dst);
|
||||
return new SeqExpr(this, Native.mkSeqReplace(nCtx, s.NativeObject, src.NativeObject, dst.NativeObject));
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a regular expression that accepts sequence s.
|
||||
*/
|
||||
public ReExpr MkToRe(SeqExpr s)
|
||||
{
|
||||
checkContextMatch(s);
|
||||
return new ReExpr(this, Native.mkSeqToRe(nCtx, s.NativeObject));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check for regular expression membership.
|
||||
*/
|
||||
public BoolExpr MkInRe(SeqExpr s, ReExpr re)
|
||||
{
|
||||
checkContextMatch(s, re);
|
||||
return new BoolExpr(this, Native.mkSeqInRe(nCtx, s.NativeObject, re.NativeObject));
|
||||
}
|
||||
|
||||
/**
|
||||
* Take the Kleene star of a regular expression.
|
||||
*/
|
||||
public ReExpr MkStar(ReExpr re)
|
||||
{
|
||||
checkContextMatch(re);
|
||||
return new ReExpr(this, Native.mkReStar(nCtx, re.NativeObject));
|
||||
}
|
||||
|
||||
/**
|
||||
* Take the Kleene plus of a regular expression.
|
||||
*/
|
||||
public ReExpr MPlus(ReExpr re)
|
||||
{
|
||||
checkContextMatch(re);
|
||||
return new ReExpr(this, Native.mkRePlus(nCtx, re.NativeObject));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the optional regular expression.
|
||||
*/
|
||||
public ReExpr MOption(ReExpr re)
|
||||
{
|
||||
checkContextMatch(re);
|
||||
return new ReExpr(this, Native.mkReOption(nCtx, re.NativeObject));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the concatenation of regular languages.
|
||||
*/
|
||||
public ReExpr MkConcat(ReExpr[] t)
|
||||
{
|
||||
checkContextMatch(t);
|
||||
return new ReExpr(this, Native.mkReConcat(nCtx, (uint)t.Length, AST.ArrayToNative(t)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the union of regular languages.
|
||||
*/
|
||||
public ReExpr MkUnion(ReExpr[] t)
|
||||
{
|
||||
checkContextMatch(t);
|
||||
return new ReExpr(this, Native.mkReUnion(nCtx, (uint)t.Length, AST.ArrayToNative(t)));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a Term of a given sort.
|
||||
* @param v A string representing the term value in decimal notation. If the given sort is a real, then the
|
||||
|
@ -3683,6 +3861,19 @@ public class Context extends IDisposable
|
|||
throw new Z3Exception("Context mismatch");
|
||||
}
|
||||
|
||||
void checkContextMatch(Z3Object other1, Z3Object other2)
|
||||
{
|
||||
checkContextMatch(other1);
|
||||
checkContextMatch(other2);
|
||||
}
|
||||
|
||||
void checkContextMatch(Z3Object other1, Z3Object other2, Z3Object other3)
|
||||
{
|
||||
checkContextMatch(other1);
|
||||
checkContextMatch(other2);
|
||||
checkContextMatch(other3);
|
||||
}
|
||||
|
||||
void checkContextMatch(Z3Object[] arr)
|
||||
{
|
||||
if (arr != null)
|
||||
|
|
|
@ -2186,6 +2186,10 @@ public class Expr extends AST
|
|||
return new FPRMExpr(ctx, obj);
|
||||
case Z3_FINITE_DOMAIN_SORT:
|
||||
return new FiniteDomainExpr(ctx, obj);
|
||||
case Z3_SEQ_SORT:
|
||||
return new SeqExpr(ctx, obj);
|
||||
case Z3_RE_SORT:
|
||||
return new ReExpr(ctx, obj);
|
||||
default: ;
|
||||
}
|
||||
|
||||
|
|
|
@ -141,6 +141,10 @@ public class Sort extends AST
|
|||
return new FPSort(ctx, obj);
|
||||
case Z3_ROUNDING_MODE_SORT:
|
||||
return new FPRMSort(ctx, obj);
|
||||
case Z3_SEQ_SORT:
|
||||
return new SeqSort(ctx, obj);
|
||||
case Z3_RE_SORT:
|
||||
return new ReSort(ctx, obj);
|
||||
default:
|
||||
throw new Z3Exception("Unknown sort kind");
|
||||
}
|
||||
|
|
|
@ -8991,7 +8991,7 @@ class SeqRef(ExprRef):
|
|||
def __getitem__(self, i):
|
||||
return SeqRef(Z3_mk_seq_at(self.ctx_ref(), self.as_ast(), i.as_ast()), self.ctx)
|
||||
|
||||
def is_string_sort(self):
|
||||
def is_string(self):
|
||||
return Z3_is_string_sort(self.ctx_ref(), Z3_get_sort(self.ctx_ref(), self.as_ast()))
|
||||
|
||||
def is_string_value(self):
|
||||
|
@ -9026,12 +9026,12 @@ def is_seq(a):
|
|||
"""
|
||||
return isinstance(a, SeqRef)
|
||||
|
||||
def is_string_sort(a):
|
||||
def is_string(a):
|
||||
"""Return `True` if `a` is a Z3 string expression.
|
||||
>>> print (is_string_sort(StringVal("ab")))
|
||||
>>> print (is_string(StringVal("ab")))
|
||||
True
|
||||
"""
|
||||
return isinstance(a, SeqRef) and a.is_string_sort()
|
||||
return isinstance(a, SeqRef) and a.is_string()
|
||||
|
||||
def is_string_value(a):
|
||||
"""return 'True' if 'a' is a Z3 string constant expression.
|
||||
|
@ -9042,11 +9042,27 @@ def is_string_value(a):
|
|||
"""
|
||||
return isinstance(a, SeqRef) and a.is_string_value()
|
||||
|
||||
|
||||
def StringVal(s, ctx=None):
|
||||
"""create a string expression"""
|
||||
ctx = _get_ctx(ctx)
|
||||
return SeqRef(Z3_mk_string(ctx.ref(), s), ctx)
|
||||
|
||||
def String(name, ctx=None):
|
||||
"""Return a string constant named `name`. If `ctx=None`, then the global context is used.
|
||||
|
||||
>>> x = String('x')
|
||||
"""
|
||||
ctx = _get_ctx(ctx)
|
||||
return SeqRef(Z3_mk_const(ctx.ref(), to_symbol(name, ctx), StringSort(ctx).ast), ctx)
|
||||
|
||||
def Strings(names, ctx=None):
|
||||
"""Return a tuple of String constants. """
|
||||
ctx = _get_ctx(ctx)
|
||||
if isinstance(names, str):
|
||||
names = names.split(" ")
|
||||
return [String(name, ctx) for name in names]
|
||||
|
||||
def Empty(s):
|
||||
"""Create the empty sequence of the given sort
|
||||
>>> e = Empty(StringSort())
|
||||
|
@ -9101,6 +9117,10 @@ def Contains(a, b):
|
|||
>>> s2 = Contains("abc", "bc")
|
||||
>>> simplify(s2)
|
||||
True
|
||||
>>> x, y, z = Strings('x y z')
|
||||
>>> s3 = Contains(Concat(x,y,z), y)
|
||||
>>> simplify(s3)
|
||||
True
|
||||
"""
|
||||
ctx = _get_ctx2(a, b)
|
||||
a = _coerce_seq(a, ctx)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue