The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V03. Sets, finite structures and big operators

Mathlib.Data.Finset.Fold

31 declarations. Mathlib docs · Source

The fold operation for a commutative associative operation over a finset.

fold

theorem Finset.fold.congr_simp E1 #

∀ {α : Type u_1} {β : Type u_2} (op op_1 : β → β → β) (e_op : op = op_1) [hc : Std.Commutative op]
  [ha : Std.Associative op] (b b_1 : β),
  b = b_1 →
    ∀ (f f_1 : α → β), f = f_1 → ∀ (s s_1 : Finset α), s = s_1 → Finset.fold op b f s = Finset.fold op_1 b_1 f_1 s_1

def Finset.fold E1 #

{α : Type u_1} →
  {β : Type u_2} → (op : β → β → β) → [hc : Std.Commutative op] → [ha : Std.Associative op] → β → (α → β) → Finset α → β

fold op b f s folds the commutative associative operation op over the f-image of s, i.e. fold (+) b f {1,2,3} = f 1 + f 2 + f 3 + b.

theorem Finset.fold_empty E0 #

∀ {α : Type u_1} {β : Type u_2} {op : β → β → β} [hc : Std.Commutative op] [ha : Std.Associative op] {f : α → β}
  {b : β}, Finset.fold op b f ∅ = b

theorem Finset.fold_cons E0 #

∀ {α : Type u_1} {β : Type u_2} {op : β → β → β} [hc : Std.Commutative op] [ha : Std.Associative op] {f : α → β} {b : β}
  {s : Finset α} {a : α} (h : a ∉ s), Finset.fold op b f (Finset.cons a s h) = op (f a) (Finset.fold op b f s)

theorem Finset.fold_insert E0 #

∀ {α : Type u_1} {β : Type u_2} {op : β → β → β} [hc : Std.Commutative op] [ha : Std.Associative op] {f : α → β} {b : β}
  {s : Finset α} {a : α} [inst : DecidableEq α],
  a ∉ s → Finset.fold op b f (insert a s) = op (f a) (Finset.fold op b f s)

theorem Finset.fold_singleton E0 #

∀ {α : Type u_1} {β : Type u_2} {op : β → β → β} [hc : Std.Commutative op] [ha : Std.Associative op] {f : α → β} {b : β}
  {a : α}, Finset.fold op b f {a} = op (f a) b

theorem Finset.fold_map E0 #

∀ {α : Type u_1} {β : Type u_2} {γ : Type u_3} {op : β → β → β} [hc : Std.Commutative op] [ha : Std.Associative op]
  {f : α → β} {b : β} {g : γ ↪ α} {s : Finset γ}, Finset.fold op b f (Finset.map g s) = Finset.fold op b (f ∘ ⇑g) s

theorem Finset.fold_image E0 #

∀ {α : Type u_1} {β : Type u_2} {γ : Type u_3} {op : β → β → β} [hc : Std.Commutative op] [ha : Std.Associative op]
  {f : α → β} {b : β} [inst : DecidableEq α] {g : γ → α} {s : Finset γ},
  Set.InjOn g ↑s → Finset.fold op b f (Finset.image g s) = Finset.fold op b (f ∘ g) s

theorem Finset.fold_congr E1 #

∀ {α : Type u_1} {β : Type u_2} {op : β → β → β} [hc : Std.Commutative op] [ha : Std.Associative op] {f : α → β} {b : β}
  {s : Finset α} {g : α → β}, (∀ x ∈ s, f x = g x) → Finset.fold op b f s = Finset.fold op b g s

theorem Finset.fold_congr' E1 #

∀ {α : Type u_1} {β : Type u_2} {op : β → β → β} [hc : Std.Commutative op] [ha : Std.Associative op] {f : α → β} {b : β}
  {s : Finset α} {op' : β → β → β} [inst : Std.Commutative op'] [inst_1 : Std.Associative op'] {g : α → β} {b' : β}
  {t : Finset α}, op = op' → b = b' → s = t → (∀ x ∈ s, f x = g x) → Finset.fold op b f s = Finset.fold op' b' g t

theorem Finset.fold_op_distrib E1 #

∀ {α : Type u_1} {β : Type u_2} {op : β → β → β} [hc : Std.Commutative op] [ha : Std.Associative op] {s : Finset α}
  {f g : α → β} {b₁ b₂ : β},
  Finset.fold op (op b₁ b₂) (fun x ↦ op (f x) (g x)) s = op (Finset.fold op b₁ f s) (Finset.fold op b₂ g s)

theorem Finset.fold_const E1 #

∀ {α : Type u_1} {β : Type u_2} {op : β → β → β} [hc : Std.Commutative op] [ha : Std.Associative op] {b : β}
  {s : Finset α} [hd : Decidable (s = ∅)] (c : β),
  op c (op b c) = op b c → Finset.fold op b (fun x ↦ c) s = if s = ∅ then b else op b c

theorem Finset.fold_hom E1 #

∀ {α : Type u_1} {β : Type u_2} {γ : Type u_3} {op : β → β → β} [hc : Std.Commutative op] [ha : Std.Associative op]
  {f : α → β} {b : β} {s : Finset α} {op' : γ → γ → γ} [inst : Std.Commutative op'] [inst_1 : Std.Associative op']
  {m : β → γ},
  (∀ (x y : β), m (op x y) = op' (m x) (m y)) → Finset.fold op' (m b) (fun x ↦ m (f x)) s = m (Finset.fold op b f s)

theorem Finset.fold_disjUnion E1 #

∀ {α : Type u_1} {β : Type u_2} {op : β → β → β} [hc : Std.Commutative op] [ha : Std.Associative op] {f : α → β}
  {s₁ s₂ : Finset α} {b₁ b₂ : β} (h : Disjoint s₁ s₂),
  Finset.fold op (op b₁ b₂) f (s₁.disjUnion s₂ h) = op (Finset.fold op b₁ f s₁) (Finset.fold op b₂ f s₂)

theorem Finset.fold_union_inter E1 #

∀ {α : Type u_1} {β : Type u_2} {op : β → β → β} [hc : Std.Commutative op] [ha : Std.Associative op] {f : α → β}
  [inst : DecidableEq α] {s₁ s₂ : Finset α} {b₁ b₂ : β},
  op (Finset.fold op b₁ f (s₁ ∪ s₂)) (Finset.fold op b₂ f (s₁ ∩ s₂)) =
    op (Finset.fold op b₂ f s₁) (Finset.fold op b₁ f s₂)

theorem Finset.fold_insert_idem E0 #

∀ {α : Type u_1} {β : Type u_2} {op : β → β → β} [hc : Std.Commutative op] [ha : Std.Associative op] {f : α → β} {b : β}
  {s : Finset α} {a : α} [inst : DecidableEq α] [hi : Std.IdempotentOp op],
  Finset.fold op b f (insert a s) = op (f a) (Finset.fold op b f s)

theorem Finset.fold_image_idem E1 #

∀ {α : Type u_1} {β : Type u_2} {γ : Type u_3} {op : β → β → β} [hc : Std.Commutative op] [ha : Std.Associative op]
  {f : α → β} {b : β} [inst : DecidableEq α] {g : γ → α} {s : Finset γ} [hi : Std.IdempotentOp op],
  Finset.fold op b f (Finset.image g s) = Finset.fold op b (f ∘ g) s

theorem Finset.fold_ite' E1 #

∀ {α : Type u_1} {β : Type u_2} {op : β → β → β} [hc : Std.Commutative op] [ha : Std.Associative op] {f : α → β} {b : β}
  {s : Finset α} {g : α → β},
  op b b = b →
    ∀ (p : α → Prop) [inst : DecidablePred p],
      Finset.fold op b (fun i ↦ if p i then f i else g i) s =
        op (Finset.fold op b f (Finset.filter p s)) (Finset.fold op b g ({i ∈ s | ¬p i}))

A stronger version of Finset.fold_ite, but relies on an explicit proof of idempotency on the seed element, rather than relying on typeclass idempotency over the whole type.

theorem Finset.fold_ite E1 #

∀ {α : Type u_1} {β : Type u_2} {op : β → β → β} [hc : Std.Commutative op] [ha : Std.Associative op] {f : α → β} {b : β}
  {s : Finset α} [Std.IdempotentOp op] {g : α → β} (p : α → Prop) [inst : DecidablePred p],
  Finset.fold op b (fun i ↦ if p i then f i else g i) s =
    op (Finset.fold op b f (Finset.filter p s)) (Finset.fold op b g ({i ∈ s | ¬p i}))

A weaker version of Finset.fold_ite', relying on typeclass idempotency over the whole type, instead of solely on the seed element. However, this is easier to use because it does not generate side goals.

theorem Finset.fold_op_rel_iff_and E1 #

∀ {α : Type u_1} {β : Type u_2} {op : β → β → β} [hc : Std.Commutative op] [ha : Std.Associative op] {f : α → β} {b : β}
  {s : Finset α} {r : β → β → Prop},
  (∀ {x y z : β}, r x (op y z) ↔ r x y ∧ r x z) → ∀ {c : β}, r c (Finset.fold op b f s) ↔ r c b ∧ ∀ x ∈ s, r c (f x)

theorem Finset.fold_op_rel_iff_or E1 #

∀ {α : Type u_1} {β : Type u_2} {op : β → β → β} [hc : Std.Commutative op] [ha : Std.Associative op] {f : α → β} {b : β}
  {s : Finset α} {r : β → β → Prop},
  (∀ {x y z : β}, r x (op y z) ↔ r x y ∨ r x z) → ∀ {c : β}, r c (Finset.fold op b f s) ↔ r c b ∨ ∃ x ∈ s, r c (f x)

theorem Finset.fold_union_empty_singleton E0 #

∀ {α : Type u_1} [inst : DecidableEq α] (s : Finset α), Finset.fold (fun x1 x2 ↦ x1 ∪ x2) ∅ singleton s = s

theorem Finset.fold_sup_bot_singleton E1 #

∀ {α : Type u_1} [inst : DecidableEq α] (s : Finset α), Finset.fold (fun x1 x2 ↦ x1 ∪ x2) ⊥ singleton s = s

theorem Finset.le_fold_min E1 #

∀ {α : Type u_1} {β : Type u_2} {f : α → β} {b : β} {s : Finset α} [inst : LinearOrder β] (c : β),
  c ≤ Finset.fold min b f s ↔ c ≤ b ∧ ∀ x ∈ s, c ≤ f x

theorem Finset.fold_min_le E1 #

∀ {α : Type u_1} {β : Type u_2} {f : α → β} {b : β} {s : Finset α} [inst : LinearOrder β] (c : β),
  Finset.fold min b f s ≤ c ↔ b ≤ c ∨ ∃ x ∈ s, f x ≤ c

theorem Finset.lt_fold_min E1 #

∀ {α : Type u_1} {β : Type u_2} {f : α → β} {b : β} {s : Finset α} [inst : LinearOrder β] (c : β),
  c < Finset.fold min b f s ↔ c < b ∧ ∀ x ∈ s, c < f x

theorem Finset.fold_min_lt E1 #

∀ {α : Type u_1} {β : Type u_2} {f : α → β} {b : β} {s : Finset α} [inst : LinearOrder β] (c : β),
  Finset.fold min b f s < c ↔ b < c ∨ ∃ x ∈ s, f x < c

theorem Finset.fold_max_le E1 #

∀ {α : Type u_1} {β : Type u_2} {f : α → β} {b : β} {s : Finset α} [inst : LinearOrder β] (c : β),
  Finset.fold max b f s ≤ c ↔ b ≤ c ∧ ∀ x ∈ s, f x ≤ c

theorem Finset.le_fold_max E1 #

∀ {α : Type u_1} {β : Type u_2} {f : α → β} {b : β} {s : Finset α} [inst : LinearOrder β] (c : β),
  c ≤ Finset.fold max b f s ↔ c ≤ b ∨ ∃ x ∈ s, c ≤ f x

theorem Finset.fold_max_lt E1 #

∀ {α : Type u_1} {β : Type u_2} {f : α → β} {b : β} {s : Finset α} [inst : LinearOrder β] (c : β),
  Finset.fold max b f s < c ↔ b < c ∧ ∀ x ∈ s, f x < c

theorem Finset.lt_fold_max E1 #

∀ {α : Type u_1} {β : Type u_2} {f : α → β} {b : β} {s : Finset α} [inst : LinearOrder β] (c : β),
  c < Finset.fold max b f s ↔ c < b ∨ ∃ x ∈ s, c < f x