Tutorials

3. Boolean constraints

Suppose a configuration has two features: storage and encryption. Encryption requires storage. A user selects encryption, and you want to find a valid configuration.

Prerequisite: sorts and symbolic terms.

Translate the requirement

Use one Boolean symbol for each feature. The dependency is the implication encryption ⇒ storage. Assert encryption separately to represent the user's choice. These two conditions force storage to be enabled.

booleans.mojo Download
"""Choose a valid feature configuration with a dependency."""

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_UF")
    solver.setOption("produce-models", "true")
    var storage = tm.mkConst(tm.getBooleanSort(), "storage")
    var encryption = tm.mkConst(tm.getBooleanSort(), "encryption")
    solver.assertFormula(tm.mkTerm(Kind.IMPLIES, encryption, storage))
    solver.assertFormula(encryption)
    var result = solver.checkSat()
    assert_true(result.isSat())
    var chosen = solver.getValue(storage)
    assert_equal(String(chosen), "true")
    print("configuration:", result)
    print("storage required:", chosen)

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

Output

configuration: sat
storage required: true

The program enables model production before the query. After obtaining sat, it asks for the value of storage in the returned model. The printed true is justified by both assertions together.

An implication expresses a one-way dependency. It allows storage without encryption. If the product requires both features to be enabled or disabled together, use Boolean equality instead. If exactly one feature must be enabled, use exclusive-or.

Combine requirements

These operator descriptions come from the pinned release:

Kind Meaning in the pinned release
Kind.AND Logical conjunction.
Kind.OR Logical disjunction.
Kind.NOT Logical negation.
Kind.IMPLIES Logical implication.
Kind.XOR Logical exclusive disjunction, left associative.
Kind.EQUAL Equality, chainable.

Separate calls to assertFormula accumulate requirements: every assertion must hold. To allow alternatives, build a single disjunction. Asserting a and then asserting b requires both, even if you intended “a or b.”

The example uses QF_UF, which accommodates these Boolean constraints. Once you add arithmetic conditions, choose an arithmetic logic containing the operations you use; chapter 5 gives examples.

Do not confuse a witness with a requirement

Remove the assertion selecting encryption. The implication alone allows several configurations. A model that happens to enable storage does not prove that storage is required. To test necessity, assert its negation in addition to the dependency and ask whether a solution remains. Chapter 7 develops this counterexample technique.

Exercise: introduce a conflict

Keep encryption selected and also require storage to be disabled.

Worked answer. Add the symbolic negation of storage. The result must be unsat: the dependency forces storage true while the added assertion forces it false. Change the example's expected result and remove its model query, because an unsatisfiable problem has no satisfying model to inspect.

Next: integer constraints.

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