feat: accept match alternatives in an ensures clause - #14701
Merged
Conversation
This PR lets the `requires` clause of a `def` contract be written like a `fun`, so a precondition may destructure the arguments of the assertion: `requires | (a, b) => a = 0 ∧ b = n` matches on the state of a state monad. The term form `requires P` and the binder form `requires s => P s` are unchanged. The clause parser gains `Term.matchAlts` as an alternative, and `expandDefContract` wraps it in the `fun` that becomes the precondition of the specification theorem.
|
Mathlib CI status (docs):
|
Collaborator
|
Reference manual CI status:
|
Moves the fun-like clause body from `requires`, whose arguments are the assertion's own and carry no shape to match on, to `ensures`, which binds the result. Both clauses now start on their own line when pretty printed, so a clause can no longer trail the alternatives of the one before it.
requires clauseensures clause
sgraf812
marked this pull request as ready for review
August 6, 2026 08:43
Contributor
Author
|
!bench |
sgraf812
enabled auto-merge
August 6, 2026 08:43
|
Benchmark results for 559fd19 against c4e6b62 are in. No significant results found. @sgraf812
Small changes (3🟥)
|
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 lets the
ensuresclause of adefcontract be written like afun, so a postcondition may be stated per shape of the result:ensures | none => False | some v => 2 * v ≤ n. A contract clause now also starts on its own line when pretty printed, as it is written in source.ensuresClausegainsTerm.matchAltsas an alternative andexpandDefContractwraps it in thefunthat becomes the postcondition of the specification theorem. I decided to keeprequiresas is, because matching on state arguments of the assertion is not a use case I intend to support.