Move a Python cvc5 model to Mojo¶
Port the mathematical model first, then adapt construction, ownership, and error handling to the available Mojo API. The bindings preserve cvc5's Python base API names; the generated API covers the supported methods. The reference shows actual Mojo declarations alongside the upstream Python signatures and marks unavailable methods.
This guide solves the same problem in both languages: find the real x
such that 2*x = 1, read its value, temporarily require x < 0, and
restore the original assertions. Both programs assert their results and
are checked against the same expected output using the pinned release.
Construct the same formula¶
Python¶
tm = TermManager()
solver = Solver(tm)
solver.setLogic("QF_LRA")
solver.setOption("produce-models", "true")
solver.setOption("incremental", "true")
x = tm.mkConst(tm.getRealSort(), "x")
one = tm.mkReal(1)
operands = [x, x]
doubled = tm.mkTerm(Kind.ADD, *operands)
solver.assertFormula(tm.mkTerm(Kind.EQUAL, doubled, one))
Mojo¶
var tm = TermManager()
var solver = Solver(tm)
solver.setLogic("QF_LRA")
solver.setOption("produce-models", "true")
solver.setOption("incremental", "true")
var x = tm.mkConst(tm.getRealSort(), "x")
var one = tm.mkReal(1)
var doubled = tm.mkTerm(Kind.ADD, x, x)
solver.assertFormula(tm.mkTerm(Kind.EQUAL, doubled, one))
The declarations express the same real equation. Both use mkReal(1)
to keep the constant exact. For rational values, mkReal also accepts
exact strings and apn_mojo's Rational; see the
constructor reference.
Python's *operands expands a list into positional arguments. The current
Mojo mkTerm declarations accept variadic Term arguments or a List[Term];
the example passes x, x directly. For a dynamic collection, pass the list
as one argument, as in tm.mkTerm(Kind.ADD, operands). Read the Mojo
declaration for each construction you port instead of assuming that Python's
list-unpacking syntax applies unchanged.
Preserve the meaning of a copy¶
Python assignment gives another reference to the same solver. Mojo assignment
also shares the underlying instance; an explicit solver.copy() does too.
Neither creates
an independent assertion stack. Both programs demonstrate that behavior
by pushing through the second reference and checking through the first:
Python shared reference¶
shared = solver
shared.push()
shared.assertFormula(tm.mkTerm(Kind.LT, x, tm.mkInteger(0)))
conflict = solver.checkSat()
assert conflict.isUnsat()
print("shared solver with x < 0:", conflict)
shared.pop()
restored = solver.checkSat()
assert restored.isSat()
print("after pop:", restored)
Mojo shared reference¶
# Copying retains the same solver; it does not clone the assertion stack.
var shared = solver.copy()
shared.push()
shared.assertFormula(tm.mkTerm(Kind.LT, x, tm.mkInteger(0)))
var conflict = solver.checkSat()
assert_true(conflict.isUnsat())
print("shared solver with x < 0:", conflict)
shared.pop()
var restored = solver.checkSat()
assert_true(restored.isSat())
print("after pop:", restored)
With x = 1/2, the temporary negative constraint is contradictory. After
pop(), the original problem is satisfiable again. If two experiments need
independent state, construct two solvers and apply the model to each.
Keep terms associated with their intended term manager.
Mojo wrappers own references and release them automatically. Move a value when transferring ownership; implicit or explicit copies retain another reference. Do not add manual C-level release calls. The ownership guide explains why returned results and terms can remain valid after the local wrapper that produced them is gone.
Translate the surrounding application code¶
| Python pattern in this example | Mojo counterpart | Why it matters |
|---|---|---|
Import from cvc5 |
Import from cvc5_mojo |
Select the binding package |
| Local assignment | var declarations |
Follow Mojo's value and ownership rules |
str(value) |
String(value) or print(value) |
Use the wrapper's Writable implementation |
assert condition |
assert_true(condition) from std.testing |
Keep executable checks in the tutorial program |
Exceptions propagate through main |
def main() raises |
A failed binding call raises a Mojo error |
shared = solver |
var shared = solver or solver.copy() |
Both references share the solver state |
The printed value (/ 1 2) is a symbolic exact rational, not a host
floating-point number. Construct further symbolic constraints from terms;
do not make application logic depend on parsing printed expression text.
The example compares text only because this equation has a known, unique
small result and its output is part of the documentation contract.
A solver status is also different from an exception. Handle sat, unsat,
and unknown explicitly. Catch a Mojo error when a binding operation fails,
for example because the term construction has incompatible sorts; see
error handling.
Run both complete programs¶
After setting up the checkout, the Python
counterpart runs in the repository's oracle environment. That environment
pins the official Python package to the same cvc5 release. The Mojo program
runs in the default environment.
Complete Python program and expected output
"""The Python counterpart of migration.mojo, checked against the same output."""
from cvc5 import Kind, Solver, TermManager
def main():
tm = TermManager()
solver = Solver(tm)
solver.setLogic("QF_LRA")
solver.setOption("produce-models", "true")
solver.setOption("incremental", "true")
x = tm.mkConst(tm.getRealSort(), "x")
one = tm.mkReal(1)
operands = [x, x]
doubled = tm.mkTerm(Kind.ADD, *operands)
solver.assertFormula(tm.mkTerm(Kind.EQUAL, doubled, one))
assert solver.checkSat().isSat()
value = solver.getValue(x)
assert str(value) == "(/ 1 2)"
print("x:", value)
shared = solver
shared.push()
shared.assertFormula(tm.mkTerm(Kind.LT, x, tm.mkInteger(0)))
conflict = solver.checkSat()
assert conflict.isUnsat()
print("shared solver with x < 0:", conflict)
shared.pop()
restored = solver.checkSat()
assert restored.isSat()
print("after pop:", restored)
if __name__ == "__main__":
main()
Run from the repository root pixi run --locked -e oracle python docs/examples/migration.py
Output
x: (/ 1 2)
shared solver with x < 0: unsat
after pop: sat
Complete Mojo program and expected output
"""Mirror the paired Python example using the currently available Mojo API."""
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_LRA")
solver.setOption("produce-models", "true")
solver.setOption("incremental", "true")
var x = tm.mkConst(tm.getRealSort(), "x")
var one = tm.mkReal(1)
var doubled = tm.mkTerm(Kind.ADD, x, x)
solver.assertFormula(tm.mkTerm(Kind.EQUAL, doubled, one))
assert_true(solver.checkSat().isSat())
var value = solver.getValue(x)
assert_equal(String(value), "(/ 1 2)")
print("x:", value)
# Copying retains the same solver; it does not clone the assertion stack.
var shared = solver.copy()
shared.push()
shared.assertFormula(tm.mkTerm(Kind.LT, x, tm.mkInteger(0)))
var conflict = solver.checkSat()
assert_true(conflict.isUnsat())
print("shared solver with x < 0:", conflict)
shared.pop()
var restored = solver.checkSat()
assert_true(restored.isSat())
print("after pop:", restored)
Run from the repository root pixi run --locked mojo run -I src docs/examples/migration.mojo
Output
x: (/ 1 2)
shared solver with x < 0: unsat
after pop: sat
Run the paired checks from the repository root, choosing fresh report directories:
pixi run --locked docs-test --only migration --output .cache/migration-mojo-001
pixi run --locked -e oracle docs-oracle-test --only migration --output .cache/migration-python-001
The Mojo check uses source and precompiled imports at O0 and O3. Both language checks compare their program's output with one manifest entry. This pair establishes the behavior shown here. The upstream tutorial also uses automatically translated cvc5 examples whose outputs are checked against the Python originals.
Port a larger model in small steps¶
Inventory the source program's constructors and solver methods against the reference before translating it. Start with terms and one satisfiability query, then add model reads, scopes, and conflict explanations. Keep small accepted and rejected instances in your application's own tests. If a required method is unavailable, use a documented construction only when it preserves the mathematics, or treat it as a binding gap; matching names alone cannot establish equivalent behavior.
Continue with using the bindings in another project to move the translated model out of this checkout.