【问题标题】:how to eliminate bitvector arithmetic in Z3如何消除Z3中的位向量算术
【发布时间】:2012-12-27 07:58:02
【问题描述】:

我正在尝试使用z3来消除表达式

not ((not x) add y)

等于

x sub y

通过这个code:

(declare-const x (_ BitVec 32))
(declare-const y (_ BitVec 32))
(assert (= (bvnot (bvadd (bvnot x) y)) (bvsub x y)))
(check-sat)
(simplify (bvnot (bvadd (bvnot x) y)))

我想得到如下结果:

sat
(bvsub x y) 

但是,结果是:

sat
(bvnot (bvadd (bvnot x) y))

有人能帮帮我吗?

【问题讨论】:

    标签: z3 bitvector


    【解决方案1】:

    我们可以使用以下脚本证明(bvnot (bvadd (bvnot x) y)) 等价于(bvsub x y)。

    (declare-const x (_ BitVec 32))
    (declare-const y (_ BitVec 32))
    (assert (not (= (bvnot (bvadd (bvnot x) y)) (bvsub x y))))
    (check-sat)
    

    在这个脚本中,我们使用 Z3 来表明 (not (= (bvnot (bvadd (bvnot x) y)) (bvsub x y))) 是不可满足的。也就是说,不可能找到x 和y 的值,使得(bvnot (bvadd (bvnot x) y)) 的值不同于(bvsub x y) 的值。

    在 Z3 中,simplify 命令只是应用重写规则,它忽略了断言的表达式集。 simplify 命令比使用check-sat 检查可满足性要快得多。此外,给定两个等效表达式F 和G,不能保证(simplify F) 的结果等于(simplify G)。例如,在 Z3 v4.3.1 中,simplify 命令为 (= (bvnot (bvadd (bvnot x) y) 和 (bvsub x y) 生成不同的结果,尽管它们是等价的表达式。另一方面,它对(= (bvneg (bvadd (bvneg x) y) 和(bvsub x y) 产生相同的结果。

    (simplify (bvnot (bvadd (bvnot x) y)))
    (simplify (bvneg (bvadd (bvneg x) y)))
    (simplify (bvsub x y))
    

    Here 是上述示例的完整脚本。

    顺便说一句,如果我们使用Z3 Python interface,这些示例的可读性会更高。

    x, y = BitVecs('x y', 32)
    prove(~(~x + y) == x - y)
    print simplify(x - y)
    print simplify(~(~x + y))
    print simplify(-(-x + y))
    

    最后,Z3 有更复杂的简化程序。它们以战术的形式提供。 Python 和 SMT 2.0 格式的教程提供了更多信息。

    另一种可能性是修改 Z3 简化器/重写器。正如您所指出的,not x 等同于-x -1。我们可以轻松地将这个重写规则:not x --> -x - 1 添加到 Z3 重写器。 例如,in this commit,我添加了一个名为“bvnot2arith”的新选项来启用此规则。 实际实现非常小(5行代码)。

    【讨论】:

    • 感谢您的回复。但事实上,我对bvnot 和bvneg 并没有混淆。我在这里需要的 IS 是按位非运算。众所周知,在位向量算术中,-x = not x + 1 => not x = -x - 1,因此,not ((not x) + y) = -((-x - 1) + y) - 1 = x + 1 - y - 1 = x - y。但是,您提到的战术确实对我有所帮助,根据Custom simplifiers,您之前回答过的问题。看来我需要一个自定义策略,对吧?无论如何,感谢您的帮助。
    • 我的错,我会修正答案。
    • 我还添加了重写规则not x --> -x -1作为如何破解Z3重写器的示例(z3.codeplex.com/SourceControl/changeset/8515044f8bec)
    猜你喜欢
    • 2016-03-26
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    相关资源
    最近更新 更多