4

How can I perform quantifier elimination using the Python API of Z3? Although I checked the tutorial and API, couldn't manage to do it.

I have a formula that has an existential quantifier and I want Z3 to give me a new formula, such that this quantifier is eliminated. I essentially want to do the same thing as this:

How to hide variable with Z3

but with the Python interface. Also my formula is in linear arithmetic.

Thanks!

Addition: After doing the quantifier elimination I will "add" the quantifier-free formula with another one. So if I make use of the Tactic, is there a way to convert a subgoal (which is the output of the tactic) to an expression in linear arithmetic?

Community
  • 1
  • 1
blurium
  • 43
  • 5

3 Answers3

6

You may use the quantifier elimination tactic for this (see the Tactic.apply docstring):

from z3 import Ints, Tactic, Exists, And
x, t1, t2 = Ints('x t1 t2')
t = Tactic('qe')
print t(Exists(x, And(t1 < x, x < t2)))
tbormer
  • 76
  • 3
  • 1
    I guess we don't need "apply" there. See below: http://rise4fun.com/Z3Py/sQs0 The problem I am having is, I cannot use the output of the tactic which is a subgoal and "and" it with another expression. See below: http://rise4fun.com/Z3Py/m7wq Somehow, I have to convert the subgoal to a boolean expression. Is there a way to do that? – blurium Jul 29 '13 at 18:31
  • 3
    The output is actually an `ApplyResult` object, which you can use `as_expr()` with. See the API documentation: http://research.microsoft.com/en-us/um/redmond/projects/z3/z3.html#ApplyResult-as_expr and here's your example: http://rise4fun.com/Z3Py/BNDc – Taylor T. Johnson Jul 30 '13 at 00:12
  • @blurium You're right about the "apply", thanks for reminding me about that. – tbormer Jul 30 '13 at 07:48
0

Possible solution using Z3Py online:

x, t1, t2 = Reals('x t1 t2')
g = Goal()
g.add(Exists(x, And(t1 < x, x < t2)))
t = Tactic('qe')
print t(g)

Output:

[[¬(0 ≤ t1 + -1·t2)]]

Run this example online here

Juan Ospina
  • 1,317
  • 1
  • 7
  • 15
  • 1
    The problem I have with Tactic is that, its output is a subgoal and belongs to the class ApplyResult and I want to further "and" the resulting quantifier free expression with another one and then I get the error: 'True, False or Z3 Boolean expression expected' as in here: http://rise4fun.com/Z3Py/Kll9 Is there a way to convert the subgoal into an expression I can manipulate further? – blurium Jul 29 '13 at 17:08
0

Possible solution using Redlog of reduce:

enter image description here

Juan Ospina
  • 1,317
  • 1
  • 7
  • 15
  • Do you have any idea whether Reduce Redlog or Z3 is more efficient while doing quantifier elimination? In other words, which one scales better in terms of number of variables and number of terms in the expression that we perform quantifier elimination on? – blurium Jul 30 '13 at 21:53
  • I think that Redlog is more powerful and efficient than Z3 doing quantifier elimination. – Juan Ospina Jul 31 '13 at 10:58