Zulip Chat Archive

Stream: batteries

Topic: Fin.Enum PR


François G. Dorais (Oct 25 2024 at 01:14):

I have a new WIP PR batteries#1007 for a replacement for Mathlib's FinEnum class. It's pretty big since there's a lot of machinery going on. It comes from my personal library, so it's not quite up to "standard form" yet. I'd like some feedback on how to bring it more "in line". I don't need much input on implementation details right now since I'm still tuning them.

This is needed because the Mathlib version is impractical. Instances rely on building a list of all elements of the type, which fits well with Fintype and Finset (which are both well designed) but is not practical for computation. The proposed alternative only builds a bijection between the type and Fin size, which can scale way beyond memory limitations.

Violeta Hernández (Oct 25 2024 at 01:27):

I don't really get what makes Fin.Enum distinct from FinEnum. Aren't both just data-carrying bijections between a type and some Fin n?

François G. Dorais (Oct 25 2024 at 01:30):

The types aren't very different (except that Equiv doesn't exist in Batteries). The instances are very different though. All the Mathlib instances require building an actual list of all elements of the type, which is prohibitive for Char and impossible for Char -> Bool. This implementation avoids that by just building the bijection and not the list.

François G. Dorais (Oct 25 2024 at 01:32):

In other words, all the hard work is in Fin.Coding not in Fin.Enum.

François G. Dorais (Oct 25 2024 at 01:35):

(Which reminds me I need to add an instance for Char...)

Eric Wieser (Oct 25 2024 at 01:41):

I like the new definition, but I think I'd prefer to see mathlib refactored to use it first, then upstream it later

Eric Wieser (Oct 25 2024 at 01:41):

In particular, having Equiv in mathlib feels like it makes a lot of constructions easier, especially with things like docs#finSumFinEquiv etc

Mario Carneiro (Oct 25 2024 at 01:41):

I think we should move Equiv to batteries actually

Mario Carneiro (Oct 25 2024 at 01:42):

It's a very simple and straightforward interface and it doesn't have poor computational behavior

Eric Wieser (Oct 25 2024 at 01:42):

But you can't pick the simp-normal form without having DFunlike too

Eric Wieser (Oct 25 2024 at 01:42):

Or at least, you'd end up with a different simp-normal form to mathlib

Mario Carneiro (Oct 25 2024 at 01:43):

I am okay with that

Mario Carneiro (Oct 25 2024 at 01:43):

I'm not sure it was a good idea to try to unify those across libraries in the first place

Mario Carneiro (Oct 25 2024 at 01:44):

somehow I suspect the call is going to be taken out of our hands anyway...

Eric Wieser (Oct 25 2024 at 01:45):

Mario Carneiro said:

I'm not sure it was a good idea to try to unify those across libraries in the first place

Perhaps, but if downstream libraries want to disagree with upstream ones, there ought to be a mechanism to unregister the simp lemmas

Eric Wieser (Oct 25 2024 at 01:45):

Otherwise it's easy to create loops

Kim Morrison (Oct 25 2024 at 01:48):

So far I haven't felt tempted to move Equiv up to Lean. :-) But I do encourage moving it to Batteries, particularly this PR seems be badly want it.

Mario Carneiro (Oct 25 2024 at 01:50):

Eric Wieser said:

Mario Carneiro said:

I'm not sure it was a good idea to try to unify those across libraries in the first place

Perhaps, but if downstream libraries want to disagree with upstream ones, there ought to be a mechanism to unregister the simp lemmas

that would indeed be nice

François G. Dorais (Oct 25 2024 at 01:54):

I'm not at all opposed to moving Equiv to Batteries and I am happy to refactor this PR after that's done.

Eric Wieser (Oct 25 2024 at 01:55):

Are you opposed to making the Enum refactor in mathlib first, so that we can leverage all the existing equivs there?

Eric Wieser (Oct 25 2024 at 01:56):

I think most of your Coding.lean file already exists scattered throughout mathlib

François G. Dorais (Oct 25 2024 at 02:00):

Can you find them all? I gave up after a few steps down the rabbit hole :-) I figured we could just alias or deprecate them when they are discovered.

François G. Dorais (Oct 25 2024 at 02:03):

It's also easier to maintain a coherent library of these standard codings.

Eric Wieser (Oct 25 2024 at 02:04):

I would hope that Loogle can find them fairly quickly

François G. Dorais (Oct 25 2024 at 02:05):

Didn't help me much but you are surely a better loogler than I am.

François G. Dorais (Oct 25 2024 at 02:06):

This is the kind of help I need!

Eric Wieser (Oct 25 2024 at 02:07):

@loogle Fin _ ⊕ Fin _ ≃ Fin _

loogle (Oct 25 2024 at 02:07):

:search: finSumFinEquiv, finSumFinEquiv_apply_left, and 4 more

Eric Wieser (Oct 25 2024 at 02:07):

(deleted)

Eric Wieser (Oct 25 2024 at 02:08):

@loogle |- _ ≃ Fin _

loogle (Oct 25 2024 at 02:08):

:search: finCongr, Fintype.equivFin, and 17 more

Eric Wieser (Oct 25 2024 at 02:08):

It's inconsistent whether the Fin is on the LHS or RHS

François G. Dorais (Oct 25 2024 at 02:13):

The Sum stuff is relevant but kind of trivial, we might as well alias those with Fin.Coding functions. Relying on Fintype and BigOperators is a no go. That's the rabbit hole!

François G. Dorais (Oct 25 2024 at 02:31):

Eric Wieser said:

Are you opposed to making the Enum refactor in mathlib first, so that we can leverage all the existing equivs there?

I am not opposed. I just don't think there is much to gain. Also, to a much lesser weight, I personally need it in Batteries sooner than later for at least two of my projects :-)

François G. Dorais (Oct 25 2024 at 02:49):

PS: The Fin.Enum name was chosen so that Mathlib could gently deprecate FinEnum. And also to allow future variations like Nat.Enum with a compatible API.

Eric Wieser (Oct 25 2024 at 03:06):

That's docs#Encodable I think

François G. Dorais (Oct 25 2024 at 03:09):

Yes, that's what I'm thinking about for Nat.Enum. We should keep that existing API in the back of our minds while thinking about the proposed API from this PR.

Kyle Miller (Oct 25 2024 at 03:14):

By the way, docs#Finite is essentially Prop version of Fin.Enum. It seems like once Fin.Enum exists it would be a reasonable next step to update that definition.

Kyle Miller (Oct 25 2024 at 03:18):

François G. Dorais said:

This is needed because the Mathlib version is impractical

I just want to mention that we know this too, and it's come up a few times in discussions — glad to see motion to get this done!

(I've also written my own Fin.Enum occasionally for projects, so looking forward to a well-developed one.)

François G. Dorais (Oct 28 2024 at 23:59):

Eric Wieser said:

Are you opposed to making the Enum refactor in mathlib first, so that we can leverage all the existing equivs there?

How about this process:

  1. Merge the Batteries PR with Fin.Coding and Fin.Enum. Nothing there clashes with Mathlib.
  2. Figure out how to harmonize Mathlib and Fin.Coding. (Leaving Fin.Enum alone, just updating Fin.Coding and Mathlib defs to match.)
  3. Gently replace Mathlib's FinEnum with Batteries's Fin.Enum, reverting back to step 2 if that fails.

Bhavik Mehta (Nov 11 2024 at 20:45):

Flagging PR #15647 by @Tom Kranz which adds 86 lines to mathlib's FinEnum and makes a new 120-line long file with definitions and lemmas about it.

François G. Dorais (Nov 12 2024 at 06:24):

At a quick glance, this contribution focuses on implementing induction on cardinality for FinEnum. This is a fine and useful addition to Mathlib. However, the recursion principle has little computational value so there is little interest in having this in Batteries.

James Gallicchio (Nov 22 2024 at 21:01):

Can we also get a (proxy_equiv% based) deriving impl for Fin.Enum in this PR? I think that would make it a complete replacement for the version I have been using :P

François G. Dorais (Nov 25 2024 at 08:10):

@James Gallicchio Since proxy_equiv% is in Mathlib, the derive handler would have to be there. Though it could be upstreamed in the near future since there seems to be universal agreement for upstreaming Equiv to batteries.


Last updated: May 02 2025 at 03:31 UTC