Zulip Chat Archive

Stream: mathlib4

Topic: Incompatible subtraction on WithTop


Daniel Weber (Jun 10 2024 at 05:26):

docs#WithTop.linearOrderedAddCommGroupWithTop seems to be incompatible with docs#WithTop.instSub, which is quite confusing. Is this intentional, or should it be resolved somehow? How?
Example:

import Mathlib.Algebra.Order.Group.WithTop
import Mathlib.Algebra.Order.Sub.WithTop
import Mathlib.Algebra.Order.Group.Int

#eval @Sub.sub _ _ 0 ( : WithTop )

#eval @Sub.sub _ LinearOrderedAddCommGroupWithTop.toSub 0 ( : WithTop )

Johan Commelin (Jun 10 2024 at 05:54):

That seems bad. Afaict, it is not intentional.

Johan Commelin (Jun 10 2024 at 05:55):

I think the instSub should be changed into whatever the other instance needs.

Daniel Weber (Jun 10 2024 at 05:58):

I think instSub is intended mainly for the case that α is canonically ordered, perhaps that can just be added as an requirement to it?

Johan Commelin (Jun 10 2024 at 06:03):

Hmm, so you are saying that higher up in the hierarchy we need two genuinely different subtractions?

Daniel Weber (Jun 10 2024 at 06:04):

I'm not sure if it's actually used, but that seems to be the intention, yeah

Johan Commelin (Jun 10 2024 at 06:04):

Then I think instSub should become a defn, with a warning about this difference in its docstring

Brendan Seamas Murphy (Jun 10 2024 at 06:29):

Both instances seem suspicious to me. If I were to use WithTop (WithBot α) I would expect -⊤ = ⊥. Doesn't the existence of a LinearOrderedAddCommGroupWithTop make this impossible? I'm not sure how much of a headache refactoring this would cause for valuations

Johan Commelin (Jun 10 2024 at 06:34):

Good point, so we should make that instance a def as well. The whole API for valuations shouldn't need that instance. And we can use the general-instance-that-should-be-a-def to declare instances for special cases.

Daniel Weber (Jun 10 2024 at 06:37):

Brendan Seamas Murphy said:

Both instances seem suspicious to me. If I were to use WithTop (WithBot α) I would expect -⊤ = ⊥. Doesn't the existence of a LinearOrderedAddCommGroupWithTop make this impossible? I'm not sure how much of a headache refactoring this would cause for valuations

I'm not sure if this is problematic, as WithBot α isn't LinearOrderedAddCommGroup so this instance wouldn't apply.

Brendan Seamas Murphy (Jun 10 2024 at 06:40):

Oh, that's a fair point. I guess I'm still a little worried about having lemma statements floating around that involve two types of subtraction on the same type? It could get confusing to read. But maybe it's fine

Ruben Van de Velde (Jun 10 2024 at 07:27):

Do we need a WithTopBot?

Sébastien Gouëzel (Jun 10 2024 at 07:47):

I agree both instances are weird:

  • In LinearOrderedAddCommGroupWithTop, we have -⊤ : = ⊤, but if would seem more natural to me to make it 0, because it's another natural junk value, which has the advantage of being smaller.
  • In WithTop.instSub, if it happened that there was a bottom value then it would be more natural to set -⊤ equal to this bottom value.

Looking at the uses in Mathlib, I have the impression that the one in LinearOrderedAddCommGroupWithTop is maybe more natural, as it corresponds to the additivization of the usual 1/0 = 0 convention. So I also think that WithTop.instSub should become a definition to avoid the conflict (because we really want WithTop ℤ to be usable as a LinearOrderedAddCommGroupWithTop for valuation stuff).

Sébastien Gouëzel (Jun 10 2024 at 07:48):

Note that for EReal negation is defined by hand, probably to avoid this mess.

Daniel Weber (Jun 10 2024 at 07:49):

There is docs#WithTop.instNeg, it agrees with the LinearOrderedAddCommGroupWithTop

Floris van Doorn (Jun 10 2024 at 07:58):

Since both instances feel a bit unnatural, one option is to scope them both.

Brendan Seamas Murphy (Jun 10 2024 at 08:16):

Ruben Van de Velde said:

Do we need a WithTopBot?

I would be happy if we had that. We could even scope instances that behave like WithTop (WithBot α) vs WithBot (WithTop α)?

Yaël Dillies (Jun 10 2024 at 08:17):

It would be good if you un #xy ed by telling us what you want to do with the extended integers

Daniel Weber (Jun 10 2024 at 08:28):

I'm working with valuations, I want to construct an AddValuation K (WithTop ℤ) given (p : F) (hp : Prime p) [IsFractionRing F K]. I needed to prove

example : (0 : WithTop ) - 1 = -1 := by
  convert (neg_eq_zero_sub ..).symm -- introduces `WithTop.instSub = SubNegMonoid.toSub`, which is false.

(I know how to prove this, rfl works, but in my use case instead of 0 there's a complicated expression and I want to change the goal to proving it's equal to 0)

Sébastien Gouëzel (Jun 10 2024 at 08:33):

I'm trying to remove the instance WithTop.instSub now.

Richard Osborn (Jun 10 2024 at 14:39):

Shouldn't WithTop.sub really be WithTop.hsub?
def WithTop.hsub [Sub α] : WithTop α → α → WithTop α

Richard Osborn (Jun 10 2024 at 14:46):

I think if the subtraction is order preserving, there shouldn't be any problems? (Although, I fully expect an edge case which will prove this wrong :big_smile: )

Brendan Seamas Murphy (Jun 10 2024 at 18:30):

(deleted)

Brendan Seamas Murphy (Jun 10 2024 at 18:31):

(sorry!)

Sébastien Gouëzel (Jun 11 2024 at 05:49):

Fixed in #13715. I keep the two instances, but make sure that they apply in disjoint situations by requiring a bottom element instead of a zero for the first instance.

Johan Commelin (Jun 11 2024 at 07:44):

@Sébastien Gouëzel Thanks for doing this! :merge:

Daniel Weber (Jun 11 2024 at 10:53):

I need to prove some lemmas about LinearOrderedAddCommGroupWithTop, and one of them is top_ne_zero, but it collides with existing docs#top_ne_zero when unscoped, and LinearOrderedAddCommGroupWithTop.top_ne_zero is really long. What's a good alternative name?


Last updated: May 02 2025 at 03:31 UTC