【问题标题】:Z3py: how to get the list of variables from a formula?Z3py:如何从公式中获取变量列表?
【发布时间】:2012-12-29 09:34:17
【问题描述】:

在 Z3Py 中,我有一个公式。如何检索公式中使用的变量列表?

谢谢。

【问题讨论】:

    标签: z3 z3py


    【解决方案1】:

    Vu Nguyen 为 Z3Py 贡献了几个有用的程序。 他实现了一个程序get_vars 来收集使用的变量列表。 这个程序和许多其他程序可以在here 找到。 他的贡献将在下一个正式版本中提供。 这是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))))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多