Zulip Chat Archive

Stream: mathlib4

Topic: Using `inferInstance` as partial fills in instance defs


Yakov Pechersky (Nov 07 2022 at 03:57):

Consider:

instance : Group αˣ :=
  { (inferInstance : MulOneClass αˣ) with
    one := 1,
    mul_assoc := fun _ _ _ => ext <| mul_assoc _ _ _,
    inv := Inv.inv, mul_left_inv := fun u => ext u.inv_val }

In mathlib3, we often refer to explicit instance names when doing such partial fills. In lean4, the anonymous instance names are pretty different, and have to be guessed or retrieved via a #synth. Is there a downside (defeq issues?) to using inferInstance as I have above?

Scott Morrison (Nov 07 2022 at 04:01):

I think it's okay. If anything it's more informative to have the type here than the term! Hopefully also without old_structure_cmd we may be able to omit more of these anyway...?

Alex J. Best (Nov 07 2022 at 11:38):

Side note: you can use docs4#inferInstanceAs here instead

Gabriel Ebner (Nov 07 2022 at 18:19):

And you can also use the __ := shorthand syntax:

instance : Group αˣ where
  __ := inferInstanceAs (MulOneClass αˣ)
  one := 1
  -- ...

Patrick Massot (Nov 07 2022 at 18:22):

Is there any documentation for this syntax?

Gabriel Ebner (Nov 07 2022 at 18:24):

Of course, but probably not in the place you'd be looking for. https://github.com/leanprover-community/mathlib4/blob/df8292b7dbdb00bfa90d626ba7c83af2eff1ce37/Mathlib/Tactic/Spread.lean#L14-L24

Patrick Massot (Nov 07 2022 at 18:25):

So this is what was denoted by .. in Lean 3?

Gabriel Ebner (Nov 07 2022 at 18:26):

Yeah, it simulates that.

James Gallicchio (Nov 07 2022 at 22:19):

(Is there a reason to use this instead of { e with one := 1 }?)

Scott Morrison (Nov 07 2022 at 22:21):

@James Gallicchio, I think the point of this thread is when you don't have a term such as e readily at hand, and want typeclass search to find it for you.

Mario Carneiro (Nov 07 2022 at 22:24):

although that doesn't quite answer the question since inferInstanceAs (MulOneClass αˣ) is exactly such an e

James Gallicchio (Nov 07 2022 at 22:25):

Can you have multiple __ operators in a single structure? i.e. is it a generalization of the with syntax?

Mario Carneiro (Nov 07 2022 at 22:25):

I believe the answer is that when Mathlib.Tactic.Spread was written structures could only have one explicit source { e with ... }

Mario Carneiro (Nov 07 2022 at 22:26):

this restriction has since been lifted, so it's possible that it is not needed anymore except that the where syntax shown above does not accept explicit sources

James Gallicchio (Nov 07 2022 at 22:30):

I wonder if we can merge these syntaxes. I wouldn't mind the { e with ...} syntax changing, honestly, since it doesn't really suggest that multiple sources can be used (which I think the __ := syntax actually does)

Gabriel Ebner (Nov 08 2022 at 04:34):

Yes, you can have multiple __ := fields.

Gabriel Ebner (Nov 08 2022 at 04:35):

{ __ := a, __ := b } is desugared into { a, b with }. The advantage of __ := is that you can also use it in where-blocks.

Gabriel Ebner (Nov 08 2022 at 04:36):

(Side note: before multiple explicit sources were allowed, the expansion of __ := was a lot more complicated.)


Last updated: Dec 20 2023 at 11:08 UTC