lean4-htt/tests/lean/defInst.lean
Kyle Miller 20eea7372f
feat: make delta deriving more robust and handle binders (#9800)
This PR improves the delta deriving handler, giving it the ability to
process definitions with binders, as well as the ability to recursively
unfold definitions. Furthermore, delta deriving now tries all explicit
non-out-param arguments to a class, and it can handle "mixin" instance
arguments. The `deriving` syntax has been changed to accept general
terms, which makes it possible to derive specific instances with for
example `deriving OfNat _ 1` or `deriving Module R`. The class is
allowed to be a pi type, to add additional hypotheses; here is a Mathlib
example:
```lean
def Sym (α : Type*) (n : ℕ) :=
  { s : Multiset α // Multiset.card s = n }
deriving [DecidableEq α] → DecidableEq _
```
This underscore stands for where `Sym α n` may be inserted, which is
necessary when `→` is used. The `deriving instance` command can refer to
scoped variables when delta deriving as well. Breaking change: the
derived instance's name uses the `instance` command's name generator,
and the new instance is added to the current namespace.

This closes
[mathlib4#380](https://github.com/leanprover-community/mathlib4/issues/380).
2025-08-10 21:21:54 +00:00

35 lines
624 B
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

def Foo := List Nat
def test (x : Nat) : Foo :=
[x, x+1, x+2]
#eval test 4
#check fun (x y : Foo) => x == y -- Error
deriving instance BEq, Repr for Foo
#eval test 4
#check fun (x y : Foo) => x == y
def Boo := List (String × String)
deriving BEq, Repr, DecidableEq
def mkBoo (s : String) : Boo :=
[(s, s)]
#eval mkBoo "hello"
#eval mkBoo "hell" == mkBoo "hello"
#eval mkBoo "hello" == mkBoo "hello"
#eval mkBoo "hello" = mkBoo "hello"
def M := ReaderT String (StateT Nat IO)
deriving Monad, MonadState, MonadReader
#print instMonadM
def action : M Unit := do
modify (· + 1)
dbg_trace "{← read}"