2

理想情况下,可以“或”表示为位向量的两个数字,但我做不到。请告诉代码中是否有错误或其他问题

line1 = BitVec('line1', 1)
line2 = BitVec('line2', 1)
s = Solver()
s.add(Or(line1, line2) == 0)
print s.check()

给出的错误是

error: 'type error'
WARNING: invalid function application for or, sort mismatch on argument at position 1,         expected Bool but given (_ BitVec 1)
WARNING: (declare-fun or (Bool Bool) Bool) applied to: 
line1 of sort (_ BitVec 1)
line2 of sort (_ BitVec 1)

从这个错误中,我了解到 Or 只能用于 bool 变量。我的问题是如何或为 BitVectors

4

1 回答 1

5

Yes, Or(a,b) is a boolean disjunction, you probably want a bitwise or since you're trying to compare bitvectors, which can be done in the Python API using | from the documentation (here's a z3py link for the example: http://rise4fun.com/Z3Py/1l0):

line1 = BitVec('line1', 2)
line2 = BitVec('line2', 2)
s = Solver()
P = (line1 | line2) != 0
print P
s.add(P)
print s.check()
print s.model() # [line2 = 0, line1 = 3]

I updated your example so that line1 and line2 are longer than 1 bit (which would be equivalent to the boolean case, but is a different type, hence the error).

Note that this is bvor in the SMT-LIB standard, see http://smtlib.cs.uiowa.edu/logics/V1/QF_BV.smt

于 2014-03-12T17:02:36.340 回答