Z3¶
Variable Type¶
Int(name: str)
: integerReal(name: str)
: real numberBool(name: str)
:True
orFalse
BitVec(name: str, length: int)
:length
bits integer
Solver¶
s = Solver()
s.add(<condition>)
: add condition to Solvers.check()
: if there’s a solve for Solver returnsat
, else returnunsat
s.model()
: return a solve of this Solver. we can uses.model()[x]
to getx
's value in this solve
Function¶
And([condition1, condition2, ...])
orAnd(condition1, condition2, ...)
: combine these conditions with andOr([condition1, condition2, ...])
orOr(condition1, condition2, ...)
: combine these conditions with or
Example¶
from z3 import *
x = Int('x')
y = Int('y')
z = Int('z')
s = Solver()
s.add(And(x > 0, y > 0, z > 0))
s.add(x + y + z > 10)
s.add(x + y < 5)
s.add(y + z < 12)
while s.check() == sat:
ans = s.model()
print(ans)
s.add(Or(x != ans[x], y != ans[y], z != ans[z]))