0

How can Z3 return a valid counterexample? The following code

from z3 import *
set_param(proof=True)
x = Real('x')
f = ForAll(x, x * x > 0)
prove(f)

outputs counterexample [].

I don't have to use prove, but I want to find a valid counterexample to a formula like f in the example. How can I do it?

Rodrigo de Azevedo
  • 1,097
  • 9
  • 17
yokke
  • 99
  • 7
  • It seems it works as expected when I drop the `ForAll`. Could anybody explain why it works now, instead of just downvoting please? – yokke Jun 11 '18 at 12:47

1 Answers1

1

To get a model, you should really use check, and assert the negation of your formula in a solver context:

from z3 import *

s = Solver()
x = Real('x')
f = x * x > 0

# Add negation of our formula
# So, if it's not valid, we'll get a model
s.add(Not(f))

print s.check()
print s.model()

This produces:

sat
[x = 0]
alias
  • 28,120
  • 2
  • 23
  • 40
  • Yes, I've looked into `prove` and that's what it does. Why is it necessary to drop the `ForAll`? – yokke Jun 11 '18 at 15:45
  • When models are constructed, Z3 only collects the variables at the top-level. With the quantifier `ForAll`, the variable becomes nested, and thus is ignored by Z3 when the model is built. You can see what z3py constructs by putting in different formulas and calling `print s.sexpr()` before you call `s.check()`. – alias Jun 11 '18 at 15:53