fix: apply rcases substitution before recording pattern info#13621
fix: apply rcases substitution before recording pattern info#13621berberman wants to merge 2 commits intoleanprover:masterfrom
rcases substitution before recording pattern info#13621Conversation
This PR hoists `fs.apply` to let `addTermInfo'` and `addLocalVarInfo` use correct expr. Previously, the substitution only happened in `rfl`/typed/tuple/alternative branches, which caused stale free variables to be recorded in the info tree. Repeated applications in recursive cases like `.paren` should be fine, because the domain of `fs` should be old fvars and replacement exprs should only refer current-goal fvars, not old-domain fvars. Proof elaboration shouldn't be affected by this PR.
|
Mathlib CI status (docs):
|
|
Reference manual CI status:
|
| cont g fs clears a | ||
| | .clear ref => | ||
| Term.addTermInfo' ref (.mdata {} e) | ||
| cont g fs (if let .fvar e := e then clears.push e else clears) a |
There was a problem hiding this comment.
This change potentially affects the clears list, but it might take some work coming up with an example that shows any difference. In any case, Lean.Elab.Tactic.RCases.finish applies the FVarSubst, though I notice it's doing it non-recursively (that's might be another bug).
Here's at least a case that currently fails on the most recent release that you could add to your test file:
example (n m : Nat) (h : n = m) : True := by
rcases n, h with ⟨_ | _, -⟩
--^ unknown free variable `_fvar.1009`
sorry
sorryI suspect you may be able to come up with an example where - doesn't clear things if one of the previous patterns created FVarSubst entries.
It would be nice to have a test case for .alts too demonstrating the fix, to prevent any regressions in the future.
There was a problem hiding this comment.
Thanks for your review. I think clears is OK with this change. Before this patch, - could record the original fvar, but finish maps every recorded clear target through the final FVarSubst, so previous substitutions were accounted for when clearing. Now - should record the current fvar instead, and fs.get in finish should be harmless for those.
Also I think a single fs.get should be enough, because FVarSubst maintains compositional substitutions: insert rewrites existing mapped values, and append preserves (s.append t).apply e = t.apply (s.apply e). So if a cleared variable is later replaced by a new fvar, fs.get gives current fvar which makes - work.
I've added more tests, though multi-branch .alts was not a failure before the patch.
This PR fixes a bug in the
rcases-family tactics where the InfoView could give "unknown free variable" errors when the cursor was inside the pattern. It hoists applying the fvar substitution to giveaddTermInfo'andaddLocalVarInfothe correct expression. Previously, the substitution only happened inrfl/typed/tuple/alternative branches, which caused stale free variables to be recorded in the info tree. Repeated applications in recursive cases like.parenshould be fine, because the domain offsshould be old fvars and replacement exprs should only refer current-goal fvars, not old-domain fvars. Proof elaboration shouldn't be affected by this PR.The fix was suggested by @kmill.
Closes #12369