Repository navigation
Conversation
`~q!(<pattern>)` matches like `~q(<pattern>)`, but the match test runs at `TransparencyMode.none`: constants compare by name and no definitions (not even `@[reducible]` ones) are unfolded. Antiquotation capture is unaffected (metavariable assignment, β/η are independent of transparency).
Covers: exact-head matching with captures, no unfolding of reducible definitions in either discriminant or pattern (with `~q` controls), higher-order capture, matching at higher universes, the `let`/`if let` forms, and `~q!` nested inside constructor patterns.
Author
|
Branch note: the PR diff is computed against the merge-base of my fork's |
…th master Upstream master bumped the toolchain past this branch (v4.36.0-rc2 -> v4.36.0 -> v4.37.0-rc1 in leanprover-community#179/leanprover-community#180), conflicting with the rc1->rc2 bump this branch carried from the fork's merge-base. Setting the branch's toolchain to master's makes the merge clean; the PR diff relative to the merge result is unchanged. Verified locally on leanprover/lean4:v4.37.0-rc1 (upstream master ad0c9f8 + this branch, toolchain conflict resolved to master's version): `lake build --wfail` passes (31 jobs, no warnings) and `lake test` passes (exit 0, 61 jobs incl. QqTest.structuralMatch).
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.
Closes #82.
What
Adds
~q!(<pattern>), a structural-matching variant of~q(<pattern>), as suggested in the issue. It works everywhere~qdoes —match,let, andif let, including patterns nested inside ordinary constructor patterns.How
~qmatching tests the elaborated pattern against the discriminant withwithReducible isDefEq, so@[reducible]definitions (includingabbrevs) are unfolded during the match — e.g.~q(myRed $x $y)matchesq(Nat.add a b)when@[reducible] def myRed := Nat.add(verified on currentmaster).~q!runs the same generated code but withwithTransparency .none: no delta-unfolding at all, so constants compare by name. Antiquotation capture is unaffected, since metavariable assignment and β/η do not depend on the transparency setting (covered by tests, including a higher-order$p x ycapture). The implementation threads astructural : Boolfrom the new syntax through the existing_qq_matchmachinery (Qq/Match.lean); pattern elaboration, thematch_eqhypothesis, and the macro plumbing are shared with~q.Tests
New
QqTest/structuralMatch.lean(picked up by theQqTest.+glob):~qcontrol that does match);abbrevunfolding:~qmatchesdouble kagainst$x + $y,~q!does not (#guard_msgspair);∃ x y, $p x y) and matching at a higher universe (Type 3vs a reducible alias);let ~q! ... | altandif let ~q!forms;~q!nested inside an ordinary constructor pattern.Verification
On the pinned toolchain (
leanprover/lean4:v4.36.0-rc2,masterb025662):lake build --wfail— passes (30 jobs, no warnings)lake test— passes (exit 0; fullQqTestsuite, 59 jobs, including the new file)master,~q!is a parse error; with the structural branch ofmkIsDefEqCoreflipped back towithReducible, the structural non-match tests and the#guard_msgscase failsorry/admit; the change is confined toQq/Match.leanplus the new test filegit hash-object) to the locally built and tested sources:Qq/Match.lean=522a53671d57a6f4a11cb4a5c020665f303701c4,QqTest/structuralMatch.lean=91670829bd2ecfa1bd6a808f573ab3c8c3ad94ca. (Two commits because the file upload API is per-file; feel free to squash.)Performance: an informal micro-benchmark (200 iterations against a 12-deep
@[reducible]tower, median-ish, this sandbox): a failing~qmatch that must unfold the whole tower took ~34µs vs ~25µs for~q!; a top-level successful match ~19µs vs ~17µs. I did not attempt to reproduce the issue's 100× figure, which presumably comes from a heavier workload; the point here is that~q!eliminates the unfolding work entirely.Note on semantics:
.nonetransparency still permits β/η and metavariable assignment, so~q!is "structural modulo β/η and antiquotation capture" rather than rawExprstructural equality — this is what makes antiquotations keep working, and matches what the issue's use case needs.