Z3 Solver

4.16.0.0 · active · verified Sat Apr 11

Z3 is a high-performance theorem prover developed by Microsoft Research, primarily used for Satisfiability Modulo Theories (SMT) problems. It supports various data types including bitvectors, booleans, arrays, floating point numbers, and strings, making it a versatile tool for formal verification, software/hardware testing, and constraint solving. The library is actively maintained with frequent minor releases and is currently on version 4.16.0.0.

Warnings

Install

Imports

Quickstart

This example demonstrates how to create integer variables, add constraints to a solver, check for satisfiability, and retrieve a model if a solution exists.

from z3 import *

# Create integer variables
x = Int('x')
y = Int('y')

# Create a solver instance
s = Solver()

# Add constraints
s.add(x + y == 10)
s.add(x > y)
s.add(y >= 0)

# Check for satisfiability
result = s.check()

if result == sat:
    # If satisfiable, print a model
    model = s.model()
    print(f"Satisfiable! Model: x={model[x]}, y={model[y]}")
elif result == unsat:
    print("Unsatisfiable!")
else:
    print("Unknown (solver could not determine satisfiability).")

view raw JSON →