Zulip Chat Archive

Stream: new members

Topic: Set.add not resolving


Javier Lopez-Contreras (Dec 16 2024 at 11:46):

Hello!

I got an error that I cornered into this very minimal code. Why is Set.add not being used to infer Add (Set M)?

Thanks!

import Mathlib

variable {R : Type*} [Semiring R]

variable {M : Type*} [AddCommMonoid M] [Module R M]

#synth Add M

#check Set.add (α := M)

set_option diagnostics true in
#synth Add (Set M)

Yaël Dillies (Dec 16 2024 at 11:46):

You don't have open scoped Pointwise

Javier Lopez-Contreras (Dec 16 2024 at 11:47):

That was quick :)

Javier Lopez-Contreras (Dec 16 2024 at 11:47):

Thanks Yaël <3

Kevin Buzzard (Dec 16 2024 at 12:36):

The clue is in the source code (jump to definition with Set.add).

/-- The pointwise multiplication of sets `s * t` and `t` is defined as `{x * y | x ∈ s, y ∈ t}` in
locale `Pointwise`. -/
@[to_additive
      "The pointwise addition of sets `s + t` is defined as `{x + y | x ∈ s, y ∈ t}` in locale
      `Pointwise`."]
protected def mul : Mul (Set α) :=
  image2 (· * ·)

scoped[Pointwise] attribute [instance] Set.mul Set.add

the last line means "don't make this a global instance, only make it an instance when open scoped Pointwise is typed"

Edward van de Meent (Dec 16 2024 at 14:24):

i don't think you need scoped for this though?

Edward van de Meent (Dec 16 2024 at 14:24):

as in, open Pointwise works fine too

Yaël Dillies (Dec 16 2024 at 14:30):

Yes but it's good practice to differentiate between scopes and namespaces

Edward van de Meent (Dec 16 2024 at 14:39):

could you elaborate? i'm not sure what you mean by that...

Kevin Buzzard (Dec 16 2024 at 22:41):

I think open Pointwise opens both the scope (i.e. notation) and the namespace. I am usually careful to only open the scope if I don't need the namespace but probably others don't worry so much about this.


Last updated: May 02 2025 at 03:31 UTC