interpreter: measure-based termination for small-step configurations - #163
Open
theebayuser wants to merge 1 commit into
Open
interpreter: measure-based termination for small-step configurations#163theebayuser wants to merge 1 commit into
theebayuser wants to merge 1 commit into
Conversation
`TerminatesWith.of_termination_and_partial` splits total correctness into a termination half and a partial-correctness half. The Iris route supplies the second; this states the first once, in symbolic form, instead of by hand per program. - `reachesNormal_of_wf` / `_of_measure`: a measure decreasing along `Step` forces a finite trace to a normal form. Kept separate from classifying that normal form, since `Config.Safe` gives error-freedom, not progress. - `terminatesWith_of_iteration` / `_of_loop`: the shape a loop proof takes, where only the back edge decreases the variant. - `totalCorrectness_of_measure_of_partial`: composition with the Iris side. `SwapElementsOpt3.opt3_func0_terminates` unrolls a straight-line program and is unaffected. The induction recurs in five places — `NumIntegerOpt3.mod3_gcd_smallStep_termination` and the `SimpleLoop`, `Gcd`, `Factorial`, `EvenOddRec` examples — which this factors. `Examples/MeasureLoopDemo.lean` reproves `SimpleLoop`'s specification, about the same `simpleLoopConfig`, through `terminatesWith_of_loop`. Existing proofs are left untouched. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
States the well-founded termination argument for small-step configurations
once, as reusable lemmas, and demonstrates it on an existing example.
TerminatesWith.of_termination_and_partialsplits total correctness into atermination half and a partial-correctness half. The Iris route supplies the
second (adequacy lands in
PartiallyMeets); the first is a separatewell-founded argument. This adds that argument in symbolic form:
reachesNormal_of_wf/reachesNormal_of_measure— a measure that strictlydecreases along
Stepforces a finite trace to a configuration with nosuccessor. Deliberately says only that a normal form is reached;
classifying it as
.donerather than.trappedor stuck is left to thecaller's invariant, because
Config.Safegives error-freedom, not progress.terminatesWith_of_iteration/terminatesWith_of_loop— the form a loopproof actually takes. Requiring the measure to decrease on every step is
too strong: the instructions inside one iteration do not decrease a trip
count, only the back edge does. These index configurations by loop state
instead, and allow the postcondition to be weakened on the way out.
totalCorrectness_of_measure_of_partial— composition with the Iris side.Config.Normal,normal_unique— supporting definitions;done_normal/trapped_normaldelegate to the existingdone_terminal/trapped_terminal.Why
The seam is already in use, and its termination halves are written by hand each
time.
SwapElementsOpt3.opt3_func0_terminatesunrolls a straight-line programstep by step, which is fine and unaffected by this PR. The other five sites all
re-derive the same strong induction over a decreasing measure:
NumIntegerOpt3.mod3_gcd_smallStep_termination, and theSimpleLoop,Gcd,FactorialandEvenOddRecexamples.Examples/MeasureLoopDemo.leanshows the difference concretely: it provesSimpleLoop's exact specification, about the samesimpleLoopConfig, bysupplying the two branches and applying
terminatesWith_of_loop. Theper-iteration trace lemmas are unchanged — those are the real semantic content
and no lemma removes them. What disappears is the induction skeleton.
I have left the existing proofs alone rather than rewrite them in this PR. If
porting any of them would be useful, I am happy to do that as a follow-up.
Validation
interpreter:lake build Interpreter runnerclean;just runner-smokepasses.codelib:lake build --wfailclean.programs/lean:lake build Project --wfailclean.sorry/admit/axiom; every new theorem's axiom footprint is[propext, Classical.choice, Quot.sound].MeasureLoopDemois imported fromExamples/Basic.lean, so CI compiles it.