refactor: make the Std.Internal.Do loop invariant non-dependent - #14601
Merged
Conversation
|
Mathlib CI status (docs):
|
Collaborator
|
Reference manual CI status:
|
sgraf812
force-pushed
the
sg/invariant-no-cursor
branch
from
July 30, 2026 11:47
db53d87 to
a787b80
Compare
This PR changes the loop invariant of `Std.Internal.Do` from a cursor indexed by the list being iterated to a plain function of the elements consumed so far and the elements remaining. The `for … invariant` clause now binds two lists, `invariant pref suff => …`, in place of the single cursor binder. `Invariant xs β Pred := List.Cursor xs → β → Pred` becomes `Invariant α β Pred := List α → List α → β → Pred`. The relation `pref ++ suff = xs` moves out of the type and into the hypotheses of the verification conditions, where it already appears as `h : xs = pref ++ cur :: suff`. This removes the `List.Cursor.cast` transports and the `by simp [ForIn.toList_list, h]` property proofs from the specification lemmas, and decouples an invariant's type from the spelling of the list a specification uses: the `forInWithInvariant` gadgets now take `inv : Invariant α β Pred`, naming neither the container nor `ForIn.toList`. `RepeatInvariant`, used for `repeatM`, is already non-dependent, so the two loop forms are now consistent. `Std.Do`, the legacy library, is unchanged.
Rewrites the `invariant` clauses and `inv` alternatives in the `vcgen` tests and benchmarks from cursor projections (`c.prefix`, `c.suffix`, `c.pos`) to the two list binders.
`Cursor.cast` existed to move a loop invariant between two spellings of the list it ranges over. The non-dependent invariant has no such spelling, so nothing transports cursors any more.
An invariant may constrain the state of the ambient monad rather than a mutable variable of the `do` block, which no test exercised.
This PR names the components of the mutable-variable tuple that the `invariant` clause elaboration builds `mutBinders`, `mutTuplePat` and `mutTupleBinder`, leaving "state" for the state of a stateful `Pred`.
sgraf812
force-pushed
the
sg/invariant-no-cursor
branch
from
July 30, 2026 12:51
19ec7a1 to
6113f93
Compare
sgraf812
marked this pull request as ready for review
July 30, 2026 13:02
sgraf812
enabled auto-merge
July 30, 2026 13:02
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.
This PR makes the loop invariant of
Std.Internal.Doa plain function of the elements consumed so far and the elements remaining, rather than a cursor indexed by the list being iterated. Thefor … invariantclause binds two lists,invariant pref suff => …, and verification conditions mention them directly instead of{ prefix := …, suffix := …, property := ⋯ }.prefix.Invariant xs β Pred := List.Cursor xs → β → PredbecomesInvariant α β Pred := List α → List α → β → Pred. The relationpref ++ suff = xsmoves out of the type and into the hypotheses of the verification conditions, where it already appears. Dropping the index removes theList.Cursor.casttransports and the property proofs from the specification lemmas, and frees theforInWithInvariantgadgets from naming a list at all: takinginv : Invariant α β Pred, they no longer need theForIn.toListbridge lemmas or the[ForIn Id ρ α]argument those required.RepeatInvariant, used forrepeatM, was already non-dependent.Std.Dois unchanged except for deletingList.Cursor.cast, which now has no use in either library.