Guides

Debugging a solver model

A solver answers the formula you submitted. If that formula omits a domain, uses the wrong units, or rules out every input, a correct solver result can still answer the wrong application question. This workshop diagnoses four such mistakes with small, executable examples.

Read checking properties and enumerating solutions first. Each case below compares an incorrect encoding with a corrected one; the complete program asserts every expected result.

Recover the domain after rewriting a logarithm

Suppose your application accepts real inputs satisfying ln(x) < 0. Monotonicity gives an algebraic condition that avoids constructing a logarithm, but positivity is part of that condition:

\ln x<0 \quad\Longleftrightarrow\quad 0<x<1.

Replacing the original requirement with only x < 1 admits x = -1, where the ordinary real logarithm is undefined. The first query fixes the input to that negative value and demonstrates the mistake. The second adds the missing lower bound:

debugging.mojo Download
# ln(x) < 0 means 0 < x < 1. Try the invalid candidate x = -1.
var candidate = tm.mkTerm(Kind.EQUAL, x, tm.mkTerm(Kind.NEG, one))
var incomplete = tm.mkTerm(Kind.AND, candidate, tm.mkTerm(Kind.LT, x, one))
check(tm, incomplete, "missing logarithm domain", True)
var corrected = tm.mkTerm(Kind.AND, incomplete, tm.mkTerm(Kind.GT, x, zero))
check(tm, corrected, "positive logarithm domain", False)

The outputs are sat and unsat, respectively. Neither query evaluates the logarithm at a negative input: they test whether the proposed rewrite admits an invalid input. This distinction helps isolate a modeling error from the cost of a transcendental query.

When removing a function from an encoding, carry its domain into the new formula. The same check applies to a denominator that must be nonzero and to the branch interval of an inverse trigonometric function. See logarithmic relations and inverse branches.

Check whether a successful proof is vacuous

To prove a property P under assumptions A, you ask whether A AND NOT P is satisfiable. If A is contradictory, that query is unsatisfiable for every property, including properties that fail on all intended inputs.

The workshop accidentally assumes both x > 0 and x < 0. It then tries to prove x > 10 by searching for x <= 10:

debugging.mojo Download
var impossible = tm.mkTerm(Kind.AND,
    tm.mkTerm(Kind.GT, x, zero), tm.mkTerm(Kind.LT, x, zero))
# The false universal claim x > 10 would appear to be proved here.
var violation = tm.mkTerm(Kind.LEQ, x, tm.mkInteger(10))
check(tm, tm.mkTerm(Kind.AND, impossible, violation), "vacuous property check", False)
check(tm, impossible, "check the assumptions alone", False)
var feasible = tm.mkTerm(Kind.GEQ, x, zero)
check(tm, feasible, "corrected assumptions", True)
check(tm, tm.mkTerm(Kind.AND, feasible, violation), "actual counterexample exists", True)

The property query returns unsat, and so does the separate check of the assumptions. The latter result exposes the empty domain. With the intended assumption x >= 0, the domain is satisfiable and a counterexample to x > 10 exists; zero is one mathematical witness.

Use two checks when establishing a property over a domain that should be nonempty:

Check Useful outcome What it establishes
A sat At least one allowed input exists
A AND NOT P unsat No allowed input violates the property

An unknown result in either check leaves that part of the argument open. For larger contradictory assumption sets, attach application labels and inspect an unsat core. A small core identifies a conflict; it does not decide which requirement the application should change.

Convert degrees at the input boundary

An application permits angles in the closed first quadrant and receives the number 90 from a user interface measured in degrees. Comparing that number directly with π/2 treats it as ninety radians and rejects it. Convert the input before using it in trigonometric expressions:

\theta_{\mathrm{rad}}=\theta_{\mathrm{deg}}\,\frac{\pi}{180}.
debugging.mojo Download
var pi = tm.mkTerm(Kind.PI)
var two = tm.mkTerm(Kind.TO_REAL, tm.mkInteger(2))
var quarter_turn = tm.mkTerm(Kind.DIVISION, pi, two)
var degrees = tm.mkTerm(Kind.TO_REAL, tm.mkInteger(90))
var half_turn_degrees = tm.mkTerm(Kind.TO_REAL, tm.mkInteger(180))
# The application permits angles in the closed first quadrant.
check(tm, tm.mkTerm(Kind.LEQ, degrees, quarter_turn), "90 treated as radians", False)
var radians = tm.mkTerm(Kind.DIVISION,
    tm.mkTerm(Kind.MULT, degrees, pi), half_turn_degrees)
check(tm, tm.mkTerm(Kind.LEQ, radians, quarter_turn), "90 degrees converted to radians", True)

The test uses a fixed nonnegative input, so only the upper quadrant bound is needed to distinguish the two encodings. For a symbolic input, include both bounds. The corrected ninety-degree input lands exactly on the allowed endpoint; there is no decimal approximation to π.

Name quantities by their units when constructing a larger model. A dimensionless real sort cannot tell degrees from radians, seconds from milliseconds, or ordinary frequency from angular frequency. For example, converting cycles per second into a sine argument introduces a factor of 2π; the damped signal application instead takes angular frequency directly in radians per second.

Exclude one tuple without excluding its neighbors

After finding (x,y) = (0,0), you want another pair. Requiring both x != 0 and y != 0 throws away pairs such as (0,1), even though they are different solutions. Negating equality of the whole tuple gives:

\neg(x=0\land y=0) \quad\Longleftrightarrow\quad x\ne0\lor y\ne0.
debugging.mojo Download
var y = tm.mkConst(tm.getRealSort(), "y")
# Old tuple: (0,0). New valid tuple: (0,1).
var next_pair = tm.mkTerm(Kind.AND,
    tm.mkTerm(Kind.EQUAL, x, zero), tm.mkTerm(Kind.EQUAL, y, one))
var changed_x = tm.mkTerm(Kind.DISTINCT, x, zero)
var changed_y = tm.mkTerm(Kind.DISTINCT, y, zero)
check(tm, tm.mkTerm(Kind.AND, next_pair,
    tm.mkTerm(Kind.AND, changed_x, changed_y)), "require both coordinates to change", False)
check(tm, tm.mkTerm(Kind.AND, next_pair,
    tm.mkTerm(Kind.OR, changed_x, changed_y)), "require at least one coordinate to change", True)

The example fixes the candidate to (0,1). The incorrect conjunction rejects it; the disjunction preserves it. Select the tuple according to what counts as a different application result. Blocking auxiliary variables as well can enumerate multiple internal representations of the same result.

A debugging routine you can reuse

First write down one input that should be accepted and one that should be rejected. Fix the corresponding symbols to those values and check each case independently. Then check the assumptions without the property or objective, inspect the printed terms for wrong sorts and units, and add requirements back in groups until the result changes. Use temporary scopes when experimenting so a diagnostic constraint cannot leak into the next query.

An inconclusive query needs a separate diagnosis: keep unknown distinct from unsat, record the logic and resource settings, and reduce the formula. Solver options and transcendental solver limits explain those controls.

Complete workshop program and expected output
debugging.mojo Download
"""Expose four modeling mistakes with paired failing and corrected queries."""

from std.testing import assert_true
from cvc5_mojo import TermManager, Solver, Term, Kind


def check(tm: TermManager, formula: Term, label: String, expect_sat: Bool) raises:
    var solver = Solver(tm)
    solver.setLogic("QF_NRAT")
    solver.setOption("tlimit-per", "1000")
    solver.setOption("rlimit-per", "10000")
    solver.assertFormula(formula)
    var result = solver.checkSat()
    if expect_sat:
        assert_true(result.isSat())
    else:
        assert_true(result.isUnsat())
    print(label, ":", result)


def main() raises:
    var tm = TermManager()
    var zero = tm.mkTerm(Kind.TO_REAL, tm.mkInteger(0))
    var one = tm.mkTerm(Kind.TO_REAL, tm.mkInteger(1))
    var x = tm.mkConst(tm.getRealSort(), "x")
    # ln(x) < 0 means 0 < x < 1. Try the invalid candidate x = -1.
    var candidate = tm.mkTerm(Kind.EQUAL, x, tm.mkTerm(Kind.NEG, one))
    var incomplete = tm.mkTerm(Kind.AND, candidate, tm.mkTerm(Kind.LT, x, one))
    check(tm, incomplete, "missing logarithm domain", True)
    var corrected = tm.mkTerm(Kind.AND, incomplete, tm.mkTerm(Kind.GT, x, zero))
    check(tm, corrected, "positive logarithm domain", False)

    var impossible = tm.mkTerm(Kind.AND,
        tm.mkTerm(Kind.GT, x, zero), tm.mkTerm(Kind.LT, x, zero))
    # The false universal claim x > 10 would appear to be proved here.
    var violation = tm.mkTerm(Kind.LEQ, x, tm.mkInteger(10))
    check(tm, tm.mkTerm(Kind.AND, impossible, violation), "vacuous property check", False)
    check(tm, impossible, "check the assumptions alone", False)
    var feasible = tm.mkTerm(Kind.GEQ, x, zero)
    check(tm, feasible, "corrected assumptions", True)
    check(tm, tm.mkTerm(Kind.AND, feasible, violation), "actual counterexample exists", True)

    var pi = tm.mkTerm(Kind.PI)
    var two = tm.mkTerm(Kind.TO_REAL, tm.mkInteger(2))
    var quarter_turn = tm.mkTerm(Kind.DIVISION, pi, two)
    var degrees = tm.mkTerm(Kind.TO_REAL, tm.mkInteger(90))
    var half_turn_degrees = tm.mkTerm(Kind.TO_REAL, tm.mkInteger(180))
    # The application permits angles in the closed first quadrant.
    check(tm, tm.mkTerm(Kind.LEQ, degrees, quarter_turn), "90 treated as radians", False)
    var radians = tm.mkTerm(Kind.DIVISION,
        tm.mkTerm(Kind.MULT, degrees, pi), half_turn_degrees)
    check(tm, tm.mkTerm(Kind.LEQ, radians, quarter_turn), "90 degrees converted to radians", True)

    var y = tm.mkConst(tm.getRealSort(), "y")
    # Old tuple: (0,0). New valid tuple: (0,1).
    var next_pair = tm.mkTerm(Kind.AND,
        tm.mkTerm(Kind.EQUAL, x, zero), tm.mkTerm(Kind.EQUAL, y, one))
    var changed_x = tm.mkTerm(Kind.DISTINCT, x, zero)
    var changed_y = tm.mkTerm(Kind.DISTINCT, y, zero)
    check(tm, tm.mkTerm(Kind.AND, next_pair,
        tm.mkTerm(Kind.AND, changed_x, changed_y)), "require both coordinates to change", False)
    check(tm, tm.mkTerm(Kind.AND, next_pair,
        tm.mkTerm(Kind.OR, changed_x, changed_y)), "require at least one coordinate to change", True)

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

Output

missing logarithm domain : sat
positive logarithm domain : unsat
vacuous property check : unsat
check the assumptions alone : unsat
corrected assumptions : sat
actual counterexample exists : sat
90 treated as radians : unsat
90 degrees converted to radians : sat
require both coordinates to change : unsat
require at least one coordinate to change : sat

Exercise: repair a uniqueness check

A program finds (a,b,c) = (1,2,3) and searches for another solution by asserting a != 1 AND b != 2 AND c != 3. It gets unsat and announces uniqueness. What should change?

Worked answer. Join the three inequalities with OR, preserving the original constraints. The corrected query excludes exactly the known tuple. unsat then proves uniqueness of those three selected values; sat supplies another tuple, and unknown leaves uniqueness undecided.

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