Metadata-Version: 2.5
Name: minismt
Version: 0.3.1
Summary: DPLL(T) SMT solver for QF_UF in Python: CDCL, congruence closure, SMT-LIB. Educational, zero deps.
Project-URL: Homepage, https://github.com/heyrtl/minismt
Project-URL: Issues, https://github.com/heyrtl/minismt/issues
Author-email: Ratul Rahman <hi@ratul.fi>
License: MIT
License-File: LICENSE
Keywords: cdcl,congruence-closure,dpll,euf,sat,smt,solver
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Mathematics
Requires-Python: >=3.10
Provides-Extra: dev
Requires-Dist: hypothesis>=6; extra == 'dev'
Requires-Dist: mypy; extra == 'dev'
Requires-Dist: pytest>=7; extra == 'dev'
Requires-Dist: ruff; extra == 'dev'
Requires-Dist: z3-solver>=4.12; extra == 'dev'
Description-Content-Type: text/markdown

# minismt

> DPLL(T) SMT solver for QF_UF in Python: CDCL, congruence closure, SMT-LIB. Educational, zero deps.

`minismt` is an educational SMT solver. It implements the **DPLL(T)** architecture from scratch:

- a **CDCL SAT solver** with two-watched literals, 1UIP clause learning, VSIDS, phase saving, and non-chronological backjumping;
- a **congruence closure** theory solver with explanation tracking;
- a **DPLL(T)** driver that feeds theory conflicts back into the SAT engine as learned clauses;
- a small **SMT-LIB v2** parser for the QF_UF subset.

Zero runtime dependencies. Pure Python 3.10+.

## What it is

- A readable reference implementation of DPLL(T) + congruence closure, split across `sat.py`, `cc.py`, `dpllt.py`, `parser.py`.
- A working QF_UF solver that handles transitivity, symmetry, congruence, negation, and propositional combinations (`and`, `or`, `=>`, `not`, `distinct`).
- A starting point for adding more theories (LRA, bit-vectors, arrays).
- **Differentially tested against Z3** on 2000+ random QF_UF programs per deep run; model validation is on by default in `check()`.

## What it is **not**

- **Not** a drop-in replacement for Z3 or cvc5. Not even close on performance.
- **Not** production software. No proofs, no unsat cores, no incremental solving.
- **Not** covering the full SMT-LIB standard. See [Limitations](#limitations).

## Install

```bash
pip install minismt
```

From source:

```bash
git clone https://github.com/heyrtl/minismt.git
cd minismt
python -m pip install -e ".[dev]"
```

## Usage

### CLI

```bash
$ minismt tests/examples/congruence.smt2
unsat
```

### As a library

Two entry points.

**`run(text)`** — execute an SMT-LIB 2 string:

```python
from minismt import run

result = run("""
(set-logic QF_UF)
(declare-fun x () U)
(declare-fun y () U)
(declare-fun f (U) U)
(assert (= x y))
(assert (not (= (f x) (f y))))
(check-sat)
""")
print(result)   # "unsat"
```

**`check(*assertions)`** — programmatic API returning a `Result`:

```python
from minismt import check

r = check("(= x y)", "(not (= y x))", declarations=("x", "y"))
print(r.sat)    # False
print(r.model)  # None

r = check("(= x y)", declarations=("x", "y"))
print(r.sat)    # True
print(r.model)  # {'x': 'x', 'y': 'x'}
```

`check()` validates the returned model against every assertion by default. If the solver ever returns an unsound `sat`, it raises `UnsoundSatError` instead of returning a wrong answer.

### Getting a model

```python
result = run("""
(set-logic QF_UF)
(declare-fun x () U)(declare-fun y () U)(declare-fun z () U)
(assert (= x y))(assert (not (= x z)))
(check-sat)(get-model)
""")
print(result)
# sat
# (
#   (define-fun x () U x)
#   (define-fun y () U x)
#   (define-fun z () U z)
# )
```

`get-model` prints one `define-fun` per variable, mapping each to its equivalence-class representative.

### Testing

```bash
pytest                                            # 29 tests, ~15s
pytest --hypothesis-profile=deep                  # + 2000 fuzz cases, ~3min
pytest --hypothesis-profile=soak                  # + 20k fuzz cases, ~30min
```

The fuzzer generates random QF_UF programs, runs them through both `minismt` and Z3, and fails on any disagreement. It also validates every `sat` model. Three soundness bugs in v0.2.0 were found this way — see the git log for `v0.3.0`.

## Supported SMT-LIB subset

| Construct | Status |
|---|---|
| `set-logic`, `set-info`, `declare-sort` | parsed, ignored (no sort checking) |
| `declare-fun`, `declare-const` | parsed, tracked for `get-model` |
| `assert` | supported |
| `check-sat` | supported |
| `get-model` | supported |
| `exit` | supported |
| `=`, `not`, `and`, `or`, `=>`, `distinct` | supported |
| Function application `(f x y)` | supported |
| `let`, `ite`, `forall`, `exists` | **not** supported |
| `push`, `pop`, `get-unsat-core` | **not** supported |

## How it works

The pipeline is the classic DPLL(T) loop:

```
SMT-LIB  ──parser──▶  AST  ──Tseitin──▶  CNF  ──CDCL──▶  Boolean model
                                                             │
                                                             ▼
                                                    ┌────────────────┐
                                                    │ Congruence     │
                                                    │ closure (EUF)  │
                                                    └────────────────┘
                                                             │
                                              conflict ◀─────┴─────▶ ok
                                                 │                   │
                                        learn + backjump       next decision
```

1. **Encode.** Each theory atom `(= a b)` becomes a SAT variable. Boolean connectives are encoded into CNF with Tseitin-style clauses.
2. **Solve.** The CDCL solver runs as normal.
3. **Check.** After propagation settles, the congruence-closure solver processes all currently-true equalities (union) and all currently-false equalities (disequalities). It also propagates congruence: if `a ≡ b` then `f(a) ≡ f(b)` for every `f`.
4. **Explain.** Every merge in the union-find carries the set of SAT literals that caused it. If a disequality is violated, that reason set plus the disequality itself forms a conflict clause.
5. **Learn.** The conflict clause is fed to `SAT.analyze()` exactly like a SAT conflict. New clause learned, backjump, continue.

For the full breakdown, read the docstrings in `src/minismt/` — `sat.py` (CDCL), `cc.py` (congruence closure), `dpllt.py` (driver), `parser.py` (SMT-LIB).

## Limitations

- **QF_UF only.** No arithmetic, arrays, or bit-vectors.
- **No incremental solving.** `(check-sat)(get-model)` is supported, but calling `(check-sat)` twice in one file re-runs the search and is not guaranteed correct after new assertions.
- **`theory_check()` is non-incremental.** It rebuilds the congruence-closure state on every call — correct but O(n²) per check.
- **`CC.process()` is O(n³) worst case** on terms with large equivalence classes.
- **No proofs or unsat cores.**
- **Parser is minimal.** No `let`, no `ite`, no nested `distinct`, no SMT-LIB round-tripping.

## Roadmap

- [x] **v0.2** — module split + `(get-model)` printing equivalence classes
- [x] **v0.3** — differential fuzzing vs Z3, model validation, `check()` API
- [ ] **v0.4** — incremental congruence closure (push/pop on the trail) + multiple `(check-sat)`
- [ ] **v0.5** — `let`, `ite` in the parser
- [ ] **v0.6** — `(get-unsat-core)`
- [ ] **v0.7** — Linear Real Arithmetic via Simplex-for-DPLL(T)
- [ ] **v0.8** — Bit-vectors via bit-blasting
- [ ] **v1.0** — pass a meaningful subset of the SMT-LIB QF_UF benchmark suite

## References

- Kroening & Strichman, [*Decision Procedures: An Algorithmic Point of View*](https://www.decision-procedures.org/) ([PDF](https://vowi.fsinf.at/images/d/d8/TU_Wien-Formale_Methoden_der_Informatik_VU_%28Egly%29_-_Book_-_Decision_Procedures_An_Algorithmic_Point_of_View_-_Daniel_Kroening.pdf)) — the standard textbook.
- Nieuwenhuis, Oliveras, Tinelli, [*Solving SAT and SAT Modulo Theories*](http://cse.unl.edu/~choueiry/Documents/SAT+SMT-nieuwenhuis.pdf) (2006) — the DPLL(T) paper.
- Nelson & Oppen, [*Fast Decision Procedures Based on Congruence Closure*](https://dl.acm.org/doi/pdf/10.1145/322186.322198) (1980).
- The "[SAT / SMT 學習筆記](https://zhuanlan.zhihu.com/p/151725312)" tutorial series — the best code-level walkthrough.

## License

MIT. See [LICENSE](LICENSE).