Finite sets #
This file defines predicates for finite and infinite sets and provides
Fintype
instances for many set constructions. It also proves basic facts
about finite sets and gives ways to manipulate Set.Finite
expressions.
Main definitions #
Set.Finite : Set α → Prop
Set.Infinite : Set α → Prop
Set.toFinite
to proveSet.Finite
for aSet
from aFinite
instance.Set.Finite.toFinset
to noncomputably produce aFinset
from aSet.Finite
proof. (SeeSet.toFinset
for a computable version.)
Implementation #
A finite set is defined to be a set whose coercion to a type has a Finite
instance.
There are two components to finiteness constructions. The first is Fintype
instances for each
construction. This gives a way to actually compute a Finset
that represents the set, and these
may be accessed using set.toFinset
. This gets the Finset
in the correct form, since otherwise
Finset.univ : Finset s
is a Finset
for the subtype for s
. The second component is
"constructors" for Set.Finite
that give proofs that Fintype
instances exist classically given
other Set.Finite
proofs. Unlike the Fintype
instances, these do not use any decidability
instances since they do not compute anything.
Tags #
finite sets
A set is finite if the corresponding Subtype
is finite,
i.e., if there exists a natural n : ℕ
and an equivalence s ≃ Fin n
.
Equations
- Set.Finite s = Finite ↑s
Instances For
Alias of the forward direction of Set.finite_def
.
Constructor for Set.Finite
using a Finite
instance.
Projection of Set.Finite
to its Finite
instance.
This is intended to be used with dot notation.
See also Set.Finite.Fintype
and Set.Finite.nonempty_fintype
.
A finite set coerced to a type is a Fintype
.
This is the Fintype
projection for a Set.Finite
.
Note that because Finite
isn't a typeclass, this definition will not fire if it
is made into an instance
Equations
Instances For
A set is infinite if it is not finite.
This is protected so that it does not conflict with global Infinite
.
Equations
- Set.Infinite s = ¬Set.Finite s
Instances For
Alias of the reverse direction of Set.not_infinite
.
See also finite_or_infinite
, fintypeOrInfinite
.
Basic properties of Set.Finite.toFinset
#
Note that this is an equality of types not holding definitionally. Use wisely.
The identity map, bundled as an equivalence between the subtypes of s : Set α
and of
h.toFinset : Finset α
, where h
is a proof of finiteness of s
.
Equations
Instances For
Alias of the reverse direction of Set.Finite.toFinset_subset_toFinset
.
Alias of the reverse direction of Set.Finite.toFinset_ssubset_toFinset
.
Fintype instances #
Every instance here should have a corresponding Set.Finite
constructor in the next section.
Equations
- Set.fintypeUniv = Fintype.ofEquiv α (Equiv.Set.univ α).symm
If (Set.univ : Set α)
is finite then α
is a finite type.
Equations
- Set.fintypeOfFiniteUniv H = Fintype.ofEquiv (↑Set.univ) (Equiv.Set.univ α)
Instances For
Equations
- Set.fintypeUnion s t = Fintype.ofFinset (Set.toFinset s ∪ Set.toFinset t) ⋯
Equations
- Set.fintypeSep s p = Fintype.ofFinset (Finset.filter p (Set.toFinset s)) ⋯
Equations
- Set.fintypeInter s t = Fintype.ofFinset (Set.toFinset s ∩ Set.toFinset t) ⋯
A Fintype
instance for set intersection where the left set has a Fintype
instance.
Equations
- Set.fintypeInterOfLeft s t = Fintype.ofFinset (Finset.filter (fun (x : α) => x ∈ t) (Set.toFinset s)) ⋯
A Fintype
instance for set intersection where the right set has a Fintype
instance.
Equations
- Set.fintypeInterOfRight s t = Fintype.ofFinset (Finset.filter (fun (x : α) => x ∈ s) (Set.toFinset t)) ⋯
A Fintype
structure on a set defines a Fintype
structure on its subset.
Equations
- Set.fintypeSubset s h = Eq.mpr ⋯ (Set.fintypeInterOfLeft s t)
Instances For
Equations
- Set.fintypeDiff s t = Fintype.ofFinset (Set.toFinset s \ Set.toFinset t) ⋯
Equations
- Set.fintypeDiffLeft s t = Set.fintypeSep s fun (x : α) => x ∈ tᶜ
Equations
- Set.fintypeiUnion f = Fintype.ofFinset (Finset.biUnion Finset.univ fun (i : PLift ι) => Set.toFinset (f i.down)) ⋯
Equations
- Set.fintypesUnion = Eq.mpr ⋯ (Set.fintypeiUnion fun (i : ↑s) => ↑i)
A union of sets with Fintype
structure over a set with Fintype
structure has a Fintype
structure.
Equations
- Set.fintypeBiUnion s t H = Fintype.ofFinset (Finset.biUnion (Finset.attach (Set.toFinset s)) fun (x : { x : ι // x ∈ Set.toFinset s }) => Set.toFinset (t ↑x)) ⋯
Instances For
Equations
- Set.fintypeBiUnion' s t = Fintype.ofFinset (Finset.biUnion (Set.toFinset s) fun (x : ι) => Set.toFinset (t x)) ⋯
If s : Set α
is a set with Fintype
instance and f : α → Set β
is a function such that
each f a
, a ∈ s
, has a Fintype
structure, then s >>= f
has a Fintype
structure.
Equations
- Set.fintypeBind s f H = Set.fintypeBiUnion s f H
Instances For
Equations
- Set.fintypeBind' s f = Set.fintypeBiUnion' s f
Equations
- Set.fintypeEmpty = Fintype.ofFinset ∅ ⋯
Equations
- Set.fintypeSingleton a = Fintype.ofFinset {a} ⋯
A Fintype
instance for inserting an element into a Set
using the
corresponding insert
function on Finset
. This requires DecidableEq α
.
There is also Set.fintypeInsert'
when a ∈ s
is decidable.
Equations
- Set.fintypeInsert a s = Fintype.ofFinset (insert a (Set.toFinset s)) ⋯
A Fintype
structure on insert a s
when inserting a new element.
Equations
- Set.fintypeInsertOfNotMem s h = Fintype.ofFinset { val := a ::ₘ (Set.toFinset s).val, nodup := ⋯ } ⋯
Instances For
A Fintype
structure on insert a s
when inserting a pre-existing element.
Equations
- Set.fintypeInsertOfMem s h = Fintype.ofFinset (Set.toFinset s) ⋯
Instances For
The Set.fintypeInsert
instance requires decidable equality, but when a ∈ s
is decidable for this particular a
we can still get a Fintype
instance by using
Set.fintypeInsertOfNotMem
or Set.fintypeInsertOfMem
.
This instance pre-dates Set.fintypeInsert
, and it is less efficient.
When Set.decidableMemOfFintype
is made a local instance, then this instance would
override Set.fintypeInsert
if not for the fact that its priority has been
adjusted. See Note [lower instance priority].
Equations
- Set.fintypeInsert' a s = if h : a ∈ s then Set.fintypeInsertOfMem s h else Set.fintypeInsertOfNotMem s h
Equations
- Set.fintypeImage s f = Fintype.ofFinset (Finset.image f (Set.toFinset s)) ⋯
If a function f
has a partial inverse and sends a set s
to a set with [Fintype]
instance,
then s
has a Fintype
structure as well.
Equations
- Set.fintypeOfFintypeImage s I = Fintype.ofFinset { val := Multiset.filterMap g (Set.toFinset (f '' s)).val, nodup := ⋯ } ⋯
Instances For
Equations
- Set.fintypeRange f = Fintype.ofFinset (Finset.image (f ∘ PLift.down) Finset.univ) ⋯
Equations
- Set.fintypeMap = Set.fintypeImage
Equations
Equations
- Set.fintypeLENat n = id (Eq.mp ⋯ (Set.fintypeLTNat (n + 1)))
This is not an instance so that it does not conflict with the one
in Mathlib/Order/LocallyFinite.lean
.
Equations
Instances For
Equations
- Set.fintypeProd s t = Fintype.ofFinset (Set.toFinset s ×ˢ Set.toFinset t) ⋯
Equations
image2 f s t
is Fintype
if s
and t
are.
Equations
- Set.fintypeImage2 f s t = Eq.mpr ⋯ (Set.fintypeImage (s ×ˢ t) fun (x : α × β) => f x.1 x.2)
Equations
- Set.fintypeSeq f s = Eq.mpr ⋯ (Set.fintypeBiUnion' f fun (x : α → β) => x '' s)
Equations
- Set.fintypeSeq' f s = Set.fintypeSeq f s
Equations
Finset #
Gives a Set.Finite
for the Finset
coerced to a Set
.
This is a wrapper around Set.toFinite
.
Finite instances #
There is seemingly some overlap between the following instances and the Fintype
instances
in Data.Set.Finite
. While every Fintype
instance gives a Finite
instance, those
instances that depend on Fintype
or Decidable
instances need an additional Finite
instance
to be able to generally apply.
Some set instances do not appear here since they are consequences of others, for example
Subtype.Finite
for subsets of a finite type.
Example: Finite (⋃ (i < n), f i)
where f : ℕ → Set α
and [∀ i, Finite (f i)]
(when given instances from Data.Nat.Interval
).
Equations
- ⋯ = ⋯
Constructors for Set.Finite
#
Every constructor here should have a corresponding Fintype
instance in the previous section
(or in the Fintype
module).
The implementation of these constructors ideally should be no more than Set.toFinite
,
after possibly setting up some Fintype
and classical Decidable
instances.
Alias of the forward direction of Set.finite_univ_iff
.
Dependent version of Finite.biUnion
.
If sets s i
are finite for all i
from a finite set t
and are empty for i ∉ t
, then the
union ⋃ i, s i
is a finite set.
There are finitely many subsets of a given finite set
Finite product of finite sets is finite
Finite product of finite sets is finite. Note this is a variant of Set.Finite.pi
without the
extra i ∈ univ
binder.
A finite union of finsets is finite.
Properties #
Analogous to Finset.induction_on'
.
If P
is some relation between terms of γ
and sets in γ
, such that every finite set
t : Set γ
has some c : γ
related to it, then there is a recursively defined sequence u
in γ
so u n
is related to the image of {0, 1, ..., n-1}
under u
.
(We use this later to show sequentially compact sets are totally bounded.)
Cardinality #
Infinite sets #
Alias of the reverse direction of Set.infinite_coe_iff
.
Embedding of ℕ
into an infinite set.
Equations
Instances For
Alias of the reverse direction of Set.infinite_image_iff
.
Order properties #
An increasing union distributes over finite intersection.
A decreasing union distributes over finite intersection.
An increasing intersection distributes over finite union.
A decreasing intersection distributes over finite union.
A version of Finite.exists_maximal_wrt
with the (weaker) hypothesis that the image of s
is finite rather than s
itself.
A version of Finite.exists_minimal_wrt
with the (weaker) hypothesis that the image of s
is finite rather than s
itself.
A finite set is bounded above.
A finite union of sets which are all bounded above is still bounded above.
A finite set is bounded below.
A finite union of sets which are all bounded below is still bounded below.
A finset is bounded above.
A finset is bounded below.
If a linear order does not contain any triple of elements x < y < z
, then this type
is finite.
If a set s
does not contain any triple of elements x < y < z
, then s
is finite.