-
Notifications
You must be signed in to change notification settings - Fork 715
feat: termination arguments for String.ValidPos and String.Slice.Pos
#10933
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
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
Collaborator
|
Mathlib CI status (docs):
|
Collaborator
|
Reference manual CI status:
|
c509e5e to
43b3808
Compare
43b3808 to
c381a0c
Compare
leanprover-community-mathlib4-bot
added a commit
to leanprover-community/batteries
that referenced
this pull request
Oct 26, 2025
leanprover-community-mathlib4-bot
added a commit
to leanprover-community/mathlib4-nightly-testing
that referenced
this pull request
Oct 26, 2025
wkrozowski
pushed a commit
to wkrozowski/lean4
that referenced
this pull request
Oct 27, 2025
…os` (leanprover#10933) This PR adds the basic infrastructure to perform termination proofs about `String.ValidPos` and `String.Slice.Pos`. We choose approach where the intended way to do termination arguments is to argue about the position itself rather than some projection of it like `remainingBytes`. The types `String.ValidPos` and `String.Slice.Pos` are equipped with a `WellFoundedRelation` instance given by the greater-than relation. This means that if a function takes a position `p` and performs a recursive call on `q`, then the decreasing obligation will be `p < q`. This works well in the common case where `q` is `p.next h`, in which case the goal `p < p.next h` is solved by the simplifier. For stepping through a string backwards, we introduce a type synonym with a `WellFoundedRelation` instance given by the less-than relation. This means that if a function takes a position `p` and performs a recursive call on `q` and specifies `termination_by p.down`, then the decreasing obligation will be `q < p`. This works well in the case where `q` is `p.prev h`, in which case the goal `p.prev h < p` is solved by the simplifier. For termination arguments invoving multiple strings, the lower-level primitive `p.remainingBytes` (landing in `Nat`) is also available. In a future PR, we will additionally provide the necessary typeclasses instances to register `String.ValidPos` and `String.Slice.Pos` with `grind` to make complex termination arguments more convenient in user code.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
builds-mathlib
CI has verified that Mathlib builds against this PR
changelog-library
Library
toolchain-available
A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN
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 adds the basic infrastructure to perform termination proofs about
String.ValidPosandString.Slice.Pos.We choose approach where the intended way to do termination arguments is to argue about the position itself rather than some projection of it like
remainingBytes.The types
String.ValidPosandString.Slice.Posare equipped with aWellFoundedRelationinstance given by the greater-than relation. This means that if a function takes a positionpand performs a recursive call onq, then the decreasing obligation will bep < q. This works well in the common case whereqisp.next h, in which case the goalp < p.next his solved by the simplifier.For stepping through a string backwards, we introduce a type synonym with a
WellFoundedRelationinstance given by the less-than relation. This means that if a function takes a positionpand performs a recursive call onqand specifiestermination_by p.down, then the decreasing obligation will beq < p. This works well in the case whereqisp.prev h, in which case the goalp.prev h < pis solved by the simplifier.For termination arguments invoving multiple strings, the lower-level primitive
p.remainingBytes(landing inNat) is also available.In a future PR, we will additionally provide the necessary typeclasses instances to register
String.ValidPosandString.Slice.Poswithgrindto make complex termination arguments more convenient in user code.