Zulip Chat Archive

Stream: mathlib4

Topic: Packaging conditions as type class possible? Other solutions


Stephan Maier (May 23 2024 at 11:34):

Hi, I find that lists of variables can get rather long. Imagine trying PL-topology without going straight into R^n. Is there a way to simplify such variable lists? Here is an easy and limited example from purely algebraic "PL-topology":

/-- These are the typs that are needed -/
class AlgebraicPLSpace (π•œ : Type u) (V : Type v) (P : Type w) where
  is_StrictOrderedCommRing : StrictOrderedCommRing π•œ
  is_AddCommGroup : AddCommGroup V
  is_Module: Module π•œ V
  is_NoZeroSMulDivisors : NoZeroSMulDivisors π•œ V
  is_AddTorsor : AddTorsor V P

/-- OK, this here does noit compile. Trying tu use Coe's of various kinds does not help: -/
example [AlgebraicPLSpace π•œ V P] (x y : P) := affineSegment π•œ x y

/-- This would be the way out. But I need to repeat this too often! -/
variable (π•œ : Type u) [OrderedCommRing π•œ]
variable (V : Type v) [AddCommGroup V] [Module π•œ V] [NoZeroSMulDivisors π•œ V]
variable (P : Type w) [AddTorsor V P]

/-- Or would I write a mcro that expands as follows: -/
-- variables [AlgebraicPLSpace π•œ V P] ---- expandes to ----> the variable list above?

Any thoughts. And sorry, if this has been asked before.
Cheers, Stephan

Calle SΓΆnne (May 23 2024 at 11:52):

I have not been able to test this, since I don't know exactly what to import to make this compile. But have you tried adding:

instance [h : AlgebraicPLSpace π•œ V P] : StrictOrderedCommRing π•œ := h.is_StrictOrderedCommRing
instance [h : AlgebraicPLSpace π•œ V P] : AddCommGroup V := h.is_AddCommGroup
(and so on)

before example [AlgebraicPLSpace π•œ V P] (x y : P) := affineSegment π•œ x y? This should make it so that lean can deduce StrictOrderedCommRing from a given instance of AlgebraicPLSpace.

Anne Baanen (May 23 2024 at 12:17):

I'm afraid it is not technically possible to follow Calle's approach or Stephan's first approach: the problem here is that to find an instance of e.g. StrictOrderedCommRing π•œ from an instance of AlgebraicPLSpace π•œ _ _, the problem is in figuring out what the V and P are supposed to be that fill in the _s. In principle that could be any type. So the algorithm doesn't like these instances.

Anne Baanen (May 23 2024 at 12:20):

There have been various proposals for a macro that would expand [AlgebraicPLSpace π•œ V P] into a full list of instances, but they have been less popular for various reasons. Mostly to do with how to deal with variables that have already been declared. That is, questions like: assume we have a line above variable [Field π•œ], should [AlgebraicPLSpace π•œ V P] be smart enough to expand into [OrderedField π•œ] instead of [OrderedCommRing π•œ]?

Anne Baanen (May 23 2024 at 12:21):

For now, the closest we have is variable? [AlgebraicPLSpace π•œ V P] which return the full variable declaration as a suggestion. At least that should save you some typing, even if reading the full declaration is still extremely long.

Calle SΓΆnne (May 23 2024 at 12:42):

Oh I see, sorry about that!

Stephan Maier (May 23 2024 at 17:25):

Anne Baanen said:

I'm afraid it is not technically possible to follow Calle's approach or Stephan's first approach: the problem here is that to find an instance of e.g. StrictOrderedCommRing π•œ from an instance of AlgebraicPLSpace π•œ _ _, the problem is in figuring out what the V and P are supposed to be that fill in the _s. In principle that could be any type. So the algorithm doesn't like these instances.

Thanks a lot. I suspected as much. I did try what Calle proposed. Never mind. I appreciate that interferences are a chelenge when generating code. So I rest my case. Thanks for the hint with variable?.


Last updated: May 02 2025 at 03:31 UTC