doc-next-gen

Mathlib.Data.Finset.Fold

Module docstring

{"# The fold operation for a commutative associative operation over a finset. ","### fold "}

Finset.fold definition
(b : β) (f : α → β) (s : Finset α) : β
Full source
/-- `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`. -/
def fold (b : β) (f : α → β) (s : Finset α) : β :=
  (s.1.map f).fold op b
Folding a commutative associative operation over a finite set
Informal description
Given a commutative and associative binary operation `op`, a starting value `b`, a function `f : α → β`, and a finite set `s : Finset α`, the function `Finset.fold op b f s` computes the result of applying `op` to the images `f x` for all `x ∈ s`, combined with `b`. More precisely, it computes `f x₁ * f x₂ * ... * f xₙ * b` where `{x₁, ..., xₙ} = s` and `*` denotes the operation `op`. The order of operations does not matter due to the commutativity and associativity of `op`.
Finset.fold_empty theorem
: (∅ : Finset α).fold op b f = b
Full source
@[simp]
theorem fold_empty : ( : Finset α).fold op b f = b :=
  rfl
Empty Set Fold Property: $\text{fold } * \text{ } b \text{ } f \text{ } \emptyset = b$
Informal description
For any commutative and associative binary operation $*$, any starting value $b \in \beta$, and any function $f : \alpha \to \beta$, the fold operation over the empty set yields $b$, i.e., $$\text{fold } * \text{ } b \text{ } f \text{ } \emptyset = b.$$
Finset.fold_cons theorem
(h : a ∉ s) : (cons a s h).fold op b f = f a * s.fold op b f
Full source
@[simp]
theorem fold_cons (h : a ∉ s) : (cons a s h).fold op b f = f a * s.fold op b f := by
  dsimp only [fold]
  rw [cons_val, Multiset.map_cons, fold_cons_left]
Fold operation on finite set with insertion: $(\text{cons } a \text{ } s).\text{fold } * \text{ } b \text{ } f = f(a) * (s.\text{fold } * \text{ } b \text{ } f)$
Informal description
For any commutative and associative binary operation $*$ on a type $\beta$, any starting value $b \in \beta$, any function $f : \alpha \to \beta$, and any finite set $s \subseteq \alpha$ not containing an element $a \in \alpha$, the fold operation satisfies $$(\text{cons } a \text{ } s).\text{fold } * \text{ } b \text{ } f = f(a) * (s.\text{fold } * \text{ } b \text{ } f),$$ where $\text{cons } a \text{ } s$ denotes the finite set obtained by inserting $a$ into $s$.
Finset.fold_insert theorem
[DecidableEq α] (h : a ∉ s) : (insert a s).fold op b f = f a * s.fold op b f
Full source
@[simp]
theorem fold_insert [DecidableEq α] (h : a ∉ s) :
    (insert a s).fold op b f = f a * s.fold op b f := by
  unfold fold
  rw [insert_val, ndinsert_of_not_mem h, Multiset.map_cons, fold_cons_left]
Insertion Property of Fold Operation: $(\text{insert } a \text{ } s).\text{fold } * \text{ } b \text{ } f = f(a) * (s.\text{fold } * \text{ } b \text{ } f)$
Informal description
Let $*$ be a commutative and associative binary operation on a type $\beta$, $b \in \beta$, $f : \alpha \to \beta$, and $s$ a finite set of elements of $\alpha$. For any element $a \notin s$, we have $$(\text{insert } a \text{ } s).\text{fold } * \text{ } b \text{ } f = f(a) * (s.\text{fold } * \text{ } b \text{ } f).$$
Finset.fold_singleton theorem
: ({ a } : Finset α).fold op b f = f a * b
Full source
@[simp]
theorem fold_singleton : ({a} : Finset α).fold op b f = f a * b :=
  rfl
Fold of Singleton Set: $\text{fold}_{*}\, b\, f\, \{a\} = f(a) * b$
Informal description
For any commutative and associative binary operation $*$ on a type $\beta$, any starting value $b \in \beta$, any function $f : \alpha \to \beta$, and any singleton set $\{a\} \subseteq \alpha$, the fold operation satisfies $$ \text{fold}_{*}\, b\, f\, \{a\} = f(a) * b. $$
Finset.fold_map theorem
{g : γ ↪ α} {s : Finset γ} : (s.map g).fold op b f = s.fold op b (f ∘ g)
Full source
@[simp]
theorem fold_map {g : γ ↪ α} {s : Finset γ} : (s.map g).fold op b f = s.fold op b (f ∘ g) := by
  simp only [fold, map, Multiset.map_map]
Equality of Folds under Injective Mapping: $\text{fold}_{*}\, b\, f\, (g(s)) = \text{fold}_{*}\, b\, (f \circ g)\, s$
Informal description
Let $*$ be a commutative and associative binary operation on a type $\beta$, $b \in \beta$ a starting value, $f : \alpha \to \beta$ a function, and $g : \gamma \hookrightarrow \alpha$ an injective function embedding. For any finite set $s \subseteq \gamma$, the fold operation satisfies: \[ \text{fold}_{*}\, b\, f\, (g(s)) = \text{fold}_{*}\, b\, (f \circ g)\, s. \] Here $g(s)$ denotes the image of $s$ under $g$.
Finset.fold_image theorem
[DecidableEq α] {g : γ → α} {s : Finset γ} (H : ∀ x ∈ s, ∀ y ∈ s, g x = g y → x = y) : (s.image g).fold op b f = s.fold op b (f ∘ g)
Full source
@[simp]
theorem fold_image [DecidableEq α] {g : γ → α} {s : Finset γ}
    (H : ∀ x ∈ s, ∀ y ∈ s, g x = g y → x = y) : (s.image g).fold op b f = s.fold op b (f ∘ g) := by
  simp only [fold, image_val_of_injOn H, Multiset.map_map]
Equality of Folds under Injective Image
Informal description
Let $\alpha$ and $\gamma$ be types with a decidable equality on $\alpha$, and let $g : \gamma \to \alpha$ be an injective function on a finite set $s \subseteq \gamma$ (i.e., for any $x, y \in s$, if $g(x) = g(y)$, then $x = y$). Let $op$ be a commutative and associative binary operation on $\beta$, with an identity element $b \in \beta$, and let $f : \alpha \to \beta$ be a function. Then, the fold of $f$ over the image of $s$ under $g$ is equal to the fold of $f \circ g$ over $s$: \[ \text{fold}_{op}\, b\, f\, (g(s)) = \text{fold}_{op}\, b\, (f \circ g)\, s. \]
Finset.fold_congr theorem
{g : α → β} (H : ∀ x ∈ s, f x = g x) : s.fold op b f = s.fold op b g
Full source
@[congr]
theorem fold_congr {g : α → β} (H : ∀ x ∈ s, f x = g x) : s.fold op b f = s.fold op b g := by
  rw [fold, fold, map_congr rfl H]
Congruence of Folds under Function Equality
Informal description
Let $s$ be a finite set, $op$ a commutative and associative binary operation, $b$ a starting value, and $f, g : \alpha \to \beta$ functions such that $f(x) = g(x)$ for all $x \in s$. Then the fold operation over $s$ with operation $op$, starting value $b$, and function $f$ is equal to the fold operation with the same operation and starting value but using function $g$ instead, i.e., \[ \text{fold}_{op}\, b\, f\, s = \text{fold}_{op}\, b\, g\, s. \]
Finset.fold_op_distrib theorem
{f g : α → β} {b₁ b₂ : β} : (s.fold op (b₁ * b₂) fun x => f x * g x) = s.fold op b₁ f * s.fold op b₂ g
Full source
theorem fold_op_distrib {f g : α → β} {b₁ b₂ : β} :
    (s.fold op (b₁ * b₂) fun x => f x * g x) = s.fold op b₁ f * s.fold op b₂ g := by
  simp only [fold, fold_distrib]
Distributivity of Fold over Pointwise Product: $\text{fold}(*, b_1 * b_2, f \cdot g, s) = \text{fold}(*, b_1, f, s) * \text{fold}(*, b_2, g, s)$
Informal description
Let $*$ be a commutative and associative binary operation on a type $\beta$, and let $f, g : \alpha \to \beta$ be functions. For any elements $b_1, b_2 \in \beta$ and any finite set $s \subseteq \alpha$, the following equality holds: \[ \text{fold}_{*}\, (b_1 * b_2)\, (\lambda x.\, f(x) * g(x))\, s = \text{fold}_{*}\, b_1\, f\, s * \text{fold}_{*}\, b_2\, g\, s. \]
Finset.fold_const theorem
[hd : Decidable (s = ∅)] (c : β) (h : op c (op b c) = op b c) : Finset.fold op b (fun _ => c) s = if s = ∅ then b else op b c
Full source
theorem fold_const [hd : Decidable (s = )] (c : β) (h : op c (op b c) = op b c) :
    Finset.fold op b (fun _ => c) s = if s = ∅ then b else op b c := by
  classical
    induction' s using Finset.induction_on with x s hx IH generalizing hd
    · simp
    · simp only [Finset.fold_insert hx, IH, if_false, Finset.insert_ne_empty]
      split_ifs
      · rw [hc.comm]
      · exact h
Fold of Constant Function: $\text{fold}_{*}\, b\, (\lambda x.\, c)\, s = \text{if } s = \emptyset \text{ then } b \text{ else } b * c$
Informal description
Let $*$ be a commutative and associative binary operation on a type $\beta$, $b \in \beta$, and $s$ a finite set. For any constant function $f(x) = c$ where $c \in \beta$ satisfies $c * (b * c) = b * c$, the fold operation over $s$ with operation $*$, starting value $b$, and constant function $f$ is equal to $b$ if $s$ is empty, and $b * c$ otherwise. That is, \[ \text{fold}_{*}\, b\, (\lambda x.\, c)\, s = \begin{cases} b & \text{if } s = \emptyset, \\ b * c & \text{otherwise.} \end{cases} \]
Finset.fold_hom theorem
{op' : γ → γ → γ} [Std.Commutative op'] [Std.Associative op'] {m : β → γ} (hm : ∀ x y, m (op x y) = op' (m x) (m y)) : (s.fold op' (m b) fun x => m (f x)) = m (s.fold op b f)
Full source
theorem fold_hom {op' : γ → γ → γ} [Std.Commutative op'] [Std.Associative op'] {m : β → γ}
    (hm : ∀ x y, m (op x y) = op' (m x) (m y)) :
    (s.fold op' (m b) fun x => m (f x)) = m (s.fold op b f) := by
  rw [fold, fold, ← Multiset.fold_hom op hm, Multiset.map_map]
  simp only [Function.comp_apply]
Homomorphism Property of Finite Set Fold: $m(\text{fold}_{*}\, b\, f\, s) = \text{fold}_{*'}\, m(b)\, (m \circ f)\, s$
Informal description
Let $*$ and $*'$ be commutative and associative binary operations on types $\beta$ and $\gamma$ respectively, and let $m : \beta \to \gamma$ be a homomorphism satisfying $m(x * y) = m(x) *' m(y)$ for all $x, y \in \beta$. Then for any starting value $b \in \beta$, any function $f : \alpha \to \beta$, and any finite set $s \subseteq \alpha$, the fold of the images $m(f(x))$ over $s$ with operation $*'$ and starting value $m(b)$ equals the image under $m$ of the fold of $f$ over $s$ with operation $*$ and starting value $b$. That is, \[ \text{fold}_{*'}~m(b)~(m \circ f)~s = m\left(\text{fold}_{*}~b~f~s\right). \]
Finset.fold_disjUnion theorem
{s₁ s₂ : Finset α} {b₁ b₂ : β} (h) : (s₁.disjUnion s₂ h).fold op (b₁ * b₂) f = s₁.fold op b₁ f * s₂.fold op b₂ f
Full source
theorem fold_disjUnion {s₁ s₂ : Finset α} {b₁ b₂ : β} (h) :
    (s₁.disjUnion s₂ h).fold op (b₁ * b₂) f = s₁.fold op b₁ f * s₂.fold op b₂ f :=
  (congr_arg _ <| Multiset.map_add _ _ _).trans (Multiset.fold_add _ _ _ _ _)
Additivity of Fold over Disjoint Union: $\text{fold}_{*}\, (b_1 * b_2)\, f\, (s_1 \sqcup s_2) = (\text{fold}_{*}\, b_1\, f\, s_1) * (\text{fold}_{*}\, b_2\, f\, s_2)$
Informal description
Let $*$ be a commutative and associative binary operation on a type $\beta$. Given two disjoint finite sets $s_1, s_2 \subseteq \alpha$ (with disjointness witnessed by $h$), and two starting values $b_1, b_2 \in \beta$, the fold of a function $f : \alpha \to \beta$ over the disjoint union $s_1 \sqcup s_2$ with initial value $b_1 * b_2$ is equal to the product of the folds over $s_1$ and $s_2$ with initial values $b_1$ and $b_2$ respectively. That is, \[ \text{fold}_{*}\, (b_1 * b_2)\, f\, (s_1 \sqcup s_2) = (\text{fold}_{*}\, b_1\, f\, s_1) * (\text{fold}_{*}\, b_2\, f\, s_2). \]
Finset.fold_union_inter theorem
[DecidableEq α] {s₁ s₂ : Finset α} {b₁ b₂ : β} : ((s₁ ∪ s₂).fold op b₁ f * (s₁ ∩ s₂).fold op b₂ f) = s₁.fold op b₂ f * s₂.fold op b₁ f
Full source
theorem fold_union_inter [DecidableEq α] {s₁ s₂ : Finset α} {b₁ b₂ : β} :
    ((s₁ ∪ s₂).fold op b₁ f * (s₁ ∩ s₂).fold op b₂ f) = s₁.fold op b₂ f * s₂.fold op b₁ f := by
  unfold fold
  rw [← fold_add op, ← Multiset.map_add, union_val, inter_val, union_add_inter, Multiset.map_add,
    hc.comm, fold_add]
Union-Intersection Fold Identity: $\text{fold}(*, b_1, f, s_1 \cup s_2) * \text{fold}(*, b_2, f, s_1 \cap s_2) = \text{fold}(*, b_2, f, s_1) * \text{fold}(*, b_1, f, s_2)$
Informal description
Let $*$ be a commutative and associative binary operation on a type $\beta$. For any two finite sets $s_1, s_2 \subseteq \alpha$ (with decidable equality) and any elements $b_1, b_2 \in \beta$, the following identity holds: \[ \text{fold}_{*}\, b_1\, f\, (s_1 \cup s_2) * \text{fold}_{*}\, b_2\, f\, (s_1 \cap s_2) = \text{fold}_{*}\, b_2\, f\, s_1 * \text{fold}_{*}\, b_1\, f\, s_2, \] where $\text{fold}_{*}\, b\, f\, s$ denotes the fold of function $f$ over set $s$ with operation $*$ and initial value $b$.
Finset.fold_insert_idem theorem
[DecidableEq α] [hi : Std.IdempotentOp op] : (insert a s).fold op b f = f a * s.fold op b f
Full source
@[simp]
theorem fold_insert_idem [DecidableEq α] [hi : Std.IdempotentOp op] :
    (insert a s).fold op b f = f a * s.fold op b f := by
  by_cases h : a ∈ s
  · rw [← insert_erase h]
    simp [← ha.assoc, hi.idempotent]
  · apply fold_insert h
Insertion Property of Fold Operation for Idempotent Operations
Informal description
Let $*$ be a commutative and associative binary operation on a type $\beta$ that is also idempotent (i.e., $x * x = x$ for all $x \in \beta$). Given $b \in \beta$, a function $f : \alpha \to \beta$, and a finite set $s \subseteq \alpha$, then for any element $a \in \alpha$ (regardless of whether $a \in s$), we have $$(\text{insert } a \text{ } s).\text{fold } * \text{ } b \text{ } f = f(a) * (s.\text{fold } * \text{ } b \text{ } f).$$
Finset.fold_image_idem theorem
[DecidableEq α] {g : γ → α} {s : Finset γ} [hi : Std.IdempotentOp op] : (image g s).fold op b f = s.fold op b (f ∘ g)
Full source
theorem fold_image_idem [DecidableEq α] {g : γ → α} {s : Finset γ} [hi : Std.IdempotentOp op] :
    (image g s).fold op b f = s.fold op b (f ∘ g) := by
  induction' s using Finset.cons_induction with x xs hx ih
  · rw [fold_empty, image_empty, fold_empty]
  · haveI := Classical.decEq γ
    rw [fold_cons, cons_eq_insert, image_insert, fold_insert_idem, ih]
    simp only [Function.comp_apply]
Image Fold Identity for Idempotent Operations: $\text{fold}_{*}\, b\, f\, (g(s)) = \text{fold}_{*}\, b\, (f \circ g)\, s$
Informal description
Let $*$ be a commutative and associative binary operation on a type $\beta$ that is also idempotent (i.e., $x * x = x$ for all $x \in \beta$). Given a function $g : \gamma \to \alpha$, a finite set $s \subseteq \gamma$, a starting value $b \in \beta$, and a function $f : \alpha \to \beta$, the fold operation satisfies \[ \text{fold}_{*}\, b\, f\, (g(s)) = \text{fold}_{*}\, b\, (f \circ g)\, s, \] where $g(s)$ denotes the image of $s$ under $g$.
Finset.fold_ite' theorem
{g : α → β} (hb : op b b = b) (p : α → Prop) [DecidablePred p] : Finset.fold op b (fun i => ite (p i) (f i) (g i)) s = op (Finset.fold op b f (s.filter p)) (Finset.fold op b g (s.filter fun i => ¬p i))
Full source
/-- 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 fold_ite' {g : α → β} (hb : op b b = b) (p : α → Prop) [DecidablePred p] :
    Finset.fold op b (fun i => ite (p i) (f i) (g i)) s =
      op (Finset.fold op b f (s.filter p)) (Finset.fold op b g (s.filter fun i => ¬p i)) := by
  classical
    induction' s using Finset.induction_on with x s hx IH
    · simp [hb]
    · simp only [Finset.fold_insert hx]
      split_ifs with h
      · have : x ∉ Finset.filter p s := by simp [hx]
        simp [Finset.filter_insert, h, Finset.fold_insert this, ha.assoc, IH]
      · have : x ∉ Finset.filter (fun i => ¬ p i) s := by simp [hx]
        simp [Finset.filter_insert, h, Finset.fold_insert this, IH, ← ha.assoc, hc.comm]
Decomposition of Fold Operation via Predicate: $\mathrm{fold}(*, b, \text{if } p \text{ then } f \text{ else } g, s) = \mathrm{fold}(*, b, f, s \cap p) * \mathrm{fold}(*, b, g, s \setminus p)$
Informal description
Let $*$ be a commutative and associative binary operation on a type $\beta$, $b \in \beta$ with $b * b = b$, and $f, g : \alpha \to \beta$. For any predicate $p : \alpha \to \mathrm{Prop}$ and finite set $s \subseteq \alpha$, we have: \[ \mathrm{fold}(*, b, \lambda i, \text{if } p(i) \text{ then } f(i) \text{ else } g(i), s) = \mathrm{fold}(*, b, f, \{x \in s \mid p(x)\}) * \mathrm{fold}(*, b, g, \{x \in s \mid \neg p(x)\}) \]
Finset.fold_ite theorem
[Std.IdempotentOp op] {g : α → β} (p : α → Prop) [DecidablePred p] : Finset.fold op b (fun i => ite (p i) (f i) (g i)) s = op (Finset.fold op b f (s.filter p)) (Finset.fold op b g (s.filter fun i => ¬p i))
Full source
/-- 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 fold_ite [Std.IdempotentOp op] {g : α → β} (p : α → Prop) [DecidablePred p] :
    Finset.fold op b (fun i => ite (p i) (f i) (g i)) s =
      op (Finset.fold op b f (s.filter p)) (Finset.fold op b g (s.filter fun i => ¬p i)) :=
  fold_ite' (Std.IdempotentOp.idempotent _) _
Idempotent Fold Decomposition via Predicate: $\mathrm{fold}(*, b, \text{if } p \text{ then } f \text{ else } g, s) = \mathrm{fold}(*, b, f, s \cap p) * \mathrm{fold}(*, b, g, s \setminus p)$
Informal description
Let $*$ be a commutative and associative binary operation on a type $\beta$ with an idempotent operation instance (i.e., $x * x = x$ for all $x \in \beta$). Given functions $f, g : \alpha \to \beta$, a predicate $p : \alpha \to \mathrm{Prop}$, and a finite set $s \subseteq \alpha$, we have: \[ \mathrm{fold}(*, b, \lambda i, \text{if } p(i) \text{ then } f(i) \text{ else } g(i), s) = \mathrm{fold}(*, b, f, \{x \in s \mid p(x)\}) * \mathrm{fold}(*, b, g, \{x \in s \mid \neg p(x)\}) \]
Finset.fold_op_rel_iff_and theorem
{r : β → β → Prop} (hr : ∀ {x y z}, r x (op y z) ↔ r x y ∧ r x z) {c : β} : r c (s.fold op b f) ↔ r c b ∧ ∀ x ∈ s, r c (f x)
Full source
theorem fold_op_rel_iff_and {r : β → β → Prop} (hr : ∀ {x y z}, r x (op y z) ↔ r x y ∧ r x z)
    {c : β} : r c (s.fold op b f) ↔ r c b ∧ ∀ x ∈ s, r c (f x) := by
  classical
    induction' s using Finset.induction_on with a s ha IH
    · simp
    rw [Finset.fold_insert ha, hr, IH, ← and_assoc, @and_comm (r c (f a)), and_assoc]
    apply and_congr Iff.rfl
    constructor
    · rintro ⟨h₁, h₂⟩
      intro b hb
      rw [Finset.mem_insert] at hb
      rcases hb with (rfl | hb) <;> solve_by_elim
    · intro h
      constructor
      · exact h a (Finset.mem_insert_self _ _)
      · exact fun b hb => h b <| Finset.mem_insert_of_mem hb
Characterization of Fold Operation via Binary Relation: $r(c, \mathrm{fold}(*, b, f, s)) \leftrightarrow r(c, b) \land (\forall x \in s, r(c, f(x)))$
Informal description
Let $*$ be a commutative and associative binary operation on a type $\beta$, $b \in \beta$, $f : \alpha \to \beta$, and $s$ a finite set of elements of $\alpha$. Suppose $r : \beta \to \beta \to \mathrm{Prop}$ is a relation such that for all $x, y, z \in \beta$, $r(x, y * z) \leftrightarrow r(x, y) \land r(x, z)$. Then for any $c \in \beta$, we have: $$r(c, \mathrm{fold}(*, b, f, s)) \leftrightarrow r(c, b) \land (\forall x \in s, r(c, f(x))).$$
Finset.fold_op_rel_iff_or theorem
{r : β → β → Prop} (hr : ∀ {x y z}, r x (op y z) ↔ r x y ∨ r x z) {c : β} : r c (s.fold op b f) ↔ r c b ∨ ∃ x ∈ s, r c (f x)
Full source
theorem fold_op_rel_iff_or {r : β → β → Prop} (hr : ∀ {x y z}, r x (op y z) ↔ r x y ∨ r x z)
    {c : β} : r c (s.fold op b f) ↔ r c b ∨ ∃ x ∈ s, r c (f x) := by
  classical
    induction' s using Finset.induction_on with a s ha IH
    · simp
    rw [Finset.fold_insert ha, hr, IH, ← or_assoc, @or_comm (r c (f a)), or_assoc]
    apply or_congr Iff.rfl
    constructor
    · rintro (h₁ | ⟨x, hx, h₂⟩)
      · use a
        simp [h₁]
      · refine ⟨x, by simp [hx], h₂⟩
    · rintro ⟨x, hx, h⟩
      exact (mem_insert.mp hx).imp (fun hx => by rwa [hx] at h) (fun hx => ⟨x, hx, h⟩)
Characterization of Fold Operation via Disjunctive Relation: $r(c, \mathrm{fold}(*, b, f, s)) \leftrightarrow r(c, b) \lor (\exists x \in s, r(c, f(x)))$
Informal description
Let $*$ be a commutative and associative binary operation on a type $\beta$, $b \in \beta$, $f : \alpha \to \beta$, and $s$ a finite set of elements of $\alpha$. Suppose $r : \beta \to \beta \to \mathrm{Prop}$ is a relation such that for all $x, y, z \in \beta$, $r(x, y * z) \leftrightarrow r(x, y) \lor r(x, z)$. Then for any $c \in \beta$, we have: $$r(c, \mathrm{fold}(*, b, f, s)) \leftrightarrow r(c, b) \lor (\exists x \in s, r(c, f(x))).$$
Finset.fold_union_empty_singleton theorem
[DecidableEq α] (s : Finset α) : Finset.fold (· ∪ ·) ∅ singleton s = s
Full source
@[simp]
theorem fold_union_empty_singleton [DecidableEq α] (s : Finset α) :
    Finset.fold (· ∪ ·)  singleton s = s := by
  induction' s using Finset.induction_on with a s has ih
  · simp only [fold_empty]
  · rw [fold_insert has, ih, insert_eq]
Fold of Union with Empty Set and Singleton Recovers Original Set
Informal description
For any finite set $s$ of elements of type $\alpha$, the fold operation with the union operation $\cup$, the empty set $\emptyset$ as the initial value, and the singleton function $\mathrm{singleton} : \alpha \to \mathrm{Finset} \alpha$ satisfies: $$\mathrm{fold}(\cup, \emptyset, \mathrm{singleton}, s) = s.$$
Finset.fold_sup_bot_singleton theorem
[DecidableEq α] (s : Finset α) : Finset.fold (· ⊔ ·) ⊥ singleton s = s
Full source
theorem fold_sup_bot_singleton [DecidableEq α] (s : Finset α) :
    Finset.fold (· ⊔ ·)  singleton s = s :=
  fold_union_empty_singleton s
Fold of Supremum with Bottom and Singleton Recovers Original Set
Informal description
For any finite set $s$ of elements of type $\alpha$, the fold operation with the supremum operation $\sqcup$, the bottom element $\bot$ as the initial value, and the singleton function $\mathrm{singleton} : \alpha \to \mathrm{Finset} \alpha$ satisfies: $$\mathrm{fold}(\sqcup, \bot, \mathrm{singleton}, s) = s.$$
Finset.le_fold_min theorem
: c ≤ s.fold min b f ↔ c ≤ b ∧ ∀ x ∈ s, c ≤ f x
Full source
theorem le_fold_min : c ≤ s.fold min b f ↔ c ≤ b ∧ ∀ x ∈ s, c ≤ f x :=
  fold_op_rel_iff_and le_min_iff
Characterization of Minimum Fold: $c \leq \mathrm{fold}(\min, b, f, s) \leftrightarrow (c \leq b \land \forall x \in s, c \leq f(x))$
Informal description
For any element $c$ in a linearly ordered set $\alpha$, a finite set $s$ of elements of $\alpha$, a starting value $b \in \alpha$, and a function $f : \alpha \to \alpha$, the inequality $c \leq \mathrm{fold}(\min, b, f, s)$ holds if and only if both $c \leq b$ and for every $x \in s$, $c \leq f(x)$.
Finset.fold_min_le theorem
: s.fold min b f ≤ c ↔ b ≤ c ∨ ∃ x ∈ s, f x ≤ c
Full source
theorem fold_min_le : s.fold min b f ≤ c ↔ b ≤ c ∨ ∃ x ∈ s, f x ≤ c := by
  show _ ≥ _ ↔ _
  apply fold_op_rel_iff_or
  intro x y z
  show _ ≤ _ ↔ _
  exact min_le_iff
Characterization of Minimum Fold: $\mathrm{fold}(\min, b, f, s) \leq c \leftrightarrow (b \leq c \lor \exists x \in s, f(x) \leq c)$
Informal description
For any elements $b, c$ in a linearly ordered set $\alpha$, a finite set $s$ of elements of $\alpha$, and a function $f : \alpha \to \alpha$, the inequality $\mathrm{fold}(\min, b, f, s) \leq c$ holds if and only if either $b \leq c$ or there exists an element $x \in s$ such that $f(x) \leq c$.
Finset.lt_fold_min theorem
: c < s.fold min b f ↔ c < b ∧ ∀ x ∈ s, c < f x
Full source
theorem lt_fold_min : c < s.fold min b f ↔ c < b ∧ ∀ x ∈ s, c < f x :=
  fold_op_rel_iff_and lt_min_iff
Characterization of Strict Inequality Under Minimum Fold: $c < \mathrm{fold}(\min, b, f, s) \leftrightarrow c < b \land (\forall x \in s, c < f(x))$
Informal description
For any elements $c, b$ in a linearly ordered set $\alpha$, a finite set $s$ of elements of $\alpha$, and a function $f : \alpha \to \alpha$, the strict inequality $c < \mathrm{fold}(\min, b, f, s)$ holds if and only if both $c < b$ and for every $x \in s$, $c < f(x)$. In symbols: \[ c < \mathrm{fold}(\min, b, f, s) \leftrightarrow c < b \land (\forall x \in s, c < f(x)). \]
Finset.fold_min_lt theorem
: s.fold min b f < c ↔ b < c ∨ ∃ x ∈ s, f x < c
Full source
theorem fold_min_lt : s.fold min b f < c ↔ b < c ∨ ∃ x ∈ s, f x < c := by
  show _ > _ ↔ _
  apply fold_op_rel_iff_or
  intro x y z
  show _ < _ ↔ _
  exact min_lt_iff
Characterization of Minimum Fold: $\mathrm{fold}(\min, b, f, s) < c \leftrightarrow (b < c \lor \exists x \in s, f(x) < c)$
Informal description
For any finite set $s$ of elements of type $\alpha$, a base value $b$ of type $\beta$, and a function $f : \alpha \to \beta$, the minimum value obtained by folding the operation $\min$ over the set $s$ with base $b$ is strictly less than $c$ if and only if either $b < c$ or there exists an element $x \in s$ such that $f(x) < c$. In symbols: \[ \mathrm{fold}(\min, b, f, s) < c \leftrightarrow (b < c \lor \exists x \in s, f(x) < c). \]
Finset.fold_max_le theorem
: s.fold max b f ≤ c ↔ b ≤ c ∧ ∀ x ∈ s, f x ≤ c
Full source
theorem fold_max_le : s.fold max b f ≤ c ↔ b ≤ c ∧ ∀ x ∈ s, f x ≤ c := by
  show _ ≥ _ ↔ _
  apply fold_op_rel_iff_and
  intro x y z
  show _ ≤ _ ↔ _
  exact max_le_iff
Characterization of Maximum Fold: $\mathrm{fold}(\max, b, f, s) \leq c \leftrightarrow (b \leq c \land \forall x \in s, f(x) \leq c)$
Informal description
For any finite set $s$ of elements of type $\alpha$, a base value $b$ of type $\beta$, and a function $f : \alpha \to \beta$, the maximum value obtained by folding the operation $\max$ over the set $s$ with base $b$ is less than or equal to $c$ if and only if both $b \leq c$ and for every element $x \in s$, $f(x) \leq c$. In symbols: \[ \mathrm{fold}(\max, b, f, s) \leq c \leftrightarrow (b \leq c \land \forall x \in s, f(x) \leq c). \]
Finset.le_fold_max theorem
: c ≤ s.fold max b f ↔ c ≤ b ∨ ∃ x ∈ s, c ≤ f x
Full source
theorem le_fold_max : c ≤ s.fold max b f ↔ c ≤ b ∨ ∃ x ∈ s, c ≤ f x :=
  fold_op_rel_iff_or le_max_iff
Characterization of Maximum Fold: $c \leq \mathrm{fold}(\max, b, f, s) \leftrightarrow (c \leq b \lor \exists x \in s, c \leq f(x))$
Informal description
For any finite set $s$ of elements of type $\alpha$, a base value $b$ of type $\beta$, and a function $f : \alpha \to \beta$, the value $c$ is less than or equal to the maximum value obtained by folding the operation $\max$ over the set $s$ with base $b$ if and only if either $c \leq b$ or there exists an element $x \in s$ such that $c \leq f(x)$. In symbols: \[ c \leq \mathrm{fold}(\max, b, f, s) \leftrightarrow (c \leq b \lor \exists x \in s, c \leq f(x)). \]
Finset.fold_max_lt theorem
: s.fold max b f < c ↔ b < c ∧ ∀ x ∈ s, f x < c
Full source
theorem fold_max_lt : s.fold max b f < c ↔ b < c ∧ ∀ x ∈ s, f x < c := by
  show _ > _ ↔ _
  apply fold_op_rel_iff_and
  intro x y z
  show _ < _ ↔ _
  exact max_lt_iff
Strict Inequality Characterization for Maximum Fold: $\mathrm{fold}(\max, b, f, s) < c \leftrightarrow (b < c \land \forall x \in s, f(x) < c)$
Informal description
For any finite set $s$ of elements of type $\alpha$, a base value $b$ of type $\beta$, and a function $f : \alpha \to \beta$, the maximum value obtained by folding the operation $\max$ over the set $s$ with base $b$ is strictly less than $c$ if and only if both $b < c$ and for every element $x \in s$, $f(x) < c$. In symbols: \[ \mathrm{fold}(\max, b, f, s) < c \leftrightarrow (b < c \land \forall x \in s, f(x) < c). \]
Finset.lt_fold_max theorem
: c < s.fold max b f ↔ c < b ∨ ∃ x ∈ s, c < f x
Full source
theorem lt_fold_max : c < s.fold max b f ↔ c < b ∨ ∃ x ∈ s, c < f x :=
  fold_op_rel_iff_or lt_max_iff
Characterization of Strict Inequality Under Maximum Fold: $c < \mathrm{fold}(\max, b, f, s) \leftrightarrow c < b \lor (\exists x \in s, c < f(x))$
Informal description
For any element $c$ in a linearly ordered set, a finite set $s$, a starting value $b$, and a function $f$, the strict inequality $c < \mathrm{fold}(\max, b, f, s)$ holds if and only if either $c < b$ or there exists an element $x \in s$ such that $c < f(x)$. In symbols: \[ c < \mathrm{fold}(\max, b, f, s) \leftrightarrow c < b \lor (\exists x \in s, c < f(x)). \]