Finite sets #
Terms of type Finset α
are one way of talking about finite subsets of α
in mathlib.
Below, Finset α
is defined as a structure with 2 fields:
Finsets in Lean are constructive in that they have an underlying List
that enumerates their
elements. In particular, any function that uses the data of the underlying list cannot depend on its
ordering. This is handled on the Multiset
level by multiset API, so in most cases one needn't
worry about it explicitly.
Finsets give a basic foundation for defining finite sums and products over types:
Lean refers to these operations as big operators.
More information can be found in Mathlib.Algebra.BigOperators.Basic
.
Finsets are directly used to define fintypes in Lean.
A Fintype α
instance for a type α
consists of a universal Finset α
containing every term of
α
, called univ
. See Mathlib.Data.Fintype.Basic
.
There is also univ'
, the noncomputable partner to univ
,
which is defined to be α
as a finset if α
is finite,
and the empty finset otherwise. See Mathlib.Data.Fintype.Basic
.
Finset.card
, the size of a finset is defined in Mathlib.Data.Finset.Card
.
This is then used to define Fintype.card
, the size of a type.
Main declarations #
Main definitions #
Finset
: Defines a type for the finite subsets ofα
. Constructing aFinset
requires two pieces of data:val
, aMultiset α
of elements, andnodup
, a proof thatval
has no duplicates.Finset.instMembershipFinset
: Defines membershipa ∈ (s : Finset α)
.Finset.instCoeTCFinsetSet
: Provides a coercions : Finset α
tos : Set α
.Finset.instCoeSortFinsetType
: Coerces : Finset α
to the type of allx ∈ s
.Finset.induction_on
: Induction on finsets. To prove a proposition about an arbitraryFinset α
, it suffices to prove it for the empty finset, and to show that if it holds for someFinset α
, then it holds for the finset obtained by inserting a new element.Finset.choose
: Given a proofh
of existence and uniqueness of a certain element satisfying a predicate,choose s h
returns the element ofs
satisfying that predicate.
Finset constructions #
Finset.instSingletonFinset
: Denoted by{a}
; the finset consisting of one element.Finset.empty
: Denoted by∅
. The finset associated to any type consisting of no elements.Finset.range
: For anyn : ℕ
,range n
is equal to{0, 1, ... , n - 1} ⊆ ℕ
. This convention is consistent with other languages and normalizescard (range n) = n
. Beware,n
is not inrange n
.Finset.attach
: Givens : Finset α
,attach s
forms a finset of elements of the subtype{a // a ∈ s}
; in other words, it attaches elements to a proof of membership in the set.
Finsets from functions #
Finset.filter
: Given a decidable predicatep : α → Prop
,s.filter p
is the finset consisting of those elements ins
satisfying the predicatep
.
The lattice structure on subsets of finsets #
There is a natural lattice structure on the subsets of a set.
In Lean, we use lattice notation to talk about things involving unions and intersections. See
Mathlib.Order.Lattice
. For the lattice structure on finsets, ⊥
is called bot
with ⊥ = ∅
and
⊤
is called top
with ⊤ = univ
.
Finset.instHasSubsetFinset
: Lots of API about lattices, otherwise behaves as one would expect.Finset.instUnionFinset
: Definess ∪ t
(ors ⊔ t
) as the union ofs
andt
. SeeFinset.sup
/Finset.biUnion
for finite unions.Finset.instInterFinset
: Definess ∩ t
(ors ⊓ t
) as the intersection ofs
andt
. SeeFinset.inf
for finite intersections.Finset.disjUnion
: Given a hypothesish
which states that finsetss
andt
are disjoint,s.disjUnion t h
is the set such thata ∈ disjUnion s t h
iffa ∈ s
ora ∈ t
; this does not require decidable equality on the typeα
.
Operations on two or more finsets #
insert
andFinset.cons
: For anya : α
,insert s a
returnss ∪ {a}
.cons s a h
returns the same except that it requires a hypothesis stating thata
is not already ins
. This does not require decidable equality on the typeα
.Finset.instUnionFinset
: see "The lattice structure on subsets of finsets"Finset.instInterFinset
: see "The lattice structure on subsets of finsets"Finset.erase
: For anya : α
,erase s a
returnss
with the elementa
removed.Finset.instSDiffFinset
: Defines the set differences \ t
for finsetss
andt
.Finset.product
: Given finsets ofα
andβ
, defines finsets ofα × β
. For arbitrary dependent products, seeMathlib.Data.Finset.Pi
.Finset.biUnion
: Finite unions of finsets; given an indexing functionf : α → Finset β
and ans : Finset α
,s.biUnion f
is the union of all finsets of the formf a
fora ∈ s
.Finset.bInter
: TODO: Implement finite intersections.
Maps constructed using finsets #
Finset.piecewise
: Given two functionsf
,g
,s.piecewise f g
is a function which is equal tof
ons
andg
on the complement.
Predicates on finsets #
Disjoint
: defined via the lattice structure on finsets; two sets are disjoint if their intersection is empty.Finset.Nonempty
: A finset is nonempty if it has elements. This is equivalent to sayings ≠ ∅
. TODO: Decide on the simp normal form.
Equivalences between finsets #
- The
Mathlib.Data.Equiv
files describe a general type of equivalence, so look in there for any lemmas. There is some API for rewriting sums and products froms
tot
given thats ≃ t
. TODO: examples
Tags #
finite sets, finset
Equations
- Finset.decidableEq x x = match x, x with | x, x_1 => decidable_of_iff (x.val = x_1.val) (_ : x.val = x_1.val ↔ x = x_1)
membership #
Equations
- Finset.decidableMem a s = Multiset.decidableMem a s.val
set coercion #
Equations
- Finset.decidableMem' a s = Finset.decidableMem a s
extensionality #
type coercion #
Subset and strict subset relations #
Equations
- (_ : WellFoundedLT (Finset α)) = (_ : IsWellFounded (Finset α) fun x x_1 => x ⊂ x_1)
Nonempty #
The property s.Nonempty
expresses the fact that the finset s
is not empty. It should be used
in theorem assumptions instead of ∃ x, x ∈ s
or s ≠ ∅
as it gives access to a nice API thanks
to the dot notation.
Equations
- Finset.Nonempty s = ∃ x, x ∈ s
Instances For
Equations
- One or more equations did not get rendered due to their size.
Alias of the reverse direction of Finset.coe_nonempty
.
Alias of the reverse direction of Finset.nonempty_coe_sort
.
empty #
Equations
- Finset.instEmptyCollectionFinset = { emptyCollection := Finset.empty }
Equations
- Finset.instOrderBotFinsetToLEToPreorderPartialOrder = OrderBot.mk (_ : ∀ (s : Finset α), ∅ ⊆ s)
Alias of the reverse direction of Finset.empty_ssubset
.
singleton #
{a} : Finset a
is the set {a}
containing a
and nothing else.
This differs from insert a ∅
in that it does not require a DecidableEq
instance for α
.
Equations
- Finset.instSingletonFinset = { singleton := fun a => { val := {a}, nodup := (_ : Multiset.Nodup {a}) } }
Alias of the forward direction of Finset.nonempty_iff_eq_singleton_default
.
A finset is nontrivial if it has at least two elements.
Equations
Instances For
cons #
cons a s h
is the set {a} ∪ s
containing a
and the elements of s
. It is the same as
insert a s
when it is defined, but unlike insert a s
it does not require DecidableEq α
,
and the union is guaranteed to be disjoint.
Equations
- Finset.cons a s h = { val := a ::ₘ s.val, nodup := (_ : Multiset.Nodup (a ::ₘ s.val)) }
Instances For
Useful in proofs by induction.
disjoint #
disjoint union #
disjUnion s t h
is the set such that a ∈ disjUnion s t h
iff a ∈ s
or a ∈ t
.
It is the same as s ∪ t
, but it does not require decidable equality on the type. The hypothesis
ensures that the sets are disjoint.
Equations
- Finset.disjUnion s t h = { val := s.val + t.val, nodup := (_ : Multiset.Nodup (s.val + t.val)) }
Instances For
insert #
insert a s
is the set {a} ∪ s
containing a
and the elements of s
.
Equations
- Finset.instInsertFinset = { insert := fun a s => { val := Multiset.ndinsert a s.val, nodup := (_ : Multiset.Nodup (Multiset.ndinsert a s.val)) } }
Equations
- One or more equations did not get rendered due to their size.
To prove a proposition about an arbitrary Finset α
,
it suffices to prove it for the empty Finset
,
and to show that if it holds for some Finset α
,
then it holds for the Finset
obtained by inserting a new element.
To prove a proposition about S : Finset α
,
it suffices to prove it for the empty Finset
,
and to show that if it holds for some Finset α ⊆ S
,
then it holds for the Finset
obtained by inserting a new element of S
.
To prove a proposition about a nonempty s : Finset α
, it suffices to show it holds for all
singletons and that if it holds for nonempty t : Finset α
, then it also holds for the Finset
obtained by inserting an element in t
.
Inserting an element to a finite set is equivalent to the option type.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Lattice structure #
s ∪ t
is the set such that a ∈ s ∪ t
iff a ∈ s
or a ∈ t
.
Equations
- Finset.instUnionFinset = { union := fun s t => { val := Multiset.ndunion s.val t.val, nodup := (_ : Multiset.Nodup (Multiset.ndunion s.val t.val)) } }
s ∩ t
is the set such that a ∈ s ∩ t
iff a ∈ s
and a ∈ t
.
Equations
- Finset.instInterFinset = { inter := fun s t => { val := Multiset.ndinter s.val t.val, nodup := (_ : Multiset.Nodup (Multiset.ndinter s.val t.val)) } }
Equations
- One or more equations did not get rendered due to their size.
union #
To prove a relation on pairs of Finset X
, it suffices to show that it is
- symmetric,
- it holds when one of the
Finset
s is empty, - it holds for pairs of singletons,
- if it holds for
[a, c]
and for[b, c]
, then it holds for[a ∪ b, c]
.
inter #
Alias of the reverse direction of Finset.not_disjoint_iff_nonempty_inter
.
Equations
- (_ : IsDirected (Finset α) fun x x_1 => x ⊆ x_1) = (_ : IsDirected (Finset α) fun x x_1 => x ≤ x_1)
erase #
erase s a
is the set s - {a}
, that is, the elements of s
which are
not equal to a
.
Equations
- Finset.erase s a = { val := Multiset.erase s.val a, nodup := (_ : Multiset.Nodup (Multiset.erase s.val a)) }
Instances For
An element of s
that is not an element of erase s a
must bea
.
sdiff #
s \ t
is the set consisting of the elements of s
that are not in t
.
Equations
- Finset.instSDiffFinset = { sdiff := fun s₁ s₂ => { val := s₁.val - s₂.val, nodup := (_ : Multiset.Nodup (s₁.val - s₂.val)) } }
Symmetric difference #
attach #
attach s
takes the elements of s
and forms a new set of elements of the subtype
{x // x ∈ s}
.
Equations
- Finset.attach s = { val := Multiset.attach s.val, nodup := (_ : Multiset.Nodup (Multiset.attach s.val)) }
Instances For
piecewise #
s.piecewise f g
is the function equal to f
on the finset s
, and to g
on its
complement.
Equations
- Finset.piecewise s f g i = if i ∈ s then f i else g i
Instances For
Equations
- Finset.instDecidableRelSubset x x = Finset.decidableDforallFinset
Equations
- Finset.instDecidableRelSSubset x x = instDecidableAnd
Equations
- Finset.instDecidableLE = Finset.instDecidableRelSubset
Equations
- Finset.instDecidableLT = Finset.instDecidableRelSSubset
decidable equality for functions whose domain is bounded by finsets
Equations
- Finset.decidableEqPiFinset = Multiset.decidableEqPiMultiset
filter #
Finset.filter p s
is the set of elements of s
that satisfy p
.
Equations
- Finset.filter p s = { val := Multiset.filter p s.val, nodup := (_ : Multiset.Nodup (Multiset.filter p s.val)) }
Instances For
If all elements of a Finset
satisfy the predicate p
, s.filter p
is s
.
If all elements of a Finset
fail to satisfy the predicate p
, s.filter p
is ∅
.
After filtering out everything that does not equal a given value, at most that value remains.
This is equivalent to filter_eq'
with the equality the other way.
After filtering out everything that does not equal a given value, at most that value remains.
This is equivalent to filter_eq
with the equality the other way.
range #
range n
is the set of natural numbers less than n
.
Equations
- Finset.range n = { val := Multiset.range n, nodup := (_ : Multiset.Nodup (Multiset.range n)) }
Instances For
Useful in proofs by induction.
Equivalence between the set of natural numbers which are ≥ k
and ℕ
, given by n → n - k
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
dedup on list and multiset #
toFinset s
removes duplicates from the multiset s
to produce a finset.
Equations
- Multiset.toFinset s = { val := Multiset.dedup s, nodup := (_ : Multiset.Nodup (Multiset.dedup s)) }
Instances For
Produce a list of the elements in the finite set using choice.
Equations
- Finset.toList s = Multiset.toList s.val
Instances For
disjiUnion #
This section is about the bounded union of a disjoint indexed family t : α → Finset β
of finite
sets over a finite set s : Finset α
. In most cases Finset.biUnion
should be preferred.
disjiUnion s f h
is the set such that a ∈ disjiUnion s f
iff a ∈ f i
for some i ∈ s
.
It is the same as s.biUnion f
, but it does not require decidable equality on the type. The
hypothesis ensures that the sets are disjoint.
Equations
- Finset.disjiUnion s t hf = { val := Multiset.bind s.val (Finset.val ∘ t), nodup := (_ : Multiset.Nodup (Multiset.bind s.val (Finset.val ∘ t))) }
Instances For
biUnion #
This section is about the bounded union of an indexed family t : α → Finset β
of finite sets
over a finite set s : Finset α
.
biUnion s t
is the union of t x
over x ∈ s
.
(This was formerly bind
due to the monad structure on types with DecidableEq
.)
Equations
- Finset.biUnion s t = Multiset.toFinset (Multiset.bind s.val fun a => (t a).val)
Instances For
choose #
Given a finset l
and a predicate p
, associate to a proof that there is a unique element of
l
satisfying p
this unique element, as an element of the corresponding subtype.
Equations
- Finset.chooseX p l hp = Multiset.chooseX p l.val hp
Instances For
Given a finset l
and a predicate p
, associate to a proof that there is a unique element of
l
satisfying p
this unique element, as an element of the ambient type.
Equations
- Finset.choose p l hp = ↑(Finset.chooseX p l hp)
Instances For
Inhabited types are equivalent to Option β
for some β
by identifying default α
with none
.
Equations
- One or more equations did not get rendered due to their size.