Multiset coercion to type #
This module defines a CoeSort
instance for multisets and gives it a Fintype
instance.
It also defines Multiset.toEnumFinset
, which is another way to enumerate the elements of
a multiset. These coercions and definitions make it easier to sum over multisets using existing
Finset
theory.
Main definitions #
- A coercion from
m : Multiset α
to aType*
. Eachx : m
has two components. The first,x.1
, can be obtained via the coercion↑x : α
, and it yields the underlying element of the multiset. The second,x.2
, is a term ofFin (m.count x)
, and its function is to ensure each term appears with the correct multiplicity. Note that this coercion requiresDecidableEq α
due to the definition usingMultiset.count
. Multiset.toEnumFinset
is aFinset
version of this.Multiset.coeEmbedding
is the embeddingm ↪ α × ℕ
, whose first component is the coercion and whose second component enumerates elements with multiplicity.Multiset.coeEquiv
is the equivalencem ≃ m.toEnumFinset
.
Tags #
multiset enumeration
Auxiliary definition for the CoeSort
instance. This prevents the CoeOut m α
instance from inadvertently applying to other sigma types.
Equations
- m.ToType = ((x : α) × Fin (Multiset.count x m))
Instances For
Create a type that has the same number of elements as the multiset.
Terms of this type are triples ⟨x, ⟨i, h⟩⟩
where x : α
, i : ℕ
, and h : i < m.count x
.
This way repeated elements of a multiset appear multiple times from different values of i
.
Equations
- instCoeSortMultisetType = { coe := Multiset.ToType }
Constructor for terms of the coercion of m
to a type.
This helps Lean pick up the correct instances.
Equations
- m.mkToType x i = ⟨x, i⟩
Instances For
As a convenience, there is a coercion from m : Type*
to α
by projecting onto the first
component.
Equations
- instCoeSortMultisetType.instCoeOutToType = { coe := fun (x : m.ToType) => x.fst }
Equations
- instFintypeElemProdNatSetOfLtSndCountFst = Fintype.ofFinset (m.toFinset.biUnion fun (x : α) => Finset.map { toFun := Prod.mk x, inj' := ⋯ } (Finset.range (Multiset.count x m))) ⋯
Construct a finset whose elements enumerate the elements of the multiset m
.
The ℕ
component is used to differentiate between equal elements: if x
appears n
times
then (x, 0)
, ..., and (x, n-1)
appear in the Finset
.
Equations
- m.toEnumFinset = {p : α × ℕ | p.2 < Multiset.count p.1 m}.toFinset
Instances For
The embedding from a multiset into α × ℕ
where the second coordinate enumerates repeats.
If you are looking for the function m → α
, that would be plain (↑)
.
Equations
- m.coeEmbedding = { toFun := fun (x : m.ToType) => (x.fst, ↑x.snd), inj' := ⋯ }
Instances For
Another way to coerce a Multiset
to a type is to go through m.toEnumFinset
and coerce
that Finset
to a type.
Equations
Instances For
Equations
- Multiset.fintypeCoe = Fintype.ofEquiv { x : α × ℕ // x ∈ m.toEnumFinset } m.coeEquiv.symm