Examples

Examples

Every program below is a complete, maintained file in docs/examples/. Each one asserts its own results, and the documentation tests build it from source and from the precompiled package at O0 and O3 with --Werror, run it, and compare its output with the expected output shown on its page. Run any of them from the repository root:

pixi run --locked mojo run -I src docs/examples/<name>.mojo

Foundations and applications

Follow the tutorial learning path for prerequisites and exercises. The early examples assume basic Mojo and introduce SMT from the beginning; the application examples assume the preceding modeling lessons.

Program Task and explanation
quickstart.mojo Rule out a negative real square
terms.mojo Inspect symbolic sorts and operations
booleans.mojo Configure dependent features
integers.mojo Recover ticket counts
reals.mojo Compare integer and real solutions
properties.mojo Prove a property and find a counterexample
incremental.mojo Try candidates in assertion scopes
unsat_core.mojo Identify conflicting bounds
enumeration.mojo Enumerate a finite solution set
coloring.mojo Color a triangle with a fixed budget
uniqueness.mojo Prove uniqueness of a selected tuple
schedule_application.mojo Build, diagnose, and optimize a four-job application
scheduling.mojo Check deadlines and resource conflicts
debugging.mojo Repair domains, assumptions, units, and blocking clauses

Transcendental functions

These examples build on real arithmetic and counterexample queries. Start with the transcendental learning path for the mathematical assumptions and solver limitations.

Program Task and explanation
transcendental_terms.mojo Construct functions, symbolic π, and exact fractions
exponentials.mojo Prove exponential positivity and growth
logarithms.mojo Encode a logarithm and prove an enclosure
trigonometry.mojo Prove sine and cosine range bounds
sine_phase.mojo Combine a phase restriction and a square
inverse_branches.mojo Select a principal inverse branch
hyperbolic.mojo Build sinh and cosh from exponentials
sine_bounds.mojo Check exact rational Taylor bounds
root_bracket.mojo Certify endpoint signs for a unique root
logarithm_base.mojo Check the sign behind a decreasing logarithm
tangent_domain.mojo Exclude a tangent pole
encoding_budgets.mojo Compare equivalent queries at fixed budgets
resource_limits.mojo Handle an inconclusive query
damped_oscillation.mojo Certify a signal envelope over uncertain parameters and time intervals

Runtime and combined workflows

Program Task and explanation
models.mojo Combine options, scopes, model values, and an unsat core
errors.mojo Catch a cvc5 error and continue
lifetimes.mojo Return a result that outlives its solver
migration.mojo, migration.py Compare equivalent Python and Mojo programs
consumer/main.mojo Import the library and a local module outside the checkout

cvc5's Python examples are also translated to Mojo in tests/cvc5/examples/. The upstream tutorial explains five of them, and the coverage report lists them all. The maintained tutorial programs complement those examples with worked reasoning and exercises; see verification. The migration pair also runs in the pinned Python oracle environment against the same output. The consumer example receives an additional working-directory check: every build route runs from an isolated directory with explicit library discovery.

Models and unsat cores

models.mojo Download
"""Incremental solving: a model inside a scope, then an unsat core after it."""

from std.testing import assert_equal, assert_true

from cvc5_mojo import TermManager, Solver, Kind


def main() raises:
    var tm = TermManager()
    var solver = Solver(tm)
    solver.setLogic("QF_LIA")
    solver.setOption("incremental", "true")
    solver.setOption("produce-models", "true")
    solver.setOption("produce-unsat-cores", "true")

    var x = tm.mkConst(tm.getIntegerSort(), "x")
    var positive = tm.mkTerm(Kind.GT, x, tm.mkInteger(0))
    solver.assertFormula(positive)

    # Inside a scope, x < 2 leaves exactly one model.
    solver.push()
    solver.assertFormula(tm.mkTerm(Kind.LT, x, tm.mkInteger(2)))
    var first = solver.checkSat()
    assert_true(first.isSat())
    var value = solver.getValue(x)
    assert_equal(String(value), "1")
    print("x > 0 and x < 2:", first, "with x =", value)
    solver.pop()

    # After the scope, x < 0 contradicts x > 0; the core names both.
    var negative = tm.mkTerm(Kind.LT, x, tm.mkInteger(0))
    solver.assertFormula(negative)
    var second = solver.checkSat()
    assert_true(second.isUnsat())
    var core = solver.getUnsatCore()
    assert_equal(len(core), 2)
    print("x > 0 and x < 0:", second)
    for formula in core:
        print("  core:", formula)

Run from the repository root pixi run --locked mojo run -I src docs/examples/models.mojo

Output

x > 0 and x < 2: sat with x = 1
x > 0 and x < 0: unsat
  core: (> x 0)
  core: (< x 0)
cvc5_mojo v1.4.1.1 for Mojo 1.2 and cvc5 1.4.1 · Apache License 2.0 with LLVM Exceptions · Source on GitHub