PySMT: A Solver-Agnostic Library for SMT Formulae Manipulation and Solving

0.9.6 · active · verified Thu Apr 16

PySMT is a Python library designed for solver-agnostic manipulation and solving of Satisfiability Modulo Theories (SMT) formulae. It provides a unified API to define, manipulate, and solve SMT problems using various SMT-LIB compliant solvers. The library is actively maintained, with regular releases, and its current stable version is 0.9.6.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates defining Boolean SMT variables, constructing a logical formula, checking its satisfiability using a shortcut, performing variable substitution, and using an explicit solver to extract a model.

from pysmt.shortcuts import Symbol, And, Not, is_sat, Solver
from pysmt.typing import BOOL

# Define Boolean symbols
varA = Symbol("A", BOOL)
varB = Symbol("B", BOOL)

# Construct a formula: (A AND (NOT B))
f = And([varA, Not(varB)])

# Check satisfiability using a shortcut
res_f = is_sat(f)
print(f"Formula '{f}' is SAT? {res_f}")

# Perform a substitution: replace B with A in f, resulting in (A AND (NOT A))
g = f.substitute({varB: varA})
res_g = is_sat(g)
print(f"Formula '{g}' is SAT? {res_g}")

# Example with explicit solver and model extraction
with Solver(name="z3", suppress_warnings=True) as solver:
    solver.add_assertion(f)
    if solver.solve():
        print(f"Model for '{f}':")
        for s in [varA, varB]:
            print(f"  {s} = {solver.get_value(s)}")
    else:
        print(f"'{f}' is UNSAT")

view raw JSON →