3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-12 12:08:18 +00:00

address some of the ugliness pointed out by abandoned pull request #5008

This commit is contained in:
Nikolaj Bjorner 2021-02-09 11:23:16 -08:00
parent 8b5094fe73
commit e722589810

View file

@ -11,6 +11,7 @@ Usage:
import common_z3 as CM_Z3 import common_z3 as CM_Z3
""" """
import ctypes
from .z3 import * from .z3 import *
def vset(seq, idfun=None, as_list=True): def vset(seq, idfun=None, as_list=True):
@ -52,8 +53,8 @@ def get_z3_version(as_str=False):
minor = ctypes.c_uint(0) minor = ctypes.c_uint(0)
build = ctypes.c_uint(0) build = ctypes.c_uint(0)
rev = ctypes.c_uint(0) rev = ctypes.c_uint(0)
Z3_get_version(major,minor,build,rev) Z3_get_version(major,minor, build, rev)
rs = map(int,(major.value,minor.value,build.value,rev.value)) rs = map(int, (major.value, minor.value, build.value, rev.value))
if as_str: if as_str:
return "{}.{}.{}.{}".format(*rs) return "{}.{}.{}.{}".format(*rs)
else: else:
@ -69,7 +70,7 @@ def ehash(v):
Note: the following doctests will fail with Python 2.x as the Note: the following doctests will fail with Python 2.x as the
default formatting doesn't match that of 3.x. default formatting doesn't match that of 3.x.
>>> x1 = Bool('x'); x2 = Bool('x'); x3 = Int('x') >>> x1 = Bool('x'); x2 = Bool('x'); x3 = Int('x')
>>> print(x1.hash(),x2.hash(),x3.hash()) #BAD: all same hash values >>> print(x1.hash(), x2.hash(), x3.hash()) #BAD: all same hash values
783810685 783810685 783810685 783810685 783810685 783810685
>>> print(ehash(x1), ehash(x2), ehash(x3)) >>> print(ehash(x1), ehash(x2), ehash(x3))
x_783810685_1 x_783810685_1 x_783810685_2 x_783810685_1 x_783810685_1 x_783810685_2
@ -78,7 +79,7 @@ def ehash(v):
if z3_debug(): if z3_debug():
assert is_expr(v) assert is_expr(v)
return "{}_{}_{}".format(str(v),v.hash(),v.sort_kind()) return "{}_{}_{}".format(str(v), v.hash(), v.sort_kind())
""" """
@ -140,7 +141,7 @@ def is_expr_val(v):
def get_vars(f,rs=[]): def get_vars(f, rs = None):
""" """
>>> x,y = Ints('x y') >>> x,y = Ints('x y')
>>> a,b = Bools('a b') >>> a,b = Bools('a b')
@ -148,6 +149,9 @@ def get_vars(f,rs=[]):
[x, y, a, b] [x, y, a, b]
""" """
if rs is None:
rs = []
if z3_debug(): if z3_debug():
assert is_expr(f) assert is_expr(f)
@ -159,13 +163,13 @@ def get_vars(f,rs=[]):
else: else:
for f_ in f.children(): for f_ in f.children():
rs = get_vars(f_,rs) rs = get_vars(f_, rs)
return vset(rs,str) return vset(rs, str)
def mk_var(name,vsort): def mk_var(name, vsort):
if vsort.kind() == Z3_INT_SORT: if vsort.kind() == Z3_INT_SORT:
v = Int(name) v = Int(name)
elif vsort.kind() == Z3_REAL_SORT: elif vsort.kind() == Z3_REAL_SORT:
@ -173,12 +177,9 @@ def mk_var(name,vsort):
elif vsort.kind() == Z3_BOOL_SORT: elif vsort.kind() == Z3_BOOL_SORT:
v = Bool(name) v = Bool(name)
elif vsort.kind() == Z3_DATATYPE_SORT: elif vsort.kind() == Z3_DATATYPE_SORT:
v = Const(name,vsort) v = Const(name, vsort)
else: else:
assert False, 'Cannot handle this sort (s: %sid: %d)'\ raise TypeError(f"Cannot handle this sort (s: {vsort}id: {vsort.kind()})")
%(vsort,vsort.kind())
return v return v
@ -231,7 +232,6 @@ def prove(claim,assume=None,verbose=0):
if z3_debug(): if z3_debug():
assert not assume or is_expr(assume) assert not assume or is_expr(assume)
to_prove = claim to_prove = claim
if assume: if assume:
if z3_debug(): if z3_debug():
@ -247,8 +247,6 @@ def prove(claim,assume=None,verbose=0):
to_prove = Implies(assume,to_prove) to_prove = Implies(assume,to_prove)
if verbose >= 2: if verbose >= 2:
print('assume: ') print('assume: ')
print(assume) print(assume)
@ -314,10 +312,8 @@ def get_models(f,k):
if z3_debug(): if z3_debug():
assert is_expr(f) assert is_expr(f)
assert k>=1 assert k >= 1
s = Solver() s = Solver()
s.add(f) s.add(f)
@ -325,12 +321,9 @@ def get_models(f,k):
i = 0 i = 0
while s.check() == sat and i < k: while s.check() == sat and i < k:
i = i + 1 i = i + 1
m = s.model() m = s.model()
if not m: #if m == [] if not m: #if m == []
break break
models.append(m) models.append(m)
@ -341,7 +334,7 @@ def get_models(f,k):
if s.check() == unknown: if s.check() == unknown:
return None return None
elif s.check() == unsat and i==0: elif s.check() == unsat and i == 0:
return False return False
else: else:
return models return models
@ -461,27 +454,27 @@ def myBinOp(op,*L):
if L: if L:
if len(L)==1: if len(L)==1:
return L[0] return L[0]
else: if op == Z3_OP_OR:
if op == Z3_OP_OR: return Or(L)
return Or(L) if op == Z3_OP_AND:
elif op == Z3_OP_AND: return And(L)
return And(L) return Implies(L[0],L[1])
else: #IMPLIES
return Implies(L[0],L[1])
else: else:
return None return None
def myAnd(*L): return myBinOp(Z3_OP_AND,*L) def myAnd(*L):
def myOr(*L): return myBinOp(Z3_OP_OR,*L) return myBinOp(Z3_OP_AND,*L)
def myImplies(a,b):return myBinOp(Z3_OP_IMPLIES,[a,b])
def myOr(*L):
return myBinOp(Z3_OP_OR,*L)
def myImplies(a,b):
return myBinOp(Z3_OP_IMPLIES,[a,b])
Iff = lambda f: And(Implies(f[0],f[1]),Implies(f[1],f[0])) Iff = lambda f: And(Implies(f[0],f[1]),Implies(f[1],f[0]))
def model_str(m,as_str=True): def model_str(m,as_str=True):
""" """
Returned a 'sorted' model (so that it's easier to see) Returned a 'sorted' model (so that it's easier to see)