Zulip Chat Archive

Stream: Is there code for X?

Topic: Set.toNonempty


Terence Tao (Jan 01 2024 at 18:47):

This is a somewhat trivial thing, but would it make sense to have an analogue of docs#Set.toFinite for non-emptiness, i.e.,

import Mathlib

theorem Set.toNonempty {α : Type u} (s : Set α) [hs: Nonempty s] : Set.Nonempty s := nonempty_coe_sort.mp hs

so that one can use s.toNonempty instead of nonempty_coe_sort.mp (s := s) (by infer_instance)? With the current API it seems unnecessarily clunky to be able to actually establish a Set.Nonempty proposition.

Johan Commelin (Jan 01 2024 at 18:50):

Sounds good. What do you think of calling it Set.of_nonempty_coe?

Terence Tao (Jan 01 2024 at 18:51):

Would that mean that Set.toFinite should be similarly aliased to Set.of_finite_coe? I'm fine with either (or both), but I suppose we should try to be consistent with naming conventions if there is no pressing reason to deviate from them.

Yaël Dillies (Jan 01 2024 at 18:54):

I think of Set.toFinite as a pretty poor name. Certainly, I can't guess it every time I need it! I think Set.Finite.of_subtype or Set.Finite.of_coe or Set.Finite.of_coe_sort would be better.

Terence Tao (Jan 01 2024 at 18:57):

Similarly one could have

theorem Set.of_infinite_coe {α : Type u} (s : Set α) [hs: Infinite s] : Set.Infinite s := infinite_coe_iff.mp hs

theorem Set.of_subsingleton_coe {α : Type u} (s : Set α) [hs: Subsingleton s] : Set.Subsingleton s := (subsingleton_coe s).mp hs

On the other hand, we already have docs#Set.to_countable. It seems that the naming conventions here are not uniform.

Yaël Dillies (Jan 01 2024 at 19:02):

Yeah, I am happy to open a PR to clean this up. I've been annoyed by it.

Terence Tao (Jan 01 2024 at 19:02):

Would you still keep Set.toFinite and Set.to_countable as aliases for (I presume) Set.of_finite_coe and Set.of_countable_coe? Certainly Set.toFinite is used extensively in PFR for instance.

Yaël Dillies (Jan 01 2024 at 19:03):

As deprecated aliases, likely.

Yaël Dillies (Jan 01 2024 at 19:04):

The point of a name like Set.Finite.of_coe is that you can write .of_coe wherever Lean expects a term of type Set.Finite _.

Terence Tao (Jan 01 2024 at 19:06):

But then conversely if one only has a name for the set s and not the Finite instance, how do you use dot notation to prove Set.Finite s using the name Set.Finite.of_coe? I find s.toFinite a convenient notation.

Yaël Dillies (Jan 01 2024 at 19:07):

What would s.toFinite do if you didn't have the Finite s instance around? I would expect that to error.

Terence Tao (Jan 01 2024 at 19:09):

"failed to synthesize instance Finite ↑s".

Terence Tao (Jan 01 2024 at 19:11):

I use s.toFinite.toFinset a lot to create Finset versions of a Finite set. Not sure how else to do it actually.

Yaël Dillies (Jan 01 2024 at 19:12):

Oh you can just use docs#Set.toFinset

Yaël Dillies (Jan 01 2024 at 19:13):

A neat alternative is the lift tactic. If s : Set α and hs : s.Finite, then lift s to Finset α using hs will replace s : Set α by s : Finset α everywhere. It's magic.

Kyle Miller (Jan 01 2024 at 19:21):

Yaël Dillies said:

The point of a name like Set.Finite.of_coe is that you can write .of_coe wherever Lean expects a term of type Set.Finite _.

I feel like dot notation is frequently used for this where expected types aren't available

Kyle Miller (Jan 01 2024 at 19:22):

There's no harm having multiple aliases though (or maybe there are simp lemmas where this matters...)

Kyle Miller (Jan 01 2024 at 19:28):

I'm not sure I agree that Set.toFinite should be fundamentally renamed, but it should be Set.to_finite per the convention, right?

Terence Tao (Jan 01 2024 at 19:30):

Yaël Dillies said:

Oh you can just use docs#Set.toFinset

Set.toFinset requires a Fintype instance, which is not automatically generated from a Finite instance (a decidability issue perhaps?).

Yaël Dillies (Jan 01 2024 at 19:30):

You can use classical to fix this.

Yury G. Kudryashov (Jan 01 2024 at 19:31):

We don't have Finite α → Fintype α as an instance in classic to avoid diamonds.

Yaël Dillies (Jan 01 2024 at 19:32):

Yes but Fintype α → Fintype s becomes an instance.

Kyle Miller (Jan 01 2024 at 19:34):

One trick is have := Fintype.ofFinite to force the classical instances that s.toFinite.toFinset gives. I'm sure this causes issues places, but you can always be more precise with the have by giving an argument

Terence Tao (Jan 01 2024 at 20:13):

Currently we have the following converters regarding finite sets:

  • Set.toFinite : converts Finite instance to Set.Finite proposition
  • Finset.finite_toSet: converts a Finset type to a Set.Finite proposition
  • Fintype.finite: converts a Fintype instance to a Finite instance
  • Set.Finite.to_subtype: converts Set.Finite proposition to a Finite instance
  • Finite.of_fintype: Instance inference from Fintype to Finite
  • Set.Finite.fintype: converts Set.Finite proposition to a Fintype instance
  • Fintype.ofFinite: converts a Finite instance to a Fintype instance
  • Fintype.ofFinset: converts a Finset type to a Fintype instance
  • Set.Finite.toFinset: converts a Set.Finite proposition to a Finset type

I could imagine that there might be a more logical naming system here (in particular we might also want to have a wrapper for Set.finite_coe_iff.mpr).

Yury G. Kudryashov (Jan 01 2024 at 21:19):

Isn't docs#Set.Finite.to_subtype a wrapper for docs#Set.finite_coe_iff ?


Last updated: May 02 2025 at 03:31 UTC