The areas / V03. Sets, finite structures and big operators
Mathlib.Data.Multiset.Defs
66 declarations. Mathlib docs · Source
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 ∈ sifxhas multiplicity at least one ins. -
Subset (Multiset α)instance:s ⊆ tif everyx ∈ salso enjoysx ∈ t. -
PartialOrder (Multiset α)instance:s ≤ tif allxhave multiplicity insless than their multiplicity int. -
Multiset.Pairwise:Pairwise r sholds iff there exists a list of elements ofssuch thatrholds pairwise. -
Multiset.Nodup:Nodup sholds 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 ofathansdoes.s + t: The multiset for which the number of occurrences of eachais the sum of the occurrences ofainsandt.s - t: The multiset for which the number of occurrences of eachais the difference of the occurrences ofainsandt.s ∪ t: The multiset for which the number of occurrences of eachais the max of the occurrences ofainsandt.s ∩ t: The multiset for which the number of occurrences of eachais the min of the occurrences ofainsandt.
Multiset.Subset
Partial order on Multisets
Cardinality
Map for partial functions
def Multiset E3 #
Type u → Type u
Multiset α is the quotient of List α by list permutation. The result
is a type of finite sets with duplicates allowed.
def Multiset.ofList E1 #
{α : Type u_1} → List α → Multiset α
The quotient map from List α to Multiset α.
instance Multiset.instCoeList E0 #
{α : Type u_1} → Coe (List α) (Multiset α)
theorem Multiset.quot_mk_to_coe E0 #
∀ {α : Type u_1} (l : List α), ⟦l⟧ = ↑l
theorem Multiset.quot_mk_to_coe' E0 #
∀ {α : Type u_1} (l : List α), Quot.mk (fun x1 x2 ↦ x1 ≈ x2) l = ↑l
theorem Multiset.quot_mk_to_coe'' E0 #
∀ {α : Type u_1} (l : List α), Quot.mk (⇑(List.isSetoid α)) l = ↑l
theorem Multiset.lift_coe E0 #
∀ {α : Type u_2} {β : Type u_3} (x : List α) (f : List α → β) (h : ∀ (a b : List α), a ≈ b → f a = f b),
Quotient.lift f h ↑x = f x
theorem Multiset.coe_eq_coe E0 #
∀ {α : Type u_1} {l₁ l₂ : List α}, ↑l₁ = ↑l₂ ↔ l₁.Perm l₂
instance Multiset.instDecidableEquivListOfDecidableEq E0 #
{α : Type u_1} → [DecidableEq α] → (l₁ l₂ : List α) → Decidable (l₁ ≈ l₂)
instance Multiset.instDecidableRListOfDecidableEq E0 #
{α : Type u_1} → [DecidableEq α] → (l₁ l₂ : List α) → Decidable ((List.isSetoid α) l₁ l₂)
instance Multiset.decidableEq E0 #
{α : Type u_1} → [DecidableEq α] → DecidableEq (Multiset α)
def Multiset.Mem E1 #
{α : Type u_1} → Multiset α → α → Prop
a ∈ s means that a has nonzero multiplicity in s.
instance Multiset.instMembership E0 #
{α : Type u_1} → Membership α (Multiset α)
theorem Multiset.mem_coe E0 #
∀ {α : Type u_1} {a : α} {l : List α}, a ∈ ↑l ↔ a ∈ l
instance Multiset.decidableMem E0 #
{α : Type u_1} → [DecidableEq α] → (a : α) → (s : Multiset α) → Decidable (a ∈ s)
def Multiset.Subset E1 #
{α : Type u_1} → Multiset α → Multiset α → Prop
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.
instance Multiset.instHasSubset E0 #
{α : Type u_1} → HasSubset (Multiset α)
instance Multiset.instHasSSubset E0 #
{α : Type u_1} → HasSSubset (Multiset α)
instance Multiset.instIsNonstrictStrictOrder E0 #
∀ {α : Type u_1}, IsNonstrictStrictOrder (Multiset α) (fun x1 x2 ↦ x1 ⊆ x2) fun x1 x2 ↦ x1 ⊂ x2
theorem Multiset.coe_subset E0 #
∀ {α : Type u_1} {l₁ l₂ : List α}, ↑l₁ ⊆ ↑l₂ ↔ l₁ ⊆ l₂
theorem Multiset.Subset.refl E0 #
∀ {α : Type u_1} (s : Multiset α), s ⊆ s
theorem Multiset.Subset.trans E1 #
∀ {α : Type u_1} {s t u : Multiset α}, s ⊆ t → t ⊆ u → s ⊆ u
theorem Multiset.subset_iff E0 #
∀ {α : Type u_1} {s t : Multiset α}, s ⊆ t ↔ ∀ ⦃x : α⦄, x ∈ s → x ∈ t
theorem Multiset.mem_of_subset E0 #
∀ {α : Type u_1} {s t : Multiset α} {a : α}, s ⊆ t → a ∈ s → a ∈ t
def Multiset.Le E1 #
{α : Type u_1} → Multiset α → Multiset α → Prop
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.
instance Multiset.instPartialOrder E0 #
{α : Type u_1} → PartialOrder (Multiset α)
instance Multiset.decidableLE E0 #
{α : Type u_1} → [DecidableEq α] → DecidableLE (Multiset α)
theorem Multiset.subset_of_le E1 #
∀ {α : Type u_1} {s t : Multiset α}, s ≤ t → s ⊆ t
theorem Multiset.Le.subset E1 #
∀ {α : Type u_1} {s t : Multiset α}, s ≤ t → s ⊆ t
Alias of Multiset.subset_of_le.
theorem Multiset.mem_of_le E0 #
∀ {α : Type u_1} {s t : Multiset α} {a : α}, s ≤ t → a ∈ s → a ∈ t
theorem Multiset.notMem_mono E1 #
∀ {α : Type u_1} {s t : Multiset α} {a : α}, s ⊆ t → a ∉ t → a ∉ s
theorem Multiset.coe_le E0 #
∀ {α : Type u_1} {l₁ l₂ : List α}, ↑l₁ ≤ ↑l₂ ↔ l₁.Subperm l₂
theorem Multiset.leInductionOn E1 #
∀ {α : Type u_1} {C : Multiset α → Multiset α → Prop} {s t : Multiset α},
s ≤ t → (∀ {l₁ l₂ : List α}, l₁.Sublist l₂ → C ↑l₁ ↑l₂) → C s t
def Multiset.card E3 #
{α : Type u_1} → Multiset α → ℕ
The cardinality of a multiset is the sum of the multiplicities of all its elements, or simply the length of the underlying list.
theorem Multiset.coe_card E0 #
∀ {α : Type u_1} (l : List α), (↑l).card = l.length
theorem Multiset.card_le_card E1 #
∀ {α : Type u_1} {s t : Multiset α}, s ≤ t → s.card ≤ t.card
theorem Multiset.card_lt_card E1 #
∀ {α : Type u_1} {s t : Multiset α}, s < t → s.card < t.card
theorem Multiset.card_mono E1 #
∀ {α : Type u_1}, Monotone Multiset.card
theorem Multiset.card_strictMono E1 #
∀ {α : Type u_1}, StrictMono Multiset.card
instance Multiset.instWellFoundedLT E0 #
∀ {α : Type u_1}, WellFoundedLT (Multiset α)
Another way of expressing strongInductionOn: the (<) relation is well-founded.
theorem Multiset.coe_reverse E0 #
∀ {α : Type u_1} (l : List α), ↑l.reverse = ↑l
def Multiset.pmap E3 #
{α : Type u_1} → {β : Type v} → {p : α → Prop} → ((a : α) → p a → β) → (s : Multiset α) → (∀ a ∈ s, p a) → Multiset β
Lift of the list pmap operation. Map a partial function f over a multiset
s whose elements are all in the domain of f.
theorem Multiset.coe_pmap E0 #
∀ {α : Type u_1} {β : Type v} {p : α → Prop} (f : (a : α) → p a → β) (l : List α) (H : ∀ a ∈ l, p a),
Multiset.pmap f (↑l) H = ↑(List.pmap f l H)
theorem Multiset.pmap_congr E1 #
∀ {α : Type u_1} {β : Type v} {p q : α → Prop} {f : (a : α) → p a → β} {g : (a : α) → q a → β} (s : Multiset α)
{H₁ : ∀ a ∈ s, p a} {H₂ : ∀ a ∈ s, q a},
(∀ a ∈ s, ∀ (h₁ : p a) (h₂ : q a), f a h₁ = g a h₂) → Multiset.pmap f s H₁ = Multiset.pmap g s H₂
theorem Multiset.mem_pmap E0 #
∀ {α : Type u_1} {β : Type v} {p : α → Prop} {f : (a : α) → p a → β} {s : Multiset α} {H : ∀ a ∈ s, p a} {b : β},
b ∈ Multiset.pmap f s H ↔ ∃ a, ∃ (h : a ∈ s), f a ⋯ = b
theorem Multiset.card_pmap E0 #
∀ {α : Type u_1} {β : Type v} {p : α → Prop} (f : (a : α) → p a → β) (s : Multiset α) (H : ∀ a ∈ s, p a),
(Multiset.pmap f s H).card = s.card
def Multiset.attach E3 #
{α : Type u_1} → (s : Multiset α) → Multiset { x // x ∈ s }
"Attach" a proof that a ∈ s to each element a in s to produce
a multiset on {x // x ∈ s}.
theorem Multiset.coe_attach E0 #
∀ {α : Type u_1} (l : List α), (↑l).attach = ↑l.attach
theorem Multiset.mem_attach E0 #
∀ {α : Type u_1} (s : Multiset α) (x : { x // x ∈ s }), x ∈ s.attach
theorem Multiset.card_attach E0 #
∀ {α : Type u_1} {m : Multiset α}, m.attach.card = m.card
def Multiset.decidableForallMultiset E1 #
{α : Type u_1} → {m : Multiset α} → {p : α → Prop} → [(a : α) → Decidable (p a)] → Decidable (∀ a ∈ m, p a)
If p is a decidable predicate,
so is the predicate that all elements of a multiset satisfy p.
instance Multiset.decidableDforallMultiset E0 #
{α : Type u_1} →
{m : Multiset α} →
{p : (a : α) → a ∈ m → Prop} →
[_hp : (a : α) → (h : a ∈ m) → Decidable (p a h)] → Decidable (∀ (a : α) (h : a ∈ m), p a h)
instance Multiset.decidableEqPiMultiset E0 #
{α : Type u_1} →
{m : Multiset α} → {β : α → Type u_2} → [(a : α) → DecidableEq (β a)] → DecidableEq ((a : α) → a ∈ m → β a)
decidable equality for functions whose domain is bounded by multisets
def Multiset.decidableExistsMultiset E1 #
{α : Type u_1} → {m : Multiset α} → {p : α → Prop} → [DecidablePred p] → Decidable (∃ x ∈ m, p x)
If p is a decidable predicate,
so is the existence of an element in a multiset satisfying p.
instance Multiset.decidableDexistsMultiset E0 #
{α : Type u_1} →
{m : Multiset α} →
{p : (a : α) → a ∈ m → Prop} →
[_hp : (a : α) → (h : a ∈ m) → Decidable (p a h)] → Decidable (∃ a, ∃ (h : a ∈ m), p a h)
def Multiset.Pairwise E3 #
{α : Type u_1} → (α → α → Prop) → Multiset α → Prop
Pairwise r m states that there exists a list of the elements s.t. r holds pairwise on this
list.
theorem Multiset.pairwise_coe_iff E0 #
∀ {α : Type u_1} {r : α → α → Prop} {l : List α}, Multiset.Pairwise r ↑l ↔ ∃ l', l.Perm l' ∧ List.Pairwise r l'
theorem Multiset.pairwise_coe_iff_pairwise E1 #
∀ {α : Type u_1} {r : α → α → Prop} [Std.Symm r] {l : List α}, Multiset.Pairwise r ↑l ↔ List.Pairwise r l
def Multiset.Nodup E3 #
{α : Type u_1} → Multiset α → Prop
Nodup s means that s has no duplicates, i.e. the multiplicity of
any element is at most 1.
theorem Multiset.coe_nodup E0 #
∀ {α : Type u_1} {l : List α}, (↑l).Nodup ↔ l.Nodup
theorem Multiset.Nodup.ext E1 #
∀ {α : Type u_1} {s t : Multiset α}, s.Nodup → t.Nodup → (s = t ↔ ∀ (a : α), a ∈ s ↔ a ∈ t)
theorem Multiset.le_iff_subset E1 #
∀ {α : Type u_1} {s t : Multiset α}, s.Nodup → (s ≤ t ↔ s ⊆ t)
theorem Multiset.nodup_of_le E1 #
∀ {α : Type u_1} {s t : Multiset α}, s ≤ t → t.Nodup → s.Nodup
instance Multiset.nodupDecidable E0 #
{α : Type u_1} → [DecidableEq α] → (s : Multiset α) → Decidable s.Nodup
def Multiset.sizeOf E1 #
{α : Type u_1} → [SizeOf α] → Multiset α → ℕ
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.
instance Multiset.instSizeOf E0 #
{α : Type u_1} → [SizeOf α] → SizeOf (Multiset α)