计算Z3量化公式中的变量数量

问题描述 投票:2回答:1

我正在尝试收集公式中的所有变量(Z3py中的量化公式)。一个小例子

w, x, y, z = Bools('w x y z')
fml = And( ForAll(x, ForAll(y, And(x, y))), ForAll(z, ForAll(w, And(z, w))) ) 

varSet = traverse( fml )

我用来遍历的代码是

def traverse(e):
  r = set()
  def collect(e):
    if is_quantifier(e):
      # Lets assume there is only one type of quantifier
      if e.is_forall():
          collect(e.body())
    else:
      if ( is_and(e) ):
          n = e.num_args()
          for i in range(n):
              collect( e.arg(i) )
      if ( is_or(e) ):
          n = e.num_args()
          for i in range(n):
              collect( e.arg(i) )
      if ( is_not(e) ):
          collect( e.arg(0) )
      if ( is_var(e) ):
          r.add( e )
  collect(e)
  return r

我得到:set([Var(0),Var(1)])。据我所知,这是由于Z3使用De Bruijn index。是否可以避免这种情况并获得所需的设置:set([Var(0),Var(1),Var(2),Var(3)])。

z3 z3py quantifiers
1个回答
1
投票

你的代码是正确的;在这个例子中没有Var(2)Var(3)。有两个顶级量词,每个量的de-Bruijn指数分别为0和1.这两个量词不会出现在另一个量词的主体内,因此不会产生混淆。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.