Zulip Chat Archive
Stream: Is there code for X?
Topic: Finset argmax
Daniel Weber (Sep 22 2024 at 10:34):
I have a nonempty Finset (Finset (Fin 31))
, and I want to get the largest set in it (I don't care how ties are broken, but I want it to be computable). What's the best way to do that? I could map them to lists using Finset.sort
, give them the shortlex order and then take the maximum, for instance, but that seems really unwieldy
Yaël Dillies (Sep 22 2024 at 10:55):
What about docs#List.argmax of Finset.sort
?
Yaël Dillies (Sep 22 2024 at 10:56):
What about docs#List.argmax of Finset.sort
?
Yaël Dillies (Sep 22 2024 at 10:56):
If you don't care about safety, you can instead unquot the finset and feed it to argmax
Daniel Weber (Sep 22 2024 at 10:58):
Yaël Dillies said:
What about docs#List.argmax of
Finset.sort
?
Hmm, it requires a total order on Finset (Fin 31)
, but it is better
Daniel Weber (Sep 22 2024 at 10:58):
Yaël Dillies said:
If you don't care about safety, you can instead unquot the finset and feed it to
argmax
I need safety, sadly
Violeta Hernández (Sep 22 2024 at 12:42):
In order for this operation to be computable, you need a definite way to break ties. I think your approach of using the shortlex order is probably the best one.
Violeta Hernández (Sep 22 2024 at 12:47):
I've ran into this issue of "needing literally any computable linear order" before. It'd be nice if there were some tactic to automatically generate one.
Daniel Weber (Sep 22 2024 at 13:03):
Perhaps there could be an instance
Violeta Hernández (Sep 22 2024 at 13:37):
But there's already a partial order defined on Finset (Fin 31)
, and the linear order wouldn't be compatible with it
Daniel Weber (Sep 22 2024 at 13:39):
It doesn't have to extend LE
, it can just be
class HasLinearOrder (α : Type*) where
r : α -> α -> Prop
linear: IsLinearOrder α r
Violeta Hernández (Sep 22 2024 at 13:44):
That'd be nice. You can then have a tactic for deriving this relation for simple inductive types, and combine that with some other mechanism to turn it into a LinearOrder
proper.
Kevin Buzzard (Sep 22 2024 at 15:24):
You'd get a diamond
Daniel Weber (Sep 22 2024 at 15:26):
I settled on ((p.image (fun a ↦ toLex (a.card, Finset.Colex.toColex a))).max' sorry).2.ofColex
, I think that's good enough
Eric Wieser (Sep 22 2024 at 16:26):
Violeta Hernández said:
I've ran into this issue of "needing literally any computable linear order" before. It'd be nice if there were some tactic to automatically generate one.
I claim we should use the Ord
typeclass for this (or rather, a strengthened version of it that says ≤ is weaker than it)
Eric Wieser (Sep 22 2024 at 16:27):
This would also solve the Repr Multiset
problem computably
Eric Wieser (Sep 22 2024 at 16:28):
(deleted)
Last updated: May 02 2025 at 03:31 UTC