Skip to content

Commit 4aaa962

Browse files
committed
fix(Translate): preserve binder names when reordering lambdas (leanprover-community#40946)
To my surprise, `lambdaMetaTelescope` does not use the lambda binder names to give names to the introduced metavariables. As a result, `reorderLambda` would rename all lambda binder names to `x`. This is fixed by only using `forallMetaTelescope` to introduce the metavariables.
1 parent daca285 commit 4aaa962

2 files changed

Lines changed: 12 additions & 13 deletions

File tree

Mathlib/Tactic/Translate/Reorder.lean

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,15 +213,13 @@ partial def reorderForall (reorder : ArgReorder) (e : Expr) : MetaM Expr := do
213213

214214
/-- Reorder the arguments of a function using the given `ArgReorder`. -/
215215
partial def reorderLambda (reorder : ArgReorder) (e : Expr) : MetaM Expr := do
216-
let (mvars, bis, e) ← lambdaMetaTelescope e reorder.range
217-
let (mvars', bis', _) ← forallMetaBoundedTelescope (← inferType e) (reorder.range - mvars.size)
218-
let mut mvars := mvars ++ mvars'
216+
let (mvars, bis, _) ← forallMetaBoundedTelescope (← inferType e) reorder.range
219217
unless mvars.size = reorder.range do
220218
throwError "the permutation (reorder := {reorder}) is out of bounds, \
221219
the function{indentExpr e}\nhas only {mvars.size} arguments"
222-
let bis := reorder.permute! (bis ++ bis') |>.toList
220+
let bis := reorder.permute! bis |>.toList
223221
-- Note that `mkLambdaFVars` also works with mvars.
224-
fixBinderInfos bis <$> mkLambdaFVars (← reorderMVars mvars reorder) (mkAppN e mvars')
222+
fixBinderInfos bis <$> mkLambdaFVars (← reorderMVars mvars reorder) (e.beta mvars)
225223

226224
end
227225

MathlibTest/Attribute/ToDual.lean

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,17 @@ def SemilatticeSup.casesOn' {α} {motive : SemilatticeSup α → Sort*} (t : Sem
5555
t.casesOn mk
5656

5757
/--
58-
info: SemilatticeInf.casesOn'.{u_1} {α : Type} {motive : SemilatticeInf α → Sort u_1}
59-
(mk :
60-
[inst : Min α] →
61-
[inst_1 : PartialOrder α] →
62-
(sup_le : ∀ (b a c : α), c ≤ a → c ≤ b → c ≤ a ⊓ b) →
63-
motive { toPartialOrder := inst_1, toMin := inst, le_inf := ⋯ })
64-
(t : SemilatticeInf α) : motive t
58+
info: @[expose] def SemilatticeInf.casesOn'.{u_1} : {α : Type} →
59+
{motive : SemilatticeInf α → Sort u_1} →
60+
([inst : Min α] →
61+
[inst_1 : PartialOrder α] →
62+
(sup_le : ∀ (b a c : α), c ≤ a → c ≤ b → c ≤ a ⊓ b) →
63+
motive { toPartialOrder := inst_1, toMin := inst, le_inf := ⋯ }) →
64+
(t : SemilatticeInf α) → motive t :=
65+
fun {α} {motive} mk t => SemilatticeInf.casesOn t fun [PartialOrder α] [Min α] sup_le => mk ⋯
6566
-/
6667
#guard_msgs in
67-
#check SemilatticeInf.casesOn'
68+
#print SemilatticeInf.casesOn'
6869

6970
class Semilattice (α : Type) extends SemilatticeInf α, SemilatticeSup α
7071
attribute [to_dual existing] Semilattice.toSemilatticeSup

0 commit comments

Comments
 (0)