Zulip Chat Archive

Stream: mathlib4

Topic: Ord ɑ from LinearOrder ɑ?


Thomas Murrills (Mar 12 2023 at 23:20):

Should we have

instance [LinearOrder α] : Ord α where
  compare a b := compareOfLessAndEq a b

somewhere in mathlib4? if so where?

Thomas Murrills (Mar 12 2023 at 23:24):

(Context: existing minimizing functions on Arrays rely on Ord, and since I'm writing more such functions, I wanted to stay consistent with that. But analogous existing minimizing functions for lists rely on LinearOrder, so I figured there should be a way to move between them in at least one direction. Then we can have theorems about these functions on arrays which depend on LinearOrder.)

Gabriel Ebner (Mar 13 2023 at 15:57):

We should definitely not have an instance because that creates diamonds, but a def would be good.

Eric Wieser (Mar 13 2023 at 16:02):

What types would it create diamonds on?

Eric Wieser (Mar 13 2023 at 16:05):

So far the only existing instance that isn't implemented as compareOfLessAndEq seems to be Fin, and that could easily be changed

Gabriel Ebner (Mar 13 2023 at 16:07):

Indeed, the extra Ord class isn't really used (yet). Notably, Bool has a different implementation as well.

Eric Wieser (Mar 13 2023 at 16:07):

I guess I can see the appeal for String or Lex ..., where likely it can be computed more efficiently

Eric Wieser (Mar 13 2023 at 16:08):

So in the long run a compare field of LinearOrder probably makes sense

Eric Wieser (Mar 13 2023 at 16:08):

But in the short term just making the above instance global (and adjusting the instances forFin and Bool) is probably more useful than doing nothing

Eric Wieser (Mar 13 2023 at 16:08):

If we have no theorems about Ord then diamonds don't matter anyway

Thomas Murrills (Mar 13 2023 at 21:36):

Hmm, would it be any harm to jump straight to the long run and have LinearOrder extend Ord with the above as a default field value? (afaict structure instances prefer typeclass synthesis of substructures to optparams for their field values, so I don’t think this would interfere with any Ord instances that were already defined, and so any diamonds should be defeq…)

Eric Wieser (Mar 13 2023 at 21:54):

I guess as a default it probably won't interfere with porting

Thomas Murrills (Mar 13 2023 at 22:15):

Now that I think about it, though, if we’re going to add compare to LinearOrder it should be lawful—namely we should add fields compare_lt_iff_lt and compare_eq_iff_eq (we can prove the would-be remaining field as a theorem)

Thomas Murrills (Mar 13 2023 at 22:23):

(These ofc can also have default proofs so that it doesn’t affect porting)

Eric Wieser (Mar 13 2023 at 22:29):

Or you could just ask the user to prove that compare a b = compareOfLessAndEq a b to save a field

Thomas Murrills (Mar 13 2023 at 22:30):

true! fields are at a premium, I gather?

Eric Wieser (Mar 13 2023 at 22:34):

I'd probably go for the single-field approach until we actually know what specialized instances to expect and which ones will be annoying to prove. Right now, almost every instance will just have rfl in that field, whereas proving your fields is very marginally harder.

Thomas Murrills (Mar 14 2023 at 00:12):

Exploring this on !4#2858 (have yet to supply the lawfulness field for Fin)

Thomas Murrills (Mar 14 2023 at 10:24):

!4#2858 is green; I've tentatively put it out for review, in case we decide to go with it.


Last updated: Dec 20 2023 at 11:08 UTC