Created
January 2, 2021 20:48
-
-
Save TeWu/7a7c57d4f556455d2c1285b587b3f349 to your computer and use it in GitHub Desktop.
Z3 incorrect ignore model in solver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from z3 import * | |
inp_length = 2 | |
vec = '' | |
for i in range(0,inp_length): | |
vec += f"inp[{i}] " # inp[0] inp[1] | |
inp = BitVecs(vec, 8) | |
solver = Solver() | |
for i in range(0,2): | |
solver.add(inp[i] & 0b111111110 == 0) # Allow only first bit to be one | |
while solver.check() == z3.sat: | |
model = solver.model() | |
# solver.add([i != model[i] for i in inp]) # <-- Incorrect | |
solver.add(Or( # <-- Correct | |
[inp[i] != model[inp[i]] for i in range(0,inp_length)] | |
)) | |
print(model) | |
## Output with correct line uncommented: | |
## [inp[1] = 0, inp[0] = 0] | |
## [inp[0] = 1, inp[1] = 1] | |
## [inp[0] = 1, inp[1] = 0] | |
## [inp[0] = 0, inp[1] = 1] | |
## Output with incorrect line uncommented: | |
## [inp[1] = 0, inp[0] = 0] | |
## [inp[0] = 1, inp[1] = 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See also: https://stackoverflow.com/questions/13395391/z3-finding-all-satisfying-models