Skip to content

Commit 2100ab2

Browse files
committed
fix(rw??): whnf on equality hypotheses (#31609)
This PR adds some missing `whnf` in the `rw??` implementation. This only affects the behaviour of rewriting with local variables. I discovered that sometimes the type can be an uninstantiated metavariable that needs to first instantiated, for example by using `whnf`.
1 parent 3647d73 commit 2100ab2

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

Mathlib/Tactic/Widget/LibraryRewrite.lean

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,10 @@ def addRewriteEntry (name : Name) (cinfo : ConstantInfo) :
148148

149149
/-- Try adding the local hypothesis to the `RefinedDiscrTree`. -/
150150
def addLocalRewriteEntry (decl : LocalDecl) :
151-
MetaM (List ((FVarId × Bool) × List (Key × LazyEntry))) :=
152-
withReducible do
153-
let (_, _, eqn) ← forallMetaTelescope decl.type
154-
let some (lhs, rhs) := eqOrIff? eqn | return []
151+
MetaM (List ((FVarId × Bool) × List (Key × LazyEntry))) := do
152+
-- The transparency is set to `reducible`. Stronger reduction may give unexpected results.
153+
let (_, _, eqn) ← forallMetaTelescopeReducing decl.type
154+
let some (lhs, rhs) := eqOrIff? (← whnf eqn) | return []
155155
let result := ((decl.fvarId, false), ← initializeLazyEntryWithEta lhs)
156156
return [result, ((decl.fvarId, true), ← initializeLazyEntryWithEta rhs)]
157157

@@ -213,8 +213,9 @@ structure Rewrite where
213213
def checkRewrite (thm e : Expr) (symm : Bool) : MetaM (Option Rewrite) := do
214214
withTraceNodeBefore `rw?? (return m!
215215
"rewriting {e} by {if symm then "" else ""}{thm}") do
216-
let (mvars, binderInfos, eqn) ← forallMetaTelescope (← inferType thm)
217-
let some (lhs, rhs) := eqOrIff? eqn | return none
216+
let (mvars, binderInfos, eqn) ← forallMetaTelescopeReducing (← inferType thm)
217+
let some (lhs, rhs) := eqOrIff? (← whnf eqn) |
218+
throwError "Expected equation, not {indentExpr eqn}"
218219
let (lhs, rhs) := if symm then (rhs, lhs) else (lhs, rhs)
219220
let unifies ← withTraceNodeBefore `rw?? (return m! "unifying {e} =?= {lhs}")
220221
(withReducible (isDefEq lhs e))
@@ -267,7 +268,8 @@ def getModuleRewrites (e : Expr) : MetaM (Array (Array (Rewrite × Name))) := do
267268
/-! ### Rewriting by hypotheses -/
268269

269270
/-- Construct the `RefinedDiscrTree` of all local hypotheses. -/
270-
def getHypotheses (except : Option FVarId) : MetaM (RefinedDiscrTree (FVarId × Bool)) := do
271+
def getHypotheses (except : Option FVarId) : MetaM (RefinedDiscrTree (FVarId × Bool)) :=
272+
withReducible do
271273
let mut tree : PreDiscrTree (FVarId × Bool) := {}
272274
for decl in ← getLCtx do
273275
if !decl.isImplementationDetail && except.all (· != decl.fvarId) then
@@ -429,8 +431,8 @@ def getRewriteInterfaces (e : Expr) (occ : Option Nat) (loc : Option Name) (exce
429431
/-- Render the matching side of the rewrite lemma.
430432
This is shown at the header of each section of rewrite results. -/
431433
def pattern {α} (type : Expr) (symm : Bool) (k : Expr → MetaM α) : MetaM α := do
432-
forallTelescope type fun _ e => do
433-
let some (lhs, rhs) := eqOrIff? e | throwError "Expected equation, not {indentExpr e}"
434+
forallTelescopeReducing type fun _ e => do
435+
let some (lhs, rhs) := eqOrIff? (← whnf e) | throwError "Expected equation, not {indentExpr e}"
434436
k (if symm then rhs else lhs)
435437

436438
/-- Render the given rewrite results. -/

0 commit comments

Comments
 (0)