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
membership #
set coercion #
Convert a finset to a set in the natural way.
Instances For
extensionality #
type coercion #
Subset and strict subset relations #
Coercion to Set α
as an OrderEmbedding
.
Instances For
Nonempty #
Alias of the reverse direction of Finset.coe_nonempty
.
Alias of the reverse direction of Finset.nonempty_coe_sort
.
empty #
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 α
.
Alias of the forward direction of Finset.nonempty_iff_eq_singleton_default
.
A finset is nontrivial if it has at least two elements.
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.
Instances For
Useful in proofs by induction.
disjoint #
disjoint union #
insert #
insert a s
is the set {a} ∪ s
containing a
and the elements of s
.
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
.
Lattice structure #
s ∪ t
is the set such that a ∈ s ∪ t
iff a ∈ s
or a ∈ t
.
s ∩ t
is the set such that a ∈ s ∩ t
iff a ∈ s
and a ∈ t
.
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
.
erase #
erase s a
is the set s - {a}
, that is, the elements of s
which are
not equal to 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
.