Skip to content

feat: support a destructuring binder on a for … invariant loop - #14682

Merged
sgraf812 merged 4 commits into
masterfrom
sg/for-invariant-binders
Aug 5, 2026
Merged

feat: support a destructuring binder on a for … invariant loop#14682
sgraf812 merged 4 commits into
masterfrom
sg/for-invariant-binders

Conversation

@sgraf812

@sgraf812 sgraf812 commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

This PR lets a for loop that destructures its binder carry an invariant clause, so a loop over a map may bind (k, v) and still state its invariant. A container that the clause cannot verify is reported where the clause appears, naming the PureForIn instance it lacks, instead of surfacing later as a vcgen gadget with no applicable specification.

This PR lets a `for` loop that destructures its binder carry an `invariant` clause, so a loop over a map may bind `(k, v)` and still state its invariant. A loop over several collections continues to reject the clause.

The clause is carried through the expansion that replaces the pattern by a fresh binder and a `match`, which leaves it unchanged because it ranges over the consumed and remaining elements and the loop state rather than over the pattern.
…rified

This PR reports a container that does not declare its loop effect-free at the `invariant` clause itself, naming the container and the missing `PureForIn` instance, rather than leaving a gadget that `vcgen` later reports as having no applicable specification.

The clause elaborator synthesizes the class its specification is stated over. The check is skipped while the container type, the element type or the monad is still an open metavariable, so it reports only what it can see.
@github-actions github-actions Bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Aug 5, 2026
@mathlib-lean-pr-testing

mathlib-lean-pr-testing Bot commented Aug 5, 2026

Copy link
Copy Markdown

Mathlib CI status (docs):

  • ❗ Mathlib CI can not be attempted yet, as the nightly-testing-2026-08-05 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-mathlib, Mathlib CI should run now. You can force Mathlib CI using the force-mathlib-ci label. (2026-08-05 09:19:46)
  • ✅ Mathlib branch lean-pr-testing-14682 has successfully built against this PR. (2026-08-05 11:24:43) View Log

@leanprover-bot

Copy link
Copy Markdown
Collaborator

Reference manual CI status:

  • ❗ Reference manual CI can not be attempted yet, as the nightly-testing-2026-08-05 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-manual, reference manual CI should run now. You can force reference manual CI using the force-manual-ci label. (2026-08-05 09:19:47)

Registering the class as a synthetic instance metavariable lets elaboration postpone it while the container, element or monad type is still open, and reports it through the usual unresolved-instance error.
@sgraf812
sgraf812 marked this pull request as ready for review August 5, 2026 09:56
Keeps one contract per binder form, plain, membership and destructuring, and states the two rejected loops with a single-line body. The membership binder was covered three times over, once per container.
@sgraf812 sgraf812 added the changelog-language Language features and metaprograms label Aug 5, 2026
@sgraf812
sgraf812 enabled auto-merge August 5, 2026 10:04
@sgraf812
sgraf812 added this pull request to the merge queue Aug 5, 2026
@github-actions github-actions Bot added the mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN label Aug 5, 2026
Merged via the queue into master with commit 82a131b Aug 5, 2026
27 of 28 checks passed
@sgraf812
sgraf812 deleted the sg/for-invariant-binders branch August 5, 2026 11:05
@mathlib-lean-pr-testing mathlib-lean-pr-testing Bot added the builds-mathlib CI has verified that Mathlib builds against this PR label Aug 5, 2026
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-language Language features and metaprograms mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants