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
.
Split the added element of cons off a Pi type.
Equations
- Finset.consPiProd f has x = (x a ⋯, fun (i : α) (hi : i ∈ s) => x i ⋯)
Instances For
Combine a product with a pi type to pi of cons.
Equations
- Finset.prodPiCons f has x i hi = if h : i = a then cast ⋯ x.1 else x.2 i ⋯
Instances For
The equivalence between pi types on cons and the product.
Equations
- Finset.consPiProdEquiv f has = { toFun := Finset.consPiProd f has, invFun := Finset.prodPiCons f has, left_inv := ⋯, right_inv := ⋯ }
Instances For
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
Split the added element of insert off a Pi type.
Equations
- Finset.insertPiProd f x = (x a ⋯, fun (i : α) (hi : i ∈ s) => x i ⋯)
Instances For
Combine a product with a pi type to pi of insert.
Equations
- Finset.prodPiInsert f x i hi = if h : i = a then cast ⋯ x.1 else x.2 i ⋯
Instances For
The equivalence between pi types on insert and the product.
Equations
- Finset.insertPiProdEquiv f has = { toFun := Finset.insertPiProd f, invFun := Finset.prodPiInsert f, left_inv := ⋯, right_inv := ⋯ }
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 (fun (x1 x2 : Finset α) => x1 ∩ x2) ⋯ ⋯ ⋯
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) ⋯
Equations
- Finset.decidableExistsAndFinsetCoe = Finset.decidableExistsAndFinset
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
.
For example, one can use s.filter (· ∈ t)
to get the intersection of s
with t : Set α
as a Finset α
(when a DecidablePred (· ∈ t)
instance is available).
Equations
- Finset.filter p s = { val := Multiset.filter p s.val, nodup := ⋯ }
Instances For
Return true
if expectedType?
is some (Finset ?α)
, throws throwUnsupportedSyntax
if it is
some (Set ?α)
, and returns false
otherwise.
Equations
- One or more equations did not get rendered due to their size.
- Mathlib.Meta.knownToBeFinsetNotSet none = pure false
Instances For
Elaborate set builder notation for Finset
.
{x ∈ s | p x}
is elaborated as Finset.filter (fun x ↦ p x) s
if either the expected type is
Finset ?α
or the expected type is not Set ?α
and s
has expected type Finset ?α
.
See also
Data.Set.Defs
for theSet
builder notation elaborator that this elaborator partly overrides.Data.Fintype.Basic
for theFinset
builder notation elaborator handling syntax of the form{x | p x}
,{x : α | p x}
,{x ∉ s | p x}
,{x ≠ a | p x}
.Order.LocallyFinite.Basic
for theFinset
builder notation elaborator handling syntax of the form{x ≤ a | p x}
,{x ≥ a | p x}
,{x < a | p x}
,{x > a | p x}
.
TODO: Write a delaborator
Equations
- One or more equations did not get rendered due to their size.
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 ∅
.
Alias of Finset.disjoint_filter_filter_neg
.
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 := ⋯ }
Instances For
Alias of the reverse direction of Finset.range_subset
.
Alias of the reverse direction of Finset.nonempty_range_iff
.
Useful in proofs by induction.
Equivalence between the set of natural numbers which are ≥ k
and ℕ
, given by n → n - k
.
Equations
- notMemRangeEquiv k = { toFun := fun (i : { n : ℕ // n ∉ Multiset.range k }) => ↑i - k, invFun := fun (j : ℕ) => ⟨j + k, ⋯⟩, left_inv := ⋯, right_inv := ⋯ }
Instances For
dedup on list and multiset #
Alias of the reverse direction of Multiset.toFinset_nonempty
.
Equations
- ⋯ = ⋯
Equations
- List.instDecidableSurjOnToSetOfDecidableEq = inferInstanceAs (Decidable (∀ x ∈ t', ∃ y ∈ s, f y = x))
Equations
- List.instDecidableInjOnToSetOfDecidableEq = inferInstanceAs (Decidable (∀ x ∈ s, ∀ y ∈ s, f x = f y → x = y))
Equations
- List.instDecidableMapsToToSetOfDecidablePredMemSet = inferInstanceAs (Decidable (∀ x ∈ s, f x ∈ t))
Equations
- List.instDecidableBijOnToSetOfDecidableEq = inferInstanceAs (Decidable (Set.MapsTo f ↑s ↑t' ∧ Set.InjOn f ↑s ∧ Set.SurjOn f ↑s ↑t'))
Alias of the reverse direction of List.toFinset_nonempty_iff
.
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
The disjoint union of finsets is a sum
Equations
- Equiv.Finset.union s t h = ((Equiv.Set.ofEq ⋯).trans (Equiv.Set.union ⋯)).symm
Instances For
The type of dependent functions on the disjoint union of finsets s ∪ t
is equivalent to the
type of pairs of functions on s
and on t
. This is similar to Equiv.sumPiEquivProdPi
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Attempt to prove that a finset is nonempty using the finsetNonempty
aesop rule-set.
You can add lemmas to the rule-set by tagging them with either:
aesop safe apply (rule_sets := [finsetNonempty])
if they are always a good idea to follow oraesop unsafe apply (rule_sets := [finsetNonempty])
if they risk directing the search to a blind alley.
TODO: should some of the lemmas be aesop safe simp
instead?
Equations
- One or more equations did not get rendered due to their size.