Zulip Chat Archive

Stream: lean4

Topic: Does Lean.Name.cmp do the correct thing?


Moritz Firsching (Aug 07 2024 at 11:37):

It seems like min_imports does not sort correctly, caused by the behavior of Name.cmp, at least if it does not do the same thing you'd get when converting to a String first.
The issue to fix it for min_imports is here: https://github.com/leanprover-community/import-graph/pull/18.
@Jon Eugster suggested to bring it up and potentially change the behavior of Name.cmp?

Moritz Firsching (Aug 07 2024 at 11:37):

ah, I see just now there is also https://github.com/leanprover/lean4/issues/4944

Damiano Testa (Aug 07 2024 at 11:59):

The sorting that Name.cmp uses is good if you are trying to figure out, for instance, how many folders you have to cross to get to a file, so I am not so sure that Name.cmp should just look at a string.

Eric Wieser (Aug 07 2024 at 12:00):

I'd expect it to do a lexicographic comparison on the lists of strings it consists of

Damiano Testa (Aug 07 2024 at 12:03):

Well, it is currently doing the "graded" lex comparison, right?

Damiano Testa (Aug 07 2024 at 12:03):

As in, depth in the hierarchy and, within a given depth, lex.

Eric Wieser (Aug 07 2024 at 12:48):

Right, and I agree with Moritz that this is not the thing you want when sorting imports

Jon Eugster (Aug 07 2024 at 12:51):

Eric Wieser said:

Right, and I agree with Moritz that this is not the thing you want when sorting imports

That for sure! My question was whether this graded sorting is the right "default" sorting on Name

Damiano Testa (Aug 07 2024 at 12:53):

Ok, I think that we all agree that imports should be sorted as a string, lexicographically, treating the . as a character. I was mostly arguing that the Name.cmp is not necessarily undesirable as a sorting for Name.

Eric Wieser (Aug 07 2024 at 12:56):

Damiano Testa said:

treating the . as a character.

I don't agree, that would make `Foo.«A.B» and `Foo.A.B equal. . needs to be treated specially

Kyle Miller (Aug 07 2024 at 12:56):

What you could do is sort the result of docs#Lean.Name.components, taking advantage of lex order for List

Damiano Testa (Aug 07 2024 at 12:57):

Note also that docs#getAllModulesSorted, which is the function that mk_all uses to sort imports in Mathlib.lean does indeed sort the strings, not the names (even if the array of strings is called names in the code).

Eric Wieser (Aug 07 2024 at 12:58):

Kyle Miller said:

What you could do is sort the result of docs#Lean.Name.components

I was expecting this to return docs#Lean.NamePart s

Kyle Miller (Aug 07 2024 at 13:18):

At least . comes before letters in ASCII, so sorting Name.toString should mostly work, though it could do some odd things with names with french-quoted dots. (@Eric Wieser, Name.toString of Foo.«A.B» is "Foo.«A.B»", so it would be sorted after Foo.A.B)

Jon Eugster (Aug 07 2024 at 13:39):

For the imports (← getEnv).minimalRequiredModules.qsort (·.toString < ·.toString) instead of .qsort Name.lt should do what we want in that case. (see commit)

Thanks for the comments

Eric Wieser (Feb 16 2025 at 16:53):

This came as a surprise to me:

import Lean

#guard Lean.Name.cmp `A `ZZZ == .lt
#guard Lean.Name.cmp `A `ZZZ.Z == .lt
#guard Lean.Name.cmp `A.A `ZZZ == .gt -- wat
#guard Lean.Name.cmp `A.A `ZZZ.Z == .lt

docs#Lean.Name.cmp is undocumented, but this is not the order I would expect.

(docs#Lean.Name.lt is consistent with cmp, but I mention it here for future Zulip searches)

Eric Wieser (Feb 16 2025 at 16:53):

(cc @Damiano Testa for #20908)

Damiano Testa (Feb 16 2025 at 22:29):

I think that something similar was discussed here as well.

Eric Wieser (Feb 16 2025 at 22:58):

Thanks, I've merged the threads


Last updated: May 02 2025 at 03:31 UTC