基于参数的函数返回错误值(Z3,Python)

基于参数的函数返回错误值(Z3,Python),z3,smt,z3py,Z3,Smt,Z3py,我正在模拟一个具有简单函数(Tab)的数组,但它没有按预期工作。如果我在SMT2中编写代码,它工作得很好,但在Z3py中它不工作 from z3 import * A = BitVec('A', 8) B1 = BitVec('B1', 8) B2 = BitVec('B2', 8) B3 = BitVec('B3', 8) B4 = BitVec('B4', 8) # Emulate Array def Tab(N): if N == 0x01: return B1 if N ==

我正在模拟一个具有简单函数(Tab)的数组,但它没有按预期工作。如果我在SMT2中编写代码,它工作得很好,但在Z3py中它不工作

from z3 import *

A = BitVec('A', 8)
B1 = BitVec('B1', 8)
B2 = BitVec('B2', 8)
B3 = BitVec('B3', 8)
B4 = BitVec('B4', 8)

# Emulate Array
def Tab(N):
  if N == 0x01: return B1
  if N == 0x02: return B2
  if N == 0x03: return B3
  if N == 0x04: return B4

s = Solver()
s.add(A == 0x01)
s.add(Tab(A + 0x02) == 0x09 )
s.check()
m = s.model()

print (m)
print("Pos:", m.eval(A + 0x02))
print("Tab(3a):", m.eval(Tab(A + 0x02)))
print("Tab(3):", m.eval(Tab(0x03)))
print("B1: ", m[B1])
print("B2: ", m[B2])
print("B3: ", m[B3])
print("B4: ", m[B4])
print("B3n:", m.eval(B3))
输出:

[B1 = 9, A = 1] <- Model
Pos: 3          <- this is OK
Tab(3a): 9      <- this is OK (based on our condition)
Tab(3):  B3     <- why does this return name B3? (should return None or 9)
B1:  9          <- this is BAD, here should be None
B2:  None
B3:  None       <- this is BAD, here should be 9
B4:  None
B3n: B3         <- why does this return name B3?! (should return None or 9)

[B1=9,A=1]这看起来和本文中的问题一样:


问题是,
if N==0x01:…
不会创建Z3 if-then-else表达式;它逐字检查
N
是否是一个具体值为1的Python int。要获得所需的表达式,您需要使用Z3。

谢谢,如果THEN在Python环境中,并且应该在Z3中,那么您是对的。解决方案是:`返回z3.If(N==0x01,B1,`
z3.If(N==0x02,B2,
z3.If(N==0x03,B3,B4))