Tutorials

7. Check a property through counterexamples

To establish that a property holds under some assumptions, ask whether the assumptions and the property's negation can hold together. An unsat answer proves that no counterexample exists within the modeled domain.

Prerequisite: results and models. This example also uses assertion scopes, explained fully in incremental solving.

Negate the conclusion

For real x, consider “if x > 0, then x*x > 0.” The counterexample conditions are x > 0 and x*x <= 0. Notice that negating a strict inequality makes it non-strict. Looking only for x*x < 0 would miss a possible zero-valued counterexample.

properties.mojo Download
"""Prove a property, then find a counterexample to a stronger claim."""

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_NRA")
    solver.setOption("incremental", "true")
    solver.setOption("produce-models", "true")
    var x = tm.mkConst(tm.getRealSort(), "x")
    var zero = tm.mkInteger(0)
    var square = tm.mkTerm(Kind.MULT, x, x)
    # Search for a counterexample to: x > 0 implies x*x > 0.
    solver.push()
    solver.assertFormula(tm.mkTerm(Kind.GT, x, zero))
    solver.assertFormula(tm.mkTerm(Kind.LEQ, square, zero))
    var proof = solver.checkSat()
    assert_true(proof.isUnsat())
    print("positive input, nonpositive square:", proof)
    solver.pop()

    # Allowing x = 0 makes that conclusion false.
    solver.assertFormula(tm.mkTerm(Kind.GEQ, x, zero))
    solver.assertFormula(tm.mkTerm(Kind.LEQ, square, zero))
    var counterexample = solver.checkSat()
    assert_true(counterexample.isSat())
    var value = solver.getValue(x)
    assert_equal(String(value), "0.0")
    print("nonnegative input, nonpositive square:", counterexample, "at", value)

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

Output

positive input, nonpositive square: unsat
nonnegative input, nonpositive square: sat at 0.0

The first query is unsatisfiable. The second changes the assumption to x >= 0, which admits the counterexample x = 0. The program reads that value only after checking the satisfiable status.

Check the assumptions too

An implication holds vacuously when its assumptions cannot be satisfied. For example, from x > 1 and x < 0, any conclusion follows because there is no permitted x. When assumptions come from a larger model, check their satisfiability separately if you need to know the property is useful for some real input.

The solver proves what you encoded. A missing precondition, an unintended integer sort, or an overly restrictive interval can change the claim. A counterexample is especially useful here: translate it back into the application's vocabulary and verify it independently.

Equality of expressions

To check an identity between arithmetic terms, assert their disequality together with the intended domain conditions. This asks for an input where the expressions differ. It avoids confusing Mojo term equality with a mathematical equivalence test.

This technique extends to transcendental bounds, but termination is no longer something to expect from every formulation. See exponential properties for examples and inconclusive results for handling the limits honestly.

Exercise: a precondition for reciprocal reasoning

What assumption is needed before interpreting x*(1/x) = 1 as the usual real-number identity?

Worked answer. Require x != 0. The ordinary reciprocal is undefined at zero, so excluding zero is part of the mathematical specification. Do not use a solver's treatment of out-of-domain operations to silently choose the application's behavior there.

Next: incremental solving.

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