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/Group/Finset.lean
.
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.lean
.
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.lean
.
Finset.card
, the size of a finset is defined in Mathlib/Data/Finset/Card.lean
.
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.lean
. 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.
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.lean
.
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 ≠ ∅
.
Equivalences between finsets #
- The
Mathlib/Logic/Equiv/Defs.lean
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
val
contains no duplicates
Equations
- x✝.decidableEq x = decidable_of_iff (x✝.val = x.val) ⋯
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
- Finset.partialOrder = PartialOrder.mk ⋯
Equations
- ⋯ = ⋯
Equations
- ⋯ = ⋯
Alias of the reverse direction of Finset.coe_subset
.
Equations
- ⋯ = ⋯
Coercion to Set α
as an OrderEmbedding
.
Equations
- Finset.coeEmb = { toFun := Finset.toSet, inj' := ⋯, map_rel_iff' := ⋯ }
Instances For
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.
Instances For
Alias of the reverse direction of Finset.coe_nonempty
.
Alias of the reverse direction of Finset.nonempty_coe_sort
.
Alias of Finset.Nonempty.exists_mem
.
empty #
Equations
- Finset.instEmptyCollection = { emptyCollection := Finset.empty }
Equations
- Finset.instOrderBot = OrderBot.mk ⋯
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.instSingleton = { singleton := fun (a : α) => { val := {a}, nodup := ⋯ } }
Alias of the forward direction of Finset.nonempty_iff_eq_singleton_default
.
Equations
- ⋯ = ⋯
Equations
- Finset.instUniqueSubtypeMemSingleton i = { default := ⟨i, ⋯⟩, uniq := ⋯ }
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 := ⋯ }
Instances For
Useful in proofs by induction.
Alias of Finset.cons_nonempty
.
disjoint #
disjoint union #
insert #
insert a s
is the set {a} ∪ s
containing a
and the elements of s
.
Equations
- Finset.instInsert = { insert := fun (a : α) (s : Finset α) => { val := Multiset.ndinsert a s.val, nodup := ⋯ } }
A version of LawfulSingleton.insert_emptyc_eq
that works with dsimp
.
Equations
- ⋯ = ⋯
Equations
- ⋯ = ⋯
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
.
s ∩ t
is the set such that a ∈ s ∩ t
iff a ∈ s
and a ∈ t
.
Equations
- Finset.instLattice = Lattice.mk ⋯ ⋯ ⋯
Equations
- U.decidableDisjoint V = decidable_of_iff (∀ ⦃a : α⦄, a ∈ U → a ∉ V) ⋯
union #
Equations
- ⋯ = ⋯
Equations
- ⋯ = ⋯
Equations
- ⋯ = ⋯
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 #
Equations
- Finset.instDistribLattice = DistribLattice.mk ⋯
Alias of Finset.inter_union_distrib_left
.
Alias of Finset.union_inter_distrib_right
.
Alias of Finset.union_inter_distrib_left
.
Alias of Finset.inter_union_distrib_right
.
Alias of the reverse direction of Finset.not_disjoint_iff_nonempty_inter
.
Equations
- ⋯ = ⋯
Equations
- ⋯ = ⋯
erase #
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.instGeneralizedBooleanAlgebra = GeneralizedBooleanAlgebra.mk ⋯ ⋯
Alias of Finset.sdiff_eq_self_iff_disjoint
.
Symmetric difference #
attach #
Alias of the reverse direction of Finset.attach_nonempty_iff
.
Equations
- x✝.instDecidableRelSubset x = Finset.decidableDforallFinset
Equations
- x✝.instDecidableRelSSubset x = instDecidableAnd
Equations
- Finset.instDecidableLE = Finset.instDecidableRelSubset
Equations
- Finset.instDecidableLT = Finset.instDecidableRelSSubset
Equations
- Finset.decidableExistsAndFinset = decidable_of_iff (∃ (a : α) (_ : a ∈ s), p a) ⋯