Zulip Chat Archive

Stream: mathlib4

Topic: Cardinal NatCast


Violeta Hernández (Sep 01 2024 at 20:38):

Currently, the cast ℕ → Cardinal is defined as #(Fin n). I get this is really nice from a mathematical perspective, but is it really useful?

Yury G. Kudryashov (Sep 01 2024 at 20:41):

What do you suggest?

Violeta Hernández (Sep 01 2024 at 20:41):

Well, I'm wondering why not just use the default natCast implementation

Violeta Hernández (Sep 01 2024 at 20:42):

That way ↑0 = 0, ↑1 = 1, and ↑(n + 1) = ↑n + 1 are all def-eq

Violeta Hernández (Sep 01 2024 at 20:42):

#(Fin n) = n can be a simp lemma rather than a definitional equality

Violeta Hernández (Sep 01 2024 at 20:43):

This is what Ordinal does btw, even though we very well could have had ↑n = @type (Fin n) (· < ·)

Yury G. Kudryashov (Sep 01 2024 at 20:44):

See #6384

Yury G. Kudryashov (Sep 01 2024 at 20:44):

Authored by @Sébastien Gouëzel

Violeta Hernández (Sep 01 2024 at 20:46):

Hm yeah, I'm not sure I agree with that rationale

Violeta Hernández (Sep 01 2024 at 20:50):

I'd argue the main reason to choose one implementation of something like NatCast over another is the def-eqs. After all, all implementations are supposed to be prop-eq anyways.

Violeta Hernández (Sep 01 2024 at 20:50):

#(Fin n) gives us nothing of the sort. The fact Zero and One had to be redefined from what I'd argue are pretty clear canonical definitions #PEmpty and #PUnit just illustrates that further

Yury G. Kudryashov (Sep 01 2024 at 21:13):

Let's wait for Sébastien.

Sébastien Gouëzel (Sep 02 2024 at 07:34):

I don't really remember the details, but for me the main point is that it's a good mathematical definition. The defeq ↑(n + 1) = ↑n + 1 that you would get from the other definition is in my opinion not something important, for computations you have lemmas, while having a definition that makes sense is much more enlightening. I think it would be a good idea to do the same for ordinals, by the way, I just didn't have time to do it then!

Violeta Hernández (Sep 02 2024 at 09:28):

I'd oppose this on ordinals even more. Def-eqs work even nicer on them, since we can define succ x as x + 1. So for instance, succ 2 = 3 is currently def-eq.

Violeta Hernández (Sep 02 2024 at 09:30):

I don't think either definition is any more illuminating than the other, they both satisfy the same theorems. But one is slightly more annoying to work with.

Violeta Hernández (Sep 02 2024 at 09:36):

Here's the example that prompted me to create this thread. Consider the statement #{a, b, c, d} ≤ 4. By using Cardinal.mk_insert_le repeatedly, you can show #{a, b, c, d} ≤ 1 + 1 + 1 + 1. But because of the nonstandard NatCast, to get from 1 + 1 + 1 + 1 to 4 you need to either use Nat.cast_add_one a couple of times or insert norm_cast somewhere.

Violeta Hernández (Sep 02 2024 at 09:40):

Hypothetically with 4 def-eq to #(Fin n) you could call Cardinal.mk_le and establish an injection between your set and Fin 4, or something of the sort, but all that seems strictly more convoluted.

Sébastien Gouëzel (Sep 02 2024 at 09:52):

Having #(Fin n) = n in a defeq way seems to me much more important that defeqs for cardinal arithmetic (which should be handled by norm_num or suitable simp lemmas, definitely not rely on defeq coincidence): Fin n is used in Mathlib as the reference set of cardinality n, so we should make sure that this is reflected in the design choices.

Violeta Hernández (Sep 02 2024 at 10:06):

I wouldn't call it "defeq coincidence", the default NatCast implementation is pretty deliberate in what def-eqs you get. 0 = ↑0, 1 = ↑1, and ↑(n + 1) = ↑n + 1.

Violeta Hernández (Sep 02 2024 at 10:07):

If the reasoning is ultimately that "#(Fin n) is the standard mathematical definition and we got to stick to it", so be it, but I'll still disagree.

Sébastien Gouëzel (Sep 02 2024 at 11:11):

I think your feeling is guided by the fact that you're doing a lot of operations on cardinals and ordinals currently, and for this the defeq you're advocating for would be most convenient. But in mathematics, 95% of the time, cardinals are used to compare the size of things, not to do operations on them, so it seems natural to have a definition fitting these 95% of uses.

Violeta Hernández (Sep 02 2024 at 17:33):

Alright, I agree with that logic.

Violeta Hernández (Sep 11 2024 at 02:27):

I've thought about this for a while and I think I do agree that using the default natcast isn't always the best idea. I'll look into redefining the cast for ordinals as type (Fin n)

Violeta Hernández (Sep 11 2024 at 02:28):

And on that note, another weird NatCast instance we have is docs#SetTheory.PGame.instNatCast, which doesn't even agree with the usual definition of natural numbers as pre-games (it yields an equivalent but not an identical game)

Violeta Hernández (Sep 11 2024 at 02:29):

I'll try and fix that

Violeta Hernández (Sep 11 2024 at 05:58):

...actually, I'd like to discuss that instance separately

Violeta Hernández (Sep 11 2024 at 06:00):

Heh, looks like I already had a thread here for it, but I can't edit it anymore


Last updated: May 02 2025 at 03:31 UTC