Module docstring
{"# The fold operation for a commutative associative operation over a multiset. ","### fold "}
{"# The fold operation for a commutative associative operation over a multiset. ","### fold "}
Multiset.fold
definition
: α → Multiset α → α
Multiset.fold_eq_foldr
theorem
(b : α) (s : Multiset α) : fold op b s = foldr op b s
theorem fold_eq_foldr (b : α) (s : Multiset α) :
fold op b s = foldr op b s :=
rfl
Multiset.coe_fold_r
theorem
(b : α) (l : List α) : fold op b l = l.foldr op b
@[simp]
theorem coe_fold_r (b : α) (l : List α) : fold op b l = l.foldr op b :=
rfl
Multiset.coe_fold_l
theorem
(b : α) (l : List α) : fold op b l = l.foldl op b
theorem coe_fold_l (b : α) (l : List α) : fold op b l = l.foldl op b :=
(coe_foldr_swap op b l).trans <| by simp [hc.comm]
Multiset.fold_eq_foldl
theorem
(b : α) (s : Multiset α) : fold op b s = foldl op b s
theorem fold_eq_foldl (b : α) (s : Multiset α) :
fold op b s = foldl op b s :=
Quot.inductionOn s fun _ => coe_fold_l _ _ _
Multiset.fold_zero
theorem
(b : α) : (0 : Multiset α).fold op b = b
Multiset.fold_cons_left
theorem
: ∀ (b a : α) (s : Multiset α), (a ::ₘ s).fold op b = a * s.fold op b
@[simp]
theorem fold_cons_left : ∀ (b a : α) (s : Multiset α), (a ::ₘ s).fold op b = a * s.fold op b :=
foldr_cons _
Multiset.fold_cons_right
theorem
(b a : α) (s : Multiset α) : (a ::ₘ s).fold op b = s.fold op b * a
Multiset.fold_cons'_right
theorem
(b a : α) (s : Multiset α) : (a ::ₘ s).fold op b = s.fold op (b * a)
theorem fold_cons'_right (b a : α) (s : Multiset α) : (a ::ₘ s).fold op b = s.fold op (b * a) := by
rw [fold_eq_foldl, foldl_cons, ← fold_eq_foldl]
Multiset.fold_cons'_left
theorem
(b a : α) (s : Multiset α) : (a ::ₘ s).fold op b = s.fold op (a * b)
theorem fold_cons'_left (b a : α) (s : Multiset α) : (a ::ₘ s).fold op b = s.fold op (a * b) := by
rw [fold_cons'_right, hc.comm]
Multiset.fold_add
theorem
(b₁ b₂ : α) (s₁ s₂ : Multiset α) : (s₁ + s₂).fold op (b₁ * b₂) = s₁.fold op b₁ * s₂.fold op b₂
theorem fold_add (b₁ b₂ : α) (s₁ s₂ : Multiset α) :
(s₁ + s₂).fold op (b₁ * b₂) = s₁.fold op b₁ * s₂.fold op b₂ :=
Multiset.induction_on s₂
(by rw [Multiset.add_zero, fold_zero, ← fold_cons'_right, ← fold_cons_right op])
(fun a b h => by rw [fold_cons_left, add_cons, fold_cons_left, h, ← ha.assoc, hc.comm a,
ha.assoc])
Multiset.fold_singleton
theorem
(b a : α) : ({ a } : Multiset α).fold op b = a * b
theorem fold_singleton (b a : α) : ({a} : Multiset α).fold op b = a * b :=
foldr_singleton _ _ _
Multiset.fold_distrib
theorem
{f g : β → α} (u₁ u₂ : α) (s : Multiset β) :
(s.map fun x => f x * g x).fold op (u₁ * u₂) = (s.map f).fold op u₁ * (s.map g).fold op u₂
theorem fold_distrib {f g : β → α} (u₁ u₂ : α) (s : Multiset β) :
(s.map fun x => f x * g x).fold op (u₁ * u₂) = (s.map f).fold op u₁ * (s.map g).fold op u₂ :=
Multiset.induction_on s (by simp) (fun a b h => by
rw [map_cons, fold_cons_left, h, map_cons, fold_cons_left, map_cons,
fold_cons_right, ha.assoc, ← ha.assoc (g a), hc.comm (g a),
ha.assoc, hc.comm (g a), ha.assoc])
Multiset.fold_hom
theorem
{op' : β → β → β} [Std.Commutative op'] [Std.Associative op'] {m : α → β} (hm : ∀ x y, m (op x y) = op' (m x) (m y))
(b : α) (s : Multiset α) : (s.map m).fold op' (m b) = m (s.fold op b)
theorem fold_hom {op' : β → β → β} [Std.Commutative op'] [Std.Associative op'] {m : α → β}
(hm : ∀ x y, m (op x y) = op' (m x) (m y)) (b : α) (s : Multiset α) :
(s.map m).fold op' (m b) = m (s.fold op b) :=
Multiset.induction_on s (by simp) (by simp +contextual [hm])
Multiset.fold_union_inter
theorem
[DecidableEq α] (s₁ s₂ : Multiset α) (b₁ b₂ : α) :
((s₁ ∪ s₂).fold op b₁ * (s₁ ∩ s₂).fold op b₂) = s₁.fold op b₁ * s₂.fold op b₂
theorem fold_union_inter [DecidableEq α] (s₁ s₂ : Multiset α) (b₁ b₂ : α) :
((s₁ ∪ s₂).fold op b₁ * (s₁ ∩ s₂).fold op b₂) = s₁.fold op b₁ * s₂.fold op b₂ := by
rw [← fold_add op, union_add_inter, fold_add op]
Multiset.fold_dedup_idem
theorem
[DecidableEq α] [hi : Std.IdempotentOp op] (s : Multiset α) (b : α) : (dedup s).fold op b = s.fold op b
@[simp]
theorem fold_dedup_idem [DecidableEq α] [hi : Std.IdempotentOp op] (s : Multiset α) (b : α) :
(dedup s).fold op b = s.fold op b :=
Multiset.induction_on s (by simp) fun a s IH => by
by_cases h : a ∈ s <;> simp [IH, h]
show fold op b s = op a (fold op b s)
rw [← cons_erase h, fold_cons_left, ← ha.assoc, hi.idempotent]