How can I get real python values from a Z3 model?
E.g.
p = Bool('p')
x = Real('x')
s = Solver()
s.add(Or(x < 5, x > 10), Or(p, x**2 == 2), Not(p))
s.check()
print s.model()[x]
print s.model()[p]
prints
-1.4142135623?
False
but those are Z3 objects and not python float/bool objects.
I know that I can check boolean values using is_true/is_false, but how can I elegantly convert ints/reals/… back to usable values (without going through strings and cutting away this extra ? symbol, for example).
For Boolean values, you can use the functions
is_trueandis_false. Numerical values can be integer, rational or algebraic. We can use the functionsis_int_value,is_rational_valueandis_algebraic_valueto test each case. The integer case is the simplest, we can use the methodas_long()to convert the Z3 integer value into a Python long. For rational values, we can use the methodsnumerator()anddenominator()to obtain the Z3 integers representing the numerator and denominator. The methodsnumerator_as_long()anddenominator_as_long()are shortcuts forself.numerator().as_long()andself.denominator().as_long(). Finally, algebraic numbers are used to represent irrational numbers. TheAlgebraicNumRefclass has a method calledapprox(self, precision). It returns a Z3 rational number that approximates the algebraic number with precision1/10^precision. Here is an example on how to use this methods. It is also available online at: http://rise4fun.com/Z3Py/Mkw