Zulip Chat Archive
Stream: general
Topic: how to make deriving handler
Asei Inoue (Oct 15 2025 at 15:15):
I want to understand how to make deriving handler, so I want to create a very simple toy example.
How to resolve this error?
import Lean
open Lean Elab Command
universe u
class ToUnit (α : Type u) where
toUnit : α → Unit
instance : ToUnit Nat where
toUnit := fun _ => ()
#guard ToUnit.toUnit 42 = ()
def deriveToUnitInstance (declNames : Array Name) : CommandElabM Bool := do
for declName in declNames do
let term := quote (k := `term) <| declName
let cmd ← `(command| instance : ToUnit $term := ⟨fun _ => ()⟩)
elabCommand cmd
return true
/-
Application type mismatch: The argument
`Foo
has type
Name
of sort `Type` but is expected to have type
Type ?u.1135
of sort `Type (?u.1135 + 1)` in the application
ToUnit `Foo
-/
run_cmd
deriveToUnitInstance #[`Foo]
initialize
registerDerivingHandler ``ToUnit deriveToUnitInstance
Robin Arnez (Oct 15 2025 at 15:22):
Replace quote (k := `term) with mkCIdent
Asei Inoue (Oct 15 2025 at 15:23):
@Robin Arnez Thank you!!!!!!
Robin Arnez (Oct 15 2025 at 15:24):
quote x is meant to create a term that evaluates to x
Robin Arnez (Oct 15 2025 at 15:24):
mkCIdent nm instead creates an identifier term that resolves to the global declaration nm
Robin Arnez (Oct 15 2025 at 15:25):
mkIdent nm just creates an identifier which will go through namespace resolution
Last updated: Dec 20 2025 at 21:32 UTC