Zulip Chat Archive
Stream: mathlib4
Topic: Instances on sum components
Sébastien Gouëzel (Mar 26 2024 at 13:06):
I want to define an auxiliary type as a sum. I can not have Lean realize that, since all components of the sum satisfy some instance, then all components of the sum satisfy this instance (yes, I wrote the same thing twice). Example:
import Mathlib.RingTheory.Bezout
variable (R : Type) [Ring R]
@[reducible] def two_rings : Unit ⊕ Unit → Type
| Sum.inl _ => R
| Sum.inr _ => R
lemma foo (i : Unit ⊕ Unit) : Ring (two_rings R i) := by infer_instance -- fails
lemma bar : (i : Unit ⊕ Unit) → Ring (two_rings R i) -- works
| Sum.inl _ | Sum.inr _ => by infer_instance
In my case, it's a topological module structure. I can define all the instances by hand just like for bar
above, but I wonder if I'm missing some construction to make this automatic for typeclass inference.
Eric Wieser (Mar 26 2024 at 13:08):
I asked about this a long time ago here
Sébastien Gouëzel (Mar 26 2024 at 13:12):
It's funny, I need this also for multilinear maps (but in my use case the two types along inl
and inr
are not the same, so I can't really take a shortcut...)
Matthew Ballard (Mar 26 2024 at 14:11):
It seems like you want something like the deriving
handler for instances on types with finitely many terms to apply Pi.ring
etc
Sébastien Gouëzel (Mar 26 2024 at 14:25):
It's not a Pi
instance, as I'm not asking to show that the global object has some typeclass. I'm only asking that each component should have some instance detected by typeclass inference, given the fact that each component has the instance to start with. It's completely tautological (or syntaxical?).
Matthew Ballard (Mar 26 2024 at 14:27):
Oh ok. You want the hypothesis of Pi.ring
Yaël Dillies (Mar 26 2024 at 17:18):
Still, it sounds like it could be a job for deriving
Eric Wieser (Mar 26 2024 at 21:11):
If we involve a metaprogram here, note that we probably also want to have instances for Option.rec
, Sigma.rec
, Nat.rec
, Weekday.rec
, ...
Eric Wieser (Mar 31 2024 at 20:44):
#11780 adds a bunch of instances along these lines; is it a coincidence that @Amelia Livingston wanted this too, or did this thread spin out of LFTCM 2024?
Kevin Buzzard (Mar 31 2024 at 23:46):
I think it's a coincidence? Amelia asked me this question the day after Sebastian made his post and I pointed her to it.
Amelia Livingston (Apr 01 2024 at 00:32):
Yeah, just a coincidence. I don't really need #11780 or #11810 for anything - I'm not sure if the other members of my LFTCM group are planning on PR'ing their work - just thought I might as well PR, but I can close them if we should be using a metaprogram instead (edit: although I've just seen you've reviewed #11780, Eric, thank you!)
Last updated: May 02 2025 at 03:31 UTC