Multisets #
Multisets are finite sets with duplicates allowed. They are implemented here as the quotient of lists by permutation. This gives them computational content.
This file contains the definition of Multiset
and the basic predicates. Most operations
have been split off into their own files. The goal is that we can define Finset
with only
importing Multiset.Defs
.
Main definitions #
Multiset
: the type of finite sets with duplicates allowed.Coe (List α) (Multiset α)
: turn a list into a multiset by forgetting the order.Multiset.pmap
: map a partial function defined on a superset of the multiset's elements.Multiset.attach
: add a proof of membership to the elements of the multiset.Multiset.card
: number of elements of a multiset (counted with repetition).Membership α (Multiset α)
instance:x ∈ s
ifx
has multiplicity at least one ins
.Subset (Multiset α)
instance:s ⊆ t
if everyx ∈ s
also enjoysx ∈ t
.PartialOrder (Multiset α)
instance:s ≤ t
if allx
have multiplicity ins
less than their multiplicity int
.Multiset.Pairwise
:Pairwise r s
holds iff there exists a list of elements ofs
such thatr
holds pairwise.Multiset.Nodup
:Nodup s
holds if the multiplicity of any element is at most 1.
Notation (defined later) #
0
: The empty multiset.{a}
: The multiset containing a single occurrence ofa
.a ::ₘ s
: The multiset containing one more occurrence ofa
thans
does.s + t
: The multiset for which the number of occurrences of eacha
is the sum of the occurrences ofa
ins
andt
.s - t
: The multiset for which the number of occurrences of eacha
is the difference of the occurrences ofa
ins
andt
.s ∪ t
: The multiset for which the number of occurrences of eacha
is the max of the occurrences ofa
ins
andt
.s ∩ t
: The multiset for which the number of occurrences of eacha
is the min of the occurrences ofa
ins
andt
.
Equations
- Multiset.instCoeList = { coe := Multiset.ofList }
Equations
- Multiset.instDecidableEquivListOfDecidableEq l₁ l₂ = inferInstanceAs (Decidable (l₁.Perm l₂))
Equations
- Multiset.instDecidableRListOfDecidableEq l₁ l₂ = inferInstanceAs (Decidable (l₁.Perm l₂))
Equations
- x✝¹.decidableEq x✝ = Quotient.recOnSubsingleton₂ x✝¹ x✝ fun (x x_1 : List α) => decidable_of_iff' (x ≈ x_1) ⋯
a ∈ s
means that a
has nonzero multiplicity in s
.
Equations
- s.Mem a = Quot.liftOn s (fun (l : List α) => a ∈ l) ⋯
Instances For
Equations
- Multiset.instMembership = { mem := Multiset.Mem }
Equations
- Multiset.decidableMem a s = Quot.recOnSubsingleton s fun (l : List α) => inferInstanceAs (Decidable (a ∈ l))
s ⊆ t
is the lift of the list subset relation. It means that any
element with nonzero multiplicity in s
has nonzero multiplicity in t
,
but it does not imply that the multiplicity of a
in s
is less or equal than in t
;
see s ≤ t
for this relation.
Instances For
Equations
- Multiset.instHasSubset = { Subset := Multiset.Subset }
s ≤ t
means that s
is a sublist of t
(up to permutation).
Equivalently, s ≤ t
means that count a s ≤ count a t
for all a
.
Equations
- s.Le t = Quotient.liftOn₂ s t (fun (x1 x2 : List α) => x1.Subperm x2) ⋯
Instances For
Equations
Equations
Alias of Multiset.subset_of_le
.
Cardinality #
The cardinality of a multiset is the sum of the multiplicities of all its elements, or simply the length of the underlying list.
Equations
Instances For
Another way of expressing strongInductionOn
: the (<)
relation is well-founded.
Map for partial functions #
Lift of the list pmap
operation. Map a partial function f
over a multiset
s
whose elements are all in the domain of f
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
"Attach" a proof that a ∈ s
to each element a
in s
to produce
a multiset on {x // x ∈ s}
.
Equations
- s.attach = Multiset.pmap Subtype.mk s ⋯
Instances For
If p
is a decidable predicate,
so is the predicate that all elements of a multiset satisfy p
.
Equations
- Multiset.decidableForallMultiset = Quotient.recOnSubsingleton m fun (l : List α) => decidable_of_iff (∀ (a : α), a ∈ l → p a) ⋯
Instances For
decidable equality for functions whose domain is bounded by multisets
Equations
- Multiset.decidableEqPiMultiset f g = decidable_of_iff (∀ (a : α) (h : a ∈ m), f a h = g a h) ⋯
If p
is a decidable predicate,
so is the existence of an element in a multiset satisfying p
.
Equations
- Multiset.decidableExistsMultiset = Quotient.recOnSubsingleton m fun (l : List α) => decidable_of_iff (∃ (a : α), a ∈ l ∧ p a) ⋯
Instances For
Nodup s
means that s
has no duplicates, i.e. the multiplicity of
any element is at most 1.
Equations
- s.Nodup = Quot.liftOn s List.Nodup ⋯
Instances For
Equations
- s.nodupDecidable = Quotient.recOnSubsingleton s fun (l : List α) => l.nodupDecidable
Defines a size for a multiset by referring to the size of the underlying list.
This has to be defined before the definition of Finset
, otherwise its automatically generated
SizeOf
instance will be wrong.
Equations
- s.sizeOf = Quot.liftOn s sizeOf ⋯
Instances For
Equations
- Multiset.instSizeOf = { sizeOf := Multiset.sizeOf }