Zulip Chat Archive

Stream: mathlib4

Topic: Can there be two instances of `Algebra R A`?


The-Anh Vu-Le (Dec 01 2024 at 22:36):

I am not well-versed in abstract algebra, so I want to ask if there can be two R-algebras with different structure maps?

I am asking to see if there are multiple instances, how does mathlib4 know which one to use?

Kevin Buzzard (Dec 01 2024 at 22:40):

It uses the last one that it saw. You can have two maps f g : R →+* S but you cannot use the [Algebra R S] typeclass for both of them; the typeclass inference system works under the assumption that there's at most one term of a typeclass type, and if there is more than one then code is likely to break in obscure ways -- this is called a "diamond" by the community here.

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

The notation R →+* S is notation for RingHom R S and this is a structure, not a class, so it's fine to have more than one term of that type.

Kevin Buzzard (Dec 01 2024 at 22:42):

Of course you can have [Algebra R A] and [Algebra R B], that's fine.

Kyle Miller (Dec 01 2024 at 23:03):

Correct me if I'm wrong, I think "diamonds" refer to the issue where depending on which way you go through the class hierarchy you might get different equal-but-not-defeq definitions.

I think the word "canonicity" is used for the assumption that there's at most one instance.

Kevin Buzzard (Dec 01 2024 at 23:10):

Oh I see, a diamond is more general than "two instances of one class", it's "two unequal paths through the typeclass system" (possibly of larger length than one)

Eric Wieser (Dec 01 2024 at 23:24):

I think "diamond" is also used for not-equal-at-all, such as the conflict caused by Algebra R A[X] and Algebra R[X] A[X] (both assuming Algebra R A) for Algebra R[X] R[X][X]

Johan Commelin (Dec 02 2024 at 15:25):

Kyle Miller said:

I think the word "canonicity" is used for the assumption that there's at most one instance.

Wait, are you claiming there is a definition of "canonical"? :rofl:

Johan Commelin (Dec 02 2024 at 15:26):

alias canonical := inferInstance

Kevin Buzzard (Dec 02 2024 at 16:01):

That's my working definition of canonical in lean!

Jon Eugster (Dec 11 2024 at 06:47):

a bit late, but just to note: If you do have a statement about two algebra structures, you could still formulate it as theorem xyz (h1 : Algebra R S) (h2 : Algebra R S) ... : ... := ... instead of using []. Then the user has to provide the algebras manually when using the theorem, but could still use inferInstance to canonically fill in one of the two arguments.

Eric Wieser (Dec 11 2024 at 06:52):

This is still going to be a nightmare because Lean will automatically pick between the two in the theorem statement in a probably-surprising way.


Last updated: May 02 2025 at 03:31 UTC