Repository navigation
Start nested loop inference from its last invariants; join only for the loop's own continues - #556
CatarinaGamboa wants to merge 3 commits into
Conversation
Each check of a loop body starts from no more facts than the one before, so a loop nested in it cannot have more invariants than at its last visit: its next visit only tries those. A check that starts from more facts (one that no longer skips a statement, or a loop starting over) forgets them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A continue of an inner loop does not reach the end of the outer body, so the values there need not be forgotten. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex review, round 1No correctness issue found. Codex wrote 21 programs and compared this branch with the #555 jar:
The two differences both come from change 2 (join only for the loop's own continues), and both are more precise:
The programs covered:
What was doneNothing to fix. I start a second round aimed at the interaction with #555's skipping, where nested results are forgotten. |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex review, round 2No problem found. Codex read the warm-start code, looking for a check that reuses an inner loop's result after a check with more facts. It found none:
The before (#555) and branch jars gave byte-for-byte identical output on three programs (
What was doneThe There are no findings in two rounds, so the Codex loop for this PR ends here. |
Clean-code reviewI audited only this PR's new code (
|
Stacked on #555; review/merge after it. Independent of #552.
Fixes #547
Part of the loop invariant inference plan (#542).
Measured first
I instrumented a throwaway build (not in this PR) to log every inference over the 401 test cases and the 8 study tasks:
MAX_PER_VARIABLE = 6)MAX_ROUNDS = 8)MAX_ROUNDSLoop checking (inference included) takes about 12% of the suite's verification time; a third of that is the candidate proofs (about 4 ms each, without cache).
Caps: unchanged. Neither cap is ever reached, so lowering them would only cost precision on unusual loops, and raising them gains nothing measurable.
What does cost: an inner loop runs its whole inference again on every check of the outer body (each outer check runs 2 inner checks), and the work multiplies with depth.
What and why
lastInferred). Each check of a loop body starts from no more facts than the one before: fewer invariants, more typestates, more skipped statements (Report an error in a loop body only with invariants proved on every path #555). So an inner loop cannot have more invariants than at its last visit, and its next visit only tries those. It usually confirms them in one check instead of two. The candidates are matched across visits by their text, with the instance before the loop renamed (i_before), since instance names change on every check.scanBodyjoins only for the loop's own continues. The end of the body was a join (forget the changed variables, keep the candidates and grew/shrank) as soon as the body held anycontinue, also one of an inner loop. Such a continue never reaches the outer end of the body. The join is now only made when acontinuetargets this loop (jumpTarget(jump) == loop, so a labelled continue from an inner loop still counts). This saves the join's proofs, and it is a strict precision improvement: aforupdate ordo-whilecondition after an inner loop with a continue keeps the values the body gave.Soundness argument
MAX_ROUNDSand dropping everything, which the stats show never happens.LoopTypestates.recordContinuecomes fromloopLeftBy), so they are unchanged.Before / after
do { i = 10; for (...) { if (next()) continue; } } while (ten(i) && next());withten(@Refinement("_ == 10") int x): before,expected i == 10 but could not prove it; after, Correct.Tests
CorrectLoopInvariants.continueOfInnerLoop(the example above; fails without change 2).CorrectLoopInvariantsandErrorLoopInvariantsalready run the warm start.Full suite passes locally. Study tasks (
verify-tasks.py, 55 variants): verdicts and messages identical toverify-cur-redesign.json.Timing (before = #555, after = this PR; same machine, shared, load about 4)
mvn -q testwall, back to back (2 runs each)verify-tasks.pywallclasses/loop_list_index_correctCorrectLoopInvariantsCorrectLoopStateFixpointErrorLoopInvariantsErrorLoopStateFixpoint(3 alternating runs)The suite and the study tasks have few nested loops, so their totals do not move beyond the noise. The saving shows where nesting is deep: 27% on the stress program.
Not done: proving all candidates in one query before proving them one by one. It would save about one 4 ms query per loop in the confirming check, since loops keep 0 to 3 invariants.
🤖 Generated with Claude Code