Zulip Chat Archive

Stream: lean4

Topic: inferInstance


Jeremy Avigad (Aug 19 2021 at 16:06):

I am trying to infer decidable equality for a defined type by unfolding it. In Lean 3, we'd use unfold and apply_instance. In Lean 4, I can do this:

def Foo := Nat
instance : DecidableEq Foo := @inferInstance (DecidableEq Nat) _

This also works just fine:

instance : DecidableEq Foo := show DecidableEq Nat from inferInstance

Does anyone know if there is a better idiom to use?

Jeremy Avigad (Aug 19 2021 at 16:08):

This also works:

instance : DecidableEq Foo := by simp [Foo]; exact inferInstance

Jannis Limperg (Aug 19 2021 at 16:09):

inferInstanceAs is probably what you want.

Sebastian Ullrich (Aug 19 2021 at 16:16):

It would be nice to have a deriving handler for this

Sebastian Ullrich (Aug 19 2021 at 16:18):

Ah, well, the current API doesn't quite allow for that yet

Jeremy Avigad (Aug 19 2021 at 16:22):

Yes, I am also doing the same thing to transfer Repr instances. But two lines of boilerplate is no problem. I am really enjoying Lean 4.

Mario Carneiro (Aug 19 2021 at 16:49):

Sebastian Ullrich said:

It would be nice to have a deriving handler for this

Actually, this came up in synport. Lean 3 has a deriving handler for this, the "delta derive handler", but lean 4 expects deriving handlers to be associated to a particular typeclass, and I don't see a space for fallback handlers

Mac (Aug 19 2021 at 17:20):

In Haskell, this type of deriving strategy is called 'newtype deriving'. Even if the current deriving syntax doesn't support it. I imagine one could write a deriving instance C for A from B to which elaborates to something like instance : C A := inferInstanceAs (C B) (for the unary case). There may even be a away to smartly infer B.

Mario Carneiro (Aug 19 2021 at 17:21):

But that has to work for all C

Mario Carneiro (Aug 19 2021 at 17:22):

Also, the syntax expects a unary typeclass which isn't always the case

Mac (Aug 19 2021 at 17:23):

Yeah, my example was just a trivial special case. There would be more nuances to getting it to work properly generally.


Last updated: Dec 20 2023 at 11:08 UTC