Z3 Mojo documentation

Z3 Mojo

Z3 Mojo lets Mojo programs call the Z3 SMT solver directly in process through libz3. You build expressions on a Context, assert them on a Solver, Optimize, or Fixedpoint instance, and query satisfiability, exact models, unsat cores, tactics, or real-closed-field numbers.

The bindings are generated from a single pinned Z3 release (currently Z3 5.3.3) and checked against the C headers, all three target platform libraries, and the official Python z3-solver package.

First program

After setting up a checkout, run docs/examples/quickstart.mojo from the repository root. It checks whether the square of a real number can be negative:

from std.testing import assert_equal
from z3_mojo import Context, Real, Solver, unsat

def main() raises:
    var ctx = Context()
    var solver = Solver.from_tactic(ctx, "qfnra-nlsat")
    var x = Real("x", ctx)
    var neg_sq = x * x < 0
    solver.add(neg_sq)
    var res = solver.check()
    assert_equal(res, unsat)
    print(neg_sq, "is", res)

A Context owns sorts, symbols, and expressions. A Solver holds assertions and checks them. Every value keeps its Context alive until Mojo destroys it, and any API error Z3 reports raises a Mojo Error.

Two API layers

Layer Import Coverage
Pythonic API from z3_mojo import ... Familiar Pythonic Z3 classes, method names (add, check() == sat, model(), solutions(), unsat_core(), to_smt2(), set(...), evaluate, project, t.solver(), g.as_expr()), Ast/Probe/RcfNum operators, safe callback traits (UserPropagator, ClauseHandler/OnClause, ModelHandler/Optimize.set_on_model), and 360+ z3.z3 functions.
Safe C-API wrappers from z3_mojo import ... All 769 safe Context and handle methods covering all 29 Z3 handle types (618 generated, 151 hand-written) and all 10 enums (370 entries). See the coverage report.
Low-level C API from z3_mojo.c import c_api, ... All 817 exported Z3_* C functions across Z3's nine headers, verified with cc -Werror, plus raw Z3_* handle types (including raw C callbacks and Z3_mk_context_rc).
Exact numbers apn_mojo + RcfNum Arbitrary-precision apn_mojo.Integer and apn_mojo.Rational, plus RcfNum for algebraic roots, pi, e, and infinitesimals.
Platforms Pixi / conda Linux x86-64, Linux ARM64, and macOS Apple Silicon (Mojo 1.2.0, Z3 5.3.3).

Find your starting point

I want to... Read
Install z3_mojo or work from a checkout Installation
Learn z3_mojo step by step (5 runnable parts) Tutorials
Understand lifetimes, callback safety, threads, and errors Ownership and errors
Solve nonlinear reals, quantified formulas, optimization, FPA, or strings User guide
Write a custom UserPropagator, OnClause, or Optimize.set_on_model callback User propagators and callbacks
Call Z3_* C functions directly (Z3_mk_context_rc, raw handles) Low-level C API guide
Port Python z3-solver code to Mojo Python to Mojo migration
Run complete Mojo examples (both Pythonic and C API) Example gallery
Look up Pythonic helpers, operators, traits, and class methods Pythonic API
Look up safe handle structs, Context.mk_* methods, or enums Safe Handle & Context API and Enums
Look up any of the 817 raw Z3_* C functions in z3_mojo.c Low-Level C API and C API coverage
Understand the generator or bump Z3 Architecture and Upgrading Z3
z3_mojo v5.3.3.0 for Mojo 1.2 and Z3 5.3.3 · Apache License 2.0 with LLVM Exceptions · Source on GitHub