Zulip Chat Archive

Stream: mathlib4

Topic: Decidable PythagoreanTriple


Markus Schmaus (Oct 11 2023 at 08:55):

I noticed that in Mathlib PythagoreanTriple isn't an instance of Decidable, even though it trivially is decidable.

I was able proof this like so:

import Mathlib

instance : Decidable (PythagoreanTriple x y z) := by
  unfold PythagoreanTriple
  infer_instance

#eval PythagoreanTriple  3 4 5

I have two questions:

  • Is this a good way to make such a Prop an instance of Decidable?
  • Should I create a PR for code like this, or should I just keep it in my own code base?

Eric Wieser (Oct 11 2023 at 09:12):

That instance looks totally reasonable to me

Yaël Dillies (Oct 11 2023 at 09:13):

Yes and yes. That's a reasonable addition to mathlib

Eric Wieser (Oct 11 2023 at 09:13):

You could golf it slightly by using docs#Int.instDecidableEqInt directly

Scott Morrison (Oct 11 2023 at 09:25):

inferInstanceAs is useful for these sorts of instances. Doing it this way is quite readable as the the reader can see quickly why it is decidable. The instance name is needlessly obscure.

Markus Schmaus (Oct 11 2023 at 10:33):

@Scott Morrison Is this what you mean?

import Mathlib
instance : Decidable (PythagoreanTriple x y z) := inferInstanceAs (Decidable (x * x + y * y = z * z))

Scott Morrison (Oct 11 2023 at 10:34):

Something like that. Is is actually defined using x * x rather than x^2? Who knew? :woman_shrugging:

Eric Wieser (Oct 11 2023 at 10:40):

I'd recommend

instance : Decidable (PythagoreanTriple x y z) := inferInstanceAs <| Decidable (_ = _)

as that tells the reader all they need to know: it's decidable because it's an equality

Eric Rodriguez (Oct 11 2023 at 11:04):

Scott Morrison said:

Something like that. Is is actually defined using x * x rather than x^2? Who knew? :woman_shrugging:

A lot of things do that to minimise typeclass arguments, e.g. Even is x+x. Oddly enough (hah) Odd is (iirc) 2 * x + 1

Eric Rodriguez (Oct 11 2023 at 11:04):

It's a bit of an odd practice

Patrick Massot (Oct 11 2023 at 12:36):

Eric Rodriguez said:

A lot of things do that to minimise typeclass arguments, e.g. Even is x+x. Oddly enough (hah) Odd is (iirc) 2 * x + 1

Is there any actual use of this definition of Even? It is rather annoying when teaching.

Eric Wieser (Oct 11 2023 at 13:13):

I guess it lets you talk about "even" elements of a free additive monoid

Eric Wieser (Oct 11 2023 at 13:14):

It probably also makes the to_additive stuff with IsSquare slightly easier than using smul / pow

Eric Rodriguez (Oct 11 2023 at 13:28):

things like docs#Even.tsub

Eric Rodriguez (Oct 11 2023 at 13:28):

because we don't have that many mixins for the CanonicallyOrderedNonsense

Eric Rodriguez (Oct 11 2023 at 13:28):

as it points out, we can't have Odd.tsub because of this:

-- `Odd.tsub` requires `CanonicallyLinearOrderedSemiring`, which we don't have

Eric Rodriguez (Oct 11 2023 at 13:32):

(although I'm not sure how that would actually hold as of course 1 - 3 = 1 - 4 = 0, so not sure what the statement would be!)

Ruben Van de Velde (Oct 11 2023 at 13:32):

Would it say Odd a → Odd b → Even (a - b)?

Ruben Van de Velde (Oct 11 2023 at 13:33):

That seems vacuously true in the bad cases

Yaël Dillies (Oct 11 2023 at 13:53):

I was considering adding it, but I would have needed a typeclass that only Nat would satisfy and we already have the lemma on Nat

Yaël Dillies (Oct 11 2023 at 13:54):

So I was thinking we could just rename Nat.Odd.sub to Odd.tsub and then everybody is happy.

Eric Rodriguez (Oct 11 2023 at 14:27):

I mean, I'm personally happy to have Odd just required Add and One, but I fear that those that teach will have headaches

Damiano Testa (Oct 11 2023 at 16:07):

I was probably the one responsible for defining Even as a doubled sum and it was essentially what Eric said: I wanted to unify Even and Square using to_additive. I had not thought of the teaching implications of this design choice. Let me find the relevant thread!

Damiano Testa (Oct 11 2023 at 16:11):

The mathlib3 PR and the relevant Zulip thread.

Martin Dvořák (Oct 21 2023 at 13:02):

Eric Rodriguez said:

because we don't have that many mixins for the CanonicallyOrderedNonsense

What are mixins?

Alex J. Best (Oct 21 2023 at 13:13):

Things like docs#NoTopOrder (typeclasses that take others as parameters), they don't live "in" the heirachy or all blah blahs, so that you can add them as side assumptions, and mix them together


Last updated: Dec 20 2023 at 11:08 UTC