Context
stringlengths
227
76.5k
target
stringlengths
0
11.6k
file_name
stringlengths
21
79
start
int64
14
3.67k
end
int64
16
3.69k
/- Copyright (c) 2018 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad -/ import Mathlib.Data.Set.BooleanAlgebra import Mathlib.Tactic.AdaptationNote /-! # Relations This file defines bundled relations. A relation between `α` and `β` is a function `α → β → Prop`. Relations are also known as set-valued functions, or partial multifunctions. ## Main declarations * `Rel α β`: Relation between `α` and `β`. * `Rel.inv`: `r.inv` is the `Rel β α` obtained by swapping the arguments of `r`. * `Rel.dom`: Domain of a relation. `x ∈ r.dom` iff there exists `y` such that `r x y`. * `Rel.codom`: Codomain, aka range, of a relation. `y ∈ r.codom` iff there exists `x` such that `r x y`. * `Rel.comp`: Relation composition. Note that the arguments order follows the `CategoryTheory/` one, so `r.comp s x z ↔ ∃ y, r x y ∧ s y z`. * `Rel.image`: Image of a set under a relation. `r.image s` is the set of `f x` over all `x ∈ s`. * `Rel.preimage`: Preimage of a set under a relation. Note that `r.preimage = r.inv.image`. * `Rel.core`: Core of a set. For `s : Set β`, `r.core s` is the set of `x : α` such that all `y` related to `x` are in `s`. * `Rel.restrict_domain`: Domain-restriction of a relation to a subtype. * `Function.graph`: Graph of a function as a relation. ## TODO The `Rel.comp` function uses the notation `r • s`, rather than the more common `r ∘ s` for things named `comp`. This is because the latter is already used for function composition, and causes a clash. A better notation should be found, perhaps a variant of `r ∘r s` or `r; s`. -/ variable {α β γ : Type*} /-- A relation on `α` and `β`, aka a set-valued function, aka a partial multifunction -/ def Rel (α β : Type*) := α → β → Prop -- The `CompleteLattice, Inhabited` instances should be constructed by a deriving handler. -- https://github.com/leanprover-community/mathlib4/issues/380 instance : CompleteLattice (Rel α β) := show CompleteLattice (α → β → Prop) from inferInstance instance : Inhabited (Rel α β) := show Inhabited (α → β → Prop) from inferInstance namespace Rel variable (r : Rel α β) @[ext] theorem ext {r s : Rel α β} : (∀ a, r a = s a) → r = s := funext /-- The inverse relation : `r.inv x y ↔ r y x`. Note that this is *not* a groupoid inverse. -/ def inv : Rel β α := flip r theorem inv_def (x : α) (y : β) : r.inv y x ↔ r x y := Iff.rfl theorem inv_inv : inv (inv r) = r := by ext x y rfl /-- Domain of a relation -/ def dom := { x | ∃ y, r x y } theorem dom_mono {r s : Rel α β} (h : r ≤ s) : dom r ⊆ dom s := fun a ⟨b, hx⟩ => ⟨b, h a b hx⟩ /-- Codomain aka range of a relation -/ def codom := { y | ∃ x, r x y } theorem codom_inv : r.inv.codom = r.dom := by ext x rfl theorem dom_inv : r.inv.dom = r.codom := by ext x rfl /-- Composition of relation; note that it follows the `CategoryTheory/` order of arguments. -/ def comp (r : Rel α β) (s : Rel β γ) : Rel α γ := fun x z => ∃ y, r x y ∧ s y z /-- Local syntax for composition of relations. -/ -- TODO: this could be replaced with `local infixr:90 " ∘ " => Rel.comp`. local infixr:90 " • " => Rel.comp theorem comp_assoc {δ : Type*} (r : Rel α β) (s : Rel β γ) (t : Rel γ δ) : (r • s) • t = r • (s • t) := by unfold comp; ext (x w); constructor · rintro ⟨z, ⟨y, rxy, syz⟩, tzw⟩; exact ⟨y, rxy, z, syz, tzw⟩ · rintro ⟨y, rxy, z, syz, tzw⟩; exact ⟨z, ⟨y, rxy, syz⟩, tzw⟩ @[simp] theorem comp_right_id (r : Rel α β) : r • @Eq β = r := by unfold comp ext y simp @[simp] theorem comp_left_id (r : Rel α β) : @Eq α • r = r := by unfold comp ext x simp @[simp] theorem comp_right_bot (r : Rel α β) : r • (⊥ : Rel β γ) = ⊥ := by ext x y simp [comp, Bot.bot] @[simp] theorem comp_left_bot (r : Rel α β) : (⊥ : Rel γ α) • r = ⊥ := by ext x y simp [comp, Bot.bot] @[simp] theorem comp_right_top (r : Rel α β) : r • (⊤ : Rel β γ) = fun x _ ↦ x ∈ r.dom := by ext x z simp [comp, Top.top, dom] @[simp] theorem comp_left_top (r : Rel α β) : (⊤ : Rel γ α) • r = fun _ y ↦ y ∈ r.codom := by ext x z simp [comp, Top.top, codom] theorem inv_id : inv (@Eq α) = @Eq α := by ext x y constructor <;> apply Eq.symm theorem inv_comp (r : Rel α β) (s : Rel β γ) : inv (r • s) = inv s • inv r := by ext x z simp [comp, inv, flip, and_comm] @[simp] theorem inv_bot : (⊥ : Rel α β).inv = (⊥ : Rel β α) := by simp [Bot.bot, inv, Function.flip_def] @[simp] theorem inv_top : (⊤ : Rel α β).inv = (⊤ : Rel β α) := by simp [Top.top, inv, Function.flip_def] /-- Image of a set under a relation -/ def image (s : Set α) : Set β := { y | ∃ x ∈ s, r x y } theorem mem_image (y : β) (s : Set α) : y ∈ image r s ↔ ∃ x ∈ s, r x y := Iff.rfl open scoped Relator in theorem image_subset : ((· ⊆ ·) ⇒ (· ⊆ ·)) r.image r.image := fun _ _ h _ ⟨x, xs, rxy⟩ => ⟨x, h xs, rxy⟩ theorem image_mono : Monotone r.image := r.image_subset theorem image_inter (s t : Set α) : r.image (s ∩ t) ⊆ r.image s ∩ r.image t := r.image_mono.map_inf_le s t theorem image_union (s t : Set α) : r.image (s ∪ t) = r.image s ∪ r.image t := le_antisymm (fun _y ⟨x, xst, rxy⟩ => xst.elim (fun xs => Or.inl ⟨x, ⟨xs, rxy⟩⟩) fun xt => Or.inr ⟨x, ⟨xt, rxy⟩⟩) (r.image_mono.le_map_sup s t) @[simp] theorem image_id (s : Set α) : image (@Eq α) s = s := by ext x simp [mem_image] theorem image_comp (s : Rel β γ) (t : Set α) : image (r • s) t = image s (image r t) := by ext z; simp only [mem_image]; constructor · rintro ⟨x, xt, y, rxy, syz⟩; exact ⟨y, ⟨x, xt, rxy⟩, syz⟩ · rintro ⟨y, ⟨x, xt, rxy⟩, syz⟩; exact ⟨x, xt, y, rxy, syz⟩ theorem image_univ : r.image Set.univ = r.codom := by ext y simp [mem_image, codom] @[simp] theorem image_empty : r.image ∅ = ∅ := by ext x simp [mem_image] @[simp] theorem image_bot (s : Set α) : (⊥ : Rel α β).image s = ∅ := by rw [Set.eq_empty_iff_forall_not_mem] intro x h simp [mem_image, Bot.bot] at h @[simp] theorem image_top {s : Set α} (h : Set.Nonempty s) : (⊤ : Rel α β).image s = Set.univ := Set.eq_univ_of_forall fun _ ↦ ⟨h.some, by simp [h.some_mem, Top.top]⟩ /-- Preimage of a set under a relation `r`. Same as the image of `s` under `r.inv` -/ def preimage (s : Set β) : Set α := r.inv.image s theorem mem_preimage (x : α) (s : Set β) : x ∈ r.preimage s ↔ ∃ y ∈ s, r x y := Iff.rfl theorem preimage_def (s : Set β) : preimage r s = { x | ∃ y ∈ s, r x y } := Set.ext fun _ => mem_preimage _ _ _ theorem preimage_mono {s t : Set β} (h : s ⊆ t) : r.preimage s ⊆ r.preimage t := image_mono _ h theorem preimage_inter (s t : Set β) : r.preimage (s ∩ t) ⊆ r.preimage s ∩ r.preimage t := image_inter _ s t theorem preimage_union (s t : Set β) : r.preimage (s ∪ t) = r.preimage s ∪ r.preimage t := image_union _ s t theorem preimage_id (s : Set α) : preimage (@Eq α) s = s := by simp only [preimage, inv_id, image_id] theorem preimage_comp (s : Rel β γ) (t : Set γ) : preimage (r • s) t = preimage r (preimage s t) := by simp only [preimage, inv_comp, image_comp] theorem preimage_univ : r.preimage Set.univ = r.dom := by rw [preimage, image_univ, codom_inv] @[simp] theorem preimage_empty : r.preimage ∅ = ∅ := by rw [preimage, image_empty] @[simp] theorem preimage_inv (s : Set α) : r.inv.preimage s = r.image s := by rw [preimage, inv_inv] @[simp] theorem preimage_bot (s : Set β) : (⊥ : Rel α β).preimage s = ∅ := by rw [preimage, inv_bot, image_bot] @[simp] theorem preimage_top {s : Set β} (h : Set.Nonempty s) : (⊤ : Rel α β).preimage s = Set.univ := by rwa [← inv_top, preimage, inv_inv, image_top] theorem image_eq_dom_of_codomain_subset {s : Set β} (h : r.codom ⊆ s) : r.preimage s = r.dom := by rw [← preimage_univ] apply Set.eq_of_subset_of_subset · exact image_subset _ (Set.subset_univ _) · intro x hx simp only [mem_preimage, Set.mem_univ, true_and] at hx rcases hx with ⟨y, ryx⟩ have hy : y ∈ s := h ⟨x, ryx⟩ exact ⟨y, ⟨hy, ryx⟩⟩ theorem preimage_eq_codom_of_domain_subset {s : Set α} (h : r.dom ⊆ s) : r.image s = r.codom := by apply r.inv.image_eq_dom_of_codomain_subset (by rwa [← codom_inv] at h) theorem image_inter_dom_eq (s : Set α) : r.image (s ∩ r.dom) = r.image s := by apply Set.eq_of_subset_of_subset · apply r.image_mono (by simp) · intro x h rw [mem_image] at * rcases h with ⟨y, hy, ryx⟩ use y suffices h : y ∈ r.dom by simp_all only [Set.mem_inter_iff, and_self] rw [dom, Set.mem_setOf_eq] use x @[simp] theorem preimage_inter_codom_eq (s : Set β) : r.preimage (s ∩ r.codom) = r.preimage s := by rw [← dom_inv, preimage, preimage, image_inter_dom_eq] theorem inter_dom_subset_preimage_image (s : Set α) : s ∩ r.dom ⊆ r.preimage (r.image s) := by intro x hx simp only [Set.mem_inter_iff, dom] at hx rcases hx with ⟨hx, ⟨y, rxy⟩⟩ use y simp only [image, Set.mem_setOf_eq] exact ⟨⟨x, hx, rxy⟩, rxy⟩ theorem image_preimage_subset_inter_codom (s : Set β) : s ∩ r.codom ⊆ r.image (r.preimage s) := by rw [← dom_inv, ← preimage_inv] apply inter_dom_subset_preimage_image /-- Core of a set `s : Set β` w.r.t `r : Rel α β` is the set of `x : α` that are related *only* to elements of `s`. Other generalization of `Function.preimage`. -/ def core (s : Set β) := { x | ∀ y, r x y → y ∈ s } theorem mem_core (x : α) (s : Set β) : x ∈ r.core s ↔ ∀ y, r x y → y ∈ s := Iff.rfl open scoped Relator in theorem core_subset : ((· ⊆ ·) ⇒ (· ⊆ ·)) r.core r.core := fun _s _t h _x h' y rxy => h (h' y rxy) theorem core_mono : Monotone r.core := r.core_subset theorem core_inter (s t : Set β) : r.core (s ∩ t) = r.core s ∩ r.core t := Set.ext (by simp [mem_core, imp_and, forall_and]) theorem core_union (s t : Set β) : r.core s ∪ r.core t ⊆ r.core (s ∪ t) := r.core_mono.le_map_sup s t @[simp] theorem core_univ : r.core Set.univ = Set.univ := Set.ext (by simp [mem_core]) theorem core_id (s : Set α) : core (@Eq α) s = s := by simp [core] theorem core_comp (s : Rel β γ) (t : Set γ) : core (r • s) t = core r (core s t) := by ext x; simp only [core, comp, forall_exists_index, and_imp, Set.mem_setOf_eq]; constructor · exact fun h y rxy z => h z y rxy · exact fun h z y rzy => h y rzy z /-- Restrict the domain of a relation to a subtype. -/ def restrictDomain (s : Set α) : Rel { x // x ∈ s } β := fun x y => r x.val y theorem image_subset_iff (s : Set α) (t : Set β) : image r s ⊆ t ↔ s ⊆ core r t := Iff.intro (fun h x xs _y rxy => h ⟨x, xs, rxy⟩) fun h y ⟨_x, xs, rxy⟩ => h xs y rxy theorem image_core_gc : GaloisConnection r.image r.core := image_subset_iff _ end Rel namespace Function /-- The graph of a function as a relation. -/ def graph (f : α → β) : Rel α β := fun x y => f x = y @[simp] lemma graph_def (f : α → β) (x y) : f.graph x y ↔ (f x = y) := Iff.rfl theorem graph_injective : Injective (graph : (α → β) → Rel α β) := by intro _ g h ext x have h2 := congr_fun₂ h x (g x) simp only [graph_def, eq_iff_iff, iff_true] at h2 exact h2 @[simp] lemma graph_inj {f g : α → β} : f.graph = g.graph ↔ f = g := graph_injective.eq_iff theorem graph_id : graph id = @Eq α := by simp +unfoldPartialApp [graph] theorem graph_comp {f : β → γ} {g : α → β} : graph (f ∘ g) = Rel.comp (graph g) (graph f) := by ext x y simp [Rel.comp] end Function theorem Equiv.graph_inv (f : α ≃ β) : (f.symm : β → α).graph = Rel.inv (f : α → β).graph := by ext x y aesop (add norm Rel.inv_def) theorem Relation.is_graph_iff (r : Rel α β) : (∃! f, Function.graph f = r) ↔ ∀ x, ∃! y, r x y := by unfold Function.graph constructor · rintro ⟨f, rfl, _⟩ x use f x simp only [forall_eq', and_self] · intro h choose f hf using fun x ↦ (h x).exists use f constructor · ext x _ constructor · rintro rfl exact hf x · exact (h x).unique (hf x) · rintro _ rfl exact funext hf namespace Set theorem image_eq (f : α → β) (s : Set α) : f '' s = (Function.graph f).image s := by rfl theorem preimage_eq (f : α → β) (s : Set β) : f ⁻¹' s = (Function.graph f).preimage s := by simp [Set.preimage, Rel.preimage, Rel.inv, flip, Rel.image] theorem preimage_eq_core (f : α → β) (s : Set β) : f ⁻¹' s = (Function.graph f).core s := by simp [Set.preimage, Rel.core] end Set
Mathlib/Data/Rel.lean
416
417
/- Copyright (c) 2023 Dagur Asgeirsson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Dagur Asgeirsson -/ import Mathlib.Topology.Category.Profinite.Nobeling.Basic import Mathlib.Topology.Category.Profinite.Nobeling.Induction import Mathlib.Topology.Category.Profinite.Nobeling.Span import Mathlib.Topology.Category.Profinite.Nobeling.Successor import Mathlib.Topology.Category.Profinite.Nobeling.ZeroLimit deprecated_module (since := "2025-04-13")
Mathlib/Topology/Category/Profinite/Nobeling.lean
1,272
1,284
/- Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Sébastien Gouëzel, Rémy Degenne -/ import Mathlib.Algebra.BigOperators.Expect import Mathlib.Algebra.BigOperators.Field import Mathlib.Analysis.Convex.Jensen import Mathlib.Analysis.Convex.SpecificFunctions.Basic import Mathlib.Analysis.SpecialFunctions.Pow.NNReal import Mathlib.Data.Real.ConjExponents /-! # Mean value inequalities In this file we prove several inequalities for finite sums, including AM-GM inequality, HM-GM inequality, Young's inequality, Hölder inequality, and Minkowski inequality. Versions for integrals of some of these inequalities are available in `Mathlib.MeasureTheory.Integral.MeanInequalities`. ## Main theorems ### AM-GM inequality: The inequality says that the geometric mean of a tuple of non-negative numbers is less than or equal to their arithmetic mean. We prove the weighted version of this inequality: if $w$ and $z$ are two non-negative vectors and $\sum_{i\in s} w_i=1$, then $$ \prod_{i\in s} z_i^{w_i} ≤ \sum_{i\in s} w_iz_i. $$ The classical version is a special case of this inequality for $w_i=\frac{1}{n}$. We prove a few versions of this inequality. Each of the following lemmas comes in two versions: a version for real-valued non-negative functions is in the `Real` namespace, and a version for `NNReal`-valued functions is in the `NNReal` namespace. - `geom_mean_le_arith_mean_weighted` : weighted version for functions on `Finset`s; - `geom_mean_le_arith_mean2_weighted` : weighted version for two numbers; - `geom_mean_le_arith_mean3_weighted` : weighted version for three numbers; - `geom_mean_le_arith_mean4_weighted` : weighted version for four numbers. ### HM-GM inequality: The inequality says that the harmonic mean of a tuple of positive numbers is less than or equal to their geometric mean. We prove the weighted version of this inequality: if $w$ and $z$ are two positive vectors and $\sum_{i\in s} w_i=1$, then $$ 1/(\sum_{i\in s} w_i/z_i) ≤ \prod_{i\in s} z_i^{w_i} $$ The classical version is proven as a special case of this inequality for $w_i=\frac{1}{n}$. The inequalities are proven only for real valued positive functions on `Finset`s, and namespaced in `Real`. The weighted version follows as a corollary of the weighted AM-GM inequality. ### Young's inequality Young's inequality says that for non-negative numbers `a`, `b`, `p`, `q` such that $\frac{1}{p}+\frac{1}{q}=1$ we have $$ ab ≤ \frac{a^p}{p} + \frac{b^q}{q}. $$ This inequality is a special case of the AM-GM inequality. It is then used to prove Hölder's inequality (see below). ### Hölder's inequality The inequality says that for two conjugate exponents `p` and `q` (i.e., for two positive numbers such that $\frac{1}{p}+\frac{1}{q}=1$) and any two non-negative vectors their inner product is less than or equal to the product of the $L_p$ norm of the first vector and the $L_q$ norm of the second vector: $$ \sum_{i\in s} a_ib_i ≤ \sqrt[p]{\sum_{i\in s} a_i^p}\sqrt[q]{\sum_{i\in s} b_i^q}. $$ We give versions of this result in `ℝ`, `ℝ≥0` and `ℝ≥0∞`. There are at least two short proofs of this inequality. In our proof we prenormalize both vectors, then apply Young's inequality to each $a_ib_i$. Another possible proof would be to deduce this inequality from the generalized mean inequality for well-chosen vectors and weights. ### Minkowski's inequality The inequality says that for `p ≥ 1` the function $$ \|a\|_p=\sqrt[p]{\sum_{i\in s} a_i^p} $$ satisfies the triangle inequality $\|a+b\|_p\le \|a\|_p+\|b\|_p$. We give versions of this result in `Real`, `ℝ≥0` and `ℝ≥0∞`. We deduce this inequality from Hölder's inequality. Namely, Hölder inequality implies that $\|a\|_p$ is the maximum of the inner product $\sum_{i\in s}a_ib_i$ over `b` such that $\|b\|_q\le 1$. Now Minkowski's inequality follows from the fact that the maximum value of the sum of two functions is less than or equal to the sum of the maximum values of the summands. ## TODO - each inequality `A ≤ B` should come with a theorem `A = B ↔ _`; one of the ways to prove them is to define `StrictConvexOn` functions. - generalized mean inequality with any `p ≤ q`, including negative numbers; - prove that the power mean tends to the geometric mean as the exponent tends to zero. -/ universe u v open Finset NNReal ENNReal open scoped BigOperators noncomputable section variable {ι : Type u} (s : Finset ι) section GeomMeanLEArithMean /-! ### AM-GM inequality -/ namespace Real /-- **AM-GM inequality**: The geometric mean is less than or equal to the arithmetic mean, weighted version for real-valued nonnegative functions. -/ theorem geom_mean_le_arith_mean_weighted (w z : ι → ℝ) (hw : ∀ i ∈ s, 0 ≤ w i) (hw' : ∑ i ∈ s, w i = 1) (hz : ∀ i ∈ s, 0 ≤ z i) : ∏ i ∈ s, z i ^ w i ≤ ∑ i ∈ s, w i * z i := by -- If some number `z i` equals zero and has non-zero weight, then LHS is 0 and RHS is nonnegative. by_cases A : ∃ i ∈ s, z i = 0 ∧ w i ≠ 0 · rcases A with ⟨i, his, hzi, hwi⟩ rw [prod_eq_zero his] · exact sum_nonneg fun j hj => mul_nonneg (hw j hj) (hz j hj) · rw [hzi] exact zero_rpow hwi -- If all numbers `z i` with non-zero weight are positive, then we apply Jensen's inequality -- for `exp` and numbers `log (z i)` with weights `w i`. · simp only [not_exists, not_and, Ne, Classical.not_not] at A have := convexOn_exp.map_sum_le hw hw' fun i _ => Set.mem_univ <| log (z i) simp only [exp_sum, smul_eq_mul, mul_comm (w _) (log _)] at this convert this using 1 <;> [apply prod_congr rfl;apply sum_congr rfl] <;> intro i hi · rcases eq_or_lt_of_le (hz i hi) with hz | hz · simp [A i hi hz.symm] · exact rpow_def_of_pos hz _ · rcases eq_or_lt_of_le (hz i hi) with hz | hz · simp [A i hi hz.symm] · rw [exp_log hz] /-- **AM-GM inequality**: The **geometric mean is less than or equal to the arithmetic mean. -/ theorem geom_mean_le_arith_mean {ι : Type*} (s : Finset ι) (w : ι → ℝ) (z : ι → ℝ) (hw : ∀ i ∈ s, 0 ≤ w i) (hw' : 0 < ∑ i ∈ s, w i) (hz : ∀ i ∈ s, 0 ≤ z i) : (∏ i ∈ s, z i ^ w i) ^ (∑ i ∈ s, w i)⁻¹ ≤ (∑ i ∈ s, w i * z i) / (∑ i ∈ s, w i) := by convert geom_mean_le_arith_mean_weighted s (fun i => (w i) / ∑ i ∈ s, w i) z ?_ ?_ hz using 2 · rw [← finset_prod_rpow _ _ (fun i hi => rpow_nonneg (hz _ hi) _) _] refine Finset.prod_congr rfl (fun _ ih => ?_) rw [div_eq_mul_inv, rpow_mul (hz _ ih)] · simp_rw [div_eq_mul_inv, mul_assoc, mul_comm, ← mul_assoc, ← Finset.sum_mul, mul_comm] · exact fun _ hi => div_nonneg (hw _ hi) (le_of_lt hw') · simp_rw [div_eq_mul_inv, ← Finset.sum_mul] exact mul_inv_cancel₀ (by linarith) theorem geom_mean_weighted_of_constant (w z : ι → ℝ) (x : ℝ) (hw : ∀ i ∈ s, 0 ≤ w i) (hw' : ∑ i ∈ s, w i = 1) (hz : ∀ i ∈ s, 0 ≤ z i) (hx : ∀ i ∈ s, w i ≠ 0 → z i = x) : ∏ i ∈ s, z i ^ w i = x := calc ∏ i ∈ s, z i ^ w i = ∏ i ∈ s, x ^ w i := by refine prod_congr rfl fun i hi => ?_ rcases eq_or_ne (w i) 0 with h₀ | h₀ · rw [h₀, rpow_zero, rpow_zero] · rw [hx i hi h₀] _ = x := by rw [← rpow_sum_of_nonneg _ hw, hw', rpow_one] have : (∑ i ∈ s, w i) ≠ 0 := by rw [hw'] exact one_ne_zero obtain ⟨i, his, hi⟩ := exists_ne_zero_of_sum_ne_zero this rw [← hx i his hi] exact hz i his theorem arith_mean_weighted_of_constant (w z : ι → ℝ) (x : ℝ) (hw' : ∑ i ∈ s, w i = 1) (hx : ∀ i ∈ s, w i ≠ 0 → z i = x) : ∑ i ∈ s, w i * z i = x := calc ∑ i ∈ s, w i * z i = ∑ i ∈ s, w i * x := by refine sum_congr rfl fun i hi => ?_ rcases eq_or_ne (w i) 0 with hwi | hwi · rw [hwi, zero_mul, zero_mul] · rw [hx i hi hwi] _ = x := by rw [← sum_mul, hw', one_mul] theorem geom_mean_eq_arith_mean_weighted_of_constant (w z : ι → ℝ) (x : ℝ) (hw : ∀ i ∈ s, 0 ≤ w i) (hw' : ∑ i ∈ s, w i = 1) (hz : ∀ i ∈ s, 0 ≤ z i) (hx : ∀ i ∈ s, w i ≠ 0 → z i = x) : ∏ i ∈ s, z i ^ w i = ∑ i ∈ s, w i * z i := by rw [geom_mean_weighted_of_constant, arith_mean_weighted_of_constant] <;> assumption /-- **AM-GM inequality - equality condition**: This theorem provides the equality condition for the *positive* weighted version of the AM-GM inequality for real-valued nonnegative functions. -/ theorem geom_mean_eq_arith_mean_weighted_iff' (w z : ι → ℝ) (hw : ∀ i ∈ s, 0 < w i) (hw' : ∑ i ∈ s, w i = 1) (hz : ∀ i ∈ s, 0 ≤ z i) : ∏ i ∈ s, z i ^ w i = ∑ i ∈ s, w i * z i ↔ ∀ j ∈ s, z j = ∑ i ∈ s, w i * z i := by by_cases A : ∃ i ∈ s, z i = 0 ∧ w i ≠ 0 · rcases A with ⟨i, his, hzi, hwi⟩ rw [prod_eq_zero his] · constructor · intro h rw [← h] intro j hj apply eq_zero_of_ne_zero_of_mul_left_eq_zero (ne_of_lt (hw j hj)).symm apply (sum_eq_zero_iff_of_nonneg ?_).mp h.symm j hj exact fun i hi => (mul_nonneg_iff_of_pos_left (hw i hi)).mpr (hz i hi) · intro h convert h i his exact hzi.symm · rw [hzi] exact zero_rpow hwi
· simp only [not_exists, not_and] at A have hz' := fun i h => lt_of_le_of_ne (hz i h) (fun a => (A i h a.symm) (ne_of_gt (hw i h))) have := strictConvexOn_exp.map_sum_eq_iff hw hw' fun i _ => Set.mem_univ <| log (z i) simp only [exp_sum, smul_eq_mul, mul_comm (w _) (log _)] at this convert this using 1
Mathlib/Analysis/MeanInequalities.lean
215
219
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Data.Multiset.UnionInter /-! # `Multiset.range n` gives `{0, 1, ..., n-1}` as a multiset. -/ assert_not_exists Monoid open List Nat namespace Multiset -- range /-- `range n` is the multiset lifted from the list `range n`, that is, the set `{0, 1, ..., n-1}`. -/ def range (n : ℕ) : Multiset ℕ := List.range n theorem coe_range (n : ℕ) : ↑(List.range n) = range n := rfl @[simp] theorem range_zero : range 0 = 0 := rfl @[simp] theorem range_succ (n : ℕ) : range (succ n) = n ::ₘ range n := by rw [range, List.range_succ, ← coe_add, Multiset.add_comm, range, coe_singleton, singleton_add] @[simp] theorem card_range (n : ℕ) : card (range n) = n := length_range theorem range_subset {m n : ℕ} : range m ⊆ range n ↔ m ≤ n := List.range_subset @[simp] theorem mem_range {m n : ℕ} : m ∈ range n ↔ m < n := List.mem_range theorem not_mem_range_self {n : ℕ} : n ∉ range n := List.not_mem_range_self theorem self_mem_range_succ (n : ℕ) : n ∈ range (n + 1) := List.self_mem_range_succ theorem range_add (a b : ℕ) : range (a + b) = range a + (range b).map (a + ·) := congr_arg ((↑) : List ℕ → Multiset ℕ) List.range_add theorem range_disjoint_map_add (a : ℕ) (m : Multiset ℕ) : Disjoint (range a) (m.map (a + ·)) := by rw [disjoint_left] intro x hxa hxb rw [range, mem_coe, List.mem_range] at hxa obtain ⟨c, _, rfl⟩ := mem_map.1 hxb exact (Nat.le_add_right _ _).not_lt hxa theorem range_add_eq_union (a b : ℕ) : range (a + b) = range a ∪ (range b).map (a + ·) := by rw [range_add, add_eq_union_iff_disjoint] apply range_disjoint_map_add section Nodup theorem nodup_range (n : ℕ) : Nodup (range n) := List.nodup_range theorem range_le {m n : ℕ} : range m ≤ range n ↔ m ≤ n := (le_iff_subset (nodup_range _)).trans range_subset
end Nodup end Multiset
Mathlib/Data/Multiset/Range.lean
73
75
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Sébastien Gouëzel, Patrick Massot -/ import Mathlib.Topology.UniformSpace.Cauchy import Mathlib.Topology.UniformSpace.Separation import Mathlib.Topology.DenseEmbedding /-! # Uniform embeddings of uniform spaces. Extension of uniform continuous functions. -/ open Filter Function Set Uniformity Topology section universe u v w variable {α : Type u} {β : Type v} {γ : Type w} [UniformSpace α] [UniformSpace β] [UniformSpace γ] {f : α → β} /-! ### Uniform inducing maps -/ /-- A map `f : α → β` between uniform spaces is called *uniform inducing* if the uniformity filter on `α` is the pullback of the uniformity filter on `β` under `Prod.map f f`. If `α` is a separated space, then this implies that `f` is injective, hence it is a `IsUniformEmbedding`. -/ @[mk_iff] structure IsUniformInducing (f : α → β) : Prop where /-- The uniformity filter on the domain is the pullback of the uniformity filter on the codomain under `Prod.map f f`. -/ comap_uniformity : comap (fun x : α × α => (f x.1, f x.2)) (𝓤 β) = 𝓤 α lemma isUniformInducing_iff_uniformSpace {f : α → β} : IsUniformInducing f ↔ ‹UniformSpace β›.comap f = ‹UniformSpace α› := by rw [isUniformInducing_iff, UniformSpace.ext_iff, Filter.ext_iff] rfl protected alias ⟨IsUniformInducing.comap_uniformSpace, _⟩ := isUniformInducing_iff_uniformSpace lemma isUniformInducing_iff' {f : α → β} : IsUniformInducing f ↔ UniformContinuous f ∧ comap (Prod.map f f) (𝓤 β) ≤ 𝓤 α := by rw [isUniformInducing_iff, UniformContinuous, tendsto_iff_comap, le_antisymm_iff, and_comm]; rfl protected lemma Filter.HasBasis.isUniformInducing_iff {ι ι'} {p : ι → Prop} {p' : ι' → Prop} {s s'} (h : (𝓤 α).HasBasis p s) (h' : (𝓤 β).HasBasis p' s') {f : α → β} : IsUniformInducing f ↔ (∀ i, p' i → ∃ j, p j ∧ ∀ x y, (x, y) ∈ s j → (f x, f y) ∈ s' i) ∧ (∀ j, p j → ∃ i, p' i ∧ ∀ x y, (f x, f y) ∈ s' i → (x, y) ∈ s j) := by simp [isUniformInducing_iff', h.uniformContinuous_iff h', (h'.comap _).le_basis_iff h, subset_def] theorem IsUniformInducing.mk' {f : α → β} (h : ∀ s, s ∈ 𝓤 α ↔ ∃ t ∈ 𝓤 β, ∀ x y : α, (f x, f y) ∈ t → (x, y) ∈ s) : IsUniformInducing f := ⟨by simp [eq_comm, Filter.ext_iff, subset_def, h]⟩ theorem IsUniformInducing.id : IsUniformInducing (@id α) := ⟨by rw [← Prod.map_def, Prod.map_id, comap_id]⟩ theorem IsUniformInducing.comp {g : β → γ} (hg : IsUniformInducing g) {f : α → β} (hf : IsUniformInducing f) : IsUniformInducing (g ∘ f) := ⟨by rw [← hf.1, ← hg.1, comap_comap]; rfl⟩ theorem IsUniformInducing.of_comp_iff {g : β → γ} (hg : IsUniformInducing g) {f : α → β} : IsUniformInducing (g ∘ f) ↔ IsUniformInducing f := by refine ⟨fun h ↦ ?_, hg.comp⟩ rw [isUniformInducing_iff, ← hg.comap_uniformity, comap_comap, ← h.comap_uniformity, Function.comp_def, Function.comp_def] theorem IsUniformInducing.basis_uniformity {f : α → β} (hf : IsUniformInducing f) {ι : Sort*} {p : ι → Prop} {s : ι → Set (β × β)} (H : (𝓤 β).HasBasis p s) : (𝓤 α).HasBasis p fun i => Prod.map f f ⁻¹' s i := hf.1 ▸ H.comap _ theorem IsUniformInducing.cauchy_map_iff {f : α → β} (hf : IsUniformInducing f) {F : Filter α} : Cauchy (map f F) ↔ Cauchy F := by simp only [Cauchy, map_neBot_iff, prod_map_map_eq, map_le_iff_le_comap, ← hf.comap_uniformity] theorem IsUniformInducing.of_comp {f : α → β} {g : β → γ} (hf : UniformContinuous f) (hg : UniformContinuous g) (hgf : IsUniformInducing (g ∘ f)) : IsUniformInducing f := by refine ⟨le_antisymm ?_ hf.le_comap⟩ rw [← hgf.1, ← Prod.map_def, ← Prod.map_def, ← Prod.map_comp_map f f g g, ← comap_comap] exact comap_mono hg.le_comap theorem IsUniformInducing.uniformContinuous {f : α → β} (hf : IsUniformInducing f) : UniformContinuous f := (isUniformInducing_iff'.1 hf).1 theorem IsUniformInducing.uniformContinuous_iff {f : α → β} {g : β → γ} (hg : IsUniformInducing g) : UniformContinuous f ↔ UniformContinuous (g ∘ f) := by dsimp only [UniformContinuous, Tendsto] simp only [← hg.comap_uniformity, ← map_le_iff_le_comap, Filter.map_map, Function.comp_def] protected theorem IsUniformInducing.isUniformInducing_comp_iff {f : α → β} {g : β → γ} (hg : IsUniformInducing g) : IsUniformInducing (g ∘ f) ↔ IsUniformInducing f := by simp only [isUniformInducing_iff, ← hg.comap_uniformity, comap_comap, Function.comp_def] theorem IsUniformInducing.uniformContinuousOn_iff {f : α → β} {g : β → γ} {S : Set α} (hg : IsUniformInducing g) : UniformContinuousOn f S ↔ UniformContinuousOn (g ∘ f) S := by dsimp only [UniformContinuousOn, Tendsto] rw [← hg.comap_uniformity, ← map_le_iff_le_comap, Filter.map_map, comp_def, comp_def] theorem IsUniformInducing.isInducing {f : α → β} (h : IsUniformInducing f) : IsInducing f := by obtain rfl := h.comap_uniformSpace exact .induced f @[deprecated (since := "2024-10-28")] alias IsUniformInducing.inducing := IsUniformInducing.isInducing @[deprecated (since := "2024-10-28")] alias UniformInducing.inducing := IsUniformInducing.isInducing theorem IsUniformInducing.prod {α' : Type*} {β' : Type*} [UniformSpace α'] [UniformSpace β'] {e₁ : α → α'} {e₂ : β → β'} (h₁ : IsUniformInducing e₁) (h₂ : IsUniformInducing e₂) : IsUniformInducing fun p : α × β => (e₁ p.1, e₂ p.2) := ⟨by simp [Function.comp_def, uniformity_prod, ← h₁.1, ← h₂.1, comap_inf, comap_comap]⟩ lemma IsUniformInducing.isDenseInducing (h : IsUniformInducing f) (hd : DenseRange f) : IsDenseInducing f where toIsInducing := h.isInducing dense := hd lemma SeparationQuotient.isUniformInducing_mk : IsUniformInducing (mk : α → SeparationQuotient α) := ⟨comap_mk_uniformity⟩ protected theorem IsUniformInducing.injective [T0Space α] {f : α → β} (h : IsUniformInducing f) : Injective f := h.isInducing.injective /-! ### Uniform embeddings -/ /-- A map `f : α → β` between uniform spaces is a *uniform embedding* if it is uniform inducing and injective. If `α` is a separated space, then the latter assumption follows from the former. -/ @[mk_iff] structure IsUniformEmbedding (f : α → β) : Prop extends IsUniformInducing f where /-- A uniform embedding is injective. -/ injective : Function.Injective f lemma IsUniformEmbedding.isUniformInducing (hf : IsUniformEmbedding f) : IsUniformInducing f := hf.toIsUniformInducing theorem isUniformEmbedding_iff' {f : α → β} : IsUniformEmbedding f ↔ Injective f ∧ UniformContinuous f ∧ comap (Prod.map f f) (𝓤 β) ≤ 𝓤 α := by rw [isUniformEmbedding_iff, and_comm, isUniformInducing_iff'] theorem Filter.HasBasis.isUniformEmbedding_iff' {ι ι'} {p : ι → Prop} {p' : ι' → Prop} {s s'} (h : (𝓤 α).HasBasis p s) (h' : (𝓤 β).HasBasis p' s') {f : α → β} : IsUniformEmbedding f ↔ Injective f ∧ (∀ i, p' i → ∃ j, p j ∧ ∀ x y, (x, y) ∈ s j → (f x, f y) ∈ s' i) ∧ (∀ j, p j → ∃ i, p' i ∧ ∀ x y, (f x, f y) ∈ s' i → (x, y) ∈ s j) := by rw [isUniformEmbedding_iff, and_comm, h.isUniformInducing_iff h'] theorem Filter.HasBasis.isUniformEmbedding_iff {ι ι'} {p : ι → Prop} {p' : ι' → Prop} {s s'} (h : (𝓤 α).HasBasis p s) (h' : (𝓤 β).HasBasis p' s') {f : α → β} : IsUniformEmbedding f ↔ Injective f ∧ UniformContinuous f ∧ (∀ j, p j → ∃ i, p' i ∧ ∀ x y, (f x, f y) ∈ s' i → (x, y) ∈ s j) := by simp only [h.isUniformEmbedding_iff' h', h.uniformContinuous_iff h'] theorem isUniformEmbedding_subtype_val {p : α → Prop} : IsUniformEmbedding (Subtype.val : Subtype p → α) := { comap_uniformity := rfl injective := Subtype.val_injective } theorem isUniformEmbedding_set_inclusion {s t : Set α} (hst : s ⊆ t) : IsUniformEmbedding (inclusion hst) where comap_uniformity := by rw [uniformity_subtype, uniformity_subtype, comap_comap]; rfl injective := inclusion_injective hst theorem IsUniformEmbedding.comp {g : β → γ} (hg : IsUniformEmbedding g) {f : α → β} (hf : IsUniformEmbedding f) : IsUniformEmbedding (g ∘ f) where toIsUniformInducing := hg.isUniformInducing.comp hf.isUniformInducing injective := hg.injective.comp hf.injective
theorem IsUniformEmbedding.of_comp_iff {g : β → γ} (hg : IsUniformEmbedding g) {f : α → β} : IsUniformEmbedding (g ∘ f) ↔ IsUniformEmbedding f := by simp_rw [isUniformEmbedding_iff, hg.isUniformInducing.of_comp_iff, hg.injective.of_comp_iff f]
Mathlib/Topology/UniformSpace/UniformEmbedding.lean
180
183
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import Mathlib.Algebra.CharP.Reduced import Mathlib.RingTheory.IntegralDomain -- TODO: remove Mathlib.Algebra.CharP.Reduced and move the last two lemmas to Lemmas /-! # Roots of unity We define roots of unity in the context of an arbitrary commutative monoid, as a subgroup of the group of units. ## Main definitions * `rootsOfUnity n M`, for `n : ℕ` is the subgroup of the units of a commutative monoid `M` consisting of elements `x` that satisfy `x ^ n = 1`. ## Main results * `rootsOfUnity.isCyclic`: the roots of unity in an integral domain form a cyclic group. ## Implementation details It is desirable that `rootsOfUnity` is a subgroup, and it will mainly be applied to rings (e.g. the ring of integers in a number field) and fields. We therefore implement it as a subgroup of the units of a commutative monoid. We have chosen to define `rootsOfUnity n` for `n : ℕ` and add a `[NeZero n]` typeclass assumption when we need `n` to be non-zero (which is the case for most interesting statements). Note that `rootsOfUnity 0 M` is the top subgroup of `Mˣ` (as the condition `ζ^0 = 1` is satisfied for all units). -/ noncomputable section open Polynomial open Finset variable {M N G R S F : Type*} variable [CommMonoid M] [CommMonoid N] [DivisionCommMonoid G] section rootsOfUnity variable {k l : ℕ} /-- `rootsOfUnity k M` is the subgroup of elements `m : Mˣ` that satisfy `m ^ k = 1`. -/ def rootsOfUnity (k : ℕ) (M : Type*) [CommMonoid M] : Subgroup Mˣ where carrier := {ζ | ζ ^ k = 1} one_mem' := one_pow _ mul_mem' _ _ := by simp_all only [Set.mem_setOf_eq, mul_pow, one_mul] inv_mem' _ := by simp_all only [Set.mem_setOf_eq, inv_pow, inv_one] @[simp] theorem mem_rootsOfUnity (k : ℕ) (ζ : Mˣ) : ζ ∈ rootsOfUnity k M ↔ ζ ^ k = 1 := Iff.rfl /-- A variant of `mem_rootsOfUnity` using `ζ : Mˣ`. -/ theorem mem_rootsOfUnity' (k : ℕ) (ζ : Mˣ) : ζ ∈ rootsOfUnity k M ↔ (ζ : M) ^ k = 1 := by rw [mem_rootsOfUnity]; norm_cast @[simp] theorem rootsOfUnity_one (M : Type*) [CommMonoid M] : rootsOfUnity 1 M = ⊥ := by ext1 simp only [mem_rootsOfUnity, pow_one, Subgroup.mem_bot] @[simp] lemma rootsOfUnity_zero (M : Type*) [CommMonoid M] : rootsOfUnity 0 M = ⊤ := by ext1 simp only [mem_rootsOfUnity, pow_zero, Subgroup.mem_top] theorem rootsOfUnity.coe_injective {n : ℕ} : Function.Injective (fun x : rootsOfUnity n M ↦ x.val.val) := Units.ext.comp fun _ _ ↦ Subtype.eq /-- Make an element of `rootsOfUnity` from a member of the base ring, and a proof that it has a positive power equal to one. -/ @[simps! coe_val] def rootsOfUnity.mkOfPowEq (ζ : M) {n : ℕ} [NeZero n] (h : ζ ^ n = 1) : rootsOfUnity n M := ⟨Units.ofPowEqOne ζ n h <| NeZero.ne n, Units.pow_ofPowEqOne _ _⟩ @[simp] theorem rootsOfUnity.coe_mkOfPowEq {ζ : M} {n : ℕ} [NeZero n] (h : ζ ^ n = 1) : ((rootsOfUnity.mkOfPowEq _ h : Mˣ) : M) = ζ := rfl theorem rootsOfUnity_le_of_dvd (h : k ∣ l) : rootsOfUnity k M ≤ rootsOfUnity l M := by obtain ⟨d, rfl⟩ := h intro ζ h simp_all only [mem_rootsOfUnity, pow_mul, one_pow] theorem map_rootsOfUnity (f : Mˣ →* Nˣ) (k : ℕ) : (rootsOfUnity k M).map f ≤ rootsOfUnity k N := by rintro _ ⟨ζ, h, rfl⟩ simp_all only [← map_pow, mem_rootsOfUnity, SetLike.mem_coe, MonoidHom.map_one] @[norm_cast] theorem rootsOfUnity.coe_pow [CommMonoid R] (ζ : rootsOfUnity k R) (m : ℕ) : (((ζ ^ m :) : Rˣ) : R) = ((ζ : Rˣ) : R) ^ m := by rw [Subgroup.coe_pow, Units.val_pow_eq_pow_val] /-- The canonical isomorphism from the `n`th roots of unity in `Mˣ` to the `n`th roots of unity in `M`. -/ def rootsOfUnityUnitsMulEquiv (M : Type*) [CommMonoid M] (n : ℕ) : rootsOfUnity n Mˣ ≃* rootsOfUnity n M where toFun ζ := ⟨ζ.val, (mem_rootsOfUnity ..).mpr <| (mem_rootsOfUnity' ..).mp ζ.prop⟩ invFun ζ := ⟨toUnits ζ.val, by simp only [mem_rootsOfUnity, ← map_pow, EmbeddingLike.map_eq_one_iff] exact (mem_rootsOfUnity ..).mp ζ.prop⟩ left_inv ζ := by simp only [toUnits_val_apply, Subtype.coe_eta] right_inv ζ := by simp only [val_toUnits_apply, Subtype.coe_eta] map_mul' ζ ζ' := by simp only [Subgroup.coe_mul, Units.val_mul, MulMemClass.mk_mul_mk] section CommMonoid variable [CommMonoid R] [CommMonoid S] [FunLike F R S] /-- Restrict a ring homomorphism to the nth roots of unity. -/ def restrictRootsOfUnity [MonoidHomClass F R S] (σ : F) (n : ℕ) : rootsOfUnity n R →* rootsOfUnity n S := { toFun := fun ξ ↦ ⟨Units.map σ (ξ : Rˣ), by rw [mem_rootsOfUnity, ← map_pow, Units.ext_iff, Units.coe_map, ξ.prop] exact map_one σ⟩ map_one' := by ext1; simp only [OneMemClass.coe_one, map_one] map_mul' := fun ξ₁ ξ₂ ↦ by ext1; simp only [Subgroup.coe_mul, map_mul, MulMemClass.mk_mul_mk] } @[simp] theorem restrictRootsOfUnity_coe_apply [MonoidHomClass F R S] (σ : F) (ζ : rootsOfUnity k R) : (restrictRootsOfUnity σ k ζ : Sˣ) = σ (ζ : Rˣ) := rfl /-- Restrict a monoid isomorphism to the nth roots of unity. -/ nonrec def MulEquiv.restrictRootsOfUnity (σ : R ≃* S) (n : ℕ) : rootsOfUnity n R ≃* rootsOfUnity n S where toFun := restrictRootsOfUnity σ n invFun := restrictRootsOfUnity σ.symm n left_inv ξ := by ext; exact σ.symm_apply_apply _ right_inv ξ := by ext; exact σ.apply_symm_apply _ map_mul' := (restrictRootsOfUnity _ n).map_mul @[simp] theorem MulEquiv.restrictRootsOfUnity_coe_apply (σ : R ≃* S) (ζ : rootsOfUnity k R) : (σ.restrictRootsOfUnity k ζ : Sˣ) = σ (ζ : Rˣ) := rfl @[simp] theorem MulEquiv.restrictRootsOfUnity_symm (σ : R ≃* S) : (σ.restrictRootsOfUnity k).symm = σ.symm.restrictRootsOfUnity k := rfl end CommMonoid section IsDomain -- The following results need `k` to be nonzero. variable [NeZero k] [CommRing R] [IsDomain R] theorem mem_rootsOfUnity_iff_mem_nthRoots {ζ : Rˣ} : ζ ∈ rootsOfUnity k R ↔ (ζ : R) ∈ nthRoots k (1 : R) := by simp only [mem_rootsOfUnity, mem_nthRoots (NeZero.pos k), Units.ext_iff, Units.val_one, Units.val_pow_eq_pow_val] variable (k R) /-- Equivalence between the `k`-th roots of unity in `R` and the `k`-th roots of `1`. This is implemented as equivalence of subtypes, because `rootsOfUnity` is a subgroup of the group of units, whereas `nthRoots` is a multiset. -/ def rootsOfUnityEquivNthRoots : rootsOfUnity k R ≃ { x // x ∈ nthRoots k (1 : R) } where toFun x := ⟨(x : Rˣ), mem_rootsOfUnity_iff_mem_nthRoots.mp x.2⟩ invFun x := by refine ⟨⟨x, ↑x ^ (k - 1 : ℕ), ?_, ?_⟩, ?_⟩ all_goals rcases x with ⟨x, hx⟩; rw [mem_nthRoots <| NeZero.pos k] at hx simp only [← pow_succ, ← pow_succ', hx, tsub_add_cancel_of_le NeZero.one_le] simp only [mem_rootsOfUnity, Units.ext_iff, Units.val_pow_eq_pow_val, hx, Units.val_one] left_inv := by rintro ⟨x, hx⟩; ext; rfl right_inv := by rintro ⟨x, hx⟩; ext; rfl variable {k R} @[simp] theorem rootsOfUnityEquivNthRoots_apply (x : rootsOfUnity k R) : (rootsOfUnityEquivNthRoots R k x : R) = ((x : Rˣ) : R) := rfl @[simp] theorem rootsOfUnityEquivNthRoots_symm_apply (x : { x // x ∈ nthRoots k (1 : R) }) : (((rootsOfUnityEquivNthRoots R k).symm x : Rˣ) : R) = (x : R) := rfl variable (k R) instance rootsOfUnity.fintype : Fintype (rootsOfUnity k R) := by classical exact Fintype.ofEquiv { x // x ∈ nthRoots k (1 : R) } (rootsOfUnityEquivNthRoots R k).symm instance rootsOfUnity.isCyclic : IsCyclic (rootsOfUnity k R) := isCyclic_of_subgroup_isDomain ((Units.coeHom R).comp (rootsOfUnity k R).subtype) coe_injective theorem card_rootsOfUnity : Fintype.card (rootsOfUnity k R) ≤ k := by classical calc Fintype.card (rootsOfUnity k R) = Fintype.card { x // x ∈ nthRoots k (1 : R) } := Fintype.card_congr (rootsOfUnityEquivNthRoots R k) _ ≤ Multiset.card (nthRoots k (1 : R)).attach := Multiset.card_le_card (Multiset.dedup_le _) _ = Multiset.card (nthRoots k (1 : R)) := Multiset.card_attach _ ≤ k := card_nthRoots k 1 variable {k R} theorem map_rootsOfUnity_eq_pow_self [FunLike F R R] [MonoidHomClass F R R] (σ : F) (ζ : rootsOfUnity k R) : ∃ m : ℕ, σ (ζ : Rˣ) = ((ζ : Rˣ) : R) ^ m := by obtain ⟨m, hm⟩ := MonoidHom.map_cyclic (restrictRootsOfUnity σ k) rw [← restrictRootsOfUnity_coe_apply, hm, ← zpow_mod_orderOf, ← Int.toNat_of_nonneg (m.emod_nonneg (Int.natCast_ne_zero.mpr (pos_iff_ne_zero.mp (orderOf_pos ζ)))), zpow_natCast, rootsOfUnity.coe_pow] exact ⟨(m % orderOf ζ).toNat, rfl⟩ end IsDomain section Reduced variable (R) [CommRing R] [IsReduced R] -- @[simp] -- Porting note: simp normal form is `mem_rootsOfUnity_prime_pow_mul_iff'` theorem mem_rootsOfUnity_prime_pow_mul_iff (p k : ℕ) (m : ℕ) [ExpChar R p] {ζ : Rˣ} : ζ ∈ rootsOfUnity (p ^ k * m) R ↔ ζ ∈ rootsOfUnity m R := by simp only [mem_rootsOfUnity', ExpChar.pow_prime_pow_mul_eq_one_iff] /-- A variant of `mem_rootsOfUnity_prime_pow_mul_iff` in terms of `ζ ^ _` -/ @[simp] theorem mem_rootsOfUnity_prime_pow_mul_iff' (p k : ℕ) (m : ℕ) [ExpChar R p] {ζ : Rˣ} : ζ ^ (p ^ k * m) = 1 ↔ ζ ∈ rootsOfUnity m R := by rw [← mem_rootsOfUnity, mem_rootsOfUnity_prime_pow_mul_iff] end Reduced end rootsOfUnity section cyclic namespace IsCyclic /-- The isomorphism from the group of group homomorphisms from a finite cyclic group `G` of order `n` into another group `G'` to the group of `n`th roots of unity in `G'` determined by a generator `g` of `G`. It sends `φ : G →* G'` to `φ g`. -/ noncomputable def monoidHomMulEquivRootsOfUnityOfGenerator {G : Type*} [CommGroup G] {g : G} (hg : ∀ (x : G), x ∈ Subgroup.zpowers g) (G' : Type*) [CommGroup G'] : (G →* G') ≃* rootsOfUnity (Nat.card G) G' where toFun φ := ⟨(IsUnit.map φ <| Group.isUnit g).unit, by simp only [mem_rootsOfUnity, Units.ext_iff, Units.val_pow_eq_pow_val, IsUnit.unit_spec, ← map_pow, pow_card_eq_one', map_one, Units.val_one]⟩ invFun ζ := monoidHomOfForallMemZpowers hg (g' := (ζ.val : G')) <| by simpa only [orderOf_eq_card_of_forall_mem_zpowers hg, orderOf_dvd_iff_pow_eq_one, ← Units.val_pow_eq_pow_val, Units.val_eq_one] using ζ.prop left_inv φ := (MonoidHom.eq_iff_eq_on_generator hg _ φ).mpr <| by simp only [IsUnit.unit_spec, monoidHomOfForallMemZpowers_apply_gen] right_inv φ := Subtype.ext <| by simp only [monoidHomOfForallMemZpowers_apply_gen, IsUnit.unit_of_val_units] map_mul' x y := by simp only [MonoidHom.mul_apply, MulMemClass.mk_mul_mk, Subtype.mk.injEq, Units.ext_iff, IsUnit.unit_spec, Units.val_mul] /-- The group of group homomorphisms from a finite cyclic group `G` of order `n` into another group `G'` is (noncanonically) isomorphic to the group of `n`th roots of unity in `G'`. -/ lemma monoidHom_mulEquiv_rootsOfUnity (G : Type*) [CommGroup G] [IsCyclic G] (G' : Type*) [CommGroup G'] :
Nonempty <| (G →* G') ≃* rootsOfUnity (Nat.card G) G' := by obtain ⟨g, hg⟩ := IsCyclic.exists_generator (α := G) exact ⟨monoidHomMulEquivRootsOfUnityOfGenerator hg G'⟩
Mathlib/RingTheory/RootsOfUnity/Basic.lean
275
278
/- Copyright (c) 2019 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Sébastien Gouëzel, Yury Kudryashov -/ import Mathlib.Analysis.Calculus.FDeriv.Basic import Mathlib.Analysis.Normed.Operator.BoundedLinearMaps /-! # The derivative of bounded linear maps For detailed documentation of the Fréchet derivative, see the module docstring of `Analysis/Calculus/FDeriv/Basic.lean`. This file contains the usual formulas (and existence assertions) for the derivative of bounded linear maps. -/ open Asymptotics section variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] variable {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] variable {F : Type*} [NormedAddCommGroup F] [NormedSpace 𝕜 F] variable {f : E → F} variable (e : E →L[𝕜] F) variable {x : E} variable {s : Set E} variable {L : Filter E} section ContinuousLinearMap /-! ### Continuous linear maps There are currently two variants of these in mathlib, the bundled version (named `ContinuousLinearMap`, and denoted `E →L[𝕜] F`), and the unbundled version (with a predicate `IsBoundedLinearMap`). We give statements for both versions. -/ @[fun_prop] protected theorem ContinuousLinearMap.hasStrictFDerivAt {x : E} : HasStrictFDerivAt e e x := .of_isLittleOTVS <| (IsLittleOTVS.zero _ _).congr_left fun x => by simp only [e.map_sub, sub_self, Pi.zero_apply] protected theorem ContinuousLinearMap.hasFDerivAtFilter : HasFDerivAtFilter e e x L := .of_isLittleOTVS <| (IsLittleOTVS.zero _ _).congr_left fun x => by simp only [e.map_sub, sub_self, Pi.zero_apply] @[fun_prop] protected theorem ContinuousLinearMap.hasFDerivWithinAt : HasFDerivWithinAt e e s x := e.hasFDerivAtFilter @[fun_prop] protected theorem ContinuousLinearMap.hasFDerivAt : HasFDerivAt e e x := e.hasFDerivAtFilter @[simp, fun_prop] protected theorem ContinuousLinearMap.differentiableAt : DifferentiableAt 𝕜 e x := e.hasFDerivAt.differentiableAt @[fun_prop] protected theorem ContinuousLinearMap.differentiableWithinAt : DifferentiableWithinAt 𝕜 e s x := e.differentiableAt.differentiableWithinAt @[simp] protected theorem ContinuousLinearMap.fderiv : fderiv 𝕜 e x = e := e.hasFDerivAt.fderiv protected theorem ContinuousLinearMap.fderivWithin (hxs : UniqueDiffWithinAt 𝕜 s x) : fderivWithin 𝕜 e s x = e := by rw [DifferentiableAt.fderivWithin e.differentiableAt hxs] exact e.fderiv @[simp, fun_prop] protected theorem ContinuousLinearMap.differentiable : Differentiable 𝕜 e := fun _ => e.differentiableAt @[fun_prop] protected theorem ContinuousLinearMap.differentiableOn : DifferentiableOn 𝕜 e s := e.differentiable.differentiableOn theorem IsBoundedLinearMap.hasFDerivAtFilter (h : IsBoundedLinearMap 𝕜 f) : HasFDerivAtFilter f h.toContinuousLinearMap x L :=
h.toContinuousLinearMap.hasFDerivAtFilter @[fun_prop] theorem IsBoundedLinearMap.hasFDerivWithinAt (h : IsBoundedLinearMap 𝕜 f) :
Mathlib/Analysis/Calculus/FDeriv/Linear.lean
87
90
/- Copyright (c) 2020 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel, Yury Kudryashov -/ import Mathlib.Analysis.Calculus.FormalMultilinearSeries import Mathlib.Analysis.SpecificLimits.Normed import Mathlib.Logic.Equiv.Fin.Basic import Mathlib.Tactic.Bound.Attribute import Mathlib.Topology.Algebra.InfiniteSum.Module /-! # Analytic functions A function is analytic in one dimension around `0` if it can be written as a converging power series `Σ pₙ zⁿ`. This definition can be extended to any dimension (even in infinite dimension) by requiring that `pₙ` is a continuous `n`-multilinear map. In general, `pₙ` is not unique (in two dimensions, taking `p₂ (x, y) (x', y') = x y'` or `y x'` gives the same map when applied to a vector `(x, y) (x, y)`). A way to guarantee uniqueness is to take a symmetric `pₙ`, but this is not always possible in nonzero characteristic (in characteristic 2, the previous example has no symmetric representative). Therefore, we do not insist on symmetry or uniqueness in the definition, and we only require the existence of a converging series. The general framework is important to say that the exponential map on bounded operators on a Banach space is analytic, as well as the inverse on invertible operators. ## Main definitions Let `p` be a formal multilinear series from `E` to `F`, i.e., `p n` is a multilinear map on `E^n` for `n : ℕ`. * `p.radius`: the largest `r : ℝ≥0∞` such that `‖p n‖ * r^n` grows subexponentially. * `p.le_radius_of_bound`, `p.le_radius_of_bound_nnreal`, `p.le_radius_of_isBigO`: if `‖p n‖ * r ^ n` is bounded above, then `r ≤ p.radius`; * `p.isLittleO_of_lt_radius`, `p.norm_mul_pow_le_mul_pow_of_lt_radius`, `p.isLittleO_one_of_lt_radius`, `p.norm_mul_pow_le_of_lt_radius`, `p.nnnorm_mul_pow_le_of_lt_radius`: if `r < p.radius`, then `‖p n‖ * r ^ n` tends to zero exponentially; * `p.lt_radius_of_isBigO`: if `r ≠ 0` and `‖p n‖ * r ^ n = O(a ^ n)` for some `-1 < a < 1`, then `r < p.radius`; * `p.partialSum n x`: the sum `∑_{i = 0}^{n-1} pᵢ xⁱ`. * `p.sum x`: the sum `∑'_{i = 0}^{∞} pᵢ xⁱ`. Additionally, let `f` be a function from `E` to `F`. * `HasFPowerSeriesOnBall f p x r`: on the ball of center `x` with radius `r`, `f (x + y) = ∑'_n pₙ yⁿ`. * `HasFPowerSeriesAt f p x`: on some ball of center `x` with positive radius, holds `HasFPowerSeriesOnBall f p x r`. * `AnalyticAt 𝕜 f x`: there exists a power series `p` such that holds `HasFPowerSeriesAt f p x`. * `AnalyticOnNhd 𝕜 f s`: the function `f` is analytic at every point of `s`. We also define versions of `HasFPowerSeriesOnBall`, `AnalyticAt`, and `AnalyticOnNhd` restricted to a set, similar to `ContinuousWithinAt`. See `Mathlib.Analysis.Analytic.Within` for basic properties. * `AnalyticWithinAt 𝕜 f s x` means a power series at `x` converges to `f` on `𝓝[s ∪ {x}] x`. * `AnalyticOn 𝕜 f s t` means `∀ x ∈ t, AnalyticWithinAt 𝕜 f s x`. We develop the basic properties of these notions, notably: * If a function admits a power series, it is continuous (see `HasFPowerSeriesOnBall.continuousOn` and `HasFPowerSeriesAt.continuousAt` and `AnalyticAt.continuousAt`). * In a complete space, the sum of a formal power series with positive radius is well defined on the disk of convergence, see `FormalMultilinearSeries.hasFPowerSeriesOnBall`. ## Implementation details We only introduce the radius of convergence of a power series, as `p.radius`. For a power series in finitely many dimensions, there is a finer (directional, coordinate-dependent) notion, describing the polydisk of convergence. This notion is more specific, and not necessary to build the general theory. We do not define it here. -/ noncomputable section variable {𝕜 E F G : Type*} open Topology NNReal Filter ENNReal Set Asymptotics namespace FormalMultilinearSeries variable [Semiring 𝕜] [AddCommMonoid E] [AddCommMonoid F] [Module 𝕜 E] [Module 𝕜 F] variable [TopologicalSpace E] [TopologicalSpace F] variable [ContinuousAdd E] [ContinuousAdd F] variable [ContinuousConstSMul 𝕜 E] [ContinuousConstSMul 𝕜 F] /-- Given a formal multilinear series `p` and a vector `x`, then `p.sum x` is the sum `Σ pₙ xⁿ`. A priori, it only behaves well when `‖x‖ < p.radius`. -/ protected def sum (p : FormalMultilinearSeries 𝕜 E F) (x : E) : F := ∑' n : ℕ, p n fun _ => x /-- Given a formal multilinear series `p` and a vector `x`, then `p.partialSum n x` is the sum `Σ pₖ xᵏ` for `k ∈ {0,..., n-1}`. -/ def partialSum (p : FormalMultilinearSeries 𝕜 E F) (n : ℕ) (x : E) : F := ∑ k ∈ Finset.range n, p k fun _ : Fin k => x /-- The partial sums of a formal multilinear series are continuous. -/ theorem partialSum_continuous (p : FormalMultilinearSeries 𝕜 E F) (n : ℕ) : Continuous (p.partialSum n) := by unfold partialSum fun_prop end FormalMultilinearSeries /-! ### The radius of a formal multilinear series -/ variable [NontriviallyNormedField 𝕜] [NormedAddCommGroup E] [NormedSpace 𝕜 E] [NormedAddCommGroup F] [NormedSpace 𝕜 F] [NormedAddCommGroup G] [NormedSpace 𝕜 G] namespace FormalMultilinearSeries variable (p : FormalMultilinearSeries 𝕜 E F) {r : ℝ≥0} /-- The radius of a formal multilinear series is the largest `r` such that the sum `Σ ‖pₙ‖ ‖y‖ⁿ` converges for all `‖y‖ < r`. This implies that `Σ pₙ yⁿ` converges for all `‖y‖ < r`, but these definitions are *not* equivalent in general. -/ def radius (p : FormalMultilinearSeries 𝕜 E F) : ℝ≥0∞ := ⨆ (r : ℝ≥0) (C : ℝ) (_ : ∀ n, ‖p n‖ * (r : ℝ) ^ n ≤ C), (r : ℝ≥0∞) /-- If `‖pₙ‖ rⁿ` is bounded in `n`, then the radius of `p` is at least `r`. -/ theorem le_radius_of_bound (C : ℝ) {r : ℝ≥0} (h : ∀ n : ℕ, ‖p n‖ * (r : ℝ) ^ n ≤ C) : (r : ℝ≥0∞) ≤ p.radius := le_iSup_of_le r <| le_iSup_of_le C <| le_iSup (fun _ => (r : ℝ≥0∞)) h /-- If `‖pₙ‖ rⁿ` is bounded in `n`, then the radius of `p` is at least `r`. -/ theorem le_radius_of_bound_nnreal (C : ℝ≥0) {r : ℝ≥0} (h : ∀ n : ℕ, ‖p n‖₊ * r ^ n ≤ C) : (r : ℝ≥0∞) ≤ p.radius := p.le_radius_of_bound C fun n => mod_cast h n /-- If `‖pₙ‖ rⁿ = O(1)`, as `n → ∞`, then the radius of `p` is at least `r`. -/ theorem le_radius_of_isBigO (h : (fun n => ‖p n‖ * (r : ℝ) ^ n) =O[atTop] fun _ => (1 : ℝ)) : ↑r ≤ p.radius := Exists.elim (isBigO_one_nat_atTop_iff.1 h) fun C hC => p.le_radius_of_bound C fun n => (le_abs_self _).trans (hC n) theorem le_radius_of_eventually_le (C) (h : ∀ᶠ n in atTop, ‖p n‖ * (r : ℝ) ^ n ≤ C) : ↑r ≤ p.radius := p.le_radius_of_isBigO <| IsBigO.of_bound C <| h.mono fun n hn => by simpa theorem le_radius_of_summable_nnnorm (h : Summable fun n => ‖p n‖₊ * r ^ n) : ↑r ≤ p.radius := p.le_radius_of_bound_nnreal (∑' n, ‖p n‖₊ * r ^ n) fun _ => h.le_tsum' _ theorem le_radius_of_summable (h : Summable fun n => ‖p n‖ * (r : ℝ) ^ n) : ↑r ≤ p.radius := p.le_radius_of_summable_nnnorm <| by simp only [← coe_nnnorm] at h exact mod_cast h theorem radius_eq_top_of_forall_nnreal_isBigO (h : ∀ r : ℝ≥0, (fun n => ‖p n‖ * (r : ℝ) ^ n) =O[atTop] fun _ => (1 : ℝ)) : p.radius = ∞ := ENNReal.eq_top_of_forall_nnreal_le fun r => p.le_radius_of_isBigO (h r) theorem radius_eq_top_of_eventually_eq_zero (h : ∀ᶠ n in atTop, p n = 0) : p.radius = ∞ := p.radius_eq_top_of_forall_nnreal_isBigO fun r => (isBigO_zero _ _).congr' (h.mono fun n hn => by simp [hn]) EventuallyEq.rfl
theorem radius_eq_top_of_forall_image_add_eq_zero (n : ℕ) (hn : ∀ m, p (m + n) = 0) : p.radius = ∞ := p.radius_eq_top_of_eventually_eq_zero <| mem_atTop_sets.2 ⟨n, fun _ hk => tsub_add_cancel_of_le hk ▸ hn _⟩
Mathlib/Analysis/Analytic/Basic.lean
156
159
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import Mathlib.RingTheory.Ideal.Operations /-! # Maps on modules and ideals Main definitions include `Ideal.map`, `Ideal.comap`, `RingHom.ker`, `Module.annihilator` and `Submodule.annihilator`. -/ assert_not_exists Basis -- See `RingTheory.Ideal.Basis` Submodule.hasQuotient -- See `RingTheory.Ideal.Quotient.Operations` universe u v w x open Pointwise namespace Ideal section MapAndComap variable {R : Type u} {S : Type v} section Semiring variable {F : Type*} [Semiring R] [Semiring S] variable [FunLike F R S] variable (f : F) variable {I J : Ideal R} {K L : Ideal S} /-- `I.map f` is the span of the image of the ideal `I` under `f`, which may be bigger than the image itself. -/ def map (I : Ideal R) : Ideal S := span (f '' I) /-- `I.comap f` is the preimage of `I` under `f`. -/ def comap [RingHomClass F R S] (I : Ideal S) : Ideal R where carrier := f ⁻¹' I add_mem' {x y} hx hy := by simp only [Set.mem_preimage, SetLike.mem_coe, map_add f] at hx hy ⊢ exact add_mem hx hy zero_mem' := by simp only [Set.mem_preimage, map_zero, SetLike.mem_coe, Submodule.zero_mem] smul_mem' c x hx := by simp only [smul_eq_mul, Set.mem_preimage, map_mul, SetLike.mem_coe] at * exact mul_mem_left I _ hx @[simp] theorem coe_comap [RingHomClass F R S] (I : Ideal S) : (comap f I : Set R) = f ⁻¹' I := rfl lemma comap_coe [RingHomClass F R S] (I : Ideal S) : I.comap (f : R →+* S) = I.comap f := rfl lemma map_coe [RingHomClass F R S] (I : Ideal R) : I.map (f : R →+* S) = I.map f := rfl variable {f} theorem map_mono (h : I ≤ J) : map f I ≤ map f J := span_mono <| Set.image_subset _ h theorem mem_map_of_mem (f : F) {I : Ideal R} {x : R} (h : x ∈ I) : f x ∈ map f I := subset_span ⟨x, h, rfl⟩ theorem apply_coe_mem_map (f : F) (I : Ideal R) (x : I) : f x ∈ I.map f := mem_map_of_mem f x.2 theorem map_le_iff_le_comap [RingHomClass F R S] : map f I ≤ K ↔ I ≤ comap f K := span_le.trans Set.image_subset_iff @[simp] theorem mem_comap [RingHomClass F R S] {x} : x ∈ comap f K ↔ f x ∈ K := Iff.rfl theorem comap_mono [RingHomClass F R S] (h : K ≤ L) : comap f K ≤ comap f L := Set.preimage_mono fun _ hx => h hx variable (f) theorem comap_ne_top [RingHomClass F R S] (hK : K ≠ ⊤) : comap f K ≠ ⊤ := (ne_top_iff_one _).2 <| by rw [mem_comap, map_one]; exact (ne_top_iff_one _).1 hK lemma exists_ideal_comap_le_prime {S} [CommSemiring S] [FunLike F R S] [RingHomClass F R S] {f : F} (P : Ideal R) [P.IsPrime] (I : Ideal S) (le : I.comap f ≤ P) : ∃ Q ≥ I, Q.IsPrime ∧ Q.comap f ≤ P := have ⟨Q, hQ, hIQ, disj⟩ := I.exists_le_prime_disjoint (P.primeCompl.map f) <| Set.disjoint_left.mpr fun _ ↦ by rintro hI ⟨r, hp, rfl⟩; exact hp (le hI) ⟨Q, hIQ, hQ, fun r hp' ↦ of_not_not fun hp ↦ Set.disjoint_left.mp disj hp' ⟨_, hp, rfl⟩⟩ variable {G : Type*} [FunLike G S R] theorem map_le_comap_of_inv_on [RingHomClass G S R] (g : G) (I : Ideal R) (hf : Set.LeftInvOn g f I) : I.map f ≤ I.comap g := by refine Ideal.span_le.2 ?_ rintro x ⟨x, hx, rfl⟩ rw [SetLike.mem_coe, mem_comap, hf hx] exact hx theorem comap_le_map_of_inv_on [RingHomClass F R S] (g : G) (I : Ideal S) (hf : Set.LeftInvOn g f (f ⁻¹' I)) : I.comap f ≤ I.map g := fun x (hx : f x ∈ I) => hf hx ▸ Ideal.mem_map_of_mem g hx /-- The `Ideal` version of `Set.image_subset_preimage_of_inverse`. -/ theorem map_le_comap_of_inverse [RingHomClass G S R] (g : G) (I : Ideal R) (h : Function.LeftInverse g f) : I.map f ≤ I.comap g := map_le_comap_of_inv_on _ _ _ <| h.leftInvOn _ variable [RingHomClass F R S] instance (priority := low) [K.IsTwoSided] : (comap f K).IsTwoSided := ⟨fun b ha ↦ by rw [mem_comap, map_mul]; exact mul_mem_right _ _ ha⟩ /-- The `Ideal` version of `Set.preimage_subset_image_of_inverse`. -/ theorem comap_le_map_of_inverse (g : G) (I : Ideal S) (h : Function.LeftInverse g f) : I.comap f ≤ I.map g := comap_le_map_of_inv_on _ _ _ <| h.leftInvOn _ instance IsPrime.comap [hK : K.IsPrime] : (comap f K).IsPrime := ⟨comap_ne_top _ hK.1, fun {x y} => by simp only [mem_comap, map_mul]; apply hK.2⟩ variable (I J K L) theorem map_top : map f ⊤ = ⊤ := (eq_top_iff_one _).2 <| subset_span ⟨1, trivial, map_one f⟩ theorem gc_map_comap : GaloisConnection (Ideal.map f) (Ideal.comap f) := fun _ _ => Ideal.map_le_iff_le_comap @[simp] theorem comap_id : I.comap (RingHom.id R) = I := Ideal.ext fun _ => Iff.rfl @[simp] lemma comap_idₐ {R S : Type*} [CommSemiring R] [Semiring S] [Algebra R S] (I : Ideal S) : Ideal.comap (AlgHom.id R S) I = I := I.comap_id @[simp] theorem map_id : I.map (RingHom.id R) = I := (gc_map_comap (RingHom.id R)).l_unique GaloisConnection.id comap_id @[simp] lemma map_idₐ {R S : Type*} [CommSemiring R] [Semiring S] [Algebra R S] (I : Ideal S) : Ideal.map (AlgHom.id R S) I = I := I.map_id theorem comap_comap {T : Type*} [Semiring T] {I : Ideal T} (f : R →+* S) (g : S →+* T) : (I.comap g).comap f = I.comap (g.comp f) := rfl lemma comap_comapₐ {R A B C : Type*} [CommSemiring R] [Semiring A] [Algebra R A] [Semiring B] [Algebra R B] [Semiring C] [Algebra R C] {I : Ideal C} (f : A →ₐ[R] B) (g : B →ₐ[R] C) : (I.comap g).comap f = I.comap (g.comp f) := I.comap_comap f.toRingHom g.toRingHom theorem map_map {T : Type*} [Semiring T] {I : Ideal R} (f : R →+* S) (g : S →+* T) : (I.map f).map g = I.map (g.comp f) := ((gc_map_comap f).compose (gc_map_comap g)).l_unique (gc_map_comap (g.comp f)) fun _ => comap_comap _ _ lemma map_mapₐ {R A B C : Type*} [CommSemiring R] [Semiring A] [Algebra R A] [Semiring B] [Algebra R B] [Semiring C] [Algebra R C] {I : Ideal A} (f : A →ₐ[R] B) (g : B →ₐ[R] C) : (I.map f).map g = I.map (g.comp f) := I.map_map f.toRingHom g.toRingHom theorem map_span (f : F) (s : Set R) : map f (span s) = span (f '' s) := by refine (Submodule.span_eq_of_le _ ?_ ?_).symm · rintro _ ⟨x, hx, rfl⟩; exact mem_map_of_mem f (subset_span hx) · rw [map_le_iff_le_comap, span_le, coe_comap, ← Set.image_subset_iff] exact subset_span variable {f I J K L} theorem map_le_of_le_comap : I ≤ K.comap f → I.map f ≤ K := (gc_map_comap f).l_le theorem le_comap_of_map_le : I.map f ≤ K → I ≤ K.comap f := (gc_map_comap f).le_u theorem le_comap_map : I ≤ (I.map f).comap f := (gc_map_comap f).le_u_l _ theorem map_comap_le : (K.comap f).map f ≤ K := (gc_map_comap f).l_u_le _ @[simp] theorem comap_top : (⊤ : Ideal S).comap f = ⊤ := (gc_map_comap f).u_top @[simp] theorem comap_eq_top_iff {I : Ideal S} : I.comap f = ⊤ ↔ I = ⊤ := ⟨fun h => I.eq_top_iff_one.mpr (map_one f ▸ mem_comap.mp ((I.comap f).eq_top_iff_one.mp h)), fun h => by rw [h, comap_top]⟩ @[simp] theorem map_bot : (⊥ : Ideal R).map f = ⊥ := (gc_map_comap f).l_bot theorem ne_bot_of_map_ne_bot (hI : map f I ≠ ⊥) : I ≠ ⊥ := fun h => hI (Eq.mpr (congrArg (fun I ↦ map f I = ⊥) h) map_bot) variable (f I J K L) @[simp] theorem map_comap_map : ((I.map f).comap f).map f = I.map f := (gc_map_comap f).l_u_l_eq_l I @[simp] theorem comap_map_comap : ((K.comap f).map f).comap f = K.comap f := (gc_map_comap f).u_l_u_eq_u K theorem map_sup : (I ⊔ J).map f = I.map f ⊔ J.map f := (gc_map_comap f : GaloisConnection (map f) (comap f)).l_sup theorem comap_inf : comap f (K ⊓ L) = comap f K ⊓ comap f L := rfl variable {ι : Sort*} theorem map_iSup (K : ι → Ideal R) : (iSup K).map f = ⨆ i, (K i).map f := (gc_map_comap f : GaloisConnection (map f) (comap f)).l_iSup theorem comap_iInf (K : ι → Ideal S) : (iInf K).comap f = ⨅ i, (K i).comap f := (gc_map_comap f : GaloisConnection (map f) (comap f)).u_iInf theorem map_sSup (s : Set (Ideal R)) : (sSup s).map f = ⨆ I ∈ s, (I : Ideal R).map f := (gc_map_comap f : GaloisConnection (map f) (comap f)).l_sSup theorem comap_sInf (s : Set (Ideal S)) : (sInf s).comap f = ⨅ I ∈ s, (I : Ideal S).comap f := (gc_map_comap f : GaloisConnection (map f) (comap f)).u_sInf theorem comap_sInf' (s : Set (Ideal S)) : (sInf s).comap f = ⨅ I ∈ comap f '' s, I := _root_.trans (comap_sInf f s) (by rw [iInf_image]) /-- Variant of `Ideal.IsPrime.comap` where ideal is explicit rather than implicit. -/ theorem comap_isPrime [H : IsPrime K] : IsPrime (comap f K) := H.comap f variable {I J K L} theorem map_inf_le : map f (I ⊓ J) ≤ map f I ⊓ map f J := (gc_map_comap f : GaloisConnection (map f) (comap f)).monotone_l.map_inf_le _ _ theorem le_comap_sup : comap f K ⊔ comap f L ≤ comap f (K ⊔ L) := (gc_map_comap f : GaloisConnection (map f) (comap f)).monotone_u.le_map_sup _ _ -- TODO: Should these be simp lemmas? theorem _root_.element_smul_restrictScalars {R S M} [CommSemiring R] [CommSemiring S] [Algebra R S] [AddCommMonoid M] [Module R M] [Module S M] [IsScalarTower R S M] (r : R) (N : Submodule S M) : (algebraMap R S r • N).restrictScalars R = r • N.restrictScalars R := SetLike.coe_injective (congrArg (· '' _) (funext (algebraMap_smul S r))) theorem smul_restrictScalars {R S M} [CommSemiring R] [CommSemiring S] [Algebra R S] [AddCommMonoid M] [Module R M] [Module S M] [IsScalarTower R S M] (I : Ideal R) (N : Submodule S M) : (I.map (algebraMap R S) • N).restrictScalars R = I • N.restrictScalars R := by simp_rw [map, Submodule.span_smul_eq, ← Submodule.coe_set_smul, Submodule.set_smul_eq_iSup, ← element_smul_restrictScalars, iSup_image] exact map_iSup₂ (Submodule.restrictScalarsLatticeHom R S M) _ @[simp] theorem smul_top_eq_map {R S : Type*} [CommSemiring R] [CommSemiring S] [Algebra R S] (I : Ideal R) : I • (⊤ : Submodule R S) = (I.map (algebraMap R S)).restrictScalars R := Eq.trans (smul_restrictScalars I (⊤ : Ideal S)).symm <| congrArg _ <| Eq.trans (Ideal.smul_eq_mul _ _) (Ideal.mul_top _) @[simp] theorem coe_restrictScalars {R S : Type*} [Semiring R] [Semiring S] [Module R S] [IsScalarTower R S S] (I : Ideal S) : (I.restrictScalars R : Set S) = ↑I := rfl /-- The smallest `S`-submodule that contains all `x ∈ I * y ∈ J` is also the smallest `R`-submodule that does so. -/ @[simp] theorem restrictScalars_mul {R S : Type*} [Semiring R] [Semiring S] [Module R S] [IsScalarTower R S S] (I J : Ideal S) : (I * J).restrictScalars R = I.restrictScalars R * J.restrictScalars R := rfl section Surjective section variable (hf : Function.Surjective f) include hf open Function theorem map_comap_of_surjective (I : Ideal S) : map f (comap f I) = I := le_antisymm (map_le_iff_le_comap.2 le_rfl) fun s hsi => let ⟨r, hfrs⟩ := hf s hfrs ▸ (mem_map_of_mem f <| show f r ∈ I from hfrs.symm ▸ hsi) /-- `map` and `comap` are adjoint, and the composition `map f ∘ comap f` is the identity -/ def giMapComap : GaloisInsertion (map f) (comap f) := GaloisInsertion.monotoneIntro (gc_map_comap f).monotone_u (gc_map_comap f).monotone_l (fun _ => le_comap_map) (map_comap_of_surjective _ hf) theorem map_surjective_of_surjective : Surjective (map f) := (giMapComap f hf).l_surjective theorem comap_injective_of_surjective : Injective (comap f) := (giMapComap f hf).u_injective theorem map_sup_comap_of_surjective (I J : Ideal S) : (I.comap f ⊔ J.comap f).map f = I ⊔ J := (giMapComap f hf).l_sup_u _ _ theorem map_iSup_comap_of_surjective (K : ι → Ideal S) : (⨆ i, (K i).comap f).map f = iSup K := (giMapComap f hf).l_iSup_u _ theorem map_inf_comap_of_surjective (I J : Ideal S) : (I.comap f ⊓ J.comap f).map f = I ⊓ J := (giMapComap f hf).l_inf_u _ _ theorem map_iInf_comap_of_surjective (K : ι → Ideal S) : (⨅ i, (K i).comap f).map f = iInf K := (giMapComap f hf).l_iInf_u _ theorem mem_image_of_mem_map_of_surjective {I : Ideal R} {y} (H : y ∈ map f I) : y ∈ f '' I := Submodule.span_induction (hx := H) (fun _ => id) ⟨0, I.zero_mem, map_zero f⟩ (fun _ _ _ _ ⟨x1, hx1i, hxy1⟩ ⟨x2, hx2i, hxy2⟩ => ⟨x1 + x2, I.add_mem hx1i hx2i, hxy1 ▸ hxy2 ▸ map_add f _ _⟩) fun c _ _ ⟨x, hxi, hxy⟩ => let ⟨d, hdc⟩ := hf c ⟨d * x, I.mul_mem_left _ hxi, hdc ▸ hxy ▸ map_mul f _ _⟩ theorem mem_map_iff_of_surjective {I : Ideal R} {y} : y ∈ map f I ↔ ∃ x, x ∈ I ∧ f x = y := ⟨fun h => (Set.mem_image _ _ _).2 (mem_image_of_mem_map_of_surjective f hf h), fun ⟨_, hx⟩ => hx.right ▸ mem_map_of_mem f hx.left⟩ theorem le_map_of_comap_le_of_surjective : comap f K ≤ I → K ≤ map f I := fun h => map_comap_of_surjective f hf K ▸ map_mono h end theorem map_comap_eq_self_of_equiv {E : Type*} [EquivLike E R S] [RingEquivClass E R S] (e : E) (I : Ideal S) : map e (comap e I) = I := I.map_comap_of_surjective e (EquivLike.surjective e) theorem map_eq_submodule_map (f : R →+* S) [h : RingHomSurjective f] (I : Ideal R) : I.map f = Submodule.map f.toSemilinearMap I := Submodule.ext fun _ => mem_map_iff_of_surjective f h.1 instance (priority := low) (f : R →+* S) [RingHomSurjective f] (I : Ideal R) [I.IsTwoSided] : (I.map f).IsTwoSided where mul_mem_of_left b ha := by rw [map_eq_submodule_map] at ha ⊢ obtain ⟨a, ha, rfl⟩ := ha obtain ⟨b, rfl⟩ := f.surjective b rw [RingHom.coe_toSemilinearMap, ← map_mul] exact ⟨_, I.mul_mem_right _ ha, rfl⟩ open Function in theorem IsMaximal.comap_piEvalRingHom {ι : Type*} {R : ι → Type*} [∀ i, Semiring (R i)] {i : ι} {I : Ideal (R i)} (h : I.IsMaximal) : (I.comap <| Pi.evalRingHom R i).IsMaximal := by refine isMaximal_iff.mpr ⟨I.ne_top_iff_one.mp h.ne_top, fun J x le hxI hxJ ↦ ?_⟩ have ⟨r, y, hy, eq⟩ := h.exists_inv hxI classical convert J.add_mem (J.mul_mem_left (update 0 i r) hxJ) (b := update 1 i y) (le <| by apply update_self i y 1 ▸ hy) ext j obtain rfl | ne := eq_or_ne j i · simpa [eq_comm] using eq · simp [update_of_ne ne] theorem comap_le_comap_iff_of_surjective (hf : Function.Surjective f) (I J : Ideal S) : comap f I ≤ comap f J ↔ I ≤ J := ⟨fun h => (map_comap_of_surjective f hf I).symm.le.trans (map_le_of_le_comap h), fun h => le_comap_of_map_le ((map_comap_of_surjective f hf I).le.trans h)⟩ /-- The map on ideals induced by a surjective map preserves inclusion. -/ def orderEmbeddingOfSurjective (hf : Function.Surjective f) : Ideal S ↪o Ideal R where toFun := comap f inj' _ _ eq := SetLike.ext' (Set.preimage_injective.mpr hf <| SetLike.ext'_iff.mp eq) map_rel_iff' := comap_le_comap_iff_of_surjective _ hf .. theorem map_eq_top_or_isMaximal_of_surjective (hf : Function.Surjective f) {I : Ideal R} (H : IsMaximal I) : map f I = ⊤ ∨ IsMaximal (map f I) := or_iff_not_imp_left.2 fun ne_top ↦ ⟨⟨ne_top, fun _J hJ ↦ comap_injective_of_surjective f hf <| H.1.2 _ (le_comap_map.trans_lt <| (orderEmbeddingOfSurjective f hf).strictMono hJ)⟩⟩ end Surjective section Injective theorem comap_bot_le_of_injective (hf : Function.Injective f) : comap f ⊥ ≤ I := by refine le_trans (fun x hx => ?_) bot_le rw [mem_comap, Submodule.mem_bot, ← map_zero f] at hx exact Eq.symm (hf hx) ▸ Submodule.zero_mem ⊥ theorem comap_bot_of_injective (hf : Function.Injective f) : Ideal.comap f ⊥ = ⊥ := le_bot_iff.mp (Ideal.comap_bot_le_of_injective f hf) end Injective /-- If `f : R ≃+* S` is a ring isomorphism and `I : Ideal R`, then `map f.symm (map f I) = I`. -/ @[simp] theorem map_of_equiv {I : Ideal R} (f : R ≃+* S) : (I.map (f : R →+* S)).map (f.symm : S →+* R) = I := by rw [← RingEquiv.toRingHom_eq_coe, ← RingEquiv.toRingHom_eq_coe, map_map, RingEquiv.toRingHom_eq_coe, RingEquiv.toRingHom_eq_coe, RingEquiv.symm_comp, map_id] /-- If `f : R ≃+* S` is a ring isomorphism and `I : Ideal R`, then `comap f (comap f.symm I) = I`. -/ @[simp] theorem comap_of_equiv {I : Ideal R} (f : R ≃+* S) : (I.comap (f.symm : S →+* R)).comap (f : R →+* S) = I := by rw [← RingEquiv.toRingHom_eq_coe, ← RingEquiv.toRingHom_eq_coe, comap_comap, RingEquiv.toRingHom_eq_coe, RingEquiv.toRingHom_eq_coe, RingEquiv.symm_comp, comap_id] /-- If `f : R ≃+* S` is a ring isomorphism and `I : Ideal R`, then `map f I = comap f.symm I`. -/ theorem map_comap_of_equiv {I : Ideal R} (f : R ≃+* S) : I.map (f : R →+* S) = I.comap f.symm := le_antisymm (Ideal.map_le_comap_of_inverse _ _ _ (Equiv.left_inv' _)) (Ideal.comap_le_map_of_inverse _ _ _ (Equiv.right_inv' _)) /-- If `f : R ≃+* S` is a ring isomorphism and `I : Ideal R`, then `comap f.symm I = map f I`. -/ @[simp] theorem comap_symm {I : Ideal R} (f : R ≃+* S) : I.comap f.symm = I.map f := (map_comap_of_equiv f).symm /-- If `f : R ≃+* S` is a ring isomorphism and `I : Ideal R`, then `map f.symm I = comap f I`. -/ @[simp] theorem map_symm {I : Ideal S} (f : R ≃+* S) : I.map f.symm = I.comap f := map_comap_of_equiv (RingEquiv.symm f) @[simp] theorem symm_apply_mem_of_equiv_iff {I : Ideal R} {f : R ≃+* S} {y : S} : f.symm y ∈ I ↔ y ∈ I.map f := by rw [← comap_symm, mem_comap] @[simp] theorem apply_mem_of_equiv_iff {I : Ideal R} {f : R ≃+* S} {x : R} : f x ∈ I.map f ↔ x ∈ I := by rw [← comap_symm, Ideal.mem_comap, f.symm_apply_apply] theorem mem_map_of_equiv {E : Type*} [EquivLike E R S] [RingEquivClass E R S] (e : E) {I : Ideal R} (y : S) : y ∈ map e I ↔ ∃ x ∈ I, e x = y := by constructor · intro h simp_rw [show map e I = _ from map_comap_of_equiv (e : R ≃+* S)] at h exact ⟨(e : R ≃+* S).symm y, h, (e : R ≃+* S).apply_symm_apply y⟩ · rintro ⟨x, hx, rfl⟩ exact mem_map_of_mem e hx section Bijective variable (hf : Function.Bijective f) {I : Ideal R} {K : Ideal S} include hf /-- Special case of the correspondence theorem for isomorphic rings -/ def relIsoOfBijective : Ideal S ≃o Ideal R where toFun := comap f invFun := map f left_inv := map_comap_of_surjective _ hf.2 right_inv J := le_antisymm (fun _ h ↦ have ⟨y, hy, eq⟩ := (mem_map_iff_of_surjective _ hf.2).mp h; hf.1 eq ▸ hy) le_comap_map map_rel_iff' {_ _} := by refine ⟨fun h ↦ ?_, comap_mono⟩ have := map_mono (f := f) h simpa only [Equiv.coe_fn_mk, map_comap_of_surjective f hf.2] using this theorem comap_le_iff_le_map : comap f K ≤ I ↔ K ≤ map f I := ⟨fun h => le_map_of_comap_le_of_surjective f hf.right h, fun h => (relIsoOfBijective f hf).right_inv I ▸ comap_mono h⟩ lemma comap_map_of_bijective : (I.map f).comap f = I := le_antisymm ((comap_le_iff_le_map f hf).mpr fun _ ↦ id) le_comap_map theorem isMaximal_map_iff_of_bijective : IsMaximal (map f I) ↔ IsMaximal I := by simpa only [isMaximal_def] using (relIsoOfBijective _ hf).symm.isCoatom_iff _ theorem isMaximal_comap_iff_of_bijective : IsMaximal (comap f K) ↔ IsMaximal K := by simpa only [isMaximal_def] using (relIsoOfBijective _ hf).isCoatom_iff _ alias ⟨_, IsMaximal.map_bijective⟩ := isMaximal_map_iff_of_bijective alias ⟨_, IsMaximal.comap_bijective⟩ := isMaximal_comap_iff_of_bijective /-- A ring isomorphism sends a maximal ideal to a maximal ideal. -/ instance map_isMaximal_of_equiv {E : Type*} [EquivLike E R S] [RingEquivClass E R S] (e : E) {p : Ideal R} [hp : p.IsMaximal] : (map e p).IsMaximal := hp.map_bijective e (EquivLike.bijective e) /-- The pullback of a maximal ideal under a ring isomorphism is a maximal ideal. -/ instance comap_isMaximal_of_equiv {E : Type*} [EquivLike E R S] [RingEquivClass E R S] (e : E) {p : Ideal S} [hp : p.IsMaximal] : (comap e p).IsMaximal := hp.comap_bijective e (EquivLike.bijective e) theorem isMaximal_iff_of_bijective : (⊥ : Ideal R).IsMaximal ↔ (⊥ : Ideal S).IsMaximal := ⟨fun h ↦ map_bot (f := f) ▸ h.map_bijective f hf, fun h ↦ have e := RingEquiv.ofBijective f hf map_bot (f := e.symm) ▸ h.map_bijective _ e.symm.bijective⟩ @[deprecated (since := "2024-12-07")] alias map.isMaximal := IsMaximal.map_bijective @[deprecated (since := "2024-12-07")] alias comap.isMaximal := IsMaximal.comap_bijective @[deprecated (since := "2024-12-07")] alias RingEquiv.bot_maximal_iff := isMaximal_iff_of_bijective end Bijective end Semiring section Ring variable {F : Type*} [Ring R] [Ring S] variable [FunLike F R S] [RingHomClass F R S] (f : F) {I : Ideal R} section Surjective theorem comap_map_of_surjective (hf : Function.Surjective f) (I : Ideal R) : comap f (map f I) = I ⊔ comap f ⊥ := le_antisymm (fun r h => let ⟨s, hsi, hfsr⟩ := mem_image_of_mem_map_of_surjective f hf h Submodule.mem_sup.2 ⟨s, hsi, r - s, (Submodule.mem_bot S).2 <| by rw [map_sub, hfsr, sub_self], add_sub_cancel s r⟩) (sup_le (map_le_iff_le_comap.1 le_rfl) (comap_mono bot_le)) /-- Correspondence theorem -/ def relIsoOfSurjective (hf : Function.Surjective f) : Ideal S ≃o { p : Ideal R // comap f ⊥ ≤ p } where toFun J := ⟨comap f J, comap_mono bot_le⟩ invFun I := map f I.1 left_inv J := map_comap_of_surjective f hf J right_inv I := Subtype.eq <| show comap f (map f I.1) = I.1 from (comap_map_of_surjective f hf I).symm ▸ le_antisymm (sup_le le_rfl I.2) le_sup_left map_rel_iff' {I1 I2} := ⟨fun H => map_comap_of_surjective f hf I1 ▸ map_comap_of_surjective f hf I2 ▸ map_mono H, comap_mono⟩ -- May not hold if `R` is a semiring: consider `ℕ →+* ZMod 2`. theorem comap_isMaximal_of_surjective (hf : Function.Surjective f) {K : Ideal S} [H : IsMaximal K] : IsMaximal (comap f K) := by refine ⟨⟨comap_ne_top _ H.1.1, fun J hJ => ?_⟩⟩ suffices map f J = ⊤ by have := congr_arg (comap f) this rw [comap_top, comap_map_of_surjective _ hf, eq_top_iff] at this rw [eq_top_iff] exact le_trans this (sup_le (le_of_eq rfl) (le_trans (comap_mono bot_le) (le_of_lt hJ))) refine H.1.2 (map f J) (lt_of_le_of_ne (le_map_of_comap_le_of_surjective _ hf (le_of_lt hJ)) fun h => ne_of_lt hJ (_root_.trans (congr_arg (comap f) h) ?_))
rw [comap_map_of_surjective _ hf, sup_eq_left] exact le_trans (comap_mono bot_le) (le_of_lt hJ)
Mathlib/RingTheory/Ideal/Maps.lean
551
553
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Michael Stoll -/ import Mathlib.Data.Nat.Squarefree import Mathlib.NumberTheory.Zsqrtd.QuadraticReciprocity import Mathlib.NumberTheory.Padics.PadicVal.Basic /-! # Sums of two squares Fermat's theorem on the sum of two squares. Every prime `p` congruent to 1 mod 4 is the sum of two squares; see `Nat.Prime.sq_add_sq` (which has the weaker assumption `p % 4 ≠ 3`). We also give the result that characterizes the (positive) natural numbers that are sums of two squares as those numbers `n` such that for every prime `q` congruent to 3 mod 4, the exponent of the largest power of `q` dividing `n` is even; see `Nat.eq_sq_add_sq_iff`. There is an alternative characterization as the numbers of the form `a^2 * b`, where `b` is a natural number such that `-1` is a square modulo `b`; see `Nat.eq_sq_add_sq_iff_eq_sq_mul`. -/ section Fermat open GaussianInt /-- **Fermat's theorem on the sum of two squares**. Every prime not congruent to 3 mod 4 is the sum of two squares. Also known as **Fermat's Christmas theorem**. -/ theorem Nat.Prime.sq_add_sq {p : ℕ} [Fact p.Prime] (hp : p % 4 ≠ 3) : ∃ a b : ℕ, a ^ 2 + b ^ 2 = p := by apply sq_add_sq_of_nat_prime_of_not_irreducible p rwa [_root_.irreducible_iff_prime, prime_iff_mod_four_eq_three_of_nat_prime p] end Fermat /-! ### Generalities on sums of two squares -/ section General /-- The set of sums of two squares is closed under multiplication in any commutative ring. See also `sq_add_sq_mul_sq_add_sq`. -/ theorem sq_add_sq_mul {R} [CommRing R] {a b x y u v : R} (ha : a = x ^ 2 + y ^ 2) (hb : b = u ^ 2 + v ^ 2) : ∃ r s : R, a * b = r ^ 2 + s ^ 2 := ⟨x * u - y * v, x * v + y * u, by rw [ha, hb]; ring⟩
/-- The set of natural numbers that are sums of two squares is closed under multiplication. -/ theorem Nat.sq_add_sq_mul {a b x y u v : ℕ} (ha : a = x ^ 2 + y ^ 2) (hb : b = u ^ 2 + v ^ 2) :
Mathlib/NumberTheory/SumTwoSquares.lean
50
52
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Yury Kudryashov -/ import Mathlib.Data.ENNReal.Operations /-! # Results about division in extended non-negative reals This file establishes basic properties related to the inversion and division operations on `ℝ≥0∞`. For instance, as a consequence of being a `DivInvOneMonoid`, `ℝ≥0∞` inherits a power operation with integer exponent. ## Main results A few order isomorphisms are worthy of mention: - `OrderIso.invENNReal : ℝ≥0∞ ≃o ℝ≥0∞ᵒᵈ`: The map `x ↦ x⁻¹` as an order isomorphism to the dual. - `orderIsoIicOneBirational : ℝ≥0∞ ≃o Iic (1 : ℝ≥0∞)`: The birational order isomorphism between `ℝ≥0∞` and the unit interval `Set.Iic (1 : ℝ≥0∞)` given by `x ↦ (x⁻¹ + 1)⁻¹` with inverse `x ↦ (x⁻¹ - 1)⁻¹` - `orderIsoIicCoe (a : ℝ≥0) : Iic (a : ℝ≥0∞) ≃o Iic a`: Order isomorphism between an initial interval in `ℝ≥0∞` and an initial interval in `ℝ≥0` given by the identity map. - `orderIsoUnitIntervalBirational : ℝ≥0∞ ≃o Icc (0 : ℝ) 1`: An order isomorphism between the extended nonnegative real numbers and the unit interval. This is `orderIsoIicOneBirational` composed with the identity order isomorphism between `Iic (1 : ℝ≥0∞)` and `Icc (0 : ℝ) 1`. -/ assert_not_exists Finset open Set NNReal namespace ENNReal noncomputable section Inv variable {a b c d : ℝ≥0∞} {r p q : ℝ≥0} protected theorem div_eq_inv_mul : a / b = b⁻¹ * a := by rw [div_eq_mul_inv, mul_comm] @[simp] theorem inv_zero : (0 : ℝ≥0∞)⁻¹ = ∞ := show sInf { b : ℝ≥0∞ | 1 ≤ 0 * b } = ∞ by simp @[simp] theorem inv_top : ∞⁻¹ = 0 := bot_unique <| le_of_forall_gt_imp_ge_of_dense fun a (h : 0 < a) => sInf_le <| by simp [*, h.ne', top_mul] theorem coe_inv_le : (↑r⁻¹ : ℝ≥0∞) ≤ (↑r)⁻¹ := le_sInf fun b (hb : 1 ≤ ↑r * b) => coe_le_iff.2 <| by rintro b rfl apply NNReal.inv_le_of_le_mul rwa [← coe_mul, ← coe_one, coe_le_coe] at hb @[simp, norm_cast] theorem coe_inv (hr : r ≠ 0) : (↑r⁻¹ : ℝ≥0∞) = (↑r)⁻¹ := coe_inv_le.antisymm <| sInf_le <| mem_setOf.2 <| by rw [← coe_mul, mul_inv_cancel₀ hr, coe_one] @[norm_cast] theorem coe_inv_two : ((2⁻¹ : ℝ≥0) : ℝ≥0∞) = 2⁻¹ := by rw [coe_inv _root_.two_ne_zero, coe_two] @[simp, norm_cast] theorem coe_div (hr : r ≠ 0) : (↑(p / r) : ℝ≥0∞) = p / r := by rw [div_eq_mul_inv, div_eq_mul_inv, coe_mul, coe_inv hr] lemma coe_div_le : ↑(p / r) ≤ (p / r : ℝ≥0∞) := by simpa only [div_eq_mul_inv, coe_mul] using mul_le_mul_left' coe_inv_le _ theorem div_zero (h : a ≠ 0) : a / 0 = ∞ := by simp [div_eq_mul_inv, h] instance : DivInvOneMonoid ℝ≥0∞ := { inferInstanceAs (DivInvMonoid ℝ≥0∞) with inv_one := by simpa only [coe_inv one_ne_zero, coe_one] using coe_inj.2 inv_one } protected theorem inv_pow : ∀ {a : ℝ≥0∞} {n : ℕ}, (a ^ n)⁻¹ = a⁻¹ ^ n | _, 0 => by simp only [pow_zero, inv_one] | ⊤, n + 1 => by simp [top_pow] | (a : ℝ≥0), n + 1 => by rcases eq_or_ne a 0 with (rfl | ha) · simp [top_pow] · have := pow_ne_zero (n + 1) ha norm_cast rw [inv_pow] protected theorem mul_inv_cancel (h0 : a ≠ 0) (ht : a ≠ ∞) : a * a⁻¹ = 1 := by lift a to ℝ≥0 using ht norm_cast at h0; norm_cast exact mul_inv_cancel₀ h0 protected theorem inv_mul_cancel (h0 : a ≠ 0) (ht : a ≠ ∞) : a⁻¹ * a = 1 := mul_comm a a⁻¹ ▸ ENNReal.mul_inv_cancel h0 ht /-- See `ENNReal.inv_mul_cancel_left` for a simpler version assuming `a ≠ 0`, `a ≠ ∞`. -/ protected lemma inv_mul_cancel_left' (ha₀ : a = 0 → b = 0) (ha : a = ∞ → b = 0) : a⁻¹ * (a * b) = b := by obtain rfl | ha₀ := eq_or_ne a 0 · simp_all obtain rfl | ha := eq_or_ne a ⊤ · simp_all · simp [← mul_assoc, ENNReal.inv_mul_cancel, *] /-- See `ENNReal.inv_mul_cancel_left'` for a stronger version. -/ protected lemma inv_mul_cancel_left (ha₀ : a ≠ 0) (ha : a ≠ ∞) : a⁻¹ * (a * b) = b := ENNReal.inv_mul_cancel_left' (by simp [ha₀]) (by simp [ha]) /-- See `ENNReal.mul_inv_cancel_left` for a simpler version assuming `a ≠ 0`, `a ≠ ∞`. -/ protected lemma mul_inv_cancel_left' (ha₀ : a = 0 → b = 0) (ha : a = ∞ → b = 0) : a * (a⁻¹ * b) = b := by obtain rfl | ha₀ := eq_or_ne a 0 · simp_all obtain rfl | ha := eq_or_ne a ⊤ · simp_all · simp [← mul_assoc, ENNReal.mul_inv_cancel, *] /-- See `ENNReal.mul_inv_cancel_left'` for a stronger version. -/ protected lemma mul_inv_cancel_left (ha₀ : a ≠ 0) (ha : a ≠ ∞) : a * (a⁻¹ * b) = b := ENNReal.mul_inv_cancel_left' (by simp [ha₀]) (by simp [ha]) /-- See `ENNReal.mul_inv_cancel_right` for a simpler version assuming `b ≠ 0`, `b ≠ ∞`. -/ protected lemma mul_inv_cancel_right' (hb₀ : b = 0 → a = 0) (hb : b = ∞ → a = 0) : a * b * b⁻¹ = a := by obtain rfl | hb₀ := eq_or_ne b 0 · simp_all obtain rfl | hb := eq_or_ne b ⊤ · simp_all · simp [mul_assoc, ENNReal.mul_inv_cancel, *] /-- See `ENNReal.mul_inv_cancel_right'` for a stronger version. -/ protected lemma mul_inv_cancel_right (hb₀ : b ≠ 0) (hb : b ≠ ∞) : a * b * b⁻¹ = a := ENNReal.mul_inv_cancel_right' (by simp [hb₀]) (by simp [hb]) /-- See `ENNReal.inv_mul_cancel_right` for a simpler version assuming `b ≠ 0`, `b ≠ ∞`. -/ protected lemma inv_mul_cancel_right' (hb₀ : b = 0 → a = 0) (hb : b = ∞ → a = 0) : a * b⁻¹ * b = a := by obtain rfl | hb₀ := eq_or_ne b 0 · simp_all obtain rfl | hb := eq_or_ne b ⊤ · simp_all · simp [mul_assoc, ENNReal.inv_mul_cancel, *] /-- See `ENNReal.inv_mul_cancel_right'` for a stronger version. -/ protected lemma inv_mul_cancel_right (hb₀ : b ≠ 0) (hb : b ≠ ∞) : a * b⁻¹ * b = a := ENNReal.inv_mul_cancel_right' (by simp [hb₀]) (by simp [hb]) /-- See `ENNReal.mul_div_cancel_right` for a simpler version assuming `b ≠ 0`, `b ≠ ∞`. -/ protected lemma mul_div_cancel_right' (hb₀ : b = 0 → a = 0) (hb : b = ∞ → a = 0) : a * b / b = a := ENNReal.mul_inv_cancel_right' hb₀ hb /-- See `ENNReal.mul_div_cancel_right'` for a stronger version. -/ protected lemma mul_div_cancel_right (hb₀ : b ≠ 0) (hb : b ≠ ∞) : a * b / b = a := ENNReal.mul_div_cancel_right' (by simp [hb₀]) (by simp [hb]) /-- See `ENNReal.div_mul_cancel` for a simpler version assuming `a ≠ 0`, `a ≠ ∞`. -/ protected lemma div_mul_cancel' (ha₀ : a = 0 → b = 0) (ha : a = ∞ → b = 0) : b / a * a = b := ENNReal.inv_mul_cancel_right' ha₀ ha /-- See `ENNReal.div_mul_cancel'` for a stronger version. -/ protected lemma div_mul_cancel (ha₀ : a ≠ 0) (ha : a ≠ ∞) : b / a * a = b := ENNReal.div_mul_cancel' (by simp [ha₀]) (by simp [ha]) /-- See `ENNReal.mul_div_cancel` for a simpler version assuming `a ≠ 0`, `a ≠ ∞`. -/ protected lemma mul_div_cancel' (ha₀ : a = 0 → b = 0) (ha : a = ∞ → b = 0) : a * (b / a) = b := by rw [mul_comm, ENNReal.div_mul_cancel' ha₀ ha] /-- See `ENNReal.mul_div_cancel'` for a stronger version. -/ protected lemma mul_div_cancel (ha₀ : a ≠ 0) (ha : a ≠ ∞) : a * (b / a) = b := ENNReal.mul_div_cancel' (by simp [ha₀]) (by simp [ha]) protected theorem mul_comm_div : a / b * c = a * (c / b) := by simp only [div_eq_mul_inv, mul_left_comm, mul_comm, mul_assoc] protected theorem mul_div_right_comm : a * b / c = a / c * b := by simp only [div_eq_mul_inv, mul_right_comm] instance : InvolutiveInv ℝ≥0∞ where inv_inv a := by by_cases a = 0 <;> cases a <;> simp_all [none_eq_top, some_eq_coe, -coe_inv, (coe_inv _).symm] @[simp] protected lemma inv_eq_one : a⁻¹ = 1 ↔ a = 1 := by rw [← inv_inj, inv_inv, inv_one] @[simp] theorem inv_eq_top : a⁻¹ = ∞ ↔ a = 0 := inv_zero ▸ inv_inj theorem inv_ne_top : a⁻¹ ≠ ∞ ↔ a ≠ 0 := by simp @[aesop (rule_sets := [finiteness]) safe apply] protected alias ⟨_, Finiteness.inv_ne_top⟩ := ENNReal.inv_ne_top @[simp] theorem inv_lt_top {x : ℝ≥0∞} : x⁻¹ < ∞ ↔ 0 < x := by simp only [lt_top_iff_ne_top, inv_ne_top, pos_iff_ne_zero] theorem div_lt_top {x y : ℝ≥0∞} (h1 : x ≠ ∞) (h2 : y ≠ 0) : x / y < ∞ := mul_lt_top h1.lt_top (inv_ne_top.mpr h2).lt_top @[simp] protected theorem inv_eq_zero : a⁻¹ = 0 ↔ a = ∞ := inv_top ▸ inv_inj protected theorem inv_ne_zero : a⁻¹ ≠ 0 ↔ a ≠ ∞ := by simp protected theorem div_pos (ha : a ≠ 0) (hb : b ≠ ∞) : 0 < a / b := ENNReal.mul_pos ha <| ENNReal.inv_ne_zero.2 hb protected theorem inv_mul_le_iff {x y z : ℝ≥0∞} (h1 : x ≠ 0) (h2 : x ≠ ∞) : x⁻¹ * y ≤ z ↔ y ≤ x * z := by rw [← mul_le_mul_left h1 h2, ← mul_assoc, ENNReal.mul_inv_cancel h1 h2, one_mul] protected theorem mul_inv_le_iff {x y z : ℝ≥0∞} (h1 : y ≠ 0) (h2 : y ≠ ∞) : x * y⁻¹ ≤ z ↔ x ≤ z * y := by
rw [mul_comm, ENNReal.inv_mul_le_iff h1 h2, mul_comm] protected theorem div_le_iff {x y z : ℝ≥0∞} (h1 : y ≠ 0) (h2 : y ≠ ∞) : x / y ≤ z ↔ x ≤ z * y := by rw [div_eq_mul_inv, ENNReal.mul_inv_le_iff h1 h2] protected theorem div_le_iff' {x y z : ℝ≥0∞} (h1 : y ≠ 0) (h2 : y ≠ ∞) : x / y ≤ z ↔ x ≤ y * z := by
Mathlib/Data/ENNReal/Inv.lean
214
221
/- Copyright (c) 2024 David Loeffler. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alex Kontorovich, David Loeffler, Heather Macbeth, Sébastien Gouëzel -/ import Mathlib.Analysis.Calculus.ParametricIntegral import Mathlib.Analysis.Calculus.ContDiff.CPolynomial import Mathlib.Analysis.Fourier.AddCircle import Mathlib.Analysis.Fourier.FourierTransform import Mathlib.Analysis.Calculus.FDeriv.Analytic import Mathlib.Analysis.Calculus.LineDeriv.IntegrationByParts import Mathlib.Analysis.Calculus.ContDiff.Bounds /-! # Derivatives of the Fourier transform In this file we compute the Fréchet derivative of the Fourier transform of `f`, where `f` is a function such that both `f` and `v ↦ ‖v‖ * ‖f v‖` are integrable. Here the Fourier transform is understood as an operator `(V → E) → (W → E)`, where `V` and `W` are normed `ℝ`-vector spaces and the Fourier transform is taken with respect to a continuous `ℝ`-bilinear pairing `L : V × W → ℝ` and a given reference measure `μ`. We also investigate higher derivatives: Assuming that `‖v‖^n * ‖f v‖` is integrable, we show that the Fourier transform of `f` is `C^n`. We also study in a parallel way the Fourier transform of the derivative, which is obtained by tensoring the Fourier transform of the original function with the bilinear form. We also get results for iterated derivatives. A consequence of these results is that, if a function is smooth and all its derivatives are integrable when multiplied by `‖v‖^k`, then the same goes for its Fourier transform, with explicit bounds. We give specialized versions of these results on inner product spaces (where `L` is the scalar product) and on the real line, where we express the one-dimensional derivative in more concrete terms, as the Fourier transform of `-2πI x * f x` (or `(-2πI x)^n * f x` for higher derivatives). ## Main definitions and results We introduce two convenience definitions: * `VectorFourier.fourierSMulRight L f`: given `f : V → E` and `L` a bilinear pairing between `V` and `W`, then this is the function `fun v ↦ -(2 * π * I) (L v ⬝) • f v`, from `V` to `Hom (W, E)`. This is essentially `ContinuousLinearMap.smulRight`, up to the factor `- 2πI` designed to make sure that the Fourier integral of `fourierSMulRight L f` is the derivative of the Fourier integral of `f`. * `VectorFourier.fourierPowSMulRight` is the higher order analogue for higher derivatives: `fourierPowSMulRight L f v n` is informally `(-(2 * π * I))^n (L v ⬝)^n • f v`, in the space of continuous multilinear maps `W [×n]→L[ℝ] E`. With these definitions, the statements read as follows, first in a general context (arbitrary `L` and `μ`): * `VectorFourier.hasFDerivAt_fourierIntegral`: the Fourier integral of `f` is differentiable, with derivative the Fourier integral of `fourierSMulRight L f`. * `VectorFourier.differentiable_fourierIntegral`: the Fourier integral of `f` is differentiable. * `VectorFourier.fderiv_fourierIntegral`: formula for the derivative of the Fourier integral of `f`. * `VectorFourier.fourierIntegral_fderiv`: formula for the Fourier integral of the derivative of `f`. * `VectorFourier.hasFTaylorSeriesUpTo_fourierIntegral`: under suitable integrability conditions, the Fourier integral of `f` has an explicit Taylor series up to order `N`, given by the Fourier integrals of `fun v ↦ fourierPowSMulRight L f v n`. * `VectorFourier.contDiff_fourierIntegral`: under suitable integrability conditions, the Fourier integral of `f` is `C^n`. * `VectorFourier.iteratedFDeriv_fourierIntegral`: under suitable integrability conditions, explicit formula for the `n`-th derivative of the Fourier integral of `f`, as the Fourier integral of `fun v ↦ fourierPowSMulRight L f v n`. * `VectorFourier.pow_mul_norm_iteratedFDeriv_fourierIntegral_le`: explicit bounds for the `n`-th derivative of the Fourier integral, multiplied by a power function, in terms of corresponding integrals for the original function. These statements are then specialized to the case of the usual Fourier transform on finite-dimensional inner product spaces with their canonical Lebesgue measure (covering in particular the case of the real line), replacing the namespace `VectorFourier` by the namespace `Real` in the above statements. We also give specialized versions of the one-dimensional real derivative (and iterated derivative) in `Real.deriv_fourierIntegral` and `Real.iteratedDeriv_fourierIntegral`. -/ noncomputable section open Real Complex MeasureTheory Filter TopologicalSpace open scoped FourierTransform Topology ContDiff -- without this local instance, Lean tries first the instance -- `secondCountableTopologyEither_of_right` (whose priority is 100) and takes a very long time to -- fail. Since we only use the left instance in this file, we make sure it is tried first. attribute [local instance 101] secondCountableTopologyEither_of_left namespace Real lemma hasDerivAt_fourierChar (x : ℝ) : HasDerivAt (𝐞 · : ℝ → ℂ) (2 * π * I * 𝐞 x) x := by have h1 (y : ℝ) : 𝐞 y = fourier 1 (y : UnitAddCircle) := by rw [fourierChar_apply, fourier_coe_apply] push_cast ring_nf simpa only [h1, Int.cast_one, ofReal_one, div_one, mul_one] using hasDerivAt_fourier 1 1 x lemma differentiable_fourierChar : Differentiable ℝ (𝐞 · : ℝ → ℂ) := fun x ↦ (Real.hasDerivAt_fourierChar x).differentiableAt lemma deriv_fourierChar (x : ℝ) : deriv (𝐞 · : ℝ → ℂ) x = 2 * π * I * 𝐞 x := (Real.hasDerivAt_fourierChar x).deriv variable {V W : Type*} [NormedAddCommGroup V] [NormedSpace ℝ V] [NormedAddCommGroup W] [NormedSpace ℝ W] (L : V →L[ℝ] W →L[ℝ] ℝ) lemma hasFDerivAt_fourierChar_neg_bilinear_right (v : V) (w : W) : HasFDerivAt (fun w ↦ (𝐞 (-L v w) : ℂ)) ((-2 * π * I * 𝐞 (-L v w)) • (ofRealCLM ∘L (L v))) w := by have ha : HasFDerivAt (fun w' : W ↦ L v w') (L v) w := ContinuousLinearMap.hasFDerivAt (L v) convert (hasDerivAt_fourierChar (-L v w)).hasFDerivAt.comp w ha.neg using 1 ext y simp only [neg_mul, ContinuousLinearMap.coe_smul', ContinuousLinearMap.coe_comp', Pi.smul_apply, Function.comp_apply, ofRealCLM_apply, smul_eq_mul, ContinuousLinearMap.comp_neg, ContinuousLinearMap.neg_apply, ContinuousLinearMap.smulRight_apply, ContinuousLinearMap.one_apply, real_smul, neg_inj] ring lemma fderiv_fourierChar_neg_bilinear_right_apply (v : V) (w y : W) : fderiv ℝ (fun w ↦ (𝐞 (-L v w) : ℂ)) w y = -2 * π * I * L v y * 𝐞 (-L v w) := by simp only [(hasFDerivAt_fourierChar_neg_bilinear_right L v w).fderiv, neg_mul, ContinuousLinearMap.coe_smul', ContinuousLinearMap.coe_comp', Pi.smul_apply, Function.comp_apply, ofRealCLM_apply, smul_eq_mul, neg_inj] ring lemma differentiable_fourierChar_neg_bilinear_right (v : V) : Differentiable ℝ (fun w ↦ (𝐞 (-L v w) : ℂ)) := fun w ↦ (hasFDerivAt_fourierChar_neg_bilinear_right L v w).differentiableAt lemma hasFDerivAt_fourierChar_neg_bilinear_left (v : V) (w : W) : HasFDerivAt (fun v ↦ (𝐞 (-L v w) : ℂ)) ((-2 * π * I * 𝐞 (-L v w)) • (ofRealCLM ∘L (L.flip w))) v := hasFDerivAt_fourierChar_neg_bilinear_right L.flip w v lemma fderiv_fourierChar_neg_bilinear_left_apply (v y : V) (w : W) : fderiv ℝ (fun v ↦ (𝐞 (-L v w) : ℂ)) v y = -2 * π * I * L y w * 𝐞 (-L v w) := by simp only [(hasFDerivAt_fourierChar_neg_bilinear_left L v w).fderiv, neg_mul, ContinuousLinearMap.coe_smul', ContinuousLinearMap.coe_comp', Pi.smul_apply, Function.comp_apply, ContinuousLinearMap.flip_apply, ofRealCLM_apply, smul_eq_mul, neg_inj] ring lemma differentiable_fourierChar_neg_bilinear_left (w : W) : Differentiable ℝ (fun v ↦ (𝐞 (-L v w) : ℂ)) := fun v ↦ (hasFDerivAt_fourierChar_neg_bilinear_left L v w).differentiableAt end Real variable {E : Type*} [NormedAddCommGroup E] [NormedSpace ℂ E] namespace VectorFourier variable {V W : Type*} [NormedAddCommGroup V] [NormedSpace ℝ V] [NormedAddCommGroup W] [NormedSpace ℝ W] (L : V →L[ℝ] W →L[ℝ] ℝ) (f : V → E) /-- Send a function `f : V → E` to the function `f : V → Hom (W, E)` given by `v ↦ (w ↦ -2 * π * I * L (v, w) • f v)`. This is designed so that the Fourier transform of `fourierSMulRight L f` is the derivative of the Fourier transform of `f`. -/ def fourierSMulRight (v : V) : (W →L[ℝ] E) := -(2 * π * I) • (L v).smulRight (f v) @[simp] lemma fourierSMulRight_apply (v : V) (w : W) : fourierSMulRight L f v w = -(2 * π * I) • L v w • f v := rfl /-- The `w`-derivative of the Fourier transform integrand. -/ lemma hasFDerivAt_fourierChar_smul (v : V) (w : W) : HasFDerivAt (fun w' ↦ 𝐞 (-L v w') • f v) (𝐞 (-L v w) • fourierSMulRight L f v) w := by have ha : HasFDerivAt (fun w' : W ↦ L v w') (L v) w := ContinuousLinearMap.hasFDerivAt (L v) convert ((hasDerivAt_fourierChar (-L v w)).hasFDerivAt.comp w ha.neg).smul_const (f v) ext w' : 1 simp_rw [fourierSMulRight, ContinuousLinearMap.smul_apply, ContinuousLinearMap.smulRight_apply] rw [ContinuousLinearMap.comp_apply, ContinuousLinearMap.neg_apply, ContinuousLinearMap.smulRight_apply, ContinuousLinearMap.one_apply, ← smul_assoc, smul_comm, ← smul_assoc, real_smul, real_smul, Submonoid.smul_def, smul_eq_mul] push_cast ring_nf lemma norm_fourierSMulRight (L : V →L[ℝ] W →L[ℝ] ℝ) (f : V → E) (v : V) : ‖fourierSMulRight L f v‖ = (2 * π) * ‖L v‖ * ‖f v‖ := by rw [fourierSMulRight, norm_smul _ (ContinuousLinearMap.smulRight (L v) (f v)), norm_neg, norm_mul, norm_mul, norm_I, mul_one, Complex.norm_of_nonneg pi_pos.le, Complex.norm_two, ContinuousLinearMap.norm_smulRight_apply, ← mul_assoc] lemma norm_fourierSMulRight_le (L : V →L[ℝ] W →L[ℝ] ℝ) (f : V → E) (v : V) : ‖fourierSMulRight L f v‖ ≤ 2 * π * ‖L‖ * ‖v‖ * ‖f v‖ := calc ‖fourierSMulRight L f v‖ = (2 * π) * ‖L v‖ * ‖f v‖ := norm_fourierSMulRight _ _ _ _ ≤ (2 * π) * (‖L‖ * ‖v‖) * ‖f v‖ := by gcongr; exact L.le_opNorm _ _ = 2 * π * ‖L‖ * ‖v‖ * ‖f v‖ := by ring lemma _root_.MeasureTheory.AEStronglyMeasurable.fourierSMulRight [SecondCountableTopologyEither V (W →L[ℝ] ℝ)] [MeasurableSpace V] [BorelSpace V] {L : V →L[ℝ] W →L[ℝ] ℝ} {f : V → E} {μ : Measure V} (hf : AEStronglyMeasurable f μ) : AEStronglyMeasurable (fun v ↦ fourierSMulRight L f v) μ := by apply AEStronglyMeasurable.const_smul' have aux0 : Continuous fun p : (W →L[ℝ] ℝ) × E ↦ p.1.smulRight p.2 := (ContinuousLinearMap.smulRightL ℝ W E).continuous₂ have aux1 : AEStronglyMeasurable (fun v ↦ (L v, f v)) μ := L.continuous.aestronglyMeasurable.prodMk hf -- Elaboration without the expected type is faster here: exact (aux0.comp_aestronglyMeasurable aux1 :) variable {f} /-- Main theorem of this section: if both `f` and `x ↦ ‖x‖ * ‖f x‖` are integrable, then the Fourier transform of `f` has a Fréchet derivative (everywhere in its domain) and its derivative is the Fourier transform of `smulRight L f`. -/ theorem hasFDerivAt_fourierIntegral [MeasurableSpace V] [BorelSpace V] [SecondCountableTopology V] {μ : Measure V} (hf : Integrable f μ) (hf' : Integrable (fun v : V ↦ ‖v‖ * ‖f v‖) μ) (w : W) : HasFDerivAt (fourierIntegral 𝐞 μ L.toLinearMap₂ f) (fourierIntegral 𝐞 μ L.toLinearMap₂ (fourierSMulRight L f) w) w := by let F : W → V → E := fun w' v ↦ 𝐞 (-L v w') • f v let F' : W → V → W →L[ℝ] E := fun w' v ↦ 𝐞 (-L v w') • fourierSMulRight L f v let B : V → ℝ := fun v ↦ 2 * π * ‖L‖ * ‖v‖ * ‖f v‖ have h0 (w' : W) : Integrable (F w') μ := (fourierIntegral_convergent_iff continuous_fourierChar (by apply L.continuous₂ : Continuous (fun p : V × W ↦ L.toLinearMap₂ p.1 p.2)) w').2 hf have h1 : ∀ᶠ w' in 𝓝 w, AEStronglyMeasurable (F w') μ := Eventually.of_forall (fun w' ↦ (h0 w').aestronglyMeasurable) have h3 : AEStronglyMeasurable (F' w) μ := by refine .smul ?_ hf.1.fourierSMulRight refine (continuous_fourierChar.comp ?_).aestronglyMeasurable fun_prop have h4 : (∀ᵐ v ∂μ, ∀ (w' : W), w' ∈ Metric.ball w 1 → ‖F' w' v‖ ≤ B v) := by filter_upwards with v w' _ rw [Circle.norm_smul _ (fourierSMulRight L f v)] exact norm_fourierSMulRight_le L f v have h5 : Integrable B μ := by simpa only [← mul_assoc] using hf'.const_mul (2 * π * ‖L‖) have h6 : ∀ᵐ v ∂μ, ∀ w', w' ∈ Metric.ball w 1 → HasFDerivAt (fun x ↦ F x v) (F' w' v) w' := ae_of_all _ (fun v w' _ ↦ hasFDerivAt_fourierChar_smul L f v w') exact hasFDerivAt_integral_of_dominated_of_fderiv_le one_pos h1 (h0 w) h3 h4 h5 h6 lemma fderiv_fourierIntegral [MeasurableSpace V] [BorelSpace V] [SecondCountableTopology V] {μ : Measure V} (hf : Integrable f μ) (hf' : Integrable (fun v : V ↦ ‖v‖ * ‖f v‖) μ) : fderiv ℝ (fourierIntegral 𝐞 μ L.toLinearMap₂ f) = fourierIntegral 𝐞 μ L.toLinearMap₂ (fourierSMulRight L f) := by ext w : 1 exact (hasFDerivAt_fourierIntegral L hf hf' w).fderiv lemma differentiable_fourierIntegral [MeasurableSpace V] [BorelSpace V] [SecondCountableTopology V] {μ : Measure V} (hf : Integrable f μ) (hf' : Integrable (fun v : V ↦ ‖v‖ * ‖f v‖) μ) : Differentiable ℝ (fourierIntegral 𝐞 μ L.toLinearMap₂ f) := fun w ↦ (hasFDerivAt_fourierIntegral L hf hf' w).differentiableAt /-- The Fourier integral of the derivative of a function is obtained by multiplying the Fourier integral of the original function by `-L w v`. -/ theorem fourierIntegral_fderiv [MeasurableSpace V] [BorelSpace V] [FiniteDimensional ℝ V] {μ : Measure V} [Measure.IsAddHaarMeasure μ] (hf : Integrable f μ) (h'f : Differentiable ℝ f) (hf' : Integrable (fderiv ℝ f) μ) : fourierIntegral 𝐞 μ L.toLinearMap₂ (fderiv ℝ f) = fourierSMulRight (-L.flip) (fourierIntegral 𝐞 μ L.toLinearMap₂ f) := by ext w y let g (v : V) : ℂ := 𝐞 (-L v w) /- First rewrite things in a simplified form, without any real change. -/ suffices ∫ x, g x • fderiv ℝ f x y ∂μ = ∫ x, (2 * ↑π * I * L y w * g x) • f x ∂μ by rw [fourierIntegral_continuousLinearMap_apply' hf'] simpa only [fourierIntegral, ContinuousLinearMap.toLinearMap₂_apply, fourierSMulRight_apply, ContinuousLinearMap.neg_apply, ContinuousLinearMap.flip_apply, ← integral_smul, neg_smul, smul_neg, ← smul_smul, coe_smul, neg_neg] -- Key step: integrate by parts with respect to `y` to switch the derivative from `f` to `g`. have A x : fderiv ℝ g x y = - 2 * ↑π * I * L y w * g x := fderiv_fourierChar_neg_bilinear_left_apply _ _ _ _ rw [integral_smul_fderiv_eq_neg_fderiv_smul_of_integrable, ← integral_neg] · congr with x simp only [A, neg_mul, neg_smul, neg_neg] · have : Integrable (fun x ↦ (-(2 * ↑π * I * ↑((L y) w)) • ((g x : ℂ) • f x))) μ := ((fourierIntegral_convergent_iff' _ _).2 hf).smul _ convert this using 2 with x simp only [A, neg_mul, neg_smul, smul_smul] · exact (fourierIntegral_convergent_iff' _ _).2 (hf'.apply_continuousLinearMap _) · exact (fourierIntegral_convergent_iff' _ _).2 hf · exact differentiable_fourierChar_neg_bilinear_left _ _ · exact h'f /-- The formal multilinear series whose `n`-th term is `(w₁, ..., wₙ) ↦ (-2πI)^n * L v w₁ * ... * L v wₙ • f v`, as a continuous multilinear map in the space `W [×n]→L[ℝ] E`. This is designed so that the Fourier transform of `v ↦ fourierPowSMulRight L f v n` is the `n`-th derivative of the Fourier transform of `f`. -/ def fourierPowSMulRight (f : V → E) (v : V) : FormalMultilinearSeries ℝ W E := fun n ↦ (- (2 * π * I))^n • ((ContinuousMultilinearMap.mkPiRing ℝ (Fin n) (f v)).compContinuousLinearMap (fun _ ↦ L v)) /- Increase the priority to make sure that this lemma is used instead of `FormalMultilinearSeries.apply_eq_prod_smul_coeff` even in dimension 1. -/ @[simp 1100] lemma fourierPowSMulRight_apply {f : V → E} {v : V} {n : ℕ} {m : Fin n → W} : fourierPowSMulRight L f v n m = (- (2 * π * I))^n • (∏ i, L v (m i)) • f v := by simp [fourierPowSMulRight] open ContinuousMultilinearMap /-- Decomposing `fourierPowSMulRight L f v n` as a composition of continuous bilinear and multilinear maps, to deduce easily its continuity and differentiability properties. -/ lemma fourierPowSMulRight_eq_comp {f : V → E} {v : V} {n : ℕ} : fourierPowSMulRight L f v n = (- (2 * π * I))^n • smulRightL ℝ (fun (_ : Fin n) ↦ W) E (compContinuousLinearMapLRight (ContinuousMultilinearMap.mkPiAlgebra ℝ (Fin n) ℝ) (fun _ ↦ L v)) (f v) := rfl @[continuity, fun_prop] lemma _root_.Continuous.fourierPowSMulRight {f : V → E} (hf : Continuous f) (n : ℕ) : Continuous (fun v ↦ fourierPowSMulRight L f v n) := by simp_rw [fourierPowSMulRight_eq_comp] apply Continuous.const_smul apply (smulRightL ℝ (fun (_ : Fin n) ↦ W) E).continuous₂.comp₂ _ hf exact Continuous.comp (map_continuous _) (continuous_pi (fun _ ↦ L.continuous)) lemma _root_.ContDiff.fourierPowSMulRight {f : V → E} {k : WithTop ℕ∞} (hf : ContDiff ℝ k f) (n : ℕ) : ContDiff ℝ k (fun v ↦ fourierPowSMulRight L f v n) := by simp_rw [fourierPowSMulRight_eq_comp] apply ContDiff.const_smul apply (smulRightL ℝ (fun (_ : Fin n) ↦ W) E).isBoundedBilinearMap.contDiff.comp₂ _ hf apply (ContinuousMultilinearMap.contDiff _).comp exact contDiff_pi.2 (fun _ ↦ L.contDiff) lemma norm_fourierPowSMulRight_le (f : V → E) (v : V) (n : ℕ) : ‖fourierPowSMulRight L f v n‖ ≤ (2 * π * ‖L‖) ^ n * ‖v‖ ^ n * ‖f v‖ := by apply ContinuousMultilinearMap.opNorm_le_bound (by positivity) (fun m ↦ ?_) calc ‖fourierPowSMulRight L f v n m‖ = (2 * π) ^ n * ((∏ x : Fin n, |(L v) (m x)|) * ‖f v‖) := by simp [abs_of_nonneg pi_nonneg, norm_smul] _ ≤ (2 * π) ^ n * ((∏ x : Fin n, ‖L‖ * ‖v‖ * ‖m x‖) * ‖f v‖) := by gcongr with i _hi exact L.le_opNorm₂ v (m i) _ = (2 * π * ‖L‖) ^ n * ‖v‖ ^ n * ‖f v‖ * ∏ i : Fin n, ‖m i‖ := by simp [Finset.prod_mul_distrib, mul_pow]; ring /-- The iterated derivative of a function multiplied by `(L v ⬝) ^ n` can be controlled in terms of the iterated derivatives of the initial function. -/ lemma norm_iteratedFDeriv_fourierPowSMulRight {f : V → E} {K : WithTop ℕ∞} {C : ℝ} (hf : ContDiff ℝ K f) {n : ℕ} {k : ℕ} (hk : k ≤ K) {v : V} (hv : ∀ i ≤ k, ∀ j ≤ n, ‖v‖ ^ j * ‖iteratedFDeriv ℝ i f v‖ ≤ C) : ‖iteratedFDeriv ℝ k (fun v ↦ fourierPowSMulRight L f v n) v‖ ≤ (2 * π) ^ n * (2 * n + 2) ^ k * ‖L‖ ^ n * C := by /- We write `fourierPowSMulRight L f v n` as a composition of bilinear and multilinear maps, thanks to `fourierPowSMulRight_eq_comp`, and then we control the iterated derivatives of these thanks to general bounds on derivatives of bilinear and multilinear maps. More precisely, `fourierPowSMulRight L f v n m = (- (2 * π * I))^n • (∏ i, L v (m i)) • f v`. Here, `(- (2 * π * I))^n` contributes `(2π)^n` to the bound. The second product is bilinear, so the iterated derivative is controlled as a weighted sum of those of `v ↦ ∏ i, L v (m i)` and of `f`. The harder part is to control the iterated derivatives of `v ↦ ∏ i, L v (m i)`. For this, one argues that this is multilinear in `v`, to apply general bounds for iterated derivatives of multilinear maps. More precisely, we write it as the composition of a multilinear map `T` (making the product operation) and the tuple of linear maps `v ↦ (L v ⬝, ..., L v ⬝)` -/ simp_rw [fourierPowSMulRight_eq_comp] -- first step: controlling the iterated derivatives of `v ↦ ∏ i, L v (m i)`, written below -- as `v ↦ T (fun _ ↦ L v)`, or `T ∘ (ContinuousLinearMap.pi (fun (_ : Fin n) ↦ L))`. let T : (W →L[ℝ] ℝ) [×n]→L[ℝ] (W [×n]→L[ℝ] ℝ) := compContinuousLinearMapLRight (ContinuousMultilinearMap.mkPiAlgebra ℝ (Fin n) ℝ) have I₁ m : ‖iteratedFDeriv ℝ m T (fun _ ↦ L v)‖ ≤ n.descFactorial m * 1 * (‖L‖ * ‖v‖) ^ (n - m) := by have : ‖T‖ ≤ 1 := by apply (norm_compContinuousLinearMapLRight_le _ _).trans simp only [norm_mkPiAlgebra, le_refl] apply (ContinuousMultilinearMap.norm_iteratedFDeriv_le _ _ _).trans simp only [Fintype.card_fin] gcongr refine (pi_norm_le_iff_of_nonneg (by positivity)).mpr (fun _ ↦ ?_) exact ContinuousLinearMap.le_opNorm _ _ have I₂ m : ‖iteratedFDeriv ℝ m (T ∘ (ContinuousLinearMap.pi (fun (_ : Fin n) ↦ L))) v‖ ≤ (n.descFactorial m * 1 * (‖L‖ * ‖v‖) ^ (n - m)) * ‖L‖ ^ m := by rw [ContinuousLinearMap.iteratedFDeriv_comp_right _ (ContinuousMultilinearMap.contDiff _) _ (mod_cast le_top)] apply (norm_compContinuousLinearMap_le _ _).trans simp only [Finset.prod_const, Finset.card_fin] gcongr · exact I₁ m · exact ContinuousLinearMap.norm_pi_le_of_le (fun _ ↦ le_rfl) (norm_nonneg _) have I₃ m : ‖iteratedFDeriv ℝ m (T ∘ (ContinuousLinearMap.pi (fun (_ : Fin n) ↦ L))) v‖ ≤ n.descFactorial m * ‖L‖ ^ n * ‖v‖ ^ (n - m) := by apply (I₂ m).trans (le_of_eq _) rcases le_or_lt m n with hm | hm · rw [show ‖L‖ ^ n = ‖L‖ ^ (m + (n - m)) by rw [Nat.add_sub_cancel' hm], pow_add] ring · simp only [Nat.descFactorial_eq_zero_iff_lt.mpr hm, CharP.cast_eq_zero, mul_one, zero_mul] -- second step: factor out the `(2 * π) ^ n` factor, and cancel it on both sides. have A : ContDiff ℝ K (fun y ↦ T (fun _ ↦ L y)) := (ContinuousMultilinearMap.contDiff _).comp (contDiff_pi.2 fun _ ↦ L.contDiff) rw [iteratedFDeriv_const_smul_apply' (hf := ((smulRightL ℝ (fun _ ↦ W) E).isBoundedBilinearMap.contDiff.comp₂ (A.of_le hk) (hf.of_le hk)).contDiffAt), norm_smul (β := V [×k]→L[ℝ] (W [×n]→L[ℝ] E))] simp only [mul_assoc, norm_pow, norm_neg, Complex.norm_mul, Complex.norm_ofNat, norm_real, Real.norm_eq_abs, abs_of_nonneg pi_nonneg, norm_I, mul_one, smulRightL_apply, ge_iff_le] gcongr -- third step: argue that the scalar multiplication is bilinear to bound the iterated derivatives -- of `v ↦ (∏ i, L v (m i)) • f v` in terms of those of `v ↦ (∏ i, L v (m i))` and of `f`. -- The former are controlled by the first step, the latter by the assumptions. apply (ContinuousLinearMap.norm_iteratedFDeriv_le_of_bilinear_of_le_one _ A hf _ hk ContinuousMultilinearMap.norm_smulRightL_le).trans calc ∑ i ∈ Finset.range (k + 1), k.choose i * ‖iteratedFDeriv ℝ i (fun (y : V) ↦ T (fun _ ↦ L y)) v‖ * ‖iteratedFDeriv ℝ (k - i) f v‖ ≤ ∑ i ∈ Finset.range (k + 1), k.choose i * (n.descFactorial i * ‖L‖ ^ n * ‖v‖ ^ (n - i)) * ‖iteratedFDeriv ℝ (k - i) f v‖ := by gcongr with i _hi exact I₃ i _ = ∑ i ∈ Finset.range (k + 1), (k.choose i * n.descFactorial i * ‖L‖ ^ n) * (‖v‖ ^ (n - i) * ‖iteratedFDeriv ℝ (k - i) f v‖) := by congr with i ring _ ≤ ∑ i ∈ Finset.range (k + 1), (k.choose i * (n + 1 : ℕ) ^ k * ‖L‖ ^ n) * C := by gcongr with i hi · rw [← Nat.cast_pow, Nat.cast_le] calc n.descFactorial i ≤ n ^ i := Nat.descFactorial_le_pow _ _ _ ≤ (n + 1) ^ i := by gcongr; omega _ ≤ (n + 1) ^ k := by gcongr; exacts [le_add_self, Finset.mem_range_succ_iff.mp hi] · exact hv _ (by omega) _ (by omega) _ = (2 * n + 2) ^ k * (‖L‖^n * C) := by simp only [← Finset.sum_mul, ← Nat.cast_sum, Nat.sum_range_choose, mul_one, ← mul_assoc, Nat.cast_pow, Nat.cast_ofNat, Nat.cast_add, Nat.cast_one, ← mul_pow, mul_add] variable [MeasurableSpace V] [BorelSpace V] {μ : Measure V} section SecondCountableTopology
variable [SecondCountableTopology V] lemma _root_.MeasureTheory.AEStronglyMeasurable.fourierPowSMulRight (hf : AEStronglyMeasurable f μ) (n : ℕ) : AEStronglyMeasurable (fun v ↦ fourierPowSMulRight L f v n) μ := by simp_rw [fourierPowSMulRight_eq_comp] apply AEStronglyMeasurable.const_smul'
Mathlib/Analysis/Fourier/FourierTransformDeriv.lean
425
432
/- Copyright (c) 2019 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Yury Kudryashov -/ import Mathlib.Data.Set.Lattice.Image import Mathlib.Order.Interval.Set.LinearOrder /-! # Extra lemmas about intervals This file contains lemmas about intervals that cannot be included into `Order.Interval.Set.Basic` because this would create an `import` cycle. Namely, lemmas in this file can use definitions from `Data.Set.Lattice`, including `Disjoint`. We consider various intersections and unions of half infinite intervals. -/ universe u v w variable {ι : Sort u} {α : Type v} {β : Type w} open Set open OrderDual (toDual) namespace Set section Preorder variable [Preorder α] {a b c : α} @[simp] theorem Iic_disjoint_Ioi (h : a ≤ b) : Disjoint (Iic a) (Ioi b) := disjoint_left.mpr fun _ ha hb => (h.trans_lt hb).not_le ha @[simp] theorem Iio_disjoint_Ici (h : a ≤ b) : Disjoint (Iio a) (Ici b) := disjoint_left.mpr fun _ ha hb => (h.trans_lt' ha).not_le hb @[simp] theorem Iic_disjoint_Ioc (h : a ≤ b) : Disjoint (Iic a) (Ioc b c) := (Iic_disjoint_Ioi h).mono le_rfl Ioc_subset_Ioi_self @[simp] theorem Ioc_disjoint_Ioc_of_le {d : α} (h : b ≤ c) : Disjoint (Ioc a b) (Ioc c d) := (Iic_disjoint_Ioc h).mono Ioc_subset_Iic_self le_rfl @[deprecated Ioc_disjoint_Ioc_of_le (since := "2025-03-04")] theorem Ioc_disjoint_Ioc_same : Disjoint (Ioc a b) (Ioc b c) := (Iic_disjoint_Ioc le_rfl).mono Ioc_subset_Iic_self le_rfl @[simp] theorem Ico_disjoint_Ico_same : Disjoint (Ico a b) (Ico b c) := disjoint_left.mpr fun _ hab hbc => hab.2.not_le hbc.1 @[simp] theorem Ici_disjoint_Iic : Disjoint (Ici a) (Iic b) ↔ ¬a ≤ b := by rw [Set.disjoint_iff_inter_eq_empty, Ici_inter_Iic, Icc_eq_empty_iff] @[simp] theorem Iic_disjoint_Ici : Disjoint (Iic a) (Ici b) ↔ ¬b ≤ a := disjoint_comm.trans Ici_disjoint_Iic @[simp] theorem Ioc_disjoint_Ioi (h : b ≤ c) : Disjoint (Ioc a b) (Ioi c) := disjoint_left.mpr (fun _ hx hy ↦ (hx.2.trans h).not_lt hy) theorem Ioc_disjoint_Ioi_same : Disjoint (Ioc a b) (Ioi b) := Ioc_disjoint_Ioi le_rfl @[simp] theorem iUnion_Iic : ⋃ a : α, Iic a = univ := iUnion_eq_univ_iff.2 fun x => ⟨x, right_mem_Iic⟩ @[simp] theorem iUnion_Ici : ⋃ a : α, Ici a = univ := iUnion_eq_univ_iff.2 fun x => ⟨x, left_mem_Ici⟩ @[simp] theorem iUnion_Icc_right (a : α) : ⋃ b, Icc a b = Ici a := by simp only [← Ici_inter_Iic, ← inter_iUnion, iUnion_Iic, inter_univ] @[simp] theorem iUnion_Ioc_right (a : α) : ⋃ b, Ioc a b = Ioi a := by simp only [← Ioi_inter_Iic, ← inter_iUnion, iUnion_Iic, inter_univ] @[simp] theorem iUnion_Icc_left (b : α) : ⋃ a, Icc a b = Iic b := by simp only [← Ici_inter_Iic, ← iUnion_inter, iUnion_Ici, univ_inter] @[simp] theorem iUnion_Ico_left (b : α) : ⋃ a, Ico a b = Iio b := by simp only [← Ici_inter_Iio, ← iUnion_inter, iUnion_Ici, univ_inter] @[simp] theorem iUnion_Iio [NoMaxOrder α] : ⋃ a : α, Iio a = univ := iUnion_eq_univ_iff.2 exists_gt @[simp] theorem iUnion_Ioi [NoMinOrder α] : ⋃ a : α, Ioi a = univ := iUnion_eq_univ_iff.2 exists_lt @[simp] theorem iUnion_Ico_right [NoMaxOrder α] (a : α) : ⋃ b, Ico a b = Ici a := by simp only [← Ici_inter_Iio, ← inter_iUnion, iUnion_Iio, inter_univ] @[simp] theorem iUnion_Ioo_right [NoMaxOrder α] (a : α) : ⋃ b, Ioo a b = Ioi a := by simp only [← Ioi_inter_Iio, ← inter_iUnion, iUnion_Iio, inter_univ] @[simp] theorem iUnion_Ioc_left [NoMinOrder α] (b : α) : ⋃ a, Ioc a b = Iic b := by simp only [← Ioi_inter_Iic, ← iUnion_inter, iUnion_Ioi, univ_inter]
@[simp] theorem iUnion_Ioo_left [NoMinOrder α] (b : α) : ⋃ a, Ioo a b = Iio b := by
Mathlib/Order/Interval/Set/Disjoint.lean
117
118
/- Copyright (c) 2020 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import Mathlib.CategoryTheory.Abelian.Exact import Mathlib.CategoryTheory.Comma.Over.Basic import Mathlib.Algebra.Category.ModuleCat.EpiMono /-! # Pseudoelements in abelian categories A *pseudoelement* of an object `X` in an abelian category `C` is an equivalence class of arrows ending in `X`, where two arrows are considered equivalent if we can find two epimorphisms with a common domain making a commutative square with the two arrows. While the construction shows that pseudoelements are actually subobjects of `X` rather than "elements", it is possible to chase these pseudoelements through commutative diagrams in an abelian category to prove exactness properties. This is done using some "diagram-chasing metatheorems" proved in this file. In many cases, a proof in the category of abelian groups can more or less directly be converted into a proof using pseudoelements. A classic application of pseudoelements are diagram lemmas like the four lemma or the snake lemma. Pseudoelements are in some ways weaker than actual elements in a concrete category. The most important limitation is that there is no extensionality principle: If `f g : X ⟶ Y`, then `∀ x ∈ X, f x = g x` does not necessarily imply that `f = g` (however, if `f = 0` or `g = 0`, it does). A corollary of this is that we can not define arrows in abelian categories by dictating their action on pseudoelements. Thus, a usual style of proofs in abelian categories is this: First, we construct some morphism using universal properties, and then we use diagram chasing of pseudoelements to verify that is has some desirable property such as exactness. It should be noted that the Freyd-Mitchell embedding theorem (see `CategoryTheory.Abelian.FreydMitchell`) gives a vastly stronger notion of pseudoelement (in particular one that gives extensionality) and this file should be updated to go use that instead! ## Main results We define the type of pseudoelements of an object and, in particular, the zero pseudoelement. We prove that every morphism maps the zero pseudoelement to the zero pseudoelement (`apply_zero`) and that a zero morphism maps every pseudoelement to the zero pseudoelement (`zero_apply`). Here are the metatheorems we provide: * A morphism `f` is zero if and only if it is the zero function on pseudoelements. * A morphism `f` is an epimorphism if and only if it is surjective on pseudoelements. * A morphism `f` is a monomorphism if and only if it is injective on pseudoelements if and only if `∀ a, f a = 0 → f = 0`. * A sequence `f, g` of morphisms is exact if and only if `∀ a, g (f a) = 0` and `∀ b, g b = 0 → ∃ a, f a = b`. * If `f` is a morphism and `a, a'` are such that `f a = f a'`, then there is some pseudoelement `a''` such that `f a'' = 0` and for every `g` we have `g a' = 0 → g a = g a''`. We can think of `a''` as `a - a'`, but don't get too carried away by that: pseudoelements of an object do not form an abelian group. ## Notations We introduce coercions from an object of an abelian category to the set of its pseudoelements and from a morphism to the function it induces on pseudoelements. These coercions must be explicitly enabled via local instances: `attribute [local instance] objectToSort homToFun` ## Implementation notes It appears that sometimes the coercion from morphisms to functions does not work, i.e., writing `g a` raises a "function expected" error. This error can be fixed by writing `(g : X ⟶ Y) a`. ## References * [F. Borceux, *Handbook of Categorical Algebra 2*][borceux-vol2] -/ open CategoryTheory open CategoryTheory.Limits open CategoryTheory.Abelian open CategoryTheory.Preadditive universe v u namespace CategoryTheory.Abelian variable {C : Type u} [Category.{v} C] attribute [local instance] Over.coeFromHom /-- This is just composition of morphisms in `C`. Another way to express this would be `(Over.map f).obj a`, but our definition has nicer definitional properties. -/ def app {P Q : C} (f : P ⟶ Q) (a : Over P) : Over Q := a.hom ≫ f @[simp] theorem app_hom {P Q : C} (f : P ⟶ Q) (a : Over P) : (app f a).hom = a.hom ≫ f := rfl /-- Two arrows `f : X ⟶ P` and `g : Y ⟶ P` are called pseudo-equal if there is some object `R` and epimorphisms `p : R ⟶ X` and `q : R ⟶ Y` such that `p ≫ f = q ≫ g`. -/ def PseudoEqual (P : C) (f g : Over P) : Prop := ∃ (R : C) (p : R ⟶ f.1) (q : R ⟶ g.1) (_ : Epi p) (_ : Epi q), p ≫ f.hom = q ≫ g.hom theorem pseudoEqual_refl {P : C} : Reflexive (PseudoEqual P) := fun f => ⟨f.1, 𝟙 f.1, 𝟙 f.1, inferInstance, inferInstance, by simp⟩ theorem pseudoEqual_symm {P : C} : Symmetric (PseudoEqual P) := fun _ _ ⟨R, p, q, ep, Eq, comm⟩ => ⟨R, q, p, Eq, ep, comm.symm⟩ variable [Abelian.{v} C] section /-- Pseudoequality is transitive: Just take the pullback. The pullback morphisms will be epimorphisms since in an abelian category, pullbacks of epimorphisms are epimorphisms. -/ theorem pseudoEqual_trans {P : C} : Transitive (PseudoEqual P) := by intro f g h ⟨R, p, q, ep, Eq, comm⟩ ⟨R', p', q', ep', eq', comm'⟩ refine ⟨pullback q p', pullback.fst _ _ ≫ p, pullback.snd _ _ ≫ q', epi_comp _ _, epi_comp _ _, ?_⟩ rw [Category.assoc, comm, ← Category.assoc, pullback.condition, Category.assoc, comm', Category.assoc] end /-- The arrows with codomain `P` equipped with the equivalence relation of being pseudo-equal. -/ def Pseudoelement.setoid (P : C) : Setoid (Over P) := ⟨_, ⟨pseudoEqual_refl, @pseudoEqual_symm _ _ _, @pseudoEqual_trans _ _ _ _⟩⟩ attribute [local instance] Pseudoelement.setoid /-- A `Pseudoelement` of `P` is just an equivalence class of arrows ending in `P` by being pseudo-equal. -/ def Pseudoelement (P : C) : Type max u v := Quotient (Pseudoelement.setoid P) namespace Pseudoelement /-- A coercion from an object of an abelian category to its pseudoelements. -/ def objectToSort : CoeSort C (Type max u v) := ⟨fun P => Pseudoelement P⟩ attribute [local instance] objectToSort scoped[Pseudoelement] attribute [instance] CategoryTheory.Abelian.Pseudoelement.objectToSort /-- A coercion from an arrow with codomain `P` to its associated pseudoelement. -/ def overToSort {P : C} : Coe (Over P) (Pseudoelement P) := ⟨Quot.mk (PseudoEqual P)⟩ attribute [local instance] overToSort theorem over_coe_def {P Q : C} (a : Q ⟶ P) : (a : Pseudoelement P) = ⟦↑a⟧ := rfl /-- If two elements are pseudo-equal, then their composition with a morphism is, too. -/ theorem pseudoApply_aux {P Q : C} (f : P ⟶ Q) (a b : Over P) : a ≈ b → app f a ≈ app f b := fun ⟨R, p, q, ep, Eq, comm⟩ => ⟨R, p, q, ep, Eq, show p ≫ a.hom ≫ f = q ≫ b.hom ≫ f by rw [reassoc_of% comm]⟩ /-- A morphism `f` induces a function `pseudoApply f` on pseudoelements. -/ def pseudoApply {P Q : C} (f : P ⟶ Q) : P → Q := Quotient.map (fun g : Over P => app f g) (pseudoApply_aux f) /-- A coercion from morphisms to functions on pseudoelements. -/ def homToFun {P Q : C} : CoeFun (P ⟶ Q) fun _ => P → Q := ⟨pseudoApply⟩ attribute [local instance] homToFun scoped[Pseudoelement] attribute [instance] CategoryTheory.Abelian.Pseudoelement.homToFun theorem pseudoApply_mk' {P Q : C} (f : P ⟶ Q) (a : Over P) : f ⟦a⟧ = ⟦↑(a.hom ≫ f)⟧ := rfl /-- Applying a pseudoelement to a composition of morphisms is the same as composing with each morphism. Sadly, this is not a definitional equality, but at least it is true. -/ theorem comp_apply {P Q R : C} (f : P ⟶ Q) (g : Q ⟶ R) (a : P) : (f ≫ g) a = g (f a) := Quotient.inductionOn a fun x => Quotient.sound <| by simp only [app] rw [← Category.assoc, Over.coe_hom] /-- Composition of functions on pseudoelements is composition of morphisms. -/ theorem comp_comp {P Q R : C} (f : P ⟶ Q) (g : Q ⟶ R) : g ∘ f = f ≫ g := funext fun _ => (comp_apply _ _ _).symm section Zero /-! In this section we prove that for every `P` there is an equivalence class that contains precisely all the zero morphisms ending in `P` and use this to define *the* zero pseudoelement. -/ section attribute [local instance] HasBinaryBiproducts.of_hasBinaryProducts /-- The arrows pseudo-equal to a zero morphism are precisely the zero morphisms. -/ theorem pseudoZero_aux {P : C} (Q : C) (f : Over P) : f ≈ (0 : Q ⟶ P) ↔ f.hom = 0 := ⟨fun ⟨R, p, q, _, _, comm⟩ => zero_of_epi_comp p (by simp [comm]), fun hf => ⟨biprod f.1 Q, biprod.fst, biprod.snd, inferInstance, inferInstance, by rw [hf, Over.coe_hom, HasZeroMorphisms.comp_zero, HasZeroMorphisms.comp_zero]⟩⟩ end theorem zero_eq_zero' {P Q R : C} : (⟦((0 : Q ⟶ P) : Over P)⟧ : Pseudoelement P) = ⟦((0 : R ⟶ P) : Over P)⟧ := Quotient.sound <| (pseudoZero_aux R _).2 rfl /-- The zero pseudoelement is the class of a zero morphism. -/ def pseudoZero {P : C} : P := ⟦(0 : P ⟶ P)⟧ -- Porting note: in mathlib3, we couldn't make this an instance -- as it would have fired on `coe_sort`. -- However now that coercions are treated differently, this is a structural instance triggered by -- the appearance of `Pseudoelement`. instance hasZero {P : C} : Zero P := ⟨pseudoZero⟩ instance {P : C} : Inhabited P := ⟨0⟩ theorem pseudoZero_def {P : C} : (0 : Pseudoelement P) = ⟦↑(0 : P ⟶ P)⟧ := rfl @[simp] theorem zero_eq_zero {P Q : C} : ⟦((0 : Q ⟶ P) : Over P)⟧ = (0 : Pseudoelement P) := zero_eq_zero' /-- The pseudoelement induced by an arrow is zero precisely when that arrow is zero. -/ theorem pseudoZero_iff {P : C} (a : Over P) : a = (0 : P) ↔ a.hom = 0 := by rw [← pseudoZero_aux P a] exact Quotient.eq' end Zero open Pseudoelement /-- Morphisms map the zero pseudoelement to the zero pseudoelement. -/ @[simp] theorem apply_zero {P Q : C} (f : P ⟶ Q) : f 0 = 0 := by rw [pseudoZero_def, pseudoApply_mk'] simp /-- The zero morphism maps every pseudoelement to 0. -/ @[simp] theorem zero_apply {P : C} (Q : C) (a : P) : (0 : P ⟶ Q) a = 0 := Quotient.inductionOn a fun a' => by rw [pseudoZero_def, pseudoApply_mk'] simp /-- An extensionality lemma for being the zero arrow. -/ theorem zero_morphism_ext {P Q : C} (f : P ⟶ Q) : (∀ a, f a = 0) → f = 0 := fun h => by rw [← Category.id_comp f] exact (pseudoZero_iff (𝟙 P ≫ f : Over Q)).1 (h (𝟙 P)) theorem zero_morphism_ext' {P Q : C} (f : P ⟶ Q) : (∀ a, f a = 0) → 0 = f := Eq.symm ∘ zero_morphism_ext f theorem eq_zero_iff {P Q : C} (f : P ⟶ Q) : f = 0 ↔ ∀ a, f a = 0 := ⟨fun h a => by simp [h], zero_morphism_ext _⟩ /-- A monomorphism is injective on pseudoelements. -/ theorem pseudo_injective_of_mono {P Q : C} (f : P ⟶ Q) [Mono f] : Function.Injective f := by intro abar abar' refine Quotient.inductionOn₂ abar abar' fun a a' ha => ?_ apply Quotient.sound have : (⟦(a.hom ≫ f : Over Q)⟧ : Quotient (setoid Q)) = ⟦↑(a'.hom ≫ f)⟧ := by convert ha have ⟨R, p, q, ep, Eq, comm⟩ := Quotient.exact this exact ⟨R, p, q, ep, Eq, (cancel_mono f).1 <| by simp only [Category.assoc] exact comm⟩ /-- A morphism that is injective on pseudoelements only maps the zero element to zero. -/ theorem zero_of_map_zero {P Q : C} (f : P ⟶ Q) : Function.Injective f → ∀ a, f a = 0 → a = 0 := fun h a ha => by rw [← apply_zero f] at ha exact h ha /-- A morphism that only maps the zero pseudoelement to zero is a monomorphism. -/ theorem mono_of_zero_of_map_zero {P Q : C} (f : P ⟶ Q) : (∀ a, f a = 0 → a = 0) → Mono f := fun h => (mono_iff_cancel_zero _).2 fun _ g hg => (pseudoZero_iff (g : Over P)).1 <| h _ <| show f g = 0 from (pseudoZero_iff (g ≫ f : Over Q)).2 hg section /-- An epimorphism is surjective on pseudoelements. -/ theorem pseudo_surjective_of_epi {P Q : C} (f : P ⟶ Q) [Epi f] : Function.Surjective f := fun qbar => Quotient.inductionOn qbar fun q => ⟨(pullback.fst f q.hom : Over P), Quotient.sound <| ⟨pullback f q.hom, 𝟙 (pullback f q.hom), pullback.snd _ _, inferInstance, inferInstance, by rw [Category.id_comp, ← pullback.condition, app_hom, Over.coe_hom]⟩⟩ end /-- A morphism that is surjective on pseudoelements is an epimorphism. -/ theorem epi_of_pseudo_surjective {P Q : C} (f : P ⟶ Q) : Function.Surjective f → Epi f := by intro h have ⟨pbar, hpbar⟩ := h (𝟙 Q) have ⟨p, hp⟩ := Quotient.exists_rep pbar have : (⟦(p.hom ≫ f : Over Q)⟧ : Quotient (setoid Q)) = ⟦↑(𝟙 Q)⟧ := by rw [← hp] at hpbar exact hpbar have ⟨R, x, y, _, ey, comm⟩ := Quotient.exact this apply @epi_of_epi_fac _ _ _ _ _ (x ≫ p.hom) f y ey dsimp at comm rw [Category.assoc, comm] apply Category.comp_id section /-- Two morphisms in an exact sequence are exact on pseudoelements. -/ theorem pseudo_exact_of_exact {S : ShortComplex C} (hS : S.Exact) : ∀ b, S.g b = 0 → ∃ a, S.f a = b := fun b' => Quotient.inductionOn b' fun b hb => by have hb' : b.hom ≫ S.g = 0 := (pseudoZero_iff _).1 hb -- By exactness, `b` factors through `im f = ker g` via some `c`. obtain ⟨c, hc⟩ := KernelFork.IsLimit.lift' hS.isLimitImage _ hb' -- We compute the pullback of the map into the image and `c`. -- The pseudoelement induced by the first pullback map will be our preimage. use pullback.fst (Abelian.factorThruImage S.f) c -- It remains to show that the image of this element under `f` is pseudo-equal to `b`. apply Quotient.sound refine ⟨pullback (Abelian.factorThruImage S.f) c, 𝟙 _, pullback.snd _ _, inferInstance, inferInstance, ?_⟩ -- Now we can verify that the diagram commutes. calc 𝟙 (pullback (Abelian.factorThruImage S.f) c) ≫ pullback.fst _ _ ≫ S.f = pullback.fst _ _ ≫ S.f := Category.id_comp _ _ = pullback.fst _ _ ≫ Abelian.factorThruImage S.f ≫ kernel.ι (cokernel.π S.f) := by rw [Abelian.image.fac] _ = (pullback.snd _ _ ≫ c) ≫ kernel.ι (cokernel.π S.f) := by rw [← Category.assoc, pullback.condition] _ = pullback.snd _ _ ≫ b.hom := by rw [Category.assoc] congr end theorem apply_eq_zero_of_comp_eq_zero {P Q R : C} (f : Q ⟶ R) (a : P ⟶ Q) : a ≫ f = 0 → f a = 0 := fun h => by simp [over_coe_def, pseudoApply_mk', Over.coe_hom, h] section /-- If two morphisms are exact on pseudoelements, they are exact. -/ theorem exact_of_pseudo_exact (S : ShortComplex C) (hS : ∀ b, S.g b = 0 → ∃ a, S.f a = b) : S.Exact := (S.exact_iff_kernel_ι_comp_cokernel_π_zero).2 (by -- If we apply `g` to the pseudoelement induced by its kernel, we get 0 (of course!). have : S.g (kernel.ι S.g) = 0 := apply_eq_zero_of_comp_eq_zero _ _ (kernel.condition _) -- By pseudo-exactness, we get a preimage. obtain ⟨a', ha⟩ := hS _ this obtain ⟨a, ha'⟩ := Quotient.exists_rep a' rw [← ha'] at ha obtain ⟨Z, r, q, _, eq, comm⟩ := Quotient.exact ha -- Consider the pullback of `kernel.ι (cokernel.π f)` and `kernel.ι g`. -- The commutative diagram given by the pseudo-equality `f a = b` induces -- a cone over this pullback, so we get a factorization `z`. obtain ⟨z, _, hz₂⟩ := @pullback.lift' _ _ _ _ _ _ (kernel.ι (cokernel.π S.f)) (kernel.ι S.g) _ (r ≫ a.hom ≫ Abelian.factorThruImage S.f) q (by simp only [Category.assoc, Abelian.image.fac] exact comm) -- Let's give a name to the second pullback morphism. let j : pullback (kernel.ι (cokernel.π S.f)) (kernel.ι S.g) ⟶ kernel S.g := pullback.snd _ _ -- Since `q` is an epimorphism, in particular this means that `j` is an epimorphism. haveI pe : Epi j := epi_of_epi_fac hz₂ -- But it is also a monomorphism, because `kernel.ι (cokernel.π f)` is: A kernel is -- always a monomorphism and the pullback of a monomorphism is a monomorphism. -- But mono + epi = iso, so `j` is an isomorphism. haveI : IsIso j := isIso_of_mono_of_epi _ -- But then `kernel.ι g` can be expressed using all of the maps of the pullback square, and we -- are done. rw [(Iso.eq_inv_comp (asIso j)).2 pullback.condition.symm] simp only [Category.assoc, kernel.condition, HasZeroMorphisms.comp_zero]) end /-- If two pseudoelements `x` and `y` have the same image under some morphism `f`, then we can form their "difference" `z`. This pseudoelement has the properties that `f z = 0` and for all morphisms `g`, if `g y = 0` then `g z = g x`. -/ theorem sub_of_eq_image {P Q : C} (f : P ⟶ Q) (x y : P) : f x = f y → ∃ z, f z = 0 ∧ ∀ (R : C) (g : P ⟶ R), (g : P ⟶ R) y = 0 → g z = g x := Quotient.inductionOn₂ x y fun a a' h => match Quotient.exact h with | ⟨R, p, q, ep, _, comm⟩ => let a'' : R ⟶ P := (p ≫ a.hom : R ⟶ P) - (q ≫ a'.hom : R ⟶ P) ⟨a'', ⟨show ⟦(a'' ≫ f : Over Q)⟧ = ⟦↑(0 : Q ⟶ Q)⟧ by dsimp at comm simp [a'', sub_eq_zero.2 comm], fun Z g hh => by obtain ⟨X, p', q', ep', _, comm'⟩ := Quotient.exact hh have : a'.hom ≫ g = 0 := by apply (epi_iff_cancel_zero _).1 ep' _ (a'.hom ≫ g) simpa using comm' apply Quotient.sound -- Can we prevent quotient.sound from giving us this weird `coe_b` thingy? change app g (a'' : Over P) ≈ app g a exact ⟨R, 𝟙 R, p, inferInstance, ep, by simp [a'', sub_eq_add_neg, this]⟩⟩⟩ variable [Limits.HasPullbacks C] /-- If `f : P ⟶ R` and `g : Q ⟶ R` are morphisms and `p : P` and `q : Q` are pseudoelements such that `f p = g q`, then there is some `s : pullback f g` such that `fst s = p` and `snd s = q`. Remark: Borceux claims that `s` is unique, but this is false. See `Counterexamples/Pseudoelement.lean` for details. -/ theorem pseudo_pullback {P Q R : C} {f : P ⟶ R} {g : Q ⟶ R} {p : P} {q : Q} : f p = g q → ∃ s, pullback.fst f g s = p ∧ pullback.snd f g s = q := Quotient.inductionOn₂ p q fun x y h => by obtain ⟨Z, a, b, ea, eb, comm⟩ := Quotient.exact h obtain ⟨l, hl₁, hl₂⟩ := @pullback.lift' _ _ _ _ _ _ f g _ (a ≫ x.hom) (b ≫ y.hom) (by simp only [Category.assoc] exact comm) exact ⟨l, ⟨Quotient.sound ⟨Z, 𝟙 Z, a, inferInstance, ea, by rwa [Category.id_comp]⟩, Quotient.sound ⟨Z, 𝟙 Z, b, inferInstance, eb, by rwa [Category.id_comp]⟩⟩⟩ section Module /-- In the category `Module R`, if `x` and `y` are pseudoequal, then the range of the associated morphisms is the same. -/ theorem ModuleCat.eq_range_of_pseudoequal {R : Type*} [Ring R] {G : ModuleCat R} {x y : Over G} (h : PseudoEqual G x y) : LinearMap.range x.hom.hom = LinearMap.range y.hom.hom := by obtain ⟨P, p, q, hp, hq, H⟩ := h refine Submodule.ext fun a => ⟨fun ha => ?_, fun ha => ?_⟩ · obtain ⟨a', ha'⟩ := ha obtain ⟨a'', ha''⟩ := (ModuleCat.epi_iff_surjective p).1 hp a' refine ⟨q a'', ?_⟩ dsimp at ha' ⊢ rw [← LinearMap.comp_apply, ← ModuleCat.hom_comp, ← H, ModuleCat.hom_comp, LinearMap.comp_apply, ha'', ha'] · obtain ⟨a', ha'⟩ := ha obtain ⟨a'', ha''⟩ := (ModuleCat.epi_iff_surjective q).1 hq a' refine ⟨p a'', ?_⟩ dsimp at ha' ⊢ rw [← LinearMap.comp_apply, ← ModuleCat.hom_comp, H, ModuleCat.hom_comp, LinearMap.comp_apply, ha'', ha'] end Module end Pseudoelement end CategoryTheory.Abelian
Mathlib/CategoryTheory/Abelian/Pseudoelements.lean
480
495
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import Mathlib.Data.Option.Basic import Batteries.Tactic.Congr import Mathlib.Data.Set.Basic import Mathlib.Tactic.Contrapose /-! # Partial Equivalences In this file, we define partial equivalences `PEquiv`, which are a bijection between a subset of `α` and a subset of `β`. Notationally, a `PEquiv` is denoted by "`≃.`" (note that the full stop is part of the notation). The way we store these internally is with two functions `f : α → Option β` and the reverse function `g : β → Option α`, with the condition that if `f a` is `some b`, then `g b` is `some a`. ## Main results - `PEquiv.ofSet`: creates a `PEquiv` from a set `s`, which sends an element to itself if it is in `s`. - `PEquiv.single`: given two elements `a : α` and `b : β`, create a `PEquiv` that sends them to each other, and ignores all other elements. - `PEquiv.injective_of_forall_ne_isSome`/`injective_of_forall_isSome`: If the domain of a `PEquiv` is all of `α` (except possibly one point), its `toFun` is injective. ## Canonical order `PEquiv` is canonically ordered by inclusion; that is, if a function `f` defined on a subset `s` is equal to `g` on that subset, but `g` is also defined on a larger set, then `f ≤ g`. We also have a definition of `⊥`, which is the empty `PEquiv` (sends all to `none`), which in the end gives us a `SemilatticeInf` with an `OrderBot` instance. ## Tags pequiv, partial equivalence -/ assert_not_exists RelIso universe u v w x /-- A `PEquiv` is a partial equivalence, a representation of a bijection between a subset of `α` and a subset of `β`. See also `PartialEquiv` for a version that requires `toFun` and `invFun` to be globally defined functions and has `source` and `target` sets as extra fields. -/ structure PEquiv (α : Type u) (β : Type v) where /-- The underlying partial function of a `PEquiv` -/ toFun : α → Option β /-- The partial inverse of `toFun` -/ invFun : β → Option α /-- `invFun` is the partial inverse of `toFun` -/ inv : ∀ (a : α) (b : β), a ∈ invFun b ↔ b ∈ toFun a /-- A `PEquiv` is a partial equivalence, a representation of a bijection between a subset of `α` and a subset of `β`. See also `PartialEquiv` for a version that requires `toFun` and `invFun` to be globally defined functions and has `source` and `target` sets as extra fields. -/ infixr:25 " ≃. " => PEquiv namespace PEquiv variable {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} open Function Option instance : FunLike (α ≃. β) α (Option β) := { coe := toFun coe_injective' := by rintro ⟨f₁, f₂, hf⟩ ⟨g₁, g₂, hg⟩ (rfl : f₁ = g₁) congr with y x simp only [hf, hg] } @[simp] theorem coe_mk (f₁ : α → Option β) (f₂ h) : (mk f₁ f₂ h : α → Option β) = f₁ := rfl theorem coe_mk_apply (f₁ : α → Option β) (f₂ : β → Option α) (h) (x : α) : (PEquiv.mk f₁ f₂ h : α → Option β) x = f₁ x := rfl @[ext] theorem ext {f g : α ≃. β} (h : ∀ x, f x = g x) : f = g := DFunLike.ext f g h /-- The identity map as a partial equivalence. -/ @[refl] protected def refl (α : Type*) : α ≃. α where toFun := some invFun := some inv _ _ := eq_comm /-- The inverse partial equivalence. -/ @[symm] protected def symm (f : α ≃. β) : β ≃. α where toFun := f.2 invFun := f.1 inv _ _ := (f.inv _ _).symm theorem mem_iff_mem (f : α ≃. β) : ∀ {a : α} {b : β}, a ∈ f.symm b ↔ b ∈ f a := f.3 _ _ theorem eq_some_iff (f : α ≃. β) : ∀ {a : α} {b : β}, f.symm b = some a ↔ f a = some b := f.3 _ _ /-- Composition of partial equivalences `f : α ≃. β` and `g : β ≃. γ`. -/ @[trans] protected def trans (f : α ≃. β) (g : β ≃. γ) : α ≃. γ where toFun a := (f a).bind g invFun a := (g.symm a).bind f.symm inv a b := by simp_all [and_comm, eq_some_iff f, eq_some_iff g, bind_eq_some_iff] @[simp] theorem refl_apply (a : α) : PEquiv.refl α a = some a := rfl @[simp] theorem symm_refl : (PEquiv.refl α).symm = PEquiv.refl α := rfl @[simp] theorem symm_symm (f : α ≃. β) : f.symm.symm = f := rfl theorem symm_bijective : Function.Bijective (PEquiv.symm : (α ≃. β) → β ≃. α) := Function.bijective_iff_has_inverse.mpr ⟨_, symm_symm, symm_symm⟩ theorem symm_injective : Function.Injective (@PEquiv.symm α β) := symm_bijective.injective theorem trans_assoc (f : α ≃. β) (g : β ≃. γ) (h : γ ≃. δ) : (f.trans g).trans h = f.trans (g.trans h) := ext fun _ => Option.bind_assoc _ _ _ theorem mem_trans (f : α ≃. β) (g : β ≃. γ) (a : α) (c : γ) : c ∈ f.trans g a ↔ ∃ b, b ∈ f a ∧ c ∈ g b := Option.bind_eq_some' theorem trans_eq_some (f : α ≃. β) (g : β ≃. γ) (a : α) (c : γ) : f.trans g a = some c ↔ ∃ b, f a = some b ∧ g b = some c := Option.bind_eq_some' theorem trans_eq_none (f : α ≃. β) (g : β ≃. γ) (a : α) : f.trans g a = none ↔ ∀ b c, b ∉ f a ∨ c ∉ g b := by simp only [eq_none_iff_forall_not_mem, mem_trans, imp_iff_not_or.symm] push_neg exact forall_swap @[simp] theorem refl_trans (f : α ≃. β) : (PEquiv.refl α).trans f = f := by ext; dsimp [PEquiv.trans]; rfl @[simp] theorem trans_refl (f : α ≃. β) : f.trans (PEquiv.refl β) = f := by ext; dsimp [PEquiv.trans]; simp protected theorem inj (f : α ≃. β) {a₁ a₂ : α} {b : β} (h₁ : b ∈ f a₁) (h₂ : b ∈ f a₂) : a₁ = a₂ := by rw [← mem_iff_mem] at *; cases h : f.symm b <;> simp_all /-- If the domain of a `PEquiv` is `α` except a point, its forward direction is injective. -/ theorem injective_of_forall_ne_isSome (f : α ≃. β) (a₂ : α) (h : ∀ a₁ : α, a₁ ≠ a₂ → isSome (f a₁)) : Injective f := HasLeftInverse.injective ⟨fun b => Option.recOn b a₂ fun b' => Option.recOn (f.symm b') a₂ id, fun x => by classical cases hfx : f x · have : x = a₂ := not_imp_comm.1 (h x) (hfx.symm ▸ by simp) simp [this] · dsimp only rw [(eq_some_iff f).2 hfx] rfl⟩ /-- If the domain of a `PEquiv` is all of `α`, its forward direction is injective. -/ theorem injective_of_forall_isSome {f : α ≃. β} (h : ∀ a : α, isSome (f a)) : Injective f := (Classical.em (Nonempty α)).elim (fun hn => injective_of_forall_ne_isSome f (Classical.choice hn) fun a _ => h a) fun hn x => (hn ⟨x⟩).elim section OfSet variable (s : Set α) [DecidablePred (· ∈ s)] /-- Creates a `PEquiv` that is the identity on `s`, and `none` outside of it. -/ def ofSet (s : Set α) [DecidablePred (· ∈ s)] : α ≃. α where toFun a := if a ∈ s then some a else none invFun a := if a ∈ s then some a else none inv a b := by split_ifs with hb ha ha · simp [eq_comm] · simp [ne_of_mem_of_not_mem hb ha] · simp [ne_of_mem_of_not_mem ha hb] · simp theorem mem_ofSet_self_iff {s : Set α} [DecidablePred (· ∈ s)] {a : α} : a ∈ ofSet s a ↔ a ∈ s := by dsimp [ofSet]; split_ifs <;> simp [*] theorem mem_ofSet_iff {s : Set α} [DecidablePred (· ∈ s)] {a b : α} : a ∈ ofSet s b ↔ a = b ∧ a ∈ s := by dsimp [ofSet] split_ifs with h · simp only [mem_def, eq_comm, some.injEq, iff_self_and] rintro rfl exact h · simp only [mem_def, false_iff, not_and, reduceCtorEq] rintro rfl exact h @[simp] theorem ofSet_eq_some_iff {s : Set α} {_ : DecidablePred (· ∈ s)} {a b : α} : ofSet s b = some a ↔ a = b ∧ a ∈ s := mem_ofSet_iff theorem ofSet_eq_some_self_iff {s : Set α} {_ : DecidablePred (· ∈ s)} {a : α} : ofSet s a = some a ↔ a ∈ s := mem_ofSet_self_iff @[simp] theorem ofSet_symm : (ofSet s).symm = ofSet s := rfl @[simp] theorem ofSet_univ : ofSet Set.univ = PEquiv.refl α := rfl
@[simp] theorem ofSet_eq_refl {s : Set α} [DecidablePred (· ∈ s)] : ofSet s = PEquiv.refl α ↔ s = Set.univ := ⟨fun h => by rw [Set.eq_univ_iff_forall] intro rw [← mem_ofSet_self_iff, h] exact rfl, fun h => by simp only [← ofSet_univ, h]⟩
Mathlib/Data/PEquiv.lean
225
234
/- Copyright (c) 2022 Yuma Mizuno. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yuma Mizuno, Junyan Xu -/ import Mathlib.CategoryTheory.PathCategory.Basic import Mathlib.CategoryTheory.Functor.FullyFaithful import Mathlib.CategoryTheory.Bicategory.Free import Mathlib.CategoryTheory.Bicategory.LocallyDiscrete /-! # The coherence theorem for bicategories In this file, we prove the coherence theorem for bicategories, stated in the following form: the free bicategory over any quiver is locally thin. The proof is almost the same as the proof of the coherence theorem for monoidal categories that has been previously formalized in mathlib, which is based on the proof described by Ilya Beylin and Peter Dybjer. The idea is to view a path on a quiver as a normal form of a 1-morphism in the free bicategory on the same quiver. A normalization procedure is then described by `normalize : Pseudofunctor (FreeBicategory B) (LocallyDiscrete (Paths B))`, which is a pseudofunctor from the free bicategory to the locally discrete bicategory on the path category. It turns out that this pseudofunctor is locally an equivalence of categories, and the coherence theorem follows immediately from this fact. ## Main statements * `locally_thin` : the free bicategory is locally thin, that is, there is at most one 2-morphism between two fixed 1-morphisms. ## References * [Ilya Beylin and Peter Dybjer, Extracting a proof of coherence for monoidal categories from a proof of normalization for monoids][beylin1996] -/ open Quiver (Path) open Quiver.Path namespace CategoryTheory open Bicategory Category universe v u namespace FreeBicategory variable {B : Type u} [Quiver.{v + 1} B] /-- Auxiliary definition for `inclusionPath`. -/ @[simp] def inclusionPathAux {a : B} : ∀ {b : B}, Path a b → Hom a b | _, nil => Hom.id a | _, cons p f => (inclusionPathAux p).comp (Hom.of f) /- Porting note: Since the following instance was removed when porting `CategoryTheory.Bicategory.Free`, we add it locally here. -/ /-- Category structure on `Hom a b`. In this file, we will use `Hom a b` for `a b : B` (precisely, `FreeBicategory.Hom a b`) instead of the definitionally equal expression `a ⟶ b` for `a b : FreeBicategory B`. The main reason is that we have to annoyingly write `@Quiver.Hom (FreeBicategory B) _ a b` to get the latter expression when given `a b : B`. -/ local instance homCategory' (a b : B) : Category (Hom a b) := homCategory a b /-- The discrete category on the paths includes into the category of 1-morphisms in the free bicategory. -/ def inclusionPath (a b : B) : Discrete (Path.{v + 1} a b) ⥤ Hom a b := Discrete.functor inclusionPathAux /-- The inclusion from the locally discrete bicategory on the path category into the free bicategory as a prelax functor. This will be promoted to a pseudofunctor after proving the coherence theorem. See `inclusion`. -/ def preinclusion (B : Type u) [Quiver.{v + 1} B] : PrelaxFunctor (LocallyDiscrete (Paths B)) (FreeBicategory B) where obj a := a.as map {a b} f := (@inclusionPath B _ a.as b.as).obj f map₂ η := (inclusionPath _ _).map η @[simp] theorem preinclusion_obj (a : B) : (preinclusion B).obj ⟨a⟩ = a := rfl @[simp] theorem preinclusion_map₂ {a b : B} (f g : Discrete (Path.{v + 1} a b)) (η : f ⟶ g) : (preinclusion B).map₂ η = eqToHom (congr_arg _ (Discrete.ext (Discrete.eq_of_hom η))) := by rcases η with ⟨⟨⟩⟩ cases Discrete.ext (by assumption) convert (inclusionPath a b).map_id _
/-- The normalization of the composition of `p : Path a b` and `f : Hom b c`. `p` will eventually be taken to be `nil` and we then get the normalization of `f` alone, but the auxiliary `p` is necessary for Lean to accept the definition of `normalizeIso` and the `whisker_left` case of `normalizeAux_congr` and `normalize_naturality`. -/
Mathlib/CategoryTheory/Bicategory/Coherence.lean
94
98
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import Mathlib.Data.Nat.Totient import Mathlib.Data.ZMod.Aut import Mathlib.Data.ZMod.QuotientGroup import Mathlib.GroupTheory.Exponent import Mathlib.GroupTheory.Subgroup.Simple import Mathlib.Tactic.Group /-! # Cyclic groups A group `G` is called cyclic if there exists an element `g : G` such that every element of `G` is of the form `g ^ n` for some `n : ℕ`. This file only deals with the predicate on a group to be cyclic. For the concrete cyclic group of order `n`, see `Data.ZMod.Basic`. ## Main definitions * `IsCyclic` is a predicate on a group stating that the group is cyclic. ## Main statements * `isCyclic_of_prime_card` proves that a finite group of prime order is cyclic. * `isSimpleGroup_of_prime_card`, `IsSimpleGroup.isCyclic`, and `IsSimpleGroup.prime_card` classify finite simple abelian groups. * `IsCyclic.exponent_eq_card`: For a finite cyclic group `G`, the exponent is equal to the group's cardinality. * `IsCyclic.exponent_eq_zero_of_infinite`: Infinite cyclic groups have exponent zero. * `IsCyclic.iff_exponent_eq_card`: A finite commutative group is cyclic iff its exponent is equal to its cardinality. ## Tags cyclic group -/ assert_not_exists Ideal TwoSidedIdeal variable {α G G' : Type*} {a : α} section Cyclic open Subgroup @[to_additive] theorem IsCyclic.exists_generator [Group α] [IsCyclic α] : ∃ g : α, ∀ x, x ∈ zpowers g := exists_zpow_surjective α @[to_additive] theorem isCyclic_iff_exists_zpowers_eq_top [Group α] : IsCyclic α ↔ ∃ g : α, zpowers g = ⊤ := by simp only [eq_top_iff', mem_zpowers_iff] exact ⟨fun ⟨h⟩ ↦ h, fun h ↦ ⟨h⟩⟩ @[to_additive] protected theorem Subgroup.isCyclic_iff_exists_zpowers_eq_top [Group α] (H : Subgroup α) : IsCyclic H ↔ ∃ g : α, Subgroup.zpowers g = H := by rw [isCyclic_iff_exists_zpowers_eq_top] simp_rw [← (map_injective H.subtype_injective).eq_iff, ← MonoidHom.range_eq_map, H.range_subtype, MonoidHom.map_zpowers, Subtype.exists, coe_subtype, exists_prop] exact exists_congr fun g ↦ and_iff_right_of_imp fun h ↦ h ▸ mem_zpowers g @[to_additive] instance (priority := 100) isCyclic_of_subsingleton [Group α] [Subsingleton α] : IsCyclic α := ⟨⟨1, fun _ => ⟨0, Subsingleton.elim _ _⟩⟩⟩ @[simp] theorem isCyclic_multiplicative_iff [SubNegMonoid α] : IsCyclic (Multiplicative α) ↔ IsAddCyclic α := ⟨fun H ↦ ⟨H.1⟩, fun H ↦ ⟨H.1⟩⟩ instance isCyclic_multiplicative [AddGroup α] [IsAddCyclic α] : IsCyclic (Multiplicative α) := isCyclic_multiplicative_iff.mpr inferInstance @[simp] theorem isAddCyclic_additive_iff [DivInvMonoid α] : IsAddCyclic (Additive α) ↔ IsCyclic α := ⟨fun H ↦ ⟨H.1⟩, fun H ↦ ⟨H.1⟩⟩ instance isAddCyclic_additive [Group α] [IsCyclic α] : IsAddCyclic (Additive α) := isAddCyclic_additive_iff.mpr inferInstance @[to_additive] instance IsCyclic.commutative [Group α] [IsCyclic α] : Std.Commutative (· * · : α → α → α) where comm x y := let ⟨_, hg⟩ := IsCyclic.exists_generator (α := α) let ⟨_, hx⟩ := hg x let ⟨_, hy⟩ := hg y hy ▸ hx ▸ zpow_mul_comm _ _ _ /-- A cyclic group is always commutative. This is not an `instance` because often we have a better proof of `CommGroup`. -/ @[to_additive "A cyclic group is always commutative. This is not an `instance` because often we have a better proof of `AddCommGroup`."] def IsCyclic.commGroup [hg : Group α] [IsCyclic α] : CommGroup α := { hg with mul_comm := commutative.comm } instance [Group G] (H : Subgroup G) [IsCyclic H] : IsMulCommutative H := ⟨IsCyclic.commutative⟩ variable [Group α] [Group G] [Group G'] /-- A non-cyclic multiplicative group is non-trivial. -/ @[to_additive "A non-cyclic additive group is non-trivial."] theorem Nontrivial.of_not_isCyclic (nc : ¬IsCyclic α) : Nontrivial α := by contrapose! nc exact @isCyclic_of_subsingleton _ _ (not_nontrivial_iff_subsingleton.mp nc) @[to_additive] theorem MonoidHom.map_cyclic [h : IsCyclic G] (σ : G →* G) : ∃ m : ℤ, ∀ g : G, σ g = g ^ m := by obtain ⟨h, hG⟩ := IsCyclic.exists_generator (α := G) obtain ⟨m, hm⟩ := hG (σ h) refine ⟨m, fun g => ?_⟩ obtain ⟨n, rfl⟩ := hG g rw [MonoidHom.map_zpow, ← hm, ← zpow_mul, ← zpow_mul'] @[to_additive] lemma isCyclic_iff_exists_orderOf_eq_natCard [Finite α] : IsCyclic α ↔ ∃ g : α, orderOf g = Nat.card α := by simp_rw [isCyclic_iff_exists_zpowers_eq_top, ← card_eq_iff_eq_top, Nat.card_zpowers] @[to_additive] lemma isCyclic_iff_exists_natCard_le_orderOf [Finite α] : IsCyclic α ↔ ∃ g : α, Nat.card α ≤ orderOf g := by rw [isCyclic_iff_exists_orderOf_eq_natCard] apply exists_congr intro g exact ⟨Eq.ge, le_antisymm orderOf_le_card⟩ @[deprecated (since := "2024-12-20")] alias isCyclic_iff_exists_ofOrder_eq_natCard := isCyclic_iff_exists_orderOf_eq_natCard @[deprecated (since := "2024-12-20")] alias isAddCyclic_iff_exists_ofOrder_eq_natCard := isAddCyclic_iff_exists_addOrderOf_eq_natCard @[deprecated (since := "2024-12-20")] alias IsCyclic.iff_exists_ofOrder_eq_natCard_of_Fintype := isCyclic_iff_exists_orderOf_eq_natCard @[deprecated (since := "2024-12-20")] alias IsAddCyclic.iff_exists_ofOrder_eq_natCard_of_Fintype := isAddCyclic_iff_exists_addOrderOf_eq_natCard @[to_additive] theorem isCyclic_of_orderOf_eq_card [Finite α] (x : α) (hx : orderOf x = Nat.card α) : IsCyclic α := isCyclic_iff_exists_orderOf_eq_natCard.mpr ⟨x, hx⟩ @[to_additive] theorem isCyclic_of_card_le_orderOf [Finite α] (x : α) (hx : Nat.card α ≤ orderOf x) : IsCyclic α := isCyclic_iff_exists_natCard_le_orderOf.mpr ⟨x, hx⟩ @[to_additive] theorem Subgroup.eq_bot_or_eq_top_of_prime_card (H : Subgroup G) [hp : Fact (Nat.card G).Prime] : H = ⊥ ∨ H = ⊤ := by have : Finite G := Nat.finite_of_card_ne_zero hp.1.ne_zero have := card_subgroup_dvd_card H rwa [Nat.dvd_prime hp.1, ← eq_bot_iff_card, card_eq_iff_eq_top] at this /-- Any non-identity element of a finite group of prime order generates the group. -/ @[to_additive "Any non-identity element of a finite group of prime order generates the group."] theorem zpowers_eq_top_of_prime_card {p : ℕ} [hp : Fact p.Prime] (h : Nat.card G = p) {g : G} (hg : g ≠ 1) : zpowers g = ⊤ := by subst h have := (zpowers g).eq_bot_or_eq_top_of_prime_card rwa [zpowers_eq_bot, or_iff_right hg] at this @[to_additive] theorem mem_zpowers_of_prime_card {p : ℕ} [hp : Fact p.Prime] (h : Nat.card G = p) {g g' : G} (hg : g ≠ 1) : g' ∈ zpowers g := by simp_rw [zpowers_eq_top_of_prime_card h hg, Subgroup.mem_top] @[to_additive] theorem mem_powers_of_prime_card {p : ℕ} [hp : Fact p.Prime] (h : Nat.card G = p) {g g' : G} (hg : g ≠ 1) : g' ∈ Submonoid.powers g := by have : Finite G := Nat.finite_of_card_ne_zero (h ▸ hp.1.ne_zero) rw [mem_powers_iff_mem_zpowers] exact mem_zpowers_of_prime_card h hg @[to_additive] theorem powers_eq_top_of_prime_card {p : ℕ} [hp : Fact p.Prime] (h : Nat.card G = p) {g : G} (hg : g ≠ 1) : Submonoid.powers g = ⊤ := by ext x simp [mem_powers_of_prime_card h hg] /-- A finite group of prime order is cyclic. -/ @[to_additive "A finite group of prime order is cyclic."] theorem isCyclic_of_prime_card {p : ℕ} [hp : Fact p.Prime] (h : Nat.card α = p) : IsCyclic α := by have : Finite α := Nat.finite_of_card_ne_zero (h ▸ hp.1.ne_zero) have : Nontrivial α := Finite.one_lt_card_iff_nontrivial.mp (h ▸ hp.1.one_lt) obtain ⟨g, hg⟩ : ∃ g : α, g ≠ 1 := exists_ne 1 exact ⟨g, fun g' ↦ mem_zpowers_of_prime_card h hg⟩ /-- A finite group of order dividing a prime is cyclic. -/ @[to_additive "A finite group of order dividing a prime is cyclic."] theorem isCyclic_of_card_dvd_prime {p : ℕ} [hp : Fact p.Prime] (h : Nat.card α ∣ p) : IsCyclic α := by rcases (Nat.dvd_prime hp.out).mp h with h | h · exact @isCyclic_of_subsingleton α _ (Nat.card_eq_one_iff_unique.mp h).1 · exact isCyclic_of_prime_card h @[to_additive] theorem isCyclic_of_surjective {F : Type*} [hH : IsCyclic G'] [FunLike F G' G] [MonoidHomClass F G' G] (f : F) (hf : Function.Surjective f) : IsCyclic G := by obtain ⟨x, hx⟩ := hH refine ⟨f x, fun a ↦ ?_⟩ obtain ⟨a, rfl⟩ := hf a obtain ⟨n, rfl⟩ := hx a exact ⟨n, (map_zpow _ _ _).symm⟩ @[to_additive] theorem orderOf_eq_card_of_forall_mem_zpowers {g : α} (hx : ∀ x, x ∈ zpowers g) : orderOf g = Nat.card α := by rw [← Nat.card_zpowers, (zpowers g).eq_top_iff'.mpr hx, card_top] @[deprecated (since := "2024-11-15")] alias orderOf_generator_eq_natCard := orderOf_eq_card_of_forall_mem_zpowers @[deprecated (since := "2024-11-15")] alias addOrderOf_generator_eq_natCard := addOrderOf_eq_card_of_forall_mem_zmultiples @[to_additive] theorem exists_pow_ne_one_of_isCyclic [G_cyclic : IsCyclic G] {k : ℕ} (k_pos : k ≠ 0) (k_lt_card_G : k < Nat.card G) : ∃ a : G, a ^ k ≠ 1 := by have : Finite G := Nat.finite_of_card_ne_zero (Nat.ne_zero_of_lt k_lt_card_G) rcases G_cyclic with ⟨a, ha⟩ use a contrapose! k_lt_card_G convert orderOf_le_of_pow_eq_one k_pos.bot_lt k_lt_card_G rw [← Nat.card_zpowers, eq_comm, card_eq_iff_eq_top, eq_top_iff] exact fun x _ ↦ ha x @[to_additive] theorem Infinite.orderOf_eq_zero_of_forall_mem_zpowers [Infinite α] {g : α} (h : ∀ x, x ∈ zpowers g) : orderOf g = 0 := by rw [orderOf_eq_card_of_forall_mem_zpowers h, Nat.card_eq_zero_of_infinite] @[to_additive] instance Bot.isCyclic : IsCyclic (⊥ : Subgroup α) := ⟨⟨1, fun x => ⟨0, Subtype.eq <| (zpow_zero (1 : α)).trans <| Eq.symm (Subgroup.mem_bot.1 x.2)⟩⟩⟩ @[to_additive] instance Subgroup.isCyclic [IsCyclic α] (H : Subgroup α) : IsCyclic H := haveI := Classical.propDecidable let ⟨g, hg⟩ := IsCyclic.exists_generator (α := α) if hx : ∃ x : α, x ∈ H ∧ x ≠ (1 : α) then let ⟨x, hx₁, hx₂⟩ := hx let ⟨k, hk⟩ := hg x have hk : g ^ k = x := hk have hex : ∃ n : ℕ, 0 < n ∧ g ^ n ∈ H := ⟨k.natAbs, Nat.pos_of_ne_zero fun h => hx₂ <| by rw [← hk, Int.natAbs_eq_zero.mp h, zpow_zero], by rcases k with k | k · rw [Int.ofNat_eq_coe, Int.natAbs_cast k, ← zpow_natCast, ← Int.ofNat_eq_coe, hk] exact hx₁ · rw [Int.natAbs_negSucc, ← Subgroup.inv_mem_iff H]; simp_all⟩ ⟨⟨⟨g ^ Nat.find hex, (Nat.find_spec hex).2⟩, fun ⟨x, hx⟩ => let ⟨k, hk⟩ := hg x have hk : g ^ k = x := hk have hk₂ : g ^ ((Nat.find hex : ℤ) * (k / Nat.find hex : ℤ)) ∈ H := by rw [zpow_mul] apply H.zpow_mem exact mod_cast (Nat.find_spec hex).2 have hk₃ : g ^ (k % Nat.find hex : ℤ) ∈ H := (Subgroup.mul_mem_cancel_right H hk₂).1 <| by rw [← zpow_add, Int.emod_add_ediv, hk]; exact hx have hk₄ : k % Nat.find hex = (k % Nat.find hex).natAbs := by rw [Int.natAbs_of_nonneg (Int.emod_nonneg _ (Int.natCast_ne_zero_iff_pos.2 (Nat.find_spec hex).1))] have hk₅ : g ^ (k % Nat.find hex).natAbs ∈ H := by rwa [← zpow_natCast, ← hk₄] have hk₆ : (k % (Nat.find hex : ℤ)).natAbs = 0 := by_contradiction fun h => Nat.find_min hex (Int.ofNat_lt.1 <| by rw [← hk₄]; exact Int.emod_lt_of_pos _ (Int.natCast_pos.2 (Nat.find_spec hex).1)) ⟨Nat.pos_of_ne_zero h, hk₅⟩ ⟨k / (Nat.find hex : ℤ), Subtype.ext_iff_val.2 (by suffices g ^ ((Nat.find hex : ℤ) * (k / Nat.find hex : ℤ)) = x by simpa [zpow_mul] rw [Int.mul_ediv_cancel' (Int.dvd_of_emod_eq_zero (Int.natAbs_eq_zero.mp hk₆)), hk])⟩⟩⟩ else by have : H = (⊥ : Subgroup α) := Subgroup.ext fun x => ⟨fun h => by simp at *; tauto, fun h => by rw [Subgroup.mem_bot.1 h]; exact H.one_mem⟩ subst this; infer_instance @[to_additive] theorem isCyclic_of_injective [IsCyclic G'] (f : G →* G') (hf : Function.Injective f) : IsCyclic G := isCyclic_of_surjective (MonoidHom.ofInjective hf).symm (MonoidHom.ofInjective hf).symm.surjective @[to_additive] lemma Subgroup.isCyclic_of_le {H H' : Subgroup G} (h : H ≤ H') [IsCyclic H'] : IsCyclic H := isCyclic_of_injective (Subgroup.inclusion h) (Subgroup.inclusion_injective h) open Finset Nat section Classical open scoped Classical in @[to_additive IsAddCyclic.card_nsmul_eq_zero_le] theorem IsCyclic.card_pow_eq_one_le [DecidableEq α] [Fintype α] [IsCyclic α] {n : ℕ} (hn0 : 0 < n) : #{a : α | a ^ n = 1} ≤ n := let ⟨g, hg⟩ := IsCyclic.exists_generator (α := α) calc #{a : α | a ^ n = 1} ≤ #(zpowers (g ^ (Fintype.card α / Nat.gcd n (Fintype.card α))) : Set α).toFinset := card_le_card fun x hx => let ⟨m, hm⟩ := show x ∈ Submonoid.powers g from mem_powers_iff_mem_zpowers.2 <| hg x Set.mem_toFinset.2 ⟨(m / (Fintype.card α / Nat.gcd n (Fintype.card α)) : ℕ), by dsimp at hm have hgmn : g ^ (m * Nat.gcd n (Fintype.card α)) = 1 := by rw [pow_mul, hm, ← pow_gcd_card_eq_one_iff]; exact (mem_filter.1 hx).2 dsimp only rw [zpow_natCast, ← pow_mul, Nat.mul_div_cancel_left', hm] refine Nat.dvd_of_mul_dvd_mul_right (gcd_pos_of_pos_left (Fintype.card α) hn0) ?_ conv_lhs => rw [Nat.div_mul_cancel (Nat.gcd_dvd_right _ _), ← Nat.card_eq_fintype_card, ← orderOf_eq_card_of_forall_mem_zpowers hg] exact orderOf_dvd_of_pow_eq_one hgmn⟩ _ ≤ n := by let ⟨m, hm⟩ := Nat.gcd_dvd_right n (Fintype.card α) have hm0 : 0 < m := Nat.pos_of_ne_zero fun hm0 => by rw [hm0, mul_zero, Fintype.card_eq_zero_iff] at hm exact hm.elim' 1 simp only [Set.toFinset_card, SetLike.coe_sort_coe] rw [Fintype.card_zpowers, orderOf_pow g, orderOf_eq_card_of_forall_mem_zpowers hg, Nat.card_eq_fintype_card] nth_rw 2 [hm]; nth_rw 3 [hm] rw [Nat.mul_div_cancel_left _ (gcd_pos_of_pos_left _ hn0), gcd_mul_left_left, hm, Nat.mul_div_cancel _ hm0] exact le_of_dvd hn0 (Nat.gcd_dvd_left _ _) end Classical @[to_additive] theorem IsCyclic.exists_monoid_generator [Finite α] [IsCyclic α] : ∃ x : α, ∀ y : α, y ∈ Submonoid.powers x := by simp_rw [mem_powers_iff_mem_zpowers] exact IsCyclic.exists_generator @[to_additive] lemma IsCyclic.exists_ofOrder_eq_natCard [h : IsCyclic α] : ∃ g : α, orderOf g = Nat.card α := by obtain ⟨g, hg⟩ := h.exists_generator use g rw [← card_zpowers g, (eq_top_iff' (zpowers g)).mpr hg] exact Nat.card_congr (Equiv.Set.univ α) variable (G) in /-- A distributive action of a monoid on a finite cyclic group of order `n` factors through an action on `ZMod n`. -/ noncomputable def MulDistribMulAction.toMonoidHomZModOfIsCyclic (M : Type*) [Monoid M] [IsCyclic G] [MulDistribMulAction M G] {n : ℕ} (hn : Nat.card G = n) : M →* ZMod n where toFun m := (MulDistribMulAction.toMonoidHom G m).map_cyclic.choose map_one' := by obtain ⟨g, hg⟩ := IsCyclic.exists_ofOrder_eq_natCard (α := G) rw [← Int.cast_one, ZMod.intCast_eq_intCast_iff, ← hn, ← hg, ← zpow_eq_zpow_iff_modEq, zpow_one, ← (MulDistribMulAction.toMonoidHom G 1).map_cyclic.choose_spec, MulDistribMulAction.toMonoidHom_apply, one_smul] map_mul' m n := by obtain ⟨g, hg⟩ := IsCyclic.exists_ofOrder_eq_natCard (α := G) rw [← Int.cast_mul, ZMod.intCast_eq_intCast_iff, ← hn, ← hg, ← zpow_eq_zpow_iff_modEq, zpow_mul', ← (MulDistribMulAction.toMonoidHom G m).map_cyclic.choose_spec, ← (MulDistribMulAction.toMonoidHom G n).map_cyclic.choose_spec, ← (MulDistribMulAction.toMonoidHom G (m * n)).map_cyclic.choose_spec, MulDistribMulAction.toMonoidHom_apply, MulDistribMulAction.toMonoidHom_apply, MulDistribMulAction.toMonoidHom_apply, mul_smul] theorem MulDistribMulAction.toMonoidHomZModOfIsCyclic_apply {M : Type*} [Monoid M] [IsCyclic G] [MulDistribMulAction M G] {n : ℕ} (hn : Nat.card G = n) (m : M) (g : G) (k : ℤ) (h : toMonoidHomZModOfIsCyclic G M hn m = k) : m • g = g ^ k := by rw [← MulDistribMulAction.toMonoidHom_apply,
(MulDistribMulAction.toMonoidHom G m).map_cyclic.choose_spec g, zpow_eq_zpow_iff_modEq] apply Int.ModEq.of_dvd (Int.natCast_dvd_natCast.mpr (orderOf_dvd_natCard g)) rwa [hn, ← ZMod.intCast_eq_intCast_iff] section variable [Fintype α] @[to_additive]
Mathlib/GroupTheory/SpecificGroups/Cyclic.lean
386
394
/- Copyright (c) 2020 Kyle Miller. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kyle Miller -/ import Mathlib.Algebra.Group.Action.Pi import Mathlib.Data.Finset.Prod import Mathlib.Data.SetLike.Basic import Mathlib.Data.Sym.Basic import Mathlib.Data.Sym.Sym2.Init /-! # The symmetric square This file defines the symmetric square, which is `α × α` modulo swapping. This is also known as the type of unordered pairs. More generally, the symmetric square is the second symmetric power (see `Data.Sym.Basic`). The equivalence is `Sym2.equivSym`. From the point of view that an unordered pair is equivalent to a multiset of cardinality two (see `Sym2.equivMultiset`), there is a `Mem` instance `Sym2.Mem`, which is a `Prop`-valued membership test. Given `h : a ∈ z` for `z : Sym2 α`, then `Mem.other h` is the other element of the pair, defined using `Classical.choice`. If `α` has decidable equality, then `h.other'` computably gives the other element. The universal property of `Sym2` is provided as `Sym2.lift`, which states that functions from `Sym2 α` are equivalent to symmetric two-argument functions from `α`. Recall that an undirected graph (allowing self loops, but no multiple edges) is equivalent to a symmetric relation on the vertex type `α`. Given a symmetric relation on `α`, the corresponding edge set is constructed by `Sym2.fromRel` which is a special case of `Sym2.lift`. ## Notation The element `Sym2.mk (a, b)` can be written as `s(a, b)` for short. ## Tags symmetric square, unordered pairs, symmetric powers -/ assert_not_exists MonoidWithZero open List (Vector) open Finset Function Sym universe u variable {α β γ : Type*} namespace Sym2 /-- This is the relation capturing the notion of pairs equivalent up to permutations. -/ @[aesop (rule_sets := [Sym2]) [safe [constructors, cases], norm]] inductive Rel (α : Type u) : α × α → α × α → Prop | refl (x y : α) : Rel _ (x, y) (x, y) | swap (x y : α) : Rel _ (x, y) (y, x) attribute [refl] Rel.refl @[symm] theorem Rel.symm {x y : α × α} : Rel α x y → Rel α y x := by aesop (rule_sets := [Sym2]) @[trans] theorem Rel.trans {x y z : α × α} (a : Rel α x y) (b : Rel α y z) : Rel α x z := by aesop (rule_sets := [Sym2]) theorem Rel.is_equivalence : Equivalence (Rel α) := { refl := fun (x, y) ↦ Rel.refl x y, symm := Rel.symm, trans := Rel.trans } /-- One can use `attribute [local instance] Sym2.Rel.setoid` to temporarily make `Quotient` functionality work for `α × α`. -/ def Rel.setoid (α : Type u) : Setoid (α × α) := ⟨Rel α, Rel.is_equivalence⟩ @[simp] theorem rel_iff' {p q : α × α} : Rel α p q ↔ p = q ∨ p = q.swap := by aesop (rule_sets := [Sym2]) theorem rel_iff {x y z w : α} : Rel α (x, y) (z, w) ↔ x = z ∧ y = w ∨ x = w ∧ y = z := by simp end Sym2 /-- `Sym2 α` is the symmetric square of `α`, which, in other words, is the type of unordered pairs. It is equivalent in a natural way to multisets of cardinality 2 (see `Sym2.equivMultiset`). -/ abbrev Sym2 (α : Type u) := Quot (Sym2.Rel α) /-- Constructor for `Sym2`. This is the quotient map `α × α → Sym2 α`. -/ protected abbrev Sym2.mk {α : Type*} (p : α × α) : Sym2 α := Quot.mk (Sym2.Rel α) p /-- `s(x, y)` is an unordered pair, which is to say a pair modulo the action of the symmetric group. It is equal to `Sym2.mk (x, y)`. -/ notation3 "s(" x ", " y ")" => Sym2.mk (x, y) namespace Sym2 protected theorem sound {p p' : α × α} (h : Sym2.Rel α p p') : Sym2.mk p = Sym2.mk p' := Quot.sound h protected theorem exact {p p' : α × α} (h : Sym2.mk p = Sym2.mk p') : Sym2.Rel α p p' := Quotient.exact (s := Sym2.Rel.setoid α) h @[simp] protected theorem eq {p p' : α × α} : Sym2.mk p = Sym2.mk p' ↔ Sym2.Rel α p p' := Quotient.eq' (s₁ := Sym2.Rel.setoid α) @[elab_as_elim, cases_eliminator, induction_eliminator] protected theorem ind {f : Sym2 α → Prop} (h : ∀ x y, f s(x, y)) : ∀ i, f i := Quot.ind <| Prod.rec <| h @[elab_as_elim] protected theorem inductionOn {f : Sym2 α → Prop} (i : Sym2 α) (hf : ∀ x y, f s(x, y)) : f i := i.ind hf @[elab_as_elim] protected theorem inductionOn₂ {f : Sym2 α → Sym2 β → Prop} (i : Sym2 α) (j : Sym2 β) (hf : ∀ a₁ a₂ b₁ b₂, f s(a₁, a₂) s(b₁, b₂)) : f i j := Quot.induction_on₂ i j <| by intro ⟨a₁, a₂⟩ ⟨b₁, b₂⟩ exact hf _ _ _ _ /-- Dependent recursion principal for `Sym2`. See `Quot.rec`. -/ @[elab_as_elim] protected def rec {motive : Sym2 α → Sort*} (f : (p : α × α) → motive (Sym2.mk p)) (h : (p q : α × α) → (h : Sym2.Rel α p q) → Eq.ndrec (f p) (Sym2.sound h) = f q) (z : Sym2 α) : motive z := Quot.rec f h z /-- Dependent recursion principal for `Sym2` when the target is a `Subsingleton` type. See `Quot.recOnSubsingleton`. -/ @[elab_as_elim] protected abbrev recOnSubsingleton {motive : Sym2 α → Sort*} [(p : α × α) → Subsingleton (motive (Sym2.mk p))] (z : Sym2 α) (f : (p : α × α) → motive (Sym2.mk p)) : motive z := Quot.recOnSubsingleton z f protected theorem «exists» {α : Sort _} {f : Sym2 α → Prop} : (∃ x : Sym2 α, f x) ↔ ∃ x y, f s(x, y) := Quot.mk_surjective.exists.trans Prod.exists protected theorem «forall» {α : Sort _} {f : Sym2 α → Prop} : (∀ x : Sym2 α, f x) ↔ ∀ x y, f s(x, y) := Quot.mk_surjective.forall.trans Prod.forall theorem eq_swap {a b : α} : s(a, b) = s(b, a) := Quot.sound (Rel.swap _ _) @[simp] theorem mk_prod_swap_eq {p : α × α} : Sym2.mk p.swap = Sym2.mk p := by cases p exact eq_swap theorem congr_right {a b c : α} : s(a, b) = s(a, c) ↔ b = c := by simp +contextual theorem congr_left {a b c : α} : s(b, a) = s(c, a) ↔ b = c := by simp +contextual theorem eq_iff {x y z w : α} : s(x, y) = s(z, w) ↔ x = z ∧ y = w ∨ x = w ∧ y = z := by simp theorem mk_eq_mk_iff {p q : α × α} : Sym2.mk p = Sym2.mk q ↔ p = q ∨ p = q.swap := by cases p cases q simp only [eq_iff, Prod.mk_inj, Prod.swap_prod_mk] /-- The universal property of `Sym2`; symmetric functions of two arguments are equivalent to functions from `Sym2`. Note that when `β` is `Prop`, it can sometimes be more convenient to use `Sym2.fromRel` instead. -/ def lift : { f : α → α → β // ∀ a₁ a₂, f a₁ a₂ = f a₂ a₁ } ≃ (Sym2 α → β) where toFun f := Quot.lift (uncurry ↑f) <| by rintro _ _ ⟨⟩ exacts [rfl, f.prop _ _] invFun F := ⟨curry (F ∘ Sym2.mk), fun _ _ => congr_arg F eq_swap⟩ left_inv _ := Subtype.ext rfl right_inv _ := funext <| Sym2.ind fun _ _ => rfl @[simp] theorem lift_mk (f : { f : α → α → β // ∀ a₁ a₂, f a₁ a₂ = f a₂ a₁ }) (a₁ a₂ : α) : lift f s(a₁, a₂) = (f : α → α → β) a₁ a₂ := rfl @[simp] theorem coe_lift_symm_apply (F : Sym2 α → β) (a₁ a₂ : α) : (lift.symm F : α → α → β) a₁ a₂ = F s(a₁, a₂) := rfl /-- A two-argument version of `Sym2.lift`. -/ def lift₂ : { f : α → α → β → β → γ // ∀ a₁ a₂ b₁ b₂, f a₁ a₂ b₁ b₂ = f a₂ a₁ b₁ b₂ ∧ f a₁ a₂ b₁ b₂ = f a₁ a₂ b₂ b₁ } ≃ (Sym2 α → Sym2 β → γ) where toFun f := Quotient.lift₂ (s₁ := Sym2.Rel.setoid α) (s₂ := Sym2.Rel.setoid β) (fun (a : α × α) (b : β × β) => f.1 a.1 a.2 b.1 b.2) (by rintro _ _ _ _ ⟨⟩ ⟨⟩ exacts [rfl, (f.2 _ _ _ _).2, (f.2 _ _ _ _).1, (f.2 _ _ _ _).1.trans (f.2 _ _ _ _).2]) invFun F := ⟨fun a₁ a₂ b₁ b₂ => F s(a₁, a₂) s(b₁, b₂), fun a₁ a₂ b₁ b₂ => by constructor exacts [congr_arg₂ F eq_swap rfl, congr_arg₂ F rfl eq_swap]⟩ left_inv _ := Subtype.ext rfl right_inv _ := funext₂ fun a b => Sym2.inductionOn₂ a b fun _ _ _ _ => rfl @[simp] theorem lift₂_mk (f : { f : α → α → β → β → γ // ∀ a₁ a₂ b₁ b₂, f a₁ a₂ b₁ b₂ = f a₂ a₁ b₁ b₂ ∧ f a₁ a₂ b₁ b₂ = f a₁ a₂ b₂ b₁ }) (a₁ a₂ : α) (b₁ b₂ : β) : lift₂ f s(a₁, a₂) s(b₁, b₂) = (f : α → α → β → β → γ) a₁ a₂ b₁ b₂ := rfl @[simp] theorem coe_lift₂_symm_apply (F : Sym2 α → Sym2 β → γ) (a₁ a₂ : α) (b₁ b₂ : β) : (lift₂.symm F : α → α → β → β → γ) a₁ a₂ b₁ b₂ = F s(a₁, a₂) s(b₁, b₂) := rfl /-- The functor `Sym2` is functorial, and this function constructs the induced maps. -/ def map (f : α → β) : Sym2 α → Sym2 β := Quot.map (Prod.map f f) (by intro _ _ h; cases h <;> constructor) @[simp] theorem map_id : map (@id α) = id := by ext ⟨⟨x, y⟩⟩ rfl theorem map_comp {g : β → γ} {f : α → β} : Sym2.map (g ∘ f) = Sym2.map g ∘ Sym2.map f := by ext ⟨⟨x, y⟩⟩ rfl theorem map_map {g : β → γ} {f : α → β} (x : Sym2 α) : map g (map f x) = map (g ∘ f) x := by induction x; aesop @[simp] theorem map_pair_eq (f : α → β) (x y : α) : map f s(x, y) = s(f x, f y) := rfl theorem map.injective {f : α → β} (hinj : Injective f) : Injective (map f) := by intro z z' refine Sym2.inductionOn₂ z z' (fun x y x' y' => ?_) simp [hinj.eq_iff] /-- `mk a` as an embedding. This is the symmetric version of `Function.Embedding.sectL`. -/ @[simps] def mkEmbedding (a : α) : α ↪ Sym2 α where toFun b := s(a, b) inj' b₁ b₁ h := by simp only [Sym2.eq, Sym2.rel_iff', Prod.mk.injEq, true_and, Prod.swap_prod_mk] at h obtain rfl | ⟨rfl, rfl⟩ := h <;> rfl /-- `Sym2.map` as an embedding. -/ @[simps] def _root_.Function.Embedding.sym2Map (f : α ↪ β) : Sym2 α ↪ Sym2 β where toFun := map f inj' := map.injective f.injective lemma lift_comp_map {g : γ → α} (f : {f : α → α → β // ∀ a₁ a₂, f a₁ a₂ = f a₂ a₁}) : lift f ∘ map g = lift ⟨fun (c₁ c₂ : γ) => f.val (g c₁) (g c₂), fun _ _ => f.prop _ _⟩ := lift.symm_apply_eq.mp rfl lemma lift_map_apply {g : γ → α} (f : {f : α → α → β // ∀ a₁ a₂, f a₁ a₂ = f a₂ a₁}) (p : Sym2 γ) : lift f (map g p) = lift ⟨fun (c₁ c₂ : γ) => f.val (g c₁) (g c₂), fun _ _ => f.prop _ _⟩ p := by conv_rhs => rw [← lift_comp_map, comp_apply] section Membership /-! ### Membership and set coercion -/ /-- This is a predicate that determines whether a given term is a member of a term of the symmetric square. From this point of view, the symmetric square is the subtype of cardinality-two multisets on `α`. -/ protected def Mem (x : α) (z : Sym2 α) : Prop := ∃ y : α, z = s(x, y) @[aesop norm (rule_sets := [Sym2])] theorem mem_iff' {a b c : α} : Sym2.Mem a s(b, c) ↔ a = b ∨ a = c := { mp := by rintro ⟨_, h⟩ rw [eq_iff] at h aesop mpr := by rintro (rfl | rfl) · exact ⟨_, rfl⟩ rw [eq_swap] exact ⟨_, rfl⟩ } instance : SetLike (Sym2 α) α where coe z := { x | z.Mem x } coe_injective' z z' h := by simp only [Set.ext_iff, Set.mem_setOf_eq] at h obtain ⟨x, y⟩ := z obtain ⟨x', y'⟩ := z' have hx := h x; have hy := h y; have hx' := h x'; have hy' := h y' simp only [mem_iff', eq_self_iff_true] at hx hy hx' hy' aesop @[simp] theorem mem_iff_mem {x : α} {z : Sym2 α} : Sym2.Mem x z ↔ x ∈ z := Iff.rfl theorem mem_iff_exists {x : α} {z : Sym2 α} : x ∈ z ↔ ∃ y : α, z = s(x, y) := Iff.rfl @[ext] theorem ext {p q : Sym2 α} (h : ∀ x, x ∈ p ↔ x ∈ q) : p = q := SetLike.ext h theorem mem_mk_left (x y : α) : x ∈ s(x, y) := ⟨y, rfl⟩ theorem mem_mk_right (x y : α) : y ∈ s(x, y) := eq_swap ▸ mem_mk_left y x @[simp, aesop norm (rule_sets := [Sym2])] theorem mem_iff {a b c : α} : a ∈ s(b, c) ↔ a = b ∨ a = c := mem_iff' theorem out_fst_mem (e : Sym2 α) : e.out.1 ∈ e := ⟨e.out.2, by rw [Sym2.mk, e.out_eq]⟩ theorem out_snd_mem (e : Sym2 α) : e.out.2 ∈ e := ⟨e.out.1, by rw [eq_swap, Sym2.mk, e.out_eq]⟩ theorem ball {p : α → Prop} {a b : α} : (∀ c ∈ s(a, b), p c) ↔ p a ∧ p b := by refine ⟨fun h => ⟨h _ <| mem_mk_left _ _, h _ <| mem_mk_right _ _⟩, fun h c hc => ?_⟩ obtain rfl | rfl := Sym2.mem_iff.1 hc · exact h.1 · exact h.2 /-- Given an element of the unordered pair, give the other element using `Classical.choose`. See also `Mem.other'` for the computable version. -/ noncomputable def Mem.other {a : α} {z : Sym2 α} (h : a ∈ z) : α := Classical.choose h @[simp]
theorem other_spec {a : α} {z : Sym2 α} (h : a ∈ z) : s(a, Mem.other h) = z := by erw [← Classical.choose_spec h]
Mathlib/Data/Sym/Sym2.lean
354
355
/- Copyright (c) 2015, 2017 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Robert Y. Lewis, Johannes Hölzl, Mario Carneiro, Sébastien Gouëzel -/ import Mathlib.Data.ENNReal.Real import Mathlib.Tactic.Bound.Attribute import Mathlib.Topology.Bornology.Basic import Mathlib.Topology.EMetricSpace.Defs import Mathlib.Topology.UniformSpace.Basic /-! ## Pseudo-metric spaces This file defines pseudo-metric spaces: these differ from metric spaces by not imposing the condition `dist x y = 0 → x = y`. Many definitions and theorems expected on (pseudo-)metric spaces are already introduced on uniform spaces and topological spaces. For example: open and closed sets, compactness, completeness, continuity and uniform continuity. ## Main definitions * `Dist α`: Endows a space `α` with a function `dist a b`. * `PseudoMetricSpace α`: A space endowed with a distance function, which can be zero even if the two elements are non-equal. * `Metric.ball x ε`: The set of all points `y` with `dist y x < ε`. * `Metric.Bounded s`: Whether a subset of a `PseudoMetricSpace` is bounded. * `MetricSpace α`: A `PseudoMetricSpace` with the guarantee `dist x y = 0 → x = y`. Additional useful definitions: * `nndist a b`: `dist` as a function to the non-negative reals. * `Metric.closedBall x ε`: The set of all points `y` with `dist y x ≤ ε`. * `Metric.sphere x ε`: The set of all points `y` with `dist y x = ε`. TODO (anyone): Add "Main results" section. ## Tags pseudo_metric, dist -/ assert_not_exists compactSpace_uniformity open Set Filter TopologicalSpace Bornology open scoped ENNReal NNReal Uniformity Topology universe u v w variable {α : Type u} {β : Type v} {X ι : Type*} theorem UniformSpace.ofDist_aux (ε : ℝ) (hε : 0 < ε) : ∃ δ > (0 : ℝ), ∀ x < δ, ∀ y < δ, x + y < ε := ⟨ε / 2, half_pos hε, fun _x hx _y hy => add_halves ε ▸ add_lt_add hx hy⟩ /-- Construct a uniform structure from a distance function and metric space axioms -/ def UniformSpace.ofDist (dist : α → α → ℝ) (dist_self : ∀ x : α, dist x x = 0) (dist_comm : ∀ x y : α, dist x y = dist y x) (dist_triangle : ∀ x y z : α, dist x z ≤ dist x y + dist y z) : UniformSpace α := .ofFun dist dist_self dist_comm dist_triangle ofDist_aux /-- Construct a bornology from a distance function and metric space axioms. -/ abbrev Bornology.ofDist {α : Type*} (dist : α → α → ℝ) (dist_comm : ∀ x y, dist x y = dist y x) (dist_triangle : ∀ x y z, dist x z ≤ dist x y + dist y z) : Bornology α := Bornology.ofBounded { s : Set α | ∃ C, ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → dist x y ≤ C } ⟨0, fun _ hx _ => hx.elim⟩ (fun _ ⟨c, hc⟩ _ h => ⟨c, fun _ hx _ hy => hc (h hx) (h hy)⟩) (fun s hs t ht => by rcases s.eq_empty_or_nonempty with rfl | ⟨x, hx⟩ · rwa [empty_union] rcases t.eq_empty_or_nonempty with rfl | ⟨y, hy⟩ · rwa [union_empty] rsuffices ⟨C, hC⟩ : ∃ C, ∀ z ∈ s ∪ t, dist x z ≤ C · refine ⟨C + C, fun a ha b hb => (dist_triangle a x b).trans ?_⟩ simpa only [dist_comm] using add_le_add (hC _ ha) (hC _ hb) rcases hs with ⟨Cs, hs⟩; rcases ht with ⟨Ct, ht⟩ refine ⟨max Cs (dist x y + Ct), fun z hz => hz.elim (fun hz => (hs hx hz).trans (le_max_left _ _)) (fun hz => (dist_triangle x y z).trans <| (add_le_add le_rfl (ht hy hz)).trans (le_max_right _ _))⟩) fun z => ⟨dist z z, forall_eq.2 <| forall_eq.2 le_rfl⟩ /-- The distance function (given an ambient metric space on `α`), which returns a nonnegative real number `dist x y` given `x y : α`. -/ @[ext] class Dist (α : Type*) where /-- Distance between two points -/ dist : α → α → ℝ export Dist (dist) -- the uniform structure and the emetric space structure are embedded in the metric space structure -- to avoid instance diamond issues. See Note [forgetful inheritance]. /-- This is an internal lemma used inside the default of `PseudoMetricSpace.edist`. -/ private theorem dist_nonneg' {α} {x y : α} (dist : α → α → ℝ) (dist_self : ∀ x : α, dist x x = 0) (dist_comm : ∀ x y : α, dist x y = dist y x) (dist_triangle : ∀ x y z : α, dist x z ≤ dist x y + dist y z) : 0 ≤ dist x y := have : 0 ≤ 2 * dist x y := calc 0 = dist x x := (dist_self _).symm _ ≤ dist x y + dist y x := dist_triangle _ _ _ _ = 2 * dist x y := by rw [two_mul, dist_comm] nonneg_of_mul_nonneg_right this two_pos /-- A pseudometric space is a type endowed with a `ℝ`-valued distance `dist` satisfying reflexivity `dist x x = 0`, commutativity `dist x y = dist y x`, and the triangle inequality `dist x z ≤ dist x y + dist y z`. Note that we do not require `dist x y = 0 → x = y`. See metric spaces (`MetricSpace`) for the similar class with that stronger assumption. Any pseudometric space is a topological space and a uniform space (see `TopologicalSpace`, `UniformSpace`), where the topology and uniformity come from the metric. Note that a T1 pseudometric space is just a metric space. We make the uniformity/topology part of the data instead of deriving it from the metric. This eg ensures that we do not get a diamond when doing `[PseudoMetricSpace α] [PseudoMetricSpace β] : TopologicalSpace (α × β)`: The product metric and product topology agree, but not definitionally so. See Note [forgetful inheritance]. -/ class PseudoMetricSpace (α : Type u) : Type u extends Dist α where dist_self : ∀ x : α, dist x x = 0 dist_comm : ∀ x y : α, dist x y = dist y x dist_triangle : ∀ x y z : α, dist x z ≤ dist x y + dist y z /-- Extended distance between two points -/ edist : α → α → ℝ≥0∞ := fun x y => ENNReal.ofNNReal ⟨dist x y, dist_nonneg' _ ‹_› ‹_› ‹_›⟩ edist_dist : ∀ x y : α, edist x y = ENNReal.ofReal (dist x y) := by intros x y; exact ENNReal.coe_nnreal_eq _ toUniformSpace : UniformSpace α := .ofDist dist dist_self dist_comm dist_triangle uniformity_dist : 𝓤 α = ⨅ ε > 0, 𝓟 { p : α × α | dist p.1 p.2 < ε } := by intros; rfl toBornology : Bornology α := Bornology.ofDist dist dist_comm dist_triangle cobounded_sets : (Bornology.cobounded α).sets = { s | ∃ C : ℝ, ∀ x ∈ sᶜ, ∀ y ∈ sᶜ, dist x y ≤ C } := by intros; rfl /-- Two pseudo metric space structures with the same distance function coincide. -/ @[ext] theorem PseudoMetricSpace.ext {α : Type*} {m m' : PseudoMetricSpace α} (h : m.toDist = m'.toDist) : m = m' := by let d := m.toDist obtain ⟨_, _, _, _, hed, _, hU, _, hB⟩ := m let d' := m'.toDist obtain ⟨_, _, _, _, hed', _, hU', _, hB'⟩ := m' obtain rfl : d = d' := h congr · ext x y : 2 rw [hed, hed'] · exact UniformSpace.ext (hU.trans hU'.symm) · ext : 2 rw [← Filter.mem_sets, ← Filter.mem_sets, hB, hB'] variable [PseudoMetricSpace α] attribute [instance] PseudoMetricSpace.toUniformSpace PseudoMetricSpace.toBornology -- see Note [lower instance priority] instance (priority := 200) PseudoMetricSpace.toEDist : EDist α := ⟨PseudoMetricSpace.edist⟩ /-- Construct a pseudo-metric space structure whose underlying topological space structure (definitionally) agrees which a pre-existing topology which is compatible with a given distance function. -/ def PseudoMetricSpace.ofDistTopology {α : Type u} [TopologicalSpace α] (dist : α → α → ℝ) (dist_self : ∀ x : α, dist x x = 0) (dist_comm : ∀ x y : α, dist x y = dist y x) (dist_triangle : ∀ x y z : α, dist x z ≤ dist x y + dist y z) (H : ∀ s : Set α, IsOpen s ↔ ∀ x ∈ s, ∃ ε > 0, ∀ y, dist x y < ε → y ∈ s) : PseudoMetricSpace α := { dist := dist dist_self := dist_self dist_comm := dist_comm dist_triangle := dist_triangle toUniformSpace := (UniformSpace.ofDist dist dist_self dist_comm dist_triangle).replaceTopology <| TopologicalSpace.ext_iff.2 fun s ↦ (H s).trans <| forall₂_congr fun x _ ↦ ((UniformSpace.hasBasis_ofFun (exists_gt (0 : ℝ)) dist dist_self dist_comm dist_triangle UniformSpace.ofDist_aux).comap (Prod.mk x)).mem_iff.symm uniformity_dist := rfl toBornology := Bornology.ofDist dist dist_comm dist_triangle cobounded_sets := rfl } @[simp] theorem dist_self (x : α) : dist x x = 0 := PseudoMetricSpace.dist_self x theorem dist_comm (x y : α) : dist x y = dist y x := PseudoMetricSpace.dist_comm x y theorem edist_dist (x y : α) : edist x y = ENNReal.ofReal (dist x y) := PseudoMetricSpace.edist_dist x y @[bound] theorem dist_triangle (x y z : α) : dist x z ≤ dist x y + dist y z := PseudoMetricSpace.dist_triangle x y z theorem dist_triangle_left (x y z : α) : dist x y ≤ dist z x + dist z y := by rw [dist_comm z]; apply dist_triangle theorem dist_triangle_right (x y z : α) : dist x y ≤ dist x z + dist y z := by rw [dist_comm y]; apply dist_triangle theorem dist_triangle4 (x y z w : α) : dist x w ≤ dist x y + dist y z + dist z w := calc dist x w ≤ dist x z + dist z w := dist_triangle x z w _ ≤ dist x y + dist y z + dist z w := add_le_add_right (dist_triangle x y z) _ theorem dist_triangle4_left (x₁ y₁ x₂ y₂ : α) : dist x₂ y₂ ≤ dist x₁ y₁ + (dist x₁ x₂ + dist y₁ y₂) := by rw [add_left_comm, dist_comm x₁, ← add_assoc] apply dist_triangle4 theorem dist_triangle4_right (x₁ y₁ x₂ y₂ : α) : dist x₁ y₁ ≤ dist x₁ x₂ + dist y₁ y₂ + dist x₂ y₂ := by rw [add_right_comm, dist_comm y₁] apply dist_triangle4 theorem dist_triangle8 (a b c d e f g h : α) : dist a h ≤ dist a b + dist b c + dist c d + dist d e + dist e f + dist f g + dist g h := by apply le_trans (dist_triangle4 a f g h) apply add_le_add_right (add_le_add_right _ (dist f g)) (dist g h) apply le_trans (dist_triangle4 a d e f) apply add_le_add_right (add_le_add_right _ (dist d e)) (dist e f) exact dist_triangle4 a b c d theorem swap_dist : Function.swap (@dist α _) = dist := by funext x y; exact dist_comm _ _ theorem abs_dist_sub_le (x y z : α) : |dist x z - dist y z| ≤ dist x y := abs_sub_le_iff.2 ⟨sub_le_iff_le_add.2 (dist_triangle _ _ _), sub_le_iff_le_add.2 (dist_triangle_left _ _ _)⟩ @[bound] theorem dist_nonneg {x y : α} : 0 ≤ dist x y := dist_nonneg' dist dist_self dist_comm dist_triangle namespace Mathlib.Meta.Positivity open Lean Meta Qq Function /-- Extension for the `positivity` tactic: distances are nonnegative. -/ @[positivity Dist.dist _ _] def evalDist : PositivityExt where eval {u α} _zα _pα e := do match u, α, e with | 0, ~q(ℝ), ~q(@Dist.dist $β $inst $a $b) => let _inst ← synthInstanceQ q(PseudoMetricSpace $β) assertInstancesCommute pure (.nonnegative q(dist_nonneg)) | _, _, _ => throwError "not dist" end Mathlib.Meta.Positivity example {x y : α} : 0 ≤ dist x y := by positivity @[simp] theorem abs_dist {a b : α} : |dist a b| = dist a b := abs_of_nonneg dist_nonneg /-- A version of `Dist` that takes value in `ℝ≥0`. -/ class NNDist (α : Type*) where /-- Nonnegative distance between two points -/ nndist : α → α → ℝ≥0 export NNDist (nndist) -- see Note [lower instance priority] /-- Distance as a nonnegative real number. -/ instance (priority := 100) PseudoMetricSpace.toNNDist : NNDist α := ⟨fun a b => ⟨dist a b, dist_nonneg⟩⟩ /-- Express `dist` in terms of `nndist` -/ theorem dist_nndist (x y : α) : dist x y = nndist x y := rfl @[simp, norm_cast] theorem coe_nndist (x y : α) : ↑(nndist x y) = dist x y := rfl /-- Express `edist` in terms of `nndist` -/ theorem edist_nndist (x y : α) : edist x y = nndist x y := by rw [edist_dist, dist_nndist, ENNReal.ofReal_coe_nnreal] /-- Express `nndist` in terms of `edist` -/ theorem nndist_edist (x y : α) : nndist x y = (edist x y).toNNReal := by simp [edist_nndist] @[simp, norm_cast] theorem coe_nnreal_ennreal_nndist (x y : α) : ↑(nndist x y) = edist x y := (edist_nndist x y).symm @[simp, norm_cast] theorem edist_lt_coe {x y : α} {c : ℝ≥0} : edist x y < c ↔ nndist x y < c := by rw [edist_nndist, ENNReal.coe_lt_coe] @[simp, norm_cast] theorem edist_le_coe {x y : α} {c : ℝ≥0} : edist x y ≤ c ↔ nndist x y ≤ c := by rw [edist_nndist, ENNReal.coe_le_coe] /-- In a pseudometric space, the extended distance is always finite -/ theorem edist_lt_top {α : Type*} [PseudoMetricSpace α] (x y : α) : edist x y < ⊤ := (edist_dist x y).symm ▸ ENNReal.ofReal_lt_top /-- In a pseudometric space, the extended distance is always finite -/ theorem edist_ne_top (x y : α) : edist x y ≠ ⊤ := (edist_lt_top x y).ne /-- `nndist x x` vanishes -/ @[simp] theorem nndist_self (a : α) : nndist a a = 0 := NNReal.coe_eq_zero.1 (dist_self a) @[simp, norm_cast] theorem dist_lt_coe {x y : α} {c : ℝ≥0} : dist x y < c ↔ nndist x y < c := Iff.rfl @[simp, norm_cast] theorem dist_le_coe {x y : α} {c : ℝ≥0} : dist x y ≤ c ↔ nndist x y ≤ c := Iff.rfl @[simp] theorem edist_lt_ofReal {x y : α} {r : ℝ} : edist x y < ENNReal.ofReal r ↔ dist x y < r := by rw [edist_dist, ENNReal.ofReal_lt_ofReal_iff_of_nonneg dist_nonneg] @[simp] theorem edist_le_ofReal {x y : α} {r : ℝ} (hr : 0 ≤ r) : edist x y ≤ ENNReal.ofReal r ↔ dist x y ≤ r := by rw [edist_dist, ENNReal.ofReal_le_ofReal_iff hr] /-- Express `nndist` in terms of `dist` -/ theorem nndist_dist (x y : α) : nndist x y = Real.toNNReal (dist x y) := by rw [dist_nndist, Real.toNNReal_coe] theorem nndist_comm (x y : α) : nndist x y = nndist y x := NNReal.eq <| dist_comm x y /-- Triangle inequality for the nonnegative distance -/ theorem nndist_triangle (x y z : α) : nndist x z ≤ nndist x y + nndist y z := dist_triangle _ _ _ theorem nndist_triangle_left (x y z : α) : nndist x y ≤ nndist z x + nndist z y := dist_triangle_left _ _ _ theorem nndist_triangle_right (x y z : α) : nndist x y ≤ nndist x z + nndist y z := dist_triangle_right _ _ _ /-- Express `dist` in terms of `edist` -/ theorem dist_edist (x y : α) : dist x y = (edist x y).toReal := by rw [edist_dist, ENNReal.toReal_ofReal dist_nonneg] namespace Metric -- instantiate pseudometric space as a topology variable {x y z : α} {δ ε ε₁ ε₂ : ℝ} {s : Set α} /-- `ball x ε` is the set of all points `y` with `dist y x < ε` -/ def ball (x : α) (ε : ℝ) : Set α := { y | dist y x < ε } @[simp] theorem mem_ball : y ∈ ball x ε ↔ dist y x < ε := Iff.rfl theorem mem_ball' : y ∈ ball x ε ↔ dist x y < ε := by rw [dist_comm, mem_ball] theorem pos_of_mem_ball (hy : y ∈ ball x ε) : 0 < ε := dist_nonneg.trans_lt hy theorem mem_ball_self (h : 0 < ε) : x ∈ ball x ε := by rwa [mem_ball, dist_self] @[simp] theorem nonempty_ball : (ball x ε).Nonempty ↔ 0 < ε := ⟨fun ⟨_x, hx⟩ => pos_of_mem_ball hx, fun h => ⟨x, mem_ball_self h⟩⟩ @[simp] theorem ball_eq_empty : ball x ε = ∅ ↔ ε ≤ 0 := by rw [← not_nonempty_iff_eq_empty, nonempty_ball, not_lt] @[simp] theorem ball_zero : ball x 0 = ∅ := by rw [ball_eq_empty] /-- If a point belongs to an open ball, then there is a strictly smaller radius whose ball also contains it. See also `exists_lt_subset_ball`. -/ theorem exists_lt_mem_ball_of_mem_ball (h : x ∈ ball y ε) : ∃ ε' < ε, x ∈ ball y ε' := by simp only [mem_ball] at h ⊢ exact ⟨(dist x y + ε) / 2, by linarith, by linarith⟩ theorem ball_eq_ball (ε : ℝ) (x : α) : UniformSpace.ball x { p | dist p.2 p.1 < ε } = Metric.ball x ε := rfl theorem ball_eq_ball' (ε : ℝ) (x : α) : UniformSpace.ball x { p | dist p.1 p.2 < ε } = Metric.ball x ε := by ext simp [dist_comm, UniformSpace.ball] @[simp] theorem iUnion_ball_nat (x : α) : ⋃ n : ℕ, ball x n = univ := iUnion_eq_univ_iff.2 fun y => exists_nat_gt (dist y x) @[simp] theorem iUnion_ball_nat_succ (x : α) : ⋃ n : ℕ, ball x (n + 1) = univ := iUnion_eq_univ_iff.2 fun y => (exists_nat_gt (dist y x)).imp fun _ h => h.trans (lt_add_one _) /-- `closedBall x ε` is the set of all points `y` with `dist y x ≤ ε` -/ def closedBall (x : α) (ε : ℝ) := { y | dist y x ≤ ε } @[simp] theorem mem_closedBall : y ∈ closedBall x ε ↔ dist y x ≤ ε := Iff.rfl theorem mem_closedBall' : y ∈ closedBall x ε ↔ dist x y ≤ ε := by rw [dist_comm, mem_closedBall] /-- `sphere x ε` is the set of all points `y` with `dist y x = ε` -/ def sphere (x : α) (ε : ℝ) := { y | dist y x = ε } @[simp] theorem mem_sphere : y ∈ sphere x ε ↔ dist y x = ε := Iff.rfl theorem mem_sphere' : y ∈ sphere x ε ↔ dist x y = ε := by rw [dist_comm, mem_sphere] theorem ne_of_mem_sphere (h : y ∈ sphere x ε) (hε : ε ≠ 0) : y ≠ x := ne_of_mem_of_not_mem h <| by simpa using hε.symm theorem nonneg_of_mem_sphere (hy : y ∈ sphere x ε) : 0 ≤ ε := dist_nonneg.trans_eq hy @[simp] theorem sphere_eq_empty_of_neg (hε : ε < 0) : sphere x ε = ∅ := Set.eq_empty_iff_forall_not_mem.mpr fun _y hy => (nonneg_of_mem_sphere hy).not_lt hε theorem sphere_eq_empty_of_subsingleton [Subsingleton α] (hε : ε ≠ 0) : sphere x ε = ∅ := Set.eq_empty_iff_forall_not_mem.mpr fun _ h => ne_of_mem_sphere h hε (Subsingleton.elim _ _) instance sphere_isEmpty_of_subsingleton [Subsingleton α] [NeZero ε] : IsEmpty (sphere x ε) := by rw [sphere_eq_empty_of_subsingleton (NeZero.ne ε)]; infer_instance theorem closedBall_eq_singleton_of_subsingleton [Subsingleton α] (h : 0 ≤ ε) : closedBall x ε = {x} := by ext x' simpa [Subsingleton.allEq x x'] theorem ball_eq_singleton_of_subsingleton [Subsingleton α] (h : 0 < ε) : ball x ε = {x} := by ext x' simpa [Subsingleton.allEq x x'] theorem mem_closedBall_self (h : 0 ≤ ε) : x ∈ closedBall x ε := by rwa [mem_closedBall, dist_self] @[simp] theorem nonempty_closedBall : (closedBall x ε).Nonempty ↔ 0 ≤ ε := ⟨fun ⟨_x, hx⟩ => dist_nonneg.trans hx, fun h => ⟨x, mem_closedBall_self h⟩⟩ @[simp] theorem closedBall_eq_empty : closedBall x ε = ∅ ↔ ε < 0 := by rw [← not_nonempty_iff_eq_empty, nonempty_closedBall, not_le] /-- Closed balls and spheres coincide when the radius is non-positive -/ theorem closedBall_eq_sphere_of_nonpos (hε : ε ≤ 0) : closedBall x ε = sphere x ε := Set.ext fun _ => (hε.trans dist_nonneg).le_iff_eq theorem ball_subset_closedBall : ball x ε ⊆ closedBall x ε := fun _y hy => mem_closedBall.2 (le_of_lt hy) theorem sphere_subset_closedBall : sphere x ε ⊆ closedBall x ε := fun _ => le_of_eq lemma sphere_subset_ball {r R : ℝ} (h : r < R) : sphere x r ⊆ ball x R := fun _x hx ↦ (mem_sphere.1 hx).trans_lt h theorem closedBall_disjoint_ball (h : δ + ε ≤ dist x y) : Disjoint (closedBall x δ) (ball y ε) := Set.disjoint_left.mpr fun _a ha1 ha2 => (h.trans <| dist_triangle_left _ _ _).not_lt <| add_lt_add_of_le_of_lt ha1 ha2 theorem ball_disjoint_closedBall (h : δ + ε ≤ dist x y) : Disjoint (ball x δ) (closedBall y ε) := (closedBall_disjoint_ball <| by rwa [add_comm, dist_comm]).symm theorem ball_disjoint_ball (h : δ + ε ≤ dist x y) : Disjoint (ball x δ) (ball y ε) := (closedBall_disjoint_ball h).mono_left ball_subset_closedBall theorem closedBall_disjoint_closedBall (h : δ + ε < dist x y) : Disjoint (closedBall x δ) (closedBall y ε) := Set.disjoint_left.mpr fun _a ha1 ha2 => h.not_le <| (dist_triangle_left _ _ _).trans <| add_le_add ha1 ha2 theorem sphere_disjoint_ball : Disjoint (sphere x ε) (ball x ε) := Set.disjoint_left.mpr fun _y hy₁ hy₂ => absurd hy₁ <| ne_of_lt hy₂ @[simp] theorem ball_union_sphere : ball x ε ∪ sphere x ε = closedBall x ε := Set.ext fun _y => (@le_iff_lt_or_eq ℝ _ _ _).symm @[simp] theorem sphere_union_ball : sphere x ε ∪ ball x ε = closedBall x ε := by rw [union_comm, ball_union_sphere] @[simp] theorem closedBall_diff_sphere : closedBall x ε \ sphere x ε = ball x ε := by rw [← ball_union_sphere, Set.union_diff_cancel_right sphere_disjoint_ball.symm.le_bot] @[simp] theorem closedBall_diff_ball : closedBall x ε \ ball x ε = sphere x ε := by rw [← ball_union_sphere, Set.union_diff_cancel_left sphere_disjoint_ball.symm.le_bot] theorem mem_ball_comm : x ∈ ball y ε ↔ y ∈ ball x ε := by rw [mem_ball', mem_ball] theorem mem_closedBall_comm : x ∈ closedBall y ε ↔ y ∈ closedBall x ε := by rw [mem_closedBall', mem_closedBall] theorem mem_sphere_comm : x ∈ sphere y ε ↔ y ∈ sphere x ε := by rw [mem_sphere', mem_sphere] @[gcongr] theorem ball_subset_ball (h : ε₁ ≤ ε₂) : ball x ε₁ ⊆ ball x ε₂ := fun _y yx => lt_of_lt_of_le (mem_ball.1 yx) h theorem closedBall_eq_bInter_ball : closedBall x ε = ⋂ δ > ε, ball x δ := by ext y; rw [mem_closedBall, ← forall_lt_iff_le', mem_iInter₂]; rfl theorem ball_subset_ball' (h : ε₁ + dist x y ≤ ε₂) : ball x ε₁ ⊆ ball y ε₂ := fun z hz => calc dist z y ≤ dist z x + dist x y := dist_triangle _ _ _ _ < ε₁ + dist x y := add_lt_add_right (mem_ball.1 hz) _ _ ≤ ε₂ := h @[gcongr] theorem closedBall_subset_closedBall (h : ε₁ ≤ ε₂) : closedBall x ε₁ ⊆ closedBall x ε₂ := fun _y (yx : _ ≤ ε₁) => le_trans yx h theorem closedBall_subset_closedBall' (h : ε₁ + dist x y ≤ ε₂) : closedBall x ε₁ ⊆ closedBall y ε₂ := fun z hz => calc dist z y ≤ dist z x + dist x y := dist_triangle _ _ _ _ ≤ ε₁ + dist x y := add_le_add_right (mem_closedBall.1 hz) _ _ ≤ ε₂ := h theorem closedBall_subset_ball (h : ε₁ < ε₂) : closedBall x ε₁ ⊆ ball x ε₂ := fun y (yh : dist y x ≤ ε₁) => lt_of_le_of_lt yh h theorem closedBall_subset_ball' (h : ε₁ + dist x y < ε₂) : closedBall x ε₁ ⊆ ball y ε₂ := fun z hz => calc dist z y ≤ dist z x + dist x y := dist_triangle _ _ _ _ ≤ ε₁ + dist x y := add_le_add_right (mem_closedBall.1 hz) _ _ < ε₂ := h theorem dist_le_add_of_nonempty_closedBall_inter_closedBall (h : (closedBall x ε₁ ∩ closedBall y ε₂).Nonempty) : dist x y ≤ ε₁ + ε₂ := let ⟨z, hz⟩ := h calc dist x y ≤ dist z x + dist z y := dist_triangle_left _ _ _ _ ≤ ε₁ + ε₂ := add_le_add hz.1 hz.2 theorem dist_lt_add_of_nonempty_closedBall_inter_ball (h : (closedBall x ε₁ ∩ ball y ε₂).Nonempty) : dist x y < ε₁ + ε₂ := let ⟨z, hz⟩ := h calc dist x y ≤ dist z x + dist z y := dist_triangle_left _ _ _ _ < ε₁ + ε₂ := add_lt_add_of_le_of_lt hz.1 hz.2 theorem dist_lt_add_of_nonempty_ball_inter_closedBall (h : (ball x ε₁ ∩ closedBall y ε₂).Nonempty) : dist x y < ε₁ + ε₂ := by rw [inter_comm] at h rw [add_comm, dist_comm] exact dist_lt_add_of_nonempty_closedBall_inter_ball h theorem dist_lt_add_of_nonempty_ball_inter_ball (h : (ball x ε₁ ∩ ball y ε₂).Nonempty) : dist x y < ε₁ + ε₂ := dist_lt_add_of_nonempty_closedBall_inter_ball <| h.mono (inter_subset_inter ball_subset_closedBall Subset.rfl) @[simp] theorem iUnion_closedBall_nat (x : α) : ⋃ n : ℕ, closedBall x n = univ := iUnion_eq_univ_iff.2 fun y => exists_nat_ge (dist y x) theorem iUnion_inter_closedBall_nat (s : Set α) (x : α) : ⋃ n : ℕ, s ∩ closedBall x n = s := by rw [← inter_iUnion, iUnion_closedBall_nat, inter_univ]
theorem ball_subset (h : dist x y ≤ ε₂ - ε₁) : ball x ε₁ ⊆ ball y ε₂ := fun z zx => by rw [← add_sub_cancel ε₁ ε₂]
Mathlib/Topology/MetricSpace/Pseudo/Defs.lean
563
564
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import Mathlib.LinearAlgebra.CliffordAlgebra.Basic import Mathlib.RingTheory.GradedAlgebra.Basic /-! # Results about the grading structure of the clifford algebra The main result is `CliffordAlgebra.gradedAlgebra`, which says that the clifford algebra is a ℤ₂-graded algebra (or "superalgebra"). -/ namespace CliffordAlgebra variable {R M : Type*} [CommRing R] [AddCommGroup M] [Module R M] variable {Q : QuadraticForm R M} open scoped DirectSum variable (Q) /-- The even or odd submodule, defined as the supremum of the even or odd powers of `(ι Q).range`. `evenOdd 0` is the even submodule, and `evenOdd 1` is the odd submodule. -/ def evenOdd (i : ZMod 2) : Submodule R (CliffordAlgebra Q) := ⨆ j : { n : ℕ // ↑n = i }, LinearMap.range (ι Q) ^ (j : ℕ) theorem one_le_evenOdd_zero : 1 ≤ evenOdd Q 0 := by refine le_trans ?_ (le_iSup _ ⟨0, Nat.cast_zero⟩) exact (pow_zero _).ge theorem range_ι_le_evenOdd_one : LinearMap.range (ι Q) ≤ evenOdd Q 1 := by refine le_trans ?_ (le_iSup _ ⟨1, Nat.cast_one⟩) exact (pow_one _).ge theorem ι_mem_evenOdd_one (m : M) : ι Q m ∈ evenOdd Q 1 := range_ι_le_evenOdd_one Q <| LinearMap.mem_range_self _ m theorem ι_mul_ι_mem_evenOdd_zero (m₁ m₂ : M) : ι Q m₁ * ι Q m₂ ∈ evenOdd Q 0 := Submodule.mem_iSup_of_mem ⟨2, rfl⟩ (by rw [Subtype.coe_mk, pow_two] exact Submodule.mul_mem_mul (LinearMap.mem_range_self (ι Q) m₁) (LinearMap.mem_range_self (ι Q) m₂)) theorem evenOdd_mul_le (i j : ZMod 2) : evenOdd Q i * evenOdd Q j ≤ evenOdd Q (i + j) := by simp_rw [evenOdd, Submodule.iSup_eq_span, Submodule.span_mul_span] apply Submodule.span_mono simp_rw [Set.iUnion_mul, Set.mul_iUnion, Set.iUnion_subset_iff, Set.mul_subset_iff] rintro ⟨xi, rfl⟩ ⟨yi, rfl⟩ x hx y hy refine Set.mem_iUnion.mpr ⟨⟨xi + yi, Nat.cast_add _ _⟩, ?_⟩ simp only [Subtype.coe_mk, Nat.cast_add, pow_add] exact Submodule.mul_mem_mul hx hy instance evenOdd.gradedMonoid : SetLike.GradedMonoid (evenOdd Q) where one_mem := Submodule.one_le.mp (one_le_evenOdd_zero Q) mul_mem _i _j _p _q hp hq := Submodule.mul_le.mp (evenOdd_mul_le Q _ _) _ hp _ hq /-- A version of `CliffordAlgebra.ι` that maps directly into the graded structure. This is primarily an auxiliary construction used to provide `CliffordAlgebra.gradedAlgebra`. -/ protected def GradedAlgebra.ι : M →ₗ[R] ⨁ i : ZMod 2, evenOdd Q i := DirectSum.lof R (ZMod 2) (fun i => ↥(evenOdd Q i)) 1 ∘ₗ (ι Q).codRestrict _ (ι_mem_evenOdd_one Q) theorem GradedAlgebra.ι_apply (m : M) : GradedAlgebra.ι Q m = DirectSum.of (fun i => ↥(evenOdd Q i)) 1 ⟨ι Q m, ι_mem_evenOdd_one Q m⟩ := rfl nonrec theorem GradedAlgebra.ι_sq_scalar (m : M) : GradedAlgebra.ι Q m * GradedAlgebra.ι Q m = algebraMap R _ (Q m) := by rw [GradedAlgebra.ι_apply Q, DirectSum.of_mul_of, DirectSum.algebraMap_apply] exact DirectSum.of_eq_of_gradedMonoid_eq (Sigma.subtype_ext rfl <| ι_sq_scalar _ _) theorem GradedAlgebra.lift_ι_eq (i' : ZMod 2) (x' : evenOdd Q i') : lift Q ⟨GradedAlgebra.ι Q, GradedAlgebra.ι_sq_scalar Q⟩ x' = DirectSum.of (fun i => evenOdd Q i) i' x' := by obtain ⟨x', hx'⟩ := x' dsimp only [Subtype.coe_mk, DirectSum.lof_eq_of] induction hx' using Submodule.iSup_induction' with | mem i x hx => obtain ⟨i, rfl⟩ := i dsimp only [Subtype.coe_mk] at hx induction hx using Submodule.pow_induction_on_left' with | algebraMap r => rw [AlgHom.commutes, DirectSum.algebraMap_apply]; rfl | add x y i hx hy ihx ihy => rw [map_add, ihx, ihy, ← AddMonoidHom.map_add] rfl | mem_mul m hm i x hx ih => obtain ⟨_, rfl⟩ := hm rw [map_mul, ih, lift_ι_apply, GradedAlgebra.ι_apply Q, DirectSum.of_mul_of] refine DirectSum.of_eq_of_gradedMonoid_eq (Sigma.subtype_ext ?_ ?_) <;> dsimp only [GradedMonoid.mk, Subtype.coe_mk] · rw [Nat.succ_eq_add_one, add_comm, Nat.cast_add, Nat.cast_one] rfl | zero => rw [map_zero] apply Eq.symm apply DFinsupp.single_eq_zero.mpr; rfl | add x y hx hy ihx ihy => rw [map_add, ihx, ihy, ← AddMonoidHom.map_add]; rfl /-- The clifford algebra is graded by the even and odd parts. -/ instance gradedAlgebra : GradedAlgebra (evenOdd Q) := GradedAlgebra.ofAlgHom (evenOdd Q) -- while not necessary, the `by apply` makes this elaborate faster (lift Q ⟨by apply GradedAlgebra.ι Q, by apply GradedAlgebra.ι_sq_scalar Q⟩) -- the proof from here onward is mostly similar to the `TensorAlgebra` case, with some extra -- handling for the `iSup` in `evenOdd`. (by ext m dsimp only [LinearMap.comp_apply, AlgHom.toLinearMap_apply, AlgHom.comp_apply, AlgHom.id_apply] rw [lift_ι_apply, GradedAlgebra.ι_apply Q, DirectSum.coeAlgHom_of, Subtype.coe_mk]) (by apply GradedAlgebra.lift_ι_eq Q) theorem iSup_ι_range_eq_top : ⨆ i : ℕ, LinearMap.range (ι Q) ^ i = ⊤ := by rw [← (DirectSum.Decomposition.isInternal (evenOdd Q)).submodule_iSup_eq_top, eq_comm] calc -- Porting note: needs extra annotations, no longer unifies against the goal in the face of -- ambiguity ⨆ (i : ZMod 2) (j : { n : ℕ // ↑n = i }), LinearMap.range (ι Q) ^ (j : ℕ) = ⨆ i : Σ i : ZMod 2, { n : ℕ // ↑n = i }, LinearMap.range (ι Q) ^ (i.2 : ℕ) := by rw [iSup_sigma] _ = ⨆ i : ℕ, LinearMap.range (ι Q) ^ i := Function.Surjective.iSup_congr (fun i => i.2) (fun i => ⟨⟨_, i, rfl⟩, rfl⟩) fun _ => rfl theorem evenOdd_isCompl : IsCompl (evenOdd Q 0) (evenOdd Q 1) := (DirectSum.Decomposition.isInternal (evenOdd Q)).isCompl zero_ne_one <| by have : (Finset.univ : Finset (ZMod 2)) = {0, 1} := rfl simpa using congr_arg ((↑) : Finset (ZMod 2) → Set (ZMod 2)) this /-- To show a property is true on the even or odd part, it suffices to show it is true on the scalars or vectors (respectively), closed under addition, and under left-multiplication by a pair of vectors. -/ @[elab_as_elim] theorem evenOdd_induction (n : ZMod 2) {motive : ∀ x, x ∈ evenOdd Q n → Prop} (range_ι_pow : ∀ (v) (h : v ∈ LinearMap.range (ι Q) ^ n.val), motive v (Submodule.mem_iSup_of_mem ⟨n.val, n.natCast_zmod_val⟩ h)) (add : ∀ x y hx hy, motive x hx → motive y hy → motive (x + y) (Submodule.add_mem _ hx hy)) (ι_mul_ι_mul : ∀ m₁ m₂ x hx, motive x hx → motive (ι Q m₁ * ι Q m₂ * x) (zero_add n ▸ SetLike.mul_mem_graded (ι_mul_ι_mem_evenOdd_zero Q m₁ m₂) hx)) (x : CliffordAlgebra Q) (hx : x ∈ evenOdd Q n) : motive x hx := by apply Submodule.iSup_induction' (motive := motive) _ _ (range_ι_pow 0 (Submodule.zero_mem _)) add refine Subtype.rec ?_
simp_rw [ZMod.natCast_eq_iff, add_comm n.val] rintro n' ⟨k, rfl⟩ xv simp_rw [pow_add, pow_mul] intro hxv
Mathlib/LinearAlgebra/CliffordAlgebra/Grading.lean
152
155
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import Mathlib.LinearAlgebra.Dimension.StrongRankCondition import Mathlib.LinearAlgebra.FreeModule.Basic import Mathlib.LinearAlgebra.Matrix.ToLin /-! # Free modules over PID A free `R`-module `M` is a module with a basis over `R`, equivalently it is an `R`-module linearly equivalent to `ι →₀ R` for some `ι`. This file proves a submodule of a free `R`-module of finite rank is also a free `R`-module of finite rank, if `R` is a principal ideal domain (PID), i.e. we have instances `[IsDomain R] [IsPrincipalIdealRing R]`. We express "free `R`-module of finite rank" as a module `M` which has a basis `b : ι → R`, where `ι` is a `Fintype`. We call the cardinality of `ι` the rank of `M` in this file; it would be equal to `finrank R M` if `R` is a field and `M` is a vector space. ## Main results In this section, `M` is a free and finitely generated `R`-module, and `N` is a submodule of `M`. - `Submodule.inductionOnRank`: if `P` holds for `⊥ : Submodule R M` and if `P N` follows from `P N'` for all `N'` that are of lower rank, then `P` holds on all submodules - `Submodule.exists_basis_of_pid`: if `R` is a PID, then `N : Submodule R M` is free and finitely generated. This is the first part of the structure theorem for modules. - `Submodule.smithNormalForm`: if `R` is a PID, then `M` has a basis `bM` and `N` has a basis `bN` such that `bN i = a i • bM i`. Equivalently, a linear map `f : M →ₗ M` with `range f = N` can be written as a matrix in Smith normal form, a diagonal matrix with the coefficients `a i` along the diagonal. ## Tags free module, finitely generated module, rank, structure theorem -/ universe u v section Ring variable {R : Type u} {M : Type v} [Ring R] [AddCommGroup M] [Module R M] variable {ι : Type*} (b : Basis ι R M) open Submodule.IsPrincipal Submodule theorem eq_bot_of_generator_maximal_map_eq_zero (b : Basis ι R M) {N : Submodule R M} {ϕ : M →ₗ[R] R} (hϕ : ∀ ψ : M →ₗ[R] R, ¬N.map ϕ < N.map ψ) [(N.map ϕ).IsPrincipal] (hgen : generator (N.map ϕ) = (0 : R)) : N = ⊥ := by rw [Submodule.eq_bot_iff] intro x hx refine b.ext_elem fun i ↦ ?_ rw [(eq_bot_iff_generator_eq_zero _).mpr hgen] at hϕ rw [LinearEquiv.map_zero, Finsupp.zero_apply] exact (Submodule.eq_bot_iff _).mp (not_bot_lt_iff.1 <| hϕ (Finsupp.lapply i ∘ₗ ↑b.repr)) _ ⟨x, hx, rfl⟩ theorem eq_bot_of_generator_maximal_submoduleImage_eq_zero {N O : Submodule R M} (b : Basis ι R O) (hNO : N ≤ O) {ϕ : O →ₗ[R] R} (hϕ : ∀ ψ : O →ₗ[R] R, ¬ϕ.submoduleImage N < ψ.submoduleImage N) [(ϕ.submoduleImage N).IsPrincipal] (hgen : generator (ϕ.submoduleImage N) = 0) : N = ⊥ := by rw [Submodule.eq_bot_iff] intro x hx refine (mk_eq_zero _ _).mp (show (⟨x, hNO hx⟩ : O) = 0 from b.ext_elem fun i ↦ ?_) rw [(eq_bot_iff_generator_eq_zero _).mpr hgen] at hϕ rw [LinearEquiv.map_zero, Finsupp.zero_apply] refine (Submodule.eq_bot_iff _).mp (not_bot_lt_iff.1 <| hϕ (Finsupp.lapply i ∘ₗ ↑b.repr)) _ ?_ exact (LinearMap.mem_submoduleImage_of_le hNO).mpr ⟨x, hx, rfl⟩ end Ring section IsDomain variable {ι : Type*} {R : Type*} [CommRing R] [IsDomain R] variable {M : Type*} [AddCommGroup M] [Module R M] {b : ι → M} open Submodule.IsPrincipal Set Submodule theorem dvd_generator_iff {I : Ideal R} [I.IsPrincipal] {x : R} (hx : x ∈ I) : x ∣ generator I ↔ I = Ideal.span {x} := by conv_rhs => rw [← span_singleton_generator I] rw [Ideal.submodule_span_eq, Ideal.span_singleton_eq_span_singleton, ← dvd_dvd_iff_associated, ← mem_iff_generator_dvd] exact ⟨fun h ↦ ⟨hx, h⟩, fun h ↦ h.2⟩ end IsDomain section PrincipalIdealDomain open Submodule.IsPrincipal Set Submodule variable {ι : Type*} {R : Type*} [CommRing R] variable {M : Type*} [AddCommGroup M] [Module R M] {b : ι → M} section StrongRankCondition variable [IsDomain R] [IsPrincipalIdealRing R] open Submodule.IsPrincipal theorem generator_maximal_submoduleImage_dvd {N O : Submodule R M} (hNO : N ≤ O) {ϕ : O →ₗ[R] R} (hϕ : ∀ ψ : O →ₗ[R] R, ¬ϕ.submoduleImage N < ψ.submoduleImage N) [(ϕ.submoduleImage N).IsPrincipal] (y : M) (yN : y ∈ N) (ϕy_eq : ϕ ⟨y, hNO yN⟩ = generator (ϕ.submoduleImage N)) (ψ : O →ₗ[R] R) : generator (ϕ.submoduleImage N) ∣ ψ ⟨y, hNO yN⟩ := by let a : R := generator (ϕ.submoduleImage N) let d : R := IsPrincipal.generator (Submodule.span R {a, ψ ⟨y, hNO yN⟩}) have d_dvd_left : d ∣ a := (mem_iff_generator_dvd _).mp (subset_span (mem_insert _ _)) have d_dvd_right : d ∣ ψ ⟨y, hNO yN⟩ := (mem_iff_generator_dvd _).mp (subset_span (mem_insert_of_mem _ (mem_singleton _))) refine dvd_trans ?_ d_dvd_right rw [dvd_generator_iff, Ideal.span, ← span_singleton_generator (Submodule.span R {a, ψ ⟨y, hNO yN⟩})] · obtain ⟨r₁, r₂, d_eq⟩ : ∃ r₁ r₂ : R, d = r₁ * a + r₂ * ψ ⟨y, hNO yN⟩ := by obtain ⟨r₁, r₂', hr₂', hr₁⟩ := mem_span_insert.mp (IsPrincipal.generator_mem (Submodule.span R {a, ψ ⟨y, hNO yN⟩})) obtain ⟨r₂, rfl⟩ := mem_span_singleton.mp hr₂' exact ⟨r₁, r₂, hr₁⟩ let ψ' : O →ₗ[R] R := r₁ • ϕ + r₂ • ψ have : span R {d} ≤ ψ'.submoduleImage N := by rw [span_le, singleton_subset_iff, SetLike.mem_coe, LinearMap.mem_submoduleImage_of_le hNO] refine ⟨y, yN, ?_⟩ change r₁ * ϕ ⟨y, hNO yN⟩ + r₂ * ψ ⟨y, hNO yN⟩ = d rw [d_eq, ϕy_eq] refine le_antisymm (this.trans (le_of_eq ?_)) (Ideal.span_singleton_le_span_singleton.mpr d_dvd_left) rw [span_singleton_generator] apply (le_trans _ this).eq_of_not_gt (hϕ ψ') rw [← span_singleton_generator (ϕ.submoduleImage N)] exact Ideal.span_singleton_le_span_singleton.mpr d_dvd_left · exact subset_span (mem_insert _ _) /-- The induction hypothesis of `Submodule.basisOfPid` and `Submodule.smithNormalForm`. Basically, it says: let `N ≤ M` be a pair of submodules, then we can find a pair of submodules `N' ≤ M'` of strictly smaller rank, whose basis we can extend to get a basis of `N` and `M`. Moreover, if the basis for `M'` is up to scalars a basis for `N'`, then the basis we find for `M` is up to scalars a basis for `N`. For `basis_of_pid` we only need the first half and can fix `M = ⊤`, for `smith_normal_form` we need the full statement, but must also feed in a basis for `M` using `basis_of_pid` to keep the induction going. -/ theorem Submodule.basis_of_pid_aux [Finite ι] {O : Type*} [AddCommGroup O] [Module R O] (M N : Submodule R O) (b'M : Basis ι R M) (N_bot : N ≠ ⊥) (N_le_M : N ≤ M) : ∃ y ∈ M, ∃ a : R, a • y ∈ N ∧ ∃ M' ≤ M, ∃ N' ≤ N, N' ≤ M' ∧ (∀ (c : R) (z : O), z ∈ M' → c • y + z = 0 → c = 0) ∧ (∀ (c : R) (z : O), z ∈ N' → c • a • y + z = 0 → c = 0) ∧ ∀ (n') (bN' : Basis (Fin n') R N'), ∃ bN : Basis (Fin (n' + 1)) R N, ∀ (m') (hn'm' : n' ≤ m') (bM' : Basis (Fin m') R M'), ∃ (hnm : n' + 1 ≤ m' + 1) (bM : Basis (Fin (m' + 1)) R M), ∀ as : Fin n' → R, (∀ i : Fin n', (bN' i : O) = as i • (bM' (Fin.castLE hn'm' i) : O)) → ∃ as' : Fin (n' + 1) → R, ∀ i : Fin (n' + 1), (bN i : O) = as' i • (bM (Fin.castLE hnm i) : O) := by -- Let `ϕ` be a maximal projection of `M` onto `R`, in the sense that there is -- no `ψ` whose image of `N` is larger than `ϕ`'s image of `N`. have : ∃ ϕ : M →ₗ[R] R, ∀ ψ : M →ₗ[R] R, ¬ϕ.submoduleImage N < ψ.submoduleImage N := by obtain ⟨P, P_eq, P_max⟩ := set_has_maximal_iff_noetherian.mpr (inferInstance : IsNoetherian R R) _ (show (Set.range fun ψ : M →ₗ[R] R ↦ ψ.submoduleImage N).Nonempty from ⟨_, Set.mem_range.mpr ⟨0, rfl⟩⟩) obtain ⟨ϕ, rfl⟩ := Set.mem_range.mp P_eq exact ⟨ϕ, fun ψ hψ ↦ P_max _ ⟨_, rfl⟩ hψ⟩ let ϕ := this.choose have ϕ_max := this.choose_spec -- Since `ϕ(N)` is an `R`-submodule of the PID `R`, -- it is principal and generated by some `a`. let a := generator (ϕ.submoduleImage N) have a_mem : a ∈ ϕ.submoduleImage N := generator_mem _ -- If `a` is zero, then the submodule is trivial. So let's assume `a ≠ 0`, `N ≠ ⊥`. by_cases a_zero : a = 0 · have := eq_bot_of_generator_maximal_submoduleImage_eq_zero b'M N_le_M ϕ_max a_zero contradiction -- We claim that `ϕ⁻¹ a = y` can be taken as basis element of `N`. obtain ⟨y, yN, ϕy_eq⟩ := (LinearMap.mem_submoduleImage_of_le N_le_M).mp a_mem have _ϕy_ne_zero : ϕ ⟨y, N_le_M yN⟩ ≠ 0 := fun h ↦ a_zero (ϕy_eq.symm.trans h) -- Write `y` as `a • y'` for some `y'`. have hdvd : ∀ i, a ∣ b'M.coord i ⟨y, N_le_M yN⟩ := fun i ↦ generator_maximal_submoduleImage_dvd N_le_M ϕ_max y yN ϕy_eq (b'M.coord i) choose c hc using hdvd cases nonempty_fintype ι let y' : O := ∑ i, c i • b'M i have y'M : y' ∈ M := M.sum_mem fun i _ ↦ M.smul_mem (c i) (b'M i).2 have mk_y' : (⟨y', y'M⟩ : M) = ∑ i, c i • b'M i := Subtype.ext (show y' = M.subtype _ by simp only [map_sum, map_smul] rfl) have a_smul_y' : a • y' = y := by refine Subtype.mk_eq_mk.mp (show (a • ⟨y', y'M⟩ : M) = ⟨y, N_le_M yN⟩ from ?_) rw [← b'M.sum_repr ⟨y, N_le_M yN⟩, mk_y', Finset.smul_sum] refine Finset.sum_congr rfl fun i _ ↦ ?_ rw [← mul_smul, ← hc] rfl -- We found a `y` and an `a`! refine ⟨y', y'M, a, a_smul_y'.symm ▸ yN, ?_⟩ have ϕy'_eq : ϕ ⟨y', y'M⟩ = 1 := mul_left_cancel₀ a_zero (calc a • ϕ ⟨y', y'M⟩ = ϕ ⟨a • y', _⟩ := (ϕ.map_smul a ⟨y', y'M⟩).symm _ = ϕ ⟨y, N_le_M yN⟩ := by simp only [a_smul_y'] _ = a := ϕy_eq _ = a * 1 := (mul_one a).symm ) have ϕy'_ne_zero : ϕ ⟨y', y'M⟩ ≠ 0 := by simpa only [ϕy'_eq] using one_ne_zero -- `M' := ker (ϕ : M → R)` is smaller than `M` and `N' := ker (ϕ : N → R)` is smaller than `N`. let M' : Submodule R O := (LinearMap.ker ϕ).map M.subtype let N' : Submodule R O := (LinearMap.ker (ϕ.comp (inclusion N_le_M))).map N.subtype have M'_le_M : M' ≤ M := M.map_subtype_le (LinearMap.ker ϕ) have N'_le_M' : N' ≤ M' := by intro x hx simp only [N', mem_map, LinearMap.mem_ker] at hx ⊢ obtain ⟨⟨x, xN⟩, hx, rfl⟩ := hx exact ⟨⟨x, N_le_M xN⟩, hx, rfl⟩ have N'_le_N : N' ≤ N := N.map_subtype_le (LinearMap.ker (ϕ.comp (inclusion N_le_M))) -- So fill in those results as well. refine ⟨M', M'_le_M, N', N'_le_N, N'_le_M', ?_⟩ -- Note that `y'` is orthogonal to `M'`. have y'_ortho_M' : ∀ (c : R), ∀ z ∈ M', c • y' + z = 0 → c = 0 := by intro c x xM' hc obtain ⟨⟨x, xM⟩, hx', rfl⟩ := Submodule.mem_map.mp xM' rw [LinearMap.mem_ker] at hx' have hc' : (c • ⟨y', y'M⟩ + ⟨x, xM⟩ : M) = 0 := by exact @Subtype.coe_injective O (· ∈ M) _ _ hc simpa only [LinearMap.map_add, LinearMap.map_zero, LinearMap.map_smul, smul_eq_mul, add_zero, mul_eq_zero, ϕy'_ne_zero, hx', or_false] using congr_arg ϕ hc' -- And `a • y'` is orthogonal to `N'`. have ay'_ortho_N' : ∀ (c : R), ∀ z ∈ N', c • a • y' + z = 0 → c = 0 := by intro c z zN' hc refine (mul_eq_zero.mp (y'_ortho_M' (a * c) z (N'_le_M' zN') ?_)).resolve_left a_zero rw [mul_comm, mul_smul, hc] -- So we can extend a basis for `N'` with `y` refine ⟨y'_ortho_M', ay'_ortho_N', fun n' bN' ↦ ⟨?_, ?_⟩⟩ · refine Basis.mkFinConsOfLE y yN bN' N'_le_N ?_ ?_ · intro c z zN' hc refine ay'_ortho_N' c z zN' ?_ rwa [← a_smul_y'] at hc · intro z zN obtain ⟨b, hb⟩ : _ ∣ ϕ ⟨z, N_le_M zN⟩ := generator_submoduleImage_dvd_of_mem N_le_M ϕ zN refine ⟨-b, Submodule.mem_map.mpr ⟨⟨_, N.sub_mem zN (N.smul_mem b yN)⟩, ?_, ?_⟩⟩ · refine LinearMap.mem_ker.mpr (show ϕ (⟨z, N_le_M zN⟩ - b • ⟨y, N_le_M yN⟩) = 0 from ?_) rw [LinearMap.map_sub, LinearMap.map_smul, hb, ϕy_eq, smul_eq_mul, mul_comm, sub_self] · simp only [sub_eq_add_neg, neg_smul, coe_subtype] -- And extend a basis for `M'` with `y'` intro m' hn'm' bM' refine ⟨Nat.succ_le_succ hn'm', ?_, ?_⟩ · refine Basis.mkFinConsOfLE y' y'M bM' M'_le_M y'_ortho_M' ?_ intro z zM refine ⟨-ϕ ⟨z, zM⟩, ⟨⟨z, zM⟩ - ϕ ⟨z, zM⟩ • ⟨y', y'M⟩, LinearMap.mem_ker.mpr ?_, ?_⟩⟩ · rw [LinearMap.map_sub, LinearMap.map_smul, ϕy'_eq, smul_eq_mul, mul_one, sub_self] · rw [LinearMap.map_sub, LinearMap.map_smul, sub_eq_add_neg, neg_smul] rfl -- It remains to show the extended bases are compatible with each other. intro as h refine ⟨Fin.cons a as, ?_⟩ intro i rw [Basis.coe_mkFinConsOfLE, Basis.coe_mkFinConsOfLE] refine Fin.cases ?_ (fun i ↦ ?_) i · simp only [Fin.cons_zero, Fin.castLE_zero] exact a_smul_y'.symm · rw [Fin.castLE_succ] simp only [Fin.cons_succ, Function.comp_apply, coe_inclusion, map_coe, coe_subtype, h i] /-- A submodule of a free `R`-module of finite rank is also a free `R`-module of finite rank, if `R` is a principal ideal domain. This is a `lemma` to make the induction a bit easier. To actually access the basis, see `Submodule.basisOfPid`. See also the stronger version `Submodule.smithNormalForm`. -/ theorem Submodule.nonempty_basis_of_pid {ι : Type*} [Finite ι] (b : Basis ι R M) (N : Submodule R M) : ∃ n : ℕ, Nonempty (Basis (Fin n) R N) := by haveI := Classical.decEq M cases nonempty_fintype ι induction N using inductionOnRank b with | ih N ih => let b' := (b.reindex (Fintype.equivFin ι)).map (LinearEquiv.ofTop _ rfl).symm by_cases N_bot : N = ⊥ · subst N_bot exact ⟨0, ⟨Basis.empty _⟩⟩ obtain ⟨y, -, a, hay, M', -, N', N'_le_N, -, -, ay_ortho, h'⟩ := Submodule.basis_of_pid_aux ⊤ N b' N_bot le_top obtain ⟨n', ⟨bN'⟩⟩ := ih N' N'_le_N _ hay ay_ortho obtain ⟨bN, _hbN⟩ := h' n' bN' exact ⟨n' + 1, ⟨bN⟩⟩ /-- A submodule of a free `R`-module of finite rank is also a free `R`-module of finite rank, if `R` is a principal ideal domain. See also the stronger version `Submodule.smithNormalForm`. -/ noncomputable def Submodule.basisOfPid {ι : Type*} [Finite ι] (b : Basis ι R M) (N : Submodule R M) : Σn : ℕ, Basis (Fin n) R N := ⟨_, (N.nonempty_basis_of_pid b).choose_spec.some⟩ theorem Submodule.basisOfPid_bot {ι : Type*} [Finite ι] (b : Basis ι R M) : Submodule.basisOfPid b ⊥ = ⟨0, Basis.empty _⟩ := by obtain ⟨n, b'⟩ := Submodule.basisOfPid b ⊥ let e : Fin n ≃ Fin 0 := b'.indexEquiv (Basis.empty _ : Basis (Fin 0) R (⊥ : Submodule R M))
obtain rfl : n = 0 := by simpa using Fintype.card_eq.mpr ⟨e⟩ exact Sigma.eq rfl (Basis.eq_of_apply_eq <| finZeroElim) /-- A submodule inside a free `R`-submodule of finite rank is also a free `R`-module of finite rank, if `R` is a principal ideal domain.
Mathlib/LinearAlgebra/FreeModule/PID.lean
312
317
/- Copyright (c) 2017 Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard, Mario Carneiro -/ import Mathlib.Algebra.Ring.CharZero import Mathlib.Algebra.Star.Basic import Mathlib.Data.Real.Basic import Mathlib.Order.Interval.Set.UnorderedInterval import Mathlib.Tactic.Ring /-! # The complex numbers The complex numbers are modelled as ℝ^2 in the obvious way and it is shown that they form a field of characteristic zero. The result that the complex numbers are algebraically closed, see `FieldTheory.AlgebraicClosure`. -/ assert_not_exists Multiset Algebra open Set Function /-! ### Definition and basic arithmetic -/ /-- Complex numbers consist of two `Real`s: a real part `re` and an imaginary part `im`. -/ structure Complex : Type where /-- The real part of a complex number. -/ re : ℝ /-- The imaginary part of a complex number. -/ im : ℝ @[inherit_doc] notation "ℂ" => Complex namespace Complex open ComplexConjugate noncomputable instance : DecidableEq ℂ := Classical.decEq _ /-- The equivalence between the complex numbers and `ℝ × ℝ`. -/ @[simps apply] def equivRealProd : ℂ ≃ ℝ × ℝ where toFun z := ⟨z.re, z.im⟩ invFun p := ⟨p.1, p.2⟩ left_inv := fun ⟨_, _⟩ => rfl right_inv := fun ⟨_, _⟩ => rfl @[simp] theorem eta : ∀ z : ℂ, Complex.mk z.re z.im = z | ⟨_, _⟩ => rfl -- We only mark this lemma with `ext` *locally* to avoid it applying whenever terms of `ℂ` appear. theorem ext : ∀ {z w : ℂ}, z.re = w.re → z.im = w.im → z = w | ⟨_, _⟩, ⟨_, _⟩, rfl, rfl => rfl attribute [local ext] Complex.ext lemma «forall» {p : ℂ → Prop} : (∀ x, p x) ↔ ∀ a b, p ⟨a, b⟩ := by aesop lemma «exists» {p : ℂ → Prop} : (∃ x, p x) ↔ ∃ a b, p ⟨a, b⟩ := by aesop theorem re_surjective : Surjective re := fun x => ⟨⟨x, 0⟩, rfl⟩ theorem im_surjective : Surjective im := fun y => ⟨⟨0, y⟩, rfl⟩ @[simp] theorem range_re : range re = univ := re_surjective.range_eq @[simp] theorem range_im : range im = univ := im_surjective.range_eq /-- The natural inclusion of the real numbers into the complex numbers. -/ @[coe] def ofReal (r : ℝ) : ℂ := ⟨r, 0⟩ instance : Coe ℝ ℂ := ⟨ofReal⟩ @[simp, norm_cast] theorem ofReal_re (r : ℝ) : Complex.re (r : ℂ) = r := rfl @[simp, norm_cast] theorem ofReal_im (r : ℝ) : (r : ℂ).im = 0 := rfl theorem ofReal_def (r : ℝ) : (r : ℂ) = ⟨r, 0⟩ := rfl @[simp, norm_cast] theorem ofReal_inj {z w : ℝ} : (z : ℂ) = w ↔ z = w := ⟨congrArg re, by apply congrArg⟩ theorem ofReal_injective : Function.Injective ((↑) : ℝ → ℂ) := fun _ _ => congrArg re instance canLift : CanLift ℂ ℝ (↑) fun z => z.im = 0 where prf z hz := ⟨z.re, ext rfl hz.symm⟩ /-- The product of a set on the real axis and a set on the imaginary axis of the complex plane, denoted by `s ×ℂ t`. -/ def reProdIm (s t : Set ℝ) : Set ℂ := re ⁻¹' s ∩ im ⁻¹' t @[deprecated (since := "2024-12-03")] protected alias Set.reProdIm := reProdIm @[inherit_doc] infixl:72 " ×ℂ " => reProdIm theorem mem_reProdIm {z : ℂ} {s t : Set ℝ} : z ∈ s ×ℂ t ↔ z.re ∈ s ∧ z.im ∈ t := Iff.rfl instance : Zero ℂ := ⟨(0 : ℝ)⟩ instance : Inhabited ℂ := ⟨0⟩ @[simp] theorem zero_re : (0 : ℂ).re = 0 := rfl @[simp] theorem zero_im : (0 : ℂ).im = 0 := rfl @[simp, norm_cast] theorem ofReal_zero : ((0 : ℝ) : ℂ) = 0 := rfl @[simp] theorem ofReal_eq_zero {z : ℝ} : (z : ℂ) = 0 ↔ z = 0 := ofReal_inj theorem ofReal_ne_zero {z : ℝ} : (z : ℂ) ≠ 0 ↔ z ≠ 0 := not_congr ofReal_eq_zero instance : One ℂ := ⟨(1 : ℝ)⟩ @[simp] theorem one_re : (1 : ℂ).re = 1 := rfl @[simp] theorem one_im : (1 : ℂ).im = 0 := rfl @[simp, norm_cast] theorem ofReal_one : ((1 : ℝ) : ℂ) = 1 := rfl @[simp] theorem ofReal_eq_one {z : ℝ} : (z : ℂ) = 1 ↔ z = 1 := ofReal_inj theorem ofReal_ne_one {z : ℝ} : (z : ℂ) ≠ 1 ↔ z ≠ 1 := not_congr ofReal_eq_one instance : Add ℂ := ⟨fun z w => ⟨z.re + w.re, z.im + w.im⟩⟩ @[simp] theorem add_re (z w : ℂ) : (z + w).re = z.re + w.re := rfl @[simp] theorem add_im (z w : ℂ) : (z + w).im = z.im + w.im := rfl -- replaced by `re_ofNat` -- replaced by `im_ofNat` @[simp, norm_cast] theorem ofReal_add (r s : ℝ) : ((r + s : ℝ) : ℂ) = r + s := Complex.ext_iff.2 <| by simp [ofReal] -- replaced by `Complex.ofReal_ofNat` instance : Neg ℂ := ⟨fun z => ⟨-z.re, -z.im⟩⟩ @[simp] theorem neg_re (z : ℂ) : (-z).re = -z.re := rfl @[simp] theorem neg_im (z : ℂ) : (-z).im = -z.im := rfl @[simp, norm_cast] theorem ofReal_neg (r : ℝ) : ((-r : ℝ) : ℂ) = -r := Complex.ext_iff.2 <| by simp [ofReal] instance : Sub ℂ := ⟨fun z w => ⟨z.re - w.re, z.im - w.im⟩⟩ instance : Mul ℂ := ⟨fun z w => ⟨z.re * w.re - z.im * w.im, z.re * w.im + z.im * w.re⟩⟩ @[simp] theorem mul_re (z w : ℂ) : (z * w).re = z.re * w.re - z.im * w.im := rfl @[simp] theorem mul_im (z w : ℂ) : (z * w).im = z.re * w.im + z.im * w.re := rfl @[simp, norm_cast] theorem ofReal_mul (r s : ℝ) : ((r * s : ℝ) : ℂ) = r * s := Complex.ext_iff.2 <| by simp [ofReal] theorem re_ofReal_mul (r : ℝ) (z : ℂ) : (r * z).re = r * z.re := by simp [ofReal] theorem im_ofReal_mul (r : ℝ) (z : ℂ) : (r * z).im = r * z.im := by simp [ofReal] lemma re_mul_ofReal (z : ℂ) (r : ℝ) : (z * r).re = z.re * r := by simp [ofReal] lemma im_mul_ofReal (z : ℂ) (r : ℝ) : (z * r).im = z.im * r := by simp [ofReal] theorem ofReal_mul' (r : ℝ) (z : ℂ) : ↑r * z = ⟨r * z.re, r * z.im⟩ := ext (re_ofReal_mul _ _) (im_ofReal_mul _ _) /-! ### The imaginary unit, `I` -/ /-- The imaginary unit. -/ def I : ℂ := ⟨0, 1⟩ @[simp] theorem I_re : I.re = 0 := rfl @[simp] theorem I_im : I.im = 1 := rfl @[simp] theorem I_mul_I : I * I = -1 := Complex.ext_iff.2 <| by simp theorem I_mul (z : ℂ) : I * z = ⟨-z.im, z.re⟩ := Complex.ext_iff.2 <| by simp @[simp] lemma I_ne_zero : (I : ℂ) ≠ 0 := mt (congr_arg im) zero_ne_one.symm theorem mk_eq_add_mul_I (a b : ℝ) : Complex.mk a b = a + b * I := Complex.ext_iff.2 <| by simp [ofReal] @[simp] theorem re_add_im (z : ℂ) : (z.re : ℂ) + z.im * I = z := Complex.ext_iff.2 <| by simp [ofReal] theorem mul_I_re (z : ℂ) : (z * I).re = -z.im := by simp theorem mul_I_im (z : ℂ) : (z * I).im = z.re := by simp theorem I_mul_re (z : ℂ) : (I * z).re = -z.im := by simp theorem I_mul_im (z : ℂ) : (I * z).im = z.re := by simp @[simp] theorem equivRealProd_symm_apply (p : ℝ × ℝ) : equivRealProd.symm p = p.1 + p.2 * I := by ext <;> simp [Complex.equivRealProd, ofReal] /-- The natural `AddEquiv` from `ℂ` to `ℝ × ℝ`. -/ @[simps! +simpRhs apply symm_apply_re symm_apply_im] def equivRealProdAddHom : ℂ ≃+ ℝ × ℝ := { equivRealProd with map_add' := by simp } theorem equivRealProdAddHom_symm_apply (p : ℝ × ℝ) : equivRealProdAddHom.symm p = p.1 + p.2 * I := equivRealProd_symm_apply p /-! ### Commutative ring instance and lemmas -/ /- We use a nonstandard formula for the `ℕ` and `ℤ` actions to make sure there is no diamond from the other actions they inherit through the `ℝ`-action on `ℂ` and action transitivity defined in `Data.Complex.Module`. -/ instance : Nontrivial ℂ := domain_nontrivial re rfl rfl namespace SMul -- The useless `0` multiplication in `smul` is to make sure that -- `RestrictScalars.module ℝ ℂ ℂ = Complex.module` definitionally. -- instance made scoped to avoid situations like instance synthesis -- of `SMul ℂ ℂ` trying to proceed via `SMul ℂ ℝ`. /-- Scalar multiplication by `R` on `ℝ` extends to `ℂ`. This is used here and in `Matlib.Data.Complex.Module` to transfer instances from `ℝ` to `ℂ`, but is not needed outside, so we make it scoped. -/ scoped instance instSMulRealComplex {R : Type*} [SMul R ℝ] : SMul R ℂ where smul r x := ⟨r • x.re - 0 * x.im, r • x.im + 0 * x.re⟩ end SMul open scoped SMul section SMul variable {R : Type*} [SMul R ℝ] theorem smul_re (r : R) (z : ℂ) : (r • z).re = r • z.re := by simp [(· • ·), SMul.smul] theorem smul_im (r : R) (z : ℂ) : (r • z).im = r • z.im := by simp [(· • ·), SMul.smul] @[simp] theorem real_smul {x : ℝ} {z : ℂ} : x • z = x * z := rfl end SMul instance addCommGroup : AddCommGroup ℂ := { zero := (0 : ℂ) add := (· + ·) neg := Neg.neg sub := Sub.sub nsmul := fun n z => n • z zsmul := fun n z => n • z zsmul_zero' := by intros; ext <;> simp [smul_re, smul_im] nsmul_zero := by intros; ext <;> simp [smul_re, smul_im] nsmul_succ := by intros; ext <;> simp [smul_re, smul_im] <;> ring zsmul_succ' := by intros; ext <;> simp [smul_re, smul_im] <;> ring zsmul_neg' := by intros; ext <;> simp [smul_re, smul_im] <;> ring add_assoc := by intros; ext <;> simp <;> ring zero_add := by intros; ext <;> simp add_zero := by intros; ext <;> simp add_comm := by intros; ext <;> simp <;> ring neg_add_cancel := by intros; ext <;> simp } instance addGroupWithOne : AddGroupWithOne ℂ := { Complex.addCommGroup with natCast := fun n => ⟨n, 0⟩ natCast_zero := by ext <;> simp [Nat.cast, AddMonoidWithOne.natCast_zero] natCast_succ := fun _ => by ext <;> simp [Nat.cast, AddMonoidWithOne.natCast_succ] intCast := fun n => ⟨n, 0⟩ intCast_ofNat := fun _ => by ext <;> rfl intCast_negSucc := fun n => by ext · simp [AddGroupWithOne.intCast_negSucc] show -(1 : ℝ) + (-n) = -(↑(n + 1)) simp [Nat.cast_add, add_comm] · simp [AddGroupWithOne.intCast_negSucc] show im ⟨n, 0⟩ = 0 rfl one := 1 } instance commRing : CommRing ℂ := { addGroupWithOne with mul := (· * ·) npow := @npowRec _ ⟨(1 : ℂ)⟩ ⟨(· * ·)⟩ add_comm := by intros; ext <;> simp <;> ring left_distrib := by intros; ext <;> simp [mul_re, mul_im] <;> ring right_distrib := by intros; ext <;> simp [mul_re, mul_im] <;> ring zero_mul := by intros; ext <;> simp mul_zero := by intros; ext <;> simp mul_assoc := by intros; ext <;> simp <;> ring one_mul := by intros; ext <;> simp mul_one := by intros; ext <;> simp mul_comm := by intros; ext <;> simp <;> ring } /-- This shortcut instance ensures we do not find `Ring` via the noncomputable `Complex.field` instance. -/ instance : Ring ℂ := by infer_instance /-- This shortcut instance ensures we do not find `CommSemiring` via the noncomputable `Complex.field` instance. -/ instance : CommSemiring ℂ := inferInstance /-- This shortcut instance ensures we do not find `Semiring` via the noncomputable `Complex.field` instance. -/ instance : Semiring ℂ := inferInstance /-- The "real part" map, considered as an additive group homomorphism. -/ def reAddGroupHom : ℂ →+ ℝ where toFun := re map_zero' := zero_re map_add' := add_re @[simp] theorem coe_reAddGroupHom : (reAddGroupHom : ℂ → ℝ) = re := rfl /-- The "imaginary part" map, considered as an additive group homomorphism. -/ def imAddGroupHom : ℂ →+ ℝ where toFun := im map_zero' := zero_im map_add' := add_im @[simp] theorem coe_imAddGroupHom : (imAddGroupHom : ℂ → ℝ) = im := rfl /-! ### Cast lemmas -/ instance instNNRatCast : NNRatCast ℂ where nnratCast q := ofReal q instance instRatCast : RatCast ℂ where ratCast q := ofReal q @[simp, norm_cast] lemma ofReal_ofNat (n : ℕ) [n.AtLeastTwo] : ofReal ofNat(n) = ofNat(n) := rfl @[simp, norm_cast] lemma ofReal_natCast (n : ℕ) : ofReal n = n := rfl @[simp, norm_cast] lemma ofReal_intCast (n : ℤ) : ofReal n = n := rfl @[simp, norm_cast] lemma ofReal_nnratCast (q : ℚ≥0) : ofReal q = q := rfl @[simp, norm_cast] lemma ofReal_ratCast (q : ℚ) : ofReal q = q := rfl @[simp] lemma re_ofNat (n : ℕ) [n.AtLeastTwo] : (ofNat(n) : ℂ).re = ofNat(n) := rfl @[simp] lemma im_ofNat (n : ℕ) [n.AtLeastTwo] : (ofNat(n) : ℂ).im = 0 := rfl @[simp, norm_cast] lemma natCast_re (n : ℕ) : (n : ℂ).re = n := rfl @[simp, norm_cast] lemma natCast_im (n : ℕ) : (n : ℂ).im = 0 := rfl @[simp, norm_cast] lemma intCast_re (n : ℤ) : (n : ℂ).re = n := rfl @[simp, norm_cast] lemma intCast_im (n : ℤ) : (n : ℂ).im = 0 := rfl @[simp, norm_cast] lemma re_nnratCast (q : ℚ≥0) : (q : ℂ).re = q := rfl @[simp, norm_cast] lemma im_nnratCast (q : ℚ≥0) : (q : ℂ).im = 0 := rfl @[simp, norm_cast] lemma ratCast_re (q : ℚ) : (q : ℂ).re = q := rfl @[simp, norm_cast] lemma ratCast_im (q : ℚ) : (q : ℂ).im = 0 := rfl lemma re_nsmul (n : ℕ) (z : ℂ) : (n • z).re = n • z.re := smul_re .. lemma im_nsmul (n : ℕ) (z : ℂ) : (n • z).im = n • z.im := smul_im .. lemma re_zsmul (n : ℤ) (z : ℂ) : (n • z).re = n • z.re := smul_re .. lemma im_zsmul (n : ℤ) (z : ℂ) : (n • z).im = n • z.im := smul_im .. @[simp] lemma re_nnqsmul (q : ℚ≥0) (z : ℂ) : (q • z).re = q • z.re := smul_re .. @[simp] lemma im_nnqsmul (q : ℚ≥0) (z : ℂ) : (q • z).im = q • z.im := smul_im .. @[simp] lemma re_qsmul (q : ℚ) (z : ℂ) : (q • z).re = q • z.re := smul_re .. @[simp] lemma im_qsmul (q : ℚ) (z : ℂ) : (q • z).im = q • z.im := smul_im .. @[norm_cast] lemma ofReal_nsmul (n : ℕ) (r : ℝ) : ↑(n • r) = n • (r : ℂ) := by simp @[norm_cast] lemma ofReal_zsmul (n : ℤ) (r : ℝ) : ↑(n • r) = n • (r : ℂ) := by simp /-! ### Complex conjugation -/ /-- This defines the complex conjugate as the `star` operation of the `StarRing ℂ`. It is recommended to use the ring endomorphism version `starRingEnd`, available under the notation `conj` in the locale `ComplexConjugate`. -/ instance : StarRing ℂ where star z := ⟨z.re, -z.im⟩ star_involutive x := by simp only [eta, neg_neg] star_mul a b := by ext <;> simp [add_comm] <;> ring star_add a b := by ext <;> simp [add_comm] @[simp] theorem conj_re (z : ℂ) : (conj z).re = z.re := rfl @[simp] theorem conj_im (z : ℂ) : (conj z).im = -z.im := rfl @[simp] theorem conj_ofReal (r : ℝ) : conj (r : ℂ) = r := Complex.ext_iff.2 <| by simp [star] @[simp] theorem conj_I : conj I = -I := Complex.ext_iff.2 <| by simp theorem conj_natCast (n : ℕ) : conj (n : ℂ) = n := map_natCast _ _ theorem conj_ofNat (n : ℕ) [n.AtLeastTwo] : conj (ofNat(n) : ℂ) = ofNat(n) := map_ofNat _ _ theorem conj_neg_I : conj (-I) = I := by simp theorem conj_eq_iff_real {z : ℂ} : conj z = z ↔ ∃ r : ℝ, z = r := ⟨fun h => ⟨z.re, ext rfl <| eq_zero_of_neg_eq (congr_arg im h)⟩, fun ⟨h, e⟩ => by rw [e, conj_ofReal]⟩ theorem conj_eq_iff_re {z : ℂ} : conj z = z ↔ (z.re : ℂ) = z := conj_eq_iff_real.trans ⟨by rintro ⟨r, rfl⟩; simp [ofReal], fun h => ⟨_, h.symm⟩⟩ theorem conj_eq_iff_im {z : ℂ} : conj z = z ↔ z.im = 0 := ⟨fun h => add_self_eq_zero.mp (neg_eq_iff_add_eq_zero.mp (congr_arg im h)), fun h => ext rfl (neg_eq_iff_add_eq_zero.mpr (add_self_eq_zero.mpr h))⟩ @[simp] theorem star_def : (Star.star : ℂ → ℂ) = conj := rfl /-! ### Norm squared -/ /-- The norm squared function. -/ @[pp_nodot] def normSq : ℂ →*₀ ℝ where toFun z := z.re * z.re + z.im * z.im map_zero' := by simp map_one' := by simp map_mul' z w := by dsimp ring theorem normSq_apply (z : ℂ) : normSq z = z.re * z.re + z.im * z.im := rfl @[simp] theorem normSq_ofReal (r : ℝ) : normSq r = r * r := by simp [normSq, ofReal] @[simp] theorem normSq_natCast (n : ℕ) : normSq n = n * n := normSq_ofReal _ @[simp] theorem normSq_intCast (z : ℤ) : normSq z = z * z := normSq_ofReal _ @[simp] theorem normSq_ratCast (q : ℚ) : normSq q = q * q := normSq_ofReal _ @[simp] theorem normSq_ofNat (n : ℕ) [n.AtLeastTwo] : normSq (ofNat(n) : ℂ) = ofNat(n) * ofNat(n) := normSq_natCast _ @[simp] theorem normSq_mk (x y : ℝ) : normSq ⟨x, y⟩ = x * x + y * y := rfl theorem normSq_add_mul_I (x y : ℝ) : normSq (x + y * I) = x ^ 2 + y ^ 2 := by rw [← mk_eq_add_mul_I, normSq_mk, sq, sq] theorem normSq_eq_conj_mul_self {z : ℂ} : (normSq z : ℂ) = conj z * z := by ext <;> simp [normSq, mul_comm, ofReal] theorem normSq_zero : normSq 0 = 0 := by simp theorem normSq_one : normSq 1 = 1 := by simp @[simp] theorem normSq_I : normSq I = 1 := by simp [normSq] theorem normSq_nonneg (z : ℂ) : 0 ≤ normSq z := add_nonneg (mul_self_nonneg _) (mul_self_nonneg _) theorem normSq_eq_zero {z : ℂ} : normSq z = 0 ↔ z = 0 := ⟨fun h => ext (eq_zero_of_mul_self_add_mul_self_eq_zero h) (eq_zero_of_mul_self_add_mul_self_eq_zero <| (add_comm _ _).trans h), fun h => h.symm ▸ normSq_zero⟩ @[simp] theorem normSq_pos {z : ℂ} : 0 < normSq z ↔ z ≠ 0 := (normSq_nonneg z).lt_iff_ne.trans <| not_congr (eq_comm.trans normSq_eq_zero) @[simp] theorem normSq_neg (z : ℂ) : normSq (-z) = normSq z := by simp [normSq] @[simp] theorem normSq_conj (z : ℂ) : normSq (conj z) = normSq z := by simp [normSq] theorem normSq_mul (z w : ℂ) : normSq (z * w) = normSq z * normSq w := normSq.map_mul z w theorem normSq_add (z w : ℂ) : normSq (z + w) = normSq z + normSq w + 2 * (z * conj w).re := by dsimp [normSq]; ring theorem re_sq_le_normSq (z : ℂ) : z.re * z.re ≤ normSq z := le_add_of_nonneg_right (mul_self_nonneg _) theorem im_sq_le_normSq (z : ℂ) : z.im * z.im ≤ normSq z := le_add_of_nonneg_left (mul_self_nonneg _) theorem mul_conj (z : ℂ) : z * conj z = normSq z := Complex.ext_iff.2 <| by simp [normSq, mul_comm, sub_eq_neg_add, add_comm, ofReal] theorem add_conj (z : ℂ) : z + conj z = (2 * z.re : ℝ) := Complex.ext_iff.2 <| by simp [two_mul, ofReal] /-- The coercion `ℝ → ℂ` as a `RingHom`. -/ def ofRealHom : ℝ →+* ℂ where toFun x := (x : ℂ) map_one' := ofReal_one map_zero' := ofReal_zero map_mul' := ofReal_mul map_add' := ofReal_add @[simp] lemma ofRealHom_eq_coe (r : ℝ) : ofRealHom r = r := rfl variable {α : Type*} @[simp] lemma ofReal_comp_add (f g : α → ℝ) : ofReal ∘ (f + g) = ofReal ∘ f + ofReal ∘ g := map_comp_add ofRealHom .. @[simp] lemma ofReal_comp_sub (f g : α → ℝ) : ofReal ∘ (f - g) = ofReal ∘ f - ofReal ∘ g := map_comp_sub ofRealHom .. @[simp] lemma ofReal_comp_neg (f : α → ℝ) : ofReal ∘ (-f) = -(ofReal ∘ f) := map_comp_neg ofRealHom _ lemma ofReal_comp_nsmul (n : ℕ) (f : α → ℝ) : ofReal ∘ (n • f) = n • (ofReal ∘ f) := map_comp_nsmul ofRealHom .. lemma ofReal_comp_zsmul (n : ℤ) (f : α → ℝ) : ofReal ∘ (n • f) = n • (ofReal ∘ f) := map_comp_zsmul ofRealHom .. @[simp] lemma ofReal_comp_mul (f g : α → ℝ) : ofReal ∘ (f * g) = ofReal ∘ f * ofReal ∘ g := map_comp_mul ofRealHom .. @[simp] lemma ofReal_comp_pow (f : α → ℝ) (n : ℕ) : ofReal ∘ (f ^ n) = (ofReal ∘ f) ^ n := map_comp_pow ofRealHom .. @[simp] theorem I_sq : I ^ 2 = -1 := by rw [sq, I_mul_I] @[simp] lemma I_pow_three : I ^ 3 = -I := by rw [pow_succ, I_sq, neg_one_mul] @[simp] theorem I_pow_four : I ^ 4 = 1 := by rw [(by norm_num : 4 = 2 * 2), pow_mul, I_sq, neg_one_sq] lemma I_pow_eq_pow_mod (n : ℕ) : I ^ n = I ^ (n % 4) := by conv_lhs => rw [← Nat.div_add_mod n 4] simp [pow_add, pow_mul, I_pow_four] @[simp] theorem sub_re (z w : ℂ) : (z - w).re = z.re - w.re := rfl @[simp] theorem sub_im (z w : ℂ) : (z - w).im = z.im - w.im := rfl @[simp, norm_cast] theorem ofReal_sub (r s : ℝ) : ((r - s : ℝ) : ℂ) = r - s := Complex.ext_iff.2 <| by simp [ofReal] @[simp, norm_cast] theorem ofReal_pow (r : ℝ) (n : ℕ) : ((r ^ n : ℝ) : ℂ) = (r : ℂ) ^ n := by induction n <;> simp [*, ofReal_mul, pow_succ] theorem sub_conj (z : ℂ) : z - conj z = (2 * z.im : ℝ) * I := Complex.ext_iff.2 <| by simp [two_mul, sub_eq_add_neg, ofReal] theorem normSq_sub (z w : ℂ) : normSq (z - w) = normSq z + normSq w - 2 * (z * conj w).re := by rw [sub_eq_add_neg, normSq_add] simp only [RingHom.map_neg, mul_neg, neg_re, normSq_neg] ring /-! ### Inversion -/ noncomputable instance : Inv ℂ := ⟨fun z => conj z * ((normSq z)⁻¹ : ℝ)⟩ theorem inv_def (z : ℂ) : z⁻¹ = conj z * ((normSq z)⁻¹ : ℝ) := rfl @[simp] theorem inv_re (z : ℂ) : z⁻¹.re = z.re / normSq z := by simp [inv_def, division_def, ofReal] @[simp] theorem inv_im (z : ℂ) : z⁻¹.im = -z.im / normSq z := by simp [inv_def, division_def, ofReal] @[simp, norm_cast] theorem ofReal_inv (r : ℝ) : ((r⁻¹ : ℝ) : ℂ) = (r : ℂ)⁻¹ := Complex.ext_iff.2 <| by simp [ofReal] protected theorem inv_zero : (0⁻¹ : ℂ) = 0 := by rw [← ofReal_zero, ← ofReal_inv, inv_zero] protected theorem mul_inv_cancel {z : ℂ} (h : z ≠ 0) : z * z⁻¹ = 1 := by rw [inv_def, ← mul_assoc, mul_conj, ← ofReal_mul, mul_inv_cancel₀ (mt normSq_eq_zero.1 h), ofReal_one] noncomputable instance instDivInvMonoid : DivInvMonoid ℂ where lemma div_re (z w : ℂ) : (z / w).re = z.re * w.re / normSq w + z.im * w.im / normSq w := by simp [div_eq_mul_inv, mul_assoc, sub_eq_add_neg] lemma div_im (z w : ℂ) : (z / w).im = z.im * w.re / normSq w - z.re * w.im / normSq w := by simp [div_eq_mul_inv, mul_assoc, sub_eq_add_neg, add_comm] /-! ### Field instance and lemmas -/ noncomputable instance instField : Field ℂ where mul_inv_cancel := @Complex.mul_inv_cancel inv_zero := Complex.inv_zero nnqsmul := (· • ·) qsmul := (· • ·) nnratCast_def q := by ext <;> simp [NNRat.cast_def, div_re, div_im, mul_div_mul_comm] ratCast_def q := by ext <;> simp [Rat.cast_def, div_re, div_im, mul_div_mul_comm] nnqsmul_def n z := Complex.ext_iff.2 <| by simp [NNRat.smul_def, smul_re, smul_im] qsmul_def n z := Complex.ext_iff.2 <| by simp [Rat.smul_def, smul_re, smul_im] @[simp, norm_cast] lemma ofReal_nnqsmul (q : ℚ≥0) (r : ℝ) : ofReal (q • r) = q • r := by simp [NNRat.smul_def] @[simp, norm_cast] lemma ofReal_qsmul (q : ℚ) (r : ℝ) : ofReal (q • r) = q • r := by simp [Rat.smul_def] theorem conj_inv (x : ℂ) : conj x⁻¹ = (conj x)⁻¹ := star_inv₀ _ @[simp, norm_cast] theorem ofReal_div (r s : ℝ) : ((r / s : ℝ) : ℂ) = r / s := map_div₀ ofRealHom r s @[simp, norm_cast] theorem ofReal_zpow (r : ℝ) (n : ℤ) : ((r ^ n : ℝ) : ℂ) = (r : ℂ) ^ n := map_zpow₀ ofRealHom r n @[simp] theorem div_I (z : ℂ) : z / I = -(z * I) := (div_eq_iff_mul_eq I_ne_zero).2 <| by simp [mul_assoc] @[simp] theorem inv_I : I⁻¹ = -I := by rw [inv_eq_one_div, div_I, one_mul] theorem normSq_inv (z : ℂ) : normSq z⁻¹ = (normSq z)⁻¹ := by simp theorem normSq_div (z w : ℂ) : normSq (z / w) = normSq z / normSq w := by simp lemma div_ofReal (z : ℂ) (x : ℝ) : z / x = ⟨z.re / x, z.im / x⟩ := by simp_rw [div_eq_inv_mul, ← ofReal_inv, ofReal_mul'] lemma div_natCast (z : ℂ) (n : ℕ) : z / n = ⟨z.re / n, z.im / n⟩ := mod_cast div_ofReal z n lemma div_intCast (z : ℂ) (n : ℤ) : z / n = ⟨z.re / n, z.im / n⟩ := mod_cast div_ofReal z n lemma div_ratCast (z : ℂ) (x : ℚ) : z / x = ⟨z.re / x, z.im / x⟩ := mod_cast div_ofReal z x
lemma div_ofNat (z : ℂ) (n : ℕ) [n.AtLeastTwo] :
Mathlib/Data/Complex/Basic.lean
729
729
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import Mathlib.LinearAlgebra.CliffordAlgebra.Basic import Mathlib.RingTheory.GradedAlgebra.Basic /-! # Results about the grading structure of the clifford algebra The main result is `CliffordAlgebra.gradedAlgebra`, which says that the clifford algebra is a ℤ₂-graded algebra (or "superalgebra"). -/ namespace CliffordAlgebra variable {R M : Type*} [CommRing R] [AddCommGroup M] [Module R M] variable {Q : QuadraticForm R M} open scoped DirectSum variable (Q) /-- The even or odd submodule, defined as the supremum of the even or odd powers of `(ι Q).range`. `evenOdd 0` is the even submodule, and `evenOdd 1` is the odd submodule. -/ def evenOdd (i : ZMod 2) : Submodule R (CliffordAlgebra Q) := ⨆ j : { n : ℕ // ↑n = i }, LinearMap.range (ι Q) ^ (j : ℕ) theorem one_le_evenOdd_zero : 1 ≤ evenOdd Q 0 := by refine le_trans ?_ (le_iSup _ ⟨0, Nat.cast_zero⟩) exact (pow_zero _).ge theorem range_ι_le_evenOdd_one : LinearMap.range (ι Q) ≤ evenOdd Q 1 := by refine le_trans ?_ (le_iSup _ ⟨1, Nat.cast_one⟩) exact (pow_one _).ge theorem ι_mem_evenOdd_one (m : M) : ι Q m ∈ evenOdd Q 1 := range_ι_le_evenOdd_one Q <| LinearMap.mem_range_self _ m theorem ι_mul_ι_mem_evenOdd_zero (m₁ m₂ : M) : ι Q m₁ * ι Q m₂ ∈ evenOdd Q 0 := Submodule.mem_iSup_of_mem ⟨2, rfl⟩ (by rw [Subtype.coe_mk, pow_two] exact Submodule.mul_mem_mul (LinearMap.mem_range_self (ι Q) m₁) (LinearMap.mem_range_self (ι Q) m₂)) theorem evenOdd_mul_le (i j : ZMod 2) : evenOdd Q i * evenOdd Q j ≤ evenOdd Q (i + j) := by simp_rw [evenOdd, Submodule.iSup_eq_span, Submodule.span_mul_span] apply Submodule.span_mono simp_rw [Set.iUnion_mul, Set.mul_iUnion, Set.iUnion_subset_iff, Set.mul_subset_iff] rintro ⟨xi, rfl⟩ ⟨yi, rfl⟩ x hx y hy refine Set.mem_iUnion.mpr ⟨⟨xi + yi, Nat.cast_add _ _⟩, ?_⟩ simp only [Subtype.coe_mk, Nat.cast_add, pow_add] exact Submodule.mul_mem_mul hx hy instance evenOdd.gradedMonoid : SetLike.GradedMonoid (evenOdd Q) where one_mem := Submodule.one_le.mp (one_le_evenOdd_zero Q) mul_mem _i _j _p _q hp hq := Submodule.mul_le.mp (evenOdd_mul_le Q _ _) _ hp _ hq /-- A version of `CliffordAlgebra.ι` that maps directly into the graded structure. This is primarily an auxiliary construction used to provide `CliffordAlgebra.gradedAlgebra`. -/ protected def GradedAlgebra.ι : M →ₗ[R] ⨁ i : ZMod 2, evenOdd Q i := DirectSum.lof R (ZMod 2) (fun i => ↥(evenOdd Q i)) 1 ∘ₗ (ι Q).codRestrict _ (ι_mem_evenOdd_one Q) theorem GradedAlgebra.ι_apply (m : M) : GradedAlgebra.ι Q m = DirectSum.of (fun i => ↥(evenOdd Q i)) 1 ⟨ι Q m, ι_mem_evenOdd_one Q m⟩ := rfl nonrec theorem GradedAlgebra.ι_sq_scalar (m : M) : GradedAlgebra.ι Q m * GradedAlgebra.ι Q m = algebraMap R _ (Q m) := by rw [GradedAlgebra.ι_apply Q, DirectSum.of_mul_of, DirectSum.algebraMap_apply] exact DirectSum.of_eq_of_gradedMonoid_eq (Sigma.subtype_ext rfl <| ι_sq_scalar _ _) theorem GradedAlgebra.lift_ι_eq (i' : ZMod 2) (x' : evenOdd Q i') : lift Q ⟨GradedAlgebra.ι Q, GradedAlgebra.ι_sq_scalar Q⟩ x' = DirectSum.of (fun i => evenOdd Q i) i' x' := by obtain ⟨x', hx'⟩ := x' dsimp only [Subtype.coe_mk, DirectSum.lof_eq_of] induction hx' using Submodule.iSup_induction' with | mem i x hx => obtain ⟨i, rfl⟩ := i dsimp only [Subtype.coe_mk] at hx induction hx using Submodule.pow_induction_on_left' with | algebraMap r => rw [AlgHom.commutes, DirectSum.algebraMap_apply]; rfl | add x y i hx hy ihx ihy => rw [map_add, ihx, ihy, ← AddMonoidHom.map_add] rfl | mem_mul m hm i x hx ih => obtain ⟨_, rfl⟩ := hm rw [map_mul, ih, lift_ι_apply, GradedAlgebra.ι_apply Q, DirectSum.of_mul_of] refine DirectSum.of_eq_of_gradedMonoid_eq (Sigma.subtype_ext ?_ ?_) <;> dsimp only [GradedMonoid.mk, Subtype.coe_mk] · rw [Nat.succ_eq_add_one, add_comm, Nat.cast_add, Nat.cast_one] rfl | zero => rw [map_zero] apply Eq.symm apply DFinsupp.single_eq_zero.mpr; rfl | add x y hx hy ihx ihy => rw [map_add, ihx, ihy, ← AddMonoidHom.map_add]; rfl /-- The clifford algebra is graded by the even and odd parts. -/ instance gradedAlgebra : GradedAlgebra (evenOdd Q) := GradedAlgebra.ofAlgHom (evenOdd Q) -- while not necessary, the `by apply` makes this elaborate faster (lift Q ⟨by apply GradedAlgebra.ι Q, by apply GradedAlgebra.ι_sq_scalar Q⟩) -- the proof from here onward is mostly similar to the `TensorAlgebra` case, with some extra -- handling for the `iSup` in `evenOdd`. (by ext m dsimp only [LinearMap.comp_apply, AlgHom.toLinearMap_apply, AlgHom.comp_apply, AlgHom.id_apply] rw [lift_ι_apply, GradedAlgebra.ι_apply Q, DirectSum.coeAlgHom_of, Subtype.coe_mk]) (by apply GradedAlgebra.lift_ι_eq Q) theorem iSup_ι_range_eq_top : ⨆ i : ℕ, LinearMap.range (ι Q) ^ i = ⊤ := by rw [← (DirectSum.Decomposition.isInternal (evenOdd Q)).submodule_iSup_eq_top, eq_comm] calc -- Porting note: needs extra annotations, no longer unifies against the goal in the face of -- ambiguity ⨆ (i : ZMod 2) (j : { n : ℕ // ↑n = i }), LinearMap.range (ι Q) ^ (j : ℕ) = ⨆ i : Σ i : ZMod 2, { n : ℕ // ↑n = i }, LinearMap.range (ι Q) ^ (i.2 : ℕ) := by rw [iSup_sigma] _ = ⨆ i : ℕ, LinearMap.range (ι Q) ^ i := Function.Surjective.iSup_congr (fun i => i.2) (fun i => ⟨⟨_, i, rfl⟩, rfl⟩) fun _ => rfl theorem evenOdd_isCompl : IsCompl (evenOdd Q 0) (evenOdd Q 1) := (DirectSum.Decomposition.isInternal (evenOdd Q)).isCompl zero_ne_one <| by have : (Finset.univ : Finset (ZMod 2)) = {0, 1} := rfl simpa using congr_arg ((↑) : Finset (ZMod 2) → Set (ZMod 2)) this /-- To show a property is true on the even or odd part, it suffices to show it is true on the scalars or vectors (respectively), closed under addition, and under left-multiplication by a pair of vectors. -/ @[elab_as_elim] theorem evenOdd_induction (n : ZMod 2) {motive : ∀ x, x ∈ evenOdd Q n → Prop} (range_ι_pow : ∀ (v) (h : v ∈ LinearMap.range (ι Q) ^ n.val), motive v (Submodule.mem_iSup_of_mem ⟨n.val, n.natCast_zmod_val⟩ h)) (add : ∀ x y hx hy, motive x hx → motive y hy → motive (x + y) (Submodule.add_mem _ hx hy)) (ι_mul_ι_mul : ∀ m₁ m₂ x hx, motive x hx → motive (ι Q m₁ * ι Q m₂ * x) (zero_add n ▸ SetLike.mul_mem_graded (ι_mul_ι_mem_evenOdd_zero Q m₁ m₂) hx)) (x : CliffordAlgebra Q) (hx : x ∈ evenOdd Q n) : motive x hx := by apply Submodule.iSup_induction' (motive := motive) _ _ (range_ι_pow 0 (Submodule.zero_mem _)) add refine Subtype.rec ?_ simp_rw [ZMod.natCast_eq_iff, add_comm n.val] rintro n' ⟨k, rfl⟩ xv simp_rw [pow_add, pow_mul] intro hxv induction hxv using Submodule.mul_induction_on' with | mem_mul_mem a ha b hb => induction ha using Submodule.pow_induction_on_left' with | algebraMap r => simp_rw [← Algebra.smul_def] exact range_ι_pow _ (Submodule.smul_mem _ _ hb)
| add x y n hx hy ihx ihy => simp_rw [add_mul] apply add _ _ _ _ ihx ihy | mem_mul x hx n'' y hy ihy => revert hx simp_rw [pow_two] intro hx2 induction hx2 using Submodule.mul_induction_on' with | mem_mul_mem m hm n hn => simp_rw [LinearMap.mem_range] at hm hn obtain ⟨m₁, rfl⟩ := hm; obtain ⟨m₂, rfl⟩ := hn simp_rw [mul_assoc _ y b] exact ι_mul_ι_mul _ _ _ _ ihy | add x hx y hy ihx ihy => simp_rw [add_mul] apply add _ _ _ _ ihx ihy | add x y hx hy ihx ihy => apply add _ _ _ _ ihx ihy /-- To show a property is true on the even parts, it suffices to show it is true on the scalars, closed under addition, and under left-multiplication by a pair of vectors. -/ @[elab_as_elim] theorem even_induction {motive : ∀ x, x ∈ evenOdd Q 0 → Prop} (algebraMap : ∀ r : R, motive (algebraMap _ _ r) (SetLike.algebraMap_mem_graded _ _)) (add : ∀ x y hx hy, motive x hx → motive y hy → motive (x + y) (Submodule.add_mem _ hx hy)) (ι_mul_ι_mul : ∀ m₁ m₂ x hx, motive x hx → motive (ι Q m₁ * ι Q m₂ * x) (zero_add (0 : ZMod 2) ▸ SetLike.mul_mem_graded (ι_mul_ι_mem_evenOdd_zero Q m₁ m₂) hx)) (x : CliffordAlgebra Q) (hx : x ∈ evenOdd Q 0) : motive x hx := by refine evenOdd_induction _ _ (motive := motive) (fun rx h => ?_) add ι_mul_ι_mul x hx obtain ⟨r, rfl⟩ := Submodule.mem_one.mp h exact algebraMap r /-- To show a property is true on the odd parts, it suffices to show it is true on the vectors, closed under addition, and under left-multiplication by a pair of vectors. -/ @[elab_as_elim] theorem odd_induction {P : ∀ x, x ∈ evenOdd Q 1 → Prop} (ι : ∀ v, P (ι Q v) (ι_mem_evenOdd_one _ _))
Mathlib/LinearAlgebra/CliffordAlgebra/Grading.lean
162
201
/- Copyright (c) 2020 Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard -/ import Mathlib.RingTheory.AdicCompletion.Basic import Mathlib.RingTheory.LocalRing.MaximalIdeal.Basic import Mathlib.RingTheory.LocalRing.RingHom.Basic import Mathlib.RingTheory.UniqueFactorizationDomain.Basic import Mathlib.RingTheory.Valuation.PrimeMultiplicity import Mathlib.RingTheory.Valuation.ValuationRing /-! # Discrete valuation rings This file defines discrete valuation rings (DVRs) and develops a basic interface for them. ## Important definitions There are various definitions of a DVR in the literature; we define a DVR to be a local PID which is not a field (the first definition in Wikipedia) and prove that this is equivalent to being a PID with a unique non-zero prime ideal (the definition in Serre's book "Local Fields"). Let R be an integral domain, assumed to be a principal ideal ring and a local ring. * `IsDiscreteValuationRing R` : a predicate expressing that R is a DVR. ### Definitions * `addVal R : AddValuation R PartENat` : the additive valuation on a DVR. ## Implementation notes It's a theorem that an element of a DVR is a uniformizer if and only if it's irreducible. We do not hence define `Uniformizer` at all, because we can use `Irreducible` instead. ## Tags discrete valuation ring -/ universe u open Ideal IsLocalRing /-- An integral domain is a *discrete valuation ring* (DVR) if it's a local PID which is not a field. -/ class IsDiscreteValuationRing (R : Type u) [CommRing R] [IsDomain R] : Prop extends IsPrincipalIdealRing R, IsLocalRing R where not_a_field' : maximalIdeal R ≠ ⊥ namespace IsDiscreteValuationRing variable (R : Type u) [CommRing R] [IsDomain R] [IsDiscreteValuationRing R] theorem not_a_field : maximalIdeal R ≠ ⊥ := not_a_field' /-- A discrete valuation ring `R` is not a field. -/ theorem not_isField : ¬IsField R := IsLocalRing.isField_iff_maximalIdeal_eq.not.mpr (not_a_field R) variable {R} open PrincipalIdealRing theorem irreducible_of_span_eq_maximalIdeal {R : Type*} [CommSemiring R] [IsLocalRing R] [IsDomain R] (ϖ : R) (hϖ : ϖ ≠ 0) (h : maximalIdeal R = Ideal.span {ϖ}) : Irreducible ϖ := by have h2 : ¬IsUnit ϖ := show ϖ ∈ maximalIdeal R from h.symm ▸ Submodule.mem_span_singleton_self ϖ refine ⟨h2, ?_⟩ intro a b hab by_contra! h obtain ⟨ha : a ∈ maximalIdeal R, hb : b ∈ maximalIdeal R⟩ := h rw [h, mem_span_singleton'] at ha hb rcases ha with ⟨a, rfl⟩ rcases hb with ⟨b, rfl⟩ rw [show a * ϖ * (b * ϖ) = ϖ * (ϖ * (a * b)) by ring] at hab apply hϖ apply eq_zero_of_mul_eq_self_right _ hab.symm exact fun hh => h2 (isUnit_of_dvd_one ⟨_, hh.symm⟩) /-- An element of a DVR is irreducible iff it is a uniformizer, that is, generates the maximal ideal of `R`. -/ theorem irreducible_iff_uniformizer (ϖ : R) : Irreducible ϖ ↔ maximalIdeal R = Ideal.span {ϖ} := ⟨fun hϖ => (eq_maximalIdeal (isMaximal_of_irreducible hϖ)).symm, fun h => irreducible_of_span_eq_maximalIdeal ϖ (fun e => not_a_field R <| by rwa [h, span_singleton_eq_bot]) h⟩ theorem _root_.Irreducible.maximalIdeal_eq {ϖ : R} (h : Irreducible ϖ) : maximalIdeal R = Ideal.span {ϖ} := (irreducible_iff_uniformizer _).mp h variable (R) /-- Uniformizers exist in a DVR. -/ theorem exists_irreducible : ∃ ϖ : R, Irreducible ϖ := by simp_rw [irreducible_iff_uniformizer] exact (IsPrincipalIdealRing.principal <| maximalIdeal R).principal /-- Uniformizers exist in a DVR. -/ theorem exists_prime : ∃ ϖ : R, Prime ϖ := (exists_irreducible R).imp fun _ => irreducible_iff_prime.1 /-- An integral domain is a DVR iff it's a PID with a unique non-zero prime ideal. -/ theorem iff_pid_with_one_nonzero_prime (R : Type u) [CommRing R] [IsDomain R] : IsDiscreteValuationRing R ↔ IsPrincipalIdealRing R ∧ ∃! P : Ideal R, P ≠ ⊥ ∧ IsPrime P := by constructor · intro RDVR rcases id RDVR with ⟨Rlocal⟩ constructor · assumption use IsLocalRing.maximalIdeal R constructor · exact ⟨Rlocal, inferInstance⟩ · rintro Q ⟨hQ1, hQ2⟩ obtain ⟨q, rfl⟩ := (IsPrincipalIdealRing.principal Q).1 have hq : q ≠ 0 := by rintro rfl apply hQ1 simp rw [submodule_span_eq, span_singleton_prime hq] at hQ2 replace hQ2 := hQ2.irreducible rw [irreducible_iff_uniformizer] at hQ2 exact hQ2.symm · rintro ⟨RPID, Punique⟩ haveI : IsLocalRing R := IsLocalRing.of_unique_nonzero_prime Punique refine { not_a_field' := ?_ } rcases Punique with ⟨P, ⟨hP1, hP2⟩, _⟩ have hPM : P ≤ maximalIdeal R := le_maximalIdeal hP2.1 intro h rw [h, le_bot_iff] at hPM exact hP1 hPM theorem associated_of_irreducible {a b : R} (ha : Irreducible a) (hb : Irreducible b) : Associated a b := by rw [irreducible_iff_uniformizer] at ha hb rw [← span_singleton_eq_span_singleton, ← ha, hb] variable (R : Type*) /-- Alternative characterisation of discrete valuation rings. -/ def HasUnitMulPowIrreducibleFactorization [CommRing R] : Prop := ∃ p : R, Irreducible p ∧ ∀ {x : R}, x ≠ 0 → ∃ n : ℕ, Associated (p ^ n) x namespace HasUnitMulPowIrreducibleFactorization variable {R} [CommRing R] theorem unique_irreducible (hR : HasUnitMulPowIrreducibleFactorization R) ⦃p q : R⦄ (hp : Irreducible p) (hq : Irreducible q) : Associated p q := by rcases hR with ⟨ϖ, hϖ, hR⟩ suffices ∀ {p : R} (_ : Irreducible p), Associated p ϖ by apply Associated.trans (this hp) (this hq).symm clear hp hq p q intro p hp obtain ⟨n, hn⟩ := hR hp.ne_zero have : Irreducible (ϖ ^ n) := hn.symm.irreducible hp rcases lt_trichotomy n 1 with (H | rfl | H) · obtain rfl : n = 0 := by clear hn this revert H n decide simp [not_irreducible_one, pow_zero] at this · simpa only [pow_one] using hn.symm · obtain ⟨n, rfl⟩ : ∃ k, n = 1 + k + 1 := Nat.exists_eq_add_of_lt H rw [pow_succ'] at this rcases this.isUnit_or_isUnit rfl with (H0 | H0) · exact (hϖ.not_isUnit H0).elim · rw [add_comm, pow_succ'] at H0 exact (hϖ.not_isUnit (isUnit_of_mul_isUnit_left H0)).elim variable [IsDomain R] /-- An integral domain in which there is an irreducible element `p` such that every nonzero element is associated to a power of `p` is a unique factorization domain. See `IsDiscreteValuationRing.ofHasUnitMulPowIrreducibleFactorization`. -/ theorem toUniqueFactorizationMonoid (hR : HasUnitMulPowIrreducibleFactorization R) : UniqueFactorizationMonoid R := let p := Classical.choose hR let spec := Classical.choose_spec hR UniqueFactorizationMonoid.of_exists_prime_factors fun x hx => by use Multiset.replicate (Classical.choose (spec.2 hx)) p constructor · intro q hq have hpq := Multiset.eq_of_mem_replicate hq rw [hpq] refine ⟨spec.1.ne_zero, spec.1.not_isUnit, ?_⟩ intro a b h by_cases ha : a = 0 · rw [ha] simp only [true_or, dvd_zero] obtain ⟨m, u, rfl⟩ := spec.2 ha rw [mul_assoc, mul_left_comm, Units.dvd_mul_left] at h rw [Units.dvd_mul_right] by_cases hm : m = 0 · simp only [hm, one_mul, pow_zero] at h ⊢ right exact h left obtain ⟨m, rfl⟩ := Nat.exists_eq_succ_of_ne_zero hm rw [pow_succ'] apply dvd_mul_of_dvd_left dvd_rfl _ · rw [Multiset.prod_replicate] exact Classical.choose_spec (spec.2 hx) theorem of_ufd_of_unique_irreducible [UniqueFactorizationMonoid R] (h₁ : ∃ p : R, Irreducible p) (h₂ : ∀ ⦃p q : R⦄, Irreducible p → Irreducible q → Associated p q) : HasUnitMulPowIrreducibleFactorization R := by obtain ⟨p, hp⟩ := h₁ refine ⟨p, hp, ?_⟩ intro x hx obtain ⟨fx, hfx⟩ := WfDvdMonoid.exists_factors x hx refine ⟨Multiset.card fx, ?_⟩ have H := hfx.2 rw [← Associates.mk_eq_mk_iff_associated] at H ⊢ rw [← H, ← Associates.prod_mk, Associates.mk_pow, ← Multiset.prod_replicate] congr 1 symm rw [Multiset.eq_replicate] simp only [true_and, and_imp, Multiset.card_map, eq_self_iff_true, Multiset.mem_map, exists_imp] rintro _ q hq rfl rw [Associates.mk_eq_mk_iff_associated] apply h₂ (hfx.1 _ hq) hp end HasUnitMulPowIrreducibleFactorization theorem aux_pid_of_ufd_of_unique_irreducible (R : Type u) [CommRing R] [IsDomain R] [UniqueFactorizationMonoid R] (h₁ : ∃ p : R, Irreducible p) (h₂ : ∀ ⦃p q : R⦄, Irreducible p → Irreducible q → Associated p q) : IsPrincipalIdealRing R := by classical constructor intro I by_cases I0 : I = ⊥ · rw [I0] use 0 simp only [Set.singleton_zero, Submodule.span_zero] obtain ⟨x, hxI, hx0⟩ : ∃ x ∈ I, x ≠ (0 : R) := I.ne_bot_iff.mp I0 obtain ⟨p, _, H⟩ := HasUnitMulPowIrreducibleFactorization.of_ufd_of_unique_irreducible h₁ h₂ have ex : ∃ n : ℕ, p ^ n ∈ I := by obtain ⟨n, u, rfl⟩ := H hx0 refine ⟨n, ?_⟩ simpa only [Units.mul_inv_cancel_right] using I.mul_mem_right (↑u⁻¹) hxI constructor use p ^ Nat.find ex show I = Ideal.span _ apply le_antisymm · intro r hr by_cases hr0 : r = 0 · simp only [hr0, Submodule.zero_mem] obtain ⟨n, u, rfl⟩ := H hr0 simp only [mem_span_singleton, Units.isUnit, IsUnit.dvd_mul_right] apply pow_dvd_pow apply Nat.find_min' simpa only [Units.mul_inv_cancel_right] using I.mul_mem_right (↑u⁻¹) hr · rw [span_singleton_le_iff_mem] exact Nat.find_spec ex /-- A unique factorization domain with at least one irreducible element in which all irreducible elements are associated is a discrete valuation ring. -/ theorem of_ufd_of_unique_irreducible {R : Type u} [CommRing R] [IsDomain R] [UniqueFactorizationMonoid R] (h₁ : ∃ p : R, Irreducible p) (h₂ : ∀ ⦃p q : R⦄, Irreducible p → Irreducible q → Associated p q) : IsDiscreteValuationRing R := by rw [iff_pid_with_one_nonzero_prime] haveI PID : IsPrincipalIdealRing R := aux_pid_of_ufd_of_unique_irreducible R h₁ h₂ obtain ⟨p, hp⟩ := h₁ refine ⟨PID, ⟨Ideal.span {p}, ⟨?_, ?_⟩, ?_⟩⟩ · rw [Submodule.ne_bot_iff] exact ⟨p, Ideal.mem_span_singleton.mpr (dvd_refl p), hp.ne_zero⟩ · rwa [Ideal.span_singleton_prime hp.ne_zero, ← UniqueFactorizationMonoid.irreducible_iff_prime] · intro I rw [← Submodule.IsPrincipal.span_singleton_generator I] rintro ⟨I0, hI⟩ apply span_singleton_eq_span_singleton.mpr apply h₂ _ hp rw [Ne, Submodule.span_singleton_eq_bot] at I0 rwa [UniqueFactorizationMonoid.irreducible_iff_prime, ← Ideal.span_singleton_prime I0] /-- An integral domain in which there is an irreducible element `p` such that every nonzero element is associated to a power of `p` is a discrete valuation ring. -/ theorem ofHasUnitMulPowIrreducibleFactorization {R : Type u} [CommRing R] [IsDomain R] (hR : HasUnitMulPowIrreducibleFactorization R) : IsDiscreteValuationRing R := by letI : UniqueFactorizationMonoid R := hR.toUniqueFactorizationMonoid apply of_ufd_of_unique_irreducible _ hR.unique_irreducible obtain ⟨p, hp, H⟩ := hR exact ⟨p, hp⟩ /- If a ring is equivalent to a DVR, it is itself a DVR. -/ theorem RingEquivClass.isDiscreteValuationRing {A B E : Type*} [CommRing A] [IsDomain A] [CommRing B] [IsDomain B] [IsDiscreteValuationRing A] [EquivLike E A B] [RingEquivClass E A B] (e : E) : IsDiscreteValuationRing B where principal := (isPrincipalIdealRing_iff _).1 <| IsPrincipalIdealRing.of_surjective _ (e : A ≃+* B).surjective __ : IsLocalRing B := (e : A ≃+* B).isLocalRing not_a_field' := by obtain ⟨a, ha⟩ := Submodule.nonzero_mem_of_bot_lt (bot_lt_iff_ne_bot.mpr <| IsDiscreteValuationRing.not_a_field A) rw [Submodule.ne_bot_iff] refine ⟨e a, ⟨?_, by simp only [ne_eq, EmbeddingLike.map_eq_zero_iff, ZeroMemClass.coe_eq_zero, ha, not_false_eq_true]⟩⟩ rw [IsLocalRing.mem_maximalIdeal, map_mem_nonunits_iff e, ← IsLocalRing.mem_maximalIdeal] exact a.2 section variable [CommRing R] [IsDomain R] [IsDiscreteValuationRing R] variable {R} theorem associated_pow_irreducible {x : R} (hx : x ≠ 0) {ϖ : R} (hirr : Irreducible ϖ) : ∃ n : ℕ, Associated x (ϖ ^ n) := by have : WfDvdMonoid R := IsNoetherianRing.wfDvdMonoid obtain ⟨fx, hfx⟩ := WfDvdMonoid.exists_factors x hx use Multiset.card fx have H := hfx.2 rw [← Associates.mk_eq_mk_iff_associated] at H ⊢ rw [← H, ← Associates.prod_mk, Associates.mk_pow, ← Multiset.prod_replicate] congr 1 rw [Multiset.eq_replicate] simp only [true_and, and_imp, Multiset.card_map, eq_self_iff_true, Multiset.mem_map, exists_imp] rintro _ _ _ rfl rw [Associates.mk_eq_mk_iff_associated] refine associated_of_irreducible _ ?_ hirr apply hfx.1 assumption theorem eq_unit_mul_pow_irreducible {x : R} (hx : x ≠ 0) {ϖ : R} (hirr : Irreducible ϖ) : ∃ (n : ℕ) (u : Rˣ), x = u * ϖ ^ n := by obtain ⟨n, hn⟩ := associated_pow_irreducible hx hirr obtain ⟨u, rfl⟩ := hn.symm use n, u apply mul_comm open Submodule.IsPrincipal theorem ideal_eq_span_pow_irreducible {s : Ideal R} (hs : s ≠ ⊥) {ϖ : R} (hirr : Irreducible ϖ) : ∃ n : ℕ, s = Ideal.span {ϖ ^ n} := by have gen_ne_zero : generator s ≠ 0 := by rw [Ne, ← eq_bot_iff_generator_eq_zero] assumption rcases associated_pow_irreducible gen_ne_zero hirr with ⟨n, u, hnu⟩ use n have : span _ = _ := Ideal.span_singleton_generator s rw [← this, ← hnu, span_singleton_eq_span_singleton] use u theorem unit_mul_pow_congr_pow {p q : R} (hp : Irreducible p) (hq : Irreducible q) (u v : Rˣ) (m n : ℕ) (h : ↑u * p ^ m = v * q ^ n) : m = n := by have key : Associated (Multiset.replicate m p).prod (Multiset.replicate n q).prod := by rw [Multiset.prod_replicate, Multiset.prod_replicate, Associated] refine ⟨u * v⁻¹, ?_⟩ simp only [Units.val_mul] rw [mul_left_comm, ← mul_assoc, h, mul_right_comm, Units.mul_inv, one_mul] have := by refine Multiset.card_eq_card_of_rel (UniqueFactorizationMonoid.factors_unique ?_ ?_ key) all_goals intro x hx obtain rfl := Multiset.eq_of_mem_replicate hx assumption simpa only [Multiset.card_replicate] theorem unit_mul_pow_congr_unit {ϖ : R} (hirr : Irreducible ϖ) (u v : Rˣ) (m n : ℕ) (h : ↑u * ϖ ^ m = v * ϖ ^ n) : u = v := by obtain rfl : m = n := unit_mul_pow_congr_pow hirr hirr u v m n h rw [← sub_eq_zero] at h rw [← sub_mul, mul_eq_zero] at h rcases h with h | h · rw [sub_eq_zero] at h exact mod_cast h · apply (hirr.ne_zero (pow_eq_zero h)).elim /-! ## The additive valuation on a DVR -/ open Classical in /-- The `ℕ∞`-valued additive valuation on a DVR. -/ noncomputable def addVal (R : Type u) [CommRing R] [IsDomain R] [IsDiscreteValuationRing R] : AddValuation R ℕ∞ := multiplicity_addValuation (Classical.choose_spec (exists_prime R)) theorem addVal_def (r : R) (u : Rˣ) {ϖ : R} (hϖ : Irreducible ϖ) (n : ℕ) (hr : r = u * ϖ ^ n) : addVal R r = n := by classical rw [addVal, multiplicity_addValuation_apply, hr, emultiplicity_eq_of_associated_left (associated_of_irreducible R hϖ (Classical.choose_spec (exists_prime R)).irreducible), emultiplicity_eq_of_associated_right (Associated.symm ⟨u, mul_comm _ _⟩), emultiplicity_pow_self_of_prime (irreducible_iff_prime.1 hϖ)] /-- An alternative definition of the additive valuation, taking units into account -/ theorem addVal_def' (u : Rˣ) {ϖ : R} (hϖ : Irreducible ϖ) (n : ℕ) : addVal R ((u : R) * ϖ ^ n) = n := addVal_def _ u hϖ n rfl theorem addVal_zero : addVal R 0 = ⊤ := (addVal R).map_zero theorem addVal_one : addVal R 1 = 0 := (addVal R).map_one @[simp] theorem addVal_uniformizer {ϖ : R} (hϖ : Irreducible ϖ) : addVal R ϖ = 1 := by simpa only [one_mul, eq_self_iff_true, Units.val_one, pow_one, forall_true_left, Nat.cast_one] using addVal_def ϖ 1 hϖ 1 theorem addVal_mul {a b : R} : addVal R (a * b) = addVal R a + addVal R b := (addVal R).map_mul _ _ theorem addVal_pow (a : R) (n : ℕ) : addVal R (a ^ n) = n • addVal R a := (addVal R).map_pow _ _ nonrec theorem _root_.Irreducible.addVal_pow {ϖ : R} (h : Irreducible ϖ) (n : ℕ) : addVal R (ϖ ^ n) = n := by rw [addVal_pow, addVal_uniformizer h, nsmul_one] theorem addVal_eq_top_iff {a : R} : addVal R a = ⊤ ↔ a = 0 := by have hi := (Classical.choose_spec (exists_prime R)).irreducible constructor · contrapose intro h obtain ⟨n, ha⟩ := associated_pow_irreducible h hi obtain ⟨u, rfl⟩ := ha.symm rw [mul_comm, addVal_def' u hi n] nofun · rintro rfl exact addVal_zero theorem addVal_le_iff_dvd {a b : R} : addVal R a ≤ addVal R b ↔ a ∣ b := by classical have hp := Classical.choose_spec (exists_prime R) constructor <;> intro h · by_cases ha0 : a = 0 · rw [ha0, addVal_zero, top_le_iff, addVal_eq_top_iff] at h rw [h] apply dvd_zero obtain ⟨n, ha⟩ := associated_pow_irreducible ha0 hp.irreducible rw [addVal, multiplicity_addValuation_apply, multiplicity_addValuation_apply,
emultiplicity_le_emultiplicity_iff] at h exact ha.dvd.trans (h n ha.symm.dvd) · rw [addVal, multiplicity_addValuation_apply, multiplicity_addValuation_apply] exact emultiplicity_le_emultiplicity_of_dvd_right h theorem addVal_add {a b : R} : min (addVal R a) (addVal R b) ≤ addVal R (a + b) := (addVal R).map_add _ _ @[simp] lemma addVal_eq_zero_of_unit (u : Rˣ) : addVal R u = 0 := by
Mathlib/RingTheory/DiscreteValuationRing/Basic.lean
446
456
/- Copyright (c) 2021 Jireh Loreaux. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jireh Loreaux -/ import Mathlib.Algebra.Algebra.Spectrum.Basic import Mathlib.FieldTheory.IsAlgClosed.Basic /-! # Spectrum mapping theorem This file develops proves the spectral mapping theorem for polynomials over algebraically closed fields. In particular, if `a` is an element of a `𝕜`-algebra `A` where `𝕜` is a field, and `p : 𝕜[X]` is a polynomial, then the spectrum of `Polynomial.aeval a p` contains the image of the spectrum of `a` under `(fun k ↦ Polynomial.eval k p)`. When `𝕜` is algebraically closed, these are in fact equal (assuming either that the spectrum of `a` is nonempty or the polynomial has positive degree), which is the **spectral mapping theorem**. In addition, this file contains the fact that every element of a finite dimensional nontrivial algebra over an algebraically closed field has nonempty spectrum. In particular, this is used in `Module.End.exists_eigenvalue` to show that every linear map from a vector space to itself has an eigenvalue. ## Main statements * `spectrum.subset_polynomial_aeval`, `spectrum.map_polynomial_aeval_of_degree_pos`, `spectrum.map_polynomial_aeval_of_nonempty`: variations on the **spectral mapping theorem**. * `spectrum.nonempty_of_isAlgClosed_of_finiteDimensional`: the spectrum is nonempty for any element of a nontrivial finite dimensional algebra over an algebraically closed field. ## Notations * `σ a` : `spectrum R a` of `a : A` -/ namespace spectrum open Set Polynomial open scoped Pointwise Polynomial universe u v section ScalarRing variable {R : Type u} {A : Type v} variable [CommRing R] [Ring A] [Algebra R A] local notation "σ" => spectrum R local notation "↑ₐ" => algebraMap R A theorem exists_mem_of_not_isUnit_aeval_prod [IsDomain R] {p : R[X]} {a : A} (h : ¬IsUnit (aeval a (Multiset.map (fun x : R => X - C x) p.roots).prod)) : ∃ k : R, k ∈ σ a ∧ eval k p = 0 := by rw [← Multiset.prod_toList, map_list_prod] at h replace h := mt List.prod_isUnit h simp only [not_forall, exists_prop, aeval_C, Multiset.mem_toList, List.mem_map, aeval_X, exists_exists_and_eq_and, Multiset.mem_map, map_sub] at h rcases h with ⟨r, r_mem, r_nu⟩ exact ⟨r, by rwa [mem_iff, ← IsUnit.sub_iff], (mem_roots'.1 r_mem).2⟩ end ScalarRing section ScalarField variable {𝕜 : Type u} {A : Type v} variable [Field 𝕜] [Ring A] [Algebra 𝕜 A] local notation "σ" => spectrum 𝕜 local notation "↑ₐ" => algebraMap 𝕜 A open Polynomial /-- Half of the spectral mapping theorem for polynomials. We prove it separately because it holds over any field, whereas `spectrum.map_polynomial_aeval_of_degree_pos` and `spectrum.map_polynomial_aeval_of_nonempty` need the field to be algebraically closed. -/ theorem subset_polynomial_aeval (a : A) (p : 𝕜[X]) : (eval · p) '' σ a ⊆ σ (aeval a p) := by rintro _ ⟨k, hk, rfl⟩ let q := C (eval k p) - p have hroot : IsRoot q k := by simp only [q, eval_C, eval_sub, sub_self, IsRoot.def] rw [← mul_div_eq_iff_isRoot, ← neg_mul_neg, neg_sub] at hroot have aeval_q_eq : ↑ₐ (eval k p) - aeval a p = aeval a q := by simp only [q, aeval_C, map_sub, sub_left_inj] rw [mem_iff, aeval_q_eq, ← hroot, aeval_mul] have hcomm := (Commute.all (C k - X) (-(q / (X - C k)))).map (aeval a : 𝕜[X] →ₐ[𝕜] A) apply mt fun h => (hcomm.isUnit_mul_iff.mp h).1 simpa only [aeval_X, aeval_C, map_sub] using hk /-- The *spectral mapping theorem* for polynomials. Note: the assumption `degree p > 0` is necessary in case `σ a = ∅`, for then the left-hand side is `∅` and the right-hand side, assuming `[Nontrivial A]`, is `{k}` where `p = Polynomial.C k`. -/ theorem map_polynomial_aeval_of_degree_pos [IsAlgClosed 𝕜] (a : A) (p : 𝕜[X]) (hdeg : 0 < degree p) : σ (aeval a p) = (eval · p) '' σ a := by -- handle the easy direction via `spectrum.subset_polynomial_aeval` refine Set.eq_of_subset_of_subset (fun k hk => ?_) (subset_polynomial_aeval a p) -- write `C k - p` product of linear factors and a constant; show `C k - p ≠ 0`. have hprod := eq_prod_roots_of_splits_id (IsAlgClosed.splits (C k - p)) have h_ne : C k - p ≠ 0 := ne_zero_of_degree_gt <| by rwa [degree_sub_eq_right_of_degree_lt (lt_of_le_of_lt degree_C_le hdeg)] have lead_ne := leadingCoeff_ne_zero.mpr h_ne have lead_unit := (Units.map ↑ₐ.toMonoidHom (Units.mk0 _ lead_ne)).isUnit /- leading coefficient is a unit so product of linear factors is not a unit; apply `exists_mem_of_not_is_unit_aeval_prod`. -/ have p_a_eq : aeval a (C k - p) = ↑ₐ k - aeval a p := by simp only [aeval_C, map_sub, sub_left_inj] rw [mem_iff, ← p_a_eq, hprod, aeval_mul, ((Commute.all _ _).map (aeval a : 𝕜[X] →ₐ[𝕜] A)).isUnit_mul_iff, aeval_C] at hk replace hk := exists_mem_of_not_isUnit_aeval_prod (not_and.mp hk lead_unit) rcases hk with ⟨r, r_mem, r_ev⟩ exact ⟨r, r_mem, symm (by simpa [eval_sub, eval_C, sub_eq_zero] using r_ev)⟩ /-- In this version of the spectral mapping theorem, we assume the spectrum is nonempty instead of assuming the degree of the polynomial is positive. -/ theorem map_polynomial_aeval_of_nonempty [IsAlgClosed 𝕜] (a : A) (p : 𝕜[X]) (hnon : (σ a).Nonempty) : σ (aeval a p) = (fun k => eval k p) '' σ a := by nontriviality A refine Or.elim (le_or_gt (degree p) 0) (fun h => ?_) (map_polynomial_aeval_of_degree_pos a p) rw [eq_C_of_degree_le_zero h] simp only [Set.image_congr, eval_C, aeval_C, scalar_eq, Set.Nonempty.image_const hnon] /-- A specialization of `spectrum.subset_polynomial_aeval` to monic monomials for convenience. -/ theorem pow_image_subset (a : A) (n : ℕ) : (fun x => x ^ n) '' σ a ⊆ σ (a ^ n) := by simpa only [eval_pow, eval_X, aeval_X_pow] using subset_polynomial_aeval a (X ^ n : 𝕜[X]) /-- A specialization of `spectrum.map_polynomial_aeval_of_nonempty` to monic monomials for convenience. -/ theorem map_pow_of_pos [IsAlgClosed 𝕜] (a : A) {n : ℕ} (hn : 0 < n) : σ (a ^ n) = (· ^ n) '' σ a := by
simpa only [aeval_X_pow, eval_pow, eval_X] using map_polynomial_aeval_of_degree_pos a (X ^ n : 𝕜[X]) (by rwa [degree_X_pow, Nat.cast_pos])
Mathlib/FieldTheory/IsAlgClosed/Spectrum.lean
129
130
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Jens Wagemaker -/ import Mathlib.Algebra.Ring.Associated import Mathlib.Algebra.Ring.Regular /-! # Monoids with normalization functions, `gcd`, and `lcm` This file defines extra structures on `CancelCommMonoidWithZero`s, including `IsDomain`s. ## Main Definitions * `NormalizationMonoid` * `GCDMonoid` * `NormalizedGCDMonoid` * `gcdMonoidOfGCD`, `gcdMonoidOfExistsGCD`, `normalizedGCDMonoidOfGCD`, `normalizedGCDMonoidOfExistsGCD` * `gcdMonoidOfLCM`, `gcdMonoidOfExistsLCM`, `normalizedGCDMonoidOfLCM`, `normalizedGCDMonoidOfExistsLCM` For the `NormalizedGCDMonoid` instances on `ℕ` and `ℤ`, see `Mathlib.Algebra.GCDMonoid.Nat`. ## Implementation Notes * `NormalizationMonoid` is defined by assigning to each element a `normUnit` such that multiplying by that unit normalizes the monoid, and `normalize` is an idempotent monoid homomorphism. This definition as currently implemented does casework on `0`. * `GCDMonoid` contains the definitions of `gcd` and `lcm` with the usual properties. They are both determined up to a unit. * `NormalizedGCDMonoid` extends `NormalizationMonoid`, so the `gcd` and `lcm` are always normalized. This makes `gcd`s of polynomials easier to work with, but excludes Euclidean domains, and monoids without zero. * `gcdMonoidOfGCD` and `normalizedGCDMonoidOfGCD` noncomputably construct a `GCDMonoid` (resp. `NormalizedGCDMonoid`) structure just from the `gcd` and its properties. * `gcdMonoidOfExistsGCD` and `normalizedGCDMonoidOfExistsGCD` noncomputably construct a `GCDMonoid` (resp. `NormalizedGCDMonoid`) structure just from a proof that any two elements have a (not necessarily normalized) `gcd`. * `gcdMonoidOfLCM` and `normalizedGCDMonoidOfLCM` noncomputably construct a `GCDMonoid` (resp. `NormalizedGCDMonoid`) structure just from the `lcm` and its properties. * `gcdMonoidOfExistsLCM` and `normalizedGCDMonoidOfExistsLCM` noncomputably construct a `GCDMonoid` (resp. `NormalizedGCDMonoid`) structure just from a proof that any two elements have a (not necessarily normalized) `lcm`. ## TODO * Port GCD facts about nats, definition of coprime * Generalize normalization monoids to commutative (cancellative) monoids with or without zero ## Tags divisibility, gcd, lcm, normalize -/ variable {α : Type*} /-- Normalization monoid: multiplying with `normUnit` gives a normal form for associated elements. -/ class NormalizationMonoid (α : Type*) [CancelCommMonoidWithZero α] where /-- `normUnit` assigns to each element of the monoid a unit of the monoid. -/ normUnit : α → αˣ /-- The proposition that `normUnit` maps `0` to the identity. -/ normUnit_zero : normUnit 0 = 1 /-- The proposition that `normUnit` respects multiplication of non-zero elements. -/ normUnit_mul : ∀ {a b}, a ≠ 0 → b ≠ 0 → normUnit (a * b) = normUnit a * normUnit b /-- The proposition that `normUnit` maps units to their inverses. -/ normUnit_coe_units : ∀ u : αˣ, normUnit u = u⁻¹ export NormalizationMonoid (normUnit normUnit_zero normUnit_mul normUnit_coe_units) attribute [simp] normUnit_coe_units normUnit_zero normUnit_mul section NormalizationMonoid variable [CancelCommMonoidWithZero α] [NormalizationMonoid α] @[simp] theorem normUnit_one : normUnit (1 : α) = 1 := normUnit_coe_units 1 /-- Chooses an element of each associate class, by multiplying by `normUnit` -/ def normalize : α →*₀ α where toFun x := x * normUnit x map_zero' := by simp only [normUnit_zero] exact mul_one (0 : α) map_one' := by rw [normUnit_one, one_mul]; rfl map_mul' x y := (by_cases fun hx : x = 0 => by rw [hx, zero_mul, zero_mul, zero_mul]) fun hx => (by_cases fun hy : y = 0 => by rw [hy, mul_zero, zero_mul, mul_zero]) fun hy => by simp only [normUnit_mul hx hy, Units.val_mul]; simp only [mul_assoc, mul_left_comm y] theorem associated_normalize (x : α) : Associated x (normalize x) := ⟨_, rfl⟩ theorem normalize_associated (x : α) : Associated (normalize x) x := (associated_normalize _).symm theorem associated_normalize_iff {x y : α} : Associated x (normalize y) ↔ Associated x y := ⟨fun h => h.trans (normalize_associated y), fun h => h.trans (associated_normalize y)⟩ theorem normalize_associated_iff {x y : α} : Associated (normalize x) y ↔ Associated x y := ⟨fun h => (associated_normalize _).trans h, fun h => (normalize_associated _).trans h⟩ theorem Associates.mk_normalize (x : α) : Associates.mk (normalize x) = Associates.mk x := Associates.mk_eq_mk_iff_associated.2 (normalize_associated _) theorem normalize_apply (x : α) : normalize x = x * normUnit x := rfl theorem normalize_zero : normalize (0 : α) = 0 := normalize.map_zero theorem normalize_one : normalize (1 : α) = 1 := normalize.map_one theorem normalize_coe_units (u : αˣ) : normalize (u : α) = 1 := by simp [normalize_apply] theorem normalize_eq_zero {x : α} : normalize x = 0 ↔ x = 0 := ⟨fun hx => (associated_zero_iff_eq_zero x).1 <| hx ▸ associated_normalize _, by rintro rfl; exact normalize_zero⟩ theorem normalize_eq_one {x : α} : normalize x = 1 ↔ IsUnit x := ⟨fun hx => isUnit_iff_exists_inv.2 ⟨_, hx⟩, fun ⟨u, hu⟩ => hu ▸ normalize_coe_units u⟩ @[simp] theorem normUnit_mul_normUnit (a : α) : normUnit (a * normUnit a) = 1 := by nontriviality α using Subsingleton.elim a 0 obtain rfl | h := eq_or_ne a 0 · rw [normUnit_zero, zero_mul, normUnit_zero] · rw [normUnit_mul h (Units.ne_zero _), normUnit_coe_units, mul_inv_eq_one] @[simp] theorem normalize_idem (x : α) : normalize (normalize x) = normalize x := by simp [normalize_apply] theorem normalize_eq_normalize {a b : α} (hab : a ∣ b) (hba : b ∣ a) : normalize a = normalize b := by nontriviality α rcases associated_of_dvd_dvd hab hba with ⟨u, rfl⟩ refine by_cases (by rintro rfl; simp only [zero_mul]) fun ha : a ≠ 0 => ?_ suffices a * ↑(normUnit a) = a * ↑u * ↑(normUnit a) * ↑u⁻¹ by simpa only [normalize_apply, mul_assoc, normUnit_mul ha u.ne_zero, normUnit_coe_units] calc a * ↑(normUnit a) = a * ↑(normUnit a) * ↑u * ↑u⁻¹ := (Units.mul_inv_cancel_right _ _).symm _ = a * ↑u * ↑(normUnit a) * ↑u⁻¹ := by rw [mul_right_comm a] theorem normalize_eq_normalize_iff {x y : α} : normalize x = normalize y ↔ x ∣ y ∧ y ∣ x := ⟨fun h => ⟨Units.dvd_mul_right.1 ⟨_, h.symm⟩, Units.dvd_mul_right.1 ⟨_, h⟩⟩, fun ⟨hxy, hyx⟩ => normalize_eq_normalize hxy hyx⟩ theorem dvd_antisymm_of_normalize_eq {a b : α} (ha : normalize a = a) (hb : normalize b = b) (hab : a ∣ b) (hba : b ∣ a) : a = b := ha ▸ hb ▸ normalize_eq_normalize hab hba theorem Associated.eq_of_normalized {a b : α} (h : Associated a b) (ha : normalize a = a) (hb : normalize b = b) : a = b := dvd_antisymm_of_normalize_eq ha hb h.dvd h.dvd' @[simp] theorem dvd_normalize_iff {a b : α} : a ∣ normalize b ↔ a ∣ b := Units.dvd_mul_right @[simp] theorem normalize_dvd_iff {a b : α} : normalize a ∣ b ↔ a ∣ b := Units.mul_right_dvd end NormalizationMonoid namespace Associates variable [CancelCommMonoidWithZero α] [NormalizationMonoid α] /-- Maps an element of `Associates` back to the normalized element of its associate class -/ protected def out : Associates α → α := (Quotient.lift (normalize : α → α)) fun a _ ⟨_, hu⟩ => hu ▸ normalize_eq_normalize ⟨_, rfl⟩ (Units.mul_right_dvd.2 <| dvd_refl a) @[simp] theorem out_mk (a : α) : (Associates.mk a).out = normalize a := rfl @[simp] theorem out_one : (1 : Associates α).out = 1 := normalize_one theorem out_mul (a b : Associates α) : (a * b).out = a.out * b.out := Quotient.inductionOn₂ a b fun _ _ => by simp only [Associates.quotient_mk_eq_mk, out_mk, mk_mul_mk, normalize.map_mul] theorem dvd_out_iff (a : α) (b : Associates α) : a ∣ b.out ↔ Associates.mk a ≤ b := Quotient.inductionOn b <| by simp [Associates.out_mk, Associates.quotient_mk_eq_mk, mk_le_mk_iff_dvd] theorem out_dvd_iff (a : α) (b : Associates α) : b.out ∣ a ↔ b ≤ Associates.mk a := Quotient.inductionOn b <| by simp [Associates.out_mk, Associates.quotient_mk_eq_mk, mk_le_mk_iff_dvd] @[simp] theorem out_top : (⊤ : Associates α).out = 0 := normalize_zero @[simp] theorem normalize_out (a : Associates α) : normalize a.out = a.out := Quotient.inductionOn a normalize_idem @[simp] theorem mk_out (a : Associates α) : Associates.mk a.out = a := Quotient.inductionOn a mk_normalize theorem out_injective : Function.Injective (Associates.out : _ → α) := Function.LeftInverse.injective mk_out end Associates /-- GCD monoid: a `CancelCommMonoidWithZero` with `gcd` (greatest common divisor) and `lcm` (least common multiple) operations, determined up to a unit. The type class focuses on `gcd` and we derive the corresponding `lcm` facts from `gcd`. -/ class GCDMonoid (α : Type*) [CancelCommMonoidWithZero α] where /-- The greatest common divisor between two elements. -/ gcd : α → α → α /-- The least common multiple between two elements. -/ lcm : α → α → α /-- The GCD is a divisor of the first element. -/ gcd_dvd_left : ∀ a b, gcd a b ∣ a /-- The GCD is a divisor of the second element. -/ gcd_dvd_right : ∀ a b, gcd a b ∣ b /-- Any common divisor of both elements is a divisor of the GCD. -/ dvd_gcd : ∀ {a b c}, a ∣ c → a ∣ b → a ∣ gcd c b /-- The product of two elements is `Associated` with the product of their GCD and LCM. -/ gcd_mul_lcm : ∀ a b, Associated (gcd a b * lcm a b) (a * b) /-- `0` is left-absorbing. -/ lcm_zero_left : ∀ a, lcm 0 a = 0 /-- `0` is right-absorbing. -/ lcm_zero_right : ∀ a, lcm a 0 = 0 /-- Normalized GCD monoid: a `CancelCommMonoidWithZero` with normalization and `gcd` (greatest common divisor) and `lcm` (least common multiple) operations. In this setting `gcd` and `lcm` form a bounded lattice on the associated elements where `gcd` is the infimum, `lcm` is the supremum, `1` is bottom, and `0` is top. The type class focuses on `gcd` and we derive the corresponding `lcm` facts from `gcd`. -/ class NormalizedGCDMonoid (α : Type*) [CancelCommMonoidWithZero α] extends NormalizationMonoid α, GCDMonoid α where /-- The GCD is normalized to itself. -/ normalize_gcd : ∀ a b, normalize (gcd a b) = gcd a b /-- The LCM is normalized to itself. -/ normalize_lcm : ∀ a b, normalize (lcm a b) = lcm a b export GCDMonoid (gcd lcm gcd_dvd_left gcd_dvd_right dvd_gcd lcm_zero_left lcm_zero_right) attribute [simp] lcm_zero_left lcm_zero_right section GCDMonoid variable [CancelCommMonoidWithZero α] instance [NormalizationMonoid α] : Nonempty (NormalizationMonoid α) := ⟨‹_›⟩ instance [GCDMonoid α] : Nonempty (GCDMonoid α) := ⟨‹_›⟩ instance [NormalizedGCDMonoid α] : Nonempty (NormalizedGCDMonoid α) := ⟨‹_›⟩ instance [h : Nonempty (NormalizedGCDMonoid α)] : Nonempty (NormalizationMonoid α) := h.elim fun _ ↦ inferInstance instance [h : Nonempty (NormalizedGCDMonoid α)] : Nonempty (GCDMonoid α) := h.elim fun _ ↦ inferInstance theorem gcd_isUnit_iff_isRelPrime [GCDMonoid α] {a b : α} : IsUnit (gcd a b) ↔ IsRelPrime a b := ⟨fun h _ ha hb ↦ isUnit_of_dvd_unit (dvd_gcd ha hb) h, (· (gcd_dvd_left a b) (gcd_dvd_right a b))⟩ @[simp] theorem normalize_gcd [NormalizedGCDMonoid α] : ∀ a b : α, normalize (gcd a b) = gcd a b := NormalizedGCDMonoid.normalize_gcd theorem gcd_mul_lcm [GCDMonoid α] : ∀ a b : α, Associated (gcd a b * lcm a b) (a * b) := GCDMonoid.gcd_mul_lcm section GCD theorem dvd_gcd_iff [GCDMonoid α] (a b c : α) : a ∣ gcd b c ↔ a ∣ b ∧ a ∣ c := Iff.intro (fun h => ⟨h.trans (gcd_dvd_left _ _), h.trans (gcd_dvd_right _ _)⟩) fun ⟨hab, hac⟩ => dvd_gcd hab hac theorem gcd_comm [NormalizedGCDMonoid α] (a b : α) : gcd a b = gcd b a := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _) (dvd_gcd (gcd_dvd_right _ _) (gcd_dvd_left _ _)) (dvd_gcd (gcd_dvd_right _ _) (gcd_dvd_left _ _)) theorem gcd_comm' [GCDMonoid α] (a b : α) : Associated (gcd a b) (gcd b a) := associated_of_dvd_dvd (dvd_gcd (gcd_dvd_right _ _) (gcd_dvd_left _ _)) (dvd_gcd (gcd_dvd_right _ _) (gcd_dvd_left _ _)) theorem gcd_assoc [NormalizedGCDMonoid α] (m n k : α) : gcd (gcd m n) k = gcd m (gcd n k) := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _) (dvd_gcd ((gcd_dvd_left (gcd m n) k).trans (gcd_dvd_left m n)) (dvd_gcd ((gcd_dvd_left (gcd m n) k).trans (gcd_dvd_right m n)) (gcd_dvd_right (gcd m n) k))) (dvd_gcd (dvd_gcd (gcd_dvd_left m (gcd n k)) ((gcd_dvd_right m (gcd n k)).trans (gcd_dvd_left n k))) ((gcd_dvd_right m (gcd n k)).trans (gcd_dvd_right n k))) theorem gcd_assoc' [GCDMonoid α] (m n k : α) : Associated (gcd (gcd m n) k) (gcd m (gcd n k)) := associated_of_dvd_dvd (dvd_gcd ((gcd_dvd_left (gcd m n) k).trans (gcd_dvd_left m n)) (dvd_gcd ((gcd_dvd_left (gcd m n) k).trans (gcd_dvd_right m n)) (gcd_dvd_right (gcd m n) k))) (dvd_gcd (dvd_gcd (gcd_dvd_left m (gcd n k)) ((gcd_dvd_right m (gcd n k)).trans (gcd_dvd_left n k))) ((gcd_dvd_right m (gcd n k)).trans (gcd_dvd_right n k))) instance [NormalizedGCDMonoid α] : Std.Commutative (α := α) gcd where comm := gcd_comm instance [NormalizedGCDMonoid α] : Std.Associative (α := α) gcd where assoc := gcd_assoc theorem gcd_eq_normalize [NormalizedGCDMonoid α] {a b c : α} (habc : gcd a b ∣ c) (hcab : c ∣ gcd a b) : gcd a b = normalize c := normalize_gcd a b ▸ normalize_eq_normalize habc hcab @[simp] theorem gcd_zero_left [NormalizedGCDMonoid α] (a : α) : gcd 0 a = normalize a := gcd_eq_normalize (gcd_dvd_right 0 a) (dvd_gcd (dvd_zero _) (dvd_refl a)) theorem gcd_zero_left' [GCDMonoid α] (a : α) : Associated (gcd 0 a) a := associated_of_dvd_dvd (gcd_dvd_right 0 a) (dvd_gcd (dvd_zero _) (dvd_refl a)) @[simp] theorem gcd_zero_right [NormalizedGCDMonoid α] (a : α) : gcd a 0 = normalize a := gcd_eq_normalize (gcd_dvd_left a 0) (dvd_gcd (dvd_refl a) (dvd_zero _)) theorem gcd_zero_right' [GCDMonoid α] (a : α) : Associated (gcd a 0) a := associated_of_dvd_dvd (gcd_dvd_left a 0) (dvd_gcd (dvd_refl a) (dvd_zero _)) @[simp] theorem gcd_eq_zero_iff [GCDMonoid α] (a b : α) : gcd a b = 0 ↔ a = 0 ∧ b = 0 := Iff.intro (fun h => by let ⟨ca, ha⟩ := gcd_dvd_left a b let ⟨cb, hb⟩ := gcd_dvd_right a b rw [h, zero_mul] at ha hb exact ⟨ha, hb⟩) fun ⟨ha, hb⟩ => by rw [ha, hb, ← zero_dvd_iff] apply dvd_gcd <;> rfl theorem gcd_ne_zero_of_left [GCDMonoid α] {a b : α} (ha : a ≠ 0) : gcd a b ≠ 0 := by simp_all theorem gcd_ne_zero_of_right [GCDMonoid α] {a b : α} (hb : b ≠ 0) : gcd a b ≠ 0 := by simp_all @[simp] theorem gcd_one_left [NormalizedGCDMonoid α] (a : α) : gcd 1 a = 1 := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) normalize_one (gcd_dvd_left _ _) (one_dvd _) @[simp] theorem isUnit_gcd_one_left [GCDMonoid α] (a : α) : IsUnit (gcd 1 a) := isUnit_of_dvd_one (gcd_dvd_left _ _) theorem gcd_one_left' [GCDMonoid α] (a : α) : Associated (gcd 1 a) 1 := by simp @[simp] theorem gcd_one_right [NormalizedGCDMonoid α] (a : α) : gcd a 1 = 1 := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) normalize_one (gcd_dvd_right _ _) (one_dvd _) @[simp] theorem isUnit_gcd_one_right [GCDMonoid α] (a : α) : IsUnit (gcd a 1) := isUnit_of_dvd_one (gcd_dvd_right _ _) theorem gcd_one_right' [GCDMonoid α] (a : α) : Associated (gcd a 1) 1 := by simp theorem gcd_dvd_gcd [GCDMonoid α] {a b c d : α} (hab : a ∣ b) (hcd : c ∣ d) : gcd a c ∣ gcd b d := dvd_gcd ((gcd_dvd_left _ _).trans hab) ((gcd_dvd_right _ _).trans hcd) protected theorem Associated.gcd [GCDMonoid α] {a₁ a₂ b₁ b₂ : α} (ha : Associated a₁ a₂) (hb : Associated b₁ b₂) : Associated (gcd a₁ b₁) (gcd a₂ b₂) := associated_of_dvd_dvd (gcd_dvd_gcd ha.dvd hb.dvd) (gcd_dvd_gcd ha.dvd' hb.dvd') @[simp] theorem gcd_same [NormalizedGCDMonoid α] (a : α) : gcd a a = normalize a := gcd_eq_normalize (gcd_dvd_left _ _) (dvd_gcd (dvd_refl a) (dvd_refl a)) @[simp] theorem gcd_mul_left [NormalizedGCDMonoid α] (a b c : α) : gcd (a * b) (a * c) = normalize a * gcd b c := (by_cases (by rintro rfl; simp only [zero_mul, gcd_zero_left, normalize_zero])) fun ha : a ≠ 0 => suffices gcd (a * b) (a * c) = normalize (a * gcd b c) by simpa let ⟨d, eq⟩ := dvd_gcd (dvd_mul_right a b) (dvd_mul_right a c) gcd_eq_normalize (eq.symm ▸ mul_dvd_mul_left a (show d ∣ gcd b c from dvd_gcd ((mul_dvd_mul_iff_left ha).1 <| eq ▸ gcd_dvd_left _ _) ((mul_dvd_mul_iff_left ha).1 <| eq ▸ gcd_dvd_right _ _))) (dvd_gcd (mul_dvd_mul_left a <| gcd_dvd_left _ _) (mul_dvd_mul_left a <| gcd_dvd_right _ _)) theorem gcd_mul_left' [GCDMonoid α] (a b c : α) : Associated (gcd (a * b) (a * c)) (a * gcd b c) := by obtain rfl | ha := eq_or_ne a 0 · simp only [zero_mul, gcd_zero_left'] obtain ⟨d, eq⟩ := dvd_gcd (dvd_mul_right a b) (dvd_mul_right a c) apply associated_of_dvd_dvd · rw [eq] apply mul_dvd_mul_left exact dvd_gcd ((mul_dvd_mul_iff_left ha).1 <| eq ▸ gcd_dvd_left _ _) ((mul_dvd_mul_iff_left ha).1 <| eq ▸ gcd_dvd_right _ _) · exact dvd_gcd (mul_dvd_mul_left a <| gcd_dvd_left _ _) (mul_dvd_mul_left a <| gcd_dvd_right _ _) @[simp] theorem gcd_mul_right [NormalizedGCDMonoid α] (a b c : α) : gcd (b * a) (c * a) = gcd b c * normalize a := by simp only [mul_comm, gcd_mul_left] @[simp] theorem gcd_mul_right' [GCDMonoid α] (a b c : α) : Associated (gcd (b * a) (c * a)) (gcd b c * a) := by simp only [mul_comm, gcd_mul_left'] theorem gcd_eq_left_iff [NormalizedGCDMonoid α] (a b : α) (h : normalize a = a) : gcd a b = a ↔ a ∣ b := (Iff.intro fun eq => eq ▸ gcd_dvd_right _ _) fun hab => dvd_antisymm_of_normalize_eq (normalize_gcd _ _) h (gcd_dvd_left _ _) (dvd_gcd (dvd_refl a) hab) theorem gcd_eq_right_iff [NormalizedGCDMonoid α] (a b : α) (h : normalize b = b) : gcd a b = b ↔ b ∣ a := by simpa only [gcd_comm a b] using gcd_eq_left_iff b a h theorem gcd_dvd_gcd_mul_left [GCDMonoid α] (m n k : α) : gcd m n ∣ gcd (k * m) n := gcd_dvd_gcd (dvd_mul_left _ _) dvd_rfl theorem gcd_dvd_gcd_mul_right [GCDMonoid α] (m n k : α) : gcd m n ∣ gcd (m * k) n := gcd_dvd_gcd (dvd_mul_right _ _) dvd_rfl theorem gcd_dvd_gcd_mul_left_right [GCDMonoid α] (m n k : α) : gcd m n ∣ gcd m (k * n) := gcd_dvd_gcd dvd_rfl (dvd_mul_left _ _) theorem gcd_dvd_gcd_mul_right_right [GCDMonoid α] (m n k : α) : gcd m n ∣ gcd m (n * k) := gcd_dvd_gcd dvd_rfl (dvd_mul_right _ _) theorem Associated.gcd_eq_left [NormalizedGCDMonoid α] {m n : α} (h : Associated m n) (k : α) : gcd m k = gcd n k := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _) (gcd_dvd_gcd h.dvd dvd_rfl) (gcd_dvd_gcd h.symm.dvd dvd_rfl) theorem Associated.gcd_eq_right [NormalizedGCDMonoid α] {m n : α} (h : Associated m n) (k : α) : gcd k m = gcd k n := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _) (gcd_dvd_gcd dvd_rfl h.dvd) (gcd_dvd_gcd dvd_rfl h.symm.dvd) theorem dvd_gcd_mul_of_dvd_mul [GCDMonoid α] {m n k : α} (H : k ∣ m * n) : k ∣ gcd k m * n := (dvd_gcd (dvd_mul_right _ n) H).trans (gcd_mul_right' n k m).dvd theorem dvd_gcd_mul_iff_dvd_mul [GCDMonoid α] {m n k : α} : k ∣ gcd k m * n ↔ k ∣ m * n := ⟨fun h => h.trans (mul_dvd_mul (gcd_dvd_right k m) dvd_rfl), dvd_gcd_mul_of_dvd_mul⟩ theorem dvd_mul_gcd_of_dvd_mul [GCDMonoid α] {m n k : α} (H : k ∣ m * n) : k ∣ m * gcd k n := by rw [mul_comm] at H ⊢ exact dvd_gcd_mul_of_dvd_mul H theorem dvd_mul_gcd_iff_dvd_mul [GCDMonoid α] {m n k : α} : k ∣ m * gcd k n ↔ k ∣ m * n := ⟨fun h => h.trans (mul_dvd_mul dvd_rfl (gcd_dvd_right k n)), dvd_mul_gcd_of_dvd_mul⟩ /-- Represent a divisor of `m * n` as a product of a divisor of `m` and a divisor of `n`. Note: In general, this representation is highly non-unique. See `Nat.dvdProdDvdOfDvdProd` for a constructive version on `ℕ`. -/ instance [h : Nonempty (GCDMonoid α)] : DecompositionMonoid α where primal k m n H := by cases h by_cases h0 : gcd k m = 0 · rw [gcd_eq_zero_iff] at h0 rcases h0 with ⟨rfl, rfl⟩ exact ⟨0, n, dvd_refl 0, dvd_refl n, by simp⟩ · obtain ⟨a, ha⟩ := gcd_dvd_left k m refine ⟨gcd k m, a, gcd_dvd_right _ _, ?_, ha⟩ rw [← mul_dvd_mul_iff_left h0, ← ha] exact dvd_gcd_mul_of_dvd_mul H theorem gcd_mul_dvd_mul_gcd [GCDMonoid α] (k m n : α) : gcd k (m * n) ∣ gcd k m * gcd k n := by obtain ⟨m', n', hm', hn', h⟩ := exists_dvd_and_dvd_of_dvd_mul (gcd_dvd_right k (m * n)) replace h : gcd k (m * n) = m' * n' := h rw [h] have hm'n' : m' * n' ∣ k := h ▸ gcd_dvd_left _ _ apply mul_dvd_mul · have hm'k : m' ∣ k := (dvd_mul_right m' n').trans hm'n' exact dvd_gcd hm'k hm' · have hn'k : n' ∣ k := (dvd_mul_left n' m').trans hm'n' exact dvd_gcd hn'k hn'
theorem gcd_pow_right_dvd_pow_gcd [GCDMonoid α] {a b : α} {k : ℕ} : gcd a (b ^ k) ∣ gcd a b ^ k := by
Mathlib/Algebra/GCDMonoid/Basic.lean
499
500
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson, Jalex Stark, Kyle Miller, Lu-Ming Zhang -/ import Mathlib.Combinatorics.SimpleGraph.Basic import Mathlib.Combinatorics.SimpleGraph.Connectivity.WalkCounting import Mathlib.LinearAlgebra.Matrix.Trace import Mathlib.LinearAlgebra.Matrix.Symmetric /-! # Adjacency Matrices This module defines the adjacency matrix of a graph, and provides theorems connecting graph properties to computational properties of the matrix. ## Main definitions * `Matrix.IsAdjMatrix`: `A : Matrix V V α` is qualified as an "adjacency matrix" if (1) every entry of `A` is `0` or `1`, (2) `A` is symmetric, (3) every diagonal entry of `A` is `0`. * `Matrix.IsAdjMatrix.to_graph`: for `A : Matrix V V α` and `h : A.IsAdjMatrix`, `h.to_graph` is the simple graph induced by `A`. * `Matrix.compl`: for `A : Matrix V V α`, `A.compl` is supposed to be the adjacency matrix of the complement graph of the graph induced by `A`. * `SimpleGraph.adjMatrix`: the adjacency matrix of a `SimpleGraph`. * `SimpleGraph.adjMatrix_pow_apply_eq_card_walk`: each entry of the `n`th power of a graph's adjacency matrix counts the number of length-`n` walks between the corresponding pair of vertices. -/ open Matrix open Finset Matrix SimpleGraph variable {V α : Type*} namespace Matrix /-- `A : Matrix V V α` is qualified as an "adjacency matrix" if (1) every entry of `A` is `0` or `1`, (2) `A` is symmetric, (3) every diagonal entry of `A` is `0`. -/ structure IsAdjMatrix [Zero α] [One α] (A : Matrix V V α) : Prop where zero_or_one : ∀ i j, A i j = 0 ∨ A i j = 1 := by aesop symm : A.IsSymm := by aesop apply_diag : ∀ i, A i i = 0 := by aesop namespace IsAdjMatrix variable {A : Matrix V V α} @[simp] theorem apply_diag_ne [MulZeroOneClass α] [Nontrivial α] (h : IsAdjMatrix A) (i : V) : ¬A i i = 1 := by simp [h.apply_diag i] @[simp] theorem apply_ne_one_iff [MulZeroOneClass α] [Nontrivial α] (h : IsAdjMatrix A) (i j : V) : ¬A i j = 1 ↔ A i j = 0 := by obtain h | h := h.zero_or_one i j <;> simp [h] @[simp] theorem apply_ne_zero_iff [MulZeroOneClass α] [Nontrivial α] (h : IsAdjMatrix A) (i j : V) : ¬A i j = 0 ↔ A i j = 1 := by rw [← apply_ne_one_iff h, Classical.not_not] /-- For `A : Matrix V V α` and `h : IsAdjMatrix A`, `h.toGraph` is the simple graph whose adjacency matrix is `A`. -/ @[simps] def toGraph [MulZeroOneClass α] [Nontrivial α] (h : IsAdjMatrix A) : SimpleGraph V where Adj i j := A i j = 1 symm i j hij := by simp only; rwa [h.symm.apply i j] loopless i := by simp [h] instance [MulZeroOneClass α] [Nontrivial α] [DecidableEq α] (h : IsAdjMatrix A) : DecidableRel h.toGraph.Adj := by simp only [toGraph] infer_instance end IsAdjMatrix /-- For `A : Matrix V V α`, `A.compl` is supposed to be the adjacency matrix of the complement graph of the graph induced by `A.adjMatrix`. -/ def compl [Zero α] [One α] [DecidableEq α] [DecidableEq V] (A : Matrix V V α) : Matrix V V α := fun i j => ite (i = j) 0 (ite (A i j = 0) 1 0) section Compl variable [DecidableEq α] [DecidableEq V] (A : Matrix V V α) @[simp] theorem compl_apply_diag [Zero α] [One α] (i : V) : A.compl i i = 0 := by simp [compl] @[simp] theorem compl_apply [Zero α] [One α] (i j : V) : A.compl i j = 0 ∨ A.compl i j = 1 := by unfold compl split_ifs <;> simp @[simp] theorem isSymm_compl [Zero α] [One α] (h : A.IsSymm) : A.compl.IsSymm := by ext simp [compl, h.apply, eq_comm] @[simp] theorem isAdjMatrix_compl [Zero α] [One α] (h : A.IsSymm) : IsAdjMatrix A.compl := { symm := by simp [h] } namespace IsAdjMatrix variable {A} @[simp] theorem compl [Zero α] [One α] (h : IsAdjMatrix A) : IsAdjMatrix A.compl := isAdjMatrix_compl A h.symm theorem toGraph_compl_eq [MulZeroOneClass α] [Nontrivial α] (h : IsAdjMatrix A) : h.compl.toGraph = h.toGraphᶜ := by ext v w rcases h.zero_or_one v w with h | h <;> by_cases hvw : v = w <;> simp [Matrix.compl, h, hvw] end IsAdjMatrix end Compl end Matrix open Matrix namespace SimpleGraph variable (G : SimpleGraph V) [DecidableRel G.Adj] variable (α) in /-- `adjMatrix G α` is the matrix `A` such that `A i j = (1 : α)` if `i` and `j` are adjacent in the simple graph `G`, and otherwise `A i j = 0`. -/ def adjMatrix [Zero α] [One α] : Matrix V V α := of fun i j => if G.Adj i j then (1 : α) else 0 -- TODO: set as an equation lemma for `adjMatrix`, see https://github.com/leanprover-community/mathlib4/pull/3024 @[simp] theorem adjMatrix_apply (v w : V) [Zero α] [One α] : G.adjMatrix α v w = if G.Adj v w then 1 else 0 := rfl @[simp] theorem transpose_adjMatrix [Zero α] [One α] : (G.adjMatrix α)ᵀ = G.adjMatrix α := by ext simp [adj_comm] @[simp] theorem isSymm_adjMatrix [Zero α] [One α] : (G.adjMatrix α).IsSymm := transpose_adjMatrix G variable (α) /-- The adjacency matrix of `G` is an adjacency matrix. -/ @[simp] theorem isAdjMatrix_adjMatrix [Zero α] [One α] : (G.adjMatrix α).IsAdjMatrix := { zero_or_one := fun i j => by by_cases h : G.Adj i j <;> simp [h] } /-- The graph induced by the adjacency matrix of `G` is `G` itself. -/ theorem toGraph_adjMatrix_eq [MulZeroOneClass α] [Nontrivial α] : (G.isAdjMatrix_adjMatrix α).toGraph = G := by ext simp only [IsAdjMatrix.toGraph_adj, adjMatrix_apply, ite_eq_left_iff, zero_ne_one] apply Classical.not_not variable {α} /-- The sum of the identity, the adjacency matrix, and its complement is the all-ones matrix. -/ theorem one_add_adjMatrix_add_compl_adjMatrix_eq_allOnes [DecidableEq V] [DecidableEq α] [AddMonoidWithOne α] : 1 + G.adjMatrix α + (G.adjMatrix α).compl = Matrix.of fun _ _ ↦ 1 := by ext i j unfold Matrix.compl rw [of_apply, add_apply, adjMatrix_apply, add_apply, adjMatrix_apply, one_apply] by_cases h : G.Adj i j · aesop · split_ifs <;> simp_all variable [Fintype V] @[simp] theorem adjMatrix_dotProduct [NonAssocSemiring α] (v : V) (vec : V → α) : dotProduct (G.adjMatrix α v) vec = ∑ u ∈ G.neighborFinset v, vec u := by simp [neighborFinset_eq_filter, dotProduct, sum_filter] @[simp] theorem dotProduct_adjMatrix [NonAssocSemiring α] (v : V) (vec : V → α) : dotProduct vec (G.adjMatrix α v) = ∑ u ∈ G.neighborFinset v, vec u := by simp [neighborFinset_eq_filter, dotProduct, sum_filter, Finset.sum_apply] @[simp] theorem adjMatrix_mulVec_apply [NonAssocSemiring α] (v : V) (vec : V → α) : (G.adjMatrix α *ᵥ vec) v = ∑ u ∈ G.neighborFinset v, vec u := by rw [mulVec, adjMatrix_dotProduct] @[simp] theorem adjMatrix_vecMul_apply [NonAssocSemiring α] (v : V) (vec : V → α) : (vec ᵥ* G.adjMatrix α) v = ∑ u ∈ G.neighborFinset v, vec u := by simp only [← dotProduct_adjMatrix, vecMul] refine congr rfl ?_; ext x rw [← transpose_apply (adjMatrix α G) x v, transpose_adjMatrix] @[simp] theorem adjMatrix_mul_apply [NonAssocSemiring α] (M : Matrix V V α) (v w : V) : (G.adjMatrix α * M) v w = ∑ u ∈ G.neighborFinset v, M u w := by simp [mul_apply, neighborFinset_eq_filter, sum_filter] @[simp] theorem mul_adjMatrix_apply [NonAssocSemiring α] (M : Matrix V V α) (v w : V) : (M * G.adjMatrix α) v w = ∑ u ∈ G.neighborFinset w, M v u := by simp [mul_apply, neighborFinset_eq_filter, sum_filter, adj_comm] variable (α) in @[simp] theorem trace_adjMatrix [AddCommMonoid α] [One α] : Matrix.trace (G.adjMatrix α) = 0 := by simp [Matrix.trace] theorem adjMatrix_mul_self_apply_self [NonAssocSemiring α] (i : V) : (G.adjMatrix α * G.adjMatrix α) i i = degree G i := by simp [filter_true_of_mem] variable {G} theorem adjMatrix_mulVec_const_apply [NonAssocSemiring α] {a : α} {v : V} :
(G.adjMatrix α *ᵥ Function.const _ a) v = G.degree v * a := by simp theorem adjMatrix_mulVec_const_apply_of_regular [NonAssocSemiring α] {d : ℕ} {a : α}
Mathlib/Combinatorics/SimpleGraph/AdjMatrix.lean
230
232
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Computability.PartrecCode import Mathlib.Data.Set.Subsingleton /-! # Computability theory and the halting problem A universal partial recursive function, Rice's theorem, and the halting problem. ## References * [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019] -/ open List (Vector) open Encodable Denumerable namespace Nat.Partrec open Computable Part theorem merge' {f g} (hf : Nat.Partrec f) (hg : Nat.Partrec g) : ∃ h, Nat.Partrec h ∧ ∀ a, (∀ x ∈ h a, x ∈ f a ∨ x ∈ g a) ∧ ((h a).Dom ↔ (f a).Dom ∨ (g a).Dom) := by obtain ⟨cf, rfl⟩ := Code.exists_code.1 hf obtain ⟨cg, rfl⟩ := Code.exists_code.1 hg have : Nat.Partrec fun n => Nat.rfindOpt fun k => cf.evaln k n <|> cg.evaln k n := Partrec.nat_iff.1 (Partrec.rfindOpt <| Primrec.option_orElse.to_comp.comp (Code.evaln_prim.to_comp.comp <| (snd.pair (const cf)).pair fst) (Code.evaln_prim.to_comp.comp <| (snd.pair (const cg)).pair fst)) refine ⟨_, this, fun n => ?_⟩ have : ∀ x ∈ rfindOpt fun k ↦ HOrElse.hOrElse (Code.evaln k cf n) fun _x ↦ Code.evaln k cg n, x ∈ Code.eval cf n ∨ x ∈ Code.eval cg n := by intro x h obtain ⟨k, e⟩ := Nat.rfindOpt_spec h revert e simp only [Option.mem_def] rcases e' : cf.evaln k n with - | y <;> simp <;> intro e · exact Or.inr (Code.evaln_sound e) · subst y exact Or.inl (Code.evaln_sound e') refine ⟨this, ⟨fun h => (this _ ⟨h, rfl⟩).imp Exists.fst Exists.fst, ?_⟩⟩ intro h rw [Nat.rfindOpt_dom] simp only [dom_iff_mem, Code.evaln_complete, Option.mem_def] at h obtain ⟨x, k, e⟩ | ⟨x, k, e⟩ := h · refine ⟨k, x, ?_⟩ simp only [e, Option.some_orElse, Option.mem_def] · refine ⟨k, ?_⟩ rcases cf.evaln k n with - | y · exact ⟨x, by simp only [e, Option.mem_def, Option.none_orElse]⟩ · exact ⟨y, by simp only [Option.some_orElse, Option.mem_def]⟩ end Nat.Partrec namespace Partrec variable {α : Type*} {β : Type*} {γ : Type*} {σ : Type*} variable [Primcodable α] [Primcodable β] [Primcodable γ] [Primcodable σ] open Computable Part open Nat.Partrec (Code) open Nat.Partrec.Code theorem merge' {f g : α →. σ} (hf : Partrec f) (hg : Partrec g) : ∃ k : α →. σ, Partrec k ∧ ∀ a, (∀ x ∈ k a, x ∈ f a ∨ x ∈ g a) ∧ ((k a).Dom ↔ (f a).Dom ∨ (g a).Dom) := by let ⟨k, hk, H⟩ := Nat.Partrec.merge' (bind_decode₂_iff.1 hf) (bind_decode₂_iff.1 hg)
let k' (a : α) := (k (encode a)).bind fun n => (decode (α := σ) n : Part σ) refine ⟨k', ((nat_iff.2 hk).comp Computable.encode).bind (Computable.decode.ofOption.comp snd).to₂, fun a => ?_⟩ have : ∀ x ∈ k' a, x ∈ f a ∨ x ∈ g a := by intro x h' simp only [k', exists_prop, mem_coe, mem_bind_iff, Option.mem_def] at h' obtain ⟨n, hn, hx⟩ := h' have := (H _).1 _ hn simp only [decode₂_encode, coe_some, bind_some, mem_map_iff] at this obtain ⟨a', ha, rfl⟩ | ⟨a', ha, rfl⟩ := this <;> simp only [encodek, Option.some_inj] at hx <;> rw [hx] at ha · exact Or.inl ha · exact Or.inr ha refine ⟨this, ⟨fun h => (this _ ⟨h, rfl⟩).imp Exists.fst Exists.fst, ?_⟩⟩ intro h rw [bind_dom] have hk : (k (encode a)).Dom := (H _).2.2 (by simpa only [encodek₂, bind_some, coe_some] using h) exists hk simp only [exists_prop, mem_map_iff, mem_coe, mem_bind_iff, Option.mem_def] at H obtain ⟨a', _, y, _, e⟩ | ⟨a', _, y, _, e⟩ := (H _).1 _ ⟨hk, rfl⟩ <;> simp only [e.symm, encodek, coe_some, some_dom] theorem merge {f g : α →. σ} (hf : Partrec f) (hg : Partrec g) (H : ∀ (a), ∀ x ∈ f a, ∀ y ∈ g a, x = y) : ∃ k : α →. σ, Partrec k ∧ ∀ a x, x ∈ k a ↔ x ∈ f a ∨ x ∈ g a :=
Mathlib/Computability/Halting.lean
77
103
/- Copyright (c) 2020 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Computability.Halting import Mathlib.Computability.TuringMachine import Mathlib.Data.Num.Lemmas import Mathlib.Tactic.DeriveFintype import Mathlib.Computability.TMConfig /-! # Modelling partial recursive functions using Turing machines The files `TMConfig` and `TMToPartrec` define a simplified basis for partial recursive functions, and a `Turing.TM2` model Turing machine for evaluating these functions. This amounts to a constructive proof that every `Partrec` function can be evaluated by a Turing machine. ## Main definitions * `PartrecToTM2.tr`: A TM2 turing machine which can evaluate `code` programs -/ open List (Vector) open Function (update) open Relation namespace Turing /-! ## Simulating sequentialized partial recursive functions in TM2 At this point we have a sequential model of partial recursive functions: the `Cfg` type and `step : Cfg → Option Cfg` function from `TMConfig.lean`. The key feature of this model is that it does a finite amount of computation (in fact, an amount which is statically bounded by the size of the program) between each step, and no individual step can diverge (unlike the compositional semantics, where every sub-part of the computation is potentially divergent). So we can utilize the same techniques as in the other TM simulations in `Computability.TuringMachine` to prove that each step corresponds to a finite number of steps in a lower level model. (We don't prove it here, but in anticipation of the complexity class P, the simulation is actually polynomial-time as well.) The target model is `Turing.TM2`, which has a fixed finite set of stacks, a bit of local storage, with programs selected from a potentially infinite (but finitely accessible) set of program positions, or labels `Λ`, each of which executes a finite sequence of basic stack commands. For this program we will need four stacks, each on an alphabet `Γ'` like so: inductive Γ' | consₗ | cons | bit0 | bit1 We represent a number as a bit sequence, lists of numbers by putting `cons` after each element, and lists of lists of natural numbers by putting `consₗ` after each list. For example: 0 ~> [] 1 ~> [bit1] 6 ~> [bit0, bit1, bit1] [1, 2] ~> [bit1, cons, bit0, bit1, cons] [[], [1, 2]] ~> [consₗ, bit1, cons, bit0, bit1, cons, consₗ] The four stacks are `main`, `rev`, `aux`, `stack`. In normal mode, `main` contains the input to the current program (a `List ℕ`) and `stack` contains data (a `List (List ℕ)`) associated to the current continuation, and in `ret` mode `main` contains the value that is being passed to the continuation and `stack` contains the data for the continuation. The `rev` and `aux` stacks are usually empty; `rev` is used to store reversed data when e.g. moving a value from one stack to another, while `aux` is used as a temporary for a `main`/`stack` swap that happens during `cons₁` evaluation. The only local store we need is `Option Γ'`, which stores the result of the last pop operation. (Most of our working data are natural numbers, which are too large to fit in the local store.) The continuations from the previous section are data-carrying, containing all the values that have been computed and are awaiting other arguments. In order to have only a finite number of continuations appear in the program so that they can be used in machine states, we separate the data part (anything with type `List ℕ`) from the `Cont` type, producing a `Cont'` type that lacks this information. The data is kept on the `stack` stack. Because we want to have subroutines for e.g. moving an entire stack to another place, we use an infinite inductive type `Λ'` so that we can execute a program and then return to do something else without having to define too many different kinds of intermediate states. (We must nevertheless prove that only finitely many labels are accessible.) The labels are: * `move p k₁ k₂ q`: move elements from stack `k₁` to `k₂` while `p` holds of the value being moved. The last element, that fails `p`, is placed in neither stack but left in the local store. At the end of the operation, `k₂` will have the elements of `k₁` in reverse order. Then do `q`. * `clear p k q`: delete elements from stack `k` until `p` is true. Like `move`, the last element is left in the local storage. Then do `q`. * `copy q`: Move all elements from `rev` to both `main` and `stack` (in reverse order), then do `q`. That is, it takes `(a, b, c, d)` to `(b.reverse ++ a, [], c, b.reverse ++ d)`. * `push k f q`: push `f s`, where `s` is the local store, to stack `k`, then do `q`. This is a duplicate of the `push` instruction that is part of the TM2 model, but by having a subroutine just for this purpose we can build up programs to execute inside a `goto` statement, where we have the flexibility to be general recursive. * `read (f : Option Γ' → Λ')`: go to state `f s` where `s` is the local store. Again this is only here for convenience. * `succ q`: perform a successor operation. Assuming `[n]` is encoded on `main` before, `[n+1]` will be on main after. This implements successor for binary natural numbers. * `pred q₁ q₂`: perform a predecessor operation or `case` statement. If `[]` is encoded on `main` before, then we transition to `q₁` with `[]` on main; if `(0 :: v)` is on `main` before then `v` will be on `main` after and we transition to `q₁`; and if `(n+1 :: v)` is on `main` before then `n :: v` will be on `main` after and we transition to `q₂`. * `ret k`: call continuation `k`. Each continuation has its own interpretation of the data in `stack` and sets up the data for the next continuation. * `ret (cons₁ fs k)`: `v :: KData` on `stack` and `ns` on `main`, and the next step expects `v` on `main` and `ns :: KData` on `stack`. So we have to do a little dance here with six reverse-moves using the `aux` stack to perform a three-point swap, each of which involves two reversals. * `ret (cons₂ k)`: `ns :: KData` is on `stack` and `v` is on `main`, and we have to put `ns.headI :: v` on `main` and `KData` on `stack`. This is done using the `head` subroutine. * `ret (fix f k)`: This stores no data, so we just check if `main` starts with `0` and if so, remove it and call `k`, otherwise `clear` the first value and call `f`. * `ret halt`: the stack is empty, and `main` has the output. Do nothing and halt. In addition to these basic states, we define some additional subroutines that are used in the above: * `push'`, `peek'`, `pop'` are special versions of the builtins that use the local store to supply inputs and outputs. * `unrev`: special case `move false rev main` to move everything from `rev` back to `main`. Used as a cleanup operation in several functions. * `moveExcl p k₁ k₂ q`: same as `move` but pushes the last value read back onto the source stack. * `move₂ p k₁ k₂ q`: double `move`, so that the result comes out in the right order at the target stack. Implemented as `moveExcl p k rev; move false rev k₂`. Assumes that neither `k₁` nor `k₂` is `rev` and `rev` is initially empty. * `head k q`: get the first natural number from stack `k` and reverse-move it to `rev`, then clear the rest of the list at `k` and then `unrev` to reverse-move the head value to `main`. This is used with `k = main` to implement regular `head`, i.e. if `v` is on `main` before then `[v.headI]` will be on `main` after; and also with `k = stack` for the `cons` operation, which has `v` on `main` and `ns :: KData` on `stack`, and results in `KData` on `stack` and `ns.headI :: v` on `main`. * `trNormal` is the main entry point, defining states that perform a given `code` computation. It mostly just dispatches to functions written above. The main theorem of this section is `tr_eval`, which asserts that for each that for each code `c`, the state `init c v` steps to `halt v'` in finitely many steps if and only if `Code.eval c v = some v'`. -/ namespace PartrecToTM2 section open ToPartrec /-- The alphabet for the stacks in the program. `bit0` and `bit1` are used to represent `ℕ` values as lists of binary digits, `cons` is used to separate `List ℕ` values, and `consₗ` is used to separate `List (List ℕ)` values. See the section documentation. -/ inductive Γ' | consₗ | cons | bit0 | bit1 deriving DecidableEq, Inhabited, Fintype /-- The four stacks used by the program. `main` is used to store the input value in `trNormal` mode and the output value in `Λ'.ret` mode, while `stack` is used to keep all the data for the continuations. `rev` is used to store reversed lists when transferring values between stacks, and `aux` is only used once in `cons₁`. See the section documentation. -/ inductive K' | main | rev | aux | stack deriving DecidableEq, Inhabited open K' /-- Continuations as in `ToPartrec.Cont` but with the data removed. This is done because we want the set of all continuations in the program to be finite (so that it can ultimately be encoded into the finite state machine of a Turing machine), but a continuation can handle a potentially infinite number of data values during execution. -/ inductive Cont' | halt | cons₁ : Code → Cont' → Cont' | cons₂ : Cont' → Cont' | comp : Code → Cont' → Cont' | fix : Code → Cont' → Cont' deriving DecidableEq, Inhabited /-- The set of program positions. We make extensive use of inductive types here to let us describe "subroutines"; for example `clear p k q` is a program that clears stack `k`, then does `q` where `q` is another label. In order to prevent this from resulting in an infinite number of distinct accessible states, we are careful to be non-recursive (although loops are okay). See the section documentation for a description of all the programs. -/ inductive Λ' | move (p : Γ' → Bool) (k₁ k₂ : K') (q : Λ') | clear (p : Γ' → Bool) (k : K') (q : Λ') | copy (q : Λ') | push (k : K') (s : Option Γ' → Option Γ') (q : Λ') | read (f : Option Γ' → Λ') | succ (q : Λ') | pred (q₁ q₂ : Λ') | ret (k : Cont') compile_inductive% Code compile_inductive% Cont' compile_inductive% K' compile_inductive% Λ' instance Λ'.instInhabited : Inhabited Λ' := ⟨Λ'.ret Cont'.halt⟩ instance Λ'.instDecidableEq : DecidableEq Λ' := fun a b => by induction a generalizing b <;> cases b <;> first | apply Decidable.isFalse; rintro ⟨⟨⟩⟩; done | exact decidable_of_iff' _ (by simp [funext_iff]; rfl) /-- The type of TM2 statements used by this machine. -/ def Stmt' := TM2.Stmt (fun _ : K' => Γ') Λ' (Option Γ') deriving Inhabited /-- The type of TM2 configurations used by this machine. -/ def Cfg' := TM2.Cfg (fun _ : K' => Γ') Λ' (Option Γ') deriving Inhabited open TM2.Stmt /-- A predicate that detects the end of a natural number, either `Γ'.cons` or `Γ'.consₗ` (or implicitly the end of the list), for use in predicate-taking functions like `move` and `clear`. -/ @[simp] def natEnd : Γ' → Bool | Γ'.consₗ => true | Γ'.cons => true | _ => false attribute [nolint simpNF] natEnd.eq_3 /-- Pop a value from the stack and place the result in local store. -/ @[simp] def pop' (k : K') : Stmt' → Stmt' := pop k fun _ v => v /-- Peek a value from the stack and place the result in local store. -/ @[simp] def peek' (k : K') : Stmt' → Stmt' := peek k fun _ v => v /-- Push the value in the local store to the given stack. -/ @[simp] def push' (k : K') : Stmt' → Stmt' := push k fun x => x.iget /-- Move everything from the `rev` stack to the `main` stack (reversed). -/ def unrev := Λ'.move (fun _ => false) rev main /-- Move elements from `k₁` to `k₂` while `p` holds, with the last element being left on `k₁`. -/ def moveExcl (p k₁ k₂ q) := Λ'.move p k₁ k₂ <| Λ'.push k₁ id q /-- Move elements from `k₁` to `k₂` without reversion, by performing a double move via the `rev` stack. -/ def move₂ (p k₁ k₂ q) := moveExcl p k₁ rev <| Λ'.move (fun _ => false) rev k₂ q /-- Assuming `trList v` is on the front of stack `k`, remove it, and push `v.headI` onto `main`. See the section documentation. -/ def head (k : K') (q : Λ') : Λ' := Λ'.move natEnd k rev <| (Λ'.push rev fun _ => some Γ'.cons) <| Λ'.read fun s => (if s = some Γ'.consₗ then id else Λ'.clear (fun x => x = Γ'.consₗ) k) <| unrev q /-- The program that evaluates code `c` with continuation `k`. This expects an initial state where `trList v` is on `main`, `trContStack k` is on `stack`, and `aux` and `rev` are empty. See the section documentation for details. -/ @[simp] def trNormal : Code → Cont' → Λ' | Code.zero', k => (Λ'.push main fun _ => some Γ'.cons) <| Λ'.ret k | Code.succ, k => head main <| Λ'.succ <| Λ'.ret k | Code.tail, k => Λ'.clear natEnd main <| Λ'.ret k | Code.cons f fs, k => (Λ'.push stack fun _ => some Γ'.consₗ) <| Λ'.move (fun _ => false) main rev <| Λ'.copy <| trNormal f (Cont'.cons₁ fs k) | Code.comp f g, k => trNormal g (Cont'.comp f k) | Code.case f g, k => Λ'.pred (trNormal f k) (trNormal g k) | Code.fix f, k => trNormal f (Cont'.fix f k) /-- The main program. See the section documentation for details. -/ def tr : Λ' → Stmt' | Λ'.move p k₁ k₂ q => pop' k₁ <| branch (fun s => s.elim true p) (goto fun _ => q) (push' k₂ <| goto fun _ => Λ'.move p k₁ k₂ q) | Λ'.push k f q => branch (fun s => (f s).isSome) ((push k fun s => (f s).iget) <| goto fun _ => q) (goto fun _ => q) | Λ'.read q => goto q | Λ'.clear p k q => pop' k <| branch (fun s => s.elim true p) (goto fun _ => q) (goto fun _ => Λ'.clear p k q) | Λ'.copy q => pop' rev <| branch Option.isSome (push' main <| push' stack <| goto fun _ => Λ'.copy q) (goto fun _ => q) | Λ'.succ q => pop' main <| branch (fun s => s = some Γ'.bit1) ((push rev fun _ => Γ'.bit0) <| goto fun _ => Λ'.succ q) <| branch (fun s => s = some Γ'.cons) ((push main fun _ => Γ'.cons) <| (push main fun _ => Γ'.bit1) <| goto fun _ => unrev q) ((push main fun _ => Γ'.bit1) <| goto fun _ => unrev q) | Λ'.pred q₁ q₂ => pop' main <| branch (fun s => s = some Γ'.bit0) ((push rev fun _ => Γ'.bit1) <| goto fun _ => Λ'.pred q₁ q₂) <| branch (fun s => natEnd s.iget) (goto fun _ => q₁) (peek' main <| branch (fun s => natEnd s.iget) (goto fun _ => unrev q₂) ((push rev fun _ => Γ'.bit0) <| goto fun _ => unrev q₂)) | Λ'.ret (Cont'.cons₁ fs k) => goto fun _ => move₂ (fun _ => false) main aux <| move₂ (fun s => s = Γ'.consₗ) stack main <| move₂ (fun _ => false) aux stack <| trNormal fs (Cont'.cons₂ k) | Λ'.ret (Cont'.cons₂ k) => goto fun _ => head stack <| Λ'.ret k | Λ'.ret (Cont'.comp f k) => goto fun _ => trNormal f k | Λ'.ret (Cont'.fix f k) => pop' main <| goto fun s => cond (natEnd s.iget) (Λ'.ret k) <| Λ'.clear natEnd main <| trNormal f (Cont'.fix f k) | Λ'.ret Cont'.halt => (load fun _ => none) <| halt @[simp] theorem tr_move (p k₁ k₂ q) : tr (Λ'.move p k₁ k₂ q) = pop' k₁ (branch (fun s => s.elim true p) (goto fun _ => q) (push' k₂ <| goto fun _ => Λ'.move p k₁ k₂ q)) := rfl @[simp] theorem tr_push (k f q) : tr (Λ'.push k f q) = branch (fun s => (f s).isSome) ((push k fun s => (f s).iget) <| goto fun _ => q) (goto fun _ => q) := rfl @[simp] theorem tr_read (q) : tr (Λ'.read q) = goto q := rfl @[simp] theorem tr_clear (p k q) : tr (Λ'.clear p k q) = pop' k (branch (fun s => s.elim true p) (goto fun _ => q) (goto fun _ => Λ'.clear p k q)) := rfl @[simp] theorem tr_copy (q) : tr (Λ'.copy q) = pop' rev (branch Option.isSome (push' main <| push' stack <| goto fun _ => Λ'.copy q) (goto fun _ => q)) := rfl @[simp] theorem tr_succ (q) : tr (Λ'.succ q) = pop' main (branch (fun s => s = some Γ'.bit1) ((push rev fun _ => Γ'.bit0) <| goto fun _ => Λ'.succ q) <| branch (fun s => s = some Γ'.cons) ((push main fun _ => Γ'.cons) <| (push main fun _ => Γ'.bit1) <| goto fun _ => unrev q) ((push main fun _ => Γ'.bit1) <| goto fun _ => unrev q)) := rfl @[simp] theorem tr_pred (q₁ q₂) : tr (Λ'.pred q₁ q₂) = pop' main (branch (fun s => s = some Γ'.bit0) ((push rev fun _ => Γ'.bit1) <| goto fun _ => Λ'.pred q₁ q₂) <| branch (fun s => natEnd s.iget) (goto fun _ => q₁) (peek' main <| branch (fun s => natEnd s.iget) (goto fun _ => unrev q₂) ((push rev fun _ => Γ'.bit0) <| goto fun _ => unrev q₂))) := rfl @[simp] theorem tr_ret_cons₁ (fs k) : tr (Λ'.ret (Cont'.cons₁ fs k)) = goto fun _ => move₂ (fun _ => false) main aux <| move₂ (fun s => s = Γ'.consₗ) stack main <| move₂ (fun _ => false) aux stack <| trNormal fs (Cont'.cons₂ k) := rfl @[simp] theorem tr_ret_cons₂ (k) : tr (Λ'.ret (Cont'.cons₂ k)) = goto fun _ => head stack <| Λ'.ret k := rfl @[simp] theorem tr_ret_comp (f k) : tr (Λ'.ret (Cont'.comp f k)) = goto fun _ => trNormal f k := rfl @[simp] theorem tr_ret_fix (f k) : tr (Λ'.ret (Cont'.fix f k)) = pop' main (goto fun s => cond (natEnd s.iget) (Λ'.ret k) <| Λ'.clear natEnd main <| trNormal f (Cont'.fix f k)) := rfl @[simp] theorem tr_ret_halt : tr (Λ'.ret Cont'.halt) = (load fun _ => none) halt := rfl /-- Translating a `Cont` continuation to a `Cont'` continuation simply entails dropping all the data. This data is instead encoded in `trContStack` in the configuration. -/ def trCont : Cont → Cont' | Cont.halt => Cont'.halt | Cont.cons₁ c _ k => Cont'.cons₁ c (trCont k) | Cont.cons₂ _ k => Cont'.cons₂ (trCont k) | Cont.comp c k => Cont'.comp c (trCont k) | Cont.fix c k => Cont'.fix c (trCont k) /-- We use `PosNum` to define the translation of binary natural numbers. A natural number is represented as a little-endian list of `bit0` and `bit1` elements: 1 = [bit1] 2 = [bit0, bit1] 3 = [bit1, bit1] 4 = [bit0, bit0, bit1] In particular, this representation guarantees no trailing `bit0`'s at the end of the list. -/ def trPosNum : PosNum → List Γ' | PosNum.one => [Γ'.bit1] | PosNum.bit0 n => Γ'.bit0 :: trPosNum n | PosNum.bit1 n => Γ'.bit1 :: trPosNum n /-- We use `Num` to define the translation of binary natural numbers. Positive numbers are translated using `trPosNum`, and `trNum 0 = []`. So there are never any trailing `bit0`'s in a translated `Num`. 0 = [] 1 = [bit1] 2 = [bit0, bit1] 3 = [bit1, bit1] 4 = [bit0, bit0, bit1] -/ def trNum : Num → List Γ' | Num.zero => [] | Num.pos n => trPosNum n /-- Because we use binary encoding, we define `trNat` in terms of `trNum`, using `Num`, which are binary natural numbers. (We could also use `Nat.binaryRecOn`, but `Num` and `PosNum` make for easy inductions.) -/ def trNat (n : ℕ) : List Γ' := trNum n @[simp] theorem trNat_zero : trNat 0 = [] := by rw [trNat, Nat.cast_zero]; rfl theorem trNat_default : trNat default = [] := trNat_zero /-- Lists are translated with a `cons` after each encoded number. For example: [] = [] [0] = [cons] [1] = [bit1, cons] [6, 0] = [bit0, bit1, bit1, cons, cons] -/ @[simp] def trList : List ℕ → List Γ' | [] => [] | n::ns => trNat n ++ Γ'.cons :: trList ns /-- Lists of lists are translated with a `consₗ` after each encoded list. For example: [] = [] [[]] = [consₗ] [[], []] = [consₗ, consₗ] [[0]] = [cons, consₗ] [[1, 2], [0]] = [bit1, cons, bit0, bit1, cons, consₗ, cons, consₗ] -/ @[simp] def trLList : List (List ℕ) → List Γ' | [] => [] | l::ls => trList l ++ Γ'.consₗ :: trLList ls /-- The data part of a continuation is a list of lists, which is encoded on the `stack` stack using `trLList`. -/ @[simp] def contStack : Cont → List (List ℕ) | Cont.halt => [] | Cont.cons₁ _ ns k => ns :: contStack k | Cont.cons₂ ns k => ns :: contStack k | Cont.comp _ k => contStack k | Cont.fix _ k => contStack k /-- The data part of a continuation is a list of lists, which is encoded on the `stack` stack using `trLList`. -/ def trContStack (k : Cont) := trLList (contStack k) /-- This is the nondependent eliminator for `K'`, but we use it specifically here in order to represent the stack data as four lists rather than as a function `K' → List Γ'`, because this makes rewrites easier. The theorems `K'.elim_update_main` et. al. show how such a function is updated after an `update` to one of the components. -/ def K'.elim (a b c d : List Γ') : K' → List Γ' | K'.main => a | K'.rev => b | K'.aux => c | K'.stack => d -- The equation lemma of `elim` simplifies to `match` structures. theorem K'.elim_main (a b c d) : K'.elim a b c d K'.main = a := rfl theorem K'.elim_rev (a b c d) : K'.elim a b c d K'.rev = b := rfl theorem K'.elim_aux (a b c d) : K'.elim a b c d K'.aux = c := rfl theorem K'.elim_stack (a b c d) : K'.elim a b c d K'.stack = d := rfl attribute [simp] K'.elim @[simp] theorem K'.elim_update_main {a b c d a'} : update (K'.elim a b c d) main a' = K'.elim a' b c d := by funext x; cases x <;> rfl @[simp] theorem K'.elim_update_rev {a b c d b'} : update (K'.elim a b c d) rev b' = K'.elim a b' c d := by funext x; cases x <;> rfl @[simp] theorem K'.elim_update_aux {a b c d c'} : update (K'.elim a b c d) aux c' = K'.elim a b c' d := by funext x; cases x <;> rfl @[simp] theorem K'.elim_update_stack {a b c d d'} : update (K'.elim a b c d) stack d' = K'.elim a b c d' := by funext x; cases x <;> rfl /-- The halting state corresponding to a `List ℕ` output value. -/ def halt (v : List ℕ) : Cfg' := ⟨none, none, K'.elim (trList v) [] [] []⟩ /-- The `Cfg` states map to `Cfg'` states almost one to one, except that in normal operation the local store contains an arbitrary garbage value. To make the final theorem cleaner we explicitly clear it in the halt state so that there is exactly one configuration corresponding to output `v`. -/ def TrCfg : Cfg → Cfg' → Prop | Cfg.ret k v, c' => ∃ s, c' = ⟨some (Λ'.ret (trCont k)), s, K'.elim (trList v) [] [] (trContStack k)⟩ | Cfg.halt v, c' => c' = halt v /-- This could be a general list definition, but it is also somewhat specialized to this application. `splitAtPred p L` will search `L` for the first element satisfying `p`. If it is found, say `L = l₁ ++ a :: l₂` where `a` satisfies `p` but `l₁` does not, then it returns `(l₁, some a, l₂)`. Otherwise, if there is no such element, it returns `(L, none, [])`. -/ def splitAtPred {α} (p : α → Bool) : List α → List α × Option α × List α | [] => ([], none, []) | a :: as => cond (p a) ([], some a, as) <| let ⟨l₁, o, l₂⟩ := splitAtPred p as ⟨a::l₁, o, l₂⟩ theorem splitAtPred_eq {α} (p : α → Bool) : ∀ L l₁ o l₂, (∀ x ∈ l₁, p x = false) → Option.elim' (L = l₁ ∧ l₂ = []) (fun a => p a = true ∧ L = l₁ ++ a::l₂) o → splitAtPred p L = (l₁, o, l₂) | [], _, none, _, _, ⟨rfl, rfl⟩ => rfl | [], l₁, some o, l₂, _, ⟨_, h₃⟩ => by simp at h₃ | a :: L, l₁, o, l₂, h₁, h₂ => by rw [splitAtPred] have IH := splitAtPred_eq p L rcases o with - | o · rcases l₁ with - | ⟨a', l₁⟩ <;> rcases h₂ with ⟨⟨⟩, rfl⟩ rw [h₁ a (List.Mem.head _), cond, IH L none [] _ ⟨rfl, rfl⟩] exact fun x h => h₁ x (List.Mem.tail _ h) · rcases l₁ with - | ⟨a', l₁⟩ <;> rcases h₂ with ⟨h₂, ⟨⟩⟩ · rw [h₂, cond] rw [h₁ a (List.Mem.head _), cond, IH l₁ (some o) l₂ _ ⟨h₂, _⟩] <;> try rfl exact fun x h => h₁ x (List.Mem.tail _ h) theorem splitAtPred_false {α} (L : List α) : splitAtPred (fun _ => false) L = (L, none, []) := splitAtPred_eq _ _ _ _ _ (fun _ _ => rfl) ⟨rfl, rfl⟩ theorem move_ok {p k₁ k₂ q s L₁ o L₂} {S : K' → List Γ'} (h₁ : k₁ ≠ k₂) (e : splitAtPred p (S k₁) = (L₁, o, L₂)) : Reaches₁ (TM2.step tr) ⟨some (Λ'.move p k₁ k₂ q), s, S⟩ ⟨some q, o, update (update S k₁ L₂) k₂ (L₁.reverseAux (S k₂))⟩ := by induction' L₁ with a L₁ IH generalizing S s · rw [(_ : [].reverseAux _ = _), Function.update_eq_self] swap · rw [Function.update_of_ne h₁.symm, List.reverseAux_nil] refine TransGen.head' rfl ?_ rw [tr]; simp only [pop', TM2.stepAux] revert e; rcases S k₁ with - | ⟨a, Sk⟩ <;> intro e · cases e
rfl simp only [splitAtPred, Option.elim, List.head?, List.tail_cons, Option.iget_some] at e ⊢ revert e; cases p a <;> intro e <;> simp only [cond_false, cond_true, Prod.mk.injEq, true_and, false_and, reduceCtorEq] at e ⊢ simp only [e] rfl · refine TransGen.head rfl ?_ rw [tr]; simp only [pop', Option.elim, TM2.stepAux, push']
Mathlib/Computability/TMToPartrec.lean
568
575
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import Mathlib.Order.PropInstances import Mathlib.Order.GaloisConnection.Defs /-! # Heyting algebras This file defines Heyting, co-Heyting and bi-Heyting algebras. A Heyting algebra is a bounded distributive lattice with an implication operation `⇨` such that `a ≤ b ⇨ c ↔ a ⊓ b ≤ c`. It also comes with a pseudo-complement `ᶜ`, such that `aᶜ = a ⇨ ⊥`. Co-Heyting algebras are dual to Heyting algebras. They have a difference `\` and a negation `¬` such that `a \ b ≤ c ↔ a ≤ b ⊔ c` and `¬a = ⊤ \ a`. Bi-Heyting algebras are Heyting algebras that are also co-Heyting algebras. From a logic standpoint, Heyting algebras precisely model intuitionistic logic, whereas boolean algebras model classical logic. Heyting algebras are the order theoretic equivalent of cartesian-closed categories. ## Main declarations * `GeneralizedHeytingAlgebra`: Heyting algebra without a top element (nor negation). * `GeneralizedCoheytingAlgebra`: Co-Heyting algebra without a bottom element (nor complement). * `HeytingAlgebra`: Heyting algebra. * `CoheytingAlgebra`: Co-Heyting algebra. * `BiheytingAlgebra`: bi-Heyting algebra. ## References * [Francis Borceux, *Handbook of Categorical Algebra III*][borceux-vol3] ## Tags Heyting, Brouwer, algebra, implication, negation, intuitionistic -/ assert_not_exists RelIso open Function OrderDual universe u variable {ι α β : Type*} /-! ### Notation -/ section variable (α β) instance Prod.instHImp [HImp α] [HImp β] : HImp (α × β) := ⟨fun a b => (a.1 ⇨ b.1, a.2 ⇨ b.2)⟩ instance Prod.instHNot [HNot α] [HNot β] : HNot (α × β) := ⟨fun a => (¬a.1, ¬a.2)⟩ instance Prod.instSDiff [SDiff α] [SDiff β] : SDiff (α × β) := ⟨fun a b => (a.1 \ b.1, a.2 \ b.2)⟩ instance Prod.instHasCompl [HasCompl α] [HasCompl β] : HasCompl (α × β) := ⟨fun a => (a.1ᶜ, a.2ᶜ)⟩ end @[simp] theorem fst_himp [HImp α] [HImp β] (a b : α × β) : (a ⇨ b).1 = a.1 ⇨ b.1 := rfl @[simp] theorem snd_himp [HImp α] [HImp β] (a b : α × β) : (a ⇨ b).2 = a.2 ⇨ b.2 := rfl @[simp] theorem fst_hnot [HNot α] [HNot β] (a : α × β) : (¬a).1 = ¬a.1 := rfl @[simp] theorem snd_hnot [HNot α] [HNot β] (a : α × β) : (¬a).2 = ¬a.2 := rfl @[simp] theorem fst_sdiff [SDiff α] [SDiff β] (a b : α × β) : (a \ b).1 = a.1 \ b.1 := rfl @[simp] theorem snd_sdiff [SDiff α] [SDiff β] (a b : α × β) : (a \ b).2 = a.2 \ b.2 := rfl @[simp] theorem fst_compl [HasCompl α] [HasCompl β] (a : α × β) : aᶜ.1 = a.1ᶜ := rfl @[simp] theorem snd_compl [HasCompl α] [HasCompl β] (a : α × β) : aᶜ.2 = a.2ᶜ := rfl namespace Pi variable {π : ι → Type*} instance [∀ i, HImp (π i)] : HImp (∀ i, π i) := ⟨fun a b i => a i ⇨ b i⟩ instance [∀ i, HNot (π i)] : HNot (∀ i, π i) := ⟨fun a i => ¬a i⟩ theorem himp_def [∀ i, HImp (π i)] (a b : ∀ i, π i) : a ⇨ b = fun i => a i ⇨ b i := rfl theorem hnot_def [∀ i, HNot (π i)] (a : ∀ i, π i) : ¬a = fun i => ¬a i := rfl @[simp] theorem himp_apply [∀ i, HImp (π i)] (a b : ∀ i, π i) (i : ι) : (a ⇨ b) i = a i ⇨ b i := rfl @[simp] theorem hnot_apply [∀ i, HNot (π i)] (a : ∀ i, π i) (i : ι) : (¬a) i = ¬a i := rfl end Pi /-- A generalized Heyting algebra is a lattice with an additional binary operation `⇨` called Heyting implication such that `(a ⇨ ·)` is right adjoint to `(a ⊓ ·)`. This generalizes `HeytingAlgebra` by not requiring a bottom element. -/ class GeneralizedHeytingAlgebra (α : Type*) extends Lattice α, OrderTop α, HImp α where /-- `(a ⇨ ·)` is right adjoint to `(a ⊓ ·)` -/ le_himp_iff (a b c : α) : a ≤ b ⇨ c ↔ a ⊓ b ≤ c /-- A generalized co-Heyting algebra is a lattice with an additional binary difference operation `\` such that `(· \ a)` is left adjoint to `(· ⊔ a)`. This generalizes `CoheytingAlgebra` by not requiring a top element. -/ class GeneralizedCoheytingAlgebra (α : Type*) extends Lattice α, OrderBot α, SDiff α where /-- `(· \ a)` is left adjoint to `(· ⊔ a)` -/ sdiff_le_iff (a b c : α) : a \ b ≤ c ↔ a ≤ b ⊔ c /-- A Heyting algebra is a bounded lattice with an additional binary operation `⇨` called Heyting implication such that `(a ⇨ ·)` is right adjoint to `(a ⊓ ·)`. -/ class HeytingAlgebra (α : Type*) extends GeneralizedHeytingAlgebra α, OrderBot α, HasCompl α where /-- `aᶜ` is defined as `a ⇨ ⊥` -/ himp_bot (a : α) : a ⇨ ⊥ = aᶜ /-- A co-Heyting algebra is a bounded lattice with an additional binary difference operation `\` such that `(· \ a)` is left adjoint to `(· ⊔ a)`. -/ class CoheytingAlgebra (α : Type*) extends GeneralizedCoheytingAlgebra α, OrderTop α, HNot α where /-- `⊤ \ a` is `¬a` -/ top_sdiff (a : α) : ⊤ \ a = ¬a /-- A bi-Heyting algebra is a Heyting algebra that is also a co-Heyting algebra. -/ class BiheytingAlgebra (α : Type*) extends HeytingAlgebra α, SDiff α, HNot α where /-- `(· \ a)` is left adjoint to `(· ⊔ a)` -/ sdiff_le_iff (a b c : α) : a \ b ≤ c ↔ a ≤ b ⊔ c /-- `⊤ \ a` is `¬a` -/ top_sdiff (a : α) : ⊤ \ a = ¬a -- See note [lower instance priority] attribute [instance 100] GeneralizedHeytingAlgebra.toOrderTop attribute [instance 100] GeneralizedCoheytingAlgebra.toOrderBot -- See note [lower instance priority] instance (priority := 100) HeytingAlgebra.toBoundedOrder [HeytingAlgebra α] : BoundedOrder α := { bot_le := ‹HeytingAlgebra α›.bot_le } -- See note [lower instance priority] instance (priority := 100) CoheytingAlgebra.toBoundedOrder [CoheytingAlgebra α] : BoundedOrder α := { ‹CoheytingAlgebra α› with } -- See note [lower instance priority] instance (priority := 100) BiheytingAlgebra.toCoheytingAlgebra [BiheytingAlgebra α] : CoheytingAlgebra α := { ‹BiheytingAlgebra α› with } -- See note [reducible non-instances] /-- Construct a Heyting algebra from the lattice structure and Heyting implication alone. -/ abbrev HeytingAlgebra.ofHImp [DistribLattice α] [BoundedOrder α] (himp : α → α → α) (le_himp_iff : ∀ a b c, a ≤ himp b c ↔ a ⊓ b ≤ c) : HeytingAlgebra α := { ‹DistribLattice α›, ‹BoundedOrder α› with himp, compl := fun a => himp a ⊥, le_himp_iff, himp_bot := fun _ => rfl } -- See note [reducible non-instances] /-- Construct a Heyting algebra from the lattice structure and complement operator alone. -/ abbrev HeytingAlgebra.ofCompl [DistribLattice α] [BoundedOrder α] (compl : α → α) (le_himp_iff : ∀ a b c, a ≤ compl b ⊔ c ↔ a ⊓ b ≤ c) : HeytingAlgebra α where himp := (compl · ⊔ ·) compl := compl le_himp_iff := le_himp_iff himp_bot _ := sup_bot_eq _ -- See note [reducible non-instances] /-- Construct a co-Heyting algebra from the lattice structure and the difference alone. -/ abbrev CoheytingAlgebra.ofSDiff [DistribLattice α] [BoundedOrder α] (sdiff : α → α → α) (sdiff_le_iff : ∀ a b c, sdiff a b ≤ c ↔ a ≤ b ⊔ c) : CoheytingAlgebra α := { ‹DistribLattice α›, ‹BoundedOrder α› with sdiff, hnot := fun a => sdiff ⊤ a, sdiff_le_iff, top_sdiff := fun _ => rfl } -- See note [reducible non-instances] /-- Construct a co-Heyting algebra from the difference and Heyting negation alone. -/ abbrev CoheytingAlgebra.ofHNot [DistribLattice α] [BoundedOrder α] (hnot : α → α) (sdiff_le_iff : ∀ a b c, a ⊓ hnot b ≤ c ↔ a ≤ b ⊔ c) : CoheytingAlgebra α where sdiff a b := a ⊓ hnot b hnot := hnot sdiff_le_iff := sdiff_le_iff top_sdiff _ := top_inf_eq _ /-! In this section, we'll give interpretations of these results in the Heyting algebra model of intuitionistic logic,- where `≤` can be interpreted as "validates", `⇨` as "implies", `⊓` as "and", `⊔` as "or", `⊥` as "false" and `⊤` as "true". Note that we confuse `→` and `⊢` because those are the same in this logic. See also `Prop.heytingAlgebra`. -/ section GeneralizedHeytingAlgebra variable [GeneralizedHeytingAlgebra α] {a b c d : α} /-- `p → q → r ↔ p ∧ q → r` -/ @[simp] theorem le_himp_iff : a ≤ b ⇨ c ↔ a ⊓ b ≤ c := GeneralizedHeytingAlgebra.le_himp_iff _ _ _ /-- `p → q → r ↔ q ∧ p → r` -/ theorem le_himp_iff' : a ≤ b ⇨ c ↔ b ⊓ a ≤ c := by rw [le_himp_iff, inf_comm] /-- `p → q → r ↔ q → p → r` -/ theorem le_himp_comm : a ≤ b ⇨ c ↔ b ≤ a ⇨ c := by rw [le_himp_iff, le_himp_iff'] /-- `p → q → p` -/ theorem le_himp : a ≤ b ⇨ a := le_himp_iff.2 inf_le_left /-- `p → p → q ↔ p → q` -/ theorem le_himp_iff_left : a ≤ a ⇨ b ↔ a ≤ b := by rw [le_himp_iff, inf_idem] /-- `p → p` -/ @[simp] theorem himp_self : a ⇨ a = ⊤ := top_le_iff.1 <| le_himp_iff.2 inf_le_right /-- `(p → q) ∧ p → q` -/ theorem himp_inf_le : (a ⇨ b) ⊓ a ≤ b := le_himp_iff.1 le_rfl /-- `p ∧ (p → q) → q` -/ theorem inf_himp_le : a ⊓ (a ⇨ b) ≤ b := by rw [inf_comm, ← le_himp_iff] /-- `p ∧ (p → q) ↔ p ∧ q` -/ @[simp] theorem inf_himp (a b : α) : a ⊓ (a ⇨ b) = a ⊓ b := le_antisymm (le_inf inf_le_left <| by rw [inf_comm, ← le_himp_iff]) <| inf_le_inf_left _ le_himp /-- `(p → q) ∧ p ↔ q ∧ p` -/ @[simp] theorem himp_inf_self (a b : α) : (a ⇨ b) ⊓ a = b ⊓ a := by rw [inf_comm, inf_himp, inf_comm] /-- The **deduction theorem** in the Heyting algebra model of intuitionistic logic: an implication holds iff the conclusion follows from the hypothesis. -/ @[simp] theorem himp_eq_top_iff : a ⇨ b = ⊤ ↔ a ≤ b := by rw [← top_le_iff, le_himp_iff, top_inf_eq] /-- `p → true`, `true → p ↔ p` -/ @[simp] theorem himp_top : a ⇨ ⊤ = ⊤ := himp_eq_top_iff.2 le_top @[simp] theorem top_himp : ⊤ ⇨ a = a := eq_of_forall_le_iff fun b => by rw [le_himp_iff, inf_top_eq] /-- `p → q → r ↔ p ∧ q → r` -/ theorem himp_himp (a b c : α) : a ⇨ b ⇨ c = a ⊓ b ⇨ c := eq_of_forall_le_iff fun d => by simp_rw [le_himp_iff, inf_assoc] /-- `(q → r) → (p → q) → q → r` -/ theorem himp_le_himp_himp_himp : b ⇨ c ≤ (a ⇨ b) ⇨ a ⇨ c := by rw [le_himp_iff, le_himp_iff, inf_assoc, himp_inf_self, ← inf_assoc, himp_inf_self, inf_assoc] exact inf_le_left @[simp] theorem himp_inf_himp_inf_le : (b ⇨ c) ⊓ (a ⇨ b) ⊓ a ≤ c := by simpa using @himp_le_himp_himp_himp /-- `p → q → r ↔ q → p → r` -/ theorem himp_left_comm (a b c : α) : a ⇨ b ⇨ c = b ⇨ a ⇨ c := by simp_rw [himp_himp, inf_comm] @[simp] theorem himp_idem : b ⇨ b ⇨ a = b ⇨ a := by rw [himp_himp, inf_idem] theorem himp_inf_distrib (a b c : α) : a ⇨ b ⊓ c = (a ⇨ b) ⊓ (a ⇨ c) := eq_of_forall_le_iff fun d => by simp_rw [le_himp_iff, le_inf_iff, le_himp_iff] theorem sup_himp_distrib (a b c : α) : a ⊔ b ⇨ c = (a ⇨ c) ⊓ (b ⇨ c) := eq_of_forall_le_iff fun d => by rw [le_inf_iff, le_himp_comm, sup_le_iff] simp_rw [le_himp_comm] theorem himp_le_himp_left (h : a ≤ b) : c ⇨ a ≤ c ⇨ b := le_himp_iff.2 <| himp_inf_le.trans h theorem himp_le_himp_right (h : a ≤ b) : b ⇨ c ≤ a ⇨ c := le_himp_iff.2 <| (inf_le_inf_left _ h).trans himp_inf_le theorem himp_le_himp (hab : a ≤ b) (hcd : c ≤ d) : b ⇨ c ≤ a ⇨ d := (himp_le_himp_right hab).trans <| himp_le_himp_left hcd @[simp] theorem sup_himp_self_left (a b : α) : a ⊔ b ⇨ a = b ⇨ a := by rw [sup_himp_distrib, himp_self, top_inf_eq] @[simp] theorem sup_himp_self_right (a b : α) : a ⊔ b ⇨ b = a ⇨ b := by rw [sup_himp_distrib, himp_self, inf_top_eq] theorem Codisjoint.himp_eq_right (h : Codisjoint a b) : b ⇨ a = a := by conv_rhs => rw [← @top_himp _ _ a] rw [← h.eq_top, sup_himp_self_left] theorem Codisjoint.himp_eq_left (h : Codisjoint a b) : a ⇨ b = b := h.symm.himp_eq_right theorem Codisjoint.himp_inf_cancel_right (h : Codisjoint a b) : a ⇨ a ⊓ b = b := by rw [himp_inf_distrib, himp_self, top_inf_eq, h.himp_eq_left] theorem Codisjoint.himp_inf_cancel_left (h : Codisjoint a b) : b ⇨ a ⊓ b = a := by rw [himp_inf_distrib, himp_self, inf_top_eq, h.himp_eq_right] /-- See `himp_le` for a stronger version in Boolean algebras. -/ theorem Codisjoint.himp_le_of_right_le (hac : Codisjoint a c) (hba : b ≤ a) : c ⇨ b ≤ a := (himp_le_himp_left hba).trans_eq hac.himp_eq_right theorem le_himp_himp : a ≤ (a ⇨ b) ⇨ b := le_himp_iff.2 inf_himp_le @[simp] lemma himp_eq_himp_iff : b ⇨ a = a ⇨ b ↔ a = b := by simp [le_antisymm_iff] lemma himp_ne_himp_iff : b ⇨ a ≠ a ⇨ b ↔ a ≠ b := himp_eq_himp_iff.not theorem himp_triangle (a b c : α) : (a ⇨ b) ⊓ (b ⇨ c) ≤ a ⇨ c := by rw [le_himp_iff, inf_right_comm, ← le_himp_iff] exact himp_inf_le.trans le_himp_himp theorem himp_inf_himp_cancel (hba : b ≤ a) (hcb : c ≤ b) : (a ⇨ b) ⊓ (b ⇨ c) = a ⇨ c := (himp_triangle _ _ _).antisymm <| le_inf (himp_le_himp_left hcb) (himp_le_himp_right hba) theorem gc_inf_himp : GaloisConnection (a ⊓ ·) (a ⇨ ·) := fun _ _ ↦ Iff.symm le_himp_iff' -- See note [lower instance priority] instance (priority := 100) GeneralizedHeytingAlgebra.toDistribLattice : DistribLattice α := DistribLattice.ofInfSupLe fun a b c => by simp_rw [inf_comm a, ← le_himp_iff, sup_le_iff, le_himp_iff, ← sup_le_iff]; rfl instance OrderDual.instGeneralizedCoheytingAlgebra : GeneralizedCoheytingAlgebra αᵒᵈ where sdiff a b := toDual (ofDual b ⇨ ofDual a) sdiff_le_iff a b c := by rw [sup_comm]; exact le_himp_iff instance Prod.instGeneralizedHeytingAlgebra [GeneralizedHeytingAlgebra β] : GeneralizedHeytingAlgebra (α × β) where le_himp_iff _ _ _ := and_congr le_himp_iff le_himp_iff instance Pi.instGeneralizedHeytingAlgebra {α : ι → Type*} [∀ i, GeneralizedHeytingAlgebra (α i)] : GeneralizedHeytingAlgebra (∀ i, α i) where le_himp_iff i := by simp [le_def] end GeneralizedHeytingAlgebra section GeneralizedCoheytingAlgebra variable [GeneralizedCoheytingAlgebra α] {a b c d : α} @[simp] theorem sdiff_le_iff : a \ b ≤ c ↔ a ≤ b ⊔ c := GeneralizedCoheytingAlgebra.sdiff_le_iff _ _ _ theorem sdiff_le_iff' : a \ b ≤ c ↔ a ≤ c ⊔ b := by rw [sdiff_le_iff, sup_comm] theorem sdiff_le_comm : a \ b ≤ c ↔ a \ c ≤ b := by rw [sdiff_le_iff, sdiff_le_iff'] theorem sdiff_le : a \ b ≤ a := sdiff_le_iff.2 le_sup_right theorem Disjoint.disjoint_sdiff_left (h : Disjoint a b) : Disjoint (a \ c) b := h.mono_left sdiff_le theorem Disjoint.disjoint_sdiff_right (h : Disjoint a b) : Disjoint a (b \ c) := h.mono_right sdiff_le theorem sdiff_le_iff_left : a \ b ≤ b ↔ a ≤ b := by rw [sdiff_le_iff, sup_idem] @[simp] theorem sdiff_self : a \ a = ⊥ := le_bot_iff.1 <| sdiff_le_iff.2 le_sup_left theorem le_sup_sdiff : a ≤ b ⊔ a \ b := sdiff_le_iff.1 le_rfl theorem le_sdiff_sup : a ≤ a \ b ⊔ b := by rw [sup_comm, ← sdiff_le_iff] theorem sup_sdiff_left : a ⊔ a \ b = a := sup_of_le_left sdiff_le theorem sup_sdiff_right : a \ b ⊔ a = a := sup_of_le_right sdiff_le theorem inf_sdiff_left : a \ b ⊓ a = a \ b := inf_of_le_left sdiff_le theorem inf_sdiff_right : a ⊓ a \ b = a \ b := inf_of_le_right sdiff_le @[simp] theorem sup_sdiff_self (a b : α) : a ⊔ b \ a = a ⊔ b := le_antisymm (sup_le_sup_left sdiff_le _) (sup_le le_sup_left le_sup_sdiff) @[simp] theorem sdiff_sup_self (a b : α) : b \ a ⊔ a = b ⊔ a := by rw [sup_comm, sup_sdiff_self, sup_comm] alias sup_sdiff_self_left := sdiff_sup_self alias sup_sdiff_self_right := sup_sdiff_self theorem sup_sdiff_eq_sup (h : c ≤ a) : a ⊔ b \ c = a ⊔ b := sup_congr_left (sdiff_le.trans le_sup_right) <| le_sup_sdiff.trans <| sup_le_sup_right h _ -- cf. `Set.union_diff_cancel'` theorem sup_sdiff_cancel' (hab : a ≤ b) (hbc : b ≤ c) : b ⊔ c \ a = c := by rw [sup_sdiff_eq_sup hab, sup_of_le_right hbc] theorem sup_sdiff_cancel_right (h : a ≤ b) : a ⊔ b \ a = b := sup_sdiff_cancel' le_rfl h theorem sdiff_sup_cancel (h : b ≤ a) : a \ b ⊔ b = a := by rw [sup_comm, sup_sdiff_cancel_right h] theorem sup_le_of_le_sdiff_left (h : b ≤ c \ a) (hac : a ≤ c) : a ⊔ b ≤ c := sup_le hac <| h.trans sdiff_le theorem sup_le_of_le_sdiff_right (h : a ≤ c \ b) (hbc : b ≤ c) : a ⊔ b ≤ c := sup_le (h.trans sdiff_le) hbc @[simp] theorem sdiff_eq_bot_iff : a \ b = ⊥ ↔ a ≤ b := by rw [← le_bot_iff, sdiff_le_iff, sup_bot_eq] @[simp] theorem sdiff_bot : a \ ⊥ = a := eq_of_forall_ge_iff fun b => by rw [sdiff_le_iff, bot_sup_eq] @[simp] theorem bot_sdiff : ⊥ \ a = ⊥ := sdiff_eq_bot_iff.2 bot_le theorem sdiff_sdiff_sdiff_le_sdiff : (a \ b) \ (a \ c) ≤ c \ b := by rw [sdiff_le_iff, sdiff_le_iff, sup_left_comm, sup_sdiff_self, sup_left_comm, sdiff_sup_self, sup_left_comm] exact le_sup_left @[simp] theorem le_sup_sdiff_sup_sdiff : a ≤ b ⊔ (a \ c ⊔ c \ b) := by simpa using @sdiff_sdiff_sdiff_le_sdiff theorem sdiff_sdiff (a b c : α) : (a \ b) \ c = a \ (b ⊔ c) := eq_of_forall_ge_iff fun d => by simp_rw [sdiff_le_iff, sup_assoc] theorem sdiff_sdiff_left : (a \ b) \ c = a \ (b ⊔ c) := sdiff_sdiff _ _ _ theorem sdiff_right_comm (a b c : α) : (a \ b) \ c = (a \ c) \ b := by simp_rw [sdiff_sdiff, sup_comm] theorem sdiff_sdiff_comm : (a \ b) \ c = (a \ c) \ b := sdiff_right_comm _ _ _ @[simp] theorem sdiff_idem : (a \ b) \ b = a \ b := by rw [sdiff_sdiff_left, sup_idem] @[simp] theorem sdiff_sdiff_self : (a \ b) \ a = ⊥ := by rw [sdiff_sdiff_comm, sdiff_self, bot_sdiff] theorem sup_sdiff_distrib (a b c : α) : (a ⊔ b) \ c = a \ c ⊔ b \ c := eq_of_forall_ge_iff fun d => by simp_rw [sdiff_le_iff, sup_le_iff, sdiff_le_iff] theorem sdiff_inf_distrib (a b c : α) : a \ (b ⊓ c) = a \ b ⊔ a \ c := eq_of_forall_ge_iff fun d => by rw [sup_le_iff, sdiff_le_comm, le_inf_iff] simp_rw [sdiff_le_comm] theorem sup_sdiff : (a ⊔ b) \ c = a \ c ⊔ b \ c := sup_sdiff_distrib _ _ _ @[simp] theorem sup_sdiff_right_self : (a ⊔ b) \ b = a \ b := by rw [sup_sdiff, sdiff_self, sup_bot_eq] @[simp] theorem sup_sdiff_left_self : (a ⊔ b) \ a = b \ a := by rw [sup_comm, sup_sdiff_right_self] @[gcongr] theorem sdiff_le_sdiff_right (h : a ≤ b) : a \ c ≤ b \ c := sdiff_le_iff.2 <| h.trans <| le_sup_sdiff @[gcongr] theorem sdiff_le_sdiff_left (h : a ≤ b) : c \ b ≤ c \ a := sdiff_le_iff.2 <| le_sup_sdiff.trans <| sup_le_sup_right h _ @[gcongr] theorem sdiff_le_sdiff (hab : a ≤ b) (hcd : c ≤ d) : a \ d ≤ b \ c := (sdiff_le_sdiff_right hab).trans <| sdiff_le_sdiff_left hcd -- cf. `IsCompl.inf_sup` theorem sdiff_inf : a \ (b ⊓ c) = a \ b ⊔ a \ c := sdiff_inf_distrib _ _ _ @[simp] theorem sdiff_inf_self_left (a b : α) : a \ (a ⊓ b) = a \ b := by rw [sdiff_inf, sdiff_self, bot_sup_eq] @[simp] theorem sdiff_inf_self_right (a b : α) : b \ (a ⊓ b) = b \ a := by rw [sdiff_inf, sdiff_self, sup_bot_eq] theorem Disjoint.sdiff_eq_left (h : Disjoint a b) : a \ b = a := by conv_rhs => rw [← @sdiff_bot _ _ a] rw [← h.eq_bot, sdiff_inf_self_left]
Mathlib/Order/Heyting/Basic.lean
533
533
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Algebra.Ring.Associated import Mathlib.Algebra.Star.Unitary import Mathlib.RingTheory.PrincipalIdealDomain import Mathlib.Tactic.Ring import Mathlib.Algebra.EuclideanDomain.Int /-! # ℤ[√d] The ring of integers adjoined with a square root of `d : ℤ`. After defining the norm, we show that it is a linearly ordered commutative ring, as well as an integral domain. We provide the universal property, that ring homomorphisms `ℤ√d →+* R` correspond to choices of square roots of `d` in `R`. -/ /-- The ring of integers adjoined with a square root of `d`. These have the form `a + b √d` where `a b : ℤ`. The components are called `re` and `im` by analogy to the negative `d` case. -/ @[ext] structure Zsqrtd (d : ℤ) where /-- Component of the integer not multiplied by `√d` -/ re : ℤ /-- Component of the integer multiplied by `√d` -/ im : ℤ deriving DecidableEq @[inherit_doc] prefix:100 "ℤ√" => Zsqrtd namespace Zsqrtd section variable {d : ℤ} /-- Convert an integer to a `ℤ√d` -/ def ofInt (n : ℤ) : ℤ√d := ⟨n, 0⟩ theorem ofInt_re (n : ℤ) : (ofInt n : ℤ√d).re = n := rfl theorem ofInt_im (n : ℤ) : (ofInt n : ℤ√d).im = 0 := rfl /-- The zero of the ring -/ instance : Zero (ℤ√d) := ⟨ofInt 0⟩ @[simp] theorem zero_re : (0 : ℤ√d).re = 0 := rfl @[simp] theorem zero_im : (0 : ℤ√d).im = 0 := rfl instance : Inhabited (ℤ√d) := ⟨0⟩ /-- The one of the ring -/ instance : One (ℤ√d) := ⟨ofInt 1⟩ @[simp] theorem one_re : (1 : ℤ√d).re = 1 := rfl @[simp] theorem one_im : (1 : ℤ√d).im = 0 := rfl /-- The representative of `√d` in the ring -/ def sqrtd : ℤ√d := ⟨0, 1⟩ @[simp] theorem sqrtd_re : (sqrtd : ℤ√d).re = 0 := rfl @[simp] theorem sqrtd_im : (sqrtd : ℤ√d).im = 1 := rfl /-- Addition of elements of `ℤ√d` -/ instance : Add (ℤ√d) := ⟨fun z w => ⟨z.1 + w.1, z.2 + w.2⟩⟩ @[simp] theorem add_def (x y x' y' : ℤ) : (⟨x, y⟩ + ⟨x', y'⟩ : ℤ√d) = ⟨x + x', y + y'⟩ := rfl @[simp] theorem add_re (z w : ℤ√d) : (z + w).re = z.re + w.re := rfl @[simp] theorem add_im (z w : ℤ√d) : (z + w).im = z.im + w.im := rfl /-- Negation in `ℤ√d` -/ instance : Neg (ℤ√d) := ⟨fun z => ⟨-z.1, -z.2⟩⟩ @[simp] theorem neg_re (z : ℤ√d) : (-z).re = -z.re := rfl @[simp] theorem neg_im (z : ℤ√d) : (-z).im = -z.im := rfl /-- Multiplication in `ℤ√d` -/ instance : Mul (ℤ√d) := ⟨fun z w => ⟨z.1 * w.1 + d * z.2 * w.2, z.1 * w.2 + z.2 * w.1⟩⟩ @[simp] theorem mul_re (z w : ℤ√d) : (z * w).re = z.re * w.re + d * z.im * w.im := rfl @[simp] theorem mul_im (z w : ℤ√d) : (z * w).im = z.re * w.im + z.im * w.re := rfl instance addCommGroup : AddCommGroup (ℤ√d) := by refine { add := (· + ·) zero := (0 : ℤ√d) sub := fun a b => a + -b neg := Neg.neg nsmul := @nsmulRec (ℤ√d) ⟨0⟩ ⟨(· + ·)⟩ zsmul := @zsmulRec (ℤ√d) ⟨0⟩ ⟨(· + ·)⟩ ⟨Neg.neg⟩ (@nsmulRec (ℤ√d) ⟨0⟩ ⟨(· + ·)⟩) add_assoc := ?_ zero_add := ?_ add_zero := ?_ neg_add_cancel := ?_ add_comm := ?_ } <;> intros <;> ext <;> simp [add_comm, add_left_comm] @[simp] theorem sub_re (z w : ℤ√d) : (z - w).re = z.re - w.re := rfl @[simp] theorem sub_im (z w : ℤ√d) : (z - w).im = z.im - w.im := rfl instance addGroupWithOne : AddGroupWithOne (ℤ√d) := { Zsqrtd.addCommGroup with natCast := fun n => ofInt n intCast := ofInt one := 1 } instance commRing : CommRing (ℤ√d) := by refine { Zsqrtd.addGroupWithOne with mul := (· * ·) npow := @npowRec (ℤ√d) ⟨1⟩ ⟨(· * ·)⟩, add_comm := ?_ left_distrib := ?_ right_distrib := ?_ zero_mul := ?_ mul_zero := ?_ mul_assoc := ?_ one_mul := ?_ mul_one := ?_ mul_comm := ?_ } <;> intros <;> ext <;> simp <;> ring instance : AddMonoid (ℤ√d) := by infer_instance instance : Monoid (ℤ√d) := by infer_instance instance : CommMonoid (ℤ√d) := by infer_instance instance : CommSemigroup (ℤ√d) := by infer_instance instance : Semigroup (ℤ√d) := by infer_instance instance : AddCommSemigroup (ℤ√d) := by infer_instance instance : AddSemigroup (ℤ√d) := by infer_instance instance : CommSemiring (ℤ√d) := by infer_instance instance : Semiring (ℤ√d) := by infer_instance instance : Ring (ℤ√d) := by infer_instance instance : Distrib (ℤ√d) := by infer_instance /-- Conjugation in `ℤ√d`. The conjugate of `a + b √d` is `a - b √d`. -/ instance : Star (ℤ√d) where star z := ⟨z.1, -z.2⟩ @[simp] theorem star_mk (x y : ℤ) : star (⟨x, y⟩ : ℤ√d) = ⟨x, -y⟩ := rfl @[simp] theorem star_re (z : ℤ√d) : (star z).re = z.re := rfl @[simp] theorem star_im (z : ℤ√d) : (star z).im = -z.im := rfl instance : StarRing (ℤ√d) where star_involutive _ := Zsqrtd.ext rfl (neg_neg _) star_mul a b := by ext <;> simp <;> ring star_add _ _ := Zsqrtd.ext rfl (neg_add _ _) -- Porting note: proof was `by decide` instance nontrivial : Nontrivial (ℤ√d) := ⟨⟨0, 1, Zsqrtd.ext_iff.not.mpr (by simp)⟩⟩ @[simp] theorem natCast_re (n : ℕ) : (n : ℤ√d).re = n := rfl @[simp] theorem ofNat_re (n : ℕ) [n.AtLeastTwo] : (ofNat(n) : ℤ√d).re = n := rfl @[simp] theorem natCast_im (n : ℕ) : (n : ℤ√d).im = 0 := rfl @[simp] theorem ofNat_im (n : ℕ) [n.AtLeastTwo] : (ofNat(n) : ℤ√d).im = 0 := rfl theorem natCast_val (n : ℕ) : (n : ℤ√d) = ⟨n, 0⟩ := rfl @[simp] theorem intCast_re (n : ℤ) : (n : ℤ√d).re = n := by cases n <;> rfl @[simp] theorem intCast_im (n : ℤ) : (n : ℤ√d).im = 0 := by cases n <;> rfl theorem intCast_val (n : ℤ) : (n : ℤ√d) = ⟨n, 0⟩ := by ext <;> simp instance : CharZero (ℤ√d) where cast_injective m n := by simp [Zsqrtd.ext_iff] @[simp] theorem ofInt_eq_intCast (n : ℤ) : (ofInt n : ℤ√d) = n := by ext <;> simp [ofInt_re, ofInt_im] @[simp] theorem nsmul_val (n : ℕ) (x y : ℤ) : (n : ℤ√d) * ⟨x, y⟩ = ⟨n * x, n * y⟩ := by ext <;> simp @[simp] theorem smul_val (n x y : ℤ) : (n : ℤ√d) * ⟨x, y⟩ = ⟨n * x, n * y⟩ := by ext <;> simp theorem smul_re (a : ℤ) (b : ℤ√d) : (↑a * b).re = a * b.re := by simp theorem smul_im (a : ℤ) (b : ℤ√d) : (↑a * b).im = a * b.im := by simp @[simp] theorem muld_val (x y : ℤ) : sqrtd (d := d) * ⟨x, y⟩ = ⟨d * y, x⟩ := by ext <;> simp @[simp] theorem dmuld : sqrtd (d := d) * sqrtd (d := d) = d := by ext <;> simp @[simp] theorem smuld_val (n x y : ℤ) : sqrtd * (n : ℤ√d) * ⟨x, y⟩ = ⟨d * n * y, n * x⟩ := by ext <;> simp theorem decompose {x y : ℤ} : (⟨x, y⟩ : ℤ√d) = x + sqrtd (d := d) * y := by ext <;> simp theorem mul_star {x y : ℤ} : (⟨x, y⟩ * star ⟨x, y⟩ : ℤ√d) = x * x - d * y * y := by ext <;> simp [sub_eq_add_neg, mul_comm] theorem intCast_dvd (z : ℤ) (a : ℤ√d) : ↑z ∣ a ↔ z ∣ a.re ∧ z ∣ a.im := by constructor · rintro ⟨x, rfl⟩ simp only [add_zero, intCast_re, zero_mul, mul_im, dvd_mul_right, and_self_iff, mul_re, mul_zero, intCast_im] · rintro ⟨⟨r, hr⟩, ⟨i, hi⟩⟩ use ⟨r, i⟩ rw [smul_val, Zsqrtd.ext_iff] exact ⟨hr, hi⟩ @[simp, norm_cast] theorem intCast_dvd_intCast (a b : ℤ) : (a : ℤ√d) ∣ b ↔ a ∣ b := by rw [intCast_dvd] constructor · rintro ⟨hre, -⟩ rwa [intCast_re] at hre · rw [intCast_re, intCast_im] exact fun hc => ⟨hc, dvd_zero a⟩ protected theorem eq_of_smul_eq_smul_left {a : ℤ} {b c : ℤ√d} (ha : a ≠ 0) (h : ↑a * b = a * c) : b = c := by rw [Zsqrtd.ext_iff] at h ⊢ apply And.imp _ _ h <;> simpa only [smul_re, smul_im] using mul_left_cancel₀ ha section Gcd theorem gcd_eq_zero_iff (a : ℤ√d) : Int.gcd a.re a.im = 0 ↔ a = 0 := by simp only [Int.gcd_eq_zero_iff, Zsqrtd.ext_iff, eq_self_iff_true, zero_im, zero_re] theorem gcd_pos_iff (a : ℤ√d) : 0 < Int.gcd a.re a.im ↔ a ≠ 0 := pos_iff_ne_zero.trans <| not_congr a.gcd_eq_zero_iff theorem isCoprime_of_dvd_isCoprime {a b : ℤ√d} (hcoprime : IsCoprime a.re a.im) (hdvd : b ∣ a) : IsCoprime b.re b.im := by apply isCoprime_of_dvd · rintro ⟨hre, him⟩ obtain rfl : b = 0 := Zsqrtd.ext hre him rw [zero_dvd_iff] at hdvd simp [hdvd, zero_im, zero_re, not_isCoprime_zero_zero] at hcoprime · rintro z hz - hzdvdu hzdvdv apply hz obtain ⟨ha, hb⟩ : z ∣ a.re ∧ z ∣ a.im := by rw [← intCast_dvd] apply dvd_trans _ hdvd rw [intCast_dvd] exact ⟨hzdvdu, hzdvdv⟩ exact hcoprime.isUnit_of_dvd' ha hb @[deprecated (since := "2025-01-23")] alias coprime_of_dvd_coprime := isCoprime_of_dvd_isCoprime theorem exists_coprime_of_gcd_pos {a : ℤ√d} (hgcd : 0 < Int.gcd a.re a.im) : ∃ b : ℤ√d, a = ((Int.gcd a.re a.im : ℤ) : ℤ√d) * b ∧ IsCoprime b.re b.im := by obtain ⟨re, im, H1, Hre, Him⟩ := Int.exists_gcd_one hgcd rw [mul_comm] at Hre Him refine ⟨⟨re, im⟩, ?_, ?_⟩ · rw [smul_val, ← Hre, ← Him] · rw [Int.isCoprime_iff_gcd_eq_one, H1] end Gcd /-- Read `SqLe a c b d` as `a √c ≤ b √d` -/ def SqLe (a c b d : ℕ) : Prop := c * a * a ≤ d * b * b theorem sqLe_of_le {c d x y z w : ℕ} (xz : z ≤ x) (yw : y ≤ w) (xy : SqLe x c y d) : SqLe z c w d := le_trans (mul_le_mul (Nat.mul_le_mul_left _ xz) xz (Nat.zero_le _) (Nat.zero_le _)) <| le_trans xy (mul_le_mul (Nat.mul_le_mul_left _ yw) yw (Nat.zero_le _) (Nat.zero_le _)) theorem sqLe_add_mixed {c d x y z w : ℕ} (xy : SqLe x c y d) (zw : SqLe z c w d) : c * (x * z) ≤ d * (y * w) := Nat.mul_self_le_mul_self_iff.1 <| by simpa [mul_comm, mul_left_comm] using mul_le_mul xy zw (Nat.zero_le _) (Nat.zero_le _) theorem sqLe_add {c d x y z w : ℕ} (xy : SqLe x c y d) (zw : SqLe z c w d) : SqLe (x + z) c (y + w) d := by have xz := sqLe_add_mixed xy zw simp? [SqLe, mul_assoc] at xy zw says simp only [SqLe, mul_assoc] at xy zw simp [SqLe, mul_add, mul_comm, mul_left_comm, add_le_add, *] theorem sqLe_cancel {c d x y z w : ℕ} (zw : SqLe y d x c) (h : SqLe (x + z) c (y + w) d) : SqLe z c w d := by apply le_of_not_gt intro l refine not_le_of_gt ?_ h simp only [SqLe, mul_add, mul_comm, mul_left_comm, add_assoc, gt_iff_lt] have hm := sqLe_add_mixed zw (le_of_lt l) simp only [SqLe, mul_assoc, gt_iff_lt] at l zw exact lt_of_le_of_lt (add_le_add_right zw _) (add_lt_add_left (add_lt_add_of_le_of_lt hm (add_lt_add_of_le_of_lt hm l)) _) theorem sqLe_smul {c d x y : ℕ} (n : ℕ) (xy : SqLe x c y d) : SqLe (n * x) c (n * y) d := by simpa [SqLe, mul_left_comm, mul_assoc] using Nat.mul_le_mul_left (n * n) xy theorem sqLe_mul {d x y z w : ℕ} : (SqLe x 1 y d → SqLe z 1 w d → SqLe (x * w + y * z) d (x * z + d * y * w) 1) ∧ (SqLe x 1 y d → SqLe w d z 1 → SqLe (x * z + d * y * w) 1 (x * w + y * z) d) ∧ (SqLe y d x 1 → SqLe z 1 w d → SqLe (x * z + d * y * w) 1 (x * w + y * z) d) ∧ (SqLe y d x 1 → SqLe w d z 1 → SqLe (x * w + y * z) d (x * z + d * y * w) 1) := by refine ⟨?_, ?_, ?_, ?_⟩ <;> · intro xy zw have := Int.mul_nonneg (sub_nonneg_of_le (Int.ofNat_le_ofNat_of_le xy)) (sub_nonneg_of_le (Int.ofNat_le_ofNat_of_le zw)) refine Int.le_of_ofNat_le_ofNat (le_of_sub_nonneg ?_) convert this using 1 simp only [one_mul, Int.natCast_add, Int.natCast_mul] ring open Int in /-- "Generalized" `nonneg`. `nonnegg c d x y` means `a √c + b √d ≥ 0`; we are interested in the case `c = 1` but this is more symmetric -/ def Nonnegg (c d : ℕ) : ℤ → ℤ → Prop | (a : ℕ), (b : ℕ) => True | (a : ℕ), -[b+1] => SqLe (b + 1) c a d | -[a+1], (b : ℕ) => SqLe (a + 1) d b c | -[_+1], -[_+1] => False theorem nonnegg_comm {c d : ℕ} {x y : ℤ} : Nonnegg c d x y = Nonnegg d c y x := by cases x <;> cases y <;> rfl theorem nonnegg_neg_pos {c d} : ∀ {a b : ℕ}, Nonnegg c d (-a) b ↔ SqLe a d b c | 0, b => ⟨by simp [SqLe, Nat.zero_le], fun _ => trivial⟩ | a + 1, b => by rfl theorem nonnegg_pos_neg {c d} {a b : ℕ} : Nonnegg c d a (-b) ↔ SqLe b c a d := by rw [nonnegg_comm]; exact nonnegg_neg_pos open Int in theorem nonnegg_cases_right {c d} {a : ℕ} : ∀ {b : ℤ}, (∀ x : ℕ, b = -x → SqLe x c a d) → Nonnegg c d a b | (b : Nat), _ => trivial | -[b+1], h => h (b + 1) rfl theorem nonnegg_cases_left {c d} {b : ℕ} {a : ℤ} (h : ∀ x : ℕ, a = -x → SqLe x d b c) : Nonnegg c d a b := cast nonnegg_comm (nonnegg_cases_right h) section Norm /-- The norm of an element of `ℤ[√d]`. -/ def norm (n : ℤ√d) : ℤ := n.re * n.re - d * n.im * n.im theorem norm_def (n : ℤ√d) : n.norm = n.re * n.re - d * n.im * n.im := rfl @[simp] theorem norm_zero : norm (0 : ℤ√d) = 0 := by simp [norm] @[simp] theorem norm_one : norm (1 : ℤ√d) = 1 := by simp [norm] @[simp] theorem norm_intCast (n : ℤ) : norm (n : ℤ√d) = n * n := by simp [norm] @[simp] theorem norm_natCast (n : ℕ) : norm (n : ℤ√d) = n * n := norm_intCast n @[simp] theorem norm_mul (n m : ℤ√d) : norm (n * m) = norm n * norm m := by simp only [norm, mul_im, mul_re] ring /-- `norm` as a `MonoidHom`. -/ def normMonoidHom : ℤ√d →* ℤ where toFun := norm map_mul' := norm_mul map_one' := norm_one theorem norm_eq_mul_conj (n : ℤ√d) : (norm n : ℤ√d) = n * star n := by ext <;> simp [norm, star, mul_comm, sub_eq_add_neg] @[simp] theorem norm_neg (x : ℤ√d) : (-x).norm = x.norm := (Int.cast_inj (α := ℤ√d)).1 <| by simp [norm_eq_mul_conj] @[simp] theorem norm_conj (x : ℤ√d) : (star x).norm = x.norm := (Int.cast_inj (α := ℤ√d)).1 <| by simp [norm_eq_mul_conj, mul_comm] theorem norm_nonneg (hd : d ≤ 0) (n : ℤ√d) : 0 ≤ n.norm := add_nonneg (mul_self_nonneg _) (by rw [mul_assoc, neg_mul_eq_neg_mul] exact mul_nonneg (neg_nonneg.2 hd) (mul_self_nonneg _)) theorem norm_eq_one_iff {x : ℤ√d} : x.norm.natAbs = 1 ↔ IsUnit x := ⟨fun h => isUnit_iff_dvd_one.2 <| (le_total 0 (norm x)).casesOn (fun hx => ⟨star x, by rwa [← Int.natCast_inj, Int.natAbs_of_nonneg hx, ← @Int.cast_inj (ℤ√d) _ _, norm_eq_mul_conj, eq_comm] at h⟩) fun hx => ⟨-star x, by rwa [← Int.natCast_inj, Int.ofNat_natAbs_of_nonpos hx, ← @Int.cast_inj (ℤ√d) _ _, Int.cast_neg, norm_eq_mul_conj, neg_mul_eq_mul_neg, eq_comm] at h⟩, fun h => by let ⟨y, hy⟩ := isUnit_iff_dvd_one.1 h have := congr_arg (Int.natAbs ∘ norm) hy rw [Function.comp_apply, Function.comp_apply, norm_mul, Int.natAbs_mul, norm_one, Int.natAbs_one, eq_comm, mul_eq_one] at this exact this.1⟩ theorem isUnit_iff_norm_isUnit {d : ℤ} (z : ℤ√d) : IsUnit z ↔ IsUnit z.norm := by rw [Int.isUnit_iff_natAbs_eq, norm_eq_one_iff] theorem norm_eq_one_iff' {d : ℤ} (hd : d ≤ 0) (z : ℤ√d) : z.norm = 1 ↔ IsUnit z := by rw [← norm_eq_one_iff, ← Int.natCast_inj, Int.natAbs_of_nonneg (norm_nonneg hd z), Int.ofNat_one] theorem norm_eq_zero_iff {d : ℤ} (hd : d < 0) (z : ℤ√d) : z.norm = 0 ↔ z = 0 := by constructor · intro h rw [norm_def, sub_eq_add_neg, mul_assoc] at h have left := mul_self_nonneg z.re have right := neg_nonneg.mpr (mul_nonpos_of_nonpos_of_nonneg hd.le (mul_self_nonneg z.im)) obtain ⟨ha, hb⟩ := (add_eq_zero_iff_of_nonneg left right).mp h ext <;> apply eq_zero_of_mul_self_eq_zero · exact ha · rw [neg_eq_zero, mul_eq_zero] at hb exact hb.resolve_left hd.ne · rintro rfl exact norm_zero theorem norm_eq_of_associated {d : ℤ} (hd : d ≤ 0) {x y : ℤ√d} (h : Associated x y) : x.norm = y.norm := by obtain ⟨u, rfl⟩ := h rw [norm_mul, (norm_eq_one_iff' hd _).mpr u.isUnit, mul_one] end Norm end section variable {d : ℕ} /-- Nonnegativity of an element of `ℤ√d`. -/ def Nonneg : ℤ√d → Prop | ⟨a, b⟩ => Nonnegg d 1 a b instance : LE (ℤ√d) := ⟨fun a b => Nonneg (b - a)⟩ instance : LT (ℤ√d) := ⟨fun a b => ¬b ≤ a⟩ instance decidableNonnegg (c d a b) : Decidable (Nonnegg c d a b) := by cases a <;> cases b <;> unfold Nonnegg SqLe <;> infer_instance instance decidableNonneg : ∀ a : ℤ√d, Decidable (Nonneg a) | ⟨_, _⟩ => Zsqrtd.decidableNonnegg _ _ _ _ instance decidableLE : DecidableLE (ℤ√d) := fun _ _ => decidableNonneg _ open Int in theorem nonneg_cases : ∀ {a : ℤ√d}, Nonneg a → ∃ x y : ℕ, a = ⟨x, y⟩ ∨ a = ⟨x, -y⟩ ∨ a = ⟨-x, y⟩ | ⟨(x : ℕ), (y : ℕ)⟩, _ => ⟨x, y, Or.inl rfl⟩ | ⟨(x : ℕ), -[y+1]⟩, _ => ⟨x, y + 1, Or.inr <| Or.inl rfl⟩ | ⟨-[x+1], (y : ℕ)⟩, _ => ⟨x + 1, y, Or.inr <| Or.inr rfl⟩ | ⟨-[_+1], -[_+1]⟩, h => False.elim h open Int in theorem nonneg_add_lem {x y z w : ℕ} (xy : Nonneg (⟨x, -y⟩ : ℤ√d)) (zw : Nonneg (⟨-z, w⟩ : ℤ√d)) : Nonneg (⟨x, -y⟩ + ⟨-z, w⟩ : ℤ√d) := by have : Nonneg ⟨Int.subNatNat x z, Int.subNatNat w y⟩ := Int.subNatNat_elim x z
(fun m n i => SqLe y d m 1 → SqLe n 1 w d → Nonneg ⟨i, Int.subNatNat w y⟩) (fun j k => Int.subNatNat_elim w y (fun m n i => SqLe n d (k + j) 1 → SqLe k 1 m d → Nonneg ⟨Int.ofNat j, i⟩)
Mathlib/NumberTheory/Zsqrtd/Basic.lean
556
559
/- Copyright (c) 2024 David Kurniadi Angdinata. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: David Kurniadi Angdinata -/ import Mathlib.AlgebraicGeometry.EllipticCurve.Group import Mathlib.NumberTheory.EllipticDivisibilitySequence /-! # Division polynomials of Weierstrass curves This file defines certain polynomials associated to division polynomials of Weierstrass curves. These are defined in terms of the auxiliary sequences for normalised elliptic divisibility sequences (EDS) as defined in `Mathlib.NumberTheory.EllipticDivisibilitySequence`. ## Mathematical background Let `W` be a Weierstrass curve over a commutative ring `R`. The sequence of `n`-division polynomials `ψₙ ∈ R[X, Y]` of `W` is the normalised EDS with initial values * `ψ₀ := 0`, * `ψ₁ := 1`, * `ψ₂ := 2Y + a₁X + a₃`, * `ψ₃ := 3X⁴ + b₂X³ + 3b₄X² + 3b₆X + b₈`, and * `ψ₄ := ψ₂ ⬝ (2X⁶ + b₂X⁵ + 5b₄X⁴ + 10b₆X³ + 10b₈X² + (b₂b₈ - b₄b₆)X + (b₄b₈ - b₆²))`. Furthermore, define the associated sequences `φₙ, ωₙ ∈ R[X, Y]` by * `φₙ := Xψₙ² - ψₙ₊₁ ⬝ ψₙ₋₁`, and * `ωₙ := (ψ₂ₙ / ψₙ - ψₙ ⬝ (a₁φₙ + a₃ψₙ²)) / 2`. Note that `ωₙ` is always well-defined as a polynomial in `R[X, Y]`. As a start, it can be shown by induction that `ψₙ` always divides `ψ₂ₙ` in `R[X, Y]`, so that `ψ₂ₙ / ψₙ` is always well-defined as a polynomial, while division by `2` is well-defined when `R` has characteristic different from `2`. In general, it can be shown that `2` always divides the polynomial `ψ₂ₙ / ψₙ - ψₙ ⬝ (a₁φₙ + a₃ψₙ²)` in the characteristic `0` universal ring `𝓡[X, Y] := ℤ[A₁, A₂, A₃, A₄, A₆][X, Y]` of `W`, where the `Aᵢ` are indeterminates. Then `ωₙ` can be equivalently defined as the image of this division under the associated universal morphism `𝓡[X, Y] → R[X, Y]` mapping `Aᵢ` to `aᵢ`. Now, in the coordinate ring `R[W]`, note that `ψ₂²` is congruent to the polynomial `Ψ₂Sq := 4X³ + b₂X² + 2b₄X + b₆ ∈ R[X]`. As such, the recurrences of a normalised EDS show that `ψₙ / ψ₂` are congruent to certain polynomials in `R[W]`. In particular, define `preΨₙ ∈ R[X]` as the auxiliary sequence for a normalised EDS with extra parameter `Ψ₂Sq²` and initial values * `preΨ₀ := 0`, * `preΨ₁ := 1`, * `preΨ₂ := 1`, * `preΨ₃ := ψ₃`, and * `preΨ₄ := ψ₄ / ψ₂`. The corresponding normalised EDS `Ψₙ ∈ R[X, Y]` is then given by * `Ψₙ := preΨₙ ⬝ ψ₂` if `n` is even, and * `Ψₙ := preΨₙ` if `n` is odd. Furthermore, define the associated sequences `ΨSqₙ, Φₙ ∈ R[X]` by * `ΨSqₙ := preΨₙ² ⬝ Ψ₂Sq` if `n` is even, * `ΨSqₙ := preΨₙ²` if `n` is odd, * `Φₙ := XΨSqₙ - preΨₙ₊₁ ⬝ preΨₙ₋₁` if `n` is even, and * `Φₙ := XΨSqₙ - preΨₙ₊₁ ⬝ preΨₙ₋₁ ⬝ Ψ₂Sq` if `n` is odd. With these definitions, `ψₙ ∈ R[X, Y]` and `φₙ ∈ R[X, Y]` are congruent in `R[W]` to `Ψₙ ∈ R[X, Y]` and `Φₙ ∈ R[X]` respectively, which are defined in terms of `Ψ₂Sq ∈ R[X]` and `preΨₙ ∈ R[X]`. ## Main definitions * `WeierstrassCurve.preΨ`: the univariate polynomials `preΨₙ`. * `WeierstrassCurve.ΨSq`: the univariate polynomials `ΨSqₙ`. * `WeierstrassCurve.Ψ`: the bivariate polynomials `Ψₙ`. * `WeierstrassCurve.Φ`: the univariate polynomials `Φₙ`. * `WeierstrassCurve.ψ`: the bivariate `n`-division polynomials `ψₙ`. * `WeierstrassCurve.φ`: the bivariate polynomials `φₙ`. * TODO: the bivariate polynomials `ωₙ`. ## Implementation notes Analogously to `Mathlib.NumberTheory.EllipticDivisibilitySequence`, the bivariate polynomials `Ψₙ` are defined in terms of the univariate polynomials `preΨₙ`. This is done partially to avoid ring division, but more crucially to allow the definition of `ΨSqₙ` and `Φₙ` as univariate polynomials without needing to work under the coordinate ring, and to allow the computation of their leading terms without ambiguity. Furthermore, evaluating these polynomials at a rational point on `W` recovers their original definition up to linear combinations of the Weierstrass equation of `W`, hence also avoiding the need to work in the coordinate ring. TODO: implementation notes for the definition of `ωₙ`. ## References [J Silverman, *The Arithmetic of Elliptic Curves*][silverman2009] ## Tags elliptic curve, division polynomial, torsion point -/ open Polynomial open scoped Polynomial.Bivariate local macro "C_simp" : tactic => `(tactic| simp only [map_ofNat, C_0, C_1, C_neg, C_add, C_sub, C_mul, C_pow]) local macro "map_simp" : tactic => `(tactic| simp only [map_ofNat, map_neg, map_add, map_sub, map_mul, map_pow, map_div₀, Polynomial.map_ofNat, Polynomial.map_one, map_C, map_X, Polynomial.map_neg, Polynomial.map_add, Polynomial.map_sub, Polynomial.map_mul, Polynomial.map_pow, Polynomial.map_div, coe_mapRingHom, apply_ite <| mapRingHom _, WeierstrassCurve.map]) universe r s u v namespace WeierstrassCurve variable {R : Type r} {S : Type s} [CommRing R] [CommRing S] (W : WeierstrassCurve R) section Ψ₂Sq /-! ### The univariate polynomial `Ψ₂Sq` -/ /-- The `2`-division polynomial `ψ₂ = Ψ₂`. -/ noncomputable def ψ₂ : R[X][Y] := W.toAffine.polynomialY /-- The univariate polynomial `Ψ₂Sq` congruent to `ψ₂²`. -/ noncomputable def Ψ₂Sq : R[X] := C 4 * X ^ 3 + C W.b₂ * X ^ 2 + C (2 * W.b₄) * X + C W.b₆ lemma C_Ψ₂Sq : C W.Ψ₂Sq = W.ψ₂ ^ 2 - 4 * W.toAffine.polynomial := by rw [Ψ₂Sq, ψ₂, b₂, b₄, b₆, Affine.polynomialY, Affine.polynomial] C_simp ring1 lemma ψ₂_sq : W.ψ₂ ^ 2 = C W.Ψ₂Sq + 4 * W.toAffine.polynomial := by rw [C_Ψ₂Sq, sub_add_cancel] lemma Affine.CoordinateRing.mk_ψ₂_sq : mk W W.ψ₂ ^ 2 = mk W (C W.Ψ₂Sq) := by rw [C_Ψ₂Sq, map_sub, map_mul, AdjoinRoot.mk_self, mul_zero, sub_zero, map_pow] -- TODO: remove `twoTorsionPolynomial` in favour of `Ψ₂Sq`
lemma Ψ₂Sq_eq : W.Ψ₂Sq = W.twoTorsionPolynomial.toPoly := rfl
Mathlib/AlgebraicGeometry/EllipticCurve/DivisionPolynomial/Basic.lean
134
135
/- Copyright (c) 2021 Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying, Rémy Degenne -/ import Mathlib.Probability.Process.Adapted import Mathlib.MeasureTheory.Constructions.BorelSpace.Order /-! # Stopping times, stopped processes and stopped values Definition and properties of stopping times. ## Main definitions * `MeasureTheory.IsStoppingTime`: a stopping time with respect to some filtration `f` is a function `τ` such that for all `i`, the preimage of `{j | j ≤ i}` along `τ` is `f i`-measurable * `MeasureTheory.IsStoppingTime.measurableSpace`: the σ-algebra associated with a stopping time ## Main results * `ProgMeasurable.stoppedProcess`: the stopped process of a progressively measurable process is progressively measurable. * `memLp_stoppedProcess`: if a process belongs to `ℒp` at every time in `ℕ`, then its stopped process belongs to `ℒp` as well. ## Tags stopping time, stochastic process -/ open Filter Order TopologicalSpace open scoped MeasureTheory NNReal ENNReal Topology namespace MeasureTheory variable {Ω β ι : Type*} {m : MeasurableSpace Ω} /-! ### Stopping times -/ /-- A stopping time with respect to some filtration `f` is a function `τ` such that for all `i`, the preimage of `{j | j ≤ i}` along `τ` is measurable with respect to `f i`. Intuitively, the stopping time `τ` describes some stopping rule such that at time `i`, we may determine it with the information we have at time `i`. -/ def IsStoppingTime [Preorder ι] (f : Filtration ι m) (τ : Ω → ι) := ∀ i : ι, MeasurableSet[f i] <| {ω | τ ω ≤ i} theorem isStoppingTime_const [Preorder ι] (f : Filtration ι m) (i : ι) : IsStoppingTime f fun _ => i := fun j => by simp only [MeasurableSet.const] section MeasurableSet section Preorder variable [Preorder ι] {f : Filtration ι m} {τ : Ω → ι} protected theorem IsStoppingTime.measurableSet_le (hτ : IsStoppingTime f τ) (i : ι) : MeasurableSet[f i] {ω | τ ω ≤ i} := hτ i theorem IsStoppingTime.measurableSet_lt_of_pred [PredOrder ι] (hτ : IsStoppingTime f τ) (i : ι) : MeasurableSet[f i] {ω | τ ω < i} := by by_cases hi_min : IsMin i · suffices {ω : Ω | τ ω < i} = ∅ by rw [this]; exact @MeasurableSet.empty _ (f i) ext1 ω simp only [Set.mem_setOf_eq, Set.mem_empty_iff_false, iff_false] rw [isMin_iff_forall_not_lt] at hi_min exact hi_min (τ ω) have : {ω : Ω | τ ω < i} = τ ⁻¹' Set.Iic (pred i) := by ext; simp [Iic_pred_of_not_isMin hi_min] rw [this] exact f.mono (pred_le i) _ (hτ.measurableSet_le <| pred i) end Preorder section CountableStoppingTime namespace IsStoppingTime variable [PartialOrder ι] {τ : Ω → ι} {f : Filtration ι m} protected theorem measurableSet_eq_of_countable_range (hτ : IsStoppingTime f τ) (h_countable : (Set.range τ).Countable) (i : ι) : MeasurableSet[f i] {ω | τ ω = i} := by have : {ω | τ ω = i} = {ω | τ ω ≤ i} \ ⋃ (j ∈ Set.range τ) (_ : j < i), {ω | τ ω ≤ j} := by ext1 a simp only [Set.mem_setOf_eq, Set.mem_range, Set.iUnion_exists, Set.iUnion_iUnion_eq', Set.mem_diff, Set.mem_iUnion, exists_prop, not_exists, not_and, not_le] constructor <;> intro h · simp only [h, lt_iff_le_not_le, le_refl, and_imp, imp_self, imp_true_iff, and_self_iff] · exact h.1.eq_or_lt.resolve_right fun h_lt => h.2 a h_lt le_rfl rw [this] refine (hτ.measurableSet_le i).diff ?_ refine MeasurableSet.biUnion h_countable fun j _ => ?_ classical rw [Set.iUnion_eq_if] split_ifs with hji · exact f.mono hji.le _ (hτ.measurableSet_le j) · exact @MeasurableSet.empty _ (f i) protected theorem measurableSet_eq_of_countable [Countable ι] (hτ : IsStoppingTime f τ) (i : ι) : MeasurableSet[f i] {ω | τ ω = i} := hτ.measurableSet_eq_of_countable_range (Set.to_countable _) i protected theorem measurableSet_lt_of_countable_range (hτ : IsStoppingTime f τ) (h_countable : (Set.range τ).Countable) (i : ι) : MeasurableSet[f i] {ω | τ ω < i} := by have : {ω | τ ω < i} = {ω | τ ω ≤ i} \ {ω | τ ω = i} := by ext1 ω; simp [lt_iff_le_and_ne] rw [this] exact (hτ.measurableSet_le i).diff (hτ.measurableSet_eq_of_countable_range h_countable i) protected theorem measurableSet_lt_of_countable [Countable ι] (hτ : IsStoppingTime f τ) (i : ι) : MeasurableSet[f i] {ω | τ ω < i} := hτ.measurableSet_lt_of_countable_range (Set.to_countable _) i protected theorem measurableSet_ge_of_countable_range {ι} [LinearOrder ι] {τ : Ω → ι} {f : Filtration ι m} (hτ : IsStoppingTime f τ) (h_countable : (Set.range τ).Countable) (i : ι) : MeasurableSet[f i] {ω | i ≤ τ ω} := by have : {ω | i ≤ τ ω} = {ω | τ ω < i}ᶜ := by ext1 ω; simp only [Set.mem_setOf_eq, Set.mem_compl_iff, not_lt] rw [this] exact (hτ.measurableSet_lt_of_countable_range h_countable i).compl protected theorem measurableSet_ge_of_countable {ι} [LinearOrder ι] {τ : Ω → ι} {f : Filtration ι m} [Countable ι] (hτ : IsStoppingTime f τ) (i : ι) : MeasurableSet[f i] {ω | i ≤ τ ω} := hτ.measurableSet_ge_of_countable_range (Set.to_countable _) i end IsStoppingTime end CountableStoppingTime section LinearOrder variable [LinearOrder ι] {f : Filtration ι m} {τ : Ω → ι} theorem IsStoppingTime.measurableSet_gt (hτ : IsStoppingTime f τ) (i : ι) : MeasurableSet[f i] {ω | i < τ ω} := by have : {ω | i < τ ω} = {ω | τ ω ≤ i}ᶜ := by ext1 ω; simp only [Set.mem_setOf_eq, Set.mem_compl_iff, not_le] rw [this] exact (hτ.measurableSet_le i).compl section TopologicalSpace variable [TopologicalSpace ι] [OrderTopology ι] [FirstCountableTopology ι] /-- Auxiliary lemma for `MeasureTheory.IsStoppingTime.measurableSet_lt`. -/ theorem IsStoppingTime.measurableSet_lt_of_isLUB (hτ : IsStoppingTime f τ) (i : ι) (h_lub : IsLUB (Set.Iio i) i) : MeasurableSet[f i] {ω | τ ω < i} := by by_cases hi_min : IsMin i · suffices {ω | τ ω < i} = ∅ by rw [this]; exact @MeasurableSet.empty _ (f i) ext1 ω simp only [Set.mem_setOf_eq, Set.mem_empty_iff_false, iff_false] exact isMin_iff_forall_not_lt.mp hi_min (τ ω) obtain ⟨seq, -, -, h_tendsto, h_bound⟩ : ∃ seq : ℕ → ι, Monotone seq ∧ (∀ j, seq j ≤ i) ∧ Tendsto seq atTop (𝓝 i) ∧ ∀ j, seq j < i := h_lub.exists_seq_monotone_tendsto (not_isMin_iff.mp hi_min) have h_Ioi_eq_Union : Set.Iio i = ⋃ j, {k | k ≤ seq j} := by ext1 k simp only [Set.mem_Iio, Set.mem_iUnion, Set.mem_setOf_eq] refine ⟨fun hk_lt_i => ?_, fun h_exists_k_le_seq => ?_⟩ · rw [tendsto_atTop'] at h_tendsto have h_nhds : Set.Ici k ∈ 𝓝 i := mem_nhds_iff.mpr ⟨Set.Ioi k, Set.Ioi_subset_Ici le_rfl, isOpen_Ioi, hk_lt_i⟩ obtain ⟨a, ha⟩ : ∃ a : ℕ, ∀ b : ℕ, b ≥ a → k ≤ seq b := h_tendsto (Set.Ici k) h_nhds exact ⟨a, ha a le_rfl⟩ · obtain ⟨j, hk_seq_j⟩ := h_exists_k_le_seq exact hk_seq_j.trans_lt (h_bound j) have h_lt_eq_preimage : {ω | τ ω < i} = τ ⁻¹' Set.Iio i := by ext1 ω; simp only [Set.mem_setOf_eq, Set.mem_preimage, Set.mem_Iio] rw [h_lt_eq_preimage, h_Ioi_eq_Union] simp only [Set.preimage_iUnion, Set.preimage_setOf_eq] exact MeasurableSet.iUnion fun n => f.mono (h_bound n).le _ (hτ.measurableSet_le (seq n)) theorem IsStoppingTime.measurableSet_lt (hτ : IsStoppingTime f τ) (i : ι) : MeasurableSet[f i] {ω | τ ω < i} := by obtain ⟨i', hi'_lub⟩ : ∃ i', IsLUB (Set.Iio i) i' := exists_lub_Iio i rcases lub_Iio_eq_self_or_Iio_eq_Iic i hi'_lub with hi'_eq_i | h_Iio_eq_Iic · rw [← hi'_eq_i] at hi'_lub ⊢ exact hτ.measurableSet_lt_of_isLUB i' hi'_lub · have h_lt_eq_preimage : {ω : Ω | τ ω < i} = τ ⁻¹' Set.Iio i := rfl rw [h_lt_eq_preimage, h_Iio_eq_Iic] exact f.mono (lub_Iio_le i hi'_lub) _ (hτ.measurableSet_le i') theorem IsStoppingTime.measurableSet_ge (hτ : IsStoppingTime f τ) (i : ι) : MeasurableSet[f i] {ω | i ≤ τ ω} := by have : {ω | i ≤ τ ω} = {ω | τ ω < i}ᶜ := by ext1 ω; simp only [Set.mem_setOf_eq, Set.mem_compl_iff, not_lt] rw [this] exact (hτ.measurableSet_lt i).compl theorem IsStoppingTime.measurableSet_eq (hτ : IsStoppingTime f τ) (i : ι) : MeasurableSet[f i] {ω | τ ω = i} := by have : {ω | τ ω = i} = {ω | τ ω ≤ i} ∩ {ω | τ ω ≥ i} := by ext1 ω; simp only [Set.mem_setOf_eq, Set.mem_inter_iff, le_antisymm_iff] rw [this] exact (hτ.measurableSet_le i).inter (hτ.measurableSet_ge i) theorem IsStoppingTime.measurableSet_eq_le (hτ : IsStoppingTime f τ) {i j : ι} (hle : i ≤ j) : MeasurableSet[f j] {ω | τ ω = i} := f.mono hle _ <| hτ.measurableSet_eq i theorem IsStoppingTime.measurableSet_lt_le (hτ : IsStoppingTime f τ) {i j : ι} (hle : i ≤ j) : MeasurableSet[f j] {ω | τ ω < i} := f.mono hle _ <| hτ.measurableSet_lt i end TopologicalSpace end LinearOrder section Countable theorem isStoppingTime_of_measurableSet_eq [Preorder ι] [Countable ι] {f : Filtration ι m} {τ : Ω → ι} (hτ : ∀ i, MeasurableSet[f i] {ω | τ ω = i}) : IsStoppingTime f τ := by intro i rw [show {ω | τ ω ≤ i} = ⋃ k ≤ i, {ω | τ ω = k} by ext; simp] refine MeasurableSet.biUnion (Set.to_countable _) fun k hk => ?_ exact f.mono hk _ (hτ k) end Countable end MeasurableSet namespace IsStoppingTime protected theorem max [LinearOrder ι] {f : Filtration ι m} {τ π : Ω → ι} (hτ : IsStoppingTime f τ) (hπ : IsStoppingTime f π) : IsStoppingTime f fun ω => max (τ ω) (π ω) := by intro i simp_rw [max_le_iff, Set.setOf_and] exact (hτ i).inter (hπ i) protected theorem max_const [LinearOrder ι] {f : Filtration ι m} {τ : Ω → ι} (hτ : IsStoppingTime f τ) (i : ι) : IsStoppingTime f fun ω => max (τ ω) i := hτ.max (isStoppingTime_const f i) protected theorem min [LinearOrder ι] {f : Filtration ι m} {τ π : Ω → ι} (hτ : IsStoppingTime f τ) (hπ : IsStoppingTime f π) : IsStoppingTime f fun ω => min (τ ω) (π ω) := by intro i simp_rw [min_le_iff, Set.setOf_or] exact (hτ i).union (hπ i) protected theorem min_const [LinearOrder ι] {f : Filtration ι m} {τ : Ω → ι} (hτ : IsStoppingTime f τ) (i : ι) : IsStoppingTime f fun ω => min (τ ω) i := hτ.min (isStoppingTime_const f i) theorem add_const [AddGroup ι] [Preorder ι] [AddRightMono ι] [AddLeftMono ι] {f : Filtration ι m} {τ : Ω → ι} (hτ : IsStoppingTime f τ) {i : ι} (hi : 0 ≤ i) : IsStoppingTime f fun ω => τ ω + i := by intro j simp_rw [← le_sub_iff_add_le] exact f.mono (sub_le_self j hi) _ (hτ (j - i)) theorem add_const_nat {f : Filtration ℕ m} {τ : Ω → ℕ} (hτ : IsStoppingTime f τ) {i : ℕ} : IsStoppingTime f fun ω => τ ω + i := by refine isStoppingTime_of_measurableSet_eq fun j => ?_ by_cases hij : i ≤ j · simp_rw [eq_comm, ← Nat.sub_eq_iff_eq_add hij, eq_comm] exact f.mono (j.sub_le i) _ (hτ.measurableSet_eq (j - i)) · rw [not_le] at hij convert @MeasurableSet.empty _ (f.1 j) ext ω simp only [Set.mem_empty_iff_false, iff_false, Set.mem_setOf] omega -- generalize to certain countable type? theorem add {f : Filtration ℕ m} {τ π : Ω → ℕ} (hτ : IsStoppingTime f τ) (hπ : IsStoppingTime f π) : IsStoppingTime f (τ + π) := by intro i rw [(_ : {ω | (τ + π) ω ≤ i} = ⋃ k ≤ i, {ω | π ω = k} ∩ {ω | τ ω + k ≤ i})] · exact MeasurableSet.iUnion fun k => MeasurableSet.iUnion fun hk => (hπ.measurableSet_eq_le hk).inter (hτ.add_const_nat i) ext ω simp only [Pi.add_apply, Set.mem_setOf_eq, Set.mem_iUnion, Set.mem_inter_iff, exists_prop] refine ⟨fun h => ⟨π ω, by omega, rfl, h⟩, ?_⟩ rintro ⟨j, hj, rfl, h⟩ assumption section Preorder variable [Preorder ι] {f : Filtration ι m} {τ π : Ω → ι} /-- The associated σ-algebra with a stopping time. -/ protected def measurableSpace (hτ : IsStoppingTime f τ) : MeasurableSpace Ω where MeasurableSet' s := ∀ i : ι, MeasurableSet[f i] (s ∩ {ω | τ ω ≤ i}) measurableSet_empty i := (Set.empty_inter {ω | τ ω ≤ i}).symm ▸ @MeasurableSet.empty _ (f i) measurableSet_compl s hs i := by rw [(_ : sᶜ ∩ {ω | τ ω ≤ i} = (sᶜ ∪ {ω | τ ω ≤ i}ᶜ) ∩ {ω | τ ω ≤ i})] · refine MeasurableSet.inter ?_ ?_ · rw [← Set.compl_inter] exact (hs i).compl · exact hτ i · rw [Set.union_inter_distrib_right] simp only [Set.compl_inter_self, Set.union_empty] measurableSet_iUnion s hs i := by rw [forall_swap] at hs rw [Set.iUnion_inter] exact MeasurableSet.iUnion (hs i) protected theorem measurableSet (hτ : IsStoppingTime f τ) (s : Set Ω) : MeasurableSet[hτ.measurableSpace] s ↔ ∀ i : ι, MeasurableSet[f i] (s ∩ {ω | τ ω ≤ i}) := Iff.rfl theorem measurableSpace_mono (hτ : IsStoppingTime f τ) (hπ : IsStoppingTime f π) (hle : τ ≤ π) : hτ.measurableSpace ≤ hπ.measurableSpace := by intro s hs i rw [(_ : s ∩ {ω | π ω ≤ i} = s ∩ {ω | τ ω ≤ i} ∩ {ω | π ω ≤ i})] · exact (hs i).inter (hπ i) · ext simp only [Set.mem_inter_iff, iff_self_and, and_congr_left_iff, Set.mem_setOf_eq] intro hle' _ exact le_trans (hle _) hle' theorem measurableSpace_le_of_countable [Countable ι] (hτ : IsStoppingTime f τ) : hτ.measurableSpace ≤ m := by intro s hs change ∀ i, MeasurableSet[f i] (s ∩ {ω | τ ω ≤ i}) at hs rw [(_ : s = ⋃ i, s ∩ {ω | τ ω ≤ i})] · exact MeasurableSet.iUnion fun i => f.le i _ (hs i) · ext ω; constructor <;> rw [Set.mem_iUnion] · exact fun hx => ⟨τ ω, hx, le_rfl⟩ · rintro ⟨_, hx, _⟩ exact hx theorem measurableSpace_le [IsCountablyGenerated (atTop : Filter ι)] [IsDirected ι (· ≤ ·)] (hτ : IsStoppingTime f τ) : hτ.measurableSpace ≤ m := by intro s hs cases isEmpty_or_nonempty ι · haveI : IsEmpty Ω := ⟨fun ω => IsEmpty.false (τ ω)⟩ apply Subsingleton.measurableSet · change ∀ i, MeasurableSet[f i] (s ∩ {ω | τ ω ≤ i}) at hs obtain ⟨seq : ℕ → ι, h_seq_tendsto⟩ := (atTop : Filter ι).exists_seq_tendsto rw [(_ : s = ⋃ n, s ∩ {ω | τ ω ≤ seq n})] · exact MeasurableSet.iUnion fun i => f.le (seq i) _ (hs (seq i)) · ext ω; constructor <;> rw [Set.mem_iUnion] · intro hx suffices ∃ i, τ ω ≤ seq i from ⟨this.choose, hx, this.choose_spec⟩ rw [tendsto_atTop] at h_seq_tendsto exact (h_seq_tendsto (τ ω)).exists · rintro ⟨_, hx, _⟩ exact hx @[deprecated (since := "2024-12-25")] alias measurableSpace_le' := measurableSpace_le example {f : Filtration ℕ m} {τ : Ω → ℕ} (hτ : IsStoppingTime f τ) : hτ.measurableSpace ≤ m := hτ.measurableSpace_le example {f : Filtration ℝ m} {τ : Ω → ℝ} (hτ : IsStoppingTime f τ) : hτ.measurableSpace ≤ m := hτ.measurableSpace_le @[simp] theorem measurableSpace_const (f : Filtration ι m) (i : ι) : (isStoppingTime_const f i).measurableSpace = f i := by ext1 s change MeasurableSet[(isStoppingTime_const f i).measurableSpace] s ↔ MeasurableSet[f i] s rw [IsStoppingTime.measurableSet] constructor <;> intro h · specialize h i simpa only [le_refl, Set.setOf_true, Set.inter_univ] using h · intro j by_cases hij : i ≤ j · simp only [hij, Set.setOf_true, Set.inter_univ] exact f.mono hij _ h · simp only [hij, Set.setOf_false, Set.inter_empty, @MeasurableSet.empty _ (f.1 j)] theorem measurableSet_inter_eq_iff (hτ : IsStoppingTime f τ) (s : Set Ω) (i : ι) : MeasurableSet[hτ.measurableSpace] (s ∩ {ω | τ ω = i}) ↔ MeasurableSet[f i] (s ∩ {ω | τ ω = i}) := by have : ∀ j, {ω : Ω | τ ω = i} ∩ {ω : Ω | τ ω ≤ j} = {ω : Ω | τ ω = i} ∩ {_ω | i ≤ j} := by intro j ext1 ω simp only [Set.mem_inter_iff, Set.mem_setOf_eq, and_congr_right_iff] intro hxi rw [hxi] constructor <;> intro h · specialize h i simpa only [Set.inter_assoc, this, le_refl, Set.setOf_true, Set.inter_univ] using h · intro j rw [Set.inter_assoc, this] by_cases hij : i ≤ j · simp only [hij, Set.setOf_true, Set.inter_univ] exact f.mono hij _ h · simp [hij] theorem measurableSpace_le_of_le_const (hτ : IsStoppingTime f τ) {i : ι} (hτ_le : ∀ ω, τ ω ≤ i) : hτ.measurableSpace ≤ f i := (measurableSpace_mono hτ _ hτ_le).trans (measurableSpace_const _ _).le theorem measurableSpace_le_of_le (hτ : IsStoppingTime f τ) {n : ι} (hτ_le : ∀ ω, τ ω ≤ n) : hτ.measurableSpace ≤ m := (hτ.measurableSpace_le_of_le_const hτ_le).trans (f.le n) theorem le_measurableSpace_of_const_le (hτ : IsStoppingTime f τ) {i : ι} (hτ_le : ∀ ω, i ≤ τ ω) : f i ≤ hτ.measurableSpace := (measurableSpace_const _ _).symm.le.trans (measurableSpace_mono _ hτ hτ_le) end Preorder instance sigmaFinite_stopping_time {ι} [SemilatticeSup ι] [OrderBot ι] [(Filter.atTop : Filter ι).IsCountablyGenerated] {μ : Measure Ω} {f : Filtration ι m} {τ : Ω → ι} [SigmaFiniteFiltration μ f] (hτ : IsStoppingTime f τ) : SigmaFinite (μ.trim hτ.measurableSpace_le) := by refine @sigmaFiniteTrim_mono _ _ ?_ _ _ _ ?_ ?_ · exact f ⊥ · exact hτ.le_measurableSpace_of_const_le fun _ => bot_le · infer_instance instance sigmaFinite_stopping_time_of_le {ι} [SemilatticeSup ι] [OrderBot ι] {μ : Measure Ω} {f : Filtration ι m} {τ : Ω → ι} [SigmaFiniteFiltration μ f] (hτ : IsStoppingTime f τ) {n : ι} (hτ_le : ∀ ω, τ ω ≤ n) : SigmaFinite (μ.trim (hτ.measurableSpace_le_of_le hτ_le)) := by refine @sigmaFiniteTrim_mono _ _ ?_ _ _ _ ?_ ?_ · exact f ⊥ · exact hτ.le_measurableSpace_of_const_le fun _ => bot_le · infer_instance section LinearOrder variable [LinearOrder ι] {f : Filtration ι m} {τ π : Ω → ι} protected theorem measurableSet_le' (hτ : IsStoppingTime f τ) (i : ι) : MeasurableSet[hτ.measurableSpace] {ω | τ ω ≤ i} := by intro j have : {ω : Ω | τ ω ≤ i} ∩ {ω : Ω | τ ω ≤ j} = {ω : Ω | τ ω ≤ min i j} := by ext1 ω; simp only [Set.mem_inter_iff, Set.mem_setOf_eq, le_min_iff] rw [this] exact f.mono (min_le_right i j) _ (hτ _) protected theorem measurableSet_gt' (hτ : IsStoppingTime f τ) (i : ι) : MeasurableSet[hτ.measurableSpace] {ω | i < τ ω} := by have : {ω : Ω | i < τ ω} = {ω : Ω | τ ω ≤ i}ᶜ := by ext1 ω; simp rw [this] exact (hτ.measurableSet_le' i).compl protected theorem measurableSet_eq' [TopologicalSpace ι] [OrderTopology ι] [FirstCountableTopology ι] (hτ : IsStoppingTime f τ) (i : ι) : MeasurableSet[hτ.measurableSpace] {ω | τ ω = i} := by rw [← Set.univ_inter {ω | τ ω = i}, measurableSet_inter_eq_iff, Set.univ_inter] exact hτ.measurableSet_eq i protected theorem measurableSet_ge' [TopologicalSpace ι] [OrderTopology ι] [FirstCountableTopology ι] (hτ : IsStoppingTime f τ) (i : ι) : MeasurableSet[hτ.measurableSpace] {ω | i ≤ τ ω} := by have : {ω | i ≤ τ ω} = {ω | τ ω = i} ∪ {ω | i < τ ω} := by ext1 ω simp only [le_iff_lt_or_eq, Set.mem_setOf_eq, Set.mem_union] rw [@eq_comm _ i, or_comm] rw [this] exact (hτ.measurableSet_eq' i).union (hτ.measurableSet_gt' i) protected theorem measurableSet_lt' [TopologicalSpace ι] [OrderTopology ι] [FirstCountableTopology ι] (hτ : IsStoppingTime f τ) (i : ι) : MeasurableSet[hτ.measurableSpace] {ω | τ ω < i} := by have : {ω | τ ω < i} = {ω | τ ω ≤ i} \ {ω | τ ω = i} := by ext1 ω simp only [lt_iff_le_and_ne, Set.mem_setOf_eq, Set.mem_diff] rw [this] exact (hτ.measurableSet_le' i).diff (hτ.measurableSet_eq' i) section Countable protected theorem measurableSet_eq_of_countable_range' (hτ : IsStoppingTime f τ) (h_countable : (Set.range τ).Countable) (i : ι) : MeasurableSet[hτ.measurableSpace] {ω | τ ω = i} := by rw [← Set.univ_inter {ω | τ ω = i}, measurableSet_inter_eq_iff, Set.univ_inter] exact hτ.measurableSet_eq_of_countable_range h_countable i protected theorem measurableSet_eq_of_countable' [Countable ι] (hτ : IsStoppingTime f τ) (i : ι) : MeasurableSet[hτ.measurableSpace] {ω | τ ω = i} := hτ.measurableSet_eq_of_countable_range' (Set.to_countable _) i protected theorem measurableSet_ge_of_countable_range' (hτ : IsStoppingTime f τ) (h_countable : (Set.range τ).Countable) (i : ι) : MeasurableSet[hτ.measurableSpace] {ω | i ≤ τ ω} := by have : {ω | i ≤ τ ω} = {ω | τ ω = i} ∪ {ω | i < τ ω} := by ext1 ω simp only [le_iff_lt_or_eq, Set.mem_setOf_eq, Set.mem_union] rw [@eq_comm _ i, or_comm] rw [this] exact (hτ.measurableSet_eq_of_countable_range' h_countable i).union (hτ.measurableSet_gt' i) protected theorem measurableSet_ge_of_countable' [Countable ι] (hτ : IsStoppingTime f τ) (i : ι) : MeasurableSet[hτ.measurableSpace] {ω | i ≤ τ ω} := hτ.measurableSet_ge_of_countable_range' (Set.to_countable _) i protected theorem measurableSet_lt_of_countable_range' (hτ : IsStoppingTime f τ) (h_countable : (Set.range τ).Countable) (i : ι) : MeasurableSet[hτ.measurableSpace] {ω | τ ω < i} := by have : {ω | τ ω < i} = {ω | τ ω ≤ i} \ {ω | τ ω = i} := by ext1 ω simp only [lt_iff_le_and_ne, Set.mem_setOf_eq, Set.mem_diff] rw [this] exact (hτ.measurableSet_le' i).diff (hτ.measurableSet_eq_of_countable_range' h_countable i) protected theorem measurableSet_lt_of_countable' [Countable ι] (hτ : IsStoppingTime f τ) (i : ι) : MeasurableSet[hτ.measurableSpace] {ω | τ ω < i} := hτ.measurableSet_lt_of_countable_range' (Set.to_countable _) i protected theorem measurableSpace_le_of_countable_range (hτ : IsStoppingTime f τ) (h_countable : (Set.range τ).Countable) : hτ.measurableSpace ≤ m := by intro s hs change ∀ i, MeasurableSet[f i] (s ∩ {ω | τ ω ≤ i}) at hs rw [(_ : s = ⋃ i ∈ Set.range τ, s ∩ {ω | τ ω ≤ i})] · exact MeasurableSet.biUnion h_countable fun i _ => f.le i _ (hs i) · ext ω constructor <;> rw [Set.mem_iUnion] · exact fun hx => ⟨τ ω, by simpa using hx⟩ · rintro ⟨i, hx⟩ simp only [Set.mem_range, Set.iUnion_exists, Set.mem_iUnion, Set.mem_inter_iff, Set.mem_setOf_eq, exists_prop, exists_and_right] at hx exact hx.2.1 end Countable protected theorem measurable [TopologicalSpace ι] [MeasurableSpace ι] [BorelSpace ι] [OrderTopology ι] [SecondCountableTopology ι] (hτ : IsStoppingTime f τ) : Measurable[hτ.measurableSpace] τ := @measurable_of_Iic ι Ω _ _ _ hτ.measurableSpace _ _ _ _ fun i => hτ.measurableSet_le' i protected theorem measurable_of_le [TopologicalSpace ι] [MeasurableSpace ι] [BorelSpace ι] [OrderTopology ι] [SecondCountableTopology ι] (hτ : IsStoppingTime f τ) {i : ι} (hτ_le : ∀ ω, τ ω ≤ i) : Measurable[f i] τ := hτ.measurable.mono (measurableSpace_le_of_le_const _ hτ_le) le_rfl theorem measurableSpace_min (hτ : IsStoppingTime f τ) (hπ : IsStoppingTime f π) : (hτ.min hπ).measurableSpace = hτ.measurableSpace ⊓ hπ.measurableSpace := by refine le_antisymm ?_ ?_ · exact le_inf (measurableSpace_mono _ hτ fun _ => min_le_left _ _) (measurableSpace_mono _ hπ fun _ => min_le_right _ _) · intro s change MeasurableSet[hτ.measurableSpace] s ∧ MeasurableSet[hπ.measurableSpace] s → MeasurableSet[(hτ.min hπ).measurableSpace] s simp_rw [IsStoppingTime.measurableSet] have : ∀ i, {ω | min (τ ω) (π ω) ≤ i} = {ω | τ ω ≤ i} ∪ {ω | π ω ≤ i} := by intro i; ext1 ω; simp simp_rw [this, Set.inter_union_distrib_left] exact fun h i => (h.left i).union (h.right i) theorem measurableSet_min_iff (hτ : IsStoppingTime f τ) (hπ : IsStoppingTime f π) (s : Set Ω) : MeasurableSet[(hτ.min hπ).measurableSpace] s ↔ MeasurableSet[hτ.measurableSpace] s ∧ MeasurableSet[hπ.measurableSpace] s := by rw [measurableSpace_min hτ hπ]; rfl theorem measurableSpace_min_const (hτ : IsStoppingTime f τ) {i : ι} : (hτ.min_const i).measurableSpace = hτ.measurableSpace ⊓ f i := by rw [hτ.measurableSpace_min (isStoppingTime_const _ i), measurableSpace_const] theorem measurableSet_min_const_iff (hτ : IsStoppingTime f τ) (s : Set Ω) {i : ι} : MeasurableSet[(hτ.min_const i).measurableSpace] s ↔ MeasurableSet[hτ.measurableSpace] s ∧ MeasurableSet[f i] s := by rw [measurableSpace_min_const hτ]; apply MeasurableSpace.measurableSet_inf theorem measurableSet_inter_le [TopologicalSpace ι] [SecondCountableTopology ι] [OrderTopology ι] [MeasurableSpace ι] [BorelSpace ι] (hτ : IsStoppingTime f τ) (hπ : IsStoppingTime f π) (s : Set Ω) (hs : MeasurableSet[hτ.measurableSpace] s) : MeasurableSet[(hτ.min hπ).measurableSpace] (s ∩ {ω | τ ω ≤ π ω}) := by simp_rw [IsStoppingTime.measurableSet] at hs ⊢ intro i have : s ∩ {ω | τ ω ≤ π ω} ∩ {ω | min (τ ω) (π ω) ≤ i} = s ∩ {ω | τ ω ≤ i} ∩ {ω | min (τ ω) (π ω) ≤ i} ∩ {ω | min (τ ω) i ≤ min (min (τ ω) (π ω)) i} := by ext1 ω simp only [min_le_iff, Set.mem_inter_iff, Set.mem_setOf_eq, le_min_iff, le_refl, true_and, true_or] by_cases hτi : τ ω ≤ i · simp only [hτi, true_or, and_true, and_congr_right_iff] intro constructor <;> intro h · exact Or.inl h · rcases h with h | h · exact h · exact hτi.trans h simp only [hτi, false_or, and_false, false_and, iff_false, not_and, not_le, and_imp] refine fun _ hτ_le_π => lt_of_lt_of_le ?_ hτ_le_π rw [← not_le] exact hτi rw [this] refine ((hs i).inter ((hτ.min hπ) i)).inter ?_ apply @measurableSet_le _ _ _ _ _ (Filtration.seq f i) _ _ _ _ _ ?_ ?_ · exact (hτ.min_const i).measurable_of_le fun _ => min_le_right _ _ · exact ((hτ.min hπ).min_const i).measurable_of_le fun _ => min_le_right _ _ theorem measurableSet_inter_le_iff [TopologicalSpace ι] [SecondCountableTopology ι] [OrderTopology ι] [MeasurableSpace ι] [BorelSpace ι] (hτ : IsStoppingTime f τ) (hπ : IsStoppingTime f π) (s : Set Ω) : MeasurableSet[hτ.measurableSpace] (s ∩ {ω | τ ω ≤ π ω}) ↔ MeasurableSet[(hτ.min hπ).measurableSpace] (s ∩ {ω | τ ω ≤ π ω}) := by constructor <;> intro h · have : s ∩ {ω | τ ω ≤ π ω} = s ∩ {ω | τ ω ≤ π ω} ∩ {ω | τ ω ≤ π ω} := by rw [Set.inter_assoc, Set.inter_self] rw [this] exact measurableSet_inter_le _ hπ _ h · rw [measurableSet_min_iff hτ hπ] at h exact h.1 theorem measurableSet_inter_le_const_iff (hτ : IsStoppingTime f τ) (s : Set Ω) (i : ι) : MeasurableSet[hτ.measurableSpace] (s ∩ {ω | τ ω ≤ i}) ↔ MeasurableSet[(hτ.min_const i).measurableSpace] (s ∩ {ω | τ ω ≤ i}) := by rw [IsStoppingTime.measurableSet_min_iff hτ (isStoppingTime_const _ i),
IsStoppingTime.measurableSpace_const, IsStoppingTime.measurableSet] refine ⟨fun h => ⟨h, ?_⟩, fun h j => h.1 j⟩ specialize h i rwa [Set.inter_assoc, Set.inter_self] at h
Mathlib/Probability/Process/Stopping.lean
601
604
/- Copyright (c) 2024 Miyahara Kō. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Miyahara Kō -/ import Mathlib.Algebra.Order.Group.Nat import Mathlib.Data.List.Defs import Mathlib.Data.Set.Function /-! # iterate Proves various lemmas about `List.iterate`. -/ variable {α : Type*} namespace List @[simp] theorem length_iterate (f : α → α) (a : α) (n : ℕ) : length (iterate f a n) = n := by induction n generalizing a <;> simp [*] @[simp] theorem iterate_eq_nil {f : α → α} {a : α} {n : ℕ} : iterate f a n = [] ↔ n = 0 := by rw [← length_eq_zero_iff, length_iterate] theorem getElem?_iterate (f : α → α) (a : α) : ∀ (n i : ℕ), i < n → (iterate f a n)[i]? = f^[i] a | n + 1, 0 , _ => by simp | n + 1, i + 1, h => by simp [getElem?_iterate f (f a) n i (by simpa using h)] @[simp] theorem getElem_iterate (f : α → α) (a : α) (n : ℕ) (i : Nat) (h : i < (iterate f a n).length) : (iterate f a n)[i] = f^[i] a := (getElem_eq_iff _).2 <| getElem?_iterate _ _ _ _ <| by rwa [length_iterate] at h @[simp] theorem mem_iterate {f : α → α} {a : α} {n : ℕ} {b : α} : b ∈ iterate f a n ↔ ∃ m < n, b = f^[m] a := by simp [List.mem_iff_get, Fin.exists_iff, eq_comm (b := b)] @[simp] theorem range_map_iterate (n : ℕ) (f : α → α) (a : α) : (List.range n).map (f^[·] a) = List.iterate f a n := by apply List.ext_getElem <;> simp theorem iterate_add (f : α → α) (a : α) (m n : ℕ) : iterate f a (m + n) = iterate f a m ++ iterate f (f^[m] a) n := by induction m generalizing a with | zero => simp | succ n ih => rw [iterate, add_right_comm, iterate, ih, Nat.iterate, cons_append] theorem take_iterate (f : α → α) (a : α) (m n : ℕ) : take m (iterate f a n) = iterate f a (min m n) := by rw [← range_map_iterate, ← range_map_iterate, ← map_take, take_range] end List
Mathlib/Data/List/Iterate.lean
62
64
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Julian Kuelshammer -/ import Mathlib.Algebra.CharP.Defs import Mathlib.Algebra.Group.Commute.Basic import Mathlib.Algebra.Group.Pointwise.Set.Finite import Mathlib.Algebra.Group.Subgroup.Finite import Mathlib.Algebra.Module.NatInt import Mathlib.Algebra.Order.Group.Action import Mathlib.Algebra.Order.Ring.Abs import Mathlib.Data.Int.ModEq import Mathlib.Dynamics.PeriodicPts.Lemmas import Mathlib.GroupTheory.Index import Mathlib.NumberTheory.Divisors import Mathlib.Order.Interval.Set.Infinite /-! # Order of an element This file defines the order of an element of a finite group. For a finite group `G` the order of `x ∈ G` is the minimal `n ≥ 1` such that `x ^ n = 1`. ## Main definitions * `IsOfFinOrder` is a predicate on an element `x` of a monoid `G` saying that `x` is of finite order. * `IsOfFinAddOrder` is the additive analogue of `IsOfFinOrder`. * `orderOf x` defines the order of an element `x` of a monoid `G`, by convention its value is `0` if `x` has infinite order. * `addOrderOf` is the additive analogue of `orderOf`. ## Tags order of an element -/ assert_not_exists Field open Function Fintype Nat Pointwise Subgroup Submonoid open scoped Finset variable {G H A α β : Type*} section Monoid variable [Monoid G] {a b x y : G} {n m : ℕ} section IsOfFinOrder -- Porting note (https://github.com/leanprover-community/mathlib4/issues/12129): additional beta reduction needed @[to_additive] theorem isPeriodicPt_mul_iff_pow_eq_one (x : G) : IsPeriodicPt (x * ·) n 1 ↔ x ^ n = 1 := by rw [IsPeriodicPt, IsFixedPt, mul_left_iterate]; beta_reduce; rw [mul_one] /-- `IsOfFinOrder` is a predicate on an element `x` of a monoid to be of finite order, i.e. there exists `n ≥ 1` such that `x ^ n = 1`. -/ @[to_additive "`IsOfFinAddOrder` is a predicate on an element `a` of an additive monoid to be of finite order, i.e. there exists `n ≥ 1` such that `n • a = 0`."] def IsOfFinOrder (x : G) : Prop := (1 : G) ∈ periodicPts (x * ·) theorem isOfFinAddOrder_ofMul_iff : IsOfFinAddOrder (Additive.ofMul x) ↔ IsOfFinOrder x := Iff.rfl theorem isOfFinOrder_ofAdd_iff {α : Type*} [AddMonoid α] {x : α} : IsOfFinOrder (Multiplicative.ofAdd x) ↔ IsOfFinAddOrder x := Iff.rfl @[to_additive] theorem isOfFinOrder_iff_pow_eq_one : IsOfFinOrder x ↔ ∃ n, 0 < n ∧ x ^ n = 1 := by simp [IsOfFinOrder, mem_periodicPts, isPeriodicPt_mul_iff_pow_eq_one] @[to_additive] alias ⟨IsOfFinOrder.exists_pow_eq_one, _⟩ := isOfFinOrder_iff_pow_eq_one @[to_additive] lemma isOfFinOrder_iff_zpow_eq_one {G} [DivisionMonoid G] {x : G} : IsOfFinOrder x ↔ ∃ (n : ℤ), n ≠ 0 ∧ x ^ n = 1 := by rw [isOfFinOrder_iff_pow_eq_one] refine ⟨fun ⟨n, hn, hn'⟩ ↦ ⟨n, Int.natCast_ne_zero_iff_pos.mpr hn, zpow_natCast x n ▸ hn'⟩, fun ⟨n, hn, hn'⟩ ↦ ⟨n.natAbs, Int.natAbs_pos.mpr hn, ?_⟩⟩ rcases (Int.natAbs_eq_iff (a := n)).mp rfl with h | h · rwa [h, zpow_natCast] at hn' · rwa [h, zpow_neg, inv_eq_one, zpow_natCast] at hn' /-- See also `injective_pow_iff_not_isOfFinOrder`. -/ @[to_additive "See also `injective_nsmul_iff_not_isOfFinAddOrder`."] theorem not_isOfFinOrder_of_injective_pow {x : G} (h : Injective fun n : ℕ => x ^ n) : ¬IsOfFinOrder x := by simp_rw [isOfFinOrder_iff_pow_eq_one, not_exists, not_and] intro n hn_pos hnx rw [← pow_zero x] at hnx rw [h hnx] at hn_pos exact irrefl 0 hn_pos /-- 1 is of finite order in any monoid. -/ @[to_additive (attr := simp) "0 is of finite order in any additive monoid."] theorem IsOfFinOrder.one : IsOfFinOrder (1 : G) := isOfFinOrder_iff_pow_eq_one.mpr ⟨1, Nat.one_pos, one_pow 1⟩ @[to_additive] lemma IsOfFinOrder.pow {n : ℕ} : IsOfFinOrder a → IsOfFinOrder (a ^ n) := by simp_rw [isOfFinOrder_iff_pow_eq_one] rintro ⟨m, hm, ha⟩ exact ⟨m, hm, by simp [pow_right_comm _ n, ha]⟩ @[to_additive] lemma IsOfFinOrder.of_pow {n : ℕ} (h : IsOfFinOrder (a ^ n)) (hn : n ≠ 0) : IsOfFinOrder a := by rw [isOfFinOrder_iff_pow_eq_one] at * rcases h with ⟨m, hm, ha⟩ exact ⟨n * m, mul_pos hn.bot_lt hm, by rwa [pow_mul]⟩ @[to_additive (attr := simp)] lemma isOfFinOrder_pow {n : ℕ} : IsOfFinOrder (a ^ n) ↔ IsOfFinOrder a ∨ n = 0 := by rcases Decidable.eq_or_ne n 0 with rfl | hn · simp · exact ⟨fun h ↦ .inl <| h.of_pow hn, fun h ↦ (h.resolve_right hn).pow⟩ /-- Elements of finite order are of finite order in submonoids. -/ @[to_additive "Elements of finite order are of finite order in submonoids."] theorem Submonoid.isOfFinOrder_coe {H : Submonoid G} {x : H} : IsOfFinOrder (x : G) ↔ IsOfFinOrder x := by rw [isOfFinOrder_iff_pow_eq_one, isOfFinOrder_iff_pow_eq_one] norm_cast theorem IsConj.isOfFinOrder (h : IsConj x y) : IsOfFinOrder x → IsOfFinOrder y := by simp_rw [isOfFinOrder_iff_pow_eq_one] rintro ⟨n, n_gt_0, eq'⟩ exact ⟨n, n_gt_0, by rw [← isConj_one_right, ← eq']; exact h.pow n⟩ /-- The image of an element of finite order has finite order. -/ @[to_additive "The image of an element of finite additive order has finite additive order."] theorem MonoidHom.isOfFinOrder [Monoid H] (f : G →* H) {x : G} (h : IsOfFinOrder x) : IsOfFinOrder <| f x := isOfFinOrder_iff_pow_eq_one.mpr <| by obtain ⟨n, npos, hn⟩ := h.exists_pow_eq_one exact ⟨n, npos, by rw [← f.map_pow, hn, f.map_one]⟩ /-- If a direct product has finite order then so does each component. -/ @[to_additive "If a direct product has finite additive order then so does each component."] theorem IsOfFinOrder.apply {η : Type*} {Gs : η → Type*} [∀ i, Monoid (Gs i)] {x : ∀ i, Gs i} (h : IsOfFinOrder x) : ∀ i, IsOfFinOrder (x i) := by obtain ⟨n, npos, hn⟩ := h.exists_pow_eq_one exact fun _ => isOfFinOrder_iff_pow_eq_one.mpr ⟨n, npos, (congr_fun hn.symm _).symm⟩ /-- The submonoid generated by an element is a group if that element has finite order. -/ @[to_additive "The additive submonoid generated by an element is an additive group if that element has finite order."] noncomputable abbrev IsOfFinOrder.groupPowers (hx : IsOfFinOrder x) : Group (Submonoid.powers x) := by obtain ⟨hpos, hx⟩ := hx.exists_pow_eq_one.choose_spec exact Submonoid.groupPowers hpos hx end IsOfFinOrder /-- `orderOf x` is the order of the element `x`, i.e. the `n ≥ 1`, s.t. `x ^ n = 1` if it exists. Otherwise, i.e. if `x` is of infinite order, then `orderOf x` is `0` by convention. -/ @[to_additive "`addOrderOf a` is the order of the element `a`, i.e. the `n ≥ 1`, s.t. `n • a = 0` if it exists. Otherwise, i.e. if `a` is of infinite order, then `addOrderOf a` is `0` by convention."] noncomputable def orderOf (x : G) : ℕ := minimalPeriod (x * ·) 1 @[simp] theorem addOrderOf_ofMul_eq_orderOf (x : G) : addOrderOf (Additive.ofMul x) = orderOf x := rfl @[simp] lemma orderOf_ofAdd_eq_addOrderOf {α : Type*} [AddMonoid α] (a : α) : orderOf (Multiplicative.ofAdd a) = addOrderOf a := rfl @[to_additive] protected lemma IsOfFinOrder.orderOf_pos (h : IsOfFinOrder x) : 0 < orderOf x := minimalPeriod_pos_of_mem_periodicPts h @[to_additive addOrderOf_nsmul_eq_zero] theorem pow_orderOf_eq_one (x : G) : x ^ orderOf x = 1 := by convert Eq.trans _ (isPeriodicPt_minimalPeriod (x * ·) 1) -- Porting note (https://github.com/leanprover-community/mathlib4/issues/12129): additional beta reduction needed in the middle of the rewrite rw [orderOf, mul_left_iterate]; beta_reduce; rw [mul_one] @[to_additive] theorem orderOf_eq_zero (h : ¬IsOfFinOrder x) : orderOf x = 0 := by rwa [orderOf, minimalPeriod, dif_neg] @[to_additive] theorem orderOf_eq_zero_iff : orderOf x = 0 ↔ ¬IsOfFinOrder x := ⟨fun h H ↦ H.orderOf_pos.ne' h, orderOf_eq_zero⟩ @[to_additive] theorem orderOf_eq_zero_iff' : orderOf x = 0 ↔ ∀ n : ℕ, 0 < n → x ^ n ≠ 1 := by simp_rw [orderOf_eq_zero_iff, isOfFinOrder_iff_pow_eq_one, not_exists, not_and] @[to_additive] theorem orderOf_eq_iff {n} (h : 0 < n) : orderOf x = n ↔ x ^ n = 1 ∧ ∀ m, m < n → 0 < m → x ^ m ≠ 1 := by simp_rw [Ne, ← isPeriodicPt_mul_iff_pow_eq_one, orderOf, minimalPeriod] split_ifs with h1 · classical rw [find_eq_iff] simp only [h, true_and] push_neg rfl · rw [iff_false_left h.ne] rintro ⟨h', -⟩ exact h1 ⟨n, h, h'⟩ /-- A group element has finite order iff its order is positive. -/ @[to_additive "A group element has finite additive order iff its order is positive."] theorem orderOf_pos_iff : 0 < orderOf x ↔ IsOfFinOrder x := by rw [iff_not_comm.mp orderOf_eq_zero_iff, pos_iff_ne_zero] @[to_additive] theorem IsOfFinOrder.mono [Monoid β] {y : β} (hx : IsOfFinOrder x) (h : orderOf y ∣ orderOf x) : IsOfFinOrder y := by rw [← orderOf_pos_iff] at hx ⊢; exact Nat.pos_of_dvd_of_pos h hx @[to_additive] theorem pow_ne_one_of_lt_orderOf (n0 : n ≠ 0) (h : n < orderOf x) : x ^ n ≠ 1 := fun j => not_isPeriodicPt_of_pos_of_lt_minimalPeriod n0 h ((isPeriodicPt_mul_iff_pow_eq_one x).mpr j) @[to_additive] theorem orderOf_le_of_pow_eq_one (hn : 0 < n) (h : x ^ n = 1) : orderOf x ≤ n := IsPeriodicPt.minimalPeriod_le hn (by rwa [isPeriodicPt_mul_iff_pow_eq_one]) @[to_additive (attr := simp)] theorem orderOf_one : orderOf (1 : G) = 1 := by rw [orderOf, ← minimalPeriod_id (x := (1 : G)), ← one_mul_eq_id] @[to_additive (attr := simp) AddMonoid.addOrderOf_eq_one_iff] theorem orderOf_eq_one_iff : orderOf x = 1 ↔ x = 1 := by rw [orderOf, minimalPeriod_eq_one_iff_isFixedPt, IsFixedPt, mul_one] @[to_additive (attr := simp) mod_addOrderOf_nsmul] lemma pow_mod_orderOf (x : G) (n : ℕ) : x ^ (n % orderOf x) = x ^ n := calc x ^ (n % orderOf x) = x ^ (n % orderOf x + orderOf x * (n / orderOf x)) := by simp [pow_add, pow_mul, pow_orderOf_eq_one] _ = x ^ n := by rw [Nat.mod_add_div] @[to_additive] theorem orderOf_dvd_of_pow_eq_one (h : x ^ n = 1) : orderOf x ∣ n := IsPeriodicPt.minimalPeriod_dvd ((isPeriodicPt_mul_iff_pow_eq_one _).mpr h) @[to_additive] theorem orderOf_dvd_iff_pow_eq_one {n : ℕ} : orderOf x ∣ n ↔ x ^ n = 1 := ⟨fun h => by rw [← pow_mod_orderOf, Nat.mod_eq_zero_of_dvd h, _root_.pow_zero], orderOf_dvd_of_pow_eq_one⟩ @[to_additive addOrderOf_smul_dvd] theorem orderOf_pow_dvd (n : ℕ) : orderOf (x ^ n) ∣ orderOf x := by rw [orderOf_dvd_iff_pow_eq_one, pow_right_comm, pow_orderOf_eq_one, one_pow] @[to_additive] lemma pow_injOn_Iio_orderOf : (Set.Iio <| orderOf x).InjOn (x ^ ·) := by simpa only [mul_left_iterate, mul_one] using iterate_injOn_Iio_minimalPeriod (f := (x * ·)) (x := 1) @[to_additive] protected lemma IsOfFinOrder.mem_powers_iff_mem_range_orderOf [DecidableEq G] (hx : IsOfFinOrder x) : y ∈ Submonoid.powers x ↔ y ∈ (Finset.range (orderOf x)).image (x ^ ·) := Finset.mem_range_iff_mem_finset_range_of_mod_eq' hx.orderOf_pos <| pow_mod_orderOf _ @[to_additive] protected lemma IsOfFinOrder.powers_eq_image_range_orderOf [DecidableEq G] (hx : IsOfFinOrder x) : (Submonoid.powers x : Set G) = (Finset.range (orderOf x)).image (x ^ ·) := Set.ext fun _ ↦ hx.mem_powers_iff_mem_range_orderOf @[to_additive] theorem pow_eq_one_iff_modEq : x ^ n = 1 ↔ n ≡ 0 [MOD orderOf x] := by rw [modEq_zero_iff_dvd, orderOf_dvd_iff_pow_eq_one] @[to_additive] theorem orderOf_map_dvd {H : Type*} [Monoid H] (ψ : G →* H) (x : G) : orderOf (ψ x) ∣ orderOf x := by apply orderOf_dvd_of_pow_eq_one rw [← map_pow, pow_orderOf_eq_one] apply map_one @[to_additive] theorem exists_pow_eq_self_of_coprime (h : n.Coprime (orderOf x)) : ∃ m : ℕ, (x ^ n) ^ m = x := by by_cases h0 : orderOf x = 0 · rw [h0, coprime_zero_right] at h exact ⟨1, by rw [h, pow_one, pow_one]⟩ by_cases h1 : orderOf x = 1 · exact ⟨0, by rw [orderOf_eq_one_iff.mp h1, one_pow, one_pow]⟩ obtain ⟨m, h⟩ := exists_mul_emod_eq_one_of_coprime h (one_lt_iff_ne_zero_and_ne_one.mpr ⟨h0, h1⟩) exact ⟨m, by rw [← pow_mul, ← pow_mod_orderOf, h, pow_one]⟩ /-- If `x^n = 1`, but `x^(n/p) ≠ 1` for all prime factors `p` of `n`, then `x` has order `n` in `G`. -/ @[to_additive addOrderOf_eq_of_nsmul_and_div_prime_nsmul "If `n * x = 0`, but `n/p * x ≠ 0` for all prime factors `p` of `n`, then `x` has order `n` in `G`."] theorem orderOf_eq_of_pow_and_pow_div_prime (hn : 0 < n) (hx : x ^ n = 1) (hd : ∀ p : ℕ, p.Prime → p ∣ n → x ^ (n / p) ≠ 1) : orderOf x = n := by -- Let `a` be `n/(orderOf x)`, and show `a = 1` obtain ⟨a, ha⟩ := exists_eq_mul_right_of_dvd (orderOf_dvd_of_pow_eq_one hx) suffices a = 1 by simp [this, ha] -- Assume `a` is not one... by_contra h have a_min_fac_dvd_p_sub_one : a.minFac ∣ n := by obtain ⟨b, hb⟩ : ∃ b : ℕ, a = b * a.minFac := exists_eq_mul_left_of_dvd a.minFac_dvd rw [hb, ← mul_assoc] at ha exact Dvd.intro_left (orderOf x * b) ha.symm -- Use the minimum prime factor of `a` as `p`. refine hd a.minFac (Nat.minFac_prime h) a_min_fac_dvd_p_sub_one ?_ rw [← orderOf_dvd_iff_pow_eq_one, Nat.dvd_div_iff_mul_dvd a_min_fac_dvd_p_sub_one, ha, mul_comm, Nat.mul_dvd_mul_iff_left (IsOfFinOrder.orderOf_pos _)] · exact Nat.minFac_dvd a · rw [isOfFinOrder_iff_pow_eq_one] exact Exists.intro n (id ⟨hn, hx⟩) @[to_additive] theorem orderOf_eq_orderOf_iff {H : Type*} [Monoid H] {y : H} : orderOf x = orderOf y ↔ ∀ n : ℕ, x ^ n = 1 ↔ y ^ n = 1 := by simp_rw [← isPeriodicPt_mul_iff_pow_eq_one, ← minimalPeriod_eq_minimalPeriod_iff, orderOf] /-- An injective homomorphism of monoids preserves orders of elements. -/ @[to_additive "An injective homomorphism of additive monoids preserves orders of elements."] theorem orderOf_injective {H : Type*} [Monoid H] (f : G →* H) (hf : Function.Injective f) (x : G) : orderOf (f x) = orderOf x := by simp_rw [orderOf_eq_orderOf_iff, ← f.map_pow, ← f.map_one, hf.eq_iff, forall_const] /-- A multiplicative equivalence preserves orders of elements. -/ @[to_additive (attr := simp) "An additive equivalence preserves orders of elements."] lemma MulEquiv.orderOf_eq {H : Type*} [Monoid H] (e : G ≃* H) (x : G) : orderOf (e x) = orderOf x := orderOf_injective e.toMonoidHom e.injective x @[to_additive] theorem Function.Injective.isOfFinOrder_iff [Monoid H] {f : G →* H} (hf : Injective f) : IsOfFinOrder (f x) ↔ IsOfFinOrder x := by rw [← orderOf_pos_iff, orderOf_injective f hf x, ← orderOf_pos_iff] @[to_additive (attr := norm_cast, simp)] theorem orderOf_submonoid {H : Submonoid G} (y : H) : orderOf (y : G) = orderOf y := orderOf_injective H.subtype Subtype.coe_injective y @[to_additive] theorem orderOf_units {y : Gˣ} : orderOf (y : G) = orderOf y := orderOf_injective (Units.coeHom G) Units.ext y /-- If the order of `x` is finite, then `x` is a unit with inverse `x ^ (orderOf x - 1)`. -/ @[to_additive (attr := simps) "If the additive order of `x` is finite, then `x` is an additive unit with inverse `(addOrderOf x - 1) • x`. "] noncomputable def IsOfFinOrder.unit {M} [Monoid M] {x : M} (hx : IsOfFinOrder x) : Mˣ := ⟨x, x ^ (orderOf x - 1), by rw [← _root_.pow_succ', tsub_add_cancel_of_le (by exact hx.orderOf_pos), pow_orderOf_eq_one], by rw [← _root_.pow_succ, tsub_add_cancel_of_le (by exact hx.orderOf_pos), pow_orderOf_eq_one]⟩ @[to_additive] lemma IsOfFinOrder.isUnit {M} [Monoid M] {x : M} (hx : IsOfFinOrder x) : IsUnit x := ⟨hx.unit, rfl⟩ variable (x) @[to_additive] theorem orderOf_pow' (h : n ≠ 0) : orderOf (x ^ n) = orderOf x / gcd (orderOf x) n := by unfold orderOf rw [← minimalPeriod_iterate_eq_div_gcd h, mul_left_iterate] @[to_additive] lemma orderOf_pow_of_dvd {x : G} {n : ℕ} (hn : n ≠ 0) (dvd : n ∣ orderOf x) : orderOf (x ^ n) = orderOf x / n := by rw [orderOf_pow' _ hn, Nat.gcd_eq_right dvd] @[to_additive] lemma orderOf_pow_orderOf_div {x : G} {n : ℕ} (hx : orderOf x ≠ 0) (hn : n ∣ orderOf x) : orderOf (x ^ (orderOf x / n)) = n := by rw [orderOf_pow_of_dvd _ (Nat.div_dvd_of_dvd hn), Nat.div_div_self hn hx] rw [← Nat.div_mul_cancel hn] at hx; exact left_ne_zero_of_mul hx variable (n) @[to_additive] protected lemma IsOfFinOrder.orderOf_pow (h : IsOfFinOrder x) : orderOf (x ^ n) = orderOf x / gcd (orderOf x) n := by unfold orderOf rw [← minimalPeriod_iterate_eq_div_gcd' h, mul_left_iterate] @[to_additive] lemma Nat.Coprime.orderOf_pow (h : (orderOf y).Coprime m) : orderOf (y ^ m) = orderOf y := by by_cases hg : IsOfFinOrder y · rw [hg.orderOf_pow y m , h.gcd_eq_one, Nat.div_one] · rw [m.coprime_zero_left.1 (orderOf_eq_zero hg ▸ h), pow_one] @[to_additive] lemma IsOfFinOrder.natCard_powers_le_orderOf (ha : IsOfFinOrder a) : Nat.card (powers a : Set G) ≤ orderOf a := by classical simpa [ha.powers_eq_image_range_orderOf, Finset.card_range, Nat.Iio_eq_range] using Finset.card_image_le (s := Finset.range (orderOf a)) @[to_additive] lemma IsOfFinOrder.finite_powers (ha : IsOfFinOrder a) : (powers a : Set G).Finite := by classical rw [ha.powers_eq_image_range_orderOf]; exact Finset.finite_toSet _ namespace Commute variable {x} @[to_additive] theorem orderOf_mul_dvd_lcm (h : Commute x y) : orderOf (x * y) ∣ Nat.lcm (orderOf x) (orderOf y) := by rw [orderOf, ← comp_mul_left] exact Function.Commute.minimalPeriod_of_comp_dvd_lcm h.function_commute_mul_left @[to_additive] theorem orderOf_dvd_lcm_mul (h : Commute x y): orderOf y ∣ Nat.lcm (orderOf x) (orderOf (x * y)) := by by_cases h0 : orderOf x = 0 · rw [h0, lcm_zero_left] apply dvd_zero conv_lhs => rw [← one_mul y, ← pow_orderOf_eq_one x, ← succ_pred_eq_of_pos (Nat.pos_of_ne_zero h0), _root_.pow_succ, mul_assoc] exact (((Commute.refl x).mul_right h).pow_left _).orderOf_mul_dvd_lcm.trans (lcm_dvd_iff.2 ⟨(orderOf_pow_dvd _).trans (dvd_lcm_left _ _), dvd_lcm_right _ _⟩) @[to_additive addOrderOf_add_dvd_mul_addOrderOf] theorem orderOf_mul_dvd_mul_orderOf (h : Commute x y): orderOf (x * y) ∣ orderOf x * orderOf y := dvd_trans h.orderOf_mul_dvd_lcm (lcm_dvd_mul _ _) @[to_additive addOrderOf_add_eq_mul_addOrderOf_of_coprime] theorem orderOf_mul_eq_mul_orderOf_of_coprime (h : Commute x y) (hco : (orderOf x).Coprime (orderOf y)) : orderOf (x * y) = orderOf x * orderOf y := by rw [orderOf, ← comp_mul_left] exact h.function_commute_mul_left.minimalPeriod_of_comp_eq_mul_of_coprime hco /-- Commuting elements of finite order are closed under multiplication. -/ @[to_additive "Commuting elements of finite additive order are closed under addition."] theorem isOfFinOrder_mul (h : Commute x y) (hx : IsOfFinOrder x) (hy : IsOfFinOrder y) : IsOfFinOrder (x * y) := orderOf_pos_iff.mp <| pos_of_dvd_of_pos h.orderOf_mul_dvd_mul_orderOf <| mul_pos hx.orderOf_pos hy.orderOf_pos /-- If each prime factor of `orderOf x` has higher multiplicity in `orderOf y`, and `x` commutes with `y`, then `x * y` has the same order as `y`. -/ @[to_additive addOrderOf_add_eq_right_of_forall_prime_mul_dvd "If each prime factor of `addOrderOf x` has higher multiplicity in `addOrderOf y`, and `x` commutes with `y`, then `x + y` has the same order as `y`."] theorem orderOf_mul_eq_right_of_forall_prime_mul_dvd (h : Commute x y) (hy : IsOfFinOrder y) (hdvd : ∀ p : ℕ, p.Prime → p ∣ orderOf x → p * orderOf x ∣ orderOf y) : orderOf (x * y) = orderOf y := by have hoy := hy.orderOf_pos have hxy := dvd_of_forall_prime_mul_dvd hdvd apply orderOf_eq_of_pow_and_pow_div_prime hoy <;> simp only [Ne, ← orderOf_dvd_iff_pow_eq_one] · exact h.orderOf_mul_dvd_lcm.trans (lcm_dvd hxy dvd_rfl) refine fun p hp hpy hd => hp.ne_one ?_ rw [← Nat.dvd_one, ← mul_dvd_mul_iff_right hoy.ne', one_mul, ← dvd_div_iff_mul_dvd hpy] refine (orderOf_dvd_lcm_mul h).trans (lcm_dvd ((dvd_div_iff_mul_dvd hpy).2 ?_) hd) by_cases h : p ∣ orderOf x exacts [hdvd p hp h, (hp.coprime_iff_not_dvd.2 h).mul_dvd_of_dvd_of_dvd hpy hxy] end Commute section PPrime variable {x n} {p : ℕ} [hp : Fact p.Prime] @[to_additive] theorem orderOf_eq_prime_iff : orderOf x = p ↔ x ^ p = 1 ∧ x ≠ 1 := by rw [orderOf, minimalPeriod_eq_prime_iff, isPeriodicPt_mul_iff_pow_eq_one, IsFixedPt, mul_one] /-- The backward direction of `orderOf_eq_prime_iff`. -/ @[to_additive "The backward direction of `addOrderOf_eq_prime_iff`."] theorem orderOf_eq_prime (hg : x ^ p = 1) (hg1 : x ≠ 1) : orderOf x = p := orderOf_eq_prime_iff.mpr ⟨hg, hg1⟩ @[to_additive addOrderOf_eq_prime_pow] theorem orderOf_eq_prime_pow (hnot : ¬x ^ p ^ n = 1) (hfin : x ^ p ^ (n + 1) = 1) : orderOf x = p ^ (n + 1) := by apply minimalPeriod_eq_prime_pow <;> rwa [isPeriodicPt_mul_iff_pow_eq_one] @[to_additive exists_addOrderOf_eq_prime_pow_iff] theorem exists_orderOf_eq_prime_pow_iff : (∃ k : ℕ, orderOf x = p ^ k) ↔ ∃ m : ℕ, x ^ (p : ℕ) ^ m = 1 := ⟨fun ⟨k, hk⟩ => ⟨k, by rw [← hk, pow_orderOf_eq_one]⟩, fun ⟨_, hm⟩ => by obtain ⟨k, _, hk⟩ := (Nat.dvd_prime_pow hp.elim).mp (orderOf_dvd_of_pow_eq_one hm) exact ⟨k, hk⟩⟩ end PPrime /-- The equivalence between `Fin (orderOf x)` and `Submonoid.powers x`, sending `i` to `x ^ i` -/ @[to_additive "The equivalence between `Fin (addOrderOf a)` and `AddSubmonoid.multiples a`, sending `i` to `i • a`"] noncomputable def finEquivPowers {x : G} (hx : IsOfFinOrder x) : Fin (orderOf x) ≃ powers x := Equiv.ofBijective (fun n ↦ ⟨x ^ (n : ℕ), ⟨n, rfl⟩⟩) ⟨fun ⟨_, h₁⟩ ⟨_, h₂⟩ ij ↦ Fin.ext (pow_injOn_Iio_orderOf h₁ h₂ (Subtype.mk_eq_mk.1 ij)), fun ⟨_, i, rfl⟩ ↦ ⟨⟨i % orderOf x, mod_lt _ hx.orderOf_pos⟩, Subtype.eq <| pow_mod_orderOf _ _⟩⟩ @[to_additive (attr := simp)] lemma finEquivPowers_apply {x : G} (hx : IsOfFinOrder x) {n : Fin (orderOf x)} : finEquivPowers hx n = ⟨x ^ (n : ℕ), n, rfl⟩ := rfl @[to_additive (attr := simp)] lemma finEquivPowers_symm_apply {x : G} (hx : IsOfFinOrder x) (n : ℕ) : (finEquivPowers hx).symm ⟨x ^ n, _, rfl⟩ = ⟨n % orderOf x, Nat.mod_lt _ hx.orderOf_pos⟩ := by rw [Equiv.symm_apply_eq, finEquivPowers_apply, Subtype.mk_eq_mk, ← pow_mod_orderOf, Fin.val_mk] variable {x n} (hx : IsOfFinOrder x) include hx @[to_additive] theorem IsOfFinOrder.pow_eq_pow_iff_modEq : x ^ n = x ^ m ↔ n ≡ m [MOD orderOf x] := by wlog hmn : m ≤ n generalizing m n · rw [eq_comm, ModEq.comm, this (le_of_not_le hmn)] obtain ⟨k, rfl⟩ := Nat.exists_eq_add_of_le hmn rw [pow_add, (hx.isUnit.pow _).mul_eq_left, pow_eq_one_iff_modEq] exact ⟨fun h ↦ Nat.ModEq.add_left _ h, fun h ↦ Nat.ModEq.add_left_cancel' _ h⟩ @[to_additive] lemma IsOfFinOrder.pow_inj_mod {n m : ℕ} : x ^ n = x ^ m ↔ n % orderOf x = m % orderOf x := hx.pow_eq_pow_iff_modEq end Monoid section CancelMonoid variable [LeftCancelMonoid G] {x y : G} {a : G} {m n : ℕ} @[to_additive] theorem pow_eq_pow_iff_modEq : x ^ n = x ^ m ↔ n ≡ m [MOD orderOf x] := by wlog hmn : m ≤ n generalizing m n · rw [eq_comm, ModEq.comm, this (le_of_not_le hmn)] obtain ⟨k, rfl⟩ := Nat.exists_eq_add_of_le hmn rw [← mul_one (x ^ m), pow_add, mul_left_cancel_iff, pow_eq_one_iff_modEq] exact ⟨fun h => Nat.ModEq.add_left _ h, fun h => Nat.ModEq.add_left_cancel' _ h⟩ @[to_additive (attr := simp)] lemma injective_pow_iff_not_isOfFinOrder : Injective (fun n : ℕ ↦ x ^ n) ↔ ¬IsOfFinOrder x := by refine ⟨fun h => not_isOfFinOrder_of_injective_pow h, fun h n m hnm => ?_⟩ rwa [pow_eq_pow_iff_modEq, orderOf_eq_zero_iff.mpr h, modEq_zero_iff] at hnm @[to_additive] lemma pow_inj_mod {n m : ℕ} : x ^ n = x ^ m ↔ n % orderOf x = m % orderOf x := pow_eq_pow_iff_modEq @[to_additive]
theorem pow_inj_iff_of_orderOf_eq_zero (h : orderOf x = 0) {n m : ℕ} : x ^ n = x ^ m ↔ n = m := by rw [pow_eq_pow_iff_modEq, h, modEq_zero_iff]
Mathlib/GroupTheory/OrderOfElement.lean
536
538
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Floris van Doorn, Violeta Hernández Palacios -/ import Mathlib.SetTheory.Ordinal.Family /-! # Ordinal exponential In this file we define the power function and the logarithm function on ordinals. The two are related by the lemma `Ordinal.opow_le_iff_le_log : b ^ c ≤ x ↔ c ≤ log b x` for nontrivial inputs `b`, `c`. -/ noncomputable section open Function Set Equiv Order open scoped Cardinal Ordinal universe u v w namespace Ordinal /-- The ordinal exponential, defined by transfinite recursion. We call this `opow` in theorems in order to disambiguate from other exponentials. -/ instance instPow : Pow Ordinal Ordinal := ⟨fun a b ↦ if a = 0 then 1 - b else limitRecOn b 1 (fun _ x ↦ x * a) fun o _ f ↦ ⨆ x : Iio o, f x.1 x.2⟩ private theorem opow_of_ne_zero {a b : Ordinal} (h : a ≠ 0) : a ^ b = limitRecOn b 1 (fun _ x ↦ x * a) fun o _ f ↦ ⨆ x : Iio o, f x.1 x.2 := if_neg h /-- `0 ^ a = 1` if `a = 0` and `0 ^ a = 0` otherwise. -/ theorem zero_opow' (a : Ordinal) : 0 ^ a = 1 - a := if_pos rfl theorem zero_opow_le (a : Ordinal) : (0 : Ordinal) ^ a ≤ 1 := by rw [zero_opow'] exact sub_le_self 1 a @[simp] theorem zero_opow {a : Ordinal} (a0 : a ≠ 0) : (0 : Ordinal) ^ a = 0 := by rwa [zero_opow', Ordinal.sub_eq_zero_iff_le, one_le_iff_ne_zero] @[simp] theorem opow_zero (a : Ordinal) : a ^ (0 : Ordinal) = 1 := by obtain rfl | h := eq_or_ne a 0 · rw [zero_opow', Ordinal.sub_zero] · rw [opow_of_ne_zero h, limitRecOn_zero] @[simp] theorem opow_succ (a b : Ordinal) : a ^ succ b = a ^ b * a := by obtain rfl | h := eq_or_ne a 0 · rw [zero_opow (succ_ne_zero b), mul_zero] · rw [opow_of_ne_zero h, opow_of_ne_zero h, limitRecOn_succ] theorem opow_limit {a b : Ordinal} (ha : a ≠ 0) (hb : IsLimit b) : a ^ b = ⨆ x : Iio b, a ^ x.1 := by simp_rw [opow_of_ne_zero ha, limitRecOn_limit _ _ _ _ hb] theorem opow_le_of_limit {a b c : Ordinal} (a0 : a ≠ 0) (h : IsLimit b) : a ^ b ≤ c ↔ ∀ b' < b, a ^ b' ≤ c := by rw [opow_limit a0 h, Ordinal.iSup_le_iff, Subtype.forall] rfl theorem lt_opow_of_limit {a b c : Ordinal} (b0 : b ≠ 0) (h : IsLimit c) : a < b ^ c ↔ ∃ c' < c, a < b ^ c' := by rw [← not_iff_not, not_exists] simp only [not_lt, opow_le_of_limit b0 h, exists_prop, not_and] @[simp] theorem opow_one (a : Ordinal) : a ^ (1 : Ordinal) = a := by rw [← succ_zero, opow_succ] simp only [opow_zero, one_mul] @[simp] theorem one_opow (a : Ordinal) : (1 : Ordinal) ^ a = 1 := by induction a using limitRecOn with | zero => simp only [opow_zero] | succ _ ih => simp only [opow_succ, ih, mul_one] | isLimit b l IH => refine eq_of_forall_ge_iff fun c => ?_ rw [opow_le_of_limit Ordinal.one_ne_zero l] exact ⟨fun H => by simpa only [opow_zero] using H 0 l.pos, fun H b' h => by rwa [IH _ h]⟩ theorem opow_pos {a : Ordinal} (b : Ordinal) (a0 : 0 < a) : 0 < a ^ b := by have h0 : 0 < a ^ (0 : Ordinal) := by simp only [opow_zero, zero_lt_one] induction b using limitRecOn with | zero => exact h0 | succ b IH => rw [opow_succ] exact mul_pos IH a0 | isLimit b l _ => exact (lt_opow_of_limit (Ordinal.pos_iff_ne_zero.1 a0) l).2 ⟨0, l.pos, h0⟩ theorem opow_ne_zero {a : Ordinal} (b : Ordinal) (a0 : a ≠ 0) : a ^ b ≠ 0 := Ordinal.pos_iff_ne_zero.1 <| opow_pos b <| Ordinal.pos_iff_ne_zero.2 a0 @[simp] theorem opow_eq_zero {a b : Ordinal} : a ^ b = 0 ↔ a = 0 ∧ b ≠ 0 := by obtain rfl | ha := eq_or_ne a 0 · obtain rfl | hb := eq_or_ne b 0 · simp · simp [hb] · simp [opow_ne_zero b ha, ha] @[simp, norm_cast] theorem opow_natCast (a : Ordinal) (n : ℕ) : a ^ (n : Ordinal) = a ^ n := by induction n with | zero => rw [Nat.cast_zero, opow_zero, pow_zero] | succ n IH => rw [Nat.cast_succ, add_one_eq_succ, opow_succ, pow_succ, IH] theorem isNormal_opow {a : Ordinal} (h : 1 < a) : IsNormal (a ^ ·) := have a0 : 0 < a := zero_lt_one.trans h ⟨fun b => by simpa only [mul_one, opow_succ] using (mul_lt_mul_iff_left (opow_pos b a0)).2 h, fun _ l _ => opow_le_of_limit (ne_of_gt a0) l⟩ theorem opow_lt_opow_iff_right {a b c : Ordinal} (a1 : 1 < a) : a ^ b < a ^ c ↔ b < c := (isNormal_opow a1).lt_iff theorem opow_le_opow_iff_right {a b c : Ordinal} (a1 : 1 < a) : a ^ b ≤ a ^ c ↔ b ≤ c := (isNormal_opow a1).le_iff theorem opow_right_inj {a b c : Ordinal} (a1 : 1 < a) : a ^ b = a ^ c ↔ b = c := (isNormal_opow a1).inj theorem isLimit_opow {a b : Ordinal} (a1 : 1 < a) : IsLimit b → IsLimit (a ^ b) := (isNormal_opow a1).isLimit theorem isLimit_opow_left {a b : Ordinal} (l : IsLimit a) (hb : b ≠ 0) : IsLimit (a ^ b) := by rcases zero_or_succ_or_limit b with (e | ⟨b, rfl⟩ | l') · exact absurd e hb · rw [opow_succ] exact isLimit_mul (opow_pos _ l.pos) l · exact isLimit_opow l.one_lt l' theorem opow_le_opow_right {a b c : Ordinal} (h₁ : 0 < a) (h₂ : b ≤ c) : a ^ b ≤ a ^ c := by rcases lt_or_eq_of_le (one_le_iff_pos.2 h₁) with h₁ | h₁ · exact (opow_le_opow_iff_right h₁).2 h₂ · subst a -- Porting note: `le_refl` is required. simp only [one_opow, le_refl] theorem opow_le_opow_left {a b : Ordinal} (c : Ordinal) (ab : a ≤ b) : a ^ c ≤ b ^ c := by by_cases a0 : a = 0 -- Porting note: `le_refl` is required. · subst a by_cases c0 : c = 0 · subst c simp only [opow_zero, le_refl] · simp only [zero_opow c0, Ordinal.zero_le] · induction c using limitRecOn with | zero => simp only [opow_zero, le_refl] | succ c IH => simpa only [opow_succ] using mul_le_mul' IH ab | isLimit c l IH => exact (opow_le_of_limit a0 l).2 fun b' h => (IH _ h).trans (opow_le_opow_right ((Ordinal.pos_iff_ne_zero.2 a0).trans_le ab) h.le) theorem opow_le_opow {a b c d : Ordinal} (hac : a ≤ c) (hbd : b ≤ d) (hc : 0 < c) : a ^ b ≤ c ^ d := (opow_le_opow_left b hac).trans (opow_le_opow_right hc hbd) theorem left_le_opow (a : Ordinal) {b : Ordinal} (b1 : 0 < b) : a ≤ a ^ b := by nth_rw 1 [← opow_one a] rcases le_or_gt a 1 with a1 | a1 · rcases lt_or_eq_of_le a1 with a0 | a1 · rw [lt_one_iff_zero] at a0 rw [a0, zero_opow Ordinal.one_ne_zero] exact Ordinal.zero_le _ rw [a1, one_opow, one_opow] rwa [opow_le_opow_iff_right a1, one_le_iff_pos] theorem left_lt_opow {a b : Ordinal} (ha : 1 < a) (hb : 1 < b) : a < a ^ b := by conv_lhs => rw [← opow_one a] rwa [opow_lt_opow_iff_right ha] theorem right_le_opow {a : Ordinal} (b : Ordinal) (a1 : 1 < a) : b ≤ a ^ b := (isNormal_opow a1).le_apply theorem opow_lt_opow_left_of_succ {a b c : Ordinal} (ab : a < b) : a ^ succ c < b ^ succ c := by rw [opow_succ, opow_succ] exact (mul_le_mul_right' (opow_le_opow_left c ab.le) a).trans_lt (mul_lt_mul_of_pos_left ab (opow_pos c ((Ordinal.zero_le a).trans_lt ab))) theorem opow_add (a b c : Ordinal) : a ^ (b + c) = a ^ b * a ^ c := by rcases eq_or_ne a 0 with (rfl | a0) · rcases eq_or_ne c 0 with (rfl | c0) · simp have : b + c ≠ 0 := ((Ordinal.pos_iff_ne_zero.2 c0).trans_le (le_add_left _ _)).ne' simp only [zero_opow c0, zero_opow this, mul_zero] rcases eq_or_lt_of_le (one_le_iff_ne_zero.2 a0) with (rfl | a1) · simp only [one_opow, mul_one] induction c using limitRecOn with | zero => simp | succ c IH => rw [add_succ, opow_succ, IH, opow_succ, mul_assoc] | isLimit c l IH => refine eq_of_forall_ge_iff fun d => (((isNormal_opow a1).trans (isNormal_add_right b)).limit_le l).trans ?_ dsimp only [Function.comp_def] simp +contextual only [IH] exact (((isNormal_mul_right <| opow_pos b (Ordinal.pos_iff_ne_zero.2 a0)).trans (isNormal_opow a1)).limit_le l).symm theorem opow_one_add (a b : Ordinal) : a ^ (1 + b) = a * a ^ b := by rw [opow_add, opow_one] theorem opow_dvd_opow (a : Ordinal) {b c : Ordinal} (h : b ≤ c) : a ^ b ∣ a ^ c := ⟨a ^ (c - b), by rw [← opow_add, Ordinal.add_sub_cancel_of_le h]⟩ theorem opow_dvd_opow_iff {a b c : Ordinal} (a1 : 1 < a) : a ^ b ∣ a ^ c ↔ b ≤ c := ⟨fun h => le_of_not_lt fun hn => not_le_of_lt ((opow_lt_opow_iff_right a1).2 hn) <| le_of_dvd (opow_ne_zero _ <| one_le_iff_ne_zero.1 <| a1.le) h, opow_dvd_opow _⟩ theorem opow_mul (a b c : Ordinal) : a ^ (b * c) = (a ^ b) ^ c := by by_cases b0 : b = 0; · simp only [b0, zero_mul, opow_zero, one_opow] by_cases a0 : a = 0 · subst a by_cases c0 : c = 0 · simp only [c0, mul_zero, opow_zero] simp only [zero_opow b0, zero_opow c0, zero_opow (mul_ne_zero b0 c0)] rcases eq_or_lt_of_le (one_le_iff_ne_zero.2 a0) with a1 | a1 · subst a1 simp only [one_opow] induction c using limitRecOn with | zero => simp only [mul_zero, opow_zero] | succ c IH => rw [mul_succ, opow_add, IH, opow_succ] | isLimit c l IH => refine eq_of_forall_ge_iff fun d => (((isNormal_opow a1).trans (isNormal_mul_right (Ordinal.pos_iff_ne_zero.2 b0))).limit_le l).trans ?_ dsimp only [Function.comp_def] simp +contextual only [IH] exact (opow_le_of_limit (opow_ne_zero _ a0) l).symm theorem opow_mul_add_pos {b v : Ordinal} (hb : b ≠ 0) (u : Ordinal) (hv : v ≠ 0) (w : Ordinal) : 0 < b ^ u * v + w := (opow_pos u <| Ordinal.pos_iff_ne_zero.2 hb).trans_le <| (le_mul_left _ <| Ordinal.pos_iff_ne_zero.2 hv).trans <| le_add_right _ _ theorem opow_mul_add_lt_opow_mul_succ {b u w : Ordinal} (v : Ordinal) (hw : w < b ^ u) : b ^ u * v + w < b ^ u * succ v := by rwa [mul_succ, add_lt_add_iff_left] theorem opow_mul_add_lt_opow_succ {b u v w : Ordinal} (hvb : v < b) (hw : w < b ^ u) : b ^ u * v + w < b ^ succ u := by convert (opow_mul_add_lt_opow_mul_succ v hw).trans_le (mul_le_mul_left' (succ_le_of_lt hvb) _) using 1 exact opow_succ b u /-! ### Ordinal logarithm -/ /-- The ordinal logarithm is the solution `u` to the equation `x = b ^ u * v + w` where `v < b` and `w < b ^ u`. -/ @[pp_nodot] def log (b : Ordinal) (x : Ordinal) : Ordinal := if 1 < b then pred (sInf { o | x < b ^ o }) else 0 /-- The set in the definition of `log` is nonempty. -/ private theorem log_nonempty {b x : Ordinal} (h : 1 < b) : { o : Ordinal | x < b ^ o }.Nonempty := ⟨_, succ_le_iff.1 (right_le_opow _ h)⟩ theorem log_def {b : Ordinal} (h : 1 < b) (x : Ordinal) : log b x = pred (sInf { o | x < b ^ o }) := if_pos h theorem log_of_left_le_one {b : Ordinal} (h : b ≤ 1) (x : Ordinal) : log b x = 0 := if_neg h.not_lt @[simp] theorem log_zero_left : ∀ b, log 0 b = 0 := log_of_left_le_one zero_le_one @[simp] theorem log_zero_right (b : Ordinal) : log b 0 = 0 := by obtain hb | hb := lt_or_le 1 b · rw [log_def hb, ← Ordinal.le_zero, pred_le, succ_zero] apply csInf_le' rw [mem_setOf, opow_one] exact bot_lt_of_lt hb · rw [log_of_left_le_one hb] @[simp] theorem log_one_left : ∀ b, log 1 b = 0 := log_of_left_le_one le_rfl theorem succ_log_def {b x : Ordinal} (hb : 1 < b) (hx : x ≠ 0) : succ (log b x) = sInf { o : Ordinal | x < b ^ o } := by let t := sInf { o : Ordinal | x < b ^ o } have : x < b ^ t := csInf_mem (log_nonempty hb) rcases zero_or_succ_or_limit t with (h | h | h) · refine ((one_le_iff_ne_zero.2 hx).not_lt ?_).elim simpa only [h, opow_zero] using this · rw [show log b x = pred t from log_def hb x, succ_pred_iff_is_succ.2 h] · rcases (lt_opow_of_limit (zero_lt_one.trans hb).ne' h).1 this with ⟨a, h₁, h₂⟩ exact h₁.not_le.elim ((le_csInf_iff'' (log_nonempty hb)).1 le_rfl a h₂) theorem lt_opow_succ_log_self {b : Ordinal} (hb : 1 < b) (x : Ordinal) : x < b ^ succ (log b x) := by rcases eq_or_ne x 0 with (rfl | hx) · apply opow_pos _ (zero_lt_one.trans hb) · rw [succ_log_def hb hx] exact csInf_mem (log_nonempty hb) theorem opow_log_le_self (b : Ordinal) {x : Ordinal} (hx : x ≠ 0) : b ^ log b x ≤ x := by
rcases eq_or_ne b 0 with (rfl | b0) · exact (zero_opow_le _).trans (one_le_iff_ne_zero.2 hx) rcases lt_or_eq_of_le (one_le_iff_ne_zero.2 b0) with (hb | rfl) · refine le_of_not_lt fun h => (lt_succ (log b x)).not_le ?_ have := @csInf_le' _ _ { o | x < b ^ o } _ h rwa [← succ_log_def hb hx] at this · rwa [one_opow, one_le_iff_ne_zero] /-- `opow b` and `log b` (almost) form a Galois connection.
Mathlib/SetTheory/Ordinal/Exponential.lean
319
327
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Data.Multiset.ZeroCons /-! # Basic results on multisets -/ -- No algebra should be required assert_not_exists Monoid universe v open List Subtype Nat Function variable {α : Type*} {β : Type v} {γ : Type*} namespace Multiset /-! ### `Multiset.toList` -/ section ToList /-- Produces a list of the elements in the multiset using choice. -/ noncomputable def toList (s : Multiset α) := s.out @[simp, norm_cast] theorem coe_toList (s : Multiset α) : (s.toList : Multiset α) = s := s.out_eq' @[simp] theorem toList_eq_nil {s : Multiset α} : s.toList = [] ↔ s = 0 := by rw [← coe_eq_zero, coe_toList] theorem empty_toList {s : Multiset α} : s.toList.isEmpty ↔ s = 0 := by simp @[simp] theorem toList_zero : (Multiset.toList 0 : List α) = [] := toList_eq_nil.mpr rfl @[simp] theorem mem_toList {a : α} {s : Multiset α} : a ∈ s.toList ↔ a ∈ s := by rw [← mem_coe, coe_toList] @[simp] theorem toList_eq_singleton_iff {a : α} {m : Multiset α} : m.toList = [a] ↔ m = {a} := by rw [← perm_singleton, ← coe_eq_coe, coe_toList, coe_singleton] @[simp] theorem toList_singleton (a : α) : ({a} : Multiset α).toList = [a] := Multiset.toList_eq_singleton_iff.2 rfl @[simp] theorem length_toList (s : Multiset α) : s.toList.length = card s := by rw [← coe_card, coe_toList] end ToList /-! ### Induction principles -/ /-- The strong induction principle for multisets. -/ @[elab_as_elim] def strongInductionOn {p : Multiset α → Sort*} (s : Multiset α) (ih : ∀ s, (∀ t < s, p t) → p s) : p s := (ih s) fun t _h => strongInductionOn t ih termination_by card s decreasing_by exact card_lt_card _h theorem strongInductionOn_eq {p : Multiset α → Sort*} (s : Multiset α) (H) : @strongInductionOn _ p s H = H s fun t _h => @strongInductionOn _ p t H := by rw [strongInductionOn] @[elab_as_elim] theorem case_strongInductionOn {p : Multiset α → Prop} (s : Multiset α) (h₀ : p 0) (h₁ : ∀ a s, (∀ t ≤ s, p t) → p (a ::ₘ s)) : p s := Multiset.strongInductionOn s fun s => Multiset.induction_on s (fun _ => h₀) fun _a _s _ ih => (h₁ _ _) fun _t h => ih _ <| lt_of_le_of_lt h <| lt_cons_self _ _ /-- Suppose that, given that `p t` can be defined on all supersets of `s` of cardinality less than `n`, one knows how to define `p s`. Then one can inductively define `p s` for all multisets `s` of cardinality less than `n`, starting from multisets of card `n` and iterating. This can be used either to define data, or to prove properties. -/ def strongDownwardInduction {p : Multiset α → Sort*} {n : ℕ} (H : ∀ t₁, (∀ {t₂ : Multiset α}, card t₂ ≤ n → t₁ < t₂ → p t₂) → card t₁ ≤ n → p t₁) (s : Multiset α) : card s ≤ n → p s := H s fun {t} ht _h => strongDownwardInduction H t ht termination_by n - card s decreasing_by simp_wf; have := (card_lt_card _h); omega theorem strongDownwardInduction_eq {p : Multiset α → Sort*} {n : ℕ} (H : ∀ t₁, (∀ {t₂ : Multiset α}, card t₂ ≤ n → t₁ < t₂ → p t₂) → card t₁ ≤ n → p t₁) (s : Multiset α) : strongDownwardInduction H s = H s fun ht _hst => strongDownwardInduction H _ ht := by rw [strongDownwardInduction] /-- Analogue of `strongDownwardInduction` with order of arguments swapped. -/ @[elab_as_elim] def strongDownwardInductionOn {p : Multiset α → Sort*} {n : ℕ} : ∀ s : Multiset α, (∀ t₁, (∀ {t₂ : Multiset α}, card t₂ ≤ n → t₁ < t₂ → p t₂) → card t₁ ≤ n → p t₁) → card s ≤ n → p s := fun s H => strongDownwardInduction H s theorem strongDownwardInductionOn_eq {p : Multiset α → Sort*} (s : Multiset α) {n : ℕ} (H : ∀ t₁, (∀ {t₂ : Multiset α}, card t₂ ≤ n → t₁ < t₂ → p t₂) → card t₁ ≤ n → p t₁) : s.strongDownwardInductionOn H = H s fun {t} ht _h => t.strongDownwardInductionOn H ht := by dsimp only [strongDownwardInductionOn] rw [strongDownwardInduction] section Choose variable (p : α → Prop) [DecidablePred p] (l : Multiset α) /-- Given a proof `hp` that there exists a unique `a ∈ l` such that `p a`, `chooseX p l hp` returns that `a` together with proofs of `a ∈ l` and `p a`. -/ def chooseX : ∀ _hp : ∃! a, a ∈ l ∧ p a, { a // a ∈ l ∧ p a } := Quotient.recOn l (fun l' ex_unique => List.chooseX p l' (ExistsUnique.exists ex_unique)) (by intros a b _ funext hp suffices all_equal : ∀ x y : { t // t ∈ b ∧ p t }, x = y by apply all_equal rintro ⟨x, px⟩ ⟨y, py⟩ rcases hp with ⟨z, ⟨_z_mem_l, _pz⟩, z_unique⟩ congr calc x = z := z_unique x px _ = y := (z_unique y py).symm ) /-- Given a proof `hp` that there exists a unique `a ∈ l` such that `p a`, `choose p l hp` returns that `a`. -/ def choose (hp : ∃! a, a ∈ l ∧ p a) : α := chooseX p l hp theorem choose_spec (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) := (chooseX p l hp).property theorem choose_mem (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1 theorem choose_property (hp : ∃! a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2 end Choose variable (α) in /-- The equivalence between lists and multisets of a subsingleton type. -/ def subsingletonEquiv [Subsingleton α] : List α ≃ Multiset α where toFun := ofList invFun := (Quot.lift id) fun (a b : List α) (h : a ~ b) => (List.ext_get h.length_eq) fun _ _ _ => Subsingleton.elim _ _ left_inv _ := rfl right_inv m := Quot.inductionOn m fun _ => rfl @[simp] theorem coe_subsingletonEquiv [Subsingleton α] : (subsingletonEquiv α : List α → Multiset α) = ofList := rfl section SizeOf set_option linter.deprecated false in @[deprecated "Deprecated without replacement." (since := "2025-02-07")] theorem sizeOf_lt_sizeOf_of_mem [SizeOf α] {x : α} {s : Multiset α} (hx : x ∈ s) : SizeOf.sizeOf x < SizeOf.sizeOf s := by induction s using Quot.inductionOn exact List.sizeOf_lt_sizeOf_of_mem hx end SizeOf end Multiset
Mathlib/Data/Multiset/Basic.lean
484
485
/- Copyright (c) 2019 Amelia Livingston. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Amelia Livingston, Jireh Loreaux -/ import Mathlib.Algebra.GroupWithZero.Hom import Mathlib.Algebra.Ring.Defs import Mathlib.Algebra.Ring.Basic /-! # Homomorphisms of semirings and rings This file defines bundled homomorphisms of (non-unital) semirings and rings. As with monoid and groups, we use the same structure `RingHom a β`, a.k.a. `α →+* β`, for both types of homomorphisms. ## Main definitions * `NonUnitalRingHom`: Non-unital (semi)ring homomorphisms. Additive monoid homomorphism which preserve multiplication. * `RingHom`: (Semi)ring homomorphisms. Monoid homomorphisms which are also additive monoid homomorphism. ## Notations * `→ₙ+*`: Non-unital (semi)ring homs * `→+*`: (Semi)ring homs ## Implementation notes * There's a coercion from bundled homs to fun, and the canonical notation is to use the bundled hom as a function via this coercion. * There is no `SemiringHom` -- the idea is that `RingHom` is used. The constructor for a `RingHom` between semirings needs a proof of `map_zero`, `map_one` and `map_add` as well as `map_mul`; a separate constructor `RingHom.mk'` will construct ring homs between rings from monoid homs given only a proof that addition is preserved. ## Tags `RingHom`, `SemiringHom` -/ assert_not_exists Function.Injective.mulZeroClass semigroupDvd Units.map Set.range open Function variable {F α β γ : Type*} /-- Bundled non-unital semiring homomorphisms `α →ₙ+* β`; use this for bundled non-unital ring homomorphisms too. When possible, instead of parametrizing results over `(f : α →ₙ+* β)`, you should parametrize over `(F : Type*) [NonUnitalRingHomClass F α β] (f : F)`. When you extend this structure, make sure to extend `NonUnitalRingHomClass`. -/ structure NonUnitalRingHom (α β : Type*) [NonUnitalNonAssocSemiring α] [NonUnitalNonAssocSemiring β] extends α →ₙ* β, α →+ β /-- `α →ₙ+* β` denotes the type of non-unital ring homomorphisms from `α` to `β`. -/ infixr:25 " →ₙ+* " => NonUnitalRingHom /-- Reinterpret a non-unital ring homomorphism `f : α →ₙ+* β` as a semigroup homomorphism `α →ₙ* β`. The `simp`-normal form is `(f : α →ₙ* β)`. -/ add_decl_doc NonUnitalRingHom.toMulHom /-- Reinterpret a non-unital ring homomorphism `f : α →ₙ+* β` as an additive monoid homomorphism `α →+ β`. The `simp`-normal form is `(f : α →+ β)`. -/ add_decl_doc NonUnitalRingHom.toAddMonoidHom section NonUnitalRingHomClass /-- `NonUnitalRingHomClass F α β` states that `F` is a type of non-unital (semi)ring homomorphisms. You should extend this class when you extend `NonUnitalRingHom`. -/ class NonUnitalRingHomClass (F : Type*) (α β : outParam Type*) [NonUnitalNonAssocSemiring α] [NonUnitalNonAssocSemiring β] [FunLike F α β] : Prop extends MulHomClass F α β, AddMonoidHomClass F α β variable [NonUnitalNonAssocSemiring α] [NonUnitalNonAssocSemiring β] [FunLike F α β] variable [NonUnitalRingHomClass F α β] /-- Turn an element of a type `F` satisfying `NonUnitalRingHomClass F α β` into an actual `NonUnitalRingHom`. This is declared as the default coercion from `F` to `α →ₙ+* β`. -/ @[coe] def NonUnitalRingHomClass.toNonUnitalRingHom (f : F) : α →ₙ+* β := { (f : α →ₙ* β), (f : α →+ β) with } /-- Any type satisfying `NonUnitalRingHomClass` can be cast into `NonUnitalRingHom` via `NonUnitalRingHomClass.toNonUnitalRingHom`. -/ instance : CoeTC F (α →ₙ+* β) := ⟨NonUnitalRingHomClass.toNonUnitalRingHom⟩ end NonUnitalRingHomClass namespace NonUnitalRingHom section coe variable [NonUnitalNonAssocSemiring α] [NonUnitalNonAssocSemiring β] instance : FunLike (α →ₙ+* β) α β where coe f := f.toFun coe_injective' f g h := by cases f cases g congr apply DFunLike.coe_injective' exact h instance : NonUnitalRingHomClass (α →ₙ+* β) α β where map_add := NonUnitalRingHom.map_add' map_zero := NonUnitalRingHom.map_zero' map_mul f := f.map_mul' initialize_simps_projections NonUnitalRingHom (toFun → apply) @[simp] theorem coe_toMulHom (f : α →ₙ+* β) : ⇑f.toMulHom = f := rfl @[simp] theorem coe_mulHom_mk (f : α → β) (h₁ h₂ h₃) : ((⟨⟨f, h₁⟩, h₂, h₃⟩ : α →ₙ+* β) : α →ₙ* β) = ⟨f, h₁⟩ := rfl theorem coe_toAddMonoidHom (f : α →ₙ+* β) : ⇑f.toAddMonoidHom = f := rfl @[simp] theorem coe_addMonoidHom_mk (f : α → β) (h₁ h₂ h₃) : ((⟨⟨f, h₁⟩, h₂, h₃⟩ : α →ₙ+* β) : α →+ β) = ⟨⟨f, h₂⟩, h₃⟩ := rfl /-- Copy of a `RingHom` with a new `toFun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (f : α →ₙ+* β) (f' : α → β) (h : f' = f) : α →ₙ+* β := { f.toMulHom.copy f' h, f.toAddMonoidHom.copy f' h with } @[simp] theorem coe_copy (f : α →ₙ+* β) (f' : α → β) (h : f' = f) : ⇑(f.copy f' h) = f' := rfl theorem copy_eq (f : α →ₙ+* β) (f' : α → β) (h : f' = f) : f.copy f' h = f := DFunLike.ext' h end coe section variable [NonUnitalNonAssocSemiring α] [NonUnitalNonAssocSemiring β] @[ext] theorem ext ⦃f g : α →ₙ+* β⦄ : (∀ x, f x = g x) → f = g := DFunLike.ext _ _ @[simp] theorem mk_coe (f : α →ₙ+* β) (h₁ h₂ h₃) : NonUnitalRingHom.mk (MulHom.mk f h₁) h₂ h₃ = f := ext fun _ => rfl theorem coe_addMonoidHom_injective : Injective fun f : α →ₙ+* β => (f : α →+ β) := Injective.of_comp (f := DFunLike.coe) DFunLike.coe_injective theorem coe_mulHom_injective : Injective fun f : α →ₙ+* β => (f : α →ₙ* β) := Injective.of_comp (f := DFunLike.coe) DFunLike.coe_injective end variable [NonUnitalNonAssocSemiring α] [NonUnitalNonAssocSemiring β] /-- The identity non-unital ring homomorphism from a non-unital semiring to itself. -/ protected def id (α : Type*) [NonUnitalNonAssocSemiring α] : α →ₙ+* α where toFun := id map_mul' _ _ := rfl map_zero' := rfl map_add' _ _ := rfl instance : Zero (α →ₙ+* β) := ⟨{ toFun := 0, map_mul' := fun _ _ => (mul_zero (0 : β)).symm, map_zero' := rfl, map_add' := fun _ _ => (add_zero (0 : β)).symm }⟩ instance : Inhabited (α →ₙ+* β) := ⟨0⟩ @[simp] theorem coe_zero : ⇑(0 : α →ₙ+* β) = 0 := rfl @[simp] theorem zero_apply (x : α) : (0 : α →ₙ+* β) x = 0 := rfl @[simp] theorem id_apply (x : α) : NonUnitalRingHom.id α x = x := rfl @[simp] theorem coe_addMonoidHom_id : (NonUnitalRingHom.id α : α →+ α) = AddMonoidHom.id α := rfl @[simp] theorem coe_mulHom_id : (NonUnitalRingHom.id α : α →ₙ* α) = MulHom.id α := rfl variable [NonUnitalNonAssocSemiring γ] /-- Composition of non-unital ring homomorphisms is a non-unital ring homomorphism. -/ def comp (g : β →ₙ+* γ) (f : α →ₙ+* β) : α →ₙ+* γ := { g.toMulHom.comp f.toMulHom, g.toAddMonoidHom.comp f.toAddMonoidHom with } /-- Composition of non-unital ring homomorphisms is associative. -/ theorem comp_assoc {δ} {_ : NonUnitalNonAssocSemiring δ} (f : α →ₙ+* β) (g : β →ₙ+* γ) (h : γ →ₙ+* δ) : (h.comp g).comp f = h.comp (g.comp f) := rfl @[simp] theorem coe_comp (g : β →ₙ+* γ) (f : α →ₙ+* β) : ⇑(g.comp f) = g ∘ f := rfl @[simp] theorem comp_apply (g : β →ₙ+* γ) (f : α →ₙ+* β) (x : α) : g.comp f x = g (f x) := rfl @[simp] theorem coe_comp_addMonoidHom (g : β →ₙ+* γ) (f : α →ₙ+* β) : AddMonoidHom.mk ⟨g ∘ f, (g.comp f).map_zero'⟩ (g.comp f).map_add' = (g : β →+ γ).comp f := rfl @[simp] theorem coe_comp_mulHom (g : β →ₙ+* γ) (f : α →ₙ+* β) : MulHom.mk (g ∘ f) (g.comp f).map_mul' = (g : β →ₙ* γ).comp f := rfl @[simp] theorem comp_zero (g : β →ₙ+* γ) : g.comp (0 : α →ₙ+* β) = 0 := by ext simp @[simp] theorem zero_comp (f : α →ₙ+* β) : (0 : β →ₙ+* γ).comp f = 0 := by ext rfl @[simp] theorem comp_id (f : α →ₙ+* β) : f.comp (NonUnitalRingHom.id α) = f := ext fun _ => rfl @[simp] theorem id_comp (f : α →ₙ+* β) : (NonUnitalRingHom.id β).comp f = f := ext fun _ => rfl instance : MonoidWithZero (α →ₙ+* α) where one := NonUnitalRingHom.id α mul := comp mul_one := comp_id one_mul := id_comp mul_assoc _ _ _ := comp_assoc _ _ _ zero := 0 mul_zero := comp_zero zero_mul := zero_comp theorem one_def : (1 : α →ₙ+* α) = NonUnitalRingHom.id α := rfl @[simp] theorem coe_one : ⇑(1 : α →ₙ+* α) = id := rfl theorem mul_def (f g : α →ₙ+* α) : f * g = f.comp g := rfl @[simp] theorem coe_mul (f g : α →ₙ+* α) : ⇑(f * g) = f ∘ g := rfl @[simp] theorem cancel_right {g₁ g₂ : β →ₙ+* γ} {f : α →ₙ+* β} (hf : Surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨fun h => ext <| hf.forall.2 (NonUnitalRingHom.ext_iff.1 h), fun h => h ▸ rfl⟩ @[simp] theorem cancel_left {g : β →ₙ+* γ} {f₁ f₂ : α →ₙ+* β} (hg : Injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨fun h => ext fun x => hg <| by rw [← comp_apply, h, comp_apply], fun h => h ▸ rfl⟩ end NonUnitalRingHom /-- Bundled semiring homomorphisms; use this for bundled ring homomorphisms too. This extends from both `MonoidHom` and `MonoidWithZeroHom` in order to put the fields in a sensible order, even though `MonoidWithZeroHom` already extends `MonoidHom`. -/ structure RingHom (α : Type*) (β : Type*) [NonAssocSemiring α] [NonAssocSemiring β] extends α →* β, α →+ β, α →ₙ+* β, α →*₀ β /-- `α →+* β` denotes the type of ring homomorphisms from `α` to `β`. -/ infixr:25 " →+* " => RingHom /-- Reinterpret a ring homomorphism `f : α →+* β` as a monoid with zero homomorphism `α →*₀ β`. The `simp`-normal form is `(f : α →*₀ β)`. -/ add_decl_doc RingHom.toMonoidWithZeroHom /-- Reinterpret a ring homomorphism `f : α →+* β` as a monoid homomorphism `α →* β`. The `simp`-normal form is `(f : α →* β)`. -/ add_decl_doc RingHom.toMonoidHom /-- Reinterpret a ring homomorphism `f : α →+* β` as an additive monoid homomorphism `α →+ β`. The `simp`-normal form is `(f : α →+ β)`. -/ add_decl_doc RingHom.toAddMonoidHom /-- Reinterpret a ring homomorphism `f : α →+* β` as a non-unital ring homomorphism `α →ₙ+* β`. The `simp`-normal form is `(f : α →ₙ+* β)`. -/ add_decl_doc RingHom.toNonUnitalRingHom section RingHomClass /-- `RingHomClass F α β` states that `F` is a type of (semi)ring homomorphisms. You should extend this class when you extend `RingHom`. This extends from both `MonoidHomClass` and `MonoidWithZeroHomClass` in order to put the fields in a sensible order, even though `MonoidWithZeroHomClass` already extends `MonoidHomClass`. -/ class RingHomClass (F : Type*) (α β : outParam Type*) [NonAssocSemiring α] [NonAssocSemiring β] [FunLike F α β] : Prop extends MonoidHomClass F α β, AddMonoidHomClass F α β, MonoidWithZeroHomClass F α β variable [FunLike F α β] -- See note [implicit instance arguments]. variable {_ : NonAssocSemiring α} {_ : NonAssocSemiring β} [RingHomClass F α β] /-- Turn an element of a type `F` satisfying `RingHomClass F α β` into an actual `RingHom`. This is declared as the default coercion from `F` to `α →+* β`. -/ @[coe] def RingHomClass.toRingHom (f : F) : α →+* β := { (f : α →* β), (f : α →+ β) with } /-- Any type satisfying `RingHomClass` can be cast into `RingHom` via `RingHomClass.toRingHom`. -/ instance : CoeTC F (α →+* β) := ⟨RingHomClass.toRingHom⟩ instance (priority := 100) RingHomClass.toNonUnitalRingHomClass : NonUnitalRingHomClass F α β := { ‹RingHomClass F α β› with } end RingHomClass namespace RingHom section coe /-! Throughout this section, some `Semiring` arguments are specified with `{}` instead of `[]`. See note [implicit instance arguments]. -/ variable {_ : NonAssocSemiring α} {_ : NonAssocSemiring β} instance instFunLike : FunLike (α →+* β) α β where coe f := f.toFun coe_injective' f g h := by cases f cases g congr apply DFunLike.coe_injective' exact h instance instRingHomClass : RingHomClass (α →+* β) α β where map_add := RingHom.map_add' map_zero := RingHom.map_zero' map_mul f := f.map_mul' map_one f := f.map_one' initialize_simps_projections RingHom (toFun → apply) theorem toFun_eq_coe (f : α →+* β) : f.toFun = f := rfl @[simp] theorem coe_mk (f : α →* β) (h₁ h₂) : ((⟨f, h₁, h₂⟩ : α →+* β) : α → β) = f := rfl @[simp] theorem coe_coe {F : Type*} [FunLike F α β] [RingHomClass F α β] (f : F) : ((f : α →+* β) : α → β) = f := rfl attribute [coe] RingHom.toMonoidHom instance coeToMonoidHom : Coe (α →+* β) (α →* β) := ⟨RingHom.toMonoidHom⟩ @[simp] theorem toMonoidHom_eq_coe (f : α →+* β) : f.toMonoidHom = f := rfl theorem toMonoidWithZeroHom_eq_coe (f : α →+* β) : (f.toMonoidWithZeroHom : α → β) = f := by rfl @[simp] theorem coe_monoidHom_mk (f : α →* β) (h₁ h₂) : ((⟨f, h₁, h₂⟩ : α →+* β) : α →* β) = f := rfl @[simp] theorem toAddMonoidHom_eq_coe (f : α →+* β) : f.toAddMonoidHom = f := rfl @[simp] theorem coe_addMonoidHom_mk (f : α → β) (h₁ h₂ h₃ h₄) : ((⟨⟨⟨f, h₁⟩, h₂⟩, h₃, h₄⟩ : α →+* β) : α →+ β) = ⟨⟨f, h₃⟩, h₄⟩ := rfl /-- Copy of a `RingHom` with a new `toFun` equal to the old one. Useful to fix definitional equalities. -/ def copy (f : α →+* β) (f' : α → β) (h : f' = f) : α →+* β := { f.toMonoidWithZeroHom.copy f' h, f.toAddMonoidHom.copy f' h with } @[simp] theorem coe_copy (f : α →+* β) (f' : α → β) (h : f' = f) : ⇑(f.copy f' h) = f' := rfl theorem copy_eq (f : α →+* β) (f' : α → β) (h : f' = f) : f.copy f' h = f := DFunLike.ext' h end coe section variable {_ : NonAssocSemiring α} {_ : NonAssocSemiring β} (f : α →+* β) protected theorem congr_fun {f g : α →+* β} (h : f = g) (x : α) : f x = g x := DFunLike.congr_fun h x protected theorem congr_arg (f : α →+* β) {x y : α} (h : x = y) : f x = f y := DFunLike.congr_arg f h theorem coe_inj ⦃f g : α →+* β⦄ (h : (f : α → β) = g) : f = g := DFunLike.coe_injective h @[ext] theorem ext ⦃f g : α →+* β⦄ : (∀ x, f x = g x) → f = g := DFunLike.ext _ _ @[simp] theorem mk_coe (f : α →+* β) (h₁ h₂ h₃ h₄) : RingHom.mk ⟨⟨f, h₁⟩, h₂⟩ h₃ h₄ = f := ext fun _ => rfl theorem coe_addMonoidHom_injective : Injective (fun f : α →+* β => (f : α →+ β)) := fun _ _ h => ext <| DFunLike.congr_fun (F := α →+ β) h theorem coe_monoidHom_injective : Injective (fun f : α →+* β => (f : α →* β)) := Injective.of_comp (f := DFunLike.coe) DFunLike.coe_injective /-- Ring homomorphisms map zero to zero. -/ protected theorem map_zero (f : α →+* β) : f 0 = 0 := map_zero f /-- Ring homomorphisms map one to one. -/ protected theorem map_one (f : α →+* β) : f 1 = 1 := map_one f /-- Ring homomorphisms preserve addition. -/ protected theorem map_add (f : α →+* β) : ∀ a b, f (a + b) = f a + f b := map_add f /-- Ring homomorphisms preserve multiplication. -/ protected theorem map_mul (f : α →+* β) : ∀ a b, f (a * b) = f a * f b := map_mul f /-- `f : α →+* β` has a trivial codomain iff `f 1 = 0`. -/ theorem codomain_trivial_iff_map_one_eq_zero : (0 : β) = 1 ↔ f 1 = 0 := by rw [map_one, eq_comm] /-- `f : α →+* β` has a trivial codomain iff it has a trivial range. -/ theorem codomain_trivial_iff_range_trivial : (0 : β) = 1 ↔ ∀ x, f x = 0 := f.codomain_trivial_iff_map_one_eq_zero.trans ⟨fun h x => by rw [← mul_one x, map_mul, h, mul_zero], fun h => h 1⟩ /-- `f : α →+* β` doesn't map `1` to `0` if `β` is nontrivial -/ theorem map_one_ne_zero [Nontrivial β] : f 1 ≠ 0 := mt f.codomain_trivial_iff_map_one_eq_zero.mpr zero_ne_one include f in /-- If there is a homomorphism `f : α →+* β` and `β` is nontrivial, then `α` is nontrivial. -/ theorem domain_nontrivial [Nontrivial β] : Nontrivial α := ⟨⟨1, 0, mt (fun h => show f 1 = 0 by rw [h, map_zero]) f.map_one_ne_zero⟩⟩ theorem codomain_trivial (f : α →+* β) [h : Subsingleton α] : Subsingleton β := (subsingleton_or_nontrivial β).resolve_right fun _ => not_nontrivial_iff_subsingleton.mpr h f.domain_nontrivial end /-- Ring homomorphisms preserve additive inverse. -/ protected theorem map_neg [NonAssocRing α] [NonAssocRing β] (f : α →+* β) (x : α) : f (-x) = -f x := map_neg f x /-- Ring homomorphisms preserve subtraction. -/ protected theorem map_sub [NonAssocRing α] [NonAssocRing β] (f : α →+* β) (x y : α) : f (x - y) = f x - f y := map_sub f x y /-- Makes a ring homomorphism from a monoid homomorphism of rings which preserves addition. -/ def mk' [NonAssocSemiring α] [NonAssocRing β] (f : α →* β) (map_add : ∀ a b, f (a + b) = f a + f b) : α →+* β := { AddMonoidHom.mk' f map_add, f with } variable {_ : NonAssocSemiring α} {_ : NonAssocSemiring β} /-- The identity ring homomorphism from a semiring to itself. -/ def id (α : Type*) [NonAssocSemiring α] : α →+* α where toFun := _root_.id map_zero' := rfl map_one' := rfl map_add' _ _ := rfl map_mul' _ _ := rfl instance : Inhabited (α →+* α) := ⟨id α⟩ @[simp, norm_cast] theorem coe_id : ⇑(RingHom.id α) = _root_.id := rfl @[simp] theorem id_apply (x : α) : RingHom.id α x = x := rfl @[simp] theorem coe_addMonoidHom_id : (id α : α →+ α) = AddMonoidHom.id α := rfl @[simp] theorem coe_monoidHom_id : (id α : α →* α) = MonoidHom.id α := rfl variable {_ : NonAssocSemiring γ} /-- Composition of ring homomorphisms is a ring homomorphism. -/ def comp (g : β →+* γ) (f : α →+* β) : α →+* γ := { g.toNonUnitalRingHom.comp f.toNonUnitalRingHom with toFun := g ∘ f, map_one' := by simp } /-- Composition of semiring homomorphisms is associative. -/ theorem comp_assoc {δ} {_ : NonAssocSemiring δ} (f : α →+* β) (g : β →+* γ) (h : γ →+* δ) : (h.comp g).comp f = h.comp (g.comp f) := rfl @[simp] theorem coe_comp (hnp : β →+* γ) (hmn : α →+* β) : (hnp.comp hmn : α → γ) = hnp ∘ hmn := rfl theorem comp_apply (hnp : β →+* γ) (hmn : α →+* β) (x : α) : (hnp.comp hmn : α → γ) x = hnp (hmn x) := rfl @[simp] theorem comp_id (f : α →+* β) : f.comp (id α) = f := ext fun _ => rfl @[simp] theorem id_comp (f : α →+* β) : (id β).comp f = f := ext fun _ => rfl instance instOne : One (α →+* α) where one := id _ instance instMul : Mul (α →+* α) where mul := comp lemma one_def : (1 : α →+* α) = id α := rfl lemma mul_def (f g : α →+* α) : f * g = f.comp g := rfl @[simp, norm_cast] lemma coe_one : ⇑(1 : α →+* α) = _root_.id := rfl @[simp, norm_cast] lemma coe_mul (f g : α →+* α) : ⇑(f * g) = f ∘ g := rfl instance instMonoid : Monoid (α →+* α) where mul_one := comp_id one_mul := id_comp mul_assoc _ _ _ := comp_assoc _ _ _ npow n f := (npowRec n f).copy f^[n] <| by induction n <;> simp [npowRec, *] npow_succ _ _ := DFunLike.coe_injective <| Function.iterate_succ _ _ @[simp, norm_cast] lemma coe_pow (f : α →+* α) (n : ℕ) : ⇑(f ^ n) = f^[n] := rfl @[simp] theorem cancel_right {g₁ g₂ : β →+* γ} {f : α →+* β} (hf : Surjective f) :
g₁.comp f = g₂.comp f ↔ g₁ = g₂ :=
Mathlib/Algebra/Ring/Hom/Defs.lean
580
580
/- Copyright (c) 2022 Yakov Pechersky. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yakov Pechersky -/ import Mathlib.Data.List.Induction import Mathlib.Data.List.TakeWhile /-! # Dropping or taking from lists on the right Taking or removing element from the tail end of a list ## Main definitions - `rdrop n`: drop `n : ℕ` elements from the tail - `rtake n`: take `n : ℕ` elements from the tail - `rdropWhile p`: remove all the elements from the tail of a list until it finds the first element for which `p : α → Bool` returns false. This element and everything before is returned. - `rtakeWhile p`: Returns the longest terminal segment of a list for which `p : α → Bool` returns true. ## Implementation detail The two predicate-based methods operate by performing the regular "from-left" operation on `List.reverse`, followed by another `List.reverse`, so they are not the most performant. The other two rely on `List.length l` so they still traverse the list twice. One could construct another function that takes a `L : ℕ` and use `L - n`. Under a proof condition that `L = l.length`, the function would do the right thing. -/ -- Make sure we don't import algebra assert_not_exists Monoid variable {α : Type*} (p : α → Bool) (l : List α) (n : ℕ) namespace List /-- Drop `n` elements from the tail end of a list. -/ def rdrop : List α := l.take (l.length - n) @[simp] theorem rdrop_nil : rdrop ([] : List α) n = [] := by simp [rdrop] @[simp] theorem rdrop_zero : rdrop l 0 = l := by simp [rdrop] theorem rdrop_eq_reverse_drop_reverse : l.rdrop n = reverse (l.reverse.drop n) := by rw [rdrop] induction' l using List.reverseRecOn with xs x IH generalizing n · simp · cases n · simp [take_append] · simp [take_append_eq_append_take, IH] @[simp] theorem rdrop_concat_succ (x : α) : rdrop (l ++ [x]) (n + 1) = rdrop l n := by simp [rdrop_eq_reverse_drop_reverse] /-- Take `n` elements from the tail end of a list. -/ def rtake : List α := l.drop (l.length - n) @[simp] theorem rtake_nil : rtake ([] : List α) n = [] := by simp [rtake] @[simp] theorem rtake_zero : rtake l 0 = [] := by simp [rtake] theorem rtake_eq_reverse_take_reverse : l.rtake n = reverse (l.reverse.take n) := by rw [rtake] induction' l using List.reverseRecOn with xs x IH generalizing n · simp · cases n · exact drop_length · simp [drop_append_eq_append_drop, IH] @[simp] theorem rtake_concat_succ (x : α) : rtake (l ++ [x]) (n + 1) = rtake l n ++ [x] := by simp [rtake_eq_reverse_take_reverse] /-- Drop elements from the tail end of a list that satisfy `p : α → Bool`. Implemented naively via `List.reverse` -/ def rdropWhile : List α := reverse (l.reverse.dropWhile p) @[simp] theorem rdropWhile_nil : rdropWhile p ([] : List α) = [] := by simp [rdropWhile, dropWhile] theorem rdropWhile_concat (x : α) : rdropWhile p (l ++ [x]) = if p x then rdropWhile p l else l ++ [x] := by simp only [rdropWhile, dropWhile, reverse_append, reverse_singleton, singleton_append] split_ifs with h <;> simp [h] @[simp] theorem rdropWhile_concat_pos (x : α) (h : p x) : rdropWhile p (l ++ [x]) = rdropWhile p l := by rw [rdropWhile_concat, if_pos h] @[simp] theorem rdropWhile_concat_neg (x : α) (h : ¬p x) : rdropWhile p (l ++ [x]) = l ++ [x] := by rw [rdropWhile_concat, if_neg h] theorem rdropWhile_singleton (x : α) : rdropWhile p [x] = if p x then [] else [x] := by rw [← nil_append [x], rdropWhile_concat, rdropWhile_nil] theorem rdropWhile_last_not (hl : l.rdropWhile p ≠ []) : ¬p ((rdropWhile p l).getLast hl) := by simp_rw [rdropWhile] rw [getLast_reverse, head_dropWhile_not p] simp theorem rdropWhile_prefix : l.rdropWhile p <+: l := by rw [← reverse_suffix, rdropWhile, reverse_reverse] exact dropWhile_suffix _ variable {p} {l} @[simp] theorem rdropWhile_eq_nil_iff : rdropWhile p l = [] ↔ ∀ x ∈ l, p x := by simp [rdropWhile] -- it is in this file because it requires `List.Infix` @[simp] theorem dropWhile_eq_self_iff : dropWhile p l = l ↔ ∀ hl : 0 < l.length, ¬p (l.get ⟨0, hl⟩) := by rcases l with - | ⟨hd, tl⟩ · simp only [dropWhile, true_iff] intro h by_contra rwa [length_nil, lt_self_iff_false] at h · rw [dropWhile] refine ⟨fun h => ?_, fun h => ?_⟩ · intro _ H rw [get] at H refine (cons_ne_self hd tl) (Sublist.antisymm ?_ (sublist_cons_self _ _)) rw [← h] simp only [H] exact List.IsSuffix.sublist (dropWhile_suffix p) · have := h (by simp only [length, Nat.succ_pos]) rw [get] at this simp_rw [this] @[simp] theorem rdropWhile_eq_self_iff : rdropWhile p l = l ↔ ∀ hl : l ≠ [], ¬p (l.getLast hl) := by simp [rdropWhile, reverse_eq_iff, getLast_eq_getElem, Nat.pos_iff_ne_zero] variable (p) (l) theorem dropWhile_idempotent : dropWhile p (dropWhile p l) = dropWhile p l := by simp only [dropWhile_eq_self_iff] exact fun h => dropWhile_get_zero_not p l h theorem rdropWhile_idempotent : rdropWhile p (rdropWhile p l) = rdropWhile p l := rdropWhile_eq_self_iff.mpr (rdropWhile_last_not _ _) /-- Take elements from the tail end of a list that satisfy `p : α → Bool`. Implemented naively via `List.reverse` -/ def rtakeWhile : List α := reverse (l.reverse.takeWhile p) @[simp] theorem rtakeWhile_nil : rtakeWhile p ([] : List α) = [] := by simp [rtakeWhile, takeWhile] theorem rtakeWhile_concat (x : α) : rtakeWhile p (l ++ [x]) = if p x then rtakeWhile p l ++ [x] else [] := by simp only [rtakeWhile, takeWhile, reverse_append, reverse_singleton, singleton_append] split_ifs with h <;> simp [h] @[simp] theorem rtakeWhile_concat_pos (x : α) (h : p x) : rtakeWhile p (l ++ [x]) = rtakeWhile p l ++ [x] := by rw [rtakeWhile_concat, if_pos h] @[simp] theorem rtakeWhile_concat_neg (x : α) (h : ¬p x) : rtakeWhile p (l ++ [x]) = [] := by rw [rtakeWhile_concat, if_neg h] theorem rtakeWhile_suffix : l.rtakeWhile p <:+ l := by rw [← reverse_prefix, rtakeWhile, reverse_reverse] exact takeWhile_prefix _ variable {p} {l} @[simp] theorem rtakeWhile_eq_self_iff : rtakeWhile p l = l ↔ ∀ x ∈ l, p x := by simp [rtakeWhile, reverse_eq_iff] @[simp] theorem rtakeWhile_eq_nil_iff : rtakeWhile p l = [] ↔ ∀ hl : l ≠ [], ¬p (l.getLast hl) := by induction' l using List.reverseRecOn with l a <;> simp [rtakeWhile] theorem mem_rtakeWhile_imp {x : α} (hx : x ∈ rtakeWhile p l) : p x := by rw [rtakeWhile, mem_reverse] at hx exact mem_takeWhile_imp hx theorem rtakeWhile_idempotent (p : α → Bool) (l : List α) : rtakeWhile p (rtakeWhile p l) = rtakeWhile p l := rtakeWhile_eq_self_iff.mpr fun _ => mem_rtakeWhile_imp lemma rdrop_add (i j : ℕ) : (l.rdrop i).rdrop j = l.rdrop (i + j) := by simp_rw [rdrop_eq_reverse_drop_reverse, reverse_reverse, drop_drop] @[simp] lemma rdrop_append_length {l₁ l₂ : List α} : List.rdrop (l₁ ++ l₂) (List.length l₂) = l₁ := by rw [rdrop_eq_reverse_drop_reverse, ← length_reverse, reverse_append, drop_left, reverse_reverse] lemma rdrop_append_of_le_length {l₁ l₂ : List α} (k : ℕ) : k ≤ length l₂ → List.rdrop (l₁ ++ l₂) k = l₁ ++ List.rdrop l₂ k := by
intro hk rw [← length_reverse] at hk
Mathlib/Data/List/DropRight.lean
210
211
/- Copyright (c) 2020 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers, Sébastien Gouëzel, Heather Macbeth -/ import Mathlib.Analysis.InnerProductSpace.Projection import Mathlib.Analysis.Normed.Lp.PiLp import Mathlib.LinearAlgebra.FiniteDimensional.Lemmas import Mathlib.LinearAlgebra.UnitaryGroup import Mathlib.Util.Superscript /-! # `L²` inner product space structure on finite products of inner product spaces The `L²` norm on a finite product of inner product spaces is compatible with an inner product $$ \langle x, y\rangle = \sum \langle x_i, y_i \rangle. $$ This is recorded in this file as an inner product space instance on `PiLp 2`. This file develops the notion of a finite dimensional Hilbert space over `𝕜 = ℂ, ℝ`, referred to as `E`. We define an `OrthonormalBasis 𝕜 ι E` as a linear isometric equivalence between `E` and `EuclideanSpace 𝕜 ι`. Then `stdOrthonormalBasis` shows that such an equivalence always exists if `E` is finite dimensional. We provide language for converting between a basis that is orthonormal and an orthonormal basis (e.g. `Basis.toOrthonormalBasis`). We show that orthonormal bases for each summand in a direct sum of spaces can be combined into an orthonormal basis for the whole sum in `DirectSum.IsInternal.subordinateOrthonormalBasis`. In the last section, various properties of matrices are explored. ## Main definitions - `EuclideanSpace 𝕜 n`: defined to be `PiLp 2 (n → 𝕜)` for any `Fintype n`, i.e., the space from functions to `n` to `𝕜` with the `L²` norm. We register several instances on it (notably that it is a finite-dimensional inner product space), and provide a `!ₚ[]` notation (for numeric subscripts like `₂`) for the case when the indexing type is `Fin n`. - `OrthonormalBasis 𝕜 ι`: defined to be an isometry to Euclidean space from a given finite-dimensional inner product space, `E ≃ₗᵢ[𝕜] EuclideanSpace 𝕜 ι`. - `Basis.toOrthonormalBasis`: constructs an `OrthonormalBasis` for a finite-dimensional Euclidean space from a `Basis` which is `Orthonormal`. - `Orthonormal.exists_orthonormalBasis_extension`: provides an existential result of an `OrthonormalBasis` extending a given orthonormal set - `exists_orthonormalBasis`: provides an orthonormal basis on a finite dimensional vector space - `stdOrthonormalBasis`: provides an arbitrarily-chosen `OrthonormalBasis` of a given finite dimensional inner product space For consequences in infinite dimension (Hilbert bases, etc.), see the file `Analysis.InnerProductSpace.L2Space`. -/ open Real Set Filter RCLike Submodule Function Uniformity Topology NNReal ENNReal ComplexConjugate DirectSum noncomputable section variable {ι ι' 𝕜 : Type*} [RCLike 𝕜] variable {E : Type*} [NormedAddCommGroup E] [InnerProductSpace 𝕜 E] variable {F : Type*} [NormedAddCommGroup F] [InnerProductSpace ℝ F] variable {F' : Type*} [NormedAddCommGroup F'] [InnerProductSpace ℝ F'] local notation "⟪" x ", " y "⟫" => @inner 𝕜 _ _ x y /- If `ι` is a finite type and each space `f i`, `i : ι`, is an inner product space, then `Π i, f i` is an inner product space as well. Since `Π i, f i` is endowed with the sup norm, we use instead `PiLp 2 f` for the product space, which is endowed with the `L^2` norm. -/ instance PiLp.innerProductSpace {ι : Type*} [Fintype ι] (f : ι → Type*) [∀ i, NormedAddCommGroup (f i)] [∀ i, InnerProductSpace 𝕜 (f i)] : InnerProductSpace 𝕜 (PiLp 2 f) where inner x y := ∑ i, inner (x i) (y i) norm_sq_eq_re_inner x := by simp only [PiLp.norm_sq_eq_of_L2, map_sum, ← norm_sq_eq_re_inner, one_div] conj_inner_symm := by intro x y unfold inner rw [map_sum] apply Finset.sum_congr rfl rintro z - apply inner_conj_symm add_left x y z := show (∑ i, inner (x i + y i) (z i)) = (∑ i, inner (x i) (z i)) + ∑ i, inner (y i) (z i) by simp only [inner_add_left, Finset.sum_add_distrib] smul_left x y r := show (∑ i : ι, inner (r • x i) (y i)) = conj r * ∑ i, inner (x i) (y i) by simp only [Finset.mul_sum, inner_smul_left] @[simp] theorem PiLp.inner_apply {ι : Type*} [Fintype ι] {f : ι → Type*} [∀ i, NormedAddCommGroup (f i)] [∀ i, InnerProductSpace 𝕜 (f i)] (x y : PiLp 2 f) : ⟪x, y⟫ = ∑ i, ⟪x i, y i⟫ := rfl /-- The standard real/complex Euclidean space, functions on a finite type. For an `n`-dimensional space use `EuclideanSpace 𝕜 (Fin n)`. For the case when `n = Fin _`, there is `!₂[x, y, ...]` notation for building elements of this type, analogous to `![x, y, ...]` notation. -/ abbrev EuclideanSpace (𝕜 : Type*) (n : Type*) : Type _ := PiLp 2 fun _ : n => 𝕜 section Notation open Lean Meta Elab Term Macro TSyntax PrettyPrinter.Delaborator SubExpr open Mathlib.Tactic (subscriptTerm) /-- Notation for vectors in Lp space. `!₂[x, y, ...]` is a shorthand for `(WithLp.equiv 2 _ _).symm ![x, y, ...]`, of type `EuclideanSpace _ (Fin _)`.
This also works for other subscripts. -/ syntax (name := PiLp.vecNotation) "!" noWs subscriptTerm noWs "[" term,* "]" : term macro_rules | `(!$p:subscript[$e:term,*]) => do
Mathlib/Analysis/InnerProductSpace/PiL2.lean
114
116
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import Mathlib.Algebra.Group.Pointwise.Set.Card import Mathlib.MeasureTheory.Group.Action import Mathlib.MeasureTheory.Measure.Prod import Mathlib.Topology.Algebra.Module.Equiv import Mathlib.Topology.ContinuousMap.CocompactMap import Mathlib.Topology.Algebra.ContinuousMonoidHom /-! # Measures on Groups We develop some properties of measures on (topological) groups * We define properties on measures: measures that are left or right invariant w.r.t. multiplication. * We define the measure `μ.inv : A ↦ μ(A⁻¹)` and show that it is right invariant iff `μ` is left invariant. * We define a class `IsHaarMeasure μ`, requiring that the measure `μ` is left-invariant, finite on compact sets, and positive on open sets. We also give analogues of all these notions in the additive world. -/ noncomputable section open scoped NNReal ENNReal Pointwise Topology open Inv Set Function MeasureTheory.Measure Filter variable {G H : Type*} [MeasurableSpace G] [MeasurableSpace H] namespace MeasureTheory section Mul variable [Mul G] {μ : Measure G} @[to_additive] theorem map_mul_left_eq_self (μ : Measure G) [IsMulLeftInvariant μ] (g : G) : map (g * ·) μ = μ := IsMulLeftInvariant.map_mul_left_eq_self g @[to_additive] theorem map_mul_right_eq_self (μ : Measure G) [IsMulRightInvariant μ] (g : G) : map (· * g) μ = μ := IsMulRightInvariant.map_mul_right_eq_self g @[to_additive MeasureTheory.isAddLeftInvariant_smul] instance isMulLeftInvariant_smul [IsMulLeftInvariant μ] (c : ℝ≥0∞) : IsMulLeftInvariant (c • μ) := ⟨fun g => by rw [Measure.map_smul, map_mul_left_eq_self]⟩ @[to_additive MeasureTheory.isAddRightInvariant_smul] instance isMulRightInvariant_smul [IsMulRightInvariant μ] (c : ℝ≥0∞) : IsMulRightInvariant (c • μ) := ⟨fun g => by rw [Measure.map_smul, map_mul_right_eq_self]⟩ @[to_additive MeasureTheory.isAddLeftInvariant_smul_nnreal] instance isMulLeftInvariant_smul_nnreal [IsMulLeftInvariant μ] (c : ℝ≥0) : IsMulLeftInvariant (c • μ) := MeasureTheory.isMulLeftInvariant_smul (c : ℝ≥0∞) @[to_additive MeasureTheory.isAddRightInvariant_smul_nnreal] instance isMulRightInvariant_smul_nnreal [IsMulRightInvariant μ] (c : ℝ≥0) : IsMulRightInvariant (c • μ) := MeasureTheory.isMulRightInvariant_smul (c : ℝ≥0∞) section MeasurableMul variable [MeasurableMul G] @[to_additive] theorem measurePreserving_mul_left (μ : Measure G) [IsMulLeftInvariant μ] (g : G) : MeasurePreserving (g * ·) μ μ := ⟨measurable_const_mul g, map_mul_left_eq_self μ g⟩ @[to_additive] theorem MeasurePreserving.mul_left (μ : Measure G) [IsMulLeftInvariant μ] (g : G) {X : Type*} [MeasurableSpace X] {μ' : Measure X} {f : X → G} (hf : MeasurePreserving f μ' μ) : MeasurePreserving (fun x => g * f x) μ' μ := (measurePreserving_mul_left μ g).comp hf @[to_additive] theorem measurePreserving_mul_right (μ : Measure G) [IsMulRightInvariant μ] (g : G) : MeasurePreserving (· * g) μ μ := ⟨measurable_mul_const g, map_mul_right_eq_self μ g⟩ @[to_additive] theorem MeasurePreserving.mul_right (μ : Measure G) [IsMulRightInvariant μ] (g : G) {X : Type*} [MeasurableSpace X] {μ' : Measure X} {f : X → G} (hf : MeasurePreserving f μ' μ) : MeasurePreserving (fun x => f x * g) μ' μ := (measurePreserving_mul_right μ g).comp hf @[to_additive] instance Subgroup.smulInvariantMeasure {G α : Type*} [Group G] [MulAction G α] [MeasurableSpace α] {μ : Measure α} [SMulInvariantMeasure G α μ] (H : Subgroup G) : SMulInvariantMeasure H α μ := ⟨fun y s hs => by convert SMulInvariantMeasure.measure_preimage_smul (μ := μ) (y : G) hs⟩ /-- An alternative way to prove that `μ` is left invariant under multiplication. -/ @[to_additive "An alternative way to prove that `μ` is left invariant under addition."] theorem forall_measure_preimage_mul_iff (μ : Measure G) : (∀ (g : G) (A : Set G), MeasurableSet A → μ ((fun h => g * h) ⁻¹' A) = μ A) ↔ IsMulLeftInvariant μ := by trans ∀ g, map (g * ·) μ = μ · simp_rw [Measure.ext_iff] refine forall_congr' fun g => forall_congr' fun A => forall_congr' fun hA => ?_ rw [map_apply (measurable_const_mul g) hA] exact ⟨fun h => ⟨h⟩, fun h => h.1⟩ /-- An alternative way to prove that `μ` is right invariant under multiplication. -/ @[to_additive "An alternative way to prove that `μ` is right invariant under addition."] theorem forall_measure_preimage_mul_right_iff (μ : Measure G) : (∀ (g : G) (A : Set G), MeasurableSet A → μ ((fun h => h * g) ⁻¹' A) = μ A) ↔ IsMulRightInvariant μ := by trans ∀ g, map (· * g) μ = μ · simp_rw [Measure.ext_iff] refine forall_congr' fun g => forall_congr' fun A => forall_congr' fun hA => ?_ rw [map_apply (measurable_mul_const g) hA] exact ⟨fun h => ⟨h⟩, fun h => h.1⟩ @[to_additive] instance Measure.prod.instIsMulLeftInvariant [IsMulLeftInvariant μ] [SFinite μ] {H : Type*} [Mul H] {mH : MeasurableSpace H} {ν : Measure H} [MeasurableMul H] [IsMulLeftInvariant ν] [SFinite ν] : IsMulLeftInvariant (μ.prod ν) := by constructor rintro ⟨g, h⟩ change map (Prod.map (g * ·) (h * ·)) (μ.prod ν) = μ.prod ν rw [← map_prod_map _ _ (measurable_const_mul g) (measurable_const_mul h), map_mul_left_eq_self μ g, map_mul_left_eq_self ν h] @[to_additive] instance Measure.prod.instIsMulRightInvariant [IsMulRightInvariant μ] [SFinite μ] {H : Type*} [Mul H] {mH : MeasurableSpace H} {ν : Measure H} [MeasurableMul H] [IsMulRightInvariant ν] [SFinite ν] : IsMulRightInvariant (μ.prod ν) := by constructor rintro ⟨g, h⟩ change map (Prod.map (· * g) (· * h)) (μ.prod ν) = μ.prod ν rw [← map_prod_map _ _ (measurable_mul_const g) (measurable_mul_const h), map_mul_right_eq_self μ g, map_mul_right_eq_self ν h] @[to_additive] theorem isMulLeftInvariant_map {H : Type*} [MeasurableSpace H] [Mul H] [MeasurableMul H] [IsMulLeftInvariant μ] (f : G →ₙ* H) (hf : Measurable f) (h_surj : Surjective f) : IsMulLeftInvariant (Measure.map f μ) := by refine ⟨fun h => ?_⟩ rw [map_map (measurable_const_mul _) hf] obtain ⟨g, rfl⟩ := h_surj h conv_rhs => rw [← map_mul_left_eq_self μ g] rw [map_map hf (measurable_const_mul _)] congr 2 ext y simp only [comp_apply, map_mul] end MeasurableMul end Mul section Semigroup variable [Semigroup G] [MeasurableMul G] {μ : Measure G} /-- The image of a left invariant measure under a left action is left invariant, assuming that the action preserves multiplication. -/ @[to_additive "The image of a left invariant measure under a left additive action is left invariant, assuming that the action preserves addition."] theorem isMulLeftInvariant_map_smul {α} [SMul α G] [SMulCommClass α G G] [MeasurableSpace α] [MeasurableSMul α G] [IsMulLeftInvariant μ] (a : α) : IsMulLeftInvariant (map (a • · : G → G) μ) := (forall_measure_preimage_mul_iff _).1 fun x _ hs => (smulInvariantMeasure_map_smul μ a).measure_preimage_smul x hs /-- The image of a right invariant measure under a left action is right invariant, assuming that the action preserves multiplication. -/ @[to_additive "The image of a right invariant measure under a left additive action is right invariant, assuming that the action preserves addition."] theorem isMulRightInvariant_map_smul {α} [SMul α G] [SMulCommClass α Gᵐᵒᵖ G] [MeasurableSpace α] [MeasurableSMul α G] [IsMulRightInvariant μ] (a : α) : IsMulRightInvariant (map (a • · : G → G) μ) := (forall_measure_preimage_mul_right_iff _).1 fun x _ hs => (smulInvariantMeasure_map_smul μ a).measure_preimage_smul (MulOpposite.op x) hs /-- The image of a left invariant measure under right multiplication is left invariant. -/ @[to_additive isMulLeftInvariant_map_add_right "The image of a left invariant measure under right addition is left invariant."] instance isMulLeftInvariant_map_mul_right [IsMulLeftInvariant μ] (g : G) : IsMulLeftInvariant (map (· * g) μ) := isMulLeftInvariant_map_smul (MulOpposite.op g) /-- The image of a right invariant measure under left multiplication is right invariant. -/ @[to_additive isMulRightInvariant_map_add_left "The image of a right invariant measure under left addition is right invariant."] instance isMulRightInvariant_map_mul_left [IsMulRightInvariant μ] (g : G) : IsMulRightInvariant (map (g * ·) μ) := isMulRightInvariant_map_smul g end Semigroup section DivInvMonoid variable [DivInvMonoid G] @[to_additive] theorem map_div_right_eq_self (μ : Measure G) [IsMulRightInvariant μ] (g : G) : map (· / g) μ = μ := by simp_rw [div_eq_mul_inv, map_mul_right_eq_self μ g⁻¹] end DivInvMonoid section Group variable [Group G] [MeasurableMul G] @[to_additive] theorem measurePreserving_div_right (μ : Measure G) [IsMulRightInvariant μ] (g : G) : MeasurePreserving (· / g) μ μ := by simp_rw [div_eq_mul_inv, measurePreserving_mul_right μ g⁻¹] /-- We shorten this from `measure_preimage_mul_left`, since left invariant is the preferred option for measures in this formalization. -/ @[to_additive (attr := simp) "We shorten this from `measure_preimage_add_left`, since left invariant is the preferred option for measures in this formalization."] theorem measure_preimage_mul (μ : Measure G) [IsMulLeftInvariant μ] (g : G) (A : Set G) : μ ((fun h => g * h) ⁻¹' A) = μ A := calc μ ((fun h => g * h) ⁻¹' A) = map (fun h => g * h) μ A := ((MeasurableEquiv.mulLeft g).map_apply A).symm _ = μ A := by rw [map_mul_left_eq_self μ g] @[to_additive (attr := simp)] theorem measure_preimage_mul_right (μ : Measure G) [IsMulRightInvariant μ] (g : G) (A : Set G) : μ ((fun h => h * g) ⁻¹' A) = μ A := calc μ ((fun h => h * g) ⁻¹' A) = map (fun h => h * g) μ A := ((MeasurableEquiv.mulRight g).map_apply A).symm _ = μ A := by rw [map_mul_right_eq_self μ g] @[to_additive] theorem map_mul_left_ae (μ : Measure G) [IsMulLeftInvariant μ] (x : G) : Filter.map (fun h => x * h) (ae μ) = ae μ := ((MeasurableEquiv.mulLeft x).map_ae μ).trans <| congr_arg ae <| map_mul_left_eq_self μ x @[to_additive] theorem map_mul_right_ae (μ : Measure G) [IsMulRightInvariant μ] (x : G) : Filter.map (fun h => h * x) (ae μ) = ae μ := ((MeasurableEquiv.mulRight x).map_ae μ).trans <| congr_arg ae <| map_mul_right_eq_self μ x @[to_additive] theorem map_div_right_ae (μ : Measure G) [IsMulRightInvariant μ] (x : G) : Filter.map (fun t => t / x) (ae μ) = ae μ := ((MeasurableEquiv.divRight x).map_ae μ).trans <| congr_arg ae <| map_div_right_eq_self μ x @[to_additive] theorem eventually_mul_left_iff (μ : Measure G) [IsMulLeftInvariant μ] (t : G) {p : G → Prop} : (∀ᵐ x ∂μ, p (t * x)) ↔ ∀ᵐ x ∂μ, p x := by conv_rhs => rw [Filter.Eventually, ← map_mul_left_ae μ t] rfl @[to_additive] theorem eventually_mul_right_iff (μ : Measure G) [IsMulRightInvariant μ] (t : G) {p : G → Prop} : (∀ᵐ x ∂μ, p (x * t)) ↔ ∀ᵐ x ∂μ, p x := by conv_rhs => rw [Filter.Eventually, ← map_mul_right_ae μ t] rfl @[to_additive] theorem eventually_div_right_iff (μ : Measure G) [IsMulRightInvariant μ] (t : G) {p : G → Prop} : (∀ᵐ x ∂μ, p (x / t)) ↔ ∀ᵐ x ∂μ, p x := by conv_rhs => rw [Filter.Eventually, ← map_div_right_ae μ t] rfl end Group namespace Measure -- TODO: noncomputable has to be specified explicitly. https://github.com/leanprover-community/mathlib4/issues/1074 (item 8) /-- The measure `A ↦ μ (A⁻¹)`, where `A⁻¹` is the pointwise inverse of `A`. -/ @[to_additive "The measure `A ↦ μ (- A)`, where `- A` is the pointwise negation of `A`."] protected noncomputable def inv [Inv G] (μ : Measure G) : Measure G := Measure.map inv μ /-- A measure is invariant under negation if `- μ = μ`. Equivalently, this means that for all measurable `A` we have `μ (- A) = μ A`, where `- A` is the pointwise negation of `A`. -/ class IsNegInvariant [Neg G] (μ : Measure G) : Prop where neg_eq_self : μ.neg = μ /-- A measure is invariant under inversion if `μ⁻¹ = μ`. Equivalently, this means that for all measurable `A` we have `μ (A⁻¹) = μ A`, where `A⁻¹` is the pointwise inverse of `A`. -/ @[to_additive existing] class IsInvInvariant [Inv G] (μ : Measure G) : Prop where inv_eq_self : μ.inv = μ section Inv variable [Inv G] @[to_additive] theorem inv_def (μ : Measure G) : μ.inv = Measure.map inv μ := rfl @[to_additive (attr := simp)] theorem inv_eq_self (μ : Measure G) [IsInvInvariant μ] : μ.inv = μ := IsInvInvariant.inv_eq_self @[to_additive (attr := simp)] theorem map_inv_eq_self (μ : Measure G) [IsInvInvariant μ] : map Inv.inv μ = μ := IsInvInvariant.inv_eq_self variable [MeasurableInv G] @[to_additive] theorem measurePreserving_inv (μ : Measure G) [IsInvInvariant μ] : MeasurePreserving Inv.inv μ μ := ⟨measurable_inv, map_inv_eq_self μ⟩ @[to_additive] instance inv.instSFinite (μ : Measure G) [SFinite μ] : SFinite μ.inv := by rw [Measure.inv]; infer_instance end Inv section InvolutiveInv variable [InvolutiveInv G] [MeasurableInv G] @[to_additive (attr := simp)] theorem inv_apply (μ : Measure G) (s : Set G) : μ.inv s = μ s⁻¹ := (MeasurableEquiv.inv G).map_apply s @[to_additive (attr := simp)] protected theorem inv_inv (μ : Measure G) : μ.inv.inv = μ := (MeasurableEquiv.inv G).map_symm_map @[to_additive (attr := simp)] theorem measure_inv (μ : Measure G) [IsInvInvariant μ] (A : Set G) : μ A⁻¹ = μ A := by rw [← inv_apply, inv_eq_self] @[to_additive] theorem measure_preimage_inv (μ : Measure G) [IsInvInvariant μ] (A : Set G) : μ (Inv.inv ⁻¹' A) = μ A := μ.measure_inv A @[to_additive] instance inv.instSigmaFinite (μ : Measure G) [SigmaFinite μ] : SigmaFinite μ.inv := (MeasurableEquiv.inv G).sigmaFinite_map end InvolutiveInv section DivisionMonoid variable [DivisionMonoid G] [MeasurableMul G] [MeasurableInv G] {μ : Measure G} @[to_additive] instance inv.instIsMulRightInvariant [IsMulLeftInvariant μ] : IsMulRightInvariant μ.inv := by constructor intro g conv_rhs => rw [← map_mul_left_eq_self μ g⁻¹] simp_rw [Measure.inv, map_map (measurable_mul_const g) measurable_inv, map_map measurable_inv (measurable_const_mul g⁻¹), Function.comp_def, mul_inv_rev, inv_inv] @[to_additive] instance inv.instIsMulLeftInvariant [IsMulRightInvariant μ] : IsMulLeftInvariant μ.inv := by constructor intro g conv_rhs => rw [← map_mul_right_eq_self μ g⁻¹] simp_rw [Measure.inv, map_map (measurable_const_mul g) measurable_inv, map_map measurable_inv (measurable_mul_const g⁻¹), Function.comp_def, mul_inv_rev, inv_inv] @[to_additive] theorem measurePreserving_div_left (μ : Measure G) [IsInvInvariant μ] [IsMulLeftInvariant μ] (g : G) : MeasurePreserving (fun t => g / t) μ μ := by simp_rw [div_eq_mul_inv]
exact (measurePreserving_mul_left μ g).comp (measurePreserving_inv μ) @[to_additive] theorem map_div_left_eq_self (μ : Measure G) [IsInvInvariant μ] [IsMulLeftInvariant μ] (g : G) :
Mathlib/MeasureTheory/Group/Measure.lean
373
376
/- Copyright (c) 2014 Parikshit Khanna. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Mario Carneiro -/ import Mathlib.Control.Basic import Mathlib.Data.Nat.Basic import Mathlib.Data.Option.Basic import Mathlib.Data.List.Defs import Mathlib.Data.List.Monad import Mathlib.Logic.OpClass import Mathlib.Logic.Unique import Mathlib.Order.Basic import Mathlib.Tactic.Common /-! # Basic properties of lists -/ assert_not_exists GroupWithZero assert_not_exists Lattice assert_not_exists Prod.swap_eq_iff_eq_swap assert_not_exists Ring assert_not_exists Set.range open Function open Nat hiding one_pos namespace List universe u v w variable {ι : Type*} {α : Type u} {β : Type v} {γ : Type w} {l₁ l₂ : List α} /-- There is only one list of an empty type -/ instance uniqueOfIsEmpty [IsEmpty α] : Unique (List α) := { instInhabitedList with uniq := fun l => match l with | [] => rfl | a :: _ => isEmptyElim a } instance : Std.LawfulIdentity (α := List α) Append.append [] where left_id := nil_append right_id := append_nil instance : Std.Associative (α := List α) Append.append where assoc := append_assoc @[simp] theorem cons_injective {a : α} : Injective (cons a) := fun _ _ => tail_eq_of_cons_eq theorem singleton_injective : Injective fun a : α => [a] := fun _ _ h => (cons_eq_cons.1 h).1 theorem set_of_mem_cons (l : List α) (a : α) : { x | x ∈ a :: l } = insert a { x | x ∈ l } := Set.ext fun _ => mem_cons /-! ### mem -/ theorem _root_.Decidable.List.eq_or_ne_mem_of_mem [DecidableEq α] {a b : α} {l : List α} (h : a ∈ b :: l) : a = b ∨ a ≠ b ∧ a ∈ l := by by_cases hab : a = b · exact Or.inl hab · exact ((List.mem_cons.1 h).elim Or.inl (fun h => Or.inr ⟨hab, h⟩)) lemma mem_pair {a b c : α} : a ∈ [b, c] ↔ a = b ∨ a = c := by rw [mem_cons, mem_singleton] -- The simpNF linter says that the LHS can be simplified via `List.mem_map`. -- However this is a higher priority lemma. -- It seems the side condition `hf` is not applied by `simpNF`. -- https://github.com/leanprover/std4/issues/207 @[simp 1100, nolint simpNF] theorem mem_map_of_injective {f : α → β} (H : Injective f) {a : α} {l : List α} : f a ∈ map f l ↔ a ∈ l := ⟨fun m => let ⟨_, m', e⟩ := exists_of_mem_map m; H e ▸ m', mem_map_of_mem⟩ @[simp] theorem _root_.Function.Involutive.exists_mem_and_apply_eq_iff {f : α → α} (hf : Function.Involutive f) (x : α) (l : List α) : (∃ y : α, y ∈ l ∧ f y = x) ↔ f x ∈ l := ⟨by rintro ⟨y, h, rfl⟩; rwa [hf y], fun h => ⟨f x, h, hf _⟩⟩ theorem mem_map_of_involutive {f : α → α} (hf : Involutive f) {a : α} {l : List α} : a ∈ map f l ↔ f a ∈ l := by rw [mem_map, hf.exists_mem_and_apply_eq_iff] /-! ### length -/ alias ⟨_, length_pos_of_ne_nil⟩ := length_pos_iff theorem length_pos_iff_ne_nil {l : List α} : 0 < length l ↔ l ≠ [] := ⟨ne_nil_of_length_pos, length_pos_of_ne_nil⟩ theorem exists_of_length_succ {n} : ∀ l : List α, l.length = n + 1 → ∃ h t, l = h :: t | [], H => absurd H.symm <| succ_ne_zero n | h :: t, _ => ⟨h, t, rfl⟩ @[simp] lemma length_injective_iff : Injective (List.length : List α → ℕ) ↔ Subsingleton α := by constructor · intro h; refine ⟨fun x y => ?_⟩; (suffices [x] = [y] by simpa using this); apply h; rfl · intros hα l1 l2 hl induction l1 generalizing l2 <;> cases l2 · rfl · cases hl · cases hl · next ih _ _ => congr · subsingleton · apply ih; simpa using hl @[simp default+1] -- Raise priority above `length_injective_iff`. lemma length_injective [Subsingleton α] : Injective (length : List α → ℕ) := length_injective_iff.mpr inferInstance theorem length_eq_two {l : List α} : l.length = 2 ↔ ∃ a b, l = [a, b] := ⟨fun _ => let [a, b] := l; ⟨a, b, rfl⟩, fun ⟨_, _, e⟩ => e ▸ rfl⟩ theorem length_eq_three {l : List α} : l.length = 3 ↔ ∃ a b c, l = [a, b, c] := ⟨fun _ => let [a, b, c] := l; ⟨a, b, c, rfl⟩, fun ⟨_, _, _, e⟩ => e ▸ rfl⟩ /-! ### set-theoretic notation of lists -/ instance instSingletonList : Singleton α (List α) := ⟨fun x => [x]⟩ instance [DecidableEq α] : Insert α (List α) := ⟨List.insert⟩ instance [DecidableEq α] : LawfulSingleton α (List α) := { insert_empty_eq := fun x => show (if x ∈ ([] : List α) then [] else [x]) = [x] from if_neg not_mem_nil } theorem singleton_eq (x : α) : ({x} : List α) = [x] := rfl theorem insert_neg [DecidableEq α] {x : α} {l : List α} (h : x ∉ l) : Insert.insert x l = x :: l := insert_of_not_mem h theorem insert_pos [DecidableEq α] {x : α} {l : List α} (h : x ∈ l) : Insert.insert x l = l := insert_of_mem h theorem doubleton_eq [DecidableEq α] {x y : α} (h : x ≠ y) : ({x, y} : List α) = [x, y] := by rw [insert_neg, singleton_eq] rwa [singleton_eq, mem_singleton] /-! ### bounded quantifiers over lists -/ theorem forall_mem_of_forall_mem_cons {p : α → Prop} {a : α} {l : List α} (h : ∀ x ∈ a :: l, p x) : ∀ x ∈ l, p x := (forall_mem_cons.1 h).2 theorem exists_mem_cons_of {p : α → Prop} {a : α} (l : List α) (h : p a) : ∃ x ∈ a :: l, p x := ⟨a, mem_cons_self, h⟩ theorem exists_mem_cons_of_exists {p : α → Prop} {a : α} {l : List α} : (∃ x ∈ l, p x) → ∃ x ∈ a :: l, p x := fun ⟨x, xl, px⟩ => ⟨x, mem_cons_of_mem _ xl, px⟩ theorem or_exists_of_exists_mem_cons {p : α → Prop} {a : α} {l : List α} : (∃ x ∈ a :: l, p x) → p a ∨ ∃ x ∈ l, p x := fun ⟨x, xal, px⟩ => Or.elim (eq_or_mem_of_mem_cons xal) (fun h : x = a => by rw [← h]; left; exact px) fun h : x ∈ l => Or.inr ⟨x, h, px⟩ theorem exists_mem_cons_iff (p : α → Prop) (a : α) (l : List α) : (∃ x ∈ a :: l, p x) ↔ p a ∨ ∃ x ∈ l, p x := Iff.intro or_exists_of_exists_mem_cons fun h => Or.elim h (exists_mem_cons_of l) exists_mem_cons_of_exists /-! ### list subset -/ theorem cons_subset_of_subset_of_mem {a : α} {l m : List α} (ainm : a ∈ m) (lsubm : l ⊆ m) : a::l ⊆ m := cons_subset.2 ⟨ainm, lsubm⟩ theorem append_subset_of_subset_of_subset {l₁ l₂ l : List α} (l₁subl : l₁ ⊆ l) (l₂subl : l₂ ⊆ l) : l₁ ++ l₂ ⊆ l := fun _ h ↦ (mem_append.1 h).elim (@l₁subl _) (@l₂subl _) theorem map_subset_iff {l₁ l₂ : List α} (f : α → β) (h : Injective f) : map f l₁ ⊆ map f l₂ ↔ l₁ ⊆ l₂ := by refine ⟨?_, map_subset f⟩; intro h2 x hx rcases mem_map.1 (h2 (mem_map_of_mem hx)) with ⟨x', hx', hxx'⟩ cases h hxx'; exact hx' /-! ### append -/ theorem append_eq_has_append {L₁ L₂ : List α} : List.append L₁ L₂ = L₁ ++ L₂ := rfl theorem append_right_injective (s : List α) : Injective fun t ↦ s ++ t := fun _ _ ↦ append_cancel_left theorem append_left_injective (t : List α) : Injective fun s ↦ s ++ t := fun _ _ ↦ append_cancel_right /-! ### replicate -/ theorem eq_replicate_length {a : α} : ∀ {l : List α}, l = replicate l.length a ↔ ∀ b ∈ l, b = a | [] => by simp | (b :: l) => by simp [eq_replicate_length, replicate_succ] theorem replicate_add (m n) (a : α) : replicate (m + n) a = replicate m a ++ replicate n a := by rw [replicate_append_replicate] theorem replicate_subset_singleton (n) (a : α) : replicate n a ⊆ [a] := fun _ h => mem_singleton.2 (eq_of_mem_replicate h) theorem subset_singleton_iff {a : α} {L : List α} : L ⊆ [a] ↔ ∃ n, L = replicate n a := by simp only [eq_replicate_iff, subset_def, mem_singleton, exists_eq_left'] theorem replicate_right_injective {n : ℕ} (hn : n ≠ 0) : Injective (@replicate α n) := fun _ _ h => (eq_replicate_iff.1 h).2 _ <| mem_replicate.2 ⟨hn, rfl⟩ theorem replicate_right_inj {a b : α} {n : ℕ} (hn : n ≠ 0) : replicate n a = replicate n b ↔ a = b := (replicate_right_injective hn).eq_iff theorem replicate_right_inj' {a b : α} : ∀ {n}, replicate n a = replicate n b ↔ n = 0 ∨ a = b | 0 => by simp | n + 1 => (replicate_right_inj n.succ_ne_zero).trans <| by simp only [n.succ_ne_zero, false_or] theorem replicate_left_injective (a : α) : Injective (replicate · a) := LeftInverse.injective (length_replicate (n := ·)) theorem replicate_left_inj {a : α} {n m : ℕ} : replicate n a = replicate m a ↔ n = m := (replicate_left_injective a).eq_iff @[simp] theorem head?_flatten_replicate {n : ℕ} (h : n ≠ 0) (l : List α) : (List.replicate n l).flatten.head? = l.head? := by obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero h induction l <;> simp [replicate] @[simp] theorem getLast?_flatten_replicate {n : ℕ} (h : n ≠ 0) (l : List α) : (List.replicate n l).flatten.getLast? = l.getLast? := by rw [← List.head?_reverse, ← List.head?_reverse, List.reverse_flatten, List.map_replicate, List.reverse_replicate, head?_flatten_replicate h] /-! ### pure -/ theorem mem_pure (x y : α) : x ∈ (pure y : List α) ↔ x = y := by simp /-! ### bind -/ @[simp] theorem bind_eq_flatMap {α β} (f : α → List β) (l : List α) : l >>= f = l.flatMap f := rfl /-! ### concat -/ /-! ### reverse -/ theorem reverse_cons' (a : α) (l : List α) : reverse (a :: l) = concat (reverse l) a := by simp only [reverse_cons, concat_eq_append] theorem reverse_concat' (l : List α) (a : α) : (l ++ [a]).reverse = a :: l.reverse := by rw [reverse_append]; rfl @[simp] theorem reverse_singleton (a : α) : reverse [a] = [a] := rfl @[simp] theorem reverse_involutive : Involutive (@reverse α) := reverse_reverse @[simp] theorem reverse_injective : Injective (@reverse α) := reverse_involutive.injective theorem reverse_surjective : Surjective (@reverse α) := reverse_involutive.surjective theorem reverse_bijective : Bijective (@reverse α) := reverse_involutive.bijective theorem concat_eq_reverse_cons (a : α) (l : List α) : concat l a = reverse (a :: reverse l) := by simp only [concat_eq_append, reverse_cons, reverse_reverse] theorem map_reverseAux (f : α → β) (l₁ l₂ : List α) : map f (reverseAux l₁ l₂) = reverseAux (map f l₁) (map f l₂) := by simp only [reverseAux_eq, map_append, map_reverse] -- TODO: Rename `List.reverse_perm` to `List.reverse_perm_self` @[simp] lemma reverse_perm' : l₁.reverse ~ l₂ ↔ l₁ ~ l₂ where mp := l₁.reverse_perm.symm.trans mpr := l₁.reverse_perm.trans @[simp] lemma perm_reverse : l₁ ~ l₂.reverse ↔ l₁ ~ l₂ where mp hl := hl.trans l₂.reverse_perm mpr hl := hl.trans l₂.reverse_perm.symm /-! ### getLast -/ attribute [simp] getLast_cons theorem getLast_append_singleton {a : α} (l : List α) : getLast (l ++ [a]) (append_ne_nil_of_right_ne_nil l (cons_ne_nil a _)) = a := by simp [getLast_append] theorem getLast_append_of_right_ne_nil (l₁ l₂ : List α) (h : l₂ ≠ []) : getLast (l₁ ++ l₂) (append_ne_nil_of_right_ne_nil l₁ h) = getLast l₂ h := by induction l₁ with | nil => simp | cons _ _ ih => simp only [cons_append]; rw [List.getLast_cons]; exact ih @[deprecated (since := "2025-02-06")] alias getLast_append' := getLast_append_of_right_ne_nil theorem getLast_concat' {a : α} (l : List α) : getLast (concat l a) (by simp) = a := by simp @[simp] theorem getLast_singleton' (a : α) : getLast [a] (cons_ne_nil a []) = a := rfl @[simp] theorem getLast_cons_cons (a₁ a₂ : α) (l : List α) : getLast (a₁ :: a₂ :: l) (cons_ne_nil _ _) = getLast (a₂ :: l) (cons_ne_nil a₂ l) := rfl theorem dropLast_append_getLast : ∀ {l : List α} (h : l ≠ []), dropLast l ++ [getLast l h] = l | [], h => absurd rfl h | [_], _ => rfl | a :: b :: l, h => by rw [dropLast_cons₂, cons_append, getLast_cons (cons_ne_nil _ _)] congr exact dropLast_append_getLast (cons_ne_nil b l) theorem getLast_congr {l₁ l₂ : List α} (h₁ : l₁ ≠ []) (h₂ : l₂ ≠ []) (h₃ : l₁ = l₂) : getLast l₁ h₁ = getLast l₂ h₂ := by subst l₁; rfl theorem getLast_replicate_succ (m : ℕ) (a : α) : (replicate (m + 1) a).getLast (ne_nil_of_length_eq_add_one length_replicate) = a := by simp only [replicate_succ'] exact getLast_append_singleton _ @[deprecated (since := "2025-02-07")] alias getLast_filter' := getLast_filter_of_pos /-! ### getLast? -/ theorem mem_getLast?_eq_getLast : ∀ {l : List α} {x : α}, x ∈ l.getLast? → ∃ h, x = getLast l h | [], x, hx => False.elim <| by simp at hx | [a], x, hx => have : a = x := by simpa using hx this ▸ ⟨cons_ne_nil a [], rfl⟩ | a :: b :: l, x, hx => by rw [getLast?_cons_cons] at hx rcases mem_getLast?_eq_getLast hx with ⟨_, h₂⟩ use cons_ne_nil _ _ assumption theorem getLast?_eq_getLast_of_ne_nil : ∀ {l : List α} (h : l ≠ []), l.getLast? = some (l.getLast h) | [], h => (h rfl).elim | [_], _ => rfl | _ :: b :: l, _ => @getLast?_eq_getLast_of_ne_nil (b :: l) (cons_ne_nil _ _) theorem mem_getLast?_cons {x y : α} : ∀ {l : List α}, x ∈ l.getLast? → x ∈ (y :: l).getLast? | [], _ => by contradiction | _ :: _, h => h theorem dropLast_append_getLast? : ∀ {l : List α}, ∀ a ∈ l.getLast?, dropLast l ++ [a] = l | [], a, ha => (Option.not_mem_none a ha).elim | [a], _, rfl => rfl | a :: b :: l, c, hc => by rw [getLast?_cons_cons] at hc rw [dropLast_cons₂, cons_append, dropLast_append_getLast? _ hc] theorem getLastI_eq_getLast? [Inhabited α] : ∀ l : List α, l.getLastI = l.getLast?.iget | [] => by simp [getLastI, Inhabited.default] | [_] => rfl | [_, _] => rfl | [_, _, _] => rfl | _ :: _ :: c :: l => by simp [getLastI, getLastI_eq_getLast? (c :: l)] theorem getLast?_append_cons : ∀ (l₁ : List α) (a : α) (l₂ : List α), getLast? (l₁ ++ a :: l₂) = getLast? (a :: l₂) | [], _, _ => rfl | [_], _, _ => rfl | b :: c :: l₁, a, l₂ => by rw [cons_append, cons_append, getLast?_cons_cons, ← cons_append, getLast?_append_cons (c :: l₁)] theorem getLast?_append_of_ne_nil (l₁ : List α) : ∀ {l₂ : List α} (_ : l₂ ≠ []), getLast? (l₁ ++ l₂) = getLast? l₂ | [], hl₂ => by contradiction | b :: l₂, _ => getLast?_append_cons l₁ b l₂ theorem mem_getLast?_append_of_mem_getLast? {l₁ l₂ : List α} {x : α} (h : x ∈ l₂.getLast?) : x ∈ (l₁ ++ l₂).getLast? := by cases l₂ · contradiction · rw [List.getLast?_append_cons] exact h /-! ### head(!?) and tail -/ @[simp] theorem head!_nil [Inhabited α] : ([] : List α).head! = default := rfl @[simp] theorem head_cons_tail (x : List α) (h : x ≠ []) : x.head h :: x.tail = x := by cases x <;> simp at h ⊢ theorem head_eq_getElem_zero {l : List α} (hl : l ≠ []) : l.head hl = l[0]'(length_pos_iff.2 hl) := (getElem_zero _).symm theorem head!_eq_head? [Inhabited α] (l : List α) : head! l = (head? l).iget := by cases l <;> rfl theorem surjective_head! [Inhabited α] : Surjective (@head! α _) := fun x => ⟨[x], rfl⟩ theorem surjective_head? : Surjective (@head? α) := Option.forall.2 ⟨⟨[], rfl⟩, fun x => ⟨[x], rfl⟩⟩ theorem surjective_tail : Surjective (@tail α) | [] => ⟨[], rfl⟩ | a :: l => ⟨a :: a :: l, rfl⟩ theorem eq_cons_of_mem_head? {x : α} : ∀ {l : List α}, x ∈ l.head? → l = x :: tail l | [], h => (Option.not_mem_none _ h).elim | a :: l, h => by simp only [head?, Option.mem_def, Option.some_inj] at h exact h ▸ rfl @[simp] theorem head!_cons [Inhabited α] (a : α) (l : List α) : head! (a :: l) = a := rfl @[simp] theorem head!_append [Inhabited α] (t : List α) {s : List α} (h : s ≠ []) : head! (s ++ t) = head! s := by induction s · contradiction · rfl theorem mem_head?_append_of_mem_head? {s t : List α} {x : α} (h : x ∈ s.head?) : x ∈ (s ++ t).head? := by cases s · contradiction · exact h theorem head?_append_of_ne_nil : ∀ (l₁ : List α) {l₂ : List α} (_ : l₁ ≠ []), head? (l₁ ++ l₂) = head? l₁ | _ :: _, _, _ => rfl theorem tail_append_singleton_of_ne_nil {a : α} {l : List α} (h : l ≠ nil) : tail (l ++ [a]) = tail l ++ [a] := by induction l · contradiction · rw [tail, cons_append, tail] theorem cons_head?_tail : ∀ {l : List α} {a : α}, a ∈ head? l → a :: tail l = l | [], a, h => by contradiction | b :: l, a, h => by simp? at h says simp only [head?_cons, Option.mem_def, Option.some.injEq] at h simp [h] theorem head!_mem_head? [Inhabited α] : ∀ {l : List α}, l ≠ [] → head! l ∈ head? l | [], h => by contradiction | _ :: _, _ => rfl theorem cons_head!_tail [Inhabited α] {l : List α} (h : l ≠ []) : head! l :: tail l = l := cons_head?_tail (head!_mem_head? h) theorem head!_mem_self [Inhabited α] {l : List α} (h : l ≠ nil) : l.head! ∈ l := by have h' : l.head! ∈ l.head! :: l.tail := mem_cons_self rwa [cons_head!_tail h] at h' theorem get_eq_getElem? (l : List α) (i : Fin l.length) : l.get i = l[i]?.get (by simp [getElem?_eq_getElem]) := by simp @[deprecated (since := "2025-02-15")] alias get_eq_get? := get_eq_getElem? theorem exists_mem_iff_getElem {l : List α} {p : α → Prop} : (∃ x ∈ l, p x) ↔ ∃ (i : ℕ) (_ : i < l.length), p l[i] := by simp only [mem_iff_getElem] exact ⟨fun ⟨_x, ⟨i, hi, hix⟩, hxp⟩ ↦ ⟨i, hi, hix ▸ hxp⟩, fun ⟨i, hi, hp⟩ ↦ ⟨_, ⟨i, hi, rfl⟩, hp⟩⟩ theorem forall_mem_iff_getElem {l : List α} {p : α → Prop} : (∀ x ∈ l, p x) ↔ ∀ (i : ℕ) (_ : i < l.length), p l[i] := by simp [mem_iff_getElem, @forall_swap α] theorem get_tail (l : List α) (i) (h : i < l.tail.length) (h' : i + 1 < l.length := (by simp only [length_tail] at h; omega)) : l.tail.get ⟨i, h⟩ = l.get ⟨i + 1, h'⟩ := by cases l <;> [cases h; rfl] /-! ### sublists -/ attribute [refl] List.Sublist.refl theorem Sublist.cons_cons {l₁ l₂ : List α} (a : α) (s : l₁ <+ l₂) : a :: l₁ <+ a :: l₂ := Sublist.cons₂ _ s lemma cons_sublist_cons' {a b : α} : a :: l₁ <+ b :: l₂ ↔ a :: l₁ <+ l₂ ∨ a = b ∧ l₁ <+ l₂ := by constructor · rintro (_ | _) · exact Or.inl ‹_› · exact Or.inr ⟨rfl, ‹_›⟩ · rintro (h | ⟨rfl, h⟩) · exact h.cons _ · rwa [cons_sublist_cons] theorem sublist_cons_of_sublist (a : α) (h : l₁ <+ l₂) : l₁ <+ a :: l₂ := h.cons _ @[deprecated (since := "2025-02-07")] alias sublist_nil_iff_eq_nil := sublist_nil @[simp] lemma sublist_singleton {l : List α} {a : α} : l <+ [a] ↔ l = [] ∨ l = [a] := by constructor <;> rintro (_ | _) <;> aesop theorem Sublist.antisymm (s₁ : l₁ <+ l₂) (s₂ : l₂ <+ l₁) : l₁ = l₂ := s₁.eq_of_length_le s₂.length_le /-- If the first element of two lists are different, then a sublist relation can be reduced. -/ theorem Sublist.of_cons_of_ne {a b} (h₁ : a ≠ b) (h₂ : a :: l₁ <+ b :: l₂) : a :: l₁ <+ l₂ := match h₁, h₂ with | _, .cons _ h => h /-! ### indexOf -/ section IndexOf variable [DecidableEq α] theorem idxOf_cons_eq {a b : α} (l : List α) : b = a → idxOf a (b :: l) = 0 | e => by rw [← e]; exact idxOf_cons_self @[deprecated (since := "2025-01-30")] alias indexOf_cons_eq := idxOf_cons_eq @[simp] theorem idxOf_cons_ne {a b : α} (l : List α) : b ≠ a → idxOf a (b :: l) = succ (idxOf a l) | h => by simp only [idxOf_cons, Bool.cond_eq_ite, beq_iff_eq, if_neg h] @[deprecated (since := "2025-01-30")] alias indexOf_cons_ne := idxOf_cons_ne theorem idxOf_eq_length_iff {a : α} {l : List α} : idxOf a l = length l ↔ a ∉ l := by induction l with | nil => exact iff_of_true rfl not_mem_nil | cons b l ih => simp only [length, mem_cons, idxOf_cons, eq_comm] rw [cond_eq_if] split_ifs with h <;> simp at h · exact iff_of_false (by rintro ⟨⟩) fun H => H <| Or.inl h.symm · simp only [Ne.symm h, false_or] rw [← ih] exact succ_inj @[simp] theorem idxOf_of_not_mem {l : List α} {a : α} : a ∉ l → idxOf a l = length l := idxOf_eq_length_iff.2 @[deprecated (since := "2025-01-30")] alias indexOf_of_not_mem := idxOf_of_not_mem theorem idxOf_le_length {a : α} {l : List α} : idxOf a l ≤ length l := by induction l with | nil => rfl | cons b l ih => ?_ simp only [length, idxOf_cons, cond_eq_if, beq_iff_eq] by_cases h : b = a · rw [if_pos h]; exact Nat.zero_le _ · rw [if_neg h]; exact succ_le_succ ih @[deprecated (since := "2025-01-30")] alias indexOf_le_length := idxOf_le_length theorem idxOf_lt_length_iff {a} {l : List α} : idxOf a l < length l ↔ a ∈ l := ⟨fun h => Decidable.byContradiction fun al => Nat.ne_of_lt h <| idxOf_eq_length_iff.2 al, fun al => (lt_of_le_of_ne idxOf_le_length) fun h => idxOf_eq_length_iff.1 h al⟩ @[deprecated (since := "2025-01-30")] alias indexOf_lt_length_iff := idxOf_lt_length_iff theorem idxOf_append_of_mem {a : α} (h : a ∈ l₁) : idxOf a (l₁ ++ l₂) = idxOf a l₁ := by induction l₁ with | nil => exfalso exact not_mem_nil h | cons d₁ t₁ ih => rw [List.cons_append] by_cases hh : d₁ = a · iterate 2 rw [idxOf_cons_eq _ hh] rw [idxOf_cons_ne _ hh, idxOf_cons_ne _ hh, ih (mem_of_ne_of_mem (Ne.symm hh) h)] @[deprecated (since := "2025-01-30")] alias indexOf_append_of_mem := idxOf_append_of_mem theorem idxOf_append_of_not_mem {a : α} (h : a ∉ l₁) : idxOf a (l₁ ++ l₂) = l₁.length + idxOf a l₂ := by induction l₁ with | nil => rw [List.nil_append, List.length, Nat.zero_add] | cons d₁ t₁ ih => rw [List.cons_append, idxOf_cons_ne _ (ne_of_not_mem_cons h).symm, List.length, ih (not_mem_of_not_mem_cons h), Nat.succ_add] @[deprecated (since := "2025-01-30")] alias indexOf_append_of_not_mem := idxOf_append_of_not_mem end IndexOf /-! ### nth element -/ section deprecated @[simp] theorem getElem?_length (l : List α) : l[l.length]? = none := getElem?_eq_none le_rfl /-- A version of `getElem_map` that can be used for rewriting. -/ theorem getElem_map_rev (f : α → β) {l} {n : Nat} {h : n < l.length} : f l[n] = (map f l)[n]'((l.length_map f).symm ▸ h) := Eq.symm (getElem_map _) theorem get_length_sub_one {l : List α} (h : l.length - 1 < l.length) : l.get ⟨l.length - 1, h⟩ = l.getLast (by rintro rfl; exact Nat.lt_irrefl 0 h) := (getLast_eq_getElem _).symm theorem take_one_drop_eq_of_lt_length {l : List α} {n : ℕ} (h : n < l.length) : (l.drop n).take 1 = [l.get ⟨n, h⟩] := by rw [drop_eq_getElem_cons h, take, take] simp theorem ext_getElem?' {l₁ l₂ : List α} (h' : ∀ n < max l₁.length l₂.length, l₁[n]? = l₂[n]?) : l₁ = l₂ := by apply ext_getElem? intro n rcases Nat.lt_or_ge n <| max l₁.length l₂.length with hn | hn · exact h' n hn · simp_all [Nat.max_le, getElem?_eq_none] @[deprecated (since := "2025-02-15")] alias ext_get?' := ext_getElem?' @[deprecated (since := "2025-02-15")] alias ext_get?_iff := List.ext_getElem?_iff theorem ext_get_iff {l₁ l₂ : List α} : l₁ = l₂ ↔ l₁.length = l₂.length ∧ ∀ n h₁ h₂, get l₁ ⟨n, h₁⟩ = get l₂ ⟨n, h₂⟩ := by constructor · rintro rfl exact ⟨rfl, fun _ _ _ ↦ rfl⟩ · intro ⟨h₁, h₂⟩ exact ext_get h₁ h₂ theorem ext_getElem?_iff' {l₁ l₂ : List α} : l₁ = l₂ ↔ ∀ n < max l₁.length l₂.length, l₁[n]? = l₂[n]? := ⟨by rintro rfl _ _; rfl, ext_getElem?'⟩ @[deprecated (since := "2025-02-15")] alias ext_get?_iff' := ext_getElem?_iff' /-- If two lists `l₁` and `l₂` are the same length and `l₁[n]! = l₂[n]!` for all `n`, then the lists are equal. -/ theorem ext_getElem! [Inhabited α] (hl : length l₁ = length l₂) (h : ∀ n : ℕ, l₁[n]! = l₂[n]!) : l₁ = l₂ := ext_getElem hl fun n h₁ h₂ ↦ by simpa only [← getElem!_pos] using h n @[simp] theorem getElem_idxOf [DecidableEq α] {a : α} : ∀ {l : List α} (h : idxOf a l < l.length), l[idxOf a l] = a | b :: l, h => by by_cases h' : b = a <;> simp [h', if_pos, if_false, getElem_idxOf] @[deprecated (since := "2025-01-30")] alias getElem_indexOf := getElem_idxOf -- This is incorrectly named and should be `get_idxOf`; -- this already exists, so will require a deprecation dance. theorem idxOf_get [DecidableEq α] {a : α} {l : List α} (h) : get l ⟨idxOf a l, h⟩ = a := by simp @[deprecated (since := "2025-01-30")] alias indexOf_get := idxOf_get @[simp] theorem getElem?_idxOf [DecidableEq α] {a : α} {l : List α} (h : a ∈ l) : l[idxOf a l]? = some a := by rw [getElem?_eq_getElem, getElem_idxOf (idxOf_lt_length_iff.2 h)] @[deprecated (since := "2025-01-30")] alias getElem?_indexOf := getElem?_idxOf @[deprecated (since := "2025-02-15")] alias idxOf_get? := getElem?_idxOf @[deprecated (since := "2025-01-30")] alias indexOf_get? := getElem?_idxOf theorem idxOf_inj [DecidableEq α] {l : List α} {x y : α} (hx : x ∈ l) (hy : y ∈ l) : idxOf x l = idxOf y l ↔ x = y := ⟨fun h => by have x_eq_y : get l ⟨idxOf x l, idxOf_lt_length_iff.2 hx⟩ = get l ⟨idxOf y l, idxOf_lt_length_iff.2 hy⟩ := by simp only [h] simp only [idxOf_get] at x_eq_y; exact x_eq_y, fun h => by subst h; rfl⟩ @[deprecated (since := "2025-01-30")] alias indexOf_inj := idxOf_inj theorem get_reverse' (l : List α) (n) (hn') : l.reverse.get n = l.get ⟨l.length - 1 - n, hn'⟩ := by simp theorem eq_cons_of_length_one {l : List α} (h : l.length = 1) : l = [l.get ⟨0, by omega⟩] := by refine ext_get (by convert h) fun n h₁ h₂ => ?_ simp congr omega end deprecated @[simp] theorem getElem_set_of_ne {l : List α} {i j : ℕ} (h : i ≠ j) (a : α) (hj : j < (l.set i a).length) : (l.set i a)[j] = l[j]'(by simpa using hj) := by rw [← Option.some_inj, ← List.getElem?_eq_getElem, List.getElem?_set_ne h, List.getElem?_eq_getElem] /-! ### map -/ -- `List.map_const` (the version with `Function.const` instead of a lambda) is already tagged -- `simp` in Core -- TODO: Upstream the tagging to Core? attribute [simp] map_const' theorem flatMap_pure_eq_map (f : α → β) (l : List α) : l.flatMap (pure ∘ f) = map f l := .symm <| map_eq_flatMap .. theorem flatMap_congr {l : List α} {f g : α → List β} (h : ∀ x ∈ l, f x = g x) : l.flatMap f = l.flatMap g := (congr_arg List.flatten <| map_congr_left h :) theorem infix_flatMap_of_mem {a : α} {as : List α} (h : a ∈ as) (f : α → List α) : f a <:+: as.flatMap f := infix_of_mem_flatten (mem_map_of_mem h) @[simp] theorem map_eq_map {α β} (f : α → β) (l : List α) : f <$> l = map f l := rfl /-- A single `List.map` of a composition of functions is equal to composing a `List.map` with another `List.map`, fully applied. This is the reverse direction of `List.map_map`. -/ theorem comp_map (h : β → γ) (g : α → β) (l : List α) : map (h ∘ g) l = map h (map g l) := map_map.symm /-- Composing a `List.map` with another `List.map` is equal to a single `List.map` of composed functions. -/ @[simp] theorem map_comp_map (g : β → γ) (f : α → β) : map g ∘ map f = map (g ∘ f) := by ext l; rw [comp_map, Function.comp_apply] section map_bijectivity theorem _root_.Function.LeftInverse.list_map {f : α → β} {g : β → α} (h : LeftInverse f g) : LeftInverse (map f) (map g) | [] => by simp_rw [map_nil] | x :: xs => by simp_rw [map_cons, h x, h.list_map xs] nonrec theorem _root_.Function.RightInverse.list_map {f : α → β} {g : β → α} (h : RightInverse f g) : RightInverse (map f) (map g) := h.list_map nonrec theorem _root_.Function.Involutive.list_map {f : α → α} (h : Involutive f) : Involutive (map f) := Function.LeftInverse.list_map h @[simp] theorem map_leftInverse_iff {f : α → β} {g : β → α} : LeftInverse (map f) (map g) ↔ LeftInverse f g := ⟨fun h x => by injection h [x], (·.list_map)⟩ @[simp] theorem map_rightInverse_iff {f : α → β} {g : β → α} : RightInverse (map f) (map g) ↔ RightInverse f g := map_leftInverse_iff @[simp] theorem map_involutive_iff {f : α → α} : Involutive (map f) ↔ Involutive f := map_leftInverse_iff theorem _root_.Function.Injective.list_map {f : α → β} (h : Injective f) : Injective (map f) | [], [], _ => rfl | x :: xs, y :: ys, hxy => by injection hxy with hxy hxys rw [h hxy, h.list_map hxys] @[simp] theorem map_injective_iff {f : α → β} : Injective (map f) ↔ Injective f := by refine ⟨fun h x y hxy => ?_, (·.list_map)⟩ suffices [x] = [y] by simpa using this apply h simp [hxy] theorem _root_.Function.Surjective.list_map {f : α → β} (h : Surjective f) : Surjective (map f) := let ⟨_, h⟩ := h.hasRightInverse; h.list_map.surjective @[simp] theorem map_surjective_iff {f : α → β} : Surjective (map f) ↔ Surjective f := by refine ⟨fun h x => ?_, (·.list_map)⟩ let ⟨[y], hxy⟩ := h [x] exact ⟨_, List.singleton_injective hxy⟩ theorem _root_.Function.Bijective.list_map {f : α → β} (h : Bijective f) : Bijective (map f) := ⟨h.1.list_map, h.2.list_map⟩ @[simp] theorem map_bijective_iff {f : α → β} : Bijective (map f) ↔ Bijective f := by simp_rw [Function.Bijective, map_injective_iff, map_surjective_iff] end map_bijectivity theorem eq_of_mem_map_const {b₁ b₂ : β} {l : List α} (h : b₁ ∈ map (const α b₂) l) : b₁ = b₂ := by rw [map_const] at h; exact eq_of_mem_replicate h /-- `eq_nil_or_concat` in simp normal form -/ lemma eq_nil_or_concat' (l : List α) : l = [] ∨ ∃ L b, l = L ++ [b] := by simpa using l.eq_nil_or_concat /-! ### foldl, foldr -/ theorem foldl_ext (f g : α → β → α) (a : α) {l : List β} (H : ∀ a : α, ∀ b ∈ l, f a b = g a b) : foldl f a l = foldl g a l := by induction l generalizing a with | nil => rfl | cons hd tl ih => unfold foldl rw [ih _ fun a b bin => H a b <| mem_cons_of_mem _ bin, H a hd mem_cons_self] theorem foldr_ext (f g : α → β → β) (b : β) {l : List α} (H : ∀ a ∈ l, ∀ b : β, f a b = g a b) : foldr f b l = foldr g b l := by induction l with | nil => rfl | cons hd tl ih => ?_ simp only [mem_cons, or_imp, forall_and, forall_eq] at H simp only [foldr, ih H.2, H.1] theorem foldl_concat (f : β → α → β) (b : β) (x : α) (xs : List α) : List.foldl f b (xs ++ [x]) = f (List.foldl f b xs) x := by simp only [List.foldl_append, List.foldl] theorem foldr_concat (f : α → β → β) (b : β) (x : α) (xs : List α) : List.foldr f b (xs ++ [x]) = (List.foldr f (f x b) xs) := by simp only [List.foldr_append, List.foldr] theorem foldl_fixed' {f : α → β → α} {a : α} (hf : ∀ b, f a b = a) : ∀ l : List β, foldl f a l = a | [] => rfl | b :: l => by rw [foldl_cons, hf b, foldl_fixed' hf l] theorem foldr_fixed' {f : α → β → β} {b : β} (hf : ∀ a, f a b = b) : ∀ l : List α, foldr f b l = b | [] => rfl | a :: l => by rw [foldr_cons, foldr_fixed' hf l, hf a] @[simp] theorem foldl_fixed {a : α} : ∀ l : List β, foldl (fun a _ => a) a l = a := foldl_fixed' fun _ => rfl @[simp] theorem foldr_fixed {b : β} : ∀ l : List α, foldr (fun _ b => b) b l = b := foldr_fixed' fun _ => rfl @[deprecated foldr_cons_nil (since := "2025-02-10")] theorem foldr_eta (l : List α) : foldr cons [] l = l := foldr_cons_nil theorem reverse_foldl {l : List α} : reverse (foldl (fun t h => h :: t) [] l) = l := by simp theorem foldl_hom₂ (l : List ι) (f : α → β → γ) (op₁ : α → ι → α) (op₂ : β → ι → β) (op₃ : γ → ι → γ) (a : α) (b : β) (h : ∀ a b i, f (op₁ a i) (op₂ b i) = op₃ (f a b) i) : foldl op₃ (f a b) l = f (foldl op₁ a l) (foldl op₂ b l) := Eq.symm <| by revert a b induction l <;> intros <;> [rfl; simp only [*, foldl]] theorem foldr_hom₂ (l : List ι) (f : α → β → γ) (op₁ : ι → α → α) (op₂ : ι → β → β) (op₃ : ι → γ → γ) (a : α) (b : β) (h : ∀ a b i, f (op₁ i a) (op₂ i b) = op₃ i (f a b)) : foldr op₃ (f a b) l = f (foldr op₁ a l) (foldr op₂ b l) := by revert a induction l <;> intros <;> [rfl; simp only [*, foldr]] theorem injective_foldl_comp {l : List (α → α)} {f : α → α} (hl : ∀ f ∈ l, Function.Injective f) (hf : Function.Injective f) : Function.Injective (@List.foldl (α → α) (α → α) Function.comp f l) := by induction l generalizing f with | nil => exact hf | cons lh lt l_ih => apply l_ih fun _ h => hl _ (List.mem_cons_of_mem _ h) apply Function.Injective.comp hf apply hl _ mem_cons_self /-- Consider two lists `l₁` and `l₂` with designated elements `a₁` and `a₂` somewhere in them: `l₁ = x₁ ++ [a₁] ++ z₁` and `l₂ = x₂ ++ [a₂] ++ z₂`. Assume the designated element `a₂` is present in neither `x₁` nor `z₁`. We conclude that the lists are equal (`l₁ = l₂`) if and only if their respective parts are equal (`x₁ = x₂ ∧ a₁ = a₂ ∧ z₁ = z₂`). -/ lemma append_cons_inj_of_not_mem {x₁ x₂ z₁ z₂ : List α} {a₁ a₂ : α} (notin_x : a₂ ∉ x₁) (notin_z : a₂ ∉ z₁) : x₁ ++ a₁ :: z₁ = x₂ ++ a₂ :: z₂ ↔ x₁ = x₂ ∧ a₁ = a₂ ∧ z₁ = z₂ := by constructor · simp only [append_eq_append_iff, cons_eq_append_iff, cons_eq_cons] rintro (⟨c, rfl, ⟨rfl, rfl, rfl⟩ | ⟨d, rfl, rfl⟩⟩ | ⟨c, rfl, ⟨rfl, rfl, rfl⟩ | ⟨d, rfl, rfl⟩⟩) <;> simp_all · rintro ⟨rfl, rfl, rfl⟩ rfl section FoldlEqFoldr -- foldl and foldr coincide when f is commutative and associative variable {f : α → α → α} theorem foldl1_eq_foldr1 [hassoc : Std.Associative f] : ∀ a b l, foldl f a (l ++ [b]) = foldr f b (a :: l) | _, _, nil => rfl | a, b, c :: l => by simp only [cons_append, foldl_cons, foldr_cons, foldl1_eq_foldr1 _ _ l] rw [hassoc.assoc] theorem foldl_eq_of_comm_of_assoc [hcomm : Std.Commutative f] [hassoc : Std.Associative f] : ∀ a b l, foldl f a (b :: l) = f b (foldl f a l) | a, b, nil => hcomm.comm a b | a, b, c :: l => by simp only [foldl_cons] have : RightCommutative f := inferInstance rw [← foldl_eq_of_comm_of_assoc .., this.right_comm, foldl_cons] theorem foldl_eq_foldr [Std.Commutative f] [Std.Associative f] : ∀ a l, foldl f a l = foldr f a l | _, nil => rfl | a, b :: l => by simp only [foldr_cons, foldl_eq_of_comm_of_assoc] rw [foldl_eq_foldr a l] end FoldlEqFoldr section FoldlEqFoldlr' variable {f : α → β → α} variable (hf : ∀ a b c, f (f a b) c = f (f a c) b) include hf theorem foldl_eq_of_comm' : ∀ a b l, foldl f a (b :: l) = f (foldl f a l) b | _, _, [] => rfl | a, b, c :: l => by rw [foldl, foldl, foldl, ← foldl_eq_of_comm' .., foldl, hf] theorem foldl_eq_foldr' : ∀ a l, foldl f a l = foldr (flip f) a l | _, [] => rfl | a, b :: l => by rw [foldl_eq_of_comm' hf, foldr, foldl_eq_foldr' ..]; rfl end FoldlEqFoldlr' section FoldlEqFoldlr' variable {f : α → β → β} theorem foldr_eq_of_comm' (hf : ∀ a b c, f a (f b c) = f b (f a c)) : ∀ a b l, foldr f a (b :: l) = foldr f (f b a) l | _, _, [] => rfl | a, b, c :: l => by rw [foldr, foldr, foldr, hf, ← foldr_eq_of_comm' hf ..]; rfl end FoldlEqFoldlr' section variable {op : α → α → α} [ha : Std.Associative op] /-- Notation for `op a b`. -/ local notation a " ⋆ " b => op a b /-- Notation for `foldl op a l`. -/ local notation l " <*> " a => foldl op a l theorem foldl_op_eq_op_foldr_assoc : ∀ {l : List α} {a₁ a₂}, ((l <*> a₁) ⋆ a₂) = a₁ ⋆ l.foldr (· ⋆ ·) a₂ | [], _, _ => rfl | a :: l, a₁, a₂ => by simp only [foldl_cons, foldr_cons, foldl_assoc, ha.assoc]; rw [foldl_op_eq_op_foldr_assoc] variable [hc : Std.Commutative op] theorem foldl_assoc_comm_cons {l : List α} {a₁ a₂} : ((a₁ :: l) <*> a₂) = a₁ ⋆ l <*> a₂ := by rw [foldl_cons, hc.comm, foldl_assoc] end /-! ### foldlM, foldrM, mapM -/ section FoldlMFoldrM variable {m : Type v → Type w} [Monad m] variable [LawfulMonad m] theorem foldrM_eq_foldr (f : α → β → m β) (b l) : foldrM f b l = foldr (fun a mb => mb >>= f a) (pure b) l := by induction l <;> simp [*] theorem foldlM_eq_foldl (f : β → α → m β) (b l) : List.foldlM f b l = foldl (fun mb a => mb >>= fun b => f b a) (pure b) l := by suffices h : ∀ mb : m β, (mb >>= fun b => List.foldlM f b l) = foldl (fun mb a => mb >>= fun b => f b a) mb l by simp [← h (pure b)] induction l with | nil => intro; simp | cons _ _ l_ih => intro; simp only [List.foldlM, foldl, ← l_ih, functor_norm] end FoldlMFoldrM /-! ### intersperse -/ @[deprecated (since := "2025-02-07")] alias intersperse_singleton := intersperse_single @[deprecated (since := "2025-02-07")] alias intersperse_cons_cons := intersperse_cons₂ /-! ### map for partial functions -/ @[deprecated "Deprecated without replacement." (since := "2025-02-07")] theorem sizeOf_lt_sizeOf_of_mem [SizeOf α] {x : α} {l : List α} (hx : x ∈ l) : SizeOf.sizeOf x < SizeOf.sizeOf l := by induction l with | nil => ?_ | cons h t ih => ?_ <;> cases hx <;> rw [cons.sizeOf_spec] · omega · specialize ih ‹_› omega /-! ### filter -/ theorem length_eq_length_filter_add {l : List (α)} (f : α → Bool) : l.length = (l.filter f).length + (l.filter (! f ·)).length := by simp_rw [← List.countP_eq_length_filter, l.length_eq_countP_add_countP f, Bool.not_eq_true, Bool.decide_eq_false] /-! ### filterMap -/ theorem filterMap_eq_flatMap_toList (f : α → Option β) (l : List α) : l.filterMap f = l.flatMap fun a ↦ (f a).toList := by induction l with | nil => ?_ | cons a l ih => ?_ <;> simp [filterMap_cons] rcases f a <;> simp [ih] theorem filterMap_congr {f g : α → Option β} {l : List α} (h : ∀ x ∈ l, f x = g x) : l.filterMap f = l.filterMap g := by induction l <;> simp_all [filterMap_cons] theorem filterMap_eq_map_iff_forall_eq_some {f : α → Option β} {g : α → β} {l : List α} : l.filterMap f = l.map g ↔ ∀ x ∈ l, f x = some (g x) where mp := by induction l with | nil => simp | cons a l ih => ?_ rcases ha : f a with - | b <;> simp [ha, filterMap_cons] · intro h simpa [show (filterMap f l).length = l.length + 1 from by simp[h], Nat.add_one_le_iff] using List.length_filterMap_le f l · rintro rfl h exact ⟨rfl, ih h⟩ mpr h := Eq.trans (filterMap_congr <| by simpa) (congr_fun filterMap_eq_map _) /-! ### filter -/ section Filter variable {p : α → Bool} theorem filter_singleton {a : α} : [a].filter p = bif p a then [a] else [] := rfl theorem filter_eq_foldr (p : α → Bool) (l : List α) : filter p l = foldr (fun a out => bif p a then a :: out else out) [] l := by induction l <;> simp [*, filter]; rfl #adaptation_note /-- nightly-2024-07-27 This has to be temporarily renamed to avoid an unintentional collision. The prime should be removed at nightly-2024-07-27. -/ @[simp] theorem filter_subset' (l : List α) : filter p l ⊆ l := filter_sublist.subset theorem of_mem_filter {a : α} {l} (h : a ∈ filter p l) : p a := (mem_filter.1 h).2 theorem mem_of_mem_filter {a : α} {l} (h : a ∈ filter p l) : a ∈ l := filter_subset' l h theorem mem_filter_of_mem {a : α} {l} (h₁ : a ∈ l) (h₂ : p a) : a ∈ filter p l := mem_filter.2 ⟨h₁, h₂⟩ @[deprecated (since := "2025-02-07")] alias monotone_filter_left := filter_subset variable (p) theorem monotone_filter_right (l : List α) ⦃p q : α → Bool⦄ (h : ∀ a, p a → q a) : l.filter p <+ l.filter q := by induction l with | nil => rfl | cons hd tl IH => by_cases hp : p hd · rw [filter_cons_of_pos hp, filter_cons_of_pos (h _ hp)] exact IH.cons_cons hd · rw [filter_cons_of_neg hp] by_cases hq : q hd · rw [filter_cons_of_pos hq] exact sublist_cons_of_sublist hd IH · rw [filter_cons_of_neg hq] exact IH lemma map_filter {f : α → β} (hf : Injective f) (l : List α) [DecidablePred fun b => ∃ a, p a ∧ f a = b] : (l.filter p).map f = (l.map f).filter fun b => ∃ a, p a ∧ f a = b := by simp [comp_def, filter_map, hf.eq_iff] @[deprecated (since := "2025-02-07")] alias map_filter' := map_filter lemma filter_attach' (l : List α) (p : {a // a ∈ l} → Bool) [DecidableEq α] : l.attach.filter p = (l.filter fun x => ∃ h, p ⟨x, h⟩).attach.map (Subtype.map id fun _ => mem_of_mem_filter) := by classical refine map_injective_iff.2 Subtype.coe_injective ?_ simp [comp_def, map_filter _ Subtype.coe_injective] lemma filter_attach (l : List α) (p : α → Bool) : (l.attach.filter fun x => p x : List {x // x ∈ l}) = (l.filter p).attach.map (Subtype.map id fun _ => mem_of_mem_filter) := map_injective_iff.2 Subtype.coe_injective <| by simp_rw [map_map, comp_def, Subtype.map, id, ← Function.comp_apply (g := Subtype.val), ← filter_map, attach_map_subtype_val] lemma filter_comm (q) (l : List α) : filter p (filter q l) = filter q (filter p l) := by simp [Bool.and_comm] @[simp] theorem filter_true (l : List α) : filter (fun _ => true) l = l := by induction l <;> simp [*, filter] @[simp] theorem filter_false (l : List α) : filter (fun _ => false) l = [] := by induction l <;> simp [*, filter] end Filter /-! ### eraseP -/ section eraseP variable {p : α → Bool} @[simp] theorem length_eraseP_add_one {l : List α} {a} (al : a ∈ l) (pa : p a) : (l.eraseP p).length + 1 = l.length := by let ⟨_, l₁, l₂, _, _, h₁, h₂⟩ := exists_of_eraseP al pa rw [h₂, h₁, length_append, length_append] rfl end eraseP /-! ### erase -/ section Erase variable [DecidableEq α] @[simp] theorem length_erase_add_one {a : α} {l : List α} (h : a ∈ l) : (l.erase a).length + 1 = l.length := by rw [erase_eq_eraseP, length_eraseP_add_one h (decide_eq_true rfl)] theorem map_erase [DecidableEq β] {f : α → β} (finj : Injective f) {a : α} (l : List α) : map f (l.erase a) = (map f l).erase (f a) := by have this : (a == ·) = (f a == f ·) := by ext b; simp [beq_eq_decide, finj.eq_iff] rw [erase_eq_eraseP, erase_eq_eraseP, eraseP_map, this]; rfl theorem map_foldl_erase [DecidableEq β] {f : α → β} (finj : Injective f) {l₁ l₂ : List α} : map f (foldl List.erase l₁ l₂) = foldl (fun l a => l.erase (f a)) (map f l₁) l₂ := by induction l₂ generalizing l₁ <;> [rfl; simp only [foldl_cons, map_erase finj, *]] theorem erase_getElem [DecidableEq ι] {l : List ι} {i : ℕ} (hi : i < l.length) : Perm (l.erase l[i]) (l.eraseIdx i) := by induction l generalizing i with | nil => simp | cons a l IH => cases i with | zero => simp | succ i => have hi' : i < l.length := by simpa using hi if ha : a = l[i] then simpa [ha] using .trans (perm_cons_erase (getElem_mem _)) (.cons _ (IH hi')) else simpa [ha] using IH hi' theorem length_eraseIdx_add_one {l : List ι} {i : ℕ} (h : i < l.length) : (l.eraseIdx i).length + 1 = l.length := by rw [length_eraseIdx] split <;> omega end Erase /-! ### diff -/ section Diff variable [DecidableEq α] @[simp] theorem map_diff [DecidableEq β] {f : α → β} (finj : Injective f) {l₁ l₂ : List α} : map f (l₁.diff l₂) = (map f l₁).diff (map f l₂) := by simp only [diff_eq_foldl, foldl_map, map_foldl_erase finj] @[deprecated (since := "2025-04-10")] alias erase_diff_erase_sublist_of_sublist := Sublist.erase_diff_erase_sublist end Diff section Choose variable (p : α → Prop) [DecidablePred p] (l : List α) theorem choose_spec (hp : ∃ a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) := (chooseX p l hp).property theorem choose_mem (hp : ∃ a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1 theorem choose_property (hp : ∃ a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2 end Choose /-! ### Forall -/ section Forall variable {p q : α → Prop} {l : List α} @[simp] theorem forall_cons (p : α → Prop) (x : α) : ∀ l : List α, Forall p (x :: l) ↔ p x ∧ Forall p l | [] => (and_iff_left_of_imp fun _ ↦ trivial).symm | _ :: _ => Iff.rfl @[simp] theorem forall_append {p : α → Prop} : ∀ {xs ys : List α}, Forall p (xs ++ ys) ↔ Forall p xs ∧ Forall p ys | [] => by simp | _ :: _ => by simp [forall_append, and_assoc] theorem forall_iff_forall_mem : ∀ {l : List α}, Forall p l ↔ ∀ x ∈ l, p x | [] => (iff_true_intro <| forall_mem_nil _).symm | x :: l => by rw [forall_mem_cons, forall_cons, forall_iff_forall_mem] theorem Forall.imp (h : ∀ x, p x → q x) : ∀ {l : List α}, Forall p l → Forall q l | [] => id | x :: l => by simp only [forall_cons, and_imp] rw [← and_imp] exact And.imp (h x) (Forall.imp h) @[simp] theorem forall_map_iff {p : β → Prop} (f : α → β) : Forall p (l.map f) ↔ Forall (p ∘ f) l := by induction l <;> simp [*] instance (p : α → Prop) [DecidablePred p] : DecidablePred (Forall p) := fun _ => decidable_of_iff' _ forall_iff_forall_mem end Forall /-! ### Miscellaneous lemmas -/ theorem get_attach (l : List α) (i) : (l.attach.get i).1 = l.get ⟨i, length_attach (l := l) ▸ i.2⟩ := by simp section Disjoint /-- The images of disjoint lists under a partially defined map are disjoint -/ theorem disjoint_pmap {p : α → Prop} {f : ∀ a : α, p a → β} {s t : List α} (hs : ∀ a ∈ s, p a) (ht : ∀ a ∈ t, p a) (hf : ∀ (a a' : α) (ha : p a) (ha' : p a'), f a ha = f a' ha' → a = a') (h : Disjoint s t) : Disjoint (s.pmap f hs) (t.pmap f ht) := by simp only [Disjoint, mem_pmap] rintro b ⟨a, ha, rfl⟩ ⟨a', ha', ha''⟩ apply h ha rwa [hf a a' (hs a ha) (ht a' ha') ha''.symm] /-- The images of disjoint lists under an injective map are disjoint -/ theorem disjoint_map {f : α → β} {s t : List α} (hf : Function.Injective f) (h : Disjoint s t) : Disjoint (s.map f) (t.map f) := by rw [← pmap_eq_map (fun _ _ ↦ trivial), ← pmap_eq_map (fun _ _ ↦ trivial)] exact disjoint_pmap _ _ (fun _ _ _ _ h' ↦ hf h') h alias Disjoint.map := disjoint_map theorem Disjoint.of_map {f : α → β} {s t : List α} (h : Disjoint (s.map f) (t.map f)) : Disjoint s t := fun _a has hat ↦ h (mem_map_of_mem has) (mem_map_of_mem hat) theorem Disjoint.map_iff {f : α → β} {s t : List α} (hf : Function.Injective f) : Disjoint (s.map f) (t.map f) ↔ Disjoint s t := ⟨fun h ↦ h.of_map, fun h ↦ h.map hf⟩ theorem Perm.disjoint_left {l₁ l₂ l : List α} (p : List.Perm l₁ l₂) : Disjoint l₁ l ↔ Disjoint l₂ l := by simp_rw [List.disjoint_left, p.mem_iff] theorem Perm.disjoint_right {l₁ l₂ l : List α} (p : List.Perm l₁ l₂) : Disjoint l l₁ ↔ Disjoint l l₂ := by simp_rw [List.disjoint_right, p.mem_iff] @[simp] theorem disjoint_reverse_left {l₁ l₂ : List α} : Disjoint l₁.reverse l₂ ↔ Disjoint l₁ l₂ := reverse_perm _ |>.disjoint_left @[simp] theorem disjoint_reverse_right {l₁ l₂ : List α} : Disjoint l₁ l₂.reverse ↔ Disjoint l₁ l₂ := reverse_perm _ |>.disjoint_right end Disjoint section lookup variable [BEq α] [LawfulBEq α] lemma lookup_graph (f : α → β) {a : α} {as : List α} (h : a ∈ as) : lookup a (as.map fun x => (x, f x)) = some (f a) := by induction as with | nil => exact (not_mem_nil h).elim | cons a' as ih => by_cases ha : a = a' · simp [ha, lookup_cons] · simpa [lookup_cons, beq_false_of_ne ha] using ih (List.mem_of_ne_of_mem ha h) end lookup section range' @[simp] lemma range'_0 (a b : ℕ) : range' a b 0 = replicate b a := by induction b with | zero => simp | succ b ih => simp [range'_succ, ih, replicate_succ] lemma left_le_of_mem_range' {a b s x : ℕ} (hx : x ∈ List.range' a b s) : a ≤ x := by obtain ⟨i, _, rfl⟩ := List.mem_range'.mp hx exact le_add_right a (s * i) end range' end List
Mathlib/Data/List/Basic.lean
2,590
2,603
/- Copyright (c) 2019 Kim Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kim Morrison, Jakob von Raumer -/ import Mathlib.CategoryTheory.Limits.Shapes.FiniteProducts import Mathlib.CategoryTheory.Limits.Shapes.Kernels /-! # Biproducts and binary biproducts We introduce the notion of (finite) biproducts. Binary biproducts are defined in `CategoryTheory.Limits.Shapes.BinaryBiproducts`. These are slightly unusual relative to the other shapes in the library, as they are simultaneously limits and colimits. (Zero objects are similar; they are "biterminal".) For results about biproducts in preadditive categories see `CategoryTheory.Preadditive.Biproducts`. For biproducts indexed by a `Fintype J`, a `bicone` consists of a cone point `X` and morphisms `π j : X ⟶ F j` and `ι j : F j ⟶ X` for each `j`, such that `ι j ≫ π j'` is the identity when `j = j'` and zero otherwise. ## Notation As `⊕` is already taken for the sum of types, we introduce the notation `X ⊞ Y` for a binary biproduct. We introduce `⨁ f` for the indexed biproduct. ## Implementation notes Prior to https://github.com/leanprover-community/mathlib3/pull/14046, `HasFiniteBiproducts` required a `DecidableEq` instance on the indexing type. As this had no pay-off (everything about limits is non-constructive in mathlib), and occasional cost (constructing decidability instances appropriate for constructions involving the indexing type), we made everything classical. -/ noncomputable section universe w w' v u open CategoryTheory Functor namespace CategoryTheory.Limits variable {J : Type w} universe uC' uC uD' uD variable {C : Type uC} [Category.{uC'} C] [HasZeroMorphisms C] variable {D : Type uD} [Category.{uD'} D] [HasZeroMorphisms D] open scoped Classical in /-- A `c : Bicone F` is: * an object `c.pt` and * morphisms `π j : pt ⟶ F j` and `ι j : F j ⟶ pt` for each `j`, * such that `ι j ≫ π j'` is the identity when `j = j'` and zero otherwise. -/ structure Bicone (F : J → C) where pt : C π : ∀ j, pt ⟶ F j ι : ∀ j, F j ⟶ pt ι_π : ∀ j j', ι j ≫ π j' = if h : j = j' then eqToHom (congrArg F h) else 0 := by aesop attribute [inherit_doc Bicone] Bicone.pt Bicone.π Bicone.ι Bicone.ι_π @[reassoc (attr := simp)] theorem bicone_ι_π_self {F : J → C} (B : Bicone F) (j : J) : B.ι j ≫ B.π j = 𝟙 (F j) := by simpa using B.ι_π j j @[reassoc (attr := simp)] theorem bicone_ι_π_ne {F : J → C} (B : Bicone F) {j j' : J} (h : j ≠ j') : B.ι j ≫ B.π j' = 0 := by simpa [h] using B.ι_π j j' variable {F : J → C} /-- A bicone morphism between two bicones for the same diagram is a morphism of the bicone points which commutes with the cone and cocone legs. -/ structure BiconeMorphism {F : J → C} (A B : Bicone F) where /-- A morphism between the two vertex objects of the bicones -/ hom : A.pt ⟶ B.pt /-- The triangle consisting of the two natural transformations and `hom` commutes -/ wπ : ∀ j : J, hom ≫ B.π j = A.π j := by aesop_cat /-- The triangle consisting of the two natural transformations and `hom` commutes -/ wι : ∀ j : J, A.ι j ≫ hom = B.ι j := by aesop_cat attribute [reassoc (attr := simp)] BiconeMorphism.wι BiconeMorphism.wπ /-- The category of bicones on a given diagram. -/ @[simps] instance Bicone.category : Category (Bicone F) where Hom A B := BiconeMorphism A B comp f g := { hom := f.hom ≫ g.hom } id B := { hom := 𝟙 B.pt } -- Porting note: if we do not have `simps` automatically generate the lemma for simplifying -- the `hom` field of a category, we need to write the `ext` lemma in terms of the categorical -- morphism, rather than the underlying structure. @[ext] theorem BiconeMorphism.ext {c c' : Bicone F} (f g : c ⟶ c') (w : f.hom = g.hom) : f = g := by cases f cases g congr namespace Bicones /-- To give an isomorphism between cocones, it suffices to give an isomorphism between their vertices which commutes with the cocone maps. -/ @[aesop apply safe (rule_sets := [CategoryTheory]), simps] def ext {c c' : Bicone F} (φ : c.pt ≅ c'.pt) (wι : ∀ j, c.ι j ≫ φ.hom = c'.ι j := by aesop_cat) (wπ : ∀ j, φ.hom ≫ c'.π j = c.π j := by aesop_cat) : c ≅ c' where hom := { hom := φ.hom } inv := { hom := φ.inv wι := fun j => φ.comp_inv_eq.mpr (wι j).symm wπ := fun j => φ.inv_comp_eq.mpr (wπ j).symm } variable (F) in /-- A functor `G : C ⥤ D` sends bicones over `F` to bicones over `G.obj ∘ F` functorially. -/ @[simps] def functoriality (G : C ⥤ D) [Functor.PreservesZeroMorphisms G] : Bicone F ⥤ Bicone (G.obj ∘ F) where obj A := { pt := G.obj A.pt π := fun j => G.map (A.π j) ι := fun j => G.map (A.ι j) ι_π := fun i j => (Functor.map_comp _ _ _).symm.trans <| by rw [A.ι_π] aesop_cat } map f := { hom := G.map f.hom wπ := fun j => by simp [-BiconeMorphism.wπ, ← f.wπ j] wι := fun j => by simp [-BiconeMorphism.wι, ← f.wι j] } variable (G : C ⥤ D) instance functoriality_full [G.PreservesZeroMorphisms] [G.Full] [G.Faithful] : (functoriality F G).Full where map_surjective t := ⟨{ hom := G.preimage t.hom wι := fun j => G.map_injective (by simpa using t.wι j) wπ := fun j => G.map_injective (by simpa using t.wπ j) }, by aesop_cat⟩ instance functoriality_faithful [G.PreservesZeroMorphisms] [G.Faithful] : (functoriality F G).Faithful where map_injective {_X} {_Y} f g h := BiconeMorphism.ext f g <| G.map_injective <| congr_arg BiconeMorphism.hom h end Bicones namespace Bicone attribute [local aesop safe tactic (rule_sets := [CategoryTheory])] CategoryTheory.Discrete.discreteCases -- Porting note: would it be okay to use this more generally? attribute [local aesop safe cases (rule_sets := [CategoryTheory])] Eq /-- Extract the cone from a bicone. -/ def toConeFunctor : Bicone F ⥤ Cone (Discrete.functor F) where obj B := { pt := B.pt, π := { app := fun j => B.π j.as } } map {_ _} F := { hom := F.hom, w := fun _ => F.wπ _ } /-- A shorthand for `toConeFunctor.obj` -/ abbrev toCone (B : Bicone F) : Cone (Discrete.functor F) := toConeFunctor.obj B -- TODO Consider changing this API to `toFan (B : Bicone F) : Fan F`. @[simp] theorem toCone_pt (B : Bicone F) : B.toCone.pt = B.pt := rfl @[simp] theorem toCone_π_app (B : Bicone F) (j : Discrete J) : B.toCone.π.app j = B.π j.as := rfl theorem toCone_π_app_mk (B : Bicone F) (j : J) : B.toCone.π.app ⟨j⟩ = B.π j := rfl @[simp] theorem toCone_proj (B : Bicone F) (j : J) : Fan.proj B.toCone j = B.π j := rfl /-- Extract the cocone from a bicone. -/ def toCoconeFunctor : Bicone F ⥤ Cocone (Discrete.functor F) where obj B := { pt := B.pt, ι := { app := fun j => B.ι j.as } } map {_ _} F := { hom := F.hom, w := fun _ => F.wι _ } /-- A shorthand for `toCoconeFunctor.obj` -/ abbrev toCocone (B : Bicone F) : Cocone (Discrete.functor F) := toCoconeFunctor.obj B @[simp] theorem toCocone_pt (B : Bicone F) : B.toCocone.pt = B.pt := rfl @[simp] theorem toCocone_ι_app (B : Bicone F) (j : Discrete J) : B.toCocone.ι.app j = B.ι j.as := rfl @[simp] theorem toCocone_inj (B : Bicone F) (j : J) : Cofan.inj B.toCocone j = B.ι j := rfl theorem toCocone_ι_app_mk (B : Bicone F) (j : J) : B.toCocone.ι.app ⟨j⟩ = B.ι j := rfl open scoped Classical in /-- We can turn any limit cone over a discrete collection of objects into a bicone. -/ @[simps] def ofLimitCone {f : J → C} {t : Cone (Discrete.functor f)} (ht : IsLimit t) : Bicone f where pt := t.pt π j := t.π.app ⟨j⟩ ι j := ht.lift (Fan.mk _ fun j' => if h : j = j' then eqToHom (congr_arg f h) else 0) ι_π j j' := by simp open scoped Classical in theorem ι_of_isLimit {f : J → C} {t : Bicone f} (ht : IsLimit t.toCone) (j : J) : t.ι j = ht.lift (Fan.mk _ fun j' => if h : j = j' then eqToHom (congr_arg f h) else 0) := ht.hom_ext fun j' => by rw [ht.fac] simp [t.ι_π] open scoped Classical in /-- We can turn any colimit cocone over a discrete collection of objects into a bicone. -/ @[simps] def ofColimitCocone {f : J → C} {t : Cocone (Discrete.functor f)} (ht : IsColimit t) : Bicone f where pt := t.pt π j := ht.desc (Cofan.mk _ fun j' => if h : j' = j then eqToHom (congr_arg f h) else 0) ι j := t.ι.app ⟨j⟩ ι_π j j' := by simp open scoped Classical in theorem π_of_isColimit {f : J → C} {t : Bicone f} (ht : IsColimit t.toCocone) (j : J) : t.π j = ht.desc (Cofan.mk _ fun j' => if h : j' = j then eqToHom (congr_arg f h) else 0) := ht.hom_ext fun j' => by rw [ht.fac] simp [t.ι_π] /-- Structure witnessing that a bicone is both a limit cone and a colimit cocone. -/ structure IsBilimit {F : J → C} (B : Bicone F) where isLimit : IsLimit B.toCone isColimit : IsColimit B.toCocone attribute [inherit_doc IsBilimit] IsBilimit.isLimit IsBilimit.isColimit attribute [simp] IsBilimit.mk.injEq attribute [local ext] Bicone.IsBilimit instance subsingleton_isBilimit {f : J → C} {c : Bicone f} : Subsingleton c.IsBilimit := ⟨fun _ _ => Bicone.IsBilimit.ext (Subsingleton.elim _ _) (Subsingleton.elim _ _)⟩ section Whisker variable {K : Type w'} /-- Whisker a bicone with an equivalence between the indexing types. -/ @[simps] def whisker {f : J → C} (c : Bicone f) (g : K ≃ J) : Bicone (f ∘ g) where pt := c.pt π k := c.π (g k) ι k := c.ι (g k) ι_π k k' := by simp only [c.ι_π] split_ifs with h h' h' <;> simp [Equiv.apply_eq_iff_eq g] at h h' <;> tauto /-- Taking the cone of a whiskered bicone results in a cone isomorphic to one gained by whiskering the cone and postcomposing with a suitable isomorphism. -/ def whiskerToCone {f : J → C} (c : Bicone f) (g : K ≃ J) : (c.whisker g).toCone ≅ (Cones.postcompose (Discrete.functorComp f g).inv).obj (c.toCone.whisker (Discrete.functor (Discrete.mk ∘ g))) := Cones.ext (Iso.refl _) (by simp) /-- Taking the cocone of a whiskered bicone results in a cone isomorphic to one gained by whiskering the cocone and precomposing with a suitable isomorphism. -/ def whiskerToCocone {f : J → C} (c : Bicone f) (g : K ≃ J) : (c.whisker g).toCocone ≅ (Cocones.precompose (Discrete.functorComp f g).hom).obj (c.toCocone.whisker (Discrete.functor (Discrete.mk ∘ g))) := Cocones.ext (Iso.refl _) (by simp) /-- Whiskering a bicone with an equivalence between types preserves being a bilimit bicone. -/ noncomputable def whiskerIsBilimitIff {f : J → C} (c : Bicone f) (g : K ≃ J) : (c.whisker g).IsBilimit ≃ c.IsBilimit := by refine equivOfSubsingletonOfSubsingleton (fun hc => ⟨?_, ?_⟩) fun hc => ⟨?_, ?_⟩ · let this := IsLimit.ofIsoLimit hc.isLimit (Bicone.whiskerToCone c g) let this := (IsLimit.postcomposeHomEquiv (Discrete.functorComp f g).symm _) this exact IsLimit.ofWhiskerEquivalence (Discrete.equivalence g) this · let this := IsColimit.ofIsoColimit hc.isColimit (Bicone.whiskerToCocone c g) let this := (IsColimit.precomposeHomEquiv (Discrete.functorComp f g) _) this exact IsColimit.ofWhiskerEquivalence (Discrete.equivalence g) this · apply IsLimit.ofIsoLimit _ (Bicone.whiskerToCone c g).symm apply (IsLimit.postcomposeHomEquiv (Discrete.functorComp f g).symm _).symm _ exact IsLimit.whiskerEquivalence hc.isLimit (Discrete.equivalence g) · apply IsColimit.ofIsoColimit _ (Bicone.whiskerToCocone c g).symm apply (IsColimit.precomposeHomEquiv (Discrete.functorComp f g) _).symm _ exact IsColimit.whiskerEquivalence hc.isColimit (Discrete.equivalence g) end Whisker end Bicone /-- A bicone over `F : J → C`, which is both a limit cone and a colimit cocone. -/ structure LimitBicone (F : J → C) where bicone : Bicone F isBilimit : bicone.IsBilimit attribute [inherit_doc LimitBicone] LimitBicone.bicone LimitBicone.isBilimit /-- `HasBiproduct F` expresses the mere existence of a bicone which is simultaneously a limit and a colimit of the diagram `F`. -/ class HasBiproduct (F : J → C) : Prop where mk' :: exists_biproduct : Nonempty (LimitBicone F) attribute [inherit_doc HasBiproduct] HasBiproduct.exists_biproduct theorem HasBiproduct.mk {F : J → C} (d : LimitBicone F) : HasBiproduct F := ⟨Nonempty.intro d⟩ /-- Use the axiom of choice to extract explicit `BiproductData F` from `HasBiproduct F`. -/ def getBiproductData (F : J → C) [HasBiproduct F] : LimitBicone F := Classical.choice HasBiproduct.exists_biproduct /-- A bicone for `F` which is both a limit cone and a colimit cocone. -/ def biproduct.bicone (F : J → C) [HasBiproduct F] : Bicone F := (getBiproductData F).bicone /-- `biproduct.bicone F` is a bilimit bicone. -/ def biproduct.isBilimit (F : J → C) [HasBiproduct F] : (biproduct.bicone F).IsBilimit := (getBiproductData F).isBilimit /-- `biproduct.bicone F` is a limit cone. -/ def biproduct.isLimit (F : J → C) [HasBiproduct F] : IsLimit (biproduct.bicone F).toCone := (getBiproductData F).isBilimit.isLimit /-- `biproduct.bicone F` is a colimit cocone. -/ def biproduct.isColimit (F : J → C) [HasBiproduct F] : IsColimit (biproduct.bicone F).toCocone := (getBiproductData F).isBilimit.isColimit instance (priority := 100) hasProduct_of_hasBiproduct [HasBiproduct F] : HasProduct F := HasLimit.mk { cone := (biproduct.bicone F).toCone isLimit := biproduct.isLimit F } instance (priority := 100) hasCoproduct_of_hasBiproduct [HasBiproduct F] : HasCoproduct F := HasColimit.mk { cocone := (biproduct.bicone F).toCocone isColimit := biproduct.isColimit F } variable (J C) /-- `C` has biproducts of shape `J` if we have a limit and a colimit, with the same cone points, of every function `F : J → C`. -/ class HasBiproductsOfShape : Prop where has_biproduct : ∀ F : J → C, HasBiproduct F attribute [instance 100] HasBiproductsOfShape.has_biproduct /-- `HasFiniteBiproducts C` represents a choice of biproduct for every family of objects in `C` indexed by a finite type. -/ class HasFiniteBiproducts : Prop where out : ∀ n, HasBiproductsOfShape (Fin n) C attribute [inherit_doc HasFiniteBiproducts] HasFiniteBiproducts.out variable {J} theorem hasBiproductsOfShape_of_equiv {K : Type w'} [HasBiproductsOfShape K C] (e : J ≃ K) : HasBiproductsOfShape J C := ⟨fun F => let ⟨⟨h⟩⟩ := HasBiproductsOfShape.has_biproduct (F ∘ e.symm) let ⟨c, hc⟩ := h HasBiproduct.mk <| by simpa only [Function.comp_def, e.symm_apply_apply] using LimitBicone.mk (c.whisker e) ((c.whiskerIsBilimitIff _).2 hc)⟩ instance (priority := 100) hasBiproductsOfShape_finite [HasFiniteBiproducts C] [Finite J] : HasBiproductsOfShape J C := by rcases Finite.exists_equiv_fin J with ⟨n, ⟨e⟩⟩ haveI : HasBiproductsOfShape (Fin n) C := HasFiniteBiproducts.out n exact hasBiproductsOfShape_of_equiv C e instance (priority := 100) hasFiniteProducts_of_hasFiniteBiproducts [HasFiniteBiproducts C] : HasFiniteProducts C where out _ := ⟨fun _ => hasLimit_of_iso Discrete.natIsoFunctor.symm⟩ instance (priority := 100) hasFiniteCoproducts_of_hasFiniteBiproducts [HasFiniteBiproducts C] : HasFiniteCoproducts C where out _ := ⟨fun _ => hasColimit_of_iso Discrete.natIsoFunctor⟩ instance (priority := 100) hasProductsOfShape_of_hasBiproductsOfShape [HasBiproductsOfShape J C] : HasProductsOfShape J C where has_limit _ := hasLimit_of_iso Discrete.natIsoFunctor.symm instance (priority := 100) hasCoproductsOfShape_of_hasBiproductsOfShape [HasBiproductsOfShape J C] : HasCoproductsOfShape J C where has_colimit _ := hasColimit_of_iso Discrete.natIsoFunctor variable {C} /-- The isomorphism between the specified limit and the specified colimit for a functor with a bilimit. -/ def biproductIso (F : J → C) [HasBiproduct F] : Limits.piObj F ≅ Limits.sigmaObj F := (IsLimit.conePointUniqueUpToIso (limit.isLimit _) (biproduct.isLimit F)).trans <| IsColimit.coconePointUniqueUpToIso (biproduct.isColimit F) (colimit.isColimit _) variable {J : Type w} {K : Type*} variable {C : Type u} [Category.{v} C] [HasZeroMorphisms C] /-- `biproduct f` computes the biproduct of a family of elements `f`. (It is defined as an abbreviation for `limit (Discrete.functor f)`, so for most facts about `biproduct f`, you will just use general facts about limits and colimits.) -/ abbrev biproduct (f : J → C) [HasBiproduct f] : C := (biproduct.bicone f).pt @[inherit_doc biproduct] notation "⨁ " f:20 => biproduct f /-- The projection onto a summand of a biproduct. -/ abbrev biproduct.π (f : J → C) [HasBiproduct f] (b : J) : ⨁ f ⟶ f b := (biproduct.bicone f).π b @[simp] theorem biproduct.bicone_π (f : J → C) [HasBiproduct f] (b : J) : (biproduct.bicone f).π b = biproduct.π f b := rfl /-- The inclusion into a summand of a biproduct. -/ abbrev biproduct.ι (f : J → C) [HasBiproduct f] (b : J) : f b ⟶ ⨁ f := (biproduct.bicone f).ι b @[simp] theorem biproduct.bicone_ι (f : J → C) [HasBiproduct f] (b : J) : (biproduct.bicone f).ι b = biproduct.ι f b := rfl /-- Note that as this lemma has an `if` in the statement, we include a `DecidableEq` argument. This means you may not be able to `simp` using this lemma unless you `open scoped Classical`. -/ @[reassoc] theorem biproduct.ι_π [DecidableEq J] (f : J → C) [HasBiproduct f] (j j' : J) : biproduct.ι f j ≫ biproduct.π f j' = if h : j = j' then eqToHom (congr_arg f h) else 0 := by convert (biproduct.bicone f).ι_π j j' @[reassoc] -- Porting note: both versions proven by simp theorem biproduct.ι_π_self (f : J → C) [HasBiproduct f] (j : J) : biproduct.ι f j ≫ biproduct.π f j = 𝟙 _ := by simp [biproduct.ι_π] @[reassoc (attr := simp)] theorem biproduct.ι_π_ne (f : J → C) [HasBiproduct f] {j j' : J} (h : j ≠ j') : biproduct.ι f j ≫ biproduct.π f j' = 0 := by simp [biproduct.ι_π, h] -- The `simpNF` linter incorrectly identifies these as simp lemmas that could never apply. -- It seems the side condition `w` is not applied by `simpNF`. -- https://github.com/leanprover-community/mathlib4/issues/5049 -- They are used by `simp` in `biproduct.whiskerEquiv` below. @[reassoc (attr := simp, nolint simpNF)] theorem biproduct.eqToHom_comp_ι (f : J → C) [HasBiproduct f] {j j' : J} (w : j = j') : eqToHom (by simp [w]) ≫ biproduct.ι f j' = biproduct.ι f j := by cases w simp -- The `simpNF` linter incorrectly identifies these as simp lemmas that could never apply. -- It seems the side condition `w` is not applied by `simpNF`. -- https://github.com/leanprover-community/mathlib4/issues/5049 -- They are used by `simp` in `biproduct.whiskerEquiv` below. @[reassoc (attr := simp, nolint simpNF)] theorem biproduct.π_comp_eqToHom (f : J → C) [HasBiproduct f] {j j' : J} (w : j = j') : biproduct.π f j ≫ eqToHom (by simp [w]) = biproduct.π f j' := by cases w simp /-- Given a collection of maps into the summands, we obtain a map into the biproduct. -/ abbrev biproduct.lift {f : J → C} [HasBiproduct f] {P : C} (p : ∀ b, P ⟶ f b) : P ⟶ ⨁ f := (biproduct.isLimit f).lift (Fan.mk P p) /-- Given a collection of maps out of the summands, we obtain a map out of the biproduct. -/ abbrev biproduct.desc {f : J → C} [HasBiproduct f] {P : C} (p : ∀ b, f b ⟶ P) : ⨁ f ⟶ P := (biproduct.isColimit f).desc (Cofan.mk P p) @[reassoc (attr := simp)] theorem biproduct.lift_π {f : J → C} [HasBiproduct f] {P : C} (p : ∀ b, P ⟶ f b) (j : J) : biproduct.lift p ≫ biproduct.π f j = p j := (biproduct.isLimit f).fac _ ⟨j⟩ @[reassoc (attr := simp)] theorem biproduct.ι_desc {f : J → C} [HasBiproduct f] {P : C} (p : ∀ b, f b ⟶ P) (j : J) : biproduct.ι f j ≫ biproduct.desc p = p j := (biproduct.isColimit f).fac _ ⟨j⟩ /-- Given a collection of maps between corresponding summands of a pair of biproducts indexed by the same type, we obtain a map between the biproducts. -/ abbrev biproduct.map {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ b, f b ⟶ g b) : ⨁ f ⟶ ⨁ g := IsLimit.map (biproduct.bicone f).toCone (biproduct.isLimit g) (Discrete.natTrans (fun j => p j.as)) /-- An alternative to `biproduct.map` constructed via colimits. This construction only exists in order to show it is equal to `biproduct.map`. -/ abbrev biproduct.map' {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ b, f b ⟶ g b) : ⨁ f ⟶ ⨁ g := IsColimit.map (biproduct.isColimit f) (biproduct.bicone g).toCocone (Discrete.natTrans fun j => p j.as) -- We put this at slightly higher priority than `biproduct.hom_ext'`, -- to get the matrix indices in the "right" order. @[ext 1001] theorem biproduct.hom_ext {f : J → C} [HasBiproduct f] {Z : C} (g h : Z ⟶ ⨁ f) (w : ∀ j, g ≫ biproduct.π f j = h ≫ biproduct.π f j) : g = h := (biproduct.isLimit f).hom_ext fun j => w j.as @[ext] theorem biproduct.hom_ext' {f : J → C} [HasBiproduct f] {Z : C} (g h : ⨁ f ⟶ Z) (w : ∀ j, biproduct.ι f j ≫ g = biproduct.ι f j ≫ h) : g = h := (biproduct.isColimit f).hom_ext fun j => w j.as /-- The canonical isomorphism between the chosen biproduct and the chosen product. -/ def biproduct.isoProduct (f : J → C) [HasBiproduct f] : ⨁ f ≅ ∏ᶜ f := IsLimit.conePointUniqueUpToIso (biproduct.isLimit f) (limit.isLimit _) @[simp] theorem biproduct.isoProduct_hom {f : J → C} [HasBiproduct f] : (biproduct.isoProduct f).hom = Pi.lift (biproduct.π f) := limit.hom_ext fun j => by simp [biproduct.isoProduct] @[simp] theorem biproduct.isoProduct_inv {f : J → C} [HasBiproduct f] : (biproduct.isoProduct f).inv = biproduct.lift (Pi.π f) := biproduct.hom_ext _ _ fun j => by simp [Iso.inv_comp_eq] /-- The canonical isomorphism between the chosen biproduct and the chosen coproduct. -/ def biproduct.isoCoproduct (f : J → C) [HasBiproduct f] : ⨁ f ≅ ∐ f := IsColimit.coconePointUniqueUpToIso (biproduct.isColimit f) (colimit.isColimit _) @[simp] theorem biproduct.isoCoproduct_inv {f : J → C} [HasBiproduct f] : (biproduct.isoCoproduct f).inv = Sigma.desc (biproduct.ι f) := colimit.hom_ext fun j => by simp [biproduct.isoCoproduct] @[simp] theorem biproduct.isoCoproduct_hom {f : J → C} [HasBiproduct f] : (biproduct.isoCoproduct f).hom = biproduct.desc (Sigma.ι f) := biproduct.hom_ext' _ _ fun j => by simp [← Iso.eq_comp_inv] /-- If a category has biproducts of a shape `J`, its `colim` and `lim` functor on diagrams over `J` are isomorphic. -/ @[simps!] def HasBiproductsOfShape.colimIsoLim [HasBiproductsOfShape J C] : colim (J := Discrete J) (C := C) ≅ lim := NatIso.ofComponents (fun F => (Sigma.isoColimit F).symm ≪≫ (biproduct.isoCoproduct _).symm ≪≫ biproduct.isoProduct _ ≪≫ Pi.isoLimit F) fun η => colimit.hom_ext fun ⟨i⟩ => limit.hom_ext fun ⟨j⟩ => by classical by_cases h : i = j <;> simp_all [h, Sigma.isoColimit, Pi.isoLimit, biproduct.ι_π, biproduct.ι_π_assoc] theorem biproduct.map_eq_map' {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ b, f b ⟶ g b) : biproduct.map p = biproduct.map' p := by classical ext dsimp simp only [Discrete.natTrans_app, Limits.IsColimit.ι_map_assoc, Limits.IsLimit.map_π, Category.assoc, ← Bicone.toCone_π_app_mk, ← biproduct.bicone_π, ← Bicone.toCocone_ι_app_mk, ← biproduct.bicone_ι] dsimp rw [biproduct.ι_π_assoc, biproduct.ι_π] split_ifs with h · subst h; simp · simp @[reassoc (attr := simp)] theorem biproduct.map_π {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) (j : J) : biproduct.map p ≫ biproduct.π g j = biproduct.π f j ≫ p j := Limits.IsLimit.map_π _ _ _ (Discrete.mk j) @[reassoc (attr := simp)] theorem biproduct.ι_map {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) (j : J) : biproduct.ι f j ≫ biproduct.map p = p j ≫ biproduct.ι g j := by rw [biproduct.map_eq_map'] apply Limits.IsColimit.ι_map (biproduct.isColimit f) (biproduct.bicone g).toCocone (Discrete.natTrans fun j => p j.as) (Discrete.mk j) @[reassoc (attr := simp)] theorem biproduct.map_desc {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) {P : C} (k : ∀ j, g j ⟶ P) : biproduct.map p ≫ biproduct.desc k = biproduct.desc fun j => p j ≫ k j := by ext; simp @[reassoc (attr := simp)] theorem biproduct.lift_map {f g : J → C} [HasBiproduct f] [HasBiproduct g] {P : C} (k : ∀ j, P ⟶ f j) (p : ∀ j, f j ⟶ g j) : biproduct.lift k ≫ biproduct.map p = biproduct.lift fun j => k j ≫ p j := by ext; simp /-- Given a collection of isomorphisms between corresponding summands of a pair of biproducts indexed by the same type, we obtain an isomorphism between the biproducts. -/ @[simps] def biproduct.mapIso {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ b, f b ≅ g b) : ⨁ f ≅ ⨁ g where hom := biproduct.map fun b => (p b).hom inv := biproduct.map fun b => (p b).inv instance biproduct.map_epi {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) [∀ j, Epi (p j)] : Epi (biproduct.map p) := by classical have : biproduct.map p = (biproduct.isoCoproduct _).hom ≫ Sigma.map p ≫ (biproduct.isoCoproduct _).inv := by ext simp only [map_π, isoCoproduct_hom, isoCoproduct_inv, Category.assoc, ι_desc_assoc, ι_colimMap_assoc, Discrete.functor_obj_eq_as, Discrete.natTrans_app, colimit.ι_desc_assoc, Cofan.mk_pt, Cofan.mk_ι_app, ι_π, ι_π_assoc] split all_goals simp_all rw [this] infer_instance instance Pi.map_epi {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) [∀ j, Epi (p j)] : Epi (Pi.map p) := by rw [show Pi.map p = (biproduct.isoProduct _).inv ≫ biproduct.map p ≫ (biproduct.isoProduct _).hom by aesop] infer_instance instance biproduct.map_mono {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) [∀ j, Mono (p j)] : Mono (biproduct.map p) := by rw [show biproduct.map p = (biproduct.isoProduct _).hom ≫ Pi.map p ≫ (biproduct.isoProduct _).inv by aesop] infer_instance instance Sigma.map_mono {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) [∀ j, Mono (p j)] : Mono (Sigma.map p) := by rw [show Sigma.map p = (biproduct.isoCoproduct _).inv ≫ biproduct.map p ≫ (biproduct.isoCoproduct _).hom by aesop] infer_instance /-- Two biproducts which differ by an equivalence in the indexing type, and up to isomorphism in the factors, are isomorphic. Unfortunately there are two natural ways to define each direction of this isomorphism (because it is true for both products and coproducts separately). We give the alternative definitions as lemmas below. -/ @[simps] def biproduct.whiskerEquiv {f : J → C} {g : K → C} (e : J ≃ K) (w : ∀ j, g (e j) ≅ f j) [HasBiproduct f] [HasBiproduct g] : ⨁ f ≅ ⨁ g where hom := biproduct.desc fun j => (w j).inv ≫ biproduct.ι g (e j) inv := biproduct.desc fun k => eqToHom (by simp) ≫ (w (e.symm k)).hom ≫ biproduct.ι f _ lemma biproduct.whiskerEquiv_hom_eq_lift {f : J → C} {g : K → C} (e : J ≃ K) (w : ∀ j, g (e j) ≅ f j) [HasBiproduct f] [HasBiproduct g] : (biproduct.whiskerEquiv e w).hom = biproduct.lift fun k => biproduct.π f (e.symm k) ≫ (w _).inv ≫ eqToHom (by simp) := by simp only [whiskerEquiv_hom] ext k j by_cases h : k = e j · subst h simp · simp only [ι_desc_assoc, Category.assoc, ne_eq, lift_π] rw [biproduct.ι_π_ne, biproduct.ι_π_ne_assoc] · simp · rintro rfl simp at h · exact Ne.symm h lemma biproduct.whiskerEquiv_inv_eq_lift {f : J → C} {g : K → C} (e : J ≃ K) (w : ∀ j, g (e j) ≅ f j) [HasBiproduct f] [HasBiproduct g] : (biproduct.whiskerEquiv e w).inv = biproduct.lift fun j => biproduct.π g (e j) ≫ (w j).hom := by simp only [whiskerEquiv_inv] ext j k by_cases h : k = e j · subst h simp only [ι_desc_assoc, ← eqToHom_iso_hom_naturality_assoc w (e.symm_apply_apply j).symm, Equiv.symm_apply_apply, eqToHom_comp_ι, Category.assoc, bicone_ι_π_self, Category.comp_id, lift_π, bicone_ι_π_self_assoc] · simp only [ι_desc_assoc, Category.assoc, ne_eq, lift_π] rw [biproduct.ι_π_ne, biproduct.ι_π_ne_assoc] · simp · exact h · rintro rfl simp at h attribute [local simp] Sigma.forall in instance {ι} (f : ι → Type*) (g : (i : ι) → (f i) → C) [∀ i, HasBiproduct (g i)] [HasBiproduct fun i => ⨁ g i] : HasBiproduct fun p : Σ i, f i => g p.1 p.2 where exists_biproduct := Nonempty.intro { bicone := { pt := ⨁ fun i => ⨁ g i ι := fun X => biproduct.ι (g X.1) X.2 ≫ biproduct.ι (fun i => ⨁ g i) X.1 π := fun X => biproduct.π (fun i => ⨁ g i) X.1 ≫ biproduct.π (g X.1) X.2 ι_π := fun ⟨j, x⟩ ⟨j', y⟩ => by split_ifs with h · obtain ⟨rfl, rfl⟩ := h simp · simp only [Sigma.mk.inj_iff, not_and] at h by_cases w : j = j' · cases w simp only [heq_eq_eq, forall_true_left] at h simp [biproduct.ι_π_ne _ h] · simp [biproduct.ι_π_ne_assoc _ w] } isBilimit := { isLimit := mkFanLimit _ (fun s => biproduct.lift fun b => biproduct.lift fun c => s.proj ⟨b, c⟩) isColimit := mkCofanColimit _ (fun s => biproduct.desc fun b => biproduct.desc fun c => s.inj ⟨b, c⟩) } } /-- An iterated biproduct is a biproduct over a sigma type. -/ @[simps] def biproductBiproductIso {ι} (f : ι → Type*) (g : (i : ι) → (f i) → C) [∀ i, HasBiproduct (g i)] [HasBiproduct fun i => ⨁ g i] : (⨁ fun i => ⨁ g i) ≅ (⨁ fun p : Σ i, f i => g p.1 p.2) where hom := biproduct.lift fun ⟨i, x⟩ => biproduct.π _ i ≫ biproduct.π _ x inv := biproduct.lift fun i => biproduct.lift fun x => biproduct.π _ (⟨i, x⟩ : Σ i, f i) section πKernel section variable (f : J → C) [HasBiproduct f] variable (p : J → Prop) [HasBiproduct (Subtype.restrict p f)] /-- The canonical morphism from the biproduct over a restricted index type to the biproduct of the full index type. -/ def biproduct.fromSubtype : ⨁ Subtype.restrict p f ⟶ ⨁ f := biproduct.desc fun j => biproduct.ι _ j.val /-- The canonical morphism from a biproduct to the biproduct over a restriction of its index type. -/ def biproduct.toSubtype : ⨁ f ⟶ ⨁ Subtype.restrict p f := biproduct.lift fun _ => biproduct.π _ _ @[reassoc (attr := simp)] theorem biproduct.fromSubtype_π [DecidablePred p] (j : J) : biproduct.fromSubtype f p ≫ biproduct.π f j = if h : p j then biproduct.π (Subtype.restrict p f) ⟨j, h⟩ else 0 := by classical ext i; dsimp rw [biproduct.fromSubtype, biproduct.ι_desc_assoc, biproduct.ι_π] by_cases h : p j · rw [dif_pos h, biproduct.ι_π] split_ifs with h₁ h₂ h₂ exacts [rfl, False.elim (h₂ (Subtype.ext h₁)), False.elim (h₁ (congr_arg Subtype.val h₂)), rfl] · rw [dif_neg h, dif_neg (show (i : J) ≠ j from fun h₂ => h (h₂ ▸ i.2)), comp_zero] theorem biproduct.fromSubtype_eq_lift [DecidablePred p] : biproduct.fromSubtype f p = biproduct.lift fun j => if h : p j then biproduct.π (Subtype.restrict p f) ⟨j, h⟩ else 0 := biproduct.hom_ext _ _ (by simp) @[reassoc] -- Porting note: both version solved using simp theorem biproduct.fromSubtype_π_subtype (j : Subtype p) : biproduct.fromSubtype f p ≫ biproduct.π f j = biproduct.π (Subtype.restrict p f) j := by classical ext rw [biproduct.fromSubtype, biproduct.ι_desc_assoc, biproduct.ι_π, biproduct.ι_π] split_ifs with h₁ h₂ h₂ exacts [rfl, False.elim (h₂ (Subtype.ext h₁)), False.elim (h₁ (congr_arg Subtype.val h₂)), rfl] @[reassoc (attr := simp)] theorem biproduct.toSubtype_π (j : Subtype p) : biproduct.toSubtype f p ≫ biproduct.π (Subtype.restrict p f) j = biproduct.π f j := biproduct.lift_π _ _ @[reassoc (attr := simp)] theorem biproduct.ι_toSubtype [DecidablePred p] (j : J) : biproduct.ι f j ≫ biproduct.toSubtype f p = if h : p j then biproduct.ι (Subtype.restrict p f) ⟨j, h⟩ else 0 := by classical ext i rw [biproduct.toSubtype, Category.assoc, biproduct.lift_π, biproduct.ι_π] by_cases h : p j · rw [dif_pos h, biproduct.ι_π] split_ifs with h₁ h₂ h₂ exacts [rfl, False.elim (h₂ (Subtype.ext h₁)), False.elim (h₁ (congr_arg Subtype.val h₂)), rfl] · rw [dif_neg h, dif_neg (show j ≠ i from fun h₂ => h (h₂.symm ▸ i.2)), zero_comp] theorem biproduct.toSubtype_eq_desc [DecidablePred p] : biproduct.toSubtype f p = biproduct.desc fun j => if h : p j then biproduct.ι (Subtype.restrict p f) ⟨j, h⟩ else 0 := biproduct.hom_ext' _ _ (by simp) @[reassoc] theorem biproduct.ι_toSubtype_subtype (j : Subtype p) : biproduct.ι f j ≫ biproduct.toSubtype f p = biproduct.ι (Subtype.restrict p f) j := by classical ext rw [biproduct.toSubtype, Category.assoc, biproduct.lift_π, biproduct.ι_π, biproduct.ι_π] split_ifs with h₁ h₂ h₂ exacts [rfl, False.elim (h₂ (Subtype.ext h₁)), False.elim (h₁ (congr_arg Subtype.val h₂)), rfl] @[reassoc (attr := simp)] theorem biproduct.ι_fromSubtype (j : Subtype p) : biproduct.ι (Subtype.restrict p f) j ≫ biproduct.fromSubtype f p = biproduct.ι f j := biproduct.ι_desc _ _ @[reassoc (attr := simp)] theorem biproduct.fromSubtype_toSubtype : biproduct.fromSubtype f p ≫ biproduct.toSubtype f p = 𝟙 (⨁ Subtype.restrict p f) := by refine biproduct.hom_ext _ _ fun j => ?_ rw [Category.assoc, biproduct.toSubtype_π, biproduct.fromSubtype_π_subtype, Category.id_comp] @[reassoc (attr := simp)] theorem biproduct.toSubtype_fromSubtype [DecidablePred p] : biproduct.toSubtype f p ≫ biproduct.fromSubtype f p = biproduct.map fun j => if p j then 𝟙 (f j) else 0 := by ext1 i by_cases h : p i · simp [h] · simp [h] end section variable (f : J → C) (i : J) [HasBiproduct f] [HasBiproduct (Subtype.restrict (fun j => j ≠ i) f)] open scoped Classical in /-- The kernel of `biproduct.π f i` is the inclusion from the biproduct which omits `i` from the index set `J` into the biproduct over `J`. -/ def biproduct.isLimitFromSubtype : IsLimit (KernelFork.ofι (biproduct.fromSubtype f fun j => j ≠ i) (by simp) : KernelFork (biproduct.π f i)) := Fork.IsLimit.mk' _ fun s => ⟨s.ι ≫ biproduct.toSubtype _ _, by apply biproduct.hom_ext; intro j rw [KernelFork.ι_ofι, Category.assoc, Category.assoc, biproduct.toSubtype_fromSubtype_assoc, biproduct.map_π] rcases Classical.em (i = j) with (rfl | h) · rw [if_neg (Classical.not_not.2 rfl), comp_zero, comp_zero, KernelFork.condition] · rw [if_pos (Ne.symm h), Category.comp_id], by intro m hm rw [← hm, KernelFork.ι_ofι, Category.assoc, biproduct.fromSubtype_toSubtype] exact (Category.comp_id _).symm⟩ instance : HasKernel (biproduct.π f i) := HasLimit.mk ⟨_, biproduct.isLimitFromSubtype f i⟩ /-- The kernel of `biproduct.π f i` is `⨁ Subtype.restrict {i}ᶜ f`. -/ @[simps!] def kernelBiproductπIso : kernel (biproduct.π f i) ≅ ⨁ Subtype.restrict (fun j => j ≠ i) f := limit.isoLimitCone ⟨_, biproduct.isLimitFromSubtype f i⟩ open scoped Classical in /-- The cokernel of `biproduct.ι f i` is the projection from the biproduct over the index set `J` onto the biproduct omitting `i`. -/ def biproduct.isColimitToSubtype : IsColimit (CokernelCofork.ofπ (biproduct.toSubtype f fun j => j ≠ i) (by simp) : CokernelCofork (biproduct.ι f i)) := Cofork.IsColimit.mk' _ fun s => ⟨biproduct.fromSubtype _ _ ≫ s.π, by apply biproduct.hom_ext'; intro j rw [CokernelCofork.π_ofπ, biproduct.toSubtype_fromSubtype_assoc, biproduct.ι_map_assoc] rcases Classical.em (i = j) with (rfl | h) · rw [if_neg (Classical.not_not.2 rfl), zero_comp, CokernelCofork.condition] · rw [if_pos (Ne.symm h), Category.id_comp], by intro m hm rw [← hm, CokernelCofork.π_ofπ, ← Category.assoc, biproduct.fromSubtype_toSubtype] exact (Category.id_comp _).symm⟩ instance : HasCokernel (biproduct.ι f i) := HasColimit.mk ⟨_, biproduct.isColimitToSubtype f i⟩ /-- The cokernel of `biproduct.ι f i` is `⨁ Subtype.restrict {i}ᶜ f`. -/ @[simps!] def cokernelBiproductιIso : cokernel (biproduct.ι f i) ≅ ⨁ Subtype.restrict (fun j => j ≠ i) f := colimit.isoColimitCocone ⟨_, biproduct.isColimitToSubtype f i⟩ end section -- Per https://github.com/leanprover-community/mathlib3/pull/15067, we only allow indexing in `Type 0` here. variable {K : Type} [Finite K] [HasFiniteBiproducts C] (f : K → C) /-- The limit cone exhibiting `⨁ Subtype.restrict pᶜ f` as the kernel of `biproduct.toSubtype f p` -/ @[simps] def kernelForkBiproductToSubtype (p : Set K) : LimitCone (parallelPair (biproduct.toSubtype f p) 0) where cone := KernelFork.ofι (biproduct.fromSubtype f pᶜ) (by classical ext j k simp only [Category.assoc, biproduct.ι_fromSubtype_assoc, biproduct.ι_toSubtype_assoc, comp_zero, zero_comp] rw [dif_neg k.2] simp only [zero_comp]) isLimit := KernelFork.IsLimit.ofι _ _ (fun {_} g _ => g ≫ biproduct.toSubtype f pᶜ) (by classical intro W' g' w ext j simp only [Category.assoc, biproduct.toSubtype_fromSubtype, Pi.compl_apply, biproduct.map_π] split_ifs with h · simp · replace w := w =≫ biproduct.π _ ⟨j, not_not.mp h⟩ simpa using w.symm) (by aesop_cat) instance (p : Set K) : HasKernel (biproduct.toSubtype f p) := HasLimit.mk (kernelForkBiproductToSubtype f p) /-- The kernel of `biproduct.toSubtype f p` is `⨁ Subtype.restrict pᶜ f`. -/ @[simps!] def kernelBiproductToSubtypeIso (p : Set K) : kernel (biproduct.toSubtype f p) ≅ ⨁ Subtype.restrict pᶜ f := limit.isoLimitCone (kernelForkBiproductToSubtype f p) /-- The colimit cocone exhibiting `⨁ Subtype.restrict pᶜ f` as the cokernel of `biproduct.fromSubtype f p` -/ @[simps] def cokernelCoforkBiproductFromSubtype (p : Set K) : ColimitCocone (parallelPair (biproduct.fromSubtype f p) 0) where cocone := CokernelCofork.ofπ (biproduct.toSubtype f pᶜ) (by classical ext j k simp only [Category.assoc, Pi.compl_apply, biproduct.ι_fromSubtype_assoc, biproduct.ι_toSubtype_assoc, comp_zero, zero_comp] rw [dif_neg] · simp only [zero_comp] · exact not_not.mpr k.2) isColimit := CokernelCofork.IsColimit.ofπ _ _ (fun {_} g _ => biproduct.fromSubtype f pᶜ ≫ g) (by classical intro W g' w ext j simp only [biproduct.toSubtype_fromSubtype_assoc, Pi.compl_apply, biproduct.ι_map_assoc] split_ifs with h · simp · replace w := biproduct.ι _ (⟨j, not_not.mp h⟩ : p) ≫= w simpa using w.symm) (by aesop_cat) instance (p : Set K) : HasCokernel (biproduct.fromSubtype f p) := HasColimit.mk (cokernelCoforkBiproductFromSubtype f p) /-- The cokernel of `biproduct.fromSubtype f p` is `⨁ Subtype.restrict pᶜ f`. -/ @[simps!] def cokernelBiproductFromSubtypeIso (p : Set K) : cokernel (biproduct.fromSubtype f p) ≅ ⨁ Subtype.restrict pᶜ f := colimit.isoColimitCocone (cokernelCoforkBiproductFromSubtype f p) end end πKernel section FiniteBiproducts variable {J : Type} [Finite J] {K : Type} [Finite K] {C : Type u} [Category.{v} C] [HasZeroMorphisms C] [HasFiniteBiproducts C] {f : J → C} {g : K → C} /-- Convert a (dependently typed) matrix to a morphism of biproducts. -/ def biproduct.matrix (m : ∀ j k, f j ⟶ g k) : ⨁ f ⟶ ⨁ g := biproduct.desc fun j => biproduct.lift fun k => m j k @[reassoc (attr := simp)] theorem biproduct.matrix_π (m : ∀ j k, f j ⟶ g k) (k : K) : biproduct.matrix m ≫ biproduct.π g k = biproduct.desc fun j => m j k := by ext simp [biproduct.matrix] @[reassoc (attr := simp)] theorem biproduct.ι_matrix (m : ∀ j k, f j ⟶ g k) (j : J) : biproduct.ι f j ≫ biproduct.matrix m = biproduct.lift fun k => m j k := by ext simp [biproduct.matrix] /-- Extract the matrix components from a morphism of biproducts. -/ def biproduct.components (m : ⨁ f ⟶ ⨁ g) (j : J) (k : K) : f j ⟶ g k := biproduct.ι f j ≫ m ≫ biproduct.π g k @[simp] theorem biproduct.matrix_components (m : ∀ j k, f j ⟶ g k) (j : J) (k : K) : biproduct.components (biproduct.matrix m) j k = m j k := by simp [biproduct.components] @[simp] theorem biproduct.components_matrix (m : ⨁ f ⟶ ⨁ g) : (biproduct.matrix fun j k => biproduct.components m j k) = m := by ext simp [biproduct.components] /-- Morphisms between direct sums are matrices. -/ @[simps] def biproduct.matrixEquiv : (⨁ f ⟶ ⨁ g) ≃ ∀ j k, f j ⟶ g k where toFun := biproduct.components invFun := biproduct.matrix left_inv := biproduct.components_matrix right_inv m := by ext apply biproduct.matrix_components end FiniteBiproducts variable {J : Type w} variable {C : Type u} [Category.{v} C] [HasZeroMorphisms C] variable {D : Type uD} [Category.{uD'} D] [HasZeroMorphisms D] instance biproduct.ι_mono (f : J → C) [HasBiproduct f] (b : J) : IsSplitMono (biproduct.ι f b) := by classical exact IsSplitMono.mk' { retraction := biproduct.desc <| Pi.single b (𝟙 (f b)) } instance biproduct.π_epi (f : J → C) [HasBiproduct f] (b : J) : IsSplitEpi (biproduct.π f b) := by classical exact IsSplitEpi.mk' { section_ := biproduct.lift <| Pi.single b (𝟙 (f b)) } /-- Auxiliary lemma for `biproduct.uniqueUpToIso`. -/ theorem biproduct.conePointUniqueUpToIso_hom (f : J → C) [HasBiproduct f] {b : Bicone f} (hb : b.IsBilimit) : (hb.isLimit.conePointUniqueUpToIso (biproduct.isLimit _)).hom = biproduct.lift b.π := rfl /-- Auxiliary lemma for `biproduct.uniqueUpToIso`. -/ theorem biproduct.conePointUniqueUpToIso_inv (f : J → C) [HasBiproduct f] {b : Bicone f} (hb : b.IsBilimit) : (hb.isLimit.conePointUniqueUpToIso (biproduct.isLimit _)).inv = biproduct.desc b.ι := by classical refine biproduct.hom_ext' _ _ fun j => hb.isLimit.hom_ext fun j' => ?_ rw [Category.assoc, IsLimit.conePointUniqueUpToIso_inv_comp, Bicone.toCone_π_app, biproduct.bicone_π, biproduct.ι_desc, biproduct.ι_π, b.toCone_π_app, b.ι_π] /-- Biproducts are unique up to isomorphism. This already follows because bilimits are limits, but in the case of biproducts we can give an isomorphism with particularly nice definitional properties, namely that `biproduct.lift b.π` and `biproduct.desc b.ι` are inverses of each other. -/ @[simps] def biproduct.uniqueUpToIso (f : J → C) [HasBiproduct f] {b : Bicone f} (hb : b.IsBilimit) : b.pt ≅ ⨁ f where hom := biproduct.lift b.π inv := biproduct.desc b.ι hom_inv_id := by rw [← biproduct.conePointUniqueUpToIso_hom f hb, ← biproduct.conePointUniqueUpToIso_inv f hb, Iso.hom_inv_id] inv_hom_id := by rw [← biproduct.conePointUniqueUpToIso_hom f hb, ← biproduct.conePointUniqueUpToIso_inv f hb, Iso.inv_hom_id] variable (C) -- see Note [lower instance priority] /-- A category with finite biproducts has a zero object. -/ instance (priority := 100) hasZeroObject_of_hasFiniteBiproducts [HasFiniteBiproducts C] : HasZeroObject C := by refine ⟨⟨biproduct Empty.elim, fun X => ⟨⟨⟨0⟩, ?_⟩⟩, fun X => ⟨⟨⟨0⟩, ?_⟩⟩⟩⟩ · intro a; apply biproduct.hom_ext'; simp · intro a; apply biproduct.hom_ext; simp section variable {C} attribute [local simp] eq_iff_true_of_subsingleton in /-- The limit bicone for the biproduct over an index type with exactly one term. -/ @[simps] def limitBiconeOfUnique [Unique J] (f : J → C) : LimitBicone f where bicone := { pt := f default π := fun j => eqToHom (by congr; rw [← Unique.uniq] ) ι := fun j => eqToHom (by congr; rw [← Unique.uniq] ) } isBilimit := { isLimit := (limitConeOfUnique f).isLimit isColimit := (colimitCoconeOfUnique f).isColimit } instance (priority := 100) hasBiproduct_unique [Subsingleton J] [Nonempty J] (f : J → C) : HasBiproduct f := let ⟨_⟩ := nonempty_unique J; .mk (limitBiconeOfUnique f) /-- A biproduct over an index type with exactly one term is just the object over that term. -/ @[simps!] def biproductUniqueIso [Unique J] (f : J → C) : ⨁ f ≅ f default := (biproduct.uniqueUpToIso _ (limitBiconeOfUnique f).isBilimit).symm end end CategoryTheory.Limits
Mathlib/CategoryTheory/Limits/Shapes/Biproducts.lean
1,825
1,828
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta, Joël Riou -/ import Mathlib.CategoryTheory.Comma.Presheaf.Basic import Mathlib.CategoryTheory.Elements import Mathlib.CategoryTheory.Functor.KanExtension.Adjunction import Mathlib.CategoryTheory.Limits.Final import Mathlib.CategoryTheory.Limits.Over /-! # Colimit of representables This file constructs an adjunction `Presheaf.yonedaAdjunction` between `(Cᵒᵖ ⥤ Type u)` and `ℰ` given a functor `A : C ⥤ ℰ`, where the right adjoint `restrictedYoneda` sends `(E : ℰ)` to `c ↦ (A.obj c ⟶ E)`, and the left adjoint `(Cᵒᵖ ⥤ Type v₁) ⥤ ℰ` is a pointwise left Kan extension of `A` along the Yoneda embedding, which exists provided `ℰ` has colimits) We also show that every presheaf is a colimit of representables. This result is also known as the density theorem, the co-Yoneda lemma and the Ninja Yoneda lemma. Two formulations are given: * `colimitOfRepresentable` uses the category of elements of a functor to types; * `isColimitTautologicalCocone` uses the category of costructured arrows. In the lemma `isLeftKanExtension_along_yoneda_iff`, we show that if `L : (Cᵒᵖ ⥤ Type v₁) ⥤ ℰ)` and `α : A ⟶ yoneda ⋙ L`, then `α` makes `L` the left Kan extension of `L` along yoneda if and only if `α` is an isomorphism (i.e. `L` extends `A`) and `L` preserves colimits. `uniqueExtensionAlongYoneda` shows `yoneda.leftKanExtension A` is unique amongst functors preserving colimits with this property, establishing the presheaf category as the free cocompletion of a category. Given a functor `F : C ⥤ D`, we also show construct an isomorphism `compYonedaIsoYonedaCompLan : F ⋙ yoneda ≅ yoneda ⋙ F.op.lan`, and show that it makes `F.op.lan` a left Kan extension of `F ⋙ yoneda`. ## Tags colimit, representable, presheaf, free cocompletion ## References * [S. MacLane, I. Moerdijk, *Sheaves in Geometry and Logic*][MM92] * https://ncatlab.org/nlab/show/Yoneda+extension -/ namespace CategoryTheory open Category Limits universe v₁ v₂ v₃ u₁ u₂ u₃ namespace Presheaf variable {C : Type u₁} [Category.{v₁} C] variable {ℰ : Type u₂} [Category.{v₁} ℰ] (A : C ⥤ ℰ) /-- The functor taking `(E : ℰ) (c : Cᵒᵖ)` to the homset `(A.obj C ⟶ E)`. It is shown in `L_adjunction` that this functor has a left adjoint (provided `E` has colimits) given by taking colimits over categories of elements. In the case where `ℰ = Cᵒᵖ ⥤ Type u` and `A = yoneda`, this functor is isomorphic to the identity. Defined as in [MM92], Chapter I, Section 5, Theorem 2. -/ @[simps!] def restrictedYoneda : ℰ ⥤ Cᵒᵖ ⥤ Type v₁ := yoneda ⋙ (whiskeringLeft _ _ (Type v₁)).obj (Functor.op A) /-- Auxiliary definition for `restrictedYonedaHomEquiv`. -/ def restrictedYonedaHomEquiv' (P : Cᵒᵖ ⥤ Type v₁) (E : ℰ) : (CostructuredArrow.proj yoneda P ⋙ A ⟶ (Functor.const (CostructuredArrow yoneda P)).obj E) ≃ (P ⟶ (restrictedYoneda A).obj E) where toFun f := { app := fun _ x => f.app (CostructuredArrow.mk (yonedaEquiv.symm x)) naturality := fun {X₁ X₂} φ => by ext x let ψ : CostructuredArrow.mk (yonedaEquiv.symm (P.toPrefunctor.map φ x)) ⟶ CostructuredArrow.mk (yonedaEquiv.symm x) := CostructuredArrow.homMk φ.unop (by dsimp [yonedaEquiv] aesop_cat ) simpa using (f.naturality ψ).symm } invFun g := { app := fun y => yonedaEquiv (y.hom ≫ g) naturality := fun {X₁ X₂} φ => by dsimp rw [← CostructuredArrow.w φ] dsimp [yonedaEquiv] simp only [comp_id, id_comp] refine (congr_fun (g.naturality φ.left.op) (X₂.hom.app (Opposite.op X₂.left) (𝟙 _))).symm.trans ?_ dsimp apply congr_arg simpa using congr_fun (X₂.hom.naturality φ.left.op).symm (𝟙 _) } left_inv f := by ext ⟨X, ⟨⟨⟩⟩, φ⟩ suffices yonedaEquiv.symm (φ.app (Opposite.op X) (𝟙 X)) = φ by dsimp erw [yonedaEquiv_apply] dsimp [CostructuredArrow.mk] erw [this] exact yonedaEquiv.injective (by aesop_cat) right_inv g := by ext X x dsimp erw [yonedaEquiv_apply] dsimp rw [yonedaEquiv_symm_app_apply] simp section example [HasColimitsOfSize.{v₁, max u₁ v₁} ℰ] : yoneda.HasPointwiseLeftKanExtension A := inferInstance variable [yoneda.HasPointwiseLeftKanExtension A] variable {A}
variable (L : (Cᵒᵖ ⥤ Type v₁) ⥤ ℰ) (α : A ⟶ yoneda ⋙ L) [L.IsLeftKanExtension α] /-- Auxiliary definition for `yonedaAdjunction`. -/ noncomputable def restrictedYonedaHomEquiv (P : Cᵒᵖ ⥤ Type v₁) (E : ℰ) : (L.obj P ⟶ E) ≃ (P ⟶ (restrictedYoneda A).obj E) := ((Functor.isPointwiseLeftKanExtensionOfIsLeftKanExtension _ α P).homEquiv E).trans
Mathlib/CategoryTheory/Limits/Presheaf.lean
121
126
/- Copyright (c) 2021 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne -/ import Mathlib.MeasureTheory.Function.AEEqOfIntegral import Mathlib.MeasureTheory.Function.ConditionalExpectation.AEMeasurable /-! # Uniqueness of the conditional expectation Two Lp functions `f, g` which are almost everywhere strongly measurable with respect to a σ-algebra `m` and verify `∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ` for all `m`-measurable sets `s` are equal almost everywhere. This proves the uniqueness of the conditional expectation, which is not yet defined in this file but is introduced in `Mathlib.MeasureTheory.Function.ConditionalExpectation.Basic`. ## Main statements * `Lp.ae_eq_of_forall_setIntegral_eq'`: two `Lp` functions verifying the equality of integrals defining the conditional expectation are equal. * `ae_eq_of_forall_setIntegral_eq_of_sigma_finite'`: two functions verifying the equality of integrals defining the conditional expectation are equal almost everywhere. Requires `[SigmaFinite (μ.trim hm)]`. -/ open scoped ENNReal MeasureTheory namespace MeasureTheory variable {α E' F' 𝕜 : Type*} {p : ℝ≥0∞} {m m0 : MeasurableSpace α} {μ : Measure α} [RCLike 𝕜] -- 𝕜 for ℝ or ℂ -- E' for an inner product space on which we compute integrals [NormedAddCommGroup E'] [InnerProductSpace 𝕜 E'] [CompleteSpace E'] [NormedSpace ℝ E'] -- F' for integrals on a Lp submodule [NormedAddCommGroup F'] [NormedSpace ℝ F'] [CompleteSpace F'] section UniquenessOfConditionalExpectation /-! ## Uniqueness of the conditional expectation -/ theorem lpMeas.ae_eq_zero_of_forall_setIntegral_eq_zero (hm : m ≤ m0) (f : lpMeas E' 𝕜 m p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (hf_int_finite : ∀ s, MeasurableSet[m] s → μ s < ∞ → IntegrableOn (f : Lp E' p μ) s μ) (hf_zero : ∀ s : Set α, MeasurableSet[m] s → μ s < ∞ → ∫ x in s, (f : Lp E' p μ) x ∂μ = 0) : f =ᵐ[μ] (0 : α → E') := by obtain ⟨g, hg_sm, hfg⟩ := lpMeas.ae_fin_strongly_measurable' hm f hp_ne_zero hp_ne_top refine hfg.trans ?_ refine ae_eq_zero_of_forall_setIntegral_eq_of_finStronglyMeasurable_trim hm ?_ ?_ hg_sm · intro s hs hμs have hfg_restrict : f =ᵐ[μ.restrict s] g := ae_restrict_of_ae hfg rw [IntegrableOn, integrable_congr hfg_restrict.symm] exact hf_int_finite s hs hμs · intro s hs hμs have hfg_restrict : f =ᵐ[μ.restrict s] g := ae_restrict_of_ae hfg rw [integral_congr_ae hfg_restrict.symm] exact hf_zero s hs hμs variable (𝕜) include 𝕜 in theorem Lp.ae_eq_zero_of_forall_setIntegral_eq_zero' (hm : m ≤ m0) (f : Lp E' p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (hf_int_finite : ∀ s, MeasurableSet[m] s → μ s < ∞ → IntegrableOn f s μ) (hf_zero : ∀ s : Set α, MeasurableSet[m] s → μ s < ∞ → ∫ x in s, f x ∂μ = 0) (hf_meas : AEStronglyMeasurable[m] f μ) : f =ᵐ[μ] 0 := by let f_meas : lpMeas E' 𝕜 m p μ := ⟨f, hf_meas⟩ have hf_f_meas : f =ᵐ[μ] f_meas := by simp [f_meas, Subtype.coe_mk] refine hf_f_meas.trans ?_ refine lpMeas.ae_eq_zero_of_forall_setIntegral_eq_zero hm f_meas hp_ne_zero hp_ne_top ?_ ?_ · intro s hs hμs have hfg_restrict : f =ᵐ[μ.restrict s] f_meas := ae_restrict_of_ae hf_f_meas rw [IntegrableOn, integrable_congr hfg_restrict.symm] exact hf_int_finite s hs hμs · intro s hs hμs have hfg_restrict : f =ᵐ[μ.restrict s] f_meas := ae_restrict_of_ae hf_f_meas rw [integral_congr_ae hfg_restrict.symm] exact hf_zero s hs hμs include 𝕜 in /-- **Uniqueness of the conditional expectation** -/ theorem Lp.ae_eq_of_forall_setIntegral_eq' (hm : m ≤ m0) (f g : Lp E' p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (hf_int_finite : ∀ s, MeasurableSet[m] s → μ s < ∞ → IntegrableOn f s μ) (hg_int_finite : ∀ s, MeasurableSet[m] s → μ s < ∞ → IntegrableOn g s μ) (hfg : ∀ s : Set α, MeasurableSet[m] s → μ s < ∞ → ∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ) (hf_meas : AEStronglyMeasurable[m] f μ) (hg_meas : AEStronglyMeasurable[m] g μ) : f =ᵐ[μ] g := by suffices h_sub : ⇑(f - g) =ᵐ[μ] 0 by rw [← sub_ae_eq_zero]; exact (Lp.coeFn_sub f g).symm.trans h_sub have hfg' : ∀ s : Set α, MeasurableSet[m] s → μ s < ∞ → (∫ x in s, (f - g) x ∂μ) = 0 := by intro s hs hμs rw [integral_congr_ae (ae_restrict_of_ae (Lp.coeFn_sub f g))] rw [integral_sub' (hf_int_finite s hs hμs) (hg_int_finite s hs hμs)] exact sub_eq_zero.mpr (hfg s hs hμs) have hfg_int : ∀ s, MeasurableSet[m] s → μ s < ∞ → IntegrableOn (⇑(f - g)) s μ := by intro s hs hμs rw [IntegrableOn, integrable_congr (ae_restrict_of_ae (Lp.coeFn_sub f g))] exact (hf_int_finite s hs hμs).sub (hg_int_finite s hs hμs) exact Lp.ae_eq_zero_of_forall_setIntegral_eq_zero' 𝕜 hm (f - g) hp_ne_zero hp_ne_top hfg_int hfg' <| (hf_meas.sub hg_meas).congr (Lp.coeFn_sub f g).symm variable {𝕜} theorem ae_eq_of_forall_setIntegral_eq_of_sigmaFinite' (hm : m ≤ m0) [SigmaFinite (μ.trim hm)] {f g : α → F'} (hf_int_finite : ∀ s, MeasurableSet[m] s → μ s < ∞ → IntegrableOn f s μ) (hg_int_finite : ∀ s, MeasurableSet[m] s → μ s < ∞ → IntegrableOn g s μ) (hfg_eq : ∀ s : Set α, MeasurableSet[m] s → μ s < ∞ → ∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ) (hfm : AEStronglyMeasurable[m] f μ) (hgm : AEStronglyMeasurable[m] g μ) : f =ᵐ[μ] g := by rw [← ae_eq_trim_iff_of_aestronglyMeasurable hm hfm hgm] have hf_mk_int_finite (s) : MeasurableSet[m] s → μ.trim hm s < ∞ → @IntegrableOn _ _ m _ _ (hfm.mk f) s (μ.trim hm) := by intro hs hμs rw [trim_measurableSet_eq hm hs] at hμs -- Porting note: `rw [IntegrableOn]` fails with -- synthesized type class instance is not definitionally equal to expression inferred by typing -- rules, synthesized m0 inferred m unfold IntegrableOn rw [restrict_trim hm _ hs] refine Integrable.trim hm ?_ hfm.stronglyMeasurable_mk exact Integrable.congr (hf_int_finite s hs hμs) (ae_restrict_of_ae hfm.ae_eq_mk) have hg_mk_int_finite (s) : MeasurableSet[m] s → μ.trim hm s < ∞ → @IntegrableOn _ _ m _ _ (hgm.mk g) s (μ.trim hm) := by intro hs hμs rw [trim_measurableSet_eq hm hs] at hμs -- Porting note: `rw [IntegrableOn]` fails with
-- synthesized type class instance is not definitionally equal to expression inferred by typing -- rules, synthesized m0 inferred m unfold IntegrableOn rw [restrict_trim hm _ hs] refine Integrable.trim hm ?_ hgm.stronglyMeasurable_mk exact Integrable.congr (hg_int_finite s hs hμs) (ae_restrict_of_ae hgm.ae_eq_mk) have hfg_mk_eq : ∀ s : Set α, MeasurableSet[m] s → μ.trim hm s < ∞ → ∫ x in s, hfm.mk f x ∂μ.trim hm = ∫ x in s, hgm.mk g x ∂μ.trim hm := by intro s hs hμs rw [trim_measurableSet_eq hm hs] at hμs rw [restrict_trim hm _ hs, ← integral_trim hm hfm.stronglyMeasurable_mk, ← integral_trim hm hgm.stronglyMeasurable_mk, integral_congr_ae (ae_restrict_of_ae hfm.ae_eq_mk.symm), integral_congr_ae (ae_restrict_of_ae hgm.ae_eq_mk.symm)] exact hfg_eq s hs hμs exact ae_eq_of_forall_setIntegral_eq_of_sigmaFinite hf_mk_int_finite hg_mk_int_finite hfg_mk_eq end UniquenessOfConditionalExpectation section IntegralNormLE variable {s : Set α} /-- Let `m` be a sub-σ-algebra of `m0`, `f` an `m0`-measurable function and `g` an `m`-measurable function, such that their integrals coincide on `m`-measurable sets with finite measure. Then `∫ x in s, ‖g x‖ ∂μ ≤ ∫ x in s, ‖f x‖ ∂μ` on all `m`-measurable sets with finite measure. -/ theorem integral_norm_le_of_forall_fin_meas_integral_eq (hm : m ≤ m0) {f g : α → ℝ} (hf : StronglyMeasurable f) (hfi : IntegrableOn f s μ) (hg : StronglyMeasurable[m] g) (hgi : IntegrableOn g s μ) (hgf : ∀ t, MeasurableSet[m] t → μ t < ∞ → ∫ x in t, g x ∂μ = ∫ x in t, f x ∂μ) (hs : MeasurableSet[m] s) (hμs : μ s ≠ ∞) : (∫ x in s, ‖g x‖ ∂μ) ≤ ∫ x in s, ‖f x‖ ∂μ := by rw [integral_norm_eq_pos_sub_neg hgi, integral_norm_eq_pos_sub_neg hfi] have h_meas_nonneg_g : MeasurableSet[m] {x | 0 ≤ g x} := (@stronglyMeasurable_const _ _ m _ _).measurableSet_le hg have h_meas_nonneg_f : MeasurableSet {x | 0 ≤ f x} := stronglyMeasurable_const.measurableSet_le hf have h_meas_nonpos_g : MeasurableSet[m] {x | g x ≤ 0} := hg.measurableSet_le (@stronglyMeasurable_const _ _ m _ _)
Mathlib/MeasureTheory/Function/ConditionalExpectation/Unique.lean
130
169
/- Copyright (c) 2022 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa -/ import Mathlib.Algebra.Polynomial.AlgebraMap import Mathlib.Algebra.Polynomial.Reverse import Mathlib.Algebra.Polynomial.Inductions import Mathlib.RingTheory.Localization.Away.Basic /-! # Laurent polynomials We introduce Laurent polynomials over a semiring `R`. Mathematically, they are expressions of the form $$ \sum_{i \in \mathbb{Z}} a_i T ^ i $$ where the sum extends over a finite subset of `ℤ`. Thus, negative exponents are allowed. The coefficients come from the semiring `R` and the variable `T` commutes with everything. Since we are going to convert back and forth between polynomials and Laurent polynomials, we decided to maintain some distinction by using the symbol `T`, rather than `X`, as the variable for Laurent polynomials. ## Notation The symbol `R[T;T⁻¹]` stands for `LaurentPolynomial R`. We also define * `C : R →+* R[T;T⁻¹]` the inclusion of constant polynomials, analogous to the one for `R[X]`; * `T : ℤ → R[T;T⁻¹]` the sequence of powers of the variable `T`. ## Implementation notes We define Laurent polynomials as `AddMonoidAlgebra R ℤ`. Thus, they are essentially `Finsupp`s `ℤ →₀ R`. This choice differs from the current irreducible design of `Polynomial`, that instead shields away the implementation via `Finsupp`s. It is closer to the original definition of polynomials. As a consequence, `LaurentPolynomial` plays well with polynomials, but there is a little roughness in establishing the API, since the `Finsupp` implementation of `R[X]` is well-shielded. Unlike the case of polynomials, I felt that the exponent notation was not too easy to use, as only natural exponents would be allowed. Moreover, in the end, it seems likely that we should aim to perform computations on exponents in `ℤ` anyway and separating this via the symbol `T` seems convenient. I made a *heavy* use of `simp` lemmas, aiming to bring Laurent polynomials to the form `C a * T n`. Any comments or suggestions for improvements is greatly appreciated! ## Future work Lots is missing! -- (Riccardo) add inclusion into Laurent series. -- A "better" definition of `trunc` would be as an `R`-linear map. This works: -- ``` -- def trunc : R[T;T⁻¹] →[R] R[X] := -- refine (?_ : R[ℕ] →[R] R[X]).comp ?_ -- · exact ⟨(toFinsuppIso R).symm, by simp⟩ -- · refine ⟨fun r ↦ comapDomain _ r -- (Set.injOn_of_injective (fun _ _ ↦ Int.ofNat.inj) _), ?_⟩ -- exact fun r f ↦ comapDomain_smul .. -- ``` -- but it would make sense to bundle the maps better, for a smoother user experience. -- I (DT) did not have the strength to embark on this (possibly short!) journey, after getting to -- this stage of the Laurent process! -- This would likely involve adding a `comapDomain` analogue of -- `AddMonoidAlgebra.mapDomainAlgHom` and an `R`-linear version of -- `Polynomial.toFinsuppIso`. -- Add `degree, intDegree, intTrailingDegree, leadingCoeff, trailingCoeff,...`. -/ open Polynomial Function AddMonoidAlgebra Finsupp noncomputable section variable {R S : Type*} /-- The semiring of Laurent polynomials with coefficients in the semiring `R`. We denote it by `R[T;T⁻¹]`. The ring homomorphism `C : R →+* R[T;T⁻¹]` includes `R` as the constant polynomials. -/ abbrev LaurentPolynomial (R : Type*) [Semiring R] := AddMonoidAlgebra R ℤ @[nolint docBlame] scoped[LaurentPolynomial] notation:9000 R "[T;T⁻¹]" => LaurentPolynomial R open LaurentPolynomial @[ext] theorem LaurentPolynomial.ext [Semiring R] {p q : R[T;T⁻¹]} (h : ∀ a, p a = q a) : p = q := Finsupp.ext h /-- The ring homomorphism, taking a polynomial with coefficients in `R` to a Laurent polynomial with coefficients in `R`. -/ def Polynomial.toLaurent [Semiring R] : R[X] →+* R[T;T⁻¹] := (mapDomainRingHom R Int.ofNatHom).comp (toFinsuppIso R) /-- This is not a simp lemma, as it is usually preferable to use the lemmas about `C` and `X` instead. -/ theorem Polynomial.toLaurent_apply [Semiring R] (p : R[X]) : toLaurent p = p.toFinsupp.mapDomain (↑) := rfl /-- The `R`-algebra map, taking a polynomial with coefficients in `R` to a Laurent polynomial with coefficients in `R`. -/ def Polynomial.toLaurentAlg [CommSemiring R] : R[X] →ₐ[R] R[T;T⁻¹] := (mapDomainAlgHom R R Int.ofNatHom).comp (toFinsuppIsoAlg R).toAlgHom @[simp] lemma Polynomial.coe_toLaurentAlg [CommSemiring R] : (toLaurentAlg : R[X] → R[T;T⁻¹]) = toLaurent := rfl theorem Polynomial.toLaurentAlg_apply [CommSemiring R] (f : R[X]) : toLaurentAlg f = toLaurent f := rfl namespace LaurentPolynomial section Semiring variable [Semiring R] theorem single_zero_one_eq_one : (Finsupp.single 0 1 : R[T;T⁻¹]) = (1 : R[T;T⁻¹]) := rfl /-! ### The functions `C` and `T`. -/ /-- The ring homomorphism `C`, including `R` into the ring of Laurent polynomials over `R` as the constant Laurent polynomials. -/ def C : R →+* R[T;T⁻¹] := singleZeroRingHom theorem algebraMap_apply {R A : Type*} [CommSemiring R] [Semiring A] [Algebra R A] (r : R) : algebraMap R (LaurentPolynomial A) r = C (algebraMap R A r) := rfl /-- When we have `[CommSemiring R]`, the function `C` is the same as `algebraMap R R[T;T⁻¹]`. (But note that `C` is defined when `R` is not necessarily commutative, in which case `algebraMap` is not available.) -/ theorem C_eq_algebraMap {R : Type*} [CommSemiring R] (r : R) : C r = algebraMap R R[T;T⁻¹] r := rfl theorem single_eq_C (r : R) : Finsupp.single 0 r = C r := rfl @[simp] lemma C_apply (t : R) (n : ℤ) : C t n = if n = 0 then t else 0 := by rw [← single_eq_C, Finsupp.single_apply]; aesop /-- The function `n ↦ T ^ n`, implemented as a sequence `ℤ → R[T;T⁻¹]`. Using directly `T ^ n` does not work, since we want the exponents to be of Type `ℤ` and there is no `ℤ`-power defined on `R[T;T⁻¹]`. Using that `T` is a unit introduces extra coercions. For these reasons, the definition of `T` is as a sequence. -/ def T (n : ℤ) : R[T;T⁻¹] := Finsupp.single n 1 @[simp] lemma T_apply (m n : ℤ) : (T n : R[T;T⁻¹]) m = if n = m then 1 else 0 := Finsupp.single_apply @[simp] theorem T_zero : (T 0 : R[T;T⁻¹]) = 1 := rfl theorem T_add (m n : ℤ) : (T (m + n) : R[T;T⁻¹]) = T m * T n := by simp [T, single_mul_single] theorem T_sub (m n : ℤ) : (T (m - n) : R[T;T⁻¹]) = T m * T (-n) := by rw [← T_add, sub_eq_add_neg] @[simp] theorem T_pow (m : ℤ) (n : ℕ) : (T m ^ n : R[T;T⁻¹]) = T (n * m) := by rw [T, T, single_pow n, one_pow, nsmul_eq_mul] /-- The `simp` version of `mul_assoc`, in the presence of `T`'s. -/ @[simp] theorem mul_T_assoc (f : R[T;T⁻¹]) (m n : ℤ) : f * T m * T n = f * T (m + n) := by simp [← T_add, mul_assoc] @[simp] theorem single_eq_C_mul_T (r : R) (n : ℤ) : (Finsupp.single n r : R[T;T⁻¹]) = (C r * T n : R[T;T⁻¹]) := by simp [C, T, single_mul_single] -- This lemma locks in the right changes and is what Lean proved directly. -- The actual `simp`-normal form of a Laurent monomial is `C a * T n`, whenever it can be reached. @[simp] theorem _root_.Polynomial.toLaurent_C_mul_T (n : ℕ) (r : R) : (toLaurent (Polynomial.monomial n r) : R[T;T⁻¹]) = C r * T n := show Finsupp.mapDomain (↑) (monomial n r).toFinsupp = (C r * T n : R[T;T⁻¹]) by rw [toFinsupp_monomial, Finsupp.mapDomain_single, single_eq_C_mul_T] @[simp] theorem _root_.Polynomial.toLaurent_C (r : R) : toLaurent (Polynomial.C r) = C r := by
convert Polynomial.toLaurent_C_mul_T 0 r
Mathlib/Algebra/Polynomial/Laurent.lean
191
191
/- Copyright (c) 2019 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Kenny Lau -/ import Mathlib.Algebra.Order.Antidiag.Finsupp import Mathlib.Data.Finsupp.Weight import Mathlib.Tactic.Linarith import Mathlib.LinearAlgebra.Pi import Mathlib.Algebra.MvPolynomial.Eval /-! # Formal (multivariate) power series This file defines multivariate formal power series and develops the basic properties of these objects. A formal power series is to a polynomial like an infinite sum is to a finite sum. We provide the natural inclusion from multivariate polynomials to multivariate formal power series. ## Main definitions - `MvPowerSeries.C`: constant power series - `MvPowerSeries.X`: the indeterminates - `MvPowerSeries.coeff`, `MvPowerSeries.constantCoeff`: the coefficients of a `MvPowerSeries`, its constant coefficient - `MvPowerSeries.monomial`: the monomials - `MvPowerSeries.coeff_mul`: computes the coefficients of the product of two `MvPowerSeries` - `MvPowerSeries.coeff_prod` : computes the coefficients of products of `MvPowerSeries` - `MvPowerSeries.coeff_pow` : computes the coefficients of powers of a `MvPowerSeries` - `MvPowerSeries.coeff_eq_zero_of_constantCoeff_nilpotent`: if the constant coefficient of a `MvPowerSeries` is nilpotent, then some coefficients of its powers are automatically zero - `MvPowerSeries.map`: apply a `RingHom` to the coefficients of a `MvPowerSeries` (as a `RingHom) - `MvPowerSeries.X_pow_dvd_iff`, `MvPowerSeries.X_dvd_iff`: equivalent conditions for (a power of) an indeterminate to divide a `MvPowerSeries` - `MvPolynomial.toMvPowerSeries`: the canonical coercion from `MvPolynomial` to `MvPowerSeries` ## Note This file sets up the (semi)ring structure on multivariate power series: additional results are in: * `Mathlib.RingTheory.MvPowerSeries.Inverse` : invertibility, formal power series over a local ring form a local ring; * `Mathlib.RingTheory.MvPowerSeries.Trunc`: truncation of power series. In `Mathlib.RingTheory.PowerSeries.Basic`, formal power series in one variable will be obtained as a particular case, defined by `PowerSeries R := MvPowerSeries Unit R`. See that file for a specific description. ## Implementation notes In this file we define multivariate formal power series with variables indexed by `σ` and coefficients in `R` as `MvPowerSeries σ R := (σ →₀ ℕ) → R`. Unfortunately there is not yet enough API to show that they are the completion of the ring of multivariate polynomials. However, we provide most of the infrastructure that is needed to do this. Once I-adic completion (topological or algebraic) is available it should not be hard to fill in the details. -/ noncomputable section open Finset (antidiagonal mem_antidiagonal) /-- Multivariate formal power series, where `σ` is the index set of the variables and `R` is the coefficient ring. -/ def MvPowerSeries (σ : Type*) (R : Type*) := (σ →₀ ℕ) → R namespace MvPowerSeries open Finsupp variable {σ R : Type*} instance [Inhabited R] : Inhabited (MvPowerSeries σ R) := ⟨fun _ => default⟩ instance [Zero R] : Zero (MvPowerSeries σ R) := Pi.instZero instance [AddMonoid R] : AddMonoid (MvPowerSeries σ R) := Pi.addMonoid instance [AddGroup R] : AddGroup (MvPowerSeries σ R) := Pi.addGroup instance [AddCommMonoid R] : AddCommMonoid (MvPowerSeries σ R) := Pi.addCommMonoid instance [AddCommGroup R] : AddCommGroup (MvPowerSeries σ R) := Pi.addCommGroup instance [Nontrivial R] : Nontrivial (MvPowerSeries σ R) := Function.nontrivial instance {A} [Semiring R] [AddCommMonoid A] [Module R A] : Module R (MvPowerSeries σ A) := Pi.module _ _ _ instance {A S} [Semiring R] [Semiring S] [AddCommMonoid A] [Module R A] [Module S A] [SMul R S] [IsScalarTower R S A] : IsScalarTower R S (MvPowerSeries σ A) := Pi.isScalarTower section Semiring variable (R) [Semiring R] /-- The `n`th monomial as multivariate formal power series: it is defined as the `R`-linear map from `R` to the semi-ring of multivariate formal power series associating to each `a` the map sending `n : σ →₀ ℕ` to the value `a` and sending all other `x : σ →₀ ℕ` different from `n` to `0`. -/ def monomial (n : σ →₀ ℕ) : R →ₗ[R] MvPowerSeries σ R := letI := Classical.decEq σ LinearMap.single R (fun _ ↦ R) n /-- The `n`th coefficient of a multivariate formal power series. -/ def coeff (n : σ →₀ ℕ) : MvPowerSeries σ R →ₗ[R] R := LinearMap.proj n theorem coeff_apply (f : MvPowerSeries σ R) (d : σ →₀ ℕ) : coeff R d f = f d := rfl variable {R} /-- Two multivariate formal power series are equal if all their coefficients are equal. -/ @[ext] theorem ext {φ ψ} (h : ∀ n : σ →₀ ℕ, coeff R n φ = coeff R n ψ) : φ = ψ := funext h /-- Two multivariate formal power series are equal if and only if all their coefficients are equal. -/ add_decl_doc MvPowerSeries.ext_iff theorem monomial_def [DecidableEq σ] (n : σ →₀ ℕ) : (monomial R n) = LinearMap.single R (fun _ ↦ R) n := by rw [monomial] -- unify the `Decidable` arguments convert rfl theorem coeff_monomial [DecidableEq σ] (m n : σ →₀ ℕ) (a : R) : coeff R m (monomial R n a) = if m = n then a else 0 := by dsimp only [coeff, MvPowerSeries] rw [monomial_def, LinearMap.proj_apply (i := m), LinearMap.single_apply, Pi.single_apply] @[simp] theorem coeff_monomial_same (n : σ →₀ ℕ) (a : R) : coeff R n (monomial R n a) = a := by classical rw [monomial_def] exact Pi.single_eq_same _ _ theorem coeff_monomial_ne {m n : σ →₀ ℕ} (h : m ≠ n) (a : R) : coeff R m (monomial R n a) = 0 := by classical rw [monomial_def] exact Pi.single_eq_of_ne h _ theorem eq_of_coeff_monomial_ne_zero {m n : σ →₀ ℕ} {a : R} (h : coeff R m (monomial R n a) ≠ 0) : m = n := by_contra fun h' => h <| coeff_monomial_ne h' a @[simp] theorem coeff_comp_monomial (n : σ →₀ ℕ) : (coeff R n).comp (monomial R n) = LinearMap.id := LinearMap.ext <| coeff_monomial_same n @[simp] theorem coeff_zero (n : σ →₀ ℕ) : coeff R n (0 : MvPowerSeries σ R) = 0 := rfl theorem eq_zero_iff_forall_coeff_zero {f : MvPowerSeries σ R} : f = 0 ↔ (∀ d : σ →₀ ℕ, coeff R d f = 0) := MvPowerSeries.ext_iff theorem ne_zero_iff_exists_coeff_ne_zero (f : MvPowerSeries σ R) : f ≠ 0 ↔ (∃ d : σ →₀ ℕ, coeff R d f ≠ 0) := by simp only [MvPowerSeries.ext_iff, ne_eq, coeff_zero, not_forall] variable (m n : σ →₀ ℕ) (φ ψ : MvPowerSeries σ R) instance : One (MvPowerSeries σ R) := ⟨monomial R (0 : σ →₀ ℕ) 1⟩ theorem coeff_one [DecidableEq σ] : coeff R n (1 : MvPowerSeries σ R) = if n = 0 then 1 else 0 := coeff_monomial _ _ _ theorem coeff_zero_one : coeff R (0 : σ →₀ ℕ) 1 = 1 := coeff_monomial_same 0 1 theorem monomial_zero_one : monomial R (0 : σ →₀ ℕ) 1 = 1 := rfl instance : AddMonoidWithOne (MvPowerSeries σ R) := { show AddMonoid (MvPowerSeries σ R) by infer_instance with natCast := fun n => monomial R 0 n natCast_zero := by simp [Nat.cast] natCast_succ := by simp [Nat.cast, monomial_zero_one] one := 1 } instance : Mul (MvPowerSeries σ R) := letI := Classical.decEq σ ⟨fun φ ψ n => ∑ p ∈ antidiagonal n, coeff R p.1 φ * coeff R p.2 ψ⟩ theorem coeff_mul [DecidableEq σ] : coeff R n (φ * ψ) = ∑ p ∈ antidiagonal n, coeff R p.1 φ * coeff R p.2 ψ := by refine Finset.sum_congr ?_ fun _ _ => rfl rw [Subsingleton.elim (Classical.decEq σ) ‹DecidableEq σ›] protected theorem zero_mul : (0 : MvPowerSeries σ R) * φ = 0 := ext fun n => by classical simp [coeff_mul] protected theorem mul_zero : φ * 0 = 0 := ext fun n => by classical simp [coeff_mul] theorem coeff_monomial_mul (a : R) : coeff R m (monomial R n a * φ) = if n ≤ m then a * coeff R (m - n) φ else 0 := by classical have : ∀ p ∈ antidiagonal m, coeff R (p : (σ →₀ ℕ) × (σ →₀ ℕ)).1 (monomial R n a) * coeff R p.2 φ ≠ 0 → p.1 = n := fun p _ hp => eq_of_coeff_monomial_ne_zero (left_ne_zero_of_mul hp) rw [coeff_mul, ← Finset.sum_filter_of_ne this, Finset.filter_fst_eq_antidiagonal _ n, Finset.sum_ite_index] simp only [Finset.sum_singleton, coeff_monomial_same, Finset.sum_empty] theorem coeff_mul_monomial (a : R) : coeff R m (φ * monomial R n a) = if n ≤ m then coeff R (m - n) φ * a else 0 := by classical have : ∀ p ∈ antidiagonal m, coeff R (p : (σ →₀ ℕ) × (σ →₀ ℕ)).1 φ * coeff R p.2 (monomial R n a) ≠ 0 → p.2 = n := fun p _ hp => eq_of_coeff_monomial_ne_zero (right_ne_zero_of_mul hp) rw [coeff_mul, ← Finset.sum_filter_of_ne this, Finset.filter_snd_eq_antidiagonal _ n, Finset.sum_ite_index] simp only [Finset.sum_singleton, coeff_monomial_same, Finset.sum_empty] theorem coeff_add_monomial_mul (a : R) : coeff R (m + n) (monomial R m a * φ) = a * coeff R n φ := by rw [coeff_monomial_mul, if_pos, add_tsub_cancel_left] exact le_add_right le_rfl theorem coeff_add_mul_monomial (a : R) : coeff R (m + n) (φ * monomial R n a) = coeff R m φ * a := by rw [coeff_mul_monomial, if_pos, add_tsub_cancel_right] exact le_add_left le_rfl @[simp] theorem commute_monomial {a : R} {n} : Commute φ (monomial R n a) ↔ ∀ m, Commute (coeff R m φ) a := by rw [commute_iff_eq, MvPowerSeries.ext_iff] refine ⟨fun h m => ?_, fun h m => ?_⟩ · have := h (m + n) rwa [coeff_add_mul_monomial, add_comm, coeff_add_monomial_mul] at this · rw [coeff_mul_monomial, coeff_monomial_mul] split_ifs <;> [apply h; rfl] protected theorem one_mul : (1 : MvPowerSeries σ R) * φ = φ := ext fun n => by simpa using coeff_add_monomial_mul 0 n φ 1 protected theorem mul_one : φ * 1 = φ := ext fun n => by simpa using coeff_add_mul_monomial n 0 φ 1 protected theorem mul_add (φ₁ φ₂ φ₃ : MvPowerSeries σ R) : φ₁ * (φ₂ + φ₃) = φ₁ * φ₂ + φ₁ * φ₃ := ext fun n => by classical simp only [coeff_mul, mul_add, Finset.sum_add_distrib, LinearMap.map_add] protected theorem add_mul (φ₁ φ₂ φ₃ : MvPowerSeries σ R) : (φ₁ + φ₂) * φ₃ = φ₁ * φ₃ + φ₂ * φ₃ := ext fun n => by classical simp only [coeff_mul, add_mul, Finset.sum_add_distrib, LinearMap.map_add] protected theorem mul_assoc (φ₁ φ₂ φ₃ : MvPowerSeries σ R) : φ₁ * φ₂ * φ₃ = φ₁ * (φ₂ * φ₃) := by ext1 n classical simp only [coeff_mul, Finset.sum_mul, Finset.mul_sum, Finset.sum_sigma'] apply Finset.sum_nbij' (fun ⟨⟨_i, j⟩, ⟨k, l⟩⟩ ↦ ⟨(k, l + j), (l, j)⟩) (fun ⟨⟨i, _j⟩, ⟨k, l⟩⟩ ↦ ⟨(i + k, l), (i, k)⟩) <;> aesop (add simp [add_assoc, mul_assoc]) instance : Semiring (MvPowerSeries σ R) := { inferInstanceAs (AddMonoidWithOne (MvPowerSeries σ R)), inferInstanceAs (Mul (MvPowerSeries σ R)), inferInstanceAs (AddCommMonoid (MvPowerSeries σ R)) with mul_one := MvPowerSeries.mul_one one_mul := MvPowerSeries.one_mul mul_assoc := MvPowerSeries.mul_assoc mul_zero := MvPowerSeries.mul_zero zero_mul := MvPowerSeries.zero_mul left_distrib := MvPowerSeries.mul_add right_distrib := MvPowerSeries.add_mul } end Semiring instance [CommSemiring R] : CommSemiring (MvPowerSeries σ R) := { show Semiring (MvPowerSeries σ R) by infer_instance with mul_comm := fun φ ψ => ext fun n => by classical simpa only [coeff_mul, mul_comm] using sum_antidiagonal_swap n fun a b => coeff R a φ * coeff R b ψ } instance [Ring R] : Ring (MvPowerSeries σ R) := { inferInstanceAs (Semiring (MvPowerSeries σ R)), inferInstanceAs (AddCommGroup (MvPowerSeries σ R)) with } instance [CommRing R] : CommRing (MvPowerSeries σ R) := { inferInstanceAs (CommSemiring (MvPowerSeries σ R)), inferInstanceAs (AddCommGroup (MvPowerSeries σ R)) with } section Semiring variable [Semiring R] theorem monomial_mul_monomial (m n : σ →₀ ℕ) (a b : R) : monomial R m a * monomial R n b = monomial R (m + n) (a * b) := by classical ext k simp only [coeff_mul_monomial, coeff_monomial] split_ifs with h₁ h₂ h₃ h₃ h₂ <;> try rfl · rw [← h₂, tsub_add_cancel_of_le h₁] at h₃ exact (h₃ rfl).elim · rw [h₃, add_tsub_cancel_right] at h₂ exact (h₂ rfl).elim · exact zero_mul b · rw [h₂] at h₁ exact (h₁ <| le_add_left le_rfl).elim variable (σ) (R) /-- The constant multivariate formal power series. -/ def C : R →+* MvPowerSeries σ R := { monomial R (0 : σ →₀ ℕ) with map_one' := rfl map_mul' := fun a b => (monomial_mul_monomial 0 0 a b).symm map_zero' := (monomial R 0).map_zero } variable {σ} {R} @[simp] theorem monomial_zero_eq_C : ⇑(monomial R (0 : σ →₀ ℕ)) = C σ R := rfl theorem monomial_zero_eq_C_apply (a : R) : monomial R (0 : σ →₀ ℕ) a = C σ R a := rfl theorem coeff_C [DecidableEq σ] (n : σ →₀ ℕ) (a : R) : coeff R n (C σ R a) = if n = 0 then a else 0 := coeff_monomial _ _ _ theorem coeff_zero_C (a : R) : coeff R (0 : σ →₀ ℕ) (C σ R a) = a := coeff_monomial_same 0 a /-- The variables of the multivariate formal power series ring. -/ def X (s : σ) : MvPowerSeries σ R := monomial R (single s 1) 1 theorem coeff_X [DecidableEq σ] (n : σ →₀ ℕ) (s : σ) : coeff R n (X s : MvPowerSeries σ R) = if n = single s 1 then 1 else 0 := coeff_monomial _ _ _ theorem coeff_index_single_X [DecidableEq σ] (s t : σ) : coeff R (single t 1) (X s : MvPowerSeries σ R) = if t = s then 1 else 0 := by simp only [coeff_X, single_left_inj (one_ne_zero : (1 : ℕ) ≠ 0)] @[simp] theorem coeff_index_single_self_X (s : σ) : coeff R (single s 1) (X s : MvPowerSeries σ R) = 1 := coeff_monomial_same _ _ theorem coeff_zero_X (s : σ) : coeff R (0 : σ →₀ ℕ) (X s : MvPowerSeries σ R) = 0 := by classical rw [coeff_X, if_neg] intro h exact one_ne_zero (single_eq_zero.mp h.symm) theorem commute_X (φ : MvPowerSeries σ R) (s : σ) : Commute φ (X s) := φ.commute_monomial.mpr fun _m => Commute.one_right _ theorem X_mul {φ : MvPowerSeries σ R} {s : σ} : X s * φ = φ * X s := φ.commute_X s |>.symm.eq theorem commute_X_pow (φ : MvPowerSeries σ R) (s : σ) (n : ℕ) : Commute φ (X s ^ n) := φ.commute_X s |>.pow_right _ theorem X_pow_mul {φ : MvPowerSeries σ R} {s : σ} {n : ℕ} : X s ^ n * φ = φ * X s ^ n := φ.commute_X_pow s n |>.symm.eq theorem X_def (s : σ) : X s = monomial R (single s 1) 1 := rfl theorem X_pow_eq (s : σ) (n : ℕ) : (X s : MvPowerSeries σ R) ^ n = monomial R (single s n) 1 := by induction n with | zero => simp | succ n ih => rw [pow_succ, ih, Finsupp.single_add, X, monomial_mul_monomial, one_mul] theorem coeff_X_pow [DecidableEq σ] (m : σ →₀ ℕ) (s : σ) (n : ℕ) : coeff R m ((X s : MvPowerSeries σ R) ^ n) = if m = single s n then 1 else 0 := by rw [X_pow_eq s n, coeff_monomial] @[simp] theorem coeff_mul_C (n : σ →₀ ℕ) (φ : MvPowerSeries σ R) (a : R) : coeff R n (φ * C σ R a) = coeff R n φ * a := by simpa using coeff_add_mul_monomial n 0 φ a @[simp] theorem coeff_C_mul (n : σ →₀ ℕ) (φ : MvPowerSeries σ R) (a : R) : coeff R n (C σ R a * φ) = a * coeff R n φ := by simpa using coeff_add_monomial_mul 0 n φ a theorem coeff_zero_mul_X (φ : MvPowerSeries σ R) (s : σ) : coeff R (0 : σ →₀ ℕ) (φ * X s) = 0 := by have : ¬single s 1 ≤ 0 := fun h => by simpa using h s simp only [X, coeff_mul_monomial, if_neg this] theorem coeff_zero_X_mul (φ : MvPowerSeries σ R) (s : σ) : coeff R (0 : σ →₀ ℕ) (X s * φ) = 0 := by rw [← (φ.commute_X s).eq, coeff_zero_mul_X] variable (σ) (R) /-- The constant coefficient of a formal power series. -/ def constantCoeff : MvPowerSeries σ R →+* R := { coeff R (0 : σ →₀ ℕ) with toFun := coeff R (0 : σ →₀ ℕ) map_one' := coeff_zero_one map_mul' := fun φ ψ => by classical simp [coeff_mul, support_single_ne_zero] map_zero' := LinearMap.map_zero _ } variable {σ} {R} @[simp] theorem coeff_zero_eq_constantCoeff : ⇑(coeff R (0 : σ →₀ ℕ)) = constantCoeff σ R := rfl theorem coeff_zero_eq_constantCoeff_apply (φ : MvPowerSeries σ R) : coeff R (0 : σ →₀ ℕ) φ = constantCoeff σ R φ := rfl @[simp] theorem constantCoeff_C (a : R) : constantCoeff σ R (C σ R a) = a := rfl @[simp] theorem constantCoeff_comp_C : (constantCoeff σ R).comp (C σ R) = RingHom.id R := rfl @[simp] theorem constantCoeff_zero : constantCoeff σ R 0 = 0 := rfl @[simp] theorem constantCoeff_one : constantCoeff σ R 1 = 1 := rfl @[simp] theorem constantCoeff_X (s : σ) : constantCoeff σ R (X s) = 0 := coeff_zero_X s @[simp] theorem constantCoeff_smul {S : Type*} [Semiring S] [Module R S] (φ : MvPowerSeries σ S) (a : R) : constantCoeff σ S (a • φ) = a • constantCoeff σ S φ := rfl /-- If a multivariate formal power series is invertible, then so is its constant coefficient. -/ theorem isUnit_constantCoeff (φ : MvPowerSeries σ R) (h : IsUnit φ) : IsUnit (constantCoeff σ R φ) := h.map _ @[simp] theorem coeff_smul (f : MvPowerSeries σ R) (n) (a : R) : coeff _ n (a • f) = a * coeff _ n f := rfl theorem smul_eq_C_mul (f : MvPowerSeries σ R) (a : R) : a • f = C σ R a * f := by ext simp theorem X_inj [Nontrivial R] {s t : σ} : (X s : MvPowerSeries σ R) = X t ↔ s = t := ⟨by classical intro h replace h := congr_arg (coeff R (single s 1)) h rw [coeff_X, if_pos rfl, coeff_X] at h split_ifs at h with H · rw [Finsupp.single_eq_single_iff] at H rcases H with H | H · exact H.1 · exfalso exact one_ne_zero H.1 · exfalso exact one_ne_zero h, congr_arg X⟩ end Semiring section Map variable {S T : Type*} [Semiring R] [Semiring S] [Semiring T] variable (f : R →+* S) (g : S →+* T) variable (σ) in /-- The map between multivariate formal power series induced by a map on the coefficients. -/ def map : MvPowerSeries σ R →+* MvPowerSeries σ S where toFun φ n := f <| coeff R n φ map_zero' := ext fun _n => f.map_zero map_one' := ext fun n => show f ((coeff R n) 1) = (coeff S n) 1 by classical rw [coeff_one, coeff_one] split_ifs with h · simp only [ite_true, map_one, h] · simp only [ite_false, map_zero, h] map_add' φ ψ := ext fun n => show f ((coeff R n) (φ + ψ)) = f ((coeff R n) φ) + f ((coeff R n) ψ) by simp map_mul' φ ψ := ext fun n => show f _ = _ by classical rw [coeff_mul, map_sum, coeff_mul] apply Finset.sum_congr rfl rintro ⟨i, j⟩ _; rw [f.map_mul]; rfl @[simp] theorem map_id : map σ (RingHom.id R) = RingHom.id _ := rfl theorem map_comp : map σ (g.comp f) = (map σ g).comp (map σ f) := rfl @[simp] theorem coeff_map (n : σ →₀ ℕ) (φ : MvPowerSeries σ R) : coeff S n (map σ f φ) = f (coeff R n φ) := rfl @[simp] theorem constantCoeff_map (φ : MvPowerSeries σ R) : constantCoeff σ S (map σ f φ) = f (constantCoeff σ R φ) := rfl @[simp] theorem map_monomial (n : σ →₀ ℕ) (a : R) : map σ f (monomial R n a) = monomial S n (f a) := by classical ext m simp [coeff_monomial, apply_ite f] @[simp] theorem map_C (a : R) : map σ f (C σ R a) = C σ S (f a) := map_monomial _ _ _ @[simp] theorem map_X (s : σ) : map σ f (X s) = X s := by simp [MvPowerSeries.X] end Map @[simp] theorem map_eq_zero {S : Type*} [DivisionSemiring R] [Semiring S] [Nontrivial S] (φ : MvPowerSeries σ R) (f : R →+* S) : φ.map σ f = 0 ↔ φ = 0 := by simp only [MvPowerSeries.ext_iff] congr! with n simp section Semiring variable [Semiring R] theorem X_pow_dvd_iff {s : σ} {n : ℕ} {φ : MvPowerSeries σ R} : (X s : MvPowerSeries σ R) ^ n ∣ φ ↔ ∀ m : σ →₀ ℕ, m s < n → coeff R m φ = 0 := by classical constructor · rintro ⟨φ, rfl⟩ m h rw [coeff_mul, Finset.sum_eq_zero] rintro ⟨i, j⟩ hij rw [coeff_X_pow, if_neg, zero_mul] contrapose! h dsimp at h subst i rw [mem_antidiagonal] at hij rw [← hij, Finsupp.add_apply, Finsupp.single_eq_same] exact Nat.le_add_right n _ · intro h refine ⟨fun m => coeff R (m + single s n) φ, ?_⟩ ext m by_cases H : m - single s n + single s n = m · rw [coeff_mul, Finset.sum_eq_single (single s n, m - single s n)] · rw [coeff_X_pow, if_pos rfl, one_mul] simpa using congr_arg (fun m : σ →₀ ℕ => coeff R m φ) H.symm · rintro ⟨i, j⟩ hij hne rw [mem_antidiagonal] at hij rw [coeff_X_pow] split_ifs with hi · exfalso apply hne rw [← hij, ← hi, Prod.mk_inj] refine ⟨rfl, ?_⟩ ext t simp only [add_tsub_cancel_left, Finsupp.add_apply, Finsupp.tsub_apply] · exact zero_mul _ · intro hni exfalso apply hni rwa [mem_antidiagonal, add_comm] · rw [h, coeff_mul, Finset.sum_eq_zero] · rintro ⟨i, j⟩ hij rw [mem_antidiagonal] at hij rw [coeff_X_pow] split_ifs with hi · exfalso apply H rw [← hij, hi] ext rw [coe_add, coe_add, Pi.add_apply, Pi.add_apply, add_tsub_cancel_left, add_comm] · exact zero_mul _ · contrapose! H ext t by_cases hst : s = t · subst t simpa using tsub_add_cancel_of_le H · simp [Finsupp.single_apply, hst] theorem X_dvd_iff {s : σ} {φ : MvPowerSeries σ R} : (X s : MvPowerSeries σ R) ∣ φ ↔ ∀ m : σ →₀ ℕ, m s = 0 → coeff R m φ = 0 := by rw [← pow_one (X s : MvPowerSeries σ R), X_pow_dvd_iff] constructor <;> intro h m hm · exact h m (hm.symm ▸ zero_lt_one) · exact h m (Nat.eq_zero_of_le_zero <| Nat.le_of_succ_le_succ hm) end Semiring section CommSemiring open Finset.HasAntidiagonal Finset variable {R : Type*} [CommSemiring R] {ι : Type*} [DecidableEq ι] /-- Coefficients of a product of power series -/ theorem coeff_prod [DecidableEq σ] (f : ι → MvPowerSeries σ R) (d : σ →₀ ℕ) (s : Finset ι) : coeff R d (∏ j ∈ s, f j) = ∑ l ∈ finsuppAntidiag s d, ∏ i ∈ s, coeff R (l i) (f i) := by induction s using Finset.induction_on generalizing d with | empty => simp only [prod_empty, sum_const, nsmul_eq_mul, mul_one, coeff_one, finsuppAntidiag_empty] split_ifs · simp only [card_singleton, Nat.cast_one] · simp only [card_empty, Nat.cast_zero] | insert a s ha ih => rw [finsuppAntidiag_insert ha, prod_insert ha, coeff_mul, sum_biUnion] · apply Finset.sum_congr rfl simp only [mem_antidiagonal, sum_map, Function.Embedding.coeFn_mk, coe_update, Prod.forall] rintro u v rfl rw [ih, Finset.mul_sum, ← Finset.sum_attach] apply Finset.sum_congr rfl simp only [mem_attach, Finset.prod_insert ha, Function.update_self, forall_true_left, Subtype.forall] rintro x - rw [Finset.prod_congr rfl] intro i hi rw [Function.update_of_ne] exact ne_of_mem_of_not_mem hi ha · simp only [Set.PairwiseDisjoint, Set.Pairwise, mem_coe, mem_antidiagonal, ne_eq, disjoint_left, mem_map, mem_attach, Function.Embedding.coeFn_mk, true_and, Subtype.exists, exists_prop, not_exists, not_and, forall_exists_index, and_imp, forall_apply_eq_imp_iff₂, Prod.forall, Prod.mk.injEq] rintro u v rfl u' v' huv h k - l - hkl obtain rfl : u' = u := by simpa only [Finsupp.coe_update, Function.update_self] using DFunLike.congr_fun hkl a simp only [add_right_inj] at huv exact h rfl huv.symm /-- The `d`th coefficient of a power of a multivariate power series is the sum, indexed by `finsuppAntidiag (Finset.range n) d`, of products of coefficients -/ theorem coeff_pow [DecidableEq σ] (f : MvPowerSeries σ R) {n : ℕ} (d : σ →₀ ℕ) : coeff R d (f ^ n) = ∑ l ∈ finsuppAntidiag (Finset.range n) d, ∏ i ∈ Finset.range n, coeff R (l i) f := by suffices f ^ n = (Finset.range n).prod fun _ ↦ f by rw [this, coeff_prod] rw [Finset.prod_const, card_range] /-- Vanishing of coefficients of powers of multivariate power series when the constant coefficient is nilpotent [N. Bourbaki, *Algebra {II}*, Chapter 4, §4, n°2, proposition 3][bourbaki1981] -/ theorem coeff_eq_zero_of_constantCoeff_nilpotent {f : MvPowerSeries σ R} {m : ℕ} (hf : constantCoeff σ R f ^ m = 0) {d : σ →₀ ℕ} {n : ℕ} (hn : m + degree d ≤ n) : coeff R d (f ^ n) = 0 := by classical rw [coeff_pow] apply sum_eq_zero intro k hk rw [mem_finsuppAntidiag] at hk set s := {i ∈ range n | k i = 0} with hs_def have hs : s ⊆ range n := filter_subset _ _ have hs' (i : ℕ) (hi : i ∈ s) : coeff R (k i) f = constantCoeff σ R f := by simp only [hs_def, mem_filter] at hi rw [hi.2, coeff_zero_eq_constantCoeff] have hs'' (i : ℕ) (hi : i ∈ s) : k i = 0 := by simp only [hs_def, mem_filter] at hi rw [hi.2] rw [← prod_sdiff (s₁ := s) (filter_subset _ _)] apply mul_eq_zero_of_right rw [prod_congr rfl hs', prod_const] suffices m ≤ #s by obtain ⟨m', hm'⟩ := Nat.exists_eq_add_of_le this rw [hm', pow_add, hf, MulZeroClass.zero_mul] rw [← Nat.add_le_add_iff_right, add_comm #s, Finset.card_sdiff_add_card_eq_card (filter_subset _ _), card_range] apply le_trans _ hn simp only [add_comm m, Nat.add_le_add_iff_right, ← hk.1, ← sum_sdiff (hs), sum_eq_zero (s := s) hs'', add_zero] rw [← hs_def] convert Finset.card_nsmul_le_sum (range n \ s) (fun x ↦ degree (k x)) 1 _ · simp only [Algebra.id.smul_eq_mul, mul_one] · simp only [degree_eq_weight_one, map_sum] · simp only [hs_def, mem_filter, mem_sdiff, mem_range, not_and, and_imp] intro i hi hi' rw [← not_lt, Nat.lt_one_iff, degree_eq_zero_iff] exact hi' hi end CommSemiring section Algebra variable {A : Type*} [CommSemiring R] [Semiring A] [Algebra R A] {B : Type*} [Semiring B] [Algebra R B] instance : Algebra R (MvPowerSeries σ A) where algebraMap := (MvPowerSeries.map σ (algebraMap R A)).comp (C σ R) commutes' := fun a φ => by ext n simp [Algebra.commutes] smul_def' := fun a σ => by ext n simp [(coeff A n).map_smul_of_tower a, Algebra.smul_def] theorem c_eq_algebraMap : C σ R = algebraMap R (MvPowerSeries σ R) := rfl theorem algebraMap_apply {r : R} : algebraMap R (MvPowerSeries σ A) r = C σ A (algebraMap R A r) := by change (MvPowerSeries.map σ (algebraMap R A)).comp (C σ R) r = _ simp /-- Change of coefficients in mv power series, as an `AlgHom` -/ def mapAlgHom (φ : A →ₐ[R] B) : MvPowerSeries σ A →ₐ[R] MvPowerSeries σ B where toRingHom := MvPowerSeries.map σ φ commutes' r := by simp only [RingHom.toMonoidHom_eq_coe, OneHom.toFun_eq_coe, MonoidHom.toOneHom_coe, MonoidHom.coe_coe, MvPowerSeries.algebraMap_apply, map_C, RingHom.coe_coe, AlgHom.commutes] theorem mapAlgHom_apply (φ : A →ₐ[R] B) (f : MvPowerSeries σ A) : mapAlgHom (σ := σ) φ f = MvPowerSeries.map σ φ f := rfl instance [Nonempty σ] [Nontrivial R] : Nontrivial (Subalgebra R (MvPowerSeries σ R)) := ⟨⟨⊥, ⊤, by classical rw [Ne, SetLike.ext_iff, not_forall] inhabit σ refine ⟨X default, ?_⟩ simp only [Algebra.mem_bot, not_exists, Set.mem_range, iff_true, Algebra.mem_top] intro x rw [MvPowerSeries.ext_iff, not_forall] refine ⟨Finsupp.single default 1, ?_⟩ simp [algebraMap_apply, coeff_C]⟩⟩ end Algebra end MvPowerSeries namespace MvPolynomial open Finsupp variable {σ : Type*} {R : Type*} [CommSemiring R] (φ ψ : MvPolynomial σ R) -- Porting note: added so we can add the `@[coe]` attribute /-- The natural inclusion from multivariate polynomials into multivariate formal power series. -/ @[coe] def toMvPowerSeries : MvPolynomial σ R → MvPowerSeries σ R := fun φ n => coeff n φ /-- The natural inclusion from multivariate polynomials into multivariate formal power series. -/ instance coeToMvPowerSeries : Coe (MvPolynomial σ R) (MvPowerSeries σ R) := ⟨toMvPowerSeries⟩
theorem coe_def : (φ : MvPowerSeries σ R) = fun n => coeff n φ := rfl @[simp, norm_cast] theorem coeff_coe (n : σ →₀ ℕ) : MvPowerSeries.coeff R n ↑φ = coeff n φ := rfl
Mathlib/RingTheory/MvPowerSeries/Basic.lean
794
799
/- Copyright (c) 2021 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne -/ import Mathlib.MeasureTheory.Function.LpSeminorm.Trim import Mathlib.MeasureTheory.Function.StronglyMeasurable.Inner import Mathlib.MeasureTheory.Function.StronglyMeasurable.Lp /-! # Functions a.e. measurable with respect to a sub-σ-algebra A function `f` verifies `AEStronglyMeasurable[m] f μ` if it is `μ`-a.e. equal to an `m`-strongly measurable function. This is similar to `AEStronglyMeasurable`, but the `MeasurableSpace` structures used for the measurability statement and for the measure are different. We define `lpMeas F 𝕜 m p μ`, the subspace of `Lp F p μ` containing functions `f` verifying `AEStronglyMeasurable[m] f μ`, i.e. functions which are `μ`-a.e. equal to an `m`-strongly measurable function. ## Main statements We define an `IsometryEquiv` between `lpMeasSubgroup` and the `Lp` space corresponding to the measure `μ.trim hm`. As a consequence, the completeness of `Lp` implies completeness of `lpMeas`. `Lp.induction_stronglyMeasurable` (see also `MemLp.induction_stronglyMeasurable`): To prove something for an `Lp` function a.e. strongly measurable with respect to a sub-σ-algebra `m` in a normed space, it suffices to show that * the property holds for (multiples of) characteristic functions which are measurable w.r.t. `m`; * is closed under addition; * the set of functions in `Lp` strongly measurable w.r.t. `m` for which the property holds is closed. -/ open TopologicalSpace Filter open scoped ENNReal MeasureTheory namespace MeasureTheory /-- A function `f` verifies `AEStronglyMeasurable[m] f μ` if it is `μ`-a.e. equal to an `m`-strongly measurable function. This is similar to `AEStronglyMeasurable`, but the `MeasurableSpace` structures used for the measurability statement and for the measure are different. -/ @[deprecated AEStronglyMeasurable (since := "2025-01-23")] def AEStronglyMeasurable' {α β} [TopologicalSpace β] (m : MeasurableSpace α) {_ : MeasurableSpace α} (f : α → β) (μ : Measure α) : Prop := AEStronglyMeasurable[m] f μ namespace AEStronglyMeasurable' variable {α β 𝕜 : Type*} {m m0 : MeasurableSpace α} {μ : Measure α} [TopologicalSpace β] {f g : α → β} @[deprecated AEStronglyMeasurable.congr (since := "2025-01-23")] theorem congr (hf : AEStronglyMeasurable[m] f μ) (hfg : f =ᵐ[μ] g) : AEStronglyMeasurable[m] g μ := AEStronglyMeasurable.congr hf hfg @[deprecated AEStronglyMeasurable.mono (since := "2025-01-23")] theorem mono {m'} (hf : AEStronglyMeasurable[m] f μ) (hm : m ≤ m') : AEStronglyMeasurable[m'] f μ := AEStronglyMeasurable.mono hm hf @[deprecated AEStronglyMeasurable.add (since := "2025-01-23")] theorem add [Add β] [ContinuousAdd β] (hf : AEStronglyMeasurable[m] f μ) (hg : AEStronglyMeasurable[m] g μ) : AEStronglyMeasurable[m] (f + g) μ := AEStronglyMeasurable.add hf hg @[deprecated AEStronglyMeasurable.neg (since := "2025-01-23")] theorem neg [Neg β] [ContinuousNeg β] {f : α → β} (hfm : AEStronglyMeasurable[m] f μ) : AEStronglyMeasurable[m] (-f) μ := AEStronglyMeasurable.neg hfm @[deprecated AEStronglyMeasurable.sub (since := "2025-01-23")] theorem sub [AddGroup β] [IsTopologicalAddGroup β] {f g : α → β} (hfm : AEStronglyMeasurable[m] f μ) (hgm : AEStronglyMeasurable[m] g μ) : AEStronglyMeasurable[m] (f - g) μ := AEStronglyMeasurable.sub hfm hgm @[deprecated AEStronglyMeasurable.const_smul (since := "2025-01-23")] theorem const_smul [SMul 𝕜 β] [ContinuousConstSMul 𝕜 β] (c : 𝕜) (hf : AEStronglyMeasurable[m] f μ) : AEStronglyMeasurable[m] (c • f) μ := AEStronglyMeasurable.const_smul hf _ @[deprecated AEStronglyMeasurable.const_inner (since := "2025-01-23")] theorem const_inner {𝕜 β} [RCLike 𝕜] [NormedAddCommGroup β] [InnerProductSpace 𝕜 β] {f : α → β} (hfm : AEStronglyMeasurable[m] f μ) (c : β) : AEStronglyMeasurable[m] (fun x => (inner c (f x) : 𝕜)) μ := AEStronglyMeasurable.const_inner hfm @[deprecated AEStronglyMeasurable.of_subsingleton_cod (since := "2025-01-23")] theorem of_subsingleton [Subsingleton β] : AEStronglyMeasurable[m] f μ := .of_subsingleton_cod @[deprecated AEStronglyMeasurable.of_subsingleton_dom (since := "2025-01-23")] theorem of_subsingleton' [Subsingleton α] : AEStronglyMeasurable[m] f μ := .of_subsingleton_dom /-- An `m`-strongly measurable function almost everywhere equal to `f`. -/ @[deprecated AEStronglyMeasurable.mk (since := "2025-01-23")] noncomputable def mk (f : α → β) (hfm : AEStronglyMeasurable[m] f μ) : α → β := AEStronglyMeasurable.mk f hfm @[deprecated AEStronglyMeasurable.stronglyMeasurable_mk (since := "2025-01-23")] theorem stronglyMeasurable_mk {f : α → β} (hfm : AEStronglyMeasurable[m] f μ) : StronglyMeasurable[m] (hfm.mk f) := AEStronglyMeasurable.stronglyMeasurable_mk hfm @[deprecated AEStronglyMeasurable.ae_eq_mk (since := "2025-01-23")] theorem ae_eq_mk {f : α → β} (hfm : AEStronglyMeasurable[m] f μ) : f =ᵐ[μ] hfm.mk f := AEStronglyMeasurable.ae_eq_mk hfm @[deprecated Continuous.comp_aestronglyMeasurable (since := "2025-01-23")] theorem continuous_comp {γ} [TopologicalSpace γ] {f : α → β} {g : β → γ} (hg : Continuous g) (hf : AEStronglyMeasurable[m] f μ) : AEStronglyMeasurable[m] (g ∘ f) μ := hg.comp_aestronglyMeasurable hf end AEStronglyMeasurable' @[deprecated AEStronglyMeasurable.of_trim (since := "2025-01-23")] theorem aeStronglyMeasurable'_of_aeStronglyMeasurable'_trim {α β} {m m0 m0' : MeasurableSpace α} [TopologicalSpace β] (hm0 : m0 ≤ m0') {μ : Measure α} {f : α → β} (hf : AEStronglyMeasurable[m] f (μ.trim hm0)) : AEStronglyMeasurable[m] f μ := .of_trim hm0 hf @[deprecated StronglyMeasurable.aestronglyMeasurable (since := "2025-01-23")] theorem StronglyMeasurable.aeStronglyMeasurable' {α β} {m _ : MeasurableSpace α} [TopologicalSpace β] {μ : Measure α} {f : α → β} (hf : StronglyMeasurable[m] f) : AEStronglyMeasurable[m] f μ := hf.aestronglyMeasurable theorem ae_eq_trim_iff_of_aestronglyMeasurable {α β} [TopologicalSpace β] [MetrizableSpace β] {m m0 : MeasurableSpace α} {μ : Measure α} {f g : α → β} (hm : m ≤ m0) (hfm : AEStronglyMeasurable[m] f μ) (hgm : AEStronglyMeasurable[m] g μ) : hfm.mk f =ᵐ[μ.trim hm] hgm.mk g ↔ f =ᵐ[μ] g := (hfm.stronglyMeasurable_mk.ae_eq_trim_iff hm hgm.stronglyMeasurable_mk).trans ⟨fun h => hfm.ae_eq_mk.trans (h.trans hgm.ae_eq_mk.symm), fun h => hfm.ae_eq_mk.symm.trans (h.trans hgm.ae_eq_mk)⟩ @[deprecated (since := "2025-04-09")] alias ae_eq_trim_iff_of_aeStronglyMeasurable' := ae_eq_trim_iff_of_aestronglyMeasurable theorem AEStronglyMeasurable.comp_ae_measurable' {α β γ : Type*} [TopologicalSpace β] {mα : MeasurableSpace α} {_ : MeasurableSpace γ} {f : α → β} {μ : Measure γ} {g : γ → α} (hf : AEStronglyMeasurable f (μ.map g)) (hg : AEMeasurable g μ) : AEStronglyMeasurable[mα.comap g] (f ∘ g) μ := ⟨hf.mk f ∘ g, hf.stronglyMeasurable_mk.comp_measurable (measurable_iff_comap_le.mpr le_rfl), ae_eq_comp hg hf.ae_eq_mk⟩ /-- If the restriction to a set `s` of a σ-algebra `m` is included in the restriction to `s` of another σ-algebra `m₂` (hypothesis `hs`), the set `s` is `m` measurable and a function `f` almost everywhere supported on `s` is `m`-ae-strongly-measurable, then `f` is also `m₂`-ae-strongly-measurable. -/ @[deprecated AEStronglyMeasurable.of_measurableSpace_le_on (since := "2025-01-23")] theorem AEStronglyMeasurable'.aeStronglyMeasurable'_of_measurableSpace_le_on {α E} {m m₂ m0 : MeasurableSpace α} {μ : Measure α} [TopologicalSpace E] [Zero E] (hm : m ≤ m0) {s : Set α} {f : α → E} (hs_m : MeasurableSet[m] s) (hs : ∀ t, MeasurableSet[m] (s ∩ t) → MeasurableSet[m₂] (s ∩ t)) (hf : AEStronglyMeasurable[m] f μ) (hf_zero : f =ᵐ[μ.restrict sᶜ] 0) : AEStronglyMeasurable[m₂] f μ := .of_measurableSpace_le_on hm hs_m hs hf hf_zero variable {α F 𝕜 : Type*} {p : ℝ≥0∞} [RCLike 𝕜] -- 𝕜 for ℝ or ℂ -- F for a Lp submodule [NormedAddCommGroup F] [NormedSpace 𝕜 F] section LpMeas /-! ## The subset `lpMeas` of `Lp` functions a.e. measurable with respect to a sub-sigma-algebra -/ variable (F) /-- `lpMeasSubgroup F m p μ` is the subspace of `Lp F p μ` containing functions `f` verifying `AEStronglyMeasurable[m] f μ`, i.e. functions which are `μ`-a.e. equal to an `m`-strongly measurable function. -/ def lpMeasSubgroup (m : MeasurableSpace α) [MeasurableSpace α] (p : ℝ≥0∞) (μ : Measure α) : AddSubgroup (Lp F p μ) where carrier := {f : Lp F p μ | AEStronglyMeasurable[m] f μ} zero_mem' := ⟨(0 : α → F), @stronglyMeasurable_zero _ _ m _ _, Lp.coeFn_zero _ _ _⟩ add_mem' {f g} hf hg := (hf.add hg).congr (Lp.coeFn_add f g).symm neg_mem' {f} hf := AEStronglyMeasurable.congr hf.neg (Lp.coeFn_neg f).symm variable (𝕜) /-- `lpMeas F 𝕜 m p μ` is the subspace of `Lp F p μ` containing functions `f` verifying `AEStronglyMeasurable[m] f μ`, i.e. functions which are `μ`-a.e. equal to an `m`-strongly measurable function. -/ def lpMeas (m : MeasurableSpace α) [MeasurableSpace α] (p : ℝ≥0∞) (μ : Measure α) : Submodule 𝕜 (Lp F p μ) where carrier := {f : Lp F p μ | AEStronglyMeasurable[m] f μ} zero_mem' := ⟨(0 : α → F), @stronglyMeasurable_zero _ _ m _ _, Lp.coeFn_zero _ _ _⟩ add_mem' {f g} hf hg := (hf.add hg).congr (Lp.coeFn_add f g).symm smul_mem' c f hf := (hf.const_smul c).congr (Lp.coeFn_smul c f).symm variable {F 𝕜} theorem mem_lpMeasSubgroup_iff_aestronglyMeasurable {m m0 : MeasurableSpace α} {μ : Measure α} {f : Lp F p μ} : f ∈ lpMeasSubgroup F m p μ ↔ AEStronglyMeasurable[m] f μ := by rw [← AddSubgroup.mem_carrier, lpMeasSubgroup, Set.mem_setOf_eq] @[deprecated (since := "2025-01-24")] alias mem_lpMeasSubgroup_iff_aeStronglyMeasurable' := mem_lpMeasSubgroup_iff_aestronglyMeasurable @[deprecated (since := "2025-04-09")] alias mem_lpMeasSubgroup_iff_aeStronglyMeasurable := mem_lpMeasSubgroup_iff_aestronglyMeasurable theorem mem_lpMeas_iff_aestronglyMeasurable {m m0 : MeasurableSpace α} {μ : Measure α} {f : Lp F p μ} : f ∈ lpMeas F 𝕜 m p μ ↔ AEStronglyMeasurable[m] f μ := by rw [← SetLike.mem_coe, ← Submodule.mem_carrier, lpMeas, Set.mem_setOf_eq] @[deprecated (since := "2025-01-24")] alias mem_lpMeas_iff_aeStronglyMeasurable' := mem_lpMeas_iff_aestronglyMeasurable @[deprecated (since := "2025-04-09")] alias mem_lpMeas_iff_aeStronglyMeasurable := mem_lpMeas_iff_aestronglyMeasurable theorem lpMeas.aestronglyMeasurable {m _ : MeasurableSpace α} {μ : Measure α} (f : lpMeas F 𝕜 m p μ) : AEStronglyMeasurable[m] (f : α → F) μ := mem_lpMeas_iff_aestronglyMeasurable.mp f.mem @[deprecated (since := "2025-01-24")] alias lpMeas.aeStronglyMeasurable' := lpMeas.aestronglyMeasurable @[deprecated (since := "2025-04-09")] alias lpMeas.aeStronglyMeasurable := lpMeas.aestronglyMeasurable theorem mem_lpMeas_self {m0 : MeasurableSpace α} (μ : Measure α) (f : Lp F p μ) : f ∈ lpMeas F 𝕜 m0 p μ := mem_lpMeas_iff_aestronglyMeasurable.mpr (Lp.aestronglyMeasurable f) theorem mem_lpMeas_indicatorConstLp {m m0 : MeasurableSpace α} (hm : m ≤ m0) {μ : Measure α} {s : Set α} (hs : MeasurableSet[m] s) (hμs : μ s ≠ ∞) {c : F} : indicatorConstLp p (hm s hs) hμs c ∈ lpMeas F 𝕜 m p μ := ⟨s.indicator fun _ : α => c, (@stronglyMeasurable_const _ _ m _ _).indicator hs, indicatorConstLp_coeFn⟩ section CompleteSubspace /-! ## The subspace `lpMeas` is complete. We define an `IsometryEquiv` between `lpMeasSubgroup` and the `Lp` space corresponding to the measure `μ.trim hm`. As a consequence, the completeness of `Lp` implies completeness of `lpMeasSubgroup` (and `lpMeas`). -/ variable {m m0 : MeasurableSpace α} {μ : Measure α} /-- If `f` belongs to `lpMeasSubgroup F m p μ`, then the measurable function it is almost everywhere equal to (given by `AEMeasurable.mk`) belongs to `ℒp` for the measure `μ.trim hm`. -/ theorem memLp_trim_of_mem_lpMeasSubgroup (hm : m ≤ m0) (f : Lp F p μ) (hf_meas : f ∈ lpMeasSubgroup F m p μ) : MemLp (mem_lpMeasSubgroup_iff_aestronglyMeasurable.mp hf_meas).choose p (μ.trim hm) := by have hf : AEStronglyMeasurable[m] f μ := mem_lpMeasSubgroup_iff_aestronglyMeasurable.mp hf_meas let g := hf.choose obtain ⟨hg, hfg⟩ := hf.choose_spec change MemLp g p (μ.trim hm) refine ⟨hg.aestronglyMeasurable, ?_⟩ have h_eLpNorm_fg : eLpNorm g p (μ.trim hm) = eLpNorm f p μ := by rw [eLpNorm_trim hm hg] exact eLpNorm_congr_ae hfg.symm rw [h_eLpNorm_fg] exact Lp.eLpNorm_lt_top f @[deprecated (since := "2025-02-21")] alias memℒp_trim_of_mem_lpMeasSubgroup := memLp_trim_of_mem_lpMeasSubgroup /-- If `f` belongs to `Lp` for the measure `μ.trim hm`, then it belongs to the subgroup `lpMeasSubgroup F m p μ`. -/ theorem mem_lpMeasSubgroup_toLp_of_trim (hm : m ≤ m0) (f : Lp F p (μ.trim hm)) : (memLp_of_memLp_trim hm (Lp.memLp f)).toLp f ∈ lpMeasSubgroup F m p μ := by let hf_mem_ℒp := memLp_of_memLp_trim hm (Lp.memLp f) rw [mem_lpMeasSubgroup_iff_aestronglyMeasurable] refine AEStronglyMeasurable.congr ?_ (MemLp.coeFn_toLp hf_mem_ℒp).symm exact (Lp.aestronglyMeasurable f).of_trim hm variable (F p μ) /-- Map from `lpMeasSubgroup` to `Lp F p (μ.trim hm)`. -/ noncomputable def lpMeasSubgroupToLpTrim (hm : m ≤ m0) (f : lpMeasSubgroup F m p μ) : Lp F p (μ.trim hm) := MemLp.toLp (mem_lpMeasSubgroup_iff_aestronglyMeasurable.mp f.mem).choose (memLp_trim_of_mem_lpMeasSubgroup hm f.1 f.mem) variable (𝕜) in /-- Map from `lpMeas` to `Lp F p (μ.trim hm)`. -/ noncomputable def lpMeasToLpTrim (hm : m ≤ m0) (f : lpMeas F 𝕜 m p μ) : Lp F p (μ.trim hm) := MemLp.toLp (mem_lpMeas_iff_aestronglyMeasurable.mp f.mem).choose (memLp_trim_of_mem_lpMeasSubgroup hm f.1 f.mem) /-- Map from `Lp F p (μ.trim hm)` to `lpMeasSubgroup`, inverse of `lpMeasSubgroupToLpTrim`. -/ noncomputable def lpTrimToLpMeasSubgroup (hm : m ≤ m0) (f : Lp F p (μ.trim hm)) : lpMeasSubgroup F m p μ := ⟨(memLp_of_memLp_trim hm (Lp.memLp f)).toLp f, mem_lpMeasSubgroup_toLp_of_trim hm f⟩ variable (𝕜) in /-- Map from `Lp F p (μ.trim hm)` to `lpMeas`, inverse of `Lp_meas_to_Lp_trim`. -/ noncomputable def lpTrimToLpMeas (hm : m ≤ m0) (f : Lp F p (μ.trim hm)) : lpMeas F 𝕜 m p μ := ⟨(memLp_of_memLp_trim hm (Lp.memLp f)).toLp f, mem_lpMeasSubgroup_toLp_of_trim hm f⟩ variable {F p μ} theorem lpMeasSubgroupToLpTrim_ae_eq (hm : m ≤ m0) (f : lpMeasSubgroup F m p μ) : lpMeasSubgroupToLpTrim F p μ hm f =ᵐ[μ] f := (ae_eq_of_ae_eq_trim (MemLp.coeFn_toLp (memLp_trim_of_mem_lpMeasSubgroup hm f.1 f.mem))).trans (mem_lpMeasSubgroup_iff_aestronglyMeasurable.mp f.mem).choose_spec.2.symm theorem lpTrimToLpMeasSubgroup_ae_eq (hm : m ≤ m0) (f : Lp F p (μ.trim hm)) : lpTrimToLpMeasSubgroup F p μ hm f =ᵐ[μ] f := MemLp.coeFn_toLp (memLp_of_memLp_trim hm (Lp.memLp f)) theorem lpMeasToLpTrim_ae_eq (hm : m ≤ m0) (f : lpMeas F 𝕜 m p μ) : lpMeasToLpTrim F 𝕜 p μ hm f =ᵐ[μ] f := (ae_eq_of_ae_eq_trim (MemLp.coeFn_toLp (memLp_trim_of_mem_lpMeasSubgroup hm f.1 f.mem))).trans (mem_lpMeasSubgroup_iff_aestronglyMeasurable.mp f.mem).choose_spec.2.symm theorem lpTrimToLpMeas_ae_eq (hm : m ≤ m0) (f : Lp F p (μ.trim hm)) : lpTrimToLpMeas F 𝕜 p μ hm f =ᵐ[μ] f := MemLp.coeFn_toLp (memLp_of_memLp_trim hm (Lp.memLp f)) /-- `lpTrimToLpMeasSubgroup` is a right inverse of `lpMeasSubgroupToLpTrim`. -/ theorem lpMeasSubgroupToLpTrim_right_inv (hm : m ≤ m0) : Function.RightInverse (lpTrimToLpMeasSubgroup F p μ hm) (lpMeasSubgroupToLpTrim F p μ hm) := by intro f ext1 refine (Lp.stronglyMeasurable _).ae_eq_trim_of_stronglyMeasurable hm (Lp.stronglyMeasurable _) ?_ exact (lpMeasSubgroupToLpTrim_ae_eq hm _).trans (lpTrimToLpMeasSubgroup_ae_eq hm _) /-- `lpTrimToLpMeasSubgroup` is a left inverse of `lpMeasSubgroupToLpTrim`. -/ theorem lpMeasSubgroupToLpTrim_left_inv (hm : m ≤ m0) : Function.LeftInverse (lpTrimToLpMeasSubgroup F p μ hm) (lpMeasSubgroupToLpTrim F p μ hm) := by intro f ext1 ext1 exact (lpTrimToLpMeasSubgroup_ae_eq hm _).trans (lpMeasSubgroupToLpTrim_ae_eq hm _) theorem lpMeasSubgroupToLpTrim_add (hm : m ≤ m0) (f g : lpMeasSubgroup F m p μ) : lpMeasSubgroupToLpTrim F p μ hm (f + g) = lpMeasSubgroupToLpTrim F p μ hm f + lpMeasSubgroupToLpTrim F p μ hm g := by ext1 refine EventuallyEq.trans ?_ (Lp.coeFn_add _ _).symm refine (Lp.stronglyMeasurable _).ae_eq_trim_of_stronglyMeasurable hm ?_ ?_ · exact (Lp.stronglyMeasurable _).add (Lp.stronglyMeasurable _) refine (lpMeasSubgroupToLpTrim_ae_eq hm _).trans ?_ refine EventuallyEq.trans ?_ (EventuallyEq.add (lpMeasSubgroupToLpTrim_ae_eq hm f).symm (lpMeasSubgroupToLpTrim_ae_eq hm g).symm) refine (Lp.coeFn_add _ _).trans ?_ filter_upwards with x using rfl theorem lpMeasSubgroupToLpTrim_neg (hm : m ≤ m0) (f : lpMeasSubgroup F m p μ) : lpMeasSubgroupToLpTrim F p μ hm (-f) = -lpMeasSubgroupToLpTrim F p μ hm f := by ext1 refine EventuallyEq.trans ?_ (Lp.coeFn_neg _).symm refine (Lp.stronglyMeasurable _).ae_eq_trim_of_stronglyMeasurable hm (Lp.stronglyMeasurable _).neg <| (lpMeasSubgroupToLpTrim_ae_eq hm _).trans <| ((Lp.coeFn_neg _).trans ?_).trans (lpMeasSubgroupToLpTrim_ae_eq hm f).symm.neg exact Eventually.of_forall fun x => by rfl theorem lpMeasSubgroupToLpTrim_sub (hm : m ≤ m0) (f g : lpMeasSubgroup F m p μ) : lpMeasSubgroupToLpTrim F p μ hm (f - g) = lpMeasSubgroupToLpTrim F p μ hm f - lpMeasSubgroupToLpTrim F p μ hm g := by rw [sub_eq_add_neg, sub_eq_add_neg, lpMeasSubgroupToLpTrim_add, lpMeasSubgroupToLpTrim_neg] theorem lpMeasToLpTrim_smul (hm : m ≤ m0) (c : 𝕜) (f : lpMeas F 𝕜 m p μ) : lpMeasToLpTrim F 𝕜 p μ hm (c • f) = c • lpMeasToLpTrim F 𝕜 p μ hm f := by ext1 refine EventuallyEq.trans ?_ (Lp.coeFn_smul _ _).symm refine (Lp.stronglyMeasurable _).ae_eq_trim_of_stronglyMeasurable hm ?_ ?_ · exact (Lp.stronglyMeasurable _).const_smul c refine (lpMeasToLpTrim_ae_eq hm _).trans ?_ refine (Lp.coeFn_smul _ _).trans ?_ refine (lpMeasToLpTrim_ae_eq hm f).mono fun x hx => ?_ simp only [Pi.smul_apply, hx] /-- `lpMeasSubgroupToLpTrim` preserves the norm. -/ theorem lpMeasSubgroupToLpTrim_norm_map [hp : Fact (1 ≤ p)] (hm : m ≤ m0) (f : lpMeasSubgroup F m p μ) : ‖lpMeasSubgroupToLpTrim F p μ hm f‖ = ‖f‖ := by rw [Lp.norm_def, eLpNorm_trim hm (Lp.stronglyMeasurable _), eLpNorm_congr_ae (lpMeasSubgroupToLpTrim_ae_eq hm _), ← Lp.norm_def] congr theorem isometry_lpMeasSubgroupToLpTrim [hp : Fact (1 ≤ p)] (hm : m ≤ m0) : Isometry (lpMeasSubgroupToLpTrim F p μ hm) := Isometry.of_dist_eq fun f g => by rw [dist_eq_norm, ← lpMeasSubgroupToLpTrim_sub, lpMeasSubgroupToLpTrim_norm_map, dist_eq_norm] variable (F p μ) /-- `lpMeasSubgroup` and `Lp F p (μ.trim hm)` are isometric. -/ noncomputable def lpMeasSubgroupToLpTrimIso [Fact (1 ≤ p)] (hm : m ≤ m0) : lpMeasSubgroup F m p μ ≃ᵢ Lp F p (μ.trim hm) where toFun := lpMeasSubgroupToLpTrim F p μ hm invFun := lpTrimToLpMeasSubgroup F p μ hm left_inv := lpMeasSubgroupToLpTrim_left_inv hm right_inv := lpMeasSubgroupToLpTrim_right_inv hm
isometry_toFun := isometry_lpMeasSubgroupToLpTrim hm variable (𝕜) /-- `lpMeasSubgroup` and `lpMeas` are isometric. -/ noncomputable def lpMeasSubgroupToLpMeasIso [Fact (1 ≤ p)] : lpMeasSubgroup F m p μ ≃ᵢ lpMeas F 𝕜 m p μ := IsometryEquiv.refl (lpMeasSubgroup F m p μ) /-- `lpMeas` and `Lp F p (μ.trim hm)` are isometric, with a linear equivalence. -/ noncomputable def lpMeasToLpTrimLie [Fact (1 ≤ p)] (hm : m ≤ m0) : lpMeas F 𝕜 m p μ ≃ₗᵢ[𝕜] Lp F p (μ.trim hm) where toFun := lpMeasToLpTrim F 𝕜 p μ hm invFun := lpTrimToLpMeas F 𝕜 p μ hm left_inv := lpMeasSubgroupToLpTrim_left_inv hm
Mathlib/MeasureTheory/Function/ConditionalExpectation/AEMeasurable.lean
396
410
/- Copyright (c) 2020 Kim Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kim Morrison, Johan Commelin, Andrew Yang, Joël Riou -/ import Mathlib.Algebra.Group.Basic import Mathlib.CategoryTheory.Limits.Preserves.Shapes.Zero import Mathlib.CategoryTheory.Monoidal.End import Mathlib.CategoryTheory.Monoidal.Discrete /-! # Shift A `Shift` on a category `C` indexed by a monoid `A` is nothing more than a monoidal functor from `A` to `C ⥤ C`. A typical example to keep in mind might be the category of complexes `⋯ → C_{n-1} → C_n → C_{n+1} → ⋯`. It has a shift indexed by `ℤ`, where we assign to each `n : ℤ` the functor `C ⥤ C` that re-indexes the terms, so the degree `i` term of `Shift n C` would be the degree `i+n`-th term of `C`. ## Main definitions * `HasShift`: A typeclass asserting the existence of a shift functor. * `shiftEquiv`: When the indexing monoid is a group, then the functor indexed by `n` and `-n` forms a self-equivalence of `C`. * `shiftComm`: When the indexing monoid is commutative, then shifts commute as well. ## Implementation Notes `[HasShift C A]` is implemented using monoidal functors from `Discrete A` to `C ⥤ C`. However, the API of monoidal functors is used only internally: one should use the API of shifts functors which includes `shiftFunctor C a : C ⥤ C` for `a : A`, `shiftFunctorZero C A : shiftFunctor C (0 : A) ≅ 𝟭 C` and `shiftFunctorAdd C i j : shiftFunctor C (i + j) ≅ shiftFunctor C i ⋙ shiftFunctor C j` (and its variant `shiftFunctorAdd'`). These isomorphisms satisfy some coherence properties which are stated in lemmas like `shiftFunctorAdd'_assoc`, `shiftFunctorAdd'_zero_add` and `shiftFunctorAdd'_add_zero`. -/ namespace CategoryTheory noncomputable section universe v u variable (C : Type u) (A : Type*) [Category.{v} C] attribute [local instance] endofunctorMonoidalCategory variable {A C} section Defs variable (A C) [AddMonoid A] /-- A category has a shift indexed by an additive monoid `A` if there is a monoidal functor from `A` to `C ⥤ C`. -/ class HasShift (C : Type u) (A : Type*) [Category.{v} C] [AddMonoid A] where /-- a shift is a monoidal functor from `A` to `C ⥤ C` -/ shift : Discrete A ⥤ C ⥤ C /-- `shift` is monoidal -/ shiftMonoidal : shift.Monoidal := by infer_instance /-- A helper structure to construct the shift functor `(Discrete A) ⥤ (C ⥤ C)`. -/ structure ShiftMkCore where /-- the family of shift functors -/ F : A → C ⥤ C /-- the shift by 0 identifies to the identity functor -/ zero : F 0 ≅ 𝟭 C /-- the composition of shift functors identifies to the shift by the sum -/ add : ∀ n m : A, F (n + m) ≅ F n ⋙ F m /-- compatibility with the associativity -/ assoc_hom_app : ∀ (m₁ m₂ m₃ : A) (X : C), (add (m₁ + m₂) m₃).hom.app X ≫ (F m₃).map ((add m₁ m₂).hom.app X) = eqToHom (by rw [add_assoc]) ≫ (add m₁ (m₂ + m₃)).hom.app X ≫ (add m₂ m₃).hom.app ((F m₁).obj X) := by aesop_cat /-- compatibility with the left addition with 0 -/ zero_add_hom_app : ∀ (n : A) (X : C), (add 0 n).hom.app X = eqToHom (by dsimp; rw [zero_add]) ≫ (F n).map (zero.inv.app X) := by aesop_cat /-- compatibility with the right addition with 0 -/ add_zero_hom_app : ∀ (n : A) (X : C), (add n 0).hom.app X = eqToHom (by dsimp; rw [add_zero]) ≫ zero.inv.app ((F n).obj X) := by aesop_cat namespace ShiftMkCore variable {C A} attribute [reassoc] assoc_hom_app @[reassoc] lemma assoc_inv_app (h : ShiftMkCore C A) (m₁ m₂ m₃ : A) (X : C) : (h.F m₃).map ((h.add m₁ m₂).inv.app X) ≫ (h.add (m₁ + m₂) m₃).inv.app X = (h.add m₂ m₃).inv.app ((h.F m₁).obj X) ≫ (h.add m₁ (m₂ + m₃)).inv.app X ≫ eqToHom (by rw [add_assoc]) := by rw [← cancel_mono ((h.add (m₁ + m₂) m₃).hom.app X ≫ (h.F m₃).map ((h.add m₁ m₂).hom.app X)), Category.assoc, Category.assoc, Category.assoc, Iso.inv_hom_id_app_assoc, ← Functor.map_comp, Iso.inv_hom_id_app, Functor.map_id, h.assoc_hom_app, eqToHom_trans_assoc, eqToHom_refl, Category.id_comp, Iso.inv_hom_id_app_assoc, Iso.inv_hom_id_app] rfl lemma zero_add_inv_app (h : ShiftMkCore C A) (n : A) (X : C) : (h.add 0 n).inv.app X = (h.F n).map (h.zero.hom.app X) ≫ eqToHom (by dsimp; rw [zero_add]) := by rw [← cancel_epi ((h.add 0 n).hom.app X), Iso.hom_inv_id_app, h.zero_add_hom_app, Category.assoc, ← Functor.map_comp_assoc, Iso.inv_hom_id_app, Functor.map_id, Category.id_comp, eqToHom_trans, eqToHom_refl] lemma add_zero_inv_app (h : ShiftMkCore C A) (n : A) (X : C) : (h.add n 0).inv.app X = h.zero.hom.app ((h.F n).obj X) ≫ eqToHom (by dsimp; rw [add_zero]) := by rw [← cancel_epi ((h.add n 0).hom.app X), Iso.hom_inv_id_app, h.add_zero_hom_app, Category.assoc, Iso.inv_hom_id_app_assoc, eqToHom_trans, eqToHom_refl] end ShiftMkCore section attribute [local simp] eqToHom_map instance (h : ShiftMkCore C A) : (Discrete.functor h.F).Monoidal := Functor.CoreMonoidal.toMonoidal { εIso := h.zero.symm μIso := fun m n ↦ (h.add m.as n.as).symm μIso_hom_natural_left := by rintro ⟨X⟩ ⟨Y⟩ ⟨⟨⟨rfl⟩⟩⟩ ⟨X'⟩ ext dsimp simp μIso_hom_natural_right := by rintro ⟨X⟩ ⟨Y⟩ ⟨X'⟩ ⟨⟨⟨rfl⟩⟩⟩ ext dsimp simp associativity := by rintro ⟨m₁⟩ ⟨m₂⟩ ⟨m₃⟩ ext X simp [endofunctorMonoidalCategory, h.assoc_inv_app_assoc] left_unitality := by rintro ⟨n⟩ ext X simp [endofunctorMonoidalCategory, h.zero_add_inv_app, ← Functor.map_comp] right_unitality := by rintro ⟨n⟩ ext X simp [endofunctorMonoidalCategory, h.add_zero_inv_app] } /-- Constructs a `HasShift C A` instance from `ShiftMkCore`. -/ def hasShiftMk (h : ShiftMkCore C A) : HasShift C A where shift := Discrete.functor h.F end section variable [HasShift C A] /-- The monoidal functor from `A` to `C ⥤ C` given a `HasShift` instance. -/ def shiftMonoidalFunctor : Discrete A ⥤ C ⥤ C := HasShift.shift instance : (shiftMonoidalFunctor C A).Monoidal := HasShift.shiftMonoidal variable {A} open Functor.Monoidal /-- The shift autoequivalence, moving objects and morphisms 'up'. -/ def shiftFunctor (i : A) : C ⥤ C := (shiftMonoidalFunctor C A).obj ⟨i⟩ /-- Shifting by `i + j` is the same as shifting by `i` and then shifting by `j`. -/ def shiftFunctorAdd (i j : A) : shiftFunctor C (i + j) ≅ shiftFunctor C i ⋙ shiftFunctor C j := (μIso (shiftMonoidalFunctor C A) ⟨i⟩ ⟨j⟩).symm /-- When `k = i + j`, shifting by `k` is the same as shifting by `i` and then shifting by `j`. -/ def shiftFunctorAdd' (i j k : A) (h : i + j = k) : shiftFunctor C k ≅ shiftFunctor C i ⋙ shiftFunctor C j := eqToIso (by rw [h]) ≪≫ shiftFunctorAdd C i j lemma shiftFunctorAdd'_eq_shiftFunctorAdd (i j : A) : shiftFunctorAdd' C i j (i+j) rfl = shiftFunctorAdd C i j := by ext1 apply Category.id_comp variable (A) in /-- Shifting by zero is the identity functor. -/ def shiftFunctorZero : shiftFunctor C (0 : A) ≅ 𝟭 C := (εIso (shiftMonoidalFunctor C A)).symm /-- Shifting by `a` such that `a = 0` identifies to the identity functor. -/ def shiftFunctorZero' (a : A) (ha : a = 0) : shiftFunctor C a ≅ 𝟭 C := eqToIso (by rw [ha]) ≪≫ shiftFunctorZero C A end variable {C A} lemma ShiftMkCore.shiftFunctor_eq (h : ShiftMkCore C A) (a : A) : letI := hasShiftMk C A h shiftFunctor C a = h.F a := rfl lemma ShiftMkCore.shiftFunctorZero_eq (h : ShiftMkCore C A) : letI := hasShiftMk C A h shiftFunctorZero C A = h.zero := rfl lemma ShiftMkCore.shiftFunctorAdd_eq (h : ShiftMkCore C A) (a b : A) : letI := hasShiftMk C A h shiftFunctorAdd C a b = h.add a b := rfl set_option quotPrecheck false in /-- shifting an object `X` by `n` is obtained by the notation `X⟦n⟧` -/ notation -- Any better notational suggestions? X "⟦" n "⟧" => (shiftFunctor _ n).obj X set_option quotPrecheck false in /-- shifting a morphism `f` by `n` is obtained by the notation `f⟦n⟧'` -/ notation f "⟦" n "⟧'" => (shiftFunctor _ n).map f variable (C) variable [HasShift C A] lemma shiftFunctorAdd'_zero_add (a : A) : shiftFunctorAdd' C 0 a a (zero_add a) = (Functor.leftUnitor _).symm ≪≫ isoWhiskerRight (shiftFunctorZero C A).symm (shiftFunctor C a) := by ext X dsimp [shiftFunctorAdd', shiftFunctorZero, shiftFunctor] simp only [eqToHom_app, obj_ε_app, Discrete.addMonoidal_leftUnitor, eqToIso.inv, eqToHom_map, Category.id_comp] rfl lemma shiftFunctorAdd'_add_zero (a : A) : shiftFunctorAdd' C a 0 a (add_zero a) = (Functor.rightUnitor _).symm ≪≫ isoWhiskerLeft (shiftFunctor C a) (shiftFunctorZero C A).symm := by ext dsimp [shiftFunctorAdd', shiftFunctorZero, shiftFunctor] simp only [eqToHom_app, ε_app_obj, Discrete.addMonoidal_rightUnitor, eqToIso.inv, eqToHom_map, Category.id_comp] rfl lemma shiftFunctorAdd'_assoc (a₁ a₂ a₃ a₁₂ a₂₃ a₁₂₃ : A) (h₁₂ : a₁ + a₂ = a₁₂) (h₂₃ : a₂ + a₃ = a₂₃) (h₁₂₃ : a₁ + a₂ + a₃ = a₁₂₃) : shiftFunctorAdd' C a₁₂ a₃ a₁₂₃ (by rw [← h₁₂, h₁₂₃]) ≪≫ isoWhiskerRight (shiftFunctorAdd' C a₁ a₂ a₁₂ h₁₂) _ ≪≫ Functor.associator _ _ _ = shiftFunctorAdd' C a₁ a₂₃ a₁₂₃ (by rw [← h₂₃, ← add_assoc, h₁₂₃]) ≪≫ isoWhiskerLeft _ (shiftFunctorAdd' C a₂ a₃ a₂₃ h₂₃) := by subst h₁₂ h₂₃ h₁₂₃ ext X dsimp simp only [shiftFunctorAdd'_eq_shiftFunctorAdd, Category.comp_id] dsimp [shiftFunctorAdd'] simp only [eqToHom_app] dsimp [shiftFunctorAdd, shiftFunctor] simp only [obj_μ_inv_app, Discrete.addMonoidal_associator, eqToIso.hom, eqToHom_map, eqToHom_app] erw [δ_μ_app_assoc, Category.assoc] rfl lemma shiftFunctorAdd_assoc (a₁ a₂ a₃ : A) : shiftFunctorAdd C (a₁ + a₂) a₃ ≪≫ isoWhiskerRight (shiftFunctorAdd C a₁ a₂) _ ≪≫ Functor.associator _ _ _ = shiftFunctorAdd' C a₁ (a₂ + a₃) _ (add_assoc a₁ a₂ a₃).symm ≪≫ isoWhiskerLeft _ (shiftFunctorAdd C a₂ a₃) := by ext X simpa [shiftFunctorAdd'_eq_shiftFunctorAdd] using NatTrans.congr_app (congr_arg Iso.hom (shiftFunctorAdd'_assoc C a₁ a₂ a₃ _ _ _ rfl rfl rfl)) X variable {C} lemma shiftFunctorAdd'_zero_add_hom_app (a : A) (X : C) : (shiftFunctorAdd' C 0 a a (zero_add a)).hom.app X = ((shiftFunctorZero C A).inv.app X)⟦a⟧' := by simpa using NatTrans.congr_app (congr_arg Iso.hom (shiftFunctorAdd'_zero_add C a)) X lemma shiftFunctorAdd_zero_add_hom_app (a : A) (X : C) : (shiftFunctorAdd C 0 a).hom.app X = eqToHom (by dsimp; rw [zero_add]) ≫ ((shiftFunctorZero C A).inv.app X)⟦a⟧' := by simp [← shiftFunctorAdd'_zero_add_hom_app, shiftFunctorAdd'] lemma shiftFunctorAdd'_zero_add_inv_app (a : A) (X : C) : (shiftFunctorAdd' C 0 a a (zero_add a)).inv.app X = ((shiftFunctorZero C A).hom.app X)⟦a⟧' := by simpa using NatTrans.congr_app (congr_arg Iso.inv (shiftFunctorAdd'_zero_add C a)) X lemma shiftFunctorAdd_zero_add_inv_app (a : A) (X : C) : (shiftFunctorAdd C 0 a).inv.app X = ((shiftFunctorZero C A).hom.app X)⟦a⟧' ≫ eqToHom (by dsimp; rw [zero_add]) := by simp [← shiftFunctorAdd'_zero_add_inv_app, shiftFunctorAdd'] lemma shiftFunctorAdd'_add_zero_hom_app (a : A) (X : C) : (shiftFunctorAdd' C a 0 a (add_zero a)).hom.app X = (shiftFunctorZero C A).inv.app (X⟦a⟧) := by simpa using NatTrans.congr_app (congr_arg Iso.hom (shiftFunctorAdd'_add_zero C a)) X lemma shiftFunctorAdd_add_zero_hom_app (a : A) (X : C) : (shiftFunctorAdd C a 0).hom.app X = eqToHom (by dsimp; rw [add_zero]) ≫ (shiftFunctorZero C A).inv.app (X⟦a⟧) := by simp [← shiftFunctorAdd'_add_zero_hom_app, shiftFunctorAdd'] lemma shiftFunctorAdd'_add_zero_inv_app (a : A) (X : C) : (shiftFunctorAdd' C a 0 a (add_zero a)).inv.app X = (shiftFunctorZero C A).hom.app (X⟦a⟧) := by simpa using NatTrans.congr_app (congr_arg Iso.inv (shiftFunctorAdd'_add_zero C a)) X lemma shiftFunctorAdd_add_zero_inv_app (a : A) (X : C) : (shiftFunctorAdd C a 0).inv.app X = (shiftFunctorZero C A).hom.app (X⟦a⟧) ≫ eqToHom (by dsimp; rw [add_zero]) := by simp [← shiftFunctorAdd'_add_zero_inv_app, shiftFunctorAdd'] @[reassoc] lemma shiftFunctorAdd'_assoc_hom_app (a₁ a₂ a₃ a₁₂ a₂₃ a₁₂₃ : A) (h₁₂ : a₁ + a₂ = a₁₂) (h₂₃ : a₂ + a₃ = a₂₃) (h₁₂₃ : a₁ + a₂ + a₃ = a₁₂₃) (X : C) : (shiftFunctorAdd' C a₁₂ a₃ a₁₂₃ (by rw [← h₁₂, h₁₂₃])).hom.app X ≫ ((shiftFunctorAdd' C a₁ a₂ a₁₂ h₁₂).hom.app X)⟦a₃⟧' = (shiftFunctorAdd' C a₁ a₂₃ a₁₂₃ (by rw [← h₂₃, ← add_assoc, h₁₂₃])).hom.app X ≫ (shiftFunctorAdd' C a₂ a₃ a₂₃ h₂₃).hom.app (X⟦a₁⟧) := by simpa using NatTrans.congr_app (congr_arg Iso.hom (shiftFunctorAdd'_assoc C _ _ _ _ _ _ h₁₂ h₂₃ h₁₂₃)) X @[reassoc] lemma shiftFunctorAdd'_assoc_inv_app (a₁ a₂ a₃ a₁₂ a₂₃ a₁₂₃ : A) (h₁₂ : a₁ + a₂ = a₁₂) (h₂₃ : a₂ + a₃ = a₂₃) (h₁₂₃ : a₁ + a₂ + a₃ = a₁₂₃) (X : C) : ((shiftFunctorAdd' C a₁ a₂ a₁₂ h₁₂).inv.app X)⟦a₃⟧' ≫ (shiftFunctorAdd' C a₁₂ a₃ a₁₂₃ (by rw [← h₁₂, h₁₂₃])).inv.app X = (shiftFunctorAdd' C a₂ a₃ a₂₃ h₂₃).inv.app (X⟦a₁⟧) ≫ (shiftFunctorAdd' C a₁ a₂₃ a₁₂₃ (by rw [← h₂₃, ← add_assoc, h₁₂₃])).inv.app X := by simpa using NatTrans.congr_app (congr_arg Iso.inv (shiftFunctorAdd'_assoc C _ _ _ _ _ _ h₁₂ h₂₃ h₁₂₃)) X @[reassoc] lemma shiftFunctorAdd_assoc_hom_app (a₁ a₂ a₃ : A) (X : C) : (shiftFunctorAdd C (a₁ + a₂) a₃).hom.app X ≫ ((shiftFunctorAdd C a₁ a₂).hom.app X)⟦a₃⟧' = (shiftFunctorAdd' C a₁ (a₂ + a₃) (a₁ + a₂ + a₃) (add_assoc _ _ _).symm).hom.app X ≫ (shiftFunctorAdd C a₂ a₃).hom.app (X⟦a₁⟧) := by simpa using NatTrans.congr_app (congr_arg Iso.hom (shiftFunctorAdd_assoc C a₁ a₂ a₃)) X @[reassoc] lemma shiftFunctorAdd_assoc_inv_app (a₁ a₂ a₃ : A) (X : C) : ((shiftFunctorAdd C a₁ a₂).inv.app X)⟦a₃⟧' ≫ (shiftFunctorAdd C (a₁ + a₂) a₃).inv.app X = (shiftFunctorAdd C a₂ a₃).inv.app (X⟦a₁⟧) ≫ (shiftFunctorAdd' C a₁ (a₂ + a₃) (a₁ + a₂ + a₃) (add_assoc _ _ _).symm).inv.app X := by simpa using NatTrans.congr_app (congr_arg Iso.inv (shiftFunctorAdd_assoc C a₁ a₂ a₃)) X end Defs section AddMonoid variable [AddMonoid A] [HasShift C A] (X Y : C) (f : X ⟶ Y) --@[simp] --theorem HasShift.shift_obj_obj (n : A) (X : C) : (HasShift.shift.obj ⟨n⟩).obj X = X⟦n⟧ := -- rfl /-- Shifting by `i + j` is the same as shifting by `i` and then shifting by `j`. -/ abbrev shiftAdd (i j : A) : X⟦i + j⟧ ≅ X⟦i⟧⟦j⟧ := (shiftFunctorAdd C i j).app _ theorem shift_shift' (i j : A) : f⟦i⟧'⟦j⟧' = (shiftAdd X i j).inv ≫ f⟦i + j⟧' ≫ (shiftAdd Y i j).hom := by symm rw [← Functor.comp_map, Iso.app_inv] apply NatIso.naturality_1 variable (A) /-- Shifting by zero is the identity functor. -/ abbrev shiftZero : X⟦(0 : A)⟧ ≅ X := (shiftFunctorZero C A).app _ theorem shiftZero' : f⟦(0 : A)⟧' = (shiftZero A X).hom ≫ f ≫ (shiftZero A Y).inv := by
symm rw [Iso.app_inv, Iso.app_hom] apply NatIso.naturality_2 variable (C) {A} /-- When `i + j = 0`, shifting by `i` and by `j` gives the identity functor -/
Mathlib/CategoryTheory/Shift/Basic.lean
369
375
/- Copyright (c) 2024 Jz Pan. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jz Pan -/ import Mathlib.LinearAlgebra.Dimension.Free import Mathlib.LinearAlgebra.Dimension.Finite import Mathlib.LinearAlgebra.FreeModule.StrongRankCondition /-! # Some results on the ranks of subalgebras This file contains some results on the ranks of subalgebras, which are corollaries of `rank_mul_rank`. Since their proof essentially depends on the fact that a non-trivial commutative ring satisfies strong rank condition, we put them into a separate file. -/ open Module namespace Subalgebra variable {R S : Type*} [CommRing R] [CommRing S] [Algebra R S] (A B : Subalgebra R S) section variable [Module.Free R A] [Module.Free A (Algebra.adjoin A (B : Set S))] theorem rank_sup_eq_rank_left_mul_rank_of_free : Module.rank R ↥(A ⊔ B) = Module.rank R A * Module.rank A (Algebra.adjoin A (B : Set S)) := by rcases subsingleton_or_nontrivial R with _ | _ · haveI := Module.subsingleton R S; simp nontriviality S using rank_subsingleton' letI : Algebra A (Algebra.adjoin A (B : Set S)) := Subalgebra.algebra _ letI : SMul A (Algebra.adjoin A (B : Set S)) := Algebra.toSMul haveI : IsScalarTower R A (Algebra.adjoin A (B : Set S)) := IsScalarTower.of_algebraMap_eq (congrFun rfl) rw [rank_mul_rank R A (Algebra.adjoin A (B : Set S))] change _ = Module.rank R ((Algebra.adjoin A (B : Set S)).restrictScalars R) rw [Algebra.restrictScalars_adjoin]; rfl theorem finrank_sup_eq_finrank_left_mul_finrank_of_free : finrank R ↥(A ⊔ B) = finrank R A * finrank A (Algebra.adjoin A (B : Set S)) := by simpa only [map_mul] using congr(Cardinal.toNat $(rank_sup_eq_rank_left_mul_rank_of_free A B))
theorem finrank_left_dvd_finrank_sup_of_free : finrank R A ∣ finrank R ↥(A ⊔ B) := ⟨_, finrank_sup_eq_finrank_left_mul_finrank_of_free A B⟩
Mathlib/Algebra/Algebra/Subalgebra/Rank.lean
47
49
/- Copyright (c) 2019 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import Mathlib.Algebra.CharP.Defs import Mathlib.Algebra.GeomSum import Mathlib.Algebra.MvPolynomial.CommRing import Mathlib.Algebra.MvPolynomial.Equiv import Mathlib.Algebra.Polynomial.BigOperators import Mathlib.RingTheory.Noetherian.Basic /-! # Ring-theoretic supplement of Algebra.Polynomial. ## Main results * `MvPolynomial.isDomain`: If a ring is an integral domain, then so is its polynomial ring over finitely many variables. * `Polynomial.isNoetherianRing`: Hilbert basis theorem, that if a ring is noetherian then so is its polynomial ring. -/ noncomputable section open Polynomial open Finset universe u v w variable {R : Type u} {S : Type*} namespace Polynomial section Semiring variable [Semiring R] instance instCharP (p : ℕ) [h : CharP R p] : CharP R[X] p := let ⟨h⟩ := h ⟨fun n => by rw [← map_natCast C, ← C_0, C_inj, h]⟩ instance instExpChar (p : ℕ) [h : ExpChar R p] : ExpChar R[X] p := by cases h; exacts [ExpChar.zero, ExpChar.prime ‹_›] variable (R) /-- The `R`-submodule of `R[X]` consisting of polynomials of degree ≤ `n`. -/ def degreeLE (n : WithBot ℕ) : Submodule R R[X] := ⨅ k : ℕ, ⨅ _ : ↑k > n, LinearMap.ker (lcoeff R k) /-- The `R`-submodule of `R[X]` consisting of polynomials of degree < `n`. -/ def degreeLT (n : ℕ) : Submodule R R[X] := ⨅ k : ℕ, ⨅ (_ : k ≥ n), LinearMap.ker (lcoeff R k) variable {R} theorem mem_degreeLE {n : WithBot ℕ} {f : R[X]} : f ∈ degreeLE R n ↔ degree f ≤ n := by simp only [degreeLE, Submodule.mem_iInf, degree_le_iff_coeff_zero, LinearMap.mem_ker]; rfl @[mono] theorem degreeLE_mono {m n : WithBot ℕ} (H : m ≤ n) : degreeLE R m ≤ degreeLE R n := fun _ hf => mem_degreeLE.2 (le_trans (mem_degreeLE.1 hf) H) theorem degreeLE_eq_span_X_pow [DecidableEq R] {n : ℕ} : degreeLE R n = Submodule.span R ↑((Finset.range (n + 1)).image fun n => (X : R[X]) ^ n) := by apply le_antisymm · intro p hp replace hp := mem_degreeLE.1 hp rw [← Polynomial.sum_monomial_eq p, Polynomial.sum] refine Submodule.sum_mem _ fun k hk => ?_ have := WithBot.coe_le_coe.1 (Finset.sup_le_iff.1 hp k hk) rw [← C_mul_X_pow_eq_monomial, C_mul'] refine Submodule.smul_mem _ _ (Submodule.subset_span <| Finset.mem_coe.2 <| Finset.mem_image.2 ⟨_, Finset.mem_range.2 (Nat.lt_succ_of_le this), rfl⟩) rw [Submodule.span_le, Finset.coe_image, Set.image_subset_iff] intro k hk apply mem_degreeLE.2 exact (degree_X_pow_le _).trans (WithBot.coe_le_coe.2 <| Nat.le_of_lt_succ <| Finset.mem_range.1 hk) theorem mem_degreeLT {n : ℕ} {f : R[X]} : f ∈ degreeLT R n ↔ degree f < n := by rw [degreeLT, Submodule.mem_iInf] conv_lhs => intro i; rw [Submodule.mem_iInf] rw [degree, Finset.max_eq_sup_coe] rw [Finset.sup_lt_iff ?_] rotate_left · apply WithBot.bot_lt_coe conv_rhs => simp only [mem_support_iff] intro b rw [Nat.cast_withBot, WithBot.coe_lt_coe, lt_iff_not_le, Ne, not_imp_not] rfl @[mono] theorem degreeLT_mono {m n : ℕ} (H : m ≤ n) : degreeLT R m ≤ degreeLT R n := fun _ hf => mem_degreeLT.2 (lt_of_lt_of_le (mem_degreeLT.1 hf) <| WithBot.coe_le_coe.2 H) theorem degreeLT_eq_span_X_pow [DecidableEq R] {n : ℕ} : degreeLT R n = Submodule.span R ↑((Finset.range n).image fun n => X ^ n : Finset R[X]) := by apply le_antisymm · intro p hp replace hp := mem_degreeLT.1 hp rw [← Polynomial.sum_monomial_eq p, Polynomial.sum] refine Submodule.sum_mem _ fun k hk => ?_ have := WithBot.coe_lt_coe.1 ((Finset.sup_lt_iff <| WithBot.bot_lt_coe n).1 hp k hk) rw [← C_mul_X_pow_eq_monomial, C_mul'] refine Submodule.smul_mem _ _ (Submodule.subset_span <| Finset.mem_coe.2 <| Finset.mem_image.2 ⟨_, Finset.mem_range.2 this, rfl⟩) rw [Submodule.span_le, Finset.coe_image, Set.image_subset_iff] intro k hk apply mem_degreeLT.2 exact lt_of_le_of_lt (degree_X_pow_le _) (WithBot.coe_lt_coe.2 <| Finset.mem_range.1 hk) /-- The first `n` coefficients on `degreeLT n` form a linear equivalence with `Fin n → R`. -/ def degreeLTEquiv (R) [Semiring R] (n : ℕ) : degreeLT R n ≃ₗ[R] Fin n → R where toFun p n := (↑p : R[X]).coeff n invFun f := ⟨∑ i : Fin n, monomial i (f i), (degreeLT R n).sum_mem fun i _ => mem_degreeLT.mpr (lt_of_le_of_lt (degree_monomial_le i (f i)) (WithBot.coe_lt_coe.mpr i.is_lt))⟩ map_add' p q := by ext dsimp rw [coeff_add] map_smul' x p := by ext dsimp rw [coeff_smul] rfl left_inv := by rintro ⟨p, hp⟩ ext1 simp only [Submodule.coe_mk] by_cases hp0 : p = 0 · subst hp0 simp only [coeff_zero, LinearMap.map_zero, Finset.sum_const_zero] rw [mem_degreeLT, degree_eq_natDegree hp0, Nat.cast_lt] at hp conv_rhs => rw [p.as_sum_range' n hp, ← Fin.sum_univ_eq_sum_range] right_inv f := by ext i simp only [finset_sum_coeff, Submodule.coe_mk] rw [Finset.sum_eq_single i, coeff_monomial, if_pos rfl] · rintro j - hji rw [coeff_monomial, if_neg] rwa [← Fin.ext_iff] · intro h exact (h (Finset.mem_univ _)).elim theorem degreeLTEquiv_eq_zero_iff_eq_zero {n : ℕ} {p : R[X]} (hp : p ∈ degreeLT R n) : degreeLTEquiv _ _ ⟨p, hp⟩ = 0 ↔ p = 0 := by simp theorem eval_eq_sum_degreeLTEquiv {n : ℕ} {p : R[X]} (hp : p ∈ degreeLT R n) (x : R) : p.eval x = ∑ i, degreeLTEquiv _ _ ⟨p, hp⟩ i * x ^ (i : ℕ) := by simp_rw [eval_eq_sum] exact (sum_fin _ (by simp_rw [zero_mul, forall_const]) (mem_degreeLT.mp hp)).symm theorem degreeLT_succ_eq_degreeLE {n : ℕ} : degreeLT R (n + 1) = degreeLE R n := by ext x by_cases x_zero : x = 0 · simp_rw [x_zero, Submodule.zero_mem] · rw [mem_degreeLT, mem_degreeLE, ← natDegree_lt_iff_degree_lt (by rwa [ne_eq]), ← natDegree_le_iff_degree_le, Nat.lt_succ] /-- The equivalence between monic polynomials of degree `n` and polynomials of degree less than `n`, formed by adding a term `X ^ n`. -/ def monicEquivDegreeLT [Nontrivial R] (n : ℕ) : { p : R[X] // p.Monic ∧ p.natDegree = n } ≃ degreeLT R n where toFun p := ⟨p.1.eraseLead, by rcases p with ⟨p, hp, rfl⟩ simp only [mem_degreeLT] refine lt_of_lt_of_le ?_ degree_le_natDegree exact degree_eraseLead_lt (ne_zero_of_ne_zero_of_monic one_ne_zero hp)⟩ invFun := fun p => ⟨X^n + p.1, monic_X_pow_add (mem_degreeLT.1 p.2), by rw [natDegree_add_eq_left_of_degree_lt] · simp · simp [mem_degreeLT.1 p.2]⟩ left_inv := by rintro ⟨p, hp, rfl⟩ ext1 simp only conv_rhs => rw [← eraseLead_add_C_mul_X_pow p] simp [Monic.def.1 hp, add_comm] right_inv := by rintro ⟨p, hp⟩ ext1 simp only rw [eraseLead_add_of_degree_lt_left] · simp · simp [mem_degreeLT.1 hp] /-- For every polynomial `p` in the span of a set `s : Set R[X]`, there exists a polynomial of `p' ∈ s` with higher degree. See also `Polynomial.exists_degree_le_of_mem_span_of_finite`. -/ theorem exists_degree_le_of_mem_span {s : Set R[X]} {p : R[X]} (hs : s.Nonempty) (hp : p ∈ Submodule.span R s) : ∃ p' ∈ s, degree p ≤ degree p' := by by_contra! h by_cases hp_zero : p = 0 · rw [hp_zero, degree_zero] at h rcases hs with ⟨x, hx⟩ exact not_lt_bot (h x hx) · have : p ∈ degreeLT R (natDegree p) := by refine (Submodule.span_le.mpr fun p' p'_mem => ?_) hp rw [SetLike.mem_coe, mem_degreeLT, Nat.cast_withBot] exact lt_of_lt_of_le (h p' p'_mem) degree_le_natDegree rwa [mem_degreeLT, Nat.cast_withBot, degree_eq_natDegree hp_zero, Nat.cast_withBot, lt_self_iff_false] at this /-- A stronger version of `Polynomial.exists_degree_le_of_mem_span` under the assumption that the set `s : R[X]` is finite. There exists a polynomial `p' ∈ s` whose degree dominates the degree of every element of `p ∈ span R s`. -/ theorem exists_degree_le_of_mem_span_of_finite {s : Set R[X]} (s_fin : s.Finite) (hs : s.Nonempty) : ∃ p' ∈ s, ∀ (p : R[X]), p ∈ Submodule.span R s → degree p ≤ degree p' := by rcases Set.Finite.exists_maximal_wrt degree s s_fin hs with ⟨a, has, hmax⟩ refine ⟨a, has, fun p hp => ?_⟩ rcases exists_degree_le_of_mem_span hs hp with ⟨p', hp'⟩ by_cases h : degree a ≤ degree p' · rw [← hmax p' hp'.left h] at hp'; exact hp'.right · exact le_trans hp'.right (not_le.mp h).le /-- The span of every finite set of polynomials is contained in a `degreeLE n` for some `n`. -/ theorem span_le_degreeLE_of_finite {s : Set R[X]} (s_fin : s.Finite) : ∃ n : ℕ, Submodule.span R s ≤ degreeLE R n := by by_cases s_emp : s.Nonempty · rcases exists_degree_le_of_mem_span_of_finite s_fin s_emp with ⟨p', _, hp'max⟩ exact ⟨natDegree p', fun p hp => mem_degreeLE.mpr ((hp'max _ hp).trans degree_le_natDegree)⟩ · rw [Set.not_nonempty_iff_eq_empty] at s_emp rw [s_emp, Submodule.span_empty] exact ⟨0, bot_le⟩ /-- The span of every finite set of polynomials is contained in a `degreeLT n` for some `n`. -/ theorem span_of_finite_le_degreeLT {s : Set R[X]} (s_fin : s.Finite) : ∃ n : ℕ, Submodule.span R s ≤ degreeLT R n := by rcases span_le_degreeLE_of_finite s_fin with ⟨n, _⟩ exact ⟨n + 1, by rwa [degreeLT_succ_eq_degreeLE]⟩ /-- If `R` is a nontrivial ring, the polynomials `R[X]` are not finite as an `R`-module. When `R` is a field, this is equivalent to `R[X]` being an infinite-dimensional vector space over `R`. -/ theorem not_finite [Nontrivial R] : ¬ Module.Finite R R[X] := by rw [Module.finite_def, Submodule.fg_def] push_neg intro s hs contra rcases span_le_degreeLE_of_finite hs with ⟨n,hn⟩ have : ((X : R[X]) ^ (n + 1)) ∈ Polynomial.degreeLE R ↑n := by rw [contra] at hn exact hn Submodule.mem_top rw [mem_degreeLE, degree_X_pow, Nat.cast_le, add_le_iff_nonpos_right, nonpos_iff_eq_zero] at this exact one_ne_zero this theorem geom_sum_X_comp_X_add_one_eq_sum (n : ℕ) : (∑ i ∈ range n, (X : R[X]) ^ i).comp (X + 1) = (Finset.range n).sum fun i : ℕ => (n.choose (i + 1) : R[X]) * X ^ i := by ext i trans (n.choose (i + 1) : R); swap · simp only [finset_sum_coeff, ← C_eq_natCast, coeff_C_mul_X_pow] rw [Finset.sum_eq_single i, if_pos rfl] · simp +contextual only [@eq_comm _ i, if_false, eq_self_iff_true, imp_true_iff] · simp +contextual only [Nat.lt_add_one_iff, Nat.choose_eq_zero_of_lt, Nat.cast_zero, Finset.mem_range, not_lt, eq_self_iff_true, if_true, imp_true_iff] induction' n with n ih generalizing i · dsimp; simp only [zero_comp, coeff_zero, Nat.cast_zero] · simp only [geom_sum_succ', ih, add_comp, X_pow_comp, coeff_add, Nat.choose_succ_succ, Nat.cast_add, coeff_X_add_one_pow] theorem Monic.geom_sum {P : R[X]} (hP : P.Monic) (hdeg : 0 < P.natDegree) {n : ℕ} (hn : n ≠ 0) : (∑ i ∈ range n, P ^ i).Monic := by nontriviality R obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero hn rw [geom_sum_succ'] refine (hP.pow _).add_of_left ?_ refine lt_of_le_of_lt (degree_sum_le _ _) ?_ rw [Finset.sup_lt_iff] · simp only [Finset.mem_range, degree_eq_natDegree (hP.pow _).ne_zero] simp only [Nat.cast_lt, hP.natDegree_pow] intro k exact nsmul_lt_nsmul_left hdeg · rw [bot_lt_iff_ne_bot, Ne, degree_eq_bot] exact (hP.pow _).ne_zero theorem Monic.geom_sum' {P : R[X]} (hP : P.Monic) (hdeg : 0 < P.degree) {n : ℕ} (hn : n ≠ 0) : (∑ i ∈ range n, P ^ i).Monic := hP.geom_sum (natDegree_pos_iff_degree_pos.2 hdeg) hn theorem monic_geom_sum_X {n : ℕ} (hn : n ≠ 0) : (∑ i ∈ range n, (X : R[X]) ^ i).Monic := by nontriviality R apply monic_X.geom_sum _ hn simp only [natDegree_X, zero_lt_one] end Semiring section Ring variable [Ring R] /-- Given a polynomial, return the polynomial whose coefficients are in the ring closure of the original coefficients. -/ def restriction (p : R[X]) : Polynomial (Subring.closure (↑p.coeffs : Set R)) := ∑ i ∈ p.support, monomial i (⟨p.coeff i, letI := Classical.decEq R if H : p.coeff i = 0 then H.symm ▸ (Subring.closure _).zero_mem else Subring.subset_closure (p.coeff_mem_coeffs _ H)⟩ : Subring.closure (↑p.coeffs : Set R)) @[simp] theorem coeff_restriction {p : R[X]} {n : ℕ} : ↑(coeff (restriction p) n) = coeff p n := by classical simp only [restriction, coeff_monomial, finset_sum_coeff, mem_support_iff, Finset.sum_ite_eq', Ne, ite_not] split_ifs with h · rw [h] rfl · rfl theorem coeff_restriction' {p : R[X]} {n : ℕ} : (coeff (restriction p) n).1 = coeff p n := by simp @[simp] theorem support_restriction (p : R[X]) : support (restriction p) = support p := by ext i simp only [mem_support_iff, not_iff_not, Ne] conv_rhs => rw [← coeff_restriction] exact ⟨fun H => by rw [H, ZeroMemClass.coe_zero], fun H => Subtype.coe_injective H⟩ @[simp] theorem map_restriction {R : Type u} [CommRing R] (p : R[X]) : p.restriction.map (algebraMap _ _) = p := ext fun n => by rw [coeff_map, Algebra.algebraMap_ofSubring_apply, coeff_restriction] @[simp] theorem degree_restriction {p : R[X]} : (restriction p).degree = p.degree := by simp [degree] @[simp] theorem natDegree_restriction {p : R[X]} : (restriction p).natDegree = p.natDegree := by simp [natDegree] @[simp] theorem monic_restriction {p : R[X]} : Monic (restriction p) ↔ Monic p := by simp only [Monic, leadingCoeff, natDegree_restriction] rw [← @coeff_restriction _ _ p] exact ⟨fun H => by rw [H, OneMemClass.coe_one], fun H => Subtype.coe_injective H⟩ @[simp] theorem restriction_zero : restriction (0 : R[X]) = 0 := by simp only [restriction, Finset.sum_empty, support_zero] @[simp] theorem restriction_one : restriction (1 : R[X]) = 1 := ext fun i => Subtype.eq <| by rw [coeff_restriction', coeff_one, coeff_one]; split_ifs <;> rfl variable [Semiring S] {f : R →+* S} {x : S} theorem eval₂_restriction {p : R[X]} : eval₂ f x p = eval₂ (f.comp (Subring.subtype (Subring.closure (p.coeffs : Set R)))) x p.restriction := by simp only [eval₂_eq_sum, sum, support_restriction, ← @coeff_restriction _ _ p, RingHom.comp_apply, Subring.coe_subtype] section ToSubring variable (p : R[X]) (T : Subring R) /-- Given a polynomial `p` and a subring `T` that contains the coefficients of `p`, return the corresponding polynomial whose coefficients are in `T`. -/ def toSubring (hp : (↑p.coeffs : Set R) ⊆ T) : T[X] := ∑ i ∈ p.support, monomial i (⟨p.coeff i, letI := Classical.decEq R if H : p.coeff i = 0 then H.symm ▸ T.zero_mem else hp (p.coeff_mem_coeffs _ H)⟩ : T) variable (hp : (↑p.coeffs : Set R) ⊆ T) @[simp] theorem coeff_toSubring {n : ℕ} : ↑(coeff (toSubring p T hp) n) = coeff p n := by classical simp only [toSubring, coeff_monomial, finset_sum_coeff, mem_support_iff, Finset.sum_ite_eq', Ne, ite_not] split_ifs with h · rw [h] rfl · rfl theorem coeff_toSubring' {n : ℕ} : (coeff (toSubring p T hp) n).1 = coeff p n := by simp @[simp] theorem support_toSubring : support (toSubring p T hp) = support p := by ext i simp only [mem_support_iff, not_iff_not, Ne] conv_rhs => rw [← coeff_toSubring p T hp] exact ⟨fun H => by rw [H, ZeroMemClass.coe_zero], fun H => Subtype.coe_injective H⟩ @[simp] theorem degree_toSubring : (toSubring p T hp).degree = p.degree := by simp [degree] @[simp] theorem natDegree_toSubring : (toSubring p T hp).natDegree = p.natDegree := by simp [natDegree] @[simp] theorem monic_toSubring : Monic (toSubring p T hp) ↔ Monic p := by simp_rw [Monic, leadingCoeff, natDegree_toSubring, ← coeff_toSubring p T hp] exact ⟨fun H => by rw [H, OneMemClass.coe_one], fun H => Subtype.coe_injective H⟩ @[simp] theorem toSubring_zero : toSubring (0 : R[X]) T (by simp [coeffs]) = 0 := by ext i simp @[simp] theorem toSubring_one : toSubring (1 : R[X]) T (Set.Subset.trans coeffs_one <| Finset.singleton_subset_set_iff.2 T.one_mem) = 1 := ext fun i => Subtype.eq <| by rw [coeff_toSubring', coeff_one, coeff_one, apply_ite Subtype.val, ZeroMemClass.coe_zero, OneMemClass.coe_one] @[simp] theorem map_toSubring : (p.toSubring T hp).map (Subring.subtype T) = p := by ext n simp [coeff_map] end ToSubring variable (T : Subring R) /-- Given a polynomial whose coefficients are in some subring, return the corresponding polynomial whose coefficients are in the ambient ring. -/ def ofSubring (p : T[X]) : R[X] := ∑ i ∈ p.support, monomial i (p.coeff i : R) theorem coeff_ofSubring (p : T[X]) (n : ℕ) : coeff (ofSubring T p) n = (coeff p n : T) := by simp only [ofSubring, coeff_monomial, finset_sum_coeff, mem_support_iff, Finset.sum_ite_eq', ite_eq_right_iff, Ne, ite_not, Classical.not_not, ite_eq_left_iff] intro h rw [h, ZeroMemClass.coe_zero] @[simp] theorem coeffs_ofSubring {p : T[X]} : (↑(p.ofSubring T).coeffs : Set R) ⊆ T := by classical intro i hi simp only [coeffs, Set.mem_image, mem_support_iff, Ne, Finset.mem_coe, (Finset.coe_image)] at hi rcases hi with ⟨n, _, h'n⟩ rw [← h'n, coeff_ofSubring] exact Subtype.mem (coeff p n : T) end Ring end Polynomial namespace Ideal open Polynomial section Semiring variable [Semiring R] /-- Transport an ideal of `R[X]` to an `R`-submodule of `R[X]`. -/ def ofPolynomial (I : Ideal R[X]) : Submodule R R[X] where carrier := I.carrier zero_mem' := I.zero_mem add_mem' := I.add_mem smul_mem' c x H := by rw [← C_mul'] exact I.mul_mem_left _ H variable {I : Ideal R[X]} theorem mem_ofPolynomial (x) : x ∈ I.ofPolynomial ↔ x ∈ I := Iff.rfl variable (I) /-- Given an ideal `I` of `R[X]`, make the `R`-submodule of `I` consisting of polynomials of degree ≤ `n`. -/ def degreeLE (n : WithBot ℕ) : Submodule R R[X] := Polynomial.degreeLE R n ⊓ I.ofPolynomial /-- Given an ideal `I` of `R[X]`, make the ideal in `R` of leading coefficients of polynomials in `I` with degree ≤ `n`. -/ def leadingCoeffNth (n : ℕ) : Ideal R := (I.degreeLE n).map <| lcoeff R n /-- Given an ideal `I` in `R[X]`, make the ideal in `R` of the leading coefficients in `I`. -/ def leadingCoeff : Ideal R := ⨆ n : ℕ, I.leadingCoeffNth n end Semiring section CommSemiring variable [CommSemiring R] [Semiring S] /-- If every coefficient of a polynomial is in an ideal `I`, then so is the polynomial itself -/ theorem polynomial_mem_ideal_of_coeff_mem_ideal (I : Ideal R[X]) (p : R[X]) (hp : ∀ n : ℕ, p.coeff n ∈ I.comap (C : R →+* R[X])) : p ∈ I := sum_C_mul_X_pow_eq p ▸ Submodule.sum_mem I fun n _ => I.mul_mem_right _ (hp n) /-- The push-forward of an ideal `I` of `R` to `R[X]` via inclusion is exactly the set of polynomials whose coefficients are in `I` -/ theorem mem_map_C_iff {I : Ideal R} {f : R[X]} : f ∈ (Ideal.map (C : R →+* R[X]) I : Ideal R[X]) ↔ ∀ n : ℕ, f.coeff n ∈ I := by constructor · intro hf refine Submodule.span_induction ?_ ?_ ?_ ?_ hf · intro f hf n obtain ⟨x, hx⟩ := (Set.mem_image _ _ _).mp hf rw [← hx.right, coeff_C] by_cases h : n = 0 · simpa [h] using hx.left · simp [h] · simp · exact fun f g _ _ hf hg n => by simp [I.add_mem (hf n) (hg n)] · refine fun f g _ hg n => ?_ rw [smul_eq_mul, coeff_mul] exact I.sum_mem fun c _ => I.mul_mem_left (f.coeff c.fst) (hg c.snd) · intro hf rw [← sum_monomial_eq f] refine (I.map C : Ideal R[X]).sum_mem fun n _ => ?_ simp only [← C_mul_X_pow_eq_monomial, ne_eq] rw [mul_comm] exact (I.map C : Ideal R[X]).mul_mem_left _ (mem_map_of_mem _ (hf n)) theorem _root_.Polynomial.ker_mapRingHom (f : R →+* S) : RingHom.ker (Polynomial.mapRingHom f) = (RingHom.ker f).map (C : R →+* R[X]) := by ext simp only [RingHom.mem_ker, coe_mapRingHom] rw [mem_map_C_iff, Polynomial.ext_iff] simp [RingHom.mem_ker] variable (I : Ideal R[X]) theorem mem_leadingCoeffNth (n : ℕ) (x) : x ∈ I.leadingCoeffNth n ↔ ∃ p ∈ I, degree p ≤ n ∧ p.leadingCoeff = x := by simp only [leadingCoeffNth, degreeLE, Submodule.mem_map, lcoeff_apply, Submodule.mem_inf, mem_degreeLE] constructor · rintro ⟨p, ⟨hpdeg, hpI⟩, rfl⟩ rcases lt_or_eq_of_le hpdeg with hpdeg | hpdeg · refine ⟨0, I.zero_mem, bot_le, ?_⟩ rw [leadingCoeff_zero, eq_comm] exact coeff_eq_zero_of_degree_lt hpdeg · refine ⟨p, hpI, le_of_eq hpdeg, ?_⟩ rw [Polynomial.leadingCoeff, natDegree, hpdeg, Nat.cast_withBot, WithBot.unbotD_coe] · rintro ⟨p, hpI, hpdeg, rfl⟩ have : natDegree p + (n - natDegree p) = n := add_tsub_cancel_of_le (natDegree_le_of_degree_le hpdeg) refine ⟨p * X ^ (n - natDegree p), ⟨?_, I.mul_mem_right _ hpI⟩, ?_⟩ · apply le_trans (degree_mul_le _ _) _ apply le_trans (add_le_add degree_le_natDegree (degree_X_pow_le _)) _ rw [← Nat.cast_add, this] · rw [Polynomial.leadingCoeff, ← coeff_mul_X_pow p (n - natDegree p), this] theorem mem_leadingCoeffNth_zero (x) : x ∈ I.leadingCoeffNth 0 ↔ C x ∈ I := (mem_leadingCoeffNth _ _ _).trans ⟨fun ⟨p, hpI, hpdeg, hpx⟩ => by rwa [← hpx, Polynomial.leadingCoeff, Nat.eq_zero_of_le_zero (natDegree_le_of_degree_le hpdeg), ← eq_C_of_degree_le_zero hpdeg], fun hx => ⟨C x, hx, degree_C_le, leadingCoeff_C x⟩⟩ theorem leadingCoeffNth_mono {m n : ℕ} (H : m ≤ n) : I.leadingCoeffNth m ≤ I.leadingCoeffNth n := by intro r hr simp only [SetLike.mem_coe, mem_leadingCoeffNth] at hr ⊢ rcases hr with ⟨p, hpI, hpdeg, rfl⟩ refine ⟨p * X ^ (n - m), I.mul_mem_right _ hpI, ?_, leadingCoeff_mul_X_pow⟩ refine le_trans (degree_mul_le _ _) ?_ refine le_trans (add_le_add hpdeg (degree_X_pow_le _)) ?_ rw [← Nat.cast_add, add_tsub_cancel_of_le H] theorem mem_leadingCoeff (x) : x ∈ I.leadingCoeff ↔ ∃ p ∈ I, Polynomial.leadingCoeff p = x := by rw [leadingCoeff, Submodule.mem_iSup_of_directed] · simp only [mem_leadingCoeffNth] constructor · rintro ⟨i, p, hpI, _, rfl⟩ exact ⟨p, hpI, rfl⟩ rintro ⟨p, hpI, rfl⟩ exact ⟨natDegree p, p, hpI, degree_le_natDegree, rfl⟩ intro i j exact ⟨i + j, I.leadingCoeffNth_mono (Nat.le_add_right _ _), I.leadingCoeffNth_mono (Nat.le_add_left _ _)⟩ /-- If `I` is an ideal, and `pᵢ` is a finite family of polynomials each satisfying `∀ k, (pᵢ)ₖ ∈ Iⁿⁱ⁻ᵏ` for some `nᵢ`, then `p = ∏ pᵢ` also satisfies `∀ k, pₖ ∈ Iⁿ⁻ᵏ` with `n = ∑ nᵢ`. -/ theorem _root_.Polynomial.coeff_prod_mem_ideal_pow_tsub {ι : Type*} (s : Finset ι) (f : ι → R[X]) (I : Ideal R) (n : ι → ℕ) (h : ∀ i ∈ s, ∀ (k), (f i).coeff k ∈ I ^ (n i - k)) (k : ℕ) : (s.prod f).coeff k ∈ I ^ (s.sum n - k) := by classical induction' s using Finset.induction with a s ha hs generalizing k · rw [sum_empty, prod_empty, coeff_one, zero_tsub, pow_zero, Ideal.one_eq_top] exact Submodule.mem_top · rw [sum_insert ha, prod_insert ha, coeff_mul] apply sum_mem rintro ⟨i, j⟩ e obtain rfl : i + j = k := mem_antidiagonal.mp e apply Ideal.pow_le_pow_right add_tsub_add_le_tsub_add_tsub rw [pow_add] exact Ideal.mul_mem_mul (h _ (Finset.mem_insert.mpr <| Or.inl rfl) _) (hs (fun i hi k => h _ (Finset.mem_insert.mpr <| Or.inr hi) _) j) end CommSemiring section Ring variable [Ring R] /-- `R[X]` is never a field for any ring `R`. -/ theorem polynomial_not_isField : ¬IsField R[X] := by nontriviality R intro hR obtain ⟨p, hp⟩ := hR.mul_inv_cancel X_ne_zero have hp0 : p ≠ 0 := right_ne_zero_of_mul_eq_one hp have := degree_lt_degree_mul_X hp0 rw [← X_mul, congr_arg degree hp, degree_one, Nat.WithBot.lt_zero_iff, degree_eq_bot] at this exact hp0 this /-- The only constant in a maximal ideal over a field is `0`. -/ theorem eq_zero_of_constant_mem_of_maximal (hR : IsField R) (I : Ideal R[X]) [hI : I.IsMaximal] (x : R) (hx : C x ∈ I) : x = 0 := by refine Classical.by_contradiction fun hx0 => hI.ne_top ((eq_top_iff_one I).2 ?_) obtain ⟨y, hy⟩ := hR.mul_inv_cancel hx0 convert I.mul_mem_left (C y) hx rw [← C.map_mul, hR.mul_comm y x, hy, RingHom.map_one] end Ring section CommRing variable [CommRing R] /-- If `P` is a prime ideal of `R`, then `P.R[x]` is a prime ideal of `R[x]`. -/ theorem isPrime_map_C_iff_isPrime (P : Ideal R) : IsPrime (map (C : R →+* R[X]) P : Ideal R[X]) ↔ IsPrime P := by -- Note: the following proof avoids quotient rings -- It can be golfed substantially by using something like -- `(Quotient.isDomain_iff_prime (map C P : Ideal R[X]))` constructor · intro H have := comap_isPrime C (map C P) convert this using 1 ext x simp only [mem_comap, mem_map_C_iff] constructor · rintro h (- | n) · rwa [coeff_C_zero] · simp only [coeff_C_ne_zero (Nat.succ_ne_zero _), Submodule.zero_mem] · intro h simpa only [coeff_C_zero] using h 0 · intro h
constructor · rw [Ne, eq_top_iff_one, mem_map_C_iff, not_forall] use 0 rw [coeff_one_zero, ← eq_top_iff_one] exact h.1 · intro f g
Mathlib/RingTheory/Polynomial/Basic.lean
665
670
/- Copyright (c) 2021 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import Mathlib.Order.Lattice import Mathlib.Data.List.Sort import Mathlib.Logic.Equiv.Fin.Basic import Mathlib.Logic.Equiv.Functor import Mathlib.Data.Fintype.Pigeonhole import Mathlib.Order.RelSeries /-! # Jordan-Hölder Theorem This file proves the Jordan Hölder theorem for a `JordanHolderLattice`, a class also defined in this file. Examples of `JordanHolderLattice` include `Subgroup G` if `G` is a group, and `Submodule R M` if `M` is an `R`-module. Using this approach the theorem need not be proved separately for both groups and modules, the proof in this file can be applied to both. ## Main definitions The main definitions in this file are `JordanHolderLattice` and `CompositionSeries`, and the relation `Equivalent` on `CompositionSeries` A `JordanHolderLattice` is the class for which the Jordan Hölder theorem is proved. A Jordan Hölder lattice is a lattice equipped with a notion of maximality, `IsMaximal`, and a notion of isomorphism of pairs `Iso`. In the example of subgroups of a group, `IsMaximal H K` means that `H` is a maximal normal subgroup of `K`, and `Iso (H₁, K₁) (H₂, K₂)` means that the quotient `H₁ / K₁` is isomorphic to the quotient `H₂ / K₂`. `Iso` must be symmetric and transitive and must satisfy the second isomorphism theorem `Iso (H, H ⊔ K) (H ⊓ K, K)`. A `CompositionSeries X` is a finite nonempty series of elements of the lattice `X` such that each element is maximal inside the next. The length of a `CompositionSeries X` is one less than the number of elements in the series. Note that there is no stipulation that a series start from the bottom of the lattice and finish at the top. For a composition series `s`, `s.last` is the largest element of the series, and `s.head` is the least element. Two `CompositionSeries X`, `s₁` and `s₂` are equivalent if there is a bijection `e : Fin s₁.length ≃ Fin s₂.length` such that for any `i`, `Iso (s₁ i, s₁ i.succ) (s₂ (e i), s₂ (e i.succ))` ## Main theorems The main theorem is `CompositionSeries.jordan_holder`, which says that if two composition series have the same least element and the same largest element, then they are `Equivalent`. ## TODO Provide instances of `JordanHolderLattice` for subgroups, and potentially for modular lattices. It is not entirely clear how this should be done. Possibly there should be no global instances of `JordanHolderLattice`, and the instances should only be defined locally in order to prove the Jordan-Hölder theorem for modules/groups and the API should be transferred because many of the theorems in this file will have stronger versions for modules. There will also need to be an API for mapping composition series across homomorphisms. It is also probably possible to provide an instance of `JordanHolderLattice` for any `ModularLattice`, and in this case the Jordan-Hölder theorem will say that there is a well defined notion of length of a modular lattice. However an instance of `JordanHolderLattice` for a modular lattice will not be able to contain the correct notion of isomorphism for modules, so a separate instance for modules will still be required and this will clash with the instance for modular lattices, and so at least one of these instances should not be a global instance. > [!NOTE] > The previous paragraph indicates that the instance of `JordanHolderLattice` for submodules should > be obtained via `ModularLattice`. This is not the case in `mathlib4`. > See `JordanHolderModule.instJordanHolderLattice`. -/ universe u open Set RelSeries /-- A `JordanHolderLattice` is the class for which the Jordan Hölder theorem is proved. A Jordan Hölder lattice is a lattice equipped with a notion of maximality, `IsMaximal`, and a notion of isomorphism of pairs `Iso`. In the example of subgroups of a group, `IsMaximal H K` means that `H` is a maximal normal subgroup of `K`, and `Iso (H₁, K₁) (H₂, K₂)` means that the quotient `H₁ / K₁` is isomorphic to the quotient `H₂ / K₂`. `Iso` must be symmetric and transitive and must satisfy the second isomorphism theorem `Iso (H, H ⊔ K) (H ⊓ K, K)`. Examples include `Subgroup G` if `G` is a group, and `Submodule R M` if `M` is an `R`-module. -/ class JordanHolderLattice (X : Type u) [Lattice X] where IsMaximal : X → X → Prop lt_of_isMaximal : ∀ {x y}, IsMaximal x y → x < y sup_eq_of_isMaximal : ∀ {x y z}, IsMaximal x z → IsMaximal y z → x ≠ y → x ⊔ y = z isMaximal_inf_left_of_isMaximal_sup : ∀ {x y}, IsMaximal x (x ⊔ y) → IsMaximal y (x ⊔ y) → IsMaximal (x ⊓ y) x Iso : X × X → X × X → Prop iso_symm : ∀ {x y}, Iso x y → Iso y x iso_trans : ∀ {x y z}, Iso x y → Iso y z → Iso x z second_iso : ∀ {x y}, IsMaximal x (x ⊔ y) → Iso (x, x ⊔ y) (x ⊓ y, y) namespace JordanHolderLattice variable {X : Type u} [Lattice X] [JordanHolderLattice X] theorem isMaximal_inf_right_of_isMaximal_sup {x y : X} (hxz : IsMaximal x (x ⊔ y)) (hyz : IsMaximal y (x ⊔ y)) : IsMaximal (x ⊓ y) y := by rw [inf_comm] rw [sup_comm] at hxz hyz exact isMaximal_inf_left_of_isMaximal_sup hyz hxz theorem isMaximal_of_eq_inf (x b : X) {a y : X} (ha : x ⊓ y = a) (hxy : x ≠ y) (hxb : IsMaximal x b) (hyb : IsMaximal y b) : IsMaximal a y := by have hb : x ⊔ y = b := sup_eq_of_isMaximal hxb hyb hxy substs a b exact isMaximal_inf_right_of_isMaximal_sup hxb hyb theorem second_iso_of_eq {x y a b : X} (hm : IsMaximal x a) (ha : x ⊔ y = a) (hb : x ⊓ y = b) : Iso (x, a) (b, y) := by substs a b; exact second_iso hm theorem IsMaximal.iso_refl {x y : X} (h : IsMaximal x y) : Iso (x, y) (x, y) := second_iso_of_eq h (sup_eq_right.2 (le_of_lt (lt_of_isMaximal h))) (inf_eq_left.2 (le_of_lt (lt_of_isMaximal h))) end JordanHolderLattice open JordanHolderLattice attribute [symm] iso_symm attribute [trans] iso_trans /-- A `CompositionSeries X` is a finite nonempty series of elements of a `JordanHolderLattice` such that each element is maximal inside the next. The length of a `CompositionSeries X` is one less than the number of elements in the series. Note that there is no stipulation that a series start from the bottom of the lattice and finish at the top. For a composition series `s`, `s.last` is the largest element of the series, and `s.head` is the least element. -/ abbrev CompositionSeries (X : Type u) [Lattice X] [JordanHolderLattice X] : Type u := RelSeries (IsMaximal (X := X)) namespace CompositionSeries variable {X : Type u} [Lattice X] [JordanHolderLattice X] theorem lt_succ (s : CompositionSeries X) (i : Fin s.length) : s (Fin.castSucc i) < s (Fin.succ i) := lt_of_isMaximal (s.step _) protected theorem strictMono (s : CompositionSeries X) : StrictMono s := Fin.strictMono_iff_lt_succ.2 s.lt_succ protected theorem injective (s : CompositionSeries X) : Function.Injective s := s.strictMono.injective @[simp] protected theorem inj (s : CompositionSeries X) {i j : Fin s.length.succ} : s i = s j ↔ i = j := s.injective.eq_iff theorem total {s : CompositionSeries X} {x y : X} (hx : x ∈ s) (hy : y ∈ s) : x ≤ y ∨ y ≤ x := by rcases Set.mem_range.1 hx with ⟨i, rfl⟩ rcases Set.mem_range.1 hy with ⟨j, rfl⟩ rw [s.strictMono.le_iff_le, s.strictMono.le_iff_le] exact le_total i j theorem toList_sorted (s : CompositionSeries X) : s.toList.Sorted (· < ·) := List.pairwise_iff_get.2 fun i j h => by dsimp only [RelSeries.toList] rw [List.get_ofFn, List.get_ofFn] exact s.strictMono h theorem toList_nodup (s : CompositionSeries X) : s.toList.Nodup := s.toList_sorted.nodup /-- Two `CompositionSeries` are equal if they have the same elements. See also `ext_fun`. -/ @[ext] theorem ext {s₁ s₂ : CompositionSeries X} (h : ∀ x, x ∈ s₁ ↔ x ∈ s₂) : s₁ = s₂ := toList_injective <| List.eq_of_perm_of_sorted (by classical exact List.perm_of_nodup_nodup_toFinset_eq s₁.toList_nodup s₂.toList_nodup (Finset.ext <| by simpa only [List.mem_toFinset, RelSeries.mem_toList])) s₁.toList_sorted s₂.toList_sorted @[simp] theorem le_last {s : CompositionSeries X} (i : Fin (s.length + 1)) : s i ≤ s.last := s.strictMono.monotone (Fin.le_last _) theorem le_last_of_mem {s : CompositionSeries X} {x : X} (hx : x ∈ s) : x ≤ s.last := let ⟨_i, hi⟩ := Set.mem_range.2 hx hi ▸ le_last _ @[simp] theorem head_le {s : CompositionSeries X} (i : Fin (s.length + 1)) : s.head ≤ s i := s.strictMono.monotone (Fin.zero_le _) theorem head_le_of_mem {s : CompositionSeries X} {x : X} (hx : x ∈ s) : s.head ≤ x := let ⟨_i, hi⟩ := Set.mem_range.2 hx hi ▸ head_le _ theorem last_eraseLast_le (s : CompositionSeries X) : s.eraseLast.last ≤ s.last := by simp [eraseLast, last, s.strictMono.le_iff_le, Fin.le_iff_val_le_val] theorem mem_eraseLast_of_ne_of_mem {s : CompositionSeries X} {x : X} (hx : x ≠ s.last) (hxs : x ∈ s) : x ∈ s.eraseLast := by rcases hxs with ⟨i, rfl⟩ have hi : (i : ℕ) < (s.length - 1).succ := by conv_rhs => rw [← Nat.succ_sub (length_pos_of_nontrivial ⟨_, ⟨i, rfl⟩, _, s.last_mem, hx⟩), Nat.add_one_sub_one] exact lt_of_le_of_ne (Nat.le_of_lt_succ i.2) (by simpa [last, s.inj, Fin.ext_iff] using hx) refine ⟨Fin.castSucc (n := s.length + 1) i, ?_⟩ simp [Fin.ext_iff, Nat.mod_eq_of_lt hi] theorem mem_eraseLast {s : CompositionSeries X} {x : X} (h : 0 < s.length) : x ∈ s.eraseLast ↔ x ≠ s.last ∧ x ∈ s := by simp only [RelSeries.mem_def, eraseLast] constructor · rintro ⟨i, rfl⟩ have hi : (i : ℕ) < s.length := by conv_rhs => rw [← Nat.add_one_sub_one s.length, Nat.succ_sub h] exact i.2 simp [last, Fin.ext_iff, ne_of_lt hi, -Set.mem_range, Set.mem_range_self] · intro h exact mem_eraseLast_of_ne_of_mem h.1 h.2 theorem lt_last_of_mem_eraseLast {s : CompositionSeries X} {x : X} (h : 0 < s.length) (hx : x ∈ s.eraseLast) : x < s.last := lt_of_le_of_ne (le_last_of_mem ((mem_eraseLast h).1 hx).2) ((mem_eraseLast h).1 hx).1 theorem isMaximal_eraseLast_last {s : CompositionSeries X} (h : 0 < s.length) : IsMaximal s.eraseLast.last s.last := by have : s.length - 1 + 1 = s.length := by conv_rhs => rw [← Nat.add_one_sub_one s.length]; rw [Nat.succ_sub h] rw [last_eraseLast, last] convert s.step ⟨s.length - 1, by omega⟩; ext; simp [this] theorem eq_snoc_eraseLast {s : CompositionSeries X} (h : 0 < s.length) : s = snoc (eraseLast s) s.last (isMaximal_eraseLast_last h) := by ext x simp only [mem_snoc, mem_eraseLast h, ne_eq] by_cases h : x = s.last <;> simp [*, s.last_mem] @[simp] theorem snoc_eraseLast_last {s : CompositionSeries X} (h : IsMaximal s.eraseLast.last s.last) : s.eraseLast.snoc s.last h = s := have h : 0 < s.length := Nat.pos_of_ne_zero (fun hs => ne_of_gt (lt_of_isMaximal h) <| by simp [last, Fin.ext_iff, hs]) (eq_snoc_eraseLast h).symm /-- Two `CompositionSeries X`, `s₁` and `s₂` are equivalent if there is a bijection `e : Fin s₁.length ≃ Fin s₂.length` such that for any `i`, `Iso (s₁ i) (s₁ i.succ) (s₂ (e i), s₂ (e i.succ))` -/ def Equivalent (s₁ s₂ : CompositionSeries X) : Prop := ∃ f : Fin s₁.length ≃ Fin s₂.length, ∀ i : Fin s₁.length, Iso (s₁ (Fin.castSucc i), s₁ i.succ) (s₂ (Fin.castSucc (f i)), s₂ (Fin.succ (f i))) namespace Equivalent @[refl] theorem refl (s : CompositionSeries X) : Equivalent s s := ⟨Equiv.refl _, fun _ => (s.step _).iso_refl⟩ @[symm] theorem symm {s₁ s₂ : CompositionSeries X} (h : Equivalent s₁ s₂) : Equivalent s₂ s₁ := ⟨h.choose.symm, fun i => iso_symm (by simpa using h.choose_spec (h.choose.symm i))⟩ @[trans] theorem trans {s₁ s₂ s₃ : CompositionSeries X} (h₁ : Equivalent s₁ s₂) (h₂ : Equivalent s₂ s₃) : Equivalent s₁ s₃ := ⟨h₁.choose.trans h₂.choose, fun i => iso_trans (h₁.choose_spec i) (h₂.choose_spec (h₁.choose i))⟩ protected theorem smash {s₁ s₂ t₁ t₂ : CompositionSeries X} (hs : s₁.last = s₂.head) (ht : t₁.last = t₂.head) (h₁ : Equivalent s₁ t₁) (h₂ : Equivalent s₂ t₂) : Equivalent (smash s₁ s₂ hs) (smash t₁ t₂ ht) := let e : Fin (s₁.length + s₂.length) ≃ Fin (t₁.length + t₂.length) := calc Fin (s₁.length + s₂.length) ≃ (Fin s₁.length) ⊕ (Fin s₂.length) := finSumFinEquiv.symm _ ≃ (Fin t₁.length) ⊕ (Fin t₂.length) := Equiv.sumCongr h₁.choose h₂.choose _ ≃ Fin (t₁.length + t₂.length) := finSumFinEquiv ⟨e, by intro i refine Fin.addCases ?_ ?_ i · intro i simpa [e, smash_castAdd, smash_succ_castAdd] using h₁.choose_spec i · intro i simpa [e, smash_natAdd, smash_succ_natAdd] using h₂.choose_spec i⟩ protected theorem snoc {s₁ s₂ : CompositionSeries X} {x₁ x₂ : X} {hsat₁ : IsMaximal s₁.last x₁} {hsat₂ : IsMaximal s₂.last x₂} (hequiv : Equivalent s₁ s₂) (hlast : Iso (s₁.last, x₁) (s₂.last, x₂)) : Equivalent (s₁.snoc x₁ hsat₁) (s₂.snoc x₂ hsat₂) := let e : Fin s₁.length.succ ≃ Fin s₂.length.succ := calc Fin (s₁.length + 1) ≃ Option (Fin s₁.length) := finSuccEquivLast _ ≃ Option (Fin s₂.length) := Functor.mapEquiv Option hequiv.choose _ ≃ Fin (s₂.length + 1) := finSuccEquivLast.symm ⟨e, fun i => by refine Fin.lastCases ?_ ?_ i · simpa [e, apply_last] using hlast · intro i simpa [e, Fin.succ_castSucc] using hequiv.choose_spec i⟩ theorem length_eq {s₁ s₂ : CompositionSeries X} (h : Equivalent s₁ s₂) : s₁.length = s₂.length := by simpa using Fintype.card_congr h.choose theorem snoc_snoc_swap {s : CompositionSeries X} {x₁ x₂ y₁ y₂ : X} {hsat₁ : IsMaximal s.last x₁} {hsat₂ : IsMaximal s.last x₂} {hsaty₁ : IsMaximal (snoc s x₁ hsat₁).last y₁} {hsaty₂ : IsMaximal (snoc s x₂ hsat₂).last y₂} (hr₁ : Iso (s.last, x₁) (x₂, y₂)) (hr₂ : Iso (x₁, y₁) (s.last, x₂)) : Equivalent (snoc (snoc s x₁ hsat₁) y₁ hsaty₁) (snoc (snoc s x₂ hsat₂) y₂ hsaty₂) := let e : Fin (s.length + 1 + 1) ≃ Fin (s.length + 1 + 1) := Equiv.swap (Fin.last _) (Fin.castSucc (Fin.last _)) have h1 : ∀ {i : Fin s.length}, (Fin.castSucc (Fin.castSucc i)) ≠ (Fin.castSucc (Fin.last _)) := by simp have h2 : ∀ {i : Fin s.length}, (Fin.castSucc (Fin.castSucc i)) ≠ Fin.last _ := by simp ⟨e, by intro i dsimp only [e] refine Fin.lastCases ?_ (fun i => ?_) i · erw [Equiv.swap_apply_left, snoc_castSucc, show (snoc s x₁ hsat₁).toFun (Fin.last _) = x₁ from last_snoc _ _ _, Fin.succ_last, show ((s.snoc x₁ hsat₁).snoc y₁ hsaty₁).toFun (Fin.last _) = y₁ from last_snoc _ _ _, snoc_castSucc, snoc_castSucc, Fin.succ_castSucc, snoc_castSucc, Fin.succ_last, show (s.snoc _ hsat₂).toFun (Fin.last _) = x₂ from last_snoc _ _ _] exact hr₂ · refine Fin.lastCases ?_ (fun i => ?_) i · erw [Equiv.swap_apply_right, snoc_castSucc, snoc_castSucc, snoc_castSucc, Fin.succ_castSucc, snoc_castSucc, Fin.succ_last, last_snoc', last_snoc', last_snoc'] exact hr₁ · erw [Equiv.swap_apply_of_ne_of_ne h2 h1, snoc_castSucc, snoc_castSucc, snoc_castSucc, snoc_castSucc, Fin.succ_castSucc, snoc_castSucc, Fin.succ_castSucc, snoc_castSucc, snoc_castSucc, snoc_castSucc] exact (s.step i).iso_refl⟩ end Equivalent theorem length_eq_zero_of_head_eq_head_of_last_eq_last_of_length_eq_zero {s₁ s₂ : CompositionSeries X} (hb : s₁.head = s₂.head) (ht : s₁.last = s₂.last) (hs₁ : s₁.length = 0) : s₂.length = 0 := by have : Fin.last s₂.length = (0 : Fin s₂.length.succ) := s₂.injective (hb.symm.trans ((congr_arg s₁ (Fin.ext (by simp [hs₁]))).trans ht)).symm simpa [Fin.ext_iff] theorem length_pos_of_head_eq_head_of_last_eq_last_of_length_pos {s₁ s₂ : CompositionSeries X} (hb : s₁.head = s₂.head) (ht : s₁.last = s₂.last) : 0 < s₁.length → 0 < s₂.length := not_imp_not.1 (by simpa only [pos_iff_ne_zero, ne_eq, Decidable.not_not] using length_eq_zero_of_head_eq_head_of_last_eq_last_of_length_eq_zero hb.symm ht.symm) theorem eq_of_head_eq_head_of_last_eq_last_of_length_eq_zero {s₁ s₂ : CompositionSeries X} (hb : s₁.head = s₂.head) (ht : s₁.last = s₂.last) (hs₁0 : s₁.length = 0) : s₁ = s₂ := by have : ∀ x, x ∈ s₁ ↔ x = s₁.last := fun x => ⟨fun hx => subsingleton_of_length_eq_zero hs₁0 hx s₁.last_mem, fun hx => hx.symm ▸ s₁.last_mem⟩ have : ∀ x, x ∈ s₂ ↔ x = s₂.last := fun x => ⟨fun hx => subsingleton_of_length_eq_zero (length_eq_zero_of_head_eq_head_of_last_eq_last_of_length_eq_zero hb ht hs₁0) hx s₂.last_mem, fun hx => hx.symm ▸ s₂.last_mem⟩ ext simp [*] /-- Given a `CompositionSeries`, `s`, and an element `x` such that `x` is maximal inside `s.last` there is a series, `t`, such that `t.last = x`, `t.head = s.head` and `snoc t s.last _` is equivalent to `s`. -/ theorem exists_last_eq_snoc_equivalent (s : CompositionSeries X) (x : X) (hm : IsMaximal x s.last) (hb : s.head ≤ x) : ∃ t : CompositionSeries X, t.head = s.head ∧ t.length + 1 = s.length ∧ ∃ htx : t.last = x, Equivalent s (snoc t s.last (show IsMaximal t.last _ from htx.symm ▸ hm)) := by induction' hn : s.length with n ih generalizing s x · exact (ne_of_gt (lt_of_le_of_lt hb (lt_of_isMaximal hm)) (subsingleton_of_length_eq_zero hn s.last_mem s.head_mem)).elim · have h0s : 0 < s.length := hn.symm ▸ Nat.succ_pos _ by_cases hetx : s.eraseLast.last = x · use s.eraseLast simp [← hetx, hn, Equivalent.refl] · have imxs : IsMaximal (x ⊓ s.eraseLast.last) s.eraseLast.last := isMaximal_of_eq_inf x s.last rfl (Ne.symm hetx) hm (isMaximal_eraseLast_last h0s) have := ih _ _ imxs (le_inf (by simpa) (le_last_of_mem s.eraseLast.head_mem)) (by simp [hn]) rcases this with ⟨t, htb, htl, htt, hteqv⟩ have hmtx : IsMaximal t.last x := isMaximal_of_eq_inf s.eraseLast.last s.last (by rw [inf_comm, htt]) hetx (isMaximal_eraseLast_last h0s) hm use snoc t x hmtx refine ⟨by simp [htb], by simp [htl], by simp, ?_⟩ have : s.Equivalent ((snoc t s.eraseLast.last <| show IsMaximal t.last _ from htt.symm ▸ imxs).snoc s.last (by simpa using isMaximal_eraseLast_last h0s)) := by conv_lhs => rw [eq_snoc_eraseLast h0s] exact Equivalent.snoc hteqv (by simpa using (isMaximal_eraseLast_last h0s).iso_refl) refine this.trans <| Equivalent.snoc_snoc_swap (iso_symm (second_iso_of_eq hm (sup_eq_of_isMaximal hm (isMaximal_eraseLast_last h0s) (Ne.symm hetx)) htt.symm)) (second_iso_of_eq (isMaximal_eraseLast_last h0s) (sup_eq_of_isMaximal (isMaximal_eraseLast_last h0s) hm hetx) (by rw [inf_comm, htt])) /-- The **Jordan-Hölder** theorem, stated for any `JordanHolderLattice`. If two composition series start and finish at the same place, they are equivalent. -/ theorem jordan_holder (s₁ s₂ : CompositionSeries X) (hb : s₁.head = s₂.head) (ht : s₁.last = s₂.last) : Equivalent s₁ s₂ := by induction' hle : s₁.length with n ih generalizing s₁ s₂ · rw [eq_of_head_eq_head_of_last_eq_last_of_length_eq_zero hb ht hle] · have h0s₂ : 0 < s₂.length := length_pos_of_head_eq_head_of_last_eq_last_of_length_pos hb ht (hle.symm ▸ Nat.succ_pos _) rcases exists_last_eq_snoc_equivalent s₁ s₂.eraseLast.last (ht.symm ▸ isMaximal_eraseLast_last h0s₂) (hb.symm ▸ s₂.head_eraseLast ▸ head_le_of_mem (last_mem _)) with ⟨t, htb, htl, htt, hteq⟩ have := ih t s₂.eraseLast (by simp [htb, ← hb]) htt (Nat.succ_inj.1 (htl.trans hle)) refine hteq.trans ?_ conv_rhs => rw [eq_snoc_eraseLast h0s₂] simp only [ht] exact Equivalent.snoc this (by simpa [htt] using (isMaximal_eraseLast_last h0s₂).iso_refl) end CompositionSeries
Mathlib/Order/JordanHolder.lean
440
445
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson -/ import Mathlib.RingTheory.Nilpotent.Basic import Mathlib.RingTheory.UniqueFactorizationDomain.GCDMonoid import Mathlib.RingTheory.UniqueFactorizationDomain.Multiplicity /-! # Squarefree elements of monoids An element of a monoid is squarefree when it is not divisible by any squares except the squares of units. Results about squarefree natural numbers are proved in `Data.Nat.Squarefree`. ## Main Definitions - `Squarefree r` indicates that `r` is only divisible by `x * x` if `x` is a unit. ## Main Results - `multiplicity.squarefree_iff_emultiplicity_le_one`: `x` is `Squarefree` iff for every `y`, either `emultiplicity y x ≤ 1` or `IsUnit y`. - `UniqueFactorizationMonoid.squarefree_iff_nodup_factors`: A nonzero element `x` of a unique factorization monoid is squarefree iff `factors x` has no duplicate factors. ## Tags squarefree, multiplicity -/ variable {R : Type*} /-- An element of a monoid is squarefree if the only squares that divide it are the squares of units. -/ def Squarefree [Monoid R] (r : R) : Prop := ∀ x : R, x * x ∣ r → IsUnit x theorem IsRelPrime.of_squarefree_mul [CommMonoid R] {m n : R} (h : Squarefree (m * n)) : IsRelPrime m n := fun c hca hcb ↦ h c (mul_dvd_mul hca hcb) @[simp] theorem IsUnit.squarefree [CommMonoid R] {x : R} (h : IsUnit x) : Squarefree x := fun _ hdvd => isUnit_of_mul_isUnit_left (isUnit_of_dvd_unit hdvd h) theorem squarefree_one [CommMonoid R] : Squarefree (1 : R) := isUnit_one.squarefree @[simp] theorem not_squarefree_zero [MonoidWithZero R] [Nontrivial R] : ¬Squarefree (0 : R) := by erw [not_forall] exact ⟨0, by simp⟩ theorem Squarefree.ne_zero [MonoidWithZero R] [Nontrivial R] {m : R} (hm : Squarefree (m : R)) : m ≠ 0 := by rintro rfl exact not_squarefree_zero hm @[simp] theorem Irreducible.squarefree [CommMonoid R] {x : R} (h : Irreducible x) : Squarefree x := by rintro y ⟨z, hz⟩ rw [mul_assoc] at hz rcases h.isUnit_or_isUnit hz with (hu | hu) · exact hu · apply isUnit_of_mul_isUnit_left hu @[simp] theorem Prime.squarefree [CancelCommMonoidWithZero R] {x : R} (h : Prime x) : Squarefree x := h.irreducible.squarefree theorem Squarefree.of_mul_left [Monoid R] {m n : R} (hmn : Squarefree (m * n)) : Squarefree m := fun p hp => hmn p (dvd_mul_of_dvd_left hp n) theorem Squarefree.of_mul_right [CommMonoid R] {m n : R} (hmn : Squarefree (m * n)) : Squarefree n := fun p hp => hmn p (dvd_mul_of_dvd_right hp m) theorem Squarefree.squarefree_of_dvd [Monoid R] {x y : R} (hdvd : x ∣ y) (hsq : Squarefree y) : Squarefree x := fun _ h => hsq _ (h.trans hdvd) theorem Squarefree.eq_zero_or_one_of_pow_of_not_isUnit [Monoid R] {x : R} {n : ℕ} (h : Squarefree (x ^ n)) (h' : ¬ IsUnit x) : n = 0 ∨ n = 1 := by contrapose! h' replace h' : 2 ≤ n := by omega have : x * x ∣ x ^ n := by rw [← sq]; exact pow_dvd_pow x h' exact h.squarefree_of_dvd this x (refl _) theorem Squarefree.pow_dvd_of_pow_dvd [Monoid R] {x y : R} {n : ℕ} (hx : Squarefree y) (h : x ^ n ∣ y) : x ^ n ∣ x := by by_cases hu : IsUnit x · exact (hu.pow n).dvd · rcases (hx.squarefree_of_dvd h).eq_zero_or_one_of_pow_of_not_isUnit hu with rfl | rfl <;> simp section SquarefreeGcdOfSquarefree variable {α : Type*} [CancelCommMonoidWithZero α] [GCDMonoid α] theorem Squarefree.gcd_right (a : α) {b : α} (hb : Squarefree b) : Squarefree (gcd a b) := hb.squarefree_of_dvd (gcd_dvd_right _ _) theorem Squarefree.gcd_left {a : α} (b : α) (ha : Squarefree a) : Squarefree (gcd a b) := ha.squarefree_of_dvd (gcd_dvd_left _ _) end SquarefreeGcdOfSquarefree theorem squarefree_iff_emultiplicity_le_one [CommMonoid R] (r : R) : Squarefree r ↔ ∀ x : R, emultiplicity x r ≤ 1 ∨ IsUnit x := by refine forall_congr' fun a => ?_ rw [← sq, pow_dvd_iff_le_emultiplicity, or_iff_not_imp_left, not_le, imp_congr _ Iff.rfl] norm_cast rw [← one_add_one_eq_two] exact Order.add_one_le_iff_of_not_isMax (by simp) @[deprecated (since := "2024-11-30")] alias multiplicity.squarefree_iff_emultiplicity_le_one := squarefree_iff_emultiplicity_le_one section Irreducible variable [CommMonoidWithZero R] [WfDvdMonoid R] theorem squarefree_iff_no_irreducibles {x : R} (hx₀ : x ≠ 0) : Squarefree x ↔ ∀ p, Irreducible p → ¬ (p * p ∣ x) := by refine ⟨fun h p hp hp' ↦ hp.not_isUnit (h p hp'), fun h d hd ↦ by_contra fun hdu ↦ ?_⟩ have hd₀ : d ≠ 0 := ne_zero_of_dvd_ne_zero (ne_zero_of_dvd_ne_zero hx₀ hd) (dvd_mul_left d d) obtain ⟨p, irr, dvd⟩ := WfDvdMonoid.exists_irreducible_factor hdu hd₀ exact h p irr ((mul_dvd_mul dvd dvd).trans hd) theorem irreducible_sq_not_dvd_iff_eq_zero_and_no_irreducibles_or_squarefree (r : R) : (∀ x : R, Irreducible x → ¬x * x ∣ r) ↔ (r = 0 ∧ ∀ x : R, ¬Irreducible x) ∨ Squarefree r := by refine ⟨fun h ↦ ?_, ?_⟩ · rcases eq_or_ne r 0 with (rfl | hr) · exact .inl (by simpa using h) · exact .inr ((squarefree_iff_no_irreducibles hr).mpr h) · rintro (⟨rfl, h⟩ | h) · simpa using h intro x hx t exact hx.not_isUnit (h x t) theorem squarefree_iff_irreducible_sq_not_dvd_of_ne_zero {r : R} (hr : r ≠ 0) : Squarefree r ↔ ∀ x : R, Irreducible x → ¬x * x ∣ r := by simpa [hr] using (irreducible_sq_not_dvd_iff_eq_zero_and_no_irreducibles_or_squarefree r).symm theorem squarefree_iff_irreducible_sq_not_dvd_of_exists_irreducible {r : R} (hr : ∃ x : R, Irreducible x) : Squarefree r ↔ ∀ x : R, Irreducible x → ¬x * x ∣ r := by rw [irreducible_sq_not_dvd_iff_eq_zero_and_no_irreducibles_or_squarefree, ← not_exists] simp only [hr, not_true, false_or, and_false] end Irreducible section IsRadical section variable [CommMonoidWithZero R] [DecompositionMonoid R]
theorem Squarefree.isRadical {x : R} (hx : Squarefree x) : IsRadical x := (isRadical_iff_pow_one_lt 2 one_lt_two).2 fun y hy ↦ by obtain ⟨a, b, ha, hb, rfl⟩ := exists_dvd_and_dvd_of_dvd_mul (sq y ▸ hy) exact (IsRelPrime.of_squarefree_mul hx).mul_dvd ha hb theorem Squarefree.dvd_pow_iff_dvd {x y : R} {n : ℕ} (hsq : Squarefree x) (h0 : n ≠ 0) : x ∣ y ^ n ↔ x ∣ y := ⟨hsq.isRadical n y, (·.pow h0)⟩ end
Mathlib/Algebra/Squarefree/Basic.lean
154
163
/- Copyright (c) 2023 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import Mathlib.Algebra.Order.Monovary import Mathlib.Algebra.Order.Ring.Basic import Mathlib.Analysis.Convex.Function import Mathlib.Tactic.FieldSimp /-! # Product of convex functions This file proves that the product of convex functions is convex, provided they monovary. As corollaries, we also prove that `x ↦ x ^ n` is convex * `Even.convexOn_pow`: for even `n : ℕ`. * `convexOn_pow`: over $[0, +∞)$ for `n : ℕ`. * `convexOn_zpow`: over $(0, +∞)$ For `n : ℤ`. -/ open Set variable {𝕜 E F : Type*} section LinearOrderedCommRing variable [CommRing 𝕜] [LinearOrder 𝕜] [IsStrictOrderedRing 𝕜] [CommRing E] [LinearOrder E] [IsStrictOrderedRing E] [AddCommGroup F] [LinearOrder F] [IsOrderedAddMonoid F] [Module 𝕜 E] [Module 𝕜 F] [Module E F] [IsScalarTower 𝕜 E F] [SMulCommClass 𝕜 E F] [OrderedSMul 𝕜 F] [OrderedSMul E F] {s : Set 𝕜} {f : 𝕜 → E} {g : 𝕜 → F} lemma ConvexOn.smul' (hf : ConvexOn 𝕜 s f) (hg : ConvexOn 𝕜 s g) (hf₀ : ∀ ⦃x⦄, x ∈ s → 0 ≤ f x) (hg₀ : ∀ ⦃x⦄, x ∈ s → 0 ≤ g x) (hfg : MonovaryOn f g s) : ConvexOn 𝕜 s (f • g) := by refine ⟨hf.1, fun x hx y hy a b ha hb hab ↦ ?_⟩ dsimp refine (smul_le_smul (hf.2 hx hy ha hb hab) (hg.2 hx hy ha hb hab) (hf₀ <| hf.1 hx hy ha hb hab) <| add_nonneg (smul_nonneg ha <| hg₀ hx) <| smul_nonneg hb <| hg₀ hy).trans ?_ calc _ = (a * a) • (f x • g x) + (b * b) • (f y • g y) + (a * b) • (f x • g y + f y • g x) := ?_ _ ≤ (a * a) • (f x • g x) + (b * b) • (f y • g y) + (a * b) • (f x • g x + f y • g y) := by gcongr _ + (a * b) • ?_; exact hfg.smul_add_smul_le_smul_add_smul hx hy _ = (a * (a + b)) • (f x • g x) + (b * (a + b)) • (f y • g y) := by simp only [mul_add, add_smul, smul_add, mul_comm _ a]; abel _ = _ := by simp_rw [hab, mul_one] simp only [mul_add, add_smul, smul_add] rw [← smul_smul_smul_comm a, ← smul_smul_smul_comm b, ← smul_smul_smul_comm a b, ← smul_smul_smul_comm b b, smul_eq_mul, smul_eq_mul, smul_eq_mul, smul_eq_mul, mul_comm b, add_comm _ ((b * b) • f y • g y), add_add_add_comm, add_comm ((a * b) • f y • g x)] lemma ConcaveOn.smul' [OrderedSMul 𝕜 E] (hf : ConcaveOn 𝕜 s f) (hg : ConcaveOn 𝕜 s g) (hf₀ : ∀ ⦃x⦄, x ∈ s → 0 ≤ f x) (hg₀ : ∀ ⦃x⦄, x ∈ s → 0 ≤ g x) (hfg : AntivaryOn f g s) : ConcaveOn 𝕜 s (f • g) := by refine ⟨hf.1, fun x hx y hy a b ha hb hab ↦ ?_⟩ dsimp refine (smul_le_smul (hf.2 hx hy ha hb hab) (hg.2 hx hy ha hb hab) (add_nonneg (smul_nonneg ha <| hf₀ hx) <| smul_nonneg hb <| hf₀ hy) (hg₀ <| hf.1 hx hy ha hb hab)).trans' ?_ calc a • f x • g x + b • f y • g y = (a * (a + b)) • (f x • g x) + (b * (a + b)) • (f y • g y) := by simp_rw [hab, mul_one] _ = (a * a) • (f x • g x) + (b * b) • (f y • g y) + (a * b) • (f x • g x + f y • g y) := by simp only [mul_add, add_smul, smul_add, mul_comm _ a]; abel _ ≤ (a * a) • (f x • g x) + (b * b) • (f y • g y) + (a * b) • (f x • g y + f y • g x) := by gcongr _ + (a * b) • ?_; exact hfg.smul_add_smul_le_smul_add_smul hx hy _ = _ := ?_ simp only [mul_add, add_smul, smul_add] rw [← smul_smul_smul_comm a, ← smul_smul_smul_comm b, ← smul_smul_smul_comm a b, ← smul_smul_smul_comm b b, smul_eq_mul, smul_eq_mul, smul_eq_mul, smul_eq_mul, mul_comm b a, add_comm ((a * b) • f x • g y), add_comm ((a * b) • f x • g y), add_add_add_comm] lemma ConvexOn.smul'' [OrderedSMul 𝕜 E] (hf : ConvexOn 𝕜 s f) (hg : ConvexOn 𝕜 s g) (hf₀ : ∀ ⦃x⦄, x ∈ s → f x ≤ 0) (hg₀ : ∀ ⦃x⦄, x ∈ s → g x ≤ 0) (hfg : AntivaryOn f g s) : ConcaveOn 𝕜 s (f • g) := by rw [← neg_smul_neg] exact hf.neg.smul' hg.neg (fun x hx ↦ neg_nonneg.2 <| hf₀ hx) (fun x hx ↦ neg_nonneg.2 <| hg₀ hx) hfg.neg lemma ConcaveOn.smul'' (hf : ConcaveOn 𝕜 s f) (hg : ConcaveOn 𝕜 s g) (hf₀ : ∀ ⦃x⦄, x ∈ s → f x ≤ 0) (hg₀ : ∀ ⦃x⦄, x ∈ s → g x ≤ 0) (hfg : MonovaryOn f g s) : ConvexOn 𝕜 s (f • g) := by rw [← neg_smul_neg] exact hf.neg.smul' hg.neg (fun x hx ↦ neg_nonneg.2 <| hf₀ hx) (fun x hx ↦ neg_nonneg.2 <| hg₀ hx) hfg.neg lemma ConvexOn.smul_concaveOn (hf : ConvexOn 𝕜 s f) (hg : ConcaveOn 𝕜 s g) (hf₀ : ∀ ⦃x⦄, x ∈ s → 0 ≤ f x) (hg₀ : ∀ ⦃x⦄, x ∈ s → g x ≤ 0) (hfg : AntivaryOn f g s) : ConcaveOn 𝕜 s (f • g) := by rw [← neg_convexOn_iff, ← smul_neg] exact hf.smul' hg.neg hf₀ (fun x hx ↦ neg_nonneg.2 <| hg₀ hx) hfg.neg_right lemma ConcaveOn.smul_convexOn [OrderedSMul 𝕜 E] (hf : ConcaveOn 𝕜 s f) (hg : ConvexOn 𝕜 s g) (hf₀ : ∀ ⦃x⦄, x ∈ s → 0 ≤ f x) (hg₀ : ∀ ⦃x⦄, x ∈ s → g x ≤ 0) (hfg : MonovaryOn f g s) : ConvexOn 𝕜 s (f • g) := by rw [← neg_concaveOn_iff, ← smul_neg] exact hf.smul' hg.neg hf₀ (fun x hx ↦ neg_nonneg.2 <| hg₀ hx) hfg.neg_right lemma ConvexOn.smul_concaveOn' [OrderedSMul 𝕜 E] (hf : ConvexOn 𝕜 s f) (hg : ConcaveOn 𝕜 s g) (hf₀ : ∀ ⦃x⦄, x ∈ s → f x ≤ 0) (hg₀ : ∀ ⦃x⦄, x ∈ s → 0 ≤ g x) (hfg : MonovaryOn f g s) : ConvexOn 𝕜 s (f • g) := by rw [← neg_concaveOn_iff, ← smul_neg] exact hf.smul'' hg.neg hf₀ (fun x hx ↦ neg_nonpos.2 <| hg₀ hx) hfg.neg_right lemma ConcaveOn.smul_convexOn' (hf : ConcaveOn 𝕜 s f) (hg : ConvexOn 𝕜 s g) (hf₀ : ∀ ⦃x⦄, x ∈ s → f x ≤ 0) (hg₀ : ∀ ⦃x⦄, x ∈ s → 0 ≤ g x) (hfg : AntivaryOn f g s) : ConcaveOn 𝕜 s (f • g) := by rw [← neg_convexOn_iff, ← smul_neg] exact hf.smul'' hg.neg hf₀ (fun x hx ↦ neg_nonpos.2 <| hg₀ hx) hfg.neg_right variable [OrderedSMul 𝕜 E] [IsScalarTower 𝕜 E E] [SMulCommClass 𝕜 E E] {f g : 𝕜 → E} lemma ConvexOn.mul (hf : ConvexOn 𝕜 s f) (hg : ConvexOn 𝕜 s g) (hf₀ : ∀ ⦃x⦄, x ∈ s → 0 ≤ f x) (hg₀ : ∀ ⦃x⦄, x ∈ s → 0 ≤ g x) (hfg : MonovaryOn f g s) : ConvexOn 𝕜 s (f * g) := hf.smul' hg hf₀ hg₀ hfg lemma ConcaveOn.mul (hf : ConcaveOn 𝕜 s f) (hg : ConcaveOn 𝕜 s g) (hf₀ : ∀ ⦃x⦄, x ∈ s → 0 ≤ f x) (hg₀ : ∀ ⦃x⦄, x ∈ s → 0 ≤ g x) (hfg : AntivaryOn f g s) : ConcaveOn 𝕜 s (f * g) := hf.smul' hg hf₀ hg₀ hfg lemma ConvexOn.mul' (hf : ConvexOn 𝕜 s f) (hg : ConvexOn 𝕜 s g) (hf₀ : ∀ ⦃x⦄, x ∈ s → f x ≤ 0) (hg₀ : ∀ ⦃x⦄, x ∈ s → g x ≤ 0) (hfg : AntivaryOn f g s) : ConcaveOn 𝕜 s (f * g) := hf.smul'' hg hf₀ hg₀ hfg lemma ConcaveOn.mul' (hf : ConcaveOn 𝕜 s f) (hg : ConcaveOn 𝕜 s g) (hf₀ : ∀ ⦃x⦄, x ∈ s → f x ≤ 0) (hg₀ : ∀ ⦃x⦄, x ∈ s → g x ≤ 0) (hfg : MonovaryOn f g s) : ConvexOn 𝕜 s (f * g) := hf.smul'' hg hf₀ hg₀ hfg lemma ConvexOn.mul_concaveOn (hf : ConvexOn 𝕜 s f) (hg : ConcaveOn 𝕜 s g) (hf₀ : ∀ ⦃x⦄, x ∈ s → 0 ≤ f x) (hg₀ : ∀ ⦃x⦄, x ∈ s → g x ≤ 0) (hfg : AntivaryOn f g s) : ConcaveOn 𝕜 s (f * g) := hf.smul_concaveOn hg hf₀ hg₀ hfg lemma ConcaveOn.mul_convexOn (hf : ConcaveOn 𝕜 s f) (hg : ConvexOn 𝕜 s g) (hf₀ : ∀ ⦃x⦄, x ∈ s → 0 ≤ f x) (hg₀ : ∀ ⦃x⦄, x ∈ s → g x ≤ 0) (hfg : MonovaryOn f g s) : ConvexOn 𝕜 s (f * g) := hf.smul_convexOn hg hf₀ hg₀ hfg lemma ConvexOn.mul_concaveOn' (hf : ConvexOn 𝕜 s f) (hg : ConcaveOn 𝕜 s g) (hf₀ : ∀ ⦃x⦄, x ∈ s → f x ≤ 0) (hg₀ : ∀ ⦃x⦄, x ∈ s → 0 ≤ g x) (hfg : MonovaryOn f g s) : ConvexOn 𝕜 s (f * g) := hf.smul_concaveOn' hg hf₀ hg₀ hfg lemma ConcaveOn.mul_convexOn' (hf : ConcaveOn 𝕜 s f) (hg : ConvexOn 𝕜 s g) (hf₀ : ∀ ⦃x⦄, x ∈ s → f x ≤ 0) (hg₀ : ∀ ⦃x⦄, x ∈ s → 0 ≤ g x) (hfg : AntivaryOn f g s) : ConcaveOn 𝕜 s (f • g) := hf.smul_convexOn' hg hf₀ hg₀ hfg lemma ConvexOn.pow (hf : ConvexOn 𝕜 s f) (hf₀ : ∀ ⦃x⦄, x ∈ s → 0 ≤ f x) : ∀ n, ConvexOn 𝕜 s (f ^ n) | 0 => by simpa using convexOn_const 1 hf.1 | n + 1 => by rw [pow_succ'] exact hf.mul (hf.pow hf₀ _) hf₀ (fun x hx ↦ pow_nonneg (hf₀ hx) _) <| (monovaryOn_self f s).pow_right₀ hf₀ n /-- `x^n`, `n : ℕ` is convex on `[0, +∞)` for all `n`. -/
lemma convexOn_pow : ∀ n, ConvexOn 𝕜 (Ici 0) fun x : 𝕜 ↦ x ^ n := (convexOn_id <| convex_Ici _).pow fun _ ↦ id /-- `x^n`, `n : ℕ` is convex on the whole real line whenever `n` is even. -/ protected lemma Even.convexOn_pow {n : ℕ} (hn : Even n) : ConvexOn 𝕜 univ fun x : 𝕜 ↦ x ^ n := by obtain ⟨n, rfl⟩ := hn simp_rw [← two_mul, pow_mul] refine ConvexOn.pow ⟨convex_univ, fun x _ y _ a b ha hb hab ↦ sub_nonneg.1 ?_⟩ (fun _ _ ↦ by positivity) _
Mathlib/Analysis/Convex/Mul.lean
152
160
/- Copyright (c) 2021 Aaron Anderson, Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson, Kevin Buzzard, Yaël Dillies, Eric Wieser -/ import Mathlib.Data.Finset.Lattice.Union import Mathlib.Data.Finset.Pairwise import Mathlib.Data.Finset.Prod import Mathlib.Data.Finset.Sigma import Mathlib.Data.Fintype.Basic import Mathlib.Order.CompleteLatticeIntervals /-! # Supremum independence In this file, we define supremum independence of indexed sets. An indexed family `f : ι → α` is sup-independent if, for all `a`, `f a` and the supremum of the rest are disjoint. ## Main definitions * `Finset.SupIndep s f`: a family of elements `f` are supremum independent on the finite set `s`. * `sSupIndep s`: a set of elements are supremum independent. * `iSupIndep f`: a family of elements are supremum independent. ## Main statements * In a distributive lattice, supremum independence is equivalent to pairwise disjointness: * `Finset.supIndep_iff_pairwiseDisjoint` * `CompleteLattice.sSupIndep_iff_pairwiseDisjoint` * `CompleteLattice.iSupIndep_iff_pairwiseDisjoint` * Otherwise, supremum independence is stronger than pairwise disjointness: * `Finset.SupIndep.pairwiseDisjoint` * `sSupIndep.pairwiseDisjoint` * `iSupIndep.pairwiseDisjoint` ## Implementation notes For the finite version, we avoid the "obvious" definition `∀ i ∈ s, Disjoint (f i) ((s.erase i).sup f)` because `erase` would require decidable equality on `ι`. -/ variable {α β ι ι' : Type*} /-! ### On lattices with a bottom element, via `Finset.sup` -/ namespace Finset section Lattice variable [Lattice α] [OrderBot α] /-- Supremum independence of finite sets. We avoid the "obvious" definition using `s.erase i` because `erase` would require decidable equality on `ι`. -/ def SupIndep (s : Finset ι) (f : ι → α) : Prop := ∀ ⦃t⦄, t ⊆ s → ∀ ⦃i⦄, i ∈ s → i ∉ t → Disjoint (f i) (t.sup f) variable {s t : Finset ι} {f : ι → α} {i : ι} /-- The RHS looks like the definition of `iSupIndep`. -/ theorem supIndep_iff_disjoint_erase [DecidableEq ι] : s.SupIndep f ↔ ∀ i ∈ s, Disjoint (f i) ((s.erase i).sup f) := ⟨fun hs _ hi => hs (erase_subset _ _) hi (not_mem_erase _ _), fun hs _ ht i hi hit => (hs i hi).mono_right (sup_mono fun _ hj => mem_erase.2 ⟨ne_of_mem_of_not_mem hj hit, ht hj⟩)⟩ /-- If both the index type and the lattice have decidable equality, then the `SupIndep` predicate is decidable. TODO: speedup the definition and drop the `[DecidableEq ι]` assumption by iterating over the pairs `(a, t)` such that `s = Finset.cons a t _` using something like `List.eraseIdx` or by generating both `f i` and `(s.erase i).sup f` in one loop over `s`. Yet another possible optimization is to precompute partial suprema of `f` over the inits and tails of the list representing `s`, store them in 2 `Array`s, then compute each `sup` in 1 operation. -/ instance [DecidableEq ι] [DecidableEq α] : Decidable (SupIndep s f) := have : ∀ i, Decidable (Disjoint (f i) ((s.erase i).sup f)) := fun _ ↦ decidable_of_iff _ disjoint_iff.symm decidable_of_iff _ supIndep_iff_disjoint_erase.symm theorem SupIndep.subset (ht : t.SupIndep f) (h : s ⊆ t) : s.SupIndep f := fun _ hu _ hi => ht (hu.trans h) (h hi) @[simp] theorem supIndep_empty (f : ι → α) : (∅ : Finset ι).SupIndep f := fun _ _ a ha => (not_mem_empty a ha).elim @[simp] theorem supIndep_singleton (i : ι) (f : ι → α) : ({i} : Finset ι).SupIndep f := fun s hs j hji hj => by rw [eq_empty_of_ssubset_singleton ⟨hs, fun h => hj (h hji)⟩, sup_empty] exact disjoint_bot_right theorem SupIndep.pairwiseDisjoint (hs : s.SupIndep f) : (s : Set ι).PairwiseDisjoint f := fun _ ha _ hb hab => sup_singleton.subst <| hs (singleton_subset_iff.2 hb) ha <| not_mem_singleton.2 hab @[deprecated (since := "2025-01-17")] alias sup_indep.pairwise_disjoint := SupIndep.pairwiseDisjoint theorem SupIndep.le_sup_iff (hs : s.SupIndep f) (hts : t ⊆ s) (hi : i ∈ s) (hf : ∀ i, f i ≠ ⊥) : f i ≤ t.sup f ↔ i ∈ t := by refine ⟨fun h => ?_, le_sup⟩ by_contra hit exact hf i (disjoint_self.1 <| (hs hts hi hit).mono_right h) theorem SupIndep.antitone_fun {g : ι → α} (hle : ∀ x ∈ s, f x ≤ g x) (h : s.SupIndep g) : s.SupIndep f := fun _t hts i his hit ↦ (h hts his hit).mono (hle i his) <| Finset.sup_mono_fun fun x hx ↦ hle x <| hts hx @[deprecated (since := "2025-01-17")] alias supIndep_antimono_fun := SupIndep.antitone_fun protected theorem SupIndep.image [DecidableEq ι] {s : Finset ι'} {g : ι' → ι} (hs : s.SupIndep (f ∘ g)) : (s.image g).SupIndep f := by intro t ht i hi hit rcases subset_image_iff.mp ht with ⟨t, hts, rfl⟩ rcases mem_image.mp hi with ⟨i, his, rfl⟩ rw [sup_image] exact hs hts his (hit <| mem_image_of_mem _ ·) theorem supIndep_map {s : Finset ι'} {g : ι' ↪ ι} : (s.map g).SupIndep f ↔ s.SupIndep (f ∘ g) := by refine ⟨fun hs t ht i hi hit => ?_, fun hs => ?_⟩ · rw [← sup_map] exact hs (map_subset_map.2 ht) ((mem_map' _).2 hi) (by rwa [mem_map']) · classical rw [map_eq_image] exact hs.image @[simp] theorem supIndep_pair [DecidableEq ι] {i j : ι} (hij : i ≠ j) : ({i, j} : Finset ι).SupIndep f ↔ Disjoint (f i) (f j) := by suffices Disjoint (f i) (f j) → Disjoint (f j) ((Finset.erase {i, j} j).sup f) by simpa [supIndep_iff_disjoint_erase, hij] rw [pair_comm] simp [hij.symm, disjoint_comm] theorem supIndep_univ_bool (f : Bool → α) : (Finset.univ : Finset Bool).SupIndep f ↔ Disjoint (f false) (f true) := haveI : true ≠ false := by simp only [Ne, not_false_iff, reduceCtorEq] (supIndep_pair this).trans disjoint_comm @[simp] theorem supIndep_univ_fin_two (f : Fin 2 → α) : (Finset.univ : Finset (Fin 2)).SupIndep f ↔ Disjoint (f 0) (f 1) := have : (0 : Fin 2) ≠ 1 := by simp supIndep_pair this @[simp] theorem supIndep_attach : (s.attach.SupIndep fun a => f a) ↔ s.SupIndep f := by simpa [Finset.attach_map_val] using (supIndep_map (s := s.attach) (g := .subtype _)).symm alias ⟨_, SupIndep.attach⟩ := supIndep_attach end Lattice section DistribLattice variable [DistribLattice α] [OrderBot α] {s : Finset ι} {f : ι → α} theorem supIndep_iff_pairwiseDisjoint : s.SupIndep f ↔ (s : Set ι).PairwiseDisjoint f := ⟨SupIndep.pairwiseDisjoint, fun hs _ ht _ hi hit => Finset.disjoint_sup_right.2 fun _ hj => hs hi (ht hj) (ne_of_mem_of_not_mem hj hit).symm⟩ alias ⟨_, _root_.Set.PairwiseDisjoint.supIndep⟩ := supIndep_iff_pairwiseDisjoint /-- Bind operation for `SupIndep`. -/ protected theorem SupIndep.sup [DecidableEq ι] {s : Finset ι'} {g : ι' → Finset ι} {f : ι → α} (hs : s.SupIndep fun i => (g i).sup f) (hg : ∀ i' ∈ s, (g i').SupIndep f) : (s.sup g).SupIndep f := by simp_rw [supIndep_iff_pairwiseDisjoint] at hs hg ⊢ rw [sup_eq_biUnion, coe_biUnion] exact hs.biUnion_finset hg /-- Bind operation for `SupIndep`. -/ protected theorem SupIndep.biUnion [DecidableEq ι] {s : Finset ι'} {g : ι' → Finset ι} {f : ι → α} (hs : s.SupIndep fun i => (g i).sup f) (hg : ∀ i' ∈ s, (g i').SupIndep f) : (s.biUnion g).SupIndep f := by rw [← sup_eq_biUnion] exact hs.sup hg /-- Bind operation for `SupIndep`. -/ protected theorem SupIndep.sigma {β : ι → Type*} {s : Finset ι} {g : ∀ i, Finset (β i)} {f : Sigma β → α} (hs : s.SupIndep fun i => (g i).sup fun b => f ⟨i, b⟩) (hg : ∀ i ∈ s, (g i).SupIndep fun b => f ⟨i, b⟩) : (s.sigma g).SupIndep f := by rintro t ht ⟨i, b⟩ hi hit rw [Finset.disjoint_sup_right] rintro ⟨j, c⟩ hj have hbc := (ne_of_mem_of_not_mem hj hit).symm replace hj := ht hj rw [mem_sigma] at hi hj obtain rfl | hij := eq_or_ne i j · exact (hg _ hj.1).pairwiseDisjoint hi.2 hj.2 (sigma_mk_injective.ne_iff.1 hbc) · refine (hs.pairwiseDisjoint hi.1 hj.1 hij).mono ?_ ?_ · convert le_sup (α := α) hi.2; simp · convert le_sup (α := α) hj.2; simp protected theorem SupIndep.product {s : Finset ι} {t : Finset ι'} {f : ι × ι' → α} (hs : s.SupIndep fun i => t.sup fun i' => f (i, i')) (ht : t.SupIndep fun i' => s.sup fun i => f (i, i')) : (s ×ˢ t).SupIndep f := by rintro u hu ⟨i, i'⟩ hi hiu rw [Finset.disjoint_sup_right] rintro ⟨j, j'⟩ hj have hij := (ne_of_mem_of_not_mem hj hiu).symm replace hj := hu hj rw [mem_product] at hi hj obtain rfl | hij := eq_or_ne i j · refine (ht.pairwiseDisjoint hi.2 hj.2 <| (Prod.mk_right_injective _).ne_iff.1 hij).mono ?_ ?_ · convert le_sup (α := α) hi.1; simp · convert le_sup (α := α) hj.1; simp · refine (hs.pairwiseDisjoint hi.1 hj.1 hij).mono ?_ ?_ · convert le_sup (α := α) hi.2; simp · convert le_sup (α := α) hj.2; simp theorem supIndep_product_iff {s : Finset ι} {t : Finset ι'} {f : ι × ι' → α} : (s.product t).SupIndep f ↔ (s.SupIndep fun i => t.sup fun i' => f (i, i')) ∧ t.SupIndep fun i' => s.sup fun i => f (i, i') := by refine ⟨?_, fun h => h.1.product h.2⟩ simp_rw [supIndep_iff_pairwiseDisjoint] refine fun h => ⟨fun i hi j hj hij => ?_, fun i hi j hj hij => ?_⟩ <;> simp_rw [Finset.disjoint_sup_left, Finset.disjoint_sup_right] <;> intro i' hi' j' hj' · exact h (mk_mem_product hi hi') (mk_mem_product hj hj') (ne_of_apply_ne Prod.fst hij) · exact h (mk_mem_product hi' hi) (mk_mem_product hj' hj) (ne_of_apply_ne Prod.snd hij) end DistribLattice end Finset /-! ### On complete lattices via `sSup` -/ section CompleteLattice variable [CompleteLattice α] open Set Function /-- An independent set of elements in a complete lattice is one in which every element is disjoint from the `Sup` of the rest. -/ def sSupIndep (s : Set α) : Prop := ∀ ⦃a⦄, a ∈ s → Disjoint a (sSup (s \ {a})) @[deprecated (since := "2024-11-24")] alias CompleteLattice.SetIndependent := sSupIndep variable {s : Set α} (hs : sSupIndep s) @[simp] theorem sSupIndep_empty : sSupIndep (∅ : Set α) := fun x hx => (Set.not_mem_empty x hx).elim @[deprecated (since := "2024-11-24")] alias CompleteLattice.setIndependent_empty := sSupIndep_empty include hs in theorem sSupIndep.mono {t : Set α} (hst : t ⊆ s) : sSupIndep t := fun _ ha => (hs (hst ha)).mono_right (sSup_le_sSup (diff_subset_diff_left hst)) @[deprecated (since := "2024-11-24")] alias CompleteLattice.SetIndependent.mono := sSupIndep.mono include hs in /-- If the elements of a set are independent, then any pair within that set is disjoint. -/ theorem sSupIndep.pairwiseDisjoint : s.PairwiseDisjoint id := fun _ hx y hy h => disjoint_sSup_right (hs hx) ((mem_diff y).mpr ⟨hy, h.symm⟩) @[deprecated (since := "2024-11-24")] alias CompleteLattice.SetIndependent.pairwiseDisjoint := sSupIndep.pairwiseDisjoint theorem sSupIndep_singleton (a : α) : sSupIndep ({a} : Set α) := fun i hi ↦ by simp_all @[deprecated (since := "2024-11-24")] alias CompleteLattice.setIndependent_singleton := sSupIndep_singleton theorem sSupIndep_pair {a b : α} (hab : a ≠ b) : sSupIndep ({a, b} : Set α) ↔ Disjoint a b := by constructor · intro h exact h.pairwiseDisjoint (mem_insert _ _) (mem_insert_of_mem _ (mem_singleton _)) hab · rintro h c ((rfl : c = a) | (rfl : c = b)) · convert h using 1 simp [hab, sSup_singleton] · convert h.symm using 1 simp [hab, sSup_singleton] @[deprecated (since := "2024-11-24")] alias CompleteLattice.setIndependent_pair := sSupIndep_pair include hs in /-- If the elements of a set are independent, then any element is disjoint from the `sSup` of some subset of the rest. -/ theorem sSupIndep.disjoint_sSup {x : α} {y : Set α} (hx : x ∈ s) (hy : y ⊆ s) (hxy : x ∉ y) : Disjoint x (sSup y) := by have := (hs.mono <| insert_subset_iff.mpr ⟨hx, hy⟩) (mem_insert x _) rw [insert_diff_of_mem _ (mem_singleton _), diff_singleton_eq_self hxy] at this exact this @[deprecated (since := "2024-11-24")] alias CompleteLattice.SetIndependent.disjoint_sSup := sSupIndep.disjoint_sSup /-- An independent indexed family of elements in a complete lattice is one in which every element is disjoint from the `iSup` of the rest. Example: an indexed family of non-zero elements in a vector space is linearly independent iff the indexed family of subspaces they generate is independent in this sense. Example: an indexed family of submodules of a module is independent in this sense if and only the natural map from the direct sum of the submodules to the module is injective. -/ def iSupIndep {ι : Sort*} {α : Type*} [CompleteLattice α] (t : ι → α) : Prop := ∀ i : ι, Disjoint (t i) (⨆ (j) (_ : j ≠ i), t j) @[deprecated (since := "2024-11-24")] alias CompleteLattice.Independent := iSupIndep theorem sSupIndep_iff {α : Type*} [CompleteLattice α] (s : Set α) : sSupIndep s ↔ iSupIndep ((↑) : s → α) := by simp_rw [iSupIndep, sSupIndep, SetCoe.forall, sSup_eq_iSup] refine forall₂_congr fun a ha => ?_ simp [iSup_subtype, iSup_and] @[deprecated (since := "2024-11-24")] alias CompleteLattice.setIndependent_iff := sSupIndep_iff variable {t : ι → α} (ht : iSupIndep t) theorem iSupIndep_def : iSupIndep t ↔ ∀ i, Disjoint (t i) (⨆ (j) (_ : j ≠ i), t j) := Iff.rfl @[deprecated (since := "2024-11-24")] alias CompleteLattice.independent_def := iSupIndep_def theorem iSupIndep_def' : iSupIndep t ↔ ∀ i, Disjoint (t i) (sSup (t '' { j | j ≠ i })) := by simp_rw [sSup_image] rfl @[deprecated (since := "2024-11-24")] alias CompleteLattice.independent_def' := iSupIndep_def' theorem iSupIndep_def'' : iSupIndep t ↔ ∀ i, Disjoint (t i) (sSup { a | ∃ j ≠ i, t j = a }) := by rw [iSupIndep_def'] aesop @[deprecated (since := "2024-11-24")] alias CompleteLattice.independent_def'' := iSupIndep_def'' @[simp] theorem iSupIndep_empty (t : Empty → α) : iSupIndep t := nofun @[deprecated (since := "2024-11-24")] alias CompleteLattice.independent_empty := iSupIndep_empty @[simp] theorem iSupIndep_pempty (t : PEmpty → α) : iSupIndep t := nofun @[deprecated (since := "2024-11-24")] alias CompleteLattice.independent_pempty := iSupIndep_pempty include ht in /-- If the elements of a set are independent, then any pair within that set is disjoint. -/ theorem iSupIndep.pairwiseDisjoint : Pairwise (Disjoint on t) := fun x y h => disjoint_sSup_right (ht x) ⟨y, iSup_pos h.symm⟩ @[deprecated (since := "2024-11-24")] alias CompleteLattice.Independent.pairwiseDisjoint := iSupIndep.pairwiseDisjoint
theorem iSupIndep.mono {s t : ι → α} (hs : iSupIndep s) (hst : t ≤ s) : iSupIndep t := fun i => (hs i).mono (hst i) <| iSup₂_mono fun j _ => hst j
Mathlib/Order/SupIndep.lean
361
363
/- Copyright (c) 2021 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne -/ import Mathlib.MeasureTheory.Function.LpSeminorm.Basic import Mathlib.MeasureTheory.Integral.MeanInequalities /-! # Triangle inequality for `Lp`-seminorm In this file we prove several versions of the triangle inequality for the `Lp` seminorm, as well as simple corollaries. -/ open Filter open scoped ENNReal Topology namespace MeasureTheory variable {α E : Type*} {m : MeasurableSpace α} [NormedAddCommGroup E] {p : ℝ≥0∞} {q : ℝ} {μ : Measure α} {f g : α → E} theorem eLpNorm'_add_le (hf : AEStronglyMeasurable f μ) (hg : AEStronglyMeasurable g μ) (hq1 : 1 ≤ q) : eLpNorm' (f + g) q μ ≤ eLpNorm' f q μ + eLpNorm' g q μ := calc (∫⁻ a, (‖(f + g) a‖₊ : ℝ≥0∞) ^ q ∂μ) ^ (1 / q) ≤ (∫⁻ a, ((fun a => (‖f a‖₊ : ℝ≥0∞)) + fun a => (‖g a‖₊ : ℝ≥0∞)) a ^ q ∂μ) ^ (1 / q) := by gcongr with a simp only [Pi.add_apply, ← ENNReal.coe_add, ENNReal.coe_le_coe, nnnorm_add_le] _ ≤ eLpNorm' f q μ + eLpNorm' g q μ := ENNReal.lintegral_Lp_add_le hf.enorm hg.enorm hq1 theorem eLpNorm'_add_le_of_le_one (hf : AEStronglyMeasurable f μ) (hq0 : 0 ≤ q) (hq1 : q ≤ 1) : eLpNorm' (f + g) q μ ≤ (2 : ℝ≥0∞) ^ (1 / q - 1) * (eLpNorm' f q μ + eLpNorm' g q μ) := calc (∫⁻ a, (‖(f + g) a‖₊ : ℝ≥0∞) ^ q ∂μ) ^ (1 / q) ≤ (∫⁻ a, ((fun a => (‖f a‖₊ : ℝ≥0∞)) + fun a => (‖g a‖₊ : ℝ≥0∞)) a ^ q ∂μ) ^ (1 / q) := by gcongr with a simp only [Pi.add_apply, ← ENNReal.coe_add, ENNReal.coe_le_coe, nnnorm_add_le] _ ≤ (2 : ℝ≥0∞) ^ (1 / q - 1) * (eLpNorm' f q μ + eLpNorm' g q μ) := ENNReal.lintegral_Lp_add_le_of_le_one hf.enorm hq0 hq1 theorem eLpNormEssSup_add_le {f g : α → E} : eLpNormEssSup (f + g) μ ≤ eLpNormEssSup f μ + eLpNormEssSup g μ := by refine le_trans (essSup_mono_ae (Eventually.of_forall fun x => ?_)) (ENNReal.essSup_add_le _ _) simp_rw [Pi.add_apply, enorm_eq_nnnorm, ← ENNReal.coe_add, ENNReal.coe_le_coe] exact nnnorm_add_le _ _ theorem eLpNorm_add_le {f g : α → E} (hf : AEStronglyMeasurable f μ) (hg : AEStronglyMeasurable g μ) (hp1 : 1 ≤ p) : eLpNorm (f + g) p μ ≤ eLpNorm f p μ + eLpNorm g p μ := by by_cases hp0 : p = 0 · simp [hp0] by_cases hp_top : p = ∞ · simp [hp_top, eLpNormEssSup_add_le] have hp1_real : 1 ≤ p.toReal := by rwa [← ENNReal.toReal_one, ENNReal.toReal_le_toReal ENNReal.one_ne_top hp_top] repeat rw [eLpNorm_eq_eLpNorm' hp0 hp_top] exact eLpNorm'_add_le hf hg hp1_real /-- A constant for the inequality `‖f + g‖_{L^p} ≤ C * (‖f‖_{L^p} + ‖g‖_{L^p})`. It is equal to `1` for `p ≥ 1` or `p = 0`, and `2^(1/p-1)` in the more tricky interval `(0, 1)`. -/ noncomputable def LpAddConst (p : ℝ≥0∞) : ℝ≥0∞ := if p ∈ Set.Ioo (0 : ℝ≥0∞) 1 then (2 : ℝ≥0∞) ^ (1 / p.toReal - 1) else 1 theorem LpAddConst_of_one_le {p : ℝ≥0∞} (hp : 1 ≤ p) : LpAddConst p = 1 := by rw [LpAddConst, if_neg] intro h exact lt_irrefl _ (h.2.trans_le hp) theorem LpAddConst_zero : LpAddConst 0 = 1 := by rw [LpAddConst, if_neg] intro h
exact lt_irrefl _ h.1 theorem LpAddConst_lt_top (p : ℝ≥0∞) : LpAddConst p < ∞ := by rw [LpAddConst]
Mathlib/MeasureTheory/Function/LpSeminorm/TriangleInequality.lean
73
76
/- Copyright (c) 2022 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import Mathlib.Topology.MetricSpace.HausdorffDistance /-! # Topological study of spaces `Π (n : ℕ), E n` When `E n` are topological spaces, the space `Π (n : ℕ), E n` is naturally a topological space (with the product topology). When `E n` are uniform spaces, it also inherits a uniform structure. However, it does not inherit a canonical metric space structure of the `E n`. Nevertheless, one can put a noncanonical metric space structure (or rather, several of them). This is done in this file. ## Main definitions and results One can define a combinatorial distance on `Π (n : ℕ), E n`, as follows: * `PiNat.cylinder x n` is the set of points `y` with `x i = y i` for `i < n`. * `PiNat.firstDiff x y` is the first index at which `x i ≠ y i`. * `PiNat.dist x y` is equal to `(1/2) ^ (firstDiff x y)`. It defines a distance on `Π (n : ℕ), E n`, compatible with the topology when the `E n` have the discrete topology. * `PiNat.metricSpace`: the metric space structure, given by this distance. Not registered as an instance. This space is a complete metric space. * `PiNat.metricSpaceOfDiscreteUniformity`: the same metric space structure, but adjusting the uniformity defeqness when the `E n` already have the discrete uniformity. Not registered as an instance * `PiNat.metricSpaceNatNat`: the particular case of `ℕ → ℕ`, not registered as an instance. These results are used to construct continuous functions on `Π n, E n`: * `PiNat.exists_retraction_of_isClosed`: given a nonempty closed subset `s` of `Π (n : ℕ), E n`, there exists a retraction onto `s`, i.e., a continuous map from the whole space to `s` restricting to the identity on `s`. * `exists_nat_nat_continuous_surjective_of_completeSpace`: given any nonempty complete metric space with second-countable topology, there exists a continuous surjection from `ℕ → ℕ` onto this space. One can also put distances on `Π (i : ι), E i` when the spaces `E i` are metric spaces (not discrete in general), and `ι` is countable. * `PiCountable.dist` is the distance on `Π i, E i` given by `dist x y = ∑' i, min (1/2)^(encode i) (dist (x i) (y i))`. * `PiCountable.metricSpace` is the corresponding metric space structure, adjusted so that the uniformity is definitionally the product uniformity. Not registered as an instance. -/ noncomputable section open Topology TopologicalSpace Set Metric Filter Function attribute [local simp] pow_le_pow_iff_right₀ one_lt_two inv_le_inv₀ zero_le_two zero_lt_two variable {E : ℕ → Type*} namespace PiNat /-! ### The firstDiff function -/ open Classical in /-- In a product space `Π n, E n`, then `firstDiff x y` is the first index at which `x` and `y` differ. If `x = y`, then by convention we set `firstDiff x x = 0`. -/ irreducible_def firstDiff (x y : ∀ n, E n) : ℕ := if h : x ≠ y then Nat.find (ne_iff.1 h) else 0 theorem apply_firstDiff_ne {x y : ∀ n, E n} (h : x ≠ y) : x (firstDiff x y) ≠ y (firstDiff x y) := by rw [firstDiff_def, dif_pos h] classical exact Nat.find_spec (ne_iff.1 h) theorem apply_eq_of_lt_firstDiff {x y : ∀ n, E n} {n : ℕ} (hn : n < firstDiff x y) : x n = y n := by rw [firstDiff_def] at hn split_ifs at hn with h · convert Nat.find_min (ne_iff.1 h) hn simp · exact (not_lt_zero' hn).elim theorem firstDiff_comm (x y : ∀ n, E n) : firstDiff x y = firstDiff y x := by classical simp only [firstDiff_def, ne_comm] theorem min_firstDiff_le (x y z : ∀ n, E n) (h : x ≠ z) : min (firstDiff x y) (firstDiff y z) ≤ firstDiff x z := by by_contra! H rw [lt_min_iff] at H refine apply_firstDiff_ne h ?_ calc x (firstDiff x z) = y (firstDiff x z) := apply_eq_of_lt_firstDiff H.1 _ = z (firstDiff x z) := apply_eq_of_lt_firstDiff H.2 /-! ### Cylinders -/ /-- In a product space `Π n, E n`, the cylinder set of length `n` around `x`, denoted `cylinder x n`, is the set of sequences `y` that coincide with `x` on the first `n` symbols, i.e., such that `y i = x i` for all `i < n`. -/ def cylinder (x : ∀ n, E n) (n : ℕ) : Set (∀ n, E n) := { y | ∀ i, i < n → y i = x i } theorem cylinder_eq_pi (x : ∀ n, E n) (n : ℕ) : cylinder x n = Set.pi (Finset.range n : Set ℕ) fun i : ℕ => {x i} := by ext y simp [cylinder] @[simp] theorem cylinder_zero (x : ∀ n, E n) : cylinder x 0 = univ := by simp [cylinder_eq_pi] theorem cylinder_anti (x : ∀ n, E n) {m n : ℕ} (h : m ≤ n) : cylinder x n ⊆ cylinder x m := fun _y hy i hi => hy i (hi.trans_le h) @[simp] theorem mem_cylinder_iff {x y : ∀ n, E n} {n : ℕ} : y ∈ cylinder x n ↔ ∀ i < n, y i = x i := Iff.rfl theorem self_mem_cylinder (x : ∀ n, E n) (n : ℕ) : x ∈ cylinder x n := by simp theorem mem_cylinder_iff_eq {x y : ∀ n, E n} {n : ℕ} : y ∈ cylinder x n ↔ cylinder y n = cylinder x n := by constructor · intro hy apply Subset.antisymm · intro z hz i hi rw [← hy i hi] exact hz i hi · intro z hz i hi rw [hy i hi] exact hz i hi · intro h rw [← h] exact self_mem_cylinder _ _ theorem mem_cylinder_comm (x y : ∀ n, E n) (n : ℕ) : y ∈ cylinder x n ↔ x ∈ cylinder y n := by simp [mem_cylinder_iff_eq, eq_comm] theorem mem_cylinder_iff_le_firstDiff {x y : ∀ n, E n} (hne : x ≠ y) (i : ℕ) : x ∈ cylinder y i ↔ i ≤ firstDiff x y := by constructor · intro h by_contra! exact apply_firstDiff_ne hne (h _ this) · intro hi j hj exact apply_eq_of_lt_firstDiff (hj.trans_le hi) theorem mem_cylinder_firstDiff (x y : ∀ n, E n) : x ∈ cylinder y (firstDiff x y) := fun _i hi => apply_eq_of_lt_firstDiff hi theorem cylinder_eq_cylinder_of_le_firstDiff (x y : ∀ n, E n) {n : ℕ} (hn : n ≤ firstDiff x y) : cylinder x n = cylinder y n := by rw [← mem_cylinder_iff_eq] intro i hi exact apply_eq_of_lt_firstDiff (hi.trans_le hn) theorem iUnion_cylinder_update (x : ∀ n, E n) (n : ℕ) : ⋃ k, cylinder (update x n k) (n + 1) = cylinder x n := by ext y simp only [mem_cylinder_iff, mem_iUnion] constructor · rintro ⟨k, hk⟩ i hi simpa [hi.ne] using hk i (Nat.lt_succ_of_lt hi) · intro H refine ⟨y n, fun i hi => ?_⟩ rcases Nat.lt_succ_iff_lt_or_eq.1 hi with (h'i | rfl) · simp [H i h'i, h'i.ne] · simp theorem update_mem_cylinder (x : ∀ n, E n) (n : ℕ) (y : E n) : update x n y ∈ cylinder x n := mem_cylinder_iff.2 fun i hi => by simp [hi.ne] section Res variable {α : Type*} open List /-- In the case where `E` has constant value `α`, the cylinder `cylinder x n` can be identified with the element of `List α` consisting of the first `n` entries of `x`. See `cylinder_eq_res`. We call this list `res x n`, the restriction of `x` to `n`. -/ def res (x : ℕ → α) : ℕ → List α | 0 => nil | Nat.succ n => x n :: res x n @[simp] theorem res_zero (x : ℕ → α) : res x 0 = @nil α := rfl @[simp] theorem res_succ (x : ℕ → α) (n : ℕ) : res x n.succ = x n :: res x n := rfl @[simp] theorem res_length (x : ℕ → α) (n : ℕ) : (res x n).length = n := by induction n <;> simp [*] /-- The restrictions of `x` and `y` to `n` are equal if and only if `x m = y m` for all `m < n`. -/ theorem res_eq_res {x y : ℕ → α} {n : ℕ} : res x n = res y n ↔ ∀ ⦃m⦄, m < n → x m = y m := by constructor <;> intro h · induction n with | zero => simp | succ n ih => intro m hm rw [Nat.lt_succ_iff_lt_or_eq] at hm simp only [res_succ, cons.injEq] at h rcases hm with hm | hm · exact ih h.2 hm rw [hm] exact h.1 · induction n with | zero => simp | succ n ih => simp only [res_succ, cons.injEq] refine ⟨h (Nat.lt_succ_self _), ih fun m hm => ?_⟩ exact h (hm.trans (Nat.lt_succ_self _)) theorem res_injective : Injective (@res α) := by intro x y h ext n apply res_eq_res.mp _ (Nat.lt_succ_self _) rw [h] /-- `cylinder x n` is equal to the set of sequences `y` with the same restriction to `n` as `x`. -/ theorem cylinder_eq_res (x : ℕ → α) (n : ℕ) : cylinder x n = { y | res y n = res x n } := by ext y dsimp [cylinder] rw [res_eq_res] end Res /-! ### A distance function on `Π n, E n` We define a distance function on `Π n, E n`, given by `dist x y = (1/2)^n` where `n` is the first index at which `x` and `y` differ. When each `E n` has the discrete topology, this distance will define the right topology on the product space. We do not record a global `Dist` instance nor a `MetricSpace` instance, as other distances may be used on these spaces, but we register them as local instances in this section. -/ open Classical in /-- The distance function on a product space `Π n, E n`, given by `dist x y = (1/2)^n` where `n` is the first index at which `x` and `y` differ. -/ protected def dist : Dist (∀ n, E n) := ⟨fun x y => if x ≠ y then (1 / 2 : ℝ) ^ firstDiff x y else 0⟩ attribute [local instance] PiNat.dist theorem dist_eq_of_ne {x y : ∀ n, E n} (h : x ≠ y) : dist x y = (1 / 2 : ℝ) ^ firstDiff x y := by simp [dist, h] protected theorem dist_self (x : ∀ n, E n) : dist x x = 0 := by simp [dist] protected theorem dist_comm (x y : ∀ n, E n) : dist x y = dist y x := by classical simp [dist, @eq_comm _ x y, firstDiff_comm] protected theorem dist_nonneg (x y : ∀ n, E n) : 0 ≤ dist x y := by rcases eq_or_ne x y with (rfl | h) · simp [dist] · simp [dist, h, zero_le_two] theorem dist_triangle_nonarch (x y z : ∀ n, E n) : dist x z ≤ max (dist x y) (dist y z) := by rcases eq_or_ne x z with (rfl | hxz) · simp [PiNat.dist_self x, PiNat.dist_nonneg] rcases eq_or_ne x y with (rfl | hxy) · simp rcases eq_or_ne y z with (rfl | hyz) · simp simp only [dist_eq_of_ne, hxz, hxy, hyz, inv_le_inv₀, one_div, inv_pow, zero_lt_two, Ne, not_false_iff, le_max_iff, pow_le_pow_iff_right₀, one_lt_two, pow_pos, min_le_iff.1 (min_firstDiff_le x y z hxz)] protected theorem dist_triangle (x y z : ∀ n, E n) : dist x z ≤ dist x y + dist y z := calc dist x z ≤ max (dist x y) (dist y z) := dist_triangle_nonarch x y z _ ≤ dist x y + dist y z := max_le_add_of_nonneg (PiNat.dist_nonneg _ _) (PiNat.dist_nonneg _ _) protected theorem eq_of_dist_eq_zero (x y : ∀ n, E n) (hxy : dist x y = 0) : x = y := by rcases eq_or_ne x y with (rfl | h); · rfl simp [dist_eq_of_ne h] at hxy theorem mem_cylinder_iff_dist_le {x y : ∀ n, E n} {n : ℕ} : y ∈ cylinder x n ↔ dist y x ≤ (1 / 2) ^ n := by rcases eq_or_ne y x with (rfl | hne) · simp [PiNat.dist_self] suffices (∀ i : ℕ, i < n → y i = x i) ↔ n ≤ firstDiff y x by simpa [dist_eq_of_ne hne] constructor · intro hy by_contra! H exact apply_firstDiff_ne hne (hy _ H) · intro h i hi exact apply_eq_of_lt_firstDiff (hi.trans_le h) theorem apply_eq_of_dist_lt {x y : ∀ n, E n} {n : ℕ} (h : dist x y < (1 / 2) ^ n) {i : ℕ} (hi : i ≤ n) : x i = y i := by rcases eq_or_ne x y with (rfl | hne) · rfl have : n < firstDiff x y := by simpa [dist_eq_of_ne hne, inv_lt_inv₀, pow_lt_pow_iff_right₀, one_lt_two] using h exact apply_eq_of_lt_firstDiff (hi.trans_lt this) /-- A function to a pseudo-metric-space is `1`-Lipschitz if and only if points in the same cylinder of length `n` are sent to points within distance `(1/2)^n`. Not expressed using `LipschitzWith` as we don't have a metric space structure -/ theorem lipschitz_with_one_iff_forall_dist_image_le_of_mem_cylinder {α : Type*} [PseudoMetricSpace α] {f : (∀ n, E n) → α} : (∀ x y : ∀ n, E n, dist (f x) (f y) ≤ dist x y) ↔ ∀ x y n, y ∈ cylinder x n → dist (f x) (f y) ≤ (1 / 2) ^ n := by constructor · intro H x y n hxy apply (H x y).trans rw [PiNat.dist_comm] exact mem_cylinder_iff_dist_le.1 hxy · intro H x y rcases eq_or_ne x y with (rfl | hne) · simp [PiNat.dist_nonneg] rw [dist_eq_of_ne hne] apply H x y (firstDiff x y) rw [firstDiff_comm] exact mem_cylinder_firstDiff _ _ variable (E) variable [∀ n, TopologicalSpace (E n)] [∀ n, DiscreteTopology (E n)] theorem isOpen_cylinder (x : ∀ n, E n) (n : ℕ) : IsOpen (cylinder x n) := by rw [PiNat.cylinder_eq_pi] exact isOpen_set_pi (Finset.range n).finite_toSet fun a _ => isOpen_discrete _ theorem isTopologicalBasis_cylinders : IsTopologicalBasis { s : Set (∀ n, E n) | ∃ (x : ∀ n, E n) (n : ℕ), s = cylinder x n } := by apply isTopologicalBasis_of_isOpen_of_nhds · rintro u ⟨x, n, rfl⟩ apply isOpen_cylinder · intro x u hx u_open obtain ⟨v, ⟨U, F, -, rfl⟩, xU, Uu⟩ : ∃ v ∈ { S : Set (∀ i : ℕ, E i) | ∃ (U : ∀ i : ℕ, Set (E i)) (F : Finset ℕ), (∀ i : ℕ, i ∈ F → U i ∈ { s : Set (E i) | IsOpen s }) ∧ S = (F : Set ℕ).pi U }, x ∈ v ∧ v ⊆ u := (isTopologicalBasis_pi fun n : ℕ => isTopologicalBasis_opens).exists_subset_of_mem_open hx u_open rcases Finset.bddAbove F with ⟨n, hn⟩ refine ⟨cylinder x (n + 1), ⟨x, n + 1, rfl⟩, self_mem_cylinder _ _, Subset.trans ?_ Uu⟩ intro y hy suffices ∀ i : ℕ, i ∈ F → y i ∈ U i by simpa intro i hi have : y i = x i := mem_cylinder_iff.1 hy i ((hn hi).trans_lt (lt_add_one n)) rw [this] simp only [Set.mem_pi, Finset.mem_coe] at xU exact xU i hi variable {E} theorem isOpen_iff_dist (s : Set (∀ n, E n)) : IsOpen s ↔ ∀ x ∈ s, ∃ ε > 0, ∀ y, dist x y < ε → y ∈ s := by constructor · intro hs x hx obtain ⟨v, ⟨y, n, rfl⟩, h'x, h's⟩ : ∃ v ∈ { s | ∃ (x : ∀ n : ℕ, E n) (n : ℕ), s = cylinder x n }, x ∈ v ∧ v ⊆ s := (isTopologicalBasis_cylinders E).exists_subset_of_mem_open hx hs rw [← mem_cylinder_iff_eq.1 h'x] at h's exact
⟨(1 / 2 : ℝ) ^ n, by simp, fun y hy => h's fun i hi => (apply_eq_of_dist_lt hy hi.le).symm⟩ · intro h refine (isTopologicalBasis_cylinders E).isOpen_iff.2 fun x hx => ?_ rcases h x hx with ⟨ε, εpos, hε⟩ obtain ⟨n, hn⟩ : ∃ n : ℕ, (1 / 2 : ℝ) ^ n < ε := exists_pow_lt_of_lt_one εpos one_half_lt_one refine ⟨cylinder x n, ⟨x, n, rfl⟩, self_mem_cylinder x n, fun y hy => hε y ?_⟩ rw [PiNat.dist_comm] exact (mem_cylinder_iff_dist_le.1 hy).trans_lt hn /-- Metric space structure on `Π (n : ℕ), E n` when the spaces `E n` have the discrete topology, where the distance is given by `dist x y = (1/2)^n`, where `n` is the smallest index where `x` and `y` differ. Not registered as a global instance by default. Warning: this definition makes sure that the topology is defeq to the original product topology, but it does not take care of a possible uniformity. If the `E n` have a uniform structure, then there will be two non-defeq uniform structures on `Π n, E n`, the product one and the one coming from the metric structure. In this case, use `metricSpaceOfDiscreteUniformity` instead. -/ protected def metricSpace : MetricSpace (∀ n, E n) := MetricSpace.ofDistTopology dist PiNat.dist_self PiNat.dist_comm PiNat.dist_triangle isOpen_iff_dist PiNat.eq_of_dist_eq_zero /-- Metric space structure on `Π (n : ℕ), E n` when the spaces `E n` have the discrete uniformity,
Mathlib/Topology/MetricSpace/PiNat.lean
365
385
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Yury Kudryashov, Sébastien Gouëzel, Rémy Degenne -/ import Mathlib.MeasureTheory.Integral.Bochner.Basic import Mathlib.MeasureTheory.Integral.Bochner.L1 import Mathlib.MeasureTheory.Integral.Bochner.VitaliCaratheodory deprecated_module (since := "2025-04-13")
Mathlib/MeasureTheory/Integral/Bochner.lean
470
472
/- Copyright (c) 2021 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Yury Kudryashov, Sébastien Gouëzel, Rémy Degenne -/ import Mathlib.MeasureTheory.Integral.FinMeasAdditive /-! # Extension of a linear function from indicators to L1 Given `T : Set α → E →L[ℝ] F` with `DominatedFinMeasAdditive μ T C`, we construct an extension of `T` to integrable simple functions, which are finite sums of indicators of measurable sets with finite measure, then to integrable functions, which are limits of integrable simple functions. The main result is a continuous linear map `(α →₁[μ] E) →L[ℝ] F`. This extension process is used to define the Bochner integral in the `Mathlib.MeasureTheory.Integral.Bochner.Basic` file and the conditional expectation of an integrable function in `Mathlib.MeasureTheory.Function.ConditionalExpectation.CondexpL1`. ## Main definitions - `setToL1 (hT : DominatedFinMeasAdditive μ T C) : (α →₁[μ] E) →L[ℝ] F`: the extension of `T` from indicators to L1. - `setToFun μ T (hT : DominatedFinMeasAdditive μ T C) (f : α → E) : F`: a version of the extension which applies to functions (with value 0 if the function is not integrable). ## Properties For most properties of `setToFun`, we provide two lemmas. One version uses hypotheses valid on all sets, like `T = T'`, and a second version which uses a primed name uses hypotheses on measurable sets with finite measure, like `∀ s, MeasurableSet s → μ s < ∞ → T s = T' s`. The lemmas listed here don't show all hypotheses. Refer to the actual lemmas for details. Linearity: - `setToFun_zero_left : setToFun μ 0 hT f = 0` - `setToFun_add_left : setToFun μ (T + T') _ f = setToFun μ T hT f + setToFun μ T' hT' f` - `setToFun_smul_left : setToFun μ (fun s ↦ c • (T s)) (hT.smul c) f = c • setToFun μ T hT f` - `setToFun_zero : setToFun μ T hT (0 : α → E) = 0` - `setToFun_neg : setToFun μ T hT (-f) = - setToFun μ T hT f` If `f` and `g` are integrable: - `setToFun_add : setToFun μ T hT (f + g) = setToFun μ T hT f + setToFun μ T hT g` - `setToFun_sub : setToFun μ T hT (f - g) = setToFun μ T hT f - setToFun μ T hT g` If `T` is verifies `∀ c : 𝕜, ∀ s x, T s (c • x) = c • T s x`: - `setToFun_smul : setToFun μ T hT (c • f) = c • setToFun μ T hT f` Other: - `setToFun_congr_ae (h : f =ᵐ[μ] g) : setToFun μ T hT f = setToFun μ T hT g` - `setToFun_measure_zero (h : μ = 0) : setToFun μ T hT f = 0` If the space is also an ordered additive group with an order closed topology and `T` is such that `0 ≤ T s x` for `0 ≤ x`, we also prove order-related properties: - `setToFun_mono_left (h : ∀ s x, T s x ≤ T' s x) : setToFun μ T hT f ≤ setToFun μ T' hT' f` - `setToFun_nonneg (hf : 0 ≤ᵐ[μ] f) : 0 ≤ setToFun μ T hT f` - `setToFun_mono (hfg : f ≤ᵐ[μ] g) : setToFun μ T hT f ≤ setToFun μ T hT g` -/ noncomputable section open scoped Topology NNReal open Set Filter TopologicalSpace ENNReal namespace MeasureTheory variable {α E F F' G 𝕜 : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] [NormedAddCommGroup F] [NormedSpace ℝ F] [NormedAddCommGroup F'] [NormedSpace ℝ F'] [NormedAddCommGroup G] {m : MeasurableSpace α} {μ : Measure α} namespace L1 open AEEqFun Lp.simpleFunc Lp namespace SimpleFunc theorem norm_eq_sum_mul (f : α →₁ₛ[μ] G) : ‖f‖ = ∑ x ∈ (toSimpleFunc f).range, μ.real (toSimpleFunc f ⁻¹' {x}) * ‖x‖ := by rw [norm_toSimpleFunc, eLpNorm_one_eq_lintegral_enorm] have h_eq := SimpleFunc.map_apply (‖·‖ₑ) (toSimpleFunc f) simp_rw [← h_eq, measureReal_def] rw [SimpleFunc.lintegral_eq_lintegral, SimpleFunc.map_lintegral, ENNReal.toReal_sum] · congr ext1 x rw [ENNReal.toReal_mul, mul_comm, ← ofReal_norm_eq_enorm, ENNReal.toReal_ofReal (norm_nonneg _)] · intro x _ by_cases hx0 : x = 0 · rw [hx0]; simp · exact ENNReal.mul_ne_top ENNReal.coe_ne_top (SimpleFunc.measure_preimage_lt_top_of_integrable _ (SimpleFunc.integrable f) hx0).ne section SetToL1S variable [NormedField 𝕜] [NormedSpace 𝕜 E] attribute [local instance] Lp.simpleFunc.module attribute [local instance] Lp.simpleFunc.normedSpace /-- Extend `Set α → (E →L[ℝ] F')` to `(α →₁ₛ[μ] E) → F'`. -/ def setToL1S (T : Set α → E →L[ℝ] F) (f : α →₁ₛ[μ] E) : F := (toSimpleFunc f).setToSimpleFunc T theorem setToL1S_eq_setToSimpleFunc (T : Set α → E →L[ℝ] F) (f : α →₁ₛ[μ] E) : setToL1S T f = (toSimpleFunc f).setToSimpleFunc T := rfl @[simp] theorem setToL1S_zero_left (f : α →₁ₛ[μ] E) : setToL1S (0 : Set α → E →L[ℝ] F) f = 0 := SimpleFunc.setToSimpleFunc_zero _ theorem setToL1S_zero_left' {T : Set α → E →L[ℝ] F} (h_zero : ∀ s, MeasurableSet s → μ s < ∞ → T s = 0) (f : α →₁ₛ[μ] E) : setToL1S T f = 0 := SimpleFunc.setToSimpleFunc_zero' h_zero _ (SimpleFunc.integrable f) theorem setToL1S_congr (T : Set α → E →L[ℝ] F) (h_zero : ∀ s, MeasurableSet s → μ s = 0 → T s = 0) (h_add : FinMeasAdditive μ T) {f g : α →₁ₛ[μ] E} (h : toSimpleFunc f =ᵐ[μ] toSimpleFunc g) : setToL1S T f = setToL1S T g := SimpleFunc.setToSimpleFunc_congr T h_zero h_add (SimpleFunc.integrable f) h theorem setToL1S_congr_left (T T' : Set α → E →L[ℝ] F) (h : ∀ s, MeasurableSet s → μ s < ∞ → T s = T' s) (f : α →₁ₛ[μ] E) : setToL1S T f = setToL1S T' f := SimpleFunc.setToSimpleFunc_congr_left T T' h (simpleFunc.toSimpleFunc f) (SimpleFunc.integrable f) /-- `setToL1S` does not change if we replace the measure `μ` by `μ'` with `μ ≪ μ'`. The statement uses two functions `f` and `f'` because they have to belong to different types, but morally these are the same function (we have `f =ᵐ[μ] f'`). -/ theorem setToL1S_congr_measure {μ' : Measure α} (T : Set α → E →L[ℝ] F) (h_zero : ∀ s, MeasurableSet s → μ s = 0 → T s = 0) (h_add : FinMeasAdditive μ T) (hμ : μ ≪ μ') (f : α →₁ₛ[μ] E) (f' : α →₁ₛ[μ'] E) (h : (f : α → E) =ᵐ[μ] f') : setToL1S T f = setToL1S T f' := by refine SimpleFunc.setToSimpleFunc_congr T h_zero h_add (SimpleFunc.integrable f) ?_ refine (toSimpleFunc_eq_toFun f).trans ?_ suffices (f' : α → E) =ᵐ[μ] simpleFunc.toSimpleFunc f' from h.trans this have goal' : (f' : α → E) =ᵐ[μ'] simpleFunc.toSimpleFunc f' := (toSimpleFunc_eq_toFun f').symm exact hμ.ae_eq goal' theorem setToL1S_add_left (T T' : Set α → E →L[ℝ] F) (f : α →₁ₛ[μ] E) : setToL1S (T + T') f = setToL1S T f + setToL1S T' f := SimpleFunc.setToSimpleFunc_add_left T T' theorem setToL1S_add_left' (T T' T'' : Set α → E →L[ℝ] F) (h_add : ∀ s, MeasurableSet s → μ s < ∞ → T'' s = T s + T' s) (f : α →₁ₛ[μ] E) : setToL1S T'' f = setToL1S T f + setToL1S T' f := SimpleFunc.setToSimpleFunc_add_left' T T' T'' h_add (SimpleFunc.integrable f) theorem setToL1S_smul_left (T : Set α → E →L[ℝ] F) (c : ℝ) (f : α →₁ₛ[μ] E) : setToL1S (fun s => c • T s) f = c • setToL1S T f := SimpleFunc.setToSimpleFunc_smul_left T c _ theorem setToL1S_smul_left' (T T' : Set α → E →L[ℝ] F) (c : ℝ) (h_smul : ∀ s, MeasurableSet s → μ s < ∞ → T' s = c • T s) (f : α →₁ₛ[μ] E) : setToL1S T' f = c • setToL1S T f := SimpleFunc.setToSimpleFunc_smul_left' T T' c h_smul (SimpleFunc.integrable f) theorem setToL1S_add (T : Set α → E →L[ℝ] F) (h_zero : ∀ s, MeasurableSet s → μ s = 0 → T s = 0) (h_add : FinMeasAdditive μ T) (f g : α →₁ₛ[μ] E) : setToL1S T (f + g) = setToL1S T f + setToL1S T g := by simp_rw [setToL1S] rw [← SimpleFunc.setToSimpleFunc_add T h_add (SimpleFunc.integrable f) (SimpleFunc.integrable g)] exact SimpleFunc.setToSimpleFunc_congr T h_zero h_add (SimpleFunc.integrable _) (add_toSimpleFunc f g) theorem setToL1S_neg {T : Set α → E →L[ℝ] F} (h_zero : ∀ s, MeasurableSet s → μ s = 0 → T s = 0) (h_add : FinMeasAdditive μ T) (f : α →₁ₛ[μ] E) : setToL1S T (-f) = -setToL1S T f := by simp_rw [setToL1S] have : simpleFunc.toSimpleFunc (-f) =ᵐ[μ] ⇑(-simpleFunc.toSimpleFunc f) := neg_toSimpleFunc f rw [SimpleFunc.setToSimpleFunc_congr T h_zero h_add (SimpleFunc.integrable _) this] exact SimpleFunc.setToSimpleFunc_neg T h_add (SimpleFunc.integrable f) theorem setToL1S_sub {T : Set α → E →L[ℝ] F} (h_zero : ∀ s, MeasurableSet s → μ s = 0 → T s = 0) (h_add : FinMeasAdditive μ T) (f g : α →₁ₛ[μ] E) : setToL1S T (f - g) = setToL1S T f - setToL1S T g := by rw [sub_eq_add_neg, setToL1S_add T h_zero h_add, setToL1S_neg h_zero h_add, sub_eq_add_neg] theorem setToL1S_smul_real (T : Set α → E →L[ℝ] F) (h_zero : ∀ s, MeasurableSet s → μ s = 0 → T s = 0) (h_add : FinMeasAdditive μ T) (c : ℝ) (f : α →₁ₛ[μ] E) : setToL1S T (c • f) = c • setToL1S T f := by simp_rw [setToL1S] rw [← SimpleFunc.setToSimpleFunc_smul_real T h_add c (SimpleFunc.integrable f)] refine SimpleFunc.setToSimpleFunc_congr T h_zero h_add (SimpleFunc.integrable _) ?_ exact smul_toSimpleFunc c f theorem setToL1S_smul {E} [NormedAddCommGroup E] [NormedSpace ℝ E] [NormedSpace 𝕜 E] [DistribSMul 𝕜 F] (T : Set α → E →L[ℝ] F) (h_zero : ∀ s, MeasurableSet s → μ s = 0 → T s = 0) (h_add : FinMeasAdditive μ T) (h_smul : ∀ c : 𝕜, ∀ s x, T s (c • x) = c • T s x) (c : 𝕜) (f : α →₁ₛ[μ] E) : setToL1S T (c • f) = c • setToL1S T f := by simp_rw [setToL1S] rw [← SimpleFunc.setToSimpleFunc_smul T h_add h_smul c (SimpleFunc.integrable f)] refine SimpleFunc.setToSimpleFunc_congr T h_zero h_add (SimpleFunc.integrable _) ?_ exact smul_toSimpleFunc c f theorem norm_setToL1S_le (T : Set α → E →L[ℝ] F) {C : ℝ} (hT_norm : ∀ s, MeasurableSet s → μ s < ∞ → ‖T s‖ ≤ C * μ.real s) (f : α →₁ₛ[μ] E) : ‖setToL1S T f‖ ≤ C * ‖f‖ := by rw [setToL1S, norm_eq_sum_mul f] exact SimpleFunc.norm_setToSimpleFunc_le_sum_mul_norm_of_integrable T hT_norm _ (SimpleFunc.integrable f) theorem setToL1S_indicatorConst {T : Set α → E →L[ℝ] F} {s : Set α} (h_zero : ∀ s, MeasurableSet s → μ s = 0 → T s = 0) (h_add : FinMeasAdditive μ T) (hs : MeasurableSet s) (hμs : μ s < ∞) (x : E) : setToL1S T (simpleFunc.indicatorConst 1 hs hμs.ne x) = T s x := by have h_empty : T ∅ = 0 := h_zero _ MeasurableSet.empty measure_empty rw [setToL1S_eq_setToSimpleFunc] refine Eq.trans ?_ (SimpleFunc.setToSimpleFunc_indicator T h_empty hs x) refine SimpleFunc.setToSimpleFunc_congr T h_zero h_add (SimpleFunc.integrable _) ?_ exact toSimpleFunc_indicatorConst hs hμs.ne x theorem setToL1S_const [IsFiniteMeasure μ] {T : Set α → E →L[ℝ] F} (h_zero : ∀ s, MeasurableSet s → μ s = 0 → T s = 0) (h_add : FinMeasAdditive μ T) (x : E) : setToL1S T (simpleFunc.indicatorConst 1 MeasurableSet.univ (measure_ne_top μ _) x) = T univ x := setToL1S_indicatorConst h_zero h_add MeasurableSet.univ (measure_lt_top _ _) x section Order variable {G'' G' : Type*} [NormedAddCommGroup G'] [PartialOrder G'] [IsOrderedAddMonoid G'] [NormedSpace ℝ G'] [NormedAddCommGroup G''] [PartialOrder G''] [IsOrderedAddMonoid G''] [NormedSpace ℝ G''] {T : Set α → G'' →L[ℝ] G'} theorem setToL1S_mono_left {T T' : Set α → E →L[ℝ] G''} (hTT' : ∀ s x, T s x ≤ T' s x) (f : α →₁ₛ[μ] E) : setToL1S T f ≤ setToL1S T' f := SimpleFunc.setToSimpleFunc_mono_left T T' hTT' _ theorem setToL1S_mono_left' {T T' : Set α → E →L[ℝ] G''} (hTT' : ∀ s, MeasurableSet s → μ s < ∞ → ∀ x, T s x ≤ T' s x) (f : α →₁ₛ[μ] E) : setToL1S T f ≤ setToL1S T' f := SimpleFunc.setToSimpleFunc_mono_left' T T' hTT' _ (SimpleFunc.integrable f) omit [IsOrderedAddMonoid G''] in theorem setToL1S_nonneg (h_zero : ∀ s, MeasurableSet s → μ s = 0 → T s = 0) (h_add : FinMeasAdditive μ T) (hT_nonneg : ∀ s, MeasurableSet s → μ s < ∞ → ∀ x, 0 ≤ x → 0 ≤ T s x) {f : α →₁ₛ[μ] G''} (hf : 0 ≤ f) : 0 ≤ setToL1S T f := by simp_rw [setToL1S] obtain ⟨f', hf', hff'⟩ := exists_simpleFunc_nonneg_ae_eq hf replace hff' : simpleFunc.toSimpleFunc f =ᵐ[μ] f' := (Lp.simpleFunc.toSimpleFunc_eq_toFun f).trans hff' rw [SimpleFunc.setToSimpleFunc_congr _ h_zero h_add (SimpleFunc.integrable _) hff'] exact SimpleFunc.setToSimpleFunc_nonneg' T hT_nonneg _ hf' ((SimpleFunc.integrable f).congr hff') theorem setToL1S_mono (h_zero : ∀ s, MeasurableSet s → μ s = 0 → T s = 0) (h_add : FinMeasAdditive μ T) (hT_nonneg : ∀ s, MeasurableSet s → μ s < ∞ → ∀ x, 0 ≤ x → 0 ≤ T s x) {f g : α →₁ₛ[μ] G''} (hfg : f ≤ g) : setToL1S T f ≤ setToL1S T g := by rw [← sub_nonneg] at hfg ⊢ rw [← setToL1S_sub h_zero h_add] exact setToL1S_nonneg h_zero h_add hT_nonneg hfg end Order variable [NormedSpace 𝕜 F] variable (α E μ 𝕜) /-- Extend `Set α → E →L[ℝ] F` to `(α →₁ₛ[μ] E) →L[𝕜] F`. -/ def setToL1SCLM' {T : Set α → E →L[ℝ] F} {C : ℝ} (hT : DominatedFinMeasAdditive μ T C) (h_smul : ∀ c : 𝕜, ∀ s x, T s (c • x) = c • T s x) : (α →₁ₛ[μ] E) →L[𝕜] F := LinearMap.mkContinuous ⟨⟨setToL1S T, setToL1S_add T (fun _ => hT.eq_zero_of_measure_zero) hT.1⟩, setToL1S_smul T (fun _ => hT.eq_zero_of_measure_zero) hT.1 h_smul⟩ C fun f => norm_setToL1S_le T hT.2 f /-- Extend `Set α → E →L[ℝ] F` to `(α →₁ₛ[μ] E) →L[ℝ] F`. -/ def setToL1SCLM {T : Set α → E →L[ℝ] F} {C : ℝ} (hT : DominatedFinMeasAdditive μ T C) : (α →₁ₛ[μ] E) →L[ℝ] F := LinearMap.mkContinuous ⟨⟨setToL1S T, setToL1S_add T (fun _ => hT.eq_zero_of_measure_zero) hT.1⟩, setToL1S_smul_real T (fun _ => hT.eq_zero_of_measure_zero) hT.1⟩ C fun f => norm_setToL1S_le T hT.2 f variable {α E μ 𝕜} variable {T T' T'' : Set α → E →L[ℝ] F} {C C' C'' : ℝ} @[simp] theorem setToL1SCLM_zero_left (hT : DominatedFinMeasAdditive μ (0 : Set α → E →L[ℝ] F) C) (f : α →₁ₛ[μ] E) : setToL1SCLM α E μ hT f = 0 := setToL1S_zero_left _ theorem setToL1SCLM_zero_left' (hT : DominatedFinMeasAdditive μ T C) (h_zero : ∀ s, MeasurableSet s → μ s < ∞ → T s = 0) (f : α →₁ₛ[μ] E) : setToL1SCLM α E μ hT f = 0 := setToL1S_zero_left' h_zero f theorem setToL1SCLM_congr_left (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (h : T = T') (f : α →₁ₛ[μ] E) : setToL1SCLM α E μ hT f = setToL1SCLM α E μ hT' f := setToL1S_congr_left T T' (fun _ _ _ => by rw [h]) f theorem setToL1SCLM_congr_left' (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (h : ∀ s, MeasurableSet s → μ s < ∞ → T s = T' s) (f : α →₁ₛ[μ] E) : setToL1SCLM α E μ hT f = setToL1SCLM α E μ hT' f := setToL1S_congr_left T T' h f theorem setToL1SCLM_congr_measure {μ' : Measure α} (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ' T C') (hμ : μ ≪ μ') (f : α →₁ₛ[μ] E) (f' : α →₁ₛ[μ'] E) (h : (f : α → E) =ᵐ[μ] f') : setToL1SCLM α E μ hT f = setToL1SCLM α E μ' hT' f' := setToL1S_congr_measure T (fun _ => hT.eq_zero_of_measure_zero) hT.1 hμ _ _ h theorem setToL1SCLM_add_left (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (f : α →₁ₛ[μ] E) : setToL1SCLM α E μ (hT.add hT') f = setToL1SCLM α E μ hT f + setToL1SCLM α E μ hT' f := setToL1S_add_left T T' f theorem setToL1SCLM_add_left' (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (hT'' : DominatedFinMeasAdditive μ T'' C'') (h_add : ∀ s, MeasurableSet s → μ s < ∞ → T'' s = T s + T' s) (f : α →₁ₛ[μ] E) : setToL1SCLM α E μ hT'' f = setToL1SCLM α E μ hT f + setToL1SCLM α E μ hT' f := setToL1S_add_left' T T' T'' h_add f theorem setToL1SCLM_smul_left (c : ℝ) (hT : DominatedFinMeasAdditive μ T C) (f : α →₁ₛ[μ] E) : setToL1SCLM α E μ (hT.smul c) f = c • setToL1SCLM α E μ hT f := setToL1S_smul_left T c f theorem setToL1SCLM_smul_left' (c : ℝ) (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (h_smul : ∀ s, MeasurableSet s → μ s < ∞ → T' s = c • T s) (f : α →₁ₛ[μ] E) : setToL1SCLM α E μ hT' f = c • setToL1SCLM α E μ hT f := setToL1S_smul_left' T T' c h_smul f theorem norm_setToL1SCLM_le {T : Set α → E →L[ℝ] F} {C : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hC : 0 ≤ C) : ‖setToL1SCLM α E μ hT‖ ≤ C := LinearMap.mkContinuous_norm_le _ hC _ theorem norm_setToL1SCLM_le' {T : Set α → E →L[ℝ] F} {C : ℝ} (hT : DominatedFinMeasAdditive μ T C) : ‖setToL1SCLM α E μ hT‖ ≤ max C 0 := LinearMap.mkContinuous_norm_le' _ _ theorem setToL1SCLM_const [IsFiniteMeasure μ] {T : Set α → E →L[ℝ] F} {C : ℝ} (hT : DominatedFinMeasAdditive μ T C) (x : E) : setToL1SCLM α E μ hT (simpleFunc.indicatorConst 1 MeasurableSet.univ (measure_ne_top μ _) x) = T univ x := setToL1S_const (fun _ => hT.eq_zero_of_measure_zero) hT.1 x section Order variable {G' G'' : Type*} [NormedAddCommGroup G''] [PartialOrder G''] [IsOrderedAddMonoid G''] [NormedSpace ℝ G''] [NormedAddCommGroup G'] [PartialOrder G'] [IsOrderedAddMonoid G'] [NormedSpace ℝ G'] theorem setToL1SCLM_mono_left {T T' : Set α → E →L[ℝ] G''} {C C' : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (hTT' : ∀ s x, T s x ≤ T' s x) (f : α →₁ₛ[μ] E) : setToL1SCLM α E μ hT f ≤ setToL1SCLM α E μ hT' f := SimpleFunc.setToSimpleFunc_mono_left T T' hTT' _ theorem setToL1SCLM_mono_left' {T T' : Set α → E →L[ℝ] G''} {C C' : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (hTT' : ∀ s, MeasurableSet s → μ s < ∞ → ∀ x, T s x ≤ T' s x) (f : α →₁ₛ[μ] E) : setToL1SCLM α E μ hT f ≤ setToL1SCLM α E μ hT' f := SimpleFunc.setToSimpleFunc_mono_left' T T' hTT' _ (SimpleFunc.integrable f) omit [IsOrderedAddMonoid G'] in theorem setToL1SCLM_nonneg {T : Set α → G' →L[ℝ] G''} {C : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT_nonneg : ∀ s, MeasurableSet s → μ s < ∞ → ∀ x, 0 ≤ x → 0 ≤ T s x) {f : α →₁ₛ[μ] G'} (hf : 0 ≤ f) : 0 ≤ setToL1SCLM α G' μ hT f := setToL1S_nonneg (fun _ => hT.eq_zero_of_measure_zero) hT.1 hT_nonneg hf theorem setToL1SCLM_mono {T : Set α → G' →L[ℝ] G''} {C : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT_nonneg : ∀ s, MeasurableSet s → μ s < ∞ → ∀ x, 0 ≤ x → 0 ≤ T s x) {f g : α →₁ₛ[μ] G'} (hfg : f ≤ g) : setToL1SCLM α G' μ hT f ≤ setToL1SCLM α G' μ hT g := setToL1S_mono (fun _ => hT.eq_zero_of_measure_zero) hT.1 hT_nonneg hfg end Order end SetToL1S end SimpleFunc open SimpleFunc section SetToL1 attribute [local instance] Lp.simpleFunc.module attribute [local instance] Lp.simpleFunc.normedSpace variable (𝕜) [NontriviallyNormedField 𝕜] [NormedSpace 𝕜 E] [NormedSpace 𝕜 F] [CompleteSpace F] {T T' T'' : Set α → E →L[ℝ] F} {C C' C'' : ℝ} /-- Extend `Set α → (E →L[ℝ] F)` to `(α →₁[μ] E) →L[𝕜] F`. -/ def setToL1' (hT : DominatedFinMeasAdditive μ T C) (h_smul : ∀ c : 𝕜, ∀ s x, T s (c • x) = c • T s x) : (α →₁[μ] E) →L[𝕜] F := (setToL1SCLM' α E 𝕜 μ hT h_smul).extend (coeToLp α E 𝕜) (simpleFunc.denseRange one_ne_top) simpleFunc.isUniformInducing variable {𝕜} /-- Extend `Set α → E →L[ℝ] F` to `(α →₁[μ] E) →L[ℝ] F`. -/ def setToL1 (hT : DominatedFinMeasAdditive μ T C) : (α →₁[μ] E) →L[ℝ] F := (setToL1SCLM α E μ hT).extend (coeToLp α E ℝ) (simpleFunc.denseRange one_ne_top) simpleFunc.isUniformInducing theorem setToL1_eq_setToL1SCLM (hT : DominatedFinMeasAdditive μ T C) (f : α →₁ₛ[μ] E) : setToL1 hT f = setToL1SCLM α E μ hT f := uniformly_extend_of_ind simpleFunc.isUniformInducing (simpleFunc.denseRange one_ne_top) (setToL1SCLM α E μ hT).uniformContinuous _ theorem setToL1_eq_setToL1' (hT : DominatedFinMeasAdditive μ T C) (h_smul : ∀ c : 𝕜, ∀ s x, T s (c • x) = c • T s x) (f : α →₁[μ] E) : setToL1 hT f = setToL1' 𝕜 hT h_smul f := rfl @[simp] theorem setToL1_zero_left (hT : DominatedFinMeasAdditive μ (0 : Set α → E →L[ℝ] F) C) (f : α →₁[μ] E) : setToL1 hT f = 0 := by suffices setToL1 hT = 0 by rw [this]; simp refine ContinuousLinearMap.extend_unique (setToL1SCLM α E μ hT) _ _ _ _ ?_ ext1 f rw [setToL1SCLM_zero_left hT f, ContinuousLinearMap.zero_comp, ContinuousLinearMap.zero_apply] theorem setToL1_zero_left' (hT : DominatedFinMeasAdditive μ T C) (h_zero : ∀ s, MeasurableSet s → μ s < ∞ → T s = 0) (f : α →₁[μ] E) : setToL1 hT f = 0 := by suffices setToL1 hT = 0 by rw [this]; simp refine ContinuousLinearMap.extend_unique (setToL1SCLM α E μ hT) _ _ _ _ ?_ ext1 f rw [setToL1SCLM_zero_left' hT h_zero f, ContinuousLinearMap.zero_comp, ContinuousLinearMap.zero_apply] theorem setToL1_congr_left (T T' : Set α → E →L[ℝ] F) {C C' : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (h : T = T') (f : α →₁[μ] E) : setToL1 hT f = setToL1 hT' f := by suffices setToL1 hT = setToL1 hT' by rw [this] refine ContinuousLinearMap.extend_unique (setToL1SCLM α E μ hT) _ _ _ _ ?_ ext1 f suffices setToL1 hT' f = setToL1SCLM α E μ hT f by rw [← this]; simp [coeToLp] rw [setToL1_eq_setToL1SCLM] exact setToL1SCLM_congr_left hT' hT h.symm f theorem setToL1_congr_left' (T T' : Set α → E →L[ℝ] F) {C C' : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (h : ∀ s, MeasurableSet s → μ s < ∞ → T s = T' s) (f : α →₁[μ] E) : setToL1 hT f = setToL1 hT' f := by suffices setToL1 hT = setToL1 hT' by rw [this] refine ContinuousLinearMap.extend_unique (setToL1SCLM α E μ hT) _ _ _ _ ?_ ext1 f suffices setToL1 hT' f = setToL1SCLM α E μ hT f by rw [← this]; simp [coeToLp] rw [setToL1_eq_setToL1SCLM] exact (setToL1SCLM_congr_left' hT hT' h f).symm theorem setToL1_add_left (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (f : α →₁[μ] E) : setToL1 (hT.add hT') f = setToL1 hT f + setToL1 hT' f := by suffices setToL1 (hT.add hT') = setToL1 hT + setToL1 hT' by rw [this, ContinuousLinearMap.add_apply] refine ContinuousLinearMap.extend_unique (setToL1SCLM α E μ (hT.add hT')) _ _ _ _ ?_ ext1 f suffices setToL1 hT f + setToL1 hT' f = setToL1SCLM α E μ (hT.add hT') f by rw [← this]; simp [coeToLp] rw [setToL1_eq_setToL1SCLM, setToL1_eq_setToL1SCLM, setToL1SCLM_add_left hT hT'] theorem setToL1_add_left' (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (hT'' : DominatedFinMeasAdditive μ T'' C'') (h_add : ∀ s, MeasurableSet s → μ s < ∞ → T'' s = T s + T' s) (f : α →₁[μ] E) : setToL1 hT'' f = setToL1 hT f + setToL1 hT' f := by suffices setToL1 hT'' = setToL1 hT + setToL1 hT' by rw [this, ContinuousLinearMap.add_apply] refine ContinuousLinearMap.extend_unique (setToL1SCLM α E μ hT'') _ _ _ _ ?_ ext1 f suffices setToL1 hT f + setToL1 hT' f = setToL1SCLM α E μ hT'' f by rw [← this]; simp [coeToLp] rw [setToL1_eq_setToL1SCLM, setToL1_eq_setToL1SCLM, setToL1SCLM_add_left' hT hT' hT'' h_add] theorem setToL1_smul_left (hT : DominatedFinMeasAdditive μ T C) (c : ℝ) (f : α →₁[μ] E) : setToL1 (hT.smul c) f = c • setToL1 hT f := by suffices setToL1 (hT.smul c) = c • setToL1 hT by rw [this, ContinuousLinearMap.smul_apply] refine ContinuousLinearMap.extend_unique (setToL1SCLM α E μ (hT.smul c)) _ _ _ _ ?_ ext1 f suffices c • setToL1 hT f = setToL1SCLM α E μ (hT.smul c) f by rw [← this]; simp [coeToLp] rw [setToL1_eq_setToL1SCLM, setToL1SCLM_smul_left c hT] theorem setToL1_smul_left' (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (c : ℝ) (h_smul : ∀ s, MeasurableSet s → μ s < ∞ → T' s = c • T s) (f : α →₁[μ] E) : setToL1 hT' f = c • setToL1 hT f := by suffices setToL1 hT' = c • setToL1 hT by rw [this, ContinuousLinearMap.smul_apply] refine ContinuousLinearMap.extend_unique (setToL1SCLM α E μ hT') _ _ _ _ ?_ ext1 f suffices c • setToL1 hT f = setToL1SCLM α E μ hT' f by rw [← this]; simp [coeToLp] rw [setToL1_eq_setToL1SCLM, setToL1SCLM_smul_left' c hT hT' h_smul] theorem setToL1_smul (hT : DominatedFinMeasAdditive μ T C) (h_smul : ∀ c : 𝕜, ∀ s x, T s (c • x) = c • T s x) (c : 𝕜) (f : α →₁[μ] E) : setToL1 hT (c • f) = c • setToL1 hT f := by rw [setToL1_eq_setToL1' hT h_smul, setToL1_eq_setToL1' hT h_smul] exact ContinuousLinearMap.map_smul _ _ _ theorem setToL1_simpleFunc_indicatorConst (hT : DominatedFinMeasAdditive μ T C) {s : Set α} (hs : MeasurableSet s) (hμs : μ s < ∞) (x : E) : setToL1 hT (simpleFunc.indicatorConst 1 hs hμs.ne x) = T s x := by rw [setToL1_eq_setToL1SCLM] exact setToL1S_indicatorConst (fun s => hT.eq_zero_of_measure_zero) hT.1 hs hμs x theorem setToL1_indicatorConstLp (hT : DominatedFinMeasAdditive μ T C) {s : Set α} (hs : MeasurableSet s) (hμs : μ s ≠ ∞) (x : E) : setToL1 hT (indicatorConstLp 1 hs hμs x) = T s x := by rw [← Lp.simpleFunc.coe_indicatorConst hs hμs x] exact setToL1_simpleFunc_indicatorConst hT hs hμs.lt_top x theorem setToL1_const [IsFiniteMeasure μ] (hT : DominatedFinMeasAdditive μ T C) (x : E) : setToL1 hT (indicatorConstLp 1 MeasurableSet.univ (measure_ne_top _ _) x) = T univ x := setToL1_indicatorConstLp hT MeasurableSet.univ (measure_ne_top _ _) x section Order variable {G' G'' : Type*} [NormedAddCommGroup G''] [PartialOrder G''] [OrderClosedTopology G''] [IsOrderedAddMonoid G''] [NormedSpace ℝ G''] [CompleteSpace G''] [NormedAddCommGroup G'] [PartialOrder G'] [NormedSpace ℝ G'] theorem setToL1_mono_left' {T T' : Set α → E →L[ℝ] G''} {C C' : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (hTT' : ∀ s, MeasurableSet s → μ s < ∞ → ∀ x, T s x ≤ T' s x) (f : α →₁[μ] E) : setToL1 hT f ≤ setToL1 hT' f := by induction f using Lp.induction (hp_ne_top := one_ne_top) with | @indicatorConst c s hs hμs => rw [setToL1_simpleFunc_indicatorConst hT hs hμs, setToL1_simpleFunc_indicatorConst hT' hs hμs] exact hTT' s hs hμs c | @add f g hf hg _ hf_le hg_le => rw [(setToL1 hT).map_add, (setToL1 hT').map_add] exact add_le_add hf_le hg_le | isClosed => exact isClosed_le (setToL1 hT).continuous (setToL1 hT').continuous theorem setToL1_mono_left {T T' : Set α → E →L[ℝ] G''} {C C' : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (hTT' : ∀ s x, T s x ≤ T' s x) (f : α →₁[μ] E) : setToL1 hT f ≤ setToL1 hT' f := setToL1_mono_left' hT hT' (fun s _ _ x => hTT' s x) f theorem setToL1_nonneg {T : Set α → G' →L[ℝ] G''} {C : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT_nonneg : ∀ s, MeasurableSet s → μ s < ∞ → ∀ x, 0 ≤ x → 0 ≤ T s x) {f : α →₁[μ] G'} (hf : 0 ≤ f) : 0 ≤ setToL1 hT f := by suffices ∀ f : { g : α →₁[μ] G' // 0 ≤ g }, 0 ≤ setToL1 hT f from this (⟨f, hf⟩ : { g : α →₁[μ] G' // 0 ≤ g }) refine fun g => @isClosed_property { g : α →₁ₛ[μ] G' // 0 ≤ g } { g : α →₁[μ] G' // 0 ≤ g } _ _ (fun g => 0 ≤ setToL1 hT g) (denseRange_coeSimpleFuncNonnegToLpNonneg 1 μ G' one_ne_top) ?_ ?_ g · exact isClosed_le continuous_zero ((setToL1 hT).continuous.comp continuous_induced_dom) · intro g have : (coeSimpleFuncNonnegToLpNonneg 1 μ G' g : α →₁[μ] G') = (g : α →₁ₛ[μ] G') := rfl rw [this, setToL1_eq_setToL1SCLM] exact setToL1S_nonneg (fun s => hT.eq_zero_of_measure_zero) hT.1 hT_nonneg g.2 theorem setToL1_mono [IsOrderedAddMonoid G'] {T : Set α → G' →L[ℝ] G''} {C : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT_nonneg : ∀ s, MeasurableSet s → μ s < ∞ → ∀ x, 0 ≤ x → 0 ≤ T s x) {f g : α →₁[μ] G'} (hfg : f ≤ g) : setToL1 hT f ≤ setToL1 hT g := by rw [← sub_nonneg] at hfg ⊢ rw [← (setToL1 hT).map_sub] exact setToL1_nonneg hT hT_nonneg hfg end Order theorem norm_setToL1_le_norm_setToL1SCLM (hT : DominatedFinMeasAdditive μ T C) : ‖setToL1 hT‖ ≤ ‖setToL1SCLM α E μ hT‖ := calc ‖setToL1 hT‖ ≤ (1 : ℝ≥0) * ‖setToL1SCLM α E μ hT‖ := by refine ContinuousLinearMap.opNorm_extend_le (setToL1SCLM α E μ hT) (coeToLp α E ℝ) (simpleFunc.denseRange one_ne_top) fun x => le_of_eq ?_ rw [NNReal.coe_one, one_mul] simp [coeToLp] _ = ‖setToL1SCLM α E μ hT‖ := by rw [NNReal.coe_one, one_mul] theorem norm_setToL1_le_mul_norm (hT : DominatedFinMeasAdditive μ T C) (hC : 0 ≤ C) (f : α →₁[μ] E) : ‖setToL1 hT f‖ ≤ C * ‖f‖ := calc ‖setToL1 hT f‖ ≤ ‖setToL1SCLM α E μ hT‖ * ‖f‖ := ContinuousLinearMap.le_of_opNorm_le _ (norm_setToL1_le_norm_setToL1SCLM hT) _ _ ≤ C * ‖f‖ := mul_le_mul (norm_setToL1SCLM_le hT hC) le_rfl (norm_nonneg _) hC theorem norm_setToL1_le_mul_norm' (hT : DominatedFinMeasAdditive μ T C) (f : α →₁[μ] E) : ‖setToL1 hT f‖ ≤ max C 0 * ‖f‖ := calc ‖setToL1 hT f‖ ≤ ‖setToL1SCLM α E μ hT‖ * ‖f‖ := ContinuousLinearMap.le_of_opNorm_le _ (norm_setToL1_le_norm_setToL1SCLM hT) _ _ ≤ max C 0 * ‖f‖ := mul_le_mul (norm_setToL1SCLM_le' hT) le_rfl (norm_nonneg _) (le_max_right _ _) theorem norm_setToL1_le (hT : DominatedFinMeasAdditive μ T C) (hC : 0 ≤ C) : ‖setToL1 hT‖ ≤ C := ContinuousLinearMap.opNorm_le_bound _ hC (norm_setToL1_le_mul_norm hT hC) theorem norm_setToL1_le' (hT : DominatedFinMeasAdditive μ T C) : ‖setToL1 hT‖ ≤ max C 0 := ContinuousLinearMap.opNorm_le_bound _ (le_max_right _ _) (norm_setToL1_le_mul_norm' hT) theorem setToL1_lipschitz (hT : DominatedFinMeasAdditive μ T C) : LipschitzWith (Real.toNNReal C) (setToL1 hT) := (setToL1 hT).lipschitz.weaken (norm_setToL1_le' hT) /-- If `fs i → f` in `L1`, then `setToL1 hT (fs i) → setToL1 hT f`. -/ theorem tendsto_setToL1 (hT : DominatedFinMeasAdditive μ T C) (f : α →₁[μ] E) {ι} (fs : ι → α →₁[μ] E) {l : Filter ι} (hfs : Tendsto fs l (𝓝 f)) : Tendsto (fun i => setToL1 hT (fs i)) l (𝓝 <| setToL1 hT f) := ((setToL1 hT).continuous.tendsto _).comp hfs end SetToL1 end L1 section Function variable [CompleteSpace F] {T T' T'' : Set α → E →L[ℝ] F} {C C' C'' : ℝ} {f g : α → E} variable (μ T) open Classical in /-- Extend `T : Set α → E →L[ℝ] F` to `(α → E) → F` (for integrable functions `α → E`). We set it to 0 if the function is not integrable. -/ def setToFun (hT : DominatedFinMeasAdditive μ T C) (f : α → E) : F := if hf : Integrable f μ then L1.setToL1 hT (hf.toL1 f) else 0 variable {μ T} theorem setToFun_eq (hT : DominatedFinMeasAdditive μ T C) (hf : Integrable f μ) : setToFun μ T hT f = L1.setToL1 hT (hf.toL1 f) := dif_pos hf theorem L1.setToFun_eq_setToL1 (hT : DominatedFinMeasAdditive μ T C) (f : α →₁[μ] E) : setToFun μ T hT f = L1.setToL1 hT f := by rw [setToFun_eq hT (L1.integrable_coeFn f), Integrable.toL1_coeFn] theorem setToFun_undef (hT : DominatedFinMeasAdditive μ T C) (hf : ¬Integrable f μ) : setToFun μ T hT f = 0 := dif_neg hf theorem setToFun_non_aestronglyMeasurable (hT : DominatedFinMeasAdditive μ T C) (hf : ¬AEStronglyMeasurable f μ) : setToFun μ T hT f = 0 := setToFun_undef hT (not_and_of_not_left _ hf) @[deprecated (since := "2025-04-09")] alias setToFun_non_aEStronglyMeasurable := setToFun_non_aestronglyMeasurable theorem setToFun_congr_left (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (h : T = T') (f : α → E) : setToFun μ T hT f = setToFun μ T' hT' f := by by_cases hf : Integrable f μ · simp_rw [setToFun_eq _ hf, L1.setToL1_congr_left T T' hT hT' h] · simp_rw [setToFun_undef _ hf] theorem setToFun_congr_left' (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (h : ∀ s, MeasurableSet s → μ s < ∞ → T s = T' s) (f : α → E) : setToFun μ T hT f = setToFun μ T' hT' f := by by_cases hf : Integrable f μ · simp_rw [setToFun_eq _ hf, L1.setToL1_congr_left' T T' hT hT' h] · simp_rw [setToFun_undef _ hf] theorem setToFun_add_left (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (f : α → E) : setToFun μ (T + T') (hT.add hT') f = setToFun μ T hT f + setToFun μ T' hT' f := by by_cases hf : Integrable f μ · simp_rw [setToFun_eq _ hf, L1.setToL1_add_left hT hT'] · simp_rw [setToFun_undef _ hf, add_zero] theorem setToFun_add_left' (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (hT'' : DominatedFinMeasAdditive μ T'' C'') (h_add : ∀ s, MeasurableSet s → μ s < ∞ → T'' s = T s + T' s) (f : α → E) : setToFun μ T'' hT'' f = setToFun μ T hT f + setToFun μ T' hT' f := by by_cases hf : Integrable f μ · simp_rw [setToFun_eq _ hf, L1.setToL1_add_left' hT hT' hT'' h_add] · simp_rw [setToFun_undef _ hf, add_zero] theorem setToFun_smul_left (hT : DominatedFinMeasAdditive μ T C) (c : ℝ) (f : α → E) : setToFun μ (fun s => c • T s) (hT.smul c) f = c • setToFun μ T hT f := by by_cases hf : Integrable f μ · simp_rw [setToFun_eq _ hf, L1.setToL1_smul_left hT c] · simp_rw [setToFun_undef _ hf, smul_zero] theorem setToFun_smul_left' (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (c : ℝ) (h_smul : ∀ s, MeasurableSet s → μ s < ∞ → T' s = c • T s) (f : α → E) : setToFun μ T' hT' f = c • setToFun μ T hT f := by by_cases hf : Integrable f μ · simp_rw [setToFun_eq _ hf, L1.setToL1_smul_left' hT hT' c h_smul] · simp_rw [setToFun_undef _ hf, smul_zero] @[simp] theorem setToFun_zero (hT : DominatedFinMeasAdditive μ T C) : setToFun μ T hT (0 : α → E) = 0 := by rw [Pi.zero_def, setToFun_eq hT (integrable_zero _ _ _)] simp only [← Pi.zero_def] rw [Integrable.toL1_zero, ContinuousLinearMap.map_zero] @[simp] theorem setToFun_zero_left {hT : DominatedFinMeasAdditive μ (0 : Set α → E →L[ℝ] F) C} : setToFun μ 0 hT f = 0 := by by_cases hf : Integrable f μ · rw [setToFun_eq hT hf]; exact L1.setToL1_zero_left hT _ · exact setToFun_undef hT hf theorem setToFun_zero_left' (hT : DominatedFinMeasAdditive μ T C) (h_zero : ∀ s, MeasurableSet s → μ s < ∞ → T s = 0) : setToFun μ T hT f = 0 := by by_cases hf : Integrable f μ · rw [setToFun_eq hT hf]; exact L1.setToL1_zero_left' hT h_zero _ · exact setToFun_undef hT hf theorem setToFun_add (hT : DominatedFinMeasAdditive μ T C) (hf : Integrable f μ) (hg : Integrable g μ) : setToFun μ T hT (f + g) = setToFun μ T hT f + setToFun μ T hT g := by rw [setToFun_eq hT (hf.add hg), setToFun_eq hT hf, setToFun_eq hT hg, Integrable.toL1_add, (L1.setToL1 hT).map_add] theorem setToFun_finset_sum' (hT : DominatedFinMeasAdditive μ T C) {ι} (s : Finset ι) {f : ι → α → E} (hf : ∀ i ∈ s, Integrable (f i) μ) : setToFun μ T hT (∑ i ∈ s, f i) = ∑ i ∈ s, setToFun μ T hT (f i) := by classical revert hf refine Finset.induction_on s ?_ ?_ · intro _ simp only [setToFun_zero, Finset.sum_empty] · intro i s his ih hf simp only [his, Finset.sum_insert, not_false_iff] rw [setToFun_add hT (hf i (Finset.mem_insert_self i s)) _] · rw [ih fun i hi => hf i (Finset.mem_insert_of_mem hi)] · convert integrable_finset_sum s fun i hi => hf i (Finset.mem_insert_of_mem hi) with x simp theorem setToFun_finset_sum (hT : DominatedFinMeasAdditive μ T C) {ι} (s : Finset ι) {f : ι → α → E} (hf : ∀ i ∈ s, Integrable (f i) μ) : (setToFun μ T hT fun a => ∑ i ∈ s, f i a) = ∑ i ∈ s, setToFun μ T hT (f i) := by convert setToFun_finset_sum' hT s hf with a; simp theorem setToFun_neg (hT : DominatedFinMeasAdditive μ T C) (f : α → E) : setToFun μ T hT (-f) = -setToFun μ T hT f := by by_cases hf : Integrable f μ · rw [setToFun_eq hT hf, setToFun_eq hT hf.neg, Integrable.toL1_neg, (L1.setToL1 hT).map_neg] · rw [setToFun_undef hT hf, setToFun_undef hT, neg_zero] rwa [← integrable_neg_iff] at hf theorem setToFun_sub (hT : DominatedFinMeasAdditive μ T C) (hf : Integrable f μ) (hg : Integrable g μ) : setToFun μ T hT (f - g) = setToFun μ T hT f - setToFun μ T hT g := by rw [sub_eq_add_neg, sub_eq_add_neg, setToFun_add hT hf hg.neg, setToFun_neg hT g] theorem setToFun_smul [NontriviallyNormedField 𝕜] [NormedSpace 𝕜 E] [NormedSpace 𝕜 F] (hT : DominatedFinMeasAdditive μ T C) (h_smul : ∀ c : 𝕜, ∀ s x, T s (c • x) = c • T s x) (c : 𝕜) (f : α → E) : setToFun μ T hT (c • f) = c • setToFun μ T hT f := by by_cases hf : Integrable f μ · rw [setToFun_eq hT hf, setToFun_eq hT, Integrable.toL1_smul', L1.setToL1_smul hT h_smul c _] · by_cases hr : c = 0 · rw [hr]; simp · have hf' : ¬Integrable (c • f) μ := by rwa [integrable_smul_iff hr f] rw [setToFun_undef hT hf, setToFun_undef hT hf', smul_zero] theorem setToFun_congr_ae (hT : DominatedFinMeasAdditive μ T C) (h : f =ᵐ[μ] g) : setToFun μ T hT f = setToFun μ T hT g := by by_cases hfi : Integrable f μ · have hgi : Integrable g μ := hfi.congr h rw [setToFun_eq hT hfi, setToFun_eq hT hgi, (Integrable.toL1_eq_toL1_iff f g hfi hgi).2 h] · have hgi : ¬Integrable g μ := by rw [integrable_congr h] at hfi; exact hfi rw [setToFun_undef hT hfi, setToFun_undef hT hgi] theorem setToFun_measure_zero (hT : DominatedFinMeasAdditive μ T C) (h : μ = 0) : setToFun μ T hT f = 0 := by have : f =ᵐ[μ] 0 := by simp [h, EventuallyEq] rw [setToFun_congr_ae hT this, setToFun_zero] theorem setToFun_measure_zero' (hT : DominatedFinMeasAdditive μ T C) (h : ∀ s, MeasurableSet s → μ s < ∞ → μ s = 0) : setToFun μ T hT f = 0 := setToFun_zero_left' hT fun s hs hμs => hT.eq_zero_of_measure_zero hs (h s hs hμs) theorem setToFun_toL1 (hT : DominatedFinMeasAdditive μ T C) (hf : Integrable f μ) : setToFun μ T hT (hf.toL1 f) = setToFun μ T hT f := setToFun_congr_ae hT hf.coeFn_toL1 theorem setToFun_indicator_const (hT : DominatedFinMeasAdditive μ T C) {s : Set α} (hs : MeasurableSet s) (hμs : μ s ≠ ∞) (x : E) : setToFun μ T hT (s.indicator fun _ => x) = T s x := by rw [setToFun_congr_ae hT (@indicatorConstLp_coeFn _ _ _ 1 _ _ _ hs hμs x).symm] rw [L1.setToFun_eq_setToL1 hT] exact L1.setToL1_indicatorConstLp hT hs hμs x theorem setToFun_const [IsFiniteMeasure μ] (hT : DominatedFinMeasAdditive μ T C) (x : E) : (setToFun μ T hT fun _ => x) = T univ x := by have : (fun _ : α => x) = Set.indicator univ fun _ => x := (indicator_univ _).symm rw [this] exact setToFun_indicator_const hT MeasurableSet.univ (measure_ne_top _ _) x section Order variable {G' G'' : Type*} [NormedAddCommGroup G''] [PartialOrder G''] [OrderClosedTopology G''] [IsOrderedAddMonoid G''] [NormedSpace ℝ G''] [CompleteSpace G''] [NormedAddCommGroup G'] [PartialOrder G'] [NormedSpace ℝ G'] theorem setToFun_mono_left' {T T' : Set α → E →L[ℝ] G''} {C C' : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (hTT' : ∀ s, MeasurableSet s → μ s < ∞ → ∀ x, T s x ≤ T' s x) (f : α → E) : setToFun μ T hT f ≤ setToFun μ T' hT' f := by by_cases hf : Integrable f μ · simp_rw [setToFun_eq _ hf]; exact L1.setToL1_mono_left' hT hT' hTT' _ · simp_rw [setToFun_undef _ hf, le_rfl] theorem setToFun_mono_left {T T' : Set α → E →L[ℝ] G''} {C C' : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (hTT' : ∀ s x, T s x ≤ T' s x) (f : α →₁[μ] E) : setToFun μ T hT f ≤ setToFun μ T' hT' f := setToFun_mono_left' hT hT' (fun s _ _ x => hTT' s x) f theorem setToFun_nonneg {T : Set α → G' →L[ℝ] G''} {C : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT_nonneg : ∀ s, MeasurableSet s → μ s < ∞ → ∀ x, 0 ≤ x → 0 ≤ T s x) {f : α → G'} (hf : 0 ≤ᵐ[μ] f) : 0 ≤ setToFun μ T hT f := by by_cases hfi : Integrable f μ · simp_rw [setToFun_eq _ hfi] refine L1.setToL1_nonneg hT hT_nonneg ?_ rw [← Lp.coeFn_le] have h0 := Lp.coeFn_zero G' 1 μ have h := Integrable.coeFn_toL1 hfi filter_upwards [h0, h, hf] with _ h0a ha hfa rw [h0a, ha] exact hfa · simp_rw [setToFun_undef _ hfi, le_rfl] theorem setToFun_mono [IsOrderedAddMonoid G'] {T : Set α → G' →L[ℝ] G''} {C : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT_nonneg : ∀ s, MeasurableSet s → μ s < ∞ → ∀ x, 0 ≤ x → 0 ≤ T s x) {f g : α → G'} (hf : Integrable f μ) (hg : Integrable g μ) (hfg : f ≤ᵐ[μ] g) : setToFun μ T hT f ≤ setToFun μ T hT g := by rw [← sub_nonneg, ← setToFun_sub hT hg hf] refine setToFun_nonneg hT hT_nonneg (hfg.mono fun a ha => ?_) rw [Pi.sub_apply, Pi.zero_apply, sub_nonneg] exact ha end Order @[continuity] theorem continuous_setToFun (hT : DominatedFinMeasAdditive μ T C) : Continuous fun f : α →₁[μ] E => setToFun μ T hT f := by simp_rw [L1.setToFun_eq_setToL1 hT]; exact ContinuousLinearMap.continuous _ /-- If `F i → f` in `L1`, then `setToFun μ T hT (F i) → setToFun μ T hT f`. -/ theorem tendsto_setToFun_of_L1 (hT : DominatedFinMeasAdditive μ T C) {ι} (f : α → E) (hfi : Integrable f μ) {fs : ι → α → E} {l : Filter ι} (hfsi : ∀ᶠ i in l, Integrable (fs i) μ) (hfs : Tendsto (fun i => ∫⁻ x, ‖fs i x - f x‖ₑ ∂μ) l (𝓝 0)) : Tendsto (fun i => setToFun μ T hT (fs i)) l (𝓝 <| setToFun μ T hT f) := by classical let f_lp := hfi.toL1 f let F_lp i := if hFi : Integrable (fs i) μ then hFi.toL1 (fs i) else 0 have tendsto_L1 : Tendsto F_lp l (𝓝 f_lp) := by rw [Lp.tendsto_Lp_iff_tendsto_eLpNorm'] simp_rw [eLpNorm_one_eq_lintegral_enorm, Pi.sub_apply] refine (tendsto_congr' ?_).mp hfs filter_upwards [hfsi] with i hi refine lintegral_congr_ae ?_ filter_upwards [hi.coeFn_toL1, hfi.coeFn_toL1] with x hxi hxf simp_rw [F_lp, dif_pos hi, hxi, f_lp, hxf] suffices Tendsto (fun i => setToFun μ T hT (F_lp i)) l (𝓝 (setToFun μ T hT f)) by refine (tendsto_congr' ?_).mp this filter_upwards [hfsi] with i hi suffices h_ae_eq : F_lp i =ᵐ[μ] fs i from setToFun_congr_ae hT h_ae_eq simp_rw [F_lp, dif_pos hi] exact hi.coeFn_toL1 rw [setToFun_congr_ae hT hfi.coeFn_toL1.symm] exact ((continuous_setToFun hT).tendsto f_lp).comp tendsto_L1 theorem tendsto_setToFun_approxOn_of_measurable (hT : DominatedFinMeasAdditive μ T C) [MeasurableSpace E] [BorelSpace E] {f : α → E} {s : Set E} [SeparableSpace s] (hfi : Integrable f μ) (hfm : Measurable f) (hs : ∀ᵐ x ∂μ, f x ∈ closure s) {y₀ : E} (h₀ : y₀ ∈ s) (h₀i : Integrable (fun _ => y₀) μ) : Tendsto (fun n => setToFun μ T hT (SimpleFunc.approxOn f hfm s y₀ h₀ n)) atTop (𝓝 <| setToFun μ T hT f) := tendsto_setToFun_of_L1 hT _ hfi (Eventually.of_forall (SimpleFunc.integrable_approxOn hfm hfi h₀ h₀i)) (SimpleFunc.tendsto_approxOn_L1_enorm hfm _ hs (hfi.sub h₀i).2) theorem tendsto_setToFun_approxOn_of_measurable_of_range_subset (hT : DominatedFinMeasAdditive μ T C) [MeasurableSpace E] [BorelSpace E] {f : α → E} (fmeas : Measurable f) (hf : Integrable f μ) (s : Set E) [SeparableSpace s] (hs : range f ∪ {0} ⊆ s) : Tendsto (fun n => setToFun μ T hT (SimpleFunc.approxOn f fmeas s 0 (hs <| by simp) n)) atTop (𝓝 <| setToFun μ T hT f) := by refine tendsto_setToFun_approxOn_of_measurable hT hf fmeas ?_ _ (integrable_zero _ _ _) exact Eventually.of_forall fun x => subset_closure (hs (Set.mem_union_left _ (mem_range_self _))) /-- Auxiliary lemma for `setToFun_congr_measure`: the function sending `f : α →₁[μ] G` to `f : α →₁[μ'] G` is continuous when `μ' ≤ c' • μ` for `c' ≠ ∞`. -/ theorem continuous_L1_toL1 {μ' : Measure α} (c' : ℝ≥0∞) (hc' : c' ≠ ∞) (hμ'_le : μ' ≤ c' • μ) : Continuous fun f : α →₁[μ] G => (Integrable.of_measure_le_smul hc' hμ'_le (L1.integrable_coeFn f)).toL1 f := by by_cases hc'0 : c' = 0 · have hμ'0 : μ' = 0 := by rw [← Measure.nonpos_iff_eq_zero']; refine hμ'_le.trans ?_; simp [hc'0] have h_im_zero : (fun f : α →₁[μ] G => (Integrable.of_measure_le_smul hc' hμ'_le (L1.integrable_coeFn f)).toL1 f) = 0 := by ext1 f; ext1; simp_rw [hμ'0]; simp only [ae_zero, EventuallyEq, eventually_bot] rw [h_im_zero] exact continuous_zero rw [Metric.continuous_iff] intro f ε hε_pos use ε / 2 / c'.toReal refine ⟨div_pos (half_pos hε_pos) (toReal_pos hc'0 hc'), ?_⟩ intro g hfg rw [Lp.dist_def] at hfg ⊢ let h_int := fun f' : α →₁[μ] G => (L1.integrable_coeFn f').of_measure_le_smul hc' hμ'_le have : eLpNorm (⇑(Integrable.toL1 g (h_int g)) - ⇑(Integrable.toL1 f (h_int f))) 1 μ' = eLpNorm (⇑g - ⇑f) 1 μ' := eLpNorm_congr_ae ((Integrable.coeFn_toL1 _).sub (Integrable.coeFn_toL1 _)) rw [this] have h_eLpNorm_ne_top : eLpNorm (⇑g - ⇑f) 1 μ ≠ ∞ := by rw [← eLpNorm_congr_ae (Lp.coeFn_sub _ _)]; exact Lp.eLpNorm_ne_top _ calc (eLpNorm (⇑g - ⇑f) 1 μ').toReal ≤ (c' * eLpNorm (⇑g - ⇑f) 1 μ).toReal := by refine toReal_mono (ENNReal.mul_ne_top hc' h_eLpNorm_ne_top) ?_ refine (eLpNorm_mono_measure (⇑g - ⇑f) hμ'_le).trans_eq ?_ rw [eLpNorm_smul_measure_of_ne_zero hc'0, smul_eq_mul] simp _ = c'.toReal * (eLpNorm (⇑g - ⇑f) 1 μ).toReal := toReal_mul _ ≤ c'.toReal * (ε / 2 / c'.toReal) := by gcongr _ = ε / 2 := by refine mul_div_cancel₀ (ε / 2) ?_; rw [Ne, toReal_eq_zero_iff]; simp [hc', hc'0] _ < ε := half_lt_self hε_pos theorem setToFun_congr_measure_of_integrable {μ' : Measure α} (c' : ℝ≥0∞) (hc' : c' ≠ ∞) (hμ'_le : μ' ≤ c' • μ) (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ' T C') (f : α → E) (hfμ : Integrable f μ) : setToFun μ T hT f = setToFun μ' T hT' f := by -- integrability for `μ` implies integrability for `μ'`. have h_int : ∀ g : α → E, Integrable g μ → Integrable g μ' := fun g hg => Integrable.of_measure_le_smul hc' hμ'_le hg -- We use `Integrable.induction` apply hfμ.induction (P := fun f => setToFun μ T hT f = setToFun μ' T hT' f) · intro c s hs hμs have hμ's : μ' s ≠ ∞ := by refine ((hμ'_le s).trans_lt ?_).ne rw [Measure.smul_apply, smul_eq_mul] exact ENNReal.mul_lt_top hc'.lt_top hμs rw [setToFun_indicator_const hT hs hμs.ne, setToFun_indicator_const hT' hs hμ's] · intro f₂ g₂ _ hf₂ hg₂ h_eq_f h_eq_g rw [setToFun_add hT hf₂ hg₂, setToFun_add hT' (h_int f₂ hf₂) (h_int g₂ hg₂), h_eq_f, h_eq_g] · refine isClosed_eq (continuous_setToFun hT) ?_ have : (fun f : α →₁[μ] E => setToFun μ' T hT' f) = fun f : α →₁[μ] E => setToFun μ' T hT' ((h_int f (L1.integrable_coeFn f)).toL1 f) := by ext1 f; exact setToFun_congr_ae hT' (Integrable.coeFn_toL1 _).symm rw [this] exact (continuous_setToFun hT').comp (continuous_L1_toL1 c' hc' hμ'_le) · intro f₂ g₂ hfg _ hf_eq have hfg' : f₂ =ᵐ[μ'] g₂ := (Measure.absolutelyContinuous_of_le_smul hμ'_le).ae_eq hfg rw [← setToFun_congr_ae hT hfg, hf_eq, setToFun_congr_ae hT' hfg'] theorem setToFun_congr_measure {μ' : Measure α} (c c' : ℝ≥0∞) (hc : c ≠ ∞) (hc' : c' ≠ ∞) (hμ_le : μ ≤ c • μ') (hμ'_le : μ' ≤ c' • μ) (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ' T C') (f : α → E) : setToFun μ T hT f = setToFun μ' T hT' f := by by_cases hf : Integrable f μ · exact setToFun_congr_measure_of_integrable c' hc' hμ'_le hT hT' f hf · -- if `f` is not integrable, both `setToFun` are 0. have h_int : ∀ g : α → E, ¬Integrable g μ → ¬Integrable g μ' := fun g => mt fun h => h.of_measure_le_smul hc hμ_le simp_rw [setToFun_undef _ hf, setToFun_undef _ (h_int f hf)] theorem setToFun_congr_measure_of_add_right {μ' : Measure α} (hT_add : DominatedFinMeasAdditive (μ + μ') T C') (hT : DominatedFinMeasAdditive μ T C) (f : α → E) (hf : Integrable f (μ + μ')) : setToFun (μ + μ') T hT_add f = setToFun μ T hT f := by refine setToFun_congr_measure_of_integrable 1 one_ne_top ?_ hT_add hT f hf rw [one_smul] nth_rw 1 [← add_zero μ] exact add_le_add le_rfl bot_le theorem setToFun_congr_measure_of_add_left {μ' : Measure α} (hT_add : DominatedFinMeasAdditive (μ + μ') T C') (hT : DominatedFinMeasAdditive μ' T C) (f : α → E) (hf : Integrable f (μ + μ')) : setToFun (μ + μ') T hT_add f = setToFun μ' T hT f := by refine setToFun_congr_measure_of_integrable 1 one_ne_top ?_ hT_add hT f hf rw [one_smul] nth_rw 1 [← zero_add μ'] exact add_le_add_right bot_le μ' theorem setToFun_top_smul_measure (hT : DominatedFinMeasAdditive (∞ • μ) T C) (f : α → E) : setToFun (∞ • μ) T hT f = 0 := by refine setToFun_measure_zero' hT fun s _ hμs => ?_ rw [lt_top_iff_ne_top] at hμs simp only [true_and, Measure.smul_apply, ENNReal.mul_eq_top, eq_self_iff_true, top_ne_zero, Ne, not_false_iff, not_or, Classical.not_not, smul_eq_mul] at hμs simp only [hμs.right, Measure.smul_apply, mul_zero, smul_eq_mul] theorem setToFun_congr_smul_measure (c : ℝ≥0∞) (hc_ne_top : c ≠ ∞) (hT : DominatedFinMeasAdditive μ T C) (hT_smul : DominatedFinMeasAdditive (c • μ) T C') (f : α → E) : setToFun μ T hT f = setToFun (c • μ) T hT_smul f := by by_cases hc0 : c = 0 · simp [hc0] at hT_smul have h : ∀ s, MeasurableSet s → μ s < ∞ → T s = 0 := fun s hs _ => hT_smul.eq_zero hs rw [setToFun_zero_left' _ h, setToFun_measure_zero] simp [hc0] refine setToFun_congr_measure c⁻¹ c ?_ hc_ne_top (le_of_eq ?_) le_rfl hT hT_smul f · simp [hc0] · rw [smul_smul, ENNReal.inv_mul_cancel hc0 hc_ne_top, one_smul] theorem norm_setToFun_le_mul_norm (hT : DominatedFinMeasAdditive μ T C) (f : α →₁[μ] E) (hC : 0 ≤ C) : ‖setToFun μ T hT f‖ ≤ C * ‖f‖ := by rw [L1.setToFun_eq_setToL1]; exact L1.norm_setToL1_le_mul_norm hT hC f theorem norm_setToFun_le_mul_norm' (hT : DominatedFinMeasAdditive μ T C) (f : α →₁[μ] E) : ‖setToFun μ T hT f‖ ≤ max C 0 * ‖f‖ := by rw [L1.setToFun_eq_setToL1]; exact L1.norm_setToL1_le_mul_norm' hT f theorem norm_setToFun_le (hT : DominatedFinMeasAdditive μ T C) (hf : Integrable f μ) (hC : 0 ≤ C) : ‖setToFun μ T hT f‖ ≤ C * ‖hf.toL1 f‖ := by rw [setToFun_eq hT hf]; exact L1.norm_setToL1_le_mul_norm hT hC _ theorem norm_setToFun_le' (hT : DominatedFinMeasAdditive μ T C) (hf : Integrable f μ) : ‖setToFun μ T hT f‖ ≤ max C 0 * ‖hf.toL1 f‖ := by rw [setToFun_eq hT hf]; exact L1.norm_setToL1_le_mul_norm' hT _ /-- Lebesgue dominated convergence theorem provides sufficient conditions under which almost everywhere convergence of a sequence of functions implies the convergence of their image by `setToFun`. We could weaken the condition `bound_integrable` to require `HasFiniteIntegral bound μ` instead (i.e. not requiring that `bound` is measurable), but in all applications proving integrability is easier. -/ theorem tendsto_setToFun_of_dominated_convergence (hT : DominatedFinMeasAdditive μ T C) {fs : ℕ → α → E} {f : α → E} (bound : α → ℝ) (fs_measurable : ∀ n, AEStronglyMeasurable (fs n) μ) (bound_integrable : Integrable bound μ) (h_bound : ∀ n, ∀ᵐ a ∂μ, ‖fs n a‖ ≤ bound a) (h_lim : ∀ᵐ a ∂μ, Tendsto (fun n => fs n a) atTop (𝓝 (f a))) : Tendsto (fun n => setToFun μ T hT (fs n)) atTop (𝓝 <| setToFun μ T hT f) := by -- `f` is a.e.-measurable, since it is the a.e.-pointwise limit of a.e.-measurable functions. have f_measurable : AEStronglyMeasurable f μ := aestronglyMeasurable_of_tendsto_ae _ fs_measurable h_lim -- all functions we consider are integrable have fs_int : ∀ n, Integrable (fs n) μ := fun n => bound_integrable.mono' (fs_measurable n) (h_bound _) have f_int : Integrable f μ := ⟨f_measurable, hasFiniteIntegral_of_dominated_convergence bound_integrable.hasFiniteIntegral h_bound h_lim⟩ -- it suffices to prove the result for the corresponding L1 functions suffices Tendsto (fun n => L1.setToL1 hT ((fs_int n).toL1 (fs n))) atTop (𝓝 (L1.setToL1 hT (f_int.toL1 f))) by convert this with n · exact setToFun_eq hT (fs_int n) · exact setToFun_eq hT f_int -- the convergence of setToL1 follows from the convergence of the L1 functions refine L1.tendsto_setToL1 hT _ _ ?_ -- up to some rewriting, what we need to prove is `h_lim` rw [tendsto_iff_norm_sub_tendsto_zero] have lintegral_norm_tendsto_zero : Tendsto (fun n => ENNReal.toReal <| ∫⁻ a, ENNReal.ofReal ‖fs n a - f a‖ ∂μ) atTop (𝓝 0) := (tendsto_toReal zero_ne_top).comp (tendsto_lintegral_norm_of_dominated_convergence fs_measurable bound_integrable.hasFiniteIntegral h_bound h_lim) convert lintegral_norm_tendsto_zero with n rw [L1.norm_def] congr 1 refine lintegral_congr_ae ?_ rw [← Integrable.toL1_sub] refine ((fs_int n).sub f_int).coeFn_toL1.mono fun x hx => ?_ dsimp only rw [hx, ofReal_norm_eq_enorm, Pi.sub_apply] /-- Lebesgue dominated convergence theorem for filters with a countable basis -/ theorem tendsto_setToFun_filter_of_dominated_convergence (hT : DominatedFinMeasAdditive μ T C) {ι} {l : Filter ι} [l.IsCountablyGenerated] {fs : ι → α → E} {f : α → E} (bound : α → ℝ) (hfs_meas : ∀ᶠ n in l, AEStronglyMeasurable (fs n) μ) (h_bound : ∀ᶠ n in l, ∀ᵐ a ∂μ, ‖fs n a‖ ≤ bound a) (bound_integrable : Integrable bound μ) (h_lim : ∀ᵐ a ∂μ, Tendsto (fun n => fs n a) l (𝓝 (f a))) : Tendsto (fun n => setToFun μ T hT (fs n)) l (𝓝 <| setToFun μ T hT f) := by rw [tendsto_iff_seq_tendsto] intro x xl have hxl : ∀ s ∈ l, ∃ a, ∀ b ≥ a, x b ∈ s := by rwa [tendsto_atTop'] at xl have h : { x : ι | (fun n => AEStronglyMeasurable (fs n) μ) x } ∩ { x : ι | (fun n => ∀ᵐ a ∂μ, ‖fs n a‖ ≤ bound a) x } ∈ l := inter_mem hfs_meas h_bound obtain ⟨k, h⟩ := hxl _ h rw [← tendsto_add_atTop_iff_nat k] refine tendsto_setToFun_of_dominated_convergence hT bound ?_ bound_integrable ?_ ?_ · exact fun n => (h _ (self_le_add_left _ _)).1 · exact fun n => (h _ (self_le_add_left _ _)).2 · filter_upwards [h_lim] refine fun a h_lin => @Tendsto.comp _ _ _ (fun n => x (n + k)) (fun n => fs n a) _ _ _ h_lin ?_ rwa [tendsto_add_atTop_iff_nat] variable {X : Type*} [TopologicalSpace X] [FirstCountableTopology X] theorem continuousWithinAt_setToFun_of_dominated (hT : DominatedFinMeasAdditive μ T C) {fs : X → α → E} {x₀ : X} {bound : α → ℝ} {s : Set X} (hfs_meas : ∀ᶠ x in 𝓝[s] x₀, AEStronglyMeasurable (fs x) μ) (h_bound : ∀ᶠ x in 𝓝[s] x₀, ∀ᵐ a ∂μ, ‖fs x a‖ ≤ bound a) (bound_integrable : Integrable bound μ) (h_cont : ∀ᵐ a ∂μ, ContinuousWithinAt (fun x => fs x a) s x₀) : ContinuousWithinAt (fun x => setToFun μ T hT (fs x)) s x₀ := tendsto_setToFun_filter_of_dominated_convergence hT bound ‹_› ‹_› ‹_› ‹_› theorem continuousAt_setToFun_of_dominated (hT : DominatedFinMeasAdditive μ T C) {fs : X → α → E} {x₀ : X} {bound : α → ℝ} (hfs_meas : ∀ᶠ x in 𝓝 x₀, AEStronglyMeasurable (fs x) μ) (h_bound : ∀ᶠ x in 𝓝 x₀, ∀ᵐ a ∂μ, ‖fs x a‖ ≤ bound a) (bound_integrable : Integrable bound μ) (h_cont : ∀ᵐ a ∂μ, ContinuousAt (fun x => fs x a) x₀) : ContinuousAt (fun x => setToFun μ T hT (fs x)) x₀ := tendsto_setToFun_filter_of_dominated_convergence hT bound ‹_› ‹_› ‹_› ‹_› theorem continuousOn_setToFun_of_dominated (hT : DominatedFinMeasAdditive μ T C) {fs : X → α → E} {bound : α → ℝ} {s : Set X} (hfs_meas : ∀ x ∈ s, AEStronglyMeasurable (fs x) μ) (h_bound : ∀ x ∈ s, ∀ᵐ a ∂μ, ‖fs x a‖ ≤ bound a) (bound_integrable : Integrable bound μ) (h_cont : ∀ᵐ a ∂μ, ContinuousOn (fun x => fs x a) s) : ContinuousOn (fun x => setToFun μ T hT (fs x)) s := by intro x hx refine continuousWithinAt_setToFun_of_dominated hT ?_ ?_ bound_integrable ?_ · filter_upwards [self_mem_nhdsWithin] with x hx using hfs_meas x hx · filter_upwards [self_mem_nhdsWithin] with x hx using h_bound x hx · filter_upwards [h_cont] with a ha using ha x hx theorem continuous_setToFun_of_dominated (hT : DominatedFinMeasAdditive μ T C) {fs : X → α → E} {bound : α → ℝ} (hfs_meas : ∀ x, AEStronglyMeasurable (fs x) μ) (h_bound : ∀ x, ∀ᵐ a ∂μ, ‖fs x a‖ ≤ bound a) (bound_integrable : Integrable bound μ) (h_cont : ∀ᵐ a ∂μ, Continuous fun x => fs x a) : Continuous fun x => setToFun μ T hT (fs x) := continuous_iff_continuousAt.mpr fun _ => continuousAt_setToFun_of_dominated hT (Eventually.of_forall hfs_meas) (Eventually.of_forall h_bound) ‹_› <| h_cont.mono fun _ => Continuous.continuousAt end Function end MeasureTheory
Mathlib/MeasureTheory/Integral/SetToL1.lean
1,302
1,307
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel, Yury Kudryashov -/ import Mathlib.Analysis.Calculus.Deriv.AffineMap import Mathlib.Analysis.Calculus.Deriv.Comp import Mathlib.Analysis.Calculus.Deriv.Mul import Mathlib.Analysis.Calculus.Deriv.Slope import Mathlib.Analysis.Normed.Group.AddTorsor import Mathlib.Analysis.Normed.Module.Convex import Mathlib.Analysis.RCLike.Basic import Mathlib.Topology.Instances.RealVectorSpace import Mathlib.Topology.LocallyConstant.Basic /-! # The mean value inequality and equalities In this file we prove the following facts: * `Convex.norm_image_sub_le_of_norm_deriv_le` : if `f` is differentiable on a convex set `s` and the norm of its derivative is bounded by `C`, then `f` is Lipschitz continuous on `s` with constant `C`; also a variant in which what is bounded by `C` is the norm of the difference of the derivative from a fixed linear map. This lemma and its versions are formulated using `RCLike`, so they work both for real and complex derivatives. * `image_le_of*`, `image_norm_le_of_*` : several similar lemmas deducing `f x ≤ B x` or `‖f x‖ ≤ B x` from upper estimates on `f'` or `‖f'‖`, respectively. These lemmas differ by their assumptions: * `of_liminf_*` lemmas assume that limit inferior of some ratio is less than `B' x`; * `of_deriv_right_*`, `of_norm_deriv_right_*` lemmas assume that the right derivative or its norm is less than `B' x`; * `of_*_lt_*` lemmas assume a strict inequality whenever `f x = B x` or `‖f x‖ = B x`; * `of_*_le_*` lemmas assume a non-strict inequality everywhere on `[a, b)`; * name of a lemma ends with `'` if (1) it assumes that `B` is continuous on `[a, b]` and has a right derivative at every point of `[a, b)`, and (2) the lemma has a counterpart assuming that `B` is differentiable everywhere on `ℝ` * `norm_image_sub_le_*_segment` : if derivative of `f` on `[a, b]` is bounded above by a constant `C`, then `‖f x - f a‖ ≤ C * ‖x - a‖`; several versions deal with right derivative and derivative within `[a, b]` (`HasDerivWithinAt` or `derivWithin`). * `Convex.is_const_of_fderivWithin_eq_zero` : if a function has derivative `0` on a convex set `s`, then it is a constant on `s`. * `hasStrictFDerivAt_of_hasFDerivAt_of_continuousAt` : a C^1 function over the reals is strictly differentiable. (This is a corollary of the mean value inequality.) -/ variable {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] {F : Type*} [NormedAddCommGroup F] [NormedSpace ℝ F] open Metric Set Asymptotics ContinuousLinearMap Filter open scoped Topology NNReal /-! ### One-dimensional fencing inequalities -/ /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has right derivative `B'` at every point of `[a, b)`; * for each `x ∈ [a, b)` the right-side limit inferior of `(f z - f x) / (z - x)` is bounded above by a function `f'`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ theorem image_le_of_liminf_slope_right_lt_deriv_boundary' {f f' : ℝ → ℝ} {a b : ℝ} (hf : ContinuousOn f (Icc a b)) -- `hf'` actually says `liminf (f z - f x) / (z - x) ≤ f' x` (hf' : ∀ x ∈ Ico a b, ∀ r, f' x < r → ∃ᶠ z in 𝓝[>] x, slope f x z < r) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : ContinuousOn B (Icc a b)) (hB' : ∀ x ∈ Ico a b, HasDerivWithinAt B (B' x) (Ici x) x) (bound : ∀ x ∈ Ico a b, f x = B x → f' x < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := by change Icc a b ⊆ { x | f x ≤ B x } set s := { x | f x ≤ B x } ∩ Icc a b have A : ContinuousOn (fun x => (f x, B x)) (Icc a b) := hf.prodMk hB have : IsClosed s := by simp only [s, inter_comm] exact A.preimage_isClosed_of_isClosed isClosed_Icc OrderClosedTopology.isClosed_le' apply this.Icc_subset_of_forall_exists_gt ha rintro x ⟨hxB : f x ≤ B x, xab⟩ y hy rcases hxB.lt_or_eq with hxB | hxB · -- If `f x < B x`, then all we need is continuity of both sides refine nonempty_of_mem (inter_mem ?_ (Ioc_mem_nhdsGT hy)) have : ∀ᶠ x in 𝓝[Icc a b] x, f x < B x := A x (Ico_subset_Icc_self xab) (IsOpen.mem_nhds (isOpen_lt continuous_fst continuous_snd) hxB) have : ∀ᶠ x in 𝓝[>] x, f x < B x := nhdsWithin_le_of_mem (Icc_mem_nhdsGT_of_mem xab) this exact this.mono fun y => le_of_lt · rcases exists_between (bound x xab hxB) with ⟨r, hfr, hrB⟩ specialize hf' x xab r hfr have HB : ∀ᶠ z in 𝓝[>] x, r < slope B x z := (hasDerivWithinAt_iff_tendsto_slope' <| lt_irrefl x).1 (hB' x xab).Ioi_of_Ici (Ioi_mem_nhds hrB) obtain ⟨z, hfz, hzB, hz⟩ : ∃ z, slope f x z < r ∧ r < slope B x z ∧ z ∈ Ioc x y := hf'.and_eventually (HB.and (Ioc_mem_nhdsGT hy)) |>.exists refine ⟨z, ?_, hz⟩ have := (hfz.trans hzB).le rwa [slope_def_field, slope_def_field, div_le_div_iff_of_pos_right (sub_pos.2 hz.1), hxB, sub_le_sub_iff_right] at this /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has derivative `B'` everywhere on `ℝ`; * for each `x ∈ [a, b)` the right-side limit inferior of `(f z - f x) / (z - x)` is bounded above by a function `f'`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ theorem image_le_of_liminf_slope_right_lt_deriv_boundary {f f' : ℝ → ℝ} {a b : ℝ} (hf : ContinuousOn f (Icc a b)) -- `hf'` actually says `liminf (f z - f x) / (z - x) ≤ f' x` (hf' : ∀ x ∈ Ico a b, ∀ r, f' x < r → ∃ᶠ z in 𝓝[>] x, slope f x z < r) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : ∀ x, HasDerivAt B (B' x) x) (bound : ∀ x ∈ Ico a b, f x = B x → f' x < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := image_le_of_liminf_slope_right_lt_deriv_boundary' hf hf' ha (fun x _ => (hB x).continuousAt.continuousWithinAt) (fun x _ => (hB x).hasDerivWithinAt) bound /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has right derivative `B'` at every point of `[a, b)`; * for each `x ∈ [a, b)` the right-side limit inferior of `(f z - f x) / (z - x)` is bounded above by `B'`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ theorem image_le_of_liminf_slope_right_le_deriv_boundary {f : ℝ → ℝ} {a b : ℝ} (hf : ContinuousOn f (Icc a b)) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : ContinuousOn B (Icc a b)) (hB' : ∀ x ∈ Ico a b, HasDerivWithinAt B (B' x) (Ici x) x) -- `bound` actually says `liminf (f z - f x) / (z - x) ≤ B' x` (bound : ∀ x ∈ Ico a b, ∀ r, B' x < r → ∃ᶠ z in 𝓝[>] x, slope f x z < r) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := by have Hr : ∀ x ∈ Icc a b, ∀ r > 0, f x ≤ B x + r * (x - a) := fun x hx r hr => by apply image_le_of_liminf_slope_right_lt_deriv_boundary' hf bound · rwa [sub_self, mul_zero, add_zero] · exact hB.add (continuousOn_const.mul (continuousOn_id.sub continuousOn_const)) · intro x hx exact (hB' x hx).add (((hasDerivWithinAt_id x (Ici x)).sub_const a).const_mul r) · intro x _ _ rw [mul_one] exact (lt_add_iff_pos_right _).2 hr exact hx intro x hx have : ContinuousWithinAt (fun r => B x + r * (x - a)) (Ioi 0) 0 := continuousWithinAt_const.add (continuousWithinAt_id.mul continuousWithinAt_const) convert continuousWithinAt_const.closure_le _ this (Hr x hx) using 1 <;> simp /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has right derivative `B'` at every point of `[a, b)`; * `f` has right derivative `f'` at every point of `[a, b)`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ theorem image_le_of_deriv_right_lt_deriv_boundary' {f f' : ℝ → ℝ} {a b : ℝ} (hf : ContinuousOn f (Icc a b)) (hf' : ∀ x ∈ Ico a b, HasDerivWithinAt f (f' x) (Ici x) x) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : ContinuousOn B (Icc a b)) (hB' : ∀ x ∈ Ico a b, HasDerivWithinAt B (B' x) (Ici x) x) (bound : ∀ x ∈ Ico a b, f x = B x → f' x < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := image_le_of_liminf_slope_right_lt_deriv_boundary' hf (fun x hx _ hr => (hf' x hx).liminf_right_slope_le hr) ha hB hB' bound /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has derivative `B'` everywhere on `ℝ`; * `f` has right derivative `f'` at every point of `[a, b)`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ theorem image_le_of_deriv_right_lt_deriv_boundary {f f' : ℝ → ℝ} {a b : ℝ} (hf : ContinuousOn f (Icc a b)) (hf' : ∀ x ∈ Ico a b, HasDerivWithinAt f (f' x) (Ici x) x) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : ∀ x, HasDerivAt B (B' x) x) (bound : ∀ x ∈ Ico a b, f x = B x → f' x < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := image_le_of_deriv_right_lt_deriv_boundary' hf hf' ha (fun x _ => (hB x).continuousAt.continuousWithinAt) (fun x _ => (hB x).hasDerivWithinAt) bound /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has derivative `B'` everywhere on `ℝ`; * `f` has right derivative `f'` at every point of `[a, b)`; * we have `f' x ≤ B' x` on `[a, b)`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ theorem image_le_of_deriv_right_le_deriv_boundary {f f' : ℝ → ℝ} {a b : ℝ} (hf : ContinuousOn f (Icc a b)) (hf' : ∀ x ∈ Ico a b, HasDerivWithinAt f (f' x) (Ici x) x) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : ContinuousOn B (Icc a b)) (hB' : ∀ x ∈ Ico a b, HasDerivWithinAt B (B' x) (Ici x) x) (bound : ∀ x ∈ Ico a b, f' x ≤ B' x) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := image_le_of_liminf_slope_right_le_deriv_boundary hf ha hB hB' fun x hx _ hr => (hf' x hx).liminf_right_slope_le (lt_of_le_of_lt (bound x hx) hr) /-! ### Vector-valued functions `f : ℝ → E` -/ section variable {f : ℝ → E} {a b : ℝ} /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `‖f a‖ ≤ B a`; * `B` has right derivative at every point of `[a, b)`; * for each `x ∈ [a, b)` the right-side limit inferior of `(‖f z‖ - ‖f x‖) / (z - x)` is bounded above by a function `f'`; * we have `f' x < B' x` whenever `‖f x‖ = B x`. Then `‖f x‖ ≤ B x` everywhere on `[a, b]`. -/ theorem image_norm_le_of_liminf_right_slope_norm_lt_deriv_boundary {E : Type*} [NormedAddCommGroup E] {f : ℝ → E} {f' : ℝ → ℝ} (hf : ContinuousOn f (Icc a b)) -- `hf'` actually says `liminf (‖f z‖ - ‖f x‖) / (z - x) ≤ f' x` (hf' : ∀ x ∈ Ico a b, ∀ r, f' x < r → ∃ᶠ z in 𝓝[>] x, slope (norm ∘ f) x z < r) {B B' : ℝ → ℝ} (ha : ‖f a‖ ≤ B a) (hB : ContinuousOn B (Icc a b)) (hB' : ∀ x ∈ Ico a b, HasDerivWithinAt B (B' x) (Ici x) x) (bound : ∀ x ∈ Ico a b, ‖f x‖ = B x → f' x < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → ‖f x‖ ≤ B x := image_le_of_liminf_slope_right_lt_deriv_boundary' (continuous_norm.comp_continuousOn hf) hf' ha hB hB' bound /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `‖f a‖ ≤ B a`; * `f` and `B` have right derivatives `f'` and `B'` respectively at every point of `[a, b)`; * the norm of `f'` is strictly less than `B'` whenever `‖f x‖ = B x`. Then `‖f x‖ ≤ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ theorem image_norm_le_of_norm_deriv_right_lt_deriv_boundary' {f' : ℝ → E} (hf : ContinuousOn f (Icc a b)) (hf' : ∀ x ∈ Ico a b, HasDerivWithinAt f (f' x) (Ici x) x) {B B' : ℝ → ℝ} (ha : ‖f a‖ ≤ B a) (hB : ContinuousOn B (Icc a b)) (hB' : ∀ x ∈ Ico a b, HasDerivWithinAt B (B' x) (Ici x) x) (bound : ∀ x ∈ Ico a b, ‖f x‖ = B x → ‖f' x‖ < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → ‖f x‖ ≤ B x := image_norm_le_of_liminf_right_slope_norm_lt_deriv_boundary hf (fun x hx _ hr => (hf' x hx).liminf_right_slope_norm_le hr) ha hB hB' bound /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `‖f a‖ ≤ B a`; * `f` has right derivative `f'` at every point of `[a, b)`; * `B` has derivative `B'` everywhere on `ℝ`; * the norm of `f'` is strictly less than `B'` whenever `‖f x‖ = B x`. Then `‖f x‖ ≤ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ theorem image_norm_le_of_norm_deriv_right_lt_deriv_boundary {f' : ℝ → E} (hf : ContinuousOn f (Icc a b)) (hf' : ∀ x ∈ Ico a b, HasDerivWithinAt f (f' x) (Ici x) x) {B B' : ℝ → ℝ} (ha : ‖f a‖ ≤ B a) (hB : ∀ x, HasDerivAt B (B' x) x) (bound : ∀ x ∈ Ico a b, ‖f x‖ = B x → ‖f' x‖ < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → ‖f x‖ ≤ B x := image_norm_le_of_norm_deriv_right_lt_deriv_boundary' hf hf' ha (fun x _ => (hB x).continuousAt.continuousWithinAt) (fun x _ => (hB x).hasDerivWithinAt) bound /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `‖f a‖ ≤ B a`; * `f` and `B` have right derivatives `f'` and `B'` respectively at every point of `[a, b)`; * we have `‖f' x‖ ≤ B x` everywhere on `[a, b)`. Then `‖f x‖ ≤ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ theorem image_norm_le_of_norm_deriv_right_le_deriv_boundary' {f' : ℝ → E} (hf : ContinuousOn f (Icc a b)) (hf' : ∀ x ∈ Ico a b, HasDerivWithinAt f (f' x) (Ici x) x) {B B' : ℝ → ℝ} (ha : ‖f a‖ ≤ B a) (hB : ContinuousOn B (Icc a b)) (hB' : ∀ x ∈ Ico a b, HasDerivWithinAt B (B' x) (Ici x) x) (bound : ∀ x ∈ Ico a b, ‖f' x‖ ≤ B' x) : ∀ ⦃x⦄, x ∈ Icc a b → ‖f x‖ ≤ B x := image_le_of_liminf_slope_right_le_deriv_boundary (continuous_norm.comp_continuousOn hf) ha hB hB' fun x hx _ hr => (hf' x hx).liminf_right_slope_norm_le ((bound x hx).trans_lt hr) /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `‖f a‖ ≤ B a`; * `f` has right derivative `f'` at every point of `[a, b)`; * `B` has derivative `B'` everywhere on `ℝ`; * we have `‖f' x‖ ≤ B x` everywhere on `[a, b)`. Then `‖f x‖ ≤ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ theorem image_norm_le_of_norm_deriv_right_le_deriv_boundary {f' : ℝ → E} (hf : ContinuousOn f (Icc a b)) (hf' : ∀ x ∈ Ico a b, HasDerivWithinAt f (f' x) (Ici x) x) {B B' : ℝ → ℝ} (ha : ‖f a‖ ≤ B a) (hB : ∀ x, HasDerivAt B (B' x) x) (bound : ∀ x ∈ Ico a b, ‖f' x‖ ≤ B' x) : ∀ ⦃x⦄, x ∈ Icc a b → ‖f x‖ ≤ B x := image_norm_le_of_norm_deriv_right_le_deriv_boundary' hf hf' ha (fun x _ => (hB x).continuousAt.continuousWithinAt) (fun x _ => (hB x).hasDerivWithinAt) bound /-- A function on `[a, b]` with the norm of the right derivative bounded by `C` satisfies `‖f x - f a‖ ≤ C * (x - a)`. -/ theorem norm_image_sub_le_of_norm_deriv_right_le_segment {f' : ℝ → E} {C : ℝ} (hf : ContinuousOn f (Icc a b)) (hf' : ∀ x ∈ Ico a b, HasDerivWithinAt f (f' x) (Ici x) x) (bound : ∀ x ∈ Ico a b, ‖f' x‖ ≤ C) : ∀ x ∈ Icc a b, ‖f x - f a‖ ≤ C * (x - a) := by let g x := f x - f a have hg : ContinuousOn g (Icc a b) := hf.sub continuousOn_const have hg' : ∀ x ∈ Ico a b, HasDerivWithinAt g (f' x) (Ici x) x := by intro x hx simp [g, hf' x hx] let B x := C * (x - a) have hB : ∀ x, HasDerivAt B C x := by intro x simpa using (hasDerivAt_const x C).mul ((hasDerivAt_id x).sub (hasDerivAt_const x a)) convert image_norm_le_of_norm_deriv_right_le_deriv_boundary hg hg' _ hB bound simp only [g, B]; rw [sub_self, norm_zero, sub_self, mul_zero] /-- A function on `[a, b]` with the norm of the derivative within `[a, b]` bounded by `C` satisfies `‖f x - f a‖ ≤ C * (x - a)`, `HasDerivWithinAt` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment' {f' : ℝ → E} {C : ℝ} (hf : ∀ x ∈ Icc a b, HasDerivWithinAt f (f' x) (Icc a b) x) (bound : ∀ x ∈ Ico a b, ‖f' x‖ ≤ C) : ∀ x ∈ Icc a b, ‖f x - f a‖ ≤ C * (x - a) := by refine norm_image_sub_le_of_norm_deriv_right_le_segment (fun x hx => (hf x hx).continuousWithinAt) (fun x hx => ?_) bound exact (hf x <| Ico_subset_Icc_self hx).mono_of_mem_nhdsWithin (Icc_mem_nhdsGE_of_mem hx) /-- A function on `[a, b]` with the norm of the derivative within `[a, b]` bounded by `C` satisfies `‖f x - f a‖ ≤ C * (x - a)`, `derivWithin` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment {C : ℝ} (hf : DifferentiableOn ℝ f (Icc a b)) (bound : ∀ x ∈ Ico a b, ‖derivWithin f (Icc a b) x‖ ≤ C) : ∀ x ∈ Icc a b, ‖f x - f a‖ ≤ C * (x - a) := by refine norm_image_sub_le_of_norm_deriv_le_segment' ?_ bound exact fun x hx => (hf x hx).hasDerivWithinAt /-- A function on `[0, 1]` with the norm of the derivative within `[0, 1]` bounded by `C` satisfies `‖f 1 - f 0‖ ≤ C`, `HasDerivWithinAt` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment_01' {f' : ℝ → E} {C : ℝ} (hf : ∀ x ∈ Icc (0 : ℝ) 1, HasDerivWithinAt f (f' x) (Icc (0 : ℝ) 1) x) (bound : ∀ x ∈ Ico (0 : ℝ) 1, ‖f' x‖ ≤ C) : ‖f 1 - f 0‖ ≤ C := by simpa only [sub_zero, mul_one] using norm_image_sub_le_of_norm_deriv_le_segment' hf bound 1 (right_mem_Icc.2 zero_le_one) /-- A function on `[0, 1]` with the norm of the derivative within `[0, 1]` bounded by `C` satisfies `‖f 1 - f 0‖ ≤ C`, `derivWithin` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment_01 {C : ℝ} (hf : DifferentiableOn ℝ f (Icc (0 : ℝ) 1)) (bound : ∀ x ∈ Ico (0 : ℝ) 1, ‖derivWithin f (Icc (0 : ℝ) 1) x‖ ≤ C) : ‖f 1 - f 0‖ ≤ C := by simpa only [sub_zero, mul_one] using norm_image_sub_le_of_norm_deriv_le_segment hf bound 1 (right_mem_Icc.2 zero_le_one) theorem constant_of_has_deriv_right_zero (hcont : ContinuousOn f (Icc a b)) (hderiv : ∀ x ∈ Ico a b, HasDerivWithinAt f 0 (Ici x) x) : ∀ x ∈ Icc a b, f x = f a := by have : ∀ x ∈ Icc a b, ‖f x - f a‖ ≤ 0 * (x - a) := fun x hx => norm_image_sub_le_of_norm_deriv_right_le_segment hcont hderiv (fun _ _ => norm_zero.le) x hx simpa only [zero_mul, norm_le_zero_iff, sub_eq_zero] using this theorem constant_of_derivWithin_zero (hdiff : DifferentiableOn ℝ f (Icc a b)) (hderiv : ∀ x ∈ Ico a b, derivWithin f (Icc a b) x = 0) : ∀ x ∈ Icc a b, f x = f a := by have H : ∀ x ∈ Ico a b, ‖derivWithin f (Icc a b) x‖ ≤ 0 := by simpa only [norm_le_zero_iff] using fun x hx => hderiv x hx simpa only [zero_mul, norm_le_zero_iff, sub_eq_zero] using fun x hx => norm_image_sub_le_of_norm_deriv_le_segment hdiff H x hx variable {f' g : ℝ → E} /-- If two continuous functions on `[a, b]` have the same right derivative and are equal at `a`, then they are equal everywhere on `[a, b]`. -/ theorem eq_of_has_deriv_right_eq (derivf : ∀ x ∈ Ico a b, HasDerivWithinAt f (f' x) (Ici x) x) (derivg : ∀ x ∈ Ico a b, HasDerivWithinAt g (f' x) (Ici x) x) (fcont : ContinuousOn f (Icc a b)) (gcont : ContinuousOn g (Icc a b)) (hi : f a = g a) : ∀ y ∈ Icc a b, f y = g y := by simp only [← @sub_eq_zero _ _ (f _)] at hi ⊢ exact hi ▸ constant_of_has_deriv_right_zero (fcont.sub gcont) fun y hy => by simpa only [sub_self] using (derivf y hy).sub (derivg y hy) /-- If two differentiable functions on `[a, b]` have the same derivative within `[a, b]` everywhere on `[a, b)` and are equal at `a`, then they are equal everywhere on `[a, b]`. -/ theorem eq_of_derivWithin_eq (fdiff : DifferentiableOn ℝ f (Icc a b)) (gdiff : DifferentiableOn ℝ g (Icc a b)) (hderiv : EqOn (derivWithin f (Icc a b)) (derivWithin g (Icc a b)) (Ico a b)) (hi : f a = g a) : ∀ y ∈ Icc a b, f y = g y := by have A : ∀ y ∈ Ico a b, HasDerivWithinAt f (derivWithin f (Icc a b) y) (Ici y) y := fun y hy => (fdiff y (mem_Icc_of_Ico hy)).hasDerivWithinAt.mono_of_mem_nhdsWithin (Icc_mem_nhdsGE_of_mem hy) have B : ∀ y ∈ Ico a b, HasDerivWithinAt g (derivWithin g (Icc a b) y) (Ici y) y := fun y hy => (gdiff y (mem_Icc_of_Ico hy)).hasDerivWithinAt.mono_of_mem_nhdsWithin (Icc_mem_nhdsGE_of_mem hy) exact eq_of_has_deriv_right_eq A (fun y hy => (hderiv hy).symm ▸ B y hy) fdiff.continuousOn gdiff.continuousOn hi end /-! ### Vector-valued functions `f : E → G` Theorems in this section work both for real and complex differentiable functions. We use assumptions `[NontriviallyNormedField 𝕜] [IsRCLikeNormedField 𝕜] [NormedSpace 𝕜 E] [NormedSpace 𝕜 G]` to achieve this result. For the domain `E` we also assume `[NormedSpace ℝ E]` to have a notion of a `Convex` set. -/ section namespace Convex variable {𝕜 G : Type*} [NontriviallyNormedField 𝕜] [IsRCLikeNormedField 𝕜] [NormedSpace 𝕜 E] [NormedAddCommGroup G] [NormedSpace 𝕜 G] {f g : E → G} {C : ℝ} {s : Set E} {x y : E} {f' g' : E → E →L[𝕜] G} {φ : E →L[𝕜] G} instance (priority := 100) : PathConnectedSpace 𝕜 := by letI : RCLike 𝕜 := IsRCLikeNormedField.rclike 𝕜 infer_instance /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `HasFDerivWithinAt`. -/ theorem norm_image_sub_le_of_norm_hasFDerivWithin_le (hf : ∀ x ∈ s, HasFDerivWithinAt f (f' x) s x) (bound : ∀ x ∈ s, ‖f' x‖ ≤ C) (hs : Convex ℝ s) (xs : x ∈ s) (ys : y ∈ s) : ‖f y - f x‖ ≤ C * ‖y - x‖ := by letI : RCLike 𝕜 := IsRCLikeNormedField.rclike 𝕜 letI : NormedSpace ℝ G := RestrictScalars.normedSpace ℝ 𝕜 G /- By composition with `AffineMap.lineMap x y`, we reduce to a statement for functions defined on `[0,1]`, for which it is proved in `norm_image_sub_le_of_norm_deriv_le_segment`. We just have to check the differentiability of the composition and bounds on its derivative, which is straightforward but tedious for lack of automation. -/ set g := (AffineMap.lineMap x y : ℝ → E) have segm : MapsTo g (Icc 0 1 : Set ℝ) s := hs.mapsTo_lineMap xs ys have hD : ∀ t ∈ Icc (0 : ℝ) 1, HasDerivWithinAt (f ∘ g) (f' (g t) (y - x)) (Icc 0 1) t := fun t ht => by simpa using ((hf (g t) (segm ht)).restrictScalars ℝ).comp_hasDerivWithinAt _ AffineMap.hasDerivWithinAt_lineMap segm have bound : ∀ t ∈ Ico (0 : ℝ) 1, ‖f' (g t) (y - x)‖ ≤ C * ‖y - x‖ := fun t ht => le_of_opNorm_le _ (bound _ <| segm <| Ico_subset_Icc_self ht) _ simpa [g] using norm_image_sub_le_of_norm_deriv_le_segment_01' hD bound /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `HasFDerivWithinAt` and `LipschitzOnWith`. -/ theorem lipschitzOnWith_of_nnnorm_hasFDerivWithin_le {C : ℝ≥0} (hf : ∀ x ∈ s, HasFDerivWithinAt f (f' x) s x) (bound : ∀ x ∈ s, ‖f' x‖₊ ≤ C) (hs : Convex ℝ s) : LipschitzOnWith C f s := by rw [lipschitzOnWith_iff_norm_sub_le] intro x x_in y y_in exact hs.norm_image_sub_le_of_norm_hasFDerivWithin_le hf bound y_in x_in /-- Let `s` be a convex set in a real normed vector space `E`, let `f : E → G` be a function differentiable within `s` in a neighborhood of `x : E` with derivative `f'`. Suppose that `f'` is continuous within `s` at `x`. Then for any number `K : ℝ≥0` larger than `‖f' x‖₊`, `f` is `K`-Lipschitz on some neighborhood of `x` within `s`. See also `Convex.exists_nhdsWithin_lipschitzOnWith_of_hasFDerivWithinAt` for a version that claims existence of `K` instead of an explicit estimate. -/ theorem exists_nhdsWithin_lipschitzOnWith_of_hasFDerivWithinAt_of_nnnorm_lt (hs : Convex ℝ s) {f : E → G} (hder : ∀ᶠ y in 𝓝[s] x, HasFDerivWithinAt f (f' y) s y) (hcont : ContinuousWithinAt f' s x) (K : ℝ≥0) (hK : ‖f' x‖₊ < K) : ∃ t ∈ 𝓝[s] x, LipschitzOnWith K f t := by obtain ⟨ε, ε0, hε⟩ : ∃ ε > 0, ball x ε ∩ s ⊆ { y | HasFDerivWithinAt f (f' y) s y ∧ ‖f' y‖₊ < K } := mem_nhdsWithin_iff.1 (hder.and <| hcont.nnnorm.eventually (gt_mem_nhds hK)) rw [inter_comm] at hε refine ⟨s ∩ ball x ε, inter_mem_nhdsWithin _ (ball_mem_nhds _ ε0), ?_⟩ exact (hs.inter (convex_ball _ _)).lipschitzOnWith_of_nnnorm_hasFDerivWithin_le (fun y hy => (hε hy).1.mono inter_subset_left) fun y hy => (hε hy).2.le /-- Let `s` be a convex set in a real normed vector space `E`, let `f : E → G` be a function differentiable within `s` in a neighborhood of `x : E` with derivative `f'`. Suppose that `f'` is continuous within `s` at `x`. Then for any number `K : ℝ≥0` larger than `‖f' x‖₊`, `f` is Lipschitz on some neighborhood of `x` within `s`. See also `Convex.exists_nhdsWithin_lipschitzOnWith_of_hasFDerivWithinAt_of_nnnorm_lt` for a version with an explicit estimate on the Lipschitz constant. -/ theorem exists_nhdsWithin_lipschitzOnWith_of_hasFDerivWithinAt (hs : Convex ℝ s) {f : E → G} (hder : ∀ᶠ y in 𝓝[s] x, HasFDerivWithinAt f (f' y) s y) (hcont : ContinuousWithinAt f' s x) : ∃ K, ∃ t ∈ 𝓝[s] x, LipschitzOnWith K f t := (exists_gt _).imp <| hs.exists_nhdsWithin_lipschitzOnWith_of_hasFDerivWithinAt_of_nnnorm_lt hder hcont /-- The mean value theorem on a convex set: if the derivative of a function within this set is bounded by `C`, then the function is `C`-Lipschitz. Version with `fderivWithin`. -/ theorem norm_image_sub_le_of_norm_fderivWithin_le (hf : DifferentiableOn 𝕜 f s) (bound : ∀ x ∈ s, ‖fderivWithin 𝕜 f s x‖ ≤ C) (hs : Convex ℝ s) (xs : x ∈ s) (ys : y ∈ s) : ‖f y - f x‖ ≤ C * ‖y - x‖ := hs.norm_image_sub_le_of_norm_hasFDerivWithin_le (fun x hx => (hf x hx).hasFDerivWithinAt) bound xs ys /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `fderivWithin` and `LipschitzOnWith`. -/ theorem lipschitzOnWith_of_nnnorm_fderivWithin_le {C : ℝ≥0} (hf : DifferentiableOn 𝕜 f s) (bound : ∀ x ∈ s, ‖fderivWithin 𝕜 f s x‖₊ ≤ C) (hs : Convex ℝ s) : LipschitzOnWith C f s := hs.lipschitzOnWith_of_nnnorm_hasFDerivWithin_le (fun x hx => (hf x hx).hasFDerivWithinAt) bound /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `fderiv`. -/ theorem norm_image_sub_le_of_norm_fderiv_le (hf : ∀ x ∈ s, DifferentiableAt 𝕜 f x) (bound : ∀ x ∈ s, ‖fderiv 𝕜 f x‖ ≤ C) (hs : Convex ℝ s) (xs : x ∈ s) (ys : y ∈ s) : ‖f y - f x‖ ≤ C * ‖y - x‖ := hs.norm_image_sub_le_of_norm_hasFDerivWithin_le (fun x hx => (hf x hx).hasFDerivAt.hasFDerivWithinAt) bound xs ys /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `fderiv` and `LipschitzOnWith`. -/ theorem lipschitzOnWith_of_nnnorm_fderiv_le {C : ℝ≥0} (hf : ∀ x ∈ s, DifferentiableAt 𝕜 f x) (bound : ∀ x ∈ s, ‖fderiv 𝕜 f x‖₊ ≤ C) (hs : Convex ℝ s) : LipschitzOnWith C f s := hs.lipschitzOnWith_of_nnnorm_hasFDerivWithin_le (fun x hx => (hf x hx).hasFDerivAt.hasFDerivWithinAt) bound /-- The mean value theorem: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `fderiv` and `LipschitzWith`. -/ theorem _root_.lipschitzWith_of_nnnorm_fderiv_le {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {f : E → G} {C : ℝ≥0} (hf : Differentiable 𝕜 f) (bound : ∀ x, ‖fderiv 𝕜 f x‖₊ ≤ C) : LipschitzWith C f := by letI : RCLike 𝕜 := IsRCLikeNormedField.rclike 𝕜 let A : NormedSpace ℝ E := RestrictScalars.normedSpace ℝ 𝕜 E rw [← lipschitzOnWith_univ] exact lipschitzOnWith_of_nnnorm_fderiv_le (fun x _ ↦ hf x) (fun x _ ↦ bound x) convex_univ /-- Variant of the mean value inequality on a convex set, using a bound on the difference between the derivative and a fixed linear map, rather than a bound on the derivative itself. Version with `HasFDerivWithinAt`. -/ theorem norm_image_sub_le_of_norm_hasFDerivWithin_le' (hf : ∀ x ∈ s, HasFDerivWithinAt f (f' x) s x) (bound : ∀ x ∈ s, ‖f' x - φ‖ ≤ C) (hs : Convex ℝ s) (xs : x ∈ s) (ys : y ∈ s) : ‖f y - f x - φ (y - x)‖ ≤ C * ‖y - x‖ := by /- We subtract `φ` to define a new function `g` for which `g' = 0`, for which the previous theorem applies, `Convex.norm_image_sub_le_of_norm_hasFDerivWithin_le`. Then, we just need to glue together the pieces, expressing back `f` in terms of `g`. -/ let g y := f y - φ y have hg : ∀ x ∈ s, HasFDerivWithinAt g (f' x - φ) s x := fun x xs => (hf x xs).sub φ.hasFDerivWithinAt calc ‖f y - f x - φ (y - x)‖ = ‖f y - f x - (φ y - φ x)‖ := by simp _ = ‖f y - φ y - (f x - φ x)‖ := by congr 1; abel _ = ‖g y - g x‖ := by simp [g] _ ≤ C * ‖y - x‖ := Convex.norm_image_sub_le_of_norm_hasFDerivWithin_le hg bound hs xs ys /-- Variant of the mean value inequality on a convex set. Version with `fderivWithin`. -/ theorem norm_image_sub_le_of_norm_fderivWithin_le' (hf : DifferentiableOn 𝕜 f s) (bound : ∀ x ∈ s, ‖fderivWithin 𝕜 f s x - φ‖ ≤ C) (hs : Convex ℝ s) (xs : x ∈ s) (ys : y ∈ s) : ‖f y - f x - φ (y - x)‖ ≤ C * ‖y - x‖ := hs.norm_image_sub_le_of_norm_hasFDerivWithin_le' (fun x hx => (hf x hx).hasFDerivWithinAt) bound xs ys /-- Variant of the mean value inequality on a convex set. Version with `fderiv`. -/ theorem norm_image_sub_le_of_norm_fderiv_le' (hf : ∀ x ∈ s, DifferentiableAt 𝕜 f x) (bound : ∀ x ∈ s, ‖fderiv 𝕜 f x - φ‖ ≤ C) (hs : Convex ℝ s) (xs : x ∈ s) (ys : y ∈ s) : ‖f y - f x - φ (y - x)‖ ≤ C * ‖y - x‖ := hs.norm_image_sub_le_of_norm_hasFDerivWithin_le' (fun x hx => (hf x hx).hasFDerivAt.hasFDerivWithinAt) bound xs ys /-- If a function has zero Fréchet derivative at every point of a convex set, then it is a constant on this set. -/ theorem is_const_of_fderivWithin_eq_zero (hs : Convex ℝ s) (hf : DifferentiableOn 𝕜 f s) (hf' : ∀ x ∈ s, fderivWithin 𝕜 f s x = 0) (hx : x ∈ s) (hy : y ∈ s) : f x = f y := by have bound : ∀ x ∈ s, ‖fderivWithin 𝕜 f s x‖ ≤ 0 := fun x hx => by simp only [hf' x hx, norm_zero, le_rfl] simpa only [(dist_eq_norm _ _).symm, zero_mul, dist_le_zero, eq_comm] using hs.norm_image_sub_le_of_norm_fderivWithin_le hf bound hx hy theorem _root_.is_const_of_fderiv_eq_zero {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {f : E → G} (hf : Differentiable 𝕜 f) (hf' : ∀ x, fderiv 𝕜 f x = 0) (x y : E) : f x = f y := by letI : RCLike 𝕜 := IsRCLikeNormedField.rclike 𝕜 let A : NormedSpace ℝ E := RestrictScalars.normedSpace ℝ 𝕜 E exact convex_univ.is_const_of_fderivWithin_eq_zero hf.differentiableOn (fun x _ => by rw [fderivWithin_univ]; exact hf' x) trivial trivial /-- If two functions have equal Fréchet derivatives at every point of a convex set, and are equal at one point in that set, then they are equal on that set. -/ theorem eqOn_of_fderivWithin_eq (hs : Convex ℝ s) (hf : DifferentiableOn 𝕜 f s) (hg : DifferentiableOn 𝕜 g s) (hs' : UniqueDiffOn 𝕜 s) (hf' : s.EqOn (fderivWithin 𝕜 f s) (fderivWithin 𝕜 g s)) (hx : x ∈ s) (hfgx : f x = g x) : s.EqOn f g := fun y hy => by suffices f x - g x = f y - g y by rwa [hfgx, sub_self, eq_comm, sub_eq_zero] at this refine hs.is_const_of_fderivWithin_eq_zero (hf.sub hg) (fun z hz => ?_) hx hy rw [fderivWithin_sub (hs' _ hz) (hf _ hz) (hg _ hz), sub_eq_zero, hf' hz] /-- If `f` has zero derivative on an open set, then `f` is locally constant on `s`. -/ -- TODO: change the spelling once we have `IsLocallyConstantOn`. theorem _root_.IsOpen.isOpen_inter_preimage_of_fderiv_eq_zero (hs : IsOpen s) (hf : DifferentiableOn 𝕜 f s) (hf' : s.EqOn (fderiv 𝕜 f) 0) (t : Set G) : IsOpen (s ∩ f ⁻¹' t) := by refine Metric.isOpen_iff.mpr fun y ⟨hy, hy'⟩ ↦ ?_ obtain ⟨r, hr, h⟩ := Metric.isOpen_iff.mp hs y hy refine ⟨r, hr, Set.subset_inter h fun x hx ↦ ?_⟩ have := (convex_ball y r).is_const_of_fderivWithin_eq_zero (hf.mono h) ?_ hx (mem_ball_self hr) · simpa [this] · intro z hz simpa only [fderivWithin_of_isOpen Metric.isOpen_ball hz] using hf' (h hz) theorem _root_.isLocallyConstant_of_fderiv_eq_zero (h₁ : Differentiable 𝕜 f) (h₂ : ∀ x, fderiv 𝕜 f x = 0) : IsLocallyConstant f := by simpa using isOpen_univ.isOpen_inter_preimage_of_fderiv_eq_zero h₁.differentiableOn fun _ _ ↦ h₂ _ /-- If `f` has zero derivative on a connected open set, then `f` is constant on `s`. -/ theorem _root_.IsOpen.exists_is_const_of_fderiv_eq_zero (hs : IsOpen s) (hs' : IsPreconnected s) (hf : DifferentiableOn 𝕜 f s) (hf' : s.EqOn (fderiv 𝕜 f) 0) : ∃ a, ∀ x ∈ s, f x = a := by obtain (rfl|⟨y, hy⟩) := s.eq_empty_or_nonempty · exact ⟨0, by simp⟩ · refine ⟨f y, fun x hx ↦ ?_⟩ have h₁ := hs.isOpen_inter_preimage_of_fderiv_eq_zero hf hf' {f y} have h₂ := hf.continuousOn.comp_continuous continuous_subtype_val (fun x ↦ x.2)
by_contra h₃ obtain ⟨t, ht, ht'⟩ := (isClosed_singleton (x := f y)).preimage h₂ have ht'' : ∀ a ∈ s, a ∈ t ↔ f a ≠ f y := by simpa [Set.ext_iff] using ht' obtain ⟨z, H₁, H₂, H₃⟩ := hs' _ _ h₁ ht (fun x h ↦ by simp [h, ht'', eq_or_ne]) ⟨y, by simpa⟩ ⟨x, by simp [ht'' _ hx, hx, h₃]⟩ exact (ht'' _ H₁).mp H₃ H₂.2
Mathlib/Analysis/Calculus/MeanValue.lean
607
613
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad, Minchao Wu, Mario Carneiro -/ import Mathlib.Data.Finset.Attach import Mathlib.Data.Finset.Disjoint import Mathlib.Data.Finset.Erase import Mathlib.Data.Finset.Filter import Mathlib.Data.Finset.Range import Mathlib.Data.Finset.SDiff import Mathlib.Data.Multiset.Basic import Mathlib.Logic.Equiv.Set import Mathlib.Order.Directed import Mathlib.Order.Interval.Set.Defs import Mathlib.Data.Set.SymmDiff /-! # Basic lemmas on finite sets This file contains lemmas on the interaction of various definitions on the `Finset` type. For an explanation of `Finset` design decisions, please see `Mathlib/Data/Finset/Defs.lean`. ## Main declarations ### Main definitions * `Finset.choose`: Given a proof `h` of existence and uniqueness of a certain element satisfying a predicate, `choose s h` returns the element of `s` satisfying that predicate. ### Equivalences between finsets * The `Mathlib/Logic/Equiv/Defs.lean` file describes a general type of equivalence, so look in there for any lemmas. There is some API for rewriting sums and products from `s` to `t` given that `s ≃ t`. TODO: examples ## Tags finite sets, finset -/ -- Assert that we define `Finset` without the material on `List.sublists`. -- Note that we cannot use `List.sublists` itself as that is defined very early. assert_not_exists List.sublistsLen Multiset.powerset CompleteLattice Monoid open Multiset Subtype Function universe u variable {α : Type*} {β : Type*} {γ : Type*} namespace Finset -- TODO: these should be global attributes, but this will require fixing other files attribute [local trans] Subset.trans Superset.trans set_option linter.deprecated false in @[deprecated "Deprecated without replacement." (since := "2025-02-07")] theorem sizeOf_lt_sizeOf_of_mem [SizeOf α] {x : α} {s : Finset α} (hx : x ∈ s) : SizeOf.sizeOf x < SizeOf.sizeOf s := by cases s dsimp [SizeOf.sizeOf, SizeOf.sizeOf, Multiset.sizeOf] rw [Nat.add_comm] refine lt_trans ?_ (Nat.lt_succ_self _) exact Multiset.sizeOf_lt_sizeOf_of_mem hx /-! ### Lattice structure -/ section Lattice variable [DecidableEq α] {s s₁ s₂ t t₁ t₂ u v : Finset α} {a b : α} /-! #### union -/ @[simp] theorem disjUnion_eq_union (s t h) : @disjUnion α s t h = s ∪ t := ext fun a => by simp @[simp] theorem disjoint_union_left : Disjoint (s ∪ t) u ↔ Disjoint s u ∧ Disjoint t u := by simp only [disjoint_left, mem_union, or_imp, forall_and] @[simp] theorem disjoint_union_right : Disjoint s (t ∪ u) ↔ Disjoint s t ∧ Disjoint s u := by simp only [disjoint_right, mem_union, or_imp, forall_and] /-! #### inter -/ theorem not_disjoint_iff_nonempty_inter : ¬Disjoint s t ↔ (s ∩ t).Nonempty := not_disjoint_iff.trans <| by simp [Finset.Nonempty] alias ⟨_, Nonempty.not_disjoint⟩ := not_disjoint_iff_nonempty_inter theorem disjoint_or_nonempty_inter (s t : Finset α) : Disjoint s t ∨ (s ∩ t).Nonempty := by rw [← not_disjoint_iff_nonempty_inter] exact em _ omit [DecidableEq α] in theorem disjoint_of_subset_iff_left_eq_empty (h : s ⊆ t) : Disjoint s t ↔ s = ∅ := disjoint_of_le_iff_left_eq_bot h lemma pairwiseDisjoint_iff {ι : Type*} {s : Set ι} {f : ι → Finset α} : s.PairwiseDisjoint f ↔ ∀ ⦃i⦄, i ∈ s → ∀ ⦃j⦄, j ∈ s → (f i ∩ f j).Nonempty → i = j := by simp [Set.PairwiseDisjoint, Set.Pairwise, Function.onFun, not_imp_comm (a := _ = _), not_disjoint_iff_nonempty_inter] end Lattice instance isDirected_le : IsDirected (Finset α) (· ≤ ·) := by classical infer_instance instance isDirected_subset : IsDirected (Finset α) (· ⊆ ·) := isDirected_le /-! ### erase -/ section Erase variable [DecidableEq α] {s t u v : Finset α} {a b : α} @[simp] theorem erase_empty (a : α) : erase ∅ a = ∅ := rfl protected lemma Nontrivial.erase_nonempty (hs : s.Nontrivial) : (s.erase a).Nonempty := (hs.exists_ne a).imp <| by aesop @[simp] lemma erase_nonempty (ha : a ∈ s) : (s.erase a).Nonempty ↔ s.Nontrivial := by simp only [Finset.Nonempty, mem_erase, and_comm (b := _ ∈ _)] refine ⟨?_, fun hs ↦ hs.exists_ne a⟩ rintro ⟨b, hb, hba⟩ exact ⟨_, hb, _, ha, hba⟩ @[simp] theorem erase_singleton (a : α) : ({a} : Finset α).erase a = ∅ := by ext x simp @[simp] theorem erase_insert_eq_erase (s : Finset α) (a : α) : (insert a s).erase a = s.erase a := ext fun x => by simp +contextual only [mem_erase, mem_insert, and_congr_right_iff, false_or, iff_self, imp_true_iff] theorem erase_insert {a : α} {s : Finset α} (h : a ∉ s) : erase (insert a s) a = s := by rw [erase_insert_eq_erase, erase_eq_of_not_mem h] theorem erase_insert_of_ne {a b : α} {s : Finset α} (h : a ≠ b) : erase (insert a s) b = insert a (erase s b) := ext fun x => by have : x ≠ b ∧ x = a ↔ x = a := and_iff_right_of_imp fun hx => hx.symm ▸ h simp only [mem_erase, mem_insert, and_or_left, this] theorem erase_cons_of_ne {a b : α} {s : Finset α} (ha : a ∉ s) (hb : a ≠ b) : erase (cons a s ha) b = cons a (erase s b) fun h => ha <| erase_subset _ _ h := by simp only [cons_eq_insert, erase_insert_of_ne hb] @[simp] theorem insert_erase (h : a ∈ s) : insert a (erase s a) = s := ext fun x => by simp only [mem_insert, mem_erase, or_and_left, dec_em, true_and] apply or_iff_right_of_imp rintro rfl exact h lemma erase_eq_iff_eq_insert (hs : a ∈ s) (ht : a ∉ t) : erase s a = t ↔ s = insert a t := by aesop lemma insert_erase_invOn : Set.InvOn (insert a) (fun s ↦ erase s a) {s : Finset α | a ∈ s} {s : Finset α | a ∉ s} := ⟨fun _s ↦ insert_erase, fun _s ↦ erase_insert⟩ theorem erase_ssubset {a : α} {s : Finset α} (h : a ∈ s) : s.erase a ⊂ s := calc s.erase a ⊂ insert a (s.erase a) := ssubset_insert <| not_mem_erase _ _ _ = _ := insert_erase h theorem ssubset_iff_exists_subset_erase {s t : Finset α} : s ⊂ t ↔ ∃ a ∈ t, s ⊆ t.erase a := by refine ⟨fun h => ?_, fun ⟨a, ha, h⟩ => ssubset_of_subset_of_ssubset h <| erase_ssubset ha⟩ obtain ⟨a, ht, hs⟩ := not_subset.1 h.2 exact ⟨a, ht, subset_erase.2 ⟨h.1, hs⟩⟩ theorem erase_ssubset_insert (s : Finset α) (a : α) : s.erase a ⊂ insert a s := ssubset_iff_exists_subset_erase.2 ⟨a, mem_insert_self _ _, erase_subset_erase _ <| subset_insert _ _⟩ theorem erase_cons {s : Finset α} {a : α} (h : a ∉ s) : (s.cons a h).erase a = s := by rw [cons_eq_insert, erase_insert_eq_erase, erase_eq_of_not_mem h] theorem subset_insert_iff {a : α} {s t : Finset α} : s ⊆ insert a t ↔ erase s a ⊆ t := by simp only [subset_iff, or_iff_not_imp_left, mem_erase, mem_insert, and_imp] exact forall_congr' fun x => forall_swap theorem erase_insert_subset (a : α) (s : Finset α) : erase (insert a s) a ⊆ s := subset_insert_iff.1 <| Subset.rfl theorem insert_erase_subset (a : α) (s : Finset α) : s ⊆ insert a (erase s a) := subset_insert_iff.2 <| Subset.rfl theorem subset_insert_iff_of_not_mem (h : a ∉ s) : s ⊆ insert a t ↔ s ⊆ t := by rw [subset_insert_iff, erase_eq_of_not_mem h] theorem erase_subset_iff_of_mem (h : a ∈ t) : s.erase a ⊆ t ↔ s ⊆ t := by rw [← subset_insert_iff, insert_eq_of_mem h] theorem erase_injOn' (a : α) : { s : Finset α | a ∈ s }.InjOn fun s => erase s a := fun s hs t ht (h : s.erase a = _) => by rw [← insert_erase hs, ← insert_erase ht, h] end Erase lemma Nontrivial.exists_cons_eq {s : Finset α} (hs : s.Nontrivial) : ∃ t a ha b hb hab, (cons b t hb).cons a (mem_cons.not.2 <| not_or_intro hab ha) = s := by classical obtain ⟨a, ha, b, hb, hab⟩ := hs have : b ∈ s.erase a := mem_erase.2 ⟨hab.symm, hb⟩ refine ⟨(s.erase a).erase b, a, ?_, b, ?_, ?_, ?_⟩ <;> simp [insert_erase this, insert_erase ha, *] /-! ### sdiff -/ section Sdiff variable [DecidableEq α] {s t u v : Finset α} {a b : α} lemma erase_sdiff_erase (hab : a ≠ b) (hb : b ∈ s) : s.erase a \ s.erase b = {b} := by ext; aesop -- TODO: Do we want to delete this lemma and `Finset.disjUnion_singleton`, -- or instead add `Finset.union_singleton`/`Finset.singleton_union`? theorem sdiff_singleton_eq_erase (a : α) (s : Finset α) : s \ {a} = erase s a := by ext rw [mem_erase, mem_sdiff, mem_singleton, and_comm] -- This lemma matches `Finset.insert_eq` in functionality. theorem erase_eq (s : Finset α) (a : α) : s.erase a = s \ {a} := (sdiff_singleton_eq_erase _ _).symm theorem disjoint_erase_comm : Disjoint (s.erase a) t ↔ Disjoint s (t.erase a) := by simp_rw [erase_eq, disjoint_sdiff_comm] lemma disjoint_insert_erase (ha : a ∉ t) : Disjoint (s.erase a) (insert a t) ↔ Disjoint s t := by rw [disjoint_erase_comm, erase_insert ha] lemma disjoint_erase_insert (ha : a ∉ s) : Disjoint (insert a s) (t.erase a) ↔ Disjoint s t := by rw [← disjoint_erase_comm, erase_insert ha] theorem disjoint_of_erase_left (ha : a ∉ t) (hst : Disjoint (s.erase a) t) : Disjoint s t := by rw [← erase_insert ha, ← disjoint_erase_comm, disjoint_insert_right] exact ⟨not_mem_erase _ _, hst⟩ theorem disjoint_of_erase_right (ha : a ∉ s) (hst : Disjoint s (t.erase a)) : Disjoint s t := by rw [← erase_insert ha, disjoint_erase_comm, disjoint_insert_left] exact ⟨not_mem_erase _ _, hst⟩ theorem inter_erase (a : α) (s t : Finset α) : s ∩ t.erase a = (s ∩ t).erase a := by simp only [erase_eq, inter_sdiff_assoc] @[simp] theorem erase_inter (a : α) (s t : Finset α) : s.erase a ∩ t = (s ∩ t).erase a := by simpa only [inter_comm t] using inter_erase a t s theorem erase_sdiff_comm (s t : Finset α) (a : α) : s.erase a \ t = (s \ t).erase a := by simp_rw [erase_eq, sdiff_right_comm] theorem erase_inter_comm (s t : Finset α) (a : α) : s.erase a ∩ t = s ∩ t.erase a := by rw [erase_inter, inter_erase] theorem erase_union_distrib (s t : Finset α) (a : α) : (s ∪ t).erase a = s.erase a ∪ t.erase a := by simp_rw [erase_eq, union_sdiff_distrib] theorem insert_inter_distrib (s t : Finset α) (a : α) : insert a (s ∩ t) = insert a s ∩ insert a t := by simp_rw [insert_eq, union_inter_distrib_left] theorem erase_sdiff_distrib (s t : Finset α) (a : α) : (s \ t).erase a = s.erase a \ t.erase a := by simp_rw [erase_eq, sdiff_sdiff, sup_sdiff_eq_sup le_rfl, sup_comm] theorem erase_union_of_mem (ha : a ∈ t) (s : Finset α) : s.erase a ∪ t = s ∪ t := by rw [← insert_erase (mem_union_right s ha), erase_union_distrib, ← union_insert, insert_erase ha] theorem union_erase_of_mem (ha : a ∈ s) (t : Finset α) : s ∪ t.erase a = s ∪ t := by rw [← insert_erase (mem_union_left t ha), erase_union_distrib, ← insert_union, insert_erase ha] theorem sdiff_union_erase_cancel (hts : t ⊆ s) (ha : a ∈ t) : s \ t ∪ t.erase a = s.erase a := by simp_rw [erase_eq, sdiff_union_sdiff_cancel hts (singleton_subset_iff.2 ha)] theorem sdiff_insert (s t : Finset α) (x : α) : s \ insert x t = (s \ t).erase x := by simp_rw [← sdiff_singleton_eq_erase, insert_eq, sdiff_sdiff_left', sdiff_union_distrib, inter_comm] theorem sdiff_insert_insert_of_mem_of_not_mem {s t : Finset α} {x : α} (hxs : x ∈ s) (hxt : x ∉ t) : insert x (s \ insert x t) = s \ t := by rw [sdiff_insert, insert_erase (mem_sdiff.mpr ⟨hxs, hxt⟩)] theorem sdiff_erase (h : a ∈ s) : s \ t.erase a = insert a (s \ t) := by rw [← sdiff_singleton_eq_erase, sdiff_sdiff_eq_sdiff_union (singleton_subset_iff.2 h), insert_eq, union_comm] theorem sdiff_erase_self (ha : a ∈ s) : s \ s.erase a = {a} := by rw [sdiff_erase ha, Finset.sdiff_self, insert_empty_eq] theorem erase_eq_empty_iff (s : Finset α) (a : α) : s.erase a = ∅ ↔ s = ∅ ∨ s = {a} := by rw [← sdiff_singleton_eq_erase, sdiff_eq_empty_iff_subset, subset_singleton_iff] --TODO@Yaël: Kill lemmas duplicate with `BooleanAlgebra` theorem sdiff_disjoint : Disjoint (t \ s) s := disjoint_left.2 fun _a ha => (mem_sdiff.1 ha).2 theorem disjoint_sdiff : Disjoint s (t \ s) := sdiff_disjoint.symm theorem disjoint_sdiff_inter (s t : Finset α) : Disjoint (s \ t) (s ∩ t) := disjoint_of_subset_right inter_subset_right sdiff_disjoint end Sdiff /-! ### attach -/ @[simp] theorem attach_empty : attach (∅ : Finset α) = ∅ := rfl @[simp] theorem attach_nonempty_iff {s : Finset α} : s.attach.Nonempty ↔ s.Nonempty := by simp [Finset.Nonempty] @[aesop safe apply (rule_sets := [finsetNonempty])] protected alias ⟨_, Nonempty.attach⟩ := attach_nonempty_iff @[simp] theorem attach_eq_empty_iff {s : Finset α} : s.attach = ∅ ↔ s = ∅ := by simp [eq_empty_iff_forall_not_mem] /-! ### filter -/ section Filter variable (p q : α → Prop) [DecidablePred p] [DecidablePred q] {s t : Finset α} theorem filter_singleton (a : α) : filter p {a} = if p a then {a} else ∅ := by classical ext x simp only [mem_singleton, forall_eq, mem_filter] split_ifs with h <;> by_cases h' : x = a <;> simp [h, h'] theorem filter_cons_of_pos (a : α) (s : Finset α) (ha : a ∉ s) (hp : p a) : filter p (cons a s ha) = cons a (filter p s) ((mem_of_mem_filter _).mt ha) := eq_of_veq <| Multiset.filter_cons_of_pos s.val hp theorem filter_cons_of_neg (a : α) (s : Finset α) (ha : a ∉ s) (hp : ¬p a) : filter p (cons a s ha) = filter p s := eq_of_veq <| Multiset.filter_cons_of_neg s.val hp theorem disjoint_filter {s : Finset α} {p q : α → Prop} [DecidablePred p] [DecidablePred q] : Disjoint (s.filter p) (s.filter q) ↔ ∀ x ∈ s, p x → ¬q x := by constructor <;> simp +contextual [disjoint_left] theorem disjoint_filter_filter' (s t : Finset α) {p q : α → Prop} [DecidablePred p] [DecidablePred q] (h : Disjoint p q) : Disjoint (s.filter p) (t.filter q) := by simp_rw [disjoint_left, mem_filter] rintro a ⟨_, hp⟩ ⟨_, hq⟩ rw [Pi.disjoint_iff] at h simpa [hp, hq] using h a theorem disjoint_filter_filter_neg (s t : Finset α) (p : α → Prop) [DecidablePred p] [∀ x, Decidable (¬p x)] : Disjoint (s.filter p) (t.filter fun a => ¬p a) := disjoint_filter_filter' s t disjoint_compl_right theorem filter_disj_union (s : Finset α) (t : Finset α) (h : Disjoint s t) : filter p (disjUnion s t h) = (filter p s).disjUnion (filter p t) (disjoint_filter_filter h) := eq_of_veq <| Multiset.filter_add _ _ _ theorem filter_cons {a : α} (s : Finset α) (ha : a ∉ s) : filter p (cons a s ha) = if p a then cons a (filter p s) ((mem_of_mem_filter _).mt ha) else filter p s := by split_ifs with h · rw [filter_cons_of_pos _ _ _ ha h] · rw [filter_cons_of_neg _ _ _ ha h] section variable [DecidableEq α] theorem filter_union (s₁ s₂ : Finset α) : (s₁ ∪ s₂).filter p = s₁.filter p ∪ s₂.filter p := ext fun _ => by simp only [mem_filter, mem_union, or_and_right] theorem filter_union_right (s : Finset α) : s.filter p ∪ s.filter q = s.filter fun x => p x ∨ q x := ext fun x => by simp [mem_filter, mem_union, ← and_or_left] theorem filter_mem_eq_inter {s t : Finset α} [∀ i, Decidable (i ∈ t)] : (s.filter fun i => i ∈ t) = s ∩ t := ext fun i => by simp [mem_filter, mem_inter] theorem filter_inter_distrib (s t : Finset α) : (s ∩ t).filter p = s.filter p ∩ t.filter p := by ext simp [mem_filter, mem_inter, and_assoc] theorem filter_inter (s t : Finset α) : filter p s ∩ t = filter p (s ∩ t) := by ext simp only [mem_inter, mem_filter, and_right_comm] theorem inter_filter (s t : Finset α) : s ∩ filter p t = filter p (s ∩ t) := by rw [inter_comm, filter_inter, inter_comm] theorem filter_insert (a : α) (s : Finset α) : filter p (insert a s) = if p a then insert a (filter p s) else filter p s := by ext x split_ifs with h <;> by_cases h' : x = a <;> simp [h, h'] theorem filter_erase (a : α) (s : Finset α) : filter p (erase s a) = erase (filter p s) a := by ext x simp only [and_assoc, mem_filter, iff_self, mem_erase] theorem filter_or (s : Finset α) : (s.filter fun a => p a ∨ q a) = s.filter p ∪ s.filter q := ext fun _ => by simp [mem_filter, mem_union, and_or_left] theorem filter_and (s : Finset α) : (s.filter fun a => p a ∧ q a) = s.filter p ∩ s.filter q := ext fun _ => by simp [mem_filter, mem_inter, and_comm, and_left_comm, and_self_iff, and_assoc] theorem filter_not (s : Finset α) : (s.filter fun a => ¬p a) = s \ s.filter p := ext fun a => by simp only [Bool.decide_coe, Bool.not_eq_true', mem_filter, and_comm, mem_sdiff, not_and_or, Bool.not_eq_true, and_or_left, and_not_self, or_false] lemma filter_and_not (s : Finset α) (p q : α → Prop) [DecidablePred p] [DecidablePred q] : s.filter (fun a ↦ p a ∧ ¬ q a) = s.filter p \ s.filter q := by rw [filter_and, filter_not, ← inter_sdiff_assoc, inter_eq_left.2 (filter_subset _ _)] theorem sdiff_eq_filter (s₁ s₂ : Finset α) : s₁ \ s₂ = filter (· ∉ s₂) s₁ := ext fun _ => by simp [mem_sdiff, mem_filter] theorem subset_union_elim {s : Finset α} {t₁ t₂ : Set α} (h : ↑s ⊆ t₁ ∪ t₂) : ∃ s₁ s₂ : Finset α, s₁ ∪ s₂ = s ∧ ↑s₁ ⊆ t₁ ∧ ↑s₂ ⊆ t₂ \ t₁ := by classical refine ⟨s.filter (· ∈ t₁), s.filter (· ∉ t₁), ?_, ?_, ?_⟩ · simp [filter_union_right, em] · intro x simp · intro x simp only [not_not, coe_filter, Set.mem_setOf_eq, Set.mem_diff, and_imp] intro hx hx₂ exact ⟨Or.resolve_left (h hx) hx₂, hx₂⟩ -- This is not a good simp lemma, as it would prevent `Finset.mem_filter` from firing -- on, e.g. `x ∈ s.filter (Eq b)`. /-- After filtering out everything that does not equal a given value, at most that value remains. This is equivalent to `filter_eq'` with the equality the other way. -/ theorem filter_eq [DecidableEq β] (s : Finset β) (b : β) : s.filter (Eq b) = ite (b ∈ s) {b} ∅ := by split_ifs with h · ext simp only [mem_filter, mem_singleton, decide_eq_true_eq] refine ⟨fun h => h.2.symm, ?_⟩ rintro rfl exact ⟨h, rfl⟩ · ext simp only [mem_filter, not_and, iff_false, not_mem_empty, decide_eq_true_eq] rintro m rfl exact h m /-- After filtering out everything that does not equal a given value, at most that value remains. This is equivalent to `filter_eq` with the equality the other way. -/ theorem filter_eq' [DecidableEq β] (s : Finset β) (b : β) : (s.filter fun a => a = b) = ite (b ∈ s) {b} ∅ := _root_.trans (filter_congr fun _ _ => by simp_rw [@eq_comm _ b]) (filter_eq s b) theorem filter_ne [DecidableEq β] (s : Finset β) (b : β) : (s.filter fun a => b ≠ a) = s.erase b := by ext simp only [mem_filter, mem_erase, Ne, decide_not, Bool.not_eq_true', decide_eq_false_iff_not] tauto theorem filter_ne' [DecidableEq β] (s : Finset β) (b : β) : (s.filter fun a => a ≠ b) = s.erase b := _root_.trans (filter_congr fun _ _ => by simp_rw [@ne_comm _ b]) (filter_ne s b) theorem filter_union_filter_of_codisjoint (s : Finset α) (h : Codisjoint p q) : s.filter p ∪ s.filter q = s := (filter_or _ _ _).symm.trans <| filter_true_of_mem fun x _ => h.top_le x trivial theorem filter_union_filter_neg_eq [∀ x, Decidable (¬p x)] (s : Finset α) : (s.filter p ∪ s.filter fun a => ¬p a) = s := filter_union_filter_of_codisjoint _ _ _ <| @codisjoint_hnot_right _ _ p end end Filter /-! ### range -/ section Range open Nat variable {n m l : ℕ} @[simp] theorem range_filter_eq {n m : ℕ} : (range n).filter (· = m) = if m < n then {m} else ∅ := by convert filter_eq (range n) m using 2 · ext rw [eq_comm] · simp end Range end Finset /-! ### dedup on list and multiset -/ namespace Multiset variable [DecidableEq α] {s t : Multiset α} @[simp] theorem toFinset_add (s t : Multiset α) : toFinset (s + t) = toFinset s ∪ toFinset t := Finset.ext <| by simp @[simp] theorem toFinset_inter (s t : Multiset α) : toFinset (s ∩ t) = toFinset s ∩ toFinset t := Finset.ext <| by simp @[simp] theorem toFinset_union (s t : Multiset α) : (s ∪ t).toFinset = s.toFinset ∪ t.toFinset := by ext; simp @[simp] theorem toFinset_eq_empty {m : Multiset α} : m.toFinset = ∅ ↔ m = 0 := Finset.val_inj.symm.trans Multiset.dedup_eq_zero @[simp] theorem toFinset_nonempty : s.toFinset.Nonempty ↔ s ≠ 0 := by simp only [toFinset_eq_empty, Ne, Finset.nonempty_iff_ne_empty] @[aesop safe apply (rule_sets := [finsetNonempty])] protected alias ⟨_, Aesop.toFinset_nonempty_of_ne⟩ := toFinset_nonempty @[simp] theorem toFinset_filter (s : Multiset α) (p : α → Prop) [DecidablePred p] : Multiset.toFinset (s.filter p) = s.toFinset.filter p := by ext; simp end Multiset namespace List variable [DecidableEq α] {l l' : List α} {a : α} {f : α → β} {s : Finset α} {t : Set β} {t' : Finset β} @[simp] theorem toFinset_union (l l' : List α) : (l ∪ l').toFinset = l.toFinset ∪ l'.toFinset := by ext simp @[simp] theorem toFinset_inter (l l' : List α) : (l ∩ l').toFinset = l.toFinset ∩ l'.toFinset := by ext simp @[aesop safe apply (rule_sets := [finsetNonempty])] alias ⟨_, Aesop.toFinset_nonempty_of_ne⟩ := toFinset_nonempty_iff @[simp] theorem toFinset_filter (s : List α) (p : α → Bool) : (s.filter p).toFinset = s.toFinset.filter (p ·) := by ext; simp [List.mem_filter] end List namespace Finset section ToList @[simp] theorem toList_eq_nil {s : Finset α} : s.toList = [] ↔ s = ∅ := Multiset.toList_eq_nil.trans val_eq_zero theorem empty_toList {s : Finset α} : s.toList.isEmpty ↔ s = ∅ := by simp @[simp] theorem toList_empty : (∅ : Finset α).toList = [] := toList_eq_nil.mpr rfl theorem Nonempty.toList_ne_nil {s : Finset α} (hs : s.Nonempty) : s.toList ≠ [] := mt toList_eq_nil.mp hs.ne_empty theorem Nonempty.not_empty_toList {s : Finset α} (hs : s.Nonempty) : ¬s.toList.isEmpty := mt empty_toList.mp hs.ne_empty end ToList /-! ### choose -/ section Choose variable (p : α → Prop) [DecidablePred p] (l : Finset α) /-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of `l` satisfying `p` this unique element, as an element of the corresponding subtype. -/ def chooseX (hp : ∃! a, a ∈ l ∧ p a) : { a // a ∈ l ∧ p a } := Multiset.chooseX p l.val hp /-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of `l` satisfying `p` this unique element, as an element of the ambient type. -/ def choose (hp : ∃! a, a ∈ l ∧ p a) : α := chooseX p l hp theorem choose_spec (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) := (chooseX p l hp).property theorem choose_mem (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1 theorem choose_property (hp : ∃! a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2 end Choose end Finset namespace Equiv variable [DecidableEq α] {s t : Finset α} open Finset /-- The disjoint union of finsets is a sum -/ def Finset.union (s t : Finset α) (h : Disjoint s t) : s ⊕ t ≃ (s ∪ t : Finset α) := Equiv.setCongr (coe_union _ _) |>.trans (Equiv.Set.union (disjoint_coe.mpr h)) |>.symm @[simp] theorem Finset.union_symm_inl (h : Disjoint s t) (x : s) : Equiv.Finset.union s t h (Sum.inl x) = ⟨x, Finset.mem_union.mpr <| Or.inl x.2⟩ := rfl @[simp] theorem Finset.union_symm_inr (h : Disjoint s t) (y : t) : Equiv.Finset.union s t h (Sum.inr y) = ⟨y, Finset.mem_union.mpr <| Or.inr y.2⟩ := rfl /-- The type of dependent functions on the disjoint union of finsets `s ∪ t` is equivalent to the type of pairs of functions on `s` and on `t`. This is similar to `Equiv.sumPiEquivProdPi`. -/ def piFinsetUnion {ι} [DecidableEq ι] (α : ι → Type*) {s t : Finset ι} (h : Disjoint s t) : ((∀ i : s, α i) × ∀ i : t, α i) ≃ ∀ i : (s ∪ t : Finset ι), α i := let e := Equiv.Finset.union s t h sumPiEquivProdPi (fun b ↦ α (e b)) |>.symm.trans (.piCongrLeft (fun i : ↥(s ∪ t) ↦ α i) e) /-- A finset is equivalent to its coercion as a set. -/ def _root_.Finset.equivToSet (s : Finset α) : s ≃ s.toSet where toFun a := ⟨a.1, mem_coe.2 a.2⟩ invFun a := ⟨a.1, mem_coe.1 a.2⟩ left_inv := fun _ ↦ rfl right_inv := fun _ ↦ rfl end Equiv namespace Multiset variable [DecidableEq α] @[simp] lemma toFinset_replicate (n : ℕ) (a : α) : (replicate n a).toFinset = if n = 0 then ∅ else {a} := by ext x simp only [mem_toFinset, Finset.mem_singleton, mem_replicate] split_ifs with hn <;> simp [hn] end Multiset
Mathlib/Data/Finset/Basic.lean
2,747
2,757
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Chris Hughes -/ import Mathlib.Algebra.GeomSum import Mathlib.Algebra.Polynomial.Roots import Mathlib.Data.Fintype.Inv import Mathlib.GroupTheory.SpecificGroups.Cyclic import Mathlib.Tactic.FieldSimp /-! # Integral domains Assorted theorems about integral domains. ## Main theorems * `isCyclic_of_subgroup_isDomain`: A finite subgroup of the units of an integral domain is cyclic. * `Fintype.fieldOfDomain`: A finite integral domain is a field. ## Notes Wedderburn's little theorem, which shows that all finite division rings are actually fields, is in `Mathlib.RingTheory.LittleWedderburn`. ## Tags integral domain, finite integral domain, finite field -/ section open Finset Polynomial Function Nat section CancelMonoidWithZero -- There doesn't seem to be a better home for these right now variable {M : Type*} [CancelMonoidWithZero M] [Finite M] theorem mul_right_bijective_of_finite₀ {a : M} (ha : a ≠ 0) : Bijective fun b => a * b := Finite.injective_iff_bijective.1 <| mul_right_injective₀ ha theorem mul_left_bijective_of_finite₀ {a : M} (ha : a ≠ 0) : Bijective fun b => b * a := Finite.injective_iff_bijective.1 <| mul_left_injective₀ ha /-- Every finite nontrivial cancel_monoid_with_zero is a group_with_zero. -/ def Fintype.groupWithZeroOfCancel (M : Type*) [CancelMonoidWithZero M] [DecidableEq M] [Fintype M] [Nontrivial M] : GroupWithZero M := { ‹Nontrivial M›, ‹CancelMonoidWithZero M› with inv := fun a => if h : a = 0 then 0 else Fintype.bijInv (mul_right_bijective_of_finite₀ h) 1 mul_inv_cancel := fun a ha => by simp only [Inv.inv, dif_neg ha] exact Fintype.rightInverse_bijInv _ _ inv_zero := by simp [Inv.inv, dif_pos rfl] } theorem exists_eq_pow_of_mul_eq_pow_of_coprime {R : Type*} [CommSemiring R] [IsDomain R] [GCDMonoid R] [Subsingleton Rˣ] {a b c : R} {n : ℕ} (cp : IsCoprime a b) (h : a * b = c ^ n) : ∃ d : R, a = d ^ n := by
refine exists_eq_pow_of_mul_eq_pow (isUnit_of_dvd_one ?_) h obtain ⟨x, y, hxy⟩ := cp rw [← hxy] exact -- Porting note: added `GCDMonoid.` twice dvd_add (dvd_mul_of_dvd_right (GCDMonoid.gcd_dvd_left _ _) _) (dvd_mul_of_dvd_right (GCDMonoid.gcd_dvd_right _ _) _) nonrec theorem Finset.exists_eq_pow_of_mul_eq_pow_of_coprime {ι R : Type*} [CommSemiring R] [IsDomain R]
Mathlib/RingTheory/IntegralDomain.lean
61
69
/- Copyright (c) 2021 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import Mathlib.Algebra.Order.Ring.Rat import Mathlib.Data.Multiset.Sort import Mathlib.Data.PNat.Basic import Mathlib.Data.PNat.Interval import Mathlib.Tactic.NormNum import Mathlib.Tactic.IntervalCases /-! # The inequality `p⁻¹ + q⁻¹ + r⁻¹ > 1` In this file we classify solutions to the inequality `(p⁻¹ + q⁻¹ + r⁻¹ : ℚ) > 1`, for positive natural numbers `p`, `q`, and `r`. The solutions are exactly of the form. * `A' q r := {1,q,r}` * `D' r := {2,2,r}` * `E6 := {2,3,3}`, or `E7 := {2,3,4}`, or `E8 := {2,3,5}` This inequality shows up in Lie theory, in the classification of Dynkin diagrams, root systems, and semisimple Lie algebras. ## Main declarations * `pqr.A' q r`, the multiset `{1,q,r}` * `pqr.D' r`, the multiset `{2,2,r}` * `pqr.E6`, the multiset `{2,3,3}` * `pqr.E7`, the multiset `{2,3,4}` * `pqr.E8`, the multiset `{2,3,5}` * `pqr.classification`, the classification of solutions to `p⁻¹ + q⁻¹ + r⁻¹ > 1` -/ namespace ADEInequality open Multiset /-- `A' q r := {1,q,r}` is a `Multiset ℕ+` that is a solution to the inequality `(p⁻¹ + q⁻¹ + r⁻¹ : ℚ) > 1`. -/ def A' (q r : ℕ+) : Multiset ℕ+ := {1, q, r} /-- `A r := {1,1,r}` is a `Multiset ℕ+` that is a solution to the inequality `(p⁻¹ + q⁻¹ + r⁻¹ : ℚ) > 1`. These solutions are related to the Dynkin diagrams $A_r$. -/ def A (r : ℕ+) : Multiset ℕ+ := A' 1 r /-- `D' r := {2,2,r}` is a `Multiset ℕ+` that is a solution to the inequality `(p⁻¹ + q⁻¹ + r⁻¹ : ℚ) > 1`. These solutions are related to the Dynkin diagrams $D_{r+2}$. -/ def D' (r : ℕ+) : Multiset ℕ+ := {2, 2, r} /-- `E' r := {2,3,r}` is a `Multiset ℕ+`. For `r ∈ {3,4,5}` is a solution to the inequality `(p⁻¹ + q⁻¹ + r⁻¹ : ℚ) > 1`. These solutions are related to the Dynkin diagrams $E_{r+3}$. -/ def E' (r : ℕ+) : Multiset ℕ+ := {2, 3, r} /-- `E6 := {2,3,3}` is a `Multiset ℕ+` that is a solution to the inequality `(p⁻¹ + q⁻¹ + r⁻¹ : ℚ) > 1`. This solution is related to the Dynkin diagrams $E_6$. -/ def E6 : Multiset ℕ+ := E' 3 /-- `E7 := {2,3,4}` is a `Multiset ℕ+` that is a solution to the inequality `(p⁻¹ + q⁻¹ + r⁻¹ : ℚ) > 1`. This solution is related to the Dynkin diagrams $E_7$. -/ def E7 : Multiset ℕ+ := E' 4 /-- `E8 := {2,3,5}` is a `Multiset ℕ+` that is a solution to the inequality `(p⁻¹ + q⁻¹ + r⁻¹ : ℚ) > 1`. This solution is related to the Dynkin diagrams $E_8$. -/ def E8 : Multiset ℕ+ := E' 5 /-- `sum_inv pqr` for a `pqr : Multiset ℕ+` is the sum of the inverses of the elements of `pqr`, as rational number. The intended argument is a multiset `{p,q,r}` of cardinality `3`. -/ def sumInv (pqr : Multiset ℕ+) : ℚ := Multiset.sum (pqr.map fun (x : ℕ+) => x⁻¹) theorem sumInv_pqr (p q r : ℕ+) : sumInv {p, q, r} = (p : ℚ)⁻¹ + (q : ℚ)⁻¹ + (r : ℚ)⁻¹ := by simp only [sumInv, add_zero, insert_eq_cons, add_assoc, map_cons, sum_cons, map_singleton, sum_singleton] /-- A multiset `pqr` of positive natural numbers is `admissible` if it is equal to `A' q r`, or `D' r`, or one of `E6`, `E7`, or `E8`. -/ def Admissible (pqr : Multiset ℕ+) : Prop := (∃ q r, A' q r = pqr) ∨ (∃ r, D' r = pqr) ∨ E' 3 = pqr ∨ E' 4 = pqr ∨ E' 5 = pqr theorem admissible_A' (q r : ℕ+) : Admissible (A' q r) := Or.inl ⟨q, r, rfl⟩ theorem admissible_D' (n : ℕ+) : Admissible (D' n) := Or.inr <| Or.inl ⟨n, rfl⟩ theorem admissible_E'3 : Admissible (E' 3) := Or.inr <| Or.inr <| Or.inl rfl theorem admissible_E'4 : Admissible (E' 4) := Or.inr <| Or.inr <| Or.inr <| Or.inl rfl theorem admissible_E'5 : Admissible (E' 5) := Or.inr <| Or.inr <| Or.inr <| Or.inr rfl theorem admissible_E6 : Admissible E6 := admissible_E'3 theorem admissible_E7 : Admissible E7 := admissible_E'4 theorem admissible_E8 : Admissible E8 := admissible_E'5 theorem Admissible.one_lt_sumInv {pqr : Multiset ℕ+} : Admissible pqr → 1 < sumInv pqr := by rw [Admissible] rintro (⟨p', q', H⟩ | ⟨n, H⟩ | H | H | H) · rw [← H, A', sumInv_pqr, add_assoc] simp only [lt_add_iff_pos_right, PNat.one_coe, inv_one, Nat.cast_one] apply add_pos <;> simp only [PNat.pos, Nat.cast_pos, inv_pos] · rw [← H, D', sumInv_pqr] norm_num all_goals rw [← H, E', sumInv_pqr] norm_num theorem lt_three {p q r : ℕ+} (hpq : p ≤ q) (hqr : q ≤ r) (H : 1 < sumInv {p, q, r}) : p < 3 := by have h3 : (0 : ℚ) < 3 := by norm_num contrapose! H rw [sumInv_pqr] have h3q := H.trans hpq have h3r := h3q.trans hqr have hp : (p : ℚ)⁻¹ ≤ 3⁻¹ := by rw [inv_le_inv₀ _ h3] · assumption_mod_cast · norm_num have hq : (q : ℚ)⁻¹ ≤ 3⁻¹ := by rw [inv_le_inv₀ _ h3] · assumption_mod_cast · norm_num have hr : (r : ℚ)⁻¹ ≤ 3⁻¹ := by rw [inv_le_inv₀ _ h3] · assumption_mod_cast · norm_num calc (p : ℚ)⁻¹ + (q : ℚ)⁻¹ + (r : ℚ)⁻¹ ≤ 3⁻¹ + 3⁻¹ + 3⁻¹ := add_le_add (add_le_add hp hq) hr _ = 1 := by norm_num theorem lt_four {q r : ℕ+} (hqr : q ≤ r) (H : 1 < sumInv {2, q, r}) : q < 4 := by have h4 : (0 : ℚ) < 4 := by norm_num contrapose! H rw [sumInv_pqr] have h4r := H.trans hqr have hq : (q : ℚ)⁻¹ ≤ 4⁻¹ := by rw [inv_le_inv₀ _ h4] · assumption_mod_cast · norm_num have hr : (r : ℚ)⁻¹ ≤ 4⁻¹ := by rw [inv_le_inv₀ _ h4] · assumption_mod_cast · norm_num calc (2⁻¹ + (q : ℚ)⁻¹ + (r : ℚ)⁻¹) ≤ 2⁻¹ + 4⁻¹ + 4⁻¹ := add_le_add (add_le_add le_rfl hq) hr _ = 1 := by norm_num theorem lt_six {r : ℕ+} (H : 1 < sumInv {2, 3, r}) : r < 6 := by have h6 : (0 : ℚ) < 6 := by norm_num contrapose! H rw [sumInv_pqr] have hr : (r : ℚ)⁻¹ ≤ 6⁻¹ := by rw [inv_le_inv₀ _ h6] · assumption_mod_cast · norm_num calc
(2⁻¹ + 3⁻¹ + (r : ℚ)⁻¹ : ℚ) ≤ 2⁻¹ + 3⁻¹ + 6⁻¹ := add_le_add (add_le_add le_rfl le_rfl) hr _ = 1 := by norm_num theorem admissible_of_one_lt_sumInv_aux' {p q r : ℕ+} (hpq : p ≤ q) (hqr : q ≤ r) (H : 1 < sumInv {p, q, r}) : Admissible {p, q, r} := by have hp3 : p < 3 := lt_three hpq hqr H -- Porting note: `interval_cases` doesn't support `ℕ+` yet. replace hp3 := Finset.mem_Iio.mpr hp3 conv at hp3 => change p ∈ ({1, 2} : Multiset ℕ+) fin_cases hp3 · exact admissible_A' q r have hq4 : q < 4 := lt_four hqr H replace hq4 := Finset.mem_Ico.mpr ⟨hpq, hq4⟩; clear hpq conv at hq4 => change q ∈ ({2, 3} : Multiset ℕ+) fin_cases hq4 · exact admissible_D' r
Mathlib/NumberTheory/ADEInequality.lean
198
213
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Patrick Massot, Casper Putz, Anne Baanen -/ import Mathlib.LinearAlgebra.Basis.Submodule import Mathlib.LinearAlgebra.Matrix.Reindex import Mathlib.LinearAlgebra.Matrix.ToLin /-! # Bases and matrices This file defines the map `Basis.toMatrix` that sends a family of vectors to the matrix of their coordinates with respect to some basis. ## Main definitions * `Basis.toMatrix e v` is the matrix whose `i, j`th entry is `e.repr (v j) i` * `basis.toMatrixEquiv` is `Basis.toMatrix` bundled as a linear equiv ## Main results * `LinearMap.toMatrix_id_eq_basis_toMatrix`: `LinearMap.toMatrix b c id` is equal to `Basis.toMatrix b c` * `Basis.toMatrix_mul_toMatrix`: multiplying `Basis.toMatrix` with another `Basis.toMatrix` gives a `Basis.toMatrix` ## Tags matrix, basis -/ noncomputable section open LinearMap Matrix Set Submodule open Matrix section BasisToMatrix variable {ι ι' κ κ' : Type*} variable {R M : Type*} [CommSemiring R] [AddCommMonoid M] [Module R M] variable {R₂ M₂ : Type*} [CommRing R₂] [AddCommGroup M₂] [Module R₂ M₂] open Function Matrix /-- From a basis `e : ι → M` and a family of vectors `v : ι' → M`, make the matrix whose columns are the vectors `v i` written in the basis `e`. -/ def Basis.toMatrix (e : Basis ι R M) (v : ι' → M) : Matrix ι ι' R := fun i j => e.repr (v j) i variable (e : Basis ι R M) (v : ι' → M) (i : ι) (j : ι') namespace Basis theorem toMatrix_apply : e.toMatrix v i j = e.repr (v j) i := rfl theorem toMatrix_transpose_apply : (e.toMatrix v)ᵀ j = e.repr (v j) := funext fun _ => rfl theorem toMatrix_eq_toMatrix_constr [Fintype ι] [DecidableEq ι] (v : ι → M) : e.toMatrix v = LinearMap.toMatrix e e (e.constr ℕ v) := by ext rw [Basis.toMatrix_apply, LinearMap.toMatrix_apply, Basis.constr_basis] -- TODO (maybe) Adjust the definition of `Basis.toMatrix` to eliminate the transpose. theorem coePiBasisFun.toMatrix_eq_transpose [Finite ι] : ((Pi.basisFun R ι).toMatrix : Matrix ι ι R → Matrix ι ι R) = Matrix.transpose := by ext M i j rfl @[simp] theorem toMatrix_self [DecidableEq ι] : e.toMatrix e = 1 := by unfold Basis.toMatrix ext i j simp [Basis.equivFun, Matrix.one_apply, Finsupp.single_apply, eq_comm] theorem toMatrix_update [DecidableEq ι'] (x : M) : e.toMatrix (Function.update v j x) = Matrix.updateCol (e.toMatrix v) j (e.repr x) := by ext i' k rw [Basis.toMatrix, Matrix.updateCol_apply, e.toMatrix_apply] split_ifs with h · rw [h, update_self j x v] · rw [update_of_ne h]
/-- The basis constructed by `unitsSMul` has vectors given by a diagonal matrix. -/ @[simp] theorem toMatrix_unitsSMul [DecidableEq ι] (e : Basis ι R₂ M₂) (w : ι → R₂ˣ) : e.toMatrix (e.unitsSMul w) = diagonal ((↑) ∘ w) := by ext i j by_cases h : i = j
Mathlib/LinearAlgebra/Matrix/Basis.lean
86
92
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Alex Kontorovich, Heather Macbeth -/ import Mathlib.MeasureTheory.Group.Action import Mathlib.MeasureTheory.Group.Pointwise import Mathlib.MeasureTheory.Integral.Lebesgue.Map import Mathlib.MeasureTheory.Integral.Bochner.Set /-! # Fundamental domain of a group action A set `s` is said to be a *fundamental domain* of an action of a group `G` on a measurable space `α` with respect to a measure `μ` if * `s` is a measurable set; * the sets `g • s` over all `g : G` cover almost all points of the whole space; * the sets `g • s`, are pairwise a.e. disjoint, i.e., `μ (g₁ • s ∩ g₂ • s) = 0` whenever `g₁ ≠ g₂`; we require this for `g₂ = 1` in the definition, then deduce it for any two `g₁ ≠ g₂`. In this file we prove that in case of a countable group `G` and a measure preserving action, any two fundamental domains have the same measure, and for a `G`-invariant function, its integrals over any two fundamental domains are equal to each other. We also generate additive versions of all theorems in this file using the `to_additive` attribute. * We define the `HasFundamentalDomain` typeclass, in particular to be able to define the `covolume` of a quotient of `α` by a group `G`, which under reasonable conditions does not depend on the choice of fundamental domain. * We define the `QuotientMeasureEqMeasurePreimage` typeclass to describe a situation in which a measure `μ` on `α ⧸ G` can be computed by taking a measure `ν` on `α` of the intersection of the pullback with a fundamental domain. ## Main declarations * `MeasureTheory.IsFundamentalDomain`: Predicate for a set to be a fundamental domain of the action of a group * `MeasureTheory.fundamentalFrontier`: Fundamental frontier of a set under the action of a group. Elements of `s` that belong to some other translate of `s`. * `MeasureTheory.fundamentalInterior`: Fundamental interior of a set under the action of a group. Elements of `s` that do not belong to any other translate of `s`. -/ open scoped ENNReal Pointwise Topology NNReal ENNReal MeasureTheory open MeasureTheory MeasureTheory.Measure Set Function TopologicalSpace Filter namespace MeasureTheory /-- A measurable set `s` is a *fundamental domain* for an additive action of an additive group `G` on a measurable space `α` with respect to a measure `α` if the sets `g +ᵥ s`, `g : G`, are pairwise a.e. disjoint and cover the whole space. -/ structure IsAddFundamentalDomain (G : Type*) {α : Type*} [Zero G] [VAdd G α] [MeasurableSpace α] (s : Set α) (μ : Measure α := by volume_tac) : Prop where protected nullMeasurableSet : NullMeasurableSet s μ protected ae_covers : ∀ᵐ x ∂μ, ∃ g : G, g +ᵥ x ∈ s protected aedisjoint : Pairwise <| (AEDisjoint μ on fun g : G => g +ᵥ s) /-- A measurable set `s` is a *fundamental domain* for an action of a group `G` on a measurable space `α` with respect to a measure `α` if the sets `g • s`, `g : G`, are pairwise a.e. disjoint and cover the whole space. -/ @[to_additive IsAddFundamentalDomain] structure IsFundamentalDomain (G : Type*) {α : Type*} [One G] [SMul G α] [MeasurableSpace α] (s : Set α) (μ : Measure α := by volume_tac) : Prop where protected nullMeasurableSet : NullMeasurableSet s μ protected ae_covers : ∀ᵐ x ∂μ, ∃ g : G, g • x ∈ s protected aedisjoint : Pairwise <| (AEDisjoint μ on fun g : G => g • s) variable {G H α β E : Type*} namespace IsFundamentalDomain variable [Group G] [Group H] [MulAction G α] [MeasurableSpace α] [MulAction H β] [MeasurableSpace β] [NormedAddCommGroup E] {s t : Set α} {μ : Measure α} /-- If for each `x : α`, exactly one of `g • x`, `g : G`, belongs to a measurable set `s`, then `s` is a fundamental domain for the action of `G` on `α`. -/ @[to_additive "If for each `x : α`, exactly one of `g +ᵥ x`, `g : G`, belongs to a measurable set `s`, then `s` is a fundamental domain for the additive action of `G` on `α`."] theorem mk' (h_meas : NullMeasurableSet s μ) (h_exists : ∀ x : α, ∃! g : G, g • x ∈ s) : IsFundamentalDomain G s μ where nullMeasurableSet := h_meas
ae_covers := Eventually.of_forall fun x => (h_exists x).exists aedisjoint a b hab := Disjoint.aedisjoint <| disjoint_left.2 fun x hxa hxb => by rw [mem_smul_set_iff_inv_smul_mem] at hxa hxb exact hab (inv_injective <| (h_exists x).unique hxa hxb) /-- For `s` to be a fundamental domain, it's enough to check `MeasureTheory.AEDisjoint (g • s) s` for `g ≠ 1`. -/
Mathlib/MeasureTheory/Group/FundamentalDomain.lean
88
94
/- Copyright (c) 2023 Frédéric Dupuis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Frédéric Dupuis -/ import Mathlib.Computability.AkraBazzi.GrowsPolynomially import Mathlib.Analysis.Calculus.Deriv.Inv import Mathlib.Analysis.SpecialFunctions.Pow.Deriv /-! # Divide-and-conquer recurrences and the Akra-Bazzi theorem A divide-and-conquer recurrence is a function `T : ℕ → ℝ` that satisfies a recurrence relation of the form `T(n) = ∑_{i=0}^{k-1} a_i T(r_i(n)) + g(n)` for large enough `n`, where `r_i(n)` is some function where `‖r_i(n) - b_i n‖ ∈ o(n / (log n)^2)` for every `i`, the `a_i`'s are some positive coefficients, and the `b_i`'s are reals `∈ (0,1)`. (Note that this can be improved to `O(n / (log n)^(1+ε))`, this is left as future work.) These recurrences arise mainly in the analysis of divide-and-conquer algorithms such as mergesort or Strassen's algorithm for matrix multiplication. This class of algorithms works by dividing an instance of the problem of size `n`, into `k` smaller instances, where the `i`'th instance is of size roughly `b_i n`, and calling itself recursively on those smaller instances. `T(n)` then represents the running time of the algorithm, and `g(n)` represents the running time required to actually divide up the instance and process the answers that come out of the recursive calls. Since virtually all such algorithms produce instances that are only approximately of size `b_i n` (they have to round up or down at the very least), we allow the instance sizes to be given by some function `r_i(n)` that approximates `b_i n`. The Akra-Bazzi theorem gives the asymptotic order of such a recurrence: it states that `T(n) ∈ Θ(n^p (1 + ∑_{u=0}^{n-1} g(n) / u^{p+1}))`, where `p` is the unique real number such that `∑ a_i b_i^p = 1`. ## Main definitions and results * `AkraBazziRecurrence T g a b r`: the predicate stating that `T : ℕ → ℝ` satisfies an Akra-Bazzi recurrence with parameters `g`, `a`, `b` and `r` as above. * `GrowsPolynomially`: The growth condition that `g` must satisfy for the theorem to apply. It roughly states that `c₁ g(n) ≤ g(u) ≤ c₂ g(n)`, for u between b*n and n for any constant `b ∈ (0,1)`. * `sumTransform`: The transformation which turns a function `g` into `n^p * ∑ u ∈ Finset.Ico n₀ n, g u / u^(p+1)`. * `asympBound`: The asymptotic bound satisfied by an Akra-Bazzi recurrence, namely `n^p (1 + ∑ g(u) / u^(p+1))` * `isTheta_asympBound`: The main result stating that `T(n) ∈ Θ(n^p (1 + ∑_{u=0}^{n-1} g(n) / u^{p+1}))` ## Implementation Note that the original version of the theorem has an integral rather than a sum in the above expression, and first considers the `T : ℝ → ℝ` case before moving on to `ℕ → ℝ`. We prove the above version with a sum, as it is simpler and more relevant for algorithms. ## TODO * Specialize this theorem to the very common case where the recurrence is of the form `T(n) = ℓT(r_i(n)) + g(n)` where `g(n) ∈ Θ(n^t)` for some `t`. (This is often called the "master theorem" in the literature.) * Add the original version of the theorem with an integral instead of a sum. ## References * Mohamad Akra and Louay Bazzi, On the solution of linear recurrence equations * Tom Leighton, Notes on better master theorems for divide-and-conquer recurrences * Manuel Eberl, Asymptotic reasoning in a proof assistant -/ open Finset Real Filter Asymptotics open scoped Topology /-! #### Definition of Akra-Bazzi recurrences This section defines the predicate `AkraBazziRecurrence T g a b r` which states that `T` satisfies the recurrence `T(n) = ∑_{i=0}^{k-1} a_i T(r_i(n)) + g(n)` with appropriate conditions on the various parameters. -/ /-- An Akra-Bazzi recurrence is a function that satisfies the recurrence `T n = (∑ i, a i * T (r i n)) + g n`. -/ structure AkraBazziRecurrence {α : Type*} [Fintype α] [Nonempty α] (T : ℕ → ℝ) (g : ℝ → ℝ) (a : α → ℝ) (b : α → ℝ) (r : α → ℕ → ℕ) where /-- Point below which the recurrence is in the base case -/ n₀ : ℕ /-- `n₀` is always `> 0` -/ n₀_gt_zero : 0 < n₀ /-- The `a`'s are nonzero -/ a_pos : ∀ i, 0 < a i /-- The `b`'s are nonzero -/ b_pos : ∀ i, 0 < b i /-- The b's are less than 1 -/ b_lt_one : ∀ i, b i < 1 /-- `g` is nonnegative -/ g_nonneg : ∀ x ≥ 0, 0 ≤ g x /-- `g` grows polynomially -/ g_grows_poly : AkraBazziRecurrence.GrowsPolynomially g /-- The actual recurrence -/ h_rec (n : ℕ) (hn₀ : n₀ ≤ n) : T n = (∑ i, a i * T (r i n)) + g n /-- Base case: `T(n) > 0` whenever `n < n₀` -/ T_gt_zero' (n : ℕ) (hn : n < n₀) : 0 < T n /-- The `r`'s always reduce `n` -/ r_lt_n : ∀ i n, n₀ ≤ n → r i n < n /-- The `r`'s approximate the `b`'s -/ dist_r_b : ∀ i, (fun n => (r i n : ℝ) - b i * n) =o[atTop] fun n => n / (log n) ^ 2 namespace AkraBazziRecurrence section min_max variable {α : Type*} [Finite α] [Nonempty α] /-- Smallest `b i` -/ noncomputable def min_bi (b : α → ℝ) : α := Classical.choose <| Finite.exists_min b /-- Largest `b i` -/ noncomputable def max_bi (b : α → ℝ) : α := Classical.choose <| Finite.exists_max b @[aesop safe apply] lemma min_bi_le {b : α → ℝ} (i : α) : b (min_bi b) ≤ b i := Classical.choose_spec (Finite.exists_min b) i @[aesop safe apply] lemma max_bi_le {b : α → ℝ} (i : α) : b i ≤ b (max_bi b) := Classical.choose_spec (Finite.exists_max b) i end min_max lemma isLittleO_self_div_log_id : (fun (n : ℕ) => n / log n ^ 2) =o[atTop] (fun (n : ℕ) => (n : ℝ)) := by calc (fun (n : ℕ) => (n : ℝ) / log n ^ 2) = fun (n : ℕ) => (n : ℝ) * ((log n) ^ 2)⁻¹ := by simp_rw [div_eq_mul_inv] _ =o[atTop] fun (n : ℕ) => (n : ℝ) * 1⁻¹ := by refine IsBigO.mul_isLittleO (isBigO_refl _ _) ?_ refine IsLittleO.inv_rev ?main ?zero case zero => simp case main => calc _ = (fun (_ : ℕ) => ((1 : ℝ) ^ 2)) := by simp _ =o[atTop] (fun (n : ℕ) => (log n)^2) := IsLittleO.pow (IsLittleO.natCast_atTop <| isLittleO_const_log_atTop) (by norm_num) _ = (fun (n : ℕ) => (n : ℝ)) := by ext; simp variable {α : Type*} [Fintype α] {T : ℕ → ℝ} {g : ℝ → ℝ} {a b : α → ℝ} {r : α → ℕ → ℕ} variable [Nonempty α] (R : AkraBazziRecurrence T g a b r) section include R lemma dist_r_b' : ∀ᶠ n in atTop, ∀ i, ‖(r i n : ℝ) - b i * n‖ ≤ n / log n ^ 2 := by rw [Filter.eventually_all] intro i simpa using IsLittleO.eventuallyLE (R.dist_r_b i) lemma eventually_b_le_r : ∀ᶠ (n : ℕ) in atTop, ∀ i, (b i : ℝ) * n - (n / log n ^ 2) ≤ r i n := by filter_upwards [R.dist_r_b'] with n hn intro i have h₁ : 0 ≤ b i := le_of_lt <| R.b_pos _ rw [sub_le_iff_le_add, add_comm, ← sub_le_iff_le_add] calc (b i : ℝ) * n - r i n = ‖b i * n‖ - ‖(r i n : ℝ)‖ := by simp only [norm_mul, RCLike.norm_natCast, sub_left_inj, Nat.cast_eq_zero, Real.norm_of_nonneg h₁] _ ≤ ‖(b i * n : ℝ) - r i n‖ := norm_sub_norm_le _ _ _ = ‖(r i n : ℝ) - b i * n‖ := norm_sub_rev _ _ _ ≤ n / log n ^ 2 := hn i lemma eventually_r_le_b : ∀ᶠ (n : ℕ) in atTop, ∀ i, r i n ≤ (b i : ℝ) * n + (n / log n ^ 2) := by filter_upwards [R.dist_r_b'] with n hn intro i calc r i n = b i * n + (r i n - b i * n) := by ring _ ≤ b i * n + ‖r i n - b i * n‖ := by gcongr; exact Real.le_norm_self _ _ ≤ b i * n + n / log n ^ 2 := by gcongr; exact hn i lemma eventually_r_lt_n : ∀ᶠ (n : ℕ) in atTop, ∀ i, r i n < n := by filter_upwards [eventually_ge_atTop R.n₀] with n hn exact fun i => R.r_lt_n i n hn lemma eventually_bi_mul_le_r : ∀ᶠ (n : ℕ) in atTop, ∀ i, (b (min_bi b) / 2) * n ≤ r i n := by have gt_zero : 0 < b (min_bi b) := R.b_pos (min_bi b) have hlo := isLittleO_self_div_log_id rw [Asymptotics.isLittleO_iff] at hlo have hlo' := hlo (by positivity : 0 < b (min_bi b) / 2) filter_upwards [hlo', R.eventually_b_le_r] with n hn hn' intro i simp only [Real.norm_of_nonneg (by positivity : 0 ≤ (n : ℝ))] at hn calc b (min_bi b) / 2 * n = b (min_bi b) * n - b (min_bi b) / 2 * n := by ring _ ≤ b (min_bi b) * n - ‖n / log n ^ 2‖ := by gcongr _ ≤ b i * n - ‖n / log n ^ 2‖ := by gcongr; aesop _ = b i * n - n / log n ^ 2 := by congr exact Real.norm_of_nonneg <| by positivity _ ≤ r i n := hn' i lemma bi_min_div_two_lt_one : b (min_bi b) / 2 < 1 := by have gt_zero : 0 < b (min_bi b) := R.b_pos (min_bi b) calc b (min_bi b) / 2 < b (min_bi b) := by aesop (add safe apply div_two_lt_of_pos) _ < 1 := R.b_lt_one _ lemma bi_min_div_two_pos : 0 < b (min_bi b) / 2 := div_pos (R.b_pos _) (by norm_num) lemma exists_eventually_const_mul_le_r : ∃ c ∈ Set.Ioo (0 : ℝ) 1, ∀ᶠ (n : ℕ) in atTop, ∀ i, c * n ≤ r i n := by have gt_zero : 0 < b (min_bi b) := R.b_pos (min_bi b) exact ⟨b (min_bi b) / 2, ⟨⟨by positivity, R.bi_min_div_two_lt_one⟩, R.eventually_bi_mul_le_r⟩⟩ lemma eventually_r_ge (C : ℝ) : ∀ᶠ (n : ℕ) in atTop, ∀ i, C ≤ r i n := by obtain ⟨c, hc_mem, hc⟩ := R.exists_eventually_const_mul_le_r filter_upwards [eventually_ge_atTop ⌈C / c⌉₊, hc] with n hn₁ hn₂ have h₁ := hc_mem.1 intro i calc C = c * (C / c) := by rw [← mul_div_assoc] exact (mul_div_cancel_left₀ _ (by positivity)).symm _ ≤ c * ⌈C / c⌉₊ := by gcongr; simp [Nat.le_ceil] _ ≤ c * n := by gcongr _ ≤ r i n := hn₂ i lemma tendsto_atTop_r (i : α) : Tendsto (r i) atTop atTop := by rw [tendsto_atTop] intro b have := R.eventually_r_ge b rw [Filter.eventually_all] at this exact_mod_cast this i lemma tendsto_atTop_r_real (i : α) : Tendsto (fun n => (r i n : ℝ)) atTop atTop := Tendsto.comp tendsto_natCast_atTop_atTop (R.tendsto_atTop_r i) lemma exists_eventually_r_le_const_mul : ∃ c ∈ Set.Ioo (0 : ℝ) 1, ∀ᶠ (n : ℕ) in atTop, ∀ i, r i n ≤ c * n := by let c := b (max_bi b) + (1 - b (max_bi b)) / 2 have h_max_bi_pos : 0 < b (max_bi b) := R.b_pos _ have h_max_bi_lt_one : 0 < 1 - b (max_bi b) := by have : b (max_bi b) < 1 := R.b_lt_one _ linarith have hc_pos : 0 < c := by positivity have h₁ : 0 < (1 - b (max_bi b)) / 2 := by positivity have hc_lt_one : c < 1 := calc b (max_bi b) + (1 - b (max_bi b)) / 2 = b (max_bi b) * (1 / 2) + 1 / 2 := by ring _ < 1 * (1 / 2) + 1 / 2 := by gcongr exact R.b_lt_one _ _ = 1 := by norm_num refine ⟨c, ⟨hc_pos, hc_lt_one⟩, ?_⟩ have hlo := isLittleO_self_div_log_id rw [Asymptotics.isLittleO_iff] at hlo have hlo' := hlo h₁ filter_upwards [hlo', R.eventually_r_le_b] with n hn hn' intro i rw [Real.norm_of_nonneg (by positivity)] at hn simp only [Real.norm_of_nonneg (by positivity : 0 ≤ (n : ℝ))] at hn calc r i n ≤ b i * n + n / log n ^ 2 := by exact hn' i _ ≤ b i * n + (1 - b (max_bi b)) / 2 * n := by gcongr _ = (b i + (1 - b (max_bi b)) / 2) * n := by ring _ ≤ (b (max_bi b) + (1 - b (max_bi b)) / 2) * n := by gcongr; exact max_bi_le _ lemma eventually_r_pos : ∀ᶠ (n : ℕ) in atTop, ∀ i, 0 < r i n := by rw [Filter.eventually_all] exact fun i => (R.tendsto_atTop_r i).eventually_gt_atTop 0 lemma eventually_log_b_mul_pos : ∀ᶠ (n : ℕ) in atTop, ∀ i, 0 < log (b i * n) := by rw [Filter.eventually_all] intro i have h : Tendsto (fun (n : ℕ) => log (b i * n)) atTop atTop := Tendsto.comp tendsto_log_atTop <| Tendsto.const_mul_atTop (b_pos R i) tendsto_natCast_atTop_atTop exact h.eventually_gt_atTop 0 @[aesop safe apply] lemma T_pos (n : ℕ) : 0 < T n := by induction n using Nat.strongRecOn with | ind n h_ind => cases lt_or_le n R.n₀ with | inl hn => exact R.T_gt_zero' n hn -- n < R.n₀ | inr hn => -- R.n₀ ≤ n rw [R.h_rec n hn] have := R.g_nonneg refine add_pos_of_pos_of_nonneg (Finset.sum_pos ?sum_elems univ_nonempty) (by aesop) exact fun i _ => mul_pos (R.a_pos i) <| h_ind _ (R.r_lt_n i _ hn) @[aesop safe apply] lemma T_nonneg (n : ℕ) : 0 ≤ T n := le_of_lt <| R.T_pos n end /-! #### Smoothing function We define `ε` as the "smoothing function" `fun n => 1 / log n`, which will be used in the form of a factor of `1 ± ε n` needed to make the induction step go through. This is its own definition to make it easier to switch to a different smoothing function. For example, choosing `1 / log n ^ δ` for a suitable choice of `δ` leads to a slightly tighter theorem at the price of a more complicated proof. This part of the file then proves several properties of this function that will be needed later in the proof. -/ /-- The "smoothing function" is defined as `1 / log n`. This is defined as an `ℝ → ℝ` function as opposed to `ℕ → ℝ` since this is more convenient for the proof, where we need to e.g. take derivatives. -/ noncomputable def smoothingFn (n : ℝ) : ℝ := 1 / log n local notation "ε" => smoothingFn lemma one_add_smoothingFn_le_two {x : ℝ} (hx : exp 1 ≤ x) : 1 + ε x ≤ 2 := by simp only [smoothingFn, ← one_add_one_eq_two] gcongr have : 1 < x := by calc 1 = exp 0 := by simp _ < exp 1 := by simp _ ≤ x := hx rw [div_le_one (log_pos this)] calc 1 = log (exp 1) := by simp _ ≤ log x := log_le_log (exp_pos _) hx lemma isLittleO_smoothingFn_one : ε =o[atTop] (fun _ => (1 : ℝ)) := by unfold smoothingFn refine isLittleO_of_tendsto (fun _ h => False.elim <| one_ne_zero h) ?_ simp only [one_div, div_one] exact Tendsto.inv_tendsto_atTop Real.tendsto_log_atTop lemma isEquivalent_one_add_smoothingFn_one : (fun x => 1 + ε x) ~[atTop] (fun _ => (1 : ℝ)) := IsEquivalent.add_isLittleO IsEquivalent.refl isLittleO_smoothingFn_one lemma isEquivalent_one_sub_smoothingFn_one : (fun x => 1 - ε x) ~[atTop] (fun _ => (1 : ℝ)) := IsEquivalent.sub_isLittleO IsEquivalent.refl isLittleO_smoothingFn_one lemma growsPolynomially_one_sub_smoothingFn : GrowsPolynomially fun x => 1 - ε x := GrowsPolynomially.of_isEquivalent_const isEquivalent_one_sub_smoothingFn_one lemma growsPolynomially_one_add_smoothingFn : GrowsPolynomially fun x => 1 + ε x := GrowsPolynomially.of_isEquivalent_const isEquivalent_one_add_smoothingFn_one lemma eventually_one_sub_smoothingFn_gt_const_real (c : ℝ) (hc : c < 1) : ∀ᶠ (x : ℝ) in atTop, c < 1 - ε x := by have h₁ : Tendsto (fun x => 1 - ε x) atTop (𝓝 1) := by rw [← isEquivalent_const_iff_tendsto one_ne_zero] exact isEquivalent_one_sub_smoothingFn_one rw [tendsto_order] at h₁ exact h₁.1 c hc lemma eventually_one_sub_smoothingFn_gt_const (c : ℝ) (hc : c < 1) : ∀ᶠ (n : ℕ) in atTop, c < 1 - ε n := Eventually.natCast_atTop (p := fun n => c < 1 - ε n) <| eventually_one_sub_smoothingFn_gt_const_real c hc lemma eventually_one_sub_smoothingFn_pos_real : ∀ᶠ (x : ℝ) in atTop, 0 < 1 - ε x := eventually_one_sub_smoothingFn_gt_const_real 0 zero_lt_one lemma eventually_one_sub_smoothingFn_pos : ∀ᶠ (n : ℕ) in atTop, 0 < 1 - ε n := (eventually_one_sub_smoothingFn_pos_real).natCast_atTop lemma eventually_one_sub_smoothingFn_nonneg : ∀ᶠ (n : ℕ) in atTop, 0 ≤ 1 - ε n := by filter_upwards [eventually_one_sub_smoothingFn_pos] with n hn; exact le_of_lt hn include R in lemma eventually_one_sub_smoothingFn_r_pos : ∀ᶠ (n : ℕ) in atTop, ∀ i, 0 < 1 - ε (r i n) := by rw [Filter.eventually_all] exact fun i => (R.tendsto_atTop_r_real i).eventually eventually_one_sub_smoothingFn_pos_real @[aesop safe apply] lemma differentiableAt_smoothingFn {x : ℝ} (hx : 1 < x) : DifferentiableAt ℝ ε x := by have : log x ≠ 0 := Real.log_ne_zero_of_pos_of_ne_one (by positivity) (ne_of_gt hx) show DifferentiableAt ℝ (fun z => 1 / log z) x simp_rw [one_div] exact DifferentiableAt.inv (differentiableAt_log (by positivity)) this @[aesop safe apply] lemma differentiableAt_one_sub_smoothingFn {x : ℝ} (hx : 1 < x) : DifferentiableAt ℝ (fun z => 1 - ε z) x := DifferentiableAt.sub (differentiableAt_const _) <| differentiableAt_smoothingFn hx lemma differentiableOn_one_sub_smoothingFn : DifferentiableOn ℝ (fun z => 1 - ε z) (Set.Ioi 1) := fun _ hx => (differentiableAt_one_sub_smoothingFn hx).differentiableWithinAt @[aesop safe apply] lemma differentiableAt_one_add_smoothingFn {x : ℝ} (hx : 1 < x) : DifferentiableAt ℝ (fun z => 1 + ε z) x := DifferentiableAt.add (differentiableAt_const _) <| differentiableAt_smoothingFn hx lemma differentiableOn_one_add_smoothingFn : DifferentiableOn ℝ (fun z => 1 + ε z) (Set.Ioi 1) := fun _ hx => (differentiableAt_one_add_smoothingFn hx).differentiableWithinAt lemma deriv_smoothingFn {x : ℝ} (hx : 1 < x) : deriv ε x = -x⁻¹ / (log x ^ 2) := by have : log x ≠ 0 := Real.log_ne_zero_of_pos_of_ne_one (by positivity) (ne_of_gt hx) show deriv (fun z => 1 / log z) x = -x⁻¹ / (log x ^ 2) rw [deriv_div] <;> aesop lemma isLittleO_deriv_smoothingFn : deriv ε =o[atTop] fun x => x⁻¹ := calc deriv ε =ᶠ[atTop] fun x => -x⁻¹ / (log x ^ 2) := by filter_upwards [eventually_gt_atTop 1] with x hx rw [deriv_smoothingFn hx] _ = fun x => (-x * log x ^ 2)⁻¹ := by simp_rw [neg_div, div_eq_mul_inv, ← mul_inv, neg_inv, neg_mul] _ =o[atTop] fun x => (x * 1)⁻¹ := by refine IsLittleO.inv_rev ?_ ?_ · refine IsBigO.mul_isLittleO (by rw [isBigO_neg_right]; aesop (add safe isBigO_refl)) ?_ rw [isLittleO_one_left_iff] exact Tendsto.comp tendsto_norm_atTop_atTop <| Tendsto.comp (tendsto_pow_atTop (by norm_num)) tendsto_log_atTop · exact Filter.Eventually.of_forall (fun x hx => by rw [mul_one] at hx; simp [hx]) _ = fun x => x⁻¹ := by simp lemma eventually_deriv_one_sub_smoothingFn : deriv (fun x => 1 - ε x) =ᶠ[atTop] fun x => x⁻¹ / (log x ^ 2) := calc deriv (fun x => 1 - ε x) =ᶠ[atTop] -(deriv ε) := by filter_upwards [eventually_gt_atTop 1] with x hx; rw [deriv_sub] <;> aesop _ =ᶠ[atTop] fun x => x⁻¹ / (log x ^ 2) := by filter_upwards [eventually_gt_atTop 1] with x hx simp [deriv_smoothingFn hx, neg_div] lemma eventually_deriv_one_add_smoothingFn : deriv (fun x => 1 + ε x) =ᶠ[atTop] fun x => -x⁻¹ / (log x ^ 2) := calc deriv (fun x => 1 + ε x) =ᶠ[atTop] deriv ε := by filter_upwards [eventually_gt_atTop 1] with x hx; rw [deriv_add] <;> aesop _ =ᶠ[atTop] fun x => -x⁻¹ / (log x ^ 2) := by filter_upwards [eventually_gt_atTop 1] with x hx simp [deriv_smoothingFn hx]
lemma isLittleO_deriv_one_sub_smoothingFn : deriv (fun x => 1 - ε x) =o[atTop] fun (x : ℝ) => x⁻¹ := calc deriv (fun x => 1 - ε x) =ᶠ[atTop] fun z => -(deriv ε z) := by filter_upwards [eventually_gt_atTop 1] with x hx; rw [deriv_sub] <;> aesop _ =o[atTop] fun x => x⁻¹ := by rw [isLittleO_neg_left]; exact isLittleO_deriv_smoothingFn
Mathlib/Computability/AkraBazzi/AkraBazzi.lean
421
425
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Patrick Massot, Sébastien Gouëzel -/ import Mathlib.MeasureTheory.Integral.IntervalIntegral.Basic import Mathlib.MeasureTheory.Integral.IntervalIntegral.FundThmCalculus import Mathlib.MeasureTheory.Integral.IntervalIntegral.IntegrationByParts deprecated_module (since := "2025-04-13")
Mathlib/MeasureTheory/Integral/IntervalIntegral.lean
1,004
1,008
/- Copyright (c) 2014 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis, Leonardo de Moura, Johannes Hölzl, Mario Carneiro -/ import Mathlib.Algebra.Field.Defs import Mathlib.Algebra.Ring.Commute import Mathlib.Algebra.Ring.Invertible import Mathlib.Order.Synonym /-! # Lemmas about division (semi)rings and (semi)fields -/ open Function OrderDual Set universe u variable {K L : Type*} section DivisionSemiring variable [DivisionSemiring K] {a b c d : K} theorem add_div (a b c : K) : (a + b) / c = a / c + b / c := by simp_rw [div_eq_mul_inv, add_mul] @[field_simps] theorem div_add_div_same (a b c : K) : a / c + b / c = (a + b) / c := (add_div _ _ _).symm theorem same_add_div (h : b ≠ 0) : (b + a) / b = 1 + a / b := by rw [← div_self h, add_div] theorem div_add_same (h : b ≠ 0) : (a + b) / b = a / b + 1 := by rw [← div_self h, add_div] theorem one_add_div (h : b ≠ 0) : 1 + a / b = (b + a) / b := (same_add_div h).symm theorem div_add_one (h : b ≠ 0) : a / b + 1 = (a + b) / b := (div_add_same h).symm /-- See `inv_add_inv` for the more convenient version when `K` is commutative. -/ theorem inv_add_inv' (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ + b⁻¹ = a⁻¹ * (a + b) * b⁻¹ := let _ := invertibleOfNonzero ha; let _ := invertibleOfNonzero hb; invOf_add_invOf a b theorem one_div_mul_add_mul_one_div_eq_one_div_add_one_div (ha : a ≠ 0) (hb : b ≠ 0) : 1 / a * (a + b) * (1 / b) = 1 / a + 1 / b := by simpa only [one_div] using (inv_add_inv' ha hb).symm theorem add_div_eq_mul_add_div (a b : K) (hc : c ≠ 0) : a + b / c = (a * c + b) / c := (eq_div_iff_mul_eq hc).2 <| by rw [right_distrib, div_mul_cancel₀ _ hc] @[field_simps] theorem add_div' (a b c : K) (hc : c ≠ 0) : b + a / c = (b * c + a) / c := by rw [add_div, mul_div_cancel_right₀ _ hc] @[field_simps] theorem div_add' (a b c : K) (hc : c ≠ 0) : a / c + b = (a + b * c) / c := by rwa [add_comm, add_div', add_comm] protected theorem Commute.div_add_div (hbc : Commute b c) (hbd : Commute b d) (hb : b ≠ 0) (hd : d ≠ 0) : a / b + c / d = (a * d + b * c) / (b * d) := by rw [add_div, mul_div_mul_right _ b hd, hbc.eq, hbd.eq, mul_div_mul_right c d hb] protected theorem Commute.one_div_add_one_div (hab : Commute a b) (ha : a ≠ 0) (hb : b ≠ 0) : 1 / a + 1 / b = (a + b) / (a * b) := by rw [(Commute.one_right a).div_add_div hab ha hb, one_mul, mul_one, add_comm] protected theorem Commute.inv_add_inv (hab : Commute a b) (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ + b⁻¹ = (a + b) / (a * b) := by rw [inv_eq_one_div, inv_eq_one_div, hab.one_div_add_one_div ha hb] variable [NeZero (2 : K)] @[simp] lemma add_self_div_two (a : K) : (a + a) / 2 = a := by rw [← mul_two, mul_div_cancel_right₀ a two_ne_zero] @[simp] lemma add_halves (a : K) : a / 2 + a / 2 = a := by rw [← add_div, add_self_div_two] end DivisionSemiring section DivisionRing variable [DivisionRing K] {a b c d : K} @[simp] theorem div_neg_self {a : K} (h : a ≠ 0) : a / -a = -1 := by rw [div_neg_eq_neg_div, div_self h] @[simp] theorem neg_div_self {a : K} (h : a ≠ 0) : -a / a = -1 := by rw [neg_div, div_self h] theorem div_sub_div_same (a b c : K) : a / c - b / c = (a - b) / c := by rw [sub_eq_add_neg, ← neg_div, div_add_div_same, sub_eq_add_neg] theorem same_sub_div {a b : K} (h : b ≠ 0) : (b - a) / b = 1 - a / b := by simpa only [← @div_self _ _ b h] using (div_sub_div_same b a b).symm theorem one_sub_div {a b : K} (h : b ≠ 0) : 1 - a / b = (b - a) / b := (same_sub_div h).symm theorem div_sub_same {a b : K} (h : b ≠ 0) : (a - b) / b = a / b - 1 := by simpa only [← @div_self _ _ b h] using (div_sub_div_same a b b).symm theorem div_sub_one {a b : K} (h : b ≠ 0) : a / b - 1 = (a - b) / b := (div_sub_same h).symm theorem sub_div (a b c : K) : (a - b) / c = a / c - b / c := (div_sub_div_same _ _ _).symm /-- See `inv_sub_inv` for the more convenient version when `K` is commutative. -/ theorem inv_sub_inv' {a b : K} (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ - b⁻¹ = a⁻¹ * (b - a) * b⁻¹ := let _ := invertibleOfNonzero ha; let _ := invertibleOfNonzero hb; invOf_sub_invOf a b theorem one_div_mul_sub_mul_one_div_eq_one_div_add_one_div (ha : a ≠ 0) (hb : b ≠ 0) : 1 / a * (b - a) * (1 / b) = 1 / a - 1 / b := by simpa only [one_div] using (inv_sub_inv' ha hb).symm -- see Note [lower instance priority] instance (priority := 100) DivisionRing.isDomain : IsDomain K := NoZeroDivisors.to_isDomain _ protected theorem Commute.div_sub_div (hbc : Commute b c) (hbd : Commute b d) (hb : b ≠ 0) (hd : d ≠ 0) : a / b - c / d = (a * d - b * c) / (b * d) := by simpa only [mul_neg, neg_div, ← sub_eq_add_neg] using hbc.neg_right.div_add_div hbd hb hd protected theorem Commute.inv_sub_inv (hab : Commute a b) (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ - b⁻¹ = (b - a) / (a * b) := by simp only [inv_eq_one_div, (Commute.one_right a).div_sub_div hab ha hb, one_mul, mul_one] variable [NeZero (2 : K)] lemma sub_half (a : K) : a - a / 2 = a / 2 := by rw [sub_eq_iff_eq_add, add_halves] lemma half_sub (a : K) : a / 2 - a = -(a / 2) := by rw [← neg_sub, sub_half] end DivisionRing section Semifield variable [Semifield K] {a b d : K} theorem div_add_div (a : K) (c : K) (hb : b ≠ 0) (hd : d ≠ 0) : a / b + c / d = (a * d + b * c) / (b * d) := (Commute.all b _).div_add_div (Commute.all _ _) hb hd theorem one_div_add_one_div (ha : a ≠ 0) (hb : b ≠ 0) : 1 / a + 1 / b = (a + b) / (a * b) := (Commute.all a _).one_div_add_one_div ha hb theorem inv_add_inv (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ + b⁻¹ = (a + b) / (a * b) := (Commute.all a _).inv_add_inv ha hb end Semifield section Field variable [Field K]
attribute [local simp] mul_assoc mul_comm mul_left_comm
Mathlib/Algebra/Field/Basic.lean
158
159
/- Copyright (c) 2019 Kim Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kim Morrison, Jakob von Raumer -/ import Mathlib.CategoryTheory.Limits.Shapes.FiniteProducts import Mathlib.CategoryTheory.Limits.Shapes.Kernels /-! # Biproducts and binary biproducts We introduce the notion of (finite) biproducts. Binary biproducts are defined in `CategoryTheory.Limits.Shapes.BinaryBiproducts`. These are slightly unusual relative to the other shapes in the library, as they are simultaneously limits and colimits. (Zero objects are similar; they are "biterminal".) For results about biproducts in preadditive categories see `CategoryTheory.Preadditive.Biproducts`. For biproducts indexed by a `Fintype J`, a `bicone` consists of a cone point `X` and morphisms `π j : X ⟶ F j` and `ι j : F j ⟶ X` for each `j`, such that `ι j ≫ π j'` is the identity when `j = j'` and zero otherwise. ## Notation As `⊕` is already taken for the sum of types, we introduce the notation `X ⊞ Y` for a binary biproduct. We introduce `⨁ f` for the indexed biproduct. ## Implementation notes Prior to https://github.com/leanprover-community/mathlib3/pull/14046, `HasFiniteBiproducts` required a `DecidableEq` instance on the indexing type. As this had no pay-off (everything about limits is non-constructive in mathlib), and occasional cost (constructing decidability instances appropriate for constructions involving the indexing type), we made everything classical. -/ noncomputable section universe w w' v u open CategoryTheory Functor namespace CategoryTheory.Limits variable {J : Type w} universe uC' uC uD' uD variable {C : Type uC} [Category.{uC'} C] [HasZeroMorphisms C] variable {D : Type uD} [Category.{uD'} D] [HasZeroMorphisms D] open scoped Classical in /-- A `c : Bicone F` is: * an object `c.pt` and * morphisms `π j : pt ⟶ F j` and `ι j : F j ⟶ pt` for each `j`, * such that `ι j ≫ π j'` is the identity when `j = j'` and zero otherwise. -/ structure Bicone (F : J → C) where pt : C π : ∀ j, pt ⟶ F j ι : ∀ j, F j ⟶ pt ι_π : ∀ j j', ι j ≫ π j' = if h : j = j' then eqToHom (congrArg F h) else 0 := by aesop attribute [inherit_doc Bicone] Bicone.pt Bicone.π Bicone.ι Bicone.ι_π @[reassoc (attr := simp)] theorem bicone_ι_π_self {F : J → C} (B : Bicone F) (j : J) : B.ι j ≫ B.π j = 𝟙 (F j) := by simpa using B.ι_π j j @[reassoc (attr := simp)] theorem bicone_ι_π_ne {F : J → C} (B : Bicone F) {j j' : J} (h : j ≠ j') : B.ι j ≫ B.π j' = 0 := by simpa [h] using B.ι_π j j' variable {F : J → C} /-- A bicone morphism between two bicones for the same diagram is a morphism of the bicone points which commutes with the cone and cocone legs. -/ structure BiconeMorphism {F : J → C} (A B : Bicone F) where /-- A morphism between the two vertex objects of the bicones -/ hom : A.pt ⟶ B.pt /-- The triangle consisting of the two natural transformations and `hom` commutes -/ wπ : ∀ j : J, hom ≫ B.π j = A.π j := by aesop_cat /-- The triangle consisting of the two natural transformations and `hom` commutes -/ wι : ∀ j : J, A.ι j ≫ hom = B.ι j := by aesop_cat attribute [reassoc (attr := simp)] BiconeMorphism.wι BiconeMorphism.wπ /-- The category of bicones on a given diagram. -/ @[simps] instance Bicone.category : Category (Bicone F) where Hom A B := BiconeMorphism A B comp f g := { hom := f.hom ≫ g.hom } id B := { hom := 𝟙 B.pt } -- Porting note: if we do not have `simps` automatically generate the lemma for simplifying -- the `hom` field of a category, we need to write the `ext` lemma in terms of the categorical -- morphism, rather than the underlying structure. @[ext] theorem BiconeMorphism.ext {c c' : Bicone F} (f g : c ⟶ c') (w : f.hom = g.hom) : f = g := by cases f cases g congr namespace Bicones /-- To give an isomorphism between cocones, it suffices to give an isomorphism between their vertices which commutes with the cocone maps. -/ @[aesop apply safe (rule_sets := [CategoryTheory]), simps] def ext {c c' : Bicone F} (φ : c.pt ≅ c'.pt) (wι : ∀ j, c.ι j ≫ φ.hom = c'.ι j := by aesop_cat) (wπ : ∀ j, φ.hom ≫ c'.π j = c.π j := by aesop_cat) : c ≅ c' where hom := { hom := φ.hom } inv := { hom := φ.inv wι := fun j => φ.comp_inv_eq.mpr (wι j).symm wπ := fun j => φ.inv_comp_eq.mpr (wπ j).symm } variable (F) in /-- A functor `G : C ⥤ D` sends bicones over `F` to bicones over `G.obj ∘ F` functorially. -/ @[simps] def functoriality (G : C ⥤ D) [Functor.PreservesZeroMorphisms G] : Bicone F ⥤ Bicone (G.obj ∘ F) where obj A := { pt := G.obj A.pt π := fun j => G.map (A.π j) ι := fun j => G.map (A.ι j) ι_π := fun i j => (Functor.map_comp _ _ _).symm.trans <| by rw [A.ι_π] aesop_cat } map f := { hom := G.map f.hom wπ := fun j => by simp [-BiconeMorphism.wπ, ← f.wπ j] wι := fun j => by simp [-BiconeMorphism.wι, ← f.wι j] } variable (G : C ⥤ D) instance functoriality_full [G.PreservesZeroMorphisms] [G.Full] [G.Faithful] : (functoriality F G).Full where map_surjective t := ⟨{ hom := G.preimage t.hom wι := fun j => G.map_injective (by simpa using t.wι j) wπ := fun j => G.map_injective (by simpa using t.wπ j) }, by aesop_cat⟩ instance functoriality_faithful [G.PreservesZeroMorphisms] [G.Faithful] : (functoriality F G).Faithful where map_injective {_X} {_Y} f g h := BiconeMorphism.ext f g <| G.map_injective <| congr_arg BiconeMorphism.hom h end Bicones namespace Bicone attribute [local aesop safe tactic (rule_sets := [CategoryTheory])] CategoryTheory.Discrete.discreteCases -- Porting note: would it be okay to use this more generally? attribute [local aesop safe cases (rule_sets := [CategoryTheory])] Eq /-- Extract the cone from a bicone. -/ def toConeFunctor : Bicone F ⥤ Cone (Discrete.functor F) where obj B := { pt := B.pt, π := { app := fun j => B.π j.as } } map {_ _} F := { hom := F.hom, w := fun _ => F.wπ _ } /-- A shorthand for `toConeFunctor.obj` -/ abbrev toCone (B : Bicone F) : Cone (Discrete.functor F) := toConeFunctor.obj B -- TODO Consider changing this API to `toFan (B : Bicone F) : Fan F`. @[simp] theorem toCone_pt (B : Bicone F) : B.toCone.pt = B.pt := rfl @[simp] theorem toCone_π_app (B : Bicone F) (j : Discrete J) : B.toCone.π.app j = B.π j.as := rfl theorem toCone_π_app_mk (B : Bicone F) (j : J) : B.toCone.π.app ⟨j⟩ = B.π j := rfl @[simp] theorem toCone_proj (B : Bicone F) (j : J) : Fan.proj B.toCone j = B.π j := rfl /-- Extract the cocone from a bicone. -/ def toCoconeFunctor : Bicone F ⥤ Cocone (Discrete.functor F) where obj B := { pt := B.pt, ι := { app := fun j => B.ι j.as } } map {_ _} F := { hom := F.hom, w := fun _ => F.wι _ } /-- A shorthand for `toCoconeFunctor.obj` -/ abbrev toCocone (B : Bicone F) : Cocone (Discrete.functor F) := toCoconeFunctor.obj B @[simp] theorem toCocone_pt (B : Bicone F) : B.toCocone.pt = B.pt := rfl @[simp] theorem toCocone_ι_app (B : Bicone F) (j : Discrete J) : B.toCocone.ι.app j = B.ι j.as := rfl @[simp] theorem toCocone_inj (B : Bicone F) (j : J) : Cofan.inj B.toCocone j = B.ι j := rfl theorem toCocone_ι_app_mk (B : Bicone F) (j : J) : B.toCocone.ι.app ⟨j⟩ = B.ι j := rfl open scoped Classical in /-- We can turn any limit cone over a discrete collection of objects into a bicone. -/ @[simps] def ofLimitCone {f : J → C} {t : Cone (Discrete.functor f)} (ht : IsLimit t) : Bicone f where pt := t.pt π j := t.π.app ⟨j⟩ ι j := ht.lift (Fan.mk _ fun j' => if h : j = j' then eqToHom (congr_arg f h) else 0) ι_π j j' := by simp open scoped Classical in theorem ι_of_isLimit {f : J → C} {t : Bicone f} (ht : IsLimit t.toCone) (j : J) : t.ι j = ht.lift (Fan.mk _ fun j' => if h : j = j' then eqToHom (congr_arg f h) else 0) := ht.hom_ext fun j' => by rw [ht.fac] simp [t.ι_π] open scoped Classical in /-- We can turn any colimit cocone over a discrete collection of objects into a bicone. -/ @[simps] def ofColimitCocone {f : J → C} {t : Cocone (Discrete.functor f)} (ht : IsColimit t) : Bicone f where pt := t.pt π j := ht.desc (Cofan.mk _ fun j' => if h : j' = j then eqToHom (congr_arg f h) else 0) ι j := t.ι.app ⟨j⟩ ι_π j j' := by simp open scoped Classical in theorem π_of_isColimit {f : J → C} {t : Bicone f} (ht : IsColimit t.toCocone) (j : J) : t.π j = ht.desc (Cofan.mk _ fun j' => if h : j' = j then eqToHom (congr_arg f h) else 0) := ht.hom_ext fun j' => by rw [ht.fac] simp [t.ι_π] /-- Structure witnessing that a bicone is both a limit cone and a colimit cocone. -/ structure IsBilimit {F : J → C} (B : Bicone F) where isLimit : IsLimit B.toCone isColimit : IsColimit B.toCocone attribute [inherit_doc IsBilimit] IsBilimit.isLimit IsBilimit.isColimit attribute [simp] IsBilimit.mk.injEq attribute [local ext] Bicone.IsBilimit instance subsingleton_isBilimit {f : J → C} {c : Bicone f} : Subsingleton c.IsBilimit := ⟨fun _ _ => Bicone.IsBilimit.ext (Subsingleton.elim _ _) (Subsingleton.elim _ _)⟩ section Whisker variable {K : Type w'} /-- Whisker a bicone with an equivalence between the indexing types. -/ @[simps] def whisker {f : J → C} (c : Bicone f) (g : K ≃ J) : Bicone (f ∘ g) where pt := c.pt π k := c.π (g k) ι k := c.ι (g k) ι_π k k' := by simp only [c.ι_π] split_ifs with h h' h' <;> simp [Equiv.apply_eq_iff_eq g] at h h' <;> tauto /-- Taking the cone of a whiskered bicone results in a cone isomorphic to one gained by whiskering the cone and postcomposing with a suitable isomorphism. -/ def whiskerToCone {f : J → C} (c : Bicone f) (g : K ≃ J) : (c.whisker g).toCone ≅ (Cones.postcompose (Discrete.functorComp f g).inv).obj (c.toCone.whisker (Discrete.functor (Discrete.mk ∘ g))) := Cones.ext (Iso.refl _) (by simp) /-- Taking the cocone of a whiskered bicone results in a cone isomorphic to one gained by whiskering the cocone and precomposing with a suitable isomorphism. -/ def whiskerToCocone {f : J → C} (c : Bicone f) (g : K ≃ J) : (c.whisker g).toCocone ≅ (Cocones.precompose (Discrete.functorComp f g).hom).obj (c.toCocone.whisker (Discrete.functor (Discrete.mk ∘ g))) := Cocones.ext (Iso.refl _) (by simp) /-- Whiskering a bicone with an equivalence between types preserves being a bilimit bicone. -/ noncomputable def whiskerIsBilimitIff {f : J → C} (c : Bicone f) (g : K ≃ J) : (c.whisker g).IsBilimit ≃ c.IsBilimit := by refine equivOfSubsingletonOfSubsingleton (fun hc => ⟨?_, ?_⟩) fun hc => ⟨?_, ?_⟩ · let this := IsLimit.ofIsoLimit hc.isLimit (Bicone.whiskerToCone c g) let this := (IsLimit.postcomposeHomEquiv (Discrete.functorComp f g).symm _) this exact IsLimit.ofWhiskerEquivalence (Discrete.equivalence g) this · let this := IsColimit.ofIsoColimit hc.isColimit (Bicone.whiskerToCocone c g) let this := (IsColimit.precomposeHomEquiv (Discrete.functorComp f g) _) this exact IsColimit.ofWhiskerEquivalence (Discrete.equivalence g) this · apply IsLimit.ofIsoLimit _ (Bicone.whiskerToCone c g).symm apply (IsLimit.postcomposeHomEquiv (Discrete.functorComp f g).symm _).symm _ exact IsLimit.whiskerEquivalence hc.isLimit (Discrete.equivalence g) · apply IsColimit.ofIsoColimit _ (Bicone.whiskerToCocone c g).symm apply (IsColimit.precomposeHomEquiv (Discrete.functorComp f g) _).symm _ exact IsColimit.whiskerEquivalence hc.isColimit (Discrete.equivalence g) end Whisker end Bicone /-- A bicone over `F : J → C`, which is both a limit cone and a colimit cocone. -/ structure LimitBicone (F : J → C) where bicone : Bicone F isBilimit : bicone.IsBilimit attribute [inherit_doc LimitBicone] LimitBicone.bicone LimitBicone.isBilimit /-- `HasBiproduct F` expresses the mere existence of a bicone which is simultaneously a limit and a colimit of the diagram `F`. -/ class HasBiproduct (F : J → C) : Prop where mk' :: exists_biproduct : Nonempty (LimitBicone F) attribute [inherit_doc HasBiproduct] HasBiproduct.exists_biproduct theorem HasBiproduct.mk {F : J → C} (d : LimitBicone F) : HasBiproduct F := ⟨Nonempty.intro d⟩ /-- Use the axiom of choice to extract explicit `BiproductData F` from `HasBiproduct F`. -/ def getBiproductData (F : J → C) [HasBiproduct F] : LimitBicone F := Classical.choice HasBiproduct.exists_biproduct /-- A bicone for `F` which is both a limit cone and a colimit cocone. -/ def biproduct.bicone (F : J → C) [HasBiproduct F] : Bicone F := (getBiproductData F).bicone /-- `biproduct.bicone F` is a bilimit bicone. -/ def biproduct.isBilimit (F : J → C) [HasBiproduct F] : (biproduct.bicone F).IsBilimit := (getBiproductData F).isBilimit /-- `biproduct.bicone F` is a limit cone. -/ def biproduct.isLimit (F : J → C) [HasBiproduct F] : IsLimit (biproduct.bicone F).toCone := (getBiproductData F).isBilimit.isLimit /-- `biproduct.bicone F` is a colimit cocone. -/ def biproduct.isColimit (F : J → C) [HasBiproduct F] : IsColimit (biproduct.bicone F).toCocone := (getBiproductData F).isBilimit.isColimit instance (priority := 100) hasProduct_of_hasBiproduct [HasBiproduct F] : HasProduct F := HasLimit.mk { cone := (biproduct.bicone F).toCone isLimit := biproduct.isLimit F } instance (priority := 100) hasCoproduct_of_hasBiproduct [HasBiproduct F] : HasCoproduct F := HasColimit.mk { cocone := (biproduct.bicone F).toCocone isColimit := biproduct.isColimit F } variable (J C) /-- `C` has biproducts of shape `J` if we have a limit and a colimit, with the same cone points, of every function `F : J → C`. -/ class HasBiproductsOfShape : Prop where has_biproduct : ∀ F : J → C, HasBiproduct F attribute [instance 100] HasBiproductsOfShape.has_biproduct /-- `HasFiniteBiproducts C` represents a choice of biproduct for every family of objects in `C` indexed by a finite type. -/ class HasFiniteBiproducts : Prop where out : ∀ n, HasBiproductsOfShape (Fin n) C attribute [inherit_doc HasFiniteBiproducts] HasFiniteBiproducts.out variable {J} theorem hasBiproductsOfShape_of_equiv {K : Type w'} [HasBiproductsOfShape K C] (e : J ≃ K) : HasBiproductsOfShape J C := ⟨fun F => let ⟨⟨h⟩⟩ := HasBiproductsOfShape.has_biproduct (F ∘ e.symm) let ⟨c, hc⟩ := h HasBiproduct.mk <| by simpa only [Function.comp_def, e.symm_apply_apply] using LimitBicone.mk (c.whisker e) ((c.whiskerIsBilimitIff _).2 hc)⟩ instance (priority := 100) hasBiproductsOfShape_finite [HasFiniteBiproducts C] [Finite J] : HasBiproductsOfShape J C := by rcases Finite.exists_equiv_fin J with ⟨n, ⟨e⟩⟩ haveI : HasBiproductsOfShape (Fin n) C := HasFiniteBiproducts.out n exact hasBiproductsOfShape_of_equiv C e instance (priority := 100) hasFiniteProducts_of_hasFiniteBiproducts [HasFiniteBiproducts C] : HasFiniteProducts C where out _ := ⟨fun _ => hasLimit_of_iso Discrete.natIsoFunctor.symm⟩ instance (priority := 100) hasFiniteCoproducts_of_hasFiniteBiproducts [HasFiniteBiproducts C] : HasFiniteCoproducts C where out _ := ⟨fun _ => hasColimit_of_iso Discrete.natIsoFunctor⟩ instance (priority := 100) hasProductsOfShape_of_hasBiproductsOfShape [HasBiproductsOfShape J C] : HasProductsOfShape J C where has_limit _ := hasLimit_of_iso Discrete.natIsoFunctor.symm instance (priority := 100) hasCoproductsOfShape_of_hasBiproductsOfShape [HasBiproductsOfShape J C] : HasCoproductsOfShape J C where has_colimit _ := hasColimit_of_iso Discrete.natIsoFunctor variable {C} /-- The isomorphism between the specified limit and the specified colimit for a functor with a bilimit. -/ def biproductIso (F : J → C) [HasBiproduct F] : Limits.piObj F ≅ Limits.sigmaObj F := (IsLimit.conePointUniqueUpToIso (limit.isLimit _) (biproduct.isLimit F)).trans <| IsColimit.coconePointUniqueUpToIso (biproduct.isColimit F) (colimit.isColimit _) variable {J : Type w} {K : Type*} variable {C : Type u} [Category.{v} C] [HasZeroMorphisms C] /-- `biproduct f` computes the biproduct of a family of elements `f`. (It is defined as an abbreviation for `limit (Discrete.functor f)`, so for most facts about `biproduct f`, you will just use general facts about limits and colimits.) -/ abbrev biproduct (f : J → C) [HasBiproduct f] : C := (biproduct.bicone f).pt @[inherit_doc biproduct] notation "⨁ " f:20 => biproduct f /-- The projection onto a summand of a biproduct. -/ abbrev biproduct.π (f : J → C) [HasBiproduct f] (b : J) : ⨁ f ⟶ f b := (biproduct.bicone f).π b @[simp] theorem biproduct.bicone_π (f : J → C) [HasBiproduct f] (b : J) : (biproduct.bicone f).π b = biproduct.π f b := rfl /-- The inclusion into a summand of a biproduct. -/ abbrev biproduct.ι (f : J → C) [HasBiproduct f] (b : J) : f b ⟶ ⨁ f := (biproduct.bicone f).ι b @[simp] theorem biproduct.bicone_ι (f : J → C) [HasBiproduct f] (b : J) : (biproduct.bicone f).ι b = biproduct.ι f b := rfl /-- Note that as this lemma has an `if` in the statement, we include a `DecidableEq` argument. This means you may not be able to `simp` using this lemma unless you `open scoped Classical`. -/ @[reassoc] theorem biproduct.ι_π [DecidableEq J] (f : J → C) [HasBiproduct f] (j j' : J) : biproduct.ι f j ≫ biproduct.π f j' = if h : j = j' then eqToHom (congr_arg f h) else 0 := by convert (biproduct.bicone f).ι_π j j' @[reassoc] -- Porting note: both versions proven by simp theorem biproduct.ι_π_self (f : J → C) [HasBiproduct f] (j : J) : biproduct.ι f j ≫ biproduct.π f j = 𝟙 _ := by simp [biproduct.ι_π] @[reassoc (attr := simp)] theorem biproduct.ι_π_ne (f : J → C) [HasBiproduct f] {j j' : J} (h : j ≠ j') : biproduct.ι f j ≫ biproduct.π f j' = 0 := by simp [biproduct.ι_π, h] -- The `simpNF` linter incorrectly identifies these as simp lemmas that could never apply. -- It seems the side condition `w` is not applied by `simpNF`. -- https://github.com/leanprover-community/mathlib4/issues/5049 -- They are used by `simp` in `biproduct.whiskerEquiv` below. @[reassoc (attr := simp, nolint simpNF)] theorem biproduct.eqToHom_comp_ι (f : J → C) [HasBiproduct f] {j j' : J} (w : j = j') : eqToHom (by simp [w]) ≫ biproduct.ι f j' = biproduct.ι f j := by cases w simp -- The `simpNF` linter incorrectly identifies these as simp lemmas that could never apply. -- It seems the side condition `w` is not applied by `simpNF`. -- https://github.com/leanprover-community/mathlib4/issues/5049 -- They are used by `simp` in `biproduct.whiskerEquiv` below. @[reassoc (attr := simp, nolint simpNF)] theorem biproduct.π_comp_eqToHom (f : J → C) [HasBiproduct f] {j j' : J} (w : j = j') : biproduct.π f j ≫ eqToHom (by simp [w]) = biproduct.π f j' := by cases w simp /-- Given a collection of maps into the summands, we obtain a map into the biproduct. -/ abbrev biproduct.lift {f : J → C} [HasBiproduct f] {P : C} (p : ∀ b, P ⟶ f b) : P ⟶ ⨁ f := (biproduct.isLimit f).lift (Fan.mk P p) /-- Given a collection of maps out of the summands, we obtain a map out of the biproduct. -/ abbrev biproduct.desc {f : J → C} [HasBiproduct f] {P : C} (p : ∀ b, f b ⟶ P) : ⨁ f ⟶ P := (biproduct.isColimit f).desc (Cofan.mk P p) @[reassoc (attr := simp)] theorem biproduct.lift_π {f : J → C} [HasBiproduct f] {P : C} (p : ∀ b, P ⟶ f b) (j : J) : biproduct.lift p ≫ biproduct.π f j = p j := (biproduct.isLimit f).fac _ ⟨j⟩ @[reassoc (attr := simp)] theorem biproduct.ι_desc {f : J → C} [HasBiproduct f] {P : C} (p : ∀ b, f b ⟶ P) (j : J) : biproduct.ι f j ≫ biproduct.desc p = p j := (biproduct.isColimit f).fac _ ⟨j⟩ /-- Given a collection of maps between corresponding summands of a pair of biproducts indexed by the same type, we obtain a map between the biproducts. -/ abbrev biproduct.map {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ b, f b ⟶ g b) : ⨁ f ⟶ ⨁ g := IsLimit.map (biproduct.bicone f).toCone (biproduct.isLimit g) (Discrete.natTrans (fun j => p j.as)) /-- An alternative to `biproduct.map` constructed via colimits. This construction only exists in order to show it is equal to `biproduct.map`. -/ abbrev biproduct.map' {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ b, f b ⟶ g b) : ⨁ f ⟶ ⨁ g := IsColimit.map (biproduct.isColimit f) (biproduct.bicone g).toCocone (Discrete.natTrans fun j => p j.as) -- We put this at slightly higher priority than `biproduct.hom_ext'`, -- to get the matrix indices in the "right" order. @[ext 1001] theorem biproduct.hom_ext {f : J → C} [HasBiproduct f] {Z : C} (g h : Z ⟶ ⨁ f) (w : ∀ j, g ≫ biproduct.π f j = h ≫ biproduct.π f j) : g = h := (biproduct.isLimit f).hom_ext fun j => w j.as @[ext] theorem biproduct.hom_ext' {f : J → C} [HasBiproduct f] {Z : C} (g h : ⨁ f ⟶ Z) (w : ∀ j, biproduct.ι f j ≫ g = biproduct.ι f j ≫ h) : g = h := (biproduct.isColimit f).hom_ext fun j => w j.as /-- The canonical isomorphism between the chosen biproduct and the chosen product. -/ def biproduct.isoProduct (f : J → C) [HasBiproduct f] : ⨁ f ≅ ∏ᶜ f := IsLimit.conePointUniqueUpToIso (biproduct.isLimit f) (limit.isLimit _) @[simp] theorem biproduct.isoProduct_hom {f : J → C} [HasBiproduct f] : (biproduct.isoProduct f).hom = Pi.lift (biproduct.π f) := limit.hom_ext fun j => by simp [biproduct.isoProduct] @[simp] theorem biproduct.isoProduct_inv {f : J → C} [HasBiproduct f] : (biproduct.isoProduct f).inv = biproduct.lift (Pi.π f) := biproduct.hom_ext _ _ fun j => by simp [Iso.inv_comp_eq] /-- The canonical isomorphism between the chosen biproduct and the chosen coproduct. -/ def biproduct.isoCoproduct (f : J → C) [HasBiproduct f] : ⨁ f ≅ ∐ f := IsColimit.coconePointUniqueUpToIso (biproduct.isColimit f) (colimit.isColimit _) @[simp] theorem biproduct.isoCoproduct_inv {f : J → C} [HasBiproduct f] : (biproduct.isoCoproduct f).inv = Sigma.desc (biproduct.ι f) := colimit.hom_ext fun j => by simp [biproduct.isoCoproduct] @[simp] theorem biproduct.isoCoproduct_hom {f : J → C} [HasBiproduct f] : (biproduct.isoCoproduct f).hom = biproduct.desc (Sigma.ι f) := biproduct.hom_ext' _ _ fun j => by simp [← Iso.eq_comp_inv] /-- If a category has biproducts of a shape `J`, its `colim` and `lim` functor on diagrams over `J` are isomorphic. -/ @[simps!] def HasBiproductsOfShape.colimIsoLim [HasBiproductsOfShape J C] : colim (J := Discrete J) (C := C) ≅ lim := NatIso.ofComponents (fun F => (Sigma.isoColimit F).symm ≪≫ (biproduct.isoCoproduct _).symm ≪≫ biproduct.isoProduct _ ≪≫ Pi.isoLimit F) fun η => colimit.hom_ext fun ⟨i⟩ => limit.hom_ext fun ⟨j⟩ => by classical by_cases h : i = j <;> simp_all [h, Sigma.isoColimit, Pi.isoLimit, biproduct.ι_π, biproduct.ι_π_assoc] theorem biproduct.map_eq_map' {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ b, f b ⟶ g b) : biproduct.map p = biproduct.map' p := by classical ext dsimp simp only [Discrete.natTrans_app, Limits.IsColimit.ι_map_assoc, Limits.IsLimit.map_π, Category.assoc, ← Bicone.toCone_π_app_mk, ← biproduct.bicone_π, ← Bicone.toCocone_ι_app_mk, ← biproduct.bicone_ι] dsimp rw [biproduct.ι_π_assoc, biproduct.ι_π] split_ifs with h · subst h; simp · simp @[reassoc (attr := simp)] theorem biproduct.map_π {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) (j : J) : biproduct.map p ≫ biproduct.π g j = biproduct.π f j ≫ p j := Limits.IsLimit.map_π _ _ _ (Discrete.mk j) @[reassoc (attr := simp)] theorem biproduct.ι_map {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) (j : J) : biproduct.ι f j ≫ biproduct.map p = p j ≫ biproduct.ι g j := by rw [biproduct.map_eq_map'] apply Limits.IsColimit.ι_map (biproduct.isColimit f) (biproduct.bicone g).toCocone (Discrete.natTrans fun j => p j.as) (Discrete.mk j) @[reassoc (attr := simp)] theorem biproduct.map_desc {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) {P : C} (k : ∀ j, g j ⟶ P) : biproduct.map p ≫ biproduct.desc k = biproduct.desc fun j => p j ≫ k j := by ext; simp @[reassoc (attr := simp)] theorem biproduct.lift_map {f g : J → C} [HasBiproduct f] [HasBiproduct g] {P : C} (k : ∀ j, P ⟶ f j) (p : ∀ j, f j ⟶ g j) : biproduct.lift k ≫ biproduct.map p = biproduct.lift fun j => k j ≫ p j := by ext; simp /-- Given a collection of isomorphisms between corresponding summands of a pair of biproducts indexed by the same type, we obtain an isomorphism between the biproducts. -/ @[simps] def biproduct.mapIso {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ b, f b ≅ g b) : ⨁ f ≅ ⨁ g where hom := biproduct.map fun b => (p b).hom inv := biproduct.map fun b => (p b).inv instance biproduct.map_epi {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) [∀ j, Epi (p j)] : Epi (biproduct.map p) := by classical have : biproduct.map p = (biproduct.isoCoproduct _).hom ≫ Sigma.map p ≫ (biproduct.isoCoproduct _).inv := by ext simp only [map_π, isoCoproduct_hom, isoCoproduct_inv, Category.assoc, ι_desc_assoc, ι_colimMap_assoc, Discrete.functor_obj_eq_as, Discrete.natTrans_app, colimit.ι_desc_assoc, Cofan.mk_pt, Cofan.mk_ι_app, ι_π, ι_π_assoc] split all_goals simp_all rw [this] infer_instance instance Pi.map_epi {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) [∀ j, Epi (p j)] : Epi (Pi.map p) := by rw [show Pi.map p = (biproduct.isoProduct _).inv ≫ biproduct.map p ≫ (biproduct.isoProduct _).hom by aesop] infer_instance instance biproduct.map_mono {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) [∀ j, Mono (p j)] : Mono (biproduct.map p) := by rw [show biproduct.map p = (biproduct.isoProduct _).hom ≫ Pi.map p ≫ (biproduct.isoProduct _).inv by aesop] infer_instance instance Sigma.map_mono {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) [∀ j, Mono (p j)] : Mono (Sigma.map p) := by rw [show Sigma.map p = (biproduct.isoCoproduct _).inv ≫ biproduct.map p ≫ (biproduct.isoCoproduct _).hom by aesop] infer_instance /-- Two biproducts which differ by an equivalence in the indexing type, and up to isomorphism in the factors, are isomorphic. Unfortunately there are two natural ways to define each direction of this isomorphism (because it is true for both products and coproducts separately). We give the alternative definitions as lemmas below. -/ @[simps] def biproduct.whiskerEquiv {f : J → C} {g : K → C} (e : J ≃ K) (w : ∀ j, g (e j) ≅ f j) [HasBiproduct f] [HasBiproduct g] : ⨁ f ≅ ⨁ g where hom := biproduct.desc fun j => (w j).inv ≫ biproduct.ι g (e j) inv := biproduct.desc fun k => eqToHom (by simp) ≫ (w (e.symm k)).hom ≫ biproduct.ι f _ lemma biproduct.whiskerEquiv_hom_eq_lift {f : J → C} {g : K → C} (e : J ≃ K) (w : ∀ j, g (e j) ≅ f j) [HasBiproduct f] [HasBiproduct g] : (biproduct.whiskerEquiv e w).hom = biproduct.lift fun k => biproduct.π f (e.symm k) ≫ (w _).inv ≫ eqToHom (by simp) := by simp only [whiskerEquiv_hom] ext k j by_cases h : k = e j · subst h simp · simp only [ι_desc_assoc, Category.assoc, ne_eq, lift_π] rw [biproduct.ι_π_ne, biproduct.ι_π_ne_assoc] · simp · rintro rfl simp at h · exact Ne.symm h lemma biproduct.whiskerEquiv_inv_eq_lift {f : J → C} {g : K → C} (e : J ≃ K) (w : ∀ j, g (e j) ≅ f j) [HasBiproduct f] [HasBiproduct g] : (biproduct.whiskerEquiv e w).inv = biproduct.lift fun j => biproduct.π g (e j) ≫ (w j).hom := by simp only [whiskerEquiv_inv] ext j k by_cases h : k = e j · subst h simp only [ι_desc_assoc, ← eqToHom_iso_hom_naturality_assoc w (e.symm_apply_apply j).symm, Equiv.symm_apply_apply, eqToHom_comp_ι, Category.assoc, bicone_ι_π_self, Category.comp_id, lift_π, bicone_ι_π_self_assoc] · simp only [ι_desc_assoc, Category.assoc, ne_eq, lift_π] rw [biproduct.ι_π_ne, biproduct.ι_π_ne_assoc] · simp · exact h · rintro rfl simp at h attribute [local simp] Sigma.forall in instance {ι} (f : ι → Type*) (g : (i : ι) → (f i) → C) [∀ i, HasBiproduct (g i)] [HasBiproduct fun i => ⨁ g i] : HasBiproduct fun p : Σ i, f i => g p.1 p.2 where exists_biproduct := Nonempty.intro { bicone := { pt := ⨁ fun i => ⨁ g i ι := fun X => biproduct.ι (g X.1) X.2 ≫ biproduct.ι (fun i => ⨁ g i) X.1 π := fun X => biproduct.π (fun i => ⨁ g i) X.1 ≫ biproduct.π (g X.1) X.2 ι_π := fun ⟨j, x⟩ ⟨j', y⟩ => by split_ifs with h · obtain ⟨rfl, rfl⟩ := h simp · simp only [Sigma.mk.inj_iff, not_and] at h by_cases w : j = j' · cases w simp only [heq_eq_eq, forall_true_left] at h simp [biproduct.ι_π_ne _ h] · simp [biproduct.ι_π_ne_assoc _ w] } isBilimit := { isLimit := mkFanLimit _ (fun s => biproduct.lift fun b => biproduct.lift fun c => s.proj ⟨b, c⟩) isColimit := mkCofanColimit _ (fun s => biproduct.desc fun b => biproduct.desc fun c => s.inj ⟨b, c⟩) } } /-- An iterated biproduct is a biproduct over a sigma type. -/ @[simps] def biproductBiproductIso {ι} (f : ι → Type*) (g : (i : ι) → (f i) → C) [∀ i, HasBiproduct (g i)] [HasBiproduct fun i => ⨁ g i] : (⨁ fun i => ⨁ g i) ≅ (⨁ fun p : Σ i, f i => g p.1 p.2) where hom := biproduct.lift fun ⟨i, x⟩ => biproduct.π _ i ≫ biproduct.π _ x inv := biproduct.lift fun i => biproduct.lift fun x => biproduct.π _ (⟨i, x⟩ : Σ i, f i) section πKernel section variable (f : J → C) [HasBiproduct f] variable (p : J → Prop) [HasBiproduct (Subtype.restrict p f)] /-- The canonical morphism from the biproduct over a restricted index type to the biproduct of the full index type. -/ def biproduct.fromSubtype : ⨁ Subtype.restrict p f ⟶ ⨁ f := biproduct.desc fun j => biproduct.ι _ j.val /-- The canonical morphism from a biproduct to the biproduct over a restriction of its index type. -/ def biproduct.toSubtype : ⨁ f ⟶ ⨁ Subtype.restrict p f := biproduct.lift fun _ => biproduct.π _ _ @[reassoc (attr := simp)] theorem biproduct.fromSubtype_π [DecidablePred p] (j : J) : biproduct.fromSubtype f p ≫ biproduct.π f j = if h : p j then biproduct.π (Subtype.restrict p f) ⟨j, h⟩ else 0 := by classical ext i; dsimp rw [biproduct.fromSubtype, biproduct.ι_desc_assoc, biproduct.ι_π] by_cases h : p j · rw [dif_pos h, biproduct.ι_π] split_ifs with h₁ h₂ h₂ exacts [rfl, False.elim (h₂ (Subtype.ext h₁)), False.elim (h₁ (congr_arg Subtype.val h₂)), rfl] · rw [dif_neg h, dif_neg (show (i : J) ≠ j from fun h₂ => h (h₂ ▸ i.2)), comp_zero] theorem biproduct.fromSubtype_eq_lift [DecidablePred p] : biproduct.fromSubtype f p = biproduct.lift fun j => if h : p j then biproduct.π (Subtype.restrict p f) ⟨j, h⟩ else 0 := biproduct.hom_ext _ _ (by simp) @[reassoc] -- Porting note: both version solved using simp theorem biproduct.fromSubtype_π_subtype (j : Subtype p) : biproduct.fromSubtype f p ≫ biproduct.π f j = biproduct.π (Subtype.restrict p f) j := by classical ext rw [biproduct.fromSubtype, biproduct.ι_desc_assoc, biproduct.ι_π, biproduct.ι_π] split_ifs with h₁ h₂ h₂ exacts [rfl, False.elim (h₂ (Subtype.ext h₁)), False.elim (h₁ (congr_arg Subtype.val h₂)), rfl] @[reassoc (attr := simp)] theorem biproduct.toSubtype_π (j : Subtype p) : biproduct.toSubtype f p ≫ biproduct.π (Subtype.restrict p f) j = biproduct.π f j := biproduct.lift_π _ _ @[reassoc (attr := simp)] theorem biproduct.ι_toSubtype [DecidablePred p] (j : J) : biproduct.ι f j ≫ biproduct.toSubtype f p = if h : p j then biproduct.ι (Subtype.restrict p f) ⟨j, h⟩ else 0 := by classical ext i rw [biproduct.toSubtype, Category.assoc, biproduct.lift_π, biproduct.ι_π] by_cases h : p j · rw [dif_pos h, biproduct.ι_π] split_ifs with h₁ h₂ h₂ exacts [rfl, False.elim (h₂ (Subtype.ext h₁)), False.elim (h₁ (congr_arg Subtype.val h₂)), rfl] · rw [dif_neg h, dif_neg (show j ≠ i from fun h₂ => h (h₂.symm ▸ i.2)), zero_comp] theorem biproduct.toSubtype_eq_desc [DecidablePred p] : biproduct.toSubtype f p = biproduct.desc fun j => if h : p j then biproduct.ι (Subtype.restrict p f) ⟨j, h⟩ else 0 := biproduct.hom_ext' _ _ (by simp) @[reassoc] theorem biproduct.ι_toSubtype_subtype (j : Subtype p) : biproduct.ι f j ≫ biproduct.toSubtype f p = biproduct.ι (Subtype.restrict p f) j := by classical ext rw [biproduct.toSubtype, Category.assoc, biproduct.lift_π, biproduct.ι_π, biproduct.ι_π] split_ifs with h₁ h₂ h₂ exacts [rfl, False.elim (h₂ (Subtype.ext h₁)), False.elim (h₁ (congr_arg Subtype.val h₂)), rfl] @[reassoc (attr := simp)] theorem biproduct.ι_fromSubtype (j : Subtype p) : biproduct.ι (Subtype.restrict p f) j ≫ biproduct.fromSubtype f p = biproduct.ι f j := biproduct.ι_desc _ _ @[reassoc (attr := simp)] theorem biproduct.fromSubtype_toSubtype : biproduct.fromSubtype f p ≫ biproduct.toSubtype f p = 𝟙 (⨁ Subtype.restrict p f) := by refine biproduct.hom_ext _ _ fun j => ?_ rw [Category.assoc, biproduct.toSubtype_π, biproduct.fromSubtype_π_subtype, Category.id_comp] @[reassoc (attr := simp)] theorem biproduct.toSubtype_fromSubtype [DecidablePred p] : biproduct.toSubtype f p ≫ biproduct.fromSubtype f p = biproduct.map fun j => if p j then 𝟙 (f j) else 0 := by ext1 i by_cases h : p i · simp [h] · simp [h] end section variable (f : J → C) (i : J) [HasBiproduct f] [HasBiproduct (Subtype.restrict (fun j => j ≠ i) f)] open scoped Classical in /-- The kernel of `biproduct.π f i` is the inclusion from the biproduct which omits `i` from the index set `J` into the biproduct over `J`. -/ def biproduct.isLimitFromSubtype : IsLimit (KernelFork.ofι (biproduct.fromSubtype f fun j => j ≠ i) (by simp) : KernelFork (biproduct.π f i)) := Fork.IsLimit.mk' _ fun s => ⟨s.ι ≫ biproduct.toSubtype _ _, by apply biproduct.hom_ext; intro j rw [KernelFork.ι_ofι, Category.assoc, Category.assoc, biproduct.toSubtype_fromSubtype_assoc, biproduct.map_π] rcases Classical.em (i = j) with (rfl | h) · rw [if_neg (Classical.not_not.2 rfl), comp_zero, comp_zero, KernelFork.condition] · rw [if_pos (Ne.symm h), Category.comp_id], by intro m hm rw [← hm, KernelFork.ι_ofι, Category.assoc, biproduct.fromSubtype_toSubtype] exact (Category.comp_id _).symm⟩ instance : HasKernel (biproduct.π f i) := HasLimit.mk ⟨_, biproduct.isLimitFromSubtype f i⟩ /-- The kernel of `biproduct.π f i` is `⨁ Subtype.restrict {i}ᶜ f`. -/ @[simps!] def kernelBiproductπIso : kernel (biproduct.π f i) ≅ ⨁ Subtype.restrict (fun j => j ≠ i) f := limit.isoLimitCone ⟨_, biproduct.isLimitFromSubtype f i⟩ open scoped Classical in /-- The cokernel of `biproduct.ι f i` is the projection from the biproduct over the index set `J` onto the biproduct omitting `i`. -/ def biproduct.isColimitToSubtype : IsColimit (CokernelCofork.ofπ (biproduct.toSubtype f fun j => j ≠ i) (by simp) : CokernelCofork (biproduct.ι f i)) := Cofork.IsColimit.mk' _ fun s => ⟨biproduct.fromSubtype _ _ ≫ s.π, by apply biproduct.hom_ext'; intro j rw [CokernelCofork.π_ofπ, biproduct.toSubtype_fromSubtype_assoc, biproduct.ι_map_assoc] rcases Classical.em (i = j) with (rfl | h) · rw [if_neg (Classical.not_not.2 rfl), zero_comp, CokernelCofork.condition] · rw [if_pos (Ne.symm h), Category.id_comp], by intro m hm rw [← hm, CokernelCofork.π_ofπ, ← Category.assoc, biproduct.fromSubtype_toSubtype] exact (Category.id_comp _).symm⟩ instance : HasCokernel (biproduct.ι f i) := HasColimit.mk ⟨_, biproduct.isColimitToSubtype f i⟩ /-- The cokernel of `biproduct.ι f i` is `⨁ Subtype.restrict {i}ᶜ f`. -/ @[simps!] def cokernelBiproductιIso : cokernel (biproduct.ι f i) ≅ ⨁ Subtype.restrict (fun j => j ≠ i) f := colimit.isoColimitCocone ⟨_, biproduct.isColimitToSubtype f i⟩ end section -- Per https://github.com/leanprover-community/mathlib3/pull/15067, we only allow indexing in `Type 0` here. variable {K : Type} [Finite K] [HasFiniteBiproducts C] (f : K → C) /-- The limit cone exhibiting `⨁ Subtype.restrict pᶜ f` as the kernel of `biproduct.toSubtype f p` -/ @[simps] def kernelForkBiproductToSubtype (p : Set K) : LimitCone (parallelPair (biproduct.toSubtype f p) 0) where cone := KernelFork.ofι (biproduct.fromSubtype f pᶜ) (by classical ext j k simp only [Category.assoc, biproduct.ι_fromSubtype_assoc, biproduct.ι_toSubtype_assoc, comp_zero, zero_comp] rw [dif_neg k.2] simp only [zero_comp]) isLimit := KernelFork.IsLimit.ofι _ _ (fun {_} g _ => g ≫ biproduct.toSubtype f pᶜ) (by classical intro W' g' w ext j simp only [Category.assoc, biproduct.toSubtype_fromSubtype, Pi.compl_apply, biproduct.map_π] split_ifs with h · simp · replace w := w =≫ biproduct.π _ ⟨j, not_not.mp h⟩ simpa using w.symm) (by aesop_cat) instance (p : Set K) : HasKernel (biproduct.toSubtype f p) := HasLimit.mk (kernelForkBiproductToSubtype f p) /-- The kernel of `biproduct.toSubtype f p` is `⨁ Subtype.restrict pᶜ f`. -/ @[simps!] def kernelBiproductToSubtypeIso (p : Set K) : kernel (biproduct.toSubtype f p) ≅ ⨁ Subtype.restrict pᶜ f := limit.isoLimitCone (kernelForkBiproductToSubtype f p) /-- The colimit cocone exhibiting `⨁ Subtype.restrict pᶜ f` as the cokernel of `biproduct.fromSubtype f p` -/ @[simps] def cokernelCoforkBiproductFromSubtype (p : Set K) : ColimitCocone (parallelPair (biproduct.fromSubtype f p) 0) where cocone := CokernelCofork.ofπ (biproduct.toSubtype f pᶜ) (by classical ext j k simp only [Category.assoc, Pi.compl_apply, biproduct.ι_fromSubtype_assoc, biproduct.ι_toSubtype_assoc, comp_zero, zero_comp] rw [dif_neg] · simp only [zero_comp] · exact not_not.mpr k.2) isColimit := CokernelCofork.IsColimit.ofπ _ _ (fun {_} g _ => biproduct.fromSubtype f pᶜ ≫ g) (by classical intro W g' w ext j simp only [biproduct.toSubtype_fromSubtype_assoc, Pi.compl_apply, biproduct.ι_map_assoc] split_ifs with h · simp · replace w := biproduct.ι _ (⟨j, not_not.mp h⟩ : p) ≫= w simpa using w.symm) (by aesop_cat) instance (p : Set K) : HasCokernel (biproduct.fromSubtype f p) := HasColimit.mk (cokernelCoforkBiproductFromSubtype f p) /-- The cokernel of `biproduct.fromSubtype f p` is `⨁ Subtype.restrict pᶜ f`. -/ @[simps!] def cokernelBiproductFromSubtypeIso (p : Set K) : cokernel (biproduct.fromSubtype f p) ≅ ⨁ Subtype.restrict pᶜ f := colimit.isoColimitCocone (cokernelCoforkBiproductFromSubtype f p) end end πKernel section FiniteBiproducts variable {J : Type} [Finite J] {K : Type} [Finite K] {C : Type u} [Category.{v} C] [HasZeroMorphisms C] [HasFiniteBiproducts C] {f : J → C} {g : K → C} /-- Convert a (dependently typed) matrix to a morphism of biproducts. -/ def biproduct.matrix (m : ∀ j k, f j ⟶ g k) : ⨁ f ⟶ ⨁ g := biproduct.desc fun j => biproduct.lift fun k => m j k @[reassoc (attr := simp)] theorem biproduct.matrix_π (m : ∀ j k, f j ⟶ g k) (k : K) : biproduct.matrix m ≫ biproduct.π g k = biproduct.desc fun j => m j k := by ext simp [biproduct.matrix] @[reassoc (attr := simp)] theorem biproduct.ι_matrix (m : ∀ j k, f j ⟶ g k) (j : J) : biproduct.ι f j ≫ biproduct.matrix m = biproduct.lift fun k => m j k := by ext simp [biproduct.matrix] /-- Extract the matrix components from a morphism of biproducts. -/ def biproduct.components (m : ⨁ f ⟶ ⨁ g) (j : J) (k : K) : f j ⟶ g k := biproduct.ι f j ≫ m ≫ biproduct.π g k @[simp] theorem biproduct.matrix_components (m : ∀ j k, f j ⟶ g k) (j : J) (k : K) : biproduct.components (biproduct.matrix m) j k = m j k := by simp [biproduct.components] @[simp] theorem biproduct.components_matrix (m : ⨁ f ⟶ ⨁ g) : (biproduct.matrix fun j k => biproduct.components m j k) = m := by ext simp [biproduct.components] /-- Morphisms between direct sums are matrices. -/ @[simps] def biproduct.matrixEquiv : (⨁ f ⟶ ⨁ g) ≃ ∀ j k, f j ⟶ g k where toFun := biproduct.components invFun := biproduct.matrix left_inv := biproduct.components_matrix right_inv m := by ext apply biproduct.matrix_components end FiniteBiproducts variable {J : Type w} variable {C : Type u} [Category.{v} C] [HasZeroMorphisms C] variable {D : Type uD} [Category.{uD'} D] [HasZeroMorphisms D] instance biproduct.ι_mono (f : J → C) [HasBiproduct f] (b : J) : IsSplitMono (biproduct.ι f b) := by classical exact IsSplitMono.mk' { retraction := biproduct.desc <| Pi.single b (𝟙 (f b)) } instance biproduct.π_epi (f : J → C) [HasBiproduct f] (b : J) : IsSplitEpi (biproduct.π f b) := by classical exact IsSplitEpi.mk' { section_ := biproduct.lift <| Pi.single b (𝟙 (f b)) } /-- Auxiliary lemma for `biproduct.uniqueUpToIso`. -/ theorem biproduct.conePointUniqueUpToIso_hom (f : J → C) [HasBiproduct f] {b : Bicone f} (hb : b.IsBilimit) : (hb.isLimit.conePointUniqueUpToIso (biproduct.isLimit _)).hom = biproduct.lift b.π := rfl /-- Auxiliary lemma for `biproduct.uniqueUpToIso`. -/ theorem biproduct.conePointUniqueUpToIso_inv (f : J → C) [HasBiproduct f] {b : Bicone f} (hb : b.IsBilimit) : (hb.isLimit.conePointUniqueUpToIso (biproduct.isLimit _)).inv = biproduct.desc b.ι := by classical refine biproduct.hom_ext' _ _ fun j => hb.isLimit.hom_ext fun j' => ?_ rw [Category.assoc, IsLimit.conePointUniqueUpToIso_inv_comp, Bicone.toCone_π_app, biproduct.bicone_π, biproduct.ι_desc, biproduct.ι_π, b.toCone_π_app, b.ι_π] /-- Biproducts are unique up to isomorphism. This already follows because bilimits are limits, but in the case of biproducts we can give an isomorphism with particularly nice definitional properties, namely that `biproduct.lift b.π` and `biproduct.desc b.ι` are inverses of each other. -/ @[simps] def biproduct.uniqueUpToIso (f : J → C) [HasBiproduct f] {b : Bicone f} (hb : b.IsBilimit) : b.pt ≅ ⨁ f where hom := biproduct.lift b.π inv := biproduct.desc b.ι hom_inv_id := by rw [← biproduct.conePointUniqueUpToIso_hom f hb, ← biproduct.conePointUniqueUpToIso_inv f hb, Iso.hom_inv_id] inv_hom_id := by rw [← biproduct.conePointUniqueUpToIso_hom f hb, ← biproduct.conePointUniqueUpToIso_inv f hb, Iso.inv_hom_id] variable (C) -- see Note [lower instance priority] /-- A category with finite biproducts has a zero object. -/ instance (priority := 100) hasZeroObject_of_hasFiniteBiproducts [HasFiniteBiproducts C] : HasZeroObject C := by refine ⟨⟨biproduct Empty.elim, fun X => ⟨⟨⟨0⟩, ?_⟩⟩, fun X => ⟨⟨⟨0⟩, ?_⟩⟩⟩⟩ · intro a; apply biproduct.hom_ext'; simp · intro a; apply biproduct.hom_ext; simp section variable {C} attribute [local simp] eq_iff_true_of_subsingleton in /-- The limit bicone for the biproduct over an index type with exactly one term. -/ @[simps] def limitBiconeOfUnique [Unique J] (f : J → C) : LimitBicone f where bicone := { pt := f default π := fun j => eqToHom (by congr; rw [← Unique.uniq] ) ι := fun j => eqToHom (by congr; rw [← Unique.uniq] ) } isBilimit := { isLimit := (limitConeOfUnique f).isLimit isColimit := (colimitCoconeOfUnique f).isColimit } instance (priority := 100) hasBiproduct_unique [Subsingleton J] [Nonempty J] (f : J → C) : HasBiproduct f := let ⟨_⟩ := nonempty_unique J; .mk (limitBiconeOfUnique f) /-- A biproduct over an index type with exactly one term is just the object over that term. -/ @[simps!] def biproductUniqueIso [Unique J] (f : J → C) : ⨁ f ≅ f default := (biproduct.uniqueUpToIso _ (limitBiconeOfUnique f).isBilimit).symm end end CategoryTheory.Limits
Mathlib/CategoryTheory/Limits/Shapes/Biproducts.lean
2,184
2,187
/- Copyright (c) 2020 Kim Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Kim Morrison -/ import Mathlib.Algebra.Category.Ring.Colimits import Mathlib.Algebra.Category.Ring.Instances import Mathlib.Algebra.Category.Ring.Limits import Mathlib.Algebra.Ring.Subring.Basic import Mathlib.RingTheory.Localization.AtPrime import Mathlib.RingTheory.Spectrum.Prime.Topology import Mathlib.Topology.Sheaves.LocalPredicate /-! # The structure sheaf on `PrimeSpectrum R`. We define the structure sheaf on `TopCat.of (PrimeSpectrum R)`, for a commutative ring `R` and prove basic properties about it. We define this as a subsheaf of the sheaf of dependent functions into the localizations, cut out by the condition that the function must be locally equal to a ratio of elements of `R`. Because the condition "is equal to a fraction" passes to smaller open subsets, the subset of functions satisfying this condition is automatically a subpresheaf. Because the condition "is locally equal to a fraction" is local, it is also a subsheaf. (It may be helpful to refer back to `Mathlib/Topology/Sheaves/SheafOfFunctions.lean`, where we show that dependent functions into any type family form a sheaf, and also `Mathlib/Topology/Sheaves/LocalPredicate.lean`, where we characterise the predicates which pick out sub-presheaves and sub-sheaves of these sheaves.) We also set up the ring structure, obtaining `structureSheaf : Sheaf CommRingCat (PrimeSpectrum.Top R)`. We then construct two basic isomorphisms, relating the structure sheaf to the underlying ring `R`. First, `StructureSheaf.stalkIso` gives an isomorphism between the stalk of the structure sheaf at a point `p` and the localization of `R` at the prime ideal `p`. Second, `StructureSheaf.basicOpenIso` gives an isomorphism between the structure sheaf on `basicOpen f` and the localization of `R` at the submonoid of powers of `f`. ## References * [Robin Hartshorne, *Algebraic Geometry*][Har77] -/ universe u noncomputable section variable (R : Type u) [CommRing R] open TopCat open TopologicalSpace open CategoryTheory open Opposite namespace AlgebraicGeometry /-- The prime spectrum, just as a topological space. -/ def PrimeSpectrum.Top : TopCat := TopCat.of (PrimeSpectrum R) namespace StructureSheaf /-- The type family over `PrimeSpectrum R` consisting of the localization over each point. -/ def Localizations (P : PrimeSpectrum.Top R) : Type u := Localization.AtPrime P.asIdeal instance commRingLocalizations (P : PrimeSpectrum.Top R) : CommRing <| Localizations R P := inferInstanceAs <| CommRing <| Localization.AtPrime P.asIdeal instance localRingLocalizations (P : PrimeSpectrum.Top R) : IsLocalRing <| Localizations R P := inferInstanceAs <| IsLocalRing <| Localization.AtPrime P.asIdeal instance (P : PrimeSpectrum.Top R) : Inhabited (Localizations R P) := ⟨1⟩ instance (U : Opens (PrimeSpectrum.Top R)) (x : U) : Algebra R (Localizations R x) := inferInstanceAs <| Algebra R (Localization.AtPrime x.1.asIdeal) instance (U : Opens (PrimeSpectrum.Top R)) (x : U) : IsLocalization.AtPrime (Localizations R x) (x : PrimeSpectrum.Top R).asIdeal := Localization.isLocalization variable {R} /-- The predicate saying that a dependent function on an open `U` is realised as a fixed fraction `r / s` in each of the stalks (which are localizations at various prime ideals). -/ def IsFraction {U : Opens (PrimeSpectrum.Top R)} (f : ∀ x : U, Localizations R x) : Prop := ∃ r s : R, ∀ x : U, ¬s ∈ x.1.asIdeal ∧ f x * algebraMap _ _ s = algebraMap _ _ r theorem IsFraction.eq_mk' {U : Opens (PrimeSpectrum.Top R)} {f : ∀ x : U, Localizations R x} (hf : IsFraction f) : ∃ r s : R, ∀ x : U, ∃ hs : s ∉ x.1.asIdeal, f x = IsLocalization.mk' (Localization.AtPrime _) r (⟨s, hs⟩ : (x : PrimeSpectrum.Top R).asIdeal.primeCompl) := by rcases hf with ⟨r, s, h⟩ refine ⟨r, s, fun x => ⟨(h x).1, (IsLocalization.mk'_eq_iff_eq_mul.mpr ?_).symm⟩⟩ exact (h x).2.symm variable (R) /-- The predicate `IsFraction` is "prelocal", in the sense that if it holds on `U` it holds on any open subset `V` of `U`. -/ def isFractionPrelocal : PrelocalPredicate (Localizations R) where pred {_} f := IsFraction f res := by rintro V U i f ⟨r, s, w⟩; exact ⟨r, s, fun x => w (i x)⟩ /-- We will define the structure sheaf as the subsheaf of all dependent functions in `Π x : U, Localizations R x` consisting of those functions which can locally be expressed as a ratio of (the images in the localization of) elements of `R`. Quoting Hartshorne: For an open set $U ⊆ Spec A$, we define $𝒪(U)$ to be the set of functions $s : U → ⨆_{𝔭 ∈ U} A_𝔭$, such that $s(𝔭) ∈ A_𝔭$ for each $𝔭$, and such that $s$ is locally a quotient of elements of $A$: to be precise, we require that for each $𝔭 ∈ U$, there is a neighborhood $V$ of $𝔭$, contained in $U$, and elements $a, f ∈ A$, such that for each $𝔮 ∈ V, f ∉ 𝔮$, and $s(𝔮) = a/f$ in $A_𝔮$. Now Hartshorne had the disadvantage of not knowing about dependent functions, so we replace his circumlocution about functions into a disjoint union with `Π x : U, Localizations x`. -/ def isLocallyFraction : LocalPredicate (Localizations R) := (isFractionPrelocal R).sheafify @[simp] theorem isLocallyFraction_pred {U : Opens (PrimeSpectrum.Top R)} (f : ∀ x : U, Localizations R x) : (isLocallyFraction R).pred f = ∀ x : U, ∃ (V : _) (_ : x.1 ∈ V) (i : V ⟶ U), ∃ r s : R, ∀ y : V, ¬s ∈ y.1.asIdeal ∧ f (i y : U) * algebraMap _ _ s = algebraMap _ _ r := rfl /-- The functions satisfying `isLocallyFraction` form a subring. -/ def sectionsSubring (U : (Opens (PrimeSpectrum.Top R))ᵒᵖ) : Subring (∀ x : U.unop, Localizations R x) where carrier := { f | (isLocallyFraction R).pred f } zero_mem' := by refine fun x => ⟨unop U, x.2, 𝟙 _, 0, 1, fun y => ⟨?_, ?_⟩⟩ · rw [← Ideal.ne_top_iff_one]; exact y.1.isPrime.1 · simp one_mem' := by refine fun x => ⟨unop U, x.2, 𝟙 _, 1, 1, fun y => ⟨?_, ?_⟩⟩ · rw [← Ideal.ne_top_iff_one]; exact y.1.isPrime.1 · simp add_mem' := by intro a b ha hb x rcases ha x with ⟨Va, ma, ia, ra, sa, wa⟩ rcases hb x with ⟨Vb, mb, ib, rb, sb, wb⟩ refine ⟨Va ⊓ Vb, ⟨ma, mb⟩, Opens.infLELeft _ _ ≫ ia, ra * sb + rb * sa, sa * sb, ?_⟩ intro ⟨y, hy⟩ rcases wa (Opens.infLELeft _ _ ⟨y, hy⟩) with ⟨nma, wa⟩ rcases wb (Opens.infLERight _ _ ⟨y, hy⟩) with ⟨nmb, wb⟩ fconstructor · intro H; cases y.isPrime.mem_or_mem H <;> contradiction · simp only [Opens.apply_mk, Pi.add_apply, RingHom.map_mul, add_mul, RingHom.map_add] at wa wb ⊢ rw [← wa, ← wb] simp only [mul_assoc] congr 2 rw [mul_comm] neg_mem' := by intro a ha x rcases ha x with ⟨V, m, i, r, s, w⟩ refine ⟨V, m, i, -r, s, ?_⟩ intro y rcases w y with ⟨nm, w⟩ fconstructor · exact nm · simp only [RingHom.map_neg, Pi.neg_apply] rw [← w] simp only [neg_mul] mul_mem' := by intro a b ha hb x rcases ha x with ⟨Va, ma, ia, ra, sa, wa⟩ rcases hb x with ⟨Vb, mb, ib, rb, sb, wb⟩ refine ⟨Va ⊓ Vb, ⟨ma, mb⟩, Opens.infLELeft _ _ ≫ ia, ra * rb, sa * sb, ?_⟩ intro ⟨y, hy⟩ rcases wa (Opens.infLELeft _ _ ⟨y, hy⟩) with ⟨nma, wa⟩ rcases wb (Opens.infLERight _ _ ⟨y, hy⟩) with ⟨nmb, wb⟩ fconstructor · intro H; cases y.isPrime.mem_or_mem H <;> contradiction · simp only [Opens.apply_mk, Pi.mul_apply, RingHom.map_mul] at wa wb ⊢ rw [← wa, ← wb] simp only [mul_left_comm, mul_assoc, mul_comm] end StructureSheaf open StructureSheaf /-- The structure sheaf (valued in `Type`, not yet `CommRingCat`) is the subsheaf consisting of functions satisfying `isLocallyFraction`. -/ def structureSheafInType : Sheaf (Type u) (PrimeSpectrum.Top R) := subsheafToTypes (isLocallyFraction R) instance commRingStructureSheafInTypeObj (U : (Opens (PrimeSpectrum.Top R))ᵒᵖ) : CommRing ((structureSheafInType R).1.obj U) := (sectionsSubring R U).toCommRing open PrimeSpectrum /-- The structure presheaf, valued in `CommRingCat`, constructed by dressing up the `Type` valued structure presheaf. -/ @[simps obj_carrier] def structurePresheafInCommRing : Presheaf CommRingCat (PrimeSpectrum.Top R) where obj U := CommRingCat.of ((structureSheafInType R).1.obj U) map {_ _} i := CommRingCat.ofHom { toFun := (structureSheafInType R).1.map i map_zero' := rfl map_add' := fun _ _ => rfl map_one' := rfl map_mul' := fun _ _ => rfl } /-- Some glue, verifying that the structure presheaf valued in `CommRingCat` agrees with the `Type` valued structure presheaf. -/ def structurePresheafCompForget : structurePresheafInCommRing R ⋙ forget CommRingCat ≅ (structureSheafInType R).1 := NatIso.ofComponents fun _ => Iso.refl _ open TopCat.Presheaf /-- The structure sheaf on $Spec R$, valued in `CommRingCat`. This is provided as a bundled `SheafedSpace` as `Spec.SheafedSpace R` later. -/ def Spec.structureSheaf : Sheaf CommRingCat (PrimeSpectrum.Top R) := ⟨structurePresheafInCommRing R, (-- We check the sheaf condition under `forget CommRingCat`. isSheaf_iff_isSheaf_comp _ _).mpr (isSheaf_of_iso (structurePresheafCompForget R).symm (structureSheafInType R).cond)⟩ open Spec (structureSheaf) namespace StructureSheaf @[simp] theorem res_apply (U V : Opens (PrimeSpectrum.Top R)) (i : V ⟶ U) (s : (structureSheaf R).1.obj (op U)) (x : V) : ((structureSheaf R).1.map i.op s).1 x = (s.1 (i x) :) := rfl /- Notation in this comment X = Spec R OX = structure sheaf In the following we construct an isomorphism between OX_p and R_p given any point p corresponding to a prime ideal in R. We do this via 8 steps: 1. def const (f g : R) (V) (hv : V ≤ D_g) : OX(V) [for api] 2. def toOpen (U) : R ⟶ OX(U) 3. [2] def toStalk (p : Spec R) : R ⟶ OX_p 4. [2] def toBasicOpen (f : R) : R_f ⟶ OX(D_f) 5. [3] def localizationToStalk (p : Spec R) : R_p ⟶ OX_p 6. def openToLocalization (U) (p) (hp : p ∈ U) : OX(U) ⟶ R_p 7. [6] def stalkToFiberRingHom (p : Spec R) : OX_p ⟶ R_p 8. [5,7] def stalkIso (p : Spec R) : OX_p ≅ R_p In the square brackets we list the dependencies of a construction on the previous steps. -/ /-- The section of `structureSheaf R` on an open `U` sending each `x ∈ U` to the element `f/g` in the localization of `R` at `x`. -/ def const (f g : R) (U : Opens (PrimeSpectrum.Top R)) (hu : ∀ x ∈ U, g ∈ (x : PrimeSpectrum.Top R).asIdeal.primeCompl) : (structureSheaf R).1.obj (op U) := ⟨fun x => IsLocalization.mk' _ f ⟨g, hu x x.2⟩, fun x => ⟨U, x.2, 𝟙 _, f, g, fun y => ⟨hu y y.2, IsLocalization.mk'_spec _ _ _⟩⟩⟩ @[simp] theorem const_apply (f g : R) (U : Opens (PrimeSpectrum.Top R)) (hu : ∀ x ∈ U, g ∈ (x : PrimeSpectrum.Top R).asIdeal.primeCompl) (x : U) : (const R f g U hu).1 x = IsLocalization.mk' (Localization.AtPrime x.1.asIdeal) f ⟨g, hu x x.2⟩ := rfl theorem const_apply' (f g : R) (U : Opens (PrimeSpectrum.Top R)) (hu : ∀ x ∈ U, g ∈ (x : PrimeSpectrum.Top R).asIdeal.primeCompl) (x : U) (hx : g ∈ (x : PrimeSpectrum.Top R).asIdeal.primeCompl) : (const R f g U hu).1 x = IsLocalization.mk' _ f ⟨g, hx⟩ := rfl theorem exists_const (U) (s : (structureSheaf R).1.obj (op U)) (x : PrimeSpectrum.Top R) (hx : x ∈ U) : ∃ (V : Opens (PrimeSpectrum.Top R)) (_ : x ∈ V) (i : V ⟶ U) (f g : R) (hg : _), const R f g V hg = (structureSheaf R).1.map i.op s := let ⟨V, hxV, iVU, f, g, hfg⟩ := s.2 ⟨x, hx⟩ ⟨V, hxV, iVU, f, g, fun y hyV => (hfg ⟨y, hyV⟩).1, Subtype.eq <| funext fun y => IsLocalization.mk'_eq_iff_eq_mul.2 <| Eq.symm <| (hfg y).2⟩ @[simp] theorem res_const (f g : R) (U hu V hv i) : (structureSheaf R).1.map i (const R f g U hu) = const R f g V hv := rfl theorem res_const' (f g : R) (V hv) : (structureSheaf R).1.map (homOfLE hv).op (const R f g (PrimeSpectrum.basicOpen g) fun _ => id) = const R f g V hv := rfl theorem const_zero (f : R) (U hu) : const R 0 f U hu = 0 := Subtype.eq <| funext fun x => IsLocalization.mk'_eq_iff_eq_mul.2 <| by rw [RingHom.map_zero] exact (mul_eq_zero_of_left rfl ((algebraMap R (Localizations R x)) _)).symm theorem const_self (f : R) (U hu) : const R f f U hu = 1 := Subtype.eq <| funext fun _ => IsLocalization.mk'_self _ _ theorem const_one (U) : (const R 1 1 U fun _ _ => Submonoid.one_mem _) = 1 := const_self R 1 U _ theorem const_add (f₁ f₂ g₁ g₂ : R) (U hu₁ hu₂) : const R f₁ g₁ U hu₁ + const R f₂ g₂ U hu₂ = const R (f₁ * g₂ + f₂ * g₁) (g₁ * g₂) U fun x hx => Submonoid.mul_mem _ (hu₁ x hx) (hu₂ x hx) := Subtype.eq <| funext fun x => Eq.symm <| IsLocalization.mk'_add _ _ ⟨g₁, hu₁ x x.2⟩ ⟨g₂, hu₂ x x.2⟩ theorem const_mul (f₁ f₂ g₁ g₂ : R) (U hu₁ hu₂) : const R f₁ g₁ U hu₁ * const R f₂ g₂ U hu₂ = const R (f₁ * f₂) (g₁ * g₂) U fun x hx => Submonoid.mul_mem _ (hu₁ x hx) (hu₂ x hx) := Subtype.eq <| funext fun x => Eq.symm <| IsLocalization.mk'_mul _ f₁ f₂ ⟨g₁, hu₁ x x.2⟩ ⟨g₂, hu₂ x x.2⟩ theorem const_ext {f₁ f₂ g₁ g₂ : R} {U hu₁ hu₂} (h : f₁ * g₂ = f₂ * g₁) : const R f₁ g₁ U hu₁ = const R f₂ g₂ U hu₂ := Subtype.eq <| funext fun x => IsLocalization.mk'_eq_of_eq (by rw [mul_comm, Subtype.coe_mk, ← h, mul_comm, Subtype.coe_mk]) theorem const_congr {f₁ f₂ g₁ g₂ : R} {U hu} (hf : f₁ = f₂) (hg : g₁ = g₂) : const R f₁ g₁ U hu = const R f₂ g₂ U (hg ▸ hu) := by substs hf hg; rfl theorem const_mul_rev (f g : R) (U hu₁ hu₂) : const R f g U hu₁ * const R g f U hu₂ = 1 := by rw [const_mul, const_congr R rfl (mul_comm g f), const_self] theorem const_mul_cancel (f g₁ g₂ : R) (U hu₁ hu₂) : const R f g₁ U hu₁ * const R g₁ g₂ U hu₂ = const R f g₂ U hu₂ := by rw [const_mul, const_ext]; rw [mul_assoc] theorem const_mul_cancel' (f g₁ g₂ : R) (U hu₁ hu₂) : const R g₁ g₂ U hu₂ * const R f g₁ U hu₁ = const R f g₂ U hu₂ := by rw [mul_comm, const_mul_cancel] /-- The canonical ring homomorphism interpreting an element of `R` as a section of the structure sheaf. -/ def toOpen (U : Opens (PrimeSpectrum.Top R)) : CommRingCat.of R ⟶ (structureSheaf R).1.obj (op U) := CommRingCat.ofHom { toFun f := ⟨fun _ => algebraMap R _ f, fun x => ⟨U, x.2, 𝟙 _, f, 1, fun y => ⟨(Ideal.ne_top_iff_one _).1 y.1.2.1, by simp [RingHom.map_one, mul_one]⟩⟩⟩ map_one' := Subtype.eq <| funext fun _ => RingHom.map_one _ map_mul' _ _ := Subtype.eq <| funext fun _ => RingHom.map_mul _ _ _ map_zero' := Subtype.eq <| funext fun _ => RingHom.map_zero _ map_add' _ _ := Subtype.eq <| funext fun _ => RingHom.map_add _ _ _ } @[simp] theorem toOpen_res (U V : Opens (PrimeSpectrum.Top R)) (i : V ⟶ U) : toOpen R U ≫ (structureSheaf R).1.map i.op = toOpen R V := rfl @[simp] theorem toOpen_apply (U : Opens (PrimeSpectrum.Top R)) (f : R) (x : U) : (toOpen R U f).1 x = algebraMap _ _ f := rfl theorem toOpen_eq_const (U : Opens (PrimeSpectrum.Top R)) (f : R) : toOpen R U f = const R f 1 U fun x _ => (Ideal.ne_top_iff_one _).1 x.2.1 := Subtype.eq <| funext fun _ => Eq.symm <| IsLocalization.mk'_one _ f /-- The canonical ring homomorphism interpreting an element of `R` as an element of the stalk of `structureSheaf R` at `x`. -/ def toStalk (x : PrimeSpectrum.Top R) : CommRingCat.of R ⟶ (structureSheaf R).presheaf.stalk x := (toOpen R ⊤ ≫ (structureSheaf R).presheaf.germ _ x (by trivial)) @[simp] theorem toOpen_germ (U : Opens (PrimeSpectrum.Top R)) (x : PrimeSpectrum.Top R) (hx : x ∈ U) :
toOpen R U ≫ (structureSheaf R).presheaf.germ U x hx = toStalk R x := by rw [← toOpen_res R ⊤ U (homOfLE le_top : U ⟶ ⊤), Category.assoc, Presheaf.germ_res]; rfl
Mathlib/AlgebraicGeometry/StructureSheaf.lean
406
408
/- Copyright (c) 2020 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker -/ import Mathlib.Algebra.Polynomial.Eval.Defs import Mathlib.LinearAlgebra.Dimension.Constructions /-! # Linear recurrence Informally, a "linear recurrence" is an assertion of the form `∀ n : ℕ, u (n + d) = a 0 * u n + a 1 * u (n+1) + ... + a (d-1) * u (n+d-1)`, where `u` is a sequence, `d` is the *order* of the recurrence and the `a i` are its *coefficients*. In this file, we define the structure `LinearRecurrence` so that `LinearRecurrence.mk d a` represents the above relation, and we call a sequence `u` which verifies it a *solution* of the linear recurrence. We prove a few basic lemmas about this concept, such as : * the space of solutions is a submodule of `(ℕ → α)` (i.e a vector space if `α` is a field) * the function that maps a solution `u` to its first `d` terms builds a `LinearEquiv` between the solution space and `Fin d → α`, aka `α ^ d`. As a consequence, two solutions are equal if and only if their first `d` terms are equals. * a geometric sequence `q ^ n` is solution iff `q` is a root of a particular polynomial, which we call the *characteristic polynomial* of the recurrence Of course, although we can inductively generate solutions (cf `mkSol`), the interesting part would be to determinate closed-forms for the solutions. This is currently *not implemented*, as we are waiting for definition and properties of eigenvalues and eigenvectors. -/ noncomputable section open Finset open Polynomial /-- A "linear recurrence relation" over a commutative semiring is given by its order `n` and `n` coefficients. -/ structure LinearRecurrence (R : Type*) [CommSemiring R] where /-- Order of the linear recurrence -/ order : ℕ /-- Coefficients of the linear recurrence -/ coeffs : Fin order → R instance (R : Type*) [CommSemiring R] : Inhabited (LinearRecurrence R) := ⟨⟨0, default⟩⟩ namespace LinearRecurrence section CommSemiring variable {R : Type*} [CommSemiring R] (E : LinearRecurrence R) /-- We say that a sequence `u` is solution of `LinearRecurrence order coeffs` when we have `u (n + order) = ∑ i : Fin order, coeffs i * u (n + i)` for any `n`. -/ def IsSolution (u : ℕ → R) := ∀ n, u (n + E.order) = ∑ i, E.coeffs i * u (n + i) /-- A solution of a `LinearRecurrence` which satisfies certain initial conditions. We will prove this is the only such solution. -/ def mkSol (init : Fin E.order → R) : ℕ → R | n => if h : n < E.order then init ⟨n, h⟩ else ∑ k : Fin E.order, have _ : n - E.order + k < n := by omega E.coeffs k * mkSol init (n - E.order + k) /-- `E.mkSol` indeed gives solutions to `E`. -/ theorem is_sol_mkSol (init : Fin E.order → R) : E.IsSolution (E.mkSol init) := by intro n rw [mkSol] simp /-- `E.mkSol init`'s first `E.order` terms are `init`. -/ theorem mkSol_eq_init (init : Fin E.order → R) : ∀ n : Fin E.order, E.mkSol init n = init n := by intro n rw [mkSol] simp only [n.is_lt, dif_pos, Fin.mk_val, Fin.eta] /-- If `u` is a solution to `E` and `init` designates its first `E.order` values, then `∀ n, u n = E.mkSol init n`. -/ theorem eq_mk_of_is_sol_of_eq_init {u : ℕ → R} {init : Fin E.order → R} (h : E.IsSolution u) (heq : ∀ n : Fin E.order, u n = init n) : ∀ n, u n = E.mkSol init n := by intro n rw [mkSol] split_ifs with h' · exact mod_cast heq ⟨n, h'⟩ · dsimp only rw [← tsub_add_cancel_of_le (le_of_not_lt h'), h (n - E.order)] congr with k
have : n - E.order + k < n := by omega rw [eq_mk_of_is_sol_of_eq_init h heq (n - E.order + k)] simp /-- If `u` is a solution to `E` and `init` designates its first `E.order` values, then `u = E.mkSol init`. This proves that `E.mkSol init` is the only solution of `E` whose first `E.order` values are given by `init`. -/ theorem eq_mk_of_is_sol_of_eq_init' {u : ℕ → R} {init : Fin E.order → R} (h : E.IsSolution u) (heq : ∀ n : Fin E.order, u n = init n) : u = E.mkSol init := funext (E.eq_mk_of_is_sol_of_eq_init h heq) /-- The space of solutions of `E`, as a `Submodule` over `R` of the module `ℕ → R`. -/ def solSpace : Submodule R (ℕ → R) where carrier := { u | E.IsSolution u } zero_mem' n := by simp add_mem' {u v} hu hv n := by simp [mul_add, sum_add_distrib, hu n, hv n]
Mathlib/Algebra/LinearRecurrence.lean
100
115
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Kenny Lau, Johan Commelin, Mario Carneiro, Kevin Buzzard, Amelia Livingston, Yury Kudryashov -/ import Mathlib.Algebra.Group.Action.Faithful import Mathlib.Algebra.Group.Nat.Defs import Mathlib.Algebra.Group.Prod import Mathlib.Algebra.Group.Submonoid.Basic import Mathlib.Algebra.Group.Submonoid.MulAction import Mathlib.Algebra.Group.TypeTags.Basic /-! # Operations on `Submonoid`s In this file we define various operations on `Submonoid`s and `MonoidHom`s. ## Main definitions ### Conversion between multiplicative and additive definitions * `Submonoid.toAddSubmonoid`, `Submonoid.toAddSubmonoid'`, `AddSubmonoid.toSubmonoid`, `AddSubmonoid.toSubmonoid'`: convert between multiplicative and additive submonoids of `M`, `Multiplicative M`, and `Additive M`. These are stated as `OrderIso`s. ### (Commutative) monoid structure on a submonoid * `Submonoid.toMonoid`, `Submonoid.toCommMonoid`: a submonoid inherits a (commutative) monoid structure. ### Group actions by submonoids * `Submonoid.MulAction`, `Submonoid.DistribMulAction`: a submonoid inherits (distributive) multiplicative actions. ### Operations on submonoids * `Submonoid.comap`: preimage of a submonoid under a monoid homomorphism as a submonoid of the domain; * `Submonoid.map`: image of a submonoid under a monoid homomorphism as a submonoid of the codomain; * `Submonoid.prod`: product of two submonoids `s : Submonoid M` and `t : Submonoid N` as a submonoid of `M × N`; ### Monoid homomorphisms between submonoid * `Submonoid.subtype`: embedding of a submonoid into the ambient monoid. * `Submonoid.inclusion`: given two submonoids `S`, `T` such that `S ≤ T`, `S.inclusion T` is the inclusion of `S` into `T` as a monoid homomorphism; * `MulEquiv.submonoidCongr`: converts a proof of `S = T` into a monoid isomorphism between `S` and `T`. * `Submonoid.prodEquiv`: monoid isomorphism between `s.prod t` and `s × t`; ### Operations on `MonoidHom`s * `MonoidHom.mrange`: range of a monoid homomorphism as a submonoid of the codomain; * `MonoidHom.mker`: kernel of a monoid homomorphism as a submonoid of the domain; * `MonoidHom.restrict`: restrict a monoid homomorphism to a submonoid; * `MonoidHom.codRestrict`: restrict the codomain of a monoid homomorphism to a submonoid; * `MonoidHom.mrangeRestrict`: restrict a monoid homomorphism to its range; ## Tags submonoid, range, product, map, comap -/ assert_not_exists MonoidWithZero open Function variable {M N P : Type*} [MulOneClass M] [MulOneClass N] [MulOneClass P] (S : Submonoid M) /-! ### Conversion to/from `Additive`/`Multiplicative` -/ section /-- Submonoids of monoid `M` are isomorphic to additive submonoids of `Additive M`. -/ @[simps] def Submonoid.toAddSubmonoid : Submonoid M ≃o AddSubmonoid (Additive M) where toFun S := { carrier := Additive.toMul ⁻¹' S zero_mem' := S.one_mem' add_mem' := fun ha hb => S.mul_mem' ha hb } invFun S := { carrier := Additive.ofMul ⁻¹' S one_mem' := S.zero_mem' mul_mem' := fun ha hb => S.add_mem' ha hb} left_inv x := by cases x; rfl right_inv x := by cases x; rfl map_rel_iff' := Iff.rfl /-- Additive submonoids of an additive monoid `Additive M` are isomorphic to submonoids of `M`. -/ abbrev AddSubmonoid.toSubmonoid' : AddSubmonoid (Additive M) ≃o Submonoid M := Submonoid.toAddSubmonoid.symm theorem Submonoid.toAddSubmonoid_closure (S : Set M) : Submonoid.toAddSubmonoid (Submonoid.closure S) = AddSubmonoid.closure (Additive.toMul ⁻¹' S) := le_antisymm (Submonoid.toAddSubmonoid.le_symm_apply.1 <| Submonoid.closure_le.2 (AddSubmonoid.subset_closure (M := Additive M))) (AddSubmonoid.closure_le.2 <| Submonoid.subset_closure (M := M)) theorem AddSubmonoid.toSubmonoid'_closure (S : Set (Additive M)) : AddSubmonoid.toSubmonoid' (AddSubmonoid.closure S) = Submonoid.closure (Additive.ofMul ⁻¹' S) := le_antisymm (AddSubmonoid.toSubmonoid'.le_symm_apply.1 <| AddSubmonoid.closure_le.2 (Submonoid.subset_closure (M := M))) (Submonoid.closure_le.2 <| AddSubmonoid.subset_closure (M := Additive M)) end section variable {A : Type*} [AddZeroClass A] /-- Additive submonoids of an additive monoid `A` are isomorphic to multiplicative submonoids of `Multiplicative A`. -/ @[simps] def AddSubmonoid.toSubmonoid : AddSubmonoid A ≃o Submonoid (Multiplicative A) where toFun S := { carrier := Multiplicative.toAdd ⁻¹' S one_mem' := S.zero_mem' mul_mem' := fun ha hb => S.add_mem' ha hb } invFun S := { carrier := Multiplicative.ofAdd ⁻¹' S zero_mem' := S.one_mem' add_mem' := fun ha hb => S.mul_mem' ha hb} left_inv x := by cases x; rfl right_inv x := by cases x; rfl map_rel_iff' := Iff.rfl /-- Submonoids of a monoid `Multiplicative A` are isomorphic to additive submonoids of `A`. -/ abbrev Submonoid.toAddSubmonoid' : Submonoid (Multiplicative A) ≃o AddSubmonoid A := AddSubmonoid.toSubmonoid.symm theorem AddSubmonoid.toSubmonoid_closure (S : Set A) : (AddSubmonoid.toSubmonoid) (AddSubmonoid.closure S) = Submonoid.closure (Multiplicative.toAdd ⁻¹' S) := le_antisymm (AddSubmonoid.toSubmonoid.to_galoisConnection.l_le <| AddSubmonoid.closure_le.2 <| Submonoid.subset_closure (M := Multiplicative A)) (Submonoid.closure_le.2 <| AddSubmonoid.subset_closure (M := A)) theorem Submonoid.toAddSubmonoid'_closure (S : Set (Multiplicative A)) : Submonoid.toAddSubmonoid' (Submonoid.closure S) = AddSubmonoid.closure (Multiplicative.ofAdd ⁻¹' S) := le_antisymm (Submonoid.toAddSubmonoid'.to_galoisConnection.l_le <| Submonoid.closure_le.2 <| AddSubmonoid.subset_closure (M := A)) (AddSubmonoid.closure_le.2 <| Submonoid.subset_closure (M := Multiplicative A)) end namespace Submonoid variable {F : Type*} [FunLike F M N] [mc : MonoidHomClass F M N] open Set /-! ### `comap` and `map` -/ /-- The preimage of a submonoid along a monoid homomorphism is a submonoid. -/ @[to_additive "The preimage of an `AddSubmonoid` along an `AddMonoid` homomorphism is an `AddSubmonoid`."] def comap (f : F) (S : Submonoid N) : Submonoid M where carrier := f ⁻¹' S one_mem' := show f 1 ∈ S by rw [map_one]; exact S.one_mem mul_mem' ha hb := show f (_ * _) ∈ S by rw [map_mul]; exact S.mul_mem ha hb @[to_additive (attr := simp)] theorem coe_comap (S : Submonoid N) (f : F) : (S.comap f : Set M) = f ⁻¹' S := rfl @[to_additive (attr := simp)] theorem mem_comap {S : Submonoid N} {f : F} {x : M} : x ∈ S.comap f ↔ f x ∈ S := Iff.rfl @[to_additive] theorem comap_comap (S : Submonoid P) (g : N →* P) (f : M →* N) : (S.comap g).comap f = S.comap (g.comp f) := rfl @[to_additive (attr := simp)] theorem comap_id (S : Submonoid P) : S.comap (MonoidHom.id P) = S := ext (by simp) /-- The image of a submonoid along a monoid homomorphism is a submonoid. -/ @[to_additive "The image of an `AddSubmonoid` along an `AddMonoid` homomorphism is an `AddSubmonoid`."] def map (f : F) (S : Submonoid M) : Submonoid N where carrier := f '' S one_mem' := ⟨1, S.one_mem, map_one f⟩ mul_mem' := by rintro _ _ ⟨x, hx, rfl⟩ ⟨y, hy, rfl⟩ exact ⟨x * y, S.mul_mem hx hy, by rw [map_mul]⟩ @[to_additive (attr := simp)] theorem coe_map (f : F) (S : Submonoid M) : (S.map f : Set N) = f '' S := rfl @[to_additive (attr := simp)] theorem map_coe_toMonoidHom (f : F) (S : Submonoid M) : S.map (f : M →* N) = S.map f := rfl @[to_additive (attr := simp)] theorem map_coe_toMulEquiv {F} [EquivLike F M N] [MulEquivClass F M N] (f : F) (S : Submonoid M) : S.map (f : M ≃* N) = S.map f := rfl @[to_additive (attr := simp)] theorem mem_map {f : F} {S : Submonoid M} {y : N} : y ∈ S.map f ↔ ∃ x ∈ S, f x = y := Iff.rfl @[to_additive] theorem mem_map_of_mem (f : F) {S : Submonoid M} {x : M} (hx : x ∈ S) : f x ∈ S.map f := mem_image_of_mem f hx @[to_additive] theorem apply_coe_mem_map (f : F) (S : Submonoid M) (x : S) : f x ∈ S.map f := mem_map_of_mem f x.2 @[to_additive] theorem map_map (g : N →* P) (f : M →* N) : (S.map f).map g = S.map (g.comp f) := SetLike.coe_injective <| image_image _ _ _ -- The simpNF linter says that the LHS can be simplified via `Submonoid.mem_map`. -- However this is a higher priority lemma. -- It seems the side condition `hf` is not applied by `simpNF`. -- https://github.com/leanprover/std4/issues/207 @[to_additive (attr := simp 1100, nolint simpNF)] theorem mem_map_iff_mem {f : F} (hf : Function.Injective f) {S : Submonoid M} {x : M} : f x ∈ S.map f ↔ x ∈ S := hf.mem_set_image @[to_additive] theorem map_le_iff_le_comap {f : F} {S : Submonoid M} {T : Submonoid N} : S.map f ≤ T ↔ S ≤ T.comap f := image_subset_iff @[to_additive] theorem gc_map_comap (f : F) : GaloisConnection (map f) (comap f) := fun _ _ => map_le_iff_le_comap @[to_additive] theorem map_le_of_le_comap {T : Submonoid N} {f : F} : S ≤ T.comap f → S.map f ≤ T := (gc_map_comap f).l_le @[to_additive] theorem le_comap_of_map_le {T : Submonoid N} {f : F} : S.map f ≤ T → S ≤ T.comap f := (gc_map_comap f).le_u @[to_additive] theorem le_comap_map {f : F} : S ≤ (S.map f).comap f := (gc_map_comap f).le_u_l _ @[to_additive] theorem map_comap_le {S : Submonoid N} {f : F} : (S.comap f).map f ≤ S := (gc_map_comap f).l_u_le _ @[to_additive] theorem monotone_map {f : F} : Monotone (map f) := (gc_map_comap f).monotone_l @[to_additive] theorem monotone_comap {f : F} : Monotone (comap f) := (gc_map_comap f).monotone_u @[to_additive (attr := simp)] theorem map_comap_map {f : F} : ((S.map f).comap f).map f = S.map f := (gc_map_comap f).l_u_l_eq_l _ @[to_additive (attr := simp)] theorem comap_map_comap {S : Submonoid N} {f : F} : ((S.comap f).map f).comap f = S.comap f := (gc_map_comap f).u_l_u_eq_u _ @[to_additive] theorem map_sup (S T : Submonoid M) (f : F) : (S ⊔ T).map f = S.map f ⊔ T.map f := (gc_map_comap f : GaloisConnection (map f) (comap f)).l_sup @[to_additive] theorem map_iSup {ι : Sort*} (f : F) (s : ι → Submonoid M) : (iSup s).map f = ⨆ i, (s i).map f := (gc_map_comap f : GaloisConnection (map f) (comap f)).l_iSup @[to_additive] theorem map_inf (S T : Submonoid M) (f : F) (hf : Function.Injective f) : (S ⊓ T).map f = S.map f ⊓ T.map f := SetLike.coe_injective (Set.image_inter hf) @[to_additive] theorem map_iInf {ι : Sort*} [Nonempty ι] (f : F) (hf : Function.Injective f) (s : ι → Submonoid M) : (iInf s).map f = ⨅ i, (s i).map f := by apply SetLike.coe_injective simpa using (Set.injOn_of_injective hf).image_iInter_eq (s := SetLike.coe ∘ s) @[to_additive] theorem comap_inf (S T : Submonoid N) (f : F) : (S ⊓ T).comap f = S.comap f ⊓ T.comap f := (gc_map_comap f : GaloisConnection (map f) (comap f)).u_inf @[to_additive] theorem comap_iInf {ι : Sort*} (f : F) (s : ι → Submonoid N) : (iInf s).comap f = ⨅ i, (s i).comap f := (gc_map_comap f : GaloisConnection (map f) (comap f)).u_iInf @[to_additive (attr := simp)] theorem map_bot (f : F) : (⊥ : Submonoid M).map f = ⊥ := (gc_map_comap f).l_bot @[to_additive (attr := simp)] theorem comap_top (f : F) : (⊤ : Submonoid N).comap f = ⊤ := (gc_map_comap f).u_top @[to_additive (attr := simp)] theorem map_id (S : Submonoid M) : S.map (MonoidHom.id M) = S := ext fun _ => ⟨fun ⟨_, h, rfl⟩ => h, fun h => ⟨_, h, rfl⟩⟩ section GaloisCoinsertion variable {ι : Type*} {f : F} /-- `map f` and `comap f` form a `GaloisCoinsertion` when `f` is injective. -/ @[to_additive "`map f` and `comap f` form a `GaloisCoinsertion` when `f` is injective."] def gciMapComap (hf : Function.Injective f) : GaloisCoinsertion (map f) (comap f) := (gc_map_comap f).toGaloisCoinsertion fun S x => by simp [mem_comap, mem_map, hf.eq_iff] variable (hf : Function.Injective f) include hf @[to_additive] theorem comap_map_eq_of_injective (S : Submonoid M) : (S.map f).comap f = S := (gciMapComap hf).u_l_eq _ @[to_additive] theorem comap_surjective_of_injective : Function.Surjective (comap f) := (gciMapComap hf).u_surjective @[to_additive] theorem map_injective_of_injective : Function.Injective (map f) := (gciMapComap hf).l_injective @[to_additive] theorem comap_inf_map_of_injective (S T : Submonoid M) : (S.map f ⊓ T.map f).comap f = S ⊓ T := (gciMapComap hf).u_inf_l _ _ @[to_additive] theorem comap_iInf_map_of_injective (S : ι → Submonoid M) : (⨅ i, (S i).map f).comap f = iInf S := (gciMapComap hf).u_iInf_l _ @[to_additive] theorem comap_sup_map_of_injective (S T : Submonoid M) : (S.map f ⊔ T.map f).comap f = S ⊔ T := (gciMapComap hf).u_sup_l _ _ @[to_additive] theorem comap_iSup_map_of_injective (S : ι → Submonoid M) : (⨆ i, (S i).map f).comap f = iSup S := (gciMapComap hf).u_iSup_l _ @[to_additive] theorem map_le_map_iff_of_injective {S T : Submonoid M} : S.map f ≤ T.map f ↔ S ≤ T := (gciMapComap hf).l_le_l_iff @[to_additive] theorem map_strictMono_of_injective : StrictMono (map f) := (gciMapComap hf).strictMono_l end GaloisCoinsertion section GaloisInsertion variable {ι : Type*} {f : F} /-- `map f` and `comap f` form a `GaloisInsertion` when `f` is surjective. -/ @[to_additive "`map f` and `comap f` form a `GaloisInsertion` when `f` is surjective."] def giMapComap (hf : Function.Surjective f) : GaloisInsertion (map f) (comap f) := (gc_map_comap f).toGaloisInsertion fun S x h => let ⟨y, hy⟩ := hf x mem_map.2 ⟨y, by simp [hy, h]⟩ variable (hf : Function.Surjective f) include hf @[to_additive] theorem map_comap_eq_of_surjective (S : Submonoid N) : (S.comap f).map f = S := (giMapComap hf).l_u_eq _ @[to_additive] theorem map_surjective_of_surjective : Function.Surjective (map f) := (giMapComap hf).l_surjective @[to_additive] theorem comap_injective_of_surjective : Function.Injective (comap f) := (giMapComap hf).u_injective @[to_additive] theorem map_inf_comap_of_surjective (S T : Submonoid N) : (S.comap f ⊓ T.comap f).map f = S ⊓ T := (giMapComap hf).l_inf_u _ _ @[to_additive] theorem map_iInf_comap_of_surjective (S : ι → Submonoid N) : (⨅ i, (S i).comap f).map f = iInf S := (giMapComap hf).l_iInf_u _ @[to_additive] theorem map_sup_comap_of_surjective (S T : Submonoid N) : (S.comap f ⊔ T.comap f).map f = S ⊔ T := (giMapComap hf).l_sup_u _ _ @[to_additive] theorem map_iSup_comap_of_surjective (S : ι → Submonoid N) : (⨆ i, (S i).comap f).map f = iSup S := (giMapComap hf).l_iSup_u _ @[to_additive] theorem comap_le_comap_iff_of_surjective {S T : Submonoid N} : S.comap f ≤ T.comap f ↔ S ≤ T := (giMapComap hf).u_le_u_iff @[to_additive] theorem comap_strictMono_of_surjective : StrictMono (comap f) := (giMapComap hf).strictMono_u end GaloisInsertion variable {M : Type*} [MulOneClass M] (S : Submonoid M) /-- The top submonoid is isomorphic to the monoid. -/ @[to_additive (attr := simps) "The top additive submonoid is isomorphic to the additive monoid."] def topEquiv : (⊤ : Submonoid M) ≃* M where toFun x := x invFun x := ⟨x, mem_top x⟩ left_inv x := x.eta _ right_inv _ := rfl map_mul' _ _ := rfl @[to_additive (attr := simp)] theorem topEquiv_toMonoidHom : ((topEquiv : _ ≃* M) : _ →* M) = (⊤ : Submonoid M).subtype := rfl /-- A subgroup is isomorphic to its image under an injective function. If you have an isomorphism, use `MulEquiv.submonoidMap` for better definitional equalities. -/ @[to_additive "An additive subgroup is isomorphic to its image under an injective function. If you have an isomorphism, use `AddEquiv.addSubmonoidMap` for better definitional equalities."] noncomputable def equivMapOfInjective (f : M →* N) (hf : Function.Injective f) : S ≃* S.map f := { Equiv.Set.image f S hf with map_mul' := fun _ _ => Subtype.ext (f.map_mul _ _) } @[to_additive (attr := simp)] theorem coe_equivMapOfInjective_apply (f : M →* N) (hf : Function.Injective f) (x : S) : (equivMapOfInjective S f hf x : N) = f x := rfl @[to_additive (attr := simp)] theorem closure_closure_coe_preimage {s : Set M} : closure (((↑) : closure s → M) ⁻¹' s) = ⊤ := eq_top_iff.2 fun x _ ↦ Subtype.recOn x fun _ hx' ↦ closure_induction (fun _ h ↦ subset_closure h) (one_mem _) (fun _ _ _ _ ↦ mul_mem) hx' /-- Given submonoids `s`, `t` of monoids `M`, `N` respectively, `s × t` as a submonoid of `M × N`. -/ @[to_additive prod "Given `AddSubmonoid`s `s`, `t` of `AddMonoid`s `A`, `B` respectively, `s × t` as an `AddSubmonoid` of `A × B`."] def prod (s : Submonoid M) (t : Submonoid N) : Submonoid (M × N) where carrier := s ×ˢ t one_mem' := ⟨s.one_mem, t.one_mem⟩ mul_mem' hp hq := ⟨s.mul_mem hp.1 hq.1, t.mul_mem hp.2 hq.2⟩ @[to_additive coe_prod] theorem coe_prod (s : Submonoid M) (t : Submonoid N) : (s.prod t : Set (M × N)) = (s : Set M) ×ˢ (t : Set N) := rfl @[to_additive mem_prod] theorem mem_prod {s : Submonoid M} {t : Submonoid N} {p : M × N} : p ∈ s.prod t ↔ p.1 ∈ s ∧ p.2 ∈ t := Iff.rfl @[to_additive prod_mono] theorem prod_mono {s₁ s₂ : Submonoid M} {t₁ t₂ : Submonoid N} (hs : s₁ ≤ s₂) (ht : t₁ ≤ t₂) : s₁.prod t₁ ≤ s₂.prod t₂ := Set.prod_mono hs ht @[to_additive prod_top] theorem prod_top (s : Submonoid M) : s.prod (⊤ : Submonoid N) = s.comap (MonoidHom.fst M N) := ext fun x => by simp [mem_prod, MonoidHom.coe_fst] @[to_additive top_prod] theorem top_prod (s : Submonoid N) : (⊤ : Submonoid M).prod s = s.comap (MonoidHom.snd M N) := ext fun x => by simp [mem_prod, MonoidHom.coe_snd] @[to_additive (attr := simp) top_prod_top] theorem top_prod_top : (⊤ : Submonoid M).prod (⊤ : Submonoid N) = ⊤ := (top_prod _).trans <| comap_top _ @[to_additive bot_prod_bot] theorem bot_prod_bot : (⊥ : Submonoid M).prod (⊥ : Submonoid N) = ⊥ := SetLike.coe_injective <| by simp [coe_prod] /-- The product of submonoids is isomorphic to their product as monoids. -/ @[to_additive prodEquiv "The product of additive submonoids is isomorphic to their product as additive monoids"] def prodEquiv (s : Submonoid M) (t : Submonoid N) : s.prod t ≃* s × t := { (Equiv.Set.prod (s : Set M) (t : Set N)) with map_mul' := fun _ _ => rfl } open MonoidHom @[to_additive] theorem map_inl (s : Submonoid M) : s.map (inl M N) = s.prod ⊥ := ext fun p => ⟨fun ⟨_, hx, hp⟩ => hp ▸ ⟨hx, Set.mem_singleton 1⟩, fun ⟨hps, hp1⟩ => ⟨p.1, hps, Prod.ext rfl <| (Set.eq_of_mem_singleton hp1).symm⟩⟩ @[to_additive] theorem map_inr (s : Submonoid N) : s.map (inr M N) = prod ⊥ s := ext fun p => ⟨fun ⟨_, hx, hp⟩ => hp ▸ ⟨Set.mem_singleton 1, hx⟩, fun ⟨hp1, hps⟩ => ⟨p.2, hps, Prod.ext (Set.eq_of_mem_singleton hp1).symm rfl⟩⟩ @[to_additive (attr := simp) prod_bot_sup_bot_prod] theorem prod_bot_sup_bot_prod (s : Submonoid M) (t : Submonoid N) : (prod s ⊥) ⊔ (prod ⊥ t) = prod s t := (le_antisymm (sup_le (prod_mono (le_refl s) bot_le) (prod_mono bot_le (le_refl t)))) fun p hp => Prod.fst_mul_snd p ▸ mul_mem ((le_sup_left : prod s ⊥ ≤ prod s ⊥ ⊔ prod ⊥ t) ⟨hp.1, Set.mem_singleton 1⟩) ((le_sup_right : prod ⊥ t ≤ prod s ⊥ ⊔ prod ⊥ t) ⟨Set.mem_singleton 1, hp.2⟩) @[to_additive] theorem mem_map_equiv {f : M ≃* N} {K : Submonoid M} {x : N} : x ∈ K.map f.toMonoidHom ↔ f.symm x ∈ K := Set.mem_image_equiv @[to_additive] theorem map_equiv_eq_comap_symm (f : M ≃* N) (K : Submonoid M) : K.map f = K.comap f.symm := SetLike.coe_injective (f.toEquiv.image_eq_preimage K) @[to_additive] theorem comap_equiv_eq_map_symm (f : N ≃* M) (K : Submonoid M) : K.comap f = K.map f.symm := (map_equiv_eq_comap_symm f.symm K).symm @[to_additive (attr := simp)] theorem map_equiv_top (f : M ≃* N) : (⊤ : Submonoid M).map f = ⊤ := SetLike.coe_injective <| Set.image_univ.trans f.surjective.range_eq @[to_additive le_prod_iff] theorem le_prod_iff {s : Submonoid M} {t : Submonoid N} {u : Submonoid (M × N)} : u ≤ s.prod t ↔ u.map (fst M N) ≤ s ∧ u.map (snd M N) ≤ t := by constructor · intro h constructor · rintro x ⟨⟨y1, y2⟩, ⟨hy1, rfl⟩⟩ exact (h hy1).1 · rintro x ⟨⟨y1, y2⟩, ⟨hy1, rfl⟩⟩ exact (h hy1).2 · rintro ⟨hH, hK⟩ ⟨x1, x2⟩ h exact ⟨hH ⟨_, h, rfl⟩, hK ⟨_, h, rfl⟩⟩ @[to_additive prod_le_iff] theorem prod_le_iff {s : Submonoid M} {t : Submonoid N} {u : Submonoid (M × N)} : s.prod t ≤ u ↔ s.map (inl M N) ≤ u ∧ t.map (inr M N) ≤ u := by constructor · intro h constructor · rintro _ ⟨x, hx, rfl⟩ apply h exact ⟨hx, Submonoid.one_mem _⟩ · rintro _ ⟨x, hx, rfl⟩ apply h exact ⟨Submonoid.one_mem _, hx⟩ · rintro ⟨hH, hK⟩ ⟨x1, x2⟩ ⟨h1, h2⟩ have h1' : inl M N x1 ∈ u := by apply hH simpa using h1 have h2' : inr M N x2 ∈ u := by apply hK simpa using h2 simpa using Submonoid.mul_mem _ h1' h2' @[to_additive closure_prod] theorem closure_prod {s : Set M} {t : Set N} (hs : 1 ∈ s) (ht : 1 ∈ t) : closure (s ×ˢ t) = (closure s).prod (closure t) := le_antisymm (closure_le.2 <| Set.prod_subset_prod_iff.2 <| .inl ⟨subset_closure, subset_closure⟩) (prod_le_iff.2 ⟨ map_le_of_le_comap _ <| closure_le.2 fun _x hx => subset_closure ⟨hx, ht⟩, map_le_of_le_comap _ <| closure_le.2 fun _y hy => subset_closure ⟨hs, hy⟩⟩) @[to_additive (attr := simp) closure_prod_zero] lemma closure_prod_one (s : Set M) : closure (s ×ˢ ({1} : Set N)) = (closure s).prod ⊥ := le_antisymm (closure_le.2 <| Set.prod_subset_prod_iff.2 <| .inl ⟨subset_closure, .rfl⟩) (prod_le_iff.2 ⟨ map_le_of_le_comap _ <| closure_le.2 fun _x hx => subset_closure ⟨hx, rfl⟩, by simp⟩) @[to_additive (attr := simp) closure_zero_prod] lemma closure_one_prod (t : Set N) : closure (({1} : Set M) ×ˢ t) = .prod ⊥ (closure t) := le_antisymm (closure_le.2 <| Set.prod_subset_prod_iff.2 <| .inl ⟨.rfl, subset_closure⟩) (prod_le_iff.2 ⟨by simp, map_le_of_le_comap _ <| closure_le.2 fun _y hy => subset_closure ⟨rfl, hy⟩⟩) end Submonoid namespace MonoidHom variable {F : Type*} [FunLike F M N] [mc : MonoidHomClass F M N] open Submonoid library_note "range copy pattern"/-- For many categories (monoids, modules, rings, ...) the set-theoretic image of a morphism `f` is a subobject of the codomain. When this is the case, it is useful to define the range of a morphism in such a way that the underlying carrier set of the range subobject is definitionally `Set.range f`. In particular this means that the types `↥(Set.range f)` and `↥f.range` are interchangeable without proof obligations. A convenient candidate definition for range which is mathematically correct is `map ⊤ f`, just as `Set.range` could have been defined as `f '' Set.univ`. However, this lacks the desired definitional convenience, in that it both does not match `Set.range`, and that it introduces a redundant `x ∈ ⊤` term which clutters proofs. In such a case one may resort to the `copy` pattern. A `copy` function converts the definitional problem for the carrier set of a subobject into a one-off propositional proof obligation which one discharges while writing the definition of the definitionally convenient range (the parameter `hs` in the example below). A good example is the case of a morphism of monoids. A convenient definition for `MonoidHom.mrange` would be `(⊤ : Submonoid M).map f`. However since this lacks the required definitional convenience, we first define `Submonoid.copy` as follows: ```lean protected def copy (S : Submonoid M) (s : Set M) (hs : s = S) : Submonoid M := { carrier := s, one_mem' := hs.symm ▸ S.one_mem', mul_mem' := hs.symm ▸ S.mul_mem' } ``` and then finally define: ```lean def mrange (f : M →* N) : Submonoid N := ((⊤ : Submonoid M).map f).copy (Set.range f) Set.image_univ.symm ``` -/ /-- The range of a monoid homomorphism is a submonoid. See Note [range copy pattern]. -/ @[to_additive "The range of an `AddMonoidHom` is an `AddSubmonoid`."] def mrange (f : F) : Submonoid N := ((⊤ : Submonoid M).map f).copy (Set.range f) Set.image_univ.symm @[to_additive (attr := simp)] theorem coe_mrange (f : F) : (mrange f : Set N) = Set.range f := rfl @[to_additive (attr := simp)] theorem mem_mrange {f : F} {y : N} : y ∈ mrange f ↔ ∃ x, f x = y := Iff.rfl @[to_additive] lemma mrange_comp {O : Type*} [MulOneClass O] (f : N →* O) (g : M →* N) : mrange (f.comp g) = (mrange g).map f := SetLike.coe_injective <| Set.range_comp f _ @[to_additive] theorem mrange_eq_map (f : F) : mrange f = (⊤ : Submonoid M).map f := Submonoid.copy_eq _ @[to_additive (attr := simp)] theorem mrange_id : mrange (MonoidHom.id M) = ⊤ := by simp [mrange_eq_map] @[to_additive] theorem map_mrange (g : N →* P) (f : M →* N) : (mrange f).map g = mrange (comp g f) := by simpa only [mrange_eq_map] using (⊤ : Submonoid M).map_map g f @[to_additive] theorem mrange_eq_top {f : F} : mrange f = (⊤ : Submonoid N) ↔ Surjective f := SetLike.ext'_iff.trans <| Iff.trans (by rw [coe_mrange, coe_top]) Set.range_eq_univ @[deprecated (since := "2024-11-11")] alias mrange_top_iff_surjective := mrange_eq_top /-- The range of a surjective monoid hom is the whole of the codomain. -/ @[to_additive (attr := simp) "The range of a surjective `AddMonoid` hom is the whole of the codomain."] theorem mrange_eq_top_of_surjective (f : F) (hf : Function.Surjective f) : mrange f = (⊤ : Submonoid N) := mrange_eq_top.2 hf @[deprecated (since := "2024-11-11")] alias mrange_top_of_surjective := mrange_eq_top_of_surjective @[to_additive] theorem mclosure_preimage_le (f : F) (s : Set N) : closure (f ⁻¹' s) ≤ (closure s).comap f := closure_le.2 fun _ hx => SetLike.mem_coe.2 <| mem_comap.2 <| subset_closure hx /-- The image under a monoid hom of the submonoid generated by a set equals the submonoid generated by the image of the set. -/ @[to_additive "The image under an `AddMonoid` hom of the `AddSubmonoid` generated by a set equals the `AddSubmonoid` generated by the image of the set."] theorem map_mclosure (f : F) (s : Set M) : (closure s).map f = closure (f '' s) := Set.image_preimage.l_comm_of_u_comm (gc_map_comap f) (Submonoid.gi N).gc (Submonoid.gi M).gc fun _ ↦ rfl @[to_additive (attr := simp)] theorem mclosure_range (f : F) : closure (Set.range f) = mrange f := by rw [← Set.image_univ, ← map_mclosure, mrange_eq_map, closure_univ] /-- Restriction of a monoid hom to a submonoid of the domain. -/ @[to_additive "Restriction of an `AddMonoid` hom to an `AddSubmonoid` of the domain."] def restrict {N S : Type*} [MulOneClass N] [SetLike S M] [SubmonoidClass S M] (f : M →* N) (s : S) : s →* N := f.comp (SubmonoidClass.subtype _) @[to_additive (attr := simp)] theorem restrict_apply {N S : Type*} [MulOneClass N] [SetLike S M] [SubmonoidClass S M] (f : M →* N) (s : S) (x : s) : f.restrict s x = f x := rfl @[to_additive (attr := simp)] theorem restrict_mrange (f : M →* N) : mrange (f.restrict S) = S.map f := by simp [SetLike.ext_iff] /-- Restriction of a monoid hom to a submonoid of the codomain. -/ @[to_additive (attr := simps apply) "Restriction of an `AddMonoid` hom to an `AddSubmonoid` of the codomain."] def codRestrict {S} [SetLike S N] [SubmonoidClass S N] (f : M →* N) (s : S) (h : ∀ x, f x ∈ s) : M →* s where toFun n := ⟨f n, h n⟩ map_one' := Subtype.eq f.map_one map_mul' x y := Subtype.eq (f.map_mul x y) @[to_additive (attr := simp)] lemma injective_codRestrict {S} [SetLike S N] [SubmonoidClass S N] (f : M →* N) (s : S) (h : ∀ x, f x ∈ s) : Function.Injective (f.codRestrict s h) ↔ Function.Injective f := ⟨fun H _ _ hxy ↦ H <| Subtype.eq hxy, fun H _ _ hxy ↦ H (congr_arg Subtype.val hxy)⟩ /-- Restriction of a monoid hom to its range interpreted as a submonoid. -/ @[to_additive "Restriction of an `AddMonoid` hom to its range interpreted as a submonoid."] def mrangeRestrict {N} [MulOneClass N] (f : M →* N) : M →* (mrange f) := (f.codRestrict (mrange f)) fun x => ⟨x, rfl⟩ @[to_additive (attr := simp)] theorem coe_mrangeRestrict {N} [MulOneClass N] (f : M →* N) (x : M) : (f.mrangeRestrict x : N) = f x := rfl @[to_additive] theorem mrangeRestrict_surjective (f : M →* N) : Function.Surjective f.mrangeRestrict := fun ⟨_, ⟨x, rfl⟩⟩ => ⟨x, rfl⟩ /-- The multiplicative kernel of a monoid hom is the submonoid of elements `x : G` such that `f x = 1` -/ @[to_additive "The additive kernel of an `AddMonoid` hom is the `AddSubmonoid` of elements such that `f x = 0`"] def mker (f : F) : Submonoid M := (⊥ : Submonoid N).comap f @[to_additive (attr := simp)] theorem mem_mker {f : F} {x : M} : x ∈ mker f ↔ f x = 1 := Iff.rfl @[to_additive] theorem coe_mker (f : F) : (mker f : Set M) = (f : M → N) ⁻¹' {1} := rfl @[to_additive] instance decidableMemMker [DecidableEq N] (f : F) : DecidablePred (· ∈ mker f) := fun x => decidable_of_iff (f x = 1) mem_mker @[to_additive] theorem comap_mker (g : N →* P) (f : M →* N) : (mker g).comap f = mker (comp g f) := rfl @[to_additive (attr := simp)] theorem comap_bot' (f : F) : (⊥ : Submonoid N).comap f = mker f := rfl @[to_additive (attr := simp)] theorem restrict_mker (f : M →* N) : mker (f.restrict S) = (MonoidHom.mker f).comap S.subtype := rfl @[to_additive] theorem mrangeRestrict_mker (f : M →* N) : mker (mrangeRestrict f) = mker f := by ext x change (⟨f x, _⟩ : mrange f) = ⟨1, _⟩ ↔ f x = 1 simp @[to_additive (attr := simp)] theorem mker_one : mker (1 : M →* N) = ⊤ := by ext simp [mem_mker] @[to_additive prod_map_comap_prod'] theorem prod_map_comap_prod' {M' : Type*} {N' : Type*} [MulOneClass M'] [MulOneClass N'] (f : M →* N) (g : M' →* N') (S : Submonoid N) (S' : Submonoid N') : (S.prod S').comap (prodMap f g) = (S.comap f).prod (S'.comap g) := SetLike.coe_injective <| Set.preimage_prod_map_prod f g _ _ @[to_additive mker_prod_map] theorem mker_prod_map {M' : Type*} {N' : Type*} [MulOneClass M'] [MulOneClass N'] (f : M →* N) (g : M' →* N') : mker (prodMap f g) = (mker f).prod (mker g) := by rw [← comap_bot', ← comap_bot', ← comap_bot', ← prod_map_comap_prod', bot_prod_bot] @[to_additive (attr := simp)] theorem mker_inl : mker (inl M N) = ⊥ := by ext x simp [mem_mker] @[to_additive (attr := simp)] theorem mker_inr : mker (inr M N) = ⊥ := by ext x simp [mem_mker] @[to_additive (attr := simp)] lemma mker_fst : mker (fst M N) = .prod ⊥ ⊤ := SetLike.ext fun _ => (iff_of_eq (and_true _)).symm @[to_additive (attr := simp)] lemma mker_snd : mker (snd M N) = .prod ⊤ ⊥ := SetLike.ext fun _ => (iff_of_eq (true_and _)).symm /-- The `MonoidHom` from the preimage of a submonoid to itself. -/ @[to_additive (attr := simps) "the `AddMonoidHom` from the preimage of an additive submonoid to itself."] def submonoidComap (f : M →* N) (N' : Submonoid N) : N'.comap f →* N' where toFun x := ⟨f x, x.2⟩ map_one' := Subtype.eq f.map_one map_mul' x y := Subtype.eq (f.map_mul x y) @[to_additive] lemma submonoidComap_surjective_of_surjective (f : M →* N) (N' : Submonoid N) (hf : Surjective f) : Surjective (f.submonoidComap N') := fun y ↦ by obtain ⟨x, hx⟩ := hf y use ⟨x, mem_comap.mpr (hx ▸ y.2)⟩ apply Subtype.val_injective simp [hx] /-- The `MonoidHom` from a submonoid to its image. See `MulEquiv.SubmonoidMap` for a variant for `MulEquiv`s. -/ @[to_additive (attr := simps) "the `AddMonoidHom` from an additive submonoid to its image. See `AddEquiv.AddSubmonoidMap` for a variant for `AddEquiv`s."] def submonoidMap (f : M →* N) (M' : Submonoid M) : M' →* M'.map f where toFun x := ⟨f x, ⟨x, x.2, rfl⟩⟩ map_one' := Subtype.eq <| f.map_one map_mul' x y := Subtype.eq <| f.map_mul x y @[to_additive] theorem submonoidMap_surjective (f : M →* N) (M' : Submonoid M) : Function.Surjective (f.submonoidMap M') := by rintro ⟨_, x, hx, rfl⟩ exact ⟨⟨x, hx⟩, rfl⟩ end MonoidHom namespace Submonoid open MonoidHom @[to_additive] theorem mrange_inl : mrange (inl M N) = prod ⊤ ⊥ := by simpa only [mrange_eq_map] using map_inl ⊤ @[to_additive] theorem mrange_inr : mrange (inr M N) = prod ⊥ ⊤ := by simpa only [mrange_eq_map] using map_inr ⊤ @[to_additive] theorem mrange_inl' : mrange (inl M N) = comap (snd M N) ⊥ := mrange_inl.trans (top_prod _) @[to_additive] theorem mrange_inr' : mrange (inr M N) = comap (fst M N) ⊥ := mrange_inr.trans (prod_top _) @[to_additive (attr := simp)] theorem mrange_fst : mrange (fst M N) = ⊤ := mrange_eq_top_of_surjective (fst M N) <| @Prod.fst_surjective _ _ ⟨1⟩ @[to_additive (attr := simp)] theorem mrange_snd : mrange (snd M N) = ⊤ := mrange_eq_top_of_surjective (snd M N) <| @Prod.snd_surjective _ _ ⟨1⟩ @[to_additive prod_eq_bot_iff] theorem prod_eq_bot_iff {s : Submonoid M} {t : Submonoid N} : s.prod t = ⊥ ↔ s = ⊥ ∧ t = ⊥ := by simp only [eq_bot_iff, prod_le_iff, (gc_map_comap _).le_iff_le, comap_bot', mker_inl, mker_inr] @[to_additive prod_eq_top_iff] theorem prod_eq_top_iff {s : Submonoid M} {t : Submonoid N} : s.prod t = ⊤ ↔ s = ⊤ ∧ t = ⊤ := by simp only [eq_top_iff, le_prod_iff, ← (gc_map_comap _).le_iff_le, ← mrange_eq_map, mrange_fst, mrange_snd] @[to_additive (attr := simp)] theorem mrange_inl_sup_mrange_inr : mrange (inl M N) ⊔ mrange (inr M N) = ⊤ := by simp only [mrange_inl, mrange_inr, prod_bot_sup_bot_prod, top_prod_top] /-- The monoid hom associated to an inclusion of submonoids. -/ @[to_additive "The `AddMonoid` hom associated to an inclusion of submonoids."] def inclusion {S T : Submonoid M} (h : S ≤ T) : S →* T := S.subtype.codRestrict _ fun x => h x.2 @[to_additive (attr := simp)] theorem mrange_subtype (s : Submonoid M) : mrange s.subtype = s := SetLike.coe_injective <| (coe_mrange _).trans <| Subtype.range_coe -- `alias` doesn't add the deprecation suggestion to the `to_additive` version -- see https://github.com/leanprover-community/mathlib4/issues/19424 @[to_additive] alias range_subtype := mrange_subtype attribute [deprecated mrange_subtype (since := "2024-11-25")] range_subtype attribute [deprecated AddSubmonoid.mrange_subtype (since := "2024-11-25")] AddSubmonoid.range_subtype @[to_additive] theorem eq_top_iff' : S = ⊤ ↔ ∀ x : M, x ∈ S := eq_top_iff.trans ⟨fun h m => h <| mem_top m, fun h m _ => h m⟩ @[to_additive] theorem eq_bot_iff_forall : S = ⊥ ↔ ∀ x ∈ S, x = (1 : M) := SetLike.ext_iff.trans <| by simp +contextual [iff_def, S.one_mem] @[to_additive] theorem eq_bot_of_subsingleton [Subsingleton S] : S = ⊥ := by rw [eq_bot_iff_forall] intro y hy simpa using congr_arg ((↑) : S → M) <| Subsingleton.elim (⟨y, hy⟩ : S) 1 @[to_additive] theorem nontrivial_iff_exists_ne_one (S : Submonoid M) : Nontrivial S ↔ ∃ x ∈ S, x ≠ (1 : M) := calc Nontrivial S ↔ ∃ x : S, x ≠ 1 := nontrivial_iff_exists_ne 1 _ ↔ ∃ (x : _) (hx : x ∈ S), (⟨x, hx⟩ : S) ≠ ⟨1, S.one_mem⟩ := Subtype.exists _ ↔ ∃ x ∈ S, x ≠ (1 : M) := by simp [Ne] /-- A submonoid is either the trivial submonoid or nontrivial. -/ @[to_additive "An additive submonoid is either the trivial additive submonoid or nontrivial."] theorem bot_or_nontrivial (S : Submonoid M) : S = ⊥ ∨ Nontrivial S := by simp only [eq_bot_iff_forall, nontrivial_iff_exists_ne_one, ← not_forall, ← Classical.not_imp, Classical.em] /-- A submonoid is either the trivial submonoid or contains a nonzero element. -/ @[to_additive "An additive submonoid is either the trivial additive submonoid or contains a nonzero element."] theorem bot_or_exists_ne_one (S : Submonoid M) : S = ⊥ ∨ ∃ x ∈ S, x ≠ (1 : M) := S.bot_or_nontrivial.imp_right S.nontrivial_iff_exists_ne_one.mp end Submonoid namespace MulEquiv variable {S} {T : Submonoid M} /-- Makes the identity isomorphism from a proof that two submonoids of a multiplicative monoid are equal. -/ @[to_additive "Makes the identity additive isomorphism from a proof two submonoids of an additive monoid are equal."] def submonoidCongr (h : S = T) : S ≃* T := { Equiv.setCongr <| congr_arg _ h with map_mul' := fun _ _ => rfl } -- this name is primed so that the version to `f.range` instead of `f.mrange` can be unprimed. /-- A monoid homomorphism `f : M →* N` with a left-inverse `g : N → M` defines a multiplicative equivalence between `M` and `f.mrange`. This is a bidirectional version of `MonoidHom.mrange_restrict`. -/ @[to_additive (attr := simps +simpRhs) "An additive monoid homomorphism `f : M →+ N` with a left-inverse `g : N → M` defines an additive equivalence between `M` and `f.mrange`. This is a bidirectional version of `AddMonoidHom.mrange_restrict`. "] def ofLeftInverse' (f : M →* N) {g : N → M} (h : Function.LeftInverse g f) : M ≃* MonoidHom.mrange f := { f.mrangeRestrict with toFun := f.mrangeRestrict invFun := g ∘ (MonoidHom.mrange f).subtype left_inv := h right_inv := fun x => Subtype.ext <| let ⟨x', hx'⟩ := MonoidHom.mem_mrange.mp x.2 show f (g x) = x by rw [← hx', h x'] } /-- A `MulEquiv` `φ` between two monoids `M` and `N` induces a `MulEquiv` between a submonoid `S ≤ M` and the submonoid `φ(S) ≤ N`. See `MonoidHom.submonoidMap` for a variant for `MonoidHom`s. -/ @[to_additive "An `AddEquiv` `φ` between two additive monoids `M` and `N` induces an `AddEquiv` between a submonoid `S ≤ M` and the submonoid `φ(S) ≤ N`. See `AddMonoidHom.addSubmonoidMap` for a variant for `AddMonoidHom`s."] def submonoidMap (e : M ≃* N) (S : Submonoid M) : S ≃* S.map e := { (e : M ≃ N).image S with map_mul' := fun _ _ => Subtype.ext (map_mul e _ _) } @[to_additive (attr := simp)] theorem coe_submonoidMap_apply (e : M ≃* N) (S : Submonoid M) (g : S) : ((submonoidMap e S g : S.map (e : M →* N)) : N) = e g := rfl @[to_additive (attr := simp) AddEquiv.add_submonoid_map_symm_apply] theorem submonoidMap_symm_apply (e : M ≃* N) (S : Submonoid M) (g : S.map (e : M →* N)) : (e.submonoidMap S).symm g = ⟨e.symm g, SetLike.mem_coe.1 <| Set.mem_image_equiv.1 g.2⟩ := rfl end MulEquiv @[to_additive (attr := simp)] theorem Submonoid.equivMapOfInjective_coe_mulEquiv (e : M ≃* N) : S.equivMapOfInjective (e : M →* N) (EquivLike.injective e) = e.submonoidMap S := by ext rfl @[to_additive] instance Submonoid.faithfulSMul {M' α : Type*} [MulOneClass M'] [SMul M' α] {S : Submonoid M'} [FaithfulSMul M' α] : FaithfulSMul S α := ⟨fun h => Subtype.ext <| eq_of_smul_eq_smul h⟩ section Units namespace Submonoid /-- The multiplicative equivalence between the type of units of `M` and the submonoid of unit elements of `M`. -/ @[to_additive (attr := simps!) " The additive equivalence between the type of additive units of `M` and the additive submonoid whose elements are the additive units of `M`. "] noncomputable def unitsTypeEquivIsUnitSubmonoid [Monoid M] : Mˣ ≃* IsUnit.submonoid M where toFun x := ⟨x, Units.isUnit x⟩ invFun x := x.prop.unit left_inv _ := IsUnit.unit_of_val_units _ right_inv x := by simp_rw [IsUnit.unit_spec] map_mul' x y := by simp_rw [Units.val_mul]; rfl end Submonoid end Units
open AddSubmonoid Set
Mathlib/Algebra/Group/Submonoid/Operations.lean
1,029
1,030
/- Copyright (c) 2021 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import Mathlib.FieldTheory.RatFunc.Defs import Mathlib.RingTheory.EuclideanDomain import Mathlib.RingTheory.Localization.FractionRing import Mathlib.RingTheory.Polynomial.Content /-! # The field structure of rational functions ## Main definitions Working with rational functions as polynomials: - `RatFunc.instField` provides a field structure You can use `IsFractionRing` API to treat `RatFunc` as the field of fractions of polynomials: * `algebraMap K[X] (RatFunc K)` maps polynomials to rational functions * `IsFractionRing.algEquiv` maps other fields of fractions of `K[X]` to `RatFunc K`, in particular: * `FractionRing.algEquiv K[X] (RatFunc K)` maps the generic field of fraction construction to `RatFunc K`. Combine this with `AlgEquiv.restrictScalars` to change the `FractionRing K[X] ≃ₐ[K[X]] RatFunc K` to `FractionRing K[X] ≃ₐ[K] RatFunc K`. Working with rational functions as fractions: - `RatFunc.num` and `RatFunc.denom` give the numerator and denominator. These values are chosen to be coprime and such that `RatFunc.denom` is monic. Lifting homomorphisms of polynomials to other types, by mapping and dividing, as long as the homomorphism retains the non-zero-divisor property: - `RatFunc.liftMonoidWithZeroHom` lifts a `K[X] →*₀ G₀` to a `RatFunc K →*₀ G₀`, where `[CommRing K] [CommGroupWithZero G₀]` - `RatFunc.liftRingHom` lifts a `K[X] →+* L` to a `RatFunc K →+* L`, where `[CommRing K] [Field L]` - `RatFunc.liftAlgHom` lifts a `K[X] →ₐ[S] L` to a `RatFunc K →ₐ[S] L`, where `[CommRing K] [Field L] [CommSemiring S] [Algebra S K[X]] [Algebra S L]` This is satisfied by injective homs. We also have lifting homomorphisms of polynomials to other polynomials, with the same condition on retaining the non-zero-divisor property across the map: - `RatFunc.map` lifts `K[X] →* R[X]` when `[CommRing K] [CommRing R]` - `RatFunc.mapRingHom` lifts `K[X] →+* R[X]` when `[CommRing K] [CommRing R]` - `RatFunc.mapAlgHom` lifts `K[X] →ₐ[S] R[X]` when `[CommRing K] [IsDomain K] [CommRing R] [IsDomain R]` -/ universe u v noncomputable section open scoped nonZeroDivisors Polynomial variable {K : Type u} namespace RatFunc section Field variable [CommRing K] /-- The zero rational function. -/ protected irreducible_def zero : RatFunc K := ⟨0⟩ instance : Zero (RatFunc K) := ⟨RatFunc.zero⟩ theorem ofFractionRing_zero : (ofFractionRing 0 : RatFunc K) = 0 := zero_def.symm /-- Addition of rational functions. -/ protected irreducible_def add : RatFunc K → RatFunc K → RatFunc K | ⟨p⟩, ⟨q⟩ => ⟨p + q⟩ instance : Add (RatFunc K) := ⟨RatFunc.add⟩ theorem ofFractionRing_add (p q : FractionRing K[X]) : ofFractionRing (p + q) = ofFractionRing p + ofFractionRing q := (add_def _ _).symm /-- Subtraction of rational functions. -/ protected irreducible_def sub : RatFunc K → RatFunc K → RatFunc K | ⟨p⟩, ⟨q⟩ => ⟨p - q⟩ instance : Sub (RatFunc K) := ⟨RatFunc.sub⟩ theorem ofFractionRing_sub (p q : FractionRing K[X]) : ofFractionRing (p - q) = ofFractionRing p - ofFractionRing q := (sub_def _ _).symm /-- Additive inverse of a rational function. -/ protected irreducible_def neg : RatFunc K → RatFunc K | ⟨p⟩ => ⟨-p⟩ instance : Neg (RatFunc K) := ⟨RatFunc.neg⟩ theorem ofFractionRing_neg (p : FractionRing K[X]) : ofFractionRing (-p) = -ofFractionRing p := (neg_def _).symm
/-- The multiplicative unit of rational functions. -/ protected irreducible_def one : RatFunc K := ⟨1⟩
Mathlib/FieldTheory/RatFunc/Basic.lean
104
106
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import Mathlib.Algebra.BigOperators.Group.Finset.Sigma import Mathlib.Algebra.Order.Interval.Finset.Basic import Mathlib.Order.Interval.Finset.Nat import Mathlib.Tactic.Linarith /-! # Results about big operators over intervals We prove results about big operators over intervals. -/ open Nat variable {α M : Type*} namespace Finset section PartialOrder variable [PartialOrder α] [CommMonoid M] {f : α → M} {a b : α} section LocallyFiniteOrder variable [LocallyFiniteOrder α] @[to_additive] lemma mul_prod_Ico_eq_prod_Icc (h : a ≤ b) : f b * ∏ x ∈ Ico a b, f x = ∏ x ∈ Icc a b, f x := by rw [Icc_eq_cons_Ico h, prod_cons] @[to_additive] lemma prod_Ico_mul_eq_prod_Icc (h : a ≤ b) : (∏ x ∈ Ico a b, f x) * f b = ∏ x ∈ Icc a b, f x := by rw [mul_comm, mul_prod_Ico_eq_prod_Icc h] @[to_additive] lemma mul_prod_Ioc_eq_prod_Icc (h : a ≤ b) : f a * ∏ x ∈ Ioc a b, f x = ∏ x ∈ Icc a b, f x := by rw [Icc_eq_cons_Ioc h, prod_cons] @[to_additive] lemma prod_Ioc_mul_eq_prod_Icc (h : a ≤ b) : (∏ x ∈ Ioc a b, f x) * f a = ∏ x ∈ Icc a b, f x := by rw [mul_comm, mul_prod_Ioc_eq_prod_Icc h] end LocallyFiniteOrder section LocallyFiniteOrderTop variable [LocallyFiniteOrderTop α] @[to_additive] lemma mul_prod_Ioi_eq_prod_Ici (a : α) : f a * ∏ x ∈ Ioi a, f x = ∏ x ∈ Ici a, f x := by rw [Ici_eq_cons_Ioi, prod_cons] @[to_additive] lemma prod_Ioi_mul_eq_prod_Ici (a : α) : (∏ x ∈ Ioi a, f x) * f a = ∏ x ∈ Ici a, f x := by rw [mul_comm, mul_prod_Ioi_eq_prod_Ici] end LocallyFiniteOrderTop section LocallyFiniteOrderBot variable [LocallyFiniteOrderBot α] @[to_additive] lemma mul_prod_Iio_eq_prod_Iic (a : α) : f a * ∏ x ∈ Iio a, f x = ∏ x ∈ Iic a, f x := by rw [Iic_eq_cons_Iio, prod_cons] @[to_additive] lemma prod_Iio_mul_eq_prod_Iic (a : α) : (∏ x ∈ Iio a, f x) * f a = ∏ x ∈ Iic a, f x := by rw [mul_comm, mul_prod_Iio_eq_prod_Iic] end LocallyFiniteOrderBot end PartialOrder section LinearOrder variable [Fintype α] [LinearOrder α] [LocallyFiniteOrderTop α] [LocallyFiniteOrderBot α] [CommMonoid M] @[to_additive] lemma prod_prod_Ioi_mul_eq_prod_prod_off_diag (f : α → α → M) : ∏ i, ∏ j ∈ Ioi i, f j i * f i j = ∏ i, ∏ j ∈ {i}ᶜ, f j i := by simp_rw [← Ioi_disjUnion_Iio, prod_disjUnion, prod_mul_distrib] congr 1 rw [prod_sigma', prod_sigma'] refine prod_nbij' (fun i ↦ ⟨i.2, i.1⟩) (fun i ↦ ⟨i.2, i.1⟩) ?_ ?_ ?_ ?_ ?_ <;> simp end LinearOrder section Generic variable [CommMonoid M] {s₂ s₁ s : Finset α} {a : α} {g f : α → M} @[to_additive] theorem prod_Ico_add' [AddCommMonoid α] [PartialOrder α] [IsOrderedCancelAddMonoid α] [ExistsAddOfLE α] [LocallyFiniteOrder α] (f : α → M) (a b c : α) : (∏ x ∈ Ico a b, f (x + c)) = ∏ x ∈ Ico (a + c) (b + c), f x := by rw [← map_add_right_Ico, prod_map] rfl @[to_additive] theorem prod_Ico_add [AddCommMonoid α] [PartialOrder α] [IsOrderedCancelAddMonoid α] [ExistsAddOfLE α] [LocallyFiniteOrder α] (f : α → M) (a b c : α) : (∏ x ∈ Ico a b, f (c + x)) = ∏ x ∈ Ico (a + c) (b + c), f x := by convert prod_Ico_add' f a b c using 2 rw [add_comm] @[to_additive (attr := simp)] theorem prod_Ico_add_right_sub_eq [AddCommMonoid α] [PartialOrder α] [IsOrderedCancelAddMonoid α] [ExistsAddOfLE α] [LocallyFiniteOrder α] [Sub α] [OrderedSub α] (a b c : α) : ∏ x ∈ Ico (a + c) (b + c), f (x - c) = ∏ x ∈ Ico a b, f x := by simp only [← map_add_right_Ico, prod_map, addRightEmbedding_apply, add_tsub_cancel_right] @[to_additive] theorem prod_Ico_succ_top {a b : ℕ} (hab : a ≤ b) (f : ℕ → M) : (∏ k ∈ Ico a (b + 1), f k) = (∏ k ∈ Ico a b, f k) * f b := by rw [Nat.Ico_succ_right_eq_insert_Ico hab, prod_insert right_not_mem_Ico, mul_comm] @[to_additive] theorem prod_eq_prod_Ico_succ_bot {a b : ℕ} (hab : a < b) (f : ℕ → M) : ∏ k ∈ Ico a b, f k = f a * ∏ k ∈ Ico (a + 1) b, f k := by have ha : a ∉ Ico (a + 1) b := by simp rw [← prod_insert ha, Nat.Ico_insert_succ_left hab] @[to_additive] theorem prod_Ico_consecutive (f : ℕ → M) {m n k : ℕ} (hmn : m ≤ n) (hnk : n ≤ k) : ((∏ i ∈ Ico m n, f i) * ∏ i ∈ Ico n k, f i) = ∏ i ∈ Ico m k, f i := Ico_union_Ico_eq_Ico hmn hnk ▸ Eq.symm (prod_union (Ico_disjoint_Ico_consecutive m n k)) @[to_additive] theorem prod_Ioc_consecutive (f : ℕ → M) {m n k : ℕ} (hmn : m ≤ n) (hnk : n ≤ k) : ((∏ i ∈ Ioc m n, f i) * ∏ i ∈ Ioc n k, f i) = ∏ i ∈ Ioc m k, f i := by rw [← Ioc_union_Ioc_eq_Ioc hmn hnk, prod_union] apply disjoint_left.2 fun x hx h'x => _ intros x hx h'x exact lt_irrefl _ ((mem_Ioc.1 h'x).1.trans_le (mem_Ioc.1 hx).2) @[to_additive] theorem prod_Ioc_succ_top {a b : ℕ} (hab : a ≤ b) (f : ℕ → M) : (∏ k ∈ Ioc a (b + 1), f k) = (∏ k ∈ Ioc a b, f k) * f (b + 1) := by rw [← prod_Ioc_consecutive _ hab (Nat.le_succ b), Nat.Ioc_succ_singleton, prod_singleton] @[to_additive] theorem prod_Icc_succ_top {a b : ℕ} (hab : a ≤ b + 1) (f : ℕ → M) : (∏ k ∈ Icc a (b + 1), f k) = (∏ k ∈ Icc a b, f k) * f (b + 1) := by rw [← Nat.Ico_succ_right, prod_Ico_succ_top hab, Nat.Ico_succ_right] @[to_additive] theorem prod_range_mul_prod_Ico (f : ℕ → M) {m n : ℕ} (h : m ≤ n) : ((∏ k ∈ range m, f k) * ∏ k ∈ Ico m n, f k) = ∏ k ∈ range n, f k := Nat.Ico_zero_eq_range ▸ Nat.Ico_zero_eq_range ▸ prod_Ico_consecutive f m.zero_le h @[to_additive] theorem prod_range_eq_mul_Ico (f : ℕ → M) {n : ℕ} (hn : 0 < n) : ∏ x ∈ Finset.range n, f x = f 0 * ∏ x ∈ Ico 1 n, f x := Finset.range_eq_Ico ▸ Finset.prod_eq_prod_Ico_succ_bot hn f @[to_additive] theorem prod_Ico_eq_mul_inv {δ : Type*} [CommGroup δ] (f : ℕ → δ) {m n : ℕ} (h : m ≤ n) : ∏ k ∈ Ico m n, f k = (∏ k ∈ range n, f k) * (∏ k ∈ range m, f k)⁻¹ := eq_mul_inv_iff_mul_eq.2 <| by (rw [mul_comm]; exact prod_range_mul_prod_Ico f h) @[to_additive] theorem prod_Ico_eq_div {δ : Type*} [CommGroup δ] (f : ℕ → δ) {m n : ℕ} (h : m ≤ n) : ∏ k ∈ Ico m n, f k = (∏ k ∈ range n, f k) / ∏ k ∈ range m, f k := by simpa only [div_eq_mul_inv] using prod_Ico_eq_mul_inv f h @[to_additive] theorem prod_range_div_prod_range {α : Type*} [CommGroup α] {f : ℕ → α} {n m : ℕ} (hnm : n ≤ m) : ((∏ k ∈ range m, f k) / ∏ k ∈ range n, f k) = ∏ k ∈ range m with n ≤ k, f k := by rw [← prod_Ico_eq_div f hnm] congr apply Finset.ext simp only [mem_Ico, mem_filter, mem_range, *] tauto /-- The two ways of summing over `(i, j)` in the range `a ≤ i ≤ j < b` are equal. -/ theorem sum_Ico_Ico_comm {M : Type*} [AddCommMonoid M] (a b : ℕ) (f : ℕ → ℕ → M) : (∑ i ∈ Finset.Ico a b, ∑ j ∈ Finset.Ico i b, f i j) = ∑ j ∈ Finset.Ico a b, ∑ i ∈ Finset.Ico a (j + 1), f i j := by rw [Finset.sum_sigma', Finset.sum_sigma'] refine sum_nbij' (fun x ↦ ⟨x.2, x.1⟩) (fun x ↦ ⟨x.2, x.1⟩) ?_ ?_ (fun _ _ ↦ rfl) (fun _ _ ↦ rfl) (fun _ _ ↦ rfl) <;> simp only [Finset.mem_Ico, Sigma.forall, Finset.mem_sigma] <;> rintro a b ⟨⟨h₁, h₂⟩, ⟨h₃, h₄⟩⟩ <;> omega /-- The two ways of summing over `(i, j)` in the range `a ≤ i < j < b` are equal. -/ theorem sum_Ico_Ico_comm' {M : Type*} [AddCommMonoid M] (a b : ℕ) (f : ℕ → ℕ → M) : (∑ i ∈ Finset.Ico a b, ∑ j ∈ Finset.Ico (i + 1) b, f i j) = ∑ j ∈ Finset.Ico a b, ∑ i ∈ Finset.Ico a j, f i j := by rw [Finset.sum_sigma', Finset.sum_sigma'] refine sum_nbij' (fun x ↦ ⟨x.2, x.1⟩) (fun x ↦ ⟨x.2, x.1⟩) ?_ ?_ (fun _ _ ↦ rfl) (fun _ _ ↦ rfl) (fun _ _ ↦ rfl) <;> simp only [Finset.mem_Ico, Sigma.forall, Finset.mem_sigma] <;> rintro a b ⟨⟨h₁, h₂⟩, ⟨h₃, h₄⟩⟩ <;> omega @[to_additive] theorem prod_Ico_eq_prod_range (f : ℕ → M) (m n : ℕ) : ∏ k ∈ Ico m n, f k = ∏ k ∈ range (n - m), f (m + k) := by by_cases h : m ≤ n · rw [← Nat.Ico_zero_eq_range, prod_Ico_add, zero_add, tsub_add_cancel_of_le h] · replace h : n ≤ m := le_of_not_ge h rw [Ico_eq_empty_of_le h, tsub_eq_zero_iff_le.mpr h, range_zero, prod_empty, prod_empty] theorem prod_Ico_reflect (f : ℕ → M) (k : ℕ) {m n : ℕ} (h : m ≤ n + 1) : (∏ j ∈ Ico k m, f (n - j)) = ∏ j ∈ Ico (n + 1 - m) (n + 1 - k), f j := by have : ∀ i < m, i ≤ n := by intro i hi exact (add_le_add_iff_right 1).1 (le_trans (Nat.lt_iff_add_one_le.1 hi) h) rcases lt_or_le k m with hkm | hkm · rw [← Nat.Ico_image_const_sub_eq_Ico (this _ hkm)] refine (prod_image ?_).symm simp only [mem_Ico] rintro i ⟨_, im⟩ j ⟨_, jm⟩ Hij rw [← tsub_tsub_cancel_of_le (this _ im), Hij, tsub_tsub_cancel_of_le (this _ jm)] · have : n + 1 - k ≤ n + 1 - m := by rw [tsub_le_tsub_iff_left h] exact hkm simp only [hkm, Ico_eq_empty_of_le, prod_empty, tsub_le_iff_right, Ico_eq_empty_of_le this] theorem sum_Ico_reflect {δ : Type*} [AddCommMonoid δ] (f : ℕ → δ) (k : ℕ) {m n : ℕ} (h : m ≤ n + 1) : (∑ j ∈ Ico k m, f (n - j)) = ∑ j ∈ Ico (n + 1 - m) (n + 1 - k), f j := @prod_Ico_reflect (Multiplicative δ) _ f k m n h theorem prod_range_reflect (f : ℕ → M) (n : ℕ) : (∏ j ∈ range n, f (n - 1 - j)) = ∏ j ∈ range n, f j := by cases n · simp · simp only [← Nat.Ico_zero_eq_range, Nat.succ_sub_succ_eq_sub, tsub_zero] rw [prod_Ico_reflect _ _ le_rfl] simp theorem sum_range_reflect {δ : Type*} [AddCommMonoid δ] (f : ℕ → δ) (n : ℕ) : (∑ j ∈ range n, f (n - 1 - j)) = ∑ j ∈ range n, f j := @prod_range_reflect (Multiplicative δ) _ f n @[simp] theorem prod_Ico_id_eq_factorial : ∀ n : ℕ, (∏ x ∈ Ico 1 (n + 1), x) = n ! | 0 => rfl | n + 1 => by rw [prod_Ico_succ_top <| Nat.succ_le_succ <| Nat.zero_le n, Nat.factorial_succ, prod_Ico_id_eq_factorial n, Nat.succ_eq_add_one, mul_comm] @[simp] theorem prod_range_add_one_eq_factorial : ∀ n : ℕ, (∏ x ∈ range n, (x + 1)) = n ! | 0 => rfl | n + 1 => by simp [factorial, Finset.range_succ, prod_range_add_one_eq_factorial n] section GaussSum /-- Gauss' summation formula -/ theorem sum_range_id_mul_two (n : ℕ) : (∑ i ∈ range n, i) * 2 = n * (n - 1) := calc (∑ i ∈ range n, i) * 2 = (∑ i ∈ range n, i) + ∑ i ∈ range n, (n - 1 - i) := by rw [sum_range_reflect (fun i => i) n, mul_two] _ = ∑ i ∈ range n, (i + (n - 1 - i)) := sum_add_distrib.symm _ = ∑ _ ∈ range n, (n - 1) := sum_congr rfl fun _ hi => add_tsub_cancel_of_le <| Nat.le_sub_one_of_lt <| mem_range.1 hi _ = n * (n - 1) := by rw [sum_const, card_range, Nat.nsmul_eq_mul] /-- Gauss' summation formula -/ theorem sum_range_id (n : ℕ) : ∑ i ∈ range n, i = n * (n - 1) / 2 := by rw [← sum_range_id_mul_two n, Nat.mul_div_cancel _ zero_lt_two] end GaussSum
@[to_additive] lemma prod_range_diag_flip (n : ℕ) (f : ℕ → ℕ → M) : (∏ m ∈ range n, ∏ k ∈ range (m + 1), f k (m - k)) =
Mathlib/Algebra/BigOperators/Intervals.lean
266
268
/- Copyright (c) 2021 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import Mathlib.Algebra.MvPolynomial.Variables /-! # Polynomials supported by a set of variables This file contains the definition and lemmas about `MvPolynomial.supported`. ## Main definitions * `MvPolynomial.supported` : Given a set `s : Set σ`, `supported R s` is the subalgebra of `MvPolynomial σ R` consisting of polynomials whose set of variables is contained in `s`. This subalgebra is isomorphic to `MvPolynomial s R`. ## Tags variables, polynomial, vars -/ universe u v w namespace MvPolynomial variable {σ : Type*} {R : Type u} section CommSemiring variable [CommSemiring R] {p : MvPolynomial σ R} variable (R) in /-- The set of polynomials whose variables are contained in `s` as a `Subalgebra` over `R`. -/ noncomputable def supported (s : Set σ) : Subalgebra R (MvPolynomial σ R) := Algebra.adjoin R (X '' s) open Algebra theorem supported_eq_range_rename (s : Set σ) : supported R s = (rename ((↑) : s → σ)).range := by rw [supported, Set.image_eq_range, adjoin_range_eq_range_aeval, rename] congr /-- The isomorphism between the subalgebra of polynomials supported by `s` and `MvPolynomial s R`. -/ noncomputable def supportedEquivMvPolynomial (s : Set σ) : supported R s ≃ₐ[R] MvPolynomial s R := (Subalgebra.equivOfEq _ _ (supported_eq_range_rename s)).trans (AlgEquiv.ofInjective (rename ((↑) : s → σ)) (rename_injective _ Subtype.val_injective)).symm @[simp] theorem supportedEquivMvPolynomial_symm_C (s : Set σ) (x : R) : (supportedEquivMvPolynomial s).symm (C x) = algebraMap R (supported R s) x := by ext1 simp [supportedEquivMvPolynomial, MvPolynomial.algebraMap_eq] @[simp] theorem supportedEquivMvPolynomial_symm_X (s : Set σ) (i : s) : (↑((supportedEquivMvPolynomial s).symm (X i : MvPolynomial s R)) : MvPolynomial σ R) = X ↑i := by simp [supportedEquivMvPolynomial] variable {s t : Set σ} theorem mem_supported : p ∈ supported R s ↔ ↑p.vars ⊆ s := by classical rw [supported_eq_range_rename, AlgHom.mem_range] constructor · rintro ⟨p, rfl⟩ refine _root_.trans (Finset.coe_subset.2 (vars_rename _ _)) ?_ simp · intro hs exact exists_rename_eq_of_vars_subset_range p ((↑) : s → σ) Subtype.val_injective (by simpa) theorem supported_eq_vars_subset : (supported R s : Set (MvPolynomial σ R)) = { p | ↑p.vars ⊆ s } := Set.ext fun _ ↦ mem_supported @[simp] theorem mem_supported_vars (p : MvPolynomial σ R) : p ∈ supported R (↑p.vars : Set σ) := by rw [mem_supported] variable (s) theorem supported_eq_adjoin_X : supported R s = Algebra.adjoin R (X '' s) := rfl @[simp] theorem supported_univ : supported R (Set.univ : Set σ) = ⊤ := by simp [Algebra.eq_top_iff, mem_supported] @[simp] theorem supported_empty : supported R (∅ : Set σ) = ⊥ := by simp [supported_eq_adjoin_X] variable {s} theorem supported_mono (st : s ⊆ t) : supported R s ≤ supported R t := Algebra.adjoin_mono (Set.image_subset _ st) @[simp] theorem X_mem_supported [Nontrivial R] {i : σ} : X i ∈ supported R s ↔ i ∈ s := by simp [mem_supported] @[simp] theorem supported_le_supported_iff [Nontrivial R] : supported R s ≤ supported R t ↔ s ⊆ t := by constructor · intro h i simpa using @h (X i) · exact supported_mono theorem supported_strictMono [Nontrivial R] : StrictMono (supported R : Set σ → Subalgebra R (MvPolynomial σ R)) := strictMono_of_le_iff_le fun _ _ ↦ supported_le_supported_iff.symm theorem exists_restrict_to_vars (R : Type*) [CommRing R] {F : MvPolynomial σ ℤ} (hF : ↑F.vars ⊆ s) : ∃ f : (s → R) → R, ∀ x : σ → R, f (x ∘ (↑) : s → R) = aeval x F := by rw [← mem_supported, supported_eq_range_rename, AlgHom.mem_range] at hF obtain ⟨F', hF'⟩ := hF use fun z ↦ aeval z F'
intro x simp only [← hF', aeval_rename]
Mathlib/Algebra/MvPolynomial/Supported.lean
117
118
/- Copyright (c) 2014 Parikshit Khanna. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Mario Carneiro -/ import Mathlib.Control.Basic import Mathlib.Data.Nat.Basic import Mathlib.Data.Option.Basic import Mathlib.Data.List.Defs import Mathlib.Data.List.Monad import Mathlib.Logic.OpClass import Mathlib.Logic.Unique import Mathlib.Order.Basic import Mathlib.Tactic.Common /-! # Basic properties of lists -/ assert_not_exists GroupWithZero assert_not_exists Lattice assert_not_exists Prod.swap_eq_iff_eq_swap assert_not_exists Ring assert_not_exists Set.range open Function open Nat hiding one_pos namespace List universe u v w variable {ι : Type*} {α : Type u} {β : Type v} {γ : Type w} {l₁ l₂ : List α} /-- There is only one list of an empty type -/ instance uniqueOfIsEmpty [IsEmpty α] : Unique (List α) := { instInhabitedList with uniq := fun l => match l with | [] => rfl | a :: _ => isEmptyElim a } instance : Std.LawfulIdentity (α := List α) Append.append [] where left_id := nil_append right_id := append_nil instance : Std.Associative (α := List α) Append.append where assoc := append_assoc @[simp] theorem cons_injective {a : α} : Injective (cons a) := fun _ _ => tail_eq_of_cons_eq theorem singleton_injective : Injective fun a : α => [a] := fun _ _ h => (cons_eq_cons.1 h).1 theorem set_of_mem_cons (l : List α) (a : α) : { x | x ∈ a :: l } = insert a { x | x ∈ l } := Set.ext fun _ => mem_cons /-! ### mem -/ theorem _root_.Decidable.List.eq_or_ne_mem_of_mem [DecidableEq α] {a b : α} {l : List α} (h : a ∈ b :: l) : a = b ∨ a ≠ b ∧ a ∈ l := by by_cases hab : a = b · exact Or.inl hab · exact ((List.mem_cons.1 h).elim Or.inl (fun h => Or.inr ⟨hab, h⟩)) lemma mem_pair {a b c : α} : a ∈ [b, c] ↔ a = b ∨ a = c := by rw [mem_cons, mem_singleton] -- The simpNF linter says that the LHS can be simplified via `List.mem_map`. -- However this is a higher priority lemma. -- It seems the side condition `hf` is not applied by `simpNF`. -- https://github.com/leanprover/std4/issues/207 @[simp 1100, nolint simpNF] theorem mem_map_of_injective {f : α → β} (H : Injective f) {a : α} {l : List α} : f a ∈ map f l ↔ a ∈ l := ⟨fun m => let ⟨_, m', e⟩ := exists_of_mem_map m; H e ▸ m', mem_map_of_mem⟩ @[simp] theorem _root_.Function.Involutive.exists_mem_and_apply_eq_iff {f : α → α} (hf : Function.Involutive f) (x : α) (l : List α) : (∃ y : α, y ∈ l ∧ f y = x) ↔ f x ∈ l := ⟨by rintro ⟨y, h, rfl⟩; rwa [hf y], fun h => ⟨f x, h, hf _⟩⟩ theorem mem_map_of_involutive {f : α → α} (hf : Involutive f) {a : α} {l : List α} : a ∈ map f l ↔ f a ∈ l := by rw [mem_map, hf.exists_mem_and_apply_eq_iff] /-! ### length -/ alias ⟨_, length_pos_of_ne_nil⟩ := length_pos_iff theorem length_pos_iff_ne_nil {l : List α} : 0 < length l ↔ l ≠ [] := ⟨ne_nil_of_length_pos, length_pos_of_ne_nil⟩ theorem exists_of_length_succ {n} : ∀ l : List α, l.length = n + 1 → ∃ h t, l = h :: t | [], H => absurd H.symm <| succ_ne_zero n | h :: t, _ => ⟨h, t, rfl⟩ @[simp] lemma length_injective_iff : Injective (List.length : List α → ℕ) ↔ Subsingleton α := by constructor · intro h; refine ⟨fun x y => ?_⟩; (suffices [x] = [y] by simpa using this); apply h; rfl · intros hα l1 l2 hl induction l1 generalizing l2 <;> cases l2 · rfl · cases hl · cases hl · next ih _ _ => congr · subsingleton · apply ih; simpa using hl @[simp default+1] -- Raise priority above `length_injective_iff`. lemma length_injective [Subsingleton α] : Injective (length : List α → ℕ) := length_injective_iff.mpr inferInstance theorem length_eq_two {l : List α} : l.length = 2 ↔ ∃ a b, l = [a, b] := ⟨fun _ => let [a, b] := l; ⟨a, b, rfl⟩, fun ⟨_, _, e⟩ => e ▸ rfl⟩ theorem length_eq_three {l : List α} : l.length = 3 ↔ ∃ a b c, l = [a, b, c] := ⟨fun _ => let [a, b, c] := l; ⟨a, b, c, rfl⟩, fun ⟨_, _, _, e⟩ => e ▸ rfl⟩ /-! ### set-theoretic notation of lists -/ instance instSingletonList : Singleton α (List α) := ⟨fun x => [x]⟩ instance [DecidableEq α] : Insert α (List α) := ⟨List.insert⟩ instance [DecidableEq α] : LawfulSingleton α (List α) := { insert_empty_eq := fun x => show (if x ∈ ([] : List α) then [] else [x]) = [x] from if_neg not_mem_nil } theorem singleton_eq (x : α) : ({x} : List α) = [x] := rfl theorem insert_neg [DecidableEq α] {x : α} {l : List α} (h : x ∉ l) : Insert.insert x l = x :: l := insert_of_not_mem h theorem insert_pos [DecidableEq α] {x : α} {l : List α} (h : x ∈ l) : Insert.insert x l = l := insert_of_mem h theorem doubleton_eq [DecidableEq α] {x y : α} (h : x ≠ y) : ({x, y} : List α) = [x, y] := by rw [insert_neg, singleton_eq] rwa [singleton_eq, mem_singleton] /-! ### bounded quantifiers over lists -/ theorem forall_mem_of_forall_mem_cons {p : α → Prop} {a : α} {l : List α} (h : ∀ x ∈ a :: l, p x) : ∀ x ∈ l, p x := (forall_mem_cons.1 h).2 theorem exists_mem_cons_of {p : α → Prop} {a : α} (l : List α) (h : p a) : ∃ x ∈ a :: l, p x := ⟨a, mem_cons_self, h⟩ theorem exists_mem_cons_of_exists {p : α → Prop} {a : α} {l : List α} : (∃ x ∈ l, p x) → ∃ x ∈ a :: l, p x := fun ⟨x, xl, px⟩ => ⟨x, mem_cons_of_mem _ xl, px⟩ theorem or_exists_of_exists_mem_cons {p : α → Prop} {a : α} {l : List α} : (∃ x ∈ a :: l, p x) → p a ∨ ∃ x ∈ l, p x := fun ⟨x, xal, px⟩ => Or.elim (eq_or_mem_of_mem_cons xal) (fun h : x = a => by rw [← h]; left; exact px) fun h : x ∈ l => Or.inr ⟨x, h, px⟩ theorem exists_mem_cons_iff (p : α → Prop) (a : α) (l : List α) : (∃ x ∈ a :: l, p x) ↔ p a ∨ ∃ x ∈ l, p x := Iff.intro or_exists_of_exists_mem_cons fun h => Or.elim h (exists_mem_cons_of l) exists_mem_cons_of_exists /-! ### list subset -/ theorem cons_subset_of_subset_of_mem {a : α} {l m : List α} (ainm : a ∈ m) (lsubm : l ⊆ m) : a::l ⊆ m := cons_subset.2 ⟨ainm, lsubm⟩ theorem append_subset_of_subset_of_subset {l₁ l₂ l : List α} (l₁subl : l₁ ⊆ l) (l₂subl : l₂ ⊆ l) : l₁ ++ l₂ ⊆ l := fun _ h ↦ (mem_append.1 h).elim (@l₁subl _) (@l₂subl _) theorem map_subset_iff {l₁ l₂ : List α} (f : α → β) (h : Injective f) : map f l₁ ⊆ map f l₂ ↔ l₁ ⊆ l₂ := by refine ⟨?_, map_subset f⟩; intro h2 x hx rcases mem_map.1 (h2 (mem_map_of_mem hx)) with ⟨x', hx', hxx'⟩ cases h hxx'; exact hx' /-! ### append -/ theorem append_eq_has_append {L₁ L₂ : List α} : List.append L₁ L₂ = L₁ ++ L₂ := rfl theorem append_right_injective (s : List α) : Injective fun t ↦ s ++ t := fun _ _ ↦ append_cancel_left theorem append_left_injective (t : List α) : Injective fun s ↦ s ++ t := fun _ _ ↦ append_cancel_right /-! ### replicate -/ theorem eq_replicate_length {a : α} : ∀ {l : List α}, l = replicate l.length a ↔ ∀ b ∈ l, b = a | [] => by simp | (b :: l) => by simp [eq_replicate_length, replicate_succ] theorem replicate_add (m n) (a : α) : replicate (m + n) a = replicate m a ++ replicate n a := by rw [replicate_append_replicate] theorem replicate_subset_singleton (n) (a : α) : replicate n a ⊆ [a] := fun _ h => mem_singleton.2 (eq_of_mem_replicate h) theorem subset_singleton_iff {a : α} {L : List α} : L ⊆ [a] ↔ ∃ n, L = replicate n a := by simp only [eq_replicate_iff, subset_def, mem_singleton, exists_eq_left'] theorem replicate_right_injective {n : ℕ} (hn : n ≠ 0) : Injective (@replicate α n) := fun _ _ h => (eq_replicate_iff.1 h).2 _ <| mem_replicate.2 ⟨hn, rfl⟩ theorem replicate_right_inj {a b : α} {n : ℕ} (hn : n ≠ 0) : replicate n a = replicate n b ↔ a = b := (replicate_right_injective hn).eq_iff theorem replicate_right_inj' {a b : α} : ∀ {n}, replicate n a = replicate n b ↔ n = 0 ∨ a = b | 0 => by simp | n + 1 => (replicate_right_inj n.succ_ne_zero).trans <| by simp only [n.succ_ne_zero, false_or] theorem replicate_left_injective (a : α) : Injective (replicate · a) := LeftInverse.injective (length_replicate (n := ·)) theorem replicate_left_inj {a : α} {n m : ℕ} : replicate n a = replicate m a ↔ n = m := (replicate_left_injective a).eq_iff @[simp] theorem head?_flatten_replicate {n : ℕ} (h : n ≠ 0) (l : List α) : (List.replicate n l).flatten.head? = l.head? := by obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero h induction l <;> simp [replicate] @[simp] theorem getLast?_flatten_replicate {n : ℕ} (h : n ≠ 0) (l : List α) : (List.replicate n l).flatten.getLast? = l.getLast? := by rw [← List.head?_reverse, ← List.head?_reverse, List.reverse_flatten, List.map_replicate, List.reverse_replicate, head?_flatten_replicate h] /-! ### pure -/ theorem mem_pure (x y : α) : x ∈ (pure y : List α) ↔ x = y := by simp /-! ### bind -/ @[simp] theorem bind_eq_flatMap {α β} (f : α → List β) (l : List α) : l >>= f = l.flatMap f := rfl /-! ### concat -/ /-! ### reverse -/ theorem reverse_cons' (a : α) (l : List α) : reverse (a :: l) = concat (reverse l) a := by simp only [reverse_cons, concat_eq_append] theorem reverse_concat' (l : List α) (a : α) : (l ++ [a]).reverse = a :: l.reverse := by rw [reverse_append]; rfl @[simp] theorem reverse_singleton (a : α) : reverse [a] = [a] := rfl @[simp] theorem reverse_involutive : Involutive (@reverse α) := reverse_reverse @[simp] theorem reverse_injective : Injective (@reverse α) := reverse_involutive.injective theorem reverse_surjective : Surjective (@reverse α) := reverse_involutive.surjective theorem reverse_bijective : Bijective (@reverse α) := reverse_involutive.bijective theorem concat_eq_reverse_cons (a : α) (l : List α) : concat l a = reverse (a :: reverse l) := by simp only [concat_eq_append, reverse_cons, reverse_reverse] theorem map_reverseAux (f : α → β) (l₁ l₂ : List α) : map f (reverseAux l₁ l₂) = reverseAux (map f l₁) (map f l₂) := by simp only [reverseAux_eq, map_append, map_reverse] -- TODO: Rename `List.reverse_perm` to `List.reverse_perm_self` @[simp] lemma reverse_perm' : l₁.reverse ~ l₂ ↔ l₁ ~ l₂ where mp := l₁.reverse_perm.symm.trans mpr := l₁.reverse_perm.trans @[simp] lemma perm_reverse : l₁ ~ l₂.reverse ↔ l₁ ~ l₂ where mp hl := hl.trans l₂.reverse_perm mpr hl := hl.trans l₂.reverse_perm.symm /-! ### getLast -/ attribute [simp] getLast_cons theorem getLast_append_singleton {a : α} (l : List α) : getLast (l ++ [a]) (append_ne_nil_of_right_ne_nil l (cons_ne_nil a _)) = a := by simp [getLast_append] theorem getLast_append_of_right_ne_nil (l₁ l₂ : List α) (h : l₂ ≠ []) : getLast (l₁ ++ l₂) (append_ne_nil_of_right_ne_nil l₁ h) = getLast l₂ h := by induction l₁ with | nil => simp | cons _ _ ih => simp only [cons_append]; rw [List.getLast_cons]; exact ih @[deprecated (since := "2025-02-06")] alias getLast_append' := getLast_append_of_right_ne_nil theorem getLast_concat' {a : α} (l : List α) : getLast (concat l a) (by simp) = a := by simp @[simp] theorem getLast_singleton' (a : α) : getLast [a] (cons_ne_nil a []) = a := rfl @[simp] theorem getLast_cons_cons (a₁ a₂ : α) (l : List α) : getLast (a₁ :: a₂ :: l) (cons_ne_nil _ _) = getLast (a₂ :: l) (cons_ne_nil a₂ l) := rfl theorem dropLast_append_getLast : ∀ {l : List α} (h : l ≠ []), dropLast l ++ [getLast l h] = l | [], h => absurd rfl h | [_], _ => rfl | a :: b :: l, h => by rw [dropLast_cons₂, cons_append, getLast_cons (cons_ne_nil _ _)] congr exact dropLast_append_getLast (cons_ne_nil b l) theorem getLast_congr {l₁ l₂ : List α} (h₁ : l₁ ≠ []) (h₂ : l₂ ≠ []) (h₃ : l₁ = l₂) : getLast l₁ h₁ = getLast l₂ h₂ := by subst l₁; rfl theorem getLast_replicate_succ (m : ℕ) (a : α) : (replicate (m + 1) a).getLast (ne_nil_of_length_eq_add_one length_replicate) = a := by simp only [replicate_succ'] exact getLast_append_singleton _ @[deprecated (since := "2025-02-07")] alias getLast_filter' := getLast_filter_of_pos /-! ### getLast? -/ theorem mem_getLast?_eq_getLast : ∀ {l : List α} {x : α}, x ∈ l.getLast? → ∃ h, x = getLast l h | [], x, hx => False.elim <| by simp at hx | [a], x, hx => have : a = x := by simpa using hx this ▸ ⟨cons_ne_nil a [], rfl⟩ | a :: b :: l, x, hx => by rw [getLast?_cons_cons] at hx rcases mem_getLast?_eq_getLast hx with ⟨_, h₂⟩ use cons_ne_nil _ _ assumption theorem getLast?_eq_getLast_of_ne_nil : ∀ {l : List α} (h : l ≠ []), l.getLast? = some (l.getLast h) | [], h => (h rfl).elim | [_], _ => rfl | _ :: b :: l, _ => @getLast?_eq_getLast_of_ne_nil (b :: l) (cons_ne_nil _ _) theorem mem_getLast?_cons {x y : α} : ∀ {l : List α}, x ∈ l.getLast? → x ∈ (y :: l).getLast? | [], _ => by contradiction | _ :: _, h => h theorem dropLast_append_getLast? : ∀ {l : List α}, ∀ a ∈ l.getLast?, dropLast l ++ [a] = l | [], a, ha => (Option.not_mem_none a ha).elim | [a], _, rfl => rfl | a :: b :: l, c, hc => by rw [getLast?_cons_cons] at hc rw [dropLast_cons₂, cons_append, dropLast_append_getLast? _ hc] theorem getLastI_eq_getLast? [Inhabited α] : ∀ l : List α, l.getLastI = l.getLast?.iget | [] => by simp [getLastI, Inhabited.default] | [_] => rfl | [_, _] => rfl | [_, _, _] => rfl | _ :: _ :: c :: l => by simp [getLastI, getLastI_eq_getLast? (c :: l)] theorem getLast?_append_cons : ∀ (l₁ : List α) (a : α) (l₂ : List α), getLast? (l₁ ++ a :: l₂) = getLast? (a :: l₂) | [], _, _ => rfl | [_], _, _ => rfl | b :: c :: l₁, a, l₂ => by rw [cons_append, cons_append, getLast?_cons_cons, ← cons_append, getLast?_append_cons (c :: l₁)] theorem getLast?_append_of_ne_nil (l₁ : List α) : ∀ {l₂ : List α} (_ : l₂ ≠ []), getLast? (l₁ ++ l₂) = getLast? l₂ | [], hl₂ => by contradiction | b :: l₂, _ => getLast?_append_cons l₁ b l₂ theorem mem_getLast?_append_of_mem_getLast? {l₁ l₂ : List α} {x : α} (h : x ∈ l₂.getLast?) : x ∈ (l₁ ++ l₂).getLast? := by cases l₂ · contradiction · rw [List.getLast?_append_cons] exact h /-! ### head(!?) and tail -/ @[simp] theorem head!_nil [Inhabited α] : ([] : List α).head! = default := rfl @[simp] theorem head_cons_tail (x : List α) (h : x ≠ []) : x.head h :: x.tail = x := by cases x <;> simp at h ⊢ theorem head_eq_getElem_zero {l : List α} (hl : l ≠ []) : l.head hl = l[0]'(length_pos_iff.2 hl) := (getElem_zero _).symm theorem head!_eq_head? [Inhabited α] (l : List α) : head! l = (head? l).iget := by cases l <;> rfl theorem surjective_head! [Inhabited α] : Surjective (@head! α _) := fun x => ⟨[x], rfl⟩ theorem surjective_head? : Surjective (@head? α) := Option.forall.2 ⟨⟨[], rfl⟩, fun x => ⟨[x], rfl⟩⟩ theorem surjective_tail : Surjective (@tail α) | [] => ⟨[], rfl⟩ | a :: l => ⟨a :: a :: l, rfl⟩ theorem eq_cons_of_mem_head? {x : α} : ∀ {l : List α}, x ∈ l.head? → l = x :: tail l | [], h => (Option.not_mem_none _ h).elim | a :: l, h => by simp only [head?, Option.mem_def, Option.some_inj] at h exact h ▸ rfl @[simp] theorem head!_cons [Inhabited α] (a : α) (l : List α) : head! (a :: l) = a := rfl @[simp] theorem head!_append [Inhabited α] (t : List α) {s : List α} (h : s ≠ []) : head! (s ++ t) = head! s := by induction s · contradiction · rfl theorem mem_head?_append_of_mem_head? {s t : List α} {x : α} (h : x ∈ s.head?) : x ∈ (s ++ t).head? := by cases s · contradiction · exact h theorem head?_append_of_ne_nil : ∀ (l₁ : List α) {l₂ : List α} (_ : l₁ ≠ []), head? (l₁ ++ l₂) = head? l₁ | _ :: _, _, _ => rfl theorem tail_append_singleton_of_ne_nil {a : α} {l : List α} (h : l ≠ nil) : tail (l ++ [a]) = tail l ++ [a] := by induction l · contradiction · rw [tail, cons_append, tail] theorem cons_head?_tail : ∀ {l : List α} {a : α}, a ∈ head? l → a :: tail l = l | [], a, h => by contradiction | b :: l, a, h => by simp? at h says simp only [head?_cons, Option.mem_def, Option.some.injEq] at h simp [h] theorem head!_mem_head? [Inhabited α] : ∀ {l : List α}, l ≠ [] → head! l ∈ head? l | [], h => by contradiction | _ :: _, _ => rfl theorem cons_head!_tail [Inhabited α] {l : List α} (h : l ≠ []) : head! l :: tail l = l := cons_head?_tail (head!_mem_head? h) theorem head!_mem_self [Inhabited α] {l : List α} (h : l ≠ nil) : l.head! ∈ l := by have h' : l.head! ∈ l.head! :: l.tail := mem_cons_self rwa [cons_head!_tail h] at h' theorem get_eq_getElem? (l : List α) (i : Fin l.length) : l.get i = l[i]?.get (by simp [getElem?_eq_getElem]) := by simp @[deprecated (since := "2025-02-15")] alias get_eq_get? := get_eq_getElem? theorem exists_mem_iff_getElem {l : List α} {p : α → Prop} : (∃ x ∈ l, p x) ↔ ∃ (i : ℕ) (_ : i < l.length), p l[i] := by simp only [mem_iff_getElem] exact ⟨fun ⟨_x, ⟨i, hi, hix⟩, hxp⟩ ↦ ⟨i, hi, hix ▸ hxp⟩, fun ⟨i, hi, hp⟩ ↦ ⟨_, ⟨i, hi, rfl⟩, hp⟩⟩ theorem forall_mem_iff_getElem {l : List α} {p : α → Prop} : (∀ x ∈ l, p x) ↔ ∀ (i : ℕ) (_ : i < l.length), p l[i] := by simp [mem_iff_getElem, @forall_swap α] theorem get_tail (l : List α) (i) (h : i < l.tail.length) (h' : i + 1 < l.length := (by simp only [length_tail] at h; omega)) : l.tail.get ⟨i, h⟩ = l.get ⟨i + 1, h'⟩ := by cases l <;> [cases h; rfl] /-! ### sublists -/ attribute [refl] List.Sublist.refl theorem Sublist.cons_cons {l₁ l₂ : List α} (a : α) (s : l₁ <+ l₂) : a :: l₁ <+ a :: l₂ := Sublist.cons₂ _ s lemma cons_sublist_cons' {a b : α} : a :: l₁ <+ b :: l₂ ↔ a :: l₁ <+ l₂ ∨ a = b ∧ l₁ <+ l₂ := by constructor · rintro (_ | _) · exact Or.inl ‹_› · exact Or.inr ⟨rfl, ‹_›⟩ · rintro (h | ⟨rfl, h⟩) · exact h.cons _ · rwa [cons_sublist_cons] theorem sublist_cons_of_sublist (a : α) (h : l₁ <+ l₂) : l₁ <+ a :: l₂ := h.cons _ @[deprecated (since := "2025-02-07")] alias sublist_nil_iff_eq_nil := sublist_nil @[simp] lemma sublist_singleton {l : List α} {a : α} : l <+ [a] ↔ l = [] ∨ l = [a] := by constructor <;> rintro (_ | _) <;> aesop theorem Sublist.antisymm (s₁ : l₁ <+ l₂) (s₂ : l₂ <+ l₁) : l₁ = l₂ := s₁.eq_of_length_le s₂.length_le /-- If the first element of two lists are different, then a sublist relation can be reduced. -/ theorem Sublist.of_cons_of_ne {a b} (h₁ : a ≠ b) (h₂ : a :: l₁ <+ b :: l₂) : a :: l₁ <+ l₂ := match h₁, h₂ with | _, .cons _ h => h /-! ### indexOf -/ section IndexOf variable [DecidableEq α] theorem idxOf_cons_eq {a b : α} (l : List α) : b = a → idxOf a (b :: l) = 0 | e => by rw [← e]; exact idxOf_cons_self @[deprecated (since := "2025-01-30")] alias indexOf_cons_eq := idxOf_cons_eq @[simp] theorem idxOf_cons_ne {a b : α} (l : List α) : b ≠ a → idxOf a (b :: l) = succ (idxOf a l) | h => by simp only [idxOf_cons, Bool.cond_eq_ite, beq_iff_eq, if_neg h] @[deprecated (since := "2025-01-30")] alias indexOf_cons_ne := idxOf_cons_ne theorem idxOf_eq_length_iff {a : α} {l : List α} : idxOf a l = length l ↔ a ∉ l := by induction l with | nil => exact iff_of_true rfl not_mem_nil | cons b l ih => simp only [length, mem_cons, idxOf_cons, eq_comm] rw [cond_eq_if] split_ifs with h <;> simp at h · exact iff_of_false (by rintro ⟨⟩) fun H => H <| Or.inl h.symm · simp only [Ne.symm h, false_or] rw [← ih] exact succ_inj @[simp] theorem idxOf_of_not_mem {l : List α} {a : α} : a ∉ l → idxOf a l = length l := idxOf_eq_length_iff.2 @[deprecated (since := "2025-01-30")] alias indexOf_of_not_mem := idxOf_of_not_mem theorem idxOf_le_length {a : α} {l : List α} : idxOf a l ≤ length l := by induction l with | nil => rfl | cons b l ih => ?_ simp only [length, idxOf_cons, cond_eq_if, beq_iff_eq] by_cases h : b = a · rw [if_pos h]; exact Nat.zero_le _ · rw [if_neg h]; exact succ_le_succ ih @[deprecated (since := "2025-01-30")] alias indexOf_le_length := idxOf_le_length theorem idxOf_lt_length_iff {a} {l : List α} : idxOf a l < length l ↔ a ∈ l := ⟨fun h => Decidable.byContradiction fun al => Nat.ne_of_lt h <| idxOf_eq_length_iff.2 al, fun al => (lt_of_le_of_ne idxOf_le_length) fun h => idxOf_eq_length_iff.1 h al⟩ @[deprecated (since := "2025-01-30")] alias indexOf_lt_length_iff := idxOf_lt_length_iff theorem idxOf_append_of_mem {a : α} (h : a ∈ l₁) : idxOf a (l₁ ++ l₂) = idxOf a l₁ := by induction l₁ with | nil => exfalso exact not_mem_nil h | cons d₁ t₁ ih => rw [List.cons_append] by_cases hh : d₁ = a · iterate 2 rw [idxOf_cons_eq _ hh] rw [idxOf_cons_ne _ hh, idxOf_cons_ne _ hh, ih (mem_of_ne_of_mem (Ne.symm hh) h)] @[deprecated (since := "2025-01-30")] alias indexOf_append_of_mem := idxOf_append_of_mem theorem idxOf_append_of_not_mem {a : α} (h : a ∉ l₁) : idxOf a (l₁ ++ l₂) = l₁.length + idxOf a l₂ := by induction l₁ with | nil => rw [List.nil_append, List.length, Nat.zero_add] | cons d₁ t₁ ih => rw [List.cons_append, idxOf_cons_ne _ (ne_of_not_mem_cons h).symm, List.length, ih (not_mem_of_not_mem_cons h), Nat.succ_add] @[deprecated (since := "2025-01-30")] alias indexOf_append_of_not_mem := idxOf_append_of_not_mem end IndexOf /-! ### nth element -/ section deprecated @[simp] theorem getElem?_length (l : List α) : l[l.length]? = none := getElem?_eq_none le_rfl /-- A version of `getElem_map` that can be used for rewriting. -/ theorem getElem_map_rev (f : α → β) {l} {n : Nat} {h : n < l.length} : f l[n] = (map f l)[n]'((l.length_map f).symm ▸ h) := Eq.symm (getElem_map _) theorem get_length_sub_one {l : List α} (h : l.length - 1 < l.length) : l.get ⟨l.length - 1, h⟩ = l.getLast (by rintro rfl; exact Nat.lt_irrefl 0 h) := (getLast_eq_getElem _).symm theorem take_one_drop_eq_of_lt_length {l : List α} {n : ℕ} (h : n < l.length) : (l.drop n).take 1 = [l.get ⟨n, h⟩] := by rw [drop_eq_getElem_cons h, take, take] simp theorem ext_getElem?' {l₁ l₂ : List α} (h' : ∀ n < max l₁.length l₂.length, l₁[n]? = l₂[n]?) : l₁ = l₂ := by apply ext_getElem? intro n rcases Nat.lt_or_ge n <| max l₁.length l₂.length with hn | hn · exact h' n hn · simp_all [Nat.max_le, getElem?_eq_none] @[deprecated (since := "2025-02-15")] alias ext_get?' := ext_getElem?' @[deprecated (since := "2025-02-15")] alias ext_get?_iff := List.ext_getElem?_iff theorem ext_get_iff {l₁ l₂ : List α} : l₁ = l₂ ↔ l₁.length = l₂.length ∧ ∀ n h₁ h₂, get l₁ ⟨n, h₁⟩ = get l₂ ⟨n, h₂⟩ := by constructor · rintro rfl exact ⟨rfl, fun _ _ _ ↦ rfl⟩ · intro ⟨h₁, h₂⟩ exact ext_get h₁ h₂ theorem ext_getElem?_iff' {l₁ l₂ : List α} : l₁ = l₂ ↔ ∀ n < max l₁.length l₂.length, l₁[n]? = l₂[n]? := ⟨by rintro rfl _ _; rfl, ext_getElem?'⟩ @[deprecated (since := "2025-02-15")] alias ext_get?_iff' := ext_getElem?_iff' /-- If two lists `l₁` and `l₂` are the same length and `l₁[n]! = l₂[n]!` for all `n`, then the lists are equal. -/ theorem ext_getElem! [Inhabited α] (hl : length l₁ = length l₂) (h : ∀ n : ℕ, l₁[n]! = l₂[n]!) : l₁ = l₂ := ext_getElem hl fun n h₁ h₂ ↦ by simpa only [← getElem!_pos] using h n @[simp] theorem getElem_idxOf [DecidableEq α] {a : α} : ∀ {l : List α} (h : idxOf a l < l.length), l[idxOf a l] = a | b :: l, h => by by_cases h' : b = a <;> simp [h', if_pos, if_false, getElem_idxOf] @[deprecated (since := "2025-01-30")] alias getElem_indexOf := getElem_idxOf -- This is incorrectly named and should be `get_idxOf`; -- this already exists, so will require a deprecation dance. theorem idxOf_get [DecidableEq α] {a : α} {l : List α} (h) : get l ⟨idxOf a l, h⟩ = a := by simp @[deprecated (since := "2025-01-30")] alias indexOf_get := idxOf_get @[simp] theorem getElem?_idxOf [DecidableEq α] {a : α} {l : List α} (h : a ∈ l) : l[idxOf a l]? = some a := by rw [getElem?_eq_getElem, getElem_idxOf (idxOf_lt_length_iff.2 h)] @[deprecated (since := "2025-01-30")] alias getElem?_indexOf := getElem?_idxOf @[deprecated (since := "2025-02-15")] alias idxOf_get? := getElem?_idxOf @[deprecated (since := "2025-01-30")] alias indexOf_get? := getElem?_idxOf theorem idxOf_inj [DecidableEq α] {l : List α} {x y : α} (hx : x ∈ l) (hy : y ∈ l) : idxOf x l = idxOf y l ↔ x = y := ⟨fun h => by have x_eq_y : get l ⟨idxOf x l, idxOf_lt_length_iff.2 hx⟩ = get l ⟨idxOf y l, idxOf_lt_length_iff.2 hy⟩ := by simp only [h] simp only [idxOf_get] at x_eq_y; exact x_eq_y, fun h => by subst h; rfl⟩ @[deprecated (since := "2025-01-30")] alias indexOf_inj := idxOf_inj theorem get_reverse' (l : List α) (n) (hn') : l.reverse.get n = l.get ⟨l.length - 1 - n, hn'⟩ := by simp theorem eq_cons_of_length_one {l : List α} (h : l.length = 1) : l = [l.get ⟨0, by omega⟩] := by refine ext_get (by convert h) fun n h₁ h₂ => ?_ simp congr omega end deprecated @[simp] theorem getElem_set_of_ne {l : List α} {i j : ℕ} (h : i ≠ j) (a : α) (hj : j < (l.set i a).length) : (l.set i a)[j] = l[j]'(by simpa using hj) := by rw [← Option.some_inj, ← List.getElem?_eq_getElem, List.getElem?_set_ne h, List.getElem?_eq_getElem] /-! ### map -/ -- `List.map_const` (the version with `Function.const` instead of a lambda) is already tagged -- `simp` in Core -- TODO: Upstream the tagging to Core? attribute [simp] map_const' theorem flatMap_pure_eq_map (f : α → β) (l : List α) : l.flatMap (pure ∘ f) = map f l := .symm <| map_eq_flatMap .. theorem flatMap_congr {l : List α} {f g : α → List β} (h : ∀ x ∈ l, f x = g x) : l.flatMap f = l.flatMap g := (congr_arg List.flatten <| map_congr_left h :) theorem infix_flatMap_of_mem {a : α} {as : List α} (h : a ∈ as) (f : α → List α) : f a <:+: as.flatMap f := infix_of_mem_flatten (mem_map_of_mem h) @[simp] theorem map_eq_map {α β} (f : α → β) (l : List α) : f <$> l = map f l := rfl /-- A single `List.map` of a composition of functions is equal to composing a `List.map` with another `List.map`, fully applied. This is the reverse direction of `List.map_map`. -/ theorem comp_map (h : β → γ) (g : α → β) (l : List α) : map (h ∘ g) l = map h (map g l) := map_map.symm /-- Composing a `List.map` with another `List.map` is equal to a single `List.map` of composed functions. -/ @[simp] theorem map_comp_map (g : β → γ) (f : α → β) : map g ∘ map f = map (g ∘ f) := by ext l; rw [comp_map, Function.comp_apply] section map_bijectivity theorem _root_.Function.LeftInverse.list_map {f : α → β} {g : β → α} (h : LeftInverse f g) : LeftInverse (map f) (map g) | [] => by simp_rw [map_nil] | x :: xs => by simp_rw [map_cons, h x, h.list_map xs] nonrec theorem _root_.Function.RightInverse.list_map {f : α → β} {g : β → α} (h : RightInverse f g) : RightInverse (map f) (map g) := h.list_map nonrec theorem _root_.Function.Involutive.list_map {f : α → α} (h : Involutive f) : Involutive (map f) := Function.LeftInverse.list_map h @[simp] theorem map_leftInverse_iff {f : α → β} {g : β → α} : LeftInverse (map f) (map g) ↔ LeftInverse f g := ⟨fun h x => by injection h [x], (·.list_map)⟩ @[simp] theorem map_rightInverse_iff {f : α → β} {g : β → α} : RightInverse (map f) (map g) ↔ RightInverse f g := map_leftInverse_iff @[simp] theorem map_involutive_iff {f : α → α} : Involutive (map f) ↔ Involutive f := map_leftInverse_iff theorem _root_.Function.Injective.list_map {f : α → β} (h : Injective f) : Injective (map f) | [], [], _ => rfl | x :: xs, y :: ys, hxy => by injection hxy with hxy hxys rw [h hxy, h.list_map hxys] @[simp] theorem map_injective_iff {f : α → β} : Injective (map f) ↔ Injective f := by refine ⟨fun h x y hxy => ?_, (·.list_map)⟩ suffices [x] = [y] by simpa using this apply h simp [hxy] theorem _root_.Function.Surjective.list_map {f : α → β} (h : Surjective f) : Surjective (map f) := let ⟨_, h⟩ := h.hasRightInverse; h.list_map.surjective @[simp] theorem map_surjective_iff {f : α → β} : Surjective (map f) ↔ Surjective f := by refine ⟨fun h x => ?_, (·.list_map)⟩ let ⟨[y], hxy⟩ := h [x] exact ⟨_, List.singleton_injective hxy⟩ theorem _root_.Function.Bijective.list_map {f : α → β} (h : Bijective f) : Bijective (map f) := ⟨h.1.list_map, h.2.list_map⟩ @[simp] theorem map_bijective_iff {f : α → β} : Bijective (map f) ↔ Bijective f := by simp_rw [Function.Bijective, map_injective_iff, map_surjective_iff] end map_bijectivity theorem eq_of_mem_map_const {b₁ b₂ : β} {l : List α} (h : b₁ ∈ map (const α b₂) l) : b₁ = b₂ := by rw [map_const] at h; exact eq_of_mem_replicate h /-- `eq_nil_or_concat` in simp normal form -/ lemma eq_nil_or_concat' (l : List α) : l = [] ∨ ∃ L b, l = L ++ [b] := by simpa using l.eq_nil_or_concat /-! ### foldl, foldr -/ theorem foldl_ext (f g : α → β → α) (a : α) {l : List β} (H : ∀ a : α, ∀ b ∈ l, f a b = g a b) : foldl f a l = foldl g a l := by induction l generalizing a with | nil => rfl | cons hd tl ih => unfold foldl rw [ih _ fun a b bin => H a b <| mem_cons_of_mem _ bin, H a hd mem_cons_self] theorem foldr_ext (f g : α → β → β) (b : β) {l : List α} (H : ∀ a ∈ l, ∀ b : β, f a b = g a b) : foldr f b l = foldr g b l := by induction l with | nil => rfl | cons hd tl ih => ?_ simp only [mem_cons, or_imp, forall_and, forall_eq] at H simp only [foldr, ih H.2, H.1] theorem foldl_concat (f : β → α → β) (b : β) (x : α) (xs : List α) : List.foldl f b (xs ++ [x]) = f (List.foldl f b xs) x := by simp only [List.foldl_append, List.foldl] theorem foldr_concat (f : α → β → β) (b : β) (x : α) (xs : List α) : List.foldr f b (xs ++ [x]) = (List.foldr f (f x b) xs) := by simp only [List.foldr_append, List.foldr] theorem foldl_fixed' {f : α → β → α} {a : α} (hf : ∀ b, f a b = a) : ∀ l : List β, foldl f a l = a | [] => rfl | b :: l => by rw [foldl_cons, hf b, foldl_fixed' hf l] theorem foldr_fixed' {f : α → β → β} {b : β} (hf : ∀ a, f a b = b) : ∀ l : List α, foldr f b l = b | [] => rfl | a :: l => by rw [foldr_cons, foldr_fixed' hf l, hf a] @[simp] theorem foldl_fixed {a : α} : ∀ l : List β, foldl (fun a _ => a) a l = a := foldl_fixed' fun _ => rfl @[simp] theorem foldr_fixed {b : β} : ∀ l : List α, foldr (fun _ b => b) b l = b := foldr_fixed' fun _ => rfl @[deprecated foldr_cons_nil (since := "2025-02-10")] theorem foldr_eta (l : List α) : foldr cons [] l = l := foldr_cons_nil theorem reverse_foldl {l : List α} : reverse (foldl (fun t h => h :: t) [] l) = l := by simp theorem foldl_hom₂ (l : List ι) (f : α → β → γ) (op₁ : α → ι → α) (op₂ : β → ι → β) (op₃ : γ → ι → γ) (a : α) (b : β) (h : ∀ a b i, f (op₁ a i) (op₂ b i) = op₃ (f a b) i) : foldl op₃ (f a b) l = f (foldl op₁ a l) (foldl op₂ b l) := Eq.symm <| by revert a b induction l <;> intros <;> [rfl; simp only [*, foldl]] theorem foldr_hom₂ (l : List ι) (f : α → β → γ) (op₁ : ι → α → α) (op₂ : ι → β → β) (op₃ : ι → γ → γ) (a : α) (b : β) (h : ∀ a b i, f (op₁ i a) (op₂ i b) = op₃ i (f a b)) : foldr op₃ (f a b) l = f (foldr op₁ a l) (foldr op₂ b l) := by revert a induction l <;> intros <;> [rfl; simp only [*, foldr]] theorem injective_foldl_comp {l : List (α → α)} {f : α → α} (hl : ∀ f ∈ l, Function.Injective f) (hf : Function.Injective f) : Function.Injective (@List.foldl (α → α) (α → α) Function.comp f l) := by induction l generalizing f with | nil => exact hf | cons lh lt l_ih => apply l_ih fun _ h => hl _ (List.mem_cons_of_mem _ h) apply Function.Injective.comp hf apply hl _ mem_cons_self /-- Consider two lists `l₁` and `l₂` with designated elements `a₁` and `a₂` somewhere in them: `l₁ = x₁ ++ [a₁] ++ z₁` and `l₂ = x₂ ++ [a₂] ++ z₂`. Assume the designated element `a₂` is present in neither `x₁` nor `z₁`. We conclude that the lists are equal (`l₁ = l₂`) if and only if their respective parts are equal (`x₁ = x₂ ∧ a₁ = a₂ ∧ z₁ = z₂`). -/ lemma append_cons_inj_of_not_mem {x₁ x₂ z₁ z₂ : List α} {a₁ a₂ : α} (notin_x : a₂ ∉ x₁) (notin_z : a₂ ∉ z₁) : x₁ ++ a₁ :: z₁ = x₂ ++ a₂ :: z₂ ↔ x₁ = x₂ ∧ a₁ = a₂ ∧ z₁ = z₂ := by constructor · simp only [append_eq_append_iff, cons_eq_append_iff, cons_eq_cons] rintro (⟨c, rfl, ⟨rfl, rfl, rfl⟩ | ⟨d, rfl, rfl⟩⟩ | ⟨c, rfl, ⟨rfl, rfl, rfl⟩ | ⟨d, rfl, rfl⟩⟩) <;> simp_all · rintro ⟨rfl, rfl, rfl⟩ rfl section FoldlEqFoldr -- foldl and foldr coincide when f is commutative and associative variable {f : α → α → α} theorem foldl1_eq_foldr1 [hassoc : Std.Associative f] : ∀ a b l, foldl f a (l ++ [b]) = foldr f b (a :: l) | _, _, nil => rfl | a, b, c :: l => by simp only [cons_append, foldl_cons, foldr_cons, foldl1_eq_foldr1 _ _ l] rw [hassoc.assoc] theorem foldl_eq_of_comm_of_assoc [hcomm : Std.Commutative f] [hassoc : Std.Associative f] : ∀ a b l, foldl f a (b :: l) = f b (foldl f a l) | a, b, nil => hcomm.comm a b | a, b, c :: l => by simp only [foldl_cons] have : RightCommutative f := inferInstance rw [← foldl_eq_of_comm_of_assoc .., this.right_comm, foldl_cons] theorem foldl_eq_foldr [Std.Commutative f] [Std.Associative f] : ∀ a l, foldl f a l = foldr f a l | _, nil => rfl | a, b :: l => by simp only [foldr_cons, foldl_eq_of_comm_of_assoc] rw [foldl_eq_foldr a l] end FoldlEqFoldr section FoldlEqFoldlr' variable {f : α → β → α} variable (hf : ∀ a b c, f (f a b) c = f (f a c) b) include hf theorem foldl_eq_of_comm' : ∀ a b l, foldl f a (b :: l) = f (foldl f a l) b | _, _, [] => rfl | a, b, c :: l => by rw [foldl, foldl, foldl, ← foldl_eq_of_comm' .., foldl, hf] theorem foldl_eq_foldr' : ∀ a l, foldl f a l = foldr (flip f) a l | _, [] => rfl | a, b :: l => by rw [foldl_eq_of_comm' hf, foldr, foldl_eq_foldr' ..]; rfl end FoldlEqFoldlr' section FoldlEqFoldlr' variable {f : α → β → β} theorem foldr_eq_of_comm' (hf : ∀ a b c, f a (f b c) = f b (f a c)) : ∀ a b l, foldr f a (b :: l) = foldr f (f b a) l | _, _, [] => rfl | a, b, c :: l => by rw [foldr, foldr, foldr, hf, ← foldr_eq_of_comm' hf ..]; rfl end FoldlEqFoldlr' section variable {op : α → α → α} [ha : Std.Associative op] /-- Notation for `op a b`. -/ local notation a " ⋆ " b => op a b /-- Notation for `foldl op a l`. -/ local notation l " <*> " a => foldl op a l theorem foldl_op_eq_op_foldr_assoc : ∀ {l : List α} {a₁ a₂}, ((l <*> a₁) ⋆ a₂) = a₁ ⋆ l.foldr (· ⋆ ·) a₂ | [], _, _ => rfl | a :: l, a₁, a₂ => by simp only [foldl_cons, foldr_cons, foldl_assoc, ha.assoc]; rw [foldl_op_eq_op_foldr_assoc] variable [hc : Std.Commutative op] theorem foldl_assoc_comm_cons {l : List α} {a₁ a₂} : ((a₁ :: l) <*> a₂) = a₁ ⋆ l <*> a₂ := by rw [foldl_cons, hc.comm, foldl_assoc] end /-! ### foldlM, foldrM, mapM -/ section FoldlMFoldrM variable {m : Type v → Type w} [Monad m] variable [LawfulMonad m] theorem foldrM_eq_foldr (f : α → β → m β) (b l) : foldrM f b l = foldr (fun a mb => mb >>= f a) (pure b) l := by induction l <;> simp [*] theorem foldlM_eq_foldl (f : β → α → m β) (b l) : List.foldlM f b l = foldl (fun mb a => mb >>= fun b => f b a) (pure b) l := by suffices h : ∀ mb : m β, (mb >>= fun b => List.foldlM f b l) = foldl (fun mb a => mb >>= fun b => f b a) mb l by simp [← h (pure b)] induction l with | nil => intro; simp | cons _ _ l_ih => intro; simp only [List.foldlM, foldl, ← l_ih, functor_norm] end FoldlMFoldrM /-! ### intersperse -/ @[deprecated (since := "2025-02-07")] alias intersperse_singleton := intersperse_single @[deprecated (since := "2025-02-07")] alias intersperse_cons_cons := intersperse_cons₂ /-! ### map for partial functions -/ @[deprecated "Deprecated without replacement." (since := "2025-02-07")] theorem sizeOf_lt_sizeOf_of_mem [SizeOf α] {x : α} {l : List α} (hx : x ∈ l) : SizeOf.sizeOf x < SizeOf.sizeOf l := by induction l with | nil => ?_ | cons h t ih => ?_ <;> cases hx <;> rw [cons.sizeOf_spec] · omega · specialize ih ‹_› omega /-! ### filter -/ theorem length_eq_length_filter_add {l : List (α)} (f : α → Bool) : l.length = (l.filter f).length + (l.filter (! f ·)).length := by simp_rw [← List.countP_eq_length_filter, l.length_eq_countP_add_countP f, Bool.not_eq_true, Bool.decide_eq_false] /-! ### filterMap -/ theorem filterMap_eq_flatMap_toList (f : α → Option β) (l : List α) : l.filterMap f = l.flatMap fun a ↦ (f a).toList := by induction l with | nil => ?_ | cons a l ih => ?_ <;> simp [filterMap_cons] rcases f a <;> simp [ih] theorem filterMap_congr {f g : α → Option β} {l : List α} (h : ∀ x ∈ l, f x = g x) : l.filterMap f = l.filterMap g := by induction l <;> simp_all [filterMap_cons] theorem filterMap_eq_map_iff_forall_eq_some {f : α → Option β} {g : α → β} {l : List α} : l.filterMap f = l.map g ↔ ∀ x ∈ l, f x = some (g x) where mp := by induction l with | nil => simp | cons a l ih => ?_ rcases ha : f a with - | b <;> simp [ha, filterMap_cons] · intro h simpa [show (filterMap f l).length = l.length + 1 from by simp[h], Nat.add_one_le_iff] using List.length_filterMap_le f l · rintro rfl h exact ⟨rfl, ih h⟩ mpr h := Eq.trans (filterMap_congr <| by simpa) (congr_fun filterMap_eq_map _) /-! ### filter -/ section Filter variable {p : α → Bool} theorem filter_singleton {a : α} : [a].filter p = bif p a then [a] else [] := rfl theorem filter_eq_foldr (p : α → Bool) (l : List α) : filter p l = foldr (fun a out => bif p a then a :: out else out) [] l := by induction l <;> simp [*, filter]; rfl #adaptation_note /-- nightly-2024-07-27 This has to be temporarily renamed to avoid an unintentional collision. The prime should be removed at nightly-2024-07-27. -/ @[simp] theorem filter_subset' (l : List α) : filter p l ⊆ l := filter_sublist.subset theorem of_mem_filter {a : α} {l} (h : a ∈ filter p l) : p a := (mem_filter.1 h).2 theorem mem_of_mem_filter {a : α} {l} (h : a ∈ filter p l) : a ∈ l := filter_subset' l h theorem mem_filter_of_mem {a : α} {l} (h₁ : a ∈ l) (h₂ : p a) : a ∈ filter p l := mem_filter.2 ⟨h₁, h₂⟩ @[deprecated (since := "2025-02-07")] alias monotone_filter_left := filter_subset variable (p) theorem monotone_filter_right (l : List α) ⦃p q : α → Bool⦄ (h : ∀ a, p a → q a) : l.filter p <+ l.filter q := by induction l with | nil => rfl | cons hd tl IH => by_cases hp : p hd · rw [filter_cons_of_pos hp, filter_cons_of_pos (h _ hp)] exact IH.cons_cons hd · rw [filter_cons_of_neg hp] by_cases hq : q hd · rw [filter_cons_of_pos hq] exact sublist_cons_of_sublist hd IH · rw [filter_cons_of_neg hq] exact IH lemma map_filter {f : α → β} (hf : Injective f) (l : List α) [DecidablePred fun b => ∃ a, p a ∧ f a = b] : (l.filter p).map f = (l.map f).filter fun b => ∃ a, p a ∧ f a = b := by simp [comp_def, filter_map, hf.eq_iff] @[deprecated (since := "2025-02-07")] alias map_filter' := map_filter lemma filter_attach' (l : List α) (p : {a // a ∈ l} → Bool) [DecidableEq α] : l.attach.filter p = (l.filter fun x => ∃ h, p ⟨x, h⟩).attach.map (Subtype.map id fun _ => mem_of_mem_filter) := by classical refine map_injective_iff.2 Subtype.coe_injective ?_ simp [comp_def, map_filter _ Subtype.coe_injective] lemma filter_attach (l : List α) (p : α → Bool) : (l.attach.filter fun x => p x : List {x // x ∈ l}) = (l.filter p).attach.map (Subtype.map id fun _ => mem_of_mem_filter) := map_injective_iff.2 Subtype.coe_injective <| by simp_rw [map_map, comp_def, Subtype.map, id, ← Function.comp_apply (g := Subtype.val), ← filter_map, attach_map_subtype_val] lemma filter_comm (q) (l : List α) : filter p (filter q l) = filter q (filter p l) := by simp [Bool.and_comm] @[simp] theorem filter_true (l : List α) : filter (fun _ => true) l = l := by induction l <;> simp [*, filter] @[simp] theorem filter_false (l : List α) : filter (fun _ => false) l = [] := by induction l <;> simp [*, filter] end Filter /-! ### eraseP -/ section eraseP variable {p : α → Bool} @[simp] theorem length_eraseP_add_one {l : List α} {a} (al : a ∈ l) (pa : p a) : (l.eraseP p).length + 1 = l.length := by let ⟨_, l₁, l₂, _, _, h₁, h₂⟩ := exists_of_eraseP al pa rw [h₂, h₁, length_append, length_append] rfl end eraseP /-! ### erase -/ section Erase variable [DecidableEq α] @[simp] theorem length_erase_add_one {a : α} {l : List α} (h : a ∈ l) : (l.erase a).length + 1 = l.length := by rw [erase_eq_eraseP, length_eraseP_add_one h (decide_eq_true rfl)] theorem map_erase [DecidableEq β] {f : α → β} (finj : Injective f) {a : α} (l : List α) : map f (l.erase a) = (map f l).erase (f a) := by have this : (a == ·) = (f a == f ·) := by ext b; simp [beq_eq_decide, finj.eq_iff] rw [erase_eq_eraseP, erase_eq_eraseP, eraseP_map, this]; rfl theorem map_foldl_erase [DecidableEq β] {f : α → β} (finj : Injective f) {l₁ l₂ : List α} : map f (foldl List.erase l₁ l₂) = foldl (fun l a => l.erase (f a)) (map f l₁) l₂ := by induction l₂ generalizing l₁ <;> [rfl; simp only [foldl_cons, map_erase finj, *]] theorem erase_getElem [DecidableEq ι] {l : List ι} {i : ℕ} (hi : i < l.length) : Perm (l.erase l[i]) (l.eraseIdx i) := by induction l generalizing i with | nil => simp | cons a l IH => cases i with | zero => simp | succ i => have hi' : i < l.length := by simpa using hi if ha : a = l[i] then simpa [ha] using .trans (perm_cons_erase (getElem_mem _)) (.cons _ (IH hi')) else simpa [ha] using IH hi' theorem length_eraseIdx_add_one {l : List ι} {i : ℕ} (h : i < l.length) : (l.eraseIdx i).length + 1 = l.length := by rw [length_eraseIdx] split <;> omega end Erase /-! ### diff -/ section Diff variable [DecidableEq α] @[simp] theorem map_diff [DecidableEq β] {f : α → β} (finj : Injective f) {l₁ l₂ : List α} : map f (l₁.diff l₂) = (map f l₁).diff (map f l₂) := by simp only [diff_eq_foldl, foldl_map, map_foldl_erase finj] @[deprecated (since := "2025-04-10")] alias erase_diff_erase_sublist_of_sublist := Sublist.erase_diff_erase_sublist end Diff section Choose variable (p : α → Prop) [DecidablePred p] (l : List α) theorem choose_spec (hp : ∃ a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) := (chooseX p l hp).property theorem choose_mem (hp : ∃ a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1 theorem choose_property (hp : ∃ a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2 end Choose /-! ### Forall -/ section Forall variable {p q : α → Prop} {l : List α} @[simp] theorem forall_cons (p : α → Prop) (x : α) : ∀ l : List α, Forall p (x :: l) ↔ p x ∧ Forall p l | [] => (and_iff_left_of_imp fun _ ↦ trivial).symm | _ :: _ => Iff.rfl @[simp] theorem forall_append {p : α → Prop} : ∀ {xs ys : List α}, Forall p (xs ++ ys) ↔ Forall p xs ∧ Forall p ys | [] => by simp | _ :: _ => by simp [forall_append, and_assoc] theorem forall_iff_forall_mem : ∀ {l : List α}, Forall p l ↔ ∀ x ∈ l, p x | [] => (iff_true_intro <| forall_mem_nil _).symm | x :: l => by rw [forall_mem_cons, forall_cons, forall_iff_forall_mem] theorem Forall.imp (h : ∀ x, p x → q x) : ∀ {l : List α}, Forall p l → Forall q l | [] => id | x :: l => by simp only [forall_cons, and_imp] rw [← and_imp] exact And.imp (h x) (Forall.imp h) @[simp] theorem forall_map_iff {p : β → Prop} (f : α → β) : Forall p (l.map f) ↔ Forall (p ∘ f) l := by induction l <;> simp [*] instance (p : α → Prop) [DecidablePred p] : DecidablePred (Forall p) := fun _ => decidable_of_iff' _ forall_iff_forall_mem end Forall /-! ### Miscellaneous lemmas -/ theorem get_attach (l : List α) (i) : (l.attach.get i).1 = l.get ⟨i, length_attach (l := l) ▸ i.2⟩ := by simp section Disjoint /-- The images of disjoint lists under a partially defined map are disjoint -/ theorem disjoint_pmap {p : α → Prop} {f : ∀ a : α, p a → β} {s t : List α} (hs : ∀ a ∈ s, p a) (ht : ∀ a ∈ t, p a) (hf : ∀ (a a' : α) (ha : p a) (ha' : p a'), f a ha = f a' ha' → a = a') (h : Disjoint s t) : Disjoint (s.pmap f hs) (t.pmap f ht) := by simp only [Disjoint, mem_pmap] rintro b ⟨a, ha, rfl⟩ ⟨a', ha', ha''⟩ apply h ha rwa [hf a a' (hs a ha) (ht a' ha') ha''.symm] /-- The images of disjoint lists under an injective map are disjoint -/ theorem disjoint_map {f : α → β} {s t : List α} (hf : Function.Injective f) (h : Disjoint s t) : Disjoint (s.map f) (t.map f) := by rw [← pmap_eq_map (fun _ _ ↦ trivial), ← pmap_eq_map (fun _ _ ↦ trivial)] exact disjoint_pmap _ _ (fun _ _ _ _ h' ↦ hf h') h alias Disjoint.map := disjoint_map theorem Disjoint.of_map {f : α → β} {s t : List α} (h : Disjoint (s.map f) (t.map f)) : Disjoint s t := fun _a has hat ↦ h (mem_map_of_mem has) (mem_map_of_mem hat) theorem Disjoint.map_iff {f : α → β} {s t : List α} (hf : Function.Injective f) : Disjoint (s.map f) (t.map f) ↔ Disjoint s t := ⟨fun h ↦ h.of_map, fun h ↦ h.map hf⟩ theorem Perm.disjoint_left {l₁ l₂ l : List α} (p : List.Perm l₁ l₂) : Disjoint l₁ l ↔ Disjoint l₂ l := by simp_rw [List.disjoint_left, p.mem_iff] theorem Perm.disjoint_right {l₁ l₂ l : List α} (p : List.Perm l₁ l₂) : Disjoint l l₁ ↔ Disjoint l l₂ := by simp_rw [List.disjoint_right, p.mem_iff] @[simp] theorem disjoint_reverse_left {l₁ l₂ : List α} : Disjoint l₁.reverse l₂ ↔ Disjoint l₁ l₂ := reverse_perm _ |>.disjoint_left @[simp] theorem disjoint_reverse_right {l₁ l₂ : List α} : Disjoint l₁ l₂.reverse ↔ Disjoint l₁ l₂ := reverse_perm _ |>.disjoint_right end Disjoint section lookup variable [BEq α] [LawfulBEq α] lemma lookup_graph (f : α → β) {a : α} {as : List α} (h : a ∈ as) : lookup a (as.map fun x => (x, f x)) = some (f a) := by induction as with | nil => exact (not_mem_nil h).elim | cons a' as ih => by_cases ha : a = a' · simp [ha, lookup_cons] · simpa [lookup_cons, beq_false_of_ne ha] using ih (List.mem_of_ne_of_mem ha h) end lookup section range' @[simp] lemma range'_0 (a b : ℕ) : range' a b 0 = replicate b a := by induction b with | zero => simp | succ b ih => simp [range'_succ, ih, replicate_succ] lemma left_le_of_mem_range' {a b s x : ℕ} (hx : x ∈ List.range' a b s) : a ≤ x := by obtain ⟨i, _, rfl⟩ := List.mem_range'.mp hx exact le_add_right a (s * i) end range' end List
Mathlib/Data/List/Basic.lean
3,563
3,564
/- Copyright (c) 2021 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import Mathlib.Order.ConditionallyCompleteLattice.Basic import Mathlib.Order.Cover import Mathlib.Order.Iterate /-! # Successor and predecessor This file defines successor and predecessor orders. `succ a`, the successor of an element `a : α` is the least element greater than `a`. `pred a` is the greatest element less than `a`. Typical examples include `ℕ`, `ℤ`, `ℕ+`, `Fin n`, but also `ENat`, the lexicographic order of a successor/predecessor order... ## Typeclasses * `SuccOrder`: Order equipped with a sensible successor function. * `PredOrder`: Order equipped with a sensible predecessor function. ## Implementation notes Maximal elements don't have a sensible successor. Thus the naïve typeclass ```lean class NaiveSuccOrder (α : Type*) [Preorder α] where (succ : α → α) (succ_le_iff : ∀ {a b}, succ a ≤ b ↔ a < b) (lt_succ_iff : ∀ {a b}, a < succ b ↔ a ≤ b) ``` can't apply to an `OrderTop` because plugging in `a = b = ⊤` into either of `succ_le_iff` and `lt_succ_iff` yields `⊤ < ⊤` (or more generally `m < m` for a maximal element `m`). The solution taken here is to remove the implications `≤ → <` and instead require that `a < succ a` for all non maximal elements (enforced by the combination of `le_succ` and the contrapositive of `max_of_succ_le`). The stricter condition of every element having a sensible successor can be obtained through the combination of `SuccOrder α` and `NoMaxOrder α`. -/ open Function OrderDual Set variable {α β : Type*} /-- Order equipped with a sensible successor function. -/ @[ext] class SuccOrder (α : Type*) [Preorder α] where /-- Successor function -/ succ : α → α /-- Proof of basic ordering with respect to `succ` -/ le_succ : ∀ a, a ≤ succ a /-- Proof of interaction between `succ` and maximal element -/ max_of_succ_le {a} : succ a ≤ a → IsMax a /-- Proof that `succ a` is the least element greater than `a` -/ succ_le_of_lt {a b} : a < b → succ a ≤ b /-- Order equipped with a sensible predecessor function. -/ @[ext] class PredOrder (α : Type*) [Preorder α] where /-- Predecessor function -/ pred : α → α /-- Proof of basic ordering with respect to `pred` -/ pred_le : ∀ a, pred a ≤ a /-- Proof of interaction between `pred` and minimal element -/ min_of_le_pred {a} : a ≤ pred a → IsMin a /-- Proof that `pred b` is the greatest element less than `b` -/ le_pred_of_lt {a b} : a < b → a ≤ pred b instance [Preorder α] [SuccOrder α] : PredOrder αᵒᵈ where pred := toDual ∘ SuccOrder.succ ∘ ofDual pred_le := by simp only [comp, OrderDual.forall, ofDual_toDual, toDual_le_toDual, SuccOrder.le_succ, implies_true] min_of_le_pred h := by apply SuccOrder.max_of_succ_le h le_pred_of_lt := by intro a b h; exact SuccOrder.succ_le_of_lt h instance [Preorder α] [PredOrder α] : SuccOrder αᵒᵈ where succ := toDual ∘ PredOrder.pred ∘ ofDual le_succ := by simp only [comp, OrderDual.forall, ofDual_toDual, toDual_le_toDual, PredOrder.pred_le, implies_true] max_of_succ_le h := by apply PredOrder.min_of_le_pred h succ_le_of_lt := by intro a b h; exact PredOrder.le_pred_of_lt h section Preorder variable [Preorder α] /-- A constructor for `SuccOrder α` usable when `α` has no maximal element. -/ def SuccOrder.ofSuccLeIff (succ : α → α) (hsucc_le_iff : ∀ {a b}, succ a ≤ b ↔ a < b) : SuccOrder α := { succ le_succ := fun _ => (hsucc_le_iff.1 le_rfl).le max_of_succ_le := fun ha => (lt_irrefl _ <| hsucc_le_iff.1 ha).elim succ_le_of_lt := fun h => hsucc_le_iff.2 h } /-- A constructor for `PredOrder α` usable when `α` has no minimal element. -/ def PredOrder.ofLePredIff (pred : α → α) (hle_pred_iff : ∀ {a b}, a ≤ pred b ↔ a < b) : PredOrder α := { pred pred_le := fun _ => (hle_pred_iff.1 le_rfl).le min_of_le_pred := fun ha => (lt_irrefl _ <| hle_pred_iff.1 ha).elim le_pred_of_lt := fun h => hle_pred_iff.2 h } end Preorder section LinearOrder variable [LinearOrder α] /-- A constructor for `SuccOrder α` for `α` a linear order. -/ @[simps] def SuccOrder.ofCore (succ : α → α) (hn : ∀ {a}, ¬IsMax a → ∀ b, a < b ↔ succ a ≤ b) (hm : ∀ a, IsMax a → succ a = a) : SuccOrder α := { succ succ_le_of_lt := fun {a b} => by_cases (fun h hab => (hm a h).symm ▸ hab.le) fun h => (hn h b).mp le_succ := fun a => by_cases (fun h => (hm a h).symm.le) fun h => le_of_lt <| by simpa using (hn h a).not max_of_succ_le := fun {a} => not_imp_not.mp fun h => by simpa using (hn h a).not } /-- A constructor for `PredOrder α` for `α` a linear order. -/ @[simps] def PredOrder.ofCore (pred : α → α) (hn : ∀ {a}, ¬IsMin a → ∀ b, b ≤ pred a ↔ b < a) (hm : ∀ a, IsMin a → pred a = a) : PredOrder α := { pred le_pred_of_lt := fun {a b} => by_cases (fun h hab => (hm b h).symm ▸ hab.le) fun h => (hn h a).mpr pred_le := fun a => by_cases (fun h => (hm a h).le) fun h => le_of_lt <| by simpa using (hn h a).not min_of_le_pred := fun {a} => not_imp_not.mp fun h => by simpa using (hn h a).not } variable (α) open Classical in /-- A well-order is a `SuccOrder`. -/ noncomputable def SuccOrder.ofLinearWellFoundedLT [WellFoundedLT α] : SuccOrder α := ofCore (fun a ↦ if h : (Ioi a).Nonempty then wellFounded_lt.min _ h else a) (fun ha _ ↦ by rw [not_isMax_iff] at ha simp_rw [Set.Nonempty, mem_Ioi, dif_pos ha] exact ⟨(wellFounded_lt.min_le · ha), lt_of_lt_of_le (wellFounded_lt.min_mem _ ha)⟩) fun _ ha ↦ dif_neg (not_not_intro ha <| not_isMax_iff.mpr ·) /-- A linear order with well-founded greater-than relation is a `PredOrder`. -/ noncomputable def PredOrder.ofLinearWellFoundedGT (α) [LinearOrder α] [WellFoundedGT α] : PredOrder α := letI := SuccOrder.ofLinearWellFoundedLT αᵒᵈ; inferInstanceAs (PredOrder αᵒᵈᵒᵈ) end LinearOrder /-! ### Successor order -/ namespace Order section Preorder variable [Preorder α] [SuccOrder α] {a b : α} /-- The successor of an element. If `a` is not maximal, then `succ a` is the least element greater than `a`. If `a` is maximal, then `succ a = a`. -/ def succ : α → α := SuccOrder.succ theorem le_succ : ∀ a : α, a ≤ succ a := SuccOrder.le_succ theorem max_of_succ_le {a : α} : succ a ≤ a → IsMax a := SuccOrder.max_of_succ_le theorem succ_le_of_lt {a b : α} : a < b → succ a ≤ b := SuccOrder.succ_le_of_lt alias _root_.LT.lt.succ_le := succ_le_of_lt @[simp] theorem succ_le_iff_isMax : succ a ≤ a ↔ IsMax a := ⟨max_of_succ_le, fun h => h <| le_succ _⟩ alias ⟨_root_.IsMax.of_succ_le, _root_.IsMax.succ_le⟩ := succ_le_iff_isMax @[simp] theorem lt_succ_iff_not_isMax : a < succ a ↔ ¬IsMax a := ⟨not_isMax_of_lt, fun ha => (le_succ a).lt_of_not_le fun h => ha <| max_of_succ_le h⟩ alias ⟨_, lt_succ_of_not_isMax⟩ := lt_succ_iff_not_isMax theorem wcovBy_succ (a : α) : a ⩿ succ a := ⟨le_succ a, fun _ hb => (succ_le_of_lt hb).not_lt⟩ theorem covBy_succ_of_not_isMax (h : ¬IsMax a) : a ⋖ succ a := (wcovBy_succ a).covBy_of_lt <| lt_succ_of_not_isMax h theorem lt_succ_of_le_of_not_isMax (hab : b ≤ a) (ha : ¬IsMax a) : b < succ a := hab.trans_lt <| lt_succ_of_not_isMax ha theorem succ_le_iff_of_not_isMax (ha : ¬IsMax a) : succ a ≤ b ↔ a < b := ⟨(lt_succ_of_not_isMax ha).trans_le, succ_le_of_lt⟩ lemma succ_lt_succ_of_not_isMax (h : a < b) (hb : ¬ IsMax b) : succ a < succ b := lt_succ_of_le_of_not_isMax (succ_le_of_lt h) hb @[simp, mono, gcongr] theorem succ_le_succ (h : a ≤ b) : succ a ≤ succ b := by by_cases hb : IsMax b · by_cases hba : b ≤ a · exact (hb <| hba.trans <| le_succ _).trans (le_succ _) · exact succ_le_of_lt ((h.lt_of_not_le hba).trans_le <| le_succ b) · rw [succ_le_iff_of_not_isMax fun ha => hb <| ha.mono h] apply lt_succ_of_le_of_not_isMax h hb theorem succ_mono : Monotone (succ : α → α) := fun _ _ => succ_le_succ /-- See also `Order.succ_eq_of_covBy`. -/ lemma le_succ_of_wcovBy (h : a ⩿ b) : b ≤ succ a := by obtain hab | ⟨-, hba⟩ := h.covBy_or_le_and_le · by_contra hba exact h.2 (lt_succ_of_not_isMax hab.lt.not_isMax) <| hab.lt.succ_le.lt_of_not_le hba · exact hba.trans (le_succ _) alias _root_.WCovBy.le_succ := le_succ_of_wcovBy theorem le_succ_iterate (k : ℕ) (x : α) : x ≤ succ^[k] x := id_le_iterate_of_id_le le_succ _ _ theorem isMax_iterate_succ_of_eq_of_lt {n m : ℕ} (h_eq : succ^[n] a = succ^[m] a) (h_lt : n < m) : IsMax (succ^[n] a) := by refine max_of_succ_le (le_trans ?_ h_eq.symm.le) rw [← iterate_succ_apply' succ] have h_le : n + 1 ≤ m := Nat.succ_le_of_lt h_lt exact Monotone.monotone_iterate_of_le_map succ_mono (le_succ a) h_le theorem isMax_iterate_succ_of_eq_of_ne {n m : ℕ} (h_eq : succ^[n] a = succ^[m] a) (h_ne : n ≠ m) : IsMax (succ^[n] a) := by rcases le_total n m with h | h · exact isMax_iterate_succ_of_eq_of_lt h_eq (lt_of_le_of_ne h h_ne) · rw [h_eq] exact isMax_iterate_succ_of_eq_of_lt h_eq.symm (lt_of_le_of_ne h h_ne.symm) theorem Iic_subset_Iio_succ_of_not_isMax (ha : ¬IsMax a) : Iic a ⊆ Iio (succ a) := fun _ => (lt_succ_of_le_of_not_isMax · ha) theorem Ici_succ_of_not_isMax (ha : ¬IsMax a) : Ici (succ a) = Ioi a := Set.ext fun _ => succ_le_iff_of_not_isMax ha theorem Icc_subset_Ico_succ_right_of_not_isMax (hb : ¬IsMax b) : Icc a b ⊆ Ico a (succ b) := by rw [← Ici_inter_Iio, ← Ici_inter_Iic] gcongr intro _ h apply lt_succ_of_le_of_not_isMax h hb theorem Ioc_subset_Ioo_succ_right_of_not_isMax (hb : ¬IsMax b) : Ioc a b ⊆ Ioo a (succ b) := by rw [← Ioi_inter_Iio, ← Ioi_inter_Iic] gcongr intro _ h apply Iic_subset_Iio_succ_of_not_isMax hb h theorem Icc_succ_left_of_not_isMax (ha : ¬IsMax a) : Icc (succ a) b = Ioc a b := by rw [← Ici_inter_Iic, Ici_succ_of_not_isMax ha, Ioi_inter_Iic] theorem Ico_succ_left_of_not_isMax (ha : ¬IsMax a) : Ico (succ a) b = Ioo a b := by rw [← Ici_inter_Iio, Ici_succ_of_not_isMax ha, Ioi_inter_Iio] section NoMaxOrder variable [NoMaxOrder α] theorem lt_succ (a : α) : a < succ a := lt_succ_of_not_isMax <| not_isMax a @[simp] theorem lt_succ_of_le : a ≤ b → a < succ b := (lt_succ_of_le_of_not_isMax · <| not_isMax b) @[simp] theorem succ_le_iff : succ a ≤ b ↔ a < b := succ_le_iff_of_not_isMax <| not_isMax a @[gcongr] theorem succ_lt_succ (hab : a < b) : succ a < succ b := by simp [hab] theorem succ_strictMono : StrictMono (succ : α → α) := fun _ _ => succ_lt_succ theorem covBy_succ (a : α) : a ⋖ succ a := covBy_succ_of_not_isMax <| not_isMax a theorem Iic_subset_Iio_succ (a : α) : Iic a ⊆ Iio (succ a) := by simp @[simp] theorem Ici_succ (a : α) : Ici (succ a) = Ioi a := Ici_succ_of_not_isMax <| not_isMax _ @[simp] theorem Icc_subset_Ico_succ_right (a b : α) : Icc a b ⊆ Ico a (succ b) := Icc_subset_Ico_succ_right_of_not_isMax <| not_isMax _ @[simp] theorem Ioc_subset_Ioo_succ_right (a b : α) : Ioc a b ⊆ Ioo a (succ b) :=
Ioc_subset_Ioo_succ_right_of_not_isMax <| not_isMax _ @[simp]
Mathlib/Order/SuccPred/Basic.lean
301
303
/- Copyright (c) 2019 Kim Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kim Morrison, Jakob von Raumer -/ import Mathlib.CategoryTheory.Limits.Shapes.FiniteProducts import Mathlib.CategoryTheory.Limits.Shapes.Kernels /-! # Biproducts and binary biproducts We introduce the notion of (finite) biproducts. Binary biproducts are defined in `CategoryTheory.Limits.Shapes.BinaryBiproducts`. These are slightly unusual relative to the other shapes in the library, as they are simultaneously limits and colimits. (Zero objects are similar; they are "biterminal".) For results about biproducts in preadditive categories see `CategoryTheory.Preadditive.Biproducts`. For biproducts indexed by a `Fintype J`, a `bicone` consists of a cone point `X` and morphisms `π j : X ⟶ F j` and `ι j : F j ⟶ X` for each `j`, such that `ι j ≫ π j'` is the identity when `j = j'` and zero otherwise. ## Notation As `⊕` is already taken for the sum of types, we introduce the notation `X ⊞ Y` for a binary biproduct. We introduce `⨁ f` for the indexed biproduct. ## Implementation notes Prior to https://github.com/leanprover-community/mathlib3/pull/14046, `HasFiniteBiproducts` required a `DecidableEq` instance on the indexing type. As this had no pay-off (everything about limits is non-constructive in mathlib), and occasional cost (constructing decidability instances appropriate for constructions involving the indexing type), we made everything classical. -/ noncomputable section universe w w' v u open CategoryTheory Functor namespace CategoryTheory.Limits variable {J : Type w} universe uC' uC uD' uD variable {C : Type uC} [Category.{uC'} C] [HasZeroMorphisms C] variable {D : Type uD} [Category.{uD'} D] [HasZeroMorphisms D] open scoped Classical in /-- A `c : Bicone F` is: * an object `c.pt` and * morphisms `π j : pt ⟶ F j` and `ι j : F j ⟶ pt` for each `j`, * such that `ι j ≫ π j'` is the identity when `j = j'` and zero otherwise. -/ structure Bicone (F : J → C) where pt : C π : ∀ j, pt ⟶ F j ι : ∀ j, F j ⟶ pt ι_π : ∀ j j', ι j ≫ π j' = if h : j = j' then eqToHom (congrArg F h) else 0 := by aesop attribute [inherit_doc Bicone] Bicone.pt Bicone.π Bicone.ι Bicone.ι_π @[reassoc (attr := simp)] theorem bicone_ι_π_self {F : J → C} (B : Bicone F) (j : J) : B.ι j ≫ B.π j = 𝟙 (F j) := by simpa using B.ι_π j j @[reassoc (attr := simp)] theorem bicone_ι_π_ne {F : J → C} (B : Bicone F) {j j' : J} (h : j ≠ j') : B.ι j ≫ B.π j' = 0 := by simpa [h] using B.ι_π j j' variable {F : J → C} /-- A bicone morphism between two bicones for the same diagram is a morphism of the bicone points which commutes with the cone and cocone legs. -/ structure BiconeMorphism {F : J → C} (A B : Bicone F) where /-- A morphism between the two vertex objects of the bicones -/ hom : A.pt ⟶ B.pt /-- The triangle consisting of the two natural transformations and `hom` commutes -/ wπ : ∀ j : J, hom ≫ B.π j = A.π j := by aesop_cat /-- The triangle consisting of the two natural transformations and `hom` commutes -/ wι : ∀ j : J, A.ι j ≫ hom = B.ι j := by aesop_cat attribute [reassoc (attr := simp)] BiconeMorphism.wι BiconeMorphism.wπ /-- The category of bicones on a given diagram. -/ @[simps] instance Bicone.category : Category (Bicone F) where Hom A B := BiconeMorphism A B comp f g := { hom := f.hom ≫ g.hom } id B := { hom := 𝟙 B.pt } -- Porting note: if we do not have `simps` automatically generate the lemma for simplifying -- the `hom` field of a category, we need to write the `ext` lemma in terms of the categorical -- morphism, rather than the underlying structure. @[ext] theorem BiconeMorphism.ext {c c' : Bicone F} (f g : c ⟶ c') (w : f.hom = g.hom) : f = g := by cases f cases g congr namespace Bicones /-- To give an isomorphism between cocones, it suffices to give an isomorphism between their vertices which commutes with the cocone maps. -/ @[aesop apply safe (rule_sets := [CategoryTheory]), simps] def ext {c c' : Bicone F} (φ : c.pt ≅ c'.pt) (wι : ∀ j, c.ι j ≫ φ.hom = c'.ι j := by aesop_cat) (wπ : ∀ j, φ.hom ≫ c'.π j = c.π j := by aesop_cat) : c ≅ c' where hom := { hom := φ.hom } inv := { hom := φ.inv wι := fun j => φ.comp_inv_eq.mpr (wι j).symm wπ := fun j => φ.inv_comp_eq.mpr (wπ j).symm } variable (F) in /-- A functor `G : C ⥤ D` sends bicones over `F` to bicones over `G.obj ∘ F` functorially. -/ @[simps] def functoriality (G : C ⥤ D) [Functor.PreservesZeroMorphisms G] : Bicone F ⥤ Bicone (G.obj ∘ F) where obj A := { pt := G.obj A.pt π := fun j => G.map (A.π j) ι := fun j => G.map (A.ι j) ι_π := fun i j => (Functor.map_comp _ _ _).symm.trans <| by rw [A.ι_π] aesop_cat } map f := { hom := G.map f.hom wπ := fun j => by simp [-BiconeMorphism.wπ, ← f.wπ j] wι := fun j => by simp [-BiconeMorphism.wι, ← f.wι j] } variable (G : C ⥤ D) instance functoriality_full [G.PreservesZeroMorphisms] [G.Full] [G.Faithful] : (functoriality F G).Full where map_surjective t := ⟨{ hom := G.preimage t.hom wι := fun j => G.map_injective (by simpa using t.wι j) wπ := fun j => G.map_injective (by simpa using t.wπ j) }, by aesop_cat⟩ instance functoriality_faithful [G.PreservesZeroMorphisms] [G.Faithful] : (functoriality F G).Faithful where map_injective {_X} {_Y} f g h := BiconeMorphism.ext f g <| G.map_injective <| congr_arg BiconeMorphism.hom h end Bicones namespace Bicone attribute [local aesop safe tactic (rule_sets := [CategoryTheory])] CategoryTheory.Discrete.discreteCases -- Porting note: would it be okay to use this more generally? attribute [local aesop safe cases (rule_sets := [CategoryTheory])] Eq /-- Extract the cone from a bicone. -/ def toConeFunctor : Bicone F ⥤ Cone (Discrete.functor F) where obj B := { pt := B.pt, π := { app := fun j => B.π j.as } } map {_ _} F := { hom := F.hom, w := fun _ => F.wπ _ } /-- A shorthand for `toConeFunctor.obj` -/ abbrev toCone (B : Bicone F) : Cone (Discrete.functor F) := toConeFunctor.obj B -- TODO Consider changing this API to `toFan (B : Bicone F) : Fan F`. @[simp] theorem toCone_pt (B : Bicone F) : B.toCone.pt = B.pt := rfl @[simp] theorem toCone_π_app (B : Bicone F) (j : Discrete J) : B.toCone.π.app j = B.π j.as := rfl theorem toCone_π_app_mk (B : Bicone F) (j : J) : B.toCone.π.app ⟨j⟩ = B.π j := rfl @[simp] theorem toCone_proj (B : Bicone F) (j : J) : Fan.proj B.toCone j = B.π j := rfl /-- Extract the cocone from a bicone. -/ def toCoconeFunctor : Bicone F ⥤ Cocone (Discrete.functor F) where obj B := { pt := B.pt, ι := { app := fun j => B.ι j.as } } map {_ _} F := { hom := F.hom, w := fun _ => F.wι _ } /-- A shorthand for `toCoconeFunctor.obj` -/ abbrev toCocone (B : Bicone F) : Cocone (Discrete.functor F) := toCoconeFunctor.obj B @[simp] theorem toCocone_pt (B : Bicone F) : B.toCocone.pt = B.pt := rfl @[simp] theorem toCocone_ι_app (B : Bicone F) (j : Discrete J) : B.toCocone.ι.app j = B.ι j.as := rfl @[simp] theorem toCocone_inj (B : Bicone F) (j : J) : Cofan.inj B.toCocone j = B.ι j := rfl theorem toCocone_ι_app_mk (B : Bicone F) (j : J) : B.toCocone.ι.app ⟨j⟩ = B.ι j := rfl open scoped Classical in /-- We can turn any limit cone over a discrete collection of objects into a bicone. -/ @[simps] def ofLimitCone {f : J → C} {t : Cone (Discrete.functor f)} (ht : IsLimit t) : Bicone f where pt := t.pt π j := t.π.app ⟨j⟩ ι j := ht.lift (Fan.mk _ fun j' => if h : j = j' then eqToHom (congr_arg f h) else 0) ι_π j j' := by simp open scoped Classical in theorem ι_of_isLimit {f : J → C} {t : Bicone f} (ht : IsLimit t.toCone) (j : J) : t.ι j = ht.lift (Fan.mk _ fun j' => if h : j = j' then eqToHom (congr_arg f h) else 0) := ht.hom_ext fun j' => by rw [ht.fac] simp [t.ι_π] open scoped Classical in /-- We can turn any colimit cocone over a discrete collection of objects into a bicone. -/ @[simps] def ofColimitCocone {f : J → C} {t : Cocone (Discrete.functor f)} (ht : IsColimit t) : Bicone f where pt := t.pt π j := ht.desc (Cofan.mk _ fun j' => if h : j' = j then eqToHom (congr_arg f h) else 0) ι j := t.ι.app ⟨j⟩ ι_π j j' := by simp open scoped Classical in theorem π_of_isColimit {f : J → C} {t : Bicone f} (ht : IsColimit t.toCocone) (j : J) : t.π j = ht.desc (Cofan.mk _ fun j' => if h : j' = j then eqToHom (congr_arg f h) else 0) := ht.hom_ext fun j' => by rw [ht.fac] simp [t.ι_π] /-- Structure witnessing that a bicone is both a limit cone and a colimit cocone. -/ structure IsBilimit {F : J → C} (B : Bicone F) where isLimit : IsLimit B.toCone isColimit : IsColimit B.toCocone attribute [inherit_doc IsBilimit] IsBilimit.isLimit IsBilimit.isColimit attribute [simp] IsBilimit.mk.injEq attribute [local ext] Bicone.IsBilimit instance subsingleton_isBilimit {f : J → C} {c : Bicone f} : Subsingleton c.IsBilimit := ⟨fun _ _ => Bicone.IsBilimit.ext (Subsingleton.elim _ _) (Subsingleton.elim _ _)⟩ section Whisker variable {K : Type w'} /-- Whisker a bicone with an equivalence between the indexing types. -/ @[simps] def whisker {f : J → C} (c : Bicone f) (g : K ≃ J) : Bicone (f ∘ g) where pt := c.pt π k := c.π (g k) ι k := c.ι (g k) ι_π k k' := by simp only [c.ι_π] split_ifs with h h' h' <;> simp [Equiv.apply_eq_iff_eq g] at h h' <;> tauto /-- Taking the cone of a whiskered bicone results in a cone isomorphic to one gained by whiskering the cone and postcomposing with a suitable isomorphism. -/ def whiskerToCone {f : J → C} (c : Bicone f) (g : K ≃ J) : (c.whisker g).toCone ≅ (Cones.postcompose (Discrete.functorComp f g).inv).obj (c.toCone.whisker (Discrete.functor (Discrete.mk ∘ g))) := Cones.ext (Iso.refl _) (by simp) /-- Taking the cocone of a whiskered bicone results in a cone isomorphic to one gained by whiskering the cocone and precomposing with a suitable isomorphism. -/ def whiskerToCocone {f : J → C} (c : Bicone f) (g : K ≃ J) : (c.whisker g).toCocone ≅ (Cocones.precompose (Discrete.functorComp f g).hom).obj (c.toCocone.whisker (Discrete.functor (Discrete.mk ∘ g))) := Cocones.ext (Iso.refl _) (by simp) /-- Whiskering a bicone with an equivalence between types preserves being a bilimit bicone. -/ noncomputable def whiskerIsBilimitIff {f : J → C} (c : Bicone f) (g : K ≃ J) : (c.whisker g).IsBilimit ≃ c.IsBilimit := by refine equivOfSubsingletonOfSubsingleton (fun hc => ⟨?_, ?_⟩) fun hc => ⟨?_, ?_⟩ · let this := IsLimit.ofIsoLimit hc.isLimit (Bicone.whiskerToCone c g) let this := (IsLimit.postcomposeHomEquiv (Discrete.functorComp f g).symm _) this exact IsLimit.ofWhiskerEquivalence (Discrete.equivalence g) this · let this := IsColimit.ofIsoColimit hc.isColimit (Bicone.whiskerToCocone c g) let this := (IsColimit.precomposeHomEquiv (Discrete.functorComp f g) _) this exact IsColimit.ofWhiskerEquivalence (Discrete.equivalence g) this · apply IsLimit.ofIsoLimit _ (Bicone.whiskerToCone c g).symm apply (IsLimit.postcomposeHomEquiv (Discrete.functorComp f g).symm _).symm _ exact IsLimit.whiskerEquivalence hc.isLimit (Discrete.equivalence g) · apply IsColimit.ofIsoColimit _ (Bicone.whiskerToCocone c g).symm apply (IsColimit.precomposeHomEquiv (Discrete.functorComp f g) _).symm _ exact IsColimit.whiskerEquivalence hc.isColimit (Discrete.equivalence g) end Whisker end Bicone /-- A bicone over `F : J → C`, which is both a limit cone and a colimit cocone. -/ structure LimitBicone (F : J → C) where bicone : Bicone F isBilimit : bicone.IsBilimit attribute [inherit_doc LimitBicone] LimitBicone.bicone LimitBicone.isBilimit /-- `HasBiproduct F` expresses the mere existence of a bicone which is simultaneously a limit and a colimit of the diagram `F`. -/ class HasBiproduct (F : J → C) : Prop where mk' :: exists_biproduct : Nonempty (LimitBicone F) attribute [inherit_doc HasBiproduct] HasBiproduct.exists_biproduct theorem HasBiproduct.mk {F : J → C} (d : LimitBicone F) : HasBiproduct F := ⟨Nonempty.intro d⟩ /-- Use the axiom of choice to extract explicit `BiproductData F` from `HasBiproduct F`. -/ def getBiproductData (F : J → C) [HasBiproduct F] : LimitBicone F := Classical.choice HasBiproduct.exists_biproduct /-- A bicone for `F` which is both a limit cone and a colimit cocone. -/ def biproduct.bicone (F : J → C) [HasBiproduct F] : Bicone F := (getBiproductData F).bicone /-- `biproduct.bicone F` is a bilimit bicone. -/ def biproduct.isBilimit (F : J → C) [HasBiproduct F] : (biproduct.bicone F).IsBilimit := (getBiproductData F).isBilimit /-- `biproduct.bicone F` is a limit cone. -/ def biproduct.isLimit (F : J → C) [HasBiproduct F] : IsLimit (biproduct.bicone F).toCone := (getBiproductData F).isBilimit.isLimit /-- `biproduct.bicone F` is a colimit cocone. -/ def biproduct.isColimit (F : J → C) [HasBiproduct F] : IsColimit (biproduct.bicone F).toCocone := (getBiproductData F).isBilimit.isColimit instance (priority := 100) hasProduct_of_hasBiproduct [HasBiproduct F] : HasProduct F := HasLimit.mk { cone := (biproduct.bicone F).toCone isLimit := biproduct.isLimit F } instance (priority := 100) hasCoproduct_of_hasBiproduct [HasBiproduct F] : HasCoproduct F := HasColimit.mk { cocone := (biproduct.bicone F).toCocone isColimit := biproduct.isColimit F } variable (J C) /-- `C` has biproducts of shape `J` if we have a limit and a colimit, with the same cone points, of every function `F : J → C`. -/ class HasBiproductsOfShape : Prop where has_biproduct : ∀ F : J → C, HasBiproduct F attribute [instance 100] HasBiproductsOfShape.has_biproduct /-- `HasFiniteBiproducts C` represents a choice of biproduct for every family of objects in `C` indexed by a finite type. -/ class HasFiniteBiproducts : Prop where out : ∀ n, HasBiproductsOfShape (Fin n) C attribute [inherit_doc HasFiniteBiproducts] HasFiniteBiproducts.out variable {J} theorem hasBiproductsOfShape_of_equiv {K : Type w'} [HasBiproductsOfShape K C] (e : J ≃ K) : HasBiproductsOfShape J C := ⟨fun F => let ⟨⟨h⟩⟩ := HasBiproductsOfShape.has_biproduct (F ∘ e.symm) let ⟨c, hc⟩ := h HasBiproduct.mk <| by simpa only [Function.comp_def, e.symm_apply_apply] using LimitBicone.mk (c.whisker e) ((c.whiskerIsBilimitIff _).2 hc)⟩ instance (priority := 100) hasBiproductsOfShape_finite [HasFiniteBiproducts C] [Finite J] : HasBiproductsOfShape J C := by rcases Finite.exists_equiv_fin J with ⟨n, ⟨e⟩⟩ haveI : HasBiproductsOfShape (Fin n) C := HasFiniteBiproducts.out n exact hasBiproductsOfShape_of_equiv C e instance (priority := 100) hasFiniteProducts_of_hasFiniteBiproducts [HasFiniteBiproducts C] : HasFiniteProducts C where out _ := ⟨fun _ => hasLimit_of_iso Discrete.natIsoFunctor.symm⟩ instance (priority := 100) hasFiniteCoproducts_of_hasFiniteBiproducts [HasFiniteBiproducts C] : HasFiniteCoproducts C where out _ := ⟨fun _ => hasColimit_of_iso Discrete.natIsoFunctor⟩ instance (priority := 100) hasProductsOfShape_of_hasBiproductsOfShape [HasBiproductsOfShape J C] : HasProductsOfShape J C where has_limit _ := hasLimit_of_iso Discrete.natIsoFunctor.symm instance (priority := 100) hasCoproductsOfShape_of_hasBiproductsOfShape [HasBiproductsOfShape J C] : HasCoproductsOfShape J C where has_colimit _ := hasColimit_of_iso Discrete.natIsoFunctor variable {C} /-- The isomorphism between the specified limit and the specified colimit for a functor with a bilimit. -/ def biproductIso (F : J → C) [HasBiproduct F] : Limits.piObj F ≅ Limits.sigmaObj F := (IsLimit.conePointUniqueUpToIso (limit.isLimit _) (biproduct.isLimit F)).trans <| IsColimit.coconePointUniqueUpToIso (biproduct.isColimit F) (colimit.isColimit _) variable {J : Type w} {K : Type*} variable {C : Type u} [Category.{v} C] [HasZeroMorphisms C] /-- `biproduct f` computes the biproduct of a family of elements `f`. (It is defined as an abbreviation for `limit (Discrete.functor f)`, so for most facts about `biproduct f`, you will just use general facts about limits and colimits.) -/ abbrev biproduct (f : J → C) [HasBiproduct f] : C := (biproduct.bicone f).pt @[inherit_doc biproduct] notation "⨁ " f:20 => biproduct f /-- The projection onto a summand of a biproduct. -/ abbrev biproduct.π (f : J → C) [HasBiproduct f] (b : J) : ⨁ f ⟶ f b := (biproduct.bicone f).π b @[simp] theorem biproduct.bicone_π (f : J → C) [HasBiproduct f] (b : J) : (biproduct.bicone f).π b = biproduct.π f b := rfl /-- The inclusion into a summand of a biproduct. -/ abbrev biproduct.ι (f : J → C) [HasBiproduct f] (b : J) : f b ⟶ ⨁ f := (biproduct.bicone f).ι b @[simp] theorem biproduct.bicone_ι (f : J → C) [HasBiproduct f] (b : J) : (biproduct.bicone f).ι b = biproduct.ι f b := rfl /-- Note that as this lemma has an `if` in the statement, we include a `DecidableEq` argument. This means you may not be able to `simp` using this lemma unless you `open scoped Classical`. -/ @[reassoc] theorem biproduct.ι_π [DecidableEq J] (f : J → C) [HasBiproduct f] (j j' : J) : biproduct.ι f j ≫ biproduct.π f j' = if h : j = j' then eqToHom (congr_arg f h) else 0 := by convert (biproduct.bicone f).ι_π j j' @[reassoc] -- Porting note: both versions proven by simp theorem biproduct.ι_π_self (f : J → C) [HasBiproduct f] (j : J) : biproduct.ι f j ≫ biproduct.π f j = 𝟙 _ := by simp [biproduct.ι_π] @[reassoc (attr := simp)] theorem biproduct.ι_π_ne (f : J → C) [HasBiproduct f] {j j' : J} (h : j ≠ j') : biproduct.ι f j ≫ biproduct.π f j' = 0 := by simp [biproduct.ι_π, h] -- The `simpNF` linter incorrectly identifies these as simp lemmas that could never apply. -- It seems the side condition `w` is not applied by `simpNF`. -- https://github.com/leanprover-community/mathlib4/issues/5049 -- They are used by `simp` in `biproduct.whiskerEquiv` below. @[reassoc (attr := simp, nolint simpNF)] theorem biproduct.eqToHom_comp_ι (f : J → C) [HasBiproduct f] {j j' : J} (w : j = j') : eqToHom (by simp [w]) ≫ biproduct.ι f j' = biproduct.ι f j := by cases w simp -- The `simpNF` linter incorrectly identifies these as simp lemmas that could never apply. -- It seems the side condition `w` is not applied by `simpNF`. -- https://github.com/leanprover-community/mathlib4/issues/5049 -- They are used by `simp` in `biproduct.whiskerEquiv` below. @[reassoc (attr := simp, nolint simpNF)] theorem biproduct.π_comp_eqToHom (f : J → C) [HasBiproduct f] {j j' : J} (w : j = j') : biproduct.π f j ≫ eqToHom (by simp [w]) = biproduct.π f j' := by cases w simp /-- Given a collection of maps into the summands, we obtain a map into the biproduct. -/ abbrev biproduct.lift {f : J → C} [HasBiproduct f] {P : C} (p : ∀ b, P ⟶ f b) : P ⟶ ⨁ f := (biproduct.isLimit f).lift (Fan.mk P p) /-- Given a collection of maps out of the summands, we obtain a map out of the biproduct. -/ abbrev biproduct.desc {f : J → C} [HasBiproduct f] {P : C} (p : ∀ b, f b ⟶ P) : ⨁ f ⟶ P := (biproduct.isColimit f).desc (Cofan.mk P p) @[reassoc (attr := simp)] theorem biproduct.lift_π {f : J → C} [HasBiproduct f] {P : C} (p : ∀ b, P ⟶ f b) (j : J) : biproduct.lift p ≫ biproduct.π f j = p j := (biproduct.isLimit f).fac _ ⟨j⟩ @[reassoc (attr := simp)] theorem biproduct.ι_desc {f : J → C} [HasBiproduct f] {P : C} (p : ∀ b, f b ⟶ P) (j : J) : biproduct.ι f j ≫ biproduct.desc p = p j := (biproduct.isColimit f).fac _ ⟨j⟩ /-- Given a collection of maps between corresponding summands of a pair of biproducts indexed by the same type, we obtain a map between the biproducts. -/ abbrev biproduct.map {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ b, f b ⟶ g b) : ⨁ f ⟶ ⨁ g := IsLimit.map (biproduct.bicone f).toCone (biproduct.isLimit g) (Discrete.natTrans (fun j => p j.as)) /-- An alternative to `biproduct.map` constructed via colimits. This construction only exists in order to show it is equal to `biproduct.map`. -/ abbrev biproduct.map' {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ b, f b ⟶ g b) : ⨁ f ⟶ ⨁ g := IsColimit.map (biproduct.isColimit f) (biproduct.bicone g).toCocone (Discrete.natTrans fun j => p j.as) -- We put this at slightly higher priority than `biproduct.hom_ext'`, -- to get the matrix indices in the "right" order. @[ext 1001] theorem biproduct.hom_ext {f : J → C} [HasBiproduct f] {Z : C} (g h : Z ⟶ ⨁ f) (w : ∀ j, g ≫ biproduct.π f j = h ≫ biproduct.π f j) : g = h := (biproduct.isLimit f).hom_ext fun j => w j.as @[ext] theorem biproduct.hom_ext' {f : J → C} [HasBiproduct f] {Z : C} (g h : ⨁ f ⟶ Z) (w : ∀ j, biproduct.ι f j ≫ g = biproduct.ι f j ≫ h) : g = h := (biproduct.isColimit f).hom_ext fun j => w j.as /-- The canonical isomorphism between the chosen biproduct and the chosen product. -/ def biproduct.isoProduct (f : J → C) [HasBiproduct f] : ⨁ f ≅ ∏ᶜ f := IsLimit.conePointUniqueUpToIso (biproduct.isLimit f) (limit.isLimit _) @[simp] theorem biproduct.isoProduct_hom {f : J → C} [HasBiproduct f] : (biproduct.isoProduct f).hom = Pi.lift (biproduct.π f) := limit.hom_ext fun j => by simp [biproduct.isoProduct] @[simp] theorem biproduct.isoProduct_inv {f : J → C} [HasBiproduct f] : (biproduct.isoProduct f).inv = biproduct.lift (Pi.π f) := biproduct.hom_ext _ _ fun j => by simp [Iso.inv_comp_eq] /-- The canonical isomorphism between the chosen biproduct and the chosen coproduct. -/ def biproduct.isoCoproduct (f : J → C) [HasBiproduct f] : ⨁ f ≅ ∐ f := IsColimit.coconePointUniqueUpToIso (biproduct.isColimit f) (colimit.isColimit _) @[simp] theorem biproduct.isoCoproduct_inv {f : J → C} [HasBiproduct f] : (biproduct.isoCoproduct f).inv = Sigma.desc (biproduct.ι f) := colimit.hom_ext fun j => by simp [biproduct.isoCoproduct] @[simp] theorem biproduct.isoCoproduct_hom {f : J → C} [HasBiproduct f] : (biproduct.isoCoproduct f).hom = biproduct.desc (Sigma.ι f) := biproduct.hom_ext' _ _ fun j => by simp [← Iso.eq_comp_inv] /-- If a category has biproducts of a shape `J`, its `colim` and `lim` functor on diagrams over `J` are isomorphic. -/ @[simps!] def HasBiproductsOfShape.colimIsoLim [HasBiproductsOfShape J C] : colim (J := Discrete J) (C := C) ≅ lim := NatIso.ofComponents (fun F => (Sigma.isoColimit F).symm ≪≫ (biproduct.isoCoproduct _).symm ≪≫ biproduct.isoProduct _ ≪≫ Pi.isoLimit F) fun η => colimit.hom_ext fun ⟨i⟩ => limit.hom_ext fun ⟨j⟩ => by classical by_cases h : i = j <;> simp_all [h, Sigma.isoColimit, Pi.isoLimit, biproduct.ι_π, biproduct.ι_π_assoc] theorem biproduct.map_eq_map' {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ b, f b ⟶ g b) : biproduct.map p = biproduct.map' p := by classical ext dsimp simp only [Discrete.natTrans_app, Limits.IsColimit.ι_map_assoc, Limits.IsLimit.map_π, Category.assoc, ← Bicone.toCone_π_app_mk, ← biproduct.bicone_π, ← Bicone.toCocone_ι_app_mk, ← biproduct.bicone_ι] dsimp rw [biproduct.ι_π_assoc, biproduct.ι_π] split_ifs with h · subst h; simp · simp @[reassoc (attr := simp)] theorem biproduct.map_π {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) (j : J) : biproduct.map p ≫ biproduct.π g j = biproduct.π f j ≫ p j := Limits.IsLimit.map_π _ _ _ (Discrete.mk j) @[reassoc (attr := simp)] theorem biproduct.ι_map {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) (j : J) : biproduct.ι f j ≫ biproduct.map p = p j ≫ biproduct.ι g j := by rw [biproduct.map_eq_map'] apply Limits.IsColimit.ι_map (biproduct.isColimit f) (biproduct.bicone g).toCocone (Discrete.natTrans fun j => p j.as) (Discrete.mk j) @[reassoc (attr := simp)] theorem biproduct.map_desc {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) {P : C} (k : ∀ j, g j ⟶ P) : biproduct.map p ≫ biproduct.desc k = biproduct.desc fun j => p j ≫ k j := by ext; simp @[reassoc (attr := simp)] theorem biproduct.lift_map {f g : J → C} [HasBiproduct f] [HasBiproduct g] {P : C} (k : ∀ j, P ⟶ f j) (p : ∀ j, f j ⟶ g j) : biproduct.lift k ≫ biproduct.map p = biproduct.lift fun j => k j ≫ p j := by ext; simp /-- Given a collection of isomorphisms between corresponding summands of a pair of biproducts indexed by the same type, we obtain an isomorphism between the biproducts. -/ @[simps] def biproduct.mapIso {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ b, f b ≅ g b) : ⨁ f ≅ ⨁ g where hom := biproduct.map fun b => (p b).hom inv := biproduct.map fun b => (p b).inv instance biproduct.map_epi {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) [∀ j, Epi (p j)] : Epi (biproduct.map p) := by classical have : biproduct.map p = (biproduct.isoCoproduct _).hom ≫ Sigma.map p ≫ (biproduct.isoCoproduct _).inv := by ext simp only [map_π, isoCoproduct_hom, isoCoproduct_inv, Category.assoc, ι_desc_assoc, ι_colimMap_assoc, Discrete.functor_obj_eq_as, Discrete.natTrans_app, colimit.ι_desc_assoc, Cofan.mk_pt, Cofan.mk_ι_app, ι_π, ι_π_assoc] split all_goals simp_all rw [this] infer_instance instance Pi.map_epi {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) [∀ j, Epi (p j)] : Epi (Pi.map p) := by rw [show Pi.map p = (biproduct.isoProduct _).inv ≫ biproduct.map p ≫ (biproduct.isoProduct _).hom by aesop] infer_instance instance biproduct.map_mono {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) [∀ j, Mono (p j)] : Mono (biproduct.map p) := by rw [show biproduct.map p = (biproduct.isoProduct _).hom ≫ Pi.map p ≫ (biproduct.isoProduct _).inv by aesop] infer_instance instance Sigma.map_mono {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) [∀ j, Mono (p j)] : Mono (Sigma.map p) := by rw [show Sigma.map p = (biproduct.isoCoproduct _).inv ≫ biproduct.map p ≫ (biproduct.isoCoproduct _).hom by aesop] infer_instance /-- Two biproducts which differ by an equivalence in the indexing type, and up to isomorphism in the factors, are isomorphic. Unfortunately there are two natural ways to define each direction of this isomorphism (because it is true for both products and coproducts separately). We give the alternative definitions as lemmas below. -/ @[simps] def biproduct.whiskerEquiv {f : J → C} {g : K → C} (e : J ≃ K) (w : ∀ j, g (e j) ≅ f j) [HasBiproduct f] [HasBiproduct g] : ⨁ f ≅ ⨁ g where hom := biproduct.desc fun j => (w j).inv ≫ biproduct.ι g (e j) inv := biproduct.desc fun k => eqToHom (by simp) ≫ (w (e.symm k)).hom ≫ biproduct.ι f _ lemma biproduct.whiskerEquiv_hom_eq_lift {f : J → C} {g : K → C} (e : J ≃ K) (w : ∀ j, g (e j) ≅ f j) [HasBiproduct f] [HasBiproduct g] : (biproduct.whiskerEquiv e w).hom = biproduct.lift fun k => biproduct.π f (e.symm k) ≫ (w _).inv ≫ eqToHom (by simp) := by simp only [whiskerEquiv_hom] ext k j by_cases h : k = e j · subst h simp · simp only [ι_desc_assoc, Category.assoc, ne_eq, lift_π] rw [biproduct.ι_π_ne, biproduct.ι_π_ne_assoc] · simp · rintro rfl simp at h · exact Ne.symm h lemma biproduct.whiskerEquiv_inv_eq_lift {f : J → C} {g : K → C} (e : J ≃ K) (w : ∀ j, g (e j) ≅ f j) [HasBiproduct f] [HasBiproduct g] : (biproduct.whiskerEquiv e w).inv = biproduct.lift fun j => biproduct.π g (e j) ≫ (w j).hom := by simp only [whiskerEquiv_inv] ext j k by_cases h : k = e j · subst h simp only [ι_desc_assoc, ← eqToHom_iso_hom_naturality_assoc w (e.symm_apply_apply j).symm, Equiv.symm_apply_apply, eqToHom_comp_ι, Category.assoc, bicone_ι_π_self, Category.comp_id, lift_π, bicone_ι_π_self_assoc] · simp only [ι_desc_assoc, Category.assoc, ne_eq, lift_π] rw [biproduct.ι_π_ne, biproduct.ι_π_ne_assoc] · simp · exact h · rintro rfl simp at h attribute [local simp] Sigma.forall in instance {ι} (f : ι → Type*) (g : (i : ι) → (f i) → C) [∀ i, HasBiproduct (g i)] [HasBiproduct fun i => ⨁ g i] : HasBiproduct fun p : Σ i, f i => g p.1 p.2 where exists_biproduct := Nonempty.intro { bicone := { pt := ⨁ fun i => ⨁ g i ι := fun X => biproduct.ι (g X.1) X.2 ≫ biproduct.ι (fun i => ⨁ g i) X.1 π := fun X => biproduct.π (fun i => ⨁ g i) X.1 ≫ biproduct.π (g X.1) X.2 ι_π := fun ⟨j, x⟩ ⟨j', y⟩ => by split_ifs with h · obtain ⟨rfl, rfl⟩ := h simp · simp only [Sigma.mk.inj_iff, not_and] at h by_cases w : j = j' · cases w simp only [heq_eq_eq, forall_true_left] at h simp [biproduct.ι_π_ne _ h] · simp [biproduct.ι_π_ne_assoc _ w] } isBilimit := { isLimit := mkFanLimit _ (fun s => biproduct.lift fun b => biproduct.lift fun c => s.proj ⟨b, c⟩) isColimit := mkCofanColimit _ (fun s => biproduct.desc fun b => biproduct.desc fun c => s.inj ⟨b, c⟩) } } /-- An iterated biproduct is a biproduct over a sigma type. -/ @[simps] def biproductBiproductIso {ι} (f : ι → Type*) (g : (i : ι) → (f i) → C) [∀ i, HasBiproduct (g i)] [HasBiproduct fun i => ⨁ g i] : (⨁ fun i => ⨁ g i) ≅ (⨁ fun p : Σ i, f i => g p.1 p.2) where hom := biproduct.lift fun ⟨i, x⟩ => biproduct.π _ i ≫ biproduct.π _ x inv := biproduct.lift fun i => biproduct.lift fun x => biproduct.π _ (⟨i, x⟩ : Σ i, f i) section πKernel section variable (f : J → C) [HasBiproduct f] variable (p : J → Prop) [HasBiproduct (Subtype.restrict p f)] /-- The canonical morphism from the biproduct over a restricted index type to the biproduct of the full index type. -/ def biproduct.fromSubtype : ⨁ Subtype.restrict p f ⟶ ⨁ f := biproduct.desc fun j => biproduct.ι _ j.val /-- The canonical morphism from a biproduct to the biproduct over a restriction of its index type. -/ def biproduct.toSubtype : ⨁ f ⟶ ⨁ Subtype.restrict p f := biproduct.lift fun _ => biproduct.π _ _ @[reassoc (attr := simp)] theorem biproduct.fromSubtype_π [DecidablePred p] (j : J) : biproduct.fromSubtype f p ≫ biproduct.π f j = if h : p j then biproduct.π (Subtype.restrict p f) ⟨j, h⟩ else 0 := by classical ext i; dsimp rw [biproduct.fromSubtype, biproduct.ι_desc_assoc, biproduct.ι_π] by_cases h : p j · rw [dif_pos h, biproduct.ι_π] split_ifs with h₁ h₂ h₂ exacts [rfl, False.elim (h₂ (Subtype.ext h₁)), False.elim (h₁ (congr_arg Subtype.val h₂)), rfl] · rw [dif_neg h, dif_neg (show (i : J) ≠ j from fun h₂ => h (h₂ ▸ i.2)), comp_zero] theorem biproduct.fromSubtype_eq_lift [DecidablePred p] : biproduct.fromSubtype f p = biproduct.lift fun j => if h : p j then biproduct.π (Subtype.restrict p f) ⟨j, h⟩ else 0 := biproduct.hom_ext _ _ (by simp) @[reassoc] -- Porting note: both version solved using simp theorem biproduct.fromSubtype_π_subtype (j : Subtype p) : biproduct.fromSubtype f p ≫ biproduct.π f j = biproduct.π (Subtype.restrict p f) j := by classical ext rw [biproduct.fromSubtype, biproduct.ι_desc_assoc, biproduct.ι_π, biproduct.ι_π] split_ifs with h₁ h₂ h₂ exacts [rfl, False.elim (h₂ (Subtype.ext h₁)), False.elim (h₁ (congr_arg Subtype.val h₂)), rfl] @[reassoc (attr := simp)] theorem biproduct.toSubtype_π (j : Subtype p) : biproduct.toSubtype f p ≫ biproduct.π (Subtype.restrict p f) j = biproduct.π f j := biproduct.lift_π _ _ @[reassoc (attr := simp)] theorem biproduct.ι_toSubtype [DecidablePred p] (j : J) : biproduct.ι f j ≫ biproduct.toSubtype f p = if h : p j then biproduct.ι (Subtype.restrict p f) ⟨j, h⟩ else 0 := by classical ext i rw [biproduct.toSubtype, Category.assoc, biproduct.lift_π, biproduct.ι_π] by_cases h : p j · rw [dif_pos h, biproduct.ι_π] split_ifs with h₁ h₂ h₂ exacts [rfl, False.elim (h₂ (Subtype.ext h₁)), False.elim (h₁ (congr_arg Subtype.val h₂)), rfl] · rw [dif_neg h, dif_neg (show j ≠ i from fun h₂ => h (h₂.symm ▸ i.2)), zero_comp] theorem biproduct.toSubtype_eq_desc [DecidablePred p] : biproduct.toSubtype f p = biproduct.desc fun j => if h : p j then biproduct.ι (Subtype.restrict p f) ⟨j, h⟩ else 0 := biproduct.hom_ext' _ _ (by simp) @[reassoc] theorem biproduct.ι_toSubtype_subtype (j : Subtype p) : biproduct.ι f j ≫ biproduct.toSubtype f p = biproduct.ι (Subtype.restrict p f) j := by classical ext rw [biproduct.toSubtype, Category.assoc, biproduct.lift_π, biproduct.ι_π, biproduct.ι_π] split_ifs with h₁ h₂ h₂ exacts [rfl, False.elim (h₂ (Subtype.ext h₁)), False.elim (h₁ (congr_arg Subtype.val h₂)), rfl] @[reassoc (attr := simp)] theorem biproduct.ι_fromSubtype (j : Subtype p) : biproduct.ι (Subtype.restrict p f) j ≫ biproduct.fromSubtype f p = biproduct.ι f j := biproduct.ι_desc _ _ @[reassoc (attr := simp)] theorem biproduct.fromSubtype_toSubtype : biproduct.fromSubtype f p ≫ biproduct.toSubtype f p = 𝟙 (⨁ Subtype.restrict p f) := by refine biproduct.hom_ext _ _ fun j => ?_ rw [Category.assoc, biproduct.toSubtype_π, biproduct.fromSubtype_π_subtype, Category.id_comp] @[reassoc (attr := simp)] theorem biproduct.toSubtype_fromSubtype [DecidablePred p] : biproduct.toSubtype f p ≫ biproduct.fromSubtype f p = biproduct.map fun j => if p j then 𝟙 (f j) else 0 := by ext1 i by_cases h : p i · simp [h] · simp [h] end section variable (f : J → C) (i : J) [HasBiproduct f] [HasBiproduct (Subtype.restrict (fun j => j ≠ i) f)] open scoped Classical in /-- The kernel of `biproduct.π f i` is the inclusion from the biproduct which omits `i` from the index set `J` into the biproduct over `J`. -/ def biproduct.isLimitFromSubtype : IsLimit (KernelFork.ofι (biproduct.fromSubtype f fun j => j ≠ i) (by simp) : KernelFork (biproduct.π f i)) := Fork.IsLimit.mk' _ fun s => ⟨s.ι ≫ biproduct.toSubtype _ _, by apply biproduct.hom_ext; intro j rw [KernelFork.ι_ofι, Category.assoc, Category.assoc, biproduct.toSubtype_fromSubtype_assoc, biproduct.map_π] rcases Classical.em (i = j) with (rfl | h) · rw [if_neg (Classical.not_not.2 rfl), comp_zero, comp_zero, KernelFork.condition] · rw [if_pos (Ne.symm h), Category.comp_id], by intro m hm rw [← hm, KernelFork.ι_ofι, Category.assoc, biproduct.fromSubtype_toSubtype] exact (Category.comp_id _).symm⟩ instance : HasKernel (biproduct.π f i) := HasLimit.mk ⟨_, biproduct.isLimitFromSubtype f i⟩ /-- The kernel of `biproduct.π f i` is `⨁ Subtype.restrict {i}ᶜ f`. -/ @[simps!] def kernelBiproductπIso : kernel (biproduct.π f i) ≅ ⨁ Subtype.restrict (fun j => j ≠ i) f := limit.isoLimitCone ⟨_, biproduct.isLimitFromSubtype f i⟩ open scoped Classical in /-- The cokernel of `biproduct.ι f i` is the projection from the biproduct over the index set `J` onto the biproduct omitting `i`. -/ def biproduct.isColimitToSubtype : IsColimit (CokernelCofork.ofπ (biproduct.toSubtype f fun j => j ≠ i) (by simp) : CokernelCofork (biproduct.ι f i)) := Cofork.IsColimit.mk' _ fun s => ⟨biproduct.fromSubtype _ _ ≫ s.π, by apply biproduct.hom_ext'; intro j rw [CokernelCofork.π_ofπ, biproduct.toSubtype_fromSubtype_assoc, biproduct.ι_map_assoc] rcases Classical.em (i = j) with (rfl | h) · rw [if_neg (Classical.not_not.2 rfl), zero_comp, CokernelCofork.condition] · rw [if_pos (Ne.symm h), Category.id_comp], by intro m hm rw [← hm, CokernelCofork.π_ofπ, ← Category.assoc, biproduct.fromSubtype_toSubtype] exact (Category.id_comp _).symm⟩ instance : HasCokernel (biproduct.ι f i) := HasColimit.mk ⟨_, biproduct.isColimitToSubtype f i⟩ /-- The cokernel of `biproduct.ι f i` is `⨁ Subtype.restrict {i}ᶜ f`. -/ @[simps!] def cokernelBiproductιIso : cokernel (biproduct.ι f i) ≅ ⨁ Subtype.restrict (fun j => j ≠ i) f := colimit.isoColimitCocone ⟨_, biproduct.isColimitToSubtype f i⟩ end section -- Per https://github.com/leanprover-community/mathlib3/pull/15067, we only allow indexing in `Type 0` here. variable {K : Type} [Finite K] [HasFiniteBiproducts C] (f : K → C) /-- The limit cone exhibiting `⨁ Subtype.restrict pᶜ f` as the kernel of `biproduct.toSubtype f p` -/ @[simps] def kernelForkBiproductToSubtype (p : Set K) : LimitCone (parallelPair (biproduct.toSubtype f p) 0) where cone := KernelFork.ofι (biproduct.fromSubtype f pᶜ) (by classical ext j k simp only [Category.assoc, biproduct.ι_fromSubtype_assoc, biproduct.ι_toSubtype_assoc, comp_zero, zero_comp] rw [dif_neg k.2] simp only [zero_comp]) isLimit := KernelFork.IsLimit.ofι _ _ (fun {_} g _ => g ≫ biproduct.toSubtype f pᶜ) (by classical intro W' g' w ext j simp only [Category.assoc, biproduct.toSubtype_fromSubtype, Pi.compl_apply, biproduct.map_π] split_ifs with h · simp · replace w := w =≫ biproduct.π _ ⟨j, not_not.mp h⟩ simpa using w.symm) (by aesop_cat) instance (p : Set K) : HasKernel (biproduct.toSubtype f p) := HasLimit.mk (kernelForkBiproductToSubtype f p) /-- The kernel of `biproduct.toSubtype f p` is `⨁ Subtype.restrict pᶜ f`. -/ @[simps!] def kernelBiproductToSubtypeIso (p : Set K) : kernel (biproduct.toSubtype f p) ≅ ⨁ Subtype.restrict pᶜ f := limit.isoLimitCone (kernelForkBiproductToSubtype f p) /-- The colimit cocone exhibiting `⨁ Subtype.restrict pᶜ f` as the cokernel of `biproduct.fromSubtype f p` -/ @[simps] def cokernelCoforkBiproductFromSubtype (p : Set K) : ColimitCocone (parallelPair (biproduct.fromSubtype f p) 0) where cocone := CokernelCofork.ofπ (biproduct.toSubtype f pᶜ) (by classical ext j k simp only [Category.assoc, Pi.compl_apply, biproduct.ι_fromSubtype_assoc, biproduct.ι_toSubtype_assoc, comp_zero, zero_comp] rw [dif_neg] · simp only [zero_comp] · exact not_not.mpr k.2) isColimit := CokernelCofork.IsColimit.ofπ _ _ (fun {_} g _ => biproduct.fromSubtype f pᶜ ≫ g) (by classical intro W g' w ext j simp only [biproduct.toSubtype_fromSubtype_assoc, Pi.compl_apply, biproduct.ι_map_assoc] split_ifs with h · simp · replace w := biproduct.ι _ (⟨j, not_not.mp h⟩ : p) ≫= w simpa using w.symm) (by aesop_cat) instance (p : Set K) : HasCokernel (biproduct.fromSubtype f p) := HasColimit.mk (cokernelCoforkBiproductFromSubtype f p) /-- The cokernel of `biproduct.fromSubtype f p` is `⨁ Subtype.restrict pᶜ f`. -/ @[simps!] def cokernelBiproductFromSubtypeIso (p : Set K) : cokernel (biproduct.fromSubtype f p) ≅ ⨁ Subtype.restrict pᶜ f := colimit.isoColimitCocone (cokernelCoforkBiproductFromSubtype f p) end end πKernel section FiniteBiproducts variable {J : Type} [Finite J] {K : Type} [Finite K] {C : Type u} [Category.{v} C] [HasZeroMorphisms C] [HasFiniteBiproducts C] {f : J → C} {g : K → C} /-- Convert a (dependently typed) matrix to a morphism of biproducts. -/ def biproduct.matrix (m : ∀ j k, f j ⟶ g k) : ⨁ f ⟶ ⨁ g := biproduct.desc fun j => biproduct.lift fun k => m j k @[reassoc (attr := simp)] theorem biproduct.matrix_π (m : ∀ j k, f j ⟶ g k) (k : K) : biproduct.matrix m ≫ biproduct.π g k = biproduct.desc fun j => m j k := by ext simp [biproduct.matrix] @[reassoc (attr := simp)] theorem biproduct.ι_matrix (m : ∀ j k, f j ⟶ g k) (j : J) : biproduct.ι f j ≫ biproduct.matrix m = biproduct.lift fun k => m j k := by ext simp [biproduct.matrix] /-- Extract the matrix components from a morphism of biproducts. -/ def biproduct.components (m : ⨁ f ⟶ ⨁ g) (j : J) (k : K) : f j ⟶ g k := biproduct.ι f j ≫ m ≫ biproduct.π g k @[simp] theorem biproduct.matrix_components (m : ∀ j k, f j ⟶ g k) (j : J) (k : K) : biproduct.components (biproduct.matrix m) j k = m j k := by simp [biproduct.components] @[simp] theorem biproduct.components_matrix (m : ⨁ f ⟶ ⨁ g) : (biproduct.matrix fun j k => biproduct.components m j k) = m := by ext simp [biproduct.components] /-- Morphisms between direct sums are matrices. -/ @[simps] def biproduct.matrixEquiv : (⨁ f ⟶ ⨁ g) ≃ ∀ j k, f j ⟶ g k where toFun := biproduct.components invFun := biproduct.matrix left_inv := biproduct.components_matrix right_inv m := by ext apply biproduct.matrix_components end FiniteBiproducts variable {J : Type w} variable {C : Type u} [Category.{v} C] [HasZeroMorphisms C] variable {D : Type uD} [Category.{uD'} D] [HasZeroMorphisms D] instance biproduct.ι_mono (f : J → C) [HasBiproduct f] (b : J) : IsSplitMono (biproduct.ι f b) := by classical exact IsSplitMono.mk' { retraction := biproduct.desc <| Pi.single b (𝟙 (f b)) } instance biproduct.π_epi (f : J → C) [HasBiproduct f] (b : J) : IsSplitEpi (biproduct.π f b) := by classical exact IsSplitEpi.mk' { section_ := biproduct.lift <| Pi.single b (𝟙 (f b)) } /-- Auxiliary lemma for `biproduct.uniqueUpToIso`. -/ theorem biproduct.conePointUniqueUpToIso_hom (f : J → C) [HasBiproduct f] {b : Bicone f} (hb : b.IsBilimit) : (hb.isLimit.conePointUniqueUpToIso (biproduct.isLimit _)).hom = biproduct.lift b.π := rfl /-- Auxiliary lemma for `biproduct.uniqueUpToIso`. -/ theorem biproduct.conePointUniqueUpToIso_inv (f : J → C) [HasBiproduct f] {b : Bicone f} (hb : b.IsBilimit) : (hb.isLimit.conePointUniqueUpToIso (biproduct.isLimit _)).inv = biproduct.desc b.ι := by classical refine biproduct.hom_ext' _ _ fun j => hb.isLimit.hom_ext fun j' => ?_ rw [Category.assoc, IsLimit.conePointUniqueUpToIso_inv_comp, Bicone.toCone_π_app, biproduct.bicone_π, biproduct.ι_desc, biproduct.ι_π, b.toCone_π_app, b.ι_π] /-- Biproducts are unique up to isomorphism. This already follows because bilimits are limits, but in the case of biproducts we can give an isomorphism with particularly nice definitional properties, namely that `biproduct.lift b.π` and `biproduct.desc b.ι` are inverses of each other. -/
@[simps] def biproduct.uniqueUpToIso (f : J → C) [HasBiproduct f] {b : Bicone f} (hb : b.IsBilimit) : b.pt ≅ ⨁ f where hom := biproduct.lift b.π
Mathlib/CategoryTheory/Limits/Shapes/Biproducts.lean
1,016
1,019
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot, Yury Kudryashov, Rémy Degenne -/ import Mathlib.Algebra.Order.Group.Abs import Mathlib.Algebra.Order.Group.Basic import Mathlib.Algebra.Order.Ring.Defs import Mathlib.Order.Interval.Set.Basic import Mathlib.Logic.Pairwise /-! ### Lemmas about arithmetic operations and intervals. -/ variable {α : Type*} namespace Set section OrderedCommGroup variable [CommGroup α] [PartialOrder α] [IsOrderedMonoid α] {a c d : α} /-! `inv_mem_Ixx_iff`, `sub_mem_Ixx_iff` -/ @[to_additive] theorem inv_mem_Icc_iff : a⁻¹ ∈ Set.Icc c d ↔ a ∈ Set.Icc d⁻¹ c⁻¹ := and_comm.trans <| and_congr inv_le' le_inv' @[to_additive] theorem inv_mem_Ico_iff : a⁻¹ ∈ Set.Ico c d ↔ a ∈ Set.Ioc d⁻¹ c⁻¹ := and_comm.trans <| and_congr inv_lt' le_inv' @[to_additive] theorem inv_mem_Ioc_iff : a⁻¹ ∈ Set.Ioc c d ↔ a ∈ Set.Ico d⁻¹ c⁻¹ := and_comm.trans <| and_congr inv_le' lt_inv' @[to_additive] theorem inv_mem_Ioo_iff : a⁻¹ ∈ Set.Ioo c d ↔ a ∈ Set.Ioo d⁻¹ c⁻¹ := and_comm.trans <| and_congr inv_lt' lt_inv' end OrderedCommGroup section OrderedAddCommGroup variable [AddCommGroup α] [PartialOrder α] [IsOrderedAddMonoid α] {a b c d : α} /-! `add_mem_Ixx_iff_left` -/ theorem add_mem_Icc_iff_left : a + b ∈ Set.Icc c d ↔ a ∈ Set.Icc (c - b) (d - b) := (and_congr sub_le_iff_le_add le_sub_iff_add_le).symm theorem add_mem_Ico_iff_left : a + b ∈ Set.Ico c d ↔ a ∈ Set.Ico (c - b) (d - b) := (and_congr sub_le_iff_le_add lt_sub_iff_add_lt).symm theorem add_mem_Ioc_iff_left : a + b ∈ Set.Ioc c d ↔ a ∈ Set.Ioc (c - b) (d - b) := (and_congr sub_lt_iff_lt_add le_sub_iff_add_le).symm theorem add_mem_Ioo_iff_left : a + b ∈ Set.Ioo c d ↔ a ∈ Set.Ioo (c - b) (d - b) := (and_congr sub_lt_iff_lt_add lt_sub_iff_add_lt).symm /-! `add_mem_Ixx_iff_right` -/ theorem add_mem_Icc_iff_right : a + b ∈ Set.Icc c d ↔ b ∈ Set.Icc (c - a) (d - a) := (and_congr sub_le_iff_le_add' le_sub_iff_add_le').symm theorem add_mem_Ico_iff_right : a + b ∈ Set.Ico c d ↔ b ∈ Set.Ico (c - a) (d - a) := (and_congr sub_le_iff_le_add' lt_sub_iff_add_lt').symm theorem add_mem_Ioc_iff_right : a + b ∈ Set.Ioc c d ↔ b ∈ Set.Ioc (c - a) (d - a) := (and_congr sub_lt_iff_lt_add' le_sub_iff_add_le').symm theorem add_mem_Ioo_iff_right : a + b ∈ Set.Ioo c d ↔ b ∈ Set.Ioo (c - a) (d - a) := (and_congr sub_lt_iff_lt_add' lt_sub_iff_add_lt').symm /-! `sub_mem_Ixx_iff_left` -/ theorem sub_mem_Icc_iff_left : a - b ∈ Set.Icc c d ↔ a ∈ Set.Icc (c + b) (d + b) := and_congr le_sub_iff_add_le sub_le_iff_le_add theorem sub_mem_Ico_iff_left : a - b ∈ Set.Ico c d ↔ a ∈ Set.Ico (c + b) (d + b) := and_congr le_sub_iff_add_le sub_lt_iff_lt_add theorem sub_mem_Ioc_iff_left : a - b ∈ Set.Ioc c d ↔ a ∈ Set.Ioc (c + b) (d + b) := and_congr lt_sub_iff_add_lt sub_le_iff_le_add theorem sub_mem_Ioo_iff_left : a - b ∈ Set.Ioo c d ↔ a ∈ Set.Ioo (c + b) (d + b) := and_congr lt_sub_iff_add_lt sub_lt_iff_lt_add /-! `sub_mem_Ixx_iff_right` -/ theorem sub_mem_Icc_iff_right : a - b ∈ Set.Icc c d ↔ b ∈ Set.Icc (a - d) (a - c) := and_comm.trans <| and_congr sub_le_comm le_sub_comm theorem sub_mem_Ico_iff_right : a - b ∈ Set.Ico c d ↔ b ∈ Set.Ioc (a - d) (a - c) := and_comm.trans <| and_congr sub_lt_comm le_sub_comm theorem sub_mem_Ioc_iff_right : a - b ∈ Set.Ioc c d ↔ b ∈ Set.Ico (a - d) (a - c) := and_comm.trans <| and_congr sub_le_comm lt_sub_comm theorem sub_mem_Ioo_iff_right : a - b ∈ Set.Ioo c d ↔ b ∈ Set.Ioo (a - d) (a - c) := and_comm.trans <| and_congr sub_lt_comm lt_sub_comm -- I think that symmetric intervals deserve attention and API: they arise all the time, -- for instance when considering metric balls in `ℝ`. theorem mem_Icc_iff_abs_le {R : Type*} [AddCommGroup R] [LinearOrder R] [IsOrderedAddMonoid R] {x y z : R} : |x - y| ≤ z ↔ y ∈ Icc (x - z) (x + z) := abs_le.trans <| and_comm.trans <| and_congr sub_le_comm neg_le_sub_iff_le_add /-! `sub_mem_Ixx_zero_right` and `sub_mem_Ixx_zero_iff_right`; this specializes the previous lemmas to the case of reflecting the interval. -/ theorem sub_mem_Icc_zero_iff_right : b - a ∈ Icc 0 b ↔ a ∈ Icc 0 b := by simp only [sub_mem_Icc_iff_right, sub_self, sub_zero] theorem sub_mem_Ico_zero_iff_right : b - a ∈ Ico 0 b ↔ a ∈ Ioc 0 b := by simp only [sub_mem_Ico_iff_right, sub_self, sub_zero] theorem sub_mem_Ioc_zero_iff_right : b - a ∈ Ioc 0 b ↔ a ∈ Ico 0 b := by simp only [sub_mem_Ioc_iff_right, sub_self, sub_zero] theorem sub_mem_Ioo_zero_iff_right : b - a ∈ Ioo 0 b ↔ a ∈ Ioo 0 b := by simp only [sub_mem_Ioo_iff_right, sub_self, sub_zero] end OrderedAddCommGroup section LinearOrderedAddCommGroup variable [AddCommGroup α] [LinearOrder α] [IsOrderedAddMonoid α] /-- If we remove a smaller interval from a larger, the result is nonempty -/ theorem nonempty_Ico_sdiff {x dx y dy : α} (h : dy < dx) (hx : 0 < dx) : Nonempty ↑(Ico x (x + dx) \ Ico y (y + dy)) := by rcases lt_or_le x y with h' | h' · use x simp [*, not_le.2 h'] · use max x (x + dy) simp [*, le_refl] end LinearOrderedAddCommGroup /-! ### Lemmas about disjointness of translates of intervals -/ open scoped Function -- required for scoped `on` notation section PairwiseDisjoint section OrderedCommGroup variable [CommGroup α] [PartialOrder α] [IsOrderedMonoid α] (a b : α) @[to_additive] theorem pairwise_disjoint_Ioc_mul_zpow : Pairwise (Disjoint on fun n : ℤ => Ioc (a * b ^ n) (a * b ^ (n + 1))) := by simp +unfoldPartialApp only [Function.onFun] simp_rw [Set.disjoint_iff] intro m n hmn x hx apply hmn have hb : 1 < b := by have : a * b ^ m < a * b ^ (m + 1) := hx.1.1.trans_le hx.1.2 rwa [mul_lt_mul_iff_left, ← mul_one (b ^ m), zpow_add_one, mul_lt_mul_iff_left] at this have i1 := hx.1.1.trans_le hx.2.2 have i2 := hx.2.1.trans_le hx.1.2 rw [mul_lt_mul_iff_left, zpow_lt_zpow_iff_right hb, Int.lt_add_one_iff] at i1 i2 exact le_antisymm i1 i2 @[to_additive] theorem pairwise_disjoint_Ico_mul_zpow : Pairwise (Disjoint on fun n : ℤ => Ico (a * b ^ n) (a * b ^ (n + 1))) := by simp +unfoldPartialApp only [Function.onFun]
simp_rw [Set.disjoint_iff] intro m n hmn x hx apply hmn have hb : 1 < b := by have : a * b ^ m < a * b ^ (m + 1) := hx.1.1.trans_lt hx.1.2 rwa [mul_lt_mul_iff_left, ← mul_one (b ^ m), zpow_add_one, mul_lt_mul_iff_left] at this have i1 := hx.1.1.trans_lt hx.2.2 have i2 := hx.2.1.trans_lt hx.1.2 rw [mul_lt_mul_iff_left, zpow_lt_zpow_iff_right hb, Int.lt_add_one_iff] at i1 i2 exact le_antisymm i1 i2 @[to_additive] theorem pairwise_disjoint_Ioo_mul_zpow :
Mathlib/Algebra/Order/Interval/Set/Group.lean
171
183
/- Copyright (c) 2019 Kim Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kim Morrison, Justus Springer -/ import Mathlib.Topology.Category.TopCat.OpenNhds import Mathlib.Topology.Sheaves.SheafCondition.UniqueGluing /-! # Stalks For a presheaf `F` on a topological space `X`, valued in some category `C`, the *stalk* of `F` at the point `x : X` is defined as the colimit of the composition of the inclusion of categories `(OpenNhds x)ᵒᵖ ⥤ (Opens X)ᵒᵖ` and the functor `F : (Opens X)ᵒᵖ ⥤ C`. For an open neighborhood `U` of `x`, we define the map `F.germ x : F.obj (op U) ⟶ F.stalk x` as the canonical morphism into this colimit. Taking stalks is functorial: For every point `x : X` we define a functor `stalkFunctor C x`, sending presheaves on `X` to objects of `C`. Furthermore, for a map `f : X ⟶ Y` between topological spaces, we define `stalkPushforward` as the induced map on the stalks `(f _* ℱ).stalk (f x) ⟶ ℱ.stalk x`. Some lemmas about stalks and germs only hold for certain classes of concrete categories. A basic property of forgetful functors of categories of algebraic structures (like `MonCat`, `CommRingCat`,...) is that they preserve filtered colimits. Since stalks are filtered colimits, this ensures that the stalks of presheaves valued in these categories behave exactly as for `Type`-valued presheaves. For example, in `germ_exist` we prove that in such a category, every element of the stalk is the germ of a section. Furthermore, if we require the forgetful functor to reflect isomorphisms and preserve limits (as is the case for most algebraic structures), we have access to the unique gluing API and can prove further properties. Most notably, in `is_iso_iff_stalk_functor_map_iso`, we prove that in such a category, a morphism of sheaves is an isomorphism if and only if all of its stalk maps are isomorphisms. See also the definition of "algebraic structures" in the stacks project: https://stacks.math.columbia.edu/tag/007L -/ assert_not_exists OrderedCommMonoid noncomputable section universe v u v' u' open CategoryTheory open TopCat open CategoryTheory.Limits open TopologicalSpace Topology open Opposite open scoped AlgebraicGeometry variable {C : Type u} [Category.{v} C] variable [HasColimits.{v} C] variable {X Y Z : TopCat.{v}} namespace TopCat.Presheaf variable (C) in /-- Stalks are functorial with respect to morphisms of presheaves over a fixed `X`. -/ def stalkFunctor (x : X) : X.Presheaf C ⥤ C := (whiskeringLeft _ _ C).obj (OpenNhds.inclusion x).op ⋙ colim /-- The stalk of a presheaf `F` at a point `x` is calculated as the colimit of the functor nbhds x ⥤ opens F.X ⥤ C -/ def stalk (ℱ : X.Presheaf C) (x : X) : C := (stalkFunctor C x).obj ℱ -- -- colimit ((open_nhds.inclusion x).op ⋙ ℱ) @[simp] theorem stalkFunctor_obj (ℱ : X.Presheaf C) (x : X) : (stalkFunctor C x).obj ℱ = ℱ.stalk x := rfl /-- The germ of a section of a presheaf over an open at a point of that open. -/ def germ (F : X.Presheaf C) (U : Opens X) (x : X) (hx : x ∈ U) : F.obj (op U) ⟶ stalk F x := colimit.ι ((OpenNhds.inclusion x).op ⋙ F) (op ⟨U, hx⟩) /-- The germ of a global section of a presheaf at a point. -/ def Γgerm (F : X.Presheaf C) (x : X) : F.obj (op ⊤) ⟶ stalk F x := F.germ ⊤ x True.intro @[reassoc] theorem germ_res (F : X.Presheaf C) {U V : Opens X} (i : U ⟶ V) (x : X) (hx : x ∈ U) : F.map i.op ≫ F.germ U x hx = F.germ V x (i.le hx) := let i' : (⟨U, hx⟩ : OpenNhds x) ⟶ ⟨V, i.le hx⟩ := i colimit.w ((OpenNhds.inclusion x).op ⋙ F) i'.op /-- A variant of `germ_res` with `op V ⟶ op U` so that the LHS is more general and simp fires more easier. -/ @[reassoc (attr := simp)] theorem germ_res' (F : X.Presheaf C) {U V : Opens X} (i : op V ⟶ op U) (x : X) (hx : x ∈ U) : F.map i ≫ F.germ U x hx = F.germ V x (i.unop.le hx) := let i' : (⟨U, hx⟩ : OpenNhds x) ⟶ ⟨V, i.unop.le hx⟩ := i.unop colimit.w ((OpenNhds.inclusion x).op ⋙ F) i'.op @[reassoc] lemma map_germ_eq_Γgerm (F : X.Presheaf C) {U : Opens X} {i : U ⟶ ⊤} (x : X) (hx : x ∈ U) : F.map i.op ≫ F.germ U x hx = F.Γgerm x := germ_res F i x hx variable {FC : C → C → Type*} {CC : C → Type*} [∀ X Y, FunLike (FC X Y) (CC X) (CC Y)] theorem germ_res_apply (F : X.Presheaf C) {U V : Opens X} (i : U ⟶ V) (x : X) (hx : x ∈ U) [ConcreteCategory C FC] (s) : F.germ U x hx (F.map i.op s) = F.germ V x (i.le hx) s := by rw [← ConcreteCategory.comp_apply, germ_res] theorem germ_res_apply' (F : X.Presheaf C) {U V : Opens X} (i : op V ⟶ op U) (x : X) (hx : x ∈ U) [ConcreteCategory C FC] (s) : F.germ U x hx (F.map i s) = F.germ V x (i.unop.le hx) s := by rw [← ConcreteCategory.comp_apply, germ_res'] lemma Γgerm_res_apply (F : X.Presheaf C) {U : Opens X} {i : U ⟶ ⊤} (x : X) (hx : x ∈ U) [ConcreteCategory C FC] (s) : F.germ U x hx (F.map i.op s) = F.Γgerm x s := F.germ_res_apply i x hx s /-- A morphism from the stalk of `F` at `x` to some object `Y` is completely determined by its composition with the `germ` morphisms. -/ @[ext] theorem stalk_hom_ext (F : X.Presheaf C) {x} {Y : C} {f₁ f₂ : F.stalk x ⟶ Y} (ih : ∀ (U : Opens X) (hxU : x ∈ U), F.germ U x hxU ≫ f₁ = F.germ U x hxU ≫ f₂) : f₁ = f₂ := colimit.hom_ext fun U => by induction U with | op U => obtain ⟨U, hxU⟩ := U; exact ih U hxU @[reassoc (attr := simp)] theorem stalkFunctor_map_germ {F G : X.Presheaf C} (U : Opens X) (x : X) (hx : x ∈ U) (f : F ⟶ G) : F.germ U x hx ≫ (stalkFunctor C x).map f = f.app (op U) ≫ G.germ U x hx := colimit.ι_map (whiskerLeft (OpenNhds.inclusion x).op f) (op ⟨U, hx⟩) theorem stalkFunctor_map_germ_apply [ConcreteCategory C FC] {F G : X.Presheaf C} (U : Opens X) (x : X) (hx : x ∈ U) (f : F ⟶ G) (s) : (stalkFunctor C x).map f (F.germ U x hx s) = G.germ U x hx (f.app (op U) s) := by rw [← ConcreteCategory.comp_apply, ← stalkFunctor_map_germ, ConcreteCategory.comp_apply] rfl -- a variant of `stalkFunctor_map_germ_apply` that makes simpNF happy. @[simp] theorem stalkFunctor_map_germ_apply' [ConcreteCategory C FC] {F G : X.Presheaf C} (U : Opens X) (x : X) (hx : x ∈ U) (f : F ⟶ G) (s) : DFunLike.coe (F := ToHom (F.stalk x) (G.stalk x)) (ConcreteCategory.hom ((stalkFunctor C x).map f)) (F.germ U x hx s) = G.germ U x hx (f.app (op U) s) := stalkFunctor_map_germ_apply U x hx f s variable (C) /-- For a presheaf `F` on a space `X`, a continuous map `f : X ⟶ Y` induces a morphisms between the stalk of `f _ * F` at `f x` and the stalk of `F` at `x`. -/ def stalkPushforward (f : X ⟶ Y) (F : X.Presheaf C) (x : X) : (f _* F).stalk (f x) ⟶ F.stalk x := by -- This is a hack; Lean doesn't like to elaborate the term written directly. refine ?_ ≫ colimit.pre _ (OpenNhds.map f x).op exact colim.map (whiskerRight (NatTrans.op (OpenNhds.inclusionMapIso f x).inv) F) @[reassoc (attr := simp), elementwise (attr := simp)] theorem stalkPushforward_germ (f : X ⟶ Y) (F : X.Presheaf C) (U : Opens Y) (x : X) (hx : f x ∈ U) : (f _* F).germ U (f x) hx ≫ F.stalkPushforward C f x = F.germ ((Opens.map f).obj U) x hx := by simp [germ, stalkPushforward] -- Here are two other potential solutions, suggested by @fpvandoorn at -- <https://github.com/leanprover-community/mathlib/pull/1018#discussion_r283978240> -- However, I can't get the subsequent two proofs to work with either one. -- def stalkPushforward'' (f : X ⟶ Y) (ℱ : X.Presheaf C) (x : X) : -- (f _* ℱ).stalk (f x) ⟶ ℱ.stalk x := -- colim.map ((Functor.associator _ _ _).inv ≫ -- whiskerRight (NatTrans.op (OpenNhds.inclusionMapIso f x).inv) ℱ) ≫ -- colimit.pre ((OpenNhds.inclusion x).op ⋙ ℱ) (OpenNhds.map f x).op -- def stalkPushforward''' (f : X ⟶ Y) (ℱ : X.Presheaf C) (x : X) : -- (f _* ℱ).stalk (f x) ⟶ ℱ.stalk x := -- (colim.map (whiskerRight (NatTrans.op (OpenNhds.inclusionMapIso f x).inv) ℱ) : -- colim.obj ((OpenNhds.inclusion (f x) ⋙ Opens.map f).op ⋙ ℱ) ⟶ _) ≫ -- colimit.pre ((OpenNhds.inclusion x).op ⋙ ℱ) (OpenNhds.map f x).op namespace stalkPushforward @[simp] theorem id (ℱ : X.Presheaf C) (x : X) : ℱ.stalkPushforward C (𝟙 X) x = (stalkFunctor C x).map (Pushforward.id ℱ).hom := by ext simp only [stalkPushforward, germ, colim_map, ι_colimMap_assoc, whiskerRight_app] erw [CategoryTheory.Functor.map_id] simp [stalkFunctor] @[simp] theorem comp (ℱ : X.Presheaf C) (f : X ⟶ Y) (g : Y ⟶ Z) (x : X) : ℱ.stalkPushforward C (f ≫ g) x = (f _* ℱ).stalkPushforward C g (f x) ≫ ℱ.stalkPushforward C f x := by ext simp [germ, stalkPushforward] theorem stalkPushforward_iso_of_isInducing {f : X ⟶ Y} (hf : IsInducing f) (F : X.Presheaf C) (x : X) : IsIso (F.stalkPushforward _ f x) := by haveI := Functor.initial_of_adjunction (hf.adjunctionNhds x) convert (Functor.Final.colimitIso (OpenNhds.map f x).op ((OpenNhds.inclusion x).op ⋙ F)).isIso_hom refine stalk_hom_ext _ fun U hU ↦ (stalkPushforward_germ _ f F _ x hU).trans ?_ symm exact colimit.ι_pre ((OpenNhds.inclusion x).op ⋙ F) (OpenNhds.map f x).op _ @[deprecated (since := "2024-10-27")] alias stalkPushforward_iso_of_isOpenEmbedding := stalkPushforward_iso_of_isInducing end stalkPushforward section stalkPullback /-- The morphism `ℱ_{f x} ⟶ (f⁻¹ℱ)ₓ` that factors through `(f_*f⁻¹ℱ)_{f x}`. -/ def stalkPullbackHom (f : X ⟶ Y) (F : Y.Presheaf C) (x : X) : F.stalk (f x) ⟶ ((pullback C f).obj F).stalk x := (stalkFunctor _ (f x)).map ((pushforwardPullbackAdjunction C f).unit.app F) ≫ stalkPushforward _ _ _ x @[reassoc (attr := simp)] lemma germ_stalkPullbackHom (f : X ⟶ Y) (F : Y.Presheaf C) (x : X) (U : Opens Y) (hU : f x ∈ U) : F.germ U (f x) hU ≫ stalkPullbackHom C f F x = ((pushforwardPullbackAdjunction C f).unit.app F).app _ ≫ ((pullback C f).obj F).germ ((Opens.map f).obj U) x hU := by simp [stalkPullbackHom, germ, stalkFunctor, stalkPushforward] /-- The morphism `(f⁻¹ℱ)(U) ⟶ ℱ_{f(x)}` for some `U ∋ x`. -/ def germToPullbackStalk (f : X ⟶ Y) (F : Y.Presheaf C) (U : Opens X) (x : X) (hx : x ∈ U) : ((pullback C f).obj F).obj (op U) ⟶ F.stalk (f x) := ((Opens.map f).op.isPointwiseLeftKanExtensionLeftKanExtensionUnit F (op U)).desc { pt := F.stalk ((f : X → Y) (x : X)) ι := { app := fun V => F.germ _ (f x) (V.hom.unop.le hx) naturality := fun _ _ i => by simp } } variable {C} in @[ext] lemma pullback_obj_obj_ext {Z : C} {f : X ⟶ Y} {F : Y.Presheaf C} (U : (Opens X)ᵒᵖ) {φ ψ : ((pullback C f).obj F).obj U ⟶ Z} (h : ∀ (V : Opens Y) (hV : U.unop ≤ (Opens.map f).obj V), ((pushforwardPullbackAdjunction C f).unit.app F).app (op V) ≫ ((pullback C f).obj F).map (homOfLE hV).op ≫ φ = ((pushforwardPullbackAdjunction C f).unit.app F).app (op V) ≫ ((pullback C f).obj F).map (homOfLE hV).op ≫ ψ) : φ = ψ := by obtain ⟨U⟩ := U apply ((Opens.map f).op.isPointwiseLeftKanExtensionLeftKanExtensionUnit F _).hom_ext rintro ⟨⟨V⟩, ⟨⟩, ⟨b⟩⟩ simpa [pushforwardPullbackAdjunction, Functor.lanAdjunction_unit] using h V (leOfHom b) @[reassoc (attr := simp)] lemma pushforwardPullbackAdjunction_unit_pullback_map_germToPullbackStalk (f : X ⟶ Y) (F : Y.Presheaf C) (U : Opens X) (x : X) (hx : x ∈ U) (V : Opens Y) (hV : U ≤ (Opens.map f).obj V) : ((pushforwardPullbackAdjunction C f).unit.app F).app (op V) ≫ ((pullback C f).obj F).map (homOfLE hV).op ≫ germToPullbackStalk C f F U x hx = F.germ _ (f x) (hV hx) := by simpa [pushforwardPullbackAdjunction] using ((Opens.map f).op.isPointwiseLeftKanExtensionLeftKanExtensionUnit F (op U)).fac _ (CostructuredArrow.mk (homOfLE hV).op) @[reassoc (attr := simp)] lemma germToPullbackStalk_stalkPullbackHom (f : X ⟶ Y) (F : Y.Presheaf C) (U : Opens X) (x : X) (hx : x ∈ U) : germToPullbackStalk C f F U x hx ≫ stalkPullbackHom C f F x = ((pullback C f).obj F).germ _ x hx := by ext V hV dsimp simp only [pushforwardPullbackAdjunction_unit_pullback_map_germToPullbackStalk_assoc, germ_stalkPullbackHom, germ_res] @[reassoc (attr := simp)] lemma pushforwardPullbackAdjunction_unit_app_app_germToPullbackStalk (f : X ⟶ Y) (F : Y.Presheaf C) (V : (Opens Y)ᵒᵖ) (x : X) (hx : f x ∈ V.unop) : ((pushforwardPullbackAdjunction C f).unit.app F).app V ≫ germToPullbackStalk C f F _ x hx = F.germ _ (f x) hx := by simpa using pushforwardPullbackAdjunction_unit_pullback_map_germToPullbackStalk C f F ((Opens.map f).obj V.unop) x hx V.unop (by rfl) /-- The morphism `(f⁻¹ℱ)ₓ ⟶ ℱ_{f(x)}`. -/ def stalkPullbackInv (f : X ⟶ Y) (F : Y.Presheaf C) (x : X) : ((pullback C f).obj F).stalk x ⟶ F.stalk (f x) := colimit.desc ((OpenNhds.inclusion x).op ⋙ (Presheaf.pullback C f).obj F) { pt := F.stalk (f x) ι := { app := fun U => F.germToPullbackStalk _ f (unop U).1 x (unop U).2 naturality := fun U V i => by dsimp ext W hW dsimp [OpenNhds.inclusion] rw [Category.comp_id, ← Functor.map_comp_assoc, pushforwardPullbackAdjunction_unit_pullback_map_germToPullbackStalk] erw [pushforwardPullbackAdjunction_unit_pullback_map_germToPullbackStalk] } } @[reassoc (attr := simp)] lemma germ_stalkPullbackInv (f : X ⟶ Y) (F : Y.Presheaf C) (x : X) (V : Opens X) (hV : x ∈ V) : ((pullback C f).obj F).germ _ x hV ≫ stalkPullbackInv C f F x = F.germToPullbackStalk _ f V x hV := by apply colimit.ι_desc /-- The isomorphism `ℱ_{f(x)} ≅ (f⁻¹ℱ)ₓ`. -/ def stalkPullbackIso (f : X ⟶ Y) (F : Y.Presheaf C) (x : X) : F.stalk (f x) ≅ ((pullback C f).obj F).stalk x where hom := stalkPullbackHom _ _ _ _ inv := stalkPullbackInv _ _ _ _ hom_inv_id := by ext U hU dsimp rw [germ_stalkPullbackHom_assoc, germ_stalkPullbackInv, Category.comp_id, pushforwardPullbackAdjunction_unit_app_app_germToPullbackStalk] inv_hom_id := by ext V hV dsimp rw [germ_stalkPullbackInv_assoc, Category.comp_id, germToPullbackStalk_stalkPullbackHom] end stalkPullback section stalkSpecializes variable {C} /-- If `x` specializes to `y`, then there is a natural map `F.stalk y ⟶ F.stalk x`. -/ noncomputable def stalkSpecializes (F : X.Presheaf C) {x y : X} (h : x ⤳ y) : F.stalk y ⟶ F.stalk x := by refine colimit.desc _ ⟨_, fun U => ?_, ?_⟩ · exact colimit.ι ((OpenNhds.inclusion x).op ⋙ F) (op ⟨(unop U).1, (specializes_iff_forall_open.mp h _ (unop U).1.2 (unop U).2 :)⟩) · intro U V i dsimp rw [Category.comp_id] let U' : OpenNhds x := ⟨_, (specializes_iff_forall_open.mp h _ (unop U).1.2 (unop U).2 :)⟩ let V' : OpenNhds x := ⟨_, (specializes_iff_forall_open.mp h _ (unop V).1.2 (unop V).2 :)⟩ exact colimit.w ((OpenNhds.inclusion x).op ⋙ F) (show V' ⟶ U' from i.unop).op @[reassoc (attr := simp), elementwise nosimp] theorem germ_stalkSpecializes (F : X.Presheaf C) {U : Opens X} {y : X} (hy : y ∈ U) {x : X} (h : x ⤳ y) : F.germ U y hy ≫ F.stalkSpecializes h = F.germ U x (h.mem_open U.isOpen hy) := colimit.ι_desc _ _
@[simp] theorem stalkSpecializes_refl {C : Type*} [Category C] [Limits.HasColimits C] {X : TopCat} (F : X.Presheaf C) (x : X) : F.stalkSpecializes (specializes_refl x) = 𝟙 _ := by ext simp
Mathlib/Topology/Sheaves/Stalks.lean
345
349
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Johan Commelin, Mario Carneiro -/ import Mathlib.Algebra.MonoidAlgebra.Degree import Mathlib.Algebra.MvPolynomial.Rename /-! # Degrees of polynomials This file establishes many results about the degree of a multivariate polynomial. The *degree set* of a polynomial $P \in R[X]$ is a `Multiset` containing, for each $x$ in the variable set, $n$ copies of $x$, where $n$ is the maximum number of copies of $x$ appearing in a monomial of $P$. ## Main declarations * `MvPolynomial.degrees p` : the multiset of variables representing the union of the multisets corresponding to each non-zero monomial in `p`. For example if `7 ≠ 0` in `R` and `p = x²y+7y³` then `degrees p = {x, x, y, y, y}` * `MvPolynomial.degreeOf n p : ℕ` : the total degree of `p` with respect to the variable `n`. For example if `p = x⁴y+yz` then `degreeOf y p = 1`. * `MvPolynomial.totalDegree p : ℕ` : the max of the sizes of the multisets `s` whose monomials `X^s` occur in `p`. For example if `p = x⁴y+yz` then `totalDegree p = 5`. ## Notation As in other polynomial files, we typically use the notation: + `σ τ : Type*` (indexing the variables) + `R : Type*` `[CommSemiring R]` (the coefficients) + `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set. This will give rise to a monomial in `MvPolynomial σ R` which mathematicians might call `X^s` + `r : R` + `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians + `p : MvPolynomial σ R` -/ noncomputable section open Set Function Finsupp AddMonoidAlgebra universe u v w variable {R : Type u} {S : Type v} namespace MvPolynomial variable {σ τ : Type*} {r : R} {e : ℕ} {n m : σ} {s : σ →₀ ℕ} section CommSemiring variable [CommSemiring R] {p q : MvPolynomial σ R} section Degrees /-! ### `degrees` -/ /-- The maximal degrees of each variable in a multi-variable polynomial, expressed as a multiset. (For example, `degrees (x^2 * y + y^3)` would be `{x, x, y, y, y}`.) -/ def degrees (p : MvPolynomial σ R) : Multiset σ := letI := Classical.decEq σ p.support.sup fun s : σ →₀ ℕ => toMultiset s theorem degrees_def [DecidableEq σ] (p : MvPolynomial σ R) : p.degrees = p.support.sup fun s : σ →₀ ℕ => Finsupp.toMultiset s := by rw [degrees]; convert rfl theorem degrees_monomial (s : σ →₀ ℕ) (a : R) : degrees (monomial s a) ≤ toMultiset s := by classical refine (supDegree_single s a).trans_le ?_ split_ifs exacts [bot_le, le_rfl] theorem degrees_monomial_eq (s : σ →₀ ℕ) (a : R) (ha : a ≠ 0) : degrees (monomial s a) = toMultiset s := by classical exact (supDegree_single s a).trans (if_neg ha) theorem degrees_C (a : R) : degrees (C a : MvPolynomial σ R) = 0 := Multiset.le_zero.1 <| degrees_monomial _ _ theorem degrees_X' (n : σ) : degrees (X n : MvPolynomial σ R) ≤ {n} := le_trans (degrees_monomial _ _) <| le_of_eq <| toMultiset_single _ _ @[simp] theorem degrees_X [Nontrivial R] (n : σ) : degrees (X n : MvPolynomial σ R) = {n} := (degrees_monomial_eq _ (1 : R) one_ne_zero).trans (toMultiset_single _ _) @[simp] theorem degrees_zero : degrees (0 : MvPolynomial σ R) = 0 := by rw [← C_0] exact degrees_C 0 @[simp] theorem degrees_one : degrees (1 : MvPolynomial σ R) = 0 := degrees_C 1 theorem degrees_add_le [DecidableEq σ] {p q : MvPolynomial σ R} : (p + q).degrees ≤ p.degrees ⊔ q.degrees := by simp_rw [degrees_def]; exact supDegree_add_le theorem degrees_sum_le {ι : Type*} [DecidableEq σ] (s : Finset ι) (f : ι → MvPolynomial σ R) : (∑ i ∈ s, f i).degrees ≤ s.sup fun i => (f i).degrees := by simp_rw [degrees_def]; exact supDegree_sum_le theorem degrees_mul_le {p q : MvPolynomial σ R} : (p * q).degrees ≤ p.degrees + q.degrees := by classical simp_rw [degrees_def] exact supDegree_mul_le (map_add _) theorem degrees_prod_le {ι : Type*} {s : Finset ι} {f : ι → MvPolynomial σ R} : (∏ i ∈ s, f i).degrees ≤ ∑ i ∈ s, (f i).degrees := by classical exact supDegree_prod_le (map_zero _) (map_add _) theorem degrees_pow_le {p : MvPolynomial σ R} {n : ℕ} : (p ^ n).degrees ≤ n • p.degrees := by simpa using degrees_prod_le (s := .range n) (f := fun _ ↦ p) @[deprecated (since := "2024-12-28")] alias degrees_add := degrees_add_le @[deprecated (since := "2024-12-28")] alias degrees_sum := degrees_sum_le @[deprecated (since := "2024-12-28")] alias degrees_mul := degrees_mul_le @[deprecated (since := "2024-12-28")] alias degrees_prod := degrees_prod_le @[deprecated (since := "2024-12-28")] alias degrees_pow := degrees_pow_le theorem mem_degrees {p : MvPolynomial σ R} {i : σ} : i ∈ p.degrees ↔ ∃ d, p.coeff d ≠ 0 ∧ i ∈ d.support := by classical simp only [degrees_def, Multiset.mem_sup, ← mem_support_iff, Finsupp.mem_toMultiset, exists_prop] theorem le_degrees_add_left (h : Disjoint p.degrees q.degrees) : p.degrees ≤ (p + q).degrees := by classical apply Finset.sup_le intro d hd rw [Multiset.disjoint_iff_ne] at h obtain rfl | h0 := eq_or_ne d 0 · rw [toMultiset_zero]; apply Multiset.zero_le · refine Finset.le_sup_of_le (b := d) ?_ le_rfl rw [mem_support_iff, coeff_add] suffices q.coeff d = 0 by rwa [this, add_zero, coeff, ← Finsupp.mem_support_iff] rw [Ne, ← Finsupp.support_eq_empty, ← Ne, ← Finset.nonempty_iff_ne_empty] at h0 obtain ⟨j, hj⟩ := h0 contrapose! h rw [mem_support_iff] at hd refine ⟨j, ?_, j, ?_, rfl⟩ all_goals rw [mem_degrees]; refine ⟨d, ?_, hj⟩; assumption @[deprecated (since := "2024-12-28")] alias le_degrees_add := le_degrees_add_left lemma le_degrees_add_right (h : Disjoint p.degrees q.degrees) : q.degrees ≤ (p + q).degrees := by simpa [add_comm] using le_degrees_add_left h.symm theorem degrees_add_of_disjoint [DecidableEq σ] (h : Disjoint p.degrees q.degrees) : (p + q).degrees = p.degrees ∪ q.degrees := degrees_add_le.antisymm <| Multiset.union_le (le_degrees_add_left h) (le_degrees_add_right h) lemma degrees_map_le [CommSemiring S] {f : R →+* S} : (map f p).degrees ≤ p.degrees := by classical exact Finset.sup_mono <| support_map_subset .. @[deprecated (since := "2024-12-28")] alias degrees_map := degrees_map_le theorem degrees_rename (f : σ → τ) (φ : MvPolynomial σ R) : (rename f φ).degrees ⊆ φ.degrees.map f := by classical intro i rw [mem_degrees, Multiset.mem_map] rintro ⟨d, hd, hi⟩ obtain ⟨x, rfl, hx⟩ := coeff_rename_ne_zero _ _ _ hd simp only [Finsupp.mapDomain, Finsupp.mem_support_iff] at hi rw [sum_apply, Finsupp.sum] at hi contrapose! hi rw [Finset.sum_eq_zero] intro j hj simp only [exists_prop, mem_degrees] at hi specialize hi j ⟨x, hx, hj⟩ rw [Finsupp.single_apply, if_neg hi] theorem degrees_map_of_injective [CommSemiring S] (p : MvPolynomial σ R) {f : R →+* S} (hf : Injective f) : (map f p).degrees = p.degrees := by simp only [degrees, MvPolynomial.support_map_of_injective _ hf] theorem degrees_rename_of_injective {p : MvPolynomial σ R} {f : σ → τ} (h : Function.Injective f) : degrees (rename f p) = (degrees p).map f := by classical simp only [degrees, Multiset.map_finset_sup p.support Finsupp.toMultiset f h, support_rename_of_injective h, Finset.sup_image] refine Finset.sup_congr rfl fun x _ => ?_ exact (Finsupp.toMultiset_map _ _).symm end Degrees section DegreeOf /-! ### `degreeOf` -/ /-- `degreeOf n p` gives the highest power of X_n that appears in `p` -/ def degreeOf (n : σ) (p : MvPolynomial σ R) : ℕ := letI := Classical.decEq σ p.degrees.count n theorem degreeOf_def [DecidableEq σ] (n : σ) (p : MvPolynomial σ R) : p.degreeOf n = p.degrees.count n := by rw [degreeOf]; convert rfl theorem degreeOf_eq_sup (n : σ) (f : MvPolynomial σ R) : degreeOf n f = f.support.sup fun m => m n := by classical rw [degreeOf_def, degrees, Multiset.count_finset_sup] congr ext simp only [count_toMultiset] theorem degreeOf_lt_iff {n : σ} {f : MvPolynomial σ R} {d : ℕ} (h : 0 < d) : degreeOf n f < d ↔ ∀ m : σ →₀ ℕ, m ∈ f.support → m n < d := by rwa [degreeOf_eq_sup, Finset.sup_lt_iff] lemma degreeOf_le_iff {n : σ} {f : MvPolynomial σ R} {d : ℕ} : degreeOf n f ≤ d ↔ ∀ m ∈ support f, m n ≤ d := by rw [degreeOf_eq_sup, Finset.sup_le_iff] @[simp] theorem degreeOf_zero (n : σ) : degreeOf n (0 : MvPolynomial σ R) = 0 := by classical simp only [degreeOf_def, degrees_zero, Multiset.count_zero] @[simp] theorem degreeOf_C (a : R) (x : σ) : degreeOf x (C a : MvPolynomial σ R) = 0 := by classical simp [degreeOf_def, degrees_C] theorem degreeOf_X [DecidableEq σ] (i j : σ) [Nontrivial R] : degreeOf i (X j : MvPolynomial σ R) = if i = j then 1 else 0 := by classical by_cases c : i = j · simp only [c, if_true, eq_self_iff_true, degreeOf_def, degrees_X, Multiset.count_singleton] simp [c, if_false, degreeOf_def, degrees_X] theorem degreeOf_add_le (n : σ) (f g : MvPolynomial σ R) : degreeOf n (f + g) ≤ max (degreeOf n f) (degreeOf n g) := by simp_rw [degreeOf_eq_sup]; exact supDegree_add_le theorem monomial_le_degreeOf (i : σ) {f : MvPolynomial σ R} {m : σ →₀ ℕ} (h_m : m ∈ f.support) : m i ≤ degreeOf i f := by rw [degreeOf_eq_sup i] apply Finset.le_sup h_m lemma degreeOf_monomial_eq (s : σ →₀ ℕ) (i : σ) {a : R} (ha : a ≠ 0) : (monomial s a).degreeOf i = s i := by classical rw [degreeOf_def, degrees_monomial_eq _ _ ha, Finsupp.count_toMultiset] -- TODO we can prove equality with `NoZeroDivisors R` theorem degreeOf_mul_le (i : σ) (f g : MvPolynomial σ R) : degreeOf i (f * g) ≤ degreeOf i f + degreeOf i g := by classical simp only [degreeOf] convert Multiset.count_le_of_le i degrees_mul_le rw [Multiset.count_add] theorem degreeOf_sum_le {ι : Type*} (i : σ) (s : Finset ι) (f : ι → MvPolynomial σ R) : degreeOf i (∑ j ∈ s, f j) ≤ s.sup fun j => degreeOf i (f j) := by simp_rw [degreeOf_eq_sup] exact supDegree_sum_le -- TODO we can prove equality with `NoZeroDivisors R` theorem degreeOf_prod_le {ι : Type*} (i : σ) (s : Finset ι) (f : ι → MvPolynomial σ R) : degreeOf i (∏ j ∈ s, f j) ≤ ∑ j ∈ s, (f j).degreeOf i := by simp_rw [degreeOf_eq_sup] exact supDegree_prod_le (by simp only [coe_zero, Pi.zero_apply]) (fun _ _ => by simp only [coe_add, Pi.add_apply]) -- TODO we can prove equality with `NoZeroDivisors R` theorem degreeOf_pow_le (i : σ) (p : MvPolynomial σ R) (n : ℕ) : degreeOf i (p ^ n) ≤ n * degreeOf i p := by simpa using degreeOf_prod_le i (Finset.range n) (fun _ => p) theorem degreeOf_mul_X_of_ne {i j : σ} (f : MvPolynomial σ R) (h : i ≠ j) : degreeOf i (f * X j) = degreeOf i f := by classical simp only [degreeOf_eq_sup i, support_mul_X, Finset.sup_map] congr ext simp only [Finsupp.single, add_eq_left, addRightEmbedding_apply, coe_mk, Pi.add_apply, comp_apply, ite_eq_right_iff, Finsupp.coe_add, Pi.single_eq_of_ne h] @[deprecated (since := "2024-12-01")] alias degreeOf_mul_X_ne := degreeOf_mul_X_of_ne theorem degreeOf_mul_X_self (j : σ) (f : MvPolynomial σ R) : degreeOf j (f * X j) ≤ degreeOf j f + 1 := by classical simp only [degreeOf] apply (Multiset.count_le_of_le j degrees_mul_le).trans simp only [Multiset.count_add, add_le_add_iff_left] convert Multiset.count_le_of_le j <| degrees_X' j rw [Multiset.count_singleton_self] @[deprecated (since := "2024-12-01")] alias degreeOf_mul_X_eq := degreeOf_mul_X_self theorem degreeOf_mul_X_eq_degreeOf_add_one_iff (j : σ) (f : MvPolynomial σ R) : degreeOf j (f * X j) = degreeOf j f + 1 ↔ f ≠ 0 := by refine ⟨fun h => by by_contra ha; simp [ha] at h, fun h => ?_⟩ apply Nat.le_antisymm (degreeOf_mul_X_self j f) have : (f.support.sup fun m ↦ m j) + 1 = (f.support.sup fun m ↦ (m j + 1)) := Finset.comp_sup_eq_sup_comp_of_nonempty @Nat.succ_le_succ (support_nonempty.mpr h) simp only [degreeOf_eq_sup, support_mul_X, this] apply Finset.sup_le intro x hx simp only [Finset.sup_map, bot_eq_zero', add_pos_iff, zero_lt_one, or_true, Finset.le_sup_iff] use x simpa using mem_support_iff.mp hx theorem degreeOf_C_mul_le (p : MvPolynomial σ R) (i : σ) (c : R) : (C c * p).degreeOf i ≤ p.degreeOf i := by unfold degreeOf convert Multiset.count_le_of_le i degrees_mul_le simp only [degrees_C, zero_add] theorem degreeOf_mul_C_le (p : MvPolynomial σ R) (i : σ) (c : R) : (p * C c).degreeOf i ≤ p.degreeOf i := by unfold degreeOf convert Multiset.count_le_of_le i degrees_mul_le simp only [degrees_C, add_zero] theorem degreeOf_rename_of_injective {p : MvPolynomial σ R} {f : σ → τ} (h : Function.Injective f) (i : σ) : degreeOf (f i) (rename f p) = degreeOf i p := by classical simp only [degreeOf, degrees_rename_of_injective h, Multiset.count_map_eq_count' f p.degrees h] end DegreeOf section TotalDegree /-! ### `totalDegree` -/ /-- `totalDegree p` gives the maximum |s| over the monomials X^s in `p` -/ def totalDegree (p : MvPolynomial σ R) : ℕ := p.support.sup fun s => s.sum fun _ e => e theorem totalDegree_eq (p : MvPolynomial σ R) : p.totalDegree = p.support.sup fun m => Multiset.card (toMultiset m) := by rw [totalDegree] congr; funext m exact (Finsupp.card_toMultiset _).symm theorem le_totalDegree {p : MvPolynomial σ R} {s : σ →₀ ℕ} (h : s ∈ p.support) : (s.sum fun _ e => e) ≤ totalDegree p := Finset.le_sup (α := ℕ) (f := fun s => sum s fun _ e => e) h theorem totalDegree_le_degrees_card (p : MvPolynomial σ R) : p.totalDegree ≤ Multiset.card p.degrees := by classical rw [totalDegree_eq] exact Finset.sup_le fun s hs => Multiset.card_le_card <| Finset.le_sup hs theorem totalDegree_le_of_support_subset (h : p.support ⊆ q.support) : totalDegree p ≤ totalDegree q := Finset.sup_mono h @[simp] theorem totalDegree_C (a : R) : (C a : MvPolynomial σ R).totalDegree = 0 := (supDegree_single 0 a).trans <| by rw [sum_zero_index, bot_eq_zero', ite_self] @[simp] theorem totalDegree_zero : (0 : MvPolynomial σ R).totalDegree = 0 := by rw [← C_0]; exact totalDegree_C (0 : R) @[simp] theorem totalDegree_one : (1 : MvPolynomial σ R).totalDegree = 0 := totalDegree_C (1 : R) @[simp] theorem totalDegree_X {R} [CommSemiring R] [Nontrivial R] (s : σ) : (X s : MvPolynomial σ R).totalDegree = 1 := by rw [totalDegree, support_X] simp only [Finset.sup, Finsupp.sum_single_index, Finset.fold_singleton, sup_bot_eq] theorem totalDegree_add (a b : MvPolynomial σ R) : (a + b).totalDegree ≤ max a.totalDegree b.totalDegree := sup_support_add_le _ _ _ theorem totalDegree_add_eq_left_of_totalDegree_lt {p q : MvPolynomial σ R} (h : q.totalDegree < p.totalDegree) : (p + q).totalDegree = p.totalDegree := by classical apply le_antisymm · rw [← max_eq_left_of_lt h] exact totalDegree_add p q by_cases hp : p = 0 · simp [hp] obtain ⟨b, hb₁, hb₂⟩ := p.support.exists_mem_eq_sup (Finsupp.support_nonempty_iff.mpr hp) fun m : σ →₀ ℕ => Multiset.card (toMultiset m) have hb : ¬b ∈ q.support := by contrapose! h rw [totalDegree_eq p, hb₂, totalDegree_eq] apply Finset.le_sup h have hbb : b ∈ (p + q).support := by apply support_sdiff_support_subset_support_add rw [Finset.mem_sdiff] exact ⟨hb₁, hb⟩ rw [totalDegree_eq, hb₂, totalDegree_eq] exact Finset.le_sup (f := fun m => Multiset.card (Finsupp.toMultiset m)) hbb theorem totalDegree_add_eq_right_of_totalDegree_lt {p q : MvPolynomial σ R} (h : q.totalDegree < p.totalDegree) : (q + p).totalDegree = p.totalDegree := by rw [add_comm, totalDegree_add_eq_left_of_totalDegree_lt h] theorem totalDegree_mul (a b : MvPolynomial σ R) : (a * b).totalDegree ≤ a.totalDegree + b.totalDegree := sup_support_mul_le (by exact (Finsupp.sum_add_index' (fun _ => rfl) (fun _ _ _ => rfl)).le) _ _ theorem totalDegree_smul_le [CommSemiring S] [DistribMulAction R S] (a : R) (f : MvPolynomial σ S) : (a • f).totalDegree ≤ f.totalDegree := Finset.sup_mono support_smul theorem totalDegree_pow (a : MvPolynomial σ R) (n : ℕ) : (a ^ n).totalDegree ≤ n * a.totalDegree := by rw [Finset.pow_eq_prod_const, ← Nat.nsmul_eq_mul, Finset.nsmul_eq_sum_const] refine supDegree_prod_le rfl (fun _ _ => ?_) exact Finsupp.sum_add_index' (fun _ => rfl) (fun _ _ _ => rfl) @[simp] theorem totalDegree_monomial (s : σ →₀ ℕ) {c : R} (hc : c ≠ 0) : (monomial s c : MvPolynomial σ R).totalDegree = s.sum fun _ e => e := by classical simp [totalDegree, support_monomial, if_neg hc] theorem totalDegree_monomial_le (s : σ →₀ ℕ) (c : R) : (monomial s c).totalDegree ≤ s.sum fun _ ↦ id := by if hc : c = 0 then simp only [hc, map_zero, totalDegree_zero, zero_le] else rw [totalDegree_monomial _ hc, Function.id_def] @[simp] theorem totalDegree_X_pow [Nontrivial R] (s : σ) (n : ℕ) : (X s ^ n : MvPolynomial σ R).totalDegree = n := by simp [X_pow_eq_monomial, one_ne_zero] theorem totalDegree_list_prod : ∀ s : List (MvPolynomial σ R), s.prod.totalDegree ≤ (s.map MvPolynomial.totalDegree).sum | [] => by rw [List.prod_nil, totalDegree_one, List.map_nil, List.sum_nil] | p::ps => by rw [List.prod_cons, List.map, List.sum_cons] exact le_trans (totalDegree_mul _ _) (add_le_add_left (totalDegree_list_prod ps) _) theorem totalDegree_multiset_prod (s : Multiset (MvPolynomial σ R)) : s.prod.totalDegree ≤ (s.map MvPolynomial.totalDegree).sum := by refine Quotient.inductionOn s fun l => ?_ rw [Multiset.quot_mk_to_coe, Multiset.prod_coe, Multiset.map_coe, Multiset.sum_coe] exact totalDegree_list_prod l theorem totalDegree_finset_prod {ι : Type*} (s : Finset ι) (f : ι → MvPolynomial σ R) : (s.prod f).totalDegree ≤ ∑ i ∈ s, (f i).totalDegree := by refine le_trans (totalDegree_multiset_prod _) ?_ simp only [Multiset.map_map, comp_apply, Finset.sum_map_val, le_refl] theorem totalDegree_finset_sum {ι : Type*} (s : Finset ι) (f : ι → MvPolynomial σ R) : (s.sum f).totalDegree ≤ Finset.sup s fun i => (f i).totalDegree := by induction' s using Finset.cons_induction with a s has hind
· exact zero_le _ · rw [Finset.sum_cons, Finset.sup_cons]
Mathlib/Algebra/MvPolynomial/Degrees.lean
469
470
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Algebra.Order.Group.Unbundled.Int import Mathlib.Algebra.Ring.Nat import Mathlib.Data.Int.GCD /-! # Congruences modulo a natural number This file defines the equivalence relation `a ≡ b [MOD n]` on the natural numbers, and proves basic properties about it such as the Chinese Remainder Theorem `modEq_and_modEq_iff_modEq_mul`. ## Notations `a ≡ b [MOD n]` is notation for `nat.ModEq n a b`, which is defined to mean `a % n = b % n`. ## Tags ModEq, congruence, mod, MOD, modulo -/ assert_not_exists OrderedAddCommMonoid Function.support namespace Nat /-- Modular equality. `n.ModEq a b`, or `a ≡ b [MOD n]`, means that `a - b` is a multiple of `n`. -/ def ModEq (n a b : ℕ) := a % n = b % n @[inherit_doc] notation:50 a " ≡ " b " [MOD " n "]" => ModEq n a b variable {m n a b c d : ℕ} -- Since `ModEq` is semi-reducible, we need to provide the decidable instance manually instance : Decidable (ModEq n a b) := inferInstanceAs <| Decidable (a % n = b % n) namespace ModEq @[refl] protected theorem refl (a : ℕ) : a ≡ a [MOD n] := rfl protected theorem rfl : a ≡ a [MOD n] := ModEq.refl _ instance : IsRefl _ (ModEq n) := ⟨ModEq.refl⟩ @[symm] protected theorem symm : a ≡ b [MOD n] → b ≡ a [MOD n] := Eq.symm @[trans] protected theorem trans : a ≡ b [MOD n] → b ≡ c [MOD n] → a ≡ c [MOD n] := Eq.trans instance : Trans (ModEq n) (ModEq n) (ModEq n) where trans := Nat.ModEq.trans protected theorem comm : a ≡ b [MOD n] ↔ b ≡ a [MOD n] := ⟨ModEq.symm, ModEq.symm⟩ end ModEq theorem modEq_zero_iff_dvd : a ≡ 0 [MOD n] ↔ n ∣ a := by rw [ModEq, zero_mod, dvd_iff_mod_eq_zero] theorem _root_.Dvd.dvd.modEq_zero_nat (h : n ∣ a) : a ≡ 0 [MOD n] := modEq_zero_iff_dvd.2 h theorem _root_.Dvd.dvd.zero_modEq_nat (h : n ∣ a) : 0 ≡ a [MOD n] := h.modEq_zero_nat.symm theorem modEq_iff_dvd : a ≡ b [MOD n] ↔ (n : ℤ) ∣ b - a := by rw [ModEq, eq_comm, ← Int.natCast_inj, Int.natCast_mod, Int.natCast_mod, Int.emod_eq_emod_iff_emod_sub_eq_zero, Int.dvd_iff_emod_eq_zero] alias ⟨ModEq.dvd, modEq_of_dvd⟩ := modEq_iff_dvd /-- A variant of `modEq_iff_dvd` with `Nat` divisibility -/ theorem modEq_iff_dvd' (h : a ≤ b) : a ≡ b [MOD n] ↔ n ∣ b - a := by rw [modEq_iff_dvd, ← Int.natCast_dvd_natCast, Int.ofNat_sub h] theorem mod_modEq (a n) : a % n ≡ a [MOD n] := mod_mod _ _ namespace ModEq lemma of_dvd (d : m ∣ n) (h : a ≡ b [MOD n]) : a ≡ b [MOD m] := modEq_of_dvd <| Int.ofNat_dvd.mpr d |>.trans h.dvd protected theorem mul_left' (c : ℕ) (h : a ≡ b [MOD n]) : c * a ≡ c * b [MOD c * n] := by unfold ModEq at *; rw [mul_mod_mul_left, mul_mod_mul_left, h] @[gcongr] protected theorem mul_left (c : ℕ) (h : a ≡ b [MOD n]) : c * a ≡ c * b [MOD n] := (h.mul_left' _).of_dvd (dvd_mul_left _ _) protected theorem mul_right' (c : ℕ) (h : a ≡ b [MOD n]) : a * c ≡ b * c [MOD n * c] := by rw [mul_comm a, mul_comm b, mul_comm n]; exact h.mul_left' c @[gcongr] protected theorem mul_right (c : ℕ) (h : a ≡ b [MOD n]) : a * c ≡ b * c [MOD n] := by rw [mul_comm a, mul_comm b]; exact h.mul_left c @[gcongr] protected theorem mul (h₁ : a ≡ b [MOD n]) (h₂ : c ≡ d [MOD n]) : a * c ≡ b * d [MOD n] := (h₂.mul_left _).trans (h₁.mul_right _)
@[gcongr] protected theorem pow (m : ℕ) (h : a ≡ b [MOD n]) : a ^ m ≡ b ^ m [MOD n] := by
Mathlib/Data/Nat/ModEq.lean
113
114
/- Copyright (c) 2023 Kim Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kim Morrison -/ import Mathlib.Algebra.Group.Defs import Batteries.Data.List.Basic /-! # Levenshtein distances We define the Levenshtein edit distance `levenshtein C xy ys` between two `List α`, with a customizable cost structure `C` for the `delete`, `insert`, and `substitute` operations. As an auxiliary function, we define `suffixLevenshtein C xs ys`, which gives the list of distances from each suffix of `xs` to `ys`. This is defined by recursion on `ys`, using the internal function `Levenshtein.impl`, which computes `suffixLevenshtein C xs (y :: ys)` using `xs`, `y`, and `suffixLevenshtein C xs ys`. (This corresponds to the usual algorithm using the last two rows of the matrix of distances between suffixes.) After setting up these definitions, we prove lemmas specifying their behaviour, particularly ``` theorem suffixLevenshtein_eq_tails_map : (suffixLevenshtein C xs ys).1 = xs.tails.map fun xs' => levenshtein C xs' ys := ... ``` and ``` theorem levenshtein_cons_cons : levenshtein C (x :: xs) (y :: ys) = min (C.delete x + levenshtein C xs (y :: ys)) (min (C.insert y + levenshtein C (x :: xs) ys) (C.substitute x y + levenshtein C xs ys)) := ... ``` -/ variable {α β δ : Type*} [AddZeroClass δ] [Min δ] namespace Levenshtein /-- A cost structure for Levenshtein edit distance. -/ structure Cost (α β δ : Type*) where /-- Cost to delete an element from a list. -/ delete : α → δ /-- Cost in insert an element into a list. -/ insert : β → δ /-- Cost to substitute one element for another in a list. -/ substitute : α → β → δ /-- The default cost structure, for which all operations cost `1`. -/ @[simps] def defaultCost [DecidableEq α] : Cost α α ℕ where delete _ := 1 insert _ := 1 substitute a b := if a = b then 0 else 1 instance [DecidableEq α] : Inhabited (Cost α α ℕ) := ⟨defaultCost⟩ /-- Cost structure given by a function. Delete and insert cost the same, and substitution costs the greater value. -/ @[simps] def weightCost (f : α → ℕ) : Cost α α ℕ where delete a := f a insert b := f b substitute a b := max (f a) (f b) /-- Cost structure for strings, where cost is the length of the token. -/ @[simps!] def stringLengthCost : Cost String String ℕ := weightCost String.length /-- Cost structure for strings, where cost is the log base 2 length of the token. -/ @[simps!] def stringLogLengthCost : Cost String String ℕ := weightCost fun s => Nat.log2 (s.length + 1) variable (C : Cost α β δ) /-- (Implementation detail for `levenshtein`) Given a list `xs` and the Levenshtein distances from each suffix of `xs` to some other list `ys`, compute the Levenshtein distances from each suffix of `xs` to `y :: ys`. (Note that we don't actually need to know `ys` itself here, so it is not an argument.) The return value is a list of length `x.length + 1`, and it is convenient for the recursive calls that we bundle this list with a proof that it is non-empty. -/ def impl (xs : List α) (y : β) (d : {r : List δ // 0 < r.length}) : {r : List δ // 0 < r.length} := let ⟨ds, w⟩ := d xs.zip (ds.zip ds.tail) |>.foldr (init := ⟨[C.insert y + ds.getLast (List.length_pos_iff.mp w)], by simp⟩) (fun ⟨x, d₀, d₁⟩ ⟨r, w⟩ => ⟨min (C.delete x + r[0]) (min (C.insert y + d₀) (C.substitute x y + d₁)) :: r, by simp⟩) variable {C} variable (x : α) (xs : List α) (y : β) (d : δ) (ds : List δ) (w : 0 < (d :: ds).length) -- Note this lemma has an unspecified proof `w'` on the right-hand-side, -- which will become an extra goal when rewriting. theorem impl_cons (w' : 0 < List.length ds) : impl C (x :: xs) y ⟨d :: ds, w⟩ = let ⟨r, w⟩ := impl C xs y ⟨ds, w'⟩ ⟨min (C.delete x + r[0]) (min (C.insert y + d) (C.substitute x y + ds[0])) :: r, by simp⟩ := match ds, w' with | _ :: _, _ => rfl -- Note this lemma has two unspecified proofs: `h` appears on the left-hand-side -- and should be found by matching, but `w'` will become an extra goal when rewriting. theorem impl_cons_fst_zero (h : 0 < (impl C (x :: xs) y ⟨d :: ds, w⟩).val.length) (w' : 0 < List.length ds) : (impl C (x :: xs) y ⟨d :: ds, w⟩).1[0] = let ⟨r, w⟩ := impl C xs y ⟨ds, w'⟩ min (C.delete x + r[0]) (min (C.insert y + d) (C.substitute x y + ds[0])) := match ds, w' with | _ :: _, _ => rfl theorem impl_length (d : {r : List δ // 0 < r.length}) (w : d.1.length = xs.length + 1) : (impl C xs y d).1.length = xs.length + 1 := by induction xs generalizing d with | nil => rfl | cons x xs ih => dsimp [impl] match d, w with | ⟨d₁ :: d₂ :: ds, _⟩, w => dsimp congr 1 exact ih ⟨d₂ :: ds, (by simp)⟩ (by simpa using w) end Levenshtein open Levenshtein variable (C : Cost α β δ) /-- `suffixLevenshtein C xs ys` computes the Levenshtein distance (using the cost functions provided by a `C : Cost α β δ`) from each suffix of the list `xs` to the list `ys`. The first element of this list is the Levenshtein distance from `xs` to `ys`. Note that if the cost functions do not satisfy the inequalities * `C.delete a + C.insert b ≥ C.substitute a b` * `C.substitute a b + C.substitute b c ≥ C.substitute a c` (or if any values are negative) then the edit distances calculated here may not agree with the general geodesic distance on the edit graph. -/ def suffixLevenshtein (xs : List α) (ys : List β) : {r : List δ // 0 < r.length} := ys.foldr (impl C xs) (xs.foldr (init := ⟨[0], by simp⟩) (fun a ⟨r, w⟩ => ⟨(C.delete a + r[0]) :: r, by simp⟩)) variable {C} theorem suffixLevenshtein_length (xs : List α) (ys : List β) : (suffixLevenshtein C xs ys).1.length = xs.length + 1 := by induction ys with | nil => dsimp [suffixLevenshtein] induction xs with | nil => rfl | cons _ xs ih => simp_all | cons y ys ih => dsimp [suffixLevenshtein] rw [impl_length] exact ih -- This is only used in keeping track of estimates. theorem suffixLevenshtein_eq (xs : List α) (y ys) : impl C xs y (suffixLevenshtein C xs ys) = suffixLevenshtein C xs (y :: ys) := by rfl variable (C) /-- `levenshtein C xs ys` computes the Levenshtein distance (using the cost functions provided by a `C : Cost α β δ`) from the list `xs` to the list `ys`. Note that if the cost functions do not satisfy the inequalities * `C.delete a + C.insert b ≥ C.substitute a b` * `C.substitute a b + C.substitute b c ≥ C.substitute a c` (or if any values are negative) then the edit distance calculated here may not agree with the general geodesic distance on the edit graph. -/ def levenshtein (xs : List α) (ys : List β) : δ := let ⟨r, w⟩ := suffixLevenshtein C xs ys r[0] variable {C} theorem suffixLevenshtein_nil_nil : (suffixLevenshtein C [] []).1 = [0] := by rfl -- Not sure if this belongs in the main `List` API, or can stay local. theorem List.eq_of_length_one (x : List α) (w : x.length = 1) : have : 0 < x.length := lt_of_lt_of_eq Nat.zero_lt_one w.symm x = [x[0]] := by match x, w with | [r], _ => rfl theorem suffixLevenshtein_nil' (ys : List β) : (suffixLevenshtein C [] ys).1 = [levenshtein C [] ys] := List.eq_of_length_one _ (suffixLevenshtein_length [] _) theorem suffixLevenshtein_cons₂ (xs : List α) (y ys) : suffixLevenshtein C xs (y :: ys) = (impl C xs) y (suffixLevenshtein C xs ys) := rfl theorem suffixLevenshtein_cons₁_aux {α} {x y : { l : List α // 0 < l.length }} (w₀ : x.1[0]'x.2 = y.1[0]'y.2) (w : x.1.tail = y.1.tail) : x = y := by match x, y with | ⟨hx :: tx, _⟩, ⟨hy :: ty, _⟩ => simp_all
theorem suffixLevenshtein_cons₁ (x : α) (xs ys) : suffixLevenshtein C (x :: xs) ys = ⟨levenshtein C (x :: xs) ys :: (suffixLevenshtein C xs ys).1, by simp⟩ := by induction ys with | nil => dsimp [levenshtein, suffixLevenshtein] | cons y ys ih => apply suffixLevenshtein_cons₁_aux · rfl · rw [suffixLevenshtein_cons₂ (x :: xs), ih, impl_cons] · rfl
Mathlib/Data/List/EditDistance/Defs.lean
226
239
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Yury Kudryashov, Kexing Ying -/ import Mathlib.Topology.Semicontinuous import Mathlib.MeasureTheory.Function.AEMeasurableSequence import Mathlib.MeasureTheory.Order.Lattice import Mathlib.Topology.Order.Lattice import Mathlib.MeasureTheory.Constructions.BorelSpace.Basic /-! # Borel sigma algebras on spaces with orders ## Main statements * `borel_eq_generateFrom_Ixx` (where Ixx is one of {Iio, Ioi, Iic, Ici, Ico, Ioc}): The Borel sigma algebra of a linear order topology is generated by intervals of the given kind. * `Dense.borel_eq_generateFrom_Ico_mem`, `Dense.borel_eq_generateFrom_Ioc_mem`: The Borel sigma algebra of a dense linear order topology is generated by intervals of a given kind, with endpoints from dense subsets. * `ext_of_Ico`, `ext_of_Ioc`: A locally finite Borel measure on a second countable conditionally complete linear order is characterized by the measures of intervals of the given kind. * `ext_of_Iic`, `ext_of_Ici`: A finite Borel measure on a second countable linear order is characterized by the measures of intervals of the given kind. * `UpperSemicontinuous.measurable`, `LowerSemicontinuous.measurable`: Semicontinuous functions are measurable. * `Measurable.iSup`, `Measurable.iInf`, `Measurable.sSup`, `Measurable.sInf`: Countable supremums and infimums of measurable functions to conditionally complete linear orders are measurable. * `Measurable.liminf`, `Measurable.limsup`: Countable liminfs and limsups of measurable functions to conditionally complete linear orders are measurable. -/ open Set Filter MeasureTheory MeasurableSpace TopologicalSpace open scoped Topology NNReal ENNReal MeasureTheory universe u v w x y variable {α β γ δ : Type*} {ι : Sort y} {s t u : Set α} section OrderTopology variable (α) variable [TopologicalSpace α] [SecondCountableTopology α] [LinearOrder α] [OrderTopology α] theorem borel_eq_generateFrom_Iio : borel α = .generateFrom (range Iio) := by refine le_antisymm ?_ (generateFrom_le ?_) · rw [borel_eq_generateFrom_of_subbasis (@OrderTopology.topology_eq_generate_intervals α _ _ _)] letI : MeasurableSpace α := MeasurableSpace.generateFrom (range Iio) have H : ∀ a : α, MeasurableSet (Iio a) := fun a => GenerateMeasurable.basic _ ⟨_, rfl⟩ refine generateFrom_le ?_ rintro _ ⟨a, rfl | rfl⟩ · rcases em (∃ b, a ⋖ b) with ⟨b, hb⟩ | hcovBy · rw [hb.Ioi_eq, ← compl_Iio] exact (H _).compl · rcases isOpen_biUnion_countable (Ioi a) Ioi fun _ _ ↦ isOpen_Ioi with ⟨t, hat, htc, htU⟩ have : Ioi a = ⋃ b ∈ t, Ici b := by refine Subset.antisymm ?_ <| iUnion₂_subset fun b hb ↦ Ici_subset_Ioi.2 (hat hb) refine Subset.trans ?_ <| iUnion₂_mono fun _ _ ↦ Ioi_subset_Ici_self simpa [CovBy, htU, subset_def] using hcovBy simp only [this, ← compl_Iio] exact .biUnion htc <| fun _ _ ↦ (H _).compl · apply H · rw [forall_mem_range] intro a exact GenerateMeasurable.basic _ isOpen_Iio theorem borel_eq_generateFrom_Ioi : borel α = .generateFrom (range Ioi) := @borel_eq_generateFrom_Iio αᵒᵈ _ (by infer_instance : SecondCountableTopology α) _ _ theorem borel_eq_generateFrom_Iic : borel α = MeasurableSpace.generateFrom (range Iic) := by rw [borel_eq_generateFrom_Ioi] refine le_antisymm ?_ ?_ · refine MeasurableSpace.generateFrom_le fun t ht => ?_ obtain ⟨u, rfl⟩ := ht rw [← compl_Iic] exact (MeasurableSpace.measurableSet_generateFrom (mem_range.mpr ⟨u, rfl⟩)).compl · refine MeasurableSpace.generateFrom_le fun t ht => ?_ obtain ⟨u, rfl⟩ := ht rw [← compl_Ioi] exact (MeasurableSpace.measurableSet_generateFrom (mem_range.mpr ⟨u, rfl⟩)).compl theorem borel_eq_generateFrom_Ici : borel α = MeasurableSpace.generateFrom (range Ici) := @borel_eq_generateFrom_Iic αᵒᵈ _ _ _ _ end OrderTopology section Orders variable [TopologicalSpace α] {mα : MeasurableSpace α} [OpensMeasurableSpace α] variable {mδ : MeasurableSpace δ} section Preorder variable [Preorder α] [OrderClosedTopology α] {a b x : α} {μ : Measure α} @[simp, measurability] theorem measurableSet_Ici : MeasurableSet (Ici a) := isClosed_Ici.measurableSet theorem nullMeasurableSet_Ici : NullMeasurableSet (Ici a) μ := measurableSet_Ici.nullMeasurableSet @[simp, measurability] theorem measurableSet_Iic : MeasurableSet (Iic a) := isClosed_Iic.measurableSet theorem nullMeasurableSet_Iic : NullMeasurableSet (Iic a) μ := measurableSet_Iic.nullMeasurableSet @[simp, measurability] theorem measurableSet_Icc : MeasurableSet (Icc a b) := isClosed_Icc.measurableSet theorem nullMeasurableSet_Icc : NullMeasurableSet (Icc a b) μ := measurableSet_Icc.nullMeasurableSet instance nhdsWithin_Ici_isMeasurablyGenerated : (𝓝[Ici b] a).IsMeasurablyGenerated := measurableSet_Ici.nhdsWithin_isMeasurablyGenerated _ instance nhdsWithin_Iic_isMeasurablyGenerated : (𝓝[Iic b] a).IsMeasurablyGenerated := measurableSet_Iic.nhdsWithin_isMeasurablyGenerated _ instance nhdsWithin_Icc_isMeasurablyGenerated : IsMeasurablyGenerated (𝓝[Icc a b] x) := by rw [← Ici_inter_Iic, nhdsWithin_inter] infer_instance instance atTop_isMeasurablyGenerated : (Filter.atTop : Filter α).IsMeasurablyGenerated := @Filter.iInf_isMeasurablyGenerated _ _ _ _ fun a => (measurableSet_Ici : MeasurableSet (Ici a)).principal_isMeasurablyGenerated instance atBot_isMeasurablyGenerated : (Filter.atBot : Filter α).IsMeasurablyGenerated := @Filter.iInf_isMeasurablyGenerated _ _ _ _ fun a => (measurableSet_Iic : MeasurableSet (Iic a)).principal_isMeasurablyGenerated instance [R1Space α] : IsMeasurablyGenerated (cocompact α) where exists_measurable_subset := by intro _ hs obtain ⟨t, ht, hts⟩ := mem_cocompact.mp hs exact ⟨(closure t)ᶜ, ht.closure.compl_mem_cocompact, isClosed_closure.measurableSet.compl, (compl_subset_compl.2 subset_closure).trans hts⟩ end Preorder section PartialOrder variable [PartialOrder α] [OrderClosedTopology α] [SecondCountableTopology α] {a b : α} @[measurability] theorem measurableSet_le' : MeasurableSet { p : α × α | p.1 ≤ p.2 } := OrderClosedTopology.isClosed_le'.measurableSet @[measurability] theorem measurableSet_le {f g : δ → α} (hf : Measurable f) (hg : Measurable g) : MeasurableSet { a | f a ≤ g a } := hf.prodMk hg measurableSet_le' end PartialOrder section LinearOrder variable [LinearOrder α] [OrderClosedTopology α] {a b x : α} {μ : Measure α} -- we open this locale only here to avoid issues with list being treated as intervals above open Interval @[simp, measurability] theorem measurableSet_Iio : MeasurableSet (Iio a) := isOpen_Iio.measurableSet theorem nullMeasurableSet_Iio : NullMeasurableSet (Iio a) μ := measurableSet_Iio.nullMeasurableSet @[simp, measurability] theorem measurableSet_Ioi : MeasurableSet (Ioi a) := isOpen_Ioi.measurableSet theorem nullMeasurableSet_Ioi : NullMeasurableSet (Ioi a) μ := measurableSet_Ioi.nullMeasurableSet @[simp, measurability] theorem measurableSet_Ioo : MeasurableSet (Ioo a b) := isOpen_Ioo.measurableSet theorem nullMeasurableSet_Ioo : NullMeasurableSet (Ioo a b) μ := measurableSet_Ioo.nullMeasurableSet @[simp, measurability] theorem measurableSet_Ioc : MeasurableSet (Ioc a b) := measurableSet_Ioi.inter measurableSet_Iic theorem nullMeasurableSet_Ioc : NullMeasurableSet (Ioc a b) μ := measurableSet_Ioc.nullMeasurableSet @[simp, measurability] theorem measurableSet_Ico : MeasurableSet (Ico a b) := measurableSet_Ici.inter measurableSet_Iio theorem nullMeasurableSet_Ico : NullMeasurableSet (Ico a b) μ := measurableSet_Ico.nullMeasurableSet instance nhdsWithin_Ioi_isMeasurablyGenerated : (𝓝[Ioi b] a).IsMeasurablyGenerated := measurableSet_Ioi.nhdsWithin_isMeasurablyGenerated _ instance nhdsWithin_Iio_isMeasurablyGenerated : (𝓝[Iio b] a).IsMeasurablyGenerated := measurableSet_Iio.nhdsWithin_isMeasurablyGenerated _ instance nhdsWithin_uIcc_isMeasurablyGenerated : IsMeasurablyGenerated (𝓝[[[a, b]]] x) := nhdsWithin_Icc_isMeasurablyGenerated @[measurability] theorem measurableSet_lt' [SecondCountableTopology α] : MeasurableSet { p : α × α | p.1 < p.2 } := (isOpen_lt continuous_fst continuous_snd).measurableSet @[measurability] theorem measurableSet_lt [SecondCountableTopology α] {f g : δ → α} (hf : Measurable f) (hg : Measurable g) : MeasurableSet { a | f a < g a } := hf.prodMk hg measurableSet_lt' theorem nullMeasurableSet_lt [SecondCountableTopology α] {μ : Measure δ} {f g : δ → α} (hf : AEMeasurable f μ) (hg : AEMeasurable g μ) : NullMeasurableSet { a | f a < g a } μ := (hf.prodMk hg).nullMeasurable measurableSet_lt' theorem nullMeasurableSet_lt' [SecondCountableTopology α] {μ : Measure (α × α)} : NullMeasurableSet { p : α × α | p.1 < p.2 } μ := measurableSet_lt'.nullMeasurableSet theorem nullMeasurableSet_le [SecondCountableTopology α] {μ : Measure δ} {f g : δ → α} (hf : AEMeasurable f μ) (hg : AEMeasurable g μ) : NullMeasurableSet { a | f a ≤ g a } μ := (hf.prodMk hg).nullMeasurable measurableSet_le' theorem Set.OrdConnected.measurableSet (h : OrdConnected s) : MeasurableSet s := by let u := ⋃ (x ∈ s) (y ∈ s), Ioo x y have huopen : IsOpen u := isOpen_biUnion fun _ _ => isOpen_biUnion fun _ _ => isOpen_Ioo have humeas : MeasurableSet u := huopen.measurableSet have hfinite : (s \ u).Finite := s.finite_diff_iUnion_Ioo have : u ⊆ s := iUnion₂_subset fun x hx => iUnion₂_subset fun y hy => Ioo_subset_Icc_self.trans (h.out hx hy) rw [← union_diff_cancel this] exact humeas.union hfinite.measurableSet theorem IsPreconnected.measurableSet (h : IsPreconnected s) : MeasurableSet s := h.ordConnected.measurableSet theorem generateFrom_Ico_mem_le_borel {α : Type*} [TopologicalSpace α] [LinearOrder α] [OrderClosedTopology α] (s t : Set α) : MeasurableSpace.generateFrom { S | ∃ l ∈ s, ∃ u ∈ t, l < u ∧ Ico l u = S } ≤ borel α := by apply generateFrom_le borelize α rintro _ ⟨a, -, b, -, -, rfl⟩ exact measurableSet_Ico theorem Dense.borel_eq_generateFrom_Ico_mem_aux {α : Type*} [TopologicalSpace α] [LinearOrder α] [OrderTopology α] [SecondCountableTopology α] {s : Set α} (hd : Dense s) (hbot : ∀ x, IsBot x → x ∈ s) (hIoo : ∀ x y : α, x < y → Ioo x y = ∅ → y ∈ s) : borel α = .generateFrom { S : Set α | ∃ l ∈ s, ∃ u ∈ s, l < u ∧ Ico l u = S } := by set S : Set (Set α) := { S | ∃ l ∈ s, ∃ u ∈ s, l < u ∧ Ico l u = S } refine le_antisymm ?_ (generateFrom_Ico_mem_le_borel _ _) letI : MeasurableSpace α := generateFrom S rw [borel_eq_generateFrom_Iio] refine generateFrom_le (forall_mem_range.2 fun a => ?_) rcases hd.exists_countable_dense_subset_bot_top with ⟨t, hts, hc, htd, htb, -⟩ by_cases ha : ∀ b < a, (Ioo b a).Nonempty · convert_to MeasurableSet (⋃ (l ∈ t) (u ∈ t) (_ : l < u) (_ : u ≤ a), Ico l u) · ext y simp only [mem_iUnion, mem_Iio, mem_Ico] constructor · intro hy rcases htd.exists_le' (fun b hb => htb _ hb (hbot b hb)) y with ⟨l, hlt, hly⟩ rcases htd.exists_mem_open isOpen_Ioo (ha y hy) with ⟨u, hut, hyu, hua⟩ exact ⟨l, hlt, u, hut, hly.trans_lt hyu, hua.le, hly, hyu⟩ · rintro ⟨l, -, u, -, -, hua, -, hyu⟩ exact hyu.trans_le hua · refine MeasurableSet.biUnion hc fun a ha => MeasurableSet.biUnion hc fun b hb => ?_ refine MeasurableSet.iUnion fun hab => MeasurableSet.iUnion fun _ => ?_ exact .basic _ ⟨a, hts ha, b, hts hb, hab, mem_singleton _⟩ · simp only [not_forall, not_nonempty_iff_eq_empty] at ha replace ha : a ∈ s := hIoo ha.choose a ha.choose_spec.fst ha.choose_spec.snd convert_to MeasurableSet (⋃ (l ∈ t) (_ : l < a), Ico l a) · symm simp only [← Ici_inter_Iio, ← iUnion_inter, inter_eq_right, subset_def, mem_iUnion, mem_Ici, mem_Iio] intro x hx rcases htd.exists_le' (fun b hb => htb _ hb (hbot b hb)) x with ⟨z, hzt, hzx⟩ exact ⟨z, hzt, hzx.trans_lt hx, hzx⟩ · refine .biUnion hc fun x hx => MeasurableSet.iUnion fun hlt => ?_ exact .basic _ ⟨x, hts hx, a, ha, hlt, mem_singleton _⟩ theorem Dense.borel_eq_generateFrom_Ico_mem {α : Type*} [TopologicalSpace α] [LinearOrder α] [OrderTopology α] [SecondCountableTopology α] [DenselyOrdered α] [NoMinOrder α] {s : Set α} (hd : Dense s) : borel α = .generateFrom { S : Set α | ∃ l ∈ s, ∃ u ∈ s, l < u ∧ Ico l u = S } := hd.borel_eq_generateFrom_Ico_mem_aux (by simp) fun _ _ hxy H => ((nonempty_Ioo.2 hxy).ne_empty H).elim theorem borel_eq_generateFrom_Ico (α : Type*) [TopologicalSpace α] [SecondCountableTopology α] [LinearOrder α] [OrderTopology α] : borel α = .generateFrom { S : Set α | ∃ (l u : α), l < u ∧ Ico l u = S } := by simpa only [exists_prop, mem_univ, true_and] using (@dense_univ α _).borel_eq_generateFrom_Ico_mem_aux (fun _ _ => mem_univ _) fun _ _ _ _ => mem_univ _ theorem Dense.borel_eq_generateFrom_Ioc_mem_aux {α : Type*} [TopologicalSpace α] [LinearOrder α] [OrderTopology α] [SecondCountableTopology α] {s : Set α} (hd : Dense s) (hbot : ∀ x, IsTop x → x ∈ s) (hIoo : ∀ x y : α, x < y → Ioo x y = ∅ → x ∈ s) : borel α = .generateFrom { S : Set α | ∃ l ∈ s, ∃ u ∈ s, l < u ∧ Ioc l u = S } := by convert hd.orderDual.borel_eq_generateFrom_Ico_mem_aux hbot fun x y hlt he => hIoo y x hlt _ using 2 · ext s constructor <;> rintro ⟨l, hl, u, hu, hlt, rfl⟩ exacts [⟨u, hu, l, hl, hlt, Ico_toDual⟩, ⟨u, hu, l, hl, hlt, Ioc_toDual⟩] · erw [Ioo_toDual] exact he theorem Dense.borel_eq_generateFrom_Ioc_mem {α : Type*} [TopologicalSpace α] [LinearOrder α] [OrderTopology α] [SecondCountableTopology α] [DenselyOrdered α] [NoMaxOrder α] {s : Set α} (hd : Dense s) : borel α = .generateFrom { S : Set α | ∃ l ∈ s, ∃ u ∈ s, l < u ∧ Ioc l u = S } := hd.borel_eq_generateFrom_Ioc_mem_aux (by simp) fun _ _ hxy H => ((nonempty_Ioo.2 hxy).ne_empty H).elim theorem borel_eq_generateFrom_Ioc (α : Type*) [TopologicalSpace α] [SecondCountableTopology α] [LinearOrder α] [OrderTopology α] : borel α = .generateFrom { S : Set α | ∃ l u, l < u ∧ Ioc l u = S } := by simpa only [exists_prop, mem_univ, true_and] using (@dense_univ α _).borel_eq_generateFrom_Ioc_mem_aux (fun _ _ => mem_univ _) fun _ _ _ _ => mem_univ _ namespace MeasureTheory.Measure /-- Two finite measures on a Borel space are equal if they agree on all closed-open intervals. If `α` is a conditionally complete linear order with no top element, `MeasureTheory.Measure.ext_of_Ico` is an extensionality lemma with weaker assumptions on `μ` and `ν`. -/ theorem ext_of_Ico_finite {α : Type*} [TopologicalSpace α] {m : MeasurableSpace α} [SecondCountableTopology α] [LinearOrder α] [OrderTopology α] [BorelSpace α] (μ ν : Measure α) [IsFiniteMeasure μ] (hμν : μ univ = ν univ) (h : ∀ ⦃a b⦄, a < b → μ (Ico a b) = ν (Ico a b)) : μ = ν := by refine ext_of_generate_finite _ (BorelSpace.measurable_eq.trans (borel_eq_generateFrom_Ico α)) (isPiSystem_Ico (id : α → α) id) ?_ hμν rintro - ⟨a, b, hlt, rfl⟩ exact h hlt /-- Two finite measures on a Borel space are equal if they agree on all open-closed intervals. If `α` is a conditionally complete linear order with no top element, `MeasureTheory.Measure.ext_of_Ioc` is an extensionality lemma with weaker assumptions on `μ` and `ν`. -/ theorem ext_of_Ioc_finite {α : Type*} [TopologicalSpace α] {m : MeasurableSpace α} [SecondCountableTopology α] [LinearOrder α] [OrderTopology α] [BorelSpace α] (μ ν : Measure α) [IsFiniteMeasure μ] (hμν : μ univ = ν univ) (h : ∀ ⦃a b⦄, a < b → μ (Ioc a b) = ν (Ioc a b)) : μ = ν := by refine @ext_of_Ico_finite αᵒᵈ _ _ _ _ _ ‹_› μ ν _ hμν fun a b hab => ?_ erw [Ico_toDual (α := α)] exact h hab /-- Two measures which are finite on closed-open intervals are equal if they agree on all closed-open intervals. -/ theorem ext_of_Ico' {α : Type*} [TopologicalSpace α] {m : MeasurableSpace α} [SecondCountableTopology α] [LinearOrder α] [OrderTopology α] [BorelSpace α] [NoMaxOrder α] (μ ν : Measure α) (hμ : ∀ ⦃a b⦄, a < b → μ (Ico a b) ≠ ∞) (h : ∀ ⦃a b⦄, a < b → μ (Ico a b) = ν (Ico a b)) : μ = ν := by rcases exists_countable_dense_bot_top α with ⟨s, hsc, hsd, hsb, _⟩ have : (⋃ (l ∈ s) (u ∈ s) (_ : l < u), {Ico l u} : Set (Set α)).Countable := hsc.biUnion fun l _ => hsc.biUnion fun u _ => countable_iUnion fun _ => countable_singleton _ simp only [← setOf_eq_eq_singleton, ← setOf_exists] at this refine Measure.ext_of_generateFrom_of_cover_subset (BorelSpace.measurable_eq.trans (borel_eq_generateFrom_Ico α)) (isPiSystem_Ico id id) ?_ this ?_ ?_ ?_ · rintro _ ⟨l, -, u, -, h, rfl⟩ exact ⟨l, u, h, rfl⟩ · refine sUnion_eq_univ_iff.2 fun x => ?_ rcases hsd.exists_le' hsb x with ⟨l, hls, hlx⟩ rcases hsd.exists_gt x with ⟨u, hus, hxu⟩ exact ⟨_, ⟨l, hls, u, hus, hlx.trans_lt hxu, rfl⟩, hlx, hxu⟩ · rintro _ ⟨l, -, u, -, hlt, rfl⟩ exact hμ hlt · rintro _ ⟨l, u, hlt, rfl⟩ exact h hlt /-- Two measures which are finite on closed-open intervals are equal if they agree on all open-closed intervals. -/ theorem ext_of_Ioc' {α : Type*} [TopologicalSpace α] {m : MeasurableSpace α} [SecondCountableTopology α] [LinearOrder α] [OrderTopology α] [BorelSpace α] [NoMinOrder α] (μ ν : Measure α) (hμ : ∀ ⦃a b⦄, a < b → μ (Ioc a b) ≠ ∞) (h : ∀ ⦃a b⦄, a < b → μ (Ioc a b) = ν (Ioc a b)) : μ = ν := by refine @ext_of_Ico' αᵒᵈ _ _ _ _ _ ‹_› _ μ ν ?_ ?_ <;> intro a b hab <;> erw [Ico_toDual (α := α)] exacts [hμ hab, h hab] /-- Two measures which are finite on closed-open intervals are equal if they agree on all closed-open intervals. -/ theorem ext_of_Ico {α : Type*} [TopologicalSpace α] {_m : MeasurableSpace α} [SecondCountableTopology α] [ConditionallyCompleteLinearOrder α] [OrderTopology α] [BorelSpace α] [NoMaxOrder α] (μ ν : Measure α) [IsLocallyFiniteMeasure μ] (h : ∀ ⦃a b⦄, a < b → μ (Ico a b) = ν (Ico a b)) : μ = ν := μ.ext_of_Ico' ν (fun _ _ _ => measure_Ico_lt_top.ne) h /-- Two measures which are finite on closed-open intervals are equal if they agree on all open-closed intervals. -/ theorem ext_of_Ioc {α : Type*} [TopologicalSpace α] {_m : MeasurableSpace α} [SecondCountableTopology α] [ConditionallyCompleteLinearOrder α] [OrderTopology α] [BorelSpace α] [NoMinOrder α] (μ ν : Measure α) [IsLocallyFiniteMeasure μ] (h : ∀ ⦃a b⦄, a < b → μ (Ioc a b) = ν (Ioc a b)) : μ = ν := μ.ext_of_Ioc' ν (fun _ _ _ => measure_Ioc_lt_top.ne) h /-- Two finite measures on a Borel space are equal if they agree on all left-infinite right-closed intervals. -/ theorem ext_of_Iic {α : Type*} [TopologicalSpace α] {m : MeasurableSpace α} [SecondCountableTopology α] [LinearOrder α] [OrderTopology α] [BorelSpace α] (μ ν : Measure α) [IsFiniteMeasure μ] (h : ∀ a, μ (Iic a) = ν (Iic a)) : μ = ν := by refine ext_of_Ioc_finite μ ν ?_ fun a b hlt => ?_ · rcases exists_countable_dense_bot_top α with ⟨s, hsc, hsd, -, hst⟩ have : DirectedOn (· ≤ ·) s := directedOn_iff_directed.2 (Subtype.mono_coe _).directed_le simp only [← biSup_measure_Iic hsc (hsd.exists_ge' hst) this, h] rw [← Iic_diff_Iic, measure_diff (Iic_subset_Iic.2 hlt.le) nullMeasurableSet_Iic, measure_diff (Iic_subset_Iic.2 hlt.le) nullMeasurableSet_Iic, h a, h b] · rw [← h a] exact measure_ne_top μ _ · exact measure_ne_top μ _ /-- Two finite measures on a Borel space are equal if they agree on all left-closed right-infinite intervals. -/ theorem ext_of_Ici {α : Type*} [TopologicalSpace α] {_ : MeasurableSpace α} [SecondCountableTopology α] [LinearOrder α] [OrderTopology α] [BorelSpace α] (μ ν : Measure α) [IsFiniteMeasure μ] (h : ∀ a, μ (Ici a) = ν (Ici a)) : μ = ν := @ext_of_Iic αᵒᵈ _ _ _ _ _ ‹_› _ _ _ h end MeasureTheory.Measure @[measurability] theorem measurableSet_uIcc : MeasurableSet (uIcc a b) := measurableSet_Icc @[measurability] theorem measurableSet_uIoc : MeasurableSet (uIoc a b) := measurableSet_Ioc variable [SecondCountableTopology α] @[measurability, fun_prop] theorem Measurable.max {f g : δ → α} (hf : Measurable f) (hg : Measurable g) : Measurable fun a => max (f a) (g a) := by simpa only [max_def'] using hf.piecewise (measurableSet_le hg hf) hg @[measurability, fun_prop] nonrec theorem AEMeasurable.max {f g : δ → α} {μ : Measure δ} (hf : AEMeasurable f μ) (hg : AEMeasurable g μ) : AEMeasurable (fun a => max (f a) (g a)) μ := ⟨fun a => max (hf.mk f a) (hg.mk g a), hf.measurable_mk.max hg.measurable_mk, EventuallyEq.comp₂ hf.ae_eq_mk _ hg.ae_eq_mk⟩ @[measurability, fun_prop] theorem Measurable.min {f g : δ → α} (hf : Measurable f) (hg : Measurable g) : Measurable fun a => min (f a) (g a) := by simpa only [min_def] using hf.piecewise (measurableSet_le hf hg) hg @[measurability, fun_prop] nonrec theorem AEMeasurable.min {f g : δ → α} {μ : Measure δ} (hf : AEMeasurable f μ) (hg : AEMeasurable g μ) : AEMeasurable (fun a => min (f a) (g a)) μ := ⟨fun a => min (hf.mk f a) (hg.mk g a), hf.measurable_mk.min hg.measurable_mk, EventuallyEq.comp₂ hf.ae_eq_mk _ hg.ae_eq_mk⟩ end LinearOrder section Lattice variable [TopologicalSpace γ] {mγ : MeasurableSpace γ} [BorelSpace γ] instance (priority := 100) ContinuousSup.measurableSup [Max γ] [ContinuousSup γ] : MeasurableSup γ where measurable_const_sup _ := (continuous_const.sup continuous_id).measurable measurable_sup_const _ := (continuous_id.sup continuous_const).measurable instance (priority := 100) ContinuousSup.measurableSup₂ [SecondCountableTopology γ] [Max γ] [ContinuousSup γ] : MeasurableSup₂ γ := ⟨continuous_sup.measurable⟩ instance (priority := 100) ContinuousInf.measurableInf [Min γ] [ContinuousInf γ] : MeasurableInf γ where measurable_const_inf _ := (continuous_const.inf continuous_id).measurable measurable_inf_const _ := (continuous_id.inf continuous_const).measurable instance (priority := 100) ContinuousInf.measurableInf₂ [SecondCountableTopology γ] [Min γ] [ContinuousInf γ] : MeasurableInf₂ γ := ⟨continuous_inf.measurable⟩ end Lattice end Orders section BorelSpace variable [TopologicalSpace α] {mα : MeasurableSpace α} [BorelSpace α] variable [TopologicalSpace β] {mβ : MeasurableSpace β} [BorelSpace β] variable {mδ : MeasurableSpace δ} section LinearOrder variable [LinearOrder α] [OrderTopology α] [SecondCountableTopology α] theorem measurable_of_Iio {f : δ → α} (hf : ∀ x, MeasurableSet (f ⁻¹' Iio x)) : Measurable f := by convert measurable_generateFrom (α := δ) _ · exact BorelSpace.measurable_eq.trans (borel_eq_generateFrom_Iio _) · rintro _ ⟨x, rfl⟩; exact hf x theorem UpperSemicontinuous.measurable [TopologicalSpace δ] [OpensMeasurableSpace δ] {f : δ → α} (hf : UpperSemicontinuous f) : Measurable f := measurable_of_Iio fun y => (hf.isOpen_preimage y).measurableSet theorem measurable_of_Ioi {f : δ → α} (hf : ∀ x, MeasurableSet (f ⁻¹' Ioi x)) : Measurable f := by convert measurable_generateFrom (α := δ) _ · exact BorelSpace.measurable_eq.trans (borel_eq_generateFrom_Ioi _) · rintro _ ⟨x, rfl⟩; exact hf x theorem LowerSemicontinuous.measurable [TopologicalSpace δ] [OpensMeasurableSpace δ] {f : δ → α} (hf : LowerSemicontinuous f) : Measurable f := measurable_of_Ioi fun y => (hf.isOpen_preimage y).measurableSet theorem measurable_of_Iic {f : δ → α} (hf : ∀ x, MeasurableSet (f ⁻¹' Iic x)) : Measurable f := by apply measurable_of_Ioi simp_rw [← compl_Iic, preimage_compl, MeasurableSet.compl_iff] assumption theorem measurable_of_Ici {f : δ → α} (hf : ∀ x, MeasurableSet (f ⁻¹' Ici x)) : Measurable f := by apply measurable_of_Iio simp_rw [← compl_Ici, preimage_compl, MeasurableSet.compl_iff] assumption /-- If a function is the least upper bound of countably many measurable functions, then it is measurable. -/ theorem Measurable.isLUB {ι} [Countable ι] {f : ι → δ → α} {g : δ → α} (hf : ∀ i, Measurable (f i)) (hg : ∀ b, IsLUB { a | ∃ i, f i b = a } (g b)) : Measurable g := by change ∀ b, IsLUB (range fun i => f i b) (g b) at hg rw [‹BorelSpace α›.measurable_eq, borel_eq_generateFrom_Ioi α] apply measurable_generateFrom rintro _ ⟨a, rfl⟩ simp_rw [Set.preimage, mem_Ioi, lt_isLUB_iff (hg _), exists_range_iff, setOf_exists] exact MeasurableSet.iUnion fun i => hf i (isOpen_lt' _).measurableSet /-- If a function is the least upper bound of countably many measurable functions on a measurable set `s`, and coincides with a measurable function outside of `s`, then it is measurable. -/ theorem Measurable.isLUB_of_mem {ι} [Countable ι] {f : ι → δ → α} {g g' : δ → α} (hf : ∀ i, Measurable (f i)) {s : Set δ} (hs : MeasurableSet s) (hg : ∀ b ∈ s, IsLUB { a | ∃ i, f i b = a } (g b)) (hg' : EqOn g g' sᶜ) (g'_meas : Measurable g') : Measurable g := by classical rcases isEmpty_or_nonempty ι with hι|⟨⟨i⟩⟩ · rcases eq_empty_or_nonempty s with rfl|⟨x, hx⟩ · convert g'_meas rwa [compl_empty, eqOn_univ] at hg' · have A : ∀ b ∈ s, IsBot (g b) := by simpa using hg have B : ∀ b ∈ s, g b = g x := by intro b hb apply le_antisymm (A b hb (g x)) (A x hx (g b)) have : g = s.piecewise (fun _y ↦ g x) g' := by ext b by_cases hb : b ∈ s · simp [hb, B]
· simp [hb, hg' hb] rw [this] exact Measurable.piecewise hs measurable_const g'_meas · have : Nonempty ι := ⟨i⟩
Mathlib/MeasureTheory/Constructions/BorelSpace/Order.lean
568
571
/- Copyright (c) 2022 Yaël Dillies, Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies, Bhavik Mehta -/ import Mathlib.Algebra.BigOperators.Ring.Finset import Mathlib.Combinatorics.SimpleGraph.Density import Mathlib.Data.Nat.Cast.Order.Field import Mathlib.Order.Partition.Equipartition import Mathlib.SetTheory.Cardinal.Order /-! # Graph uniformity and uniform partitions In this file we define uniformity of a pair of vertices in a graph and uniformity of a partition of vertices of a graph. Both are also known as ε-regularity. Finsets of vertices `s` and `t` are `ε`-uniform in a graph `G` if their edge density is at most `ε`-far from the density of any big enough `s'` and `t'` where `s' ⊆ s`, `t' ⊆ t`. The definition is pretty technical, but it amounts to the edges between `s` and `t` being "random" The literature contains several definitions which are equivalent up to scaling `ε` by some constant when the partition is equitable. A partition `P` of the vertices is `ε`-uniform if the proportion of non `ε`-uniform pairs of parts is less than `ε`. ## Main declarations * `SimpleGraph.IsUniform`: Graph uniformity of a pair of finsets of vertices. * `SimpleGraph.nonuniformWitness`: `G.nonuniformWitness ε s t` and `G.nonuniformWitness ε t s` together witness the non-uniformity of `s` and `t`. * `Finpartition.nonUniforms`: Non uniform pairs of parts of a partition. * `Finpartition.IsUniform`: Uniformity of a partition. * `Finpartition.nonuniformWitnesses`: For each non-uniform pair of parts of a partition, pick witnesses of non-uniformity and dump them all together. ## References [Yaël Dillies, Bhavik Mehta, *Formalising Szemerédi’s Regularity Lemma in Lean*][srl_itp] -/ open Finset variable {α 𝕜 : Type*} [Field 𝕜] [LinearOrder 𝕜] [IsStrictOrderedRing 𝕜] /-! ### Graph uniformity -/ namespace SimpleGraph variable (G : SimpleGraph α) [DecidableRel G.Adj] (ε : 𝕜) {s t : Finset α} {a b : α} /-- A pair of finsets of vertices is `ε`-uniform (aka `ε`-regular) iff their edge density is close to the density of any big enough pair of subsets. Intuitively, the edges between them are random-like. -/ def IsUniform (s t : Finset α) : Prop := ∀ ⦃s'⦄, s' ⊆ s → ∀ ⦃t'⦄, t' ⊆ t → (#s : 𝕜) * ε ≤ #s' → (#t : 𝕜) * ε ≤ #t' → |(G.edgeDensity s' t' : 𝕜) - (G.edgeDensity s t : 𝕜)| < ε variable {G ε} instance IsUniform.instDecidableRel : DecidableRel (G.IsUniform ε) := by unfold IsUniform; infer_instance theorem IsUniform.mono {ε' : 𝕜} (h : ε ≤ ε') (hε : IsUniform G ε s t) : IsUniform G ε' s t := fun s' hs' t' ht' hs ht => by refine (hε hs' ht' (le_trans ?_ hs) (le_trans ?_ ht)).trans_le h <;> gcongr omit [IsStrictOrderedRing 𝕜] in theorem IsUniform.symm : Symmetric (IsUniform G ε) := fun s t h t' ht' s' hs' ht hs => by rw [edgeDensity_comm _ t', edgeDensity_comm _ t] exact h hs' ht' hs ht variable (G) omit [IsStrictOrderedRing 𝕜] in theorem isUniform_comm : IsUniform G ε s t ↔ IsUniform G ε t s := ⟨fun h => h.symm, fun h => h.symm⟩ lemma isUniform_one : G.IsUniform (1 : 𝕜) s t := by intro s' hs' t' ht' hs ht rw [mul_one] at hs ht rw [eq_of_subset_of_card_le hs' (Nat.cast_le.1 hs), eq_of_subset_of_card_le ht' (Nat.cast_le.1 ht), sub_self, abs_zero] exact zero_lt_one variable {G} lemma IsUniform.pos (hG : G.IsUniform ε s t) : 0 < ε := not_le.1 fun hε ↦ (hε.trans <| abs_nonneg _).not_lt <| hG (empty_subset _) (empty_subset _) (by simpa using mul_nonpos_of_nonneg_of_nonpos (Nat.cast_nonneg _) hε) (by simpa using mul_nonpos_of_nonneg_of_nonpos (Nat.cast_nonneg _) hε) @[simp] lemma isUniform_singleton : G.IsUniform ε {a} {b} ↔ 0 < ε := by refine ⟨IsUniform.pos, fun hε s' hs' t' ht' hs ht ↦ ?_⟩ rw [card_singleton, Nat.cast_one, one_mul] at hs ht obtain rfl | rfl := Finset.subset_singleton_iff.1 hs' · replace hs : ε ≤ 0 := by simpa using hs exact (hε.not_le hs).elim obtain rfl | rfl := Finset.subset_singleton_iff.1 ht' · replace ht : ε ≤ 0 := by simpa using ht exact (hε.not_le ht).elim · rwa [sub_self, abs_zero] theorem not_isUniform_zero : ¬G.IsUniform (0 : 𝕜) s t := fun h => (abs_nonneg _).not_lt <| h (empty_subset _) (empty_subset _) (by simp) (by simp) theorem not_isUniform_iff : ¬G.IsUniform ε s t ↔ ∃ s', s' ⊆ s ∧ ∃ t', t' ⊆ t ∧ #s * ε ≤ #s' ∧ #t * ε ≤ #t' ∧ ε ≤ |G.edgeDensity s' t' - G.edgeDensity s t| := by unfold IsUniform simp only [not_forall, not_lt, exists_prop, exists_and_left, Rat.cast_abs, Rat.cast_sub] variable (G) /-- An arbitrary pair of subsets witnessing the non-uniformity of `(s, t)`. If `(s, t)` is uniform, returns `(s, t)`. Witnesses for `(s, t)` and `(t, s)` don't necessarily match. See `SimpleGraph.nonuniformWitness`. -/ noncomputable def nonuniformWitnesses (ε : 𝕜) (s t : Finset α) : Finset α × Finset α := if h : ¬G.IsUniform ε s t then ((not_isUniform_iff.1 h).choose, (not_isUniform_iff.1 h).choose_spec.2.choose) else (s, t) theorem left_nonuniformWitnesses_subset (h : ¬G.IsUniform ε s t) : (G.nonuniformWitnesses ε s t).1 ⊆ s := by rw [nonuniformWitnesses, dif_pos h] exact (not_isUniform_iff.1 h).choose_spec.1 theorem left_nonuniformWitnesses_card (h : ¬G.IsUniform ε s t) : #s * ε ≤ #(G.nonuniformWitnesses ε s t).1 := by rw [nonuniformWitnesses, dif_pos h] exact (not_isUniform_iff.1 h).choose_spec.2.choose_spec.2.1 theorem right_nonuniformWitnesses_subset (h : ¬G.IsUniform ε s t) : (G.nonuniformWitnesses ε s t).2 ⊆ t := by rw [nonuniformWitnesses, dif_pos h] exact (not_isUniform_iff.1 h).choose_spec.2.choose_spec.1 theorem right_nonuniformWitnesses_card (h : ¬G.IsUniform ε s t) : #t * ε ≤ #(G.nonuniformWitnesses ε s t).2 := by rw [nonuniformWitnesses, dif_pos h] exact (not_isUniform_iff.1 h).choose_spec.2.choose_spec.2.2.1 theorem nonuniformWitnesses_spec (h : ¬G.IsUniform ε s t) : ε ≤ |G.edgeDensity (G.nonuniformWitnesses ε s t).1 (G.nonuniformWitnesses ε s t).2 - G.edgeDensity s t| := by rw [nonuniformWitnesses, dif_pos h] exact (not_isUniform_iff.1 h).choose_spec.2.choose_spec.2.2.2 open scoped Classical in /-- Arbitrary witness of non-uniformity. `G.nonuniformWitness ε s t` and `G.nonuniformWitness ε t s` form a pair of subsets witnessing the non-uniformity of `(s, t)`. If `(s, t)` is uniform, returns `s`. -/ noncomputable def nonuniformWitness (ε : 𝕜) (s t : Finset α) : Finset α := if WellOrderingRel s t then (G.nonuniformWitnesses ε s t).1 else (G.nonuniformWitnesses ε t s).2 theorem nonuniformWitness_subset (h : ¬G.IsUniform ε s t) : G.nonuniformWitness ε s t ⊆ s := by unfold nonuniformWitness split_ifs · exact G.left_nonuniformWitnesses_subset h · exact G.right_nonuniformWitnesses_subset fun i => h i.symm theorem le_card_nonuniformWitness (h : ¬G.IsUniform ε s t) : #s * ε ≤ #(G.nonuniformWitness ε s t) := by unfold nonuniformWitness split_ifs · exact G.left_nonuniformWitnesses_card h · exact G.right_nonuniformWitnesses_card fun i => h i.symm theorem nonuniformWitness_spec (h₁ : s ≠ t) (h₂ : ¬G.IsUniform ε s t) : ε ≤ |G.edgeDensity (G.nonuniformWitness ε s t) (G.nonuniformWitness ε t s) - G.edgeDensity s t| := by unfold nonuniformWitness rcases trichotomous_of WellOrderingRel s t with (lt | rfl | gt) · rw [if_pos lt, if_neg (asymm lt)] exact G.nonuniformWitnesses_spec h₂ · cases h₁ rfl · rw [if_neg (asymm gt), if_pos gt, edgeDensity_comm, edgeDensity_comm _ s] apply G.nonuniformWitnesses_spec fun i => h₂ i.symm end SimpleGraph /-! ### Uniform partitions -/ variable [DecidableEq α] {A : Finset α} (P : Finpartition A) (G : SimpleGraph α) [DecidableRel G.Adj] {ε δ : 𝕜} {u v : Finset α} namespace Finpartition /-- The pairs of parts of a partition `P` which are not `ε`-dense in a graph `G`. Note that we dismiss the diagonal. We do not care whether `s` is `ε`-dense with itself. -/ def sparsePairs (ε : 𝕜) : Finset (Finset α × Finset α) := P.parts.offDiag.filter fun (u, v) ↦ G.edgeDensity u v < ε omit [IsStrictOrderedRing 𝕜] in @[simp] lemma mk_mem_sparsePairs (u v : Finset α) (ε : 𝕜) : (u, v) ∈ P.sparsePairs G ε ↔ u ∈ P.parts ∧ v ∈ P.parts ∧ u ≠ v ∧ G.edgeDensity u v < ε := by rw [sparsePairs, mem_filter, mem_offDiag, and_assoc, and_assoc] omit [IsStrictOrderedRing 𝕜] in lemma sparsePairs_mono {ε ε' : 𝕜} (h : ε ≤ ε') : P.sparsePairs G ε ⊆ P.sparsePairs G ε' := monotone_filter_right _ fun _ ↦ h.trans_lt' /-- The pairs of parts of a partition `P` which are not `ε`-uniform in a graph `G`. Note that we dismiss the diagonal. We do not care whether `s` is `ε`-uniform with itself. -/ def nonUniforms (ε : 𝕜) : Finset (Finset α × Finset α) := P.parts.offDiag.filter fun (u, v) ↦ ¬G.IsUniform ε u v omit [IsStrictOrderedRing 𝕜] in @[simp] lemma mk_mem_nonUniforms : (u, v) ∈ P.nonUniforms G ε ↔ u ∈ P.parts ∧ v ∈ P.parts ∧ u ≠ v ∧ ¬G.IsUniform ε u v := by rw [nonUniforms, mem_filter, mem_offDiag, and_assoc, and_assoc] theorem nonUniforms_mono {ε ε' : 𝕜} (h : ε ≤ ε') : P.nonUniforms G ε' ⊆ P.nonUniforms G ε := monotone_filter_right _ fun _ => mt <| SimpleGraph.IsUniform.mono h theorem nonUniforms_bot (hε : 0 < ε) : (⊥ : Finpartition A).nonUniforms G ε = ∅ := by rw [eq_empty_iff_forall_not_mem] rintro ⟨u, v⟩ simp only [mk_mem_nonUniforms, parts_bot, mem_map, not_and, Classical.not_not, exists_imp]; dsimp rintro x ⟨_, rfl⟩ y ⟨_,rfl⟩ _ rwa [SimpleGraph.isUniform_singleton] /-- A finpartition of a graph's vertex set is `ε`-uniform (aka `ε`-regular) iff the proportion of its pairs of parts that are not `ε`-uniform is at most `ε`. -/ def IsUniform (ε : 𝕜) : Prop := (#(P.nonUniforms G ε) : 𝕜) ≤ (#P.parts * (#P.parts - 1) : ℕ) * ε lemma bot_isUniform (hε : 0 < ε) : (⊥ : Finpartition A).IsUniform G ε := by rw [Finpartition.IsUniform, Finpartition.card_bot, nonUniforms_bot _ hε, Finset.card_empty, Nat.cast_zero] exact mul_nonneg (Nat.cast_nonneg _) hε.le lemma isUniform_one : P.IsUniform G (1 : 𝕜) := by rw [IsUniform, mul_one, Nat.cast_le] refine (card_filter_le _ (fun uv => ¬SimpleGraph.IsUniform G 1 (Prod.fst uv) (Prod.snd uv))).trans ?_ rw [offDiag_card, Nat.mul_sub_left_distrib, mul_one] variable {P G} theorem IsUniform.mono {ε ε' : 𝕜} (hP : P.IsUniform G ε) (h : ε ≤ ε') : P.IsUniform G ε' := ((Nat.cast_le.2 <| card_le_card <| P.nonUniforms_mono G h).trans hP).trans <| by gcongr omit [IsStrictOrderedRing 𝕜] in theorem isUniformOfEmpty (hP : P.parts = ∅) : P.IsUniform G ε := by simp [IsUniform, hP, nonUniforms] omit [IsStrictOrderedRing 𝕜] in theorem nonempty_of_not_uniform (h : ¬P.IsUniform G ε) : P.parts.Nonempty := nonempty_of_ne_empty fun h₁ => h <| isUniformOfEmpty h₁ variable (P G ε) (s : Finset α) /-- A choice of witnesses of non-uniformity among the parts of a finpartition. -/
noncomputable def nonuniformWitnesses : Finset (Finset α) := {t ∈ P.parts | s ≠ t ∧ ¬G.IsUniform ε s t}.image (G.nonuniformWitness ε s) variable {P G ε s} {t : Finset α}
Mathlib/Combinatorics/SimpleGraph/Regularity/Uniform.lean
260
264
/- Copyright (c) 2022 Xavier Roblot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Xavier Roblot -/ import Mathlib.Algebra.Module.ZLattice.Basic import Mathlib.Analysis.InnerProductSpace.ProdL2 import Mathlib.MeasureTheory.Measure.Haar.Unique import Mathlib.NumberTheory.NumberField.FractionalIdeal import Mathlib.NumberTheory.NumberField.Units.Basic /-! # Canonical embedding of a number field The canonical embedding of a number field `K` of degree `n` is the ring homomorphism `K →+* ℂ^n` that sends `x ∈ K` to `(φ_₁(x),...,φ_n(x))` where the `φ_i`'s are the complex embeddings of `K`. Note that we do not choose an ordering of the embeddings, but instead map `K` into the type `(K →+* ℂ) → ℂ` of `ℂ`-vectors indexed by the complex embeddings. ## Main definitions and results * `NumberField.canonicalEmbedding`: the ring homomorphism `K →+* ((K →+* ℂ) → ℂ)` defined by sending `x : K` to the vector `(φ x)` indexed by `φ : K →+* ℂ`. * `NumberField.canonicalEmbedding.integerLattice.inter_ball_finite`: the intersection of the image of the ring of integers by the canonical embedding and any ball centered at `0` of finite radius is finite. * `NumberField.mixedEmbedding`: the ring homomorphism from `K` to the mixed space `K →+* ({ w // IsReal w } → ℝ) × ({ w // IsComplex w } → ℂ)` that sends `x ∈ K` to `(φ_w x)_w` where `φ_w` is the embedding associated to the infinite place `w`. In particular, if `w` is real then `φ_w : K →+* ℝ` and, if `w` is complex, `φ_w` is an arbitrary choice between the two complex embeddings defining the place `w`. ## Tags number field, infinite places -/ variable (K : Type*) [Field K] namespace NumberField.canonicalEmbedding /-- The canonical embedding of a number field `K` of degree `n` into `ℂ^n`. -/ def _root_.NumberField.canonicalEmbedding : K →+* ((K →+* ℂ) → ℂ) := Pi.ringHom fun φ => φ theorem _root_.NumberField.canonicalEmbedding_injective [NumberField K] : Function.Injective (NumberField.canonicalEmbedding K) := RingHom.injective _ variable {K} @[simp] theorem apply_at (φ : K →+* ℂ) (x : K) : (NumberField.canonicalEmbedding K x) φ = φ x := rfl open scoped ComplexConjugate /-- The image of `canonicalEmbedding` lives in the `ℝ`-submodule of the `x ∈ ((K →+* ℂ) → ℂ)` such that `conj x_φ = x_(conj φ)` for all `∀ φ : K →+* ℂ`. -/ theorem conj_apply {x : ((K →+* ℂ) → ℂ)} (φ : K →+* ℂ) (hx : x ∈ Submodule.span ℝ (Set.range (canonicalEmbedding K))) : conj (x φ) = x (ComplexEmbedding.conjugate φ) := by refine Submodule.span_induction ?_ ?_ (fun _ _ _ _ hx hy => ?_) (fun a _ _ hx => ?_) hx · rintro _ ⟨x, rfl⟩ rw [apply_at, apply_at, ComplexEmbedding.conjugate_coe_eq] · rw [Pi.zero_apply, Pi.zero_apply, map_zero] · rw [Pi.add_apply, Pi.add_apply, map_add, hx, hy] · rw [Pi.smul_apply, Complex.real_smul, map_mul, Complex.conj_ofReal] exact congrArg ((a : ℂ) * ·) hx theorem nnnorm_eq [NumberField K] (x : K) : ‖canonicalEmbedding K x‖₊ = Finset.univ.sup (fun φ : K →+* ℂ => ‖φ x‖₊) := by simp_rw [Pi.nnnorm_def, apply_at] theorem norm_le_iff [NumberField K] (x : K) (r : ℝ) : ‖canonicalEmbedding K x‖ ≤ r ↔ ∀ φ : K →+* ℂ, ‖φ x‖ ≤ r := by obtain hr | hr := lt_or_le r 0 · obtain ⟨φ⟩ := (inferInstance : Nonempty (K →+* ℂ)) refine iff_of_false ?_ ?_ · exact (hr.trans_le (norm_nonneg _)).not_le · exact fun h => hr.not_le (le_trans (norm_nonneg _) (h φ)) · lift r to NNReal using hr simp_rw [← coe_nnnorm, nnnorm_eq, NNReal.coe_le_coe, Finset.sup_le_iff, Finset.mem_univ, forall_true_left] variable (K) /-- The image of `𝓞 K` as a subring of `ℂ^n`. -/ def integerLattice : Subring ((K →+* ℂ) → ℂ) := (RingHom.range (algebraMap (𝓞 K) K)).map (canonicalEmbedding K) theorem integerLattice.inter_ball_finite [NumberField K] (r : ℝ) : ((integerLattice K : Set ((K →+* ℂ) → ℂ)) ∩ Metric.closedBall 0 r).Finite := by obtain hr | _ := lt_or_le r 0 · simp [Metric.closedBall_eq_empty.2 hr] · have heq : ∀ x, canonicalEmbedding K x ∈ Metric.closedBall 0 r ↔ ∀ φ : K →+* ℂ, ‖φ x‖ ≤ r := by intro x; rw [← norm_le_iff, mem_closedBall_zero_iff] convert (Embeddings.finite_of_norm_le K ℂ r).image (canonicalEmbedding K) ext; constructor · rintro ⟨⟨_, ⟨x, rfl⟩, rfl⟩, hx⟩ exact ⟨x, ⟨SetLike.coe_mem x, fun φ => (heq _).mp hx φ⟩, rfl⟩ · rintro ⟨x, ⟨hx1, hx2⟩, rfl⟩ exact ⟨⟨x, ⟨⟨x, hx1⟩, rfl⟩, rfl⟩, (heq x).mpr hx2⟩ open Module Fintype Module /-- A `ℂ`-basis of `ℂ^n` that is also a `ℤ`-basis of the `integerLattice`. -/ noncomputable def latticeBasis [NumberField K] : Basis (Free.ChooseBasisIndex ℤ (𝓞 K)) ℂ ((K →+* ℂ) → ℂ) := by classical -- Let `B` be the canonical basis of `(K →+* ℂ) → ℂ`. We prove that the determinant of -- the image by `canonicalEmbedding` of the integral basis of `K` is nonzero. This -- will imply the result. let B := Pi.basisFun ℂ (K →+* ℂ) let e : (K →+* ℂ) ≃ Free.ChooseBasisIndex ℤ (𝓞 K) := equivOfCardEq ((Embeddings.card K ℂ).trans (finrank_eq_card_basis (integralBasis K))) let M := B.toMatrix (fun i => canonicalEmbedding K (integralBasis K (e i))) suffices M.det ≠ 0 by rw [← isUnit_iff_ne_zero, ← Basis.det_apply, ← is_basis_iff_det] at this exact (basisOfPiSpaceOfLinearIndependent this.1).reindex e -- In order to prove that the determinant is nonzero, we show that it is equal to the -- square of the discriminant of the integral basis and thus it is not zero let N := Algebra.embeddingsMatrixReindex ℚ ℂ (fun i => integralBasis K (e i)) RingHom.equivRatAlgHom rw [show M = N.transpose by { ext : 2; rfl }] rw [Matrix.det_transpose, ← pow_ne_zero_iff two_ne_zero] convert (map_ne_zero_iff _ (algebraMap ℚ ℂ).injective).mpr (Algebra.discr_not_zero_of_basis ℚ (integralBasis K)) rw [← Algebra.discr_reindex ℚ (integralBasis K) e.symm] exact (Algebra.discr_eq_det_embeddingsMatrixReindex_pow_two ℚ ℂ (fun i => integralBasis K (e i)) RingHom.equivRatAlgHom).symm @[simp] theorem latticeBasis_apply [NumberField K] (i : Free.ChooseBasisIndex ℤ (𝓞 K)) : latticeBasis K i = (canonicalEmbedding K) (integralBasis K i) := by simp [latticeBasis, integralBasis_apply, coe_basisOfPiSpaceOfLinearIndependent, Function.comp_apply, Equiv.apply_symm_apply] theorem mem_span_latticeBasis [NumberField K] {x : (K →+* ℂ) → ℂ} : x ∈ Submodule.span ℤ (Set.range (latticeBasis K)) ↔ x ∈ ((canonicalEmbedding K).comp (algebraMap (𝓞 K) K)).range := by rw [show Set.range (latticeBasis K) = (canonicalEmbedding K).toIntAlgHom.toLinearMap '' (Set.range (integralBasis K)) by rw [← Set.range_comp]; exact congrArg Set.range (funext (fun i => latticeBasis_apply K i))] rw [← Submodule.map_span, ← SetLike.mem_coe, Submodule.map_coe] rw [← RingHom.map_range, Subring.mem_map, Set.mem_image] simp only [SetLike.mem_coe, mem_span_integralBasis K] rfl theorem mem_rat_span_latticeBasis [NumberField K] (x : K) : canonicalEmbedding K x ∈ Submodule.span ℚ (Set.range (latticeBasis K)) := by rw [← Basis.sum_repr (integralBasis K) x, map_sum] simp_rw [map_rat_smul] refine Submodule.sum_smul_mem _ _ (fun i _ ↦ Submodule.subset_span ?_) rw [← latticeBasis_apply] exact Set.mem_range_self i theorem integralBasis_repr_apply [NumberField K] (x : K) (i : Free.ChooseBasisIndex ℤ (𝓞 K)) : (latticeBasis K).repr (canonicalEmbedding K x) i = (integralBasis K).repr x i := by rw [← Basis.restrictScalars_repr_apply ℚ _ ⟨_, mem_rat_span_latticeBasis K x⟩, eq_ratCast, Rat.cast_inj] let f := (canonicalEmbedding K).toRatAlgHom.toLinearMap.codRestrict _ (fun x ↦ mem_rat_span_latticeBasis K x) suffices ((latticeBasis K).restrictScalars ℚ).repr.toLinearMap ∘ₗ f = (integralBasis K).repr.toLinearMap from DFunLike.congr_fun (LinearMap.congr_fun this x) i refine Basis.ext (integralBasis K) (fun i ↦ ?_) have : f (integralBasis K i) = ((latticeBasis K).restrictScalars ℚ) i := by apply Subtype.val_injective rw [LinearMap.codRestrict_apply, AlgHom.toLinearMap_apply, Basis.restrictScalars_apply, latticeBasis_apply] rfl simp_rw [LinearMap.coe_comp, LinearEquiv.coe_coe, Function.comp_apply, this, Basis.repr_self] end NumberField.canonicalEmbedding namespace NumberField.mixedEmbedding open NumberField.InfinitePlace Module Finset /-- The mixed space `ℝ^r₁ × ℂ^r₂` with `(r₁, r₂)` the signature of `K`. -/ abbrev mixedSpace := ({w : InfinitePlace K // IsReal w} → ℝ) × ({w : InfinitePlace K // IsComplex w} → ℂ) /-- The mixed embedding of a number field `K` into the mixed space of `K`. -/ noncomputable def _root_.NumberField.mixedEmbedding : K →+* (mixedSpace K) := RingHom.prod (Pi.ringHom fun w => embedding_of_isReal w.prop) (Pi.ringHom fun w => w.val.embedding) @[simp] theorem mixedEmbedding_apply_isReal (x : K) (w : {w // IsReal w}) : (mixedEmbedding K x).1 w = embedding_of_isReal w.prop x := by simp_rw [mixedEmbedding, RingHom.prod_apply, Pi.ringHom_apply] @[simp] theorem mixedEmbedding_apply_isComplex (x : K) (w : {w // IsComplex w}) : (mixedEmbedding K x).2 w = w.val.embedding x := by simp_rw [mixedEmbedding, RingHom.prod_apply, Pi.ringHom_apply] @[deprecated (since := "2025-02-28")] alias mixedEmbedding_apply_ofIsReal := mixedEmbedding_apply_isReal @[deprecated (since := "2025-02-28")] alias mixedEmbedding_apply_ofIsComplex := mixedEmbedding_apply_isComplex instance [NumberField K] : Nontrivial (mixedSpace K) := by obtain ⟨w⟩ := (inferInstance : Nonempty (InfinitePlace K)) obtain hw | hw := w.isReal_or_isComplex · have : Nonempty {w : InfinitePlace K // IsReal w} := ⟨⟨w, hw⟩⟩ exact nontrivial_prod_left · have : Nonempty {w : InfinitePlace K // IsComplex w} := ⟨⟨w, hw⟩⟩ exact nontrivial_prod_right protected theorem finrank [NumberField K] : finrank ℝ (mixedSpace K) = finrank ℚ K := by classical rw [finrank_prod, finrank_pi, finrank_pi_fintype, Complex.finrank_real_complex, sum_const, card_univ, ← nrRealPlaces, ← nrComplexPlaces, ← card_real_embeddings, Algebra.id.smul_eq_mul, mul_comm, ← card_complex_embeddings, ← NumberField.Embeddings.card K ℂ, Fintype.card_subtype_compl, Nat.add_sub_of_le (Fintype.card_subtype_le _)] theorem _root_.NumberField.mixedEmbedding_injective [NumberField K] : Function.Injective (NumberField.mixedEmbedding K) := by exact RingHom.injective _ section Measure open MeasureTheory.Measure MeasureTheory variable [NumberField K] open Classical in instance : IsAddHaarMeasure (volume : Measure (mixedSpace K)) := prod.instIsAddHaarMeasure volume volume open Classical in instance : NoAtoms (volume : Measure (mixedSpace K)) := by obtain ⟨w⟩ := (inferInstance : Nonempty (InfinitePlace K)) by_cases hw : IsReal w · have : NoAtoms (volume : Measure ({w : InfinitePlace K // IsReal w} → ℝ)) := pi_noAtoms ⟨w, hw⟩ exact prod.instNoAtoms_fst · have : NoAtoms (volume : Measure ({w : InfinitePlace K // IsComplex w} → ℂ)) := pi_noAtoms ⟨w, not_isReal_iff_isComplex.mp hw⟩ exact prod.instNoAtoms_snd variable {K} in open Classical in /-- The set of points in the mixedSpace that are equal to `0` at a fixed (real) place has volume zero. -/ theorem volume_eq_zero (w : {w // IsReal w}) : volume ({x : mixedSpace K | x.1 w = 0}) = 0 := by let A : AffineSubspace ℝ (mixedSpace K) := Submodule.toAffineSubspace (Submodule.mk ⟨⟨{x | x.1 w = 0}, by aesop⟩, rfl⟩ (by aesop)) convert Measure.addHaar_affineSubspace volume A fun h ↦ ?_ simpa [A] using (h ▸ Set.mem_univ _ : 1 ∈ A) end Measure section commMap /-- The linear map that makes `canonicalEmbedding` and `mixedEmbedding` commute, see `commMap_canonical_eq_mixed`. -/ noncomputable def commMap : ((K →+* ℂ) → ℂ) →ₗ[ℝ] (mixedSpace K) where toFun := fun x => ⟨fun w => (x w.val.embedding).re, fun w => x w.val.embedding⟩ map_add' := by simp only [Pi.add_apply, Complex.add_re, Prod.mk_add_mk, Prod.mk.injEq] exact fun _ _ => ⟨rfl, rfl⟩ map_smul' := by simp only [Pi.smul_apply, Complex.real_smul, Complex.mul_re, Complex.ofReal_re, Complex.ofReal_im, zero_mul, sub_zero, RingHom.id_apply, Prod.smul_mk, Prod.mk.injEq] exact fun _ _ => ⟨rfl, rfl⟩ theorem commMap_apply_of_isReal (x : (K →+* ℂ) → ℂ) {w : InfinitePlace K} (hw : IsReal w) : (commMap K x).1 ⟨w, hw⟩ = (x w.embedding).re := rfl theorem commMap_apply_of_isComplex (x : (K →+* ℂ) → ℂ) {w : InfinitePlace K} (hw : IsComplex w) : (commMap K x).2 ⟨w, hw⟩ = x w.embedding := rfl @[simp] theorem commMap_canonical_eq_mixed (x : K) : commMap K (canonicalEmbedding K x) = mixedEmbedding K x := by simp only [canonicalEmbedding, commMap, LinearMap.coe_mk, AddHom.coe_mk, Pi.ringHom_apply, mixedEmbedding, RingHom.prod_apply, Prod.mk.injEq] exact ⟨rfl, rfl⟩ /-- This is a technical result to ensure that the image of the `ℂ`-basis of `ℂ^n` defined in `canonicalEmbedding.latticeBasis` is a `ℝ`-basis of the mixed space `ℝ^r₁ × ℂ^r₂`, see `mixedEmbedding.latticeBasis`. -/ theorem disjoint_span_commMap_ker [NumberField K] : Disjoint (Submodule.span ℝ (Set.range (canonicalEmbedding.latticeBasis K))) (LinearMap.ker (commMap K)) := by refine LinearMap.disjoint_ker.mpr (fun x h_mem h_zero => ?_) replace h_mem : x ∈ Submodule.span ℝ (Set.range (canonicalEmbedding K)) := by refine (Submodule.span_mono ?_) h_mem rintro _ ⟨i, rfl⟩ exact ⟨integralBasis K i, (canonicalEmbedding.latticeBasis_apply K i).symm⟩ ext1 φ rw [Pi.zero_apply] by_cases hφ : ComplexEmbedding.IsReal φ · apply Complex.ext · rw [← embedding_mk_eq_of_isReal hφ, ← commMap_apply_of_isReal K x ⟨φ, hφ, rfl⟩] exact congrFun (congrArg (fun x => x.1) h_zero) ⟨InfinitePlace.mk φ, _⟩ · rw [Complex.zero_im, ← Complex.conj_eq_iff_im, canonicalEmbedding.conj_apply _ h_mem, ComplexEmbedding.isReal_iff.mp hφ] · have := congrFun (congrArg (fun x => x.2) h_zero) ⟨InfinitePlace.mk φ, ⟨φ, hφ, rfl⟩⟩ cases embedding_mk_eq φ with | inl h => rwa [← h, ← commMap_apply_of_isComplex K x ⟨φ, hφ, rfl⟩] | inr h => apply RingHom.injective (starRingEnd ℂ) rwa [canonicalEmbedding.conj_apply _ h_mem, ← h, map_zero, ← commMap_apply_of_isComplex K x ⟨φ, hφ, rfl⟩] end commMap noncomputable section norm variable {K} open scoped Classical in /-- The norm at the infinite place `w` of an element of the mixed space -/ def normAtPlace (w : InfinitePlace K) : (mixedSpace K) →*₀ ℝ where toFun x := if hw : IsReal w then ‖x.1 ⟨w, hw⟩‖ else ‖x.2 ⟨w, not_isReal_iff_isComplex.mp hw⟩‖ map_zero' := by simp map_one' := by simp map_mul' x y := by split_ifs <;> simp theorem normAtPlace_nonneg (w : InfinitePlace K) (x : mixedSpace K) : 0 ≤ normAtPlace w x := by rw [normAtPlace, MonoidWithZeroHom.coe_mk, ZeroHom.coe_mk] split_ifs <;> exact norm_nonneg _ theorem normAtPlace_neg (w : InfinitePlace K) (x : mixedSpace K) : normAtPlace w (- x) = normAtPlace w x := by rw [normAtPlace, MonoidWithZeroHom.coe_mk, ZeroHom.coe_mk] split_ifs <;> simp theorem normAtPlace_add_le (w : InfinitePlace K) (x y : mixedSpace K) : normAtPlace w (x + y) ≤ normAtPlace w x + normAtPlace w y := by rw [normAtPlace, MonoidWithZeroHom.coe_mk, ZeroHom.coe_mk] split_ifs <;> exact norm_add_le _ _ theorem normAtPlace_smul (w : InfinitePlace K) (x : mixedSpace K) (c : ℝ) : normAtPlace w (c • x) = |c| * normAtPlace w x := by rw [normAtPlace, MonoidWithZeroHom.coe_mk, ZeroHom.coe_mk] split_ifs <;> simp theorem normAtPlace_real (w : InfinitePlace K) (c : ℝ) : normAtPlace w ((fun _ ↦ c, fun _ ↦ c) : (mixedSpace K)) = |c| := by rw [show ((fun _ ↦ c, fun _ ↦ c) : (mixedSpace K)) = c • 1 by ext <;> simp, normAtPlace_smul, map_one, mul_one] theorem normAtPlace_apply_of_isReal {w : InfinitePlace K} (hw : IsReal w) (x : mixedSpace K) : normAtPlace w x = ‖x.1 ⟨w, hw⟩‖ := by rw [normAtPlace, MonoidWithZeroHom.coe_mk, ZeroHom.coe_mk, dif_pos] theorem normAtPlace_apply_of_isComplex {w : InfinitePlace K} (hw : IsComplex w) (x : mixedSpace K) : normAtPlace w x = ‖x.2 ⟨w, hw⟩‖ := by rw [normAtPlace, MonoidWithZeroHom.coe_mk, ZeroHom.coe_mk, dif_neg (not_isReal_iff_isComplex.mpr hw)] @[deprecated (since := "2025-02-28")] alias normAtPlace_apply_isReal := normAtPlace_apply_of_isReal @[deprecated (since := "2025-02-28")] alias normAtPlace_apply_isComplex := normAtPlace_apply_of_isComplex @[simp] theorem normAtPlace_apply (w : InfinitePlace K) (x : K) : normAtPlace w (mixedEmbedding K x) = w x := by simp_rw [normAtPlace, MonoidWithZeroHom.coe_mk, ZeroHom.coe_mk, mixedEmbedding, RingHom.prod_apply, Pi.ringHom_apply, norm_embedding_of_isReal, norm_embedding_eq, dite_eq_ite, ite_id] theorem forall_normAtPlace_eq_zero_iff {x : mixedSpace K} : (∀ w, normAtPlace w x = 0) ↔ x = 0 := by refine ⟨fun h ↦ ?_, fun h ↦ ?_⟩ · ext w · exact norm_eq_zero.mp (normAtPlace_apply_of_isReal w.prop _ ▸ h w.1) · exact norm_eq_zero.mp (normAtPlace_apply_of_isComplex w.prop _ ▸ h w.1) · simp_rw [h, map_zero, implies_true] @[simp] theorem exists_normAtPlace_ne_zero_iff {x : mixedSpace K} : (∃ w, normAtPlace w x ≠ 0) ↔ x ≠ 0 := by rw [ne_eq, ← forall_normAtPlace_eq_zero_iff, not_forall] @[fun_prop] theorem continuous_normAtPlace (w : InfinitePlace K) : Continuous (normAtPlace w) := by simp_rw [normAtPlace, MonoidWithZeroHom.coe_mk, ZeroHom.coe_mk] split_ifs <;> fun_prop variable [NumberField K] open scoped Classical in theorem nnnorm_eq_sup_normAtPlace (x : mixedSpace K) : ‖x‖₊ = univ.sup fun w ↦ ⟨normAtPlace w x, normAtPlace_nonneg w x⟩ := by have : (univ : Finset (InfinitePlace K)) = (univ.image (fun w : {w : InfinitePlace K // IsReal w} ↦ w.1)) ∪ (univ.image (fun w : {w : InfinitePlace K // IsComplex w} ↦ w.1)) := by ext; simp [isReal_or_isComplex] rw [this, sup_union, univ.sup_image, univ.sup_image, Prod.nnnorm_def, Pi.nnnorm_def, Pi.nnnorm_def] congr · ext w simp [normAtPlace_apply_of_isReal w.prop] · ext w simp [normAtPlace_apply_of_isComplex w.prop] open scoped Classical in theorem norm_eq_sup'_normAtPlace (x : mixedSpace K) : ‖x‖ = univ.sup' univ_nonempty fun w ↦ normAtPlace w x := by rw [← coe_nnnorm, nnnorm_eq_sup_normAtPlace, ← sup'_eq_sup univ_nonempty, ← NNReal.val_eq_coe, ← OrderHom.Subtype.val_coe, map_finset_sup', OrderHom.Subtype.val_coe] simp only [Function.comp_apply] /-- The norm of `x` is `∏ w, (normAtPlace x) ^ mult w`. It is defined such that the norm of `mixedEmbedding K a` for `a : K` is equal to the absolute value of the norm of `a` over `ℚ`, see `norm_eq_norm`. -/ protected def norm : (mixedSpace K) →*₀ ℝ where toFun x := ∏ w, (normAtPlace w x) ^ (mult w) map_one' := by simp only [map_one, one_pow, prod_const_one] map_zero' := by simp [mult] map_mul' _ _ := by simp only [map_mul, mul_pow, prod_mul_distrib] protected theorem norm_apply (x : mixedSpace K) : mixedEmbedding.norm x = ∏ w, (normAtPlace w x) ^ (mult w) := rfl protected theorem norm_nonneg (x : mixedSpace K) : 0 ≤ mixedEmbedding.norm x := univ.prod_nonneg fun _ _ ↦ pow_nonneg (normAtPlace_nonneg _ _) _ protected theorem norm_eq_zero_iff {x : mixedSpace K} : mixedEmbedding.norm x = 0 ↔ ∃ w, normAtPlace w x = 0 := by simp_rw [mixedEmbedding.norm, MonoidWithZeroHom.coe_mk, ZeroHom.coe_mk, prod_eq_zero_iff, mem_univ, true_and, pow_eq_zero_iff mult_ne_zero] protected theorem norm_ne_zero_iff {x : mixedSpace K} : mixedEmbedding.norm x ≠ 0 ↔ ∀ w, normAtPlace w x ≠ 0 := by rw [← not_iff_not] simp_rw [ne_eq, mixedEmbedding.norm_eq_zero_iff, not_not, not_forall, not_not] theorem norm_eq_of_normAtPlace_eq {x y : mixedSpace K} (h : ∀ w, normAtPlace w x = normAtPlace w y) : mixedEmbedding.norm x = mixedEmbedding.norm y := by simp_rw [mixedEmbedding.norm_apply, h] theorem norm_smul (c : ℝ) (x : mixedSpace K) : mixedEmbedding.norm (c • x) = |c| ^ finrank ℚ K * (mixedEmbedding.norm x) := by simp_rw [mixedEmbedding.norm_apply, normAtPlace_smul, mul_pow, prod_mul_distrib, prod_pow_eq_pow_sum, sum_mult_eq] theorem norm_real (c : ℝ) : mixedEmbedding.norm ((fun _ ↦ c, fun _ ↦ c) : (mixedSpace K)) = |c| ^ finrank ℚ K := by rw [show ((fun _ ↦ c, fun _ ↦ c) : (mixedSpace K)) = c • 1 by ext <;> simp, norm_smul, map_one, mul_one] @[simp] theorem norm_eq_norm (x : K) : mixedEmbedding.norm (mixedEmbedding K x) = |Algebra.norm ℚ x| := by simp_rw [mixedEmbedding.norm_apply, normAtPlace_apply, prod_eq_abs_norm] theorem norm_unit (u : (𝓞 K)ˣ) : mixedEmbedding.norm (mixedEmbedding K u) = 1 := by rw [norm_eq_norm, Units.norm, Rat.cast_one] theorem norm_eq_zero_iff' {x : mixedSpace K} (hx : x ∈ Set.range (mixedEmbedding K)) : mixedEmbedding.norm x = 0 ↔ x = 0 := by obtain ⟨a, rfl⟩ := hx rw [norm_eq_norm, Rat.cast_abs, abs_eq_zero, Rat.cast_eq_zero, Algebra.norm_eq_zero_iff, map_eq_zero] variable (K) in @[fun_prop] protected theorem continuous_norm : Continuous (mixedEmbedding.norm : (mixedSpace K) → ℝ) := by refine continuous_finset_prod Finset.univ fun _ _ ↦ ?_ simp_rw [normAtPlace, MonoidWithZeroHom.coe_mk, ZeroHom.coe_mk, dite_pow] split_ifs <;> fun_prop end norm noncomputable section stdBasis open Complex MeasureTheory MeasureTheory.Measure ZSpan Matrix ComplexConjugate variable [NumberField K] /-- The type indexing the basis `stdBasis`. -/ abbrev index := {w : InfinitePlace K // IsReal w} ⊕ ({w : InfinitePlace K // IsComplex w}) × (Fin 2) open scoped Classical in /-- The `ℝ`-basis of the mixed space of `K` formed by the vector equal to `1` at `w` and `0` elsewhere for `IsReal w` and by the couple of vectors equal to `1` (resp. `I`) at `w` and `0` elsewhere for `IsComplex w`. -/ def stdBasis : Basis (index K) ℝ (mixedSpace K) := Basis.prod (Pi.basisFun ℝ _) (Basis.reindex (Pi.basis fun _ => basisOneI) (Equiv.sigmaEquivProd _ _)) variable {K} @[simp] theorem stdBasis_apply_isReal (x : mixedSpace K) (w : {w : InfinitePlace K // IsReal w}) : (stdBasis K).repr x (Sum.inl w) = x.1 w := rfl @[simp] theorem stdBasis_apply_isComplex_fst (x : mixedSpace K) (w : {w : InfinitePlace K // IsComplex w}) : (stdBasis K).repr x (Sum.inr ⟨w, 0⟩) = (x.2 w).re := rfl @[simp] theorem stdBasis_apply_isComplex_snd (x : mixedSpace K) (w : {w : InfinitePlace K // IsComplex w}) : (stdBasis K).repr x (Sum.inr ⟨w, 1⟩) = (x.2 w).im := rfl @[deprecated (since := "2025-02-28")] alias stdBasis_apply_ofIsReal := stdBasis_apply_isReal @[deprecated (since := "2025-02-28")] alias stdBasis_apply_ofIsComplex_fst := stdBasis_apply_isComplex_fst @[deprecated (since := "2025-02-28")] alias stdBasis_apply_ofIsComplex_snd := stdBasis_apply_isComplex_snd variable (K) open scoped Classical in theorem fundamentalDomain_stdBasis : fundamentalDomain (stdBasis K) = (Set.univ.pi fun _ => Set.Ico 0 1) ×ˢ (Set.univ.pi fun _ => Complex.measurableEquivPi⁻¹' (Set.univ.pi fun _ => Set.Ico 0 1)) := by ext simp [stdBasis, mem_fundamentalDomain, Complex.measurableEquivPi] open scoped Classical in theorem volume_fundamentalDomain_stdBasis : volume (fundamentalDomain (stdBasis K)) = 1 := by rw [fundamentalDomain_stdBasis, volume_eq_prod, prod_prod, volume_pi, volume_pi, pi_pi, pi_pi, Complex.volume_preserving_equiv_pi.measure_preimage ?_, volume_pi, pi_pi, Real.volume_Ico, sub_zero, ENNReal.ofReal_one, prod_const_one, prod_const_one, prod_const_one, one_mul] exact (MeasurableSet.pi Set.countable_univ (fun _ _ => measurableSet_Ico)).nullMeasurableSet open scoped Classical in /-- The `Equiv` between `index K` and `K →+* ℂ` defined by sending a real infinite place `w` to the unique corresponding embedding `w.embedding`, and the pair `⟨w, 0⟩` (resp. `⟨w, 1⟩`) for a complex infinite place `w` to `w.embedding` (resp. `conjugate w.embedding`). -/ def indexEquiv : (index K) ≃ (K →+* ℂ) := by refine Equiv.ofBijective (fun c => ?_) ((Fintype.bijective_iff_surjective_and_card _).mpr ⟨?_, ?_⟩) · cases c with | inl w => exact w.val.embedding | inr wj => rcases wj with ⟨w, j⟩ exact if j = 0 then w.val.embedding else ComplexEmbedding.conjugate w.val.embedding · intro φ by_cases hφ : ComplexEmbedding.IsReal φ · exact ⟨Sum.inl (InfinitePlace.mkReal ⟨φ, hφ⟩), by simp [embedding_mk_eq_of_isReal hφ]⟩ · by_cases hw : (InfinitePlace.mk φ).embedding = φ · exact ⟨Sum.inr ⟨InfinitePlace.mkComplex ⟨φ, hφ⟩, 0⟩, by simp [hw]⟩ · exact ⟨Sum.inr ⟨InfinitePlace.mkComplex ⟨φ, hφ⟩, 1⟩, by simp [(embedding_mk_eq φ).resolve_left hw]⟩ · rw [Embeddings.card, ← mixedEmbedding.finrank K, ← Module.finrank_eq_card_basis (stdBasis K)] variable {K} @[simp] theorem indexEquiv_apply_isReal (w : {w : InfinitePlace K // IsReal w}) : (indexEquiv K) (Sum.inl w) = w.val.embedding := rfl @[simp] theorem indexEquiv_apply_isComplex_fst (w : {w : InfinitePlace K // IsComplex w}) : (indexEquiv K) (Sum.inr ⟨w, 0⟩) = w.val.embedding := rfl @[simp] theorem indexEquiv_apply_isComplex_snd (w : {w : InfinitePlace K // IsComplex w}) : (indexEquiv K) (Sum.inr ⟨w, 1⟩) = ComplexEmbedding.conjugate w.val.embedding := rfl @[deprecated (since := "2025-02-28")] alias indexEquiv_apply_ofIsReal := indexEquiv_apply_isReal @[deprecated (since := "2025-02-28")] alias indexEquiv_apply_ofIsComplex_fst := indexEquiv_apply_isComplex_fst @[deprecated (since := "2025-02-28")] alias indexEquiv_apply_ofIsComplex_snd := indexEquiv_apply_isComplex_snd variable (K) open scoped Classical in /-- The matrix that gives the representation on `stdBasis` of the image by `commMap` of an element `x` of `(K →+* ℂ) → ℂ` fixed by the map `x_φ ↦ conj x_(conjugate φ)`, see `stdBasis_repr_eq_matrixToStdBasis_mul`. -/ def matrixToStdBasis : Matrix (index K) (index K) ℂ := fromBlocks (diagonal fun _ => 1) 0 0 <| reindex (Equiv.prodComm _ _) (Equiv.prodComm _ _) (blockDiagonal (fun _ => (2 : ℂ)⁻¹ • !![1, 1; - I, I])) open scoped Classical in theorem det_matrixToStdBasis :
(matrixToStdBasis K).det = (2⁻¹ * I) ^ nrComplexPlaces K := calc _ = ∏ _k : { w : InfinitePlace K // IsComplex w }, det ((2 : ℂ)⁻¹ • !![1, 1; -I, I]) := by rw [matrixToStdBasis, det_fromBlocks_zero₂₁, det_diagonal, prod_const_one, one_mul, det_reindex_self, det_blockDiagonal] _ = ∏ _k : { w : InfinitePlace K // IsComplex w }, (2⁻¹ * Complex.I) := by refine prod_congr (Eq.refl _) (fun _ _ => ?_)
Mathlib/NumberTheory/NumberField/CanonicalEmbedding/Basic.lean
587
593
/- Copyright (c) 2020 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa, Alex Meiburg -/ import Mathlib.Algebra.BigOperators.Fin import Mathlib.Algebra.Polynomial.Degree.Lemmas import Mathlib.Algebra.Polynomial.Degree.Monomial /-! # Erase the leading term of a univariate polynomial ## Definition * `eraseLead f`: the polynomial `f - leading term of f` `eraseLead` serves as reduction step in an induction, shaving off one monomial from a polynomial. The definition is set up so that it does not mention subtraction in the definition, and thus works for polynomials over semirings as well as rings. -/ noncomputable section open Polynomial open Polynomial Finset namespace Polynomial variable {R : Type*} [Semiring R] {f : R[X]} /-- `eraseLead f` for a polynomial `f` is the polynomial obtained by subtracting from `f` the leading term of `f`. -/ def eraseLead (f : R[X]) : R[X] := Polynomial.erase f.natDegree f section EraseLead theorem eraseLead_support (f : R[X]) : f.eraseLead.support = f.support.erase f.natDegree := by simp only [eraseLead, support_erase] theorem eraseLead_coeff (i : ℕ) : f.eraseLead.coeff i = if i = f.natDegree then 0 else f.coeff i := by simp only [eraseLead, coeff_erase] @[simp] theorem eraseLead_coeff_natDegree : f.eraseLead.coeff f.natDegree = 0 := by simp [eraseLead_coeff] theorem eraseLead_coeff_of_ne (i : ℕ) (hi : i ≠ f.natDegree) : f.eraseLead.coeff i = f.coeff i := by simp [eraseLead_coeff, hi] @[simp] theorem eraseLead_zero : eraseLead (0 : R[X]) = 0 := by simp only [eraseLead, erase_zero] @[simp] theorem eraseLead_add_monomial_natDegree_leadingCoeff (f : R[X]) : f.eraseLead + monomial f.natDegree f.leadingCoeff = f := (add_comm _ _).trans (f.monomial_add_erase _) @[simp] theorem eraseLead_add_C_mul_X_pow (f : R[X]) : f.eraseLead + C f.leadingCoeff * X ^ f.natDegree = f := by rw [C_mul_X_pow_eq_monomial, eraseLead_add_monomial_natDegree_leadingCoeff] @[simp] theorem self_sub_monomial_natDegree_leadingCoeff {R : Type*} [Ring R] (f : R[X]) : f - monomial f.natDegree f.leadingCoeff = f.eraseLead := (eq_sub_iff_add_eq.mpr (eraseLead_add_monomial_natDegree_leadingCoeff f)).symm @[simp] theorem self_sub_C_mul_X_pow {R : Type*} [Ring R] (f : R[X]) : f - C f.leadingCoeff * X ^ f.natDegree = f.eraseLead := by rw [C_mul_X_pow_eq_monomial, self_sub_monomial_natDegree_leadingCoeff] theorem eraseLead_ne_zero (f0 : 2 ≤ #f.support) : eraseLead f ≠ 0 := by rw [Ne, ← card_support_eq_zero, eraseLead_support] exact (zero_lt_one.trans_le <| (tsub_le_tsub_right f0 1).trans Finset.pred_card_le_card_erase).ne.symm theorem lt_natDegree_of_mem_eraseLead_support {a : ℕ} (h : a ∈ (eraseLead f).support) : a < f.natDegree := by rw [eraseLead_support, mem_erase] at h exact (le_natDegree_of_mem_supp a h.2).lt_of_ne h.1 theorem ne_natDegree_of_mem_eraseLead_support {a : ℕ} (h : a ∈ (eraseLead f).support) : a ≠ f.natDegree := (lt_natDegree_of_mem_eraseLead_support h).ne theorem natDegree_not_mem_eraseLead_support : f.natDegree ∉ (eraseLead f).support := fun h => ne_natDegree_of_mem_eraseLead_support h rfl theorem eraseLead_support_card_lt (h : f ≠ 0) : #(eraseLead f).support < #f.support := by rw [eraseLead_support] exact card_lt_card (erase_ssubset <| natDegree_mem_support_of_nonzero h) theorem card_support_eraseLead_add_one (h : f ≠ 0) : #f.eraseLead.support + 1 = #f.support := by set c := #f.support with hc cases h₁ : c case zero => by_contra exact h (card_support_eq_zero.mp h₁) case succ => rw [eraseLead_support, card_erase_of_mem (natDegree_mem_support_of_nonzero h), ← hc, h₁] rfl @[simp] theorem card_support_eraseLead : #f.eraseLead.support = #f.support - 1 := by by_cases hf : f = 0 · rw [hf, eraseLead_zero, support_zero, card_empty] · rw [← card_support_eraseLead_add_one hf, add_tsub_cancel_right] theorem card_support_eraseLead' {c : ℕ} (fc : #f.support = c + 1) : #f.eraseLead.support = c := by rw [card_support_eraseLead, fc, add_tsub_cancel_right] theorem card_support_eq_one_of_eraseLead_eq_zero (h₀ : f ≠ 0) (h₁ : f.eraseLead = 0) : #f.support = 1 := (card_support_eq_zero.mpr h₁ ▸ card_support_eraseLead_add_one h₀).symm theorem card_support_le_one_of_eraseLead_eq_zero (h : f.eraseLead = 0) : #f.support ≤ 1 := by by_cases hpz : f = 0 case pos => simp [hpz] case neg => exact le_of_eq (card_support_eq_one_of_eraseLead_eq_zero hpz h) @[simp] theorem eraseLead_monomial (i : ℕ) (r : R) : eraseLead (monomial i r) = 0 := by classical by_cases hr : r = 0 · subst r simp only [monomial_zero_right, eraseLead_zero] · rw [eraseLead, natDegree_monomial, if_neg hr, erase_monomial] @[simp] theorem eraseLead_C (r : R) : eraseLead (C r) = 0 := eraseLead_monomial _ _ @[simp] theorem eraseLead_X : eraseLead (X : R[X]) = 0 := eraseLead_monomial _ _ @[simp] theorem eraseLead_X_pow (n : ℕ) : eraseLead (X ^ n : R[X]) = 0 := by rw [X_pow_eq_monomial, eraseLead_monomial] @[simp] theorem eraseLead_C_mul_X_pow (r : R) (n : ℕ) : eraseLead (C r * X ^ n) = 0 := by rw [C_mul_X_pow_eq_monomial, eraseLead_monomial] @[simp] lemma eraseLead_C_mul_X (r : R) : eraseLead (C r * X) = 0 := by simpa using eraseLead_C_mul_X_pow _ 1 theorem eraseLead_add_of_degree_lt_left {p q : R[X]} (pq : q.degree < p.degree) : (p + q).eraseLead = p.eraseLead + q := by ext n by_cases nd : n = p.natDegree · rw [nd, eraseLead_coeff, if_pos (natDegree_add_eq_left_of_degree_lt pq).symm] simpa using (coeff_eq_zero_of_degree_lt (lt_of_lt_of_le pq degree_le_natDegree)).symm · rw [eraseLead_coeff, coeff_add, coeff_add, eraseLead_coeff, if_neg, if_neg nd] rintro rfl exact nd (natDegree_add_eq_left_of_degree_lt pq) theorem eraseLead_add_of_natDegree_lt_left {p q : R[X]} (pq : q.natDegree < p.natDegree) : (p + q).eraseLead = p.eraseLead + q := eraseLead_add_of_degree_lt_left (degree_lt_degree pq) theorem eraseLead_add_of_degree_lt_right {p q : R[X]} (pq : p.degree < q.degree) : (p + q).eraseLead = p + q.eraseLead := by ext n by_cases nd : n = q.natDegree · rw [nd, eraseLead_coeff, if_pos (natDegree_add_eq_right_of_degree_lt pq).symm] simpa using (coeff_eq_zero_of_degree_lt (lt_of_lt_of_le pq degree_le_natDegree)).symm · rw [eraseLead_coeff, coeff_add, coeff_add, eraseLead_coeff, if_neg, if_neg nd] rintro rfl exact nd (natDegree_add_eq_right_of_degree_lt pq) theorem eraseLead_add_of_natDegree_lt_right {p q : R[X]} (pq : p.natDegree < q.natDegree) : (p + q).eraseLead = p + q.eraseLead := eraseLead_add_of_degree_lt_right (degree_lt_degree pq) theorem eraseLead_degree_le : (eraseLead f).degree ≤ f.degree := f.degree_erase_le _ theorem degree_eraseLead_lt (hf : f ≠ 0) : (eraseLead f).degree < f.degree := f.degree_erase_lt hf theorem eraseLead_natDegree_le_aux : (eraseLead f).natDegree ≤ f.natDegree := natDegree_le_natDegree eraseLead_degree_le theorem eraseLead_natDegree_lt (f0 : 2 ≤ #f.support) : (eraseLead f).natDegree < f.natDegree := lt_of_le_of_ne eraseLead_natDegree_le_aux <| ne_natDegree_of_mem_eraseLead_support <| natDegree_mem_support_of_nonzero <| eraseLead_ne_zero f0 theorem natDegree_pos_of_eraseLead_ne_zero (h : f.eraseLead ≠ 0) : 0 < f.natDegree := by by_contra h₂ rw [eq_C_of_natDegree_eq_zero (Nat.eq_zero_of_not_pos h₂)] at h simp at h theorem eraseLead_natDegree_lt_or_eraseLead_eq_zero (f : R[X]) : (eraseLead f).natDegree < f.natDegree ∨ f.eraseLead = 0 := by by_cases h : #f.support ≤ 1 · right rw [← C_mul_X_pow_eq_self h] simp · left apply eraseLead_natDegree_lt (lt_of_not_ge h) theorem eraseLead_natDegree_le (f : R[X]) : (eraseLead f).natDegree ≤ f.natDegree - 1 := by rcases f.eraseLead_natDegree_lt_or_eraseLead_eq_zero with (h | h) · exact Nat.le_sub_one_of_lt h · simp only [h, natDegree_zero, zero_le] lemma natDegree_eraseLead (h : f.nextCoeff ≠ 0) : f.eraseLead.natDegree = f.natDegree - 1 := by have := natDegree_pos_of_nextCoeff_ne_zero h refine f.eraseLead_natDegree_le.antisymm <| le_natDegree_of_ne_zero ?_ rwa [eraseLead_coeff_of_ne _ (tsub_lt_self _ _).ne, ← nextCoeff_of_natDegree_pos] all_goals positivity lemma natDegree_eraseLead_add_one (h : f.nextCoeff ≠ 0) : f.eraseLead.natDegree + 1 = f.natDegree := by rw [natDegree_eraseLead h, tsub_add_cancel_of_le] exact natDegree_pos_of_nextCoeff_ne_zero h theorem natDegree_eraseLead_le_of_nextCoeff_eq_zero (h : f.nextCoeff = 0) : f.eraseLead.natDegree ≤ f.natDegree - 2 := by refine natDegree_le_pred (n := f.natDegree - 1) (eraseLead_natDegree_le f) ?_ rw [nextCoeff_eq_zero, natDegree_eq_zero] at h obtain ⟨a, rfl⟩ | ⟨hf, h⟩ := h · simp rw [eraseLead_coeff_of_ne _ (tsub_lt_self hf zero_lt_one).ne, ← nextCoeff_of_natDegree_pos hf] simp [nextCoeff_eq_zero, h, eq_zero_or_pos] lemma two_le_natDegree_of_nextCoeff_eraseLead (hlead : f.eraseLead ≠ 0) (hnext : f.nextCoeff = 0) : 2 ≤ f.natDegree := by contrapose! hlead rw [Nat.lt_succ_iff, Nat.le_one_iff_eq_zero_or_eq_one, natDegree_eq_zero, natDegree_eq_one] at hlead obtain ⟨a, rfl⟩ | ⟨a, ha, b, rfl⟩ := hlead · simp · rw [nextCoeff_C_mul_X_add_C ha] at hnext subst b simp theorem leadingCoeff_eraseLead_eq_nextCoeff (h : f.nextCoeff ≠ 0) : f.eraseLead.leadingCoeff = f.nextCoeff := by have := natDegree_pos_of_nextCoeff_ne_zero h rw [leadingCoeff, nextCoeff, natDegree_eraseLead h, if_neg, eraseLead_coeff_of_ne _ (tsub_lt_self _ _).ne] all_goals positivity theorem nextCoeff_eq_zero_of_eraseLead_eq_zero (h : f.eraseLead = 0) : f.nextCoeff = 0 := by by_contra h₂ exact leadingCoeff_ne_zero.mp (leadingCoeff_eraseLead_eq_nextCoeff h₂ ▸ h₂) h end EraseLead /-- An induction lemma for polynomials. It takes a natural number `N` as a parameter, that is required to be at least as big as the `nat_degree` of the polynomial. This is useful to prove results where you want to change each term in a polynomial to something else depending on the `nat_degree` of the polynomial itself and not on the specific `nat_degree` of each term. -/ theorem induction_with_natDegree_le (P : R[X] → Prop) (N : ℕ) (P_0 : P 0) (P_C_mul_pow : ∀ n : ℕ, ∀ r : R, r ≠ 0 → n ≤ N → P (C r * X ^ n)) (P_C_add : ∀ f g : R[X], f.natDegree < g.natDegree → g.natDegree ≤ N → P f → P g → P (f + g)) : ∀ f : R[X], f.natDegree ≤ N → P f := by intro f df generalize hd : #f.support = c revert f induction' c with c hc · intro f _ f0 convert P_0 simpa [support_eq_empty, card_eq_zero] using f0 · intro f df f0 rw [← eraseLead_add_C_mul_X_pow f] cases c · convert P_C_mul_pow f.natDegree f.leadingCoeff ?_ df using 1 · convert zero_add (C (leadingCoeff f) * X ^ f.natDegree) rw [← card_support_eq_zero, card_support_eraseLead' f0] · rw [leadingCoeff_ne_zero, Ne, ← card_support_eq_zero, f0] exact zero_ne_one.symm refine P_C_add f.eraseLead _ ?_ ?_ ?_ ?_ · refine (eraseLead_natDegree_lt ?_).trans_le (le_of_eq ?_) · exact (Nat.succ_le_succ (Nat.succ_le_succ (Nat.zero_le _))).trans f0.ge · rw [natDegree_C_mul_X_pow _ _ (leadingCoeff_ne_zero.mpr _)] rintro rfl simp at f0 · exact (natDegree_C_mul_X_pow_le f.leadingCoeff f.natDegree).trans df · exact hc _ (eraseLead_natDegree_le_aux.trans df) (card_support_eraseLead' f0) · refine P_C_mul_pow _ _ ?_ df rw [Ne, leadingCoeff_eq_zero, ← card_support_eq_zero, f0] exact Nat.succ_ne_zero _ /-- Let `φ : R[x] → S[x]` be an additive map, `k : ℕ` a bound, and `fu : ℕ → ℕ` a "sufficiently monotone" map. Assume also that * `φ` maps to `0` all monomials of degree less than `k`, * `φ` maps each monomial `m` in `R[x]` to a polynomial `φ m` of degree `fu (deg m)`. Then, `φ` maps each polynomial `p` in `R[x]` to a polynomial of degree `fu (deg p)`. -/ theorem mono_map_natDegree_eq {S F : Type*} [Semiring S] [FunLike F R[X] S[X]] [AddMonoidHomClass F R[X] S[X]] {φ : F} {p : R[X]} (k : ℕ) (fu : ℕ → ℕ) (fu0 : ∀ {n}, n ≤ k → fu n = 0) (fc : ∀ {n m}, k ≤ n → n < m → fu n < fu m) (φ_k : ∀ {f : R[X]}, f.natDegree < k → φ f = 0) (φ_mon_nat : ∀ n c, c ≠ 0 → (φ (monomial n c)).natDegree = fu n) : (φ p).natDegree = fu p.natDegree := by refine induction_with_natDegree_le (fun p => (φ p).natDegree = fu p.natDegree) p.natDegree (by simp [fu0]) ?_ ?_ _ rfl.le · intro n r r0 _ rw [natDegree_C_mul_X_pow _ _ r0, C_mul_X_pow_eq_monomial, φ_mon_nat _ _ r0] · intro f g fg _ fk gk rw [natDegree_add_eq_right_of_natDegree_lt fg, map_add] by_cases FG : k ≤ f.natDegree · rw [natDegree_add_eq_right_of_natDegree_lt, gk] rw [fk, gk] exact fc FG fg · cases k · exact (FG (Nat.zero_le _)).elim · rwa [φ_k (not_le.mp FG), zero_add] theorem map_natDegree_eq_sub {S F : Type*} [Semiring S] [FunLike F R[X] S[X]] [AddMonoidHomClass F R[X] S[X]] {φ : F} {p : R[X]} {k : ℕ} (φ_k : ∀ f : R[X], f.natDegree < k → φ f = 0) (φ_mon : ∀ n c, c ≠ 0 → (φ (monomial n c)).natDegree = n - k) : (φ p).natDegree = p.natDegree - k := mono_map_natDegree_eq k (fun j => j - k) (by simp_all) (@fun _ _ h => (tsub_lt_tsub_iff_right h).mpr) (φ_k _) φ_mon theorem map_natDegree_eq_natDegree {S F : Type*} [Semiring S] [FunLike F R[X] S[X]] [AddMonoidHomClass F R[X] S[X]] {φ : F} (p) (φ_mon_nat : ∀ n c, c ≠ 0 → (φ (monomial n c)).natDegree = n) : (φ p).natDegree = p.natDegree := (map_natDegree_eq_sub (fun _ h => (Nat.not_lt_zero _ h).elim) (by simpa)).trans p.natDegree.sub_zero theorem card_support_eq' {n : ℕ} (k : Fin n → ℕ) (x : Fin n → R) (hk : Function.Injective k) (hx : ∀ i, x i ≠ 0) : #(∑ i, C (x i) * X ^ k i).support = n := by suffices (∑ i, C (x i) * X ^ k i).support = image k univ by rw [this, univ.card_image_of_injective hk, card_fin] simp_rw [Finset.ext_iff, mem_support_iff, finset_sum_coeff, coeff_C_mul_X_pow, mem_image, mem_univ, true_and] refine fun i => ⟨fun h => ?_, ?_⟩ · obtain ⟨j, _, h⟩ := exists_ne_zero_of_sum_ne_zero h exact ⟨j, (ite_ne_right_iff.mp h).1.symm⟩ · rintro ⟨j, _, rfl⟩ rw [sum_eq_single_of_mem j (mem_univ j), if_pos rfl] · exact hx j · exact fun m _ hmj => if_neg fun h => hmj.symm (hk h) theorem card_support_eq {n : ℕ} : #f.support = n ↔ ∃ (k : Fin n → ℕ) (x : Fin n → R) (_ : StrictMono k) (_ : ∀ i, x i ≠ 0), f = ∑ i, C (x i) * X ^ k i := by refine ⟨?_, fun ⟨k, x, hk, hx, hf⟩ => hf.symm ▸ card_support_eq' k x hk.injective hx⟩ induction n generalizing f with | zero => exact fun hf => ⟨0, 0, fun x => x.elim0, fun x => x.elim0, card_support_eq_zero.mp hf⟩ | succ n hn => intro h obtain ⟨k, x, hk, hx, hf⟩ := hn (card_support_eraseLead' h) have H : ¬∃ k : Fin n, Fin.castSucc k = Fin.last n := by rintro ⟨i, hi⟩ exact i.castSucc_lt_last.ne hi refine ⟨Function.extend Fin.castSucc k fun _ => f.natDegree, Function.extend Fin.castSucc x fun _ => f.leadingCoeff, ?_, ?_, ?_⟩ · intro i j hij have hi : i ∈ Set.range (Fin.castSucc : Fin n → Fin (n + 1)) := by rw [Fin.range_castSucc, Set.mem_def] exact lt_of_lt_of_le hij (Nat.lt_succ_iff.mp j.2) obtain ⟨i, rfl⟩ := hi rw [Fin.strictMono_castSucc.injective.extend_apply] by_cases hj : ∃ j₀, Fin.castSucc j₀ = j · obtain ⟨j, rfl⟩ := hj rwa [Fin.strictMono_castSucc.injective.extend_apply, hk.lt_iff_lt, ← Fin.castSucc_lt_castSucc_iff] · rw [Function.extend_apply' _ _ _ hj] apply lt_natDegree_of_mem_eraseLead_support rw [mem_support_iff, hf, finset_sum_coeff] rw [sum_eq_single, coeff_C_mul, coeff_X_pow_self, mul_one] · exact hx i · intro j _ hji rw [coeff_C_mul, coeff_X_pow, if_neg (hk.injective.ne hji.symm), mul_zero] · exact fun hi => (hi (mem_univ i)).elim · intro i by_cases hi : ∃ i₀, Fin.castSucc i₀ = i · obtain ⟨i, rfl⟩ := hi rw [Fin.strictMono_castSucc.injective.extend_apply] exact hx i · rw [Function.extend_apply' _ _ _ hi, Ne, leadingCoeff_eq_zero, ← card_support_eq_zero, h] exact n.succ_ne_zero · rw [Fin.sum_univ_castSucc] simp only [Fin.strictMono_castSucc.injective.extend_apply] rw [← hf, Function.extend_apply', Function.extend_apply', eraseLead_add_C_mul_X_pow] all_goals exact H theorem card_support_eq_one : #f.support = 1 ↔ ∃ (k : ℕ) (x : R) (_ : x ≠ 0), f = C x * X ^ k := by refine ⟨fun h => ?_, ?_⟩ · obtain ⟨k, x, _, hx, rfl⟩ := card_support_eq.mp h exact ⟨k 0, x 0, hx 0, Fin.sum_univ_one _⟩ · rintro ⟨k, x, hx, rfl⟩ rw [support_C_mul_X_pow k hx, card_singleton] theorem card_support_eq_two : #f.support = 2 ↔ ∃ (k m : ℕ) (_ : k < m) (x y : R) (_ : x ≠ 0) (_ : y ≠ 0), f = C x * X ^ k + C y * X ^ m := by refine ⟨fun h => ?_, ?_⟩ · obtain ⟨k, x, hk, hx, rfl⟩ := card_support_eq.mp h refine ⟨k 0, k 1, hk Nat.zero_lt_one, x 0, x 1, hx 0, hx 1, ?_⟩ rw [Fin.sum_univ_castSucc, Fin.sum_univ_one] rfl · rintro ⟨k, m, hkm, x, y, hx, hy, rfl⟩ exact card_support_binomial hkm.ne hx hy theorem card_support_eq_three : #f.support = 3 ↔ ∃ (k m n : ℕ) (_ : k < m) (_ : m < n) (x y z : R) (_ : x ≠ 0) (_ : y ≠ 0) (_ : z ≠ 0), f = C x * X ^ k + C y * X ^ m + C z * X ^ n := by refine ⟨fun h => ?_, ?_⟩ · obtain ⟨k, x, hk, hx, rfl⟩ := card_support_eq.mp h refine ⟨k 0, k 1, k 2, hk Nat.zero_lt_one, hk (Nat.lt_succ_self 1), x 0, x 1, x 2, hx 0, hx 1, hx 2, ?_⟩ rw [Fin.sum_univ_castSucc, Fin.sum_univ_castSucc, Fin.sum_univ_one]
rfl · rintro ⟨k, m, n, hkm, hmn, x, y, z, hx, hy, hz, rfl⟩ exact card_support_trinomial hkm hmn hx hy hz end Polynomial
Mathlib/Algebra/Polynomial/EraseLead.lean
424
430
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies, Peter Nelson -/ import Mathlib.Order.Antichain /-! # Minimality and Maximality This file proves basic facts about minimality and maximality of an element with respect to a predicate `P` on an ordered type `α`. ## Implementation Details This file underwent a refactor from a version where minimality and maximality were defined using sets rather than predicates, and with an unbundled order relation rather than a `LE` instance. A side effect is that it has become less straightforward to state that something is minimal with respect to a relation that is *not* defeq to the default `LE`. One possible way would be with a type synonym, and another would be with an ad hoc `LE` instance and `@` notation. This was not an issue in practice anywhere in mathlib at the time of the refactor, but it may be worth re-examining this to make it easier in the future; see the TODO below. ## TODO * In the linearly ordered case, versions of lemmas like `minimal_mem_image` will hold with `MonotoneOn`/`AntitoneOn` assumptions rather than the stronger `x ≤ y ↔ f x ≤ f y` assumptions. * `Set.maximal_iff_forall_insert` and `Set.minimal_iff_forall_diff_singleton` will generalize to lemmas about covering in the case of an `IsStronglyAtomic`/`IsStronglyCoatomic` order. * `Finset` versions of the lemmas about sets. * API to allow for easily expressing min/maximality with respect to an arbitrary non-`LE` relation. * API for `MinimalFor`/`MaximalFor` -/ assert_not_exists CompleteLattice open Set OrderDual variable {α : Type*} {P Q : α → Prop} {a x y : α} section LE variable [LE α] @[simp] theorem minimal_toDual : Minimal (fun x ↦ P (ofDual x)) (toDual x) ↔ Maximal P x := Iff.rfl alias ⟨Minimal.of_dual, Minimal.dual⟩ := minimal_toDual @[simp] theorem maximal_toDual : Maximal (fun x ↦ P (ofDual x)) (toDual x) ↔ Minimal P x := Iff.rfl alias ⟨Maximal.of_dual, Maximal.dual⟩ := maximal_toDual @[simp] theorem minimal_false : ¬ Minimal (fun _ ↦ False) x := by simp [Minimal] @[simp] theorem maximal_false : ¬ Maximal (fun _ ↦ False) x := by simp [Maximal] @[simp] theorem minimal_true : Minimal (fun _ ↦ True) x ↔ IsMin x := by simp [IsMin, Minimal] @[simp] theorem maximal_true : Maximal (fun _ ↦ True) x ↔ IsMax x := minimal_true (α := αᵒᵈ) @[simp] theorem minimal_subtype {x : Subtype Q} : Minimal (fun x ↦ P x.1) x ↔ Minimal (P ⊓ Q) x := by obtain ⟨x, hx⟩ := x simp only [Minimal, Subtype.forall, Subtype.mk_le_mk, Pi.inf_apply, inf_Prop_eq] tauto @[simp] theorem maximal_subtype {x : Subtype Q} : Maximal (fun x ↦ P x.1) x ↔ Maximal (P ⊓ Q) x := minimal_subtype (α := αᵒᵈ) theorem maximal_true_subtype {x : Subtype P} : Maximal (fun _ ↦ True) x ↔ Maximal P x := by obtain ⟨x, hx⟩ := x simp [Maximal, hx] theorem minimal_true_subtype {x : Subtype P} : Minimal (fun _ ↦ True) x ↔ Minimal P x := by obtain ⟨x, hx⟩ := x simp [Minimal, hx] @[simp] theorem minimal_minimal : Minimal (Minimal P) x ↔ Minimal P x := ⟨fun h ↦ h.prop, fun h ↦ ⟨h, fun _ hy hyx ↦ h.le_of_le hy.prop hyx⟩⟩ @[simp] theorem maximal_maximal : Maximal (Maximal P) x ↔ Maximal P x := minimal_minimal (α := αᵒᵈ) /-- If `P` is down-closed, then minimal elements satisfying `P` are exactly the globally minimal elements satisfying `P`. -/ theorem minimal_iff_isMin (hP : ∀ ⦃x y⦄, P y → x ≤ y → P x) : Minimal P x ↔ P x ∧ IsMin x := ⟨fun h ↦ ⟨h.prop, fun _ h' ↦ h.le_of_le (hP h.prop h') h'⟩, fun h ↦ ⟨h.1, fun _ _ h' ↦ h.2 h'⟩⟩ /-- If `P` is up-closed, then maximal elements satisfying `P` are exactly the globally maximal elements satisfying `P`. -/ theorem maximal_iff_isMax (hP : ∀ ⦃x y⦄, P y → y ≤ x → P x) : Maximal P x ↔ P x ∧ IsMax x := ⟨fun h ↦ ⟨h.prop, fun _ h' ↦ h.le_of_ge (hP h.prop h') h'⟩, fun h ↦ ⟨h.1, fun _ _ h' ↦ h.2 h'⟩⟩ theorem Minimal.mono (h : Minimal P x) (hle : Q ≤ P) (hQ : Q x) : Minimal Q x := ⟨hQ, fun y hQy ↦ h.le_of_le (hle y hQy)⟩ theorem Maximal.mono (h : Maximal P x) (hle : Q ≤ P) (hQ : Q x) : Maximal Q x := ⟨hQ, fun y hQy ↦ h.le_of_ge (hle y hQy)⟩ theorem Minimal.and_right (h : Minimal P x) (hQ : Q x) : Minimal (fun x ↦ P x ∧ Q x) x := h.mono (fun _ ↦ And.left) ⟨h.prop, hQ⟩ theorem Minimal.and_left (h : Minimal P x) (hQ : Q x) : Minimal (fun x ↦ (Q x ∧ P x)) x := h.mono (fun _ ↦ And.right) ⟨hQ, h.prop⟩ theorem Maximal.and_right (h : Maximal P x) (hQ : Q x) : Maximal (fun x ↦ (P x ∧ Q x)) x := h.mono (fun _ ↦ And.left) ⟨h.prop, hQ⟩ theorem Maximal.and_left (h : Maximal P x) (hQ : Q x) : Maximal (fun x ↦ (Q x ∧ P x)) x := h.mono (fun _ ↦ And.right) ⟨hQ, h.prop⟩ @[simp] theorem minimal_eq_iff : Minimal (· = y) x ↔ x = y := by simp +contextual [Minimal] @[simp] theorem maximal_eq_iff : Maximal (· = y) x ↔ x = y := by simp +contextual [Maximal] theorem not_minimal_iff (hx : P x) : ¬ Minimal P x ↔ ∃ y, P y ∧ y ≤ x ∧ ¬ (x ≤ y) := by simp [Minimal, hx] theorem not_maximal_iff (hx : P x) : ¬ Maximal P x ↔ ∃ y, P y ∧ x ≤ y ∧ ¬ (y ≤ x) := not_minimal_iff (α := αᵒᵈ) hx theorem Minimal.or (h : Minimal (fun x ↦ P x ∨ Q x) x) : Minimal P x ∨ Minimal Q x := by obtain ⟨h | h, hmin⟩ := h · exact .inl ⟨h, fun y hy hyx ↦ hmin (Or.inl hy) hyx⟩ exact .inr ⟨h, fun y hy hyx ↦ hmin (Or.inr hy) hyx⟩ theorem Maximal.or (h : Maximal (fun x ↦ P x ∨ Q x) x) : Maximal P x ∨ Maximal Q x := Minimal.or (α := αᵒᵈ) h theorem minimal_and_iff_right_of_imp (hPQ : ∀ ⦃x⦄, P x → Q x) : Minimal (fun x ↦ P x ∧ Q x) x ↔ (Minimal P x) ∧ Q x := by simp_rw [and_iff_left_of_imp (fun x ↦ hPQ x), iff_self_and] exact fun h ↦ hPQ h.prop theorem minimal_and_iff_left_of_imp (hPQ : ∀ ⦃x⦄, P x → Q x) : Minimal (fun x ↦ Q x ∧ P x) x ↔ Q x ∧ (Minimal P x) := by simp_rw [iff_comm, and_comm, minimal_and_iff_right_of_imp hPQ, and_comm] theorem maximal_and_iff_right_of_imp (hPQ : ∀ ⦃x⦄, P x → Q x) : Maximal (fun x ↦ P x ∧ Q x) x ↔ (Maximal P x) ∧ Q x := minimal_and_iff_right_of_imp (α := αᵒᵈ) hPQ theorem maximal_and_iff_left_of_imp (hPQ : ∀ ⦃x⦄, P x → Q x) : Maximal (fun x ↦ Q x ∧ P x) x ↔ Q x ∧ (Maximal P x) := minimal_and_iff_left_of_imp (α := αᵒᵈ) hPQ end LE section Preorder variable [Preorder α] theorem minimal_iff_forall_lt : Minimal P x ↔ P x ∧ ∀ ⦃y⦄, y < x → ¬ P y := by simp [Minimal, lt_iff_le_not_le, not_imp_not, imp.swap] theorem maximal_iff_forall_gt : Maximal P x ↔ P x ∧ ∀ ⦃y⦄, x < y → ¬ P y := minimal_iff_forall_lt (α := αᵒᵈ) theorem Minimal.not_prop_of_lt (h : Minimal P x) (hlt : y < x) : ¬ P y := (minimal_iff_forall_lt.1 h).2 hlt theorem Maximal.not_prop_of_gt (h : Maximal P x) (hlt : x < y) : ¬ P y := (maximal_iff_forall_gt.1 h).2 hlt theorem Minimal.not_lt (h : Minimal P x) (hy : P y) : ¬ (y < x) := fun hlt ↦ h.not_prop_of_lt hlt hy theorem Maximal.not_gt (h : Maximal P x) (hy : P y) : ¬ (x < y) := fun hlt ↦ h.not_prop_of_gt hlt hy @[simp] theorem minimal_le_iff : Minimal (· ≤ y) x ↔ x ≤ y ∧ IsMin x := minimal_iff_isMin (fun _ _ h h' ↦ h'.trans h) @[simp] theorem maximal_ge_iff : Maximal (y ≤ ·) x ↔ y ≤ x ∧ IsMax x := minimal_le_iff (α := αᵒᵈ) @[simp] theorem minimal_lt_iff : Minimal (· < y) x ↔ x < y ∧ IsMin x := minimal_iff_isMin (fun _ _ h h' ↦ h'.trans_lt h) @[simp] theorem maximal_gt_iff : Maximal (y < ·) x ↔ y < x ∧ IsMax x := minimal_lt_iff (α := αᵒᵈ) theorem not_minimal_iff_exists_lt (hx : P x) : ¬ Minimal P x ↔ ∃ y, y < x ∧ P y := by simp_rw [not_minimal_iff hx, lt_iff_le_not_le, and_comm] alias ⟨exists_lt_of_not_minimal, _⟩ := not_minimal_iff_exists_lt theorem not_maximal_iff_exists_gt (hx : P x) : ¬ Maximal P x ↔ ∃ y, x < y ∧ P y := not_minimal_iff_exists_lt (α := αᵒᵈ) hx alias ⟨exists_gt_of_not_maximal, _⟩ := not_maximal_iff_exists_gt end Preorder section PartialOrder variable [PartialOrder α] theorem Minimal.eq_of_ge (hx : Minimal P x) (hy : P y) (hge : y ≤ x) : x = y := (hx.2 hy hge).antisymm hge theorem Minimal.eq_of_le (hx : Minimal P x) (hy : P y) (hle : y ≤ x) : y = x := (hx.eq_of_ge hy hle).symm theorem Maximal.eq_of_le (hx : Maximal P x) (hy : P y) (hle : x ≤ y) : x = y := hle.antisymm <| hx.2 hy hle theorem Maximal.eq_of_ge (hx : Maximal P x) (hy : P y) (hge : x ≤ y) : y = x := (hx.eq_of_le hy hge).symm theorem minimal_iff : Minimal P x ↔ P x ∧ ∀ ⦃y⦄, P y → y ≤ x → x = y := ⟨fun h ↦ ⟨h.1, fun _ ↦ h.eq_of_ge⟩, fun h ↦ ⟨h.1, fun _ hy hle ↦ (h.2 hy hle).le⟩⟩ theorem maximal_iff : Maximal P x ↔ P x ∧ ∀ ⦃y⦄, P y → x ≤ y → x = y := minimal_iff (α := αᵒᵈ) theorem minimal_mem_iff {s : Set α} : Minimal (· ∈ s) x ↔ x ∈ s ∧ ∀ ⦃y⦄, y ∈ s → y ≤ x → x = y := minimal_iff theorem maximal_mem_iff {s : Set α} : Maximal (· ∈ s) x ↔ x ∈ s ∧ ∀ ⦃y⦄, y ∈ s → x ≤ y → x = y := maximal_iff /-- If `P y` holds, and everything satisfying `P` is above `y`, then `y` is the unique minimal element satisfying `P`. -/ theorem minimal_iff_eq (hy : P y) (hP : ∀ ⦃x⦄, P x → y ≤ x) : Minimal P x ↔ x = y := ⟨fun h ↦ h.eq_of_ge hy (hP h.prop), by rintro rfl; exact ⟨hy, fun z hz _ ↦ hP hz⟩⟩ /-- If `P y` holds, and everything satisfying `P` is below `y`, then `y` is the unique maximal element satisfying `P`. -/ theorem maximal_iff_eq (hy : P y) (hP : ∀ ⦃x⦄, P x → x ≤ y) : Maximal P x ↔ x = y := minimal_iff_eq (α := αᵒᵈ) hy hP @[simp] theorem minimal_ge_iff : Minimal (y ≤ ·) x ↔ x = y := minimal_iff_eq rfl.le fun _ ↦ id @[simp] theorem maximal_le_iff : Maximal (· ≤ y) x ↔ x = y := maximal_iff_eq rfl.le fun _ ↦ id theorem minimal_iff_minimal_of_imp_of_forall (hPQ : ∀ ⦃x⦄, Q x → P x) (h : ∀ ⦃x⦄, P x → ∃ y, y ≤ x ∧ Q y) : Minimal P x ↔ Minimal Q x := by refine ⟨fun h' ↦ ⟨?_, fun y hy hyx ↦ h'.le_of_le (hPQ hy) hyx⟩, fun h' ↦ ⟨hPQ h'.prop, fun y hy hyx ↦ ?_⟩⟩ · obtain ⟨y, hyx, hy⟩ := h h'.prop rwa [((h'.le_of_le (hPQ hy)) hyx).antisymm hyx] obtain ⟨z, hzy, hz⟩ := h hy exact (h'.le_of_le hz (hzy.trans hyx)).trans hzy theorem maximal_iff_maximal_of_imp_of_forall (hPQ : ∀ ⦃x⦄, Q x → P x) (h : ∀ ⦃x⦄, P x → ∃ y, x ≤ y ∧ Q y) : Maximal P x ↔ Maximal Q x := minimal_iff_minimal_of_imp_of_forall (α := αᵒᵈ) hPQ h end PartialOrder section Subset variable {P : Set α → Prop} {s t : Set α} theorem Minimal.eq_of_superset (h : Minimal P s) (ht : P t) (hts : t ⊆ s) : s = t := h.eq_of_ge ht hts theorem Maximal.eq_of_subset (h : Maximal P s) (ht : P t) (hst : s ⊆ t) : s = t := h.eq_of_le ht hst theorem Minimal.eq_of_subset (h : Minimal P s) (ht : P t) (hts : t ⊆ s) : t = s := h.eq_of_le ht hts theorem Maximal.eq_of_superset (h : Maximal P s) (ht : P t) (hst : s ⊆ t) : t = s := h.eq_of_ge ht hst theorem minimal_subset_iff : Minimal P s ↔ P s ∧ ∀ ⦃t⦄, P t → t ⊆ s → s = t := _root_.minimal_iff theorem maximal_subset_iff : Maximal P s ↔ P s ∧ ∀ ⦃t⦄, P t → s ⊆ t → s = t := _root_.maximal_iff theorem minimal_subset_iff' : Minimal P s ↔ P s ∧ ∀ ⦃t⦄, P t → t ⊆ s → s ⊆ t := Iff.rfl theorem maximal_subset_iff' : Maximal P s ↔ P s ∧ ∀ ⦃t⦄, P t → s ⊆ t → t ⊆ s := Iff.rfl theorem not_minimal_subset_iff (hs : P s) : ¬ Minimal P s ↔ ∃ t, t ⊂ s ∧ P t := not_minimal_iff_exists_lt hs theorem not_maximal_subset_iff (hs : P s) : ¬ Maximal P s ↔ ∃ t, s ⊂ t ∧ P t := not_maximal_iff_exists_gt hs theorem Set.minimal_iff_forall_ssubset : Minimal P s ↔ P s ∧ ∀ ⦃t⦄, t ⊂ s → ¬ P t := minimal_iff_forall_lt theorem Minimal.not_prop_of_ssubset (h : Minimal P s) (ht : t ⊂ s) : ¬ P t := (minimal_iff_forall_lt.1 h).2 ht theorem Minimal.not_ssubset (h : Minimal P s) (ht : P t) : ¬ t ⊂ s := h.not_lt ht theorem Maximal.mem_of_prop_insert (h : Maximal P s) (hx : P (insert x s)) : x ∈ s := h.eq_of_subset hx (subset_insert _ _) ▸ mem_insert .. theorem Minimal.not_mem_of_prop_diff_singleton (h : Minimal P s) (hx : P (s \ {x})) : x ∉ s := fun hxs ↦ ((h.eq_of_superset hx diff_subset).subset hxs).2 rfl theorem Set.minimal_iff_forall_diff_singleton (hP : ∀ ⦃s t⦄, P t → t ⊆ s → P s) : Minimal P s ↔ P s ∧ ∀ x ∈ s, ¬ P (s \ {x}) := ⟨fun h ↦ ⟨h.1, fun _ hx hP ↦ h.not_mem_of_prop_diff_singleton hP hx⟩, fun h ↦ ⟨h.1, fun _ ht hts x hxs ↦ by_contra fun hxt ↦ h.2 x hxs (hP ht <| subset_diff_singleton hts hxt)⟩⟩ theorem Set.exists_diff_singleton_of_not_minimal (hP : ∀ ⦃s t⦄, P t → t ⊆ s → P s) (hs : P s) (h : ¬ Minimal P s) : ∃ x ∈ s, P (s \ {x}) := by simpa [Set.minimal_iff_forall_diff_singleton hP, hs] using h theorem Set.maximal_iff_forall_ssuperset : Maximal P s ↔ P s ∧ ∀ ⦃t⦄, s ⊂ t → ¬ P t := maximal_iff_forall_gt theorem Maximal.not_prop_of_ssuperset (h : Maximal P s) (ht : s ⊂ t) : ¬ P t := (maximal_iff_forall_gt.1 h).2 ht theorem Maximal.not_ssuperset (h : Maximal P s) (ht : P t) : ¬ s ⊂ t := h.not_gt ht theorem Set.maximal_iff_forall_insert (hP : ∀ ⦃s t⦄, P t → s ⊆ t → P s) : Maximal P s ↔ P s ∧ ∀ x ∉ s, ¬ P (insert x s) := by simp only [not_imp_not] exact ⟨fun h ↦ ⟨h.1, fun x ↦ h.mem_of_prop_insert⟩, fun h ↦ ⟨h.1, fun t ht hst x hxt ↦ h.2 x (hP ht <| insert_subset hxt hst)⟩⟩ theorem Set.exists_insert_of_not_maximal (hP : ∀ ⦃s t⦄, P t → s ⊆ t → P s) (hs : P s) (h : ¬ Maximal P s) : ∃ x ∉ s, P (insert x s) := by simpa [Set.maximal_iff_forall_insert hP, hs] using h /- TODO : generalize `minimal_iff_forall_diff_singleton` and `maximal_iff_forall_insert` to `IsStronglyCoatomic`/`IsStronglyAtomic` orders. -/ end Subset section Set variable {s t : Set α} section Preorder variable [Preorder α] theorem setOf_minimal_subset (s : Set α) : {x | Minimal (· ∈ s) x} ⊆ s := sep_subset .. theorem setOf_maximal_subset (s : Set α) : {x | Maximal (· ∈ s) x} ⊆ s := sep_subset .. theorem Set.Subsingleton.maximal_mem_iff (h : s.Subsingleton) : Maximal (· ∈ s) x ↔ x ∈ s := by obtain (rfl | ⟨x, rfl⟩) := h.eq_empty_or_singleton <;> simp theorem Set.Subsingleton.minimal_mem_iff (h : s.Subsingleton) : Minimal (· ∈ s) x ↔ x ∈ s := by obtain (rfl | ⟨x, rfl⟩) := h.eq_empty_or_singleton <;> simp theorem IsLeast.minimal (h : IsLeast s x) : Minimal (· ∈ s) x := ⟨h.1, fun _b hb _ ↦ h.2 hb⟩ theorem IsGreatest.maximal (h : IsGreatest s x) : Maximal (· ∈ s) x := ⟨h.1, fun _b hb _ ↦ h.2 hb⟩ theorem IsAntichain.minimal_mem_iff (hs : IsAntichain (· ≤ ·) s) : Minimal (· ∈ s) x ↔ x ∈ s := ⟨fun h ↦ h.prop, fun h ↦ ⟨h, fun _ hys hyx ↦ (hs.eq hys h hyx).symm.le⟩⟩ theorem IsAntichain.maximal_mem_iff (hs : IsAntichain (· ≤ ·) s) : Maximal (· ∈ s) x ↔ x ∈ s := hs.to_dual.minimal_mem_iff /-- If `t` is an antichain shadowing and including the set of maximal elements of `s`, then `t` *is* the set of maximal elements of `s`. -/ theorem IsAntichain.eq_setOf_maximal (ht : IsAntichain (· ≤ ·) t) (h : ∀ x, Maximal (· ∈ s) x → x ∈ t) (hs : ∀ a ∈ t, ∃ b, b ≤ a ∧ Maximal (· ∈ s) b) : {x | Maximal (· ∈ s) x} = t := by refine Set.ext fun x ↦ ⟨h _, fun hx ↦ ?_⟩ obtain ⟨y, hyx, hy⟩ := hs x hx rwa [← ht.eq (h y hy) hx hyx] /-- If `t` is an antichain shadowed by and including the set of minimal elements of `s`, then `t` *is* the set of minimal elements of `s`. -/ theorem IsAntichain.eq_setOf_minimal (ht : IsAntichain (· ≤ ·) t) (h : ∀ x, Minimal (· ∈ s) x → x ∈ t) (hs : ∀ a ∈ t, ∃ b, a ≤ b ∧ Minimal (· ∈ s) b) : {x | Minimal (· ∈ s) x} = t := ht.to_dual.eq_setOf_maximal h hs end Preorder section PartialOrder variable [PartialOrder α] theorem setOf_maximal_antichain (P : α → Prop) : IsAntichain (· ≤ ·) {x | Maximal P x} := fun _ hx _ ⟨hy, _⟩ hne hle ↦ hne (hle.antisymm <| hx.2 hy hle) theorem setOf_minimal_antichain (P : α → Prop) : IsAntichain (· ≤ ·) {x | Minimal P x} := (setOf_maximal_antichain (α := αᵒᵈ) P).swap theorem IsLeast.minimal_iff (h : IsLeast s a) : Minimal (· ∈ s) x ↔ x = a := ⟨fun h' ↦ h'.eq_of_ge h.1 (h.2 h'.prop), fun h' ↦ h' ▸ h.minimal⟩ theorem IsGreatest.maximal_iff (h : IsGreatest s a) : Maximal (· ∈ s) x ↔ x = a := ⟨fun h' ↦ h'.eq_of_le h.1 (h.2 h'.prop), fun h' ↦ h' ▸ h.maximal⟩ end PartialOrder end Set section Image variable [Preorder α] {β : Type*} [Preorder β] {s : Set α} {t : Set β} section Function variable {f : α → β} theorem minimal_mem_image_monotone (hf : ∀ ⦃x y⦄, x ∈ s → y ∈ s → (f x ≤ f y ↔ x ≤ y)) (hx : Minimal (· ∈ s) x) : Minimal (· ∈ f '' s) (f x) := by refine ⟨mem_image_of_mem f hx.prop, ?_⟩ rintro _ ⟨y, hy, rfl⟩
rw [hf hx.prop hy, hf hy hx.prop] exact hx.le_of_le hy
Mathlib/Order/Minimal.lean
431
433
/- Copyright (c) 2024 Andrew Yang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Andrew Yang -/ import Mathlib.LinearAlgebra.Dimension.Constructions import Mathlib.LinearAlgebra.Dimension.Finite import Mathlib.LinearAlgebra.Isomorphisms import Mathlib.Logic.Equiv.Fin.Rotate /-! # The rank nullity theorem In this file we provide the rank nullity theorem as a typeclass, and prove various corollaries of the theorem. The main definition is `HasRankNullity.{u} R`, which states that 1. Every `R`-module `M : Type u` has a linear independent subset of cardinality `Module.rank R M`. 2. `rank (M ⧸ N) + rank N = rank M` for every `R`-module `M : Type u` and every `N : Submodule R M`. The following instances are provided in mathlib: 1. `DivisionRing.hasRankNullity` for division rings in `LinearAlgebra/Dimension/DivisionRing.lean`. 2. `IsDomain.hasRankNullity` for commutative domains in `LinearAlgebra/Dimension/Localization.lean`. TODO: prove the rank-nullity theorem for `[Ring R] [IsDomain R] [StrongRankCondition R]`. See `nonempty_oreSet_of_strongRankCondition` for a start. -/ universe u v open Function Set Cardinal Submodule LinearMap variable {R} {M M₁ M₂ M₃ : Type u} {M' : Type v} [Ring R] variable [AddCommGroup M] [AddCommGroup M₁] [AddCommGroup M₂] [AddCommGroup M₃] [AddCommGroup M'] variable [Module R M] [Module R M₁] [Module R M₂] [Module R M₃] [Module R M'] /-- `HasRankNullity.{u}` is a class of rings satisfying 1. Every `R`-module `M : Type u` has a linear independent subset of cardinality `Module.rank R M`. 2. `rank (M ⧸ N) + rank N = rank M` for every `R`-module `M : Type u` and every `N : Submodule R M`. Usually such a ring satisfies `HasRankNullity.{w}` for all universes `w`, and the universe argument is there because of technical limitations to universe polymorphism. See `DivisionRing.hasRankNullity` and `IsDomain.hasRankNullity`. -/ @[pp_with_univ] class HasRankNullity (R : Type v) [inst : Ring R] : Prop where exists_set_linearIndependent : ∀ (M : Type u) [AddCommGroup M] [Module R M], ∃ s : Set M, #s = Module.rank R M ∧ LinearIndepOn R id s rank_quotient_add_rank : ∀ {M : Type u} [AddCommGroup M] [Module R M] (N : Submodule R M), Module.rank R (M ⧸ N) + Module.rank R N = Module.rank R M variable [HasRankNullity.{u} R] lemma Submodule.rank_quotient_add_rank (N : Submodule R M) : Module.rank R (M ⧸ N) + Module.rank R N = Module.rank R M := HasRankNullity.rank_quotient_add_rank N variable (R M) in lemma exists_set_linearIndependent : ∃ s : Set M, #s = Module.rank R M ∧ LinearIndependent (ι := s) R Subtype.val := HasRankNullity.exists_set_linearIndependent M variable (R) in theorem nontrivial_of_hasRankNullity : Nontrivial R := by refine (subsingleton_or_nontrivial R).resolve_left fun H ↦ ?_ have := rank_quotient_add_rank (R := R) (M := PUnit) ⊥ simp [one_add_one_eq_two] at this attribute [local instance] nontrivial_of_hasRankNullity theorem LinearMap.lift_rank_range_add_rank_ker (f : M →ₗ[R] M') : lift.{u} (Module.rank R (LinearMap.range f)) + lift.{v} (Module.rank R (LinearMap.ker f)) = lift.{v} (Module.rank R M) := by haveI := fun p : Submodule R M => Classical.decEq (M ⧸ p) rw [← f.quotKerEquivRange.lift_rank_eq, ← lift_add, rank_quotient_add_rank] /-- The **rank-nullity theorem** -/ theorem LinearMap.rank_range_add_rank_ker (f : M →ₗ[R] M₁) : Module.rank R (LinearMap.range f) + Module.rank R (LinearMap.ker f) = Module.rank R M := by haveI := fun p : Submodule R M => Classical.decEq (M ⧸ p) rw [← f.quotKerEquivRange.rank_eq, rank_quotient_add_rank] theorem LinearMap.lift_rank_eq_of_surjective {f : M →ₗ[R] M'} (h : Surjective f) : lift.{v} (Module.rank R M) = lift.{u} (Module.rank R M') + lift.{v} (Module.rank R (LinearMap.ker f)) := by rw [← lift_rank_range_add_rank_ker f, ← rank_range_of_surjective f h] theorem LinearMap.rank_eq_of_surjective {f : M →ₗ[R] M₁} (h : Surjective f) : Module.rank R M = Module.rank R M₁ + Module.rank R (LinearMap.ker f) := by rw [← rank_range_add_rank_ker f, ← rank_range_of_surjective f h] theorem exists_linearIndepOn_of_lt_rank [StrongRankCondition R] {s : Set M} (hs : LinearIndepOn R id s) : ∃ t, s ⊆ t ∧ #t = Module.rank R M ∧ LinearIndepOn R id t := by obtain ⟨t, ht, ht'⟩ := exists_set_linearIndependent R (M ⧸ Submodule.span R s) choose sec hsec using Submodule.mkQ_surjective (Submodule.span R s) have hsec' : (Submodule.mkQ _) ∘ sec = _root_.id := funext hsec have hst : Disjoint s (sec '' t) := by rw [Set.disjoint_iff] rintro _ ⟨hxs, ⟨x, hxt, rfl⟩⟩ apply ht'.ne_zero ⟨x, hxt⟩ rw [Subtype.coe_mk, ← hsec x,mkQ_apply, Quotient.mk_eq_zero] exact Submodule.subset_span hxs refine ⟨s ∪ sec '' t, subset_union_left, ?_, ?_⟩ · rw [Cardinal.mk_union_of_disjoint hst, Cardinal.mk_image_eq, ht, ← rank_quotient_add_rank (Submodule.span R s), add_comm, rank_span_set hs] exact HasLeftInverse.injective ⟨Submodule.Quotient.mk, hsec⟩ · apply LinearIndepOn.union_id_of_quotient Submodule.subset_span hs rwa [linearIndepOn_iff_image (hsec'.symm ▸ injective_id).injOn.image_of_comp, ← image_comp, hsec', image_id] @[deprecated (since := "2025-02-17")] alias
exists_linearIndependent_of_lt_rank := exists_linearIndepOn_of_lt_rank /-- Given a family of `n` linearly independent vectors in a space of dimension `> n`, one may extend the family by another vector while retaining linear independence. -/ theorem exists_linearIndependent_cons_of_lt_rank [StrongRankCondition R] {n : ℕ} {v : Fin n → M} (hv : LinearIndependent R v) (h : n < Module.rank R M) : ∃ (x : M), LinearIndependent R (Fin.cons x v) := by obtain ⟨t, h₁, h₂, h₃⟩ := exists_linearIndepOn_of_lt_rank hv.linearIndepOn_id have : range v ≠ t := by refine fun e ↦ h.ne ?_ rw [← e, ← lift_injective.eq_iff, mk_range_eq_of_injective hv.injective] at h₂
Mathlib/LinearAlgebra/Dimension/RankNullity.lean
113
123
/- Copyright (c) 2022 Yaël Dillies, Sara Rousta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies, Sara Rousta -/ import Mathlib.Logic.Equiv.Set import Mathlib.Order.Interval.Set.OrderEmbedding import Mathlib.Order.SetNotation /-! # Properties of unbundled upper/lower sets This file proves results on `IsUpperSet` and `IsLowerSet`, including their interactions with set operations, images, preimages and order duals, and properties that reflect stronger assumptions on the underlying order (such as `PartialOrder` and `LinearOrder`). ## TODO * Lattice structure on antichains. * Order equivalence between upper/lower sets and antichains. -/ open OrderDual Set variable {α β : Type*} {ι : Sort*} {κ : ι → Sort*} attribute [aesop norm unfold] IsUpperSet IsLowerSet section LE variable [LE α] {s t : Set α} {a : α} theorem isUpperSet_empty : IsUpperSet (∅ : Set α) := fun _ _ _ => id theorem isLowerSet_empty : IsLowerSet (∅ : Set α) := fun _ _ _ => id theorem isUpperSet_univ : IsUpperSet (univ : Set α) := fun _ _ _ => id theorem isLowerSet_univ : IsLowerSet (univ : Set α) := fun _ _ _ => id theorem IsUpperSet.compl (hs : IsUpperSet s) : IsLowerSet sᶜ := fun _a _b h hb ha => hb <| hs h ha theorem IsLowerSet.compl (hs : IsLowerSet s) : IsUpperSet sᶜ := fun _a _b h hb ha => hb <| hs h ha @[simp] theorem isUpperSet_compl : IsUpperSet sᶜ ↔ IsLowerSet s := ⟨fun h => by convert h.compl rw [compl_compl], IsLowerSet.compl⟩ @[simp] theorem isLowerSet_compl : IsLowerSet sᶜ ↔ IsUpperSet s := ⟨fun h => by convert h.compl rw [compl_compl], IsUpperSet.compl⟩ theorem IsUpperSet.union (hs : IsUpperSet s) (ht : IsUpperSet t) : IsUpperSet (s ∪ t) := fun _ _ h => Or.imp (hs h) (ht h) theorem IsLowerSet.union (hs : IsLowerSet s) (ht : IsLowerSet t) : IsLowerSet (s ∪ t) := fun _ _ h => Or.imp (hs h) (ht h) theorem IsUpperSet.inter (hs : IsUpperSet s) (ht : IsUpperSet t) : IsUpperSet (s ∩ t) := fun _ _ h => And.imp (hs h) (ht h) theorem IsLowerSet.inter (hs : IsLowerSet s) (ht : IsLowerSet t) : IsLowerSet (s ∩ t) := fun _ _ h => And.imp (hs h) (ht h) theorem isUpperSet_sUnion {S : Set (Set α)} (hf : ∀ s ∈ S, IsUpperSet s) : IsUpperSet (⋃₀ S) := fun _ _ h => Exists.imp fun _ hs => ⟨hs.1, hf _ hs.1 h hs.2⟩ theorem isLowerSet_sUnion {S : Set (Set α)} (hf : ∀ s ∈ S, IsLowerSet s) : IsLowerSet (⋃₀ S) := fun _ _ h => Exists.imp fun _ hs => ⟨hs.1, hf _ hs.1 h hs.2⟩ theorem isUpperSet_iUnion {f : ι → Set α} (hf : ∀ i, IsUpperSet (f i)) : IsUpperSet (⋃ i, f i) := isUpperSet_sUnion <| forall_mem_range.2 hf theorem isLowerSet_iUnion {f : ι → Set α} (hf : ∀ i, IsLowerSet (f i)) : IsLowerSet (⋃ i, f i) := isLowerSet_sUnion <| forall_mem_range.2 hf theorem isUpperSet_iUnion₂ {f : ∀ i, κ i → Set α} (hf : ∀ i j, IsUpperSet (f i j)) : IsUpperSet (⋃ (i) (j), f i j) := isUpperSet_iUnion fun i => isUpperSet_iUnion <| hf i theorem isLowerSet_iUnion₂ {f : ∀ i, κ i → Set α} (hf : ∀ i j, IsLowerSet (f i j)) : IsLowerSet (⋃ (i) (j), f i j) := isLowerSet_iUnion fun i => isLowerSet_iUnion <| hf i theorem isUpperSet_sInter {S : Set (Set α)} (hf : ∀ s ∈ S, IsUpperSet s) : IsUpperSet (⋂₀ S) := fun _ _ h => forall₂_imp fun s hs => hf s hs h theorem isLowerSet_sInter {S : Set (Set α)} (hf : ∀ s ∈ S, IsLowerSet s) : IsLowerSet (⋂₀ S) := fun _ _ h => forall₂_imp fun s hs => hf s hs h theorem isUpperSet_iInter {f : ι → Set α} (hf : ∀ i, IsUpperSet (f i)) : IsUpperSet (⋂ i, f i) := isUpperSet_sInter <| forall_mem_range.2 hf theorem isLowerSet_iInter {f : ι → Set α} (hf : ∀ i, IsLowerSet (f i)) : IsLowerSet (⋂ i, f i) := isLowerSet_sInter <| forall_mem_range.2 hf theorem isUpperSet_iInter₂ {f : ∀ i, κ i → Set α} (hf : ∀ i j, IsUpperSet (f i j)) : IsUpperSet (⋂ (i) (j), f i j) := isUpperSet_iInter fun i => isUpperSet_iInter <| hf i theorem isLowerSet_iInter₂ {f : ∀ i, κ i → Set α} (hf : ∀ i j, IsLowerSet (f i j)) : IsLowerSet (⋂ (i) (j), f i j) := isLowerSet_iInter fun i => isLowerSet_iInter <| hf i @[simp] theorem isLowerSet_preimage_ofDual_iff : IsLowerSet (ofDual ⁻¹' s) ↔ IsUpperSet s := Iff.rfl @[simp] theorem isUpperSet_preimage_ofDual_iff : IsUpperSet (ofDual ⁻¹' s) ↔ IsLowerSet s := Iff.rfl @[simp] theorem isLowerSet_preimage_toDual_iff {s : Set αᵒᵈ} : IsLowerSet (toDual ⁻¹' s) ↔ IsUpperSet s := Iff.rfl @[simp] theorem isUpperSet_preimage_toDual_iff {s : Set αᵒᵈ} : IsUpperSet (toDual ⁻¹' s) ↔ IsLowerSet s := Iff.rfl alias ⟨_, IsUpperSet.toDual⟩ := isLowerSet_preimage_ofDual_iff alias ⟨_, IsLowerSet.toDual⟩ := isUpperSet_preimage_ofDual_iff alias ⟨_, IsUpperSet.ofDual⟩ := isLowerSet_preimage_toDual_iff alias ⟨_, IsLowerSet.ofDual⟩ := isUpperSet_preimage_toDual_iff lemma IsUpperSet.isLowerSet_preimage_coe (hs : IsUpperSet s) : IsLowerSet ((↑) ⁻¹' t : Set s) ↔ ∀ b ∈ s, ∀ c ∈ t, b ≤ c → b ∈ t := by aesop lemma IsLowerSet.isUpperSet_preimage_coe (hs : IsLowerSet s) : IsUpperSet ((↑) ⁻¹' t : Set s) ↔ ∀ b ∈ s, ∀ c ∈ t, c ≤ b → b ∈ t := by aesop lemma IsUpperSet.sdiff (hs : IsUpperSet s) (ht : ∀ b ∈ s, ∀ c ∈ t, b ≤ c → b ∈ t) : IsUpperSet (s \ t) := fun _b _c hbc hb ↦ ⟨hs hbc hb.1, fun hc ↦ hb.2 <| ht _ hb.1 _ hc hbc⟩ lemma IsLowerSet.sdiff (hs : IsLowerSet s) (ht : ∀ b ∈ s, ∀ c ∈ t, c ≤ b → b ∈ t) : IsLowerSet (s \ t) := fun _b _c hcb hb ↦ ⟨hs hcb hb.1, fun hc ↦ hb.2 <| ht _ hb.1 _ hc hcb⟩ lemma IsUpperSet.sdiff_of_isLowerSet (hs : IsUpperSet s) (ht : IsLowerSet t) : IsUpperSet (s \ t) := hs.sdiff <| by aesop lemma IsLowerSet.sdiff_of_isUpperSet (hs : IsLowerSet s) (ht : IsUpperSet t) : IsLowerSet (s \ t) := hs.sdiff <| by aesop lemma IsUpperSet.erase (hs : IsUpperSet s) (has : ∀ b ∈ s, b ≤ a → b = a) : IsUpperSet (s \ {a}) := hs.sdiff <| by simpa using has lemma IsLowerSet.erase (hs : IsLowerSet s) (has : ∀ b ∈ s, a ≤ b → b = a) : IsLowerSet (s \ {a}) := hs.sdiff <| by simpa using has end LE section Preorder variable [Preorder α] [Preorder β] {s : Set α} {p : α → Prop} (a : α) theorem isUpperSet_Ici : IsUpperSet (Ici a) := fun _ _ => ge_trans theorem isLowerSet_Iic : IsLowerSet (Iic a) := fun _ _ => le_trans theorem isUpperSet_Ioi : IsUpperSet (Ioi a) := fun _ _ => flip lt_of_lt_of_le theorem isLowerSet_Iio : IsLowerSet (Iio a) := fun _ _ => lt_of_le_of_lt theorem isUpperSet_iff_Ici_subset : IsUpperSet s ↔ ∀ ⦃a⦄, a ∈ s → Ici a ⊆ s := by simp [IsUpperSet, subset_def, @forall_swap (_ ∈ s)] theorem isLowerSet_iff_Iic_subset : IsLowerSet s ↔ ∀ ⦃a⦄, a ∈ s → Iic a ⊆ s := by simp [IsLowerSet, subset_def, @forall_swap (_ ∈ s)] alias ⟨IsUpperSet.Ici_subset, _⟩ := isUpperSet_iff_Ici_subset alias ⟨IsLowerSet.Iic_subset, _⟩ := isLowerSet_iff_Iic_subset theorem IsUpperSet.Ioi_subset (h : IsUpperSet s) ⦃a⦄ (ha : a ∈ s) : Ioi a ⊆ s := Ioi_subset_Ici_self.trans <| h.Ici_subset ha theorem IsLowerSet.Iio_subset (h : IsLowerSet s) ⦃a⦄ (ha : a ∈ s) : Iio a ⊆ s := h.toDual.Ioi_subset ha theorem IsUpperSet.ordConnected (h : IsUpperSet s) : s.OrdConnected := ⟨fun _ ha _ _ => Icc_subset_Ici_self.trans <| h.Ici_subset ha⟩ theorem IsLowerSet.ordConnected (h : IsLowerSet s) : s.OrdConnected := ⟨fun _ _ _ hb => Icc_subset_Iic_self.trans <| h.Iic_subset hb⟩ theorem IsUpperSet.preimage (hs : IsUpperSet s) {f : β → α} (hf : Monotone f) : IsUpperSet (f ⁻¹' s : Set β) := fun _ _ h => hs <| hf h theorem IsLowerSet.preimage (hs : IsLowerSet s) {f : β → α} (hf : Monotone f) : IsLowerSet (f ⁻¹' s : Set β) := fun _ _ h => hs <| hf h theorem IsUpperSet.image (hs : IsUpperSet s) (f : α ≃o β) : IsUpperSet (f '' s : Set β) := by change IsUpperSet ((f : α ≃ β) '' s) rw [Set.image_equiv_eq_preimage_symm] exact hs.preimage f.symm.monotone theorem IsLowerSet.image (hs : IsLowerSet s) (f : α ≃o β) : IsLowerSet (f '' s : Set β) := by change IsLowerSet ((f : α ≃ β) '' s) rw [Set.image_equiv_eq_preimage_symm] exact hs.preimage f.symm.monotone theorem OrderEmbedding.image_Ici (e : α ↪o β) (he : IsUpperSet (range e)) (a : α) : e '' Ici a = Ici (e a) := by rw [← e.preimage_Ici, image_preimage_eq_inter_range, inter_eq_left.2 <| he.Ici_subset (mem_range_self _)] theorem OrderEmbedding.image_Iic (e : α ↪o β) (he : IsLowerSet (range e)) (a : α) : e '' Iic a = Iic (e a) := e.dual.image_Ici he a theorem OrderEmbedding.image_Ioi (e : α ↪o β) (he : IsUpperSet (range e)) (a : α) : e '' Ioi a = Ioi (e a) := by rw [← e.preimage_Ioi, image_preimage_eq_inter_range, inter_eq_left.2 <| he.Ioi_subset (mem_range_self _)] theorem OrderEmbedding.image_Iio (e : α ↪o β) (he : IsLowerSet (range e)) (a : α) : e '' Iio a = Iio (e a) := e.dual.image_Ioi he a @[simp] theorem Set.monotone_mem : Monotone (· ∈ s) ↔ IsUpperSet s := Iff.rfl @[simp] theorem Set.antitone_mem : Antitone (· ∈ s) ↔ IsLowerSet s := forall_swap @[simp] theorem isUpperSet_setOf : IsUpperSet { a | p a } ↔ Monotone p := Iff.rfl @[simp] theorem isLowerSet_setOf : IsLowerSet { a | p a } ↔ Antitone p := forall_swap lemma IsUpperSet.upperBounds_subset (hs : IsUpperSet s) : s.Nonempty → upperBounds s ⊆ s := fun ⟨_a, ha⟩ _b hb ↦ hs (hb ha) ha lemma IsLowerSet.lowerBounds_subset (hs : IsLowerSet s) : s.Nonempty → lowerBounds s ⊆ s := fun ⟨_a, ha⟩ _b hb ↦ hs (hb ha) ha section OrderTop variable [OrderTop α] theorem IsLowerSet.top_mem (hs : IsLowerSet s) : ⊤ ∈ s ↔ s = univ := ⟨fun h => eq_univ_of_forall fun _ => hs le_top h, fun h => h.symm ▸ mem_univ _⟩ theorem IsUpperSet.top_mem (hs : IsUpperSet s) : ⊤ ∈ s ↔ s.Nonempty := ⟨fun h => ⟨_, h⟩, fun ⟨_a, ha⟩ => hs le_top ha⟩ theorem IsUpperSet.not_top_mem (hs : IsUpperSet s) : ⊤ ∉ s ↔ s = ∅ := hs.top_mem.not.trans not_nonempty_iff_eq_empty end OrderTop section OrderBot variable [OrderBot α] theorem IsUpperSet.bot_mem (hs : IsUpperSet s) : ⊥ ∈ s ↔ s = univ := ⟨fun h => eq_univ_of_forall fun _ => hs bot_le h, fun h => h.symm ▸ mem_univ _⟩ theorem IsLowerSet.bot_mem (hs : IsLowerSet s) : ⊥ ∈ s ↔ s.Nonempty := ⟨fun h => ⟨_, h⟩, fun ⟨_a, ha⟩ => hs bot_le ha⟩ theorem IsLowerSet.not_bot_mem (hs : IsLowerSet s) : ⊥ ∉ s ↔ s = ∅ := hs.bot_mem.not.trans not_nonempty_iff_eq_empty end OrderBot section NoMaxOrder variable [NoMaxOrder α] theorem IsUpperSet.not_bddAbove (hs : IsUpperSet s) : s.Nonempty → ¬BddAbove s := by rintro ⟨a, ha⟩ ⟨b, hb⟩ obtain ⟨c, hc⟩ := exists_gt b exact hc.not_le (hb <| hs ((hb ha).trans hc.le) ha) theorem not_bddAbove_Ici : ¬BddAbove (Ici a) := (isUpperSet_Ici _).not_bddAbove nonempty_Ici theorem not_bddAbove_Ioi : ¬BddAbove (Ioi a) := (isUpperSet_Ioi _).not_bddAbove nonempty_Ioi end NoMaxOrder section NoMinOrder variable [NoMinOrder α] theorem IsLowerSet.not_bddBelow (hs : IsLowerSet s) : s.Nonempty → ¬BddBelow s := by rintro ⟨a, ha⟩ ⟨b, hb⟩ obtain ⟨c, hc⟩ := exists_lt b exact hc.not_le (hb <| hs (hc.le.trans <| hb ha) ha) theorem not_bddBelow_Iic : ¬BddBelow (Iic a) := (isLowerSet_Iic _).not_bddBelow nonempty_Iic theorem not_bddBelow_Iio : ¬BddBelow (Iio a) := (isLowerSet_Iio _).not_bddBelow nonempty_Iio end NoMinOrder end Preorder section PartialOrder variable [PartialOrder α] {s : Set α} theorem isUpperSet_iff_forall_lt : IsUpperSet s ↔ ∀ ⦃a b : α⦄, a < b → a ∈ s → b ∈ s := forall_congr' fun a => by simp [le_iff_eq_or_lt, or_imp, forall_and] theorem isLowerSet_iff_forall_lt : IsLowerSet s ↔ ∀ ⦃a b : α⦄, b < a → a ∈ s → b ∈ s := forall_congr' fun a => by simp [le_iff_eq_or_lt, or_imp, forall_and] theorem isUpperSet_iff_Ioi_subset : IsUpperSet s ↔ ∀ ⦃a⦄, a ∈ s → Ioi a ⊆ s := by simp [isUpperSet_iff_forall_lt, subset_def, @forall_swap (_ ∈ s)] theorem isLowerSet_iff_Iio_subset : IsLowerSet s ↔ ∀ ⦃a⦄, a ∈ s → Iio a ⊆ s := by simp [isLowerSet_iff_forall_lt, subset_def, @forall_swap (_ ∈ s)] end PartialOrder section LinearOrder variable [LinearOrder α] {s t : Set α} theorem IsUpperSet.total (hs : IsUpperSet s) (ht : IsUpperSet t) : s ⊆ t ∨ t ⊆ s := by by_contra! h simp_rw [Set.not_subset] at h obtain ⟨⟨a, has, hat⟩, b, hbt, hbs⟩ := h obtain hab | hba := le_total a b · exact hbs (hs hab has) · exact hat (ht hba hbt) theorem IsLowerSet.total (hs : IsLowerSet s) (ht : IsLowerSet t) : s ⊆ t ∨ t ⊆ s := hs.toDual.total ht.toDual end LinearOrder
Mathlib/Order/UpperLower/Basic.lean
1,070
1,072
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Heather Macbeth -/ import Mathlib.Analysis.Convex.Cone.Extension import Mathlib.Analysis.NormedSpace.RCLike import Mathlib.Analysis.NormedSpace.Extend import Mathlib.Analysis.RCLike.Lemmas /-! # Extension Hahn-Banach theorem In this file we prove the analytic Hahn-Banach theorem. For any continuous linear function on a subspace, we can extend it to a function on the entire space without changing its norm. We prove * `Real.exists_extension_norm_eq`: Hahn-Banach theorem for continuous linear functions on normed spaces over `ℝ`. * `exists_extension_norm_eq`: Hahn-Banach theorem for continuous linear functions on normed spaces over `ℝ` or `ℂ`. In order to state and prove the corollaries uniformly, we prove the statements for a field `𝕜` satisfying `RCLike 𝕜`. In this setting, `exists_dual_vector` states that, for any nonzero `x`, there exists a continuous linear form `g` of norm `1` with `g x = ‖x‖` (where the norm has to be interpreted as an element of `𝕜`). -/ universe u v namespace Real variable {E : Type*} [SeminormedAddCommGroup E] [NormedSpace ℝ E] /-- **Hahn-Banach theorem** for continuous linear functions over `ℝ`. See also `exists_extension_norm_eq` in the root namespace for a more general version that works both for `ℝ` and `ℂ`. -/ theorem exists_extension_norm_eq (p : Subspace ℝ E) (f : p →L[ℝ] ℝ) : ∃ g : E →L[ℝ] ℝ, (∀ x : p, g x = f x) ∧ ‖g‖ = ‖f‖ := by rcases exists_extension_of_le_sublinear ⟨p, f⟩ (fun x => ‖f‖ * ‖x‖) (fun c hc x => by simp only [norm_smul c x, Real.norm_eq_abs, abs_of_pos hc, mul_left_comm]) (fun x y => by rw [← left_distrib] exact mul_le_mul_of_nonneg_left (norm_add_le x y) (@norm_nonneg _ _ f)) fun x => le_trans (le_abs_self _) (f.le_opNorm _) with ⟨g, g_eq, g_le⟩ set g' := g.mkContinuous ‖f‖ fun x => abs_le.2 ⟨neg_le.1 <| g.map_neg x ▸ norm_neg x ▸ g_le (-x), g_le x⟩ refine ⟨g', g_eq, ?_⟩ apply le_antisymm (g.mkContinuous_norm_le (norm_nonneg f) _) refine f.opNorm_le_bound (norm_nonneg _) fun x => ?_ dsimp at g_eq rw [← g_eq] apply g'.le_opNorm end Real section RCLike open RCLike variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] [IsRCLikeNormedField 𝕜] {E F : Type*} [SeminormedAddCommGroup E] [NormedSpace 𝕜 E] [NormedAddCommGroup F] [NormedSpace 𝕜 F] /-- **Hahn-Banach theorem** for continuous linear functions over `𝕜` satisfying `IsRCLikeNormedField 𝕜`. -/ theorem exists_extension_norm_eq (p : Subspace 𝕜 E) (f : p →L[𝕜] 𝕜) : ∃ g : E →L[𝕜] 𝕜, (∀ x : p, g x = f x) ∧ ‖g‖ = ‖f‖ := by letI : RCLike 𝕜 := IsRCLikeNormedField.rclike 𝕜 letI : Module ℝ E := RestrictScalars.module ℝ 𝕜 E letI : IsScalarTower ℝ 𝕜 E := RestrictScalars.isScalarTower _ _ _ letI : NormedSpace ℝ E := NormedSpace.restrictScalars _ 𝕜 _ -- Let `fr: p →L[ℝ] ℝ` be the real part of `f`. let fr := reCLM.comp (f.restrictScalars ℝ) -- Use the real version to get a norm-preserving extension of `fr`, which -- we'll call `g : E →L[ℝ] ℝ`. rcases Real.exists_extension_norm_eq (p.restrictScalars ℝ) fr with ⟨g, ⟨hextends, hnormeq⟩⟩ -- Now `g` can be extended to the `E →L[𝕜] 𝕜` we need. refine ⟨g.extendTo𝕜, ?_⟩ -- It is an extension of `f`. have h : ∀ x : p, g.extendTo𝕜 x = f x := by intro x rw [ContinuousLinearMap.extendTo𝕜_apply, ← Submodule.coe_smul] -- This used to be `rw`, but we need `erw` after https://github.com/leanprover/lean4/pull/2644 -- The goal has a coercion from `RestrictScalars ℝ 𝕜 E →L[ℝ] ℝ`, but -- `hextends` involves a coercion from `E →L[ℝ] ℝ`. erw [hextends] erw [hextends] have : (fr x : 𝕜) - I * ↑(fr ((I : 𝕜) • x)) = (re (f x) : 𝕜) - (I : 𝕜) * re (f ((I : 𝕜) • x)) := by rfl -- This used to be `rw`, but we need `erw` after https://github.com/leanprover/lean4/pull/2644 erw [this] apply ext · simp only [add_zero, Algebra.id.smul_eq_mul, I_re, ofReal_im, AddMonoidHom.map_add, zero_sub, I_im', zero_mul, ofReal_re, eq_self_iff_true, sub_zero, mul_neg, ofReal_neg, mul_re, mul_zero, sub_neg_eq_add, ContinuousLinearMap.map_smul] · simp only [Algebra.id.smul_eq_mul, I_re, ofReal_im, AddMonoidHom.map_add, zero_sub, I_im', zero_mul, ofReal_re, mul_neg, mul_im, zero_add, ofReal_neg, mul_re, sub_neg_eq_add, ContinuousLinearMap.map_smul] -- And we derive the equality of the norms by bounding on both sides. refine ⟨h, le_antisymm ?_ ?_⟩ · calc ‖g.extendTo𝕜‖ = ‖g‖ := g.norm_extendTo𝕜 _ = ‖fr‖ := hnormeq _ ≤ ‖reCLM‖ * ‖f‖ := ContinuousLinearMap.opNorm_comp_le _ _ _ = ‖f‖ := by rw [reCLM_norm, one_mul] · exact f.opNorm_le_bound g.extendTo𝕜.opNorm_nonneg fun x => h x ▸ g.extendTo𝕜.le_opNorm x open Module /-- Corollary of the **Hahn-Banach theorem**: if `f : p → F` is a continuous linear map from a submodule of a normed space `E` over `𝕜`, `𝕜 = ℝ` or `𝕜 = ℂ`, with a finite dimensional range, then `f` admits an extension to a continuous linear map `E → F`. Note that contrary to the case `F = 𝕜`, see `exists_extension_norm_eq`, we provide no estimates on the norm of the extension. -/ lemma ContinuousLinearMap.exist_extension_of_finiteDimensional_range {p : Submodule 𝕜 E} (f : p →L[𝕜] F) [FiniteDimensional 𝕜 (LinearMap.range f)] : ∃ g : E →L[𝕜] F, f = g.comp p.subtypeL := by letI : RCLike 𝕜 := IsRCLikeNormedField.rclike 𝕜 set b := Module.finBasis 𝕜 (LinearMap.range f) set e := b.equivFunL set fi := fun i ↦ (LinearMap.toContinuousLinearMap (b.coord i)).comp (f.codRestrict _ <| LinearMap.mem_range_self _) choose gi hgf _ using fun i ↦ exists_extension_norm_eq p (fi i) use (LinearMap.range f).subtypeL.comp <| e.symm.toContinuousLinearMap.comp (.pi gi) ext x simp [fi, e, hgf] /-- A finite dimensional submodule over `ℝ` or `ℂ` is `Submodule.ClosedComplemented`. -/ lemma Submodule.ClosedComplemented.of_finiteDimensional (p : Submodule 𝕜 F) [FiniteDimensional 𝕜 p] : p.ClosedComplemented := let ⟨g, hg⟩ := (ContinuousLinearMap.id 𝕜 p).exist_extension_of_finiteDimensional_range ⟨g, DFunLike.congr_fun hg.symm⟩ end RCLike section DualVector variable (𝕜 : Type v) [RCLike 𝕜] variable {E : Type u} [NormedAddCommGroup E] [NormedSpace 𝕜 E] open ContinuousLinearEquiv Submodule theorem coord_norm' {x : E} (h : x ≠ 0) : ‖(‖x‖ : 𝕜) • coord 𝕜 x h‖ = 1 := by rw [norm_smul (α := 𝕜) (x := coord 𝕜 x h), RCLike.norm_coe_norm, coord_norm, mul_inv_cancel₀ (mt norm_eq_zero.mp h)] /-- Corollary of Hahn-Banach. Given a nonzero element `x` of a normed space, there exists an element of the dual space, of norm `1`, whose value on `x` is `‖x‖`. -/ theorem exists_dual_vector (x : E) (h : x ≠ 0) : ∃ g : E →L[𝕜] 𝕜, ‖g‖ = 1 ∧ g x = ‖x‖ := by let p : Submodule 𝕜 E := 𝕜 ∙ x let f := (‖x‖ : 𝕜) • coord 𝕜 x h obtain ⟨g, hg⟩ := exists_extension_norm_eq p f refine ⟨g, ?_, ?_⟩ · rw [hg.2, coord_norm'] · calc g x = g (⟨x, mem_span_singleton_self x⟩ : 𝕜 ∙ x) := by rw [coe_mk] _ = ((‖x‖ : 𝕜) • coord 𝕜 x h) (⟨x, mem_span_singleton_self x⟩ : 𝕜 ∙ x) := by rw [← hg.1] _ = ‖x‖ := by simp [-algebraMap_smul] /-- Variant of Hahn-Banach, eliminating the hypothesis that `x` be nonzero, and choosing the dual element arbitrarily when `x = 0`. -/ theorem exists_dual_vector' [Nontrivial E] (x : E) : ∃ g : E →L[𝕜] 𝕜, ‖g‖ = 1 ∧ g x = ‖x‖ := by by_cases hx : x = 0 · obtain ⟨y, hy⟩ := exists_ne (0 : E) obtain ⟨g, hg⟩ : ∃ g : E →L[𝕜] 𝕜, ‖g‖ = 1 ∧ g y = ‖y‖ := exists_dual_vector 𝕜 y hy refine ⟨g, hg.left, ?_⟩ simp [hx] · exact exists_dual_vector 𝕜 x hx /-- Variant of Hahn-Banach, eliminating the hypothesis that `x` be nonzero, but only ensuring that the dual element has norm at most `1` (this can not be improved for the trivial vector space). -/ theorem exists_dual_vector'' (x : E) : ∃ g : E →L[𝕜] 𝕜, ‖g‖ ≤ 1 ∧ g x = ‖x‖ := by by_cases hx : x = 0 · refine ⟨0, by simp, ?_⟩ symm simp [hx] · rcases exists_dual_vector 𝕜 x hx with ⟨g, g_norm, g_eq⟩ exact ⟨g, g_norm.le, g_eq⟩ end DualVector
Mathlib/Analysis/NormedSpace/HahnBanach/Extension.lean
194
200
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot, Yury Kudryashov, Rémy Degenne -/ import Mathlib.Data.Set.Subsingleton import Mathlib.Order.Interval.Set.Defs /-! # Intervals In any preorder, we define intervals (which on each side can be either infinite, open or closed) using the following naming conventions: - `i`: infinite - `o`: open - `c`: closed Each interval has the name `I` + letter for left side + letter for right side. For instance, `Ioc a b` denotes the interval `(a, b]`. The definitions can be found in `Mathlib.Order.Interval.Set.Defs`. This file contains basic facts on inclusion of and set operations on intervals (where the precise statements depend on the order's properties; statements requiring `LinearOrder` are in `Mathlib.Order.Interval.Set.LinearOrder`). TODO: This is just the beginning; a lot of rules are missing -/ assert_not_exists RelIso open Function open OrderDual (toDual ofDual) variable {α : Type*} namespace Set section Preorder variable [Preorder α] {a a₁ a₂ b b₁ b₂ c x : α} instance decidableMemIoo [Decidable (a < x ∧ x < b)] : Decidable (x ∈ Ioo a b) := by assumption instance decidableMemIco [Decidable (a ≤ x ∧ x < b)] : Decidable (x ∈ Ico a b) := by assumption instance decidableMemIio [Decidable (x < b)] : Decidable (x ∈ Iio b) := by assumption instance decidableMemIcc [Decidable (a ≤ x ∧ x ≤ b)] : Decidable (x ∈ Icc a b) := by assumption instance decidableMemIic [Decidable (x ≤ b)] : Decidable (x ∈ Iic b) := by assumption instance decidableMemIoc [Decidable (a < x ∧ x ≤ b)] : Decidable (x ∈ Ioc a b) := by assumption instance decidableMemIci [Decidable (a ≤ x)] : Decidable (x ∈ Ici a) := by assumption instance decidableMemIoi [Decidable (a < x)] : Decidable (x ∈ Ioi a) := by assumption theorem left_mem_Ioo : a ∈ Ioo a b ↔ False := by simp [lt_irrefl] theorem left_mem_Ico : a ∈ Ico a b ↔ a < b := by simp [le_refl] theorem left_mem_Icc : a ∈ Icc a b ↔ a ≤ b := by simp [le_refl] theorem left_mem_Ioc : a ∈ Ioc a b ↔ False := by simp [lt_irrefl] theorem left_mem_Ici : a ∈ Ici a := by simp theorem right_mem_Ioo : b ∈ Ioo a b ↔ False := by simp [lt_irrefl] theorem right_mem_Ico : b ∈ Ico a b ↔ False := by simp [lt_irrefl] theorem right_mem_Icc : b ∈ Icc a b ↔ a ≤ b := by simp [le_refl] theorem right_mem_Ioc : b ∈ Ioc a b ↔ a < b := by simp [le_refl] theorem right_mem_Iic : a ∈ Iic a := by simp @[simp] theorem Ici_toDual : Ici (toDual a) = ofDual ⁻¹' Iic a := rfl @[deprecated (since := "2025-03-20")] alias dual_Ici := Ici_toDual @[simp] theorem Iic_toDual : Iic (toDual a) = ofDual ⁻¹' Ici a := rfl @[deprecated (since := "2025-03-20")] alias dual_Iic := Iic_toDual @[simp] theorem Ioi_toDual : Ioi (toDual a) = ofDual ⁻¹' Iio a := rfl @[deprecated (since := "2025-03-20")] alias dual_Ioi := Ioi_toDual @[simp] theorem Iio_toDual : Iio (toDual a) = ofDual ⁻¹' Ioi a := rfl @[deprecated (since := "2025-03-20")] alias dual_Iio := Iio_toDual @[simp] theorem Icc_toDual : Icc (toDual a) (toDual b) = ofDual ⁻¹' Icc b a := Set.ext fun _ => and_comm @[deprecated (since := "2025-03-20")] alias dual_Icc := Icc_toDual @[simp] theorem Ioc_toDual : Ioc (toDual a) (toDual b) = ofDual ⁻¹' Ico b a := Set.ext fun _ => and_comm @[deprecated (since := "2025-03-20")] alias dual_Ioc := Ioc_toDual @[simp] theorem Ico_toDual : Ico (toDual a) (toDual b) = ofDual ⁻¹' Ioc b a := Set.ext fun _ => and_comm @[deprecated (since := "2025-03-20")] alias dual_Ico := Ico_toDual @[simp] theorem Ioo_toDual : Ioo (toDual a) (toDual b) = ofDual ⁻¹' Ioo b a := Set.ext fun _ => and_comm @[deprecated (since := "2025-03-20")] alias dual_Ioo := Ioo_toDual @[simp] theorem Ici_ofDual {x : αᵒᵈ} : Ici (ofDual x) = toDual ⁻¹' Iic x := rfl @[simp] theorem Iic_ofDual {x : αᵒᵈ} : Iic (ofDual x) = toDual ⁻¹' Ici x := rfl @[simp] theorem Ioi_ofDual {x : αᵒᵈ} : Ioi (ofDual x) = toDual ⁻¹' Iio x := rfl @[simp] theorem Iio_ofDual {x : αᵒᵈ} : Iio (ofDual x) = toDual ⁻¹' Ioi x := rfl @[simp] theorem Icc_ofDual {x y : αᵒᵈ} : Icc (ofDual y) (ofDual x) = toDual ⁻¹' Icc x y := Set.ext fun _ => and_comm @[simp] theorem Ico_ofDual {x y : αᵒᵈ} : Ico (ofDual y) (ofDual x) = toDual ⁻¹' Ioc x y := Set.ext fun _ => and_comm @[simp] theorem Ioc_ofDual {x y : αᵒᵈ} : Ioc (ofDual y) (ofDual x) = toDual ⁻¹' Ico x y := Set.ext fun _ => and_comm @[simp] theorem Ioo_ofDual {x y : αᵒᵈ} : Ioo (ofDual y) (ofDual x) = toDual ⁻¹' Ioo x y := Set.ext fun _ => and_comm @[simp] theorem nonempty_Icc : (Icc a b).Nonempty ↔ a ≤ b := ⟨fun ⟨_, hx⟩ => hx.1.trans hx.2, fun h => ⟨a, left_mem_Icc.2 h⟩⟩ @[simp] theorem nonempty_Ico : (Ico a b).Nonempty ↔ a < b := ⟨fun ⟨_, hx⟩ => hx.1.trans_lt hx.2, fun h => ⟨a, left_mem_Ico.2 h⟩⟩ @[simp] theorem nonempty_Ioc : (Ioc a b).Nonempty ↔ a < b := ⟨fun ⟨_, hx⟩ => hx.1.trans_le hx.2, fun h => ⟨b, right_mem_Ioc.2 h⟩⟩ @[simp] theorem nonempty_Ici : (Ici a).Nonempty := ⟨a, left_mem_Ici⟩ @[simp] theorem nonempty_Iic : (Iic a).Nonempty := ⟨a, right_mem_Iic⟩ @[simp] theorem nonempty_Ioo [DenselyOrdered α] : (Ioo a b).Nonempty ↔ a < b := ⟨fun ⟨_, ha, hb⟩ => ha.trans hb, exists_between⟩ @[simp] theorem nonempty_Ioi [NoMaxOrder α] : (Ioi a).Nonempty := exists_gt a @[simp] theorem nonempty_Iio [NoMinOrder α] : (Iio a).Nonempty := exists_lt a theorem nonempty_Icc_subtype (h : a ≤ b) : Nonempty (Icc a b) := Nonempty.to_subtype (nonempty_Icc.mpr h) theorem nonempty_Ico_subtype (h : a < b) : Nonempty (Ico a b) := Nonempty.to_subtype (nonempty_Ico.mpr h) theorem nonempty_Ioc_subtype (h : a < b) : Nonempty (Ioc a b) := Nonempty.to_subtype (nonempty_Ioc.mpr h) /-- An interval `Ici a` is nonempty. -/ instance nonempty_Ici_subtype : Nonempty (Ici a) := Nonempty.to_subtype nonempty_Ici /-- An interval `Iic a` is nonempty. -/ instance nonempty_Iic_subtype : Nonempty (Iic a) := Nonempty.to_subtype nonempty_Iic theorem nonempty_Ioo_subtype [DenselyOrdered α] (h : a < b) : Nonempty (Ioo a b) := Nonempty.to_subtype (nonempty_Ioo.mpr h) /-- In an order without maximal elements, the intervals `Ioi` are nonempty. -/ instance nonempty_Ioi_subtype [NoMaxOrder α] : Nonempty (Ioi a) := Nonempty.to_subtype nonempty_Ioi /-- In an order without minimal elements, the intervals `Iio` are nonempty. -/ instance nonempty_Iio_subtype [NoMinOrder α] : Nonempty (Iio a) := Nonempty.to_subtype nonempty_Iio instance [NoMinOrder α] : NoMinOrder (Iio a) := ⟨fun a => let ⟨b, hb⟩ := exists_lt (a : α) ⟨⟨b, lt_trans hb a.2⟩, hb⟩⟩ instance [NoMinOrder α] : NoMinOrder (Iic a) := ⟨fun a => let ⟨b, hb⟩ := exists_lt (a : α) ⟨⟨b, hb.le.trans a.2⟩, hb⟩⟩ instance [NoMaxOrder α] : NoMaxOrder (Ioi a) := OrderDual.noMaxOrder (α := Iio (toDual a)) instance [NoMaxOrder α] : NoMaxOrder (Ici a) := OrderDual.noMaxOrder (α := Iic (toDual a)) @[simp] theorem Icc_eq_empty (h : ¬a ≤ b) : Icc a b = ∅ := eq_empty_iff_forall_not_mem.2 fun _ ⟨ha, hb⟩ => h (ha.trans hb) @[simp] theorem Ico_eq_empty (h : ¬a < b) : Ico a b = ∅ := eq_empty_iff_forall_not_mem.2 fun _ ⟨ha, hb⟩ => h (ha.trans_lt hb) @[simp] theorem Ioc_eq_empty (h : ¬a < b) : Ioc a b = ∅ := eq_empty_iff_forall_not_mem.2 fun _ ⟨ha, hb⟩ => h (ha.trans_le hb) @[simp] theorem Ioo_eq_empty (h : ¬a < b) : Ioo a b = ∅ := eq_empty_iff_forall_not_mem.2 fun _ ⟨ha, hb⟩ => h (ha.trans hb) @[simp] theorem Icc_eq_empty_of_lt (h : b < a) : Icc a b = ∅ := Icc_eq_empty h.not_le @[simp] theorem Ico_eq_empty_of_le (h : b ≤ a) : Ico a b = ∅ := Ico_eq_empty h.not_lt @[simp] theorem Ioc_eq_empty_of_le (h : b ≤ a) : Ioc a b = ∅ := Ioc_eq_empty h.not_lt @[simp] theorem Ioo_eq_empty_of_le (h : b ≤ a) : Ioo a b = ∅ := Ioo_eq_empty h.not_lt theorem Ico_self (a : α) : Ico a a = ∅ := Ico_eq_empty <| lt_irrefl _ theorem Ioc_self (a : α) : Ioc a a = ∅ := Ioc_eq_empty <| lt_irrefl _ theorem Ioo_self (a : α) : Ioo a a = ∅ := Ioo_eq_empty <| lt_irrefl _ @[simp] theorem Ici_subset_Ici : Ici a ⊆ Ici b ↔ b ≤ a := ⟨fun h => h <| left_mem_Ici, fun h _ hx => h.trans hx⟩ @[gcongr] alias ⟨_, _root_.GCongr.Ici_subset_Ici_of_le⟩ := Ici_subset_Ici @[simp] theorem Ici_ssubset_Ici : Ici a ⊂ Ici b ↔ b < a where mp h := by obtain ⟨ab, c, cb, ac⟩ := ssubset_iff_exists.mp h exact lt_of_le_not_le (Ici_subset_Ici.mp ab) (fun h' ↦ ac (h'.trans cb)) mpr h := (ssubset_iff_of_subset (Ici_subset_Ici.mpr h.le)).mpr ⟨b, right_mem_Iic, fun h' => h.not_le h'⟩ @[gcongr] alias ⟨_, _root_.GCongr.Ici_ssubset_Ici_of_le⟩ := Ici_ssubset_Ici @[simp] theorem Iic_subset_Iic : Iic a ⊆ Iic b ↔ a ≤ b := @Ici_subset_Ici αᵒᵈ _ _ _ @[gcongr] alias ⟨_, _root_.GCongr.Iic_subset_Iic_of_le⟩ := Iic_subset_Iic @[simp] theorem Iic_ssubset_Iic : Iic a ⊂ Iic b ↔ a < b := @Ici_ssubset_Ici αᵒᵈ _ _ _ @[gcongr] alias ⟨_, _root_.GCongr.Iic_ssubset_Iic_of_le⟩ := Iic_ssubset_Iic @[simp] theorem Ici_subset_Ioi : Ici a ⊆ Ioi b ↔ b < a := ⟨fun h => h left_mem_Ici, fun h _ hx => h.trans_le hx⟩ @[simp] theorem Iic_subset_Iio : Iic a ⊆ Iio b ↔ a < b := ⟨fun h => h right_mem_Iic, fun h _ hx => lt_of_le_of_lt hx h⟩ @[gcongr] theorem Ioo_subset_Ioo (h₁ : a₂ ≤ a₁) (h₂ : b₁ ≤ b₂) : Ioo a₁ b₁ ⊆ Ioo a₂ b₂ := fun _ ⟨hx₁, hx₂⟩ => ⟨h₁.trans_lt hx₁, hx₂.trans_le h₂⟩ @[gcongr] theorem Ioo_subset_Ioo_left (h : a₁ ≤ a₂) : Ioo a₂ b ⊆ Ioo a₁ b := Ioo_subset_Ioo h le_rfl @[gcongr] theorem Ioo_subset_Ioo_right (h : b₁ ≤ b₂) : Ioo a b₁ ⊆ Ioo a b₂ := Ioo_subset_Ioo le_rfl h @[gcongr] theorem Ico_subset_Ico (h₁ : a₂ ≤ a₁) (h₂ : b₁ ≤ b₂) : Ico a₁ b₁ ⊆ Ico a₂ b₂ := fun _ ⟨hx₁, hx₂⟩ => ⟨h₁.trans hx₁, hx₂.trans_le h₂⟩ @[gcongr] theorem Ico_subset_Ico_left (h : a₁ ≤ a₂) : Ico a₂ b ⊆ Ico a₁ b := Ico_subset_Ico h le_rfl @[gcongr] theorem Ico_subset_Ico_right (h : b₁ ≤ b₂) : Ico a b₁ ⊆ Ico a b₂ := Ico_subset_Ico le_rfl h @[gcongr] theorem Icc_subset_Icc (h₁ : a₂ ≤ a₁) (h₂ : b₁ ≤ b₂) : Icc a₁ b₁ ⊆ Icc a₂ b₂ := fun _ ⟨hx₁, hx₂⟩ => ⟨h₁.trans hx₁, le_trans hx₂ h₂⟩ @[gcongr] theorem Icc_subset_Icc_left (h : a₁ ≤ a₂) : Icc a₂ b ⊆ Icc a₁ b := Icc_subset_Icc h le_rfl @[gcongr] theorem Icc_subset_Icc_right (h : b₁ ≤ b₂) : Icc a b₁ ⊆ Icc a b₂ := Icc_subset_Icc le_rfl h theorem Icc_subset_Ioo (ha : a₂ < a₁) (hb : b₁ < b₂) : Icc a₁ b₁ ⊆ Ioo a₂ b₂ := fun _ hx => ⟨ha.trans_le hx.1, hx.2.trans_lt hb⟩ theorem Icc_subset_Ici_self : Icc a b ⊆ Ici a := fun _ => And.left theorem Icc_subset_Iic_self : Icc a b ⊆ Iic b := fun _ => And.right theorem Ioc_subset_Iic_self : Ioc a b ⊆ Iic b := fun _ => And.right @[gcongr] theorem Ioc_subset_Ioc (h₁ : a₂ ≤ a₁) (h₂ : b₁ ≤ b₂) : Ioc a₁ b₁ ⊆ Ioc a₂ b₂ := fun _ ⟨hx₁, hx₂⟩ => ⟨h₁.trans_lt hx₁, hx₂.trans h₂⟩ @[gcongr] theorem Ioc_subset_Ioc_left (h : a₁ ≤ a₂) : Ioc a₂ b ⊆ Ioc a₁ b := Ioc_subset_Ioc h le_rfl @[gcongr] theorem Ioc_subset_Ioc_right (h : b₁ ≤ b₂) : Ioc a b₁ ⊆ Ioc a b₂ := Ioc_subset_Ioc le_rfl h theorem Ico_subset_Ioo_left (h₁ : a₁ < a₂) : Ico a₂ b ⊆ Ioo a₁ b := fun _ => And.imp_left h₁.trans_le theorem Ioc_subset_Ioo_right (h : b₁ < b₂) : Ioc a b₁ ⊆ Ioo a b₂ := fun _ => And.imp_right fun h' => h'.trans_lt h theorem Icc_subset_Ico_right (h₁ : b₁ < b₂) : Icc a b₁ ⊆ Ico a b₂ := fun _ => And.imp_right fun h₂ => h₂.trans_lt h₁ theorem Ioo_subset_Ico_self : Ioo a b ⊆ Ico a b := fun _ => And.imp_left le_of_lt theorem Ioo_subset_Ioc_self : Ioo a b ⊆ Ioc a b := fun _ => And.imp_right le_of_lt theorem Ico_subset_Icc_self : Ico a b ⊆ Icc a b := fun _ => And.imp_right le_of_lt theorem Ioc_subset_Icc_self : Ioc a b ⊆ Icc a b := fun _ => And.imp_left le_of_lt theorem Ioo_subset_Icc_self : Ioo a b ⊆ Icc a b := Subset.trans Ioo_subset_Ico_self Ico_subset_Icc_self theorem Ico_subset_Iio_self : Ico a b ⊆ Iio b := fun _ => And.right theorem Ioo_subset_Iio_self : Ioo a b ⊆ Iio b := fun _ => And.right theorem Ioc_subset_Ioi_self : Ioc a b ⊆ Ioi a := fun _ => And.left theorem Ioo_subset_Ioi_self : Ioo a b ⊆ Ioi a := fun _ => And.left theorem Ioi_subset_Ici_self : Ioi a ⊆ Ici a := fun _ hx => le_of_lt hx theorem Iio_subset_Iic_self : Iio a ⊆ Iic a := fun _ hx => le_of_lt hx theorem Ico_subset_Ici_self : Ico a b ⊆ Ici a := fun _ => And.left theorem Ioi_ssubset_Ici_self : Ioi a ⊂ Ici a := ⟨Ioi_subset_Ici_self, fun h => lt_irrefl a (h le_rfl)⟩ theorem Iio_ssubset_Iic_self : Iio a ⊂ Iic a := @Ioi_ssubset_Ici_self αᵒᵈ _ _ theorem Icc_subset_Icc_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Icc a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ ≤ b₂ := ⟨fun h => ⟨(h ⟨le_rfl, h₁⟩).1, (h ⟨h₁, le_rfl⟩).2⟩, fun ⟨h, h'⟩ _ ⟨hx, hx'⟩ => ⟨h.trans hx, hx'.trans h'⟩⟩ theorem Icc_subset_Ioo_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ioo a₂ b₂ ↔ a₂ < a₁ ∧ b₁ < b₂ := ⟨fun h => ⟨(h ⟨le_rfl, h₁⟩).1, (h ⟨h₁, le_rfl⟩).2⟩, fun ⟨h, h'⟩ _ ⟨hx, hx'⟩ => ⟨h.trans_le hx, hx'.trans_lt h'⟩⟩ theorem Icc_subset_Ico_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ico a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ < b₂ := ⟨fun h => ⟨(h ⟨le_rfl, h₁⟩).1, (h ⟨h₁, le_rfl⟩).2⟩, fun ⟨h, h'⟩ _ ⟨hx, hx'⟩ => ⟨h.trans hx, hx'.trans_lt h'⟩⟩ theorem Icc_subset_Ioc_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ioc a₂ b₂ ↔ a₂ < a₁ ∧ b₁ ≤ b₂ := ⟨fun h => ⟨(h ⟨le_rfl, h₁⟩).1, (h ⟨h₁, le_rfl⟩).2⟩, fun ⟨h, h'⟩ _ ⟨hx, hx'⟩ => ⟨h.trans_le hx, hx'.trans h'⟩⟩ theorem Icc_subset_Iio_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Iio b₂ ↔ b₁ < b₂ := ⟨fun h => h ⟨h₁, le_rfl⟩, fun h _ ⟨_, hx'⟩ => hx'.trans_lt h⟩ theorem Icc_subset_Ioi_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ioi a₂ ↔ a₂ < a₁ := ⟨fun h => h ⟨le_rfl, h₁⟩, fun h _ ⟨hx, _⟩ => h.trans_le hx⟩ theorem Icc_subset_Iic_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Iic b₂ ↔ b₁ ≤ b₂ := ⟨fun h => h ⟨h₁, le_rfl⟩, fun h _ ⟨_, hx'⟩ => hx'.trans h⟩ theorem Icc_subset_Ici_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ici a₂ ↔ a₂ ≤ a₁ := ⟨fun h => h ⟨le_rfl, h₁⟩, fun h _ ⟨hx, _⟩ => h.trans hx⟩ theorem Icc_ssubset_Icc_left (hI : a₂ ≤ b₂) (ha : a₂ < a₁) (hb : b₁ ≤ b₂) : Icc a₁ b₁ ⊂ Icc a₂ b₂ := (ssubset_iff_of_subset (Icc_subset_Icc (le_of_lt ha) hb)).mpr ⟨a₂, left_mem_Icc.mpr hI, not_and.mpr fun f _ => lt_irrefl a₂ (ha.trans_le f)⟩ theorem Icc_ssubset_Icc_right (hI : a₂ ≤ b₂) (ha : a₂ ≤ a₁) (hb : b₁ < b₂) : Icc a₁ b₁ ⊂ Icc a₂ b₂ := (ssubset_iff_of_subset (Icc_subset_Icc ha (le_of_lt hb))).mpr ⟨b₂, right_mem_Icc.mpr hI, fun f => lt_irrefl b₁ (hb.trans_le f.2)⟩ /-- If `a ≤ b`, then `(b, +∞) ⊆ (a, +∞)`. In preorders, this is just an implication. If you need the equivalence in linear orders, use `Ioi_subset_Ioi_iff`. -/ @[gcongr] theorem Ioi_subset_Ioi (h : a ≤ b) : Ioi b ⊆ Ioi a := fun _ hx => h.trans_lt hx /-- If `a < b`, then `(b, +∞) ⊂ (a, +∞)`. In preorders, this is just an implication. If you need the equivalence in linear orders, use `Ioi_ssubset_Ioi_iff`. -/ @[gcongr] theorem Ioi_ssubset_Ioi (h : a < b) : Ioi b ⊂ Ioi a := (ssubset_iff_of_subset (Ioi_subset_Ioi h.le)).mpr ⟨b, h, lt_irrefl b⟩ /-- If `a ≤ b`, then `(b, +∞) ⊆ [a, +∞)`. In preorders, this is just an implication. If you need the equivalence in dense linear orders, use `Ioi_subset_Ici_iff`. -/ theorem Ioi_subset_Ici (h : a ≤ b) : Ioi b ⊆ Ici a := Subset.trans (Ioi_subset_Ioi h) Ioi_subset_Ici_self /-- If `a ≤ b`, then `(-∞, a) ⊆ (-∞, b)`. In preorders, this is just an implication. If you need the equivalence in linear orders, use `Iio_subset_Iio_iff`. -/ @[gcongr] theorem Iio_subset_Iio (h : a ≤ b) : Iio a ⊆ Iio b := fun _ hx => lt_of_lt_of_le hx h /-- If `a < b`, then `(-∞, a) ⊂ (-∞, b)`. In preorders, this is just an implication. If you need the equivalence in linear orders, use `Iio_ssubset_Iio_iff`. -/ @[gcongr] theorem Iio_ssubset_Iio (h : a < b) : Iio a ⊂ Iio b := (ssubset_iff_of_subset (Iio_subset_Iio h.le)).mpr ⟨a, h, lt_irrefl a⟩ /-- If `a ≤ b`, then `(-∞, a) ⊆ (-∞, b]`. In preorders, this is just an implication. If you need the equivalence in dense linear orders, use `Iio_subset_Iic_iff`. -/ theorem Iio_subset_Iic (h : a ≤ b) : Iio a ⊆ Iic b := Subset.trans (Iio_subset_Iio h) Iio_subset_Iic_self theorem Ici_inter_Iic : Ici a ∩ Iic b = Icc a b := rfl theorem Ici_inter_Iio : Ici a ∩ Iio b = Ico a b := rfl theorem Ioi_inter_Iic : Ioi a ∩ Iic b = Ioc a b := rfl theorem Ioi_inter_Iio : Ioi a ∩ Iio b = Ioo a b := rfl theorem Iic_inter_Ici : Iic a ∩ Ici b = Icc b a := inter_comm _ _ theorem Iio_inter_Ici : Iio a ∩ Ici b = Ico b a := inter_comm _ _ theorem Iic_inter_Ioi : Iic a ∩ Ioi b = Ioc b a := inter_comm _ _ theorem Iio_inter_Ioi : Iio a ∩ Ioi b = Ioo b a := inter_comm _ _ theorem mem_Icc_of_Ioo (h : x ∈ Ioo a b) : x ∈ Icc a b := Ioo_subset_Icc_self h theorem mem_Ico_of_Ioo (h : x ∈ Ioo a b) : x ∈ Ico a b := Ioo_subset_Ico_self h theorem mem_Ioc_of_Ioo (h : x ∈ Ioo a b) : x ∈ Ioc a b := Ioo_subset_Ioc_self h theorem mem_Icc_of_Ico (h : x ∈ Ico a b) : x ∈ Icc a b := Ico_subset_Icc_self h theorem mem_Icc_of_Ioc (h : x ∈ Ioc a b) : x ∈ Icc a b := Ioc_subset_Icc_self h theorem mem_Ici_of_Ioi (h : x ∈ Ioi a) : x ∈ Ici a := Ioi_subset_Ici_self h theorem mem_Iic_of_Iio (h : x ∈ Iio a) : x ∈ Iic a := Iio_subset_Iic_self h theorem Icc_eq_empty_iff : Icc a b = ∅ ↔ ¬a ≤ b := by rw [← not_nonempty_iff_eq_empty, not_iff_not, nonempty_Icc] theorem Ico_eq_empty_iff : Ico a b = ∅ ↔ ¬a < b := by rw [← not_nonempty_iff_eq_empty, not_iff_not, nonempty_Ico] theorem Ioc_eq_empty_iff : Ioc a b = ∅ ↔ ¬a < b := by rw [← not_nonempty_iff_eq_empty, not_iff_not, nonempty_Ioc] theorem Ioo_eq_empty_iff [DenselyOrdered α] : Ioo a b = ∅ ↔ ¬a < b := by rw [← not_nonempty_iff_eq_empty, not_iff_not, nonempty_Ioo] theorem _root_.IsTop.Iic_eq (h : IsTop a) : Iic a = univ := eq_univ_of_forall h theorem _root_.IsBot.Ici_eq (h : IsBot a) : Ici a = univ := eq_univ_of_forall h @[simp] theorem Ioi_eq_empty_iff : Ioi a = ∅ ↔ IsMax a := by simp only [isMax_iff_forall_not_lt, eq_empty_iff_forall_not_mem, mem_Ioi] @[simp] theorem Iio_eq_empty_iff : Iio a = ∅ ↔ IsMin a := Ioi_eq_empty_iff (α := αᵒᵈ) @[simp] alias ⟨_, _root_.IsMax.Ioi_eq⟩ := Ioi_eq_empty_iff @[simp] alias ⟨_, _root_.IsMin.Iio_eq⟩ := Iio_eq_empty_iff @[simp] lemma Iio_nonempty : (Iio a).Nonempty ↔ ¬ IsMin a := by simp [nonempty_iff_ne_empty] @[simp] lemma Ioi_nonempty : (Ioi a).Nonempty ↔ ¬ IsMax a := by simp [nonempty_iff_ne_empty] theorem Iic_inter_Ioc_of_le (h : a ≤ c) : Iic a ∩ Ioc b c = Ioc b a := ext fun _ => ⟨fun H => ⟨H.2.1, H.1⟩, fun H => ⟨H.2, H.1, H.2.trans h⟩⟩ theorem not_mem_Icc_of_lt (ha : c < a) : c ∉ Icc a b := fun h => ha.not_le h.1 theorem not_mem_Icc_of_gt (hb : b < c) : c ∉ Icc a b := fun h => hb.not_le h.2 theorem not_mem_Ico_of_lt (ha : c < a) : c ∉ Ico a b := fun h => ha.not_le h.1 theorem not_mem_Ioc_of_gt (hb : b < c) : c ∉ Ioc a b := fun h => hb.not_le h.2 theorem not_mem_Ioi_self : a ∉ Ioi a := lt_irrefl _ theorem not_mem_Iio_self : b ∉ Iio b := lt_irrefl _ theorem not_mem_Ioc_of_le (ha : c ≤ a) : c ∉ Ioc a b := fun h => lt_irrefl _ <| h.1.trans_le ha theorem not_mem_Ico_of_ge (hb : b ≤ c) : c ∉ Ico a b := fun h => lt_irrefl _ <| h.2.trans_le hb theorem not_mem_Ioo_of_le (ha : c ≤ a) : c ∉ Ioo a b := fun h => lt_irrefl _ <| h.1.trans_le ha theorem not_mem_Ioo_of_ge (hb : b ≤ c) : c ∉ Ioo a b := fun h => lt_irrefl _ <| h.2.trans_le hb section matched_intervals @[simp] theorem Icc_eq_Ioc_same_iff : Icc a b = Ioc a b ↔ ¬a ≤ b where mp h := by simpa using Set.ext_iff.mp h a mpr h := by rw [Icc_eq_empty h, Ioc_eq_empty (mt le_of_lt h)] @[simp] theorem Icc_eq_Ico_same_iff : Icc a b = Ico a b ↔ ¬a ≤ b where mp h := by simpa using Set.ext_iff.mp h b mpr h := by rw [Icc_eq_empty h, Ico_eq_empty (mt le_of_lt h)] @[simp] theorem Icc_eq_Ioo_same_iff : Icc a b = Ioo a b ↔ ¬a ≤ b where mp h := by simpa using Set.ext_iff.mp h b mpr h := by rw [Icc_eq_empty h, Ioo_eq_empty (mt le_of_lt h)] @[simp] theorem Ioc_eq_Ico_same_iff : Ioc a b = Ico a b ↔ ¬a < b where mp h := by simpa using Set.ext_iff.mp h a mpr h := by rw [Ioc_eq_empty h, Ico_eq_empty h] @[simp] theorem Ioo_eq_Ioc_same_iff : Ioo a b = Ioc a b ↔ ¬a < b where mp h := by simpa using Set.ext_iff.mp h b mpr h := by rw [Ioo_eq_empty h, Ioc_eq_empty h] @[simp] theorem Ioo_eq_Ico_same_iff : Ioo a b = Ico a b ↔ ¬a < b where mp h := by simpa using Set.ext_iff.mp h a mpr h := by rw [Ioo_eq_empty h, Ico_eq_empty h] -- Mirrored versions of the above for `simp`. @[simp] theorem Ioc_eq_Icc_same_iff : Ioc a b = Icc a b ↔ ¬a ≤ b := eq_comm.trans Icc_eq_Ioc_same_iff @[simp] theorem Ico_eq_Icc_same_iff : Ico a b = Icc a b ↔ ¬a ≤ b := eq_comm.trans Icc_eq_Ico_same_iff @[simp] theorem Ioo_eq_Icc_same_iff : Ioo a b = Icc a b ↔ ¬a ≤ b := eq_comm.trans Icc_eq_Ioo_same_iff @[simp] theorem Ico_eq_Ioc_same_iff : Ico a b = Ioc a b ↔ ¬a < b := eq_comm.trans Ioc_eq_Ico_same_iff @[simp] theorem Ioc_eq_Ioo_same_iff : Ioc a b = Ioo a b ↔ ¬a < b := eq_comm.trans Ioo_eq_Ioc_same_iff @[simp] theorem Ico_eq_Ioo_same_iff : Ico a b = Ioo a b ↔ ¬a < b := eq_comm.trans Ioo_eq_Ico_same_iff end matched_intervals end Preorder section PartialOrder variable [PartialOrder α] {a b c : α} @[simp] theorem Icc_self (a : α) : Icc a a = {a} := Set.ext <| by simp [Icc, le_antisymm_iff, and_comm] instance instIccUnique : Unique (Set.Icc a a) where default := ⟨a, by simp⟩ uniq y := Subtype.ext <| by simpa using y.2 @[simp] theorem Icc_eq_singleton_iff : Icc a b = {c} ↔ a = c ∧ b = c := by refine ⟨fun h => ?_, ?_⟩ · have hab : a ≤ b := nonempty_Icc.1 (h.symm.subst <| singleton_nonempty c) exact ⟨eq_of_mem_singleton <| h ▸ left_mem_Icc.2 hab, eq_of_mem_singleton <| h ▸ right_mem_Icc.2 hab⟩ · rintro ⟨rfl, rfl⟩ exact Icc_self _ lemma subsingleton_Icc_of_ge (hba : b ≤ a) : Set.Subsingleton (Icc a b) := fun _x ⟨hax, hxb⟩ _y ⟨hay, hyb⟩ ↦ le_antisymm (le_implies_le_of_le_of_le hxb hay hba) (le_implies_le_of_le_of_le hyb hax hba) @[simp] lemma subsingleton_Icc_iff {α : Type*} [LinearOrder α] {a b : α} : Set.Subsingleton (Icc a b) ↔ b ≤ a := by refine ⟨fun h ↦ ?_, subsingleton_Icc_of_ge⟩ contrapose! h simp only [gt_iff_lt, not_subsingleton_iff] exact ⟨a, ⟨le_refl _, h.le⟩, b, ⟨h.le, le_refl _⟩, h.ne⟩ @[simp] theorem Icc_diff_left : Icc a b \ {a} = Ioc a b := ext fun x => by simp [lt_iff_le_and_ne, eq_comm, and_right_comm] @[simp] theorem Icc_diff_right : Icc a b \ {b} = Ico a b := ext fun x => by simp [lt_iff_le_and_ne, and_assoc] @[simp] theorem Ico_diff_left : Ico a b \ {a} = Ioo a b := ext fun x => by simp [and_right_comm, ← lt_iff_le_and_ne, eq_comm] @[simp] theorem Ioc_diff_right : Ioc a b \ {b} = Ioo a b := ext fun x => by simp [and_assoc, ← lt_iff_le_and_ne] @[simp] theorem Icc_diff_both : Icc a b \ {a, b} = Ioo a b := by rw [insert_eq, ← diff_diff, Icc_diff_left, Ioc_diff_right] @[simp] theorem Ici_diff_left : Ici a \ {a} = Ioi a := ext fun x => by simp [lt_iff_le_and_ne, eq_comm] @[simp] theorem Iic_diff_right : Iic a \ {a} = Iio a := ext fun x => by simp [lt_iff_le_and_ne] @[simp] theorem Ico_diff_Ioo_same (h : a < b) : Ico a b \ Ioo a b = {a} := by rw [← Ico_diff_left, diff_diff_cancel_left (singleton_subset_iff.2 <| left_mem_Ico.2 h)] @[simp] theorem Ioc_diff_Ioo_same (h : a < b) : Ioc a b \ Ioo a b = {b} := by rw [← Ioc_diff_right, diff_diff_cancel_left (singleton_subset_iff.2 <| right_mem_Ioc.2 h)] @[simp] theorem Icc_diff_Ico_same (h : a ≤ b) : Icc a b \ Ico a b = {b} := by rw [← Icc_diff_right, diff_diff_cancel_left (singleton_subset_iff.2 <| right_mem_Icc.2 h)] @[simp] theorem Icc_diff_Ioc_same (h : a ≤ b) : Icc a b \ Ioc a b = {a} := by rw [← Icc_diff_left, diff_diff_cancel_left (singleton_subset_iff.2 <| left_mem_Icc.2 h)] @[simp] theorem Icc_diff_Ioo_same (h : a ≤ b) : Icc a b \ Ioo a b = {a, b} := by rw [← Icc_diff_both, diff_diff_cancel_left] simp [insert_subset_iff, h] @[simp] theorem Ici_diff_Ioi_same : Ici a \ Ioi a = {a} := by rw [← Ici_diff_left, diff_diff_cancel_left (singleton_subset_iff.2 left_mem_Ici)] @[simp] theorem Iic_diff_Iio_same : Iic a \ Iio a = {a} := by rw [← Iic_diff_right, diff_diff_cancel_left (singleton_subset_iff.2 right_mem_Iic)] theorem Ioi_union_left : Ioi a ∪ {a} = Ici a := ext fun x => by simp [eq_comm, le_iff_eq_or_lt] theorem Iio_union_right : Iio a ∪ {a} = Iic a := ext fun _ => le_iff_lt_or_eq.symm theorem Ioo_union_left (hab : a < b) : Ioo a b ∪ {a} = Ico a b := by rw [← Ico_diff_left, diff_union_self, union_eq_self_of_subset_right (singleton_subset_iff.2 <| left_mem_Ico.2 hab)] theorem Ioo_union_right (hab : a < b) : Ioo a b ∪ {b} = Ioc a b := by simpa only [Ioo_toDual, Ico_toDual] using Ioo_union_left hab.dual theorem Ioo_union_both (h : a ≤ b) : Ioo a b ∪ {a, b} = Icc a b := by have : (Icc a b \ {a, b}) ∪ {a, b} = Icc a b := diff_union_of_subset fun | x, .inl rfl => left_mem_Icc.mpr h | x, .inr rfl => right_mem_Icc.mpr h rw [← this, Icc_diff_both] theorem Ioc_union_left (hab : a ≤ b) : Ioc a b ∪ {a} = Icc a b := by rw [← Icc_diff_left, diff_union_self, union_eq_self_of_subset_right (singleton_subset_iff.2 <| left_mem_Icc.2 hab)] theorem Ico_union_right (hab : a ≤ b) : Ico a b ∪ {b} = Icc a b := by simpa only [Ioc_toDual, Icc_toDual] using Ioc_union_left hab.dual @[simp] theorem Ico_insert_right (h : a ≤ b) : insert b (Ico a b) = Icc a b := by rw [insert_eq, union_comm, Ico_union_right h] @[simp] theorem Ioc_insert_left (h : a ≤ b) : insert a (Ioc a b) = Icc a b := by rw [insert_eq, union_comm, Ioc_union_left h] @[simp] theorem Ioo_insert_left (h : a < b) : insert a (Ioo a b) = Ico a b := by rw [insert_eq, union_comm, Ioo_union_left h] @[simp] theorem Ioo_insert_right (h : a < b) : insert b (Ioo a b) = Ioc a b := by rw [insert_eq, union_comm, Ioo_union_right h] @[simp] theorem Iio_insert : insert a (Iio a) = Iic a := ext fun _ => le_iff_eq_or_lt.symm @[simp] theorem Ioi_insert : insert a (Ioi a) = Ici a := ext fun _ => (or_congr_left eq_comm).trans le_iff_eq_or_lt.symm theorem mem_Ici_Ioi_of_subset_of_subset {s : Set α} (ho : Ioi a ⊆ s) (hc : s ⊆ Ici a) : s ∈ ({Ici a, Ioi a} : Set (Set α)) := by_cases (fun h : a ∈ s => Or.inl <| Subset.antisymm hc <| by rw [← Ioi_union_left, union_subset_iff]; simp [*]) fun h => Or.inr <| Subset.antisymm (fun _ hx => lt_of_le_of_ne (hc hx) fun heq => h <| heq.symm ▸ hx) ho theorem mem_Iic_Iio_of_subset_of_subset {s : Set α} (ho : Iio a ⊆ s) (hc : s ⊆ Iic a) : s ∈ ({Iic a, Iio a} : Set (Set α)) := @mem_Ici_Ioi_of_subset_of_subset αᵒᵈ _ a s ho hc theorem mem_Icc_Ico_Ioc_Ioo_of_subset_of_subset {s : Set α} (ho : Ioo a b ⊆ s) (hc : s ⊆ Icc a b) : s ∈ ({Icc a b, Ico a b, Ioc a b, Ioo a b} : Set (Set α)) := by classical by_cases ha : a ∈ s <;> by_cases hb : b ∈ s · refine Or.inl (Subset.antisymm hc ?_) rwa [← Ico_diff_left, diff_singleton_subset_iff, insert_eq_of_mem ha, ← Icc_diff_right, diff_singleton_subset_iff, insert_eq_of_mem hb] at ho · refine Or.inr <| Or.inl <| Subset.antisymm ?_ ?_ · rw [← Icc_diff_right] exact subset_diff_singleton hc hb · rwa [← Ico_diff_left, diff_singleton_subset_iff, insert_eq_of_mem ha] at ho · refine Or.inr <| Or.inr <| Or.inl <| Subset.antisymm ?_ ?_ · rw [← Icc_diff_left] exact subset_diff_singleton hc ha · rwa [← Ioc_diff_right, diff_singleton_subset_iff, insert_eq_of_mem hb] at ho · refine Or.inr <| Or.inr <| Or.inr <| Subset.antisymm ?_ ho rw [← Ico_diff_left, ← Icc_diff_right] apply_rules [subset_diff_singleton] theorem eq_left_or_mem_Ioo_of_mem_Ico {x : α} (hmem : x ∈ Ico a b) : x = a ∨ x ∈ Ioo a b := hmem.1.eq_or_gt.imp_right fun h => ⟨h, hmem.2⟩ theorem eq_right_or_mem_Ioo_of_mem_Ioc {x : α} (hmem : x ∈ Ioc a b) : x = b ∨ x ∈ Ioo a b := hmem.2.eq_or_lt.imp_right <| And.intro hmem.1 theorem eq_endpoints_or_mem_Ioo_of_mem_Icc {x : α} (hmem : x ∈ Icc a b) : x = a ∨ x = b ∨ x ∈ Ioo a b := hmem.1.eq_or_gt.imp_right fun h => eq_right_or_mem_Ioo_of_mem_Ioc ⟨h, hmem.2⟩ theorem _root_.IsMax.Ici_eq (h : IsMax a) : Ici a = {a} := eq_singleton_iff_unique_mem.2 ⟨left_mem_Ici, fun _ => h.eq_of_ge⟩ theorem _root_.IsMin.Iic_eq (h : IsMin a) : Iic a = {a} := h.toDual.Ici_eq theorem Ici_injective : Injective (Ici : α → Set α) := fun _ _ => eq_of_forall_ge_iff ∘ Set.ext_iff.1 theorem Iic_injective : Injective (Iic : α → Set α) := fun _ _ => eq_of_forall_le_iff ∘ Set.ext_iff.1 theorem Ici_inj : Ici a = Ici b ↔ a = b := Ici_injective.eq_iff theorem Iic_inj : Iic a = Iic b ↔ a = b := Iic_injective.eq_iff @[simp] theorem Icc_inter_Icc_eq_singleton (hab : a ≤ b) (hbc : b ≤ c) : Icc a b ∩ Icc b c = {b} := by rw [← Ici_inter_Iic, ← Iic_inter_Ici, inter_inter_inter_comm, Iic_inter_Ici] simp [hab, hbc] lemma Icc_eq_Icc_iff {d : α} (h : a ≤ b) : Icc a b = Icc c d ↔ a = c ∧ b = d := by refine ⟨fun heq ↦ ?_, by rintro ⟨rfl, rfl⟩; rfl⟩ have h' : c ≤ d := by by_contra contra; rw [Icc_eq_empty_iff.mpr contra, Icc_eq_empty_iff] at heq; contradiction simp only [Set.ext_iff, mem_Icc] at heq obtain ⟨-, h₁⟩ := (heq b).mp ⟨h, le_refl _⟩ obtain ⟨h₂, -⟩ := (heq a).mp ⟨le_refl _, h⟩ obtain ⟨h₃, -⟩ := (heq c).mpr ⟨le_refl _, h'⟩ obtain ⟨-, h₄⟩ := (heq d).mpr ⟨h', le_refl _⟩ exact ⟨le_antisymm h₃ h₂, le_antisymm h₁ h₄⟩ end PartialOrder section OrderTop @[simp] theorem Ici_top [PartialOrder α] [OrderTop α] : Ici (⊤ : α) = {⊤} := isMax_top.Ici_eq variable [Preorder α] [OrderTop α] {a : α} theorem Ioi_top : Ioi (⊤ : α) = ∅ := isMax_top.Ioi_eq @[simp] theorem Iic_top : Iic (⊤ : α) = univ := isTop_top.Iic_eq @[simp] theorem Icc_top : Icc a ⊤ = Ici a := by simp [← Ici_inter_Iic] @[simp] theorem Ioc_top : Ioc a ⊤ = Ioi a := by simp [← Ioi_inter_Iic] end OrderTop section OrderBot @[simp] theorem Iic_bot [PartialOrder α] [OrderBot α] : Iic (⊥ : α) = {⊥} := isMin_bot.Iic_eq variable [Preorder α] [OrderBot α] {a : α} theorem Iio_bot : Iio (⊥ : α) = ∅ := isMin_bot.Iio_eq @[simp] theorem Ici_bot : Ici (⊥ : α) = univ := isBot_bot.Ici_eq @[simp] theorem Icc_bot : Icc ⊥ a = Iic a := by simp [← Ici_inter_Iic] @[simp] theorem Ico_bot : Ico ⊥ a = Iio a := by simp [← Ici_inter_Iio] end OrderBot theorem Icc_bot_top [Preorder α] [BoundedOrder α] : Icc (⊥ : α) ⊤ = univ := by simp section Lattice section Inf variable [SemilatticeInf α] @[simp]
theorem Iic_inter_Iic {a b : α} : Iic a ∩ Iic b = Iic (a ⊓ b) := by ext x
Mathlib/Order/Interval/Set/Basic.lean
907
908
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Yakov Pechersky -/ import Mathlib.Data.List.Nodup import Mathlib.Data.List.Infix import Mathlib.Data.Quot /-! # List rotation This file proves basic results about `List.rotate`, the list rotation. ## Main declarations * `List.IsRotated l₁ l₂`: States that `l₁` is a rotated version of `l₂`. * `List.cyclicPermutations l`: The list of all cyclic permutants of `l`, up to the length of `l`. ## Tags rotated, rotation, permutation, cycle -/ universe u variable {α : Type u} open Nat Function namespace List theorem rotate_mod (l : List α) (n : ℕ) : l.rotate (n % l.length) = l.rotate n := by simp [rotate] @[simp] theorem rotate_nil (n : ℕ) : ([] : List α).rotate n = [] := by simp [rotate] @[simp] theorem rotate_zero (l : List α) : l.rotate 0 = l := by simp [rotate] theorem rotate'_nil (n : ℕ) : ([] : List α).rotate' n = [] := by simp @[simp] theorem rotate'_zero (l : List α) : l.rotate' 0 = l := by cases l <;> rfl theorem rotate'_cons_succ (l : List α) (a : α) (n : ℕ) : (a :: l : List α).rotate' n.succ = (l ++ [a]).rotate' n := by simp [rotate'] @[simp] theorem length_rotate' : ∀ (l : List α) (n : ℕ), (l.rotate' n).length = l.length | [], _ => by simp | _ :: _, 0 => rfl | a :: l, n + 1 => by rw [List.rotate', length_rotate' (l ++ [a]) n]; simp theorem rotate'_eq_drop_append_take : ∀ {l : List α} {n : ℕ}, n ≤ l.length → l.rotate' n = l.drop n ++ l.take n | [], n, h => by simp [drop_append_of_le_length h] | l, 0, h => by simp [take_append_of_le_length h] | a :: l, n + 1, h => by have hnl : n ≤ l.length := le_of_succ_le_succ h have hnl' : n ≤ (l ++ [a]).length := by rw [length_append, length_cons, List.length]; exact le_of_succ_le h rw [rotate'_cons_succ, rotate'_eq_drop_append_take hnl', drop, take, drop_append_of_le_length hnl, take_append_of_le_length hnl]; simp theorem rotate'_rotate' : ∀ (l : List α) (n m : ℕ), (l.rotate' n).rotate' m = l.rotate' (n + m) | a :: l, 0, m => by simp | [], n, m => by simp | a :: l, n + 1, m => by rw [rotate'_cons_succ, rotate'_rotate' _ n, Nat.add_right_comm, ← rotate'_cons_succ, Nat.succ_eq_add_one] @[simp] theorem rotate'_length (l : List α) : rotate' l l.length = l := by rw [rotate'_eq_drop_append_take le_rfl]; simp @[simp] theorem rotate'_length_mul (l : List α) : ∀ n : ℕ, l.rotate' (l.length * n) = l | 0 => by simp | n + 1 => calc l.rotate' (l.length * (n + 1)) = (l.rotate' (l.length * n)).rotate' (l.rotate' (l.length * n)).length := by simp [-rotate'_length, Nat.mul_succ, rotate'_rotate'] _ = l := by rw [rotate'_length, rotate'_length_mul l n] theorem rotate'_mod (l : List α) (n : ℕ) : l.rotate' (n % l.length) = l.rotate' n := calc l.rotate' (n % l.length) = (l.rotate' (n % l.length)).rotate' ((l.rotate' (n % l.length)).length * (n / l.length)) := by rw [rotate'_length_mul] _ = l.rotate' n := by rw [rotate'_rotate', length_rotate', Nat.mod_add_div] theorem rotate_eq_rotate' (l : List α) (n : ℕ) : l.rotate n = l.rotate' n := if h : l.length = 0 then by simp_all [length_eq_zero_iff] else by rw [← rotate'_mod, rotate'_eq_drop_append_take (le_of_lt (Nat.mod_lt _ (Nat.pos_of_ne_zero h)))] simp [rotate] @[simp] theorem rotate_cons_succ (l : List α) (a : α) (n : ℕ) : (a :: l : List α).rotate (n + 1) = (l ++ [a]).rotate n := by rw [rotate_eq_rotate', rotate_eq_rotate', rotate'_cons_succ] @[simp] theorem mem_rotate : ∀ {l : List α} {a : α} {n : ℕ}, a ∈ l.rotate n ↔ a ∈ l | [], _, n => by simp | a :: l, _, 0 => by simp | a :: l, _, n + 1 => by simp [rotate_cons_succ, mem_rotate, or_comm] @[simp] theorem length_rotate (l : List α) (n : ℕ) : (l.rotate n).length = l.length := by rw [rotate_eq_rotate', length_rotate'] @[simp] theorem rotate_replicate (a : α) (n : ℕ) (k : ℕ) : (replicate n a).rotate k = replicate n a := eq_replicate_iff.2 ⟨by rw [length_rotate, length_replicate], fun b hb => eq_of_mem_replicate <| mem_rotate.1 hb⟩ theorem rotate_eq_drop_append_take {l : List α} {n : ℕ} : n ≤ l.length → l.rotate n = l.drop n ++ l.take n := by rw [rotate_eq_rotate']; exact rotate'_eq_drop_append_take theorem rotate_eq_drop_append_take_mod {l : List α} {n : ℕ} : l.rotate n = l.drop (n % l.length) ++ l.take (n % l.length) := by rcases l.length.zero_le.eq_or_lt with hl | hl · simp [eq_nil_of_length_eq_zero hl.symm] rw [← rotate_eq_drop_append_take (n.mod_lt hl).le, rotate_mod] @[simp] theorem rotate_append_length_eq (l l' : List α) : (l ++ l').rotate l.length = l' ++ l := by rw [rotate_eq_rotate'] induction l generalizing l' · simp · simp_all [rotate'] theorem rotate_rotate (l : List α) (n m : ℕ) : (l.rotate n).rotate m = l.rotate (n + m) := by rw [rotate_eq_rotate', rotate_eq_rotate', rotate_eq_rotate', rotate'_rotate'] @[simp] theorem rotate_length (l : List α) : rotate l l.length = l := by rw [rotate_eq_rotate', rotate'_length] @[simp] theorem rotate_length_mul (l : List α) (n : ℕ) : l.rotate (l.length * n) = l := by rw [rotate_eq_rotate', rotate'_length_mul] theorem rotate_perm (l : List α) (n : ℕ) : l.rotate n ~ l := by rw [rotate_eq_rotate'] induction' n with n hn generalizing l · simp · rcases l with - | ⟨hd, tl⟩ · simp · rw [rotate'_cons_succ] exact (hn _).trans (perm_append_singleton _ _) @[simp] theorem nodup_rotate {l : List α} {n : ℕ} : Nodup (l.rotate n) ↔ Nodup l := (rotate_perm l n).nodup_iff @[simp] theorem rotate_eq_nil_iff {l : List α} {n : ℕ} : l.rotate n = [] ↔ l = [] := by induction' n with n hn generalizing l · simp · rcases l with - | ⟨hd, tl⟩ · simp · simp [rotate_cons_succ, hn] theorem nil_eq_rotate_iff {l : List α} {n : ℕ} : [] = l.rotate n ↔ [] = l := by rw [eq_comm, rotate_eq_nil_iff, eq_comm] @[simp] theorem rotate_singleton (x : α) (n : ℕ) : [x].rotate n = [x] := rotate_replicate x 1 n theorem zipWith_rotate_distrib {β γ : Type*} (f : α → β → γ) (l : List α) (l' : List β) (n : ℕ) (h : l.length = l'.length) : (zipWith f l l').rotate n = zipWith f (l.rotate n) (l'.rotate n) := by rw [rotate_eq_drop_append_take_mod, rotate_eq_drop_append_take_mod, rotate_eq_drop_append_take_mod, h, zipWith_append, ← drop_zipWith, ← take_zipWith, List.length_zipWith, h, min_self] rw [length_drop, length_drop, h] theorem zipWith_rotate_one {β : Type*} (f : α → α → β) (x y : α) (l : List α) : zipWith f (x :: y :: l) ((x :: y :: l).rotate 1) = f x y :: zipWith f (y :: l) (l ++ [x]) := by simp theorem getElem?_rotate {l : List α} {n m : ℕ} (hml : m < l.length) : (l.rotate n)[m]? = l[(m + n) % l.length]? := by rw [rotate_eq_drop_append_take_mod] rcases lt_or_le m (l.drop (n % l.length)).length with hm | hm · rw [getElem?_append_left hm, getElem?_drop, ← add_mod_mod] rw [length_drop, Nat.lt_sub_iff_add_lt] at hm rw [mod_eq_of_lt hm, Nat.add_comm] · have hlt : n % length l < length l := mod_lt _ (m.zero_le.trans_lt hml) rw [getElem?_append_right hm, getElem?_take_of_lt, length_drop] · congr 1 rw [length_drop] at hm have hm' := Nat.sub_le_iff_le_add'.1 hm have : n % length l + m - length l < length l := by rw [Nat.sub_lt_iff_lt_add hm'] exact Nat.add_lt_add hlt hml conv_rhs => rw [Nat.add_comm m, ← mod_add_mod, mod_eq_sub_mod hm', mod_eq_of_lt this] omega · rwa [Nat.sub_lt_iff_lt_add' hm, length_drop, Nat.sub_add_cancel hlt.le] theorem getElem_rotate (l : List α) (n : ℕ) (k : Nat) (h : k < (l.rotate n).length) : (l.rotate n)[k] = l[(k + n) % l.length]'(mod_lt _ (length_rotate l n ▸ k.zero_le.trans_lt h)) := by rw [← Option.some_inj, ← getElem?_eq_getElem, ← getElem?_eq_getElem, getElem?_rotate] exact h.trans_eq (length_rotate _ _) set_option linter.deprecated false in @[deprecated getElem?_rotate (since := "2025-02-14")] theorem get?_rotate {l : List α} {n m : ℕ} (hml : m < l.length) : (l.rotate n).get? m = l.get? ((m + n) % l.length) := by simp only [get?_eq_getElem?, length_rotate, hml, getElem?_eq_getElem, getElem_rotate] rw [← getElem?_eq_getElem] theorem get_rotate (l : List α) (n : ℕ) (k : Fin (l.rotate n).length) : (l.rotate n).get k = l.get ⟨(k + n) % l.length, mod_lt _ (length_rotate l n ▸ k.pos)⟩ := by simp [getElem_rotate] theorem head?_rotate {l : List α} {n : ℕ} (h : n < l.length) : head? (l.rotate n) = l[n]? := by rw [head?_eq_getElem?, getElem?_rotate (n.zero_le.trans_lt h), Nat.zero_add, Nat.mod_eq_of_lt h] theorem get_rotate_one (l : List α) (k : Fin (l.rotate 1).length) : (l.rotate 1).get k = l.get ⟨(k + 1) % l.length, mod_lt _ (length_rotate l 1 ▸ k.pos)⟩ := get_rotate l 1 k /-- A version of `List.getElem_rotate` that represents `l[k]` in terms of `(List.rotate l n)[⋯]`, not vice versa. Can be used instead of rewriting `List.getElem_rotate` from right to left. -/ theorem getElem_eq_getElem_rotate (l : List α) (n : ℕ) (k : Nat) (hk : k < l.length) : l[k] = ((l.rotate n)[(l.length - n % l.length + k) % l.length]' ((Nat.mod_lt _ (k.zero_le.trans_lt hk)).trans_eq (length_rotate _ _).symm)) := by rw [getElem_rotate] refine congr_arg l.get (Fin.eq_of_val_eq ?_) simp only [mod_add_mod] rw [← add_mod_mod, Nat.add_right_comm, Nat.sub_add_cancel, add_mod_left, mod_eq_of_lt] exacts [hk, (mod_lt _ (k.zero_le.trans_lt hk)).le] /-- A version of `List.get_rotate` that represents `List.get l` in terms of `List.get (List.rotate l n)`, not vice versa. Can be used instead of rewriting `List.get_rotate` from right to left. -/ theorem get_eq_get_rotate (l : List α) (n : ℕ) (k : Fin l.length) : l.get k = (l.rotate n).get ⟨(l.length - n % l.length + k) % l.length, (Nat.mod_lt _ (k.1.zero_le.trans_lt k.2)).trans_eq (length_rotate _ _).symm⟩ := by rw [get_rotate] refine congr_arg l.get (Fin.eq_of_val_eq ?_) simp only [mod_add_mod] rw [← add_mod_mod, Nat.add_right_comm, Nat.sub_add_cancel, add_mod_left, mod_eq_of_lt] exacts [k.2, (mod_lt _ (k.1.zero_le.trans_lt k.2)).le] theorem rotate_eq_self_iff_eq_replicate [hα : Nonempty α] : ∀ {l : List α}, (∀ n, l.rotate n = l) ↔ ∃ a, l = replicate l.length a | [] => by simp | a :: l => ⟨fun h => ⟨a, ext_getElem length_replicate.symm fun n h₁ h₂ => by rw [getElem_replicate, ← Option.some_inj, ← getElem?_eq_getElem, ← head?_rotate h₁, h, head?_cons]⟩, fun ⟨b, hb⟩ n => by rw [hb, rotate_replicate]⟩ theorem rotate_one_eq_self_iff_eq_replicate [Nonempty α] {l : List α} : l.rotate 1 = l ↔ ∃ a : α, l = List.replicate l.length a := ⟨fun h => rotate_eq_self_iff_eq_replicate.mp fun n => Nat.rec l.rotate_zero (fun n hn => by rwa [Nat.succ_eq_add_one, ← l.rotate_rotate, hn]) n, fun h => rotate_eq_self_iff_eq_replicate.mpr h 1⟩ theorem rotate_injective (n : ℕ) : Function.Injective fun l : List α => l.rotate n := by rintro l l' (h : l.rotate n = l'.rotate n) have hle : l.length = l'.length := (l.length_rotate n).symm.trans (h.symm ▸ l'.length_rotate n) rw [rotate_eq_drop_append_take_mod, rotate_eq_drop_append_take_mod] at h obtain ⟨hd, ht⟩ := append_inj h (by simp_all) rw [← take_append_drop _ l, ht, hd, take_append_drop] @[simp] theorem rotate_eq_rotate {l l' : List α} {n : ℕ} : l.rotate n = l'.rotate n ↔ l = l' := (rotate_injective n).eq_iff theorem rotate_eq_iff {l l' : List α} {n : ℕ} : l.rotate n = l' ↔ l = l'.rotate (l'.length - n % l'.length) := by rw [← @rotate_eq_rotate _ l _ n, rotate_rotate, ← rotate_mod l', add_mod] rcases l'.length.zero_le.eq_or_lt with hl | hl · rw [eq_nil_of_length_eq_zero hl.symm, rotate_nil] · rcases (Nat.zero_le (n % l'.length)).eq_or_lt with hn | hn · simp [← hn] · rw [mod_eq_of_lt (Nat.sub_lt hl hn), Nat.sub_add_cancel, mod_self, rotate_zero] exact (Nat.mod_lt _ hl).le @[simp] theorem rotate_eq_singleton_iff {l : List α} {n : ℕ} {x : α} : l.rotate n = [x] ↔ l = [x] := by rw [rotate_eq_iff, rotate_singleton] @[simp] theorem singleton_eq_rotate_iff {l : List α} {n : ℕ} {x : α} : [x] = l.rotate n ↔ [x] = l := by rw [eq_comm, rotate_eq_singleton_iff, eq_comm] theorem reverse_rotate (l : List α) (n : ℕ) : (l.rotate n).reverse = l.reverse.rotate (l.length - n % l.length) := by rw [← length_reverse, ← rotate_eq_iff] induction' n with n hn generalizing l · simp · rcases l with - | ⟨hd, tl⟩ · simp · rw [rotate_cons_succ, ← rotate_rotate, hn] simp theorem rotate_reverse (l : List α) (n : ℕ) : l.reverse.rotate n = (l.rotate (l.length - n % l.length)).reverse := by rw [← reverse_reverse l] simp_rw [reverse_rotate, reverse_reverse, rotate_eq_iff, rotate_rotate, length_rotate, length_reverse] rw [← length_reverse] let k := n % l.reverse.length rcases hk' : k with - | k' · simp_all! [k, length_reverse, ← rotate_rotate] · rcases l with - | ⟨x, l⟩ · simp · rw [Nat.mod_eq_of_lt, Nat.sub_add_cancel, rotate_length] · exact Nat.sub_le _ _ · exact Nat.sub_lt (by simp) (by simp_all! [k]) theorem map_rotate {β : Type*} (f : α → β) (l : List α) (n : ℕ) : map f (l.rotate n) = (map f l).rotate n := by induction' n with n hn IH generalizing l · simp · rcases l with - | ⟨hd, tl⟩ · simp · simp [hn] theorem Nodup.rotate_congr {l : List α} (hl : l.Nodup) (hn : l ≠ []) (i j : ℕ) (h : l.rotate i = l.rotate j) : i % l.length = j % l.length := by rw [← rotate_mod l i, ← rotate_mod l j] at h simpa only [head?_rotate, mod_lt, length_pos_of_ne_nil hn, getElem?_eq_getElem, Option.some_inj, hl.getElem_inj_iff, Fin.ext_iff] using congr_arg head? h theorem Nodup.rotate_congr_iff {l : List α} (hl : l.Nodup) {i j : ℕ} : l.rotate i = l.rotate j ↔ i % l.length = j % l.length ∨ l = [] := by rcases eq_or_ne l [] with rfl | hn · simp · simp only [hn, or_false] refine ⟨hl.rotate_congr hn _ _, fun h ↦ ?_⟩ rw [← rotate_mod, h, rotate_mod] theorem Nodup.rotate_eq_self_iff {l : List α} (hl : l.Nodup) {n : ℕ} : l.rotate n = l ↔ n % l.length = 0 ∨ l = [] := by rw [← zero_mod, ← hl.rotate_congr_iff, rotate_zero] section IsRotated variable (l l' : List α) /-- `IsRotated l₁ l₂` or `l₁ ~r l₂` asserts that `l₁` and `l₂` are cyclic permutations of each other. This is defined by claiming that `∃ n, l.rotate n = l'`. -/ def IsRotated : Prop := ∃ n, l.rotate n = l' @[inherit_doc List.IsRotated] -- This matches the precedence of the infix `~` for `List.Perm`, and of other relation infixes infixr:50 " ~r " => IsRotated variable {l l'} @[refl] theorem IsRotated.refl (l : List α) : l ~r l := ⟨0, by simp⟩ @[symm] theorem IsRotated.symm (h : l ~r l') : l' ~r l := by obtain ⟨n, rfl⟩ := h rcases l with - | ⟨hd, tl⟩ · exists 0 · use (hd :: tl).length * n - n rw [rotate_rotate, Nat.add_sub_cancel', rotate_length_mul] exact Nat.le_mul_of_pos_left _ (by simp) theorem isRotated_comm : l ~r l' ↔ l' ~r l := ⟨IsRotated.symm, IsRotated.symm⟩ @[simp] protected theorem IsRotated.forall (l : List α) (n : ℕ) : l.rotate n ~r l := IsRotated.symm ⟨n, rfl⟩ @[trans] theorem IsRotated.trans : ∀ {l l' l'' : List α}, l ~r l' → l' ~r l'' → l ~r l'' | _, _, _, ⟨n, rfl⟩, ⟨m, rfl⟩ => ⟨n + m, by rw [rotate_rotate]⟩ theorem IsRotated.eqv : Equivalence (@IsRotated α) := Equivalence.mk IsRotated.refl IsRotated.symm IsRotated.trans /-- The relation `List.IsRotated l l'` forms a `Setoid` of cycles. -/ def IsRotated.setoid (α : Type*) : Setoid (List α) where r := IsRotated iseqv := IsRotated.eqv theorem IsRotated.perm (h : l ~r l') : l ~ l' := Exists.elim h fun _ hl => hl ▸ (rotate_perm _ _).symm theorem IsRotated.nodup_iff (h : l ~r l') : Nodup l ↔ Nodup l' := h.perm.nodup_iff theorem IsRotated.mem_iff (h : l ~r l') {a : α} : a ∈ l ↔ a ∈ l' := h.perm.mem_iff @[simp] theorem isRotated_nil_iff : l ~r [] ↔ l = [] := ⟨fun ⟨n, hn⟩ => by simpa using hn, fun h => h ▸ by rfl⟩ @[simp] theorem isRotated_nil_iff' : [] ~r l ↔ [] = l := by rw [isRotated_comm, isRotated_nil_iff, eq_comm] @[simp] theorem isRotated_singleton_iff {x : α} : l ~r [x] ↔ l = [x] := ⟨fun ⟨n, hn⟩ => by simpa using hn, fun h => h ▸ by rfl⟩ @[simp] theorem isRotated_singleton_iff' {x : α} : [x] ~r l ↔ [x] = l := by rw [isRotated_comm, isRotated_singleton_iff, eq_comm] theorem isRotated_concat (hd : α) (tl : List α) : (tl ++ [hd]) ~r (hd :: tl) := IsRotated.symm ⟨1, by simp⟩ theorem isRotated_append : (l ++ l') ~r (l' ++ l) := ⟨l.length, by simp⟩ theorem IsRotated.reverse (h : l ~r l') : l.reverse ~r l'.reverse := by obtain ⟨n, rfl⟩ := h exact ⟨_, (reverse_rotate _ _).symm⟩
theorem isRotated_reverse_comm_iff : l.reverse ~r l' ↔ l ~r l'.reverse := by constructor <;>
Mathlib/Data/List/Rotate.lean
433
434
/- Copyright (c) 2024 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Heather Macbeth -/ import Mathlib.Analysis.Calculus.Deriv.Pi import Mathlib.Analysis.InnerProductSpace.EuclideanDist import Mathlib.Analysis.InnerProductSpace.NormPow import Mathlib.Data.Finset.Interval import Mathlib.MeasureTheory.Integral.IntegralEqImproper /-! # Gagliardo-Nirenberg-Sobolev inequality In this file we prove the Gagliardo-Nirenberg-Sobolev inequality. This states that for compactly supported `C¹`-functions between finite dimensional vector spaces, we can bound the `L^p`-norm of `u` by the `L^q` norm of the derivative of `u`. The bound is up to a constant that is independent of the function `u`. Let `n` be the dimension of the domain. The main step in the proof, which we dubbed the "grid-lines lemma" below, is a complicated inductive argument that involves manipulating an `n+1`-fold iterated integral and a product of `n+2` factors. In each step, one pushes one of the integral inside (all but one of) the factors of the product using Hölder's inequality. The precise formulation of the induction hypothesis (`MeasureTheory.GridLines.T_insert_le_T_lmarginal_singleton`) is tricky, and none of the 5 sources we referenced stated it. In the formalization we use the operation `MeasureTheory.lmarginal` to work with the iterated integrals, and use `MeasureTheory.lmarginal_insert'` to conveniently push one of the integrals inside. The Hölder's inequality step is done using `ENNReal.lintegral_mul_prod_norm_pow_le`. The conclusions of the main results below are an estimation up to a constant multiple. We don't really care about this constant, other than that it only depends on some pieces of data, typically `E`, `μ`, `p` and sometimes also the codomain of `u` or the support of `u`. We state these constants as separate definitions. ## Main results * `MeasureTheory.eLpNorm_le_eLpNorm_fderiv_of_eq`: The bound holds for `1 ≤ p`, `0 < n` and `q⁻¹ = p⁻¹ - n⁻¹` * `MeasureTheory.eLpNorm_le_eLpNorm_fderiv_of_le`: The bound holds when `1 ≤ p < n`, `0 ≤ q` and `p⁻¹ - n⁻¹ ≤ q⁻¹`. Note that in this case the constant depends on the support of `u`. Potentially also useful: * `MeasureTheory.eLpNorm_le_eLpNorm_fderiv_one`: this is the inequality for `q = 1`. In this version, the codomain can be an arbitrary Banach space. * `MeasureTheory.eLpNorm_le_eLpNorm_fderiv_of_eq_inner`: in this version, the codomain is assumed to be a Hilbert space, without restrictions on its dimension. -/ open scoped ENNReal NNReal open Set Function Finset MeasureTheory Measure Filter noncomputable section variable {ι : Type*} local prefix:max "#" => Fintype.card /-! ## The grid-lines lemma -/ variable {A : ι → Type*} [∀ i, MeasurableSpace (A i)] (μ : ∀ i, Measure (A i)) namespace MeasureTheory section DecidableEq variable [DecidableEq ι] namespace GridLines /-- The "grid-lines operation" (not a standard name) which is central in the inductive proof of the Sobolev inequality. For a finite dependent product `Π i : ι, A i` of sigma-finite measure spaces, a finite set `s` of indices from `ι`, and a (later assumed nonnegative) real number `p`, this operation acts on a function `f` from `Π i, A i` into the extended nonnegative reals. The operation is to partially integrate, in the `s` co-ordinates, the function whose value at `x : Π i, A i` is obtained by multiplying a certain power of `f` with the product, for each co-ordinate `i` in `s`, of a certain power of the integral of `f` along the "grid line" in the `i` direction through `x`. We are most interested in this operation when the set `s` is the universe in `ι`, but as a proxy for "induction on dimension" we define it for a general set `s` of co-ordinates: the `s`-grid-lines operation on a function `f` which is constant along the co-ordinates in `sᶜ` is morally (that is, up to type-theoretic nonsense) the same thing as the universe-grid-lines operation on the associated function on the "lower-dimensional" space `Π i : s, A i`. -/ def T (p : ℝ) (f : (∀ i, A i) → ℝ≥0∞) (s : Finset ι) : (∀ i, A i) → ℝ≥0∞ := ∫⋯∫⁻_s, f ^ (1 - (s.card - 1 : ℝ) * p) * ∏ i ∈ s, (∫⋯∫⁻_{i}, f ∂μ) ^ p ∂μ variable {p : ℝ} @[simp] lemma T_univ [Fintype ι] [∀ i, SigmaFinite (μ i)] (f : (∀ i, A i) → ℝ≥0∞) (x : ∀ i, A i) : T μ p f univ x = ∫⁻ (x : ∀ i, A i), (f x ^ (1 - (#ι - 1 : ℝ) * p) * ∏ i : ι, (∫⁻ t : A i, f (update x i t) ∂(μ i)) ^ p) ∂(.pi μ) := by simp [T, lmarginal_singleton] @[simp] lemma T_empty (f : (∀ i, A i) → ℝ≥0∞) (x : ∀ i, A i) : T μ p f ∅ x = f x ^ (1 + p) := by simp [T] /-- The main inductive step in the grid-lines lemma for the Gagliardo-Nirenberg-Sobolev inequality. The grid-lines operation `GridLines.T` on a nonnegative function on a finitary product type is less than or equal to the grid-lines operation of its partial integral in one co-ordinate (the latter intuitively considered as a function on a space "one dimension down"). -/ theorem T_insert_le_T_lmarginal_singleton [∀ i, SigmaFinite (μ i)] (hp₀ : 0 ≤ p) (s : Finset ι) (hp : (s.card : ℝ) * p ≤ 1) (i : ι) (hi : i ∉ s) {f : (∀ i, A i) → ℝ≥0∞} (hf : Measurable f) : T μ p f (insert i s) ≤ T μ p (∫⋯∫⁻_{i}, f ∂μ) s := by /- The proof is a tricky computation that relies on Hölder's inequality at its heart. The left-hand-side is an `|s|+1`-times iterated integral. Let `xᵢ` denote the `i`-th variable. We first push the integral over the `i`-th variable as the innermost integral. This is done in a single step with `MeasureTheory.lmarginal_insert'`, but in fact hides a repeated application of Fubini's theorem. The integrand is a product of `|s|+2` factors, in `|s|+1` of them we integrate over one additional variable. We split of the factor that integrates over `xᵢ`, and apply Hölder's inequality to the remaining factors (whose powers sum exactly to 1). After reordering factors, and combining two factors into one we obtain the right-hand side. -/ calc T μ p f (insert i s) = ∫⋯∫⁻_insert i s, f ^ (1 - (s.card : ℝ) * p) * ∏ j ∈ insert i s, (∫⋯∫⁻_{j}, f ∂μ) ^ p ∂μ := by -- unfold `T` and reformulate the exponents simp_rw [T, card_insert_of_not_mem hi] congr! push_cast ring _ = ∫⋯∫⁻_s, (fun x ↦ ∫⁻ (t : A i), (f (update x i t) ^ (1 - (s.card : ℝ) * p) * ∏ j ∈ insert i s, (∫⋯∫⁻_{j}, f ∂μ) (update x i t) ^ p) ∂ (μ i)) ∂μ := by -- pull out the integral over `xᵢ` rw [lmarginal_insert' _ _ hi] · congr! with x t simp only [Pi.mul_apply, Pi.pow_apply, Finset.prod_apply] · change Measurable (fun x ↦ _) simp only [Pi.mul_apply, Pi.pow_apply, Finset.prod_apply] refine (hf.pow_const _).mul <| Finset.measurable_prod _ ?_ exact fun _ _ ↦ hf.lmarginal μ |>.pow_const _ _ ≤ T μ p (∫⋯∫⁻_{i}, f ∂μ) s := lmarginal_mono (s := s) (fun x ↦ ?_) -- The remainder of the computation happens within an `|s|`-fold iterated integral simp only [Pi.mul_apply, Pi.pow_apply, Finset.prod_apply] set X := update x i have hF₁ : ∀ {j : ι}, Measurable fun t ↦ (∫⋯∫⁻_{j}, f ∂μ) (X t) := fun {_} ↦ hf.lmarginal μ |>.comp <| measurable_update _ have hF₀ : Measurable fun t ↦ f (X t) := hf.comp <| measurable_update _ let k : ℝ := s.card have hk' : 0 ≤ 1 - k * p := by linarith only [hp] calc ∫⁻ t, f (X t) ^ (1 - k * p) * ∏ j ∈ insert i s, (∫⋯∫⁻_{j}, f ∂μ) (X t) ^ p ∂ (μ i) = ∫⁻ t, (∫⋯∫⁻_{i}, f ∂μ) (X t) ^ p * (f (X t) ^ (1 - k * p) * ∏ j ∈ s, ((∫⋯∫⁻_{j}, f ∂μ) (X t) ^ p)) ∂(μ i) := by -- rewrite integrand so that `(∫⋯∫⁻_insert i s, f ∂μ) ^ p` comes first clear_value X congr! 2 with t simp_rw [prod_insert hi] ring_nf _ = (∫⋯∫⁻_{i}, f ∂μ) x ^ p * ∫⁻ t, f (X t) ^ (1 - k * p) * ∏ j ∈ s, ((∫⋯∫⁻_{j}, f ∂μ) (X t)) ^ p ∂(μ i) := by -- pull out this constant factor have : ∀ t, (∫⋯∫⁻_{i}, f ∂μ) (X t) = (∫⋯∫⁻_{i}, f ∂μ) x := by intro t rw [lmarginal_update_of_mem] exact Iff.mpr Finset.mem_singleton rfl simp_rw [this] rw [lintegral_const_mul] exact (hF₀.pow_const _).mul <| Finset.measurable_prod _ fun _ _ ↦ hF₁.pow_const _ _ ≤ (∫⋯∫⁻_{i}, f ∂μ) x ^ p * ((∫⁻ t, f (X t) ∂μ i) ^ (1 - k * p) * ∏ j ∈ s, (∫⁻ t, (∫⋯∫⁻_{j}, f ∂μ) (X t) ∂μ i) ^ p) := by -- apply Hölder's inequality gcongr apply ENNReal.lintegral_mul_prod_norm_pow_le · exact hF₀.aemeasurable · intros exact hF₁.aemeasurable · simp only [sum_const, nsmul_eq_mul] ring · exact hk' · exact fun _ _ ↦ hp₀ _ = (∫⋯∫⁻_{i}, f ∂μ) x ^ p * ((∫⋯∫⁻_{i}, f ∂μ) x ^ (1 - k * p) * ∏ j ∈ s, (∫⋯∫⁻_{i, j}, f ∂μ) x ^ p) := by -- absorb the newly-created integrals into `∫⋯∫` congr! 2 · rw [lmarginal_singleton] refine prod_congr rfl fun j hj => ?_ have hi' : i ∉ ({j} : Finset ι) := by simp only [Finset.mem_singleton, Finset.mem_insert, Finset.mem_compl] at hj ⊢ exact fun h ↦ hi (h ▸ hj) rw [lmarginal_insert _ hf hi'] _ = (∫⋯∫⁻_{i}, f ∂μ) x ^ (p + (1 - k * p)) * ∏ j ∈ s, (∫⋯∫⁻_{i, j}, f ∂μ) x ^ p := by -- combine two `(∫⋯∫⁻_insert i s, f ∂μ) x` terms rw [ENNReal.rpow_add_of_nonneg] · ring · exact hp₀ · exact hk' _ ≤ (∫⋯∫⁻_{i}, f ∂μ) x ^ (1 - (s.card - 1 : ℝ) * p) * ∏ j ∈ s, (∫⋯∫⁻_{j}, (∫⋯∫⁻_{i}, f ∂μ) ∂μ) x ^ p := by -- identify the result with the RHS integrand congr! 2 with j hj · ring · congr! 1 rw [← lmarginal_union μ f hf] · congr rw [Finset.union_comm] rfl · rw [Finset.disjoint_singleton] simp only [Finset.mem_insert, Finset.mem_compl] at hj exact fun h ↦ hi (h ▸ hj) /-- Auxiliary result for the grid-lines lemma. Given a nonnegative function on a finitary product type indexed by `ι`, and a set `s` in `ι`, consider partially integrating over the variables in `sᶜ` and performing the "grid-lines operation" (see `GridLines.T`) to the resulting function in the variables `s`. This theorem states that this operation decreases as the number of grid-lines taken increases. -/ theorem T_lmarginal_antitone [Fintype ι] [∀ i, SigmaFinite (μ i)] (hp₀ : 0 ≤ p) (hp : (#ι - 1 : ℝ) * p ≤ 1) {f : (∀ i, A i) → ℝ≥0∞} (hf : Measurable f) : Antitone (fun s ↦ T μ p (∫⋯∫⁻_sᶜ, f ∂μ) s) := by -- Reformulate (by induction): a function is decreasing on `Finset ι` if it decreases under the -- insertion of any element to any set. rw [Finset.antitone_iff_forall_insert_le] intro s i hi -- apply the lemma designed to encapsulate the inductive step convert T_insert_le_T_lmarginal_singleton μ hp₀ s ?_ i hi (hf.lmarginal μ) using 2 · rw [← lmarginal_union μ f hf] · rw [← insert_compl_insert hi] rfl rw [Finset.disjoint_singleton_left, not_mem_compl] exact mem_insert_self i s · -- the main nontrivial point is to check that an exponent `p` satisfying `0 ≤ p` and -- `(#ι - 1) * p ≤ 1` is in the valid range for the inductive-step lemma refine le_trans ?_ hp gcongr suffices (s.card : ℝ) + 1 ≤ #ι by linarith rw [← card_add_card_compl s] norm_cast gcongr have hi' : sᶜ.Nonempty := ⟨i, by rwa [Finset.mem_compl]⟩ rwa [← card_pos] at hi' end GridLines /-- The "grid-lines lemma" (not a standard name), stated with a general parameter `p` as the exponent. Compare with `lintegral_prod_lintegral_pow_le`. For any finite dependent product `Π i : ι, A i` of sigma-finite measure spaces, for any nonnegative real number `p` such that `(#ι - 1) * p ≤ 1`, for any function `f` from `Π i, A i` into the extended nonnegative reals, we consider an associated "grid-lines quantity", the integral of an associated function from `Π i, A i` into the extended nonnegative reals. The value of this function at `x : Π i, A i` is obtained by multiplying a certain power of `f` with the product, for each co-ordinate `i`, of a certain power of the integral of `f` along the "grid line" in the `i` direction through `x`. This lemma bounds the Lebesgue integral of the grid-lines quantity by a power of the Lebesgue integral of `f`. -/ theorem lintegral_mul_prod_lintegral_pow_le [Fintype ι] [∀ i, SigmaFinite (μ i)] {p : ℝ} (hp₀ : 0 ≤ p) (hp : (#ι - 1 : ℝ) * p ≤ 1) {f : (∀ i : ι, A i) → ℝ≥0∞} (hf : Measurable f) : ∫⁻ x, f x ^ (1 - (#ι - 1 : ℝ) * p) * ∏ i, (∫⁻ xᵢ, f (update x i xᵢ) ∂μ i) ^ p ∂.pi μ ≤ (∫⁻ x, f x ∂.pi μ) ^ (1 + p) := by cases isEmpty_or_nonempty (∀ i, A i) · simp_rw [lintegral_of_isEmpty]; refine zero_le _ inhabit ∀ i, A i have H : (∅ : Finset ι) ≤ Finset.univ := Finset.empty_subset _ simpa [lmarginal_univ] using GridLines.T_lmarginal_antitone μ hp₀ hp hf H default /-- Special case of the grid-lines lemma `lintegral_mul_prod_lintegral_pow_le`, taking the extremal exponent `p = (#ι - 1)⁻¹`. -/ theorem lintegral_prod_lintegral_pow_le [Fintype ι] [∀ i, SigmaFinite (μ i)] {p : ℝ} (hp : Real.HolderConjugate #ι p) {f} (hf : Measurable f) : ∫⁻ x, ∏ i, (∫⁻ xᵢ, f (update x i xᵢ) ∂μ i) ^ ((1 : ℝ) / (#ι - 1 : ℝ)) ∂.pi μ ≤ (∫⁻ x, f x ∂.pi μ) ^ p := by have : Nontrivial ι := Fintype.one_lt_card_iff_nontrivial.mp (by exact_mod_cast hp.lt) have h0 : (1 : ℝ) < #ι := by norm_cast; exact Fintype.one_lt_card have h1 : (0 : ℝ) < #ι - 1 := by linarith have h2 : 0 ≤ ((1 : ℝ) / (#ι - 1 : ℝ)) := by positivity have h3 : (#ι - 1 : ℝ) * ((1 : ℝ) / (#ι - 1 : ℝ)) ≤ 1 := by field_simp have h4 : p = 1 + 1 / (↑#ι - 1) := by field_simp; rw [mul_comm, hp.sub_one_mul_conj] rw [h4] convert lintegral_mul_prod_lintegral_pow_le μ h2 h3 hf using 2 field_simp end DecidableEq /-! ## The Gagliardo-Nirenberg-Sobolev inequality -/ variable {F : Type*} [NormedAddCommGroup F] [NormedSpace ℝ F] /-- The **Gagliardo-Nirenberg-Sobolev inequality**. Let `u` be a continuously differentiable compactly-supported function `u` on `ℝⁿ`, for `n ≥ 2`. (More literally we encode `ℝⁿ` as `ι → ℝ` where `n := #ι` is finite and at least 2.) Then the Lebesgue integral of the pointwise expression `|u x| ^ (n / (n - 1))` is bounded above by the `n / (n - 1)`-th power of the Lebesgue integral of the Fréchet derivative of `u`. For a basis-free version, see `lintegral_pow_le_pow_lintegral_fderiv`. -/ theorem lintegral_pow_le_pow_lintegral_fderiv_aux [Fintype ι] {p : ℝ} (hp : Real.HolderConjugate #ι p) {u : (ι → ℝ) → F} (hu : ContDiff ℝ 1 u) (h2u : HasCompactSupport u) : ∫⁻ x, ‖u x‖ₑ ^ p ≤ (∫⁻ x, ‖fderiv ℝ u x‖ₑ) ^ p := by classical /- For a function `f` in one variable and `t ∈ ℝ` we have `|f(t)| = `|∫_{-∞}^t Df(s)∂s| ≤ ∫_ℝ |Df(s)| ∂s` where we use the fundamental theorem of calculus. For each `x ∈ ℝⁿ` we let `u` vary in one of the `n` coordinates and apply the inequality above. By taking the product over these `n` factors, raising them to the power `(n-1)⁻¹` and integrating, we get the inequality `∫ |u| ^ (n/(n-1)) ≤ ∫ x, ∏ i, (∫ xᵢ, |Du(update x i xᵢ)|)^(n-1)⁻¹`. The result then follows from the grid-lines lemma. -/ have : (1 : ℝ) ≤ ↑#ι - 1 := by have hι : (2 : ℝ) ≤ #ι := by exact_mod_cast hp.lt linarith calc ∫⁻ x, ‖u x‖ₑ ^ p = ∫⁻ x, (‖u x‖ₑ ^ (1 / (#ι - 1 : ℝ))) ^ (#ι : ℝ) := by -- a little algebraic manipulation of the exponent congr! 2 with x rw [← ENNReal.rpow_mul, hp.conjugate_eq] field_simp _ = ∫⁻ x, ∏ _i : ι, ‖u x‖ₑ ^ (1 / (#ι - 1 : ℝ)) := by -- express the left-hand integrand as a product of identical factors congr! 2 with x simp_rw [prod_const] norm_cast _ ≤ ∫⁻ x, ∏ i, (∫⁻ xᵢ, ‖fderiv ℝ u (update x i xᵢ)‖ₑ) ^ ((1 : ℝ) / (#ι - 1 : ℝ)) := ?_ _ ≤ (∫⁻ x, ‖fderiv ℝ u x‖ₑ) ^ p := by -- apply the grid-lines lemma apply lintegral_prod_lintegral_pow_le _ hp have : Continuous (fderiv ℝ u) := hu.continuous_fderiv le_rfl fun_prop -- we estimate |u x| using the fundamental theorem of calculus. gcongr with x i calc ‖u x‖ₑ _ ≤ ∫⁻ xᵢ in Iic (x i), ‖deriv (u ∘ update x i) xᵢ‖ₑ := by apply le_trans (by simp) (HasCompactSupport.enorm_le_lintegral_Ici_deriv _ _ _) · exact hu.comp (by convert contDiff_update 1 x i) · exact h2u.comp_isClosedEmbedding (isClosedEmbedding_update x i) _ ≤ ∫⁻ xᵢ, ‖fderiv ℝ u (update x i xᵢ)‖ₑ := ?_ gcongr · exact Measure.restrict_le_self intro y dsimp -- bound the derivative which appears calc ‖deriv (u ∘ update x i) y‖ₑ = ‖fderiv ℝ u (update x i y) (deriv (update x i) y)‖ₑ := by rw [fderiv_comp_deriv _ (hu.differentiable le_rfl).differentiableAt (hasDerivAt_update x i y).differentiableAt] _ ≤ ‖fderiv ℝ u (update x i y)‖ₑ * ‖deriv (update x i) y‖ₑ := ContinuousLinearMap.le_opENorm _ _ _ ≤ ‖fderiv ℝ u (update x i y)‖ₑ := by simp [deriv_update, Pi.enorm_single] variable {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] [MeasurableSpace E] [BorelSpace E] [FiniteDimensional ℝ E] (μ : Measure E) [IsAddHaarMeasure μ] open Module /-- The constant factor occurring in the conclusion of `lintegral_pow_le_pow_lintegral_fderiv`. It only depends on `E`, `μ` and `p`. It is determined by the ratio of the measures on `E` and `ℝⁿ` and the operator norm of a chosen equivalence `E ≃ ℝⁿ` (raised to suitable powers involving `p`). -/ irreducible_def lintegralPowLePowLIntegralFDerivConst (p : ℝ) : ℝ≥0 := by let ι := Fin (finrank ℝ E) have : finrank ℝ E = finrank ℝ (ι → ℝ) := by rw [finrank_fintype_fun_eq_card, Fintype.card_fin (finrank ℝ E)] let e : E ≃L[ℝ] ι → ℝ := ContinuousLinearEquiv.ofFinrankEq this let c := addHaarScalarFactor μ ((volume : Measure (ι → ℝ)).map e.symm) exact (c * ‖(e.symm : (ι → ℝ) →L[ℝ] E)‖₊ ^ p) * (c ^ p)⁻¹ /-- The **Gagliardo-Nirenberg-Sobolev inequality**. Let `u` be a continuously differentiable compactly-supported function `u` on a normed space `E` of finite dimension `n ≥ 2`, equipped with Haar measure. Then the Lebesgue integral of the pointwise expression `|u x| ^ (n / (n - 1))` is bounded above by a constant times the `n / (n - 1)`-th power of the Lebesgue integral of the Fréchet derivative of `u`. -/ theorem lintegral_pow_le_pow_lintegral_fderiv {u : E → F} (hu : ContDiff ℝ 1 u) (h2u : HasCompactSupport u) {p : ℝ} (hp : Real.HolderConjugate (finrank ℝ E) p) : ∫⁻ x, ‖u x‖ₑ ^ p ∂μ ≤ lintegralPowLePowLIntegralFDerivConst μ p * (∫⁻ x, ‖fderiv ℝ u x‖ₑ ∂μ) ^ p := by /- We reduce to the case where `E` is `ℝⁿ`, for which we have already proved the result using an explicit basis in `MeasureTheory.lintegral_pow_le_pow_lintegral_fderiv_aux`. This proof is not too hard, but takes quite some steps, reasoning about the equivalence `e : E ≃ ℝⁿ`, relating the measures on each sides of the equivalence, and estimating the derivative using the chain rule. -/ set C := lintegralPowLePowLIntegralFDerivConst μ p let ι := Fin (finrank ℝ E) have hιcard : #ι = finrank ℝ E := Fintype.card_fin (finrank ℝ E) have : finrank ℝ E = finrank ℝ (ι → ℝ) := by simp [hιcard] let e : E ≃L[ℝ] ι → ℝ := ContinuousLinearEquiv.ofFinrankEq this have : IsAddHaarMeasure ((volume : Measure (ι → ℝ)).map e.symm) := (e.symm : (ι → ℝ) ≃+ E).isAddHaarMeasure_map _ e.symm.continuous e.symm.symm.continuous have hp : Real.HolderConjugate #ι p := by rwa [hιcard] have h0p : 0 ≤ p := hp.symm.nonneg let c := addHaarScalarFactor μ ((volume : Measure (ι → ℝ)).map e.symm) have hc : 0 < c := addHaarScalarFactor_pos_of_isAddHaarMeasure .. have h2c : μ = c • ((volume : Measure (ι → ℝ)).map e.symm) := isAddLeftInvariant_eq_smul .. have h3c : (c : ℝ≥0∞) ≠ 0 := by simp_rw [ne_eq, ENNReal.coe_eq_zero, hc.ne', not_false_eq_true] have h0C : C = (c * ‖(e.symm : (ι → ℝ) →L[ℝ] E)‖₊ ^ p) * (c ^ p)⁻¹ := by simp_rw [c, ι, C, e, lintegralPowLePowLIntegralFDerivConst] have hC : C * c ^ p = c * ‖(e.symm : (ι → ℝ) →L[ℝ] E)‖₊ ^ p := by rw [h0C, inv_mul_cancel_right₀ (NNReal.rpow_pos hc).ne'] simp only [h2c, ENNReal.smul_def, lintegral_smul_measure, smul_eq_mul] let v : (ι → ℝ) → F := u ∘ e.symm have hv : ContDiff ℝ 1 v := hu.comp e.symm.contDiff have h2v : HasCompactSupport v := h2u.comp_homeomorph e.symm.toHomeomorph have := calc ∫⁻ x, ‖u x‖ₑ ^ p ∂(volume : Measure (ι → ℝ)).map e.symm = ∫⁻ y, ‖v y‖ₑ ^ p := by refine lintegral_map ?_ e.symm.continuous.measurable borelize F exact hu.continuous.measurable.nnnorm.coe_nnreal_ennreal.pow_const _ _ ≤ (∫⁻ y, ‖fderiv ℝ v y‖ₑ) ^ p := lintegral_pow_le_pow_lintegral_fderiv_aux hp hv h2v _ = (∫⁻ y, ‖(fderiv ℝ u (e.symm y)).comp (fderiv ℝ e.symm y)‖ₑ) ^ p := by congr! with y apply fderiv_comp _ (hu.differentiable le_rfl _) exact e.symm.differentiableAt _ ≤ (∫⁻ y, ‖fderiv ℝ u (e.symm y)‖ₑ * ‖(e.symm : (ι → ℝ) →L[ℝ] E)‖ₑ) ^ p := by gcongr with y rw [e.symm.fderiv] apply ContinuousLinearMap.opENorm_comp_le _ = (‖(e.symm : (ι → ℝ) →L[ℝ] E)‖ₑ * ∫⁻ y, ‖fderiv ℝ u (e.symm y)‖ₑ) ^ p := by rw [lintegral_mul_const, mul_comm] refine (Continuous.nnnorm ?_).measurable.coe_nnreal_ennreal exact (hu.continuous_fderiv le_rfl).comp e.symm.continuous _ = (‖(e.symm : (ι → ℝ) →L[ℝ] E)‖₊ ^ p : ℝ≥0) * (∫⁻ y, ‖fderiv ℝ u (e.symm y)‖ₑ) ^ p := by rw [ENNReal.mul_rpow_of_nonneg _ _ h0p, enorm_eq_nnnorm, ← ENNReal.coe_rpow_of_nonneg _ h0p] _ = (‖(e.symm : (ι → ℝ) →L[ℝ] E)‖₊ ^ p : ℝ≥0) * (∫⁻ x, ‖fderiv ℝ u x‖ₑ ∂(volume : Measure (ι → ℝ)).map e.symm) ^ p := by congr rw [lintegral_map _ e.symm.continuous.measurable] have : Continuous (fderiv ℝ u) := hu.continuous_fderiv le_rfl fun_prop rw [← ENNReal.mul_le_mul_left h3c ENNReal.coe_ne_top, ← mul_assoc, ← ENNReal.coe_mul, ← hC, ENNReal.coe_mul] at this rw [ENNReal.mul_rpow_of_nonneg _ _ h0p, ← mul_assoc, ← ENNReal.coe_rpow_of_ne_zero hc.ne'] exact this /-- The constant factor occurring in the conclusion of `eLpNorm_le_eLpNorm_fderiv_one`. It only depends on `E`, `μ` and `p`. -/ irreducible_def eLpNormLESNormFDerivOneConst (p : ℝ) : ℝ≥0 := lintegralPowLePowLIntegralFDerivConst μ p ^ p⁻¹ /-- The **Gagliardo-Nirenberg-Sobolev inequality**. Let `u` be a continuously differentiable compactly-supported function `u` on a normed space `E` of finite dimension `n ≥ 2`, equipped with Haar measure. Then the `Lᵖ` norm of `u`, where `p := n / (n - 1)`, is bounded above by a constant times the `L¹` norm of the Fréchet derivative of `u`. -/ theorem eLpNorm_le_eLpNorm_fderiv_one {u : E → F} (hu : ContDiff ℝ 1 u) (h2u : HasCompactSupport u) {p : ℝ≥0} (hp : NNReal.HolderConjugate (finrank ℝ E) p) : eLpNorm u p μ ≤ eLpNormLESNormFDerivOneConst μ p * eLpNorm (fderiv ℝ u) 1 μ := by have h0p : 0 < (p : ℝ) := hp.coe.symm.pos rw [eLpNorm_one_eq_lintegral_enorm, ← ENNReal.rpow_le_rpow_iff h0p, ENNReal.mul_rpow_of_nonneg _ _ h0p.le, ← ENNReal.coe_rpow_of_nonneg _ h0p.le, eLpNormLESNormFDerivOneConst, ← NNReal.rpow_mul, eLpNorm_nnreal_pow_eq_lintegral hp.symm.pos.ne', inv_mul_cancel₀ h0p.ne', NNReal.rpow_one] exact lintegral_pow_le_pow_lintegral_fderiv μ hu h2u hp.coe /-- The constant factor occurring in the conclusion of `eLpNorm_le_eLpNorm_fderiv_of_eq_inner`. It only depends on `E`, `μ` and `p`. -/ def eLpNormLESNormFDerivOfEqInnerConst (p : ℝ) : ℝ≥0 := let n := finrank ℝ E eLpNormLESNormFDerivOneConst μ (NNReal.conjExponent n) * (p * (n - 1) / (n - p)).toNNReal variable {F' : Type*} [NormedAddCommGroup F'] [InnerProductSpace ℝ F'] [CompleteSpace F']
/-- The **Gagliardo-Nirenberg-Sobolev inequality**. Let `u` be a continuously differentiable compactly-supported function `u` on a normed space `E` of finite dimension `n`, equipped with Haar measure, let `1 ≤ p < n` and let `p'⁻¹ := p⁻¹ - n⁻¹`. Then the `Lᵖ'` norm of `u` is bounded above by a constant times the `Lᵖ` norm of the Fréchet derivative of `u`. Note: The codomain of `u` needs to be a Hilbert space. -/ theorem eLpNorm_le_eLpNorm_fderiv_of_eq_inner {u : E → F'} (hu : ContDiff ℝ 1 u) (h2u : HasCompactSupport u) {p p' : ℝ≥0} (hp : 1 ≤ p) (hn : 0 < finrank ℝ E) (hp' : (p' : ℝ)⁻¹ = p⁻¹ - (finrank ℝ E : ℝ)⁻¹) : eLpNorm u p' μ ≤ eLpNormLESNormFDerivOfEqInnerConst μ p * eLpNorm (fderiv ℝ u) p μ := by /- Here we derive the GNS-inequality for `p ≥ 1` from the version with `p = 1`. For `p > 1` we apply the previous version to the function `|u|^γ` for a suitably chosen `γ`. The proof requires that `x ↦ |x|^p` is smooth in the codomain, so we require that it is a Hilbert space. -/ by_cases hp'0 : p' = 0 · simp [hp'0] set n := finrank ℝ E let n' := NNReal.conjExponent n have h2p : (p : ℝ) < n := by have : 0 < p⁻¹ - (n : ℝ)⁻¹ := NNReal.coe_lt_coe.mpr (pos_iff_ne_zero.mpr (inv_ne_zero hp'0)) |>.trans_eq hp' rwa [NNReal.coe_inv, sub_pos, inv_lt_inv₀ _ (zero_lt_one.trans_le (NNReal.coe_le_coe.mpr hp))] at this exact_mod_cast hn have h0n : 2 ≤ n := Nat.succ_le_of_lt <| Nat.one_lt_cast.mp <| hp.trans_lt h2p have hn : NNReal.HolderConjugate n n' := .conjExponent (by norm_cast) have h1n : 1 ≤ (n : ℝ≥0) := hn.lt.le have h2n : (0 : ℝ) < n - 1 := by simp_rw [sub_pos]; exact hn.coe.lt have hnp : (0 : ℝ) < n - p := by simp_rw [sub_pos]; exact h2p rcases hp.eq_or_lt with rfl|hp -- the case `p = 1` · convert eLpNorm_le_eLpNorm_fderiv_one μ hu h2u hn using 2 · suffices (p' : ℝ) = n' by simpa using this rw [← inv_inj, hp'] field_simp [n', NNReal.conjExponent] · norm_cast simp_rw [n', n, eLpNormLESNormFDerivOfEqInnerConst] field_simp -- the case `p > 1` let q := Real.conjExponent p have hq : Real.HolderConjugate p q := .conjExponent hp have h0p : p ≠ 0 := zero_lt_one.trans hp |>.ne' have h1p : (p : ℝ) ≠ 1 := hq.lt.ne' have h3p : (p : ℝ) - 1 ≠ 0 := sub_ne_zero_of_ne h1p have h0p' : p' ≠ 0 := by suffices 0 < (p' : ℝ) from (show 0 < p' from this) |>.ne' rw [← inv_pos, hp', sub_pos] exact inv_strictAnti₀ hq.pos h2p have h2q : 1 / n' - 1 / q = 1 / p' := by simp_rw -zeta [one_div, hp'] rw [← hq.one_sub_inv, ← hn.coe.one_sub_inv, sub_sub_sub_cancel_left] simp only [NNReal.coe_natCast, NNReal.coe_inv] let γ : ℝ≥0 := ⟨p * (n - 1) / (n - p), by positivity⟩ have h0γ : (γ : ℝ) = p * (n - 1) / (n - p) := rfl have h1γ : 1 < (γ : ℝ) := by rwa [h0γ, one_lt_div hnp, mul_sub, mul_one, sub_lt_sub_iff_right, lt_mul_iff_one_lt_left] exact hn.coe.pos have h2γ : γ * n' = p' := by rw [← NNReal.coe_inj, ← inv_inj, hp', NNReal.coe_mul, h0γ, hn.coe.conjugate_eq] field_simp; ring have h3γ : (γ - 1) * q = p' := by rw [← inv_inj, hp', h0γ, hq.conjugate_eq] have : (p : ℝ) * (n - 1) - (n - p) = n * (p - 1) := by ring field_simp [this]; ring have h4γ : (γ : ℝ) ≠ 0 := (zero_lt_one.trans h1γ).ne' by_cases h3u : ∫⁻ x, ‖u x‖ₑ ^ (p' : ℝ) ∂μ = 0 · rw [eLpNorm_nnreal_eq_lintegral h0p', h3u, ENNReal.zero_rpow_of_pos] <;> positivity have h4u : ∫⁻ x, ‖u x‖ₑ ^ (p' : ℝ) ∂μ ≠ ∞ := by refine lintegral_rpow_enorm_lt_top_of_eLpNorm'_lt_top ((NNReal.coe_pos.trans pos_iff_ne_zero).mpr h0p') ?_ |>.ne rw [← eLpNorm_nnreal_eq_eLpNorm' h0p'] exact hu.continuous.memLp_of_hasCompactSupport (μ := μ) h2u |>.eLpNorm_lt_top have h5u : (∫⁻ x, ‖u x‖ₑ ^ (p' : ℝ) ∂μ) ^ (1 / q) ≠ 0 := ENNReal.rpow_pos (pos_iff_ne_zero.mpr h3u) h4u |>.ne' have h6u : (∫⁻ x, ‖u x‖ₑ ^ (p' : ℝ) ∂μ) ^ (1 / q) ≠ ∞ := ENNReal.rpow_ne_top_of_nonneg (div_nonneg zero_le_one hq.symm.nonneg) h4u have h7u := hu.continuous -- for fun_prop have h8u := (hu.fderiv_right (m := 0) le_rfl).continuous -- for fun_prop let v : E → ℝ := fun x ↦ ‖u x‖ ^ (γ : ℝ) have hv : ContDiff ℝ 1 v := hu.norm_rpow h1γ have h2v : HasCompactSupport v := h2u.norm.rpow_const h4γ set C := eLpNormLESNormFDerivOneConst μ n' have := calc (∫⁻ x, ‖u x‖ₑ ^ (p' : ℝ) ∂μ) ^ (1 / (n' : ℝ)) = eLpNorm v n' μ := by rw [← h2γ, eLpNorm_nnreal_eq_lintegral hn.symm.pos.ne'] simp (discharger := positivity) [v, Real.enorm_rpow_of_nonneg, ENNReal.rpow_mul, ← ENNReal.coe_rpow_of_nonneg] _ ≤ C * eLpNorm (fderiv ℝ v) 1 μ := eLpNorm_le_eLpNorm_fderiv_one μ hv h2v hn _ = C * ∫⁻ x, ‖fderiv ℝ v x‖ₑ ∂μ := by rw [eLpNorm_one_eq_lintegral_enorm] _ ≤ C * γ * ∫⁻ x, ‖u x‖ₑ ^ ((γ : ℝ) - 1) * ‖fderiv ℝ u x‖ₑ ∂μ := by rw [mul_assoc, ← lintegral_const_mul γ] gcongr simp_rw [← mul_assoc] exact enorm_fderiv_norm_rpow_le (hu.differentiable le_rfl) h1γ dsimp [enorm] fun_prop _ ≤ C * γ * ((∫⁻ x, ‖u x‖ₑ ^ (p' : ℝ) ∂μ) ^ (1 / q) * (∫⁻ x, ‖fderiv ℝ u x‖ₑ ^ (p : ℝ) ∂μ) ^ (1 / (p : ℝ))) := by gcongr convert ENNReal.lintegral_mul_le_Lp_mul_Lq μ (.symm <| .conjExponent <| show 1 < (p : ℝ) from hp) ?_ ?_ using 5 · simp [γ, n, q, ← ENNReal.rpow_mul, ← h3γ] · borelize F' fun_prop · fun_prop _ = C * γ * (∫⁻ x, ‖fderiv ℝ u x‖ₑ ^ (p : ℝ) ∂μ) ^ (1 / (p : ℝ)) * (∫⁻ x, ‖u x‖ₑ ^ (p' : ℝ) ∂μ) ^ (1 / q) := by ring calc eLpNorm u p' μ = (∫⁻ x, ‖u x‖ₑ ^ (p' : ℝ) ∂μ) ^ (1 / (p' : ℝ)) := eLpNorm_nnreal_eq_lintegral h0p' _ ≤ C * γ * (∫⁻ x, ‖fderiv ℝ u x‖ₑ ^ (p : ℝ) ∂μ) ^ (1 / (p : ℝ)) := by rwa [← h2q, ENNReal.rpow_sub _ _ h3u h4u, ENNReal.div_le_iff h5u h6u] _ = eLpNormLESNormFDerivOfEqInnerConst μ p * eLpNorm (fderiv ℝ u) (↑p) μ := by
Mathlib/Analysis/FunctionalSpaces/SobolevInequality.lean
463
578
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Jeremy Avigad -/ import Mathlib.Data.Set.Finite.Basic import Mathlib.Data.Set.Finite.Range import Mathlib.Data.Set.Lattice import Mathlib.Topology.Defs.Filter /-! # Openness and closedness of a set This file provides lemmas relating to the predicates `IsOpen` and `IsClosed` of a set endowed with a topology. ## Implementation notes Topology in mathlib heavily uses filters (even more than in Bourbaki). See explanations in <https://leanprover-community.github.io/theories/topology.html>. ## References * [N. Bourbaki, *General Topology*][bourbaki1966] * [I. M. James, *Topologies and Uniformities*][james1999] ## Tags topological space -/ open Set Filter Topology universe u v /-- A constructor for topologies by specifying the closed sets, and showing that they satisfy the appropriate conditions. -/ def TopologicalSpace.ofClosed {X : Type u} (T : Set (Set X)) (empty_mem : ∅ ∈ T) (sInter_mem : ∀ A, A ⊆ T → ⋂₀ A ∈ T) (union_mem : ∀ A, A ∈ T → ∀ B, B ∈ T → A ∪ B ∈ T) : TopologicalSpace X where IsOpen X := Xᶜ ∈ T isOpen_univ := by simp [empty_mem] isOpen_inter s t hs ht := by simpa only [compl_inter] using union_mem sᶜ hs tᶜ ht isOpen_sUnion s hs := by simp only [Set.compl_sUnion] exact sInter_mem (compl '' s) fun z ⟨y, hy, hz⟩ => hz ▸ hs y hy section TopologicalSpace variable {X : Type u} {ι : Sort v} {α : Type*} {x : X} {s s₁ s₂ t : Set X} {p p₁ p₂ : X → Prop} lemma isOpen_mk {p h₁ h₂ h₃} : IsOpen[⟨p, h₁, h₂, h₃⟩] s ↔ p s := Iff.rfl @[ext (iff := false)] protected theorem TopologicalSpace.ext : ∀ {f g : TopologicalSpace X}, IsOpen[f] = IsOpen[g] → f = g | ⟨_, _, _, _⟩, ⟨_, _, _, _⟩, rfl => rfl protected theorem TopologicalSpace.ext_iff {t t' : TopologicalSpace X} : t = t' ↔ ∀ s, IsOpen[t] s ↔ IsOpen[t'] s := ⟨fun h _ => h ▸ Iff.rfl, fun h => by ext; exact h _⟩ theorem isOpen_fold {t : TopologicalSpace X} : t.IsOpen s = IsOpen[t] s := rfl variable [TopologicalSpace X] theorem isOpen_iUnion {f : ι → Set X} (h : ∀ i, IsOpen (f i)) : IsOpen (⋃ i, f i) := isOpen_sUnion (forall_mem_range.2 h) theorem isOpen_biUnion {s : Set α} {f : α → Set X} (h : ∀ i ∈ s, IsOpen (f i)) : IsOpen (⋃ i ∈ s, f i) := isOpen_iUnion fun i => isOpen_iUnion fun hi => h i hi theorem IsOpen.union (h₁ : IsOpen s₁) (h₂ : IsOpen s₂) : IsOpen (s₁ ∪ s₂) := by rw [union_eq_iUnion]; exact isOpen_iUnion (Bool.forall_bool.2 ⟨h₂, h₁⟩) lemma isOpen_iff_of_cover {f : α → Set X} (ho : ∀ i, IsOpen (f i)) (hU : (⋃ i, f i) = univ) : IsOpen s ↔ ∀ i, IsOpen (f i ∩ s) := by refine ⟨fun h i ↦ (ho i).inter h, fun h ↦ ?_⟩ rw [← s.inter_univ, inter_comm, ← hU, iUnion_inter] exact isOpen_iUnion fun i ↦ h i @[simp] theorem isOpen_empty : IsOpen (∅ : Set X) := by rw [← sUnion_empty]; exact isOpen_sUnion fun a => False.elim theorem Set.Finite.isOpen_sInter {s : Set (Set X)} (hs : s.Finite) (h : ∀ t ∈ s, IsOpen t) : IsOpen (⋂₀ s) := by induction s, hs using Set.Finite.induction_on with | empty => rw [sInter_empty]; exact isOpen_univ | insert _ _ ih => simp only [sInter_insert, forall_mem_insert] at h ⊢ exact h.1.inter (ih h.2) theorem Set.Finite.isOpen_biInter {s : Set α} {f : α → Set X} (hs : s.Finite) (h : ∀ i ∈ s, IsOpen (f i)) : IsOpen (⋂ i ∈ s, f i) := sInter_image f s ▸ (hs.image _).isOpen_sInter (forall_mem_image.2 h) theorem isOpen_iInter_of_finite [Finite ι] {s : ι → Set X} (h : ∀ i, IsOpen (s i)) : IsOpen (⋂ i, s i) := (finite_range _).isOpen_sInter (forall_mem_range.2 h) theorem isOpen_biInter_finset {s : Finset α} {f : α → Set X} (h : ∀ i ∈ s, IsOpen (f i)) : IsOpen (⋂ i ∈ s, f i) := s.finite_toSet.isOpen_biInter h @[simp] theorem isOpen_const {p : Prop} : IsOpen { _x : X | p } := by by_cases p <;> simp [*] theorem IsOpen.and : IsOpen { x | p₁ x } → IsOpen { x | p₂ x } → IsOpen { x | p₁ x ∧ p₂ x } := IsOpen.inter @[simp] theorem isOpen_compl_iff : IsOpen sᶜ ↔ IsClosed s := ⟨fun h => ⟨h⟩, fun h => h.isOpen_compl⟩ theorem TopologicalSpace.ext_iff_isClosed {X} {t₁ t₂ : TopologicalSpace X} : t₁ = t₂ ↔ ∀ s, IsClosed[t₁] s ↔ IsClosed[t₂] s := by rw [TopologicalSpace.ext_iff, compl_surjective.forall] simp only [@isOpen_compl_iff _ _ t₁, @isOpen_compl_iff _ _ t₂] alias ⟨_, TopologicalSpace.ext_isClosed⟩ := TopologicalSpace.ext_iff_isClosed theorem isClosed_const {p : Prop} : IsClosed { _x : X | p } := ⟨isOpen_const (p := ¬p)⟩ @[simp] theorem isClosed_empty : IsClosed (∅ : Set X) := isClosed_const @[simp] theorem isClosed_univ : IsClosed (univ : Set X) := isClosed_const lemma IsOpen.isLocallyClosed (hs : IsOpen s) : IsLocallyClosed s := ⟨_, _, hs, isClosed_univ, (inter_univ _).symm⟩ lemma IsClosed.isLocallyClosed (hs : IsClosed s) : IsLocallyClosed s := ⟨_, _, isOpen_univ, hs, (univ_inter _).symm⟩ theorem IsClosed.union : IsClosed s₁ → IsClosed s₂ → IsClosed (s₁ ∪ s₂) := by simpa only [← isOpen_compl_iff, compl_union] using IsOpen.inter theorem isClosed_sInter {s : Set (Set X)} : (∀ t ∈ s, IsClosed t) → IsClosed (⋂₀ s) := by simpa only [← isOpen_compl_iff, compl_sInter, sUnion_image] using isOpen_biUnion theorem isClosed_iInter {f : ι → Set X} (h : ∀ i, IsClosed (f i)) : IsClosed (⋂ i, f i) := isClosed_sInter <| forall_mem_range.2 h theorem isClosed_biInter {s : Set α} {f : α → Set X} (h : ∀ i ∈ s, IsClosed (f i)) : IsClosed (⋂ i ∈ s, f i) := isClosed_iInter fun i => isClosed_iInter <| h i @[simp] theorem isClosed_compl_iff {s : Set X} : IsClosed sᶜ ↔ IsOpen s := by rw [← isOpen_compl_iff, compl_compl] alias ⟨_, IsOpen.isClosed_compl⟩ := isClosed_compl_iff theorem IsOpen.sdiff (h₁ : IsOpen s) (h₂ : IsClosed t) : IsOpen (s \ t) := IsOpen.inter h₁ h₂.isOpen_compl theorem IsClosed.inter (h₁ : IsClosed s₁) (h₂ : IsClosed s₂) : IsClosed (s₁ ∩ s₂) := by rw [← isOpen_compl_iff] at * rw [compl_inter] exact IsOpen.union h₁ h₂ theorem IsClosed.sdiff (h₁ : IsClosed s) (h₂ : IsOpen t) : IsClosed (s \ t) := IsClosed.inter h₁ (isClosed_compl_iff.mpr h₂) theorem Set.Finite.isClosed_biUnion {s : Set α} {f : α → Set X} (hs : s.Finite) (h : ∀ i ∈ s, IsClosed (f i)) : IsClosed (⋃ i ∈ s, f i) := by simp only [← isOpen_compl_iff, compl_iUnion] at * exact hs.isOpen_biInter h lemma isClosed_biUnion_finset {s : Finset α} {f : α → Set X} (h : ∀ i ∈ s, IsClosed (f i)) : IsClosed (⋃ i ∈ s, f i) := s.finite_toSet.isClosed_biUnion h theorem isClosed_iUnion_of_finite [Finite ι] {s : ι → Set X} (h : ∀ i, IsClosed (s i)) : IsClosed (⋃ i, s i) := by simp only [← isOpen_compl_iff, compl_iUnion] at * exact isOpen_iInter_of_finite h theorem isClosed_imp {p q : X → Prop} (hp : IsOpen { x | p x }) (hq : IsClosed { x | q x }) : IsClosed { x | p x → q x } := by simpa only [imp_iff_not_or] using hp.isClosed_compl.union hq theorem IsClosed.not : IsClosed { a | p a } → IsOpen { a | ¬p a } := isOpen_compl_iff.mpr /-! ### Limits of filters in topological spaces In this section we define functions that return a limit of a filter (or of a function along a filter), if it exists, and a random point otherwise. These functions are rarely used in Mathlib, most of the theorems are written using `Filter.Tendsto`. One of the reasons is that `Filter.limUnder f g = x` is not equivalent to `Filter.Tendsto g f (𝓝 x)` unless the codomain is a Hausdorff space and `g` has a limit along `f`. -/ section lim /-- If a filter `f` is majorated by some `𝓝 x`, then it is majorated by `𝓝 (Filter.lim f)`. We formulate this lemma with a `[Nonempty X]` argument of `lim` derived from `h` to make it useful for types without a `[Nonempty X]` instance. Because of the built-in proof irrelevance, Lean will unify this instance with any other instance. -/ theorem le_nhds_lim {f : Filter X} (h : ∃ x, f ≤ 𝓝 x) : f ≤ 𝓝 (@lim _ _ (nonempty_of_exists h) f) := Classical.epsilon_spec h /-- If `g` tends to some `𝓝 x` along `f`, then it tends to `𝓝 (Filter.limUnder f g)`. We formulate this lemma with a `[Nonempty X]` argument of `lim` derived from `h` to make it useful for types without a `[Nonempty X]` instance. Because of the built-in proof irrelevance, Lean will unify this instance with any other instance. -/ theorem tendsto_nhds_limUnder {f : Filter α} {g : α → X} (h : ∃ x, Tendsto g f (𝓝 x)) : Tendsto g f (𝓝 (@limUnder _ _ _ (nonempty_of_exists h) f g)) := le_nhds_lim h theorem limUnder_of_not_tendsto [hX : Nonempty X] {f : Filter α} {g : α → X} (h : ¬ ∃ x, Tendsto g f (𝓝 x)) : limUnder f g = Classical.choice hX := by simp_rw [Tendsto] at h simp_rw [limUnder, lim, Classical.epsilon, Classical.strongIndefiniteDescription, dif_neg h] end lim end TopologicalSpace
Mathlib/Topology/Basic.lean
1,808
1,809
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Benjamin Davidson -/ import Mathlib.Algebra.Field.NegOnePow import Mathlib.Algebra.Field.Periodic import Mathlib.Algebra.QuadraticDiscriminant import Mathlib.Analysis.SpecialFunctions.Exp /-! # Trigonometric functions ## Main definitions This file contains the definition of `π`. See also `Analysis.SpecialFunctions.Trigonometric.Inverse` and `Analysis.SpecialFunctions.Trigonometric.Arctan` for the inverse trigonometric functions. See also `Analysis.SpecialFunctions.Complex.Arg` and `Analysis.SpecialFunctions.Complex.Log` for the complex argument function and the complex logarithm. ## Main statements Many basic inequalities on the real trigonometric functions are established. The continuity of the usual trigonometric functions is proved. Several facts about the real trigonometric functions have the proofs deferred to `Analysis.SpecialFunctions.Trigonometric.Complex`, as they are most easily proved by appealing to the corresponding fact for complex trigonometric functions. See also `Analysis.SpecialFunctions.Trigonometric.Chebyshev` for the multiple angle formulas in terms of Chebyshev polynomials. ## Tags sin, cos, tan, angle -/ noncomputable section open Topology Filter Set namespace Complex @[continuity, fun_prop] theorem continuous_sin : Continuous sin := by change Continuous fun z => (exp (-z * I) - exp (z * I)) * I / 2 fun_prop @[fun_prop] theorem continuousOn_sin {s : Set ℂ} : ContinuousOn sin s := continuous_sin.continuousOn @[continuity, fun_prop] theorem continuous_cos : Continuous cos := by change Continuous fun z => (exp (z * I) + exp (-z * I)) / 2 fun_prop @[fun_prop] theorem continuousOn_cos {s : Set ℂ} : ContinuousOn cos s := continuous_cos.continuousOn @[continuity, fun_prop] theorem continuous_sinh : Continuous sinh := by change Continuous fun z => (exp z - exp (-z)) / 2 fun_prop @[continuity, fun_prop] theorem continuous_cosh : Continuous cosh := by change Continuous fun z => (exp z + exp (-z)) / 2 fun_prop end Complex namespace Real variable {x y z : ℝ} @[continuity, fun_prop] theorem continuous_sin : Continuous sin := Complex.continuous_re.comp (Complex.continuous_sin.comp Complex.continuous_ofReal) @[fun_prop] theorem continuousOn_sin {s} : ContinuousOn sin s := continuous_sin.continuousOn @[continuity, fun_prop] theorem continuous_cos : Continuous cos := Complex.continuous_re.comp (Complex.continuous_cos.comp Complex.continuous_ofReal) @[fun_prop] theorem continuousOn_cos {s} : ContinuousOn cos s := continuous_cos.continuousOn @[continuity, fun_prop] theorem continuous_sinh : Continuous sinh := Complex.continuous_re.comp (Complex.continuous_sinh.comp Complex.continuous_ofReal) @[continuity, fun_prop] theorem continuous_cosh : Continuous cosh := Complex.continuous_re.comp (Complex.continuous_cosh.comp Complex.continuous_ofReal) end Real namespace Real theorem exists_cos_eq_zero : 0 ∈ cos '' Icc (1 : ℝ) 2 := intermediate_value_Icc' (by norm_num) continuousOn_cos ⟨le_of_lt cos_two_neg, le_of_lt cos_one_pos⟩ /-- The number π = 3.14159265... Defined here using choice as twice a zero of cos in [1,2], from which one can derive all its properties. For explicit bounds on π, see `Data.Real.Pi.Bounds`. Denoted `π`, once the `Real` namespace is opened. -/ protected noncomputable def pi : ℝ := 2 * Classical.choose exists_cos_eq_zero @[inherit_doc] scoped notation "π" => Real.pi @[simp] theorem cos_pi_div_two : cos (π / 2) = 0 := by rw [Real.pi, mul_div_cancel_left₀ _ (two_ne_zero' ℝ)] exact (Classical.choose_spec exists_cos_eq_zero).2 theorem one_le_pi_div_two : (1 : ℝ) ≤ π / 2 := by rw [Real.pi, mul_div_cancel_left₀ _ (two_ne_zero' ℝ)] exact (Classical.choose_spec exists_cos_eq_zero).1.1 theorem pi_div_two_le_two : π / 2 ≤ 2 := by rw [Real.pi, mul_div_cancel_left₀ _ (two_ne_zero' ℝ)] exact (Classical.choose_spec exists_cos_eq_zero).1.2 theorem two_le_pi : (2 : ℝ) ≤ π := (div_le_div_iff_of_pos_right (show (0 : ℝ) < 2 by norm_num)).1 (by rw [div_self (two_ne_zero' ℝ)]; exact one_le_pi_div_two) theorem pi_le_four : π ≤ 4 := (div_le_div_iff_of_pos_right (show (0 : ℝ) < 2 by norm_num)).1 (calc π / 2 ≤ 2 := pi_div_two_le_two _ = 4 / 2 := by norm_num) @[bound] theorem pi_pos : 0 < π := lt_of_lt_of_le (by norm_num) two_le_pi @[bound] theorem pi_nonneg : 0 ≤ π := pi_pos.le theorem pi_ne_zero : π ≠ 0 := pi_pos.ne' theorem pi_div_two_pos : 0 < π / 2 := half_pos pi_pos theorem two_pi_pos : 0 < 2 * π := by linarith [pi_pos] end Real namespace Mathlib.Meta.Positivity open Lean.Meta Qq /-- Extension for the `positivity` tactic: `π` is always positive. -/ @[positivity Real.pi] def evalRealPi : PositivityExt where eval {u α} _zα _pα e := do match u, α, e with | 0, ~q(ℝ), ~q(Real.pi) => assertInstancesCommute pure (.positive q(Real.pi_pos)) | _, _, _ => throwError "not Real.pi" end Mathlib.Meta.Positivity namespace NNReal open Real open Real NNReal /-- `π` considered as a nonnegative real. -/ noncomputable def pi : ℝ≥0 := ⟨π, Real.pi_pos.le⟩ @[simp] theorem coe_real_pi : (pi : ℝ) = π := rfl theorem pi_pos : 0 < pi := mod_cast Real.pi_pos theorem pi_ne_zero : pi ≠ 0 := pi_pos.ne' end NNReal namespace Real @[simp] theorem sin_pi : sin π = 0 := by rw [← mul_div_cancel_left₀ π (two_ne_zero' ℝ), two_mul, add_div, sin_add, cos_pi_div_two]; simp @[simp] theorem cos_pi : cos π = -1 := by rw [← mul_div_cancel_left₀ π (two_ne_zero' ℝ), mul_div_assoc, cos_two_mul, cos_pi_div_two] norm_num @[simp] theorem sin_two_pi : sin (2 * π) = 0 := by simp [two_mul, sin_add] @[simp] theorem cos_two_pi : cos (2 * π) = 1 := by simp [two_mul, cos_add] theorem sin_antiperiodic : Function.Antiperiodic sin π := by simp [sin_add] theorem sin_periodic : Function.Periodic sin (2 * π) := sin_antiperiodic.periodic_two_mul @[simp] theorem sin_add_pi (x : ℝ) : sin (x + π) = -sin x := sin_antiperiodic x @[simp] theorem sin_add_two_pi (x : ℝ) : sin (x + 2 * π) = sin x := sin_periodic x @[simp] theorem sin_sub_pi (x : ℝ) : sin (x - π) = -sin x := sin_antiperiodic.sub_eq x @[simp] theorem sin_sub_two_pi (x : ℝ) : sin (x - 2 * π) = sin x := sin_periodic.sub_eq x @[simp] theorem sin_pi_sub (x : ℝ) : sin (π - x) = sin x := neg_neg (sin x) ▸ sin_neg x ▸ sin_antiperiodic.sub_eq' @[simp] theorem sin_two_pi_sub (x : ℝ) : sin (2 * π - x) = -sin x := sin_neg x ▸ sin_periodic.sub_eq' @[simp] theorem sin_nat_mul_pi (n : ℕ) : sin (n * π) = 0 := sin_antiperiodic.nat_mul_eq_of_eq_zero sin_zero n @[simp] theorem sin_int_mul_pi (n : ℤ) : sin (n * π) = 0 := sin_antiperiodic.int_mul_eq_of_eq_zero sin_zero n @[simp] theorem sin_add_nat_mul_two_pi (x : ℝ) (n : ℕ) : sin (x + n * (2 * π)) = sin x := sin_periodic.nat_mul n x @[simp] theorem sin_add_int_mul_two_pi (x : ℝ) (n : ℤ) : sin (x + n * (2 * π)) = sin x := sin_periodic.int_mul n x @[simp] theorem sin_sub_nat_mul_two_pi (x : ℝ) (n : ℕ) : sin (x - n * (2 * π)) = sin x := sin_periodic.sub_nat_mul_eq n @[simp] theorem sin_sub_int_mul_two_pi (x : ℝ) (n : ℤ) : sin (x - n * (2 * π)) = sin x := sin_periodic.sub_int_mul_eq n @[simp] theorem sin_nat_mul_two_pi_sub (x : ℝ) (n : ℕ) : sin (n * (2 * π) - x) = -sin x := sin_neg x ▸ sin_periodic.nat_mul_sub_eq n @[simp] theorem sin_int_mul_two_pi_sub (x : ℝ) (n : ℤ) : sin (n * (2 * π) - x) = -sin x := sin_neg x ▸ sin_periodic.int_mul_sub_eq n theorem sin_add_int_mul_pi (x : ℝ) (n : ℤ) : sin (x + n * π) = (-1) ^ n * sin x := n.cast_negOnePow ℝ ▸ sin_antiperiodic.add_int_mul_eq n theorem sin_add_nat_mul_pi (x : ℝ) (n : ℕ) : sin (x + n * π) = (-1) ^ n * sin x := sin_antiperiodic.add_nat_mul_eq n theorem sin_sub_int_mul_pi (x : ℝ) (n : ℤ) : sin (x - n * π) = (-1) ^ n * sin x := n.cast_negOnePow ℝ ▸ sin_antiperiodic.sub_int_mul_eq n theorem sin_sub_nat_mul_pi (x : ℝ) (n : ℕ) : sin (x - n * π) = (-1) ^ n * sin x := sin_antiperiodic.sub_nat_mul_eq n theorem sin_int_mul_pi_sub (x : ℝ) (n : ℤ) : sin (n * π - x) = -((-1) ^ n * sin x) := by simpa only [sin_neg, mul_neg, Int.cast_negOnePow] using sin_antiperiodic.int_mul_sub_eq n theorem sin_nat_mul_pi_sub (x : ℝ) (n : ℕ) : sin (n * π - x) = -((-1) ^ n * sin x) := by simpa only [sin_neg, mul_neg] using sin_antiperiodic.nat_mul_sub_eq n theorem cos_antiperiodic : Function.Antiperiodic cos π := by simp [cos_add] theorem cos_periodic : Function.Periodic cos (2 * π) := cos_antiperiodic.periodic_two_mul @[simp] theorem abs_cos_int_mul_pi (k : ℤ) : |cos (k * π)| = 1 := by simp [abs_cos_eq_sqrt_one_sub_sin_sq] @[simp] theorem cos_add_pi (x : ℝ) : cos (x + π) = -cos x := cos_antiperiodic x @[simp] theorem cos_add_two_pi (x : ℝ) : cos (x + 2 * π) = cos x := cos_periodic x @[simp] theorem cos_sub_pi (x : ℝ) : cos (x - π) = -cos x := cos_antiperiodic.sub_eq x @[simp] theorem cos_sub_two_pi (x : ℝ) : cos (x - 2 * π) = cos x := cos_periodic.sub_eq x @[simp] theorem cos_pi_sub (x : ℝ) : cos (π - x) = -cos x := cos_neg x ▸ cos_antiperiodic.sub_eq' @[simp] theorem cos_two_pi_sub (x : ℝ) : cos (2 * π - x) = cos x := cos_neg x ▸ cos_periodic.sub_eq' @[simp] theorem cos_nat_mul_two_pi (n : ℕ) : cos (n * (2 * π)) = 1 := (cos_periodic.nat_mul_eq n).trans cos_zero @[simp] theorem cos_int_mul_two_pi (n : ℤ) : cos (n * (2 * π)) = 1 := (cos_periodic.int_mul_eq n).trans cos_zero @[simp] theorem cos_add_nat_mul_two_pi (x : ℝ) (n : ℕ) : cos (x + n * (2 * π)) = cos x := cos_periodic.nat_mul n x @[simp] theorem cos_add_int_mul_two_pi (x : ℝ) (n : ℤ) : cos (x + n * (2 * π)) = cos x := cos_periodic.int_mul n x @[simp] theorem cos_sub_nat_mul_two_pi (x : ℝ) (n : ℕ) : cos (x - n * (2 * π)) = cos x := cos_periodic.sub_nat_mul_eq n @[simp] theorem cos_sub_int_mul_two_pi (x : ℝ) (n : ℤ) : cos (x - n * (2 * π)) = cos x := cos_periodic.sub_int_mul_eq n @[simp] theorem cos_nat_mul_two_pi_sub (x : ℝ) (n : ℕ) : cos (n * (2 * π) - x) = cos x := cos_neg x ▸ cos_periodic.nat_mul_sub_eq n @[simp] theorem cos_int_mul_two_pi_sub (x : ℝ) (n : ℤ) : cos (n * (2 * π) - x) = cos x := cos_neg x ▸ cos_periodic.int_mul_sub_eq n theorem cos_add_int_mul_pi (x : ℝ) (n : ℤ) : cos (x + n * π) = (-1) ^ n * cos x := n.cast_negOnePow ℝ ▸ cos_antiperiodic.add_int_mul_eq n theorem cos_add_nat_mul_pi (x : ℝ) (n : ℕ) : cos (x + n * π) = (-1) ^ n * cos x := cos_antiperiodic.add_nat_mul_eq n theorem cos_sub_int_mul_pi (x : ℝ) (n : ℤ) : cos (x - n * π) = (-1) ^ n * cos x := n.cast_negOnePow ℝ ▸ cos_antiperiodic.sub_int_mul_eq n theorem cos_sub_nat_mul_pi (x : ℝ) (n : ℕ) : cos (x - n * π) = (-1) ^ n * cos x := cos_antiperiodic.sub_nat_mul_eq n theorem cos_int_mul_pi_sub (x : ℝ) (n : ℤ) : cos (n * π - x) = (-1) ^ n * cos x := n.cast_negOnePow ℝ ▸ cos_neg x ▸ cos_antiperiodic.int_mul_sub_eq n theorem cos_nat_mul_pi_sub (x : ℝ) (n : ℕ) : cos (n * π - x) = (-1) ^ n * cos x := cos_neg x ▸ cos_antiperiodic.nat_mul_sub_eq n theorem cos_nat_mul_two_pi_add_pi (n : ℕ) : cos (n * (2 * π) + π) = -1 := by simpa only [cos_zero] using (cos_periodic.nat_mul n).add_antiperiod_eq cos_antiperiodic theorem cos_int_mul_two_pi_add_pi (n : ℤ) : cos (n * (2 * π) + π) = -1 := by simpa only [cos_zero] using (cos_periodic.int_mul n).add_antiperiod_eq cos_antiperiodic theorem cos_nat_mul_two_pi_sub_pi (n : ℕ) : cos (n * (2 * π) - π) = -1 := by simpa only [cos_zero] using (cos_periodic.nat_mul n).sub_antiperiod_eq cos_antiperiodic theorem cos_int_mul_two_pi_sub_pi (n : ℤ) : cos (n * (2 * π) - π) = -1 := by simpa only [cos_zero] using (cos_periodic.int_mul n).sub_antiperiod_eq cos_antiperiodic theorem sin_pos_of_pos_of_lt_pi {x : ℝ} (h0x : 0 < x) (hxp : x < π) : 0 < sin x := if hx2 : x ≤ 2 then sin_pos_of_pos_of_le_two h0x hx2 else have : (2 : ℝ) + 2 = 4 := by norm_num have : π - x ≤ 2 := sub_le_iff_le_add.2 (le_trans pi_le_four (this ▸ add_le_add_left (le_of_not_ge hx2) _)) sin_pi_sub x ▸ sin_pos_of_pos_of_le_two (sub_pos.2 hxp) this theorem sin_pos_of_mem_Ioo {x : ℝ} (hx : x ∈ Ioo 0 π) : 0 < sin x := sin_pos_of_pos_of_lt_pi hx.1 hx.2 theorem sin_nonneg_of_mem_Icc {x : ℝ} (hx : x ∈ Icc 0 π) : 0 ≤ sin x := by rw [← closure_Ioo pi_ne_zero.symm] at hx exact closure_lt_subset_le continuous_const continuous_sin (closure_mono (fun y => sin_pos_of_mem_Ioo) hx) theorem sin_nonneg_of_nonneg_of_le_pi {x : ℝ} (h0x : 0 ≤ x) (hxp : x ≤ π) : 0 ≤ sin x := sin_nonneg_of_mem_Icc ⟨h0x, hxp⟩ theorem sin_neg_of_neg_of_neg_pi_lt {x : ℝ} (hx0 : x < 0) (hpx : -π < x) : sin x < 0 := neg_pos.1 <| sin_neg x ▸ sin_pos_of_pos_of_lt_pi (neg_pos.2 hx0) (neg_lt.1 hpx) theorem sin_nonpos_of_nonnpos_of_neg_pi_le {x : ℝ} (hx0 : x ≤ 0) (hpx : -π ≤ x) : sin x ≤ 0 := neg_nonneg.1 <| sin_neg x ▸ sin_nonneg_of_nonneg_of_le_pi (neg_nonneg.2 hx0) (neg_le.1 hpx) @[simp] theorem sin_pi_div_two : sin (π / 2) = 1 := have : sin (π / 2) = 1 ∨ sin (π / 2) = -1 := by simpa [sq, mul_self_eq_one_iff] using sin_sq_add_cos_sq (π / 2) this.resolve_right fun h => show ¬(0 : ℝ) < -1 by norm_num <| h ▸ sin_pos_of_pos_of_lt_pi pi_div_two_pos (half_lt_self pi_pos) theorem sin_add_pi_div_two (x : ℝ) : sin (x + π / 2) = cos x := by simp [sin_add] theorem sin_sub_pi_div_two (x : ℝ) : sin (x - π / 2) = -cos x := by simp [sub_eq_add_neg, sin_add] theorem sin_pi_div_two_sub (x : ℝ) : sin (π / 2 - x) = cos x := by simp [sub_eq_add_neg, sin_add] theorem cos_add_pi_div_two (x : ℝ) : cos (x + π / 2) = -sin x := by simp [cos_add] theorem cos_sub_pi_div_two (x : ℝ) : cos (x - π / 2) = sin x := by simp [sub_eq_add_neg, cos_add] theorem cos_pi_div_two_sub (x : ℝ) : cos (π / 2 - x) = sin x := by rw [← cos_neg, neg_sub, cos_sub_pi_div_two] theorem cos_pos_of_mem_Ioo {x : ℝ} (hx : x ∈ Ioo (-(π / 2)) (π / 2)) : 0 < cos x := sin_add_pi_div_two x ▸ sin_pos_of_mem_Ioo ⟨by linarith [hx.1], by linarith [hx.2]⟩ theorem cos_nonneg_of_mem_Icc {x : ℝ} (hx : x ∈ Icc (-(π / 2)) (π / 2)) : 0 ≤ cos x := sin_add_pi_div_two x ▸ sin_nonneg_of_mem_Icc ⟨by linarith [hx.1], by linarith [hx.2]⟩ theorem cos_nonneg_of_neg_pi_div_two_le_of_le {x : ℝ} (hl : -(π / 2) ≤ x) (hu : x ≤ π / 2) : 0 ≤ cos x := cos_nonneg_of_mem_Icc ⟨hl, hu⟩ theorem cos_neg_of_pi_div_two_lt_of_lt {x : ℝ} (hx₁ : π / 2 < x) (hx₂ : x < π + π / 2) : cos x < 0 := neg_pos.1 <| cos_pi_sub x ▸ cos_pos_of_mem_Ioo ⟨by linarith, by linarith⟩ theorem cos_nonpos_of_pi_div_two_le_of_le {x : ℝ} (hx₁ : π / 2 ≤ x) (hx₂ : x ≤ π + π / 2) : cos x ≤ 0 :=
neg_nonneg.1 <| cos_pi_sub x ▸ cos_nonneg_of_mem_Icc ⟨by linarith, by linarith⟩
Mathlib/Analysis/SpecialFunctions/Trigonometric/Basic.lean
457
458
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Jeremy Avigad, Simon Hudon -/ import Mathlib.Algebra.Notation.Defs import Mathlib.Data.Set.Subsingleton import Mathlib.Logic.Equiv.Defs /-! # Partial values of a type This file defines `Part α`, the partial values of a type. `o : Part α` carries a proposition `o.Dom`, its domain, along with a function `get : o.Dom → α`, its value. The rule is then that every partial value has a value but, to access it, you need to provide a proof of the domain. `Part α` behaves the same as `Option α` except that `o : Option α` is decidably `none` or `some a` for some `a : α`, while the domain of `o : Part α` doesn't have to be decidable. That means you can translate back and forth between a partial value with a decidable domain and an option, and `Option α` and `Part α` are classically equivalent. In general, `Part α` is bigger than `Option α`. In current mathlib, `Part ℕ`, aka `PartENat`, is used to move decidability of the order to decidability of `PartENat.find` (which is the smallest natural satisfying a predicate, or `∞` if there's none). ## Main declarations `Option`-like declarations: * `Part.none`: The partial value whose domain is `False`. * `Part.some a`: The partial value whose domain is `True` and whose value is `a`. * `Part.ofOption`: Converts an `Option α` to a `Part α` by sending `none` to `none` and `some a` to `some a`. * `Part.toOption`: Converts a `Part α` with a decidable domain to an `Option α`. * `Part.equivOption`: Classical equivalence between `Part α` and `Option α`. Monadic structure: * `Part.bind`: `o.bind f` has value `(f (o.get _)).get _` (`f o` morally) and is defined when `o` and `f (o.get _)` are defined. * `Part.map`: Maps the value and keeps the same domain. Other: * `Part.restrict`: `Part.restrict p o` replaces the domain of `o : Part α` by `p : Prop` so long as `p → o.Dom`. * `Part.assert`: `assert p f` appends `p` to the domains of the values of a partial function. * `Part.unwrap`: Gets the value of a partial value regardless of its domain. Unsound. ## Notation For `a : α`, `o : Part α`, `a ∈ o` means that `o` is defined and equal to `a`. Formally, it means `o.Dom` and `o.get _ = a`. -/ assert_not_exists RelIso open Function /-- `Part α` is the type of "partial values" of type `α`. It is similar to `Option α` except the domain condition can be an arbitrary proposition, not necessarily decidable. -/ structure Part.{u} (α : Type u) : Type u where /-- The domain of a partial value -/ Dom : Prop /-- Extract a value from a partial value given a proof of `Dom` -/ get : Dom → α namespace Part variable {α : Type*} {β : Type*} {γ : Type*} /-- Convert a `Part α` with a decidable domain to an option -/ def toOption (o : Part α) [Decidable o.Dom] : Option α := if h : Dom o then some (o.get h) else none @[simp] lemma toOption_isSome (o : Part α) [Decidable o.Dom] : o.toOption.isSome ↔ o.Dom := by by_cases h : o.Dom <;> simp [h, toOption] @[simp] lemma toOption_eq_none (o : Part α) [Decidable o.Dom] : o.toOption = none ↔ ¬o.Dom := by by_cases h : o.Dom <;> simp [h, toOption] /-- `Part` extensionality -/ theorem ext' : ∀ {o p : Part α}, (o.Dom ↔ p.Dom) → (∀ h₁ h₂, o.get h₁ = p.get h₂) → o = p | ⟨od, o⟩, ⟨pd, p⟩, H1, H2 => by have t : od = pd := propext H1 cases t; rw [show o = p from funext fun p => H2 p p] /-- `Part` eta expansion -/ @[simp] theorem eta : ∀ o : Part α, (⟨o.Dom, fun h => o.get h⟩ : Part α) = o | ⟨_, _⟩ => rfl /-- `a ∈ o` means that `o` is defined and equal to `a` -/ protected def Mem (o : Part α) (a : α) : Prop := ∃ h, o.get h = a instance : Membership α (Part α) := ⟨Part.Mem⟩ theorem mem_eq (a : α) (o : Part α) : (a ∈ o) = ∃ h, o.get h = a := rfl theorem dom_iff_mem : ∀ {o : Part α}, o.Dom ↔ ∃ y, y ∈ o | ⟨_, f⟩ => ⟨fun h => ⟨f h, h, rfl⟩, fun ⟨_, h, rfl⟩ => h⟩ theorem get_mem {o : Part α} (h) : get o h ∈ o := ⟨_, rfl⟩ @[simp] theorem mem_mk_iff {p : Prop} {o : p → α} {a : α} : a ∈ Part.mk p o ↔ ∃ h, o h = a := Iff.rfl /-- `Part` extensionality -/ @[ext] theorem ext {o p : Part α} (H : ∀ a, a ∈ o ↔ a ∈ p) : o = p := (ext' ⟨fun h => ((H _).1 ⟨h, rfl⟩).fst, fun h => ((H _).2 ⟨h, rfl⟩).fst⟩) fun _ _ => ((H _).2 ⟨_, rfl⟩).snd /-- The `none` value in `Part` has a `False` domain and an empty function. -/ def none : Part α := ⟨False, False.rec⟩ instance : Inhabited (Part α) := ⟨none⟩ @[simp] theorem not_mem_none (a : α) : a ∉ @none α := fun h => h.fst /-- The `some a` value in `Part` has a `True` domain and the function returns `a`. -/ def some (a : α) : Part α := ⟨True, fun _ => a⟩ @[simp] theorem some_dom (a : α) : (some a).Dom := trivial theorem mem_unique : ∀ {a b : α} {o : Part α}, a ∈ o → b ∈ o → a = b | _, _, ⟨_, _⟩, ⟨_, rfl⟩, ⟨_, rfl⟩ => rfl theorem mem_right_unique : ∀ {a : α} {o p : Part α}, a ∈ o → a ∈ p → o = p | _, _, _, ⟨ho, _⟩, ⟨hp, _⟩ => ext' (iff_of_true ho hp) (by simp [*]) theorem Mem.left_unique : Relator.LeftUnique ((· ∈ ·) : α → Part α → Prop) := fun _ _ _ => mem_unique theorem Mem.right_unique : Relator.RightUnique ((· ∈ ·) : α → Part α → Prop) := fun _ _ _ => mem_right_unique theorem get_eq_of_mem {o : Part α} {a} (h : a ∈ o) (h') : get o h' = a := mem_unique ⟨_, rfl⟩ h protected theorem subsingleton (o : Part α) : Set.Subsingleton { a | a ∈ o } := fun _ ha _ hb => mem_unique ha hb @[simp] theorem get_some {a : α} (ha : (some a).Dom) : get (some a) ha = a := rfl theorem mem_some (a : α) : a ∈ some a := ⟨trivial, rfl⟩ @[simp] theorem mem_some_iff {a b} : b ∈ (some a : Part α) ↔ b = a := ⟨fun ⟨_, e⟩ => e.symm, fun e => ⟨trivial, e.symm⟩⟩ theorem eq_some_iff {a : α} {o : Part α} : o = some a ↔ a ∈ o := ⟨fun e => e.symm ▸ mem_some _, fun ⟨h, e⟩ => e ▸ ext' (iff_true_intro h) fun _ _ => rfl⟩ theorem eq_none_iff {o : Part α} : o = none ↔ ∀ a, a ∉ o := ⟨fun e => e.symm ▸ not_mem_none, fun h => ext (by simpa)⟩ theorem eq_none_iff' {o : Part α} : o = none ↔ ¬o.Dom := ⟨fun e => e.symm ▸ id, fun h => eq_none_iff.2 fun _ h' => h h'.fst⟩ @[simp] theorem not_none_dom : ¬(none : Part α).Dom := id @[simp] theorem some_ne_none (x : α) : some x ≠ none := by intro h exact true_ne_false (congr_arg Dom h) @[simp] theorem none_ne_some (x : α) : none ≠ some x := (some_ne_none x).symm theorem ne_none_iff {o : Part α} : o ≠ none ↔ ∃ x, o = some x := by constructor · rw [Ne, eq_none_iff', not_not] exact fun h => ⟨o.get h, eq_some_iff.2 (get_mem h)⟩ · rintro ⟨x, rfl⟩ apply some_ne_none theorem eq_none_or_eq_some (o : Part α) : o = none ∨ ∃ x, o = some x := or_iff_not_imp_left.2 ne_none_iff.1 theorem some_injective : Injective (@Part.some α) := fun _ _ h => congr_fun (eq_of_heq (Part.mk.inj h).2) trivial @[simp] theorem some_inj {a b : α} : Part.some a = some b ↔ a = b := some_injective.eq_iff @[simp] theorem some_get {a : Part α} (ha : a.Dom) : Part.some (Part.get a ha) = a := Eq.symm (eq_some_iff.2 ⟨ha, rfl⟩) theorem get_eq_iff_eq_some {a : Part α} {ha : a.Dom} {b : α} : a.get ha = b ↔ a = some b := ⟨fun h => by simp [h.symm], fun h => by simp [h]⟩ theorem get_eq_get_of_eq (a : Part α) (ha : a.Dom) {b : Part α} (h : a = b) : a.get ha = b.get (h ▸ ha) := by congr theorem get_eq_iff_mem {o : Part α} {a : α} (h : o.Dom) : o.get h = a ↔ a ∈ o := ⟨fun H => ⟨h, H⟩, fun ⟨_, H⟩ => H⟩ theorem eq_get_iff_mem {o : Part α} {a : α} (h : o.Dom) : a = o.get h ↔ a ∈ o := eq_comm.trans (get_eq_iff_mem h) @[simp] theorem none_toOption [Decidable (@none α).Dom] : (none : Part α).toOption = Option.none := dif_neg id @[simp] theorem some_toOption (a : α) [Decidable (some a).Dom] : (some a).toOption = Option.some a := dif_pos trivial instance noneDecidable : Decidable (@none α).Dom := instDecidableFalse instance someDecidable (a : α) : Decidable (some a).Dom := instDecidableTrue /-- Retrieves the value of `a : Part α` if it exists, and return the provided default value otherwise. -/ def getOrElse (a : Part α) [Decidable a.Dom] (d : α) := if ha : a.Dom then a.get ha else d theorem getOrElse_of_dom (a : Part α) (h : a.Dom) [Decidable a.Dom] (d : α) : getOrElse a d = a.get h := dif_pos h theorem getOrElse_of_not_dom (a : Part α) (h : ¬a.Dom) [Decidable a.Dom] (d : α) : getOrElse a d = d := dif_neg h @[simp] theorem getOrElse_none (d : α) [Decidable (none : Part α).Dom] : getOrElse none d = d := none.getOrElse_of_not_dom not_none_dom d @[simp] theorem getOrElse_some (a : α) (d : α) [Decidable (some a).Dom] : getOrElse (some a) d = a := (some a).getOrElse_of_dom (some_dom a) d -- `simp`-normal form is `toOption_eq_some_iff`. theorem mem_toOption {o : Part α} [Decidable o.Dom] {a : α} : a ∈ toOption o ↔ a ∈ o := by unfold toOption by_cases h : o.Dom <;> simp [h] · exact ⟨fun h => ⟨_, h⟩, fun ⟨_, h⟩ => h⟩ · exact mt Exists.fst h @[simp] theorem toOption_eq_some_iff {o : Part α} [Decidable o.Dom] {a : α} : toOption o = Option.some a ↔ a ∈ o := by rw [← Option.mem_def, mem_toOption] protected theorem Dom.toOption {o : Part α} [Decidable o.Dom] (h : o.Dom) : o.toOption = o.get h := dif_pos h theorem toOption_eq_none_iff {a : Part α} [Decidable a.Dom] : a.toOption = Option.none ↔ ¬a.Dom := Ne.dite_eq_right_iff fun _ => Option.some_ne_none _ @[simp] theorem elim_toOption {α β : Type*} (a : Part α) [Decidable a.Dom] (b : β) (f : α → β) : a.toOption.elim b f = if h : a.Dom then f (a.get h) else b := by split_ifs with h · rw [h.toOption] rfl · rw [Part.toOption_eq_none_iff.2 h] rfl /-- Converts an `Option α` into a `Part α`. -/ @[coe] def ofOption : Option α → Part α | Option.none => none | Option.some a => some a @[simp] theorem mem_ofOption {a : α} : ∀ {o : Option α}, a ∈ ofOption o ↔ a ∈ o | Option.none => ⟨fun h => h.fst.elim, fun h => Option.noConfusion h⟩ | Option.some _ => ⟨fun h => congr_arg Option.some h.snd, fun h => ⟨trivial, Option.some.inj h⟩⟩ @[simp] theorem ofOption_dom {α} : ∀ o : Option α, (ofOption o).Dom ↔ o.isSome | Option.none => by simp [ofOption, none] | Option.some a => by simp [ofOption] theorem ofOption_eq_get {α} (o : Option α) : ofOption o = ⟨_, @Option.get _ o⟩ := Part.ext' (ofOption_dom o) fun h₁ h₂ => by cases o · simp at h₂ · rfl instance : Coe (Option α) (Part α) := ⟨ofOption⟩ theorem mem_coe {a : α} {o : Option α} : a ∈ (o : Part α) ↔ a ∈ o := mem_ofOption @[simp] theorem coe_none : (@Option.none α : Part α) = none := rfl @[simp] theorem coe_some (a : α) : (Option.some a : Part α) = some a := rfl @[elab_as_elim] protected theorem induction_on {P : Part α → Prop} (a : Part α) (hnone : P none) (hsome : ∀ a : α, P (some a)) : P a := (Classical.em a.Dom).elim (fun h => Part.some_get h ▸ hsome _) fun h => (eq_none_iff'.2 h).symm ▸ hnone instance ofOptionDecidable : ∀ o : Option α, Decidable (ofOption o).Dom | Option.none => Part.noneDecidable | Option.some a => Part.someDecidable a @[simp] theorem to_ofOption (o : Option α) : toOption (ofOption o) = o := by cases o <;> rfl @[simp] theorem of_toOption (o : Part α) [Decidable o.Dom] : ofOption (toOption o) = o := ext fun _ => mem_ofOption.trans mem_toOption /-- `Part α` is (classically) equivalent to `Option α`. -/ noncomputable def equivOption : Part α ≃ Option α := haveI := Classical.dec ⟨fun o => toOption o, ofOption, fun o => of_toOption o, fun o => Eq.trans (by dsimp; congr) (to_ofOption o)⟩ /-- We give `Part α` the order where everything is greater than `none`. -/ instance : PartialOrder (Part α) where le x y := ∀ i, i ∈ x → i ∈ y le_refl _ _ := id le_trans _ _ _ f g _ := g _ ∘ f _ le_antisymm _ _ f g := Part.ext fun _ => ⟨f _, g _⟩ instance : OrderBot (Part α) where bot := none bot_le := by rintro x _ ⟨⟨_⟩, _⟩ theorem le_total_of_le_of_le {x y : Part α} (z : Part α) (hx : x ≤ z) (hy : y ≤ z) : x ≤ y ∨ y ≤ x := by rcases Part.eq_none_or_eq_some x with (h | ⟨b, h₀⟩) · rw [h] left apply OrderBot.bot_le _ right; intro b' h₁ rw [Part.eq_some_iff] at h₀ have hx := hx _ h₀; have hy := hy _ h₁ have hx := Part.mem_unique hx hy; subst hx exact h₀ /-- `assert p f` is a bind-like operation which appends an additional condition `p` to the domain and uses `f` to produce the value. -/ def assert (p : Prop) (f : p → Part α) : Part α := ⟨∃ h : p, (f h).Dom, fun ha => (f ha.fst).get ha.snd⟩ /-- The bind operation has value `g (f.get)`, and is defined when all the parts are defined. -/ protected def bind (f : Part α) (g : α → Part β) : Part β := assert (Dom f) fun b => g (f.get b) /-- The map operation for `Part` just maps the value and maintains the same domain. -/ @[simps] def map (f : α → β) (o : Part α) : Part β := ⟨o.Dom, f ∘ o.get⟩ theorem mem_map (f : α → β) {o : Part α} : ∀ {a}, a ∈ o → f a ∈ map f o | _, ⟨_, rfl⟩ => ⟨_, rfl⟩ @[simp] theorem mem_map_iff (f : α → β) {o : Part α} {b} : b ∈ map f o ↔ ∃ a ∈ o, f a = b := ⟨fun hb => match b, hb with | _, ⟨_, rfl⟩ => ⟨_, ⟨_, rfl⟩, rfl⟩, fun ⟨_, h₁, h₂⟩ => h₂ ▸ mem_map f h₁⟩ @[simp] theorem map_none (f : α → β) : map f none = none := eq_none_iff.2 fun a => by simp @[simp] theorem map_some (f : α → β) (a : α) : map f (some a) = some (f a) := eq_some_iff.2 <| mem_map f <| mem_some _ theorem mem_assert {p : Prop} {f : p → Part α} : ∀ {a} (h : p), a ∈ f h → a ∈ assert p f | _, x, ⟨h, rfl⟩ => ⟨⟨x, h⟩, rfl⟩ @[simp] theorem mem_assert_iff {p : Prop} {f : p → Part α} {a} : a ∈ assert p f ↔ ∃ h : p, a ∈ f h := ⟨fun ha => match a, ha with | _, ⟨_, rfl⟩ => ⟨_, ⟨_, rfl⟩⟩, fun ⟨_, h⟩ => mem_assert _ h⟩ theorem assert_pos {p : Prop} {f : p → Part α} (h : p) : assert p f = f h := by dsimp [assert] cases h' : f h simp only [h', mk.injEq, h, exists_prop_of_true, true_and] apply Function.hfunext · simp only [h, h', exists_prop_of_true] · simp theorem assert_neg {p : Prop} {f : p → Part α} (h : ¬p) : assert p f = none := by dsimp [assert, none]; congr · simp only [h, not_false_iff, exists_prop_of_false] · apply Function.hfunext · simp only [h, not_false_iff, exists_prop_of_false] simp at * theorem mem_bind {f : Part α} {g : α → Part β} : ∀ {a b}, a ∈ f → b ∈ g a → b ∈ f.bind g | _, _, ⟨h, rfl⟩, ⟨h₂, rfl⟩ => ⟨⟨h, h₂⟩, rfl⟩ @[simp] theorem mem_bind_iff {f : Part α} {g : α → Part β} {b} : b ∈ f.bind g ↔ ∃ a ∈ f, b ∈ g a := ⟨fun hb => match b, hb with | _, ⟨⟨_, _⟩, rfl⟩ => ⟨_, ⟨_, rfl⟩, ⟨_, rfl⟩⟩, fun ⟨_, h₁, h₂⟩ => mem_bind h₁ h₂⟩ protected theorem Dom.bind {o : Part α} (h : o.Dom) (f : α → Part β) : o.bind f = f (o.get h) := by ext b simp only [Part.mem_bind_iff, exists_prop] refine ⟨?_, fun hb => ⟨o.get h, Part.get_mem _, hb⟩⟩ rintro ⟨a, ha, hb⟩ rwa [Part.get_eq_of_mem ha] theorem Dom.of_bind {f : α → Part β} {a : Part α} (h : (a.bind f).Dom) : a.Dom := h.1 @[simp] theorem bind_none (f : α → Part β) : none.bind f = none := eq_none_iff.2 fun a => by simp @[simp] theorem bind_some (a : α) (f : α → Part β) : (some a).bind f = f a := ext <| by simp theorem bind_of_mem {o : Part α} {a : α} (h : a ∈ o) (f : α → Part β) : o.bind f = f a := by rw [eq_some_iff.2 h, bind_some] theorem bind_some_eq_map (f : α → β) (x : Part α) : x.bind (fun y => some (f y)) = map f x := ext <| by simp [eq_comm] theorem bind_toOption (f : α → Part β) (o : Part α) [Decidable o.Dom] [∀ a, Decidable (f a).Dom] [Decidable (o.bind f).Dom] : (o.bind f).toOption = o.toOption.elim Option.none fun a => (f a).toOption := by by_cases h : o.Dom · simp_rw [h.toOption, h.bind] rfl · rw [Part.toOption_eq_none_iff.2 h] exact Part.toOption_eq_none_iff.2 fun ho => h ho.of_bind theorem bind_assoc {γ} (f : Part α) (g : α → Part β) (k : β → Part γ) : (f.bind g).bind k = f.bind fun x => (g x).bind k := ext fun a => by simp only [mem_bind_iff] exact ⟨fun ⟨_, ⟨_, h₁, h₂⟩, h₃⟩ => ⟨_, h₁, _, h₂, h₃⟩, fun ⟨_, h₁, _, h₂, h₃⟩ => ⟨_, ⟨_, h₁, h₂⟩, h₃⟩⟩ @[simp] theorem bind_map {γ} (f : α → β) (x) (g : β → Part γ) : (map f x).bind g = x.bind fun y => g (f y) := by rw [← bind_some_eq_map, bind_assoc]; simp @[simp] theorem map_bind {γ} (f : α → Part β) (x : Part α) (g : β → γ) : map g (x.bind f) = x.bind fun y => map g (f y) := by rw [← bind_some_eq_map, bind_assoc]; simp [bind_some_eq_map] theorem map_map (g : β → γ) (f : α → β) (o : Part α) : map g (map f o) = map (g ∘ f) o := by simp [map, Function.comp_assoc] instance : Monad Part where pure := @some map := @map bind := @Part.bind instance : LawfulMonad Part where bind_pure_comp := @bind_some_eq_map id_map f := by cases f; rfl pure_bind := @bind_some bind_assoc := @bind_assoc map_const := by simp [Functor.mapConst, Functor.map] --Porting TODO : In Lean3 these were automatic by a tactic seqLeft_eq x y := ext' (by simp [SeqLeft.seqLeft, Part.bind, assert, Seq.seq, const, (· <$> ·), and_comm]) (fun _ _ => rfl) seqRight_eq x y := ext' (by simp [SeqRight.seqRight, Part.bind, assert, Seq.seq, const, (· <$> ·), and_comm]) (fun _ _ => rfl) pure_seq x y := ext' (by simp [Seq.seq, Part.bind, assert, (· <$> ·), pure]) (fun _ _ => rfl) bind_map x y := ext' (by simp [(· >>= ·), Part.bind, assert, Seq.seq, get, (· <$> ·)] ) (fun _ _ => rfl) theorem map_id' {f : α → α} (H : ∀ x : α, f x = x) (o) : map f o = o := by rw [show f = id from funext H]; exact id_map o @[simp] theorem bind_some_right (x : Part α) : x.bind some = x := by rw [bind_some_eq_map] simp [map_id'] @[simp] theorem pure_eq_some (a : α) : pure a = some a := rfl @[simp] theorem ret_eq_some (a : α) : (return a : Part α) = some a := rfl @[simp] theorem map_eq_map {α β} (f : α → β) (o : Part α) : f <$> o = map f o := rfl @[simp] theorem bind_eq_bind {α β} (f : Part α) (g : α → Part β) : f >>= g = f.bind g := rfl theorem bind_le {α} (x : Part α) (f : α → Part β) (y : Part β) : x >>= f ≤ y ↔ ∀ a, a ∈ x → f a ≤ y := by constructor <;> intro h · intro a h' b have h := h b simp only [and_imp, exists_prop, bind_eq_bind, mem_bind_iff, exists_imp] at h apply h _ h' · intro b h' simp only [exists_prop, bind_eq_bind, mem_bind_iff] at h' rcases h' with ⟨a, h₀, h₁⟩ apply h _ h₀ _ h₁ -- TODO: if `MonadFail` is defined, define the below instance. -- instance : MonadFail Part := -- { Part.monad with fail := fun _ _ => none } /-- `restrict p o h` replaces the domain of `o` with `p`, and is well defined when `p` implies `o` is defined. -/ def restrict (p : Prop) (o : Part α) (H : p → o.Dom) : Part α := ⟨p, fun h => o.get (H h)⟩ @[simp] theorem mem_restrict (p : Prop) (o : Part α) (h : p → o.Dom) (a : α) : a ∈ restrict p o h ↔ p ∧ a ∈ o := by dsimp [restrict, mem_eq]; constructor · rintro ⟨h₀, h₁⟩ exact ⟨h₀, ⟨_, h₁⟩⟩ rintro ⟨h₀, _, h₂⟩; exact ⟨h₀, h₂⟩ /-- `unwrap o` gets the value at `o`, ignoring the condition. This function is unsound. -/ unsafe def unwrap (o : Part α) : α := o.get lcProof theorem assert_defined {p : Prop} {f : p → Part α} : ∀ h : p, (f h).Dom → (assert p f).Dom := Exists.intro theorem bind_defined {f : Part α} {g : α → Part β} : ∀ h : f.Dom, (g (f.get h)).Dom → (f.bind g).Dom := assert_defined @[simp] theorem bind_dom {f : Part α} {g : α → Part β} : (f.bind g).Dom ↔ ∃ h : f.Dom, (g (f.get h)).Dom := Iff.rfl section Instances /-! We define several instances for constants and operations on `Part α` inherited from `α`. This section could be moved to a separate file to avoid the import of `Mathlib.Algebra.Group.Defs`. -/ @[to_additive] instance [One α] : One (Part α) where one := pure 1 @[to_additive] instance [Mul α] : Mul (Part α) where mul a b := (· * ·) <$> a <*> b @[to_additive] instance [Inv α] : Inv (Part α) where inv := map Inv.inv @[to_additive] instance [Div α] : Div (Part α) where div a b := (· / ·) <$> a <*> b instance [Mod α] : Mod (Part α) where mod a b := (· % ·) <$> a <*> b instance [Append α] : Append (Part α) where append a b := (· ++ ·) <$> a <*> b instance [Inter α] : Inter (Part α) where inter a b := (· ∩ ·) <$> a <*> b instance [Union α] : Union (Part α) where union a b := (· ∪ ·) <$> a <*> b instance [SDiff α] : SDiff (Part α) where sdiff a b := (· \ ·) <$> a <*> b section @[to_additive] theorem mul_def [Mul α] (a b : Part α) : a * b = bind a fun y ↦ map (y * ·) b := rfl @[to_additive] theorem one_def [One α] : (1 : Part α) = some 1 := rfl @[to_additive] theorem inv_def [Inv α] (a : Part α) : a⁻¹ = Part.map (· ⁻¹) a := rfl @[to_additive] theorem div_def [Div α] (a b : Part α) : a / b = bind a fun y => map (y / ·) b := rfl theorem mod_def [Mod α] (a b : Part α) : a % b = bind a fun y => map (y % ·) b := rfl theorem append_def [Append α] (a b : Part α) : a ++ b = bind a fun y => map (y ++ ·) b := rfl theorem inter_def [Inter α] (a b : Part α) : a ∩ b = bind a fun y => map (y ∩ ·) b := rfl theorem union_def [Union α] (a b : Part α) : a ∪ b = bind a fun y => map (y ∪ ·) b := rfl theorem sdiff_def [SDiff α] (a b : Part α) : a \ b = bind a fun y => map (y \ ·) b := rfl end @[to_additive] theorem one_mem_one [One α] : (1 : α) ∈ (1 : Part α) := ⟨trivial, rfl⟩ @[to_additive] theorem mul_mem_mul [Mul α] (a b : Part α) (ma mb : α) (ha : ma ∈ a) (hb : mb ∈ b) : ma * mb ∈ a * b := ⟨⟨ha.1, hb.1⟩, by simp only [← ha.2, ← hb.2]; rfl⟩ @[to_additive] theorem left_dom_of_mul_dom [Mul α] {a b : Part α} (hab : Dom (a * b)) : a.Dom := hab.1 @[to_additive] theorem right_dom_of_mul_dom [Mul α] {a b : Part α} (hab : Dom (a * b)) : b.Dom := hab.2 @[to_additive (attr := simp)] theorem mul_get_eq [Mul α] (a b : Part α) (hab : Dom (a * b)) : (a * b).get hab = a.get (left_dom_of_mul_dom hab) * b.get (right_dom_of_mul_dom hab) := rfl @[to_additive] theorem some_mul_some [Mul α] (a b : α) : some a * some b = some (a * b) := by simp [mul_def] @[to_additive] theorem inv_mem_inv [Inv α] (a : Part α) (ma : α) (ha : ma ∈ a) : ma⁻¹ ∈ a⁻¹ := by simp [inv_def]; aesop @[to_additive] theorem inv_some [Inv α] (a : α) : (some a)⁻¹ = some a⁻¹ := rfl @[to_additive] theorem div_mem_div [Div α] (a b : Part α) (ma mb : α) (ha : ma ∈ a) (hb : mb ∈ b) : ma / mb ∈ a / b := by simp [div_def]; aesop @[to_additive] theorem left_dom_of_div_dom [Div α] {a b : Part α} (hab : Dom (a / b)) : a.Dom := hab.1 @[to_additive] theorem right_dom_of_div_dom [Div α] {a b : Part α} (hab : Dom (a / b)) : b.Dom := hab.2 @[to_additive (attr := simp)] theorem div_get_eq [Div α] (a b : Part α) (hab : Dom (a / b)) : (a / b).get hab = a.get (left_dom_of_div_dom hab) / b.get (right_dom_of_div_dom hab) := by simp [div_def]; aesop @[to_additive] theorem some_div_some [Div α] (a b : α) : some a / some b = some (a / b) := by simp [div_def] theorem mod_mem_mod [Mod α] (a b : Part α) (ma mb : α) (ha : ma ∈ a) (hb : mb ∈ b) : ma % mb ∈ a % b := by simp [mod_def]; aesop theorem left_dom_of_mod_dom [Mod α] {a b : Part α} (hab : Dom (a % b)) : a.Dom := hab.1 theorem right_dom_of_mod_dom [Mod α] {a b : Part α} (hab : Dom (a % b)) : b.Dom := hab.2 @[simp] theorem mod_get_eq [Mod α] (a b : Part α) (hab : Dom (a % b)) : (a % b).get hab = a.get (left_dom_of_mod_dom hab) % b.get (right_dom_of_mod_dom hab) := by simp [mod_def]; aesop theorem some_mod_some [Mod α] (a b : α) : some a % some b = some (a % b) := by simp [mod_def] theorem append_mem_append [Append α] (a b : Part α) (ma mb : α) (ha : ma ∈ a) (hb : mb ∈ b) : ma ++ mb ∈ a ++ b := by simp [append_def]; aesop theorem left_dom_of_append_dom [Append α] {a b : Part α} (hab : Dom (a ++ b)) : a.Dom := hab.1 theorem right_dom_of_append_dom [Append α] {a b : Part α} (hab : Dom (a ++ b)) : b.Dom := hab.2 @[simp] theorem append_get_eq [Append α] (a b : Part α) (hab : Dom (a ++ b)) : (a ++ b).get hab = a.get (left_dom_of_append_dom hab) ++ b.get (right_dom_of_append_dom hab) := by simp [append_def]; aesop theorem some_append_some [Append α] (a b : α) : some a ++ some b = some (a ++ b) := by simp [append_def] theorem inter_mem_inter [Inter α] (a b : Part α) (ma mb : α) (ha : ma ∈ a) (hb : mb ∈ b) : ma ∩ mb ∈ a ∩ b := by simp [inter_def]; aesop theorem left_dom_of_inter_dom [Inter α] {a b : Part α} (hab : Dom (a ∩ b)) : a.Dom := hab.1 theorem right_dom_of_inter_dom [Inter α] {a b : Part α} (hab : Dom (a ∩ b)) : b.Dom := hab.2 @[simp] theorem inter_get_eq [Inter α] (a b : Part α) (hab : Dom (a ∩ b)) : (a ∩ b).get hab = a.get (left_dom_of_inter_dom hab) ∩ b.get (right_dom_of_inter_dom hab) := by simp [inter_def]; aesop theorem some_inter_some [Inter α] (a b : α) : some a ∩ some b = some (a ∩ b) := by simp [inter_def] theorem union_mem_union [Union α] (a b : Part α) (ma mb : α) (ha : ma ∈ a) (hb : mb ∈ b) : ma ∪ mb ∈ a ∪ b := by simp [union_def]; aesop theorem left_dom_of_union_dom [Union α] {a b : Part α} (hab : Dom (a ∪ b)) : a.Dom := hab.1 theorem right_dom_of_union_dom [Union α] {a b : Part α} (hab : Dom (a ∪ b)) : b.Dom := hab.2 @[simp] theorem union_get_eq [Union α] (a b : Part α) (hab : Dom (a ∪ b)) : (a ∪ b).get hab = a.get (left_dom_of_union_dom hab) ∪ b.get (right_dom_of_union_dom hab) := by simp [union_def]; aesop theorem some_union_some [Union α] (a b : α) : some a ∪ some b = some (a ∪ b) := by simp [union_def] theorem sdiff_mem_sdiff [SDiff α] (a b : Part α) (ma mb : α) (ha : ma ∈ a) (hb : mb ∈ b) : ma \ mb ∈ a \ b := by simp [sdiff_def]; aesop theorem left_dom_of_sdiff_dom [SDiff α] {a b : Part α} (hab : Dom (a \ b)) : a.Dom := hab.1 theorem right_dom_of_sdiff_dom [SDiff α] {a b : Part α} (hab : Dom (a \ b)) : b.Dom := hab.2 @[simp] theorem sdiff_get_eq [SDiff α] (a b : Part α) (hab : Dom (a \ b)) : (a \ b).get hab = a.get (left_dom_of_sdiff_dom hab) \ b.get (right_dom_of_sdiff_dom hab) := by simp [sdiff_def]; aesop theorem some_sdiff_some [SDiff α] (a b : α) : some a \ some b = some (a \ b) := by simp [sdiff_def] end Instances end Part
Mathlib/Data/Part.lean
840
841
/- Copyright (c) 2023 Andrew Yang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Andrew Yang -/ import Mathlib.RingTheory.DedekindDomain.Ideal import Mathlib.RingTheory.Discriminant import Mathlib.RingTheory.DedekindDomain.IntegralClosure import Mathlib.NumberTheory.KummerDedekind import Mathlib.RingTheory.IntegralClosure.IntegralRestrict import Mathlib.RingTheory.Trace.Quotient /-! # The different ideal ## Main definition - `Submodule.traceDual`: The dual `L`-sub `B`-module under the trace form. - `FractionalIdeal.dual`: The dual fractional ideal under the trace form. - `differentIdeal`: The different ideal of an extension of integral domains. ## Main results - `conductor_mul_differentIdeal`: If `L = K[x]`, with `x` integral over `A`, then `𝔣 * 𝔇 = (f'(x))` with `f` being the minimal polynomial of `x`. - `aeval_derivative_mem_differentIdeal`: If `L = K[x]`, with `x` integral over `A`, then `f'(x) ∈ 𝔇` with `f` being the minimal polynomial of `x`. ## TODO - Show properties of the different ideal -/ universe u attribute [local instance] FractionRing.liftAlgebra FractionRing.isScalarTower_liftAlgebra variable (A K : Type*) {L : Type u} {B} [CommRing A] [Field K] [CommRing B] [Field L] variable [Algebra A K] [Algebra B L] [Algebra A B] [Algebra K L] [Algebra A L] variable [IsScalarTower A K L] [IsScalarTower A B L] open nonZeroDivisors IsLocalization Matrix Algebra section BIsDomain /-- Under the AKLB setting, `Iᵛ := traceDual A K (I : Submodule B L)` is the `Submodule B L` such that `x ∈ Iᵛ ↔ ∀ y ∈ I, Tr(x, y) ∈ A` -/ noncomputable def Submodule.traceDual (I : Submodule B L) : Submodule B L where __ := (traceForm K L).dualSubmodule (I.restrictScalars A) smul_mem' c x hx a ha := by rw [traceForm_apply, smul_mul_assoc, mul_comm, ← smul_mul_assoc, mul_comm] exact hx _ (Submodule.smul_mem _ c ha) variable {A K} local notation:max I:max "ᵛ" => Submodule.traceDual A K I namespace Submodule lemma mem_traceDual {I : Submodule B L} {x} : x ∈ Iᵛ ↔ ∀ a ∈ I, traceForm K L x a ∈ (algebraMap A K).range := forall₂_congr fun _ _ ↦ mem_one lemma le_traceDual_iff_map_le_one {I J : Submodule B L} : I ≤ Jᵛ ↔ ((I * J : Submodule B L).restrictScalars A).map ((trace K L).restrictScalars A) ≤ 1 := by rw [Submodule.map_le_iff_le_comap, Submodule.restrictScalars_mul, Submodule.mul_le] simp [SetLike.le_def, mem_traceDual] lemma le_traceDual_mul_iff {I J J' : Submodule B L} : I ≤ (J * J')ᵛ ↔ I * J ≤ J'ᵛ := by simp_rw [le_traceDual_iff_map_le_one, mul_assoc] lemma le_traceDual {I J : Submodule B L} : I ≤ Jᵛ ↔ I * J ≤ 1ᵛ := by rw [← le_traceDual_mul_iff, mul_one] lemma le_traceDual_comm {I J : Submodule B L} : I ≤ Jᵛ ↔ J ≤ Iᵛ := by rw [le_traceDual, mul_comm, ← le_traceDual] lemma le_traceDual_traceDual {I : Submodule B L} : I ≤ Iᵛᵛ := le_traceDual_comm.mpr le_rfl @[simp] lemma traceDual_bot : (⊥ : Submodule B L)ᵛ = ⊤ := by ext; simpa [mem_traceDual, -RingHom.mem_range] using zero_mem _ open scoped Classical in lemma traceDual_top' : (⊤ : Submodule B L)ᵛ = if ((LinearMap.range (Algebra.trace K L)).restrictScalars A ≤ 1) then ⊤ else ⊥ := by classical split_ifs with h · rw [_root_.eq_top_iff] exact fun _ _ _ _ ↦ h ⟨_, rfl⟩ · simp only [SetLike.le_def, restrictScalars_mem, LinearMap.mem_range, mem_one, forall_exists_index, forall_apply_eq_imp_iff, not_forall, not_exists] at h obtain ⟨b, hb⟩ := h simp_rw [eq_bot_iff, SetLike.le_def, mem_bot, mem_traceDual, mem_top, true_implies, traceForm_apply, RingHom.mem_range] contrapose! hb with hx' obtain ⟨c, hc, hc0⟩ := hx' simpa [hc0] using hc (c⁻¹ * b) variable [IsDomain A] [IsFractionRing A K] [FiniteDimensional K L] [Algebra.IsSeparable K L] lemma traceDual_top [Decidable (IsField A)] : (⊤ : Submodule B L)ᵛ = if IsField A then ⊤ else ⊥ := by convert traceDual_top' rw [← IsFractionRing.surjective_iff_isField (R := A) (K := K), LinearMap.range_eq_top.mpr (Algebra.trace_surjective K L), ← RingHom.range_eq_top, _root_.eq_top_iff] simp [SetLike.le_def] end Submodule open Submodule variable [IsFractionRing A K] variable (A K) in lemma map_equiv_traceDual [IsDomain A] [IsFractionRing B L] [IsDomain B] [FaithfulSMul A B] (I : Submodule B (FractionRing B)) : (traceDual A (FractionRing A) I).map (FractionRing.algEquiv B L) = traceDual A K (I.map (FractionRing.algEquiv B L)) := by show Submodule.map (FractionRing.algEquiv B L).toLinearEquiv.toLinearMap _ = traceDual A K (I.map (FractionRing.algEquiv B L).toLinearEquiv.toLinearMap) rw [Submodule.map_equiv_eq_comap_symm, Submodule.map_equiv_eq_comap_symm] ext x simp only [AlgEquiv.toLinearEquiv_symm, AlgEquiv.toLinearEquiv_toLinearMap, traceDual, traceForm_apply, Submodule.mem_comap, AlgEquiv.toLinearMap_apply, Submodule.mem_mk, AddSubmonoid.mem_mk, AddSubsemigroup.mem_mk, Set.mem_setOf_eq] apply (FractionRing.algEquiv B L).forall_congr simp only [restrictScalars_mem, traceForm_apply, AlgEquiv.toEquiv_eq_coe, EquivLike.coe_coe, mem_comap, AlgEquiv.toLinearMap_apply, AlgEquiv.symm_apply_apply] refine fun {y} ↦ (forall_congr' fun hy ↦ ?_) rw [Algebra.trace_eq_of_equiv_equiv (FractionRing.algEquiv A K).toRingEquiv (FractionRing.algEquiv B L).toRingEquiv] swap · apply IsLocalization.ringHom_ext (M := A⁰); ext simp only [AlgEquiv.toRingEquiv_eq_coe, AlgEquiv.toRingEquiv_toRingHom, RingHom.coe_comp, RingHom.coe_coe, Function.comp_apply, AlgEquiv.commutes, ← IsScalarTower.algebraMap_apply] rw [IsScalarTower.algebraMap_apply A B (FractionRing B), AlgEquiv.commutes, ← IsScalarTower.algebraMap_apply] simp only [AlgEquiv.toRingEquiv_eq_coe, map_mul, AlgEquiv.coe_ringEquiv, AlgEquiv.apply_symm_apply, ← AlgEquiv.symm_toRingEquiv, mem_one, AlgEquiv.algebraMap_eq_apply] variable [IsIntegrallyClosed A] lemma Submodule.mem_traceDual_iff_isIntegral {I : Submodule B L} {x} : x ∈ Iᵛ ↔ ∀ a ∈ I, IsIntegral A (traceForm K L x a) := forall₂_congr fun _ _ ↦ mem_one.trans IsIntegrallyClosed.isIntegral_iff.symm variable [FiniteDimensional K L] [IsIntegralClosure B A L] lemma Submodule.one_le_traceDual_one : (1 : Submodule B L) ≤ 1ᵛ := by rw [le_traceDual_iff_map_le_one, mul_one, one_eq_range] rintro _ ⟨x, ⟨x, rfl⟩, rfl⟩ rw [mem_one] apply IsIntegrallyClosed.isIntegral_iff.mp apply isIntegral_trace rw [IsIntegralClosure.isIntegral_iff (A := B)] exact ⟨_, rfl⟩ variable [Algebra.IsSeparable K L] /-- If `b` is an `A`-integral basis of `L` with discriminant `b`, then `d • a * x` is integral over `A` for all `a ∈ I` and `x ∈ Iᵛ`. -/ lemma isIntegral_discr_mul_of_mem_traceDual (I : Submodule B L) {ι} [DecidableEq ι] [Fintype ι] {b : Basis ι K L} (hb : ∀ i, IsIntegral A (b i)) {a x : L} (ha : a ∈ I) (hx : x ∈ Iᵛ) : IsIntegral A ((discr K b) • a * x) := by have hinv : IsUnit (traceMatrix K b).det := by simpa [← discr_def] using discr_isUnit_of_basis _ b have H := mulVec_cramer (traceMatrix K b) fun i => trace K L (x * a * b i) have : Function.Injective (traceMatrix K b).mulVec := by rwa [mulVec_injective_iff_isUnit, isUnit_iff_isUnit_det] rw [← traceMatrix_of_basis_mulVec, ← mulVec_smul, this.eq_iff, traceMatrix_of_basis_mulVec] at H rw [← b.equivFun.symm_apply_apply (_ * _), b.equivFun_symm_apply] apply IsIntegral.sum intro i _ rw [smul_mul_assoc, b.equivFun.map_smul, discr_def, mul_comm, ← H, Algebra.smul_def] refine RingHom.IsIntegralElem.mul _ ?_ (hb _) apply IsIntegral.algebraMap rw [cramer_apply] apply IsIntegral.det intros j k rw [updateCol_apply] split · rw [mul_assoc] rw [mem_traceDual_iff_isIntegral] at hx apply hx have ⟨y, hy⟩ := (IsIntegralClosure.isIntegral_iff (A := B)).mp (hb j) rw [mul_comm, ← hy, ← Algebra.smul_def] exact I.smul_mem _ (ha) · exact isIntegral_trace (RingHom.IsIntegralElem.mul _ (hb j) (hb k)) variable (A K) variable [IsDomain A] [IsFractionRing B L] [Nontrivial B] [NoZeroDivisors B] namespace FractionalIdeal open scoped Classical in /-- The dual of a non-zero fractional ideal is the dual of the submodule under the traceform. -/ noncomputable def dual (I : FractionalIdeal B⁰ L) : FractionalIdeal B⁰ L := if hI : I = 0 then 0 else ⟨Iᵛ, by classical have ⟨s, b, hb⟩ := FiniteDimensional.exists_is_basis_integral A K L obtain ⟨x, hx, hx'⟩ := exists_ne_zero_mem_isInteger hI have ⟨y, hy⟩ := (IsIntegralClosure.isIntegral_iff (A := B)).mp (IsIntegral.algebraMap (B := L) (discr_isIntegral K hb)) refine ⟨y * x, mem_nonZeroDivisors_iff_ne_zero.mpr (mul_ne_zero ?_ hx), fun z hz ↦ ?_⟩
· rw [← (IsIntegralClosure.algebraMap_injective B A L).ne_iff, hy, RingHom.map_zero, ← (algebraMap K L).map_zero, (algebraMap K L).injective.ne_iff] exact discr_not_zero_of_basis K b · convert isIntegral_discr_mul_of_mem_traceDual I hb hx' hz using 1 · ext w; exact (IsIntegralClosure.isIntegral_iff (A := B)).symm
Mathlib/RingTheory/DedekindDomain/Different.lean
220
224
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Chris Hughes, Anne Baanen -/ import Mathlib.Data.Matrix.Basic import Mathlib.Data.Matrix.Block import Mathlib.Data.Matrix.Notation import Mathlib.Data.Matrix.RowCol import Mathlib.GroupTheory.GroupAction.Ring import Mathlib.GroupTheory.Perm.Fin import Mathlib.LinearAlgebra.Alternating.Basic import Mathlib.LinearAlgebra.Matrix.SemiringInverse /-! # Determinant of a matrix This file defines the determinant of a matrix, `Matrix.det`, and its essential properties. ## Main definitions - `Matrix.det`: the determinant of a square matrix, as a sum over permutations - `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix ## Main results - `det_mul`: the determinant of `A * B` is the product of determinants - `det_zero_of_row_eq`: the determinant is zero if there is a repeated row - `det_block_diagonal`: the determinant of a block diagonal matrix is a product of the blocks' determinants ## Implementation notes It is possible to configure `simp` to compute determinants. See the file `MathlibTest/matrix.lean` for some examples. -/ universe u v w z open Equiv Equiv.Perm Finset Function namespace Matrix variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m] variable {R : Type v} [CommRing R] local notation "ε " σ:arg => ((sign σ : ℤ) : R) /-- `det` is an `AlternatingMap` in the rows of the matrix. -/ def detRowAlternating : (n → R) [⋀^n]→ₗ[R] R := MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj) /-- The determinant of a matrix given by the Leibniz formula. -/ abbrev det (M : Matrix n n R) : R := detRowAlternating M theorem det_apply (M : Matrix n n R) : M.det = ∑ σ : Perm n, Equiv.Perm.sign σ • ∏ i, M (σ i) i := MultilinearMap.alternatization_apply _ M -- This is what the old definition was. We use it to avoid having to change the old proofs below theorem det_apply' (M : Matrix n n R) : M.det = ∑ σ : Perm n, ε σ * ∏ i, M (σ i) i := by simp [det_apply, Units.smul_def] theorem det_eq_detp_sub_detp (M : Matrix n n R) : M.det = M.detp 1 - M.detp (-1) := by rw [det_apply, ← Equiv.sum_comp (Equiv.inv (Perm n)), ← ofSign_disjUnion, sum_disjUnion] simp_rw [inv_apply, sign_inv, sub_eq_add_neg, detp, ← sum_neg_distrib] refine congr_arg₂ (· + ·) (sum_congr rfl fun σ hσ ↦ ?_) (sum_congr rfl fun σ hσ ↦ ?_) <;> rw [mem_ofSign.mp hσ, ← Equiv.prod_comp σ] <;> simp @[simp] theorem det_diagonal {d : n → R} : det (diagonal d) = ∏ i, d i := by rw [det_apply'] refine (Finset.sum_eq_single 1 ?_ ?_).trans ?_ · rintro σ - h2 obtain ⟨x, h3⟩ := not_forall.1 (mt Equiv.ext h2) convert mul_zero (ε σ) apply Finset.prod_eq_zero (mem_univ x) exact if_neg h3 · simp · simp theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 := (detRowAlternating : (n → R) [⋀^n]→ₗ[R] R).map_zero @[simp] theorem det_one : det (1 : Matrix n n R) = 1 := by rw [← diagonal_one]; simp [-diagonal_one] theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply] @[simp] theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R → R) = Function.const _ 1 := by ext exact det_isEmpty theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 := haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h det_isEmpty /-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element. Although `Unique` implies `DecidableEq` and `Fintype`, the instances might not be syntactically equal. Thus, we need to fill in the args explicitly. -/ @[simp] theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) : det A = A default default := by simp [det_apply, univ_unique] theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) : det A = A k k := by have := uniqueOfSubsingleton k convert det_unique A theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) : det A = A k k := haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le det_eq_elem_of_subsingleton _ _ theorem det_mul_aux {M N : Matrix n n R} {p : n → n} (H : ¬Bijective p) : (∑ σ : Perm n, ε σ * ∏ x, M (σ x) (p x) * N (p x) x) = 0 := by obtain ⟨i, j, hpij, hij⟩ : ∃ i j, p i = p j ∧ i ≠ j := by rw [← Finite.injective_iff_bijective, Injective] at H push_neg at H exact H exact sum_involution (fun σ _ => σ * Equiv.swap i j) (fun σ _ => by have : (∏ x, M (σ x) (p x)) = ∏ x, M ((σ * Equiv.swap i j) x) (p x) := Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij]) simp [this, sign_swap hij, -sign_swap', prod_mul_distrib]) (fun σ _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun σ _ => mul_swap_involutive i j σ @[simp] theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N := calc det (M * N) = ∑ p : n → n, ∑ σ : Perm n, ε σ * ∏ i, M (σ i) (p i) * N (p i) i := by simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ] rw [Finset.sum_comm] _ = ∑ p : n → n with Bijective p, ∑ σ : Perm n, ε σ * ∏ i, M (σ i) (p i) * N (p i) i := by refine (sum_subset (filter_subset _ _) fun f _ hbij ↦ det_mul_aux ?_).symm simpa only [true_and, mem_filter, mem_univ] using hbij _ = ∑ τ : Perm n, ∑ σ : Perm n, ε σ * ∏ i, M (σ i) (τ i) * N (τ i) i := sum_bij (fun p h ↦ Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ ↦ mem_univ _) (fun _ _ _ _ h ↦ by injection h) (fun b _ ↦ ⟨b, mem_filter.2 ⟨mem_univ _, b.bijective⟩, coe_fn_injective rfl⟩) fun _ _ ↦ rfl _ = ∑ σ : Perm n, ∑ τ : Perm n, (∏ i, N (σ i) i) * ε τ * ∏ j, M (τ j) (σ j) := by simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc] _ = ∑ σ : Perm n, ∑ τ : Perm n, (∏ i, N (σ i) i) * (ε σ * ε τ) * ∏ i, M (τ i) i := (sum_congr rfl fun σ _ => Fintype.sum_equiv (Equiv.mulRight σ⁻¹) _ _ fun τ => by have : (∏ j, M (τ j) (σ j)) = ∏ j, M ((τ * σ⁻¹) j) j := by rw [← (σ⁻¹ : _ ≃ _).prod_comp] simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply] have h : ε σ * ε (τ * σ⁻¹) = ε τ := calc ε σ * ε (τ * σ⁻¹) = ε (τ * σ⁻¹ * σ) := by rw [mul_comm, sign_mul (τ * σ⁻¹)] simp only [Int.cast_mul, Units.val_mul] _ = ε τ := by simp only [inv_mul_cancel_right] simp_rw [Equiv.coe_mulRight, h] simp only [this]) _ = det M * det N := by simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc] /-- The determinant of a matrix, as a monoid homomorphism. -/ def detMonoidHom : Matrix n n R →* R where toFun := det map_one' := det_one map_mul' := det_mul @[simp] theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R → R) = det := rfl /-- On square matrices, `mul_comm` applies under `det`. -/ theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by rw [det_mul, det_mul, mul_comm] /-- On square matrices, `mul_left_comm` applies under `det`. -/ theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by rw [← Matrix.mul_assoc, ← Matrix.mul_assoc, det_mul, det_mul_comm M N, ← det_mul] /-- On square matrices, `mul_right_comm` applies under `det`. -/ theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, ← det_mul] -- TODO(https://github.com/leanprover-community/mathlib4/issues/6607): fix elaboration so `val` isn't needed theorem det_units_conj (M : (Matrix m m R)ˣ) (N : Matrix m m R) : det (M.val * N * M⁻¹.val) = det N := by rw [det_mul_right_comm, Units.mul_inv, one_mul] -- TODO(https://github.com/leanprover-community/mathlib4/issues/6607): fix elaboration so `val` isn't needed theorem det_units_conj' (M : (Matrix m m R)ˣ) (N : Matrix m m R) : det (M⁻¹.val * N * ↑M.val) = det N := det_units_conj M⁻¹ N /-- Transposing a matrix preserves the determinant. -/ @[simp] theorem det_transpose (M : Matrix n n R) : Mᵀ.det = M.det := by rw [det_apply', det_apply'] refine Fintype.sum_bijective _ inv_involutive.bijective _ _ ?_ intro σ rw [sign_inv] congr 1 apply Fintype.prod_equiv σ simp /-- Permuting the columns changes the sign of the determinant. -/ theorem det_permute (σ : Perm n) (M : Matrix n n R) : (M.submatrix σ id).det = Perm.sign σ * M.det := ((detRowAlternating : (n → R) [⋀^n]→ₗ[R] R).map_perm M σ).trans (by simp [Units.smul_def]) /-- Permuting the rows changes the sign of the determinant. -/ theorem det_permute' (σ : Perm n) (M : Matrix n n R) : (M.submatrix id σ).det = Perm.sign σ * M.det := by rw [← det_transpose, transpose_submatrix, det_permute, det_transpose] /-- Permuting rows and columns with the same equivalence does not change the determinant. -/ @[simp] theorem det_submatrix_equiv_self (e : n ≃ m) (A : Matrix m m R) : det (A.submatrix e e) = det A := by rw [det_apply', det_apply'] apply Fintype.sum_equiv (Equiv.permCongr e) intro σ rw [Equiv.Perm.sign_permCongr e σ] congr 1 apply Fintype.prod_equiv e intro i rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply] /-- Permuting rows and columns with two equivalences does not change the absolute value of the determinant. -/ @[simp] theorem abs_det_submatrix_equiv_equiv {R : Type*} [CommRing R] [LinearOrder R] [IsStrictOrderedRing R] (e₁ e₂ : n ≃ m) (A : Matrix m m R) : |(A.submatrix e₁ e₂).det| = |A.det| := by have hee : e₂ = e₁.trans (e₁.symm.trans e₂) := by ext; simp rw [hee] show |((A.submatrix id (e₁.symm.trans e₂)).submatrix e₁ e₁).det| = |A.det| rw [Matrix.det_submatrix_equiv_self, Matrix.det_permute', abs_mul, abs_unit_intCast, one_mul] /-- Reindexing both indices along the same equivalence preserves the determinant. For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because `Matrix.reindex_apply` unfolds `reindex` first. -/ theorem det_reindex_self (e : m ≃ n) (A : Matrix m m R) : det (reindex e e A) = det A := det_submatrix_equiv_self e.symm A /-- Reindexing both indices along equivalences preserves the absolute of the determinant. For the `simp` version of this lemma, see `abs_det_submatrix_equiv_equiv`; this one is unsuitable because `Matrix.reindex_apply` unfolds `reindex` first. -/ theorem abs_det_reindex {R : Type*} [CommRing R] [LinearOrder R] [IsStrictOrderedRing R] (e₁ e₂ : m ≃ n) (A : Matrix m m R) : |det (reindex e₁ e₂ A)| = |det A| := abs_det_submatrix_equiv_equiv e₁.symm e₂.symm A theorem det_smul (A : Matrix n n R) (c : R) : det (c • A) = c ^ Fintype.card n * det A := calc det (c • A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul] _ = det (diagonal fun _ => c) * det A := det_mul _ _ _ = c ^ Fintype.card n * det A := by simp @[simp] theorem det_smul_of_tower {α} [Monoid α] [MulAction α R] [IsScalarTower α R R] [SMulCommClass α R R] (c : α) (A : Matrix n n R) : det (c • A) = c ^ Fintype.card n • det A := by rw [← smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul] theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by rw [← det_smul, neg_one_smul] /-- A variant of `Matrix.det_neg` with scalar multiplication by `Units ℤ` instead of multiplication by `R`. -/ theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units ℤ) ^ Fintype.card n • det A := by rw [← det_smul_of_tower, Units.neg_smul, one_smul] /-- Multiplying each row by a fixed `v i` multiplies the determinant by the product of the `v`s. -/ theorem det_mul_row (v : n → R) (A : Matrix n n R) : det (of fun i j => v j * A i j) = (∏ i, v i) * det A := calc det (of fun i j => v j * A i j) = det (A * diagonal v) := congr_arg det <| by ext simp [mul_comm] _ = (∏ i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm] /-- Multiplying each column by a fixed `v j` multiplies the determinant by the product of the `v`s. -/ theorem det_mul_column (v : n → R) (A : Matrix n n R) : det (of fun i j => v i * A i j) = (∏ i, v i) * det A := MultilinearMap.map_smul_univ _ v A @[simp] theorem det_pow (M : Matrix m m R) (n : ℕ) : det (M ^ n) = det M ^ n := (detMonoidHom : Matrix m m R →* R).map_pow M n section HomMap variable {S : Type w} [CommRing S] theorem _root_.RingHom.map_det (f : R →+* S) (M : Matrix n n R) : f M.det = Matrix.det (f.mapMatrix M) := by simp [Matrix.det_apply', map_sum f, map_prod f] theorem _root_.RingEquiv.map_det (f : R ≃+* S) (M : Matrix n n R) : f M.det = Matrix.det (f.mapMatrix M) := f.toRingHom.map_det _ theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S →ₐ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) := f.toRingHom.map_det _ theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ≃ₐ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) := f.toAlgHom.map_det _ @[norm_cast] theorem _root_.Int.cast_det (M : Matrix n n ℤ) : (M.det : R) = (M.map fun x ↦ (x : R)).det := Int.castRingHom R |>.map_det M @[norm_cast] theorem _root_.Rat.cast_det {F : Type*} [Field F] [CharZero F] (M : Matrix n n ℚ) : (M.det : F) = (M.map fun x ↦ (x : F)).det := Rat.castHom F |>.map_det M end HomMap @[simp] theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mᴴ = star (det M) := ((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose section DetZero /-! ### `det_zero` section Prove that a matrix with a repeated column has determinant equal to zero. -/ theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : ∀ j, A i j = 0) : det A = 0 := (detRowAlternating : (n → R) [⋀^n]→ₗ[R] R).map_coord_zero i (funext h) theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : ∀ i, A i j = 0) : det A = 0 := by rw [← det_transpose] exact det_eq_zero_of_row_eq_zero j h variable {M : Matrix n n R} {i j : n} /-- If a matrix has a repeated row, the determinant will be zero. -/ theorem det_zero_of_row_eq (i_ne_j : i ≠ j) (hij : M i = M j) : M.det = 0 := (detRowAlternating : (n → R) [⋀^n]→ₗ[R] R).map_eq_zero_of_eq M hij i_ne_j /-- If a matrix has a repeated column, the determinant will be zero. -/ theorem det_zero_of_column_eq (i_ne_j : i ≠ j) (hij : ∀ k, M k i = M k j) : M.det = 0 := by rw [← det_transpose, det_zero_of_row_eq i_ne_j] exact funext hij /-- If we repeat a row of a matrix, we get a matrix of determinant zero. -/ theorem det_updateRow_eq_zero (h : i ≠ j) : (M.updateRow j (M i)).det = 0 := det_zero_of_row_eq h (by simp [h]) /-- If we repeat a column of a matrix, we get a matrix of determinant zero. -/ theorem det_updateCol_eq_zero (h : i ≠ j) : (M.updateCol j (fun k ↦ M k i)).det = 0 := det_zero_of_column_eq h (by simp [h]) @[deprecated (since := "2024-12-11")] alias det_updateColumn_eq_zero := det_updateCol_eq_zero end DetZero theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n → R) : det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) := (detRowAlternating : (n → R) [⋀^n]→ₗ[R] R).map_update_add M j u v theorem det_updateCol_add (M : Matrix n n R) (j : n) (u v : n → R) : det (updateCol M j <| u + v) = det (updateCol M j u) + det (updateCol M j v) := by rw [← det_transpose, ← updateRow_transpose, det_updateRow_add] simp [updateRow_transpose, det_transpose] @[deprecated (since := "2024-12-11")] alias det_updateColumn_add := det_updateCol_add theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n → R) : det (updateRow M j <| s • u) = s * det (updateRow M j u) := (detRowAlternating : (n → R) [⋀^n]→ₗ[R] R).map_update_smul M j s u theorem det_updateCol_smul (M : Matrix n n R) (j : n) (s : R) (u : n → R) : det (updateCol M j <| s • u) = s * det (updateCol M j u) := by rw [← det_transpose, ← updateRow_transpose, det_updateRow_smul] simp [updateRow_transpose, det_transpose] @[deprecated (since := "2024-12-11")] alias det_updateColumn_smul := det_updateCol_smul theorem det_updateRow_smul_left (M : Matrix n n R) (j : n) (s : R) (u : n → R) : det (updateRow (s • M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) := MultilinearMap.map_update_smul_left _ M j s u @[deprecated (since := "2024-11-03")] alias det_updateRow_smul' := det_updateRow_smul_left theorem det_updateCol_smul_left (M : Matrix n n R) (j : n) (s : R) (u : n → R) : det (updateCol (s • M) j u) = s ^ (Fintype.card n - 1) * det (updateCol M j u) := by rw [← det_transpose, ← updateRow_transpose, transpose_smul, det_updateRow_smul_left] simp [updateRow_transpose, det_transpose] @[deprecated (since := "2024-12-11")] alias det_updateColumn_smul' := det_updateCol_smul_left @[deprecated (since := "2024-12-11")] alias det_updateColumn_smul_left := det_updateCol_smul_left theorem det_updateRow_sum_aux (M : Matrix n n R) {j : n} (s : Finset n) (hj : j ∉ s) (c : n → R) (a : R) : (M.updateRow j (a • M j + ∑ k ∈ s, (c k) • M k)).det = a • M.det := by induction s using Finset.induction_on with | empty => rw [Finset.sum_empty, add_zero, smul_eq_mul, det_updateRow_smul, updateRow_eq_self] | insert k _ hk h_ind => have h : k ≠ j := fun h ↦ (h ▸ hj) (Finset.mem_insert_self _ _) rw [Finset.sum_insert hk, add_comm ((c k) • M k), ← add_assoc, det_updateRow_add, det_updateRow_smul, det_updateRow_eq_zero h, mul_zero, add_zero, h_ind] exact fun h ↦ hj (Finset.mem_insert_of_mem h) /-- If we replace a row of a matrix by a linear combination of its rows, then the determinant is multiplied by the coefficient of that row. -/ theorem det_updateRow_sum (A : Matrix n n R) (j : n) (c : n → R) : (A.updateRow j (∑ k, (c k) • A k)).det = (c j) • A.det := by convert det_updateRow_sum_aux A (Finset.univ.erase j) (Finset.univ.not_mem_erase j) c (c j) rw [← Finset.univ.add_sum_erase _ (Finset.mem_univ j)] /-- If we replace a column of a matrix by a linear combination of its columns, then the determinant is multiplied by the coefficient of that column. -/ theorem det_updateCol_sum (A : Matrix n n R) (j : n) (c : n → R) : (A.updateCol j (fun k ↦ ∑ i, (c i) • A k i)).det = (c j) • A.det := by rw [← det_transpose, ← updateRow_transpose, ← det_transpose A] convert det_updateRow_sum A.transpose j c simp only [smul_eq_mul, Finset.sum_apply, Pi.smul_apply, transpose_apply] @[deprecated (since := "2024-12-11")] alias det_updateColumn_sum := det_updateCol_sum section DetEq /-! ### `det_eq` section Lemmas showing the determinant is invariant under a variety of operations. -/ theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1) (hA : A = B * C) : det A = det B := calc det A = det (B * C) := congr_arg _ hA _ = det B * det C := det_mul _ _ _ = det B := by rw [hC, mul_one] theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1) (hA : A = C * B) : det A = det B := calc det A = det (C * B) := congr_arg _ hA _ = det C * det B := det_mul _ _ _ = det B := by rw [hC, one_mul] theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i ≠ j) : det (updateRow A i (A i + A j)) = det A := by simp [det_updateRow_add, det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)] theorem det_updateCol_add_self (A : Matrix n n R) {i j : n} (hij : i ≠ j) :
det (updateCol A i fun k => A k i + A k j) = det A := by rw [← det_transpose, ← updateRow_transpose, ← det_transpose A] exact det_updateRow_add_self Aᵀ hij
Mathlib/LinearAlgebra/Matrix/Determinant/Basic.lean
472
475
/- Copyright (c) 2019 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner, Anatole Dedecker, Yury Kudryashov -/ import Mathlib.Analysis.Calculus.Deriv.Basic import Mathlib.Analysis.Calculus.FDeriv.Mul import Mathlib.Analysis.Calculus.FDeriv.Add /-! # Derivative of `f x * g x` In this file we prove formulas for `(f x * g x)'` and `(f x • g x)'`. For a more detailed overview of one-dimensional derivatives in mathlib, see the module docstring of `Analysis/Calculus/Deriv/Basic`. ## Keywords derivative, multiplication -/ universe u v w noncomputable section open scoped Topology Filter ENNReal open Filter Asymptotics Set open ContinuousLinearMap (smulRight smulRight_one_eq_iff) variable {𝕜 : Type u} [NontriviallyNormedField 𝕜] variable {F : Type v} [NormedAddCommGroup F] [NormedSpace 𝕜 F] variable {E : Type w} [NormedAddCommGroup E] [NormedSpace 𝕜 E] variable {G : Type*} [NormedAddCommGroup G] [NormedSpace 𝕜 G] variable {f : 𝕜 → F} variable {f' : F} variable {x : 𝕜} variable {s : Set 𝕜} variable {L : Filter 𝕜} /-! ### Derivative of bilinear maps -/ namespace ContinuousLinearMap variable {B : E →L[𝕜] F →L[𝕜] G} {u : 𝕜 → E} {v : 𝕜 → F} {u' : E} {v' : F} theorem hasDerivWithinAt_of_bilinear (hu : HasDerivWithinAt u u' s x) (hv : HasDerivWithinAt v v' s x) : HasDerivWithinAt (fun x ↦ B (u x) (v x)) (B (u x) v' + B u' (v x)) s x := by
simpa using (B.hasFDerivWithinAt_of_bilinear hu.hasFDerivWithinAt hv.hasFDerivWithinAt).hasDerivWithinAt theorem hasDerivAt_of_bilinear (hu : HasDerivAt u u' x) (hv : HasDerivAt v v' x) : HasDerivAt (fun x ↦ B (u x) (v x)) (B (u x) v' + B u' (v x)) x := by
Mathlib/Analysis/Calculus/Deriv/Mul.lean
52
56