Below is the “standard recipe” people use with an SMT solver such as Z3 to decide whether two constraints (or two sets of constraints) are logically equivalent.
TL;DR
Two formulas ϕ and ψ are equivalent ⇔ the formula ϕ ⊕ ψ (exclusive-or) is UNSAT.
So: feed ϕ ⊕ ψ to a solver, ask check(), and look at the result.
────────────────────────────────────────────
- Intuition
• If Z3 can find a model in which ϕ and ψ have different truth values, they are not equivalent and the model is your counter-example.
• If Z3 reports UNSAT, there is no interpretation that separates them, hence they are equivalent.
────────────────────────────────────────────
2. One-liner in z3-py
is_equiv = Solver(); is_equiv.add(Xor(phi, psi))
eq = (is_equiv.check() == unsat)
eq is True ⇒ equivalent.
────────────────────────────────────────────
3. Complete worked example
from z3 import *
# Variables
x, y = Ints('x y')
# Formula/group 1
phi = x + y > 10
# Formula/group 2 (obviously not the same)
psi = And(x > 5, y > 5)
# Build “difference” query
s = Solver()
s.add(Xor(phi, psi)) # xor is true ⇔ formulas differ
result = s.check()
if result == sat:
print("Not equivalent!")
print("Model that separates them:", s.model())
else:
print("Equivalent!")
Output:
Not equivalent!
Model that separates them: [y = 10, x = 1]
Indeed, with x=1,y=10 we have phi true, psi false.
────────────────────────────────────────────
4. Multiple constraints
Often you have sets instead of single formulas.
E.g. C1 = {c11, c12, …} and C2 = {c21, c22, …}.
Combine each set with an And (conjunction):
phi = And(c11, c12, ...) # whole first block
psi = And(c21, c22, ...) # whole second block
Then do the same XOR trick.
────────────────────────────────────────────
5. Quantifiers / prove
If the constraints contain quantifiers you can still do:
prove(Not(Xor(phi, psi))) # succeed ⇒ equivalent
(Internally it performs the same UNSAT check on the XOR.)
────────────────────────────────────────────
6. Alternative: check the two implications
You can also ask the solver for
(1) ¬(ϕ ⇒ ψ) and (2) ¬(ψ ⇒ ϕ)
If both are UNSAT, the two formulas imply each other, therefore are equivalent.
The XOR technique is just the conjunction of these two tests packed into one.
────────────────────────────────────────────
7. Caveats & notes
• “Equivalent” here means “same truth value under every assignment of the currently declared variables.”
It does NOT mean syntactically identical, nor does it prove equivalence for additional, as-yet-undeclared symbols.
• If one formula uses extra variables, remember to declare them in the other (possibly as unconstrained) so they share the same vocabulary.
• For bit-vectors or other non-Booleans, equality of the results is what matters:
Xor( (a + b) == 0, (b + a) == 0 ) — etc.
• simplify sometimes collapses two expressions to the exact same AST, but simplification is heuristic.
The XOR-UNSAT method is principled and complete.
────────────────────────────────────────────
Quick reference code snippet
def equivalent(phi, psi):
s = Solver()
s.add(Xor(phi, psi))
return s.check() == unsat # True <=> equivalent
Enjoy fluent equivalence checking with Z3!