Skip to content

feat: add ~q! pattern matching with structural equality - #178

Open
josaum wants to merge 4 commits into
leanprover-community:masterfrom
josaum:feat/structural-match
Open

josaum wants to merge 4 commits into
leanprover-community:masterfrom
josaum:feat/structural-match

Conversation

@josaum

@josaum josaum commented Nov 12, 2026

Copy link
Copy Markdown

Closes #82.

What

Adds ~q!(<pattern>), a structural-matching variant of ~q(<pattern>), as suggested in the issue. It works everywhere ~q does — match, let, and if let, including patterns nested inside ordinary constructor patterns.

abbrev double (a : Nat) : Nat := a + a

example : Q(Nat) → MetaM Bool := fun e => do
  match e with
  | ~q!($a + $b) => return true   -- only a literal `HAdd.hAdd` application matches
  | _ => return false
-- on q(double k): `~q` returns true (unfolds `double` to `k + k`), `~q!` returns false

How

~q matching tests the elaborated pattern against the discriminant with withReducible isDefEq, so @[reducible] definitions (including abbrevs) are unfolded during the match — e.g. ~q(myRed $x $y) matches q(Nat.add a b) when @[reducible] def myRed := Nat.add (verified on current master).

~q! runs the same generated code but with withTransparency .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 y capture). The implementation threads a structural : Bool from the new syntax through the existing _qq_match machinery (Qq/Match.lean); pattern elaboration, the match_eq hypothesis, and the macro plumbing are shared with ~q.

Tests

New QqTest/structuralMatch.lean (picked up by the QqTest.+ glob):

  • exact-head structural matching with captures;
  • no unfolding of a reducible definition in the discriminant or in the pattern (each with a ~q control that does match);
  • default-transparency definitions unfold in neither variant;
  • abbrev unfolding: ~q matches double k against $x + $y, ~q! does not (#guard_msgs pair);
  • higher-order capture (∃ x y, $p x y) and matching at a higher universe (Type 3 vs a reducible alias);
  • the let ~q! ... | alt and if let ~q! forms;
  • ~q! nested inside an ordinary constructor pattern.

Verification

On the pinned toolchain (leanprover/lean4:v4.36.0-rc2, master b025662):

  • lake build --wfail — passes (30 jobs, no warnings)
  • lake test — passes (exit 0; full QqTest suite, 59 jobs, including the new file)
  • Teeth checks: on unmodified master, ~q! is a parse error; with the structural branch of mkIsDefEqCore flipped back to withReducible, the structural non-match tests and the #guard_msgs case fail
  • No sorry/admit; the change is confined to Qq/Match.lean plus the new test file
  • Both pushed files verified byte-identical (git 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 ~q match 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: .none transparency still permits β/η and metavariable assignment, so ~q! is "structural modulo β/η and antiquotation capture" rather than raw Expr structural equality — this is what makes antiquotations keep working, and matches what the issue's use case needs.

`~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.
@josaum

josaum commented Nov 12, 2026

Copy link
Copy Markdown
Author

Branch note: the PR diff is computed against the merge-base of my fork's master (859a214, pinned at v4.36.0-rc1) and upstream master (b025662, v4.36.0-rc2), so it also shows the one-line lean-toolchain bump from #170, which upstream already has — merging is content-neutral for that file. The intended change is exactly Qq/Match.lean and the new QqTest/structuralMatch.lean.

…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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add an option to pattern match using structural equality instead of definitional equality.

1 participant