doc-next-gen

Mathlib.Data.Sum.Basic

Module docstring

{"# Additional lemmas about sum types

Most of the former contents of this file have been moved to Batteries. ","### Ternary sum

Abbreviations for the maps from the summands to α ⊕ β ⊕ γ. This is useful for pattern-matching. ","### PSum "}

not_isLeft_and_isRight theorem
{x : α ⊕ β} : ¬(x.isLeft ∧ x.isRight)
Full source
lemma not_isLeft_and_isRight {x : α ⊕ β} : ¬(x.isLeft ∧ x.isRight) := by simp
Disjointness of Left and Right Injections in Sum Type
Informal description
For any element $x$ of the sum type $\alpha \oplus \beta$, it is not the case that $x$ is both a left injection (i.e., of the form $\text{inl}(a)$ for some $a \in \alpha$) and a right injection (i.e., of the form $\text{inr}(b)$ for some $b \in \beta$).
Sum.exists_sum theorem
{γ : α ⊕ β → Sort*} (p : (∀ ab, γ ab) → Prop) : (∃ fab, p fab) ↔ (∃ fa fb, p (Sum.rec fa fb))
Full source
theorem exists_sum {γ : α ⊕ β → Sort*} (p : (∀ ab, γ ab) → Prop) :
    (∃ fab, p fab) ↔ (∃ fa fb, p (Sum.rec fa fb)) := by
  rw [← not_forall_not, forall_sum]
  simp
Existence of Dependent Function on Sum Type via Component Functions
Informal description
For any family of types $\gamma$ indexed by the sum type $\alpha \oplus \beta$ and any predicate $p$ on dependent functions $\prod_{ab \in \alpha \oplus \beta} \gamma(ab)$, the following equivalence holds: $$(\exists fab : \prod_{ab \in \alpha \oplus \beta} \gamma(ab), p(fab)) \leftrightarrow (\exists fa : \prod_{a \in \alpha} \gamma(\text{inl}(a)), \exists fb : \prod_{b \in \beta} \gamma(\text{inr}(b)), p(\text{Sum.rec}(fa, fb)))$$
Sum.inl_injective theorem
: Function.Injective (inl : α → α ⊕ β)
Full source
theorem inl_injective : Function.Injective (inl : α → α ⊕ β) := fun _ _ ↦ inl.inj
Injectivity of Left Injection in Sum Type
Informal description
The left injection function $\text{inl} : \alpha \to \alpha \oplus \beta$ is injective, meaning that for any $a_1, a_2 \in \alpha$, if $\text{inl}(a_1) = \text{inl}(a_2)$, then $a_1 = a_2$.
Sum.inr_injective theorem
: Function.Injective (inr : β → α ⊕ β)
Full source
theorem inr_injective : Function.Injective (inr : β → α ⊕ β) := fun _ _ ↦ inr.inj
Injectivity of the Right Sum Injection
Informal description
The right injection function $\text{inr} : \beta \to \alpha \oplus \beta$ is injective, meaning that for any $b_1, b_2 \in \beta$, if $\text{inr}(b_1) = \text{inr}(b_2)$, then $b_1 = b_2$.
Sum.sum_rec_congr theorem
(P : α ⊕ β → Sort*) (f : ∀ i, P (inl i)) (g : ∀ i, P (inr i)) {x y : α ⊕ β} (h : x = y) : @Sum.rec _ _ _ f g x = cast (congr_arg P h.symm) (@Sum.rec _ _ _ f g y)
Full source
theorem sum_rec_congr (P : α ⊕ β → Sort*) (f : ∀ i, P (inl i)) (g : ∀ i, P (inr i))
    {x y : α ⊕ β} (h : x = y) :
    @Sum.rec _ _ _ f g x = cast (congr_arg P h.symm) (@Sum.rec _ _ _ f g y) := by cases h; rfl
Congruence Property for Sum Type Recursion
Informal description
Let $P : \alpha \oplus \beta \to \text{Sort}*$ be a type family, $f : \forall i, P(\text{inl } i)$ and $g : \forall i, P(\text{inr } i)$ be functions, and $x, y : \alpha \oplus \beta$ be terms such that $x = y$. Then the recursively defined function $\text{Sum.rec } f g$ satisfies $\text{Sum.rec } f g x = \text{cast } (\text{congr\_arg } P h.\text{symm}) (\text{Sum.rec } f g y)$, where $\text{inl}$ and $\text{inr}$ are the left and right injections into the sum type $\alpha \oplus \beta$.
Sum.getLeft_eq_getLeft? theorem
(h₁ : x.isLeft) (h₂ : x.getLeft?.isSome) : x.getLeft h₁ = x.getLeft?.get h₂
Full source
theorem getLeft_eq_getLeft? (h₁ : x.isLeft) (h₂ : x.getLeft?.isSome) :
    x.getLeft h₁ = x.getLeft?.get h₂ := by simp [← getLeft?_eq_some_iff]
Equality of Left Projections in Sum Type
Informal description
For a sum type element $x \in \alpha \oplus \beta$, if $x$ is in the left component (i.e., $x.\text{isLeft}$ holds) and the optional left projection $x.\text{getLeft?}$ is non-empty (i.e., $x.\text{getLeft?}.\text{isSome}$ holds), then the direct left projection $x.\text{getLeft}$ equals the extracted value from the optional left projection $x.\text{getLeft?}.\text{get}$.
Sum.getRight_eq_getRight? theorem
(h₁ : x.isRight) (h₂ : x.getRight?.isSome) : x.getRight h₁ = x.getRight?.get h₂
Full source
theorem getRight_eq_getRight? (h₁ : x.isRight) (h₂ : x.getRight?.isSome) :
    x.getRight h₁ = x.getRight?.get h₂ := by simp [← getRight?_eq_some_iff]
Equality of Direct and Optional Right Projections in Sum Types
Informal description
For a sum type element $x \in \alpha \oplus \beta$, if $x$ is a right injection (i.e., $x.\text{isRight}$ holds) and the optional right projection $x.\text{getRight?}$ is non-empty (i.e., $x.\text{getRight?}.\text{isSome}$ holds), then the direct right projection $x.\text{getRight}$ equals the extracted value from the optional right projection $x.\text{getRight?}.\text{get}$.
Sum.isSome_getRight?_iff_isRight theorem
: x.getRight?.isSome ↔ x.isRight
Full source
@[simp] theorem isSome_getRight?_iff_isRight : x.getRight?.isSome ↔ x.isRight := by
  rw [isRight_iff, Option.isSome_iff_exists]; simp
Right Injection Characterized by getRight? Being Some
Informal description
For a sum type element $x \in \alpha \oplus \beta$, the option `x.getRight?` has a value (i.e., `isSome` holds) if and only if $x$ is a right injection (i.e., `isRight` holds).
Sum.update_elim_inl theorem
[DecidableEq α] [DecidableEq (α ⊕ β)] {f : α → γ} {g : β → γ} {i : α} {x : γ} : update (Sum.elim f g) (inl i) x = Sum.elim (update f i x) g
Full source
@[simp]
theorem update_elim_inl [DecidableEq α] [DecidableEq (α ⊕ β)] {f : α → γ} {g : β → γ} {i : α}
    {x : γ} : update (Sum.elim f g) (inl i) x = Sum.elim (update f i x) g :=
  update_eq_iff.2 ⟨by simp, by simp +contextual⟩
Update of Sum Elimination at Left Injection Equals Sum Elimination with Updated Left Function
Informal description
Let $\alpha$ and $\beta$ be types with decidable equality, and let $\alpha \oplus \beta$ also have decidable equality. For any functions $f : \alpha \to \gamma$ and $g : \beta \to \gamma$, any element $i \in \alpha$, and any value $x \in \gamma$, updating the sum elimination function $\text{Sum.elim } f \, g$ at the left injection $\text{inl } i$ with value $x$ is equal to the sum elimination of the updated function $\text{update } f \, i \, x$ with $g$. In other words: \[ \text{update } (\text{Sum.elim } f \, g) \, (\text{inl } i) \, x = \text{Sum.elim } (\text{update } f \, i \, x) \, g. \]
Sum.update_elim_inr theorem
[DecidableEq β] [DecidableEq (α ⊕ β)] {f : α → γ} {g : β → γ} {i : β} {x : γ} : update (Sum.elim f g) (inr i) x = Sum.elim f (update g i x)
Full source
@[simp]
theorem update_elim_inr [DecidableEq β] [DecidableEq (α ⊕ β)] {f : α → γ} {g : β → γ} {i : β}
    {x : γ} : update (Sum.elim f g) (inr i) x = Sum.elim f (update g i x) :=
  update_eq_iff.2 ⟨by simp, by simp +contextual⟩
Update of Sum Elimination at Right Injection Equals Sum Elimination with Updated Right Function
Informal description
Let $\alpha$ and $\beta$ be types with decidable equality, and let $\alpha \oplus \beta$ also have decidable equality. For any functions $f : \alpha \to \gamma$ and $g : \beta \to \gamma$, any element $i \in \beta$, and any value $x \in \gamma$, updating the sum elimination function $\text{Sum.elim } f \, g$ at the right injection $\text{inr } i$ with value $x$ is equal to the sum elimination of $f$ with the updated function $\text{update } g \, i \, x$. In other words: \[ \text{update } (\text{Sum.elim } f \, g) \, (\text{inr } i) \, x = \text{Sum.elim } f \, (\text{update } g \, i \, x). \]
Sum.update_inl_comp_inl theorem
[DecidableEq α] [DecidableEq (α ⊕ β)] {f : α ⊕ β → γ} {i : α} {x : γ} : update f (inl i) x ∘ inl = update (f ∘ inl) i x
Full source
@[simp]
theorem update_inl_comp_inl [DecidableEq α] [DecidableEq (α ⊕ β)] {f : α ⊕ β → γ} {i : α}
    {x : γ} : updateupdate f (inl i) x ∘ inl = update (f ∘ inl) i x :=
  update_comp_eq_of_injective _ inl_injective _ _
Update of Left Injection Composition in Sum Type
Informal description
Let $\alpha$ and $\beta$ be types with decidable equality, and let $\alpha \oplus \beta$ also have decidable equality. For any function $f : \alpha \oplus \beta \to \gamma$, any $i \in \alpha$, and any $x \in \gamma$, the composition of the updated function $\text{update } f \, (\text{inl } i) \, x$ with the left injection $\text{inl}$ is equal to the function obtained by updating $f \circ \text{inl}$ at $i$ with $x$, i.e., \[ (\text{update } f \, (\text{inl } i) \, x) \circ \text{inl} = \text{update } (f \circ \text{inl}) \, i \, x. \]
Sum.update_inl_apply_inl theorem
[DecidableEq α] [DecidableEq (α ⊕ β)] {f : α ⊕ β → γ} {i j : α} {x : γ} : update f (inl i) x (inl j) = update (f ∘ inl) i x j
Full source
@[simp]
theorem update_inl_apply_inl [DecidableEq α] [DecidableEq (α ⊕ β)] {f : α ⊕ β → γ} {i j : α}
    {x : γ} : update f (inl i) x (inl j) = update (f ∘ inl) i x j := by
  rw [← update_inl_comp_inl, Function.comp_apply]
Update at Left Injection Preserves Left Injection Evaluation
Informal description
Let $\alpha$ and $\beta$ be types with decidable equality, and let $\alpha \oplus \beta$ also have decidable equality. For any function $f : \alpha \oplus \beta \to \gamma$, any elements $i, j \in \alpha$, and any value $x \in \gamma$, the updated function $\text{update } f \, (\text{inl } i) \, x$ evaluated at $\text{inl } j$ is equal to the function $\text{update } (f \circ \text{inl}) \, i \, x$ evaluated at $j$. In other words: \[ \text{update } f \, (\text{inl } i) \, x \, (\text{inl } j) = \text{update } (f \circ \text{inl}) \, i \, x \, j. \]
Sum.update_inl_comp_inr theorem
[DecidableEq (α ⊕ β)] {f : α ⊕ β → γ} {i : α} {x : γ} : update f (inl i) x ∘ inr = f ∘ inr
Full source
@[simp]
theorem update_inl_comp_inr [DecidableEq (α ⊕ β)] {f : α ⊕ β → γ} {i : α} {x : γ} :
    updateupdate f (inl i) x ∘ inr = f ∘ inr :=
  (update_comp_eq_of_forall_ne _ _) fun _ ↦ inr_ne_inl
Update at Left Injection Preserves Right Injection Composition
Informal description
Let $\alpha$ and $\beta$ be types with decidable equality on their sum $\alpha \oplus \beta$. For any function $f : \alpha \oplus \beta \to \gamma$, any element $i \in \alpha$, and any value $x \in \gamma$, the composition of the updated function $\text{update } f \, (\text{inl } i) \, x$ with the right injection $\text{inr}$ is equal to the composition of $f$ with $\text{inr}$. That is, \[ (\text{update } f \, (\text{inl } i) \, x) \circ \text{inr} = f \circ \text{inr}. \]
Sum.update_inl_apply_inr theorem
[DecidableEq (α ⊕ β)] {f : α ⊕ β → γ} {i : α} {j : β} {x : γ} : update f (inl i) x (inr j) = f (inr j)
Full source
theorem update_inl_apply_inr [DecidableEq (α ⊕ β)] {f : α ⊕ β → γ} {i : α} {j : β} {x : γ} :
    update f (inl i) x (inr j) = f (inr j) :=
  Function.update_of_ne inr_ne_inl ..
Update at Left Injection Preserves Right Injection Evaluation
Informal description
Let $\alpha$ and $\beta$ be types with decidable equality, and let $\gamma$ be any type. For any function $f : \alpha \oplus \beta \to \gamma$, any element $i \in \alpha$, any element $j \in \beta$, and any value $x \in \gamma$, updating $f$ at the left injection $\text{inl}(i)$ with value $x$ and then evaluating at the right injection $\text{inr}(j)$ yields the same result as simply evaluating $f$ at $\text{inr}(j)$. In other words, the following equality holds: \[ \text{update } f \, (\text{inl } i) \, x \, (\text{inr } j) = f (\text{inr } j). \]
Sum.update_inr_comp_inl theorem
[DecidableEq (α ⊕ β)] {f : α ⊕ β → γ} {i : β} {x : γ} : update f (inr i) x ∘ inl = f ∘ inl
Full source
@[simp]
theorem update_inr_comp_inl [DecidableEq (α ⊕ β)] {f : α ⊕ β → γ} {i : β} {x : γ} :
    updateupdate f (inr i) x ∘ inl = f ∘ inl :=
  (update_comp_eq_of_forall_ne _ _) fun _ ↦ inl_ne_inr
Update at Right Injection Preserves Left Injection Composition
Informal description
Let $\alpha$ and $\beta$ be types with decidable equality on their sum $\alpha \oplus \beta$. For any function $f : \alpha \oplus \beta \to \gamma$, any element $i \in \beta$, and any value $x \in \gamma$, the composition of the updated function $\text{update } f \, (\text{inr } i) \, x$ with the left injection $\text{inl}$ is equal to the composition of $f$ with $\text{inl}$. That is, \[ (\text{update } f \, (\text{inr } i) \, x) \circ \text{inl} = f \circ \text{inl}. \]
Sum.update_inr_apply_inl theorem
[DecidableEq (α ⊕ β)] {f : α ⊕ β → γ} {i : α} {j : β} {x : γ} : update f (inr j) x (inl i) = f (inl i)
Full source
theorem update_inr_apply_inl [DecidableEq (α ⊕ β)] {f : α ⊕ β → γ} {i : α} {j : β} {x : γ} :
    update f (inr j) x (inl i) = f (inl i) :=
  Function.update_of_ne inl_ne_inr ..
Update at Right Injection Preserves Left Injection Evaluation
Informal description
Let $\alpha$ and $\beta$ be types with decidable equality, and let $\gamma$ be any type. For any function $f : \alpha \oplus \beta \to \gamma$, any element $i \in \alpha$, any element $j \in \beta$, and any value $x \in \gamma$, updating $f$ at the right injection $\text{inr}(j)$ with value $x$ and then evaluating at the left injection $\text{inl}(i)$ yields the same result as simply evaluating $f$ at $\text{inl}(i)$. In other words, the following equality holds: \[ \text{update } f \, (\text{inr } j) \, x \, (\text{inl } i) = f (\text{inl } i). \]
Sum.update_inr_comp_inr theorem
[DecidableEq β] [DecidableEq (α ⊕ β)] {f : α ⊕ β → γ} {i : β} {x : γ} : update f (inr i) x ∘ inr = update (f ∘ inr) i x
Full source
@[simp]
theorem update_inr_comp_inr [DecidableEq β] [DecidableEq (α ⊕ β)] {f : α ⊕ β → γ} {i : β}
    {x : γ} : updateupdate f (inr i) x ∘ inr = update (f ∘ inr) i x :=
  update_comp_eq_of_injective _ inr_injective _ _
Update Commutes with Right Injection Composition in Sum Types
Informal description
Let $\alpha$ and $\beta$ be types with decidable equality, and let $\gamma$ be any type. For any function $f : \alpha \oplus \beta \to \gamma$, any element $i \in \beta$, and any value $x \in \gamma$, the composition of the updated function $\text{update } f \, (\text{inr } i) \, x$ with the right injection $\text{inr}$ is equal to updating the composition $f \circ \text{inr}$ at $i$ with $x$. In other words, the following equality holds: \[ (\text{update } f \, (\text{inr } i) \, x) \circ \text{inr} = \text{update } (f \circ \text{inr}) \, i \, x. \]
Sum.update_inr_apply_inr theorem
[DecidableEq β] [DecidableEq (α ⊕ β)] {f : α ⊕ β → γ} {i j : β} {x : γ} : update f (inr i) x (inr j) = update (f ∘ inr) i x j
Full source
@[simp]
theorem update_inr_apply_inr [DecidableEq β] [DecidableEq (α ⊕ β)] {f : α ⊕ β → γ} {i j : β}
    {x : γ} : update f (inr i) x (inr j) = update (f ∘ inr) i x j := by
  rw [← update_inr_comp_inr, Function.comp_apply]
Update Commutes with Right Injection Evaluation in Sum Types
Informal description
Let $\alpha$ and $\beta$ be types with decidable equality, and let $\gamma$ be any type. For any function $f : \alpha \oplus \beta \to \gamma$ and any elements $i, j \in \beta$, updating $f$ at the right injection $\text{inr}(i)$ with value $x$ and then evaluating at $\text{inr}(j)$ is equivalent to updating the composition $f \circ \text{inr}$ at $i$ with $x$ and evaluating at $j$. In other words, the following equality holds: \[ \text{update } f \, (\text{inr } i) \, x \, (\text{inr } j) = \text{update } (f \circ \text{inr}) \, i \, x \, j. \]
Sum.update_inl_apply_inl' theorem
{γ : α ⊕ β → Type*} [DecidableEq α] [DecidableEq (α ⊕ β)] {f : (i : α ⊕ β) → γ i} {i : α} {x : γ (.inl i)} (j : α) : update f (.inl i) x (Sum.inl j) = update (fun j ↦ f (.inl j)) i x j
Full source
@[simp]
theorem update_inl_apply_inl' {γ : α ⊕ β → Type*} [DecidableEq α] [DecidableEq (α ⊕ β)]
    {f : (i : α ⊕ β) → γ i} {i : α} {x : γ (.inl i)} (j : α) :
    update f (.inl i) x (Sum.inl j) = update (fun j ↦ f (.inl j)) i x j :=
  Function.update_apply_of_injective f Sum.inl_injective i x j
Update Equality for Left Injection in Sum Type
Informal description
Let $\alpha$ and $\beta$ be types with decidable equality, and let $\gamma : \alpha \oplus \beta \to \text{Type}$ be a type family. Given a function $f : (i : \alpha \oplus \beta) \to \gamma i$, an element $i \in \alpha$, and a value $x \in \gamma (\text{inl } i)$, then for any $j \in \alpha$, the following equality holds: \[ \text{update } f \, (\text{inl } i) \, x \, (\text{inl } j) = \text{update } (\lambda j, f (\text{inl } j)) \, i \, x \, j. \]
Sum.update_inr_apply_inr' theorem
{γ : α ⊕ β → Type*} [DecidableEq β] [DecidableEq (α ⊕ β)] {f : (i : α ⊕ β) → γ i} {i : β} {x : γ (.inr i)} (j : β) : update f (.inr i) x (Sum.inr j) = update (fun j ↦ f (.inr j)) i x j
Full source
@[simp]
theorem update_inr_apply_inr' {γ : α ⊕ β → Type*} [DecidableEq β] [DecidableEq (α ⊕ β)]
    {f : (i : α ⊕ β) → γ i} {i : β} {x : γ (.inr i)} (j : β) :
    update f (.inr i) x (Sum.inr j) = update (fun j ↦ f (.inr j)) i x j :=
  Function.update_apply_of_injective f Sum.inr_injective i x j
Update Equality for Right Injection in Sum Type
Informal description
Let $\alpha$ and $\beta$ be types with decidable equality, and let $\gamma : \alpha \oplus \beta \to \text{Type}$ be a type family. Given a function $f : (i : \alpha \oplus \beta) \to \gamma i$, an element $i \in \beta$, and a value $x \in \gamma (\text{inr } i)$, then for any $j \in \beta$, the following equality holds: \[ \text{update } f \, (\text{inr } i) \, x \, (\text{inr } j) = \text{update } (\lambda j, f (\text{inr } j)) \, i \, x \, j. \]
Sum.rec_update_left theorem
{γ : α ⊕ β → Sort*} [DecidableEq α] [DecidableEq β] (f : ∀ a, γ (.inl a)) (g : ∀ b, γ (.inr b)) (a : α) (x : γ (.inl a)) : Sum.rec (update f a x) g = update (Sum.rec f g) (.inl a) x
Full source
@[simp]
lemma rec_update_left {γ : α ⊕ β → Sort*} [DecidableEq α] [DecidableEq β]
    (f : ∀ a, γ (.inl a)) (g : ∀ b, γ (.inr b)) (a : α) (x : γ (.inl a)) :
    Sum.rec (update f a x) g = update (Sum.rec f g) (.inl a) x :=
  Function.rec_update Sum.inl_injective (Sum.rec · g) (fun _ _ => rfl) (fun
    | _, _, .inl _, h => (h _ rfl).elim
    | _, _, .inr _, _ => rfl) _ _ _
Recursor Commutes with Left Update for Sum Types
Informal description
Let $\alpha$ and $\beta$ be types with decidable equality, and let $\gamma : \alpha \oplus \beta \to \text{Type}$ be a type family. Given functions $f : \forall a, \gamma (\text{inl } a)$ and $g : \forall b, \gamma (\text{inr } b)$, for any $a \in \alpha$ and $x \in \gamma (\text{inl } a)$, the following equality holds: \[ \text{Sum.rec } (\text{update } f \, a \, x) \, g = \text{update } (\text{Sum.rec } f \, g) (\text{inl } a) x \]
Sum.rec_update_right theorem
{γ : α ⊕ β → Sort*} [DecidableEq α] [DecidableEq β] (f : ∀ a, γ (.inl a)) (g : ∀ b, γ (.inr b)) (b : β) (x : γ (.inr b)) : Sum.rec f (update g b x) = update (Sum.rec f g) (.inr b) x
Full source
@[simp]
lemma rec_update_right {γ : α ⊕ β → Sort*} [DecidableEq α] [DecidableEq β]
    (f : ∀ a, γ (.inl a)) (g : ∀ b, γ (.inr b)) (b : β) (x : γ (.inr b)) :
    Sum.rec f (update g b x) = update (Sum.rec f g) (.inr b) x :=
  Function.rec_update Sum.inr_injective (Sum.rec f) (fun _ _ => rfl) (fun
    | _, _, .inr _, h => (h _ rfl).elim
    | _, _, .inl _, _ => rfl) _ _ _
Recursor Commutes with Right Update for Sum Types
Informal description
Let $\alpha$ and $\beta$ be types with decidable equality, and let $\gamma : \alpha \oplus \beta \to \text{Sort}$ be a type family. Given functions $f : \forall a, \gamma (\text{inl } a)$ and $g : \forall b, \gamma (\text{inr } b)$, for any $b \in \beta$ and $x \in \gamma (\text{inr } b)$, the following equality holds: \[ \text{Sum.rec } f \, (\text{update } g \, b \, x) = \text{update } (\text{Sum.rec } f \, g) (\text{inr } b) x \]
Sum.elim_update_left theorem
{γ : Sort*} [DecidableEq α] [DecidableEq β] (f : α → γ) (g : β → γ) (a : α) (x : γ) : Sum.elim (update f a x) g = update (Sum.elim f g) (.inl a) x
Full source
@[simp]
lemma elim_update_left {γ : Sort*} [DecidableEq α] [DecidableEq β]
    (f : α → γ) (g : β → γ) (a : α) (x : γ) :
    Sum.elim (update f a x) g = update (Sum.elim f g) (.inl a) x :=
  rec_update_left _ _ _ _
Sum Elimination Commutes with Left Function Update
Informal description
Let $\alpha$ and $\beta$ be types with decidable equality, and let $\gamma$ be a type. Given functions $f : \alpha \to \gamma$ and $g : \beta \to \gamma$, for any $a \in \alpha$ and $x \in \gamma$, the following equality holds: \[ \text{Sum.elim } (\text{update } f \, a \, x) \, g = \text{update } (\text{Sum.elim } f \, g) (\text{inl } a) x \]
Sum.elim_update_right theorem
{γ : Sort*} [DecidableEq α] [DecidableEq β] (f : α → γ) (g : β → γ) (b : β) (x : γ) : Sum.elim f (update g b x) = update (Sum.elim f g) (.inr b) x
Full source
@[simp]
lemma elim_update_right {γ : Sort*} [DecidableEq α] [DecidableEq β]
    (f : α → γ) (g : β → γ) (b : β) (x : γ) :
    Sum.elim f (update g b x) = update (Sum.elim f g) (.inr b) x :=
  rec_update_right _ _ _ _
Sum Elimination Commutes with Right Function Update
Informal description
Let $\alpha$ and $\beta$ be types with decidable equality, and let $\gamma$ be a type. Given functions $f : \alpha \to \gamma$ and $g : \beta \to \gamma$, for any $b \in \beta$ and $x \in \gamma$, the following equality holds: \[ \text{Sum.elim } f \, (\text{update } g \, b \, x) = \text{update } (\text{Sum.elim } f \, g) (\text{inr } b) x \]
Sum.swap_leftInverse theorem
: Function.LeftInverse (@swap α β) swap
Full source
@[simp]
theorem swap_leftInverse : Function.LeftInverse (@swap α β) swap :=
  swap_swap
Self-Inverse Property of Sum Type Swap Function
Informal description
The function `swap : α ⊕ β → β ⊕ α` is a left inverse of itself, i.e., for any element $x$ in the sum type $\alpha \oplus \beta$, we have $\text{swap}(\text{swap}(x)) = x$.
Sum.swap_rightInverse theorem
: Function.RightInverse (@swap α β) swap
Full source
@[simp]
theorem swap_rightInverse : Function.RightInverse (@swap α β) swap :=
  swap_swap
Self-Inverse Property of Sum Type Swap Function (Right Version)
Informal description
The function $\text{swap} : \alpha \oplus \beta \to \beta \oplus \alpha$ is a right inverse of itself, i.e., for any element $y$ in the sum type $\beta \oplus \alpha$, we have $\text{swap}(\text{swap}(y)) = y$.
Sum.LiftRel.isLeft_congr theorem
(h : LiftRel r s x y) : x.isLeft ↔ y.isLeft
Full source
theorem isLeft_congr (h : LiftRel r s x y) : x.isLeft ↔ y.isLeft := by cases h <;> rfl
Equivalence of Left Injection Property under LiftRel
Informal description
Given a relation `LiftRel r s` between two sum types `x` and `y`, the property of being a left injection (i.e., `isLeft`) is equivalent for `x` and `y`. In other words, if `x` and `y` are related by `LiftRel r s`, then `x` is a left injection if and only if `y` is a left injection.
Sum.LiftRel.isRight_congr theorem
(h : LiftRel r s x y) : x.isRight ↔ y.isRight
Full source
theorem isRight_congr (h : LiftRel r s x y) : x.isRight ↔ y.isRight := by cases h <;> rfl
Preservation of Right Summand Property under LiftRel
Informal description
Given a relation `LiftRel r s` between sum types `x` and `y`, the property of being a right summand is preserved, i.e., `x.isRight` if and only if `y.isRight`.
Sum.LiftRel.isLeft_left theorem
(h : LiftRel r s x (inl c)) : x.isLeft
Full source
theorem isLeft_left (h : LiftRel r s x (inl c)) : x.isLeft := by cases h; rfl
Left Injection Preservation under LiftRel Relation
Informal description
Given a relation `LiftRel r s` between sum types and an element `x` in the left sum type, if `x` is related to an element `inl c` (the left injection of `c`) under this relation, then `x` must be a left injection (i.e., `x.isLeft` holds).
Sum.LiftRel.isLeft_right theorem
(h : LiftRel r s (inl a) y) : y.isLeft
Full source
theorem isLeft_right (h : LiftRel r s (inl a) y) : y.isLeft := by cases h; rfl
Left Summand Implies Left Result in LiftRel
Informal description
Given a relation `LiftRel r s` between sum types and an element `(inl a)` in the left summand, if `(inl a)` is related to `y` under `LiftRel r s`, then `y` must be in the left summand (i.e., `y.isLeft` holds).
Sum.LiftRel.isRight_left theorem
(h : LiftRel r s x (inr d)) : x.isRight
Full source
theorem isRight_left (h : LiftRel r s x (inr d)) : x.isRight := by cases h; rfl
Right Injection Preservation under LiftRel
Informal description
Given a relation `LiftRel r s` between elements of sum types, if `x` is related to `inr d` under this relation, then `x` must be a right injection (i.e., of the form `inr b` for some `b`).
Sum.LiftRel.isRight_right theorem
(h : LiftRel r s (inr b) y) : y.isRight
Full source
theorem isRight_right (h : LiftRel r s (inr b) y) : y.isRight := by cases h; rfl
Right injection preserves rightness in sum type relations
Informal description
For any relation `LiftRel r s` between sum types, if `(inr b)` is related to `y` under this relation, then `y` must be a right injection (i.e., `y.isRight` holds).
Sum.LiftRel.exists_of_isLeft_left theorem
(h₁ : LiftRel r s x y) (h₂ : x.isLeft) : ∃ a c, r a c ∧ x = inl a ∧ y = inl c
Full source
theorem exists_of_isLeft_left (h₁ : LiftRel r s x y) (h₂ : x.isLeft) :
    ∃ a c, r a c ∧ x = inl a ∧ y = inl c := by
  rcases isLeft_iff.mp h₂ with ⟨_, rfl⟩
  simp only [liftRel_iff, false_and, and_false, exists_false, or_false, reduceCtorEq] at h₁
  exact h₁
Existence of Related Left Elements in LiftRel
Informal description
Given a relation `LiftRel r s` between sum types `x` and `y`, if `x` is a left injection (i.e., `x.isLeft` holds), then there exist elements `a` and `c` such that `r a c` holds, `x = inl a`, and `y = inl c`.
Sum.LiftRel.exists_of_isLeft_right theorem
(h₁ : LiftRel r s x y) (h₂ : y.isLeft) : ∃ a c, r a c ∧ x = inl a ∧ y = inl c
Full source
theorem exists_of_isLeft_right (h₁ : LiftRel r s x y) (h₂ : y.isLeft) :
    ∃ a c, r a c ∧ x = inl a ∧ y = inl c := exists_of_isLeft_left h₁ ((isLeft_congr h₁).mpr h₂)
Existence of Related Left Elements in LiftRel When Right is Left Injection
Informal description
Given a relation `LiftRel r s` between sum types `x` and `y`, if `y` is a left injection (i.e., `y.isLeft` holds), then there exist elements `a` and `c` such that `r a c` holds, `x = inl a`, and `y = inl c`.
Sum.LiftRel.exists_of_isRight_left theorem
(h₁ : LiftRel r s x y) (h₂ : x.isRight) : ∃ b d, s b d ∧ x = inr b ∧ y = inr d
Full source
theorem exists_of_isRight_left (h₁ : LiftRel r s x y) (h₂ : x.isRight) :
    ∃ b d, s b d ∧ x = inr b ∧ y = inr d := by
  rcases isRight_iff.mp h₂ with ⟨_, rfl⟩
  simp only [liftRel_iff, false_and, and_false, exists_false, false_or, reduceCtorEq] at h₁
  exact h₁
Existence of Related Right Elements in LiftRel
Informal description
Given a relation `LiftRel r s` between sum types `x` and `y`, if `x` is a right injection (i.e., `x.isRight` holds), then there exist elements `b` and `d` such that the relation `s b d` holds, and `x` and `y` are equal to the right injections of `b` and `d` respectively.
Sum.LiftRel.exists_of_isRight_right theorem
(h₁ : LiftRel r s x y) (h₂ : y.isRight) : ∃ b d, s b d ∧ x = inr b ∧ y = inr d
Full source
theorem exists_of_isRight_right (h₁ : LiftRel r s x y) (h₂ : y.isRight) :
    ∃ b d, s b d ∧ x = inr b ∧ y = inr d :=
  exists_of_isRight_left h₁ ((isRight_congr h₁).mpr h₂)
Existence of Related Right Elements in LiftRel when Right Summand is on the Right
Informal description
Given a relation `LiftRel r s` between sum types `x` and `y`, if `y` is a right injection (i.e., `y.isRight` holds), then there exist elements `b` and `d` such that the relation `s b d` holds, and `x` and `y` are equal to the right injections of `b` and `d` respectively.
Function.Injective.sumElim theorem
{f : α → γ} {g : β → γ} (hf : Injective f) (hg : Injective g) (hfg : ∀ a b, f a ≠ g b) : Injective (Sum.elim f g)
Full source
theorem Injective.sumElim {f : α → γ} {g : β → γ} (hf : Injective f) (hg : Injective g)
    (hfg : ∀ a b, f a ≠ g b) : Injective (Sum.elim f g)
  | inl _, inl _, h => congr_arg inl <| hf h
  | inl _, inr _, h => (hfg _ _ h).elim
  | inr _, inl _, h => (hfg _ _ h.symm).elim
  | inr _, inr _, h => congr_arg inr <| hg h
Injectivity of Sum Elimination under Disjoint Images
Informal description
Given injective functions $f : \alpha \to \gamma$ and $g : \beta \to \gamma$ such that $f(a) \neq g(b)$ for all $a \in \alpha$ and $b \in \beta$, the function $\mathrm{Sum.elim}\, f\, g : \alpha \oplus \beta \to \gamma$ is injective.
Function.Injective.sumMap theorem
{f : α → β} {g : α' → β'} (hf : Injective f) (hg : Injective g) : Injective (Sum.map f g)
Full source
theorem Injective.sumMap {f : α → β} {g : α' → β'} (hf : Injective f) (hg : Injective g) :
    Injective (Sum.map f g)
  | inl _, inl _, h => congr_arg inl <| hf <| inl.inj h
  | inr _, inr _, h => congr_arg inr <| hg <| inr.inj h
Injectivity of Sum Map under Component Injectivity
Informal description
Let $f : \alpha \to \beta$ and $g : \alpha' \to \beta'$ be injective functions. Then the sum map $\text{Sum.map}\, f\, g : \alpha \oplus \alpha' \to \beta \oplus \beta'$ is also injective.
Function.Surjective.sumMap theorem
{f : α → β} {g : α' → β'} (hf : Surjective f) (hg : Surjective g) : Surjective (Sum.map f g)
Full source
theorem Surjective.sumMap {f : α → β} {g : α' → β'} (hf : Surjective f) (hg : Surjective g) :
    Surjective (Sum.map f g)
  | inl y =>
    let ⟨x, hx⟩ := hf y
    ⟨inl x, congr_arg inl hx⟩
  | inr y =>
    let ⟨x, hx⟩ := hg y
    ⟨inr x, congr_arg inr hx⟩
Surjectivity of Sum Map under Component Surjectivity
Informal description
Given surjective functions $f : \alpha \to \beta$ and $g : \alpha' \to \beta'$, the sum map $\mathrm{Sum.map}\, f\, g : \alpha \oplus \alpha' \to \beta \oplus \beta'$ is also surjective.
Function.Bijective.sumMap theorem
{f : α → β} {g : α' → β'} (hf : Bijective f) (hg : Bijective g) : Bijective (Sum.map f g)
Full source
theorem Bijective.sumMap {f : α → β} {g : α' → β'} (hf : Bijective f) (hg : Bijective g) :
    Bijective (Sum.map f g) :=
  ⟨hf.injective.sumMap hg.injective, hf.surjective.sumMap hg.surjective⟩
Bijectivity of Sum Map under Component Bijectivity
Informal description
Let $f : \alpha \to \beta$ and $g : \alpha' \to \beta'$ be bijective functions. Then the sum map $\mathrm{Sum.map}\, f\, g : \alpha \oplus \alpha' \to \beta \oplus \beta'$ is also bijective.
Sum.map_injective theorem
{f : α → γ} {g : β → δ} : Injective (Sum.map f g) ↔ Injective f ∧ Injective g
Full source
@[simp]
theorem map_injective {f : α → γ} {g : β → δ} :
    InjectiveInjective (Sum.map f g) ↔ Injective f ∧ Injective g :=
  ⟨fun h =>
    ⟨fun a₁ a₂ ha => inl_injective <| @h (inl a₁) (inl a₂) (congr_arg inl ha :), fun b₁ b₂ hb =>
      inr_injective <| @h (inr b₁) (inr b₂) (congr_arg inr hb :)⟩,
    fun h => h.1.sumMap h.2⟩
Injectivity of Sum Map is Equivalent to Component Injectivity
Informal description
For any functions $f : \alpha \to \gamma$ and $g : \beta \to \delta$, the sum map $\mathrm{Sum.map}\, f\, g : \alpha \oplus \beta \to \gamma \oplus \delta$ is injective if and only if both $f$ and $g$ are injective.
Sum.map_surjective theorem
{f : α → γ} {g : β → δ} : Surjective (Sum.map f g) ↔ Surjective f ∧ Surjective g
Full source
@[simp]
theorem map_surjective {f : α → γ} {g : β → δ} :
    SurjectiveSurjective (Sum.map f g) ↔ Surjective f ∧ Surjective g :=
  ⟨ fun h => ⟨
      (fun c => by
        obtain ⟨a | b, h⟩ := h (inl c)
        · exact ⟨a, inl_injective h⟩
        · cases h),
      (fun d => by
        obtain ⟨a | b, h⟩ := h (inr d)
        · cases h
        · exact ⟨b, inr_injective h⟩)⟩,
    fun h => h.1.sumMap h.2⟩
Surjectivity of Sum Map is Equivalent to Component Surjectivity
Informal description
For any functions $f : \alpha \to \gamma$ and $g : \beta \to \delta$, the sum map $\mathrm{Sum.map}\, f\, g : \alpha \oplus \beta \to \gamma \oplus \delta$ is surjective if and only if both $f$ and $g$ are surjective.
Sum.map_bijective theorem
{f : α → γ} {g : β → δ} : Bijective (Sum.map f g) ↔ Bijective f ∧ Bijective g
Full source
@[simp]
theorem map_bijective {f : α → γ} {g : β → δ} :
    BijectiveBijective (Sum.map f g) ↔ Bijective f ∧ Bijective g :=
  (map_injective.and map_surjective).trans <| and_and_and_comm
Bijectivity of Sum Map is Equivalent to Component Bijectivity
Informal description
For any functions $f : \alpha \to \gamma$ and $g : \beta \to \delta$, the sum map $\mathrm{Sum.map}\, f\, g : \alpha \oplus \beta \to \gamma \oplus \delta$ is bijective if and only if both $f$ and $g$ are bijective.
Sum3.in₀ definition
(a : α) : α ⊕ (β ⊕ γ)
Full source
/-- The map from the first summand into a ternary sum. -/
@[match_pattern, simp, reducible]
def in₀ (a : α) : α ⊕ (β ⊕ γ) :=
  inl a
Injection into first summand of ternary sum
Informal description
The function maps an element \( a \) of type \( \alpha \) to the left summand of the ternary sum type \( \alpha \oplus (\beta \oplus \gamma) \).
Sum3.in₁ definition
(b : β) : α ⊕ (β ⊕ γ)
Full source
/-- The map from the second summand into a ternary sum. -/
@[match_pattern, simp, reducible]
def in₁ (b : β) : α ⊕ (β ⊕ γ) :=
  inr <| inl b
Injection into the second summand of a ternary sum
Informal description
The function maps an element \( b \) of type \( \beta \) to the second summand in the ternary sum type \( \alpha \oplus (\beta \oplus \gamma) \), specifically as \( \text{inr}(\text{inl}(b)) \).
Sum3.in₂ definition
(c : γ) : α ⊕ (β ⊕ γ)
Full source
/-- The map from the third summand into a ternary sum. -/
@[match_pattern, simp, reducible]
def in₂ (c : γ) : α ⊕ (β ⊕ γ) :=
  inr <| inr c
Injection into third summand of ternary sum
Informal description
The function maps an element \( c \) of type \( \gamma \) to the third summand in the ternary sum type \( \alpha \oplus (\beta \oplus \gamma) \), specifically as \( \text{inr}(\text{inr}(c)) \).
PSum.inl_injective theorem
: Function.Injective (PSum.inl : α → α ⊕' β)
Full source
theorem inl_injective : Function.Injective (PSum.inl : α → α ⊕' β) := fun _ _ ↦ inl.inj
Injectivity of Left Injection in Heterogeneous Sum Type
Informal description
The function $\text{inl} : \alpha \to \alpha \oplus' \beta$ is injective, meaning that for any $a_1, a_2 \in \alpha$, if $\text{inl}(a_1) = \text{inl}(a_2)$, then $a_1 = a_2$.
PSum.inr_injective theorem
: Function.Injective (PSum.inr : β → α ⊕' β)
Full source
theorem inr_injective : Function.Injective (PSum.inr : β → α ⊕' β) := fun _ _ ↦ inr.inj
Injectivity of the Right Inclusion in a Pseudo-Sum Type
Informal description
The function $\text{inr} : \beta \to \alpha \oplus' \beta$ is injective, meaning that for any $b_1, b_2 \in \beta$, if $\text{inr}(b_1) = \text{inr}(b_2)$, then $b_1 = b_2$.