Zulip Chat Archive

Stream: mathlib4

Topic: nontrivial LinearOrder.compare


Johan Commelin (Mar 12 2024 at 16:01):

LinearOrder has a field compare. How can I find instances of LinearOrder that do not use the default argument compareOfLessAndEq?

Adam Topaz (Mar 12 2024 at 16:02):

git grep compareOfLessAndEq?

Adam Topaz (Mar 12 2024 at 16:03):

using that it seems that there are no such instances?

Adam Topaz (Mar 12 2024 at 16:03):

That wouldn't account for instances defined using angle brackets, of course.

Adam Topaz (Mar 12 2024 at 16:04):

oh sorry, I misunderstood

Adam Topaz (Mar 12 2024 at 16:04):

compare doesn't have a default value?

Adam Topaz (Mar 12 2024 at 16:05):

no, it does as you said. I'm an idiot. sorry for the noise

Adam Topaz (Mar 12 2024 at 16:06):

I guess you could write a metaprogram that looks at all such instances, picks out the compare projection, looks at its value, and use something like docs#Lean.Expr.getUsedConstants to check whether it uses compareOfLessandEq.

Johan Commelin (Mar 12 2024 at 16:12):

Yes, probably, if only I knew how to actually do that :sad:

Adam Topaz (Mar 12 2024 at 16:13):

I'm hacking something together now

Adam Topaz (Mar 12 2024 at 16:20):

import Mathlib
import Lean

open Lean

#eval show MetaM Unit from do
  let cs := ( getEnv).constants
  for (n,c) in cs.toList do
    if let .app (.const `LinearOrder _) _ := c.type then
      let some val := c.value? | continue
      unless val.getUsedConstantsAsSet.contains `compareOfLessAndEq do
        IO.println n

Adam Topaz (Mar 12 2024 at 16:20):

just an initial approximation. I didn't debug or think too carefully about whether this actually does what you want.

Adam Topaz (Mar 12 2024 at 16:21):

The result is

instPNatLinearOrder
instENatLinearOrder
String.instLinearOrderString
Int.instLinearOrderInt
instPNatLinearOrder._cstage1
instLinearOrderChar
Prop.linearOrder
Nat.linearOrder._cstage1
Int.instLinearOrderInt._cstage1
Real.linearOrder
NONote.instLinearOrderNONote._cstage1
NatOrdinal.linearOrder
NONote.instLinearOrderNONote
instENatLinearOrder._cstage1
Bool.linearOrder
Nat.linearOrder

Johan Commelin (Mar 12 2024 at 16:23):

Merci!

Adam Topaz (Mar 12 2024 at 16:24):

Well, I don't think that's the right answer.

Adam Topaz (Mar 12 2024 at 16:25):

For example, I guess Bool.linearOrder shouldn't be in there.

Adam Topaz (Mar 12 2024 at 16:26):

Oh, no maybe it's ok, since the ord instance for bool is defined as docs#instOrdBool

Johan Commelin (Mar 12 2024 at 16:30):

But if I look up Nat.linearOrder, then I don't see a custom compare field.

Adam Topaz (Mar 12 2024 at 16:31):

right, that comes from docs#instOrdNat which does actually use that lemma, so the code above doesn't quite do what you want.

Adam Topaz (Mar 12 2024 at 16:32):

Probably just need to unfold a bit... how about this?

#eval show MetaM Unit from do
  let cs := ( getEnv).constants
  for (n,c) in cs.toList do
    if let .app (.const `LinearOrder [u]) α := c.type then
      let some val := c.value? | continue
      let ordInst : Expr := .app (.app (.const `LinearOrder.toOrd [u]) α) val
      let compare : Expr := .app (.app (.const `Ord.compare [u]) α) ordInst
      unless ( Meta.whnf compare).getUsedConstantsAsSet.contains `compareOfLessAndEq do
        IO.println n

Johan Commelin (Mar 12 2024 at 16:36):

That prints some things, but I still don't find any nontrivial compare field

Adam Topaz (Mar 12 2024 at 16:37):

look at the one for bool

Adam Topaz (Mar 12 2024 at 16:38):

it comes from docs#instOrdBool

Adam Topaz (Mar 12 2024 at 16:38):

compare is part of Ord.

Adam Topaz (Mar 12 2024 at 16:40):

it seems that docs#instPNatLinearOrder might be another one?

Adam Topaz (Mar 12 2024 at 16:42):

for some reason I can't seem to go to that definition, but #check instPNatLinearOrder throws no errors.

Adam Topaz (Mar 12 2024 at 16:46):

Oh, I think it's because this is derived from the following line: https://github.com/leanprover-community/mathlib4/blob/bbf2206016a67e7d54812dcf76f830c704df378c/Mathlib/Data/PNat/Basic.lean#L24


Last updated: May 02 2025 at 03:31 UTC