Zulip Chat Archive

Stream: lean4

Topic: match_expr functionality?


Thomas Murrills (Apr 06 2024 at 21:17):

I was curious if there are any plans to extend the functionality of match_expr, or if its intended scope is specifically simple .app and .const matches.

I tried replacing Qq matches in Mathlib with match_expr, and found that almost none of them could be replaced. Some necessary features simply weren't available, whereas some matches couldn't be replaced without making the code verbose and difficult to read. (Readability and brevity is especially important for maintainability here, of course, since the regions of code that require matching against expressions are often already verbose and relatively esoteric.)

I'm not insisting that the scope be expanded if this is the intended scope, necessarily—I'm just curious as to what its intended role is and where it's going. :)

If it's of interest, here are the issues I encountered.

Eric Wieser (Apr 06 2024 at 21:30):

I think there's a more crucial gap there, which is "no defeq matching", which is crucial for matching against a + b

Eric Wieser (Apr 06 2024 at 21:32):

My main wish for match_expr would be for it to be generalized to extensible match handlers; match_expr could then be match e with ~e(Nat.add $x $y) =>, and Qq could hook into the same machinery. A wide class of bugs in Qq (return, continue, let mut not working with it) are due to it being a second-class citizen that is not permitted to take part in the do control-flow rewriting

Eric Wieser (Apr 06 2024 at 21:35):

(Of course, match_expr is still great, and the choice to make it different syntax to match meant that it landed much more quickly than designing such an extension mechanism, leaving plenty of time to unify things the "right" way later)

Henrik Böving (Apr 06 2024 at 21:35):

Eric Wieser said:

I think there's a more crucial gap there, which is "no defeq matching", which is crucial for matching against a + b

I'm not 100% certain on this but as I understand match_expr is supposed to be a light weight tooling that does not call into potentially expensive things like isDefEq.

Maybe @Leonardo de Moura can explain his vision for match_expr?

Eric Wieser (Apr 06 2024 at 21:36):

To be clear, my point there was to say why match_expr is not a substitute for Qq; it certainly has a niche

Eric Wieser (Apr 06 2024 at 21:37):

And I think that niche is well-aligned with not supporting nested expressions or typeclass-based notation, as those are both places where using whnfR before matching isn't enough

Thomas Murrills (Apr 06 2024 at 21:37):

Eric Wieser said:

I think there's a more crucial gap there, which is "no defeq matching", which is crucial for matching against a + b

True! It would be great to have a way to support defeq matching somehow, as it is not infrequently crucial, but I will note that many Qq matches in Mathlib did not seem to need defeq matching, and were blocked only by the other issues.

Eric Wieser (Apr 06 2024 at 21:38):

Mathlib used to be full of defeq-matching mistakes, but it was hard to tell as an end-user; I fixed many of these in !3#18129

Thomas Murrills (Apr 06 2024 at 21:39):

Yeah, it is also possible that the defeq matching is actually crucial in most ~q matches despite how it seemed on first glance...


Last updated: May 02 2025 at 03:31 UTC