Multiset coercion to type #
THIS FILE IS SYNCHRONIZED WITH MATHLIB4. Any changes to this file require a corresponding PR to mathlib4.
This module defines a has_coe_to_sort
instance for multisets and gives it a fintype
instance.
It also defines multiset.to_enum_finset
, 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*
. Forx : m
, then there is a coercion↑x : α
, andx.2
is a term offin (m.count x)
. The second component is what ensures each term appears with the correct multiplicity. Note that this coercion requiresdecidable_eq α
due tomultiset.count
. multiset.to_enum_finset
is afinset
version of this.multiset.coe_embedding
is the embeddingm ↪ α × ℕ
, whose first component is the coercion and whose second component enumerates elements with multiplicity.multiset.coe_equiv
is the equivalencem ≃ m.to_enum_finset
.
Tags #
multiset enumeration
Auxiliary definition for the has_coe_to_sort
instance. This prevents the has_coe m α
instance from inadverently applying to other sigma types. One should not use this definition
directly.
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 with different values of i
.
Equations
- multiset.has_coe_to_sort = {coe := multiset.to_type (λ (a b : α), _inst_1 a b)}
Constructor for terms of the coercion of m
to a type.
This helps Lean pick up the correct instances.
Equations
- m.mk_to_type x i = ⟨x, i⟩
As a convenience, there is a coercion from m : Type*
to α
by projecting onto the first
component.
Equations
- set_of.fintype = fintype.of_finset (m.to_finset.bUnion (λ (x : α), finset.map {to_fun := prod.mk x, inj' := _} (finset.range (multiset.count x m)))) set_of.fintype._proof_1
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.to_enum_finset = {p : α × ℕ | p.snd < multiset.count p.fst m}.to_finset
The embedding from a multiset into α × ℕ
where the second coordinate enumerates repeats.
If you are looking for the function m → α
, that would be plain coe
.