doc-next-gen

Mathlib.Logic.Function.Basic

Module docstring

{"# Miscellaneous function constructions and lemmas ","### Bijectivity of Eq.rec, Eq.mp, Eq.mpr, and cast ","Note these lemmas apply to Type* not Sort*, as the latter interferes with simp, and is trivial anyway. "}

Function.eval definition
{β : α → Sort*} (x : α) (f : ∀ x, β x) : β x
Full source
/-- Evaluate a function at an argument. Useful if you want to talk about the partially applied
  `Function.eval x : (∀ x, β x) → β x`. -/
@[reducible, simp] def eval {β : α → Sort*} (x : α) (f : ∀ x, β x) : β x := f x
Function evaluation
Informal description
The evaluation function takes a dependent function $f$ (where for each $x : \alpha$, $f x$ has type $\beta x$) and an argument $x : \alpha$, and returns the value $f x$ of type $\beta x$.
Function.eval_apply theorem
{β : α → Sort*} (x : α) (f : ∀ x, β x) : eval x f = f x
Full source
theorem eval_apply {β : α → Sort*} (x : α) (f : ∀ x, β x) : eval x f = f x :=
  rfl
Evaluation of Dependent Function: $\mathrm{eval}\,x\,f = f\,x$
Informal description
For any dependent function $f : \forall x, \beta x$ and any element $x : \alpha$, the evaluation of $f$ at $x$ is equal to $f x$.
Function.const_def theorem
{y : β} : (fun _ : α ↦ y) = const α y
Full source
theorem const_def {y : β} : (fun _ : α ↦ y) = const α y :=
  rfl
Definition of Constant Function via Lambda Abstraction
Informal description
For any type $\alpha$ and any element $y$ of type $\beta$, the constant function that maps every element of $\alpha$ to $y$ is equal to the function `const α y`.
Function.const_injective theorem
[Nonempty α] : Injective (const α : β → α → β)
Full source
theorem const_injective [Nonempty α] : Injective (const α : β → α → β) := fun _ _ h ↦
  let ⟨x⟩ := ‹Nonempty α›
  congr_fun h x
Injectivity of Constant Function Constructor
Informal description
For any nonempty type $\alpha$ and any type $\beta$, the constant function constructor $\mathrm{const}_\alpha : \beta \to (\alpha \to \beta)$ is injective. That is, if $\mathrm{const}_\alpha y_1 = \mathrm{const}_\alpha y_2$ for $y_1, y_2 \in \beta$, then $y_1 = y_2$.
Function.const_inj theorem
[Nonempty α] {y₁ y₂ : β} : const α y₁ = const α y₂ ↔ y₁ = y₂
Full source
@[simp]
theorem const_inj [Nonempty α] {y₁ y₂ : β} : constconst α y₁ = const α y₂ ↔ y₁ = y₂ :=
  ⟨fun h ↦ const_injective h, fun h ↦ h ▸ rfl⟩
Equality of Constant Functions is Equivalent to Equality of Values
Informal description
For any nonempty type $\alpha$ and any type $\beta$, and for any elements $y_1, y_2 \in \beta$, the constant functions $\mathrm{const}_\alpha y_1$ and $\mathrm{const}_\alpha y_2$ are equal if and only if $y_1 = y_2$. That is: \[ \mathrm{const}_\alpha y_1 = \mathrm{const}_\alpha y_2 \leftrightarrow y_1 = y_2. \]
Function.onFun_apply theorem
(f : β → β → γ) (g : α → β) (a b : α) : onFun f g a b = f (g a) (g b)
Full source
theorem onFun_apply (f : β → β → γ) (g : α → β) (a b : α) : onFun f g a b = f (g a) (g b) :=
  rfl
Evaluation of Binary Function Composition via Unary Function
Informal description
For any binary function $f \colon \beta \to \beta \to \gamma$, any function $g \colon \alpha \to \beta$, and any elements $a, b \in \alpha$, the function `onFun` satisfies the equation: \[ \text{onFun}(f, g)(a, b) = f(g(a), g(b)). \]
Function.hfunext theorem
{α α' : Sort u} {β : α → Sort v} {β' : α' → Sort v} {f : ∀ a, β a} {f' : ∀ a, β' a} (hα : α = α') (h : ∀ a a', HEq a a' → HEq (f a) (f' a')) : HEq f f'
Full source
lemma hfunext {α α' : Sort u} {β : α → Sort v} {β' : α' → Sort v} {f : ∀a, β a} {f' : ∀a, β' a}
    (hα : α = α') (h : ∀a a', HEq a a' → HEq (f a) (f' a')) : HEq f f' := by
  subst hα
  have : ∀a, HEq (f a) (f' a) := fun a ↦ h a a (HEq.refl a)
  have : β = β' := by funext a; exact type_eq_of_heq (this a)
  subst this
  apply heq_of_eq
  funext a
  exact eq_of_heq (this a)
Heterogeneous Function Extensionality
Informal description
Given types $\alpha$ and $\alpha'$ in universe $u$, type families $\beta : \alpha \to \Sort v$ and $\beta' : \alpha' \to \Sort v$, and dependent functions $f : \forall a, \beta a$ and $f' : \forall a, \beta' a$, if $\alpha = \alpha'$ and for all $a$ and $a'$ such that $a$ is heterogeneously equal to $a'$ we have $f a$ heterogeneously equal to $f' a'$, then $f$ is heterogeneously equal to $f'$.
Function.ne_iff theorem
{β : α → Sort*} {f₁ f₂ : ∀ a, β a} : f₁ ≠ f₂ ↔ ∃ a, f₁ a ≠ f₂ a
Full source
theorem ne_iff {β : α → Sort*} {f₁ f₂ : ∀ a, β a} : f₁ ≠ f₂f₁ ≠ f₂ ↔ ∃ a, f₁ a ≠ f₂ a :=
  funext_iff.not.trans not_forall
Inequality of Functions via Pointwise Inequality
Informal description
For any family of types $\beta : \alpha \to \text{Sort}^*$ and any two functions $f_1, f_2 : \forall a, \beta a$, the functions are not equal if and only if there exists some $a : \alpha$ such that $f_1(a) \neq f_2(a)$. In other words, $f_1 \neq f_2 \leftrightarrow \exists a, f_1(a) \neq f_2(a)$.
Function.funext_iff_of_subsingleton theorem
[Subsingleton α] {g : α → β} (x y : α) : f x = g y ↔ f = g
Full source
lemma funext_iff_of_subsingleton [Subsingleton α] {g : α → β} (x y : α) :
    f x = g y ↔ f = g := by
  refine ⟨fun h ↦ funext fun z ↦ ?_, fun h ↦ ?_⟩
  · rwa [Subsingleton.elim x z, Subsingleton.elim y z] at h
  · rw [h, Subsingleton.elim x y]
Function Extensionality Criterion for Subsingleton Domains
Informal description
Let $\alpha$ be a type with at most one element (i.e., a subsingleton), and let $f, g : \alpha \to \beta$ be functions. For any $x, y \in \alpha$, we have $f(x) = g(y)$ if and only if $f = g$.
Function.swap_lt theorem
{α} [LT α] : swap (· < · : α → α → _) = (· > ·)
Full source
theorem swap_lt {α} [LT α] : swap (· < · : α → α → _) = (· > ·) := rfl
Swapping Arguments of Strict Less-Than Yields Strict Greater-Than
Informal description
For any type $\alpha$ equipped with a strict less-than relation $<$, swapping the arguments of $<$ yields the strict greater-than relation $>$, i.e., $\operatorname{swap} (<) = (>)$.
Function.swap_le theorem
{α} [LE α] : swap (· ≤ · : α → α → _) = (· ≥ ·)
Full source
theorem swap_le {α} [LE α] : swap (· ≤ · : α → α → _) = (· ≥ ·) := rfl
Swapping Arguments of $\leq$ Yields $\geq$
Informal description
For any type $\alpha$ with a less-than-or-equal relation $\leq$, swapping the arguments of $\leq$ yields the greater-than-or-equal relation $\geq$, i.e., $\operatorname{swap}(\leq) = \geq$.
Function.swap_gt theorem
{α} [LT α] : swap (· > · : α → α → _) = (· < ·)
Full source
theorem swap_gt {α} [LT α] : swap (· > · : α → α → _) = (· < ·) := rfl
Swapping Arguments of Greater-Than Yields Less-Than
Informal description
For any type $\alpha$ with a strict order relation $<$, swapping the arguments of the greater-than relation $>$ yields the less-than relation $<$. That is, $\operatorname{swap} (>) = (<)$.
Function.swap_ge theorem
{α} [LE α] : swap (· ≥ · : α → α → _) = (· ≤ ·)
Full source
theorem swap_ge {α} [LE α] : swap (· ≥ · : α → α → _) = (· ≤ ·) := rfl
Swapping Arguments of $\geq$ Yields $\leq$
Informal description
For any type $\alpha$ equipped with a less-than-or-equal relation $\leq$, swapping the arguments of the greater-than-or-equal relation $\geq$ yields the less-than-or-equal relation, i.e., $\operatorname{swap}(\geq) = \leq$.
Function.Bijective.injective theorem
{f : α → β} (hf : Bijective f) : Injective f
Full source
protected theorem Bijective.injective {f : α → β} (hf : Bijective f) : Injective f := hf.1
Bijectivity implies injectivity
Informal description
For any function $f : \alpha \to \beta$, if $f$ is bijective, then $f$ is injective.
Function.Bijective.surjective theorem
{f : α → β} (hf : Bijective f) : Surjective f
Full source
protected theorem Bijective.surjective {f : α → β} (hf : Bijective f) : Surjective f := hf.2
Bijective Functions are Surjective
Informal description
If a function $f : \alpha \to \beta$ is bijective, then it is surjective.
Function.not_injective_iff theorem
: ¬Injective f ↔ ∃ a b, f a = f b ∧ a ≠ b
Full source
theorem not_injective_iff : ¬ Injective f¬ Injective f ↔ ∃ a b, f a = f b ∧ a ≠ b := by
  simp only [Injective, not_forall, exists_prop]
Non-injectivity Criterion: $\neg \text{Injective}\, f \leftrightarrow \exists a \neq b, f(a) = f(b)$
Informal description
A function $f : \alpha \to \beta$ is not injective if and only if there exist distinct elements $a, b \in \alpha$ such that $f(a) = f(b)$.
Function.Injective.decidableEq definition
[DecidableEq β] (I : Injective f) : DecidableEq α
Full source
/-- If the co-domain `β` of an injective function `f : α → β` has decidable equality, then
the domain `α` also has decidable equality. -/
protected def Injective.decidableEq [DecidableEq β] (I : Injective f) : DecidableEq α :=
  fun _ _ ↦ decidable_of_iff _ I.eq_iff
Decidable equality induced by an injective function
Informal description
Given a function \( f : \alpha \to \beta \) and a decidable equality on \( \beta \), if \( f \) is injective, then \( \alpha \) also has decidable equality. Specifically, for any \( x, y \in \alpha \), the equality \( x = y \) is decidable by checking \( f(x) = f(y) \).
Function.Injective.of_comp theorem
{g : γ → α} (I : Injective (f ∘ g)) : Injective g
Full source
theorem Injective.of_comp {g : γ → α} (I : Injective (f ∘ g)) : Injective g :=
  fun _ _ h ↦ I <| congr_arg f h
Injectivity of Composition Implies Injectivity of Inner Function
Informal description
Let $f : \alpha \to \beta$ and $g : \gamma \to \alpha$ be functions. If the composition $f \circ g$ is injective, then $g$ is injective.
Function.Injective.of_comp_iff theorem
(hf : Injective f) (g : γ → α) : Injective (f ∘ g) ↔ Injective g
Full source
@[simp]
theorem Injective.of_comp_iff (hf : Injective f) (g : γ → α) :
    InjectiveInjective (f ∘ g) ↔ Injective g :=
  ⟨Injective.of_comp, hf.comp⟩
Injectivity of Composition Equivalence for Injective Functions
Informal description
Let $f \colon \alpha \to \beta$ be an injective function and $g \colon \gamma \to \alpha$ be any function. Then the composition $f \circ g$ is injective if and only if $g$ is injective.
Function.Injective.of_comp_right theorem
{g : γ → α} (I : Injective (f ∘ g)) (hg : Surjective g) : Injective f
Full source
theorem Injective.of_comp_right {g : γ → α} (I : Injective (f ∘ g)) (hg : Surjective g) :
    Injective f := fun x y h ↦ by
  obtain ⟨x, rfl⟩ := hg x
  obtain ⟨y, rfl⟩ := hg y
  exact congr_arg g (I h)
Injectivity of a Function via Composition with a Surjective Function
Informal description
Let \( f : \alpha \to \beta \) and \( g : \gamma \to \alpha \) be functions. If the composition \( f \circ g \) is injective and \( g \) is surjective, then \( f \) is injective.
Function.Surjective.bijective₂_of_injective theorem
{g : γ → α} (hf : Surjective f) (hg : Surjective g) (I : Injective (f ∘ g)) : Bijective f ∧ Bijective g
Full source
theorem Surjective.bijective₂_of_injective {g : γ → α} (hf : Surjective f) (hg : Surjective g)
    (I : Injective (f ∘ g)) : BijectiveBijective f ∧ Bijective g :=
  ⟨⟨I.of_comp_right hg, hf⟩, I.of_comp, hg⟩
Bijectivity of Composed Functions via Surjectivity and Injectivity
Informal description
Let $f : \alpha \to \beta$ and $g : \gamma \to \alpha$ be functions. If $f$ and $g$ are both surjective and the composition $f \circ g$ is injective, then both $f$ and $g$ are bijective.
Function.Injective.of_comp_iff' theorem
(f : α → β) {g : γ → α} (hg : Bijective g) : Injective (f ∘ g) ↔ Injective f
Full source
@[simp]
theorem Injective.of_comp_iff' (f : α → β) {g : γ → α} (hg : Bijective g) :
    InjectiveInjective (f ∘ g) ↔ Injective f :=
  ⟨fun I ↦ I.of_comp_right hg.2, fun h ↦ h.comp hg.injective⟩
Injectivity of $f$ via composition with a bijective function $g$: $f \circ g$ injective $\leftrightarrow$ $f$ injective
Informal description
For any function $f \colon \alpha \to \beta$ and a bijective function $g \colon \gamma \to \alpha$, the composition $f \circ g$ is injective if and only if $f$ is injective.
Function.Injective.piMap theorem
{ι : Sort*} {α β : ι → Sort*} {f : ∀ i, α i → β i} (hf : ∀ i, Injective (f i)) : Injective (Pi.map f)
Full source
theorem Injective.piMap {ι : Sort*} {α β : ι → Sort*} {f : ∀ i, α i → β i}
    (hf : ∀ i, Injective (f i)) : Injective (Pi.map f) := fun _ _ h ↦
  funext fun i ↦ hf i <| congrFun h _
Injectivity of Component-wise Mapping for Dependent Functions
Informal description
Let $\iota$ be a type, and for each $i \in \iota$, let $\alpha_i$ and $\beta_i$ be types with functions $f_i : \alpha_i \to \beta_i$. If each $f_i$ is injective, then the component-wise mapping function $\text{Pi.map} \, f : (\forall i, \alpha_i) \to (\forall i, \beta_i)$ is also injective.
Function.Injective.comp_left theorem
{g : β → γ} (hg : Injective g) : Injective (g ∘ · : (α → β) → α → γ)
Full source
/-- Composition by an injective function on the left is itself injective. -/
theorem Injective.comp_left {g : β → γ} (hg : Injective g) : Injective (g ∘ · : (α → β) → α → γ) :=
  .piMap fun _ ↦ hg
Injectivity of Left Composition with Injective Function
Informal description
Let $g : \beta \to \gamma$ be an injective function. Then the left composition with $g$, i.e., the function $(g \circ \cdot) : (\alpha \to \beta) \to (\alpha \to \gamma)$ defined by $(g \circ \cdot)(f) = g \circ f$, is also injective.
Function.injective_comp_left_iff theorem
[Nonempty α] {g : β → γ} : Injective (g ∘ · : (α → β) → α → γ) ↔ Injective g
Full source
theorem injective_comp_left_iff [Nonempty α] {g : β → γ} :
    InjectiveInjective (g ∘ · : (α → β) → α → γ) ↔ Injective g :=
  ⟨fun h b₁ b₂ eq ↦ Nonempty.elim ‹_›
    (congr_fun <| h (a₁ := fun _ ↦ b₁) (a₂ := fun _ ↦ b₂) <| funext fun _ ↦ eq), (·.comp_left)⟩
Injectivity of Left Composition is Equivalent to Injectivity of $g$
Informal description
Let $\alpha$ be a nonempty type and $g : \beta \to \gamma$ be a function. The left composition with $g$, i.e., the function $(g \circ \cdot) : (\alpha \to \beta) \to (\alpha \to \gamma)$ defined by $(g \circ \cdot)(f) = g \circ f$, is injective if and only if $g$ is injective.
Function.injective_of_subsingleton theorem
[Subsingleton α] (f : α → β) : Injective f
Full source
@[nontriviality] theorem injective_of_subsingleton [Subsingleton α] (f : α → β) : Injective f :=
  fun _ _ _ ↦ Subsingleton.elim _ _
Injectivity of Functions from Subsingleton Types
Informal description
For any subsingleton type $\alpha$ (a type with at most one element) and any function $f : \alpha \to \beta$, the function $f$ is injective.
Function.Injective.dite theorem
(p : α → Prop) [DecidablePred p] {f : { a : α // p a } → β} {f' : { a : α // ¬p a } → β} (hf : Injective f) (hf' : Injective f') (im_disj : ∀ {x x' : α} {hx : p x} {hx' : ¬p x'}, f ⟨x, hx⟩ ≠ f' ⟨x', hx'⟩) : Function.Injective (fun x ↦ if h : p x then f ⟨x, h⟩ else f' ⟨x, h⟩)
Full source
lemma Injective.dite (p : α → Prop) [DecidablePred p]
    {f : {a : α // p a} → β} {f' : {a : α // ¬ p a} → β}
    (hf : Injective f) (hf' : Injective f')
    (im_disj : ∀ {x x' : α} {hx : p x} {hx' : ¬ p x'}, f ⟨x, hx⟩ ≠ f' ⟨x', hx'⟩) :
    Function.Injective (fun x ↦ if h : p x then f ⟨x, h⟩ else f' ⟨x, h⟩) := fun x₁ x₂ h => by
 dsimp only at h
 by_cases h₁ : p x₁ <;> by_cases h₂ : p x₂
 · rw [dif_pos h₁, dif_pos h₂] at h; injection (hf h)
 · rw [dif_pos h₁, dif_neg h₂] at h; exact (im_disj h).elim
 · rw [dif_neg h₁, dif_pos h₂] at h; exact (im_disj h.symm).elim
 · rw [dif_neg h₁, dif_neg h₂] at h; injection (hf' h)
Injectivity of Piecewise Defined Function via Decidable Predicate
Informal description
Let $p : \alpha \to \mathrm{Prop}$ be a decidable predicate, and let $f : \{a \in \alpha \mid p(a)\} \to \beta$ and $f' : \{a \in \alpha \mid \neg p(a)\} \to \beta$ be injective functions. If for all $x, x' \in \alpha$ with $p(x)$ and $\neg p(x')$, we have $f(x) \neq f'(x')$, then the function defined by \[ x \mapsto \begin{cases} f(x) & \text{if } p(x), \\ f'(x) & \text{otherwise} \end{cases} \] is injective.
Function.Surjective.of_comp theorem
{g : γ → α} (S : Surjective (f ∘ g)) : Surjective f
Full source
theorem Surjective.of_comp {g : γ → α} (S : Surjective (f ∘ g)) : Surjective f := fun y ↦
  let ⟨x, h⟩ := S y
  ⟨g x, h⟩
Surjectivity of a Function via Composition
Informal description
Let $f : \alpha \to \beta$ and $g : \gamma \to \alpha$ be functions. If the composition $f \circ g$ is surjective, then $f$ is surjective.
Function.Surjective.of_comp_iff theorem
(f : α → β) {g : γ → α} (hg : Surjective g) : Surjective (f ∘ g) ↔ Surjective f
Full source
@[simp]
theorem Surjective.of_comp_iff (f : α → β) {g : γ → α} (hg : Surjective g) :
    SurjectiveSurjective (f ∘ g) ↔ Surjective f :=
  ⟨Surjective.of_comp, fun h ↦ h.comp hg⟩
Surjectivity of Composition Equivalence: $f \circ g$ is surjective iff $f$ is surjective (given $g$ is surjective)
Informal description
Let $f \colon \alpha \to \beta$ be a function and $g \colon \gamma \to \alpha$ be a surjective function. Then the composition $f \circ g$ is surjective if and only if $f$ is surjective.
Function.Surjective.of_comp_left theorem
{g : γ → α} (S : Surjective (f ∘ g)) (hf : Injective f) : Surjective g
Full source
theorem Surjective.of_comp_left {g : γ → α} (S : Surjective (f ∘ g)) (hf : Injective f) :
    Surjective g := fun a ↦ let ⟨c, hc⟩ := S (f a); ⟨c, hf hc⟩
Surjectivity of a Function via Composition with an Injective Function
Informal description
Let \( f : \alpha \to \beta \) be an injective function and \( g : \gamma \to \alpha \) be any function. If the composition \( f \circ g \) is surjective, then \( g \) is surjective.
Function.Injective.bijective₂_of_surjective theorem
{g : γ → α} (hf : Injective f) (hg : Injective g) (S : Surjective (f ∘ g)) : Bijective f ∧ Bijective g
Full source
theorem Injective.bijective₂_of_surjective {g : γ → α} (hf : Injective f) (hg : Injective g)
    (S : Surjective (f ∘ g)) : BijectiveBijective f ∧ Bijective g :=
  ⟨⟨hf, S.of_comp⟩, hg, S.of_comp_left hf⟩
Bijectivity of Injective Functions with Surjective Composition
Informal description
Let $f \colon \alpha \to \beta$ and $g \colon \gamma \to \alpha$ be injective functions. If the composition $f \circ g$ is surjective, then both $f$ and $g$ are bijective.
Function.Surjective.of_comp_iff' theorem
(hf : Bijective f) (g : γ → α) : Surjective (f ∘ g) ↔ Surjective g
Full source
@[simp]
theorem Surjective.of_comp_iff' (hf : Bijective f) (g : γ → α) :
    SurjectiveSurjective (f ∘ g) ↔ Surjective g :=
  ⟨fun S ↦ S.of_comp_left hf.1, hf.surjective.comp⟩
Surjectivity of Composition with Bijective Function
Informal description
Let $f \colon \alpha \to \beta$ be a bijective function and $g \colon \gamma \to \alpha$ be any function. Then the composition $f \circ g \colon \gamma \to \beta$ is surjective if and only if $g$ is surjective.
Function.decidableEqPFun instance
(p : Prop) [Decidable p] (α : p → Type*) [∀ hp, DecidableEq (α hp)] : DecidableEq (∀ hp, α hp)
Full source
instance decidableEqPFun (p : Prop) [Decidable p] (α : p → Type*) [∀ hp, DecidableEq (α hp)] :
    DecidableEq (∀ hp, α hp)
  | f, g => decidable_of_iff (∀ hp, f hp = g hp) funext_iff.symm
Decidable Equality for Dependent Functions over Decidable Propositions
Informal description
For any proposition $p$ with a decidable instance, and any family of types $\alpha : p \to \text{Type*}$ where each $\alpha\ hp$ has decidable equality, the type of dependent functions $\forall hp, \alpha\ hp$ also has decidable equality.
Function.Surjective.forall theorem
(hf : Surjective f) {p : β → Prop} : (∀ y, p y) ↔ ∀ x, p (f x)
Full source
protected theorem Surjective.forall (hf : Surjective f) {p : β → Prop} :
    (∀ y, p y) ↔ ∀ x, p (f x) :=
  ⟨fun h x ↦ h (f x), fun h y ↦
    let ⟨x, hx⟩ := hf y
    hx ▸ h x⟩
Universal Quantifier Preservation under Surjective Functions
Informal description
Let $f : \alpha \to \beta$ be a surjective function. For any predicate $p : \beta \to \text{Prop}$, the statement $(\forall y \in \beta, p(y))$ holds if and only if $(\forall x \in \alpha, p(f(x)))$ holds.
Function.Surjective.forall₂ theorem
(hf : Surjective f) {p : β → β → Prop} : (∀ y₁ y₂, p y₁ y₂) ↔ ∀ x₁ x₂, p (f x₁) (f x₂)
Full source
protected theorem Surjective.forall₂ (hf : Surjective f) {p : β → β → Prop} :
    (∀ y₁ y₂, p y₁ y₂) ↔ ∀ x₁ x₂, p (f x₁) (f x₂) :=
  hf.forall.trans <| forall_congr' fun _ ↦ hf.forall
Universal Quantifier Preservation for Binary Predicates under Surjective Functions
Informal description
Let $f : \alpha \to \beta$ be a surjective function. For any binary predicate $p : \beta \to \beta \to \text{Prop}$, the statement $(\forall y_1 y_2 \in \beta, p(y_1, y_2))$ holds if and only if $(\forall x_1 x_2 \in \alpha, p(f(x_1), f(x_2)))$ holds.
Function.Surjective.forall₃ theorem
(hf : Surjective f) {p : β → β → β → Prop} : (∀ y₁ y₂ y₃, p y₁ y₂ y₃) ↔ ∀ x₁ x₂ x₃, p (f x₁) (f x₂) (f x₃)
Full source
protected theorem Surjective.forall₃ (hf : Surjective f) {p : β → β → β → Prop} :
    (∀ y₁ y₂ y₃, p y₁ y₂ y₃) ↔ ∀ x₁ x₂ x₃, p (f x₁) (f x₂) (f x₃) :=
  hf.forall.trans <| forall_congr' fun _ ↦ hf.forall₂
Universal Quantifier Preservation for Ternary Predicates under Surjective Functions
Informal description
Let $f : \alpha \to \beta$ be a surjective function. For any ternary predicate $p : \beta \to \beta \to \beta \to \text{Prop}$, the statement $(\forall y_1 y_2 y_3 \in \beta, p(y_1, y_2, y_3))$ holds if and only if $(\forall x_1 x_2 x_3 \in \alpha, p(f(x_1), f(x_2), f(x_3)))$ holds.
Function.Surjective.exists theorem
(hf : Surjective f) {p : β → Prop} : (∃ y, p y) ↔ ∃ x, p (f x)
Full source
protected theorem Surjective.exists (hf : Surjective f) {p : β → Prop} :
    (∃ y, p y) ↔ ∃ x, p (f x) :=
  ⟨fun ⟨y, hy⟩ ↦
    let ⟨x, hx⟩ := hf y
    ⟨x, hx.symm ▸ hy⟩,
    fun ⟨x, hx⟩ ↦ ⟨f x, hx⟩⟩
Existence Transfer via Surjective Function
Informal description
Let $f : \alpha \to \beta$ be a surjective function. For any predicate $p$ on $\beta$, there exists an element $y \in \beta$ satisfying $p(y)$ if and only if there exists an element $x \in \alpha$ such that $p(f(x))$ holds.
Function.Surjective.exists₂ theorem
(hf : Surjective f) {p : β → β → Prop} : (∃ y₁ y₂, p y₁ y₂) ↔ ∃ x₁ x₂, p (f x₁) (f x₂)
Full source
protected theorem Surjective.exists₂ (hf : Surjective f) {p : β → β → Prop} :
    (∃ y₁ y₂, p y₁ y₂) ↔ ∃ x₁ x₂, p (f x₁) (f x₂) :=
  hf.exists.trans <| exists_congr fun _ ↦ hf.exists
Existence Transfer for Binary Predicates via Surjective Function
Informal description
Let $f : \alpha \to \beta$ be a surjective function. For any binary predicate $p$ on $\beta$, there exist elements $y_1, y_2 \in \beta$ satisfying $p(y_1, y_2)$ if and only if there exist elements $x_1, x_2 \in \alpha$ such that $p(f(x_1), f(x_2))$ holds.
Function.Surjective.exists₃ theorem
(hf : Surjective f) {p : β → β → β → Prop} : (∃ y₁ y₂ y₃, p y₁ y₂ y₃) ↔ ∃ x₁ x₂ x₃, p (f x₁) (f x₂) (f x₃)
Full source
protected theorem Surjective.exists₃ (hf : Surjective f) {p : β → β → β → Prop} :
    (∃ y₁ y₂ y₃, p y₁ y₂ y₃) ↔ ∃ x₁ x₂ x₃, p (f x₁) (f x₂) (f x₃) :=
  hf.exists.trans <| exists_congr fun _ ↦ hf.exists₂
Existence Transfer for Ternary Predicates via Surjective Function
Informal description
Let $f : \alpha \to \beta$ be a surjective function. For any ternary predicate $p$ on $\beta$, there exist elements $y_1, y_2, y_3 \in \beta$ satisfying $p(y_1, y_2, y_3)$ if and only if there exist elements $x_1, x_2, x_3 \in \alpha$ such that $p(f(x_1), f(x_2), f(x_3))$ holds.
Function.Surjective.injective_comp_right theorem
(hf : Surjective f) : Injective fun g : β → γ ↦ g ∘ f
Full source
theorem Surjective.injective_comp_right (hf : Surjective f) : Injective fun g : β → γ ↦ g ∘ f :=
  fun _ _ h ↦ funext <| hf.forall.2 <| congr_fun h
Injectivity of Post-Composition with a Surjective Function
Informal description
Let $f : \alpha \to \beta$ be a surjective function. Then the function composition map $g \mapsto g \circ f$ from $\beta \to \gamma$ to $\alpha \to \gamma$ is injective.
Function.injective_comp_right_iff_surjective theorem
{γ : Type*} [Nontrivial γ] : Injective (fun g : β → γ ↦ g ∘ f) ↔ Surjective f
Full source
theorem injective_comp_right_iff_surjective {γ : Type*} [Nontrivial γ] :
    InjectiveInjective (fun g : β → γ ↦ g ∘ f) ↔ Surjective f := by
  refine ⟨not_imp_not.mp fun not_surj inj ↦ not_subsingleton γ ⟨fun c c' ↦ ?_⟩,
    (·.injective_comp_right)⟩
  have ⟨b₀, hb⟩ := not_forall.mp not_surj
  classical have := inj (a₁ := fun _ ↦ c) (a₂ := (if · = b₀ then c' else c)) ?_
  · simpa using congr_fun this b₀
  ext a; simp only [comp_apply, if_neg fun h ↦ hb ⟨a, h⟩]
Injectivity of Post-Composition Equivalence to Surjectivity of Function
Informal description
For any nontrivial type $\gamma$, the function composition map $g \mapsto g \circ f$ from $\beta \to \gamma$ to $\alpha \to \gamma$ is injective if and only if the function $f : \alpha \to \beta$ is surjective.
Function.Surjective.right_cancellable theorem
(hf : Surjective f) {g₁ g₂ : β → γ} : g₁ ∘ f = g₂ ∘ f ↔ g₁ = g₂
Full source
protected theorem Surjective.right_cancellable (hf : Surjective f) {g₁ g₂ : β → γ} :
    g₁ ∘ fg₁ ∘ f = g₂ ∘ f ↔ g₁ = g₂ :=
  hf.injective_comp_right.eq_iff
Right Cancellation Property for Surjective Functions
Informal description
Let $f : \alpha \to \beta$ be a surjective function. For any two functions $g_1, g_2 : \beta \to \gamma$, the compositions $g_1 \circ f$ and $g_2 \circ f$ are equal if and only if $g_1 = g_2$.
Function.surjective_of_right_cancellable_Prop theorem
(h : ∀ g₁ g₂ : β → Prop, g₁ ∘ f = g₂ ∘ f → g₁ = g₂) : Surjective f
Full source
theorem surjective_of_right_cancellable_Prop (h : ∀ g₁ g₂ : β → Prop, g₁ ∘ f = g₂ ∘ f → g₁ = g₂) :
    Surjective f :=
  injective_comp_right_iff_surjective.mp h
Surjectivity from Right Cancellation in Prop Composition
Informal description
A function $f \colon \alpha \to \beta$ is surjective if for any two functions $g_1, g_2 \colon \beta \to \mathrm{Prop}$, the equality $g_1 \circ f = g_2 \circ f$ implies $g_1 = g_2$.
Function.bijective_iff_existsUnique theorem
(f : α → β) : Bijective f ↔ ∀ b : β, ∃! a : α, f a = b
Full source
theorem bijective_iff_existsUnique (f : α → β) : BijectiveBijective f ↔ ∀ b : β, ∃! a : α, f a = b :=
  ⟨fun hf b ↦
      let ⟨a, ha⟩ := hf.surjective b
      ⟨a, ha, fun _ ha' ↦ hf.injective (ha'.trans ha.symm)⟩,
    fun he ↦ ⟨fun {_a a'} h ↦ (he (f a')).unique h rfl, fun b ↦ (he b).exists⟩⟩
Bijectivity Characterized by Unique Preimages
Informal description
A function $f : \alpha \to \beta$ is bijective if and only if for every $b \in \beta$, there exists a unique $a \in \alpha$ such that $f(a) = b$.
Function.Bijective.existsUnique theorem
{f : α → β} (hf : Bijective f) (b : β) : ∃! a : α, f a = b
Full source
/-- Shorthand for using projection notation with `Function.bijective_iff_existsUnique`. -/
protected theorem Bijective.existsUnique {f : α → β} (hf : Bijective f) (b : β) :
    ∃! a : α, f a = b :=
  (bijective_iff_existsUnique f).mp hf b
Unique Preimage Existence for Bijective Functions
Informal description
For any bijective function $f \colon \alpha \to \beta$ and any element $b \in \beta$, there exists a unique element $a \in \alpha$ such that $f(a) = b$.
Function.Bijective.existsUnique_iff theorem
{f : α → β} (hf : Bijective f) {p : β → Prop} : (∃! y, p y) ↔ ∃! x, p (f x)
Full source
theorem Bijective.existsUnique_iff {f : α → β} (hf : Bijective f) {p : β → Prop} :
    (∃! y, p y) ↔ ∃! x, p (f x) :=
  ⟨fun ⟨y, hpy, hy⟩ ↦
    let ⟨x, hx⟩ := hf.surjective y
    ⟨x, by simpa [hx], fun z (hz : p (f z)) ↦ hf.injective <| hx.symm ▸ hy _ hz⟩,
    fun ⟨x, hpx, hx⟩ ↦
    ⟨f x, hpx, fun y hy ↦
      let ⟨z, hz⟩ := hf.surjective y
      hz ▸ congr_arg f (hx _ (by simpa [hz]))⟩⟩
Bijection Preserves Unique Existence of Predicates
Informal description
Let $f : \alpha \to \beta$ be a bijective function. For any predicate $p : \beta \to \mathrm{Prop}$, there exists a unique $y \in \beta$ satisfying $p(y)$ if and only if there exists a unique $x \in \alpha$ satisfying $p(f(x))$.
Function.Bijective.of_comp_iff theorem
(f : α → β) {g : γ → α} (hg : Bijective g) : Bijective (f ∘ g) ↔ Bijective f
Full source
theorem Bijective.of_comp_iff (f : α → β) {g : γ → α} (hg : Bijective g) :
    BijectiveBijective (f ∘ g) ↔ Bijective f :=
  and_congr (Injective.of_comp_iff' _ hg) (Surjective.of_comp_iff _ hg.surjective)
Bijectivity of Composition with Bijective Function: $f \circ g$ bijective $\leftrightarrow$ $f$ bijective
Informal description
Let $f \colon \alpha \to \beta$ be a function and $g \colon \gamma \to \alpha$ be a bijective function. Then the composition $f \circ g \colon \gamma \to \beta$ is bijective if and only if $f$ is bijective.
Function.Bijective.of_comp_iff' theorem
{f : α → β} (hf : Bijective f) (g : γ → α) : Function.Bijective (f ∘ g) ↔ Function.Bijective g
Full source
theorem Bijective.of_comp_iff' {f : α → β} (hf : Bijective f) (g : γ → α) :
    Function.BijectiveFunction.Bijective (f ∘ g) ↔ Function.Bijective g :=
  and_congr (Injective.of_comp_iff hf.injective _) (Surjective.of_comp_iff' hf _)
Bijectivity of Composition with Bijective Function
Informal description
Let $f \colon \alpha \to \beta$ be a bijective function and $g \colon \gamma \to \alpha$ be any function. Then the composition $f \circ g \colon \gamma \to \beta$ is bijective if and only if $g$ is bijective.
Function.cantor_surjective theorem
{α} (f : α → Set α) : ¬Surjective f
Full source
/-- **Cantor's diagonal argument** implies that there are no surjective functions from `α`
to `Set α`. -/
theorem cantor_surjective {α} (f : α → Set α) : ¬Surjective f
  | h => let ⟨D, e⟩ := h {a | ¬ f a a}
        @iff_not_self (D ∈ f D) <| iff_of_eq <| congr_arg (D ∈ ·) e
Cantor's Theorem: No Surjection from a Set to its Power Set
Informal description
For any type $\alpha$, there is no surjective function from $\alpha$ to the power set of $\alpha$, i.e., there is no function $f : \alpha \to \mathcal{P}(\alpha)$ such that for every subset $S \subseteq \alpha$, there exists an element $a \in \alpha$ with $f(a) = S$.
Function.cantor_injective theorem
{α : Type*} (f : Set α → α) : ¬Injective f
Full source
/-- **Cantor's diagonal argument** implies that there are no injective functions from `Set α`
to `α`. -/
theorem cantor_injective {α : Type*} (f : Set α → α) : ¬Injective f
  | i => cantor_surjective (fun a ↦ {b | ∀ U, a = f U → U b}) <|
         RightInverse.surjective (fun U ↦ Set.ext fun _ ↦ ⟨fun h ↦ h U rfl, fun h _ e ↦ i e ▸ h⟩)
Cantor's Theorem: No Injection from Power Set to Set
Informal description
For any type $\alpha$, there is no injective function from the power set of $\alpha$ to $\alpha$, i.e., there is no function $f : \mathcal{P}(\alpha) \to \alpha$ such that for any two subsets $S, T \subseteq \alpha$, $f(S) = f(T)$ implies $S = T$.
Function.not_surjective_Type theorem
{α : Type u} (f : α → Type max u v) : ¬Surjective f
Full source
/-- There is no surjection from `α : Type u` into `Type (max u v)`. This theorem
  demonstrates why `Type : Type` would be inconsistent in Lean. -/
theorem not_surjective_Type {α : Type u} (f : α → Type max u v) : ¬Surjective f := by
  intro hf
  let T : Type max u v := Sigma f
  cases hf (Set T) with | intro U hU =>
  let g : Set T → T := fun s ↦ ⟨U, cast hU.symm s⟩
  have hg : Injective g := by
    intro s t h
    suffices cast hU (g s).2 = cast hU (g t).2 by
      simp only [g, cast_cast, cast_eq] at this
      assumption
    · congr
  exact cantor_injective g hg
No Surjection from Type to Higher Universe
Informal description
For any type $\alpha$ in universe level $u$, there is no surjective function from $\alpha$ to the type universe $\text{Type}(\max(u,v))$. That is, for any function $f : \alpha \to \text{Type}(\max(u,v))$, there exists some type $T \in \text{Type}(\max(u,v))$ such that $T$ is not in the image of $f$.
Function.IsPartialInv definition
{α β} (f : α → β) (g : β → Option α) : Prop
Full source
/-- `g` is a partial inverse to `f` (an injective but not necessarily
  surjective function) if `g y = some x` implies `f x = y`, and `g y = none`
  implies that `y` is not in the range of `f`. -/
def IsPartialInv {α β} (f : α → β) (g : β → Option α) : Prop :=
  ∀ x y, g y = some x ↔ f x = y
Partial inverse of a function
Informal description
Given a function $f : \alpha \to \beta$, a function $g : \beta \to \text{Option } \alpha$ is called a *partial inverse* to $f$ if for all $x \in \alpha$ and $y \in \beta$, the condition $g(y) = \text{some }x$ holds if and only if $f(x) = y$. This means $g$ correctly identifies preimages of $f$ when they exist and returns `none` otherwise.
Function.isPartialInv_left theorem
{α β} {f : α → β} {g} (H : IsPartialInv f g) (x) : g (f x) = some x
Full source
theorem isPartialInv_left {α β} {f : α → β} {g} (H : IsPartialInv f g) (x) : g (f x) = some x :=
  (H _ _).2 rfl
Partial Inverse Property: $g(f(x)) = \text{some }x$
Informal description
For any function $f : \alpha \to \beta$ and partial inverse $g : \beta \to \text{Option } \alpha$, if $g$ is a partial inverse of $f$ (i.e., $g(y) = \text{some }x$ if and only if $f(x) = y$), then for any $x \in \alpha$, we have $g(f(x)) = \text{some }x$.
Function.injective_of_isPartialInv theorem
{α β} {f : α → β} {g} (H : IsPartialInv f g) : Injective f
Full source
theorem injective_of_isPartialInv {α β} {f : α → β} {g} (H : IsPartialInv f g) :
    Injective f := fun _ _ h ↦
  Option.some.inj <| ((H _ _).2 h).symm.trans ((H _ _).2 rfl)
Injectivity from Partial Inverse
Informal description
Given a function $f : \alpha \to \beta$ and a partial inverse $g : \beta \to \text{Option } \alpha$ of $f$, the function $f$ is injective.
Function.injective_of_isPartialInv_right theorem
{α β} {f : α → β} {g} (H : IsPartialInv f g) (x y b) (h₁ : b ∈ g x) (h₂ : b ∈ g y) : x = y
Full source
theorem injective_of_isPartialInv_right {α β} {f : α → β} {g} (H : IsPartialInv f g) (x y b)
    (h₁ : b ∈ g x) (h₂ : b ∈ g y) : x = y :=
  ((H _ _).1 h₁).symm.trans ((H _ _).1 h₂)
Partial Inverse Implies Right Injectivity
Informal description
Let $f : \alpha \to \beta$ be a function with a partial inverse $g : \beta \to \text{Option } \alpha$. For any $x, y \in \beta$ and $b \in \alpha$, if $b$ is in the preimage of $x$ under $g$ (i.e., $g(x) = \text{some }b$) and also in the preimage of $y$ under $g$ (i.e., $g(y) = \text{some }b$), then $x = y$.
Function.LeftInverse.comp_eq_id theorem
{f : α → β} {g : β → α} (h : LeftInverse f g) : f ∘ g = id
Full source
theorem LeftInverse.comp_eq_id {f : α → β} {g : β → α} (h : LeftInverse f g) : f ∘ g = id :=
  funext h
Composition of a function with its left inverse yields the identity function
Informal description
Given functions $f : \alpha \to \beta$ and $g : \beta \to \alpha$, if $g$ is a left inverse of $f$, then the composition $f \circ g$ is equal to the identity function on $\beta$.
Function.leftInverse_iff_comp theorem
{f : α → β} {g : β → α} : LeftInverse f g ↔ f ∘ g = id
Full source
theorem leftInverse_iff_comp {f : α → β} {g : β → α} : LeftInverseLeftInverse f g ↔ f ∘ g = id :=
  ⟨LeftInverse.comp_eq_id, congr_fun⟩
Left Inverse Characterization via Composition with Identity
Informal description
For functions $f : \alpha \to \beta$ and $g : \beta \to \alpha$, the function $g$ is a left inverse of $f$ if and only if the composition $f \circ g$ is equal to the identity function on $\beta$, i.e., $f \circ g = \text{id}$.
Function.RightInverse.comp_eq_id theorem
{f : α → β} {g : β → α} (h : RightInverse f g) : g ∘ f = id
Full source
theorem RightInverse.comp_eq_id {f : α → β} {g : β → α} (h : RightInverse f g) : g ∘ f = id :=
  funext h
Composition of a function with its right inverse yields the identity function
Informal description
Given functions $f : \alpha \to \beta$ and $g : \beta \to \alpha$, if $g$ is a right inverse of $f$, then the composition $g \circ f$ is equal to the identity function on $\alpha$.
Function.rightInverse_iff_comp theorem
{f : α → β} {g : β → α} : RightInverse f g ↔ g ∘ f = id
Full source
theorem rightInverse_iff_comp {f : α → β} {g : β → α} : RightInverseRightInverse f g ↔ g ∘ f = id :=
  ⟨RightInverse.comp_eq_id, congr_fun⟩
Right Inverse Characterization via Composition with Identity
Informal description
For functions $f : \alpha \to \beta$ and $g : \beta \to \alpha$, the function $g$ is a right inverse of $f$ if and only if the composition $g \circ f$ is equal to the identity function on $\alpha$, i.e., $g \circ f = \text{id}$.
Function.LeftInverse.comp theorem
{f : α → β} {g : β → α} {h : β → γ} {i : γ → β} (hf : LeftInverse f g) (hh : LeftInverse h i) : LeftInverse (h ∘ f) (g ∘ i)
Full source
theorem LeftInverse.comp {f : α → β} {g : β → α} {h : β → γ} {i : γ → β} (hf : LeftInverse f g)
    (hh : LeftInverse h i) : LeftInverse (h ∘ f) (g ∘ i) :=
  fun a ↦ show h (f (g (i a))) = a by rw [hf (i a), hh a]
Composition Preserves Left Inverses
Informal description
Given functions $f : \alpha \to \beta$, $g : \beta \to \alpha$, $h : \beta \to \gamma$, and $i : \gamma \to \beta$, if $g$ is a left inverse of $f$ and $i$ is a left inverse of $h$, then the composition $g \circ i$ is a left inverse of the composition $h \circ f$.
Function.RightInverse.comp theorem
{f : α → β} {g : β → α} {h : β → γ} {i : γ → β} (hf : RightInverse f g) (hh : RightInverse h i) : RightInverse (h ∘ f) (g ∘ i)
Full source
theorem RightInverse.comp {f : α → β} {g : β → α} {h : β → γ} {i : γ → β} (hf : RightInverse f g)
    (hh : RightInverse h i) : RightInverse (h ∘ f) (g ∘ i) :=
  LeftInverse.comp hh hf
Composition Preserves Right Inverses
Informal description
Given functions $f : \alpha \to \beta$, $g : \beta \to \alpha$, $h : \beta \to \gamma$, and $i : \gamma \to \beta$, if $f$ is a right inverse of $g$ and $h$ is a right inverse of $i$, then the composition $h \circ f$ is a right inverse of the composition $g \circ i$.
Function.LeftInverse.rightInverse theorem
{f : α → β} {g : β → α} (h : LeftInverse g f) : RightInverse f g
Full source
theorem LeftInverse.rightInverse {f : α → β} {g : β → α} (h : LeftInverse g f) : RightInverse f g :=
  h
Left Inverse Implies Right Inverse
Informal description
Given functions $f : \alpha \to \beta$ and $g : \beta \to \alpha$, if $g$ is a left inverse of $f$ (i.e., $g(f(x)) = x$ for all $x \in \alpha$), then $f$ is a right inverse of $g$ (i.e., $f(g(y)) = y$ for all $y \in \beta$).
Function.RightInverse.leftInverse theorem
{f : α → β} {g : β → α} (h : RightInverse g f) : LeftInverse f g
Full source
theorem RightInverse.leftInverse {f : α → β} {g : β → α} (h : RightInverse g f) : LeftInverse f g :=
  h
Right Inverse Implies Left Inverse
Informal description
Given functions $f : \alpha \to \beta$ and $g : \beta \to \alpha$, if $g$ is a right inverse of $f$ (i.e., $g(f(y)) = y$ for all $y \in \beta$), then $f$ is a left inverse of $g$ (i.e., $f(g(x)) = x$ for all $x \in \alpha$).
Function.LeftInverse.surjective theorem
{f : α → β} {g : β → α} (h : LeftInverse f g) : Surjective f
Full source
theorem LeftInverse.surjective {f : α → β} {g : β → α} (h : LeftInverse f g) : Surjective f :=
  h.rightInverse.surjective
Left Inverse Implies Surjectivity
Informal description
Given functions $f : \alpha \to \beta$ and $g : \beta \to \alpha$, if $f$ is a left inverse of $g$ (i.e., $f(g(y)) = y$ for all $y \in \beta$), then $f$ is surjective.
Function.RightInverse.injective theorem
{f : α → β} {g : β → α} (h : RightInverse f g) : Injective f
Full source
theorem RightInverse.injective {f : α → β} {g : β → α} (h : RightInverse f g) : Injective f :=
  h.leftInverse.injective
Right Inverse Implies Injective Function
Informal description
For any functions $f : \alpha \to \beta$ and $g : \beta \to \alpha$, if $f$ is a right inverse of $g$ (i.e., $f(g(y)) = y$ for all $y \in \beta$), then $f$ is injective.
Function.LeftInverse.rightInverse_of_injective theorem
{f : α → β} {g : β → α} (h : LeftInverse f g) (hf : Injective f) : RightInverse f g
Full source
theorem LeftInverse.rightInverse_of_injective {f : α → β} {g : β → α} (h : LeftInverse f g)
    (hf : Injective f) : RightInverse f g :=
  fun x ↦ hf <| h (f x)
Left Inverse with Injective Function Implies Right Inverse
Informal description
Let $f : \alpha \to \beta$ and $g : \beta \to \alpha$ be functions such that $g$ is a left inverse of $f$ (i.e., $g(f(x)) = x$ for all $x \in \alpha$). If $f$ is injective, then $g$ is also a right inverse of $f$ (i.e., $f(g(y)) = y$ for all $y \in \beta$).
Function.LeftInverse.rightInverse_of_surjective theorem
{f : α → β} {g : β → α} (h : LeftInverse f g) (hg : Surjective g) : RightInverse f g
Full source
theorem LeftInverse.rightInverse_of_surjective {f : α → β} {g : β → α} (h : LeftInverse f g)
    (hg : Surjective g) : RightInverse f g :=
  fun x ↦ let ⟨y, hy⟩ := hg x; hy ▸ congr_arg g (h y)
Left Inverse with Surjective Function Implies Right Inverse
Informal description
Let $f : \alpha \to \beta$ and $g : \beta \to \alpha$ be functions such that $g$ is a left inverse of $f$ (i.e., $g(f(x)) = x$ for all $x \in \alpha$). If $g$ is surjective, then $g$ is also a right inverse of $f$ (i.e., $f(g(y)) = y$ for all $y \in \beta$).
Function.RightInverse.leftInverse_of_surjective theorem
{f : α → β} {g : β → α} : RightInverse f g → Surjective f → LeftInverse f g
Full source
theorem RightInverse.leftInverse_of_surjective {f : α → β} {g : β → α} :
    RightInverse f g → Surjective f → LeftInverse f g :=
  LeftInverse.rightInverse_of_surjective
Right Inverse with Surjective Function Implies Left Inverse
Informal description
For any functions $f : \alpha \to \beta$ and $g : \beta \to \alpha$, if $g$ is a right inverse of $f$ (i.e., $f(g(y)) = y$ for all $y \in \beta$) and $f$ is surjective, then $g$ is also a left inverse of $f$ (i.e., $g(f(x)) = x$ for all $x \in \alpha$).
Function.RightInverse.leftInverse_of_injective theorem
{f : α → β} {g : β → α} : RightInverse f g → Injective g → LeftInverse f g
Full source
theorem RightInverse.leftInverse_of_injective {f : α → β} {g : β → α} :
    RightInverse f g → Injective g → LeftInverse f g :=
  LeftInverse.rightInverse_of_injective
Right Inverse with Injective Function Implies Left Inverse
Informal description
For any functions $f : \alpha \to \beta$ and $g : \beta \to \alpha$, if $g$ is a right inverse of $f$ (i.e., $f(g(y)) = y$ for all $y \in \beta$) and $g$ is injective, then $g$ is also a left inverse of $f$ (i.e., $g(f(x)) = x$ for all $x \in \alpha$).
Function.partialInv definition
{α β} (f : α → β) (b : β) : Option α
Full source
/-- We can use choice to construct explicitly a partial inverse for
  a given injective function `f`. -/
noncomputable def partialInv {α β} (f : α → β) (b : β) : Option α :=
  open scoped Classical in
  if h : ∃ a, f a = b then some (Classical.choose h) else none
Partial inverse of an injective function
Informal description
Given an injective function \( f : \alpha \to \beta \), the partial inverse function \( \text{partialInv} \, f : \beta \to \text{Option} \, \alpha \) is defined such that for any \( b \in \beta \), if there exists an \( a \in \alpha \) with \( f(a) = b \), then \( \text{partialInv} \, f \, b \) returns \( \text{some} \, a \) where \( a \) is chosen (using the axiom of choice) from the set of such preimages; otherwise, it returns \( \text{none} \).
Function.partialInv_of_injective theorem
{α β} {f : α → β} (I : Injective f) : IsPartialInv f (partialInv f)
Full source
theorem partialInv_of_injective {α β} {f : α → β} (I : Injective f) : IsPartialInv f (partialInv f)
  | a, b =>
  ⟨fun h =>
    open scoped Classical in
    have hpi : partialInv f b = if h : ∃ a, f a = b then some (Classical.choose h) else none :=
      rfl
    if h' : ∃ a, f a = b
    then by rw [hpi, dif_pos h'] at h
            injection h with h
            subst h
            apply Classical.choose_spec h'
    else by rw [hpi, dif_neg h'] at h; contradiction,
  fun e => e ▸ have h : ∃ a', f a' = f a := ⟨_, rfl⟩
              (dif_pos h).trans (congr_arg _ (I <| Classical.choose_spec h))⟩
Partial Inverse Property for Injective Functions
Informal description
For any injective function $f : \alpha \to \beta$, the partial inverse function $\text{partialInv}\, f$ is indeed a partial inverse of $f$, meaning that for all $x \in \alpha$ and $y \in \beta$, $\text{partialInv}\, f\, y = \text{some}\, x$ if and only if $f(x) = y$.
Function.partialInv_left theorem
{α β} {f : α → β} (I : Injective f) : ∀ x, partialInv f (f x) = some x
Full source
theorem partialInv_left {α β} {f : α → β} (I : Injective f) : ∀ x, partialInv f (f x) = some x :=
  isPartialInv_left (partialInv_of_injective I)
Partial Inverse Property for Injective Functions: \( \text{partialInv}\, f (f(x)) = \text{some}\, x \)
Informal description
For any injective function \( f : \alpha \to \beta \), the partial inverse function \( \text{partialInv}\, f \) satisfies \( \text{partialInv}\, f (f(x)) = \text{some}\, x \) for all \( x \in \alpha \).
Function.invFun definition
{α : Sort u} {β} [Nonempty α] (f : α → β) : β → α
Full source
/-- The inverse of a function (which is a left inverse if `f` is injective
  and a right inverse if `f` is surjective). -/
-- Explicit Sort so that `α` isn't inferred to be Prop via `exists_prop_decidable`
noncomputable def invFun {α : Sort u} {β} [Nonempty α] (f : α → β) : β → α :=
  open scoped Classical in
  fun y ↦ if h : (∃ x, f x = y) then h.choose else Classical.arbitrary α
Inverse function (left inverse for injective functions, right inverse for surjective functions)
Informal description
Given a nonempty type $\alpha$ and a function $f : \alpha \to \beta$, the function $\text{invFun}\, f : \beta \to \alpha$ is defined as follows: for any $y \in \beta$, if there exists an $x \in \alpha$ such that $f(x) = y$, then $\text{invFun}\, f\, y$ returns such an $x$; otherwise, it returns an arbitrary element of $\alpha$. This serves as a left inverse when $f$ is injective and a right inverse when $f$ is surjective.
Function.invFun_eq theorem
(h : ∃ a, f a = b) : f (invFun f b) = b
Full source
theorem invFun_eq (h : ∃ a, f a = b) : f (invFun f b) = b := by
  simp only [invFun, dif_pos h, h.choose_spec]
Inverse Function Property: $f(\text{invFun}\, f\, b) = b$ when $b$ is in the image of $f$
Informal description
For any function $f : \alpha \to \beta$ and element $b \in \beta$, if there exists an $a \in \alpha$ such that $f(a) = b$, then $f$ applied to the inverse function $\text{invFun}\, f$ evaluated at $b$ equals $b$, i.e., $f(\text{invFun}\, f\, b) = b$.
Function.apply_invFun_apply theorem
{α β : Type*} {f : α → β} {a : α} : f (@invFun _ _ ⟨a⟩ f (f a)) = f a
Full source
theorem apply_invFun_apply {α β : Type*} {f : α → β} {a : α} :
    f (@invFun _ _ ⟨a⟩ f (f a)) = f a :=
  @invFun_eq _ _ ⟨a⟩ _ _ ⟨_, rfl⟩
Inverse Function Property at $f(a)$: $f(\text{invFun}\, f\, (f(a))) = f(a)$
Informal description
For any types $\alpha$ and $\beta$, and any function $f : \alpha \to \beta$, if $a \in \alpha$ is a witness that $\alpha$ is nonempty, then applying $f$ to the inverse function $\text{invFun}\, f$ evaluated at $f(a)$ yields $f(a)$, i.e., $f(\text{invFun}\, f\, (f(a))) = f(a)$.
Function.invFun_neg theorem
(h : ¬∃ a, f a = b) : invFun f b = Classical.choice ‹_›
Full source
theorem invFun_neg (h : ¬∃ a, f a = b) : invFun f b = Classical.choice ‹_› :=
  dif_neg h
Behavior of Inverse Function When No Preimage Exists
Informal description
For a function $f : \alpha \to \beta$ and an element $b \in \beta$, if there does not exist any $a \in \alpha$ such that $f(a) = b$, then the inverse function $\text{invFun}\, f\, b$ returns an arbitrary element of $\alpha$ (chosen via the axiom of choice).
Function.invFun_eq_of_injective_of_rightInverse theorem
{g : β → α} (hf : Injective f) (hg : RightInverse g f) : invFun f = g
Full source
theorem invFun_eq_of_injective_of_rightInverse {g : β → α} (hf : Injective f)
    (hg : RightInverse g f) : invFun f = g :=
  funext fun b ↦
    hf
      (by
        rw [hg b]
        exact invFun_eq ⟨g b, hg b⟩)
Inverse Function Equals Right Inverse for Injective Functions
Informal description
For any injective function $f : \alpha \to \beta$ and any right inverse $g : \beta \to \alpha$ of $f$, the inverse function $\text{invFun}\, f$ is equal to $g$.
Function.rightInverse_invFun theorem
(hf : Surjective f) : RightInverse (invFun f) f
Full source
theorem rightInverse_invFun (hf : Surjective f) : RightInverse (invFun f) f :=
  fun b ↦ invFun_eq <| hf b
Inverse Function as Right Inverse for Surjective Functions
Informal description
For any surjective function $f : \alpha \to \beta$, the inverse function $\text{invFun}\, f$ is a right inverse of $f$, meaning that for every $y \in \beta$, we have $f((\text{invFun}\, f)(y)) = y$.
Function.leftInverse_invFun theorem
(hf : Injective f) : LeftInverse (invFun f) f
Full source
theorem leftInverse_invFun (hf : Injective f) : LeftInverse (invFun f) f :=
  fun b ↦ hf <| invFun_eq ⟨b, rfl⟩
Inverse Function as Left Inverse for Injective Functions
Informal description
For any injective function $f : \alpha \to \beta$, the inverse function $\text{invFun}\, f$ is a left inverse of $f$, meaning that for every $x \in \alpha$, we have $(\text{invFun}\, f)(f(x)) = x$.
Function.invFun_surjective theorem
(hf : Injective f) : Surjective (invFun f)
Full source
theorem invFun_surjective (hf : Injective f) : Surjective (invFun f) :=
  (leftInverse_invFun hf).surjective
Surjectivity of the Inverse Function for Injective Functions
Informal description
For any injective function $f : \alpha \to \beta$, the inverse function $\text{invFun}\, f : \beta \to \alpha$ is surjective.
Function.invFun_comp theorem
(hf : Injective f) : invFun f ∘ f = id
Full source
theorem invFun_comp (hf : Injective f) : invFuninvFun f ∘ f = id :=
  funext <| leftInverse_invFun hf
Inverse Function Composition Identity for Injective Functions
Informal description
For any injective function $f : \alpha \to \beta$, the composition of the inverse function $\text{invFun}\, f$ with $f$ is equal to the identity function on $\alpha$, i.e., $(\text{invFun}\, f) \circ f = \text{id}_\alpha$.
Function.Injective.hasLeftInverse theorem
(hf : Injective f) : HasLeftInverse f
Full source
theorem Injective.hasLeftInverse (hf : Injective f) : HasLeftInverse f :=
  ⟨invFun f, leftInverse_invFun hf⟩
Injective Functions Have Left Inverses
Informal description
If a function $f : \alpha \to \beta$ is injective, then it has a left inverse, i.e., there exists a function $g : \beta \to \alpha$ such that $g \circ f = \text{id}_\alpha$.
Function.surjInv definition
{f : α → β} (h : Surjective f) (b : β) : α
Full source
/-- The inverse of a surjective function. (Unlike `invFun`, this does not require
  `α` to be inhabited.) -/
noncomputable def surjInv {f : α → β} (h : Surjective f) (b : β) : α :=
  Classical.choose (h b)
Right inverse of a surjective function
Informal description
Given a surjective function \( f : \alpha \to \beta \) and an element \( b \in \beta \), the function returns an element \( a \in \alpha \) such that \( f(a) = b \). This is constructed using the axiom of choice.
Function.surjInv_eq theorem
(h : Surjective f) (b) : f (surjInv h b) = b
Full source
theorem surjInv_eq (h : Surjective f) (b) : f (surjInv h b) = b :=
  Classical.choose_spec (h b)
Right Inverse Property of Surjective Functions
Informal description
For any surjective function $f : \alpha \to \beta$ and any element $b \in \beta$, we have $f(\text{surjInv}_f(b)) = b$, where $\text{surjInv}_f$ is a right inverse of $f$.
Function.rightInverse_surjInv theorem
(hf : Surjective f) : RightInverse (surjInv hf) f
Full source
theorem rightInverse_surjInv (hf : Surjective f) : RightInverse (surjInv hf) f :=
  surjInv_eq hf
Right Inverse Property of the Surjective Inverse Function
Informal description
For any surjective function $f : \alpha \to \beta$, the function $\text{surjInv}_f$ is a right inverse of $f$, meaning that $f \circ \text{surjInv}_f = \text{id}_\beta$.
Function.leftInverse_surjInv theorem
(hf : Bijective f) : LeftInverse (surjInv hf.2) f
Full source
theorem leftInverse_surjInv (hf : Bijective f) : LeftInverse (surjInv hf.2) f :=
  rightInverse_of_injective_of_leftInverse hf.1 (rightInverse_surjInv hf.2)
Left Inverse Property of the Surjective Inverse for Bijective Functions
Informal description
For any bijective function $f : \alpha \to \beta$, the function $\text{surjInv}_f$ (constructed from the surjectivity of $f$) is a left inverse of $f$, meaning that $\text{surjInv}_f \circ f = \text{id}_\alpha$.
Function.surjective_iff_hasRightInverse theorem
: Surjective f ↔ HasRightInverse f
Full source
theorem surjective_iff_hasRightInverse : SurjectiveSurjective f ↔ HasRightInverse f :=
  ⟨Surjective.hasRightInverse, HasRightInverse.surjective⟩
Surjectivity Equivalent to Existence of Right Inverse
Informal description
A function $f : \alpha \to \beta$ is surjective if and only if it has a right inverse, i.e., there exists a function $g : \beta \to \alpha$ such that $f \circ g = \text{id}_\beta$.
Function.bijective_iff_has_inverse theorem
: Bijective f ↔ ∃ g, LeftInverse g f ∧ RightInverse g f
Full source
theorem bijective_iff_has_inverse : BijectiveBijective f ↔ ∃ g, LeftInverse g f ∧ RightInverse g f :=
  ⟨fun hf ↦ ⟨_, leftInverse_surjInv hf, rightInverse_surjInv hf.2⟩, fun ⟨_, gl, gr⟩ ↦
    ⟨gl.injective, gr.surjective⟩⟩
Characterization of Bijective Functions via Existence of Two-Sided Inverse
Informal description
A function $f : \alpha \to \beta$ is bijective if and only if there exists a function $g : \beta \to \alpha$ such that $g$ is both a left inverse and a right inverse of $f$, i.e., $g \circ f = \text{id}_\alpha$ and $f \circ g = \text{id}_\beta$.
Function.injective_surjInv theorem
(h : Surjective f) : Injective (surjInv h)
Full source
theorem injective_surjInv (h : Surjective f) : Injective (surjInv h) :=
  (rightInverse_surjInv h).injective
Injectivity of the Right Inverse of a Surjective Function
Informal description
For any surjective function $f : \alpha \to \beta$, the right inverse function $\text{surjInv}_f$ is injective.
Function.surjective_to_subsingleton theorem
[na : Nonempty α] [Subsingleton β] (f : α → β) : Surjective f
Full source
theorem surjective_to_subsingleton [na : Nonempty α] [Subsingleton β] (f : α → β) :
    Surjective f :=
  fun _ ↦ let ⟨a⟩ := na; ⟨a, Subsingleton.elim _ _⟩
Surjectivity of Functions to Subsingletons
Informal description
Let $α$ be a nonempty type and $β$ be a subsingleton type (i.e., any two elements of $β$ are equal). Then any function $f : α → β$ is surjective.
Function.Surjective.piMap theorem
{ι : Sort*} {α β : ι → Sort*} {f : ∀ i, α i → β i} (hf : ∀ i, Surjective (f i)) : Surjective (Pi.map f)
Full source
theorem Surjective.piMap {ι : Sort*} {α β : ι → Sort*} {f : ∀ i, α i → β i}
    (hf : ∀ i, Surjective (f i)) : Surjective (Pi.map f) := fun g ↦
  ⟨fun i ↦ surjInv (hf i) (g i), funext fun _ ↦ rightInverse_surjInv _ _⟩
Surjectivity of Component-wise Mapping for Dependent Functions
Informal description
Let $\iota$ be a type, and for each $i \in \iota$, let $\alpha_i$ and $\beta_i$ be types with a function $f_i : \alpha_i \to \beta_i$. If each $f_i$ is surjective, then the component-wise mapping function $\text{Pi.map} \, f : (\forall i, \alpha_i) \to (\forall i, \beta_i)$ is also surjective.
Function.Surjective.comp_left theorem
{g : β → γ} (hg : Surjective g) : Surjective (g ∘ · : (α → β) → α → γ)
Full source
/-- Composition by a surjective function on the left is itself surjective. -/
theorem Surjective.comp_left {g : β → γ} (hg : Surjective g) :
    Surjective (g ∘ · : (α → β) → α → γ) :=
  .piMap fun _ ↦ hg
Surjectivity of Left Composition with a Surjective Function
Informal description
Let $g : \beta \to \gamma$ be a surjective function. Then the left composition with $g$, which maps any function $f : \alpha \to \beta$ to $g \circ f : \alpha \to \gamma$, is also surjective.
Function.surjective_comp_left_iff theorem
[Nonempty α] {g : β → γ} : Surjective (g ∘ · : (α → β) → α → γ) ↔ Surjective g
Full source
theorem surjective_comp_left_iff [Nonempty α] {g : β → γ} :
    SurjectiveSurjective (g ∘ · : (α → β) → α → γ) ↔ Surjective g := by
  refine ⟨fun h c ↦ Nonempty.elim ‹_› fun a ↦ ?_, (·.comp_left)⟩
  have ⟨f, hf⟩ := h fun _ ↦ c
  exact ⟨f a, congr_fun hf _⟩
Surjectivity of Left Composition is Equivalent to Surjectivity of the Function
Informal description
Let $\alpha$ be a nonempty type. For a function $g : \beta \to \gamma$, the left composition with $g$ (i.e., the map $f \mapsto g \circ f$ from $(\alpha \to \beta)$ to $(\alpha \to \gamma)$) is surjective if and only if $g$ is surjective.
Function.Bijective.piMap theorem
{ι : Sort*} {α β : ι → Sort*} {f : ∀ i, α i → β i} (hf : ∀ i, Bijective (f i)) : Bijective (Pi.map f)
Full source
theorem Bijective.piMap {ι : Sort*} {α β : ι → Sort*} {f : ∀ i, α i → β i}
    (hf : ∀ i, Bijective (f i)) : Bijective (Pi.map f) :=
  ⟨.piMap fun i ↦ (hf i).1, .piMap fun i ↦ (hf i).2⟩
Bijectivity of Component-wise Mapping for Dependent Functions
Informal description
Let $\iota$ be a type, and for each $i \in \iota$, let $\alpha_i$ and $\beta_i$ be types with a function $f_i : \alpha_i \to \beta_i$. If each $f_i$ is bijective, then the component-wise mapping function $\text{Pi.map} \, f : (\forall i, \alpha_i) \to (\forall i, \beta_i)$ is also bijective.
Function.Bijective.comp_left theorem
{g : β → γ} (hg : Bijective g) : Bijective (g ∘ · : (α → β) → α → γ)
Full source
/-- Composition by a bijective function on the left is itself bijective. -/
theorem Bijective.comp_left {g : β → γ} (hg : Bijective g) :
    Bijective (g ∘ · : (α → β) → α → γ) :=
  ⟨hg.injective.comp_left, hg.surjective.comp_left⟩
Bijectivity of Left Composition with a Bijective Function
Informal description
Let $g : \beta \to \gamma$ be a bijective function. Then the left composition with $g$, i.e., the function $(g \circ \cdot) : (\alpha \to \beta) \to (\alpha \to \gamma)$ defined by $(g \circ \cdot)(f) = g \circ f$, is also bijective.
Function.update definition
(f : ∀ a, β a) (a' : α) (v : β a') (a : α) : β a
Full source
/-- Replacing the value of a function at a given point by a given value. -/
def update (f : ∀ a, β a) (a' : α) (v : β a') (a : α) : β a :=
  if h : a = a' then Eq.ndrec v h.symm else f a
Function update at a point
Informal description
The function `Function.update` takes a function \( f : \forall a, \beta a \), a point \( a' : \alpha \), and a value \( v : \beta a' \), and returns a new function that is equal to \( f \) everywhere except at \( a' \), where it takes the value \( v \). Formally, for any \( a : \alpha \), the updated function is defined as: \[ \text{update } f \, a' \, v \, a = \begin{cases} v & \text{if } a = a', \\ f a & \text{otherwise.} \end{cases} \]
Function.update_self theorem
(a : α) (v : β a) (f : ∀ a, β a) : update f a v a = v
Full source
@[simp]
theorem update_self (a : α) (v : β a) (f : ∀ a, β a) : update f a v a = v :=
  dif_pos rfl
Self-Update Property of Function Update
Informal description
For any function $f : \forall a, \beta a$, any point $a : \alpha$, and any value $v : \beta a$, updating $f$ at $a$ with $v$ yields a new function that evaluates to $v$ at $a$, i.e., $\text{update } f \, a \, v \, a = v$.
Function.update_of_ne theorem
{a a' : α} (h : a ≠ a') (v : β a') (f : ∀ a, β a) : update f a' v a = f a
Full source
@[simp]
theorem update_of_ne {a a' : α} (h : a ≠ a') (v : β a') (f : ∀ a, β a) : update f a' v a = f a :=
  dif_neg h
Function Update Preserves Value at Distinct Points
Informal description
For any distinct elements $a$ and $a'$ of type $\alpha$ (i.e., $a \neq a'$), any value $v$ of type $\beta a'$, and any function $f : \forall a, \beta a$, the updated function satisfies $\text{update } f \, a' \, v \, a = f a$.
Function.update_apply theorem
{β : Sort*} (f : α → β) (a' : α) (b : β) (a : α) : update f a' b a = if a = a' then b else f a
Full source
/-- On non-dependent functions, `Function.update` can be expressed as an `ite` -/
theorem update_apply {β : Sort*} (f : α → β) (a' : α) (b : β) (a : α) :
    update f a' b a = if a = a' then b else f a := by
  rcases Decidable.eq_or_ne a a' with rfl | hne <;> simp [*]
Function Update as Conditional Expression
Informal description
For any function $f : \alpha \to \beta$, point $a' \in \alpha$, value $b \in \beta$, and input $a \in \alpha$, the updated function satisfies: \[ \text{update } f \, a' \, b \, a = \begin{cases} b & \text{if } a = a', \\ f a & \text{otherwise.} \end{cases} \]
Function.update_eq_const_of_subsingleton theorem
[Subsingleton α] (a : α) (v : α') (f : α → α') : update f a v = const α v
Full source
@[nontriviality]
theorem update_eq_const_of_subsingleton [Subsingleton α] (a : α) (v : α') (f : α → α') :
    update f a v = const α v :=
  funext fun a' ↦ Subsingleton.elim a a' ▸ update_self ..
Function Update Yields Constant Function on Subsingleton Domain
Informal description
For any type $\alpha$ with at most one element (i.e., a subsingleton), any element $a \in \alpha$, any value $v \in \alpha'$, and any function $f : \alpha \to \alpha'$, updating $f$ at $a$ with $v$ results in the constant function that maps every element of $\alpha$ to $v$, i.e., $\text{update } f \, a \, v = \lambda \_, v$.
Function.surjective_eval theorem
{α : Sort u} {β : α → Sort v} [h : ∀ a, Nonempty (β a)] (a : α) : Surjective (eval a : (∀ a, β a) → β a)
Full source
theorem surjective_eval {α : Sort u} {β : α → Sort v} [h : ∀ a, Nonempty (β a)] (a : α) :
    Surjective (eval a : (∀ a, β a) → β a) := fun b ↦
  ⟨@update _ _ (Classical.decEq α) (fun a ↦ (h a).some) a b,
   @update_self _ _ (Classical.decEq α) _ _ _⟩
Surjectivity of Dependent Function Evaluation
Informal description
For any type family $\beta : \alpha \to \text{Sort}\, v$ where each $\beta a$ is nonempty, the evaluation function $\text{eval}\, a : (\forall a, \beta a) \to \beta a$ is surjective. That is, for every $b \in \beta a$, there exists a function $f : \forall a, \beta a$ such that $f a = b$.
Function.update_injective theorem
(f : ∀ a, β a) (a' : α) : Injective (update f a')
Full source
theorem update_injective (f : ∀ a, β a) (a' : α) : Injective (update f a') := fun v v' h ↦ by
  have := congr_fun h a'
  rwa [update_self, update_self] at this
Injectivity of Function Update at a Point
Informal description
For any dependent function \( f : \forall a, \beta a \) and any point \( a' : \alpha \), the function update operation \( \text{update } f \, a' \) is injective. That is, for any two values \( v_1, v_2 : \beta a' \), if \( \text{update } f \, a' \, v_1 = \text{update } f \, a' \, v_2 \), then \( v_1 = v_2 \).
Function.forall_update_iff theorem
(f : ∀ a, β a) {a : α} {b : β a} (p : ∀ a, β a → Prop) : (∀ x, p x (update f a b x)) ↔ p a b ∧ ∀ x, x ≠ a → p x (f x)
Full source
lemma forall_update_iff (f : ∀a, β a) {a : α} {b : β a} (p : ∀a, β a → Prop) :
    (∀ x, p x (update f a b x)) ↔ p a b ∧ ∀ x, x ≠ a → p x (f x) := by
  rw [← and_forall_ne a, update_self]
  simp +contextual
Universal Quantifier Characterization of Function Update
Informal description
For any dependent function $f : \forall a, \beta a$, any point $a : \alpha$, any value $b : \beta a$, and any predicate $p : \forall a, \beta a \to \text{Prop}$, the following equivalence holds: \[ (\forall x, p\,x\,(\text{update}\,f\,a\,b\,x)) \leftrightarrow p\,a\,b \land (\forall x \neq a, p\,x\,(f\,x)). \]
Function.exists_update_iff theorem
(f : ∀ a, β a) {a : α} {b : β a} (p : ∀ a, β a → Prop) : (∃ x, p x (update f a b x)) ↔ p a b ∨ ∃ x ≠ a, p x (f x)
Full source
theorem exists_update_iff (f : ∀ a, β a) {a : α} {b : β a} (p : ∀ a, β a → Prop) :
    (∃ x, p x (update f a b x)) ↔ p a b ∨ ∃ x ≠ a, p x (f x) := by
  rw [← not_forall_not, forall_update_iff f fun a b ↦ ¬p a b]
  simp [-not_and, not_and_or]
Existential Quantifier Characterization of Function Update
Informal description
For any dependent function $f : \forall a, \beta a$, any point $a : \alpha$, any value $b : \beta a$, and any predicate $p : \forall a, \beta a \to \text{Prop}$, the following equivalence holds: \[ (\exists x, p\,x\,(\text{update}\,f\,a\,b\,x)) \leftrightarrow p\,a\,b \lor (\exists x \neq a, p\,x\,(f\,x)). \]
Function.update_eq_iff theorem
{a : α} {b : β a} {f g : ∀ a, β a} : update f a b = g ↔ b = g a ∧ ∀ x ≠ a, f x = g x
Full source
theorem update_eq_iff {a : α} {b : β a} {f g : ∀ a, β a} :
    updateupdate f a b = g ↔ b = g a ∧ ∀ x ≠ a, f x = g x :=
  funext_iff.trans <| forall_update_iff _ fun x y ↦ y = g x
Equality Characterization of Function Update
Informal description
For a function $f : \forall a, \beta a$, a point $a : \alpha$, a value $b : \beta a$, and another function $g : \forall a, \beta a$, the updated function $\text{update } f \, a \, b$ is equal to $g$ if and only if $b = g(a)$ and for all $x \neq a$, $f(x) = g(x)$. In other words: \[ \text{update } f \, a \, b = g \leftrightarrow b = g(a) \land (\forall x \neq a, f(x) = g(x)). \]
Function.update_eq_self_iff theorem
: update f a b = f ↔ b = f a
Full source
@[simp] lemma update_eq_self_iff : updateupdate f a b = f ↔ b = f a := by simp [update_eq_iff]
Equality of Function Update to Original Function if and only if New Value Equals Original Value
Informal description
For a function $f : \forall a, \beta a$, a point $a : \alpha$, and a value $b : \beta a$, the updated function $\text{update } f \, a \, b$ is equal to $f$ if and only if $b$ equals $f(a)$. In other words: \[ \text{update } f \, a \, b = f \leftrightarrow b = f(a). \]
Function.ne_update_self_iff theorem
: f ≠ update f a b ↔ f a ≠ b
Full source
lemma ne_update_self_iff : f ≠ update f a bf ≠ update f a b ↔ f a ≠ b := eq_update_self_iff.not
Inequality of Function and Its Update at a Point
Informal description
For a function $f : \forall a, \beta a$, a point $a : \alpha$, and a value $b : \beta a$, the function $f$ is not equal to its update at $a$ with value $b$ if and only if $f(a) \neq b$. In other words: \[ f \neq \text{update } f \, a \, b \leftrightarrow f(a) \neq b \]
Function.update_ne_self_iff theorem
: update f a b ≠ f ↔ b ≠ f a
Full source
lemma update_ne_self_iff : updateupdate f a b ≠ fupdate f a b ≠ f ↔ b ≠ f a := update_eq_self_iff.not
Inequality of Function Update and Original Function if and only if New Value Differs from Original Value
Informal description
For a function $f : \forall a, \beta a$, a point $a : \alpha$, and a value $b : \beta a$, the updated function $\text{update } f \, a \, b$ is not equal to $f$ if and only if $b$ is not equal to $f(a)$. In other words: \[ \text{update } f \, a \, b \neq f \leftrightarrow b \neq f(a). \]
Function.update_eq_self theorem
(a : α) (f : ∀ a, β a) : update f a (f a) = f
Full source
@[simp]
theorem update_eq_self (a : α) (f : ∀ a, β a) : update f a (f a) = f :=
  update_eq_iff.2 ⟨rfl, fun _ _ ↦ rfl⟩
Function Update with Original Value Preserves Identity
Informal description
For any function $f : \forall a, \beta a$ and any point $a : \alpha$, updating $f$ at $a$ with its own value $f(a)$ leaves the function unchanged, i.e., $\text{update } f \, a \, (f a) = f$.
Function.update_comp_eq_of_forall_ne' theorem
{α'} (g : ∀ a, β a) {f : α' → α} {i : α} (a : β i) (h : ∀ x, f x ≠ i) : (fun j ↦ (update g i a) (f j)) = fun j ↦ g (f j)
Full source
theorem update_comp_eq_of_forall_ne' {α'} (g : ∀ a, β a) {f : α' → α} {i : α} (a : β i)
    (h : ∀ x, f x ≠ i) : (fun j ↦ (update g i a) (f j)) = fun j ↦ g (f j) :=
  funext fun _ ↦ update_of_ne (h _) _ _
Composition with Update Preserves Function When Disjoint from Update Point
Informal description
Let $g : \forall a, \beta a$ be a function, $f : \alpha' \to \alpha$ a map, $i \in \alpha$, and $a \in \beta i$. If for all $x \in \alpha'$, $f(x) \neq i$, then the composition of the updated function $\text{update } g \, i \, a$ with $f$ equals the composition of $g$ with $f$. That is, \[ (\lambda j, (\text{update } g \, i \, a)(f j)) = \lambda j, g (f j). \]
Function.update_comp_eq_of_forall_ne theorem
{α β : Sort*} (g : α' → β) {f : α → α'} {i : α'} (a : β) (h : ∀ x, f x ≠ i) : update g i a ∘ f = g ∘ f
Full source
/-- Non-dependent version of `Function.update_comp_eq_of_forall_ne'` -/
theorem update_comp_eq_of_forall_ne {α β : Sort*} (g : α' → β) {f : α → α'} {i : α'} (a : β)
    (h : ∀ x, f x ≠ i) : updateupdate g i a ∘ f = g ∘ f :=
  update_comp_eq_of_forall_ne' g a h
Function Update Composition Equality When Disjoint from Update Point
Informal description
Let $g : \alpha' \to \beta$ be a function, $f : \alpha \to \alpha'$ a map, $i \in \alpha'$, and $a \in \beta$. If for all $x \in \alpha$, $f(x) \neq i$, then the composition of the updated function $\text{update } g \, i \, a$ with $f$ equals the composition of $g$ with $f$. That is, \[ \text{update } g \, i \, a \circ f = g \circ f. \]
Function.update_comp_eq_of_injective' theorem
(g : ∀ a, β a) {f : α' → α} (hf : Function.Injective f) (i : α') (a : β (f i)) : (fun j ↦ update g (f i) a (f j)) = update (fun i ↦ g (f i)) i a
Full source
theorem update_comp_eq_of_injective' (g : ∀ a, β a) {f : α' → α} (hf : Function.Injective f)
    (i : α') (a : β (f i)) : (fun j ↦ update g (f i) a (f j)) = update (fun i ↦ g (f i)) i a :=
  eq_update_iff.2 ⟨update_self .., fun _ hj ↦ update_of_ne (hf.ne hj) _ _⟩
Equality of Function Update Composition with Injective Function
Informal description
Let $g : \forall a, \beta a$ be a function, $f : \alpha' \to \alpha$ an injective function, $i \in \alpha'$, and $a \in \beta (f i)$. Then the function defined by $\lambda j, \text{update } g \, (f i) \, a \, (f j)$ is equal to the function obtained by updating $\lambda i, g (f i)$ at $i$ with $a$, i.e., \[ (\lambda j, \text{update } g \, (f i) \, a \, (f j)) = \text{update } (\lambda i, g (f i)) \, i \, a. \]
Function.update_apply_of_injective theorem
(g : ∀ a, β a) {f : α' → α} (hf : Function.Injective f) (i : α') (a : β (f i)) (j : α') : update g (f i) a (f j) = update (fun i ↦ g (f i)) i a j
Full source
theorem update_apply_of_injective
    (g : ∀ a, β a) {f : α' → α} (hf : Function.Injective f)
    (i : α') (a : β (f i)) (j : α') :
    update g (f i) a (f j) = update (fun i ↦ g (f i)) i a j :=
  congr_fun (update_comp_eq_of_injective' g hf i a) j
Function Update Equality for Injective Functions
Informal description
Let $g : \forall a, \beta a$ be a function, $f : \alpha' \to \alpha$ an injective function, $i \in \alpha'$, and $a \in \beta (f i)$. Then for any $j \in \alpha'$, the updated function $\text{update } g \, (f i) \, a$ evaluated at $f j$ equals the updated function $\text{update } (\lambda i, g (f i)) \, i \, a$ evaluated at $j$, i.e., \[ \text{update } g \, (f i) \, a \, (f j) = \text{update } (\lambda i, g (f i)) \, i \, a \, j. \]
Function.update_comp_eq_of_injective theorem
{β : Sort*} (g : α' → β) {f : α → α'} (hf : Function.Injective f) (i : α) (a : β) : Function.update g (f i) a ∘ f = Function.update (g ∘ f) i a
Full source
/-- Non-dependent version of `Function.update_comp_eq_of_injective'` -/
theorem update_comp_eq_of_injective {β : Sort*} (g : α' → β) {f : α → α'}
    (hf : Function.Injective f) (i : α) (a : β) :
    Function.updateFunction.update g (f i) a ∘ f = Function.update (g ∘ f) i a :=
  update_comp_eq_of_injective' g hf i a
Function Update Commutes with Composition for Injective Functions
Informal description
Let $g : \alpha' \to \beta$ be a function, $f : \alpha \to \alpha'$ an injective function, $i \in \alpha$, and $a \in \beta$. Then the composition of the updated function $\text{update } g \, (f i) \, a$ with $f$ is equal to the function obtained by updating $g \circ f$ at $i$ with $a$, i.e., \[ (\text{update } g \, (f i) \, a) \circ f = \text{update } (g \circ f) \, i \, a. \]
Function.rec_update theorem
{ι κ : Sort*} {α : κ → Sort*} [DecidableEq ι] [DecidableEq κ] {ctor : ι → κ} (hctor : Function.Injective ctor) (recursor : ((i : ι) → α (ctor i)) → ((i : κ) → α i)) (h : ∀ f i, recursor f (ctor i) = f i) (h2 : ∀ f₁ f₂ k, (∀ i, ctor i ≠ k) → recursor f₁ k = recursor f₂ k) (f : (i : ι) → α (ctor i)) (i : ι) (x : α (ctor i)) : recursor (update f i x) = update (recursor f) (ctor i) x
Full source
/-- Recursors can be pushed inside `Function.update`.

The `ctor` argument should be a one-argument constructor like `Sum.inl`,
and `recursor` should be an inductive recursor partially applied in all but that constructor,
such as `(Sum.rec · g)`.

In future, we should build some automation to generate applications like `Option.rec_update` for all
inductive types. -/
lemma rec_update {ι κ : Sort*} {α : κ → Sort*} [DecidableEq ι] [DecidableEq κ]
    {ctor : ι → κ} (hctor : Function.Injective ctor)
    (recursor : ((i : ι) → α (ctor i)) → ((i : κ) → α i))
    (h : ∀ f i, recursor f (ctor i) = f i)
    (h2 : ∀ f₁ f₂ k, (∀ i, ctor i ≠ k) → recursor f₁ k = recursor f₂ k)
    (f : (i : ι) → α (ctor i)) (i : ι) (x : α (ctor i)) :
    recursor (update f i x) = update (recursor f) (ctor i) x := by
  ext k
  by_cases h : ∃ i, ctor i = k
  · obtain ⟨i', rfl⟩ := h
    obtain rfl | hi := eq_or_ne i' i
    · simp [h]
    · have hk := hctor.ne hi
      simp [h, hi, hk, Function.update_of_ne]
  · rw [not_exists] at h
    rw [h2 _ f _ h]
    rw [Function.update_of_ne (Ne.symm <| h i)]
Recursor Commutes with Function Update for Injective Constructors
Informal description
Let $\iota$ and $\kappa$ be types with decidable equality, and let $\alpha : \kappa \to \text{Type}$ be a type family. Given an injective constructor function $\text{ctor} : \iota \to \kappa$ and a recursor function $\text{recursor} : \big((i : ι) \to \alpha (\text{ctor } i)\big) \to \big((i : κ) \to \alpha i\big)$ satisfying: 1. For any $f : (i : ι) \to \alpha (\text{ctor } i)$ and any $i : ι$, we have $\text{recursor } f (\text{ctor } i) = f i$. 2. For any $f_1, f_2 : (i : ι) \to \alpha (\text{ctor } i)$ and any $k : κ$ such that $\text{ctor } i \neq k$ for all $i : ι$, we have $\text{recursor } f_1 k = \text{recursor } f_2 k$. Then, for any $f : (i : ι) \to \alpha (\text{ctor } i)$, any $i : ι$, and any $x : \alpha (\text{ctor } i)$, the following equality holds: \[ \text{recursor } (\text{update } f \, i \, x) = \text{update } (\text{recursor } f) (\text{ctor } i) x \]
Option.rec_update theorem
{α : Type*} {β : Option α → Sort*} [DecidableEq α] (f : β none) (g : ∀ a, β (.some a)) (a : α) (x : β (.some a)) : Option.rec f (update g a x) = update (Option.rec f g) (.some a) x
Full source
@[simp]
lemma _root_.Option.rec_update {α : Type*} {β : Option α → Sort*} [DecidableEq α]
    (f : β none) (g : ∀ a, β (.some a)) (a : α) (x : β (.some a)) :
    Option.rec f (update g a x) = update (Option.rec f g) (.some a) x :=
  Function.rec_update (@Option.some.inj _) (Option.rec f) (fun _ _ => rfl) (fun
    | _, _, .some _, h => (h _ rfl).elim
    | _, _, .none, _ => rfl) _ _ _
Recursor Commutes with Update for Option Type
Informal description
Let $\alpha$ be a type with decidable equality, and let $\beta : \text{Option } \alpha \to \text{Type}$ be a type family. For any element $f : \beta \text{none}$, any function $g : \forall a, \beta (\text{some } a)$, any element $a : \alpha$, and any element $x : \beta (\text{some } a)$, the following equality holds: \[ \text{Option.rec } f \, (\text{update } g \, a \, x) = \text{update } (\text{Option.rec } f \, g) \, (\text{some } a) \, x \]
Function.apply_update theorem
{ι : Sort*} [DecidableEq ι] {α β : ι → Sort*} (f : ∀ i, α i → β i) (g : ∀ i, α i) (i : ι) (v : α i) (j : ι) : f j (update g i v j) = update (fun k ↦ f k (g k)) i (f i v) j
Full source
theorem apply_update {ι : Sort*} [DecidableEq ι] {α β : ι → Sort*} (f : ∀ i, α i → β i)
    (g : ∀ i, α i) (i : ι) (v : α i) (j : ι) :
    f j (update g i v j) = update (fun k ↦ f k (g k)) i (f i v) j := by
  by_cases h : j = i
  · subst j
    simp
  · simp [h]
Function Application Commutes with Update
Informal description
Let $\iota$ be a type with decidable equality, and let $\alpha, \beta : \iota \to \text{Type}$ be type families. For any function $f : \forall i, \alpha i \to \beta i$, any function $g : \forall i, \alpha i$, any index $i : \iota$, and any value $v : \alpha i$, the following holds for all $j : \iota$: \[ f_j \big(\text{update } g \, i \, v \, j\big) = \text{update } (\lambda k, f_k (g k)) \, i \, (f_i v) \, j \]
Function.apply_update₂ theorem
{ι : Sort*} [DecidableEq ι] {α β γ : ι → Sort*} (f : ∀ i, α i → β i → γ i) (g : ∀ i, α i) (h : ∀ i, β i) (i : ι) (v : α i) (w : β i) (j : ι) : f j (update g i v j) (update h i w j) = update (fun k ↦ f k (g k) (h k)) i (f i v w) j
Full source
theorem apply_update₂ {ι : Sort*} [DecidableEq ι] {α β γ : ι → Sort*} (f : ∀ i, α i → β i → γ i)
    (g : ∀ i, α i) (h : ∀ i, β i) (i : ι) (v : α i) (w : β i) (j : ι) :
    f j (update g i v j) (update h i w j) = update (fun k ↦ f k (g k) (h k)) i (f i v w) j := by
  by_cases h : j = i
  · subst j
    simp
  · simp [h]
Double Update Property for Function Application
Informal description
Let $\iota$ be a type with decidable equality, and let $\alpha, \beta, \gamma : \iota \to \text{Sort*}$ be type families. For any function $f : \forall i, \alpha i \to \beta i \to \gamma i$, any functions $g : \forall i, \alpha i$ and $h : \forall i, \beta i$, any index $i : \iota$, and any values $v : \alpha i$ and $w : \beta i$, the following holds for all $j : \iota$: \[ f_j \big(\text{update } g \, i \, v \, j\big) \big(\text{update } h \, i \, w \, j\big) = \text{update } (\lambda k, f_k (g k) (h k)) \, i \, (f_i v w) \, j \]
Function.pred_update theorem
(P : ∀ ⦃a⦄, β a → Prop) (f : ∀ a, β a) (a' : α) (v : β a') (a : α) : P (update f a' v a) ↔ a = a' ∧ P v ∨ a ≠ a' ∧ P (f a)
Full source
theorem pred_update (P : ∀ ⦃a⦄, β a → Prop) (f : ∀ a, β a) (a' : α) (v : β a') (a : α) :
    P (update f a' v a) ↔ a = a' ∧ P v ∨ a ≠ a' ∧ P (f a) := by
  rw [apply_update P, update_apply, ite_prop_iff_or]
Predicate Preservation Under Function Update
Informal description
Let $\alpha$ be a type and $\beta : \alpha \to \text{Type}$ be a family of types indexed by $\alpha$. For any predicate $P$ on $\beta$, any function $f : \forall a, \beta a$, any point $a' \in \alpha$, any value $v \in \beta a'$, and any input $a \in \alpha$, the following equivalence holds: \[ P(\text{update } f \, a' \, v \, a) \leftrightarrow (a = a' \land P v) \lor (a \neq a' \land P (f a)) \] where $\text{update } f \, a' \, v$ denotes the function equal to $f$ everywhere except at $a'$, where it takes the value $v$.
Function.comp_update theorem
{α' : Sort*} {β : Sort*} (f : α' → β) (g : α → α') (i : α) (v : α') : f ∘ update g i v = update (f ∘ g) i (f v)
Full source
theorem comp_update {α' : Sort*} {β : Sort*} (f : α' → β) (g : α → α') (i : α) (v : α') :
    f ∘ update g i v = update (f ∘ g) i (f v) :=
  funext <| apply_update _ _ _ _
Composition Commutes with Function Update
Informal description
Let $\alpha$ and $\alpha'$ be types, and let $\beta$ be another type. For any function $f \colon \alpha' \to \beta$, any function $g \colon \alpha \to \alpha'$, any index $i \in \alpha$, and any value $v \in \alpha'$, the composition of $f$ with the updated function $\text{update } g \, i \, v$ is equal to updating the composition $f \circ g$ at $i$ with $f(v)$. That is, \[ f \circ (\text{update } g \, i \, v) = \text{update } (f \circ g) \, i \, (f v). \]
Function.update_comm theorem
{α} [DecidableEq α] {β : α → Sort*} {a b : α} (h : a ≠ b) (v : β a) (w : β b) (f : ∀ a, β a) : update (update f a v) b w = update (update f b w) a v
Full source
theorem update_comm {α} [DecidableEq α] {β : α → Sort*} {a b : α} (h : a ≠ b) (v : β a) (w : β b)
    (f : ∀ a, β a) : update (update f a v) b w = update (update f b w) a v := by
  funext c
  simp only [update]
  by_cases h₁ : c = b <;> by_cases h₂ : c = a
  · rw [dif_pos h₁, dif_pos h₂]
    cases h (h₂.symm.trans h₁)
  · rw [dif_pos h₁, dif_pos h₁, dif_neg h₂]
  · rw [dif_neg h₁, dif_neg h₁]
  · rw [dif_neg h₁, dif_neg h₁]
Commutation of Function Updates at Distinct Points
Informal description
Let $\alpha$ be a type with decidable equality, and let $\beta : \alpha \to \text{Sort}*$ be a family of types indexed by $\alpha$. For any distinct elements $a, b \in \alpha$, values $v \in \beta a$ and $w \in \beta b$, and function $f : \forall a, \beta a$, we have: \[ \text{update } (\text{update } f \, a \, v) \, b \, w = \text{update } (\text{update } f \, b \, w) \, a \, v \] where $\text{update}$ denotes the function update operation.
Function.update_idem theorem
{α} [DecidableEq α] {β : α → Sort*} {a : α} (v w : β a) (f : ∀ a, β a) : update (update f a v) a w = update f a w
Full source
@[simp]
theorem update_idem {α} [DecidableEq α] {β : α → Sort*} {a : α} (v w : β a) (f : ∀ a, β a) :
    update (update f a v) a w = update f a w := by
  funext b
  by_cases h : b = a <;> simp [update, h]
Idempotence of Function Update at a Point
Informal description
For any type $\alpha$ with decidable equality, any family of types $\beta$ indexed by $\alpha$, any element $a \in \alpha$, and any two values $v, w \in \beta(a)$, updating a function $f \colon \forall a, \beta(a)$ first at $a$ with $v$ and then again at $a$ with $w$ is equivalent to updating $f$ at $a$ directly with $w$. That is, \[ \text{update } (\text{update } f \, a \, v) \, a \, w = \text{update } f \, a \, w. \]
Function.extend definition
(f : α → β) (g : α → γ) (j : β → γ) : β → γ
Full source
/-- Extension of a function `g : α → γ` along a function `f : α → β`.

For every `a : α`, `f a` is sent to `g a`. `f` might not be surjective, so we use an auxiliary
function `j : β → γ` by sending `b : β` not in the range of `f` to `j b`. If you do not care about
the behavior outside the range, `j` can be used as a junk value by setting it to be `0` or
`Classical.arbitrary` (assuming `γ` is nonempty).

This definition is mathematically meaningful only when `f a₁ = f a₂ → g a₁ = g a₂` (spelled
`g.FactorsThrough f`). In particular this holds if `f` is injective.

A typical use case is extending a function from a subtype to the entire type. If you wish to extend
`g : {b : β // p b} → γ` to a function `β → γ`, you should use `Function.extend Subtype.val g j`. -/
def extend (f : α → β) (g : α → γ) (j : β → γ) : β → γ := fun b ↦
  open scoped Classical in
  if h : ∃ a, f a = b then g (Classical.choose h) else j b
Extension of a function along another function
Informal description
Given functions $f : \alpha \to \beta$, $g : \alpha \to \gamma$, and $j : \beta \to \gamma$, the function $\text{extend}\,f\,g\,j : \beta \to \gamma$ is defined as follows: for each $b \in \beta$, if there exists $a \in \alpha$ such that $f(a) = b$, then $\text{extend}\,f\,g\,j\,b = g(a)$; otherwise, $\text{extend}\,f\,g\,j\,b = j(b)$. This construction is mathematically meaningful when $g$ factors through $f$, i.e., $f(a_1) = f(a_2)$ implies $g(a_1) = g(a_2)$ for all $a_1, a_2 \in \alpha$. In particular, this condition holds if $f$ is injective.
Function.FactorsThrough definition
(g : α → γ) (f : α → β) : Prop
Full source
/-- g factors through f : `f a = f b → g a = g b` -/
def FactorsThrough (g : α → γ) (f : α → β) : Prop :=
  ∀ ⦃a b⦄, f a = f b → g a = g b
Factorization of a function through another function
Informal description
A function \( g : \alpha \to \gamma \) factors through a function \( f : \alpha \to \beta \) if for any \( a, b \in \alpha \), whenever \( f(a) = f(b) \), then \( g(a) = g(b) \). This means that \( g \) can be expressed as a composition of \( f \) with another function.
Function.extend_def theorem
(f : α → β) (g : α → γ) (e' : β → γ) (b : β) [Decidable (∃ a, f a = b)] : extend f g e' b = if h : ∃ a, f a = b then g (Classical.choose h) else e' b
Full source
theorem extend_def (f : α → β) (g : α → γ) (e' : β → γ) (b : β) [Decidable (∃ a, f a = b)] :
    extend f g e' b = if h : ∃ a, f a = b then g (Classical.choose h) else e' b := by
  unfold extend
  congr
Definition of Function Extension via Cases
Informal description
Given functions $f : \alpha \to \beta$, $g : \alpha \to \gamma$, and $e' : \beta \to \gamma$, and for any $b \in \beta$ with a decidability condition on the existence of $a \in \alpha$ such that $f(a) = b$, the extension function satisfies: \[ \text{extend}\,f\,g\,e'\,b = \begin{cases} g(a) & \text{if } \exists a \in \alpha \text{ such that } f(a) = b, \\ e'(b) & \text{otherwise.} \end{cases} \]
Function.Injective.factorsThrough theorem
(hf : Injective f) (g : α → γ) : g.FactorsThrough f
Full source
lemma Injective.factorsThrough (hf : Injective f) (g : α → γ) : g.FactorsThrough f :=
  fun _ _ h => congr_arg g (hf h)
Factorization through an injective function
Informal description
For any injective function $f : \alpha \to \beta$ and any function $g : \alpha \to \gamma$, the function $g$ factors through $f$. That is, there exists a function $h : \beta \to \gamma$ such that $g = h \circ f$.
Function.FactorsThrough.extend_apply theorem
{g : α → γ} (hf : g.FactorsThrough f) (e' : β → γ) (a : α) : extend f g e' (f a) = g a
Full source
lemma FactorsThrough.extend_apply {g : α → γ} (hf : g.FactorsThrough f) (e' : β → γ) (a : α) :
    extend f g e' (f a) = g a := by
  classical
  simp only [extend_def, dif_pos, exists_apply_eq_apply]
  exact hf (Classical.choose_spec (exists_apply_eq_apply f a))
Extension of a Factoring Function Preserves Composition
Informal description
Let $f : \alpha \to \beta$ and $g : \alpha \to \gamma$ be functions such that $g$ factors through $f$, and let $e' : \beta \to \gamma$ be an arbitrary function. Then for any $a \in \alpha$, the extension of $g$ along $f$ with default value $e'$ satisfies $\text{extend}\,f\,g\,e'\,(f(a)) = g(a)$.
Function.Injective.extend_apply theorem
(hf : Injective f) (g : α → γ) (e' : β → γ) (a : α) : extend f g e' (f a) = g a
Full source
@[simp]
theorem Injective.extend_apply (hf : Injective f) (g : α → γ) (e' : β → γ) (a : α) :
    extend f g e' (f a) = g a :=
  (hf.factorsThrough g).extend_apply e' a
Extension of a Function Along an Injective Map Preserves Composition
Informal description
Let $f : \alpha \to \beta$ be an injective function, $g : \alpha \to \gamma$ any function, and $e' : \beta \to \gamma$ an arbitrary function. Then for any $a \in \alpha$, the extension of $g$ along $f$ with default value $e'$ satisfies $\text{extend}\,f\,g\,e'\,(f(a)) = g(a)$.
Function.extend_apply' theorem
(g : α → γ) (e' : β → γ) (b : β) (hb : ¬∃ a, f a = b) : extend f g e' b = e' b
Full source
@[simp]
theorem extend_apply' (g : α → γ) (e' : β → γ) (b : β) (hb : ¬∃ a, f a = b) :
    extend f g e' b = e' b := by
  classical
  simp [Function.extend_def, hb]
Extension Function Default Case: $\text{extend}\,f\,g\,e'\,b = e'(b)$ when $b \notin \text{range}(f)$
Informal description
For any functions $g : \alpha \to \gamma$ and $e' : \beta \to \gamma$, and any element $b \in \beta$ such that there does not exist $a \in \alpha$ with $f(a) = b$, the extension function satisfies $\text{extend}\,f\,g\,e'\,b = e'(b)$.
Function.factorsThrough_iff theorem
(g : α → γ) [Nonempty γ] : g.FactorsThrough f ↔ ∃ (e : β → γ), g = e ∘ f
Full source
lemma factorsThrough_iff (g : α → γ) [Nonempty γ] : g.FactorsThrough f ↔ ∃ (e : β → γ), g = e ∘ f :=
  ⟨fun hf => ⟨extend f g (const β (Classical.arbitrary γ)),
      funext (fun x => by simp only [comp_apply, hf.extend_apply])⟩,
  fun h _ _ hf => by rw [Classical.choose_spec h, comp_apply, comp_apply, hf]⟩
Factorization Criterion for Functions: $g$ Factors Through $f$ iff $g = e \circ f$ for Some $e$
Informal description
A function $g : \alpha \to \gamma$ factors through a function $f : \alpha \to \beta$ if and only if there exists a function $e : \beta \to \gamma$ such that $g = e \circ f$. Here, $\gamma$ is assumed to be nonempty.
Function.apply_extend theorem
{δ} {g : α → γ} (F : γ → δ) (f : α → β) (e' : β → γ) (b : β) : F (extend f g e' b) = extend f (F ∘ g) (F ∘ e') b
Full source
lemma apply_extend {δ} {g : α → γ} (F : γ → δ) (f : α → β) (e' : β → γ) (b : β) :
    F (extend f g e' b) = extend f (F ∘ g) (F ∘ e') b :=
  open scoped Classical in apply_dite F _ _ _
Commutativity of Function Extension with Composition: $F \circ \text{extend}\,f\,g\,e' = \text{extend}\,f\,(F \circ g)\,(F \circ e')$
Informal description
For any function $F : \gamma \to \delta$, functions $f : \alpha \to \beta$, $g : \alpha \to \gamma$, and $e' : \beta \to \gamma$, and any element $b \in \beta$, the following equality holds: $$ F(\text{extend}\,f\,g\,e'\,b) = \text{extend}\,f\,(F \circ g)\,(F \circ e')\,b. $$
Function.extend_injective theorem
(hf : Injective f) (e' : β → γ) : Injective fun g ↦ extend f g e'
Full source
theorem extend_injective (hf : Injective f) (e' : β → γ) : Injective fun g ↦ extend f g e' := by
  intro g₁ g₂ hg
  refine funext fun x ↦ ?_
  have H := congr_fun hg (f x)
  simp only [hf.extend_apply] at H
  exact H
Injectivity of Function Extension Along an Injective Map
Informal description
Let $f : \alpha \to \beta$ be an injective function and $e' : \beta \to \gamma$ an arbitrary function. Then the mapping $g \mapsto \text{extend}\,f\,g\,e'$ is injective on functions $g : \alpha \to \gamma$.
Function.FactorsThrough.extend_comp theorem
{g : α → γ} (e' : β → γ) (hf : FactorsThrough g f) : extend f g e' ∘ f = g
Full source
lemma FactorsThrough.extend_comp {g : α → γ} (e' : β → γ) (hf : FactorsThrough g f) :
    extendextend f g e' ∘ f = g :=
  funext fun a => hf.extend_apply e' a
Composition of Extension with Original Function Yields Original Function
Informal description
Let $f : \alpha \to \beta$ and $g : \alpha \to \gamma$ be functions such that $g$ factors through $f$, and let $e' : \beta \to \gamma$ be an arbitrary function. Then the composition of the extension of $g$ along $f$ with default value $e'$ and $f$ equals $g$, i.e., $$ \text{extend}\,f\,g\,e' \circ f = g. $$
Function.extend_const theorem
(f : α → β) (c : γ) : extend f (fun _ ↦ c) (fun _ ↦ c) = fun _ ↦ c
Full source
@[simp]
lemma extend_const (f : α → β) (c : γ) : extend f (fun _ ↦ c) (fun _ ↦ c) = fun _ ↦ c :=
  funext fun _ ↦ open scoped Classical in ite_id _
Constant Extension of a Function Yields Constant Function
Informal description
For any function $f : \alpha \to \beta$ and constant $c \in \gamma$, the extension of $f$ with the constant function $\lambda \_, c$ and the constant function $\lambda \_, c$ is equal to the constant function $\lambda \_, c$. In other words, $\text{extend}\,f\,(\lambda \_, c)\,(\lambda \_, c) = \lambda \_, c$.
Function.extend_comp theorem
(hf : Injective f) (g : α → γ) (e' : β → γ) : extend f g e' ∘ f = g
Full source
@[simp]
theorem extend_comp (hf : Injective f) (g : α → γ) (e' : β → γ) : extendextend f g e' ∘ f = g :=
  funext fun a ↦ hf.extend_apply g e' a
Composition of Extension with Injective Function Preserves Original Function
Informal description
Let $f : \alpha \to \beta$ be an injective function, $g : \alpha \to \gamma$ any function, and $e' : \beta \to \gamma$ an arbitrary function. Then the composition of the extension of $g$ along $f$ with default value $e'$ and $f$ equals $g$, i.e., $$ \text{extend}\,f\,g\,e' \circ f = g. $$
Function.Injective.surjective_comp_right' theorem
(hf : Injective f) (g₀ : β → γ) : Surjective fun g : β → γ ↦ g ∘ f
Full source
theorem Injective.surjective_comp_right' (hf : Injective f) (g₀ : β → γ) :
    Surjective fun g : β → γ ↦ g ∘ f :=
  fun g ↦ ⟨extend f g g₀, extend_comp hf _ _⟩
Surjectivity of Right Composition with Injective Function
Informal description
Let $f : \alpha \to \beta$ be an injective function and $g_0 : \beta \to \gamma$ an arbitrary function. Then the map $g \mapsto g \circ f$ from functions $\beta \to \gamma$ to functions $\alpha \to \gamma$ is surjective. In other words, for any function $h : \alpha \to \gamma$, there exists a function $g : \beta \to \gamma$ such that $g \circ f = h$.
Function.Injective.surjective_comp_right theorem
[Nonempty γ] (hf : Injective f) : Surjective fun g : β → γ ↦ g ∘ f
Full source
theorem Injective.surjective_comp_right [Nonempty γ] (hf : Injective f) :
    Surjective fun g : β → γ ↦ g ∘ f :=
  hf.surjective_comp_right' fun _ ↦ Classical.choice ‹_›
Surjectivity of Post-Composition with an Injective Function
Informal description
Let $\gamma$ be a nonempty type and $f : \alpha \to \beta$ be an injective function. Then the map $g \mapsto g \circ f$ from functions $\beta \to \gamma$ to functions $\alpha \to \gamma$ is surjective. That is, for any function $h : \alpha \to \gamma$, there exists a function $g : \beta \to \gamma$ such that $g \circ f = h$.
Function.surjective_comp_right_iff_injective theorem
{γ : Type*} [Nontrivial γ] : Surjective (fun g : β → γ ↦ g ∘ f) ↔ Injective f
Full source
theorem surjective_comp_right_iff_injective {γ : Type*} [Nontrivial γ] :
    SurjectiveSurjective (fun g : β → γ ↦ g ∘ f) ↔ Injective f := by
  classical
  refine ⟨not_imp_not.mp fun not_inj surj ↦ not_subsingleton γ ⟨fun c c' ↦ ?_⟩,
    (·.surjective_comp_right)⟩
  simp only [Injective, not_forall] at not_inj
  have ⟨a₁, a₂, eq, ne⟩ := not_inj
  have ⟨f, hf⟩ := surj (if · = a₂ then c else c')
  have h₁ := congr_fun hf a₁
  have h₂ := congr_fun hf a₂
  simp only [comp_apply, if_neg ne, reduceIte] at h₁ h₂
  rw [← h₁, eq, h₂]
Surjectivity of Post-Composition Equals Injectivity of $f$
Informal description
For any nontrivial type $\gamma$, the map $g \mapsto g \circ f$ from functions $\beta \to \gamma$ to functions $\alpha \to \gamma$ is surjective if and only if the function $f \colon \alpha \to \beta$ is injective.
Function.Bijective.comp_right theorem
(hf : Bijective f) : Bijective fun g : β → γ ↦ g ∘ f
Full source
theorem Bijective.comp_right (hf : Bijective f) : Bijective fun g : β → γ ↦ g ∘ f :=
  ⟨hf.surjective.injective_comp_right, fun g ↦
    ⟨g ∘ surjInv hf.surjective,
     by simp only [comp_assoc g _ f, (leftInverse_surjInv hf).comp_eq_id, comp_id]⟩⟩
Bijectivity of Post-Composition with a Bijective Function
Informal description
Let $f \colon \alpha \to \beta$ be a bijective function. Then the function composition map $g \mapsto g \circ f$ from $\beta \to \gamma$ to $\alpha \to \gamma$ is also bijective.
Function.FactorsThrough.rfl theorem
{α β : Sort*} {f : α → β} : FactorsThrough f f
Full source
protected theorem rfl {α β : Sort*} {f : α → β} : FactorsThrough f f := fun _ _ ↦ id
Reflexivity of Function Factorization
Informal description
For any function $f : \alpha \to \beta$, the function $f$ factors through itself. That is, the relation $\text{FactorsThrough}(f, f)$ holds.
Function.FactorsThrough.comp_left theorem
{α β γ δ : Sort*} {f : α → β} {g : α → γ} (h : FactorsThrough g f) (g' : γ → δ) : FactorsThrough (g' ∘ g) f
Full source
theorem comp_left {α β γ δ : Sort*} {f : α → β} {g : α → γ} (h : FactorsThrough g f) (g' : γ → δ) :
    FactorsThrough (g' ∘ g) f := fun _x _y hxy ↦
  congr_arg g' (h hxy)
Composition Preserves Factorization Through a Function
Informal description
Let $f \colon \alpha \to \beta$ and $g \colon \alpha \to \gamma$ be functions such that $g$ factors through $f$. Then for any function $g' \colon \gamma \to \delta$, the composition $g' \circ g$ also factors through $f$.
Function.FactorsThrough.comp_right theorem
{α β γ δ : Sort*} {f : α → β} {g : α → γ} (h : FactorsThrough g f) (g' : δ → α) : FactorsThrough (g ∘ g') (f ∘ g')
Full source
theorem comp_right {α β γ δ : Sort*} {f : α → β} {g : α → γ} (h : FactorsThrough g f) (g' : δ → α) :
    FactorsThrough (g ∘ g') (f ∘ g') := fun _x _y hxy ↦
  h hxy
Factorization Through Composed Functions
Informal description
Let $f \colon \alpha \to \beta$ and $g \colon \alpha \to \gamma$ be functions such that $g$ factors through $f$. Then for any function $g' \colon \delta \to \alpha$, the composition $g \circ g'$ factors through the composition $f \circ g'$.
Function.uncurry_def theorem
{α β γ} (f : α → β → γ) : uncurry f = fun p ↦ f p.1 p.2
Full source
theorem uncurry_def {α β γ} (f : α → β → γ) : uncurry f = fun p ↦ f p.1 p.2 :=
  rfl
Definition of Uncurrying: $\text{uncurry}(f)(a, b) = f(a, b)$
Informal description
For any function $f \colon \alpha \to \beta \to \gamma$, the uncurried version of $f$ is equal to the function that maps a pair $(a, b)$ to $f(a, b)$.
Function.bicompl definition
(f : γ → δ → ε) (g : α → γ) (h : β → δ) (a b)
Full source
/-- Compose a binary function `f` with a pair of unary functions `g` and `h`.
If both arguments of `f` have the same type and `g = h`, then `bicompl f g g = f on g`. -/
def bicompl (f : γ → δ → ε) (g : α → γ) (h : β → δ) (a b) :=
  f (g a) (h b)
Binary function composition with unary functions on both arguments
Informal description
Given a binary function \( f : \gamma \to \delta \to \varepsilon \) and two unary functions \( g : \alpha \to \gamma \) and \( h : \beta \to \delta \), the function `bicompl f g h` is defined as the composition \( f (g a) (h b) \) for any \( a \in \alpha \) and \( b \in \beta \). If \( g = h \), then this simplifies to \( f \) applied to \( g a \) and \( g b \), i.e., \( f \) composed with \( g \) on both arguments.
Function.bicompr definition
(f : γ → δ) (g : α → β → γ) (a b)
Full source
/-- Compose a unary function `f` with a binary function `g`. -/
def bicompr (f : γ → δ) (g : α → β → γ) (a b) :=
  f (g a b)
Binary composition of functions
Informal description
Given a unary function \( f : \gamma \to \delta \) and a binary function \( g : \alpha \to \beta \to \gamma \), the function `Function.bicompr` constructs a new binary function \( h : \alpha \to \beta \to \delta \) defined by \( h(a, b) = f(g(a, b)) \).
Function.uncurry_bicompr theorem
(f : α → β → γ) (g : γ → δ) : uncurry (g ∘₂ f) = g ∘ uncurry f
Full source
theorem uncurry_bicompr (f : α → β → γ) (g : γ → δ) : uncurry (g ∘₂ f) = g ∘ uncurry f :=
  rfl
Uncurrying Preserves Binary Composition
Informal description
For any functions $f : \alpha \to \beta \to \gamma$ and $g : \gamma \to \delta$, the uncurried form of the composition $g \circ_2 f$ is equal to the composition $g \circ \text{uncurry}\, f$, i.e., \[ \text{uncurry}\, (g \circ_2 f) = g \circ (\text{uncurry}\, f). \]
Function.uncurry_bicompl theorem
(f : γ → δ → ε) (g : α → γ) (h : β → δ) : uncurry (bicompl f g h) = uncurry f ∘ Prod.map g h
Full source
theorem uncurry_bicompl (f : γ → δ → ε) (g : α → γ) (h : β → δ) :
    uncurry (bicompl f g h) = uncurryuncurry f ∘ Prod.map g h :=
  rfl
Uncurrying Commutes with Binary Function Composition via Product Map
Informal description
For any binary function $f : \gamma \to \delta \to \varepsilon$ and unary functions $g : \alpha \to \gamma$, $h : \beta \to \delta$, the uncurried version of the composition $f \circ (g, h)$ is equal to the composition of the uncurried version of $f$ with the product map $(g, h)$, i.e., \[ \text{uncurry} (f \circ (g, h)) = \text{uncurry}(f) \circ (g \times h). \]
Function.HasUncurry structure
(α : Type*) (β : outParam Type*) (γ : outParam Type*)
Full source
/-- Records a way to turn an element of `α` into a function from `β` to `γ`. The most generic use
is to recursively uncurry. For instance `f : α → β → γ → δ` will be turned into
`↿f : α × β × γ → δ`. One can also add instances for bundled maps. -/
class HasUncurry (α : Type*) (β : outParam Type*) (γ : outParam Type*) where
  /-- Uncurrying operator. The most generic use is to recursively uncurry. For instance
  `f : α → β → γ → δ` will be turned into `↿f : α × β × γ → δ`. One can also add instances
  for bundled maps. -/
  uncurry : α → β → γ
Uncurrying Structure
Informal description
The structure `Function.HasUncurry` provides a way to convert an element of type `α` into a function from `β` to `γ`. This is most commonly used to recursively uncurry functions. For example, a function `f : α → β → γ → δ` can be transformed into `↿f : α × β × γ → δ`. The structure can also be instantiated for bundled maps.
Function.term↿_ definition
: Lean.ParserDescr✝
Full source
@[inherit_doc] notation:arg "↿" x:arg => HasUncurry.uncurry x
Uncurry notation
Informal description
The notation `↿f` represents the uncurried version of a function `f`, where `f` is transformed from a curried form (taking arguments sequentially) to a form that takes a single pair/tuple of arguments.
Function.hasUncurryBase instance
: HasUncurry (α → β) α β
Full source
instance hasUncurryBase : HasUncurry (α → β) α β :=
  ⟨id⟩
Uncurrying Structure for Functions
Informal description
For any types $\alpha$ and $\beta$, there is a canonical uncurrying structure that converts a function $f : \alpha \to \beta$ into a function from $\alpha$ to $\beta$.
Function.hasUncurryInduction instance
[HasUncurry β γ δ] : HasUncurry (α → β) (α × γ) δ
Full source
instance hasUncurryInduction [HasUncurry β γ δ] : HasUncurry (α → β) (α × γ) δ :=
  ⟨fun f p ↦ (↿(f p.1)) p.2⟩
Inductive Uncurrying of Functions
Informal description
For any types $\alpha$, $\beta$, $\gamma$, and $\delta$, if there exists an uncurrying structure from $\beta$ to $\gamma$ to $\delta$, then there is an uncurrying structure from functions $\alpha \to \beta$ to pairs $\alpha \times \gamma$ to $\delta$.
Function.Involutive definition
{α} (f : α → α) : Prop
Full source
/-- A function is involutive, if `f ∘ f = id`. -/
def Involutive {α} (f : α → α) : Prop :=
  ∀ x, f (f x) = x
Involutive function
Informal description
A function \( f : \alpha \to \alpha \) is called *involutive* if it satisfies \( f(f(x)) = x \) for all \( x \in \alpha \). In other words, applying \( f \) twice returns the original input.
Bool.involutive_not theorem
: Involutive not
Full source
theorem _root_.Bool.involutive_not : Involutive not :=
  Bool.not_not
Involutivity of Boolean Negation: $\text{not} \circ \text{not} = \text{id}$
Informal description
The logical negation function `not` on boolean values is involutive, meaning that for any boolean value `b`, applying `not` twice returns `b`, i.e., $\text{not}(\text{not}(b)) = b$.
Function.Involutive.comp_self theorem
: f ∘ f = id
Full source
@[simp]
theorem comp_self : f ∘ f = id :=
  funext h
Composition of Involutive Function with Itself Yields Identity
Informal description
For any involutive function $f : \alpha \to \alpha$, the composition of $f$ with itself equals the identity function, i.e., $f \circ f = \text{id}$.
Function.Involutive.leftInverse theorem
: LeftInverse f f
Full source
protected theorem leftInverse : LeftInverse f f := h
Involutive Functions are Self-Inverse
Informal description
An involutive function $f : \alpha \to \alpha$ is its own left inverse, i.e., $f \circ f = \text{id}$ implies that $f(f(x)) = x$ for all $x \in \alpha$.
Function.Involutive.leftInverse_iff theorem
{g : α → α} : g.LeftInverse f ↔ g = f
Full source
theorem leftInverse_iff {g : α → α} :
    g.LeftInverse f ↔ g = f :=
  ⟨fun hg ↦ funext fun x ↦ by rw [← h x, hg, h], fun he ↦ he ▸ h.leftInverse⟩
Left Inverse Characterization for Involutive Functions: $g \circ f = \text{id} \leftrightarrow g = f$
Informal description
For any function $g : \alpha \to \alpha$, $g$ is a left inverse of an involutive function $f : \alpha \to \alpha$ if and only if $g$ is equal to $f$.
Function.Involutive.rightInverse theorem
: RightInverse f f
Full source
protected theorem rightInverse : RightInverse f f := h
Involutive Functions are Self-Right-Inverse
Informal description
An involutive function $f : \alpha \to \alpha$ is its own right inverse, i.e., $f \circ f = \text{id}$ implies that $f(f(x)) = x$ for all $x \in \alpha$.
Function.Involutive.injective theorem
: Injective f
Full source
protected theorem injective : Injective f := h.leftInverse.injective
Involutive Functions are Injective
Informal description
An involutive function $f : \alpha \to \alpha$ is injective.
Function.Involutive.surjective theorem
: Surjective f
Full source
protected theorem surjective : Surjective f := fun x ↦ ⟨f x, h x⟩
Involutive Functions are Surjective
Informal description
If a function $f : \alpha \to \alpha$ is involutive (i.e., $f(f(x)) = x$ for all $x \in \alpha$), then $f$ is surjective.
Function.Involutive.bijective theorem
: Bijective f
Full source
protected theorem bijective : Bijective f := ⟨h.injective, h.surjective⟩
Involutive Functions are Bijective
Informal description
An involutive function $f : \alpha \to \alpha$ is bijective.
Function.Involutive.ite_not theorem
(P : Prop) [Decidable P] (x : α) : f (ite P x (f x)) = ite (¬P) x (f x)
Full source
/-- Involuting an `ite` of an involuted value `x : α` negates the `Prop` condition in the `ite`. -/
protected theorem ite_not (P : Prop) [Decidable P] (x : α) :
    f (ite P x (f x)) = ite (¬P) x (f x) := by rw [apply_ite f, h, ite_not]
Involutive Function Acts on Conditional by Negating Condition
Informal description
Let \( f : \alpha \to \alpha \) be an involutive function and \( P \) be a decidable proposition. Then for any \( x \in \alpha \), applying \( f \) to the conditional expression \(\text{ite}(P, x, f(x))\) is equivalent to the conditional expression \(\text{ite}(\neg P, x, f(x))\). That is, \[ f(\text{if } P \text{ then } x \text{ else } f(x)) = \text{if } \neg P \text{ then } x \text{ else } f(x). \]
Function.not_involutive theorem
: Involutive Not
Full source
lemma not_involutive : Involutive Not := fun _ ↦ propext not_not
Double Negation is Involutive
Informal description
The logical negation function $\neg$ is involutive, meaning that for any proposition $P$, we have $\neg (\neg P) = P$.
Function.symmetric_apply_eq_iff theorem
{α : Sort*} {f : α → α} : Symmetric (f · = ·) ↔ Involutive f
Full source
@[simp]
lemma symmetric_apply_eq_iff {α : Sort*} {f : α → α} : SymmetricSymmetric (f · = ·) ↔ Involutive f := by
  simp [Symmetric, Involutive]
Symmetry of $f(x) = y$ Relation is Equivalent to Involutivity of $f$
Informal description
For any function $f : \alpha \to \alpha$, the relation $\{(x, y) \mid f(x) = y\}$ is symmetric if and only if $f$ is involutive, i.e., $f(f(x)) = x$ for all $x \in \alpha$.
Function.Injective2 definition
{α β γ : Sort*} (f : α → β → γ) : Prop
Full source
/-- The property of a binary function `f : α → β → γ` being injective.
Mathematically this should be thought of as the corresponding function `α × β → γ` being injective.
-/
def Injective2 {α β γ : Sort*} (f : α → β → γ) : Prop :=
  ∀ ⦃a₁ a₂ b₁ b₂⦄, f a₁ b₁ = f a₂ b₂ → a₁ = a₂ ∧ b₁ = b₂
Injectivity of a binary function
Informal description
A binary function \( f : \alpha \to \beta \to \gamma \) is called *injective* if for any \( a_1, a_2 \in \alpha \) and \( b_1, b_2 \in \beta \), the equality \( f(a_1, b_1) = f(a_2, b_2) \) implies \( a_1 = a_2 \) and \( b_1 = b_2 \). This is equivalent to the corresponding function \( \alpha \times \beta \to \gamma \) being injective.
Function.Injective2.left theorem
(hf : Injective2 f) (b : β) : Function.Injective fun a ↦ f a b
Full source
/-- A binary injective function is injective when only the left argument varies. -/
protected theorem left (hf : Injective2 f) (b : β) : Function.Injective fun a ↦ f a b :=
  fun _ _ h ↦ (hf h).left
Left Argument Injectivity of Binary Injective Function
Informal description
Let $f : \alpha \to \beta \to \gamma$ be an injective binary function. Then for any fixed $b \in \beta$, the unary function $a \mapsto f(a, b)$ is injective.
Function.Injective2.right theorem
(hf : Injective2 f) (a : α) : Function.Injective (f a)
Full source
/-- A binary injective function is injective when only the right argument varies. -/
protected theorem right (hf : Injective2 f) (a : α) : Function.Injective (f a) :=
  fun _ _ h ↦ (hf h).right
Injectivity of a Binary Function in the Right Argument
Informal description
If a binary function $f : \alpha \to \beta \to \gamma$ is injective in both arguments (i.e., $f(a_1, b_1) = f(a_2, b_2)$ implies $a_1 = a_2$ and $b_1 = b_2$), then for any fixed $a \in \alpha$, the unary function $f(a) : \beta \to \gamma$ is injective.
Function.Injective2.uncurry theorem
{α β γ : Type*} {f : α → β → γ} (hf : Injective2 f) : Function.Injective (uncurry f)
Full source
protected theorem uncurry {α β γ : Type*} {f : α → β → γ} (hf : Injective2 f) :
    Function.Injective (uncurry f) :=
  fun ⟨_, _⟩ ⟨_, _⟩ h ↦ (hf h).elim (congr_arg₂ _)
Injectivity of Uncurried Binary Function from Component-wise Injectivity
Informal description
For any types $\alpha, \beta, \gamma$ and a binary function $f : \alpha \to \beta \to \gamma$, if $f$ is injective in both arguments (i.e., $f(a_1, b_1) = f(a_2, b_2)$ implies $a_1 = a_2$ and $b_1 = b_2$), then the uncurried version of $f$ (i.e., the function $(a, b) \mapsto f(a, b)$) is injective.
Function.Injective2.left' theorem
(hf : Injective2 f) [Nonempty β] : Function.Injective f
Full source
/-- As a map from the left argument to a unary function, `f` is injective. -/
theorem left' (hf : Injective2 f) [Nonempty β] : Function.Injective f := fun _ _ h ↦
  let ⟨b⟩ := ‹Nonempty β›
  hf.left b <| (congr_fun h b :)
Injectivity of Binary Function in First Argument with Nonempty Second Argument
Informal description
Let $f : \alpha \to \beta \to \gamma$ be an injective binary function, and suppose $\beta$ is nonempty. Then the function $f$ is injective in its first argument, i.e., for any $a_1, a_2 \in \alpha$, if $f(a_1) = f(a_2)$, then $a_1 = a_2$.
Function.Injective2.right' theorem
(hf : Injective2 f) [Nonempty α] : Function.Injective fun b a ↦ f a b
Full source
/-- As a map from the right argument to a unary function, `f` is injective. -/
theorem right' (hf : Injective2 f) [Nonempty α] : Function.Injective fun b a ↦ f a b :=
  fun _ _ h ↦
    let ⟨a⟩ := ‹Nonempty α›
    hf.right a <| (congr_fun h a :)
Injectivity of Binary Function in Second Argument with Nonempty First Argument
Informal description
Let $f : \alpha \to \beta \to \gamma$ be an injective binary function, and suppose $\alpha$ is nonempty. Then the function $\lambda b a \mapsto f(a, b)$ is injective in its second argument, i.e., for any $b_1, b_2 \in \beta$, if $f(a, b_1) = f(a, b_2)$ for all $a \in \alpha$, then $b_1 = b_2$.
Function.sometimes definition
{α β} [Nonempty β] (f : α → β) : β
Full source
/-- `sometimes f` evaluates to some value of `f`, if it exists. This function is especially
interesting in the case where `α` is a proposition, in which case `f` is necessarily a
constant function, so that `sometimes f = f a` for all `a`. -/
noncomputable def sometimes {α β} [Nonempty β] (f : α → β) : β :=
  open scoped Classical in
  if h : Nonempty α then f (Classical.choice h) else Classical.choice ‹_›
Arbitrary value of a function
Informal description
Given a nonempty type $\beta$ and a function $f : \alpha \to \beta$, the function `sometimes f` evaluates to some value of $f$ if $\alpha$ is nonempty. If $\alpha$ is empty, it returns an arbitrary element of $\beta$. When $\alpha$ is a proposition, $f$ is constant and `sometimes f` equals $f(a)$ for any $a : \alpha$.
Function.sometimes_eq theorem
{p : Prop} {α} [Nonempty α] (f : p → α) (a : p) : sometimes f = f a
Full source
theorem sometimes_eq {p : Prop} {α} [Nonempty α] (f : p → α) (a : p) : sometimes f = f a :=
  dif_pos ⟨a⟩
Value of `sometimes` on a Propositional Domain
Informal description
For any nonempty type $\alpha$ and any function $f : p \to \alpha$ where $p$ is a proposition, if $a$ is a proof of $p$, then $\text{sometimes}(f) = f(a)$.
Function.sometimes_spec theorem
{p : Prop} {α} [Nonempty α] (P : α → Prop) (f : p → α) (a : p) (h : P (f a)) : P (sometimes f)
Full source
theorem sometimes_spec {p : Prop} {α} [Nonempty α] (P : α → Prop) (f : p → α) (a : p)
    (h : P (f a)) : P (sometimes f) := by
  rwa [sometimes_eq]
Preservation of Predicate under `sometimes` for Propositional Domains
Informal description
For any nonempty type $\alpha$, any predicate $P$ on $\alpha$, any function $f : p \to \alpha$ where $p$ is a proposition, and any proof $a$ of $p$, if $P(f(a))$ holds, then $P(\text{sometimes}(f))$ also holds.
forall_existsUnique_iff theorem
{r : α → β → Prop} : (∀ a, ∃! b, r a b) ↔ ∃ f : α → β, ∀ {a b}, r a b ↔ f a = b
Full source
/-- A relation `r : α → β → Prop` is "function-like"
(for each `a` there exists a unique `b` such that `r a b`)
if and only if it is `(f · = ·)` for some function `f`. -/
lemma forall_existsUnique_iff {r : α → β → Prop} :
    (∀ a, ∃! b, r a b) ↔ ∃ f : α → β, ∀ {a b}, r a b ↔ f a = b := by
  refine ⟨fun h ↦ ?_, ?_⟩
  · refine ⟨fun a ↦ (h a).choose, fun hr ↦ ?_, fun h' ↦ h' ▸ ?_⟩
    exacts [((h _).choose_spec.2 _ hr).symm, (h _).choose_spec.1]
  · rintro ⟨f, hf⟩
    simp [hf]
Function-like Relations are Exactly Those Induced by Functions
Informal description
A relation $r : \alpha \to \beta \to \mathrm{Prop}$ is function-like (i.e., for every $a \in \alpha$ there exists a unique $b \in \beta$ such that $r(a, b)$ holds) if and only if there exists a function $f : \alpha \to \beta$ such that for all $a \in \alpha$ and $b \in \beta$, the relation $r(a, b)$ is equivalent to $f(a) = b$.
forall_existsUnique_iff' theorem
{r : α → β → Prop} : (∀ a, ∃! b, r a b) ↔ ∃ f : α → β, r = (f · = ·)
Full source
/-- A relation `r : α → β → Prop` is "function-like"
(for each `a` there exists a unique `b` such that `r a b`)
if and only if it is `(f · = ·)` for some function `f`. -/
lemma forall_existsUnique_iff' {r : α → β → Prop} :
    (∀ a, ∃! b, r a b) ↔ ∃ f : α → β, r = (f · = ·) := by
  simp [forall_existsUnique_iff, funext_iff]
Function-like Relations are Induced by Functions
Informal description
A relation $r : \alpha \to \beta \to \mathrm{Prop}$ is function-like (i.e., for every $a \in \alpha$ there exists a unique $b \in \beta$ such that $r(a, b)$ holds) if and only if there exists a function $f : \alpha \to \beta$ such that $r$ is equal to the relation $(f(a) = b)$.
Symmetric.forall_existsUnique_iff' theorem
{r : α → α → Prop} (hr : Symmetric r) : (∀ a, ∃! b, r a b) ↔ ∃ f : α → α, Involutive f ∧ r = (f · = ·)
Full source
/-- A symmetric relation `r : α → α → Prop` is "function-like"
(for each `a` there exists a unique `b` such that `r a b`)
if and only if it is `(f · = ·)` for some involutive function `f`. -/
protected lemma Symmetric.forall_existsUnique_iff' {r : α → α → Prop} (hr : Symmetric r) :
    (∀ a, ∃! b, r a b) ↔ ∃ f : α → α, Involutive f ∧ r = (f · = ·) := by
  refine ⟨fun h ↦ ?_, fun ⟨f, _, hf⟩ ↦ forall_existsUnique_iff'.2 ⟨f, hf⟩⟩
  rcases forall_existsUnique_iff'.1 h with ⟨f, rfl : r = _⟩
  exact ⟨f, symmetric_apply_eq_iff.1 hr, rfl⟩
Symmetric Function-like Relations are Induced by Involutive Functions
Informal description
For any symmetric relation $r : \alpha \to \alpha \to \mathrm{Prop}$, the following are equivalent: 1. For every $a \in \alpha$, there exists a unique $b \in \alpha$ such that $r(a, b)$ holds. 2. There exists an involutive function $f : \alpha \to \alpha$ (i.e., $f(f(x)) = x$ for all $x \in \alpha$) such that $r$ is equal to the relation $\{(x, y) \mid f(x) = y\}$.
Symmetric.forall_existsUnique_iff theorem
{r : α → α → Prop} (hr : Symmetric r) : (∀ a, ∃! b, r a b) ↔ ∃ f : α → α, Involutive f ∧ ∀ {a b}, r a b ↔ f a = b
Full source
/-- A symmetric relation `r : α → α → Prop` is "function-like"
(for each `a` there exists a unique `b` such that `r a b`)
if and only if it is `(f · = ·)` for some involutive function `f`. -/
protected lemma Symmetric.forall_existsUnique_iff {r : α → α → Prop} (hr : Symmetric r) :
    (∀ a, ∃! b, r a b) ↔ ∃ f : α → α, Involutive f ∧ ∀ {a b}, r a b ↔ f a = b := by
  simp [hr.forall_existsUnique_iff', funext_iff]
Symmetric Function-like Relations are Induced by Involutive Functions
Informal description
For any symmetric relation $r : \alpha \to \alpha \to \mathrm{Prop}$, the following are equivalent: 1. For every $a \in \alpha$, there exists a unique $b \in \alpha$ such that $r(a, b)$ holds. 2. There exists an involutive function $f : \alpha \to \alpha$ (i.e., $f(f(x)) = x$ for all $x \in \alpha$) such that for all $a, b \in \alpha$, $r(a, b)$ holds if and only if $f(a) = b$.
Set.piecewise definition
{α : Type u} {β : α → Sort v} (s : Set α) (f g : ∀ i, β i) [∀ j, Decidable (j ∈ s)] : ∀ i, β i
Full source
/-- `s.piecewise f g` is the function equal to `f` on the set `s`, and to `g` on its complement. -/
def Set.piecewise {α : Type u} {β : α → Sort v} (s : Set α) (f g : ∀ i, β i)
    [∀ j, Decidable (j ∈ s)] : ∀ i, β i :=
  fun i ↦ if i ∈ s then f i else g i
Piecewise function defined on a set
Informal description
Given a set $s \subseteq \alpha$ and two functions $f, g : \forall i, \beta i$, the function $s.\text{piecewise}\ f\ g$ is defined piecewise as: \[ (s.\text{piecewise}\ f\ g)(i) = \begin{cases} f(i) & \text{if } i \in s, \\ g(i) & \text{otherwise.} \end{cases} \] Here, the condition $i \in s$ is decidable for each $i \in \alpha$.
cast_bijective theorem
{α β : Sort _} (h : α = β) : Function.Bijective (cast h)
Full source
theorem cast_bijective {α β : Sort _} (h : α = β) : Function.Bijective (cast h) := by
  cases h
  exact ⟨fun _ _ ↦ id, fun x ↦ ⟨x, rfl⟩⟩
Bijectivity of Type Casting via Equality Proof
Informal description
For any types $\alpha$ and $\beta$ and an equality proof $h : \alpha = \beta$, the type cast function $\mathrm{cast}\,h : \alpha \to \beta$ is bijective. That is, it is both injective (preserves distinctness) and surjective (covers all elements of $\beta$).
cast_inj theorem
{α β : Type u} (h : α = β) {x y : α} : cast h x = cast h y ↔ x = y
Full source
@[simp]
theorem cast_inj {α β : Type u} (h : α = β) {x y : α} : castcast h x = cast h y ↔ x = y :=
  (cast_bijective h).injective.eq_iff
Injectivity of Type Casting via Equality Proof
Informal description
For any types $\alpha$ and $\beta$ and an equality proof $h : \alpha = \beta$, the type cast function $\mathrm{cast}\,h$ is injective. That is, for any $x, y \in \alpha$, we have $\mathrm{cast}\,h\,x = \mathrm{cast}\,h\,y$ if and only if $x = y$.
Function.LeftInverse.cast_eq theorem
{γ : β → Sort v} {f : α → β} {g : β → α} (h : Function.LeftInverse g f) (C : ∀ a : α, γ (f a)) (a : α) : cast (congr_arg (fun a ↦ γ (f a)) (h a)) (C (g (f a))) = C a
Full source
theorem Function.LeftInverse.cast_eq {γ : β → Sort v} {f : α → β} {g : β → α}
    (h : Function.LeftInverse g f) (C : ∀ a : α, γ (f a)) (a : α) :
    cast (congr_arg (fun a ↦ γ (f a)) (h a)) (C (g (f a))) = C a := by
  rw [cast_eq_iff_heq, h]
Cast Equality for Left Inverse Functions with Dependent Types
Informal description
Let $\gamma : \beta \to \text{Sort}\ v$ be a type family, $f : \alpha \to \beta$ and $g : \beta \to \alpha$ be functions such that $g$ is a left inverse of $f$ (i.e., $g(f(a)) = a$ for all $a \in \alpha$). For any dependent function $C : \forall a : \alpha, \gamma(f(a))$ and any $a \in \alpha$, we have the equality: \[ \text{cast}\big(\text{congr\_arg}\ (\lambda a \mapsto \gamma(f(a)))\ (h\ a)\big)\ \big(C(g(f(a)))\big) = C(a) \] where $\text{cast}$ converts between equal types and $\text{congr\_arg}$ constructs equality of applications from equality of arguments.
Set.SeparatesPoints definition
{α β : Type*} (A : Set (α → β)) : Prop
Full source
/-- A set of functions "separates points"
if for each pair of distinct points there is a function taking different values on them. -/
def Set.SeparatesPoints {α β : Type*} (A : Set (α → β)) : Prop :=
  ∀ ⦃x y : α⦄, x ≠ y∃ f ∈ A, (f x : β) ≠ f y
Set of functions separates points
Informal description
A set $A$ of functions from $\alpha$ to $\beta$ is said to *separate points* if for any two distinct elements $x, y \in \alpha$, there exists a function $f \in A$ such that $f(x) \neq f(y)$.
InvImage.equivalence theorem
{α : Sort u} {β : Sort v} (r : β → β → Prop) (f : α → β) (h : Equivalence r) : Equivalence (InvImage r f)
Full source
theorem InvImage.equivalence {α : Sort u} {β : Sort v} (r : β → β → Prop) (f : α → β)
    (h : Equivalence r) : Equivalence (InvImage r f) :=
  ⟨fun _ ↦ h.1 _, h.symm, h.trans⟩
Equivalence Relation Induced by Function via Inverse Image
Informal description
Let $r$ be an equivalence relation on a type $\beta$, and let $f : \alpha \to \beta$ be a function. Then the relation $\text{InvImage}(r, f)$ defined by $(x, y) \mapsto r(f(x), f(y))$ is also an equivalence relation on $\alpha$.
instDecidableUncurryOfFstSnd_mathlib instance
{α β : Type*} {r : α → β → Prop} {x : α × β} [Decidable (r x.1 x.2)] : Decidable (uncurry r x)
Full source
instance {α β : Type*} {r : α → β → Prop} {x : α × β} [Decidable (r x.1 x.2)] :
  Decidable (uncurry r x) :=
‹Decidable _›
Decidability of Uncurried Relations from Curried Relations
Informal description
For any types $\alpha$ and $\beta$, a relation $r : \alpha \to \beta \to \mathrm{Prop}$, and a pair $x : \alpha \times \beta$, if the proposition $r(x.1, x.2)$ is decidable, then the uncurried relation $\mathrm{uncurry}\, r\, x$ is also decidable.
instDecidableCurryOfMk_mathlib instance
{α β : Type*} {r : α × β → Prop} {a : α} {b : β} [Decidable (r (a, b))] : Decidable (curry r a b)
Full source
instance {α β : Type*} {r : α × β → Prop} {a : α} {b : β} [Decidable (r (a, b))] :
  Decidable (curry r a b) :=
‹Decidable _›
Decidability of Curried Relations
Informal description
For any types $\alpha$ and $\beta$, and a relation $r$ on $\alpha \times \beta$, if $r(a, b)$ is decidable for any pair $(a, b) \in \alpha \times \beta$, then the curried relation $\text{curry}\, r\, a\, b$ is also decidable for any $a \in \alpha$ and $b \in \beta$.