Guides

Low-level C API (z3_mojo.c)

z3_mojo keeps Z3's full C API accessible alongside the Pythonic API. Import z3_mojo.c whenever you want direct 1-to-1 access to all 817 Z3_* functions, raw Z3_* handle types, manual Z3_mk_context_rc AST reference counting, or raw C callbacks.

from z3_mojo.c import (
    c_api,
    c_bytes,
    to_string,
    null,
    Lbool,
    Z3_context,
    Z3_ast,
)

Every Z3_* function lives on the cached CApi table returned by c_api(). The shared library libz3 is loaded once per process, and every call taking a Z3_context automatically checks Z3_get_error_code(ctx) afterwards and raises a Mojo Error("z3: ...") if Z3 reports an error (except destructors and the error-query functions themselves).

When to choose z3_mojo.c

Need Recommended layer
Day-to-day SMT solving, operators, Pythonic idioms, exact apn_mojo models Pythonic API (from z3_mojo import ...)
Safe RAII handles with 1-to-1 Z3 method names (ctx.mk_*, solver.*) Safe handle API (from z3_mojo import ...)
Custom theory propagators, OnClause, Optimize.set_on_model Safe callback traits (UserPropagator, ClauseHandler, ModelHandler)
Manual AST refcounting (Z3_mk_context_rc, Z3_inc_ref, Z3_dec_ref) Low-level C API (from z3_mojo.c import ...)
Raw C proof-check or fixedpoint callbacks (Z3_set_proof_check_callback, Z3_fixedpoint_add_callback) Low-level C API (from z3_mojo.c import ...)
Porting C/C++ Z3 code line-by-line Low-level C API (from z3_mojo.c import ...)

Lifecycle and error handling

When creating a context directly through z3_mojo.c:

  1. Create a Z3_config with Z3_mk_config(), pass it to Z3_mk_context(cfg) or Z3_mk_context_rc(cfg), and delete the config with Z3_del_config(cfg).
  2. Disable Z3's default aborting error handler immediately: api[].Z3_set_error_handler(ctx, null[NoneType]()). Without this call, Z3 exits the process on invalid API arguments instead of letting CApi raise a Mojo Error.
  3. Release every handle you create:
  4. Ref-counted handles (Z3_solver, Z3_model, Z3_goal, Z3_tactic, Z3_optimize, Z3_ast_vector, etc.) must be paired with Z3_<handle>_inc_ref and Z3_<handle>_dec_ref.
  5. Under Z3_mk_context_rc, every Z3_ast (and Z3_sort / Z3_func_decl cast via Z3_sort_to_ast / Z3_func_decl_to_ast) requires explicit Z3_inc_ref and Z3_dec_ref calls before Z3_del_context(ctx).
  6. Delete the context last with api[].Z3_del_context(ctx).

Example: Z3_mk_context_rc with manual refcounting

The runnable example in docs/examples/c_api_low_level.mojo solves x > 40 using only z3_mojo.c:

from z3_mojo.c import c_api, c_bytes, to_string, null, Lbool

var api = c_api()
var cfg = api[].Z3_mk_config()
var ctx = api[].Z3_mk_context_rc(cfg)
api[].Z3_del_config(cfg)
api[].Z3_set_error_handler(ctx, null[NoneType]())

var int_sort = api[].Z3_mk_int_sort(ctx)
var int_sort_ast = api[].Z3_sort_to_ast(ctx, int_sort)
api[].Z3_inc_ref(ctx, int_sort_ast)

var x_name = c_bytes("x")
var x_sym = api[].Z3_mk_string_symbol(ctx, x_name.unsafe_ptr())
var x = api[].Z3_mk_const(ctx, x_sym, int_sort)
api[].Z3_inc_ref(ctx, x)

var forty = api[].Z3_mk_int(ctx, 40, int_sort)
api[].Z3_inc_ref(ctx, forty)
var gt = api[].Z3_mk_gt(ctx, x, forty)
api[].Z3_inc_ref(ctx, gt)

var solver = api[].Z3_mk_solver(ctx)
api[].Z3_solver_inc_ref(ctx, solver)
api[].Z3_solver_assert(ctx, solver, gt)
var res = api[].Z3_solver_check(ctx, solver)
if res == Lbool.TRUE:
    var model = api[].Z3_solver_get_model(ctx, solver)
    api[].Z3_model_inc_ref(ctx, model)
    print(to_string(api[].Z3_model_to_string(ctx, model)))
    api[].Z3_model_dec_ref(ctx, model)

api[].Z3_solver_dec_ref(ctx, solver)
api[].Z3_dec_ref(ctx, gt)
api[].Z3_dec_ref(ctx, forty)
api[].Z3_dec_ref(ctx, x)
api[].Z3_dec_ref(ctx, int_sort_ast)
api[].Z3_del_context(ctx)

FFI helper functions

z3_mojo.c re-exports the helpers used internally by the generated layer:

  • c_bytes(s: String) raises -> List[Int8]: NUL-terminated byte buffer for const char* parameters. Rejects strings containing embedded NUL bytes. Keep the List[Int8] variable alive across the C call when passing .unsafe_ptr().
  • to_string(p: CChars) raises -> String: immediately copies a Z3-returned const char* into an owned Mojo String. Z3 reuses internal string buffers on subsequent calls on the same context, so always copy before making another Z3 call.
  • null[T](): typed NULL pointer for optional C parameters and disabling callbacks.
  • to_c_uint32(v: Int) and to_c_int32(v: Int): range-checked integer conversions for C unsigned and int parameters.

Raw C callbacks

For the five raw-only callback registration functions (Z3_set_proof_check_callback, Z3_fixedpoint_init, Z3_fixedpoint_set_reduce_assign_callback, Z3_fixedpoint_set_reduce_app_callback, Z3_fixedpoint_add_callback), pass a def (...) abi("C") trampoline function pointer and a heap-pinned void* state pointer. Never allow a Mojo Error to unwind across a C callback boundary; catch exceptions inside the trampoline and check them after the Z3 call returns.

For the full catalog of all 817 functions and signatures, see the Low-Level C API Reference and C API Coverage Report.

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