Skip to main content
 首页 » 编程设计

python之Z3 发现模型与公理不一致

2024年12月31日8mate10pro

当使用 z3-solver 模块 (4.8.0.0) 在 Python 3.6.7 上运行此代码时,z3 返回的模型似乎对公理无效。

f = z3.Function('f', z3.IntSort(), z3.IntSort(), z3.IntSort()) 
x = z3.Int('x') 
s = z3.Solver() 
s.add(f(1, 10) == 42) 
s.add(z3.ForAll([x], f(2, x) == f(1, x))) 
s.check() 
m = s.model() 
print(m.eval(f(1, 10)))  # print 0 
print(m.eval(f(2, 10)))  # print 0 

为什么我们没有像预期的那样得到 42?公理或函数有问题吗?

请您参考如下方法:

看起来你的安装可能被破坏了,因为我无法复制它:

$ cat a.py 
import z3 
f = z3.Function('f', z3.IntSort(), z3.IntSort(), z3.IntSort()) 
x = z3.Int('x') 
s = z3.Solver() 
s.add(f(1, 10) == 42) 
s.add(z3.ForAll([x], f(2, x) == f(1, x))) 
print s.sexpr() 
s.check() 
m = s.model() 
print(m.eval(f(1, 10)))  # print 0 
print(m.eval(f(2, 10)))  # print 0 
 
$ python a.py 
(declare-fun f (Int Int) Int) 
(assert (= (f 1 10) 42)) 
(assert (forall ((x Int)) (= (f 2 x) (f 1 x)))) 
 
42 
42 

请注意,我将 print s.sexpr() 添加到您的代码中,它很好地打印了生成的 SMTLib。你看一样吗?