The areas / V03. Sets, finite structures and big operators
Mathlib.Data.Multiset.Filter
77 declarations and 2 deprecated aliases. Mathlib docs · Source
Filtering multisets by a predicate
Main definitions
Multiset.filter:filter p sis the multiset of elements insthat satisfyp.Multiset.filterMap:filterMap f sis the multiset ofbs wheresome b ∈ map f s.
Multiset.filter
Simultaneously filter and map elements of a multiset
countP
Multiplicity of an element
Subtraction
theorem Multiset.filter.congr_simp E1 #
∀ {α : Type u_1} (p p_1 : α → Prop),
p = p_1 →
∀ {inst : DecidablePred p} [inst_1 : DecidablePred p_1] (s s_1 : Multiset α),
s = s_1 → Multiset.filter p s = Multiset.filter p_1 s_1
def Multiset.filter E3 #
{α : Type u_1} → (p : α → Prop) → [DecidablePred p] → Multiset α → Multiset α
Filter p s returns the elements in s (with the same multiplicities)
which satisfy p, and removes the rest.
theorem Multiset.filter_coe E0 #
∀ {α : Type u_1} (p : α → Prop) [inst : DecidablePred p] (l : List α),
Multiset.filter p ↑l = ↑(List.filter (fun b ↦ decide (p b)) l)
theorem Multiset.filter_zero E0 #
∀ {α : Type u_1} (p : α → Prop) [inst : DecidablePred p], Multiset.filter p 0 = 0
theorem Multiset.filter_congr E1 #
∀ {α : Type u_1} {p q : α → Prop} [inst : DecidablePred p] [inst_1 : DecidablePred q] {s : Multiset α},
(∀ x ∈ s, p x ↔ q x) → Multiset.filter p s = Multiset.filter q s
theorem Multiset.filter_congr' E1 #
∀ {α : Type u_1} {p q : α → Prop} [inst : DecidablePred p] [inst_1 : DecidablePred q] {s t : Multiset α},
s = t → (∀ x ∈ s, p x ↔ q x) → Multiset.filter p s = Multiset.filter q t
theorem Multiset.filter_add E0 #
∀ {α : Type u_1} (p : α → Prop) [inst : DecidablePred p] (s t : Multiset α),
Multiset.filter p (s + t) = Multiset.filter p s + Multiset.filter p t
theorem Multiset.filter_le E0 #
∀ {α : Type u_1} (p : α → Prop) [inst : DecidablePred p] (s : Multiset α), Multiset.filter p s ≤ s
theorem Multiset.filter_subset E0 #
∀ {α : Type u_1} (p : α → Prop) [inst : DecidablePred p] (s : Multiset α), Multiset.filter p s ⊆ s
theorem Multiset.filter_le_filter E1 #
∀ {α : Type u_1} (p : α → Prop) [inst : DecidablePred p] {s t : Multiset α},
s ≤ t → Multiset.filter p s ≤ Multiset.filter p t
theorem Multiset.monotone_filter_left E1 #
∀ {α : Type u_1} (p : α → Prop) [inst : DecidablePred p], Monotone (Multiset.filter p)
theorem Multiset.monotone_filter_right E1 #
∀ {α : Type u_1} (s : Multiset α) ⦃p q : α → Prop⦄ [inst : DecidablePred p] [inst_1 : DecidablePred q],
(∀ (b : α), p b → q b) → Multiset.filter p s ≤ Multiset.filter q s
theorem Multiset.filter_cons_of_pos E0 #
∀ {α : Type u_1} {p : α → Prop} [inst : DecidablePred p] {a : α} (s : Multiset α),
p a → Multiset.filter p (a ::ₘ s) = a ::ₘ Multiset.filter p s
theorem Multiset.filter_cons_of_neg E0 #
∀ {α : Type u_1} {p : α → Prop} [inst : DecidablePred p] {a : α} (s : Multiset α),
¬p a → Multiset.filter p (a ::ₘ s) = Multiset.filter p s
theorem Multiset.mem_filter E0 #
∀ {α : Type u_1} {p : α → Prop} [inst : DecidablePred p] {a : α} {s : Multiset α}, a ∈ Multiset.filter p s ↔ a ∈ s ∧ p a
theorem Multiset.of_mem_filter E1 #
∀ {α : Type u_1} {p : α → Prop} [inst : DecidablePred p] {a : α} {s : Multiset α}, a ∈ Multiset.filter p s → p a
theorem Multiset.mem_of_mem_filter E0 #
∀ {α : Type u_1} {p : α → Prop} [inst : DecidablePred p] {a : α} {s : Multiset α}, a ∈ Multiset.filter p s → a ∈ s
theorem Multiset.mem_filter_of_mem E0 #
∀ {α : Type u_1} {p : α → Prop} [inst : DecidablePred p] {a : α} {l : Multiset α}, a ∈ l → p a → a ∈ Multiset.filter p l
theorem Multiset.filter_eq_self E0 #
∀ {α : Type u_1} {p : α → Prop} [inst : DecidablePred p] {s : Multiset α}, Multiset.filter p s = s ↔ ∀ a ∈ s, p a
theorem Multiset.filter_eq_nil E0 #
∀ {α : Type u_1} {p : α → Prop} [inst : DecidablePred p] {s : Multiset α}, Multiset.filter p s = 0 ↔ ∀ a ∈ s, ¬p a
theorem Multiset.filter_true E0 #
∀ {α : Type u_1} (s : Multiset α), Multiset.filter (fun x ↦ True) s = s
theorem Multiset.filter_false E0 #
∀ {α : Type u_1} (s : Multiset α), Multiset.filter (fun x ↦ False) s = 0
theorem Multiset.le_filter E1 #
∀ {α : Type u_1} {p : α → Prop} [inst : DecidablePred p] {s t : Multiset α},
s ≤ Multiset.filter p t ↔ s ≤ t ∧ ∀ a ∈ s, p a
theorem Multiset.filter_cons E1 #
∀ {α : Type u_1} {p : α → Prop} [inst : DecidablePred p] {a : α} (s : Multiset α),
Multiset.filter p (a ::ₘ s) = (if p a then {a} else 0) + Multiset.filter p s
theorem Multiset.filter_singleton E1 #
∀ {α : Type u_1} {a : α} (p : α → Prop) [inst : DecidablePred p], Multiset.filter p {a} = if p a then {a} else ∅
theorem Multiset.filter_filter E0 #
∀ {α : Type u_1} (p : α → Prop) [inst : DecidablePred p] (q : α → Prop) [inst_1 : DecidablePred q] (s : Multiset α),
Multiset.filter p (Multiset.filter q s) = Multiset.filter (fun a ↦ p a ∧ q a) s
theorem Multiset.filter_comm E1 #
∀ {α : Type u_1} (p : α → Prop) [inst : DecidablePred p] (q : α → Prop) [inst_1 : DecidablePred q] (s : Multiset α),
Multiset.filter p (Multiset.filter q s) = Multiset.filter q (Multiset.filter p s)
theorem Multiset.filter_add_filter E1 #
∀ {α : Type u_1} (p : α → Prop) [inst : DecidablePred p] (q : α → Prop) [inst_1 : DecidablePred q] (s : Multiset α),
Multiset.filter p s + Multiset.filter q s =
Multiset.filter (fun a ↦ p a ∨ q a) s + Multiset.filter (fun a ↦ p a ∧ q a) s
theorem Multiset.filter_add_not E1 #
∀ {α : Type u_1} (p : α → Prop) [inst : DecidablePred p] (s : Multiset α),
Multiset.filter p s + Multiset.filter (fun a ↦ ¬p a) s = s
theorem Multiset.filter_map E1 #
∀ {α : Type u_1} {β : Type v} (p : α → Prop) [inst : DecidablePred p] (f : β → α) (s : Multiset β),
Multiset.filter p (Multiset.map f s) = Multiset.map f (Multiset.filter (p ∘ f) s)
theorem Multiset.map_filter E0 #
∀ {α : Type u_1} {β : Type v} (p : α → Prop) [inst : DecidablePred p] {f : α → β},
Function.Injective f →
∀ (s : Multiset α) [inst_1 : DecidablePred fun b ↦ ∃ a, p a ∧ f a = b],
Multiset.map f (Multiset.filter p s) = Multiset.filter (fun b ↦ ∃ a, p a ∧ f a = b) (Multiset.map f s)
theorem Multiset.card_filter_le_iff E0 #
∀ {α : Type u_1} (s : Multiset α) (P : α → Prop) [inst : DecidablePred P] (n : ℕ),
(Multiset.filter P s).card ≤ n ↔ ∀ s' ≤ s, n < s'.card → ∃ a ∈ s', ¬P a
def Multiset.filterMap E3 #
{α : Type u_1} → {β : Type v} → (α → Option β) → Multiset α → Multiset β
filterMap f s is a combination filter/map operation on s.
The function f : α → Option β is applied to each element of s;
if f a is some b then b is added to the result, otherwise
a is removed from the resulting multiset.
theorem Multiset.filterMap_coe E0 #
∀ {α : Type u_1} {β : Type v} (f : α → Option β) (l : List α), Multiset.filterMap f ↑l = ↑(List.filterMap f l)
theorem Multiset.filterMap_zero E0 #
∀ {α : Type u_1} {β : Type v} (f : α → Option β), Multiset.filterMap f 0 = 0
theorem Multiset.filterMap_cons_none E0 #
∀ {α : Type u_1} {β : Type v} {f : α → Option β} (a : α) (s : Multiset α),
f a = none → Multiset.filterMap f (a ::ₘ s) = Multiset.filterMap f s
theorem Multiset.filterMap_cons_some E0 #
∀ {α : Type u_1} {β : Type v} (f : α → Option β) (a : α) (s : Multiset α) {b : β},
f a = some b → Multiset.filterMap f (a ::ₘ s) = b ::ₘ Multiset.filterMap f s
theorem Multiset.filterMap_cons E1 #
∀ {α : Type u_1} {β : Type v} (f : α → Option β) (a : α) (s : Multiset α),
Multiset.filterMap f (a ::ₘ s) = (Option.map singleton (f a)).getD 0 + Multiset.filterMap f s
theorem Multiset.filterMap_add E0 #
∀ {α : Type u_1} {β : Type v} (f : α → Option β) (s t : Multiset α),
Multiset.filterMap f (s + t) = Multiset.filterMap f s + Multiset.filterMap f t
theorem Multiset.filterMap_eq_map E1 #
∀ {α : Type u_1} {β : Type v} (f : α → β), Multiset.filterMap (some ∘ f) = Multiset.map f
theorem Multiset.filterMap_eq_filter E1 #
∀ {α : Type u_1} (p : α → Prop) [inst : DecidablePred p],
Multiset.filterMap (Option.guard fun b ↦ decide (p b)) = Multiset.filter p
theorem Multiset.filterMap_filterMap E1 #
∀ {α : Type u_1} {β : Type v} {γ : Type u_2} (f : α → Option β) (g : β → Option γ) (s : Multiset α),
Multiset.filterMap g (Multiset.filterMap f s) = Multiset.filterMap (fun x ↦ (f x).bind g) s
theorem Multiset.map_filterMap E0 #
∀ {α : Type u_1} {β : Type v} {γ : Type u_2} (f : α → Option β) (g : β → γ) (s : Multiset α),
Multiset.map g (Multiset.filterMap f s) = Multiset.filterMap (fun x ↦ Option.map g (f x)) s
theorem Multiset.filterMap_map E1 #
∀ {α : Type u_1} {β : Type v} {γ : Type u_2} (f : α → β) (g : β → Option γ) (s : Multiset α),
Multiset.filterMap g (Multiset.map f s) = Multiset.filterMap (g ∘ f) s
theorem Multiset.filter_filterMap E1 #
∀ {α : Type u_1} {β : Type v} (f : α → Option β) (p : β → Prop) [inst : DecidablePred p] (s : Multiset α),
Multiset.filter p (Multiset.filterMap f s) = Multiset.filterMap (fun x ↦ Option.filter (fun b ↦ decide (p b)) (f x)) s
theorem Multiset.filterMap_filter E1 #
∀ {α : Type u_1} {β : Type v} (p : α → Prop) [inst : DecidablePred p] (f : α → Option β) (s : Multiset α),
Multiset.filterMap f (Multiset.filter p s) = Multiset.filterMap (fun x ↦ if p x then f x else none) s
theorem Multiset.filterMap_some E0 #
∀ {α : Type u_1} (s : Multiset α), Multiset.filterMap some s = s
theorem Multiset.mem_filterMap E0 #
∀ {α : Type u_1} {β : Type v} (f : α → Option β) (s : Multiset α) {b : β},
b ∈ Multiset.filterMap f s ↔ ∃ a ∈ s, f a = some b
theorem Multiset.map_filterMap_of_inv E0 #
∀ {α : Type u_1} {β : Type v} (f : α → Option β) (g : β → α),
(∀ (x : α), Option.map g (f x) = some x) → ∀ (s : Multiset α), Multiset.map g (Multiset.filterMap f s) = s
theorem Multiset.filterMap_le_filterMap E1 #
∀ {α : Type u_1} {β : Type v} (f : α → Option β) {s t : Multiset α},
s ≤ t → Multiset.filterMap f s ≤ Multiset.filterMap f t
theorem Multiset.map_filter_eq_filterMap E0 #
∀ {α : Type u_1} {β : Type v} (f : α → β) (p : α → Prop) [inst : DecidablePred p] (s : Multiset α),
Multiset.map f (Multiset.filter p s) = Multiset.filterMap (fun a ↦ if p a then some (f a) else none) s
theorem Multiset.countP_eq_card_filter E1 #
∀ {α : Type u_1} (p : α → Prop) [inst : DecidablePred p] (s : Multiset α),
Multiset.countP p s = (Multiset.filter p s).card
theorem Multiset.countP_filter E0 #
∀ {α : Type u_1} (p : α → Prop) [inst : DecidablePred p] (q : α → Prop) [inst_1 : DecidablePred q] (s : Multiset α),
Multiset.countP p (Multiset.filter q s) = Multiset.countP (fun a ↦ p a ∧ q a) s
theorem Multiset.countP_eq_countP_filter_add E1 #
∀ {α : Type u_1} (s : Multiset α) (p q : α → Prop) [inst : DecidablePred p] [inst_1 : DecidablePred q],
Multiset.countP p s = Multiset.countP p (Multiset.filter q s) + Multiset.countP p (Multiset.filter (fun a ↦ ¬q a) s)
theorem Multiset.countP_map E1 #
∀ {α : Type u_1} {β : Type v} (f : α → β) (s : Multiset α) (p : β → Prop) [inst : DecidablePred p],
Multiset.countP p (Multiset.map f s) = (Multiset.filter (fun a ↦ p (f a)) s).card
theorem Multiset.filter_attach E1 #
∀ {α : Type u_1} (s : Multiset α) (p : α → Prop) [inst : DecidablePred p],
Multiset.filter (fun a ↦ p ↑a) s.attach = Multiset.map (Subtype.map id ⋯) (Multiset.filter p s).attach
theorem Multiset.count_filter_of_pos E0 #
∀ {α : Type u_1} [inst : DecidableEq α] {p : α → Prop} [inst_1 : DecidablePred p] {a : α} {s : Multiset α},
p a → Multiset.count a (Multiset.filter p s) = Multiset.count a s
theorem Multiset.count_filter_of_neg E1 #
∀ {α : Type u_1} [inst : DecidableEq α] {p : α → Prop} [inst_1 : DecidablePred p] {a : α} {s : Multiset α},
¬p a → Multiset.count a (Multiset.filter p s) = 0
theorem Multiset.count_filter E1 #
∀ {α : Type u_1} [inst : DecidableEq α] {p : α → Prop} [inst_1 : DecidablePred p] {a : α} {s : Multiset α},
Multiset.count a (Multiset.filter p s) = if p a then Multiset.count a s else 0
theorem Multiset.count_map E1 #
∀ {α : Type u_3} {β : Type u_4} (f : α → β) (s : Multiset α) [inst : DecidableEq β] (b : β),
Multiset.count b (Multiset.map f s) = (Multiset.filter (fun a ↦ b = f a) s).card
theorem Multiset.count_map_eq_count E1 #
∀ {α : Type u_1} {β : Type v} [inst : DecidableEq α] [inst_1 : DecidableEq β] (f : α → β) (s : Multiset α),
Set.InjOn f {x | x ∈ s} → ∀ x ∈ s, Multiset.count (f x) (Multiset.map f s) = Multiset.count x s
Multiset.map f preserves count if f is injective on the set of elements contained in
the multiset
theorem Multiset.count_map_eq_count' E1 #
∀ {α : Type u_1} {β : Type v} [inst : DecidableEq α] [inst_1 : DecidableEq β] (f : α → β) (s : Multiset α),
Function.Injective f → ∀ (x : α), Multiset.count (f x) (Multiset.map f s) = Multiset.count x s
Multiset.map f preserves count if f is injective
theorem Multiset.filter_eq' E1 #
∀ {α : Type u_1} [inst : DecidableEq α] (s : Multiset α) (b : α),
Multiset.filter (fun x ↦ x = b) s = Multiset.replicate (Multiset.count b s) b
theorem Multiset.filter_eq E0 #
∀ {α : Type u_1} [inst : DecidableEq α] (s : Multiset α) (b : α),
Multiset.filter (Eq b) s = Multiset.replicate (Multiset.count b s) b
theorem Multiset.filter_sub E0 #
∀ {α : Type u_1} [inst : DecidableEq α] (p : α → Prop) [inst_1 : DecidablePred p] (s t : Multiset α),
Multiset.filter p (s - t) = Multiset.filter p s - Multiset.filter p t
theorem Multiset.sub_filter_eq_filter_not E0 #
∀ {α : Type u_1} [inst : DecidableEq α] (p : α → Prop) [inst_1 : DecidablePred p] (s : Multiset α),
s - Multiset.filter p s = Multiset.filter (fun a ↦ ¬p a) s
theorem Multiset.map_le_map_iff E0 #
∀ {α : Type u_1} {β : Type v} {f : α → β},
Function.Injective f → ∀ {s t : Multiset α}, Multiset.map f s ≤ Multiset.map f t ↔ s ≤ t
def Multiset.mapEmbedding E1 #
{α : Type u_1} → {β : Type v} → (α ↪ β) → Multiset α ↪o Multiset β
Associate to an embedding f from α to β the order embedding that maps a multiset to its
image under f.
theorem Multiset.mapEmbedding_apply E0 #
∀ {α : Type u_1} {β : Type v} (f : α ↪ β) (s : Multiset α), (Multiset.mapEmbedding f) s = Multiset.map (⇑f) s
theorem Multiset.count_eq_card_filter_eq E0 #
∀ {α : Type u_1} [inst : DecidableEq α] (s : Multiset α) (a : α),
Multiset.count a s = (Multiset.filter (fun x ↦ a = x) s).card
theorem Multiset.map_count_true_eq_filter_card E0 #
∀ {α : Type u_1} (s : Multiset α) (p : α → Prop) [inst : DecidablePred p] [inst_1 : DecidableEq Prop],
Multiset.count True (Multiset.map p s) = (Multiset.filter p s).card
Mapping a multiset through a predicate and counting the Trues yields the cardinality of the set
filtered by the predicate. Note that this uses the notion of a multiset of Props - due to the
decidability requirements of count, the decidability instance on the LHS is different from the
RHS. In particular, the decidability instance on the left leaks Classical.decEq.
See here
for more discussion.
theorem Multiset.filter_attach' E1 #
∀ {α : Type u_1} (s : Multiset α) (p : { a // a ∈ s } → Prop) [inst : DecidableEq α] [inst_1 : DecidablePred p],
Multiset.filter p s.attach =
Multiset.map (Subtype.map id ⋯) (Multiset.filter (fun x ↦ ∃ (h : x ∈ s), p ⟨x, h⟩) s).attach
theorem Multiset.Nodup.filter E1 #
∀ {α : Type u_1} (p : α → Prop) [inst : DecidablePred p] {s : Multiset α}, s.Nodup → (Multiset.filter p s).Nodup
theorem Multiset.Nodup.erase_eq_filter E1 #
∀ {α : Type u_1} [inst : DecidableEq α] (a : α) {s : Multiset α},
s.Nodup → s.erase a = Multiset.filter (fun x ↦ x ≠ a) s
theorem Multiset.Nodup.filterMap E1 #
∀ {α : Type u_1} {β : Type v} {s : Multiset α} (f : α → Option β),
(∀ (a a' : α), ∀ b ∈ f a, b ∈ f a' → a = a') → s.Nodup → (Multiset.filterMap f s).Nodup
theorem Multiset.Nodup.mem_erase_iff E0 #
∀ {α : Type u_1} [inst : DecidableEq α] {a b : α} {l : Multiset α}, l.Nodup → (a ∈ l.erase b ↔ a ≠ b ∧ a ∈ l)
theorem Multiset.Nodup.notMem_erase E1 #
∀ {α : Type u_1} [inst : DecidableEq α] {a : α} {s : Multiset α}, s.Nodup → a ∉ s.erase a
Deprecated aliases
| Name | New name | Since |
|---|---|---|
Multiset.map_count_True_eq_filter_card | Multiset.map_count_true_eq_filter_card | 2026-10-27 |
Multiset.map_filter' | Multiset.map_filter | 2026-08-20 |