Can somebody please explain why I am getting a counter example with this py code.
a = Int('a')
def X(a):
if (a == 3):
return 1
else:
return 2
z3.prove( X(a) == If (a == 3, 1, 2) )
counterexample [a = 3]
Can somebody please explain why I am getting a counter example with this py code.
a = Int('a')
def X(a):
if (a == 3):
return 1
else:
return 2
z3.prove( X(a) == If (a == 3, 1, 2) )
counterexample [a = 3]
Your function 'X' will always return '2' for all symbolic variables. That is:
will always print:
This is precisely why Z3 provides the
If
function. Python's 'if' does not work for symbolic values and the tests will simply take theelse
branch.Aside:
I'd call this a weakness of the Python interface to Z3, since it's really weakly typed. Ideally, this program should be rejected because of the illegal use of if-then-else, but Python isn't a sufficiently strongly-typed language to give you that level of safety. Bindings to Z3 from other languages would reject the equivalent expression as type incorrect at compile time.
X(.)
is a Python function, not a logical or Z3 function. It is evaluated when it is called. This means that the value ofX(a)
inis determined to be
2
beforez3.prove
is called, becausea
is anInt('a')
(and not3
). In this particular case, it's easy to debug such problems by simply printing the Z3 expressions, e.g.reports