fix: instance name for deriving ToExpr (#10682)

This PR changes the instance name for `deriving ToExpr` to be consistent
with other derived instance since #10271. Fixes #10678.
This commit is contained in:
Joachim Breitner 2025-10-06 13:46:46 +02:00 committed by GitHub
parent fbfb0757ca
commit 30f41fe542
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 75 additions and 10 deletions

View file

@ -84,7 +84,7 @@ where
let ctx ← mkContext ``Inhabited "default" inductiveTypeName
let auxFunName := ctx.auxFunNames[0]!
`(def $(mkIdent auxFunName):ident $binders:bracketedBinder* : $type := $val
instance $binders:bracketedBinder* : Inhabited $type := ⟨$(mkIdent auxFunName)⟩)
instance $(mkIdent ctx.instName):ident $binders:bracketedBinder* : Inhabited $type := ⟨$(mkIdent auxFunName)⟩)
mkInstanceCmd? : TermElabM (Option Syntax) := do

View file

@ -6,13 +6,11 @@ Authors: Kyle Miller
module
prelude
public import Lean.Meta.Transform
public import Lean.Elab.Deriving.Basic
public import Lean.Elab.Deriving.Util
public import Lean.ToLevel
public import Lean.ToExpr
public section
import Lean.Meta.Transform
import Lean.Elab.Deriving.Basic
import Lean.Elab.Deriving.Util
import Lean.ToLevel
import Lean.ToExpr
/-!
# `ToExpr` deriving handler
@ -190,7 +188,8 @@ open TSyntax.Compat in
/--
Assuming all of the auxiliary definitions exist,
creates all the `instance` commands for the `ToExpr` instances for the (mutual) inductive type(s).
This is a modified copy of `Lean.Elab.Deriving.mkInstanceCmds` to account for `ToLevel` instances.
This is a modified copy of `Lean.Elab.Deriving.mkInstanceCmds` to account for `ToLevel` instances
parameters.
-/
def mkInstanceCmds (ctx : Deriving.Context) (typeNames : Array Name) :
TermElabM (Array Command) := do
@ -206,7 +205,8 @@ def mkInstanceCmds (ctx : Deriving.Context) (typeNames : Array Name) :
let binders := binders ++ levelBinders
let indType ← updateIndType indVal (← mkInductiveApp indVal argNames)
let toTypeExpr ← mkToTypeExpr indVal argNames
let instCmd ← `(instance $binders:implicitBinder* : ToExpr $indType where
let instName ← mkInstName ``ToExpr indVal.name
let instCmd ← `(instance $(mkIdent instName):ident $binders:implicitBinder* : ToExpr $indType where
toExpr := $(mkIdent auxFunName) $toLevelInsts*
toTypeExpr := $toTypeExpr)
instances := instances.push instCmd

View file

@ -0,0 +1,65 @@
import Lean
namespace A
structure A (α : Type u) where
a : α
deriving Lean.ToExpr, Inhabited
-- same namespace for instance and aux decls
/--
info: def A.instToExprA.{u} : {α : Type u} → [Lean.ToExpr α] → [Lean.ToLevel] → Lean.ToExpr (A α) :=
fun {α} [Lean.ToExpr α] [inst_1 : Lean.ToLevel] =>
{ toExpr := instToExprA.toExpr inst_1, toTypeExpr := (Lean.Expr.const `A.A [Lean.toLevel]).app (Lean.toTypeExpr α) }
-/
#guard_msgs in #print A.instToExprA
/--
info: def A.instInhabitedA.{u_1} : {a : Type u_1} → [Inhabited a] → Inhabited (A a) :=
fun {a} [Inhabited a] => { default := instInhabitedA.default }
-/
#guard_msgs in #print A.instInhabitedA
end A
mutual
inductive B (α : Type u) : Type _ where
| leaf
| mk (a : C α)
deriving Lean.ToExpr, Inhabited
inductive C (α : Type u) : Type _ where
| mk (b : B α)
deriving Lean.ToExpr, Inhabited
end
/--
info: def instToExprB.{u} : {α : Type u} → [Lean.ToExpr α] → [Lean.ToLevel] → Lean.ToExpr (B α) :=
fun {α} [Lean.ToExpr α] [inst_1 : Lean.ToLevel] =>
{ toExpr := instToExprB.toExpr_1 inst_1, toTypeExpr := (Lean.Expr.const `B [Lean.toLevel]).app (Lean.toTypeExpr α) }
-/
#guard_msgs in
#print instToExprB
/--
info: def instToExprC.{u} : {α : Type u} → [Lean.ToExpr α] → [Lean.ToLevel] → Lean.ToExpr (C α) :=
fun {α} [Lean.ToExpr α] [inst_1 : Lean.ToLevel] =>
{ toExpr := instToExprB.toExpr_2 inst_1, toTypeExpr := (Lean.Expr.const `C [Lean.toLevel]).app (Lean.toTypeExpr α) }
-/
#guard_msgs in
#print instToExprC
/--
info: def instInhabitedB.{u_1} : {a : Type u_1} → Inhabited (B a) :=
fun {a} => { default := instInhabitedB.default_1 }
-/
#guard_msgs in
#print instInhabitedB
/--
info: def instInhabitedC.{u_1} : {a : Type u_1} → Inhabited (C a) :=
fun {a} => { default := instInhabitedC.default_1 }
-/
#guard_msgs in
#print instInhabitedC