Get started

Installation

z3_mojo targets Mojo 1.2.0, Z3 5.3.3, and APN Mojo 1.0.2 on:

  • Linux x86-64 (linux-64)
  • Linux ARM64 (linux-aarch64)
  • macOS Apple Silicon (osx-arm64, macOS 15+)

Install with Pixi

Once published to the modular-community channel:

pixi init my-solver-project \
  --platform linux-64 --platform linux-aarch64 --platform osx-arm64 \
  -c https://conda.modular.com/max \
  -c https://repo.prefix.dev/modular-community \
  -c conda-forge
cd my-solver-project
pixi add "mojo==1.2.0" "z3_mojo==5.3.3.0"

The package places the precompiled module at ${CONDA_PREFIX}/lib/mojo/z3_mojo.mojoc (so from z3_mojo import ... works without -I), bundles the matching libz3 under ${CONDA_PREFIX}/lib/z3_mojo/lib/, and pulls in apn_mojo==1.0.2.

Work from a checkout

git clone https://github.com/Spellbound-Mojo/z3_mojo.git
cd z3_mojo
pixi install --all
pixi run fetch
pixi run test
pixi run docs-test

pixi run fetch downloads the pinned Z3 release archives listed in z3-version.toml, checks their SHA-256 hashes, and unpacks them into third_party/.

Where libz3 is found

On the first call into Z3 (usually Context()), z3_mojo looks for libz3.so (or libz3.dylib on macOS) in this order:

  1. $Z3_MOJO_LIBRARY, if set. If it points to a missing file, loading fails immediately rather than falling back silently.
  2. $CONDA_PREFIX/lib/z3_mojo/lib/libz3.{so,dylib} (the installed package layout, kept separate from any conda-forge libz3).
  3. third_party/z3/bin/libz3.{so,dylib} (a local checkout after pixi run fetch).
  4. $CONDA_PREFIX/lib/libz3.{so,dylib} (a fallback for environments that install libz3 from conda-forge).

The library is opened once per process, and all C symbols are resolved into a single cached CApi table. Creating additional Context or Solver objects does not call dlopen or dlsym again.

Arbitrary-precision numbers (apn_mojo)

Z3 handles integers and rationals of arbitrary size. z3_mojo converts them directly to and from apn_mojo:

  • ctx.mk_integer(val: Integer) and ctx.mk_real(val: Rational) build exact numerals of any width. When a numerator and denominator fit in Int64, mk_real uses Z3's fast 64-bit constructor; larger values go through Z3_mk_numeral.
  • ast.as_integer(), ast.as_rational(), model.get_integer(ast), and model.get_rational(ast) return Optional[Integer] and Optional[Rational] without rounding to floating point.
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