The Z3 solver is the main interface for checking satisfiability of formulas. It supports incremental solving, allowing you to add constraints progressively and check satisfiability multiple times.
from z3 import *x, y, z = Ints('x y z')s = Solver()s.add(x > 0) # Level 0s.push() # Level 1s.add(y > x)s.push() # Level 2 s.add(z > y)print(s.check()) # Check at level 2s.pop() # Back to level 1s.pop() # Back to level 0print(s) # Only: x > 0
from z3 import *x, y = Ints('x y')s = Solver()# Set timeout (in milliseconds)s.set("timeout", 5000) # 5 second timeout# Add expensive constraintss.add(x * y == 1000)s.add(x * x + y * y == 500)result = s.check()if result == unknown: print("Timeout or unknown:", s.reason_unknown())
from z3 import *s = Solver()# View available parametersprint(s.param_descrs())# Set parameterss.set("timeout", 10000) # Timeout in mss.set("random_seed", 42) # Random seeds.set("auto_config", False) # Disable auto-configs.set("mbqi", True) # Model-based quantifier instantiation# For specific theory parameterss.set("smt.arith.solver", 2) # Arithmetic solver version
from z3 import *x, y = Ints('x y')s = Solver()s.add(x > 0)s.add(y > x)s.add(x + y < 10)# Get all assertionsassertions = s.assertions()print("Current assertions:")for a in assertions: print(f" {a}")# Number of assertionsprint(f"Total: {len(assertions)} assertions")
from z3 import *A, B, C = Bools('A B C')s = Solver()s.add(Implies(A, B))s.add(Implies(B, C))# What can we conclude if C is false?assumptions = [Not(C)]variables = [A, B, C]consequences = s.consequences(assumptions, variables)print(consequences)# Can conclude: A is false, B is false