I'm new to using Z3 but I am seeing some strange behavior when trying to enumerate solutions to a set of boolean constraints. My code is below:
from z3 import Bool, Xor, Or, And, Not, Solver, sat
x0 = Bool("x0")
x1 = Bool("x1")
x2 = Bool("x2")
x3 = Bool("x3")
solver = Solver()
solver.add(Xor(x0, x1))
solver.add(Xor(Or(x2, x0), Not(x3)))
while solver.check() == sat:
model = solver.model()
print()
print(solver)
print(model)
cons = Or([d() != model[d] for d in model])
solver.add(cons)
Which outputs
[Xor(x0, x1), Xor(Or(x2, x0), Not(x3))]
[x3 = False, x2 = False, x1 = True, x0 = False]
[Xor(x0, x1),
Xor(Or(x2, x0), Not(x3)),
Or(x3 != False, x2 != False, x1 != True, x0 != False)]
[x3 = True, x1 = False, x0 = True]
[Xor(x0, x1),
Xor(Or(x2, x0), Not(x3)),
Or(x3 != False, x2 != False, x1 != True, x0 != False),
Or(x3 != True, x1 != False, x0 != True)]
[x3 = True, x2 = True, x1 = True, x0 = False]
You can see that the second model gives [x3 = True, x1 = False, x0 = True]
with no reference to the value of x2
even though x2
clearly has constraints in the solver.