-->

Z3py:如何获得从公式变量列表?(Z3py: how to get the list of var

2019-07-17 16:12发布

在Z3Py,我有一个公式。 我怎样才能检索使用公式中的变量列表?

谢谢。

Answer 1:

武阮促成Z3Py几个有用的程序。 他实现的过程get_vars收集使用的变量列表。 这个过程和其他许多人都可以找到这里 。 他的贡献将在未来正式发布上市。 这里是一个版本get_vars可以在网上被执行rise4fun 。

# Wrapper for allowing Z3 ASTs to be stored into Python Hashtables. 
class AstRefKey:
    def __init__(self, n):
        self.n = n
    def __hash__(self):
        return self.n.hash()
    def __eq__(self, other):
        return self.n.eq(other.n)
    def __repr__(self):
        return str(self.n)

def askey(n):
    assert isinstance(n, AstRef)
    return AstRefKey(n)

def get_vars(f):
    r = set()
    def collect(f):
      if is_const(f): 
          if f.decl().kind() == Z3_OP_UNINTERPRETED and not askey(f) in r:
              r.add(askey(f))
      else:
          for c in f.children():
              collect(c)
    collect(f)
    return r

x,y = Ints('x y') 
a,b = Bools('a b') 
print get_vars(Implies(And(x + y == 0, x*2 == 10),Or(a, Implies(a, b == False))))


文章来源: Z3py: how to get the list of variables from a formula?
标签: z3 z3py