Skip to content

std::algo::smt: read integer queries (QF_LIA), and solve them with the SAT solver and simplex - #4417

Open
hiraditya wants to merge 2 commits into
mainfrom
smt-integer-queries
Open

hiraditya wants to merge 2 commits into
mainfrom
smt-integer-queries

Conversation

@hiraditya

@hiraditya hiraditya commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Step 7 of #4268, second task. Fixes #4389.

What changes

  • The reader. The session of std::algo::smt now reads the Int sort and numerals of any size. It also reads +, -, * by a constant, mod and div by a constant, and ite of Ints. The comparisons are <=, <, >=, >, = and distinct, also with more than two arguments. set-logic QF_LIA works. A product of two names and mod by a name are errors, because they are QF_NIA.
  • Constants fold as the reader reads them. The prover writes wrapping arithmetic as (- (mod (+ x 2^63) 2^64) 2^63). When x is a constant, the whole term becomes one constant. An ite whose condition folds to true or false is its branch.
  • Atoms. Each comparison becomes a fact s <= b over integer variables. The solver divides the fact by the GCD of its coefficients and rounds b down. It gives the fact a sign, so that equal facts are one Boolean variable, an atom. So (= (* 2 x) 1) is the AND of x <= 0 and its negation, which is false with no search. Atoms with the same sum share one row of the simplex.
  • mod and div add the variables q and r with four unit clauses, and two of them with the same t and k share q and r. An Int ite adds a variable with four clauses.
  • The loop. The SAT solver sets the atoms. The simplex gets s <= b for a true atom and s >= b + 1 for a false one, and searches with branch-and-bound and cuts. Each conflict of the simplex comes back as a lemma, the negation of the atoms in its certificate. :rlimit limits the steps of the two together.
  • get-value gives an Int as 5 or (- 5). get-proof gives each lemma with its certificate tree, and then the LRAT proof. The lemmas are input clauses of the LRAT proof.
  • The top of smt.vx lists the rules of the translation, with the order of the numbers of the variables. hir: check the unsat answers of the integer solver in vxc, with the LRAT proof and a certificate for each clause from simplex #4390 can then make the same clauses in vxc.
  • docs/solver/integers.md is a new page with a compiled example, the rules, the loop step by step, and the proof format.

Tests

  • tests/backend/pass/std_algo_smt_integers.vx has one small query for each operator, each with one answer that is easy to find by hand. It also checks three proofs, the step limit, a constant above 2^128 in a scope, and the errors. It prints the same with --legacy-codegen.
  • A product by 0, such as (* 0 x), made a term with the coefficient 0. The division by the GCD then divided by 0, and the solver stopped. The second commit drops such terms, and the test checks 0x + y = 7 and 0x >= 1.
  • tests/integration_test/smt_integer_queries_agree_with_z3.rs runs the 45 queries of tests/solver/queries/. Each answer must be the answer of z3. For each sat answer, z3 must accept the values of get-value.
  • std_algo_smt_scopes_and_errors.vx now uses QF_NIA as its unsupported logic.

I broke smt.vx 20 ways, one at a time, and ran the hand test after each break. Each break made a check fail:

  • Wrong answers or values (12 breaks):
    • A bound rounded toward zero, and a false atom read as s >= b.
    • No r <= |k| - 1, and the wrong sign of c in an ite.
    • < read as <=, and = without its negation.
    • mod and div of constants folded toward zero, the wrong branch of a folded ite, and a folded < read as <=.
    • A numeral that drops digits, and no values from the simplex.
  • Wrong proofs (5 breaks): no division by the GCD, no flip of the sign, no r >= 0, and atoms or mod variables that are not shared. The answers stay the same. The proof checks catch these, so the test covers the rules that hir: check the unsat answers of the integer solver in vxc, with the LRAT proof and a certificate for each clause from simplex #4390 needs.
  • (- 7) written as (-7) changed the printed values. A missing step limit gave unsat in place of unknown. Lemmas with the wrong signs made the test run until the 300 s timeout.

I also broke the code 4 ways for the test of the 45 queries. Values plus 1, a false atom read as s >= b, and < read as <= each gave values that z3 did not accept. A sat of the simplex read as unsat failed the check of the answer.

The queries of the prover

I ran each of the 1,569 QF_LIA queries of #4388 through the solver, in its own process, 8 at a time, with a time cap. Each query kept its (set-option :rlimit 1000000). The setup: an Apple M4 with 32 GB, macOS 27.0.1, z3 4.15.4, and this branch on main 4f861d5. A debug vxc compiled the runner with vxc -O2 --action emit-obj.

Run Queries sat unsat Not ended
Cap of 10 s 1,569 972 584 13
The 13 again, cap of 300 s 13 0 1 12

Found on the way

Not in this PR

…e SAT solver and simplex

The session of std::algo::smt now reads the Int sort, numerals of any size, `+`, `-`, `*` by a
constant, `mod` and `div` by a constant, `ite` of Ints, and the comparisons `<=`, `<`, `>=`, `>`,
`=` and `distinct`. The reader folds each term of constants, so the wrapping arithmetic of the
prover becomes one constant when its argument is a constant.

Each comparison becomes a fact `s <= b` over integer variables. The solver divides the fact by
the GCD of its coefficients, and gives it a sign so that equal facts are one Boolean variable, an
atom. `mod` and `div` add the variables `q` and `r` with four unit clauses, and an Int `ite` adds
a variable with four clauses. The SAT solver sets the atoms, the simplex checks the bounds of the
atoms, and each conflict of the simplex comes back as a lemma clause. `get-value` gives the value
of each Int name, and `get-proof` gives each lemma with its certificate tree before the LRAT
proof. The top of smt.vx lists the rules, so that the checker of #4390 can make the same clauses.

tests/backend/pass/std_algo_smt_integers.vx checks one small query for each operator by hand.
tests/integration_test/smt_integer_queries_agree_with_z3.rs runs the 45 queries of
tests/solver/queries/: each answer is the answer of z3, and z3 accepts the values of each `sat`.
docs/solver/integers.md describes the reader, the atoms, the loop and the proof.

Fixes: #4389
`(* 0 x)` made the term `0*x` in its sum. A fact on that sum then
divided its coefficients by their GCD, which was 0, and the solver
stopped with "BigInt::div_rem: division by zero". A sum now drops each
term whose coefficient is 0. The rules at the top of smt.vx say so,
because the checker in vxc must make the same sums.

std_algo_smt_integers.vx checks `0x + y = 7` and `0x >= 1`.

Part of #4389.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

std::algo::smt: read integer queries (QF_LIA), and solve them with the SAT solver and simplex

1 participant