Tutorials

9. Diagnose conflicting requirements

An unsat core is a subset of asserted formulas that is already unsatisfiable. It helps explain a contradiction in the language of the requirements you supplied.

Prerequisite: results and models.

Keep requirements recognizable

This model requires an integer to be at least five and at most three. Neither condition is impossible alone, but they cannot hold together.

unsat_core.mojo Download
"""Identify conflicting requirements without depending on core order."""

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("produce-unsat-cores", "true")
    var x = tm.mkConst(tm.getIntegerSort(), "x")
    var lower = tm.mkTerm(Kind.GEQ, x, tm.mkInteger(5))
    var upper = tm.mkTerm(Kind.LEQ, x, tm.mkInteger(3))
    solver.assertFormula(lower)
    solver.assertFormula(upper)
    var result = solver.checkSat()
    assert_true(result.isUnsat())
    var core = solver.getUnsatCore()
    assert_equal(len(core), 2)
    var has_lower = False
    var has_upper = False
    for formula in core:
        has_lower = has_lower or formula == lower
        has_upper = has_upper or formula == upper
    assert_true(has_lower and has_upper)
    print("requirements:", result)
    print("core contains both conflicting bounds")

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

Output

requirements: unsat
core contains both conflicting bounds

Enable core production before the query and request the core only after an unsat result. The program retains handles to both assertions so that it can identify returned formulas by term equality. This is a use of term identity, not an attempt to prove arithmetic equivalence through ==.

The test verifies membership without depending on the order of the returned formulas. A UI can use the same idea to attach application descriptions, such as “minimum staffing” or “available budget,” to the formulas it asserts.

A core is an explanation, not an automatic repair

Do not assume a returned core is the smallest possible core or the only one. Even a subset-minimal core, from which no member can be removed, need not have minimum cardinality among all cores. Those are different goals.

The core in this example needs both formulas: removing either makes the remaining requirement satisfiable. That conclusion follows directly from the example's mathematics; it is not a general guarantee about every core the API returns.

Deleting one member of one core does not guarantee that a large problem becomes satisfiable. Another independent conflict may remain. Recheck the whole model after a proposed repair. Choosing which requirement to relax is an application decision, such as preferring an optional feature over a mandatory safety condition.

Assertion granularity matters

If you assert every requirement as a separate formula, the core can refer to those individual requirements. If you put the whole model into one large conjunction and assert it once, the explanation may identify only that conjunction. Keep the boundaries useful to the people diagnosing the problem.

Exercise: conflicting intervals

Replace the upper bound with x <= 5. What must change in the program?

Worked answer. The result becomes sat, with the unique value five. The code must stop requesting an unsat core. Enable model production if you want to display the witness instead.

Next: enumerating solutions.

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